1*f0865ec9SKyle Evans /*
2*f0865ec9SKyle Evans * Copyright (C) 2021 - This file is part of libecc project
3*f0865ec9SKyle Evans *
4*f0865ec9SKyle Evans * Authors:
5*f0865ec9SKyle Evans * Ryad BENADJILA <ryadbenadjila@gmail.com>
6*f0865ec9SKyle Evans * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7*f0865ec9SKyle Evans *
8*f0865ec9SKyle Evans * This software is licensed under a dual BSD and GPL v2 license.
9*f0865ec9SKyle Evans * See LICENSE file at the root folder of the project.
10*f0865ec9SKyle Evans */
11*f0865ec9SKyle Evans #include "sdsa.h"
12*f0865ec9SKyle Evans
13*f0865ec9SKyle Evans
14*f0865ec9SKyle Evans /* We include the rand external dependency because we have to generate
15*f0865ec9SKyle Evans * some random data for the nonces.
16*f0865ec9SKyle Evans */
17*f0865ec9SKyle Evans #include <libecc/external_deps/rand.h>
18*f0865ec9SKyle Evans /* We include the printf external dependency for printf output */
19*f0865ec9SKyle Evans #include <libecc/external_deps/print.h>
20*f0865ec9SKyle Evans /* We include our common helpers */
21*f0865ec9SKyle Evans #include "../common/common.h"
22*f0865ec9SKyle Evans
23*f0865ec9SKyle Evans /*
24*f0865ec9SKyle Evans * The purpose of this example is to implement the Schnorr signature
25*f0865ec9SKyle Evans * scheme (aka SDSA for Schnorr DSA) based on libecc arithmetic primitives.
26*f0865ec9SKyle Evans * Many "variants" of Schnorr signature schemes exist, we implement here the
27*f0865ec9SKyle Evans * one corresponding to SDSA as described in the ISO14888-3 standard.
28*f0865ec9SKyle Evans *
29*f0865ec9SKyle Evans * XXX: Please be aware that libecc has been designed for Elliptic
30*f0865ec9SKyle Evans * Curve cryptography, and as so the arithmetic primitives are
31*f0865ec9SKyle Evans * not optimized for big numbers >= 1024 bits usually used for SDSA.
32*f0865ec9SKyle Evans * Additionnaly, a hard limit of our NN values makes it impossible
33*f0865ec9SKyle Evans * to exceed ~5300 bits in the best case (words of size 64 bits).
34*f0865ec9SKyle Evans *
35*f0865ec9SKyle Evans * All in all, please see this as a proof of concept.
36*f0865ec9SKyle Evans * Use it at your own risk!
37*f0865ec9SKyle Evans *
38*f0865ec9SKyle Evans * !! DISCLAIMER !!
39*f0865ec9SKyle Evans * ================
40*f0865ec9SKyle Evans *
41*f0865ec9SKyle Evans * Althoug some efforts have been made to secure this implementation
42*f0865ec9SKyle Evans * of Schnorr DSA (e.g. by protecting the private key and nonces using constant
43*f0865ec9SKyle Evans * time and blinding WHEN activated with BLINDING=1), please consider this
44*f0865ec9SKyle Evans * code as a proof of concept and use it at your own risk.
45*f0865ec9SKyle Evans *
46*f0865ec9SKyle Evans * All-in-all, this piece of code can be useful in some contexts, or risky to
47*f0865ec9SKyle Evans * use in other sensitive ones where advanced side-channels or fault attacks
48*f0865ec9SKyle Evans * have to be considered. Use this SDSA code knowingly and at your own risk!
49*f0865ec9SKyle Evans *
50*f0865ec9SKyle Evans */
51*f0865ec9SKyle Evans
52*f0865ec9SKyle Evans /* NOTE: since SDSA is very similar to DSA, we reuse some of our DSA
53*f0865ec9SKyle Evans * primitives to factorize some code. Also, SDSA private and public keys
54*f0865ec9SKyle Evans * have the exact same type as DSA keys.
55*f0865ec9SKyle Evans */
56*f0865ec9SKyle Evans
57*f0865ec9SKyle Evans /* Import a SDSA private key from buffers */
sdsa_import_priv_key(sdsa_priv_key * priv,const u8 * p,u16 plen,const u8 * q,u16 qlen,const u8 * g,u16 glen,const u8 * x,u16 xlen)58*f0865ec9SKyle Evans int sdsa_import_priv_key(sdsa_priv_key *priv, const u8 *p, u16 plen,
59*f0865ec9SKyle Evans const u8 *q, u16 qlen,
60*f0865ec9SKyle Evans const u8 *g, u16 glen,
61*f0865ec9SKyle Evans const u8 *x, u16 xlen)
62*f0865ec9SKyle Evans {
63*f0865ec9SKyle Evans return dsa_import_priv_key(priv, p, plen, q, qlen, g, glen, x, xlen);
64*f0865ec9SKyle Evans }
65*f0865ec9SKyle Evans
66*f0865ec9SKyle Evans /* Import a SDSA public key from buffers */
sdsa_import_pub_key(sdsa_pub_key * pub,const u8 * p,u16 plen,const u8 * q,u16 qlen,const u8 * g,u16 glen,const u8 * y,u16 ylen)67*f0865ec9SKyle Evans int sdsa_import_pub_key(sdsa_pub_key *pub, const u8 *p, u16 plen,
68*f0865ec9SKyle Evans const u8 *q, u16 qlen,
69*f0865ec9SKyle Evans const u8 *g, u16 glen,
70*f0865ec9SKyle Evans const u8 *y, u16 ylen)
71*f0865ec9SKyle Evans {
72*f0865ec9SKyle Evans return dsa_import_pub_key(pub, p, plen, q, qlen, g, glen, y, ylen);
73*f0865ec9SKyle Evans }
74*f0865ec9SKyle Evans
75*f0865ec9SKyle Evans
76*f0865ec9SKyle Evans
77*f0865ec9SKyle Evans /* Compute a SDSA public key from a private key.
78*f0865ec9SKyle Evans * The public key is computed using modular exponentiation of the generator
79*f0865ec9SKyle Evans * with the private key.
80*f0865ec9SKyle Evans */
sdsa_compute_pub_from_priv(sdsa_pub_key * pub,const sdsa_priv_key * priv)81*f0865ec9SKyle Evans int sdsa_compute_pub_from_priv(sdsa_pub_key *pub, const sdsa_priv_key *priv)
82*f0865ec9SKyle Evans {
83*f0865ec9SKyle Evans return dsa_compute_pub_from_priv(pub, priv);
84*f0865ec9SKyle Evans }
85*f0865ec9SKyle Evans
86*f0865ec9SKyle Evans /* Generate a SDSA signature
87*f0865ec9SKyle Evans */
sdsa_sign(const sdsa_priv_key * priv,const u8 * msg,u32 msglen,const u8 * nonce,u16 noncelen,u8 * sig,u16 siglen,gen_hash_alg_type sdsa_hash)88*f0865ec9SKyle Evans int sdsa_sign(const sdsa_priv_key *priv, const u8 *msg, u32 msglen,
89*f0865ec9SKyle Evans const u8 *nonce, u16 noncelen,
90*f0865ec9SKyle Evans u8 *sig, u16 siglen, gen_hash_alg_type sdsa_hash)
91*f0865ec9SKyle Evans {
92*f0865ec9SKyle Evans int ret, iszero;
93*f0865ec9SKyle Evans /* alpha is the bit length of p, beta is the bit length of q */
94*f0865ec9SKyle Evans bitcnt_t alpha, beta;
95*f0865ec9SKyle Evans /* Length of the hash function (hlen is "gamma") */
96*f0865ec9SKyle Evans u8 hlen, block_size;
97*f0865ec9SKyle Evans nn_src_t p, q, g, x;
98*f0865ec9SKyle Evans /* The nonce and its protected version */
99*f0865ec9SKyle Evans nn k, k_;
100*f0865ec9SKyle Evans /* r, s, pi */
101*f0865ec9SKyle Evans nn r, s;
102*f0865ec9SKyle Evans nn_t pi;
103*f0865ec9SKyle Evans /* This is a bit too much for stack space, but we need it for
104*f0865ec9SKyle Evans * the computation of "pi" I2BS representation ...
105*f0865ec9SKyle Evans */
106*f0865ec9SKyle Evans u8 pi_buf[NN_USABLE_MAX_BYTE_LEN];
107*f0865ec9SKyle Evans /* hash context */
108*f0865ec9SKyle Evans gen_hash_context hash_ctx;
109*f0865ec9SKyle Evans #ifdef USE_SIG_BLINDING
110*f0865ec9SKyle Evans /* b is the blinding mask */
111*f0865ec9SKyle Evans nn b;
112*f0865ec9SKyle Evans b.magic = WORD(0);
113*f0865ec9SKyle Evans #endif /* USE_SIG_BLINDING */
114*f0865ec9SKyle Evans k.magic = k_.magic = r.magic = s.magic = WORD(0);
115*f0865ec9SKyle Evans
116*f0865ec9SKyle Evans /* Sanity checks */
117*f0865ec9SKyle Evans MUST_HAVE((priv != NULL) && (msg != NULL) && (sig != NULL), ret, err);
118*f0865ec9SKyle Evans
119*f0865ec9SKyle Evans ret = local_memset(pi_buf, 0, sizeof(pi_buf)); EG(ret, err);
120*f0865ec9SKyle Evans
121*f0865ec9SKyle Evans /* Make things more readable */
122*f0865ec9SKyle Evans p = &(priv->p);
123*f0865ec9SKyle Evans q = &(priv->q);
124*f0865ec9SKyle Evans g = &(priv->g);
125*f0865ec9SKyle Evans x = &(priv->x);
126*f0865ec9SKyle Evans
127*f0865ec9SKyle Evans /* Sanity checks */
128*f0865ec9SKyle Evans ret = nn_check_initialized(p); EG(ret, err);
129*f0865ec9SKyle Evans ret = nn_check_initialized(q); EG(ret, err);
130*f0865ec9SKyle Evans ret = nn_check_initialized(g); EG(ret, err);
131*f0865ec9SKyle Evans ret = nn_check_initialized(x); EG(ret, err);
132*f0865ec9SKyle Evans
133*f0865ec9SKyle Evans /* Let alpha be the bit length of p */
134*f0865ec9SKyle Evans ret = nn_bitlen(p, &alpha); EG(ret, err);
135*f0865ec9SKyle Evans /* Let beta be the bit length of q */
136*f0865ec9SKyle Evans ret = nn_bitlen(q, &beta); EG(ret, err);
137*f0865ec9SKyle Evans /* Get the hash sizes (8*"gamma") */
138*f0865ec9SKyle Evans ret = gen_hash_get_hash_sizes(sdsa_hash, &hlen, &block_size); EG(ret, err);
139*f0865ec9SKyle Evans MUST_HAVE((hlen <= MAX_DIGEST_SIZE), ret, err);
140*f0865ec9SKyle Evans
141*f0865ec9SKyle Evans /* Sanity check on the signature length:
142*f0865ec9SKyle Evans * the signature is of size hash function plus an integer modulo q
143*f0865ec9SKyle Evans * "gamma" + beta
144*f0865ec9SKyle Evans */
145*f0865ec9SKyle Evans MUST_HAVE((siglen == (hlen + BYTECEIL(beta))), ret, err);
146*f0865ec9SKyle Evans
147*f0865ec9SKyle Evans restart:
148*f0865ec9SKyle Evans /* If the nonce is imposed, use it. Else get a random modulo q */
149*f0865ec9SKyle Evans if(nonce != NULL){
150*f0865ec9SKyle Evans ret = _os2ip(&k, nonce, noncelen); EG(ret, err);
151*f0865ec9SKyle Evans }
152*f0865ec9SKyle Evans else{
153*f0865ec9SKyle Evans ret = nn_get_random_mod(&k, q); EG(ret, err);
154*f0865ec9SKyle Evans }
155*f0865ec9SKyle Evans
156*f0865ec9SKyle Evans /* Fix the MSB of our scalar */
157*f0865ec9SKyle Evans ret = nn_copy(&k_, &k); EG(ret, err);
158*f0865ec9SKyle Evans #ifdef USE_SIG_BLINDING
159*f0865ec9SKyle Evans /* Blind the scalar */
160*f0865ec9SKyle Evans ret = _blind_scalar(&k_, q, &k_); EG(ret, err);
161*f0865ec9SKyle Evans #endif /* USE_SIG_BLINDING */
162*f0865ec9SKyle Evans ret = _fix_scalar_msb(&k_, q, &k_); EG(ret, err);
163*f0865ec9SKyle Evans /* Use r as aliasing for pi to save some space */
164*f0865ec9SKyle Evans pi = &r;
165*f0865ec9SKyle Evans /* pi = (g**k mod p) */
166*f0865ec9SKyle Evans ret = nn_init(pi, 0); EG(ret, err);
167*f0865ec9SKyle Evans /* Exponentiation modulo p */
168*f0865ec9SKyle Evans ret = nn_mod_pow(pi, g, &k_, p); EG(ret, err);
169*f0865ec9SKyle Evans
170*f0865ec9SKyle Evans /* Compute I2BS(alpha, pi)
171*f0865ec9SKyle Evans */
172*f0865ec9SKyle Evans ret = _i2osp(pi, pi_buf, (u16)BYTECEIL(alpha)); EG(ret, err);
173*f0865ec9SKyle Evans
174*f0865ec9SKyle Evans /* r = h(I2BS(alpha, pi) || M) */
175*f0865ec9SKyle Evans ret = gen_hash_init(&hash_ctx, sdsa_hash); EG(ret, err);
176*f0865ec9SKyle Evans ret = gen_hash_update(&hash_ctx, pi_buf, (u16)BYTECEIL(alpha), sdsa_hash); EG(ret, err);
177*f0865ec9SKyle Evans ret = gen_hash_update(&hash_ctx, msg, msglen, sdsa_hash); EG(ret, err);
178*f0865ec9SKyle Evans /* Export r result of the hash function in sig */
179*f0865ec9SKyle Evans ret = gen_hash_final(&hash_ctx, sig, sdsa_hash); EG(ret, err);
180*f0865ec9SKyle Evans
181*f0865ec9SKyle Evans /* Import r as an integer modulo q */
182*f0865ec9SKyle Evans ret = _os2ip(&r, sig, hlen); EG(ret, err);
183*f0865ec9SKyle Evans ret = nn_mod(&r, &r, q); EG(ret, err);
184*f0865ec9SKyle Evans
185*f0865ec9SKyle Evans /* If r is 0, restart the process */
186*f0865ec9SKyle Evans ret = nn_iszero(&r, &iszero); EG(ret, err);
187*f0865ec9SKyle Evans if (iszero) {
188*f0865ec9SKyle Evans IGNORE_RET_VAL(local_memset(sig, 0, hlen));
189*f0865ec9SKyle Evans goto restart;
190*f0865ec9SKyle Evans }
191*f0865ec9SKyle Evans
192*f0865ec9SKyle Evans #ifdef USE_SIG_BLINDING
193*f0865ec9SKyle Evans /* Note: if we use blinding, r and k are multiplied by
194*f0865ec9SKyle Evans * a random value b in ]0,q[ */
195*f0865ec9SKyle Evans ret = nn_get_random_mod(&b, q); EG(ret, err);
196*f0865ec9SKyle Evans /* Blind r with b */
197*f0865ec9SKyle Evans ret = nn_mod_mul(&r, &r, &b, q); EG(ret, err);
198*f0865ec9SKyle Evans /* Blind k with b */
199*f0865ec9SKyle Evans ret = nn_mod_mul(&k, &k, &b, q); EG(ret, err);
200*f0865ec9SKyle Evans /*
201*f0865ec9SKyle Evans * In case of blinding, we compute b^-1 with
202*f0865ec9SKyle Evans * little Fermat theorem. This will be used to
203*f0865ec9SKyle Evans * unblind s.
204*f0865ec9SKyle Evans */
205*f0865ec9SKyle Evans ret = nn_modinv_fermat(&b, &b, q); EG(ret, err);
206*f0865ec9SKyle Evans #endif /* USE_SIG_BLINDING */
207*f0865ec9SKyle Evans
208*f0865ec9SKyle Evans /* Compute s = (k + r x) mod q */
209*f0865ec9SKyle Evans ret = nn_mod_mul(&s, &r, x, q); EG(ret, err);
210*f0865ec9SKyle Evans ret = nn_mod_add(&s, &s, &k, q); EG(ret, err);
211*f0865ec9SKyle Evans
212*f0865ec9SKyle Evans #ifdef USE_SIG_BLINDING
213*f0865ec9SKyle Evans /* In case of blinding, unblind s */
214*f0865ec9SKyle Evans ret = nn_mod_mul(&s, &s, &b, q); EG(ret, err);
215*f0865ec9SKyle Evans #endif /* USE_SIG_BLINDING */
216*f0865ec9SKyle Evans /* If s is 0, restart the process */
217*f0865ec9SKyle Evans ret = nn_iszero(&s, &iszero); EG(ret, err);
218*f0865ec9SKyle Evans if (iszero) {
219*f0865ec9SKyle Evans goto restart;
220*f0865ec9SKyle Evans }
221*f0865ec9SKyle Evans
222*f0865ec9SKyle Evans /* Export s */
223*f0865ec9SKyle Evans ret = _i2osp(&s, sig + hlen, (u16)(siglen - hlen)); EG(ret, err);
224*f0865ec9SKyle Evans
225*f0865ec9SKyle Evans err:
226*f0865ec9SKyle Evans if(ret && (sig != NULL)){
227*f0865ec9SKyle Evans IGNORE_RET_VAL(local_memset(sig, 0, siglen));
228*f0865ec9SKyle Evans }
229*f0865ec9SKyle Evans
230*f0865ec9SKyle Evans nn_uninit(&k);
231*f0865ec9SKyle Evans nn_uninit(&k_);
232*f0865ec9SKyle Evans #ifdef USE_SIG_BLINDING
233*f0865ec9SKyle Evans nn_uninit(&b);
234*f0865ec9SKyle Evans #endif
235*f0865ec9SKyle Evans nn_uninit(&r);
236*f0865ec9SKyle Evans nn_uninit(&s);
237*f0865ec9SKyle Evans
238*f0865ec9SKyle Evans PTR_NULLIFY(pi);
239*f0865ec9SKyle Evans
240*f0865ec9SKyle Evans PTR_NULLIFY(p);
241*f0865ec9SKyle Evans PTR_NULLIFY(q);
242*f0865ec9SKyle Evans PTR_NULLIFY(g);
243*f0865ec9SKyle Evans PTR_NULLIFY(x);
244*f0865ec9SKyle Evans
245*f0865ec9SKyle Evans return ret;
246*f0865ec9SKyle Evans }
247*f0865ec9SKyle Evans
248*f0865ec9SKyle Evans
249*f0865ec9SKyle Evans
250*f0865ec9SKyle Evans /* Verify a SDSA signature
251*f0865ec9SKyle Evans */
sdsa_verify(const sdsa_pub_key * pub,const u8 * msg,u32 msglen,const u8 * sig,u16 siglen,gen_hash_alg_type sdsa_hash)252*f0865ec9SKyle Evans int sdsa_verify(const sdsa_pub_key *pub, const u8 *msg, u32 msglen,
253*f0865ec9SKyle Evans const u8 *sig, u16 siglen, gen_hash_alg_type sdsa_hash)
254*f0865ec9SKyle Evans {
255*f0865ec9SKyle Evans int ret, iszero, cmp;
256*f0865ec9SKyle Evans /* alpha is the bit length of p, beta is the bit length of q */
257*f0865ec9SKyle Evans bitcnt_t alpha, beta;
258*f0865ec9SKyle Evans /* Length of the hash function */
259*f0865ec9SKyle Evans u8 hlen, block_size;
260*f0865ec9SKyle Evans nn_src_t p, q, g, y;
261*f0865ec9SKyle Evans /* r, s */
262*f0865ec9SKyle Evans nn r, s;
263*f0865ec9SKyle Evans /* u, and pi */
264*f0865ec9SKyle Evans nn u, pi;
265*f0865ec9SKyle Evans /* This is a bit too much for stack space, but we need it for
266*f0865ec9SKyle Evans * the computation of "pi" I2BS representation ...
267*f0865ec9SKyle Evans */
268*f0865ec9SKyle Evans u8 pi_buf[NN_USABLE_MAX_BYTE_LEN];
269*f0865ec9SKyle Evans /* Hash */
270*f0865ec9SKyle Evans u8 hash[MAX_DIGEST_SIZE];
271*f0865ec9SKyle Evans /* hash context */
272*f0865ec9SKyle Evans gen_hash_context hash_ctx;
273*f0865ec9SKyle Evans r.magic = s.magic = u.magic = pi.magic = WORD(0);
274*f0865ec9SKyle Evans
275*f0865ec9SKyle Evans /* Sanity checks */
276*f0865ec9SKyle Evans MUST_HAVE((pub != NULL) && (msg != NULL) && (sig != NULL), ret, err);
277*f0865ec9SKyle Evans
278*f0865ec9SKyle Evans ret = local_memset(pi_buf, 0, sizeof(pi_buf)); EG(ret, err);
279*f0865ec9SKyle Evans ret = local_memset(hash, 0, sizeof(hash)); EG(ret, err);
280*f0865ec9SKyle Evans
281*f0865ec9SKyle Evans /* Make things more readable */
282*f0865ec9SKyle Evans p = &(pub->p);
283*f0865ec9SKyle Evans q = &(pub->q);
284*f0865ec9SKyle Evans g = &(pub->g);
285*f0865ec9SKyle Evans y = &(pub->y);
286*f0865ec9SKyle Evans
287*f0865ec9SKyle Evans /* Sanity checks */
288*f0865ec9SKyle Evans ret = nn_check_initialized(p); EG(ret, err);
289*f0865ec9SKyle Evans ret = nn_check_initialized(q); EG(ret, err);
290*f0865ec9SKyle Evans ret = nn_check_initialized(g); EG(ret, err);
291*f0865ec9SKyle Evans ret = nn_check_initialized(y); EG(ret, err);
292*f0865ec9SKyle Evans
293*f0865ec9SKyle Evans /* Let alpha be the bit length of p */
294*f0865ec9SKyle Evans ret = nn_bitlen(p, &alpha); EG(ret, err);
295*f0865ec9SKyle Evans /* Let beta be the bit length of q */
296*f0865ec9SKyle Evans ret = nn_bitlen(q, &beta); EG(ret, err);
297*f0865ec9SKyle Evans /* Get the hash sizes (8*"gamma") */
298*f0865ec9SKyle Evans ret = gen_hash_get_hash_sizes(sdsa_hash, &hlen, &block_size); EG(ret, err);
299*f0865ec9SKyle Evans MUST_HAVE((hlen <= MAX_DIGEST_SIZE), ret, err);
300*f0865ec9SKyle Evans
301*f0865ec9SKyle Evans /* Sanity check on the signature length */
302*f0865ec9SKyle Evans MUST_HAVE((siglen == (hlen + BYTECEIL(beta))), ret, err);
303*f0865ec9SKyle Evans
304*f0865ec9SKyle Evans /* Extract r and s */
305*f0865ec9SKyle Evans ret = _os2ip(&r, sig, hlen); EG(ret, err);
306*f0865ec9SKyle Evans ret = _os2ip(&s, sig + hlen, (u16)(siglen - hlen)); EG(ret, err);
307*f0865ec9SKyle Evans
308*f0865ec9SKyle Evans /* Return an error if r = 0 or s = 0 */
309*f0865ec9SKyle Evans ret = nn_iszero(&r, &iszero); EG(ret, err);
310*f0865ec9SKyle Evans MUST_HAVE((!iszero), ret, err);
311*f0865ec9SKyle Evans ret = nn_iszero(&s, &iszero); EG(ret, err);
312*f0865ec9SKyle Evans MUST_HAVE((!iszero), ret, err);
313*f0865ec9SKyle Evans /* Check that 0 < s < q */
314*f0865ec9SKyle Evans ret = nn_cmp(&s, q, &cmp); EG(ret, err);
315*f0865ec9SKyle Evans MUST_HAVE((cmp < 0), ret, err);
316*f0865ec9SKyle Evans
317*f0865ec9SKyle Evans /* Take r modulo q */
318*f0865ec9SKyle Evans ret = nn_mod(&r, &r, q); EG(ret, err);
319*f0865ec9SKyle Evans
320*f0865ec9SKyle Evans /* Initialize internal variables */
321*f0865ec9SKyle Evans ret = nn_init(&u, 0); EG(ret, err);
322*f0865ec9SKyle Evans ret = nn_init(&pi, 0); EG(ret, err);
323*f0865ec9SKyle Evans
324*f0865ec9SKyle Evans /* NOTE: no need to use a secure exponentiation here as we only
325*f0865ec9SKyle Evans * manipulate public data.
326*f0865ec9SKyle Evans */
327*f0865ec9SKyle Evans /* Compute (y ** -r) mod (p) */
328*f0865ec9SKyle Evans ret = nn_sub(&r, q, &r); EG(ret, err); /* compute -r = (q - r) mod q */
329*f0865ec9SKyle Evans ret = _nn_mod_pow_insecure(&u, y, &r, p); EG(ret, err);
330*f0865ec9SKyle Evans /* Compute (g ** s) mod (p) */
331*f0865ec9SKyle Evans ret = _nn_mod_pow_insecure(&pi, g, &s, p); EG(ret, err);
332*f0865ec9SKyle Evans /* Compute (y ** -r) * (g ** s) mod (p) */
333*f0865ec9SKyle Evans ret = nn_mod_mul(&pi, &pi, &u, p); EG(ret, err);
334*f0865ec9SKyle Evans
335*f0865ec9SKyle Evans /* Compute r' */
336*f0865ec9SKyle Evans /* I2BS(alpha, pi)
337*f0865ec9SKyle Evans */
338*f0865ec9SKyle Evans ret = _i2osp(&pi, pi_buf, (u16)BYTECEIL(alpha)); EG(ret, err);
339*f0865ec9SKyle Evans /* r' = h(I2BS(alpha, pi) || M) */
340*f0865ec9SKyle Evans ret = gen_hash_init(&hash_ctx, sdsa_hash); EG(ret, err);
341*f0865ec9SKyle Evans ret = gen_hash_update(&hash_ctx, pi_buf, (u16)BYTECEIL(alpha), sdsa_hash); EG(ret, err);
342*f0865ec9SKyle Evans ret = gen_hash_update(&hash_ctx, msg, msglen, sdsa_hash); EG(ret, err);
343*f0865ec9SKyle Evans ret = gen_hash_final(&hash_ctx, hash, sdsa_hash); EG(ret, err);
344*f0865ec9SKyle Evans
345*f0865ec9SKyle Evans /* Check that hash values r' == r */
346*f0865ec9SKyle Evans ret = are_equal(sig, hash, hlen, &cmp); EG(ret, err);
347*f0865ec9SKyle Evans ret = (cmp != 1) ? -1 : 0;
348*f0865ec9SKyle Evans
349*f0865ec9SKyle Evans err:
350*f0865ec9SKyle Evans nn_uninit(&r);
351*f0865ec9SKyle Evans nn_uninit(&s);
352*f0865ec9SKyle Evans nn_uninit(&u);
353*f0865ec9SKyle Evans nn_uninit(&pi);
354*f0865ec9SKyle Evans
355*f0865ec9SKyle Evans PTR_NULLIFY(p);
356*f0865ec9SKyle Evans PTR_NULLIFY(q);
357*f0865ec9SKyle Evans PTR_NULLIFY(g);
358*f0865ec9SKyle Evans PTR_NULLIFY(y);
359*f0865ec9SKyle Evans
360*f0865ec9SKyle Evans return ret;
361*f0865ec9SKyle Evans }
362*f0865ec9SKyle Evans
363*f0865ec9SKyle Evans #ifdef SDSA
364*f0865ec9SKyle Evans #include <libecc/utils/print_buf.h>
main(int argc,char * argv[])365*f0865ec9SKyle Evans int main(int argc, char *argv[])
366*f0865ec9SKyle Evans {
367*f0865ec9SKyle Evans int ret = 0;
368*f0865ec9SKyle Evans
369*f0865ec9SKyle Evans /* This example is taken from ISO14888-3 SDSA (Appendix F "Numerical examples" */
370*f0865ec9SKyle Evans const u8 p[] = {
371*f0865ec9SKyle Evans 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C, 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
372*f0865ec9SKyle Evans 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1, 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
373*f0865ec9SKyle Evans 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D, 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
374*f0865ec9SKyle Evans 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72, 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
375*f0865ec9SKyle Evans 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E, 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
376*f0865ec9SKyle Evans 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1, 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
377*f0865ec9SKyle Evans 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77, 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
378*f0865ec9SKyle Evans 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83, 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
379*f0865ec9SKyle Evans 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A, 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
380*f0865ec9SKyle Evans 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30, 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
381*f0865ec9SKyle Evans 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9, 0x1E, 0x1A, 0x15, 0x97,
382*f0865ec9SKyle Evans };
383*f0865ec9SKyle Evans
384*f0865ec9SKyle Evans const u8 q[] = {
385*f0865ec9SKyle Evans 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76, 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
386*f0865ec9SKyle Evans 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3,
387*f0865ec9SKyle Evans };
388*f0865ec9SKyle Evans
389*f0865ec9SKyle Evans const u8 g[] = {
390*f0865ec9SKyle Evans 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66, 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
391*f0865ec9SKyle Evans 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50, 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
392*f0865ec9SKyle Evans 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF, 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
393*f0865ec9SKyle Evans 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31, 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
394*f0865ec9SKyle Evans 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2, 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
395*f0865ec9SKyle Evans 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2, 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
396*f0865ec9SKyle Evans 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85, 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
397*f0865ec9SKyle Evans 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2, 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
398*f0865ec9SKyle Evans 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37, 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
399*f0865ec9SKyle Evans 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84, 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
400*f0865ec9SKyle Evans 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F, 0x6C, 0xC4, 0x16, 0x59,
401*f0865ec9SKyle Evans };
402*f0865ec9SKyle Evans
403*f0865ec9SKyle Evans const u8 x[] = {
404*f0865ec9SKyle Evans 0x73, 0x01, 0x88, 0x95, 0x20, 0xD4, 0x7A, 0xA0, 0x55, 0x99, 0x5B, 0xA1, 0xD8, 0xFC, 0xD7, 0x01, 0x6E, 0xA6, 0x2E, 0x09, 0x18, 0x89, 0x2E, 0x07,
405*f0865ec9SKyle Evans 0xB7, 0xDC, 0x23, 0xAF, 0x69, 0x00, 0x6B, 0x88,
406*f0865ec9SKyle Evans };
407*f0865ec9SKyle Evans
408*f0865ec9SKyle Evans const u8 y[] = {
409*f0865ec9SKyle Evans 0x57, 0xA1, 0x72, 0x58, 0xD4, 0xA3, 0xF4, 0x7C, 0x45, 0x45, 0xAD, 0x51, 0xF3, 0x10, 0x9C, 0x5D, 0xB4, 0x1B, 0x78, 0x78, 0x79, 0xFC, 0xFE, 0x53,
410*f0865ec9SKyle Evans 0x8D, 0xC1, 0xDD, 0x5D, 0x35, 0xCE, 0x42, 0xFF, 0x3A, 0x9F, 0x22, 0x5E, 0xDE, 0x65, 0x02, 0x12, 0x64, 0x08, 0xFC, 0xB1, 0x3A, 0xEA, 0x22, 0x31,
411*f0865ec9SKyle Evans 0x80, 0xB1, 0x49, 0xC4, 0x64, 0xE1, 0x76, 0xEB, 0xF0, 0x3B, 0xA6, 0x51, 0x0D, 0x82, 0x06, 0xC9, 0x20, 0xF6, 0xB1, 0xE0, 0x93, 0x92, 0xE6, 0xC8,
412*f0865ec9SKyle Evans 0x40, 0xA0, 0x5B, 0xDB, 0x9D, 0x68, 0x75, 0xAB, 0x3F, 0x48, 0x17, 0xEC, 0x3A, 0x65, 0xA6, 0x65, 0xB7, 0x88, 0xEC, 0xBB, 0x44, 0x71, 0x88, 0xC7,
413*f0865ec9SKyle Evans 0xDF, 0x2E, 0xB4, 0xD3, 0xD9, 0x42, 0x4E, 0x57, 0xD9, 0x64, 0x39, 0x8D, 0xBE, 0x1C, 0x63, 0x62, 0x65, 0x9C, 0x6B, 0xD8, 0x55, 0xC1, 0xD3, 0xE5,
414*f0865ec9SKyle Evans 0x1D, 0x64, 0x79, 0x6C, 0xA5, 0x98, 0x48, 0x0D, 0xFD, 0xD9, 0x58, 0x0E, 0x55, 0x08, 0x53, 0x45, 0xC1, 0x5E, 0x34, 0xD6, 0xA3, 0x3A, 0x2F, 0x43,
415*f0865ec9SKyle Evans 0xE2, 0x22, 0x40, 0x7A, 0xCE, 0x05, 0x89, 0x72, 0xD3, 0x49, 0x52, 0xAE, 0x2B, 0x70, 0x5C, 0x53, 0x22, 0x43, 0xBE, 0x39, 0x4B, 0x22, 0x23, 0x29,
416*f0865ec9SKyle Evans 0x61, 0x61, 0x14, 0x5E, 0xF2, 0x92, 0x7C, 0xDB, 0xC5, 0x5B, 0xBD, 0x56, 0x4A, 0xAE, 0x8D, 0xE4, 0xBA, 0x45, 0x00, 0xA7, 0xFA, 0x43, 0x2F, 0xE7,
417*f0865ec9SKyle Evans 0x8B, 0x0F, 0x06, 0x89, 0x1E, 0x40, 0x80, 0x83, 0x7E, 0x76, 0x10, 0x57, 0xBC, 0x6C, 0xB8, 0xAC, 0x18, 0xFD, 0x43, 0x20, 0x75, 0x82, 0x03, 0x2A,
418*f0865ec9SKyle Evans 0xFB, 0x63, 0xC6, 0x24, 0xF3, 0x2E, 0x66, 0xB0, 0x5F, 0xC3, 0x1C, 0x5D, 0xFF, 0xB2, 0x5F, 0xA9, 0x2D, 0x4D, 0x00, 0xE2, 0xB0, 0xD4, 0xF7, 0x21,
419*f0865ec9SKyle Evans 0xE8, 0x8C, 0x41, 0x7D, 0x2E, 0x57, 0x79, 0x7B, 0x8F, 0x55, 0xA2, 0xFF, 0xC6, 0xEE, 0x4D, 0xDB,
420*f0865ec9SKyle Evans };
421*f0865ec9SKyle Evans
422*f0865ec9SKyle Evans const u8 msg[] = "abc";
423*f0865ec9SKyle Evans
424*f0865ec9SKyle Evans const u8 nonce[] = {
425*f0865ec9SKyle Evans 0x2B, 0x73, 0xE8, 0xFF, 0x3A, 0x7C, 0x01, 0x68, 0x6C, 0xA5, 0x56, 0xE0, 0xFA, 0xBF, 0xD7, 0x4A, 0xC8, 0xD1, 0xFD, 0xA4, 0xAD, 0x3D, 0x50, 0x3F,
426*f0865ec9SKyle Evans 0x23, 0xB8, 0xEB, 0x8A, 0xEE, 0xC6, 0x33, 0x05,
427*f0865ec9SKyle Evans };
428*f0865ec9SKyle Evans
429*f0865ec9SKyle Evans sdsa_priv_key priv;
430*f0865ec9SKyle Evans sdsa_pub_key pub;
431*f0865ec9SKyle Evans sdsa_pub_key pub2;
432*f0865ec9SKyle Evans u8 sig[32*2] = { 0 };
433*f0865ec9SKyle Evans
434*f0865ec9SKyle Evans FORCE_USED_VAR(argc);
435*f0865ec9SKyle Evans FORCE_USED_VAR(argv);
436*f0865ec9SKyle Evans
437*f0865ec9SKyle Evans /* Sanity check on size for DSA.
438*f0865ec9SKyle Evans * NOTE: the double parentheses are here to handle -Wunreachable-code
439*f0865ec9SKyle Evans */
440*f0865ec9SKyle Evans if((NN_USABLE_MAX_BIT_LEN) < (4096)){
441*f0865ec9SKyle Evans ext_printf("Error: you seem to have compiled libecc with usable NN size < 4096, not suitable for DSA.\n");
442*f0865ec9SKyle Evans ext_printf(" => Please recompile libecc with EXTRA_CFLAGS=\"-DUSER_NN_BIT_LEN=4096\"\n");
443*f0865ec9SKyle Evans ext_printf(" This will increase usable NN for proper DSA up to 4096 bits.\n");
444*f0865ec9SKyle Evans ext_printf(" Then recompile the current examples with the same EXTRA_CFLAGS=\"-DUSER_NN_BIT_LEN=4096\" flag and execute again!\n");
445*f0865ec9SKyle Evans /* NOTE: ret = 0 here to pass self tests even if the library is not compatible */
446*f0865ec9SKyle Evans ret = 0;
447*f0865ec9SKyle Evans goto err;
448*f0865ec9SKyle Evans }
449*f0865ec9SKyle Evans
450*f0865ec9SKyle Evans
451*f0865ec9SKyle Evans ret = sdsa_import_priv_key(&priv, p, sizeof(p), q, sizeof(q), g, sizeof(g), x, sizeof(x)); EG(ret, err);
452*f0865ec9SKyle Evans ret = sdsa_import_pub_key(&pub, p, sizeof(p), q, sizeof(q), g, sizeof(g), y, sizeof(y)); EG(ret, err);
453*f0865ec9SKyle Evans ret = sdsa_compute_pub_from_priv(&pub2, &priv); EG(ret, err);
454*f0865ec9SKyle Evans
455*f0865ec9SKyle Evans nn_print("y", &(pub2.y));
456*f0865ec9SKyle Evans
457*f0865ec9SKyle Evans ret = sdsa_sign(&priv, msg, sizeof(msg)-1, nonce, sizeof(nonce), sig, sizeof(sig), HASH_SHA256); EG(ret, err);
458*f0865ec9SKyle Evans
459*f0865ec9SKyle Evans buf_print("sig", sig, sizeof(sig));
460*f0865ec9SKyle Evans
461*f0865ec9SKyle Evans ret = sdsa_verify(&pub, msg, sizeof(msg)-1, sig, sizeof(sig), HASH_SHA256);
462*f0865ec9SKyle Evans ext_printf("Signature result %d\n", ret);
463*f0865ec9SKyle Evans
464*f0865ec9SKyle Evans err:
465*f0865ec9SKyle Evans return ret;
466*f0865ec9SKyle Evans }
467*f0865ec9SKyle Evans #endif
468