crypto stream_state() used to be "self-contained" before OTP 22.0

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

crypto stream_state() used to be "self-contained" before OTP 22.0

Zhengji Li
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]).
```

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: crypto stream_state() used to be "self-contained" before OTP 22.0

Hans Nilsson R (AL/EAB)

The "State" could not be re-used in OTP-22.0 or later.
/Hans Nilsson

Från: [hidden email] <[hidden email]> för Zhengji Li <[hidden email]>
Skickat: den 12 oktober 2019 12:15
Till: Lista de Erlang <[hidden email]>
Ä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]).
```

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions