[erlang-questions] crypto stream_state() used to be "self-contained" before OTP 22.0

Zhengji Li foldl@REDACTED
Sat Oct 12 12:15:26 CEST 2019


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/20191012/464ecb03/attachment.htm>


More information about the erlang-questions mailing list