Skip to content

Commit

Permalink
wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Jul 4, 2023
1 parent 0621424 commit a54612b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
7 changes: 5 additions & 2 deletions lib/sec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@ defmodule CA.CRYPTO do
cms = testCMSX509

Check warning on line 52 in lib/sec.ex

View workflow job for this annotation

GitHub Actions / build

variable "testCMSX509" does not exist and is being expanded to "testCMSX509()", please use parentheses to remove the ambiguity or change the variable name
{_,{:ContentInfo,_,{:EnvelopedData,_,_,x,{:EncryptedContentInfo,_,{_,_,{_,iv}},msg},_}}} = cms
[{:kari,{_,:v3,{_,{_,_,publicKey}},_,_,[{_,_,encryptedKey}]}}|y] = x

Check warning on line 54 in lib/sec.ex

View workflow job for this annotation

GitHub Actions / build

variable "y" is unused (if the variable is not meant to be used, prefix it with an underscore)
encryptedKey2 = :binary.part(encryptedKey, 2, 16)
maximS = shared(aliceP,maximK,scheme)
aliceS = shared(maximP,aliceK,scheme)
aliceS == maximS

Check warning on line 58 in lib/sec.ex

View workflow job for this annotation

GitHub Actions / build

use of operator '==' has no effect
derived = kdf(:sha256, aliceS, :erlang.size(aliceS))
# unwrap = :aes_kw.unwrap(derived, encryptedKey)
unwrap = :aes_kw.unwrap(encryptedKey2, derived, iv)

Check warning on line 60 in lib/sec.ex

View workflow job for this annotation

GitHub Actions / build

variable "unwrap" is unused (if the variable is not meant to be used, prefix it with an underscore)
:io.format('~p~n',
[{cms,[ publicKey: publicKey,
[{cms,[ publicKey: aliceP,
senderPublic: publicKey,
encryptedKey: encryptedKey,
kdf: derived,
# unwrapped: unwrap,
encryptedMessage: msg,
iv: iv]}])
# decryptCBC(msg, unwrap, iv)
end
Expand Down
41 changes: 8 additions & 33 deletions src/aes_kw.erl
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
-module(aes_kw).

%% API
-export([wrap/2]).
-export([wrap/3]).
-export([unwrap/2]).
-export([unwrap/3]).
-export([test/0]).


-define(MSB64, 1/unsigned-big-integer-unit:64).
-define(DEFAULT_IV, << 16#A6A6A6A6A6A6A6A6:?MSB64 >>).

%%====================================================================
%% API functions
%%====================================================================

wrap(PlainText, KEK) ->
wrap(PlainText, KEK, ?DEFAULT_IV).

wrap(PlainText, KEK) -> wrap(PlainText, KEK, ?DEFAULT_IV).
wrap(PlainText, KEK, IV)
when (byte_size(PlainText) rem 8) =:= 0
andalso (bit_size(KEK) =:= 128
Expand All @@ -27,9 +18,7 @@ wrap(PlainText, KEK, IV)
BlockCount = (byte_size(Buffer) div 8) - 1,
do_wrap(Buffer, 0, BlockCount, KEK).

unwrap(CipherText, KEK) ->
unwrap(CipherText, KEK, ?DEFAULT_IV).

unwrap(CipherText, KEK) -> unwrap(CipherText, KEK, ?DEFAULT_IV).
unwrap(CipherText, KEK, IV)
when (byte_size(CipherText) rem 8) =:= 0
andalso (bit_size(KEK) =:= 128
Expand All @@ -44,23 +33,14 @@ unwrap(CipherText, KEK, IV)
erlang:error({badarg, [CipherText, KEK, IV]})
end.

%%%-------------------------------------------------------------------
%%% Internal functions
%%%-------------------------------------------------------------------

%% @private
do_wrap(Buffer, 6, _BlockCount, _KEK) ->
Buffer;
do_wrap(Buffer, J, BlockCount, KEK) ->
do_wrap(do_wrap(Buffer, J, 1, BlockCount, KEK), J + 1, BlockCount, KEK).
do_wrap(Buffer, 6, _BlockCount, _KEK) -> Buffer;
do_wrap(Buffer, J, BlockCount, KEK) -> do_wrap(do_wrap(Buffer, J, 1, BlockCount, KEK), J + 1, BlockCount, KEK).

codec(128) -> aes_128_ecb;
codec(192) -> aes_192_ecb;
codec(256) -> aes_256_ecb.

%% @private
do_wrap(Buffer, _J, I, BlockCount, _KEK) when I > BlockCount ->
Buffer;
do_wrap(Buffer, _J, I, BlockCount, _KEK) when I > BlockCount -> Buffer;
do_wrap(<< A0:8/binary, Rest/binary >>, J, I, BlockCount, KEK) ->
HeadSize = (I - 1) * 8,
<< Head:HeadSize/binary, B0:8/binary, Tail/binary >> = Rest,
Expand All @@ -70,15 +50,10 @@ do_wrap(<< A0:8/binary, Rest/binary >>, J, I, BlockCount, KEK) ->
A2 = A1 bxor Round,
do_wrap(<< A2:?MSB64, Head/binary, B1/binary, Tail/binary >>, J, I + 1, BlockCount, KEK).

%% @private
do_unwrap(Buffer, J, _BlockCount, _KEK) when J < 0 ->
Buffer;
do_unwrap(Buffer, J, BlockCount, KEK) ->
do_unwrap(do_unwrap(Buffer, J, BlockCount, BlockCount, KEK), J - 1, BlockCount, KEK).
do_unwrap(Buffer, J, _BlockCount, _KEK) when J < 0 -> Buffer;
do_unwrap(Buffer, J, BlockCount, KEK) -> do_unwrap(do_unwrap(Buffer, J, BlockCount, BlockCount, KEK), J - 1, BlockCount, KEK).

%% @private
do_unwrap(Buffer, _J, I, _BlockCount, _KEK) when I < 1 ->
Buffer;
do_unwrap(Buffer, _J, I, _BlockCount, _KEK) when I < 1 -> Buffer;
do_unwrap(<< A0:?MSB64, Rest/binary >>, J, I, BlockCount, KEK) ->
HeadSize = (I - 1) * 8,
<< Head:HeadSize/binary, B0:8/binary, Tail/binary >> = Rest,
Expand Down

0 comments on commit a54612b

Please sign in to comment.