Path: blob/main/crypto/libecc/src/examples/sig/dsa/dsa.c
34928 views
/*1* Copyright (C) 2021 - This file is part of libecc project2*3* Authors:4* Ryad BENADJILA <[email protected]>5* Arnaud EBALARD <[email protected]>6*7* This software is licensed under a dual BSD and GPL v2 license.8* See LICENSE file at the root folder of the project.9*/10#include "dsa.h"111213/* We include the rand external dependency because we have to generate14* some random data for the nonces.15*/16#include <libecc/external_deps/rand.h>17/* We include the printf external dependency for printf output */18#include <libecc/external_deps/print.h>19/* We include our common helpers */20#include "../common/common.h"2122/*23* The purpose of this example is to implement the DSA24* related algorithms as per FIPS 186-4 based on libecc arithmetic25* primitives.26*27* XXX: Please be aware that libecc has been designed for Elliptic28* Curve cryptography, and as so the arithmetic primitives are29* not optimized for big numbers >= 1024 bits usually used for DSA.30* Additionnaly, a hard limit of our NN values makes it impossible31* to exceed ~5300 bits in the best case (words of size 64 bits).32*33* All in all, please see this as a proof of concept of implementing34* FIPS 186-4 rather than a production code. Use it at your own risk!35*36* !! DISCLAIMER !!37* ================38*39* Althoug some efforts have been made to secure this implementation40* of DSA (e.g. by protecting the private key and nonces using constant41* time and blinding WHEN activated with BLINDING=1), please consider this42* code as a proof of concept and use it at your own risk.43*44* All-in-all, this piece of code can be useful in some contexts, or risky to45* use in other sensitive ones where advanced side-channels or fault attacks46* have to be considered. Use this DSA code knowingly and at your own risk!47*48*/495051/* Import a DSA private key from buffers */52int dsa_import_priv_key(dsa_priv_key *priv, const u8 *p, u16 plen,53const u8 *q, u16 qlen,54const u8 *g, u16 glen,55const u8 *x, u16 xlen)56{57int ret, cmp;5859/* Sanity checks */60MUST_HAVE((priv != NULL), ret, err);61MUST_HAVE((p != NULL) && (q != NULL) && (g != NULL) && (x != NULL), ret, err);6263/* Import our big numbers */64ret = _os2ip(&(priv->p), p, plen); EG(ret, err);65ret = _os2ip(&(priv->q), q, qlen); EG(ret, err);66ret = _os2ip(&(priv->g), g, glen); EG(ret, err);67ret = _os2ip(&(priv->x), x, xlen); EG(ret, err);6869/* Sanity check that q < p */70ret = nn_cmp(&(priv->q), &(priv->p), &cmp); EG(ret, err);71MUST_HAVE((cmp < 0), ret, err);72/* Sanity check that g < p */73ret = nn_cmp(&(priv->g), &(priv->p), &cmp); EG(ret, err);74MUST_HAVE((cmp < 0), ret, err);75/* Sanity check that x < q */76ret = nn_cmp(&(priv->x), &(priv->q), &cmp); EG(ret, err);77MUST_HAVE((cmp < 0), ret, err);7879err:80if(ret && (priv != NULL)){81IGNORE_RET_VAL(local_memset(priv, 0, sizeof(dsa_priv_key)));82}8384return ret;85}8687/* Import a DSA public key from buffers */88int dsa_import_pub_key(dsa_pub_key *pub, const u8 *p, u16 plen,89const u8 *q, u16 qlen,90const u8 *g, u16 glen,91const u8 *y, u16 ylen)92{93int ret, cmp;9495/* Sanity checks */96MUST_HAVE((pub != NULL), ret, err);97MUST_HAVE((p != NULL) && (q != NULL) && (g != NULL) && (y != NULL), ret, err);9899/* Import our big numbers */100ret = _os2ip(&(pub->p), p, plen); EG(ret, err);101ret = _os2ip(&(pub->q), q, qlen); EG(ret, err);102ret = _os2ip(&(pub->g), g, glen); EG(ret, err);103ret = _os2ip(&(pub->y), y, ylen); EG(ret, err);104105/* Sanity check that q < p */106ret = nn_cmp(&(pub->q), &(pub->p), &cmp); EG(ret, err);107MUST_HAVE((cmp < 0), ret, err);108/* Sanity check that g < p */109ret = nn_cmp(&(pub->g), &(pub->p), &cmp); EG(ret, err);110MUST_HAVE((cmp < 0), ret, err);111/* Sanity check that y < p */112ret = nn_cmp(&(pub->y), &(pub->p), &cmp); EG(ret, err);113MUST_HAVE((cmp < 0), ret, err);114115err:116if(ret && (pub != NULL)){117IGNORE_RET_VAL(local_memset(pub, 0, sizeof(dsa_pub_key)));118}119120return ret;121}122123124125/* Compute a DSA public key from a private key.126* The public key is computed using modular exponentiation of the generator127* with the private key.128*/129int dsa_compute_pub_from_priv(dsa_pub_key *pub, const dsa_priv_key *priv)130{131int ret, cmp;132nn_src_t p, q, g, x;133nn x_;134x_.magic = WORD(0);135136MUST_HAVE((pub != NULL) && (priv != NULL), ret, err);137138/* Make things more readable */139p = &(priv->p);140q = &(priv->q);141g = &(priv->g);142x = &(priv->x);143144/* Sanity checks */145ret = nn_check_initialized(p); EG(ret, err);146ret = nn_check_initialized(q); EG(ret, err);147ret = nn_check_initialized(g); EG(ret, err);148ret = nn_check_initialized(x); EG(ret, err);149150/* Sanity check that q < p */151ret = nn_cmp(&(priv->q), &(priv->p), &cmp); EG(ret, err);152MUST_HAVE((cmp < 0), ret, err);153/* Sanity check that g < p */154ret = nn_cmp(&(priv->g), &(priv->p), &cmp); EG(ret, err);155MUST_HAVE((cmp < 0), ret, err);156/* Sanity check that x < q */157ret = nn_cmp(&(priv->x), &(priv->q), &cmp); EG(ret, err);158MUST_HAVE((cmp < 0), ret, err);159160ret = nn_init(&x_, 0); EG(ret, err);161/* Blind the private key in all cases as this is a162* sensitive value.163*/164ret = _blind_scalar(x, q, &x_); EG(ret, err);165ret = _fix_scalar_msb(&x_, q, &x_); EG(ret, err);166167/* Perform the exponentiation y = g**x mod (p) */168ret = nn_mod_pow(&(pub->y), g, &x_, p); EG(ret, err);169170/* Initialize the public key */171ret = nn_copy(&(pub->p), p); EG(ret, err);172ret = nn_copy(&(pub->q), q); EG(ret, err);173ret = nn_copy(&(pub->g), g);174175err:176if(ret && (pub != NULL)){177IGNORE_RET_VAL(local_memset(pub, 0, sizeof(dsa_pub_key)));178}179180nn_uninit(&x_);181182PTR_NULLIFY(p);183PTR_NULLIFY(q);184PTR_NULLIFY(g);185PTR_NULLIFY(x);186187return ret;188}189190/* Generate a DSA signature191* This implements "DSA Signature Generation" in section 4.6 of FIPS 186-4.192*/193int dsa_sign(const dsa_priv_key *priv, const u8 *msg, u32 msglen,194const u8 *nonce, u16 noncelen,195u8 *sig, u16 siglen, gen_hash_alg_type dsa_hash)196{197int ret, iszero;198/* N is the bit length of q */199bitcnt_t N, rshift;200/* Length of the hash function */201u8 hlen, block_size;202nn_src_t p, q, g, x;203/* The nonce, it inverse and its protected version */204nn k, kinv, k_;205/* r, s and z */206nn r, s, z;207/* Hash */208u8 hash[MAX_DIGEST_SIZE];209#ifdef USE_SIG_BLINDING210/* b is the blinding mask */211nn b;212b.magic = WORD(0);213#endif /* USE_SIG_BLINDING */214k.magic = kinv.magic = k_.magic = r.magic = s.magic = z.magic = WORD(0);215216/* Sanity checks */217MUST_HAVE((priv != NULL) && (msg != NULL) && (sig != NULL), ret, err);218219ret = local_memset(hash, 0, sizeof(hash)); EG(ret, err);220221/* Make things more readable */222p = &(priv->p);223q = &(priv->q);224g = &(priv->g);225x = &(priv->x);226227/* Sanity checks */228ret = nn_check_initialized(p); EG(ret, err);229ret = nn_check_initialized(q); EG(ret, err);230ret = nn_check_initialized(g); EG(ret, err);231ret = nn_check_initialized(x); EG(ret, err);232233/* Let N be the bit length of q. Let min(N, outlen) denote the minimum234* of the positive integers N and outlen, where outlen is the bit length235* of the hash function output block.236*/237ret = nn_bitlen(q, &N); EG(ret, err);238ret = gen_hash_get_hash_sizes(dsa_hash, &hlen, &block_size); EG(ret, err);239MUST_HAVE((hlen <= MAX_DIGEST_SIZE), ret, err);240241/* Sanity check on the signature length */242MUST_HAVE((siglen == (2 * BYTECEIL(N))), ret, err);243244restart:245/* If the nonce is imposed, use it. Else get a random modulo q */246if(nonce != NULL){247ret = _os2ip(&k, nonce, noncelen); EG(ret, err);248}249else{250ret = nn_get_random_mod(&k, q); EG(ret, err);251}252253/* Fix the MSB of our scalar */254ret = nn_copy(&k_, &k); EG(ret, err);255#ifdef USE_SIG_BLINDING256/* Blind the scalar */257ret = _blind_scalar(&k_, q, &k_); EG(ret, err);258#endif /* USE_SIG_BLINDING */259ret = _fix_scalar_msb(&k_, q, &k_); EG(ret, err);260/* r = (g**k mod p) mod q */261ret = nn_init(&r, 0); EG(ret, err);262/* Exponentiation modulo p */263ret = nn_mod_pow(&r, g, &k_, p); EG(ret, err);264/* Modulo q */265ret = nn_mod(&r, &r, q); EG(ret, err);266267/* If r is 0, restart the process */268ret = nn_iszero(&r, &iszero); EG(ret, err);269if (iszero) {270goto restart;271}272273/* Export r */274ret = _i2osp(&r, sig, (siglen / 2)); EG(ret, err);275276/* Compute the hash */277ret = gen_hash_hfunc(msg, msglen, hash, dsa_hash); EG(ret, err);278/* z = the leftmost min(N, outlen) bits of Hash(M) */279rshift = 0;280if ((hlen * 8) > N) {281rshift = (bitcnt_t)((hlen * 8) - N);282}283ret = _os2ip(&z, hash, hlen); EG(ret, err);284if (rshift) {285ret = nn_rshift_fixedlen(&z, &z, rshift); EG(ret, err);286}287ret = nn_mod(&z, &z, q); EG(ret, err);288289#ifdef USE_SIG_BLINDING290/* Note: if we use blinding, r and e are multiplied by291* a random value b in ]0,q[ */292ret = nn_get_random_mod(&b, q); EG(ret, err);293/* Blind r with b */294ret = nn_mod_mul(&r, &r, &b, q); EG(ret, err);295/* Blind the message z */296ret = nn_mod_mul(&z, &z, &b, q); EG(ret, err);297/*298* In case of blinding, we compute (b*k)^-1, and b^-1 will299* automatically unblind (r*x) in the following.300*/301ret = nn_mod_mul(&k, &k, &b, q); EG(ret, err);302#endif /* USE_SIG_BLINDING */303304/* Compute s = k^-1 * (xr + z) mod q */305/* Compute k^-1 mod q */306/* NOTE: we use Fermat's little theorem inversion for307* constant time here. This is possible since q is prime.308*/309ret = nn_modinv_fermat(&kinv, &k, q); EG(ret, err);310ret = nn_mod_mul(&s, &r, x, q); EG(ret, err);311ret = nn_mod_add(&s, &s, &z, q); EG(ret, err);312ret = nn_mod_mul(&s, &kinv, &s, q); EG(ret, err);313314/* If s is 0, restart the process */315ret = nn_iszero(&s, &iszero); EG(ret, err);316if (iszero) {317goto restart;318}319320/* Export s */321ret = _i2osp(&s, sig + (siglen / 2), (siglen / 2));322323err:324if(ret && (sig != NULL)){325IGNORE_RET_VAL(local_memset(sig, 0, siglen));326}327328nn_uninit(&k);329nn_uninit(&kinv);330nn_uninit(&k_);331#ifdef USE_SIG_BLINDING332nn_uninit(&b);333#endif334nn_uninit(&r);335nn_uninit(&s);336nn_uninit(&z);337338PTR_NULLIFY(p);339PTR_NULLIFY(q);340PTR_NULLIFY(g);341PTR_NULLIFY(x);342343return ret;344}345346347348/* Verify a DSA signature349* This implements "DSA Signature Verification and Validation" in section 4.7 of FIPS 186-4.350*/351int dsa_verify(const dsa_pub_key *pub, const u8 *msg, u32 msglen,352const u8 *sig, u16 siglen, gen_hash_alg_type dsa_hash)353{354int ret, iszero, cmp;355/* N is the bit length of q */356bitcnt_t N, rshift;357/* Length of the hash function */358u8 hlen, block_size;359nn_src_t p, q, g, y;360/* r, s */361nn r, s, z;362/* u1, u2, and v */363nn u1, u2, v;364/* Hash */365u8 hash[MAX_DIGEST_SIZE];366r.magic = s.magic = z.magic = u1.magic = u2.magic = WORD(0);367368/* Sanity checks */369MUST_HAVE((pub != NULL) && (msg != NULL) && (sig != NULL), ret, err);370371ret = local_memset(hash, 0, sizeof(hash)); EG(ret, err);372373/* Make things more readable */374p = &(pub->p);375q = &(pub->q);376g = &(pub->g);377y = &(pub->y);378379/* Sanity checks */380ret = nn_check_initialized(p); EG(ret, err);381ret = nn_check_initialized(q); EG(ret, err);382ret = nn_check_initialized(g); EG(ret, err);383ret = nn_check_initialized(y); EG(ret, err);384385/* Sanity check on the signature length */386ret = nn_bitlen(q, &N); EG(ret, err);387MUST_HAVE((siglen == (2 * BYTECEIL(N))), ret, err);388389/* Extract r and s */390ret = _os2ip(&r, sig, (siglen / 2)); EG(ret, err);391ret = _os2ip(&s, sig + (siglen / 2), (siglen / 2)); EG(ret, err);392393/* Return an error if r = 0 or s = 0 */394ret = nn_iszero(&r, &iszero); EG(ret, err);395MUST_HAVE((!iszero), ret, err);396ret = nn_iszero(&s, &iszero); EG(ret, err);397MUST_HAVE((!iszero), ret, err);398/* Check thatt 0 < r′ < q and 0 < s′ < q */399ret = nn_cmp(&r, q, &cmp); EG(ret, err);400MUST_HAVE((cmp < 0), ret, err);401ret = nn_cmp(&s, q, &cmp); EG(ret, err);402MUST_HAVE((cmp < 0), ret, err);403404/* Compute the hash */405ret = gen_hash_get_hash_sizes(dsa_hash, &hlen, &block_size); EG(ret, err);406MUST_HAVE((hlen <= MAX_DIGEST_SIZE), ret, err);407ret = gen_hash_hfunc(msg, msglen, hash, dsa_hash); EG(ret, err);408/* z = the leftmost min(N, outlen) bits of Hash(M) */409rshift = 0;410if ((hlen * 8) > N) {411rshift = (bitcnt_t)((hlen * 8) - N);412}413ret = _os2ip(&z, hash, hlen); EG(ret, err);414if (rshift) {415ret = nn_rshift_fixedlen(&z, &z, rshift); EG(ret, err);416}417ret = nn_mod(&z, &z, q); EG(ret, err);418419/* Compute w = s**-1 mod (q) in s */420ret = nn_modinv(&s, &s, q); EG(ret, err);421422/* u1 = (zw) mod q */423ret = nn_mod_mul(&u1, &z, &s, q); EG(ret, err);424/* u2 = ((r′)w) mod q */425ret = nn_mod_mul(&u2, &r, &s, q); EG(ret, err);426/* Now compute v = = ((g**u1 y**u2) mod p) mod q */427/* NOTE: no need to use a secure exponentiation here as we only428* manipulate public data.429*/430ret = _nn_mod_pow_insecure(&v, g, &u1, p); EG(ret, err);431ret = _nn_mod_pow_insecure(&s, y, &u2, p); EG(ret, err);432ret = nn_mod_mul(&v, &v, &s, p); EG(ret, err);433ret = nn_mod(&v, &v, q); EG(ret, err);434435/* Check that v = r */436ret = nn_cmp(&v, &r, &cmp); EG(ret, err);437ret = (cmp != 0) ? -1 : 0;438439err:440nn_uninit(&r);441nn_uninit(&s);442nn_uninit(&z);443nn_uninit(&u1);444nn_uninit(&u2);445nn_uninit(&v);446447PTR_NULLIFY(p);448PTR_NULLIFY(q);449PTR_NULLIFY(g);450PTR_NULLIFY(y);451452return ret;453}454455#ifdef DSA456#include <libecc/utils/print_buf.h>457int main(int argc, char *argv[])458{459int ret = 0;460461const u8 p[] = {4620x90, 0x06, 0x64, 0x55, 0xB5, 0xCF, 0xC3, 0x8F, 0x9C, 0xAA, 0x4A, 0x48, 0xB4, 0x28, 0x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 0xC4, 0x16, 0xB4, 0xB2, 0xCB, 0x73,476};477478const u8 q[] = {4790xCF, 0xA0, 0x47, 0x8A, 0x54, 0x71, 0x7B, 0x08, 0xCE, 0x64, 0x80, 0x5B, 0x76, 0xE5, 0xB1, 0x42, 0x49, 0xA7, 0x7A, 0x48, 0x38, 0x46, 0x9D, 0xF7, 0xF7, 0xDC, 0x98, 0x7E,4800xFC, 0xCF, 0xB1, 0x1D,481};482483const u8 g[] = {4840x5E, 0x5C, 0xBA, 0x99, 0x2E, 0x0A, 0x68, 0x0D, 0x88, 0x5E, 0xB9, 0x03, 0xAE, 0xA7, 0x8E, 0x4A, 0x45, 0xA4, 0x69, 0x10, 0x3D, 0x44, 0x8E, 0xDE, 0x3B, 0x7A, 0xCC, 0xC5,4850x4D, 0x52, 0x1E, 0x37, 0xF8, 0x4A, 0x4B, 0xDD, 0x5B, 0x06, 0xB0, 0x97, 0x0C, 0xC2, 0xD2, 0xBB, 0xB7, 0x15, 0xF7, 0xB8, 0x28, 0x46, 0xF9, 0xA0, 0xC3, 0x93, 0x91, 0x4C,4860x79, 0x2E, 0x6A, 0x92, 0x3E, 0x21, 0x17, 0xAB, 0x80, 0x52, 0x76, 0xA9, 0x75, 0xAA, 0xDB, 0x52, 0x61, 0xD9, 0x16, 0x73, 0xEA, 0x9A, 0xAF, 0xFE, 0xEC, 0xBF, 0xA6, 0x18,4870x3D, 0xFC, 0xB5, 0xD3, 0xB7, 0x33, 0x2A, 0xA1, 0x92, 0x75, 0xAF, 0xA1, 0xF8, 0xEC, 0x0B, 0x60, 0xFB, 0x6F, 0x66, 0xCC, 0x23, 0xAE, 0x48, 0x70, 0x79, 0x1D, 0x59, 0x82,4880xAA, 0xD1, 0xAA, 0x94, 0x85, 0xFD, 0x8F, 0x4A, 0x60, 0x12, 0x6F, 0xEB, 0x2C, 0xF0, 0x5D, 0xB8, 0xA7, 0xF0, 0xF0, 0x9B, 0x33, 0x97, 0xF3, 0x93, 0x7F, 0x2E, 0x90,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, 0xB6, 0x3A, 0x6D, 0x7E, 0xD0, 0x74, 0x7E, 0xC5, 0x9E, 0x0E, 0x0A, 0x23, 0xCE, 0x7D, 0x8A,4960x74, 0xC1, 0xD2, 0xC2, 0xA7, 0xAF, 0xB6, 0xA2, 0x97, 0x99, 0x62, 0x0F, 0x00, 0xE1, 0x1C, 0x33, 0x78, 0x7F, 0x7D, 0xED, 0x3B, 0x30, 0xE1, 0xA2, 0x2D, 0x09, 0xF1, 0xFB,4970xDA, 0x1A, 0xBB, 0xBF, 0xBF, 0x25, 0xCA, 0xE0, 0x5A, 0x13, 0xF8, 0x12, 0xE3, 0x45, 0x63, 0xF9, 0x94, 0x10, 0xE7, 0x3B,498};499500const u8 x[] = {5010x3A, 0xBC, 0x15, 0x87, 0x29, 0x7C, 0xE7, 0xB9, 0xEA, 0x1A, 0xD6, 0x65, 0x1C, 0xF2, 0xBC, 0x4D, 0x7F, 0x92, 0xED, 0x25, 0xCA, 0xBC, 0x85, 0x53, 0xF5, 0x67, 0xD1, 0xB4,5020x0E, 0xBB, 0x87, 0x64,503};504505const u8 y[] = {5060x8b, 0x89, 0x1c, 0x86, 0x92, 0xd3, 0xde, 0x87, 0x58, 0x79, 0x39, 0x0f, 0x26, 0x98, 0xb2, 0x6f, 0xbe, 0xcc, 0xa6, 0xb0, 0x75, 0x53, 0x5d, 0xce, 0x6b, 0x0c, 0x86, 0x25, 0x77, 0xf9, 0xfa, 0x0d, 0xef, 0x60, 0x74, 0xe7, 0xa7, 0x62, 0x41, 0x21, 0x22, 0x4a, 0x59, 0x58, 0x96, 0xab, 0xd4, 0xcd, 0xa5, 0x6b, 0x2c, 0xef, 0xb9, 0x42, 0xe0, 0x25, 0xd2, 0xa4, 0x28, 0x2f, 0xfa, 0xa9, 0x8a, 0x48, 0xcd, 0xb4, 0x7e, 0x1a, 0x6f, 0xcb, 0x5c, 0xfb, 0x39, 0x3e, 0xf3, 0x5a, 0xf9, 0xdf, 0x91, 0x31, 0x02, 0xbb, 0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 0x24, 0x04, 0xdb, 0x88, 0x3d, 0x21, 0x5f, 0x4e, 0x06, 0x76, 0xcf, 0x54, 0x93, 0x95, 0x0a, 0xc5, 0x59, 0x16, 0x97, 0xbf, 0xea, 0x8d, 0x1e, 0xe6, 0xec, 0x01, 0x6b, 0x89, 0xba, 0x51, 0xca, 0xfb, 0x5f, 0x9c, 0x84, 0xc9, 0x89, 0xfa, 0x11, 0x73, 0x75, 0xe9, 0x45, 0x78, 0xf2, 0x8b, 0xe0, 0xb3, 0x4c, 0xe0, 0x54, 0x5d, 0xa4, 0x62, 0x66, 0xfd, 0x77, 0xf6, 0x2d, 0x8f, 0x2c, 0xee, 0x92, 0xab, 0x77, 0x01, 0x2a, 0xfe, 0xbc, 0x11, 0x00, 0x89, 0x85, 0xa8, 0x21, 0xcd, 0x2d, 0x97, 0x8c, 0x7e, 0x6f, 0xe7, 0x49, 0x9d, 0x1a, 0xaf, 0x8d, 0xe6, 0x32, 0xc2, 0x1b, 0xb4, 0x8c, 0xa5, 0xcb, 0xf9, 0xf3, 0x10, 0x98, 0xfd, 0x3f, 0xd3, 0x85, 0x4c, 0x49, 0xa6, 0x5d, 0x92, 0x01, 0x74, 0x4a, 0xac, 0xe5, 0x40, 0x35, 0x49, 0x74, 0xf9,507};508509const u8 msg[] = "abc";510511const u8 nonce[] = {5120xA6, 0x90, 0x2C, 0x1E, 0x6E, 0x39, 0x43, 0xC5, 0x62, 0x80, 0x61, 0x58, 0x8A, 0x8B, 0x00, 0x7B, 0xCC, 0xEA, 0x91, 0xDB, 0xF1, 0x29, 0x15, 0x48, 0x3F, 0x04, 0xB2, 0x4A,5130xB0, 0x67, 0x8B, 0xEE,514};515516dsa_priv_key priv;517dsa_pub_key pub;518dsa_pub_key pub2;519u8 sig[32*2] = { 0 };520521FORCE_USED_VAR(argc);522FORCE_USED_VAR(argv);523524/* Sanity check on size for DSA.525* NOTE: the double parentheses are here to handle -Wunreachable-code526*/527if((NN_USABLE_MAX_BIT_LEN) < (4096)){528ext_printf("Error: you seem to have compiled libecc with usable NN size < 4096, not suitable for DSA.\n");529ext_printf(" => Please recompile libecc with EXTRA_CFLAGS=\"-DUSER_NN_BIT_LEN=4096\"\n");530ext_printf(" This will increase usable NN for proper DSA up to 4096 bits.\n");531ext_printf(" Then recompile the current examples with the same EXTRA_CFLAGS=\"-DUSER_NN_BIT_LEN=4096\" flag and execute again!\n");532/* NOTE: ret = 0 here to pass self tests even if the library is not compatible */533ret = 0;534goto err;535}536537538ret = dsa_import_priv_key(&priv, p, sizeof(p), q, sizeof(q), g, sizeof(g), x, sizeof(x)); EG(ret, err);539ret = dsa_import_pub_key(&pub, p, sizeof(p), q, sizeof(q), g, sizeof(g), y, sizeof(y)); EG(ret, err);540ret = dsa_compute_pub_from_priv(&pub2, &priv); EG(ret, err);541542nn_print("y", &(pub2.y));543544ret = dsa_sign(&priv, msg, sizeof(msg)-1, nonce, sizeof(nonce), sig, sizeof(sig), HASH_SHA256); EG(ret, err);545546buf_print("sig", sig, sizeof(sig));547548ret = dsa_verify(&pub, msg, sizeof(msg)-1, sig, sizeof(sig), HASH_SHA256);549ext_printf("Signature result %d\n", ret);550551err:552return ret;553}554#endif555556557