2011-08-16 00:25:40 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : scram.erl
|
|
|
|
%%% Author : Stephen Röttger <stephen.roettger@googlemail.com>
|
|
|
|
%%% Purpose : SCRAM (RFC 5802)
|
|
|
|
%%% Created : 7 Aug 2011 by Stephen Röttger <stephen.roettger@googlemail.com>
|
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2011-08-16 00:25:40 +02:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2011-08-16 00:25:40 +02:00
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2011-08-16 00:25:03 +02:00
|
|
|
-module(scram).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2011-08-16 00:25:03 +02:00
|
|
|
-author('stephen.roettger@googlemail.com').
|
|
|
|
|
|
|
|
%% External exports
|
2011-09-05 12:48:26 +02:00
|
|
|
%% ejabberd doesn't implement SASLPREP, so we use the similar RESOURCEPREP instead
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([salted_password/3, stored_key/1, server_key/1,
|
|
|
|
server_signature/2, client_signature/2, client_key/1,
|
|
|
|
client_key/2]).
|
|
|
|
|
|
|
|
-spec salted_password(binary(), binary(), non_neg_integer()) -> binary().
|
|
|
|
|
2011-08-16 00:25:03 +02:00
|
|
|
salted_password(Password, Salt, IterationCount) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
hi(jid:resourceprep(Password), Salt, IterationCount).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec client_key(binary()) -> binary().
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
client_key(SaltedPassword) ->
|
2015-03-20 11:57:27 +01:00
|
|
|
sha_mac(SaltedPassword, <<"Client Key">>).
|
2011-08-16 00:25:03 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec stored_key(binary()) -> binary().
|
|
|
|
|
2017-02-13 16:35:57 +01:00
|
|
|
stored_key(ClientKey) -> crypto:hash(sha, ClientKey).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec server_key(binary()) -> binary().
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
server_key(SaltedPassword) ->
|
2015-03-20 11:57:27 +01:00
|
|
|
sha_mac(SaltedPassword, <<"Server Key">>).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec client_signature(binary(), binary()) -> binary().
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
client_signature(StoredKey, AuthMessage) ->
|
2015-03-20 11:57:27 +01:00
|
|
|
sha_mac(StoredKey, AuthMessage).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec client_key(binary(), binary()) -> binary().
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
client_key(ClientProof, ClientSignature) ->
|
2017-01-17 20:37:44 +01:00
|
|
|
crypto:exor(ClientProof, ClientSignature).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec server_signature(binary(), binary()) -> binary().
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
server_signature(ServerKey, AuthMessage) ->
|
2015-03-20 11:57:27 +01:00
|
|
|
sha_mac(ServerKey, AuthMessage).
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
hi(Password, Salt, IterationCount) ->
|
2015-03-20 11:57:27 +01:00
|
|
|
U1 = sha_mac(Password, <<Salt/binary, 0, 0, 0, 1>>),
|
2017-01-17 20:37:44 +01:00
|
|
|
crypto:exor(U1, hi_round(Password, U1, IterationCount - 1)).
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
hi_round(Password, UPrev, 1) ->
|
2015-03-20 11:57:27 +01:00
|
|
|
sha_mac(Password, UPrev);
|
2011-08-16 00:25:03 +02:00
|
|
|
hi_round(Password, UPrev, IterationCount) ->
|
2015-03-20 11:57:27 +01:00
|
|
|
U = sha_mac(Password, UPrev),
|
2017-01-17 20:37:44 +01:00
|
|
|
crypto:exor(U, hi_round(Password, U, IterationCount - 1)).
|
2015-03-20 11:57:27 +01:00
|
|
|
|
|
|
|
sha_mac(Key, Data) ->
|
2015-10-07 00:19:42 +02:00
|
|
|
crypto:hmac(sha, Key, Data).
|