[erlang-questions] crypto stream_state() used to be "self-contained" before OTP 22.0
Hans Nilsson R
hans.r.nilsson@REDACTED
Wed Oct 16 14:20:44 CEST 2019
The "State" could not be re-used in OTP-22.0 or later.
/Hans Nilsson
________________________________
Från: erlang-questions-bounces@REDACTED <erlang-questions-bounces@REDACTED> för Zhengji Li <foldl@REDACTED>
Skickat: den 12 oktober 2019 12:15
Till: Lista de Erlang <erlang-questions@REDACTED>
Ämne: [erlang-questions] crypto stream_state() used to be "self-contained" before OTP 22.0
Hi all,
Could we infer that "State" is a "self-contained" piece of raw data that we can pass
to different function calls?
stream_encrypt(State, PlainText) -> {NewState, CipherText}
Compare it with:
file:write(IoDevice, Bytes) -> ok | {error, Reason}
I think the answer should be "Yes". And things truly works before OTP 22.0.
Although codes can be fixed for OTP 22 easily, my concern is that, if a function in OTP is declared as
f(State, ...) -> {NewState, ...}
Can we infer that the old State can be used again?
Below is a simple test.
```
t() ->
Key = <<12,137,124,112,128,97,39,1,112,248,35,27,186,168,219,244>>,
IVec = <<78,237,251,180,107,173,97,170,32,214,233,193,147,64,117, 126>>,
Data = <<1,2,3,4>>,
Enc0 = enc(Key, IVec, Data),
Dec0 = dec(Key, IVec, Enc0),
show({Data, Enc0, Dec0}),
Cipher = crypto:stream_init(aes_ctr, Key, IVec),
Enc1 = enc(Cipher, Data),
Dec1 = dec(Cipher, Enc1),
show({Data, Enc1, Dec1}). % FAIL in OTP 22.0
enc(Key, IVec, Data) ->
Cipher = crypto:stream_init(aes_ctr, Key, IVec),
{_, Enc} = crypto:stream_encrypt(Cipher, Data),
Enc.
dec(Key, IVec, Data) ->
Cipher = crypto:stream_init(aes_ctr, Key, IVec),
{_, Dec} = crypto:stream_decrypt(Cipher, Data),
Dec.
enc(Cipher, Data) ->
{_, Enc} = crypto:stream_encrypt(Cipher, Data),
Enc.
dec(Cipher, Data) ->
{_, Dec} = crypto:stream_decrypt(Cipher, Data),
Dec.
show({A, B, A}) -> io:format("GOOD: ~p -> ~p -> ~p~n", [A,B,A]);
show({A, B, C}) -> io:format(" BAD: ~p -> ~p -> ~p~n", [A,B,C]).
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20191016/2364b418/attachment.htm>
More information about the erlang-questions
mailing list