1%% -*- mode: erlang; tab-width: 4; indent-tabs-mode: 1; st-rulers: [70] -*-
2%% vim: ts=4 sw=4 ft=erlang noet
3%%%-------------------------------------------------------------------
4%%% @author Andrew Bennett <potatosaladx@gmail.com>
5%%% @copyright 2014-2016, Andrew Bennett
6%%% @doc
7%%%
8%%% @end
9%%% Created :  07 Jan 2016 by Andrew Bennett <potatosaladx@gmail.com>
10%%%-------------------------------------------------------------------
11-module(jose_jwa_curve25519).
12
13-behaviour(jose_curve25519).
14
15%% jose_curve25519 callbacks
16-export([eddsa_keypair/0]).
17-export([eddsa_keypair/1]).
18-export([eddsa_secret_to_public/1]).
19-export([ed25519_sign/2]).
20-export([ed25519_verify/3]).
21-export([ed25519ph_sign/2]).
22-export([ed25519ph_verify/3]).
23-export([x25519_keypair/0]).
24-export([x25519_keypair/1]).
25-export([x25519_secret_to_public/1]).
26-export([x25519_shared_secret/2]).
27
28%%====================================================================
29%% jose_curve25519 callbacks
30%%====================================================================
31
32% EdDSA
33eddsa_keypair() ->
34	jose_jwa_ed25519:keypair().
35
36eddsa_keypair(Seed)
37		when is_binary(Seed) ->
38	jose_jwa_ed25519:keypair(Seed).
39
40eddsa_secret_to_public(SecretKey)
41		when is_binary(SecretKey) ->
42	jose_jwa_ed25519:secret_to_pk(SecretKey).
43
44% Ed25519
45ed25519_sign(Message, SecretKey)
46		when is_binary(Message)
47		andalso is_binary(SecretKey) ->
48	jose_jwa_ed25519:sign(Message, SecretKey).
49
50ed25519_verify(Signature, Message, PublicKey)
51		when is_binary(Signature)
52		andalso is_binary(Message)
53		andalso is_binary(PublicKey) ->
54	try
55		jose_jwa_ed25519:verify(Signature, Message, PublicKey)
56	catch
57		_:_ ->
58			false
59	end.
60
61% Ed25519ph
62ed25519ph_sign(Message, SecretKey)
63		when is_binary(Message)
64		andalso is_binary(SecretKey) ->
65	jose_jwa_ed25519:sign_with_prehash(Message, SecretKey).
66
67ed25519ph_verify(Signature, Message, PublicKey)
68		when is_binary(Signature)
69		andalso is_binary(Message)
70		andalso is_binary(PublicKey) ->
71	try
72		jose_jwa_ed25519:verify_with_prehash(Signature, Message, PublicKey)
73	catch
74		_:_ ->
75			false
76	end.
77
78% X25519
79x25519_keypair() ->
80	jose_jwa_x25519:keypair().
81
82x25519_keypair(Seed)
83		when is_binary(Seed) ->
84	jose_jwa_x25519:keypair(Seed).
85
86x25519_secret_to_public(SecretKey)
87		when is_binary(SecretKey) ->
88	jose_jwa_x25519:sk_to_pk(SecretKey).
89
90x25519_shared_secret(MySecretKey, YourPublicKey)
91		when is_binary(MySecretKey)
92		andalso is_binary(YourPublicKey) ->
93	jose_jwa_x25519:x25519(MySecretKey, YourPublicKey).
94