enacl_nif.erl 2.89 KB
Newer Older
1
2
%%% @doc module enacl_nif provides the low-level interface to the NaCl/Sodium NIFs.
%%% @end
3
%%% @private
Jesper Louis Andersen's avatar
Jesper Louis Andersen committed
4
5
-module(enacl_nif).

6
%% Public key auth
Jesper Louis Andersen's avatar
Jesper Louis Andersen committed
7
-export([
8
9
10
11
12
	crypto_box_keypair/0,
	crypto_box/4,
	crypto_box_open/4,
	crypto_box_NONCEBYTES/0,
	crypto_box_ZEROBYTES/0,
13
14
	crypto_box_BOXZEROBYTES/0,
	crypto_box_PUBLICKEYBYTES/0,
15
16
17
18
19
20
21
	crypto_box_SECRETKEYBYTES/0,

	crypto_sign_PUBLICKEYBYTES/0,
	crypto_sign_SECRETKEYBYTES/0,
	crypto_sign_keypair/0,
	crypto_sign/2,
	crypto_sign_open/2
Jesper Louis Andersen's avatar
Jesper Louis Andersen committed
22
]).
23

24
25
26
27
28
29
30
%% Secret key crypto
-export([
	crypto_secretbox/3,
	crypto_secretbox_open/3,
	crypto_secretbox_NONCEBYTES/0,
	crypto_secretbox_ZEROBYTES/0,
	crypto_secretbox_BOXZEROBYTES/0,
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	crypto_secretbox_KEYBYTES/0,

	crypto_stream_KEYBYTES/0,
	crypto_stream_NONCEBYTES/0,
	crypto_stream/3,
	crypto_stream_xor/3,

	crypto_auth_KEYBYTES/0,
	crypto_auth/2,
	crypto_auth_verify/3,

	crypto_onetimeauth_KEYBYTES/0,
	crypto_onetimeauth/2,
	crypto_onetimeauth_verify/3
45
46
]).

47
48
%% Miscellaneous helper functions
-export([
49
50
51
	crypto_hash/1,
	crypto_verify_16/2,
	crypto_verify_32/2
52
53
]).

Jesper Louis Andersen's avatar
Jesper Louis Andersen committed
54
55
56
57
58
59
60
61
62
63
64
65
-on_load(init/0).

init() ->
	SoName = filename:join(
		case code:priv_dir(enacl) of
		    {error, bad_name} ->
		        filename:join(filename:dirname(filename:dirname(code:which(?MODULE))), "priv");
		    Dir ->
		        Dir
		end, atom_to_list(?MODULE)),
	erlang:load_nif(SoName, 0).

66
67
68
69
70
71
not_loaded() ->
    error({nif_not_loaded, ?MODULE}).

crypto_box_NONCEBYTES() -> not_loaded().
crypto_box_ZEROBYTES() -> not_loaded().
crypto_box_BOXZEROBYTES() -> not_loaded().
72
73
crypto_box_PUBLICKEYBYTES() -> not_loaded().
crypto_box_SECRETKEYBYTES() -> not_loaded().
74
75
76
77
crypto_box_keypair() -> not_loaded().
crypto_box(_PaddedMsg, _Nonce, _PK, _SK) -> not_loaded().
crypto_box_open(_CipherText, _Nonce, _PK, _SK) -> not_loaded().

78
79
80
81
82
83
crypto_sign_PUBLICKEYBYTES() -> not_loaded().
crypto_sign_SECRETKEYBYTES() -> not_loaded().
crypto_sign_keypair() -> not_loaded().
crypto_sign(_M, _SK) -> not_loaded().
crypto_sign_open(_SignedMessage, _PK) -> not_loaded().

84
85
86
87
88
89
90
crypto_secretbox_NONCEBYTES() -> not_loaded().
crypto_secretbox_ZEROBYTES() -> not_loaded().
crypto_secretbox_KEYBYTES() -> not_loaded().
crypto_secretbox_BOXZEROBYTES() -> not_loaded().
crypto_secretbox(_CipherText, _Nonce, _Key) -> not_loaded().
crypto_secretbox_open(_CipherText, _Nonce, _Key) -> not_loaded().

91
92
93
94
95
96
97
98
99
100
101
102
103
crypto_stream_KEYBYTES() -> not_loaded().
crypto_stream_NONCEBYTES() -> not_loaded().
crypto_stream(_Bytes, _Nonce, _Key) -> not_loaded().
crypto_stream_xor(_M, _Nonce, _Key) -> not_loaded().

crypto_auth_KEYBYTES() -> not_loaded().
crypto_auth(_Msg, _Key) -> not_loaded().
crypto_auth_verify(_Authenticator, _Msg, _Key) -> not_loaded().

crypto_onetimeauth_KEYBYTES() -> not_loaded().
crypto_onetimeauth(_Msg, _Key) -> not_loaded().
crypto_onetimeauth_verify(_Authenticator, _Msg, _Key) -> not_loaded().

104
crypto_hash(Input) when is_binary(Input) -> not_loaded().
105
106
107
crypto_verify_16(_X, _Y) -> not_loaded().
crypto_verify_32(_X, _Y) -> not_loaded().

108

Jesper Louis Andersen's avatar
Jesper Louis Andersen committed
109