Skip to content

Commit

Permalink
Merge pull request #2 from J0eGoodwin/erlang-24-support
Browse files Browse the repository at this point in the history
Replace hmac with macN for OTP 24 support
  • Loading branch information
dingosky authored Jun 4, 2024
2 parents b2922a0 + ad62e17 commit 752ca11
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/rncryptor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@

-type rn_pw_cryptor() :: [rnheader() | salt64() | salt64() | aes_block() | binary() | hmac_sig()].
-type rn_key_cryptor() :: [rnheader() | aes_block() | binary() | hmac_sig()].
-type rncryptor() :: rn_pw_cryptor() | rn_key_cryptor().
-type rnpacket() :: [hmac_key() | rncryptor()].
-type rncryptor() :: rn_pw_cryptor() | rn_key_cryptor() | binary().
-type rnpacket() :: [hmac_key() | rncryptor()] | binary().

%%======================================================================================
%%
Expand Down Expand Up @@ -138,7 +138,7 @@ encrypt_key(<<Key/binary>>, <<IVec:?AES256_IVEC_SIZE/binary>>,
PaddedText = rncryptor_util:enpad(PlainText),
CipherText = crypto:crypto_one_time(CipherIV, Key, IVec, PaddedText, true),
Message = <<?RN_V3, ?RN_OPT_KEY, IVec/binary, CipherText/binary>>,
RNHmac = crypto:hmac(sha256, HmacKey, Message, ?HMAC_SHA256_SIZE),
RNHmac = crypto:macN(hmac, sha256, HmacKey, Message, ?HMAC_SHA256_SIZE),
<<Message/binary, RNHmac/binary>>;
encrypt_key(_Key, _IVec, _HmacKey, _PlainText) ->
{error, "Invalid arguments"}.
Expand Down Expand Up @@ -270,7 +270,7 @@ encrypt_pw(KdfSalt, KdfKey, IVec, HmacSalt, HmacKey, PlainText) ->
PaddedText = rncryptor_util:enpad(PlainText),
CipherText = crypto:crypto_one_time(CipherIV, KdfKey, IVec, PaddedText, true),
RNData = <<?RN_V3, ?RN_OPT_PW, KdfSalt/binary, HmacSalt/binary, IVec/binary, CipherText/binary>>,
RNHmac = crypto:hmac(sha256, HmacKey, RNData, ?HMAC_SHA256_SIZE),
RNHmac = crypto:macN(hmac, sha256, HmacKey, RNData, ?HMAC_SHA256_SIZE),
<<RNData/binary, RNHmac/binary>>.

%%======================================================================================
Expand Down Expand Up @@ -346,7 +346,7 @@ hmac_challenge(HmacKey, RNCryptor) ->
RNSize = erlang:byte_size(RNCryptor),
RNData = erlang:binary_part(RNCryptor, {0, RNSize-?HMAC_SHA256_SIZE}),
RNHmac = erlang:binary_part(RNCryptor, {RNSize, -?HMAC_SHA256_SIZE}),
Challenge = crypto:hmac(sha256, HmacKey, RNData, ?HMAC_SHA256_SIZE),
Challenge = crypto:macN(hmac, sha256, HmacKey, RNData, ?HMAC_SHA256_SIZE),
case rncryptor_util:const_compare(RNHmac, Challenge) of
true ->
{ok, RNData};
Expand Down
4 changes: 2 additions & 2 deletions src/rncryptor_kdf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pbkdf2(Password, Salt, Rounds, KeySize) ->
%%======================================================================================
%% @private
pbkdf2_key(Password, Salt, Rounds, KeySize, BlockNum, PassKey) ->
InitBlock = crypto:hmac(sha, Password, <<Salt/binary, BlockNum:32/integer>>, ?PBKDF2_SHA1_SIZE),
InitBlock = crypto:macN(hmac, sha, Password, <<Salt/binary, BlockNum:32/integer>>, ?PBKDF2_SHA1_SIZE),
BlockKey = pbkdf2_block_key(Password, Rounds, 2, InitBlock, InitBlock),
NumBlocks = rncryptor_util:ceil(KeySize / ?PBKDF2_SHA1_SIZE),
case BlockNum =:= NumBlocks of
Expand All @@ -97,7 +97,7 @@ pbkdf2_key(Password, Salt, Rounds, KeySize, BlockNum, PassKey) ->
pbkdf2_block_key(_Password, Rounds, Round, _PrevBlock, Block) when Round > Rounds ->
Block;
pbkdf2_block_key(Password, Rounds, Round, PrevBlock, Block) ->
NextBlock = crypto:hmac(sha, Password, PrevBlock, ?PBKDF2_SHA1_SIZE),
NextBlock = crypto:macN(hmac, sha, Password, PrevBlock, ?PBKDF2_SHA1_SIZE),
Block2 = crypto:exor(NextBlock, Block),
pbkdf2_block_key(Password, Rounds, Round + 1, NextBlock, Block2).

Expand Down

0 comments on commit 752ca11

Please sign in to comment.