Improve type specs
This commit is contained in:
parent
c06bbae07d
commit
7c7ad54a6a
@ -9,3 +9,4 @@
|
|||||||
locals_not_used,
|
locals_not_used,
|
||||||
deprecated_function_calls, deprecated_functions]}.
|
deprecated_function_calls, deprecated_functions]}.
|
||||||
|
|
||||||
|
{dialyzer, [{warnings, [unknown]}]}.
|
||||||
|
@ -64,11 +64,9 @@ binary().
|
|||||||
send_msg := send_msg_fun(),
|
send_msg := send_msg_fun(),
|
||||||
state := term() }.
|
state := term() }.
|
||||||
%% Noise communication state - used to parameterize a handshake. Consists of a
|
%% Noise communication state - used to parameterize a handshake. Consists of a
|
||||||
%% send function one receive function and an internal state.
|
%% send function, one receive function, and an internal state.
|
||||||
|
|
||||||
-type noise_split_state() :: #{ rx := enoise_cipher_state:state(),
|
-type noise_split_state() :: enoise_hs_state:noise_split_state().
|
||||||
tx := enoise_cipher_state:state(),
|
|
||||||
hs_hash := binary() }.
|
|
||||||
%% Return value from the final `split' operation. Provides a CipherState for
|
%% Return value from the final `split' operation. Provides a CipherState for
|
||||||
%% receiving and a CipherState transmission. Also includes the final handshake
|
%% receiving and a CipherState transmission. Also includes the final handshake
|
||||||
%% hash for channel binding.
|
%% hash for channel binding.
|
||||||
@ -140,7 +138,7 @@ connect(TcpSock, Options) ->
|
|||||||
%% @end
|
%% @end
|
||||||
-spec accept(TcpSock :: gen_tcp:socket(),
|
-spec accept(TcpSock :: gen_tcp:socket(),
|
||||||
Options :: noise_options()) ->
|
Options :: noise_options()) ->
|
||||||
{ok, noise_socket()} | {error, term()}.
|
{ok, noise_socket(), enoise_hs_state:state()} | {error, term()}.
|
||||||
accept(TcpSock, Options) ->
|
accept(TcpSock, Options) ->
|
||||||
tcp_handshake(TcpSock, responder, Options).
|
tcp_handshake(TcpSock, responder, Options).
|
||||||
|
|
||||||
|
@ -50,11 +50,16 @@ set_nonce(CState = #noise_cs{}, Nonce) ->
|
|||||||
CState#noise_cs{ n = Nonce }.
|
CState#noise_cs{ n = Nonce }.
|
||||||
|
|
||||||
-spec encrypt_with_ad(CState :: state(), AD :: binary(), PlainText :: binary()) ->
|
-spec encrypt_with_ad(CState :: state(), AD :: binary(), PlainText :: binary()) ->
|
||||||
{ok, state(), binary()}.
|
{ok, state(), binary()} | {error, term()}.
|
||||||
encrypt_with_ad(CState = #noise_cs{ k = empty }, _AD, PlainText) ->
|
encrypt_with_ad(CState = #noise_cs{ k = empty }, _AD, PlainText) ->
|
||||||
{ok, CState, PlainText};
|
{ok, CState, PlainText};
|
||||||
encrypt_with_ad(CState = #noise_cs{ k = K, n = N, cipher = Cipher }, AD, PlainText) ->
|
encrypt_with_ad(CState = #noise_cs{ k = K, n = N, cipher = Cipher }, AD, PlainText) ->
|
||||||
{ok, CState#noise_cs{ n = N+1 }, enoise_crypto:encrypt(Cipher, K, N, AD, PlainText)}.
|
case enoise_crypto:encrypt(Cipher, K, N, AD, PlainText) of
|
||||||
|
Encrypted when is_binary(Encrypted) ->
|
||||||
|
{ok, CState#noise_cs{ n = N+1 }, Encrypted};
|
||||||
|
Err = {error, _} ->
|
||||||
|
Err
|
||||||
|
end.
|
||||||
|
|
||||||
-spec decrypt_with_ad(CState :: state(), AD :: binary(), CipherText :: binary()) ->
|
-spec decrypt_with_ad(CState :: state(), AD :: binary(), CipherText :: binary()) ->
|
||||||
{ok, state(), binary()} | {error, term()}.
|
{ok, state(), binary()} | {error, term()}.
|
||||||
|
@ -53,15 +53,19 @@ start_link(TcpSock, Rx, Tx, Owner, {Active0, Buf}) ->
|
|||||||
Err
|
Err
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
-spec send(Noise :: pid(), Data :: binary()) -> ok | {error, term()}.
|
||||||
send(Noise, Data) ->
|
send(Noise, Data) ->
|
||||||
gen_server:call(Noise, {send, Data}).
|
gen_server:call(Noise, {send, Data}).
|
||||||
|
|
||||||
|
-spec set_active(Noise :: pid(), Active :: boolean()) -> ok | {error, term()}.
|
||||||
set_active(Noise, Active) ->
|
set_active(Noise, Active) ->
|
||||||
gen_server:call(Noise, {active, self(), Active}).
|
gen_server:call(Noise, {active, self(), Active}).
|
||||||
|
|
||||||
|
-spec close(Noise :: pid()) -> ok | {error, term()}.
|
||||||
close(Noise) ->
|
close(Noise) ->
|
||||||
gen_server:call(Noise, close).
|
gen_server:call(Noise, close).
|
||||||
|
|
||||||
|
-spec controlling_process(Noise :: pid(), NewPid :: pid()) -> ok | {error, term()}.
|
||||||
controlling_process(Noise, NewPid) ->
|
controlling_process(Noise, NewPid) ->
|
||||||
gen_server:call(Noise, {controlling_process, self(), NewPid}, 100).
|
gen_server:call(Noise, {controlling_process, self(), NewPid}, 100).
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ hkdf(Hash, Key, Data) ->
|
|||||||
[Output1, Output2, Output3].
|
[Output1, Output2, Output3].
|
||||||
|
|
||||||
-spec rekey(Cipher :: enoise_cipher_state:noise_cipher(),
|
-spec rekey(Cipher :: enoise_cipher_state:noise_cipher(),
|
||||||
Key :: binary()) -> binary().
|
Key :: binary()) -> binary() | {error, term()}.
|
||||||
rekey(Cipher, K) ->
|
rekey(Cipher, K) ->
|
||||||
encrypt(Cipher, K, ?MAX_NONCE, <<>>, <<0:(32*8)>>).
|
encrypt(Cipher, K, ?MAX_NONCE, <<>>, <<0:(32*8)>>).
|
||||||
|
|
||||||
@ -80,7 +80,10 @@ decrypt('AESGCM', K, N, Ad, CipherText0) ->
|
|||||||
CTLen = byte_size(CipherText0) - ?MAC_LEN,
|
CTLen = byte_size(CipherText0) - ?MAC_LEN,
|
||||||
<<CipherText:CTLen/binary, MAC:?MAC_LEN/binary>> = CipherText0,
|
<<CipherText:CTLen/binary, MAC:?MAC_LEN/binary>> = CipherText0,
|
||||||
Nonce = <<0:32, N:64>>,
|
Nonce = <<0:32, N:64>>,
|
||||||
crypto:block_decrypt(aes_gcm, K, Nonce, {Ad, CipherText, MAC}).
|
case crypto:block_decrypt(aes_gcm, K, Nonce, {Ad, CipherText, MAC}) of
|
||||||
|
error -> {error, decrypt_failed};
|
||||||
|
Data -> Data
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
-spec hash(Hash :: enoise_sym_state:noise_hash(), Data :: binary()) -> binary().
|
-spec hash(Hash :: enoise_sym_state:noise_hash(), Data :: binary()) -> binary().
|
||||||
|
@ -21,6 +21,11 @@
|
|||||||
-type noise_dh() :: dh25519 | dh448.
|
-type noise_dh() :: dh25519 | dh448.
|
||||||
-type noise_token() :: s | e | ee | ss | es | se.
|
-type noise_token() :: s | e | ee | ss | es | se.
|
||||||
-type keypair() :: enoise_keypair:keypair().
|
-type keypair() :: enoise_keypair:keypair().
|
||||||
|
-type noise_split_state() :: #{ rx := enoise_cipher_state:state(),
|
||||||
|
tx := enoise_cipher_state:state(),
|
||||||
|
hs_hash := binary(),
|
||||||
|
final_state => state() }.
|
||||||
|
|
||||||
|
|
||||||
-record(noise_hs, { ss :: enoise_sym_state:state()
|
-record(noise_hs, { ss :: enoise_sym_state:state()
|
||||||
, s :: keypair() | undefined
|
, s :: keypair() | undefined
|
||||||
@ -32,7 +37,7 @@
|
|||||||
, msgs = [] :: [enoise_protocol:noise_msg()] }).
|
, msgs = [] :: [enoise_protocol:noise_msg()] }).
|
||||||
|
|
||||||
-opaque state() :: #noise_hs{}.
|
-opaque state() :: #noise_hs{}.
|
||||||
-export_type([noise_dh/0, noise_role/0, noise_token/0, state/0]).
|
-export_type([noise_dh/0, noise_role/0, noise_split_state/0, noise_token/0, state/0]).
|
||||||
|
|
||||||
-spec init(Protocol :: string() | enoise_protocol:protocol(),
|
-spec init(Protocol :: string() | enoise_protocol:protocol(),
|
||||||
Role :: noise_role(), Prologue :: binary(),
|
Role :: noise_role(), Prologue :: binary(),
|
||||||
@ -54,7 +59,7 @@ init(Protocol, Role, Prologue, {S, E, RS, RE}) ->
|
|||||||
({in, [e]}, HS0) -> mix_hash(HS0, enoise_keypair:pubkey(RE))
|
({in, [e]}, HS0) -> mix_hash(HS0, enoise_keypair:pubkey(RE))
|
||||||
end, HS, PreMsgs).
|
end, HS, PreMsgs).
|
||||||
|
|
||||||
-spec finalize(HS :: state()) -> {ok, map()} | {error, term()}.
|
-spec finalize(HS :: state()) -> {ok, noise_split_state()} | {error, term()}.
|
||||||
finalize(HS = #noise_hs{ msgs = [], ss = SS, role = Role }) ->
|
finalize(HS = #noise_hs{ msgs = [], ss = SS, role = Role }) ->
|
||||||
{C1, C2} = enoise_sym_state:split(SS),
|
{C1, C2} = enoise_sym_state:split(SS),
|
||||||
HSHash = enoise_sym_state:h(SS),
|
HSHash = enoise_sym_state:h(SS),
|
||||||
@ -68,7 +73,7 @@ finalize(_) ->
|
|||||||
|
|
||||||
-spec next_message(HS :: state()) -> in | out | done.
|
-spec next_message(HS :: state()) -> in | out | done.
|
||||||
next_message(#noise_hs{ msgs = [{Dir, _} | _] }) -> Dir;
|
next_message(#noise_hs{ msgs = [{Dir, _} | _] }) -> Dir;
|
||||||
next_message(_) -> done.
|
next_message(#noise_hs{ }) -> done.
|
||||||
|
|
||||||
-spec write_message(HS :: state(), PayLoad :: binary()) -> {ok, state(), binary()}.
|
-spec write_message(HS :: state(), PayLoad :: binary()) -> {ok, state(), binary()}.
|
||||||
write_message(HS = #noise_hs{ msgs = [{out, Msg} | Msgs] }, PayLoad) ->
|
write_message(HS = #noise_hs{ msgs = [{out, Msg} | Msgs] }, PayLoad) ->
|
||||||
|
@ -36,7 +36,9 @@ new(Type) ->
|
|||||||
%% @doc Create a new keypair of type `Type'. If `Public' is `undefined'
|
%% @doc Create a new keypair of type `Type'. If `Public' is `undefined'
|
||||||
%% it will be computed from the `Secret' (using the curve/algorithm
|
%% it will be computed from the `Secret' (using the curve/algorithm
|
||||||
%% indicated by `Type').
|
%% indicated by `Type').
|
||||||
-spec new(Type :: key_type(), Secret :: binary(), Public :: binary() | undefined) -> keypair().
|
-spec new(Type :: key_type(),
|
||||||
|
Secret :: binary() | undefined,
|
||||||
|
Public :: binary() | undefined) -> keypair().
|
||||||
new(Type, Secret, undefined) ->
|
new(Type, Secret, undefined) ->
|
||||||
new(Type, Secret, pubkey_from_secret(Type, Secret));
|
new(Type, Secret, pubkey_from_secret(Type, Secret));
|
||||||
new(Type, Secret, Public) ->
|
new(Type, Secret, Public) ->
|
||||||
|
@ -90,6 +90,7 @@ pre_msgs(Role, #noise_protocol{ hs_pattern = Pattern }) ->
|
|||||||
{PreMsgs, _Msgs} = protocol(Pattern),
|
{PreMsgs, _Msgs} = protocol(Pattern),
|
||||||
role_adapt(Role, PreMsgs).
|
role_adapt(Role, PreMsgs).
|
||||||
|
|
||||||
|
-spec role_adapt(Role :: enoise_hs_state:noise_role(), [noise_msg()]) -> [noise_msg()].
|
||||||
role_adapt(initiator, Msgs) ->
|
role_adapt(initiator, Msgs) ->
|
||||||
Msgs;
|
Msgs;
|
||||||
role_adapt(responder, Msgs) ->
|
role_adapt(responder, Msgs) ->
|
||||||
|
Loading…
x
Reference in New Issue
Block a user