summaryrefslogtreecommitdiff
path: root/src/monocypher.erl
blob: acdf488402054d6b9ee407836602011050802e14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
% SPDX-FileCopyrightText: 2020 pukkamustard <pukkamustard@posteo.net>
%
% SPDX-License-Identifier: LGPL-3.0-or-later

-module(monocypher).
-export([crypto_lock/4,
         crypto_unlock/4]).
-export([crypto_blake2b/1,
         crypto_blake2b_general/3]).
-export([crypto_ietf_chacha20/3,
         crypto_ietf_chacha20_ctr/4]).
-on_load(init/0).

-define(APPNAME, monocypher).
-define(LIBNAME, monocypher).

%% Authenticated Encryption

-spec crypto_lock(binary(), binary(), binary(), binary()) -> binary().
crypto_lock(_,_,_,_) ->
    not_loaded(?LINE).

-spec crypto_unlock(binary(), binary(), binary(), binary()) -> {'ok', binary()} | {'error', integer()}.
crypto_unlock(_,_,_,_) ->
    not_loaded(?LINE).

%% Hashing

-spec crypto_blake2b(binary()) -> binary().
crypto_blake2b(_) ->
    not_loaded(?LINE).

-spec crypto_blake2b_general(integer(), binary(), binary()) -> binary().
crypto_blake2b_general(_,_,_) ->
    not_loaded(?LINE).

%% IETF ChaCha20

-spec crypto_ietf_chacha20(binary(), binary(), binary()) -> binary().
crypto_ietf_chacha20(_,_,_) ->
    not_loaded(?LINE).

-spec crypto_ietf_chacha20_ctr(binary(), binary(), binary(), pos_integer()) -> binary().
crypto_ietf_chacha20_ctr(_,_,_,_) ->
    not_loaded(?LINE).

init() ->
    SoName = case code:priv_dir(?APPNAME) of
        {error, bad_name} ->
            case filelib:is_dir(filename:join(["..", priv])) of
                true ->
                    filename:join(["..", priv, ?LIBNAME]);
                _ ->
                    filename:join([priv, ?LIBNAME])
            end;
        Dir ->
            filename:join(Dir, ?LIBNAME)
    end,
    erlang:load_nif(SoName, 0).

not_loaded(Line) ->
    erlang:nif_error({not_loaded, [{module, ?MODULE}, {line, Line}]}).