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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2014-03-13 12:29:21 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2014 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.
|
|
|
|
%%%
|
|
|
|
%%% 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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
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) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
hi(jlib:resourceprep(Password), Salt, IterationCount).
|
|
|
|
|
|
|
|
-spec client_key(binary()) -> binary().
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
client_key(SaltedPassword) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
crypto: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().
|
|
|
|
|
2013-11-05 11:05:12 +01:00
|
|
|
stored_key(ClientKey) -> p1_sha:sha1(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) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
crypto:sha_mac(SaltedPassword, <<"Server Key">>).
|
|
|
|
|
|
|
|
-spec client_signature(binary(), binary()) -> binary().
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
client_signature(StoredKey, AuthMessage) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
crypto:sha_mac(StoredKey, AuthMessage).
|
|
|
|
|
|
|
|
-spec client_key(binary(), binary()) -> binary().
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
client_key(ClientProof, ClientSignature) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
list_to_binary(lists:zipwith(fun (X, Y) -> X bxor Y end,
|
|
|
|
binary_to_list(ClientProof),
|
|
|
|
binary_to_list(ClientSignature))).
|
|
|
|
|
|
|
|
-spec server_signature(binary(), binary()) -> binary().
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
server_signature(ServerKey, AuthMessage) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
crypto:sha_mac(ServerKey, AuthMessage).
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
hi(Password, Salt, IterationCount) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
U1 = crypto:sha_mac(Password, <<Salt/binary, 0, 0, 0, 1>>),
|
|
|
|
list_to_binary(lists:zipwith(fun (X, Y) -> X bxor Y end,
|
|
|
|
binary_to_list(U1),
|
|
|
|
binary_to_list(hi_round(Password, U1,
|
|
|
|
IterationCount - 1)))).
|
2011-08-16 00:25:03 +02:00
|
|
|
|
|
|
|
hi_round(Password, UPrev, 1) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
crypto:sha_mac(Password, UPrev);
|
2011-08-16 00:25:03 +02:00
|
|
|
hi_round(Password, UPrev, IterationCount) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
U = crypto:sha_mac(Password, UPrev),
|
|
|
|
list_to_binary(lists:zipwith(fun (X, Y) -> X bxor Y end,
|
|
|
|
binary_to_list(U),
|
|
|
|
binary_to_list(hi_round(Password, U,
|
|
|
|
IterationCount - 1)))).
|