1 /* eddsa.h
2 
3    Copyright (C) 2014 Niels Möller
4 
5    This file is part of GNU Nettle.
6 
7    GNU Nettle is free software: you can redistribute it and/or
8    modify it under the terms of either:
9 
10      * the GNU Lesser General Public License as published by the Free
11        Software Foundation; either version 3 of the License, or (at your
12        option) any later version.
13 
14    or
15 
16      * the GNU General Public License as published by the Free
17        Software Foundation; either version 2 of the License, or (at your
18        option) any later version.
19 
20    or both in parallel, as here.
21 
22    GNU Nettle is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25    General Public License for more details.
26 
27    You should have received copies of the GNU General Public License and
28    the GNU Lesser General Public License along with this program.  If
29    not, see http://www.gnu.org/licenses/.
30 */
31 
32 #ifndef GNUTLS_LIB_NETTLE_ECC_NETTLE_EDDSA_INTERNAL_H
33 #define GNUTLS_LIB_NETTLE_ECC_NETTLE_EDDSA_INTERNAL_H
34 
35 #include <nettle/nettle-types.h>
36 #include <nettle/bignum.h>
37 
38 #define _eddsa_compress _gnutls_nettle_ecc_eddsa_compress
39 #define _eddsa_compress_itch _gnutls_nettle_ecc_eddsa_compress_itch
40 #define _eddsa_decompress _gnutls_nettle_ecc_eddsa_decompress
41 #define _eddsa_decompress_itch _gnutls_nettle_ecc_eddsa_decompress_itch
42 #define _eddsa_hash _gnutls_nettle_ecc_eddsa_hash
43 #define _eddsa_expand_key _gnutls_nettle_ecc_eddsa_expand_key
44 #define _eddsa_sign _gnutls_nettle_ecc_eddsa_sign
45 #define _eddsa_sign_itch _gnutls_nettle_ecc_eddsa_sign_itch
46 #define _eddsa_verify _gnutls_nettle_ecc_eddsa_verify
47 #define _eddsa_verify_itch _gnutls_nettle_ecc_eddsa_verify_itch
48 #define _eddsa_public_key_itch _gnutls_nettle_ecc_eddsa_public_key_itch
49 #define _eddsa_public_key _gnutls_nettle_ecc_eddsa_public_key
50 
51 /* Low-level internal functions */
52 
53 struct ecc_curve;
54 struct ecc_modulo;
55 
56 typedef void nettle_eddsa_dom_func(void *ctx);
57 
58 struct ecc_eddsa
59 {
60   /* Hash function to use */
61   nettle_hash_update_func *update;
62   nettle_hash_digest_func *digest;
63   nettle_eddsa_dom_func *dom;
64   /* For generating the secret scalar */
65   mp_limb_t low_mask;
66   mp_limb_t high_bit;
67 };
68 
69 #define _nettle__ed25519_sha512 _gnutls_nettle_ecc__ed25519_sha512
70 extern const struct ecc_eddsa _nettle_ed25519_sha512;
71 #define _nettle__ed448_shake256 _gnutls_nettle_ecc__ed448_shake256
72 extern const struct ecc_eddsa _nettle_ed448_shake256;
73 
74 mp_size_t
75 _eddsa_compress_itch (const struct ecc_curve *ecc);
76 void
77 _eddsa_compress (const struct ecc_curve *ecc, uint8_t *r, mp_limb_t *p,
78 		 mp_limb_t *scratch);
79 
80 mp_size_t
81 _eddsa_decompress_itch (const struct ecc_curve *ecc);
82 int
83 _eddsa_decompress (const struct ecc_curve *ecc, mp_limb_t *p,
84 		   const uint8_t *cp,
85 		   mp_limb_t *scratch);
86 
87 void
88 _eddsa_hash (const struct ecc_modulo *m,
89 	     mp_limb_t *rp, size_t digest_size, const uint8_t *digest);
90 
91 mp_size_t
92 _eddsa_sign_itch (const struct ecc_curve *ecc);
93 
94 void
95 _eddsa_sign (const struct ecc_curve *ecc,
96 	     const struct ecc_eddsa *eddsa,
97 	     void *ctx,
98 	     const uint8_t *pub,
99 	     const uint8_t *k1,
100 	     const mp_limb_t *k2,
101 	     size_t length,
102 	     const uint8_t *msg,
103 	     uint8_t *signature,
104 	     mp_limb_t *scratch);
105 
106 mp_size_t
107 _eddsa_verify_itch (const struct ecc_curve *ecc);
108 
109 int
110 _eddsa_verify (const struct ecc_curve *ecc,
111 	       const struct ecc_eddsa *eddsa,
112 	       const uint8_t *pub,
113 	       const mp_limb_t *A,
114 	       void *ctx,
115 	       size_t length,
116 	       const uint8_t *msg,
117 	       const uint8_t *signature,
118 	       mp_limb_t *scratch);
119 
120 void
121 _eddsa_expand_key (const struct ecc_curve *ecc,
122 		   const struct ecc_eddsa *eddsa,
123 		   void *ctx,
124 		   const uint8_t *key,
125 		   uint8_t *digest,
126 		   mp_limb_t *k2);
127 
128 mp_size_t
129 _eddsa_public_key_itch (const struct ecc_curve *ecc);
130 
131 void
132 _eddsa_public_key (const struct ecc_curve *ecc,
133 		   const mp_limb_t *k, uint8_t *pub, mp_limb_t *scratch);
134 
135 #endif /* GNUTLS_LIB_NETTLE_ECC_NETTLE_EDDSA_INTERNAL_H */
136