/* * Copyright (C) 2021 - This file is part of libecc project * * Authors: * Ryad BENADJILA * Arnaud EBALARD * * This software is licensed under a dual BSD and GPL v2 license. * See LICENSE file at the root folder of the project. */ #include "dsa.h" /* We include the rand external dependency because we have to generate * some random data for the nonces. */ #include /* We include the printf external dependency for printf output */ #include /* We include our common helpers */ #include "../common/common.h" /* * The purpose of this example is to implement the DSA * related algorithms as per FIPS 186-4 based on libecc arithmetic * primitives. * * XXX: Please be aware that libecc has been designed for Elliptic * Curve cryptography, and as so the arithmetic primitives are * not optimized for big numbers >= 1024 bits usually used for DSA. * Additionnaly, a hard limit of our NN values makes it impossible * to exceed ~5300 bits in the best case (words of size 64 bits). * * All in all, please see this as a proof of concept of implementing * FIPS 186-4 rather than a production code. Use it at your own risk! * * !! DISCLAIMER !! * ================ * * Althoug some efforts have been made to secure this implementation * of DSA (e.g. by protecting the private key and nonces using constant * time and blinding WHEN activated with BLINDING=1), please consider this * code as a proof of concept and use it at your own risk. * * All-in-all, this piece of code can be useful in some contexts, or risky to * use in other sensitive ones where advanced side-channels or fault attacks * have to be considered. Use this DSA code knowingly and at your own risk! * */ /* Import a DSA private key from buffers */ int dsa_import_priv_key(dsa_priv_key *priv, const u8 *p, u16 plen, const u8 *q, u16 qlen, const u8 *g, u16 glen, const u8 *x, u16 xlen) { int ret, cmp; /* Sanity checks */ MUST_HAVE((priv != NULL), ret, err); MUST_HAVE((p != NULL) && (q != NULL) && (g != NULL) && (x != NULL), ret, err); /* Import our big numbers */ ret = _os2ip(&(priv->p), p, plen); EG(ret, err); ret = _os2ip(&(priv->q), q, qlen); EG(ret, err); ret = _os2ip(&(priv->g), g, glen); EG(ret, err); ret = _os2ip(&(priv->x), x, xlen); EG(ret, err); /* Sanity check that q < p */ ret = nn_cmp(&(priv->q), &(priv->p), &cmp); EG(ret, err); MUST_HAVE((cmp < 0), ret, err); /* Sanity check that g < p */ ret = nn_cmp(&(priv->g), &(priv->p), &cmp); EG(ret, err); MUST_HAVE((cmp < 0), ret, err); /* Sanity check that x < q */ ret = nn_cmp(&(priv->x), &(priv->q), &cmp); EG(ret, err); MUST_HAVE((cmp < 0), ret, err); err: if(ret && (priv != NULL)){ IGNORE_RET_VAL(local_memset(priv, 0, sizeof(dsa_priv_key))); } return ret; } /* Import a DSA public key from buffers */ int dsa_import_pub_key(dsa_pub_key *pub, const u8 *p, u16 plen, const u8 *q, u16 qlen, const u8 *g, u16 glen, const u8 *y, u16 ylen) { int ret, cmp; /* Sanity checks */ MUST_HAVE((pub != NULL), ret, err); MUST_HAVE((p != NULL) && (q != NULL) && (g != NULL) && (y != NULL), ret, err); /* Import our big numbers */ ret = _os2ip(&(pub->p), p, plen); EG(ret, err); ret = _os2ip(&(pub->q), q, qlen); EG(ret, err); ret = _os2ip(&(pub->g), g, glen); EG(ret, err); ret = _os2ip(&(pub->y), y, ylen); EG(ret, err); /* Sanity check that q < p */ ret = nn_cmp(&(pub->q), &(pub->p), &cmp); EG(ret, err); MUST_HAVE((cmp < 0), ret, err); /* Sanity check that g < p */ ret = nn_cmp(&(pub->g), &(pub->p), &cmp); EG(ret, err); MUST_HAVE((cmp < 0), ret, err); /* Sanity check that y < p */ ret = nn_cmp(&(pub->y), &(pub->p), &cmp); EG(ret, err); MUST_HAVE((cmp < 0), ret, err); err: if(ret && (pub != NULL)){ IGNORE_RET_VAL(local_memset(pub, 0, sizeof(dsa_pub_key))); } return ret; } /* Compute a DSA public key from a private key. * The public key is computed using modular exponentiation of the generator * with the private key. */ int dsa_compute_pub_from_priv(dsa_pub_key *pub, const dsa_priv_key *priv) { int ret, cmp; nn_src_t p, q, g, x; nn x_; x_.magic = WORD(0); MUST_HAVE((pub != NULL) && (priv != NULL), ret, err); /* Make things more readable */ p = &(priv->p); q = &(priv->q); g = &(priv->g); x = &(priv->x); /* Sanity checks */ ret = nn_check_initialized(p); EG(ret, err); ret = nn_check_initialized(q); EG(ret, err); ret = nn_check_initialized(g); EG(ret, err); ret = nn_check_initialized(x); EG(ret, err); /* Sanity check that q < p */ ret = nn_cmp(&(priv->q), &(priv->p), &cmp); EG(ret, err); MUST_HAVE((cmp < 0), ret, err); /* Sanity check that g < p */ ret = nn_cmp(&(priv->g), &(priv->p), &cmp); EG(ret, err); MUST_HAVE((cmp < 0), ret, err); /* Sanity check that x < q */ ret = nn_cmp(&(priv->x), &(priv->q), &cmp); EG(ret, err); MUST_HAVE((cmp < 0), ret, err); ret = nn_init(&x_, 0); EG(ret, err); /* Blind the private key in all cases as this is a * sensitive value. */ ret = _blind_scalar(x, q, &x_); EG(ret, err); ret = _fix_scalar_msb(&x_, q, &x_); EG(ret, err); /* Perform the exponentiation y = g**x mod (p) */ ret = nn_mod_pow(&(pub->y), g, &x_, p); EG(ret, err); /* Initialize the public key */ ret = nn_copy(&(pub->p), p); EG(ret, err); ret = nn_copy(&(pub->q), q); EG(ret, err); ret = nn_copy(&(pub->g), g); err: if(ret && (pub != NULL)){ IGNORE_RET_VAL(local_memset(pub, 0, sizeof(dsa_pub_key))); } nn_uninit(&x_); PTR_NULLIFY(p); PTR_NULLIFY(q); PTR_NULLIFY(g); PTR_NULLIFY(x); return ret; } /* Generate a DSA signature * This implements "DSA Signature Generation" in section 4.6 of FIPS 186-4. */ int dsa_sign(const dsa_priv_key *priv, const u8 *msg, u32 msglen, const u8 *nonce, u16 noncelen, u8 *sig, u16 siglen, gen_hash_alg_type dsa_hash) { int ret, iszero; /* N is the bit length of q */ bitcnt_t N, rshift; /* Length of the hash function */ u8 hlen, block_size; nn_src_t p, q, g, x; /* The nonce, it inverse and its protected version */ nn k, kinv, k_; /* r, s and z */ nn r, s, z; /* Hash */ u8 hash[MAX_DIGEST_SIZE]; #ifdef USE_SIG_BLINDING /* b is the blinding mask */ nn b; b.magic = WORD(0); #endif /* USE_SIG_BLINDING */ k.magic = kinv.magic = k_.magic = r.magic = s.magic = z.magic = WORD(0); /* Sanity checks */ MUST_HAVE((priv != NULL) && (msg != NULL) && (sig != NULL), ret, err); ret = local_memset(hash, 0, sizeof(hash)); EG(ret, err); /* Make things more readable */ p = &(priv->p); q = &(priv->q); g = &(priv->g); x = &(priv->x); /* Sanity checks */ ret = nn_check_initialized(p); EG(ret, err); ret = nn_check_initialized(q); EG(ret, err); ret = nn_check_initialized(g); EG(ret, err); ret = nn_check_initialized(x); EG(ret, err); /* Let N be the bit length of q. Let min(N, outlen) denote the minimum * of the positive integers N and outlen, where outlen is the bit length * of the hash function output block. */ ret = nn_bitlen(q, &N); EG(ret, err); ret = gen_hash_get_hash_sizes(dsa_hash, &hlen, &block_size); EG(ret, err); MUST_HAVE((hlen <= MAX_DIGEST_SIZE), ret, err); /* Sanity check on the signature length */ MUST_HAVE((siglen == (2 * BYTECEIL(N))), ret, err); restart: /* If the nonce is imposed, use it. Else get a random modulo q */ if(nonce != NULL){ ret = _os2ip(&k, nonce, noncelen); EG(ret, err); } else{ ret = nn_get_random_mod(&k, q); EG(ret, err); } /* Fix the MSB of our scalar */ ret = nn_copy(&k_, &k); EG(ret, err); #ifdef USE_SIG_BLINDING /* Blind the scalar */ ret = _blind_scalar(&k_, q, &k_); EG(ret, err); #endif /* USE_SIG_BLINDING */ ret = _fix_scalar_msb(&k_, q, &k_); EG(ret, err); /* r = (g**k mod p) mod q */ ret = nn_init(&r, 0); EG(ret, err); /* Exponentiation modulo p */ ret = nn_mod_pow(&r, g, &k_, p); EG(ret, err); /* Modulo q */ ret = nn_mod(&r, &r, q); EG(ret, err); /* If r is 0, restart the process */ ret = nn_iszero(&r, &iszero); EG(ret, err); if (iszero) { goto restart; } /* Export r */ ret = _i2osp(&r, sig, (siglen / 2)); EG(ret, err); /* Compute the hash */ ret = gen_hash_hfunc(msg, msglen, hash, dsa_hash); EG(ret, err); /* z = the leftmost min(N, outlen) bits of Hash(M) */ rshift = 0; if ((hlen * 8) > N) { rshift = (bitcnt_t)((hlen * 8) - N); } ret = _os2ip(&z, hash, hlen); EG(ret, err); if (rshift) { ret = nn_rshift_fixedlen(&z, &z, rshift); EG(ret, err); } ret = nn_mod(&z, &z, q); EG(ret, err); #ifdef USE_SIG_BLINDING /* Note: if we use blinding, r and e are multiplied by * a random value b in ]0,q[ */ ret = nn_get_random_mod(&b, q); EG(ret, err); /* Blind r with b */ ret = nn_mod_mul(&r, &r, &b, q); EG(ret, err); /* Blind the message z */ ret = nn_mod_mul(&z, &z, &b, q); EG(ret, err); /* * In case of blinding, we compute (b*k)^-1, and b^-1 will * automatically unblind (r*x) in the following. */ ret = nn_mod_mul(&k, &k, &b, q); EG(ret, err); #endif /* USE_SIG_BLINDING */ /* Compute s = k^-1 * (xr + z) mod q */ /* Compute k^-1 mod q */ /* NOTE: we use Fermat's little theorem inversion for * constant time here. This is possible since q is prime. */ ret = nn_modinv_fermat(&kinv, &k, q); EG(ret, err); ret = nn_mod_mul(&s, &r, x, q); EG(ret, err); ret = nn_mod_add(&s, &s, &z, q); EG(ret, err); ret = nn_mod_mul(&s, &kinv, &s, q); EG(ret, err); /* If s is 0, restart the process */ ret = nn_iszero(&s, &iszero); EG(ret, err); if (iszero) { goto restart; } /* Export s */ ret = _i2osp(&s, sig + (siglen / 2), (siglen / 2)); err: if(ret && (sig != NULL)){ IGNORE_RET_VAL(local_memset(sig, 0, siglen)); } nn_uninit(&k); nn_uninit(&kinv); nn_uninit(&k_); #ifdef USE_SIG_BLINDING nn_uninit(&b); #endif nn_uninit(&r); nn_uninit(&s); nn_uninit(&z); PTR_NULLIFY(p); PTR_NULLIFY(q); PTR_NULLIFY(g); PTR_NULLIFY(x); return ret; } /* Verify a DSA signature * This implements "DSA Signature Verification and Validation" in section 4.7 of FIPS 186-4. */ int dsa_verify(const dsa_pub_key *pub, const u8 *msg, u32 msglen, const u8 *sig, u16 siglen, gen_hash_alg_type dsa_hash) { int ret, iszero, cmp; /* N is the bit length of q */ bitcnt_t N, rshift; /* Length of the hash function */ u8 hlen, block_size; nn_src_t p, q, g, y; /* r, s */ nn r, s, z; /* u1, u2, and v */ nn u1, u2, v; /* Hash */ u8 hash[MAX_DIGEST_SIZE]; r.magic = s.magic = z.magic = u1.magic = u2.magic = WORD(0); /* Sanity checks */ MUST_HAVE((pub != NULL) && (msg != NULL) && (sig != NULL), ret, err); ret = local_memset(hash, 0, sizeof(hash)); EG(ret, err); /* Make things more readable */ p = &(pub->p); q = &(pub->q); g = &(pub->g); y = &(pub->y); /* Sanity checks */ ret = nn_check_initialized(p); EG(ret, err); ret = nn_check_initialized(q); EG(ret, err); ret = nn_check_initialized(g); EG(ret, err); ret = nn_check_initialized(y); EG(ret, err); /* Sanity check on the signature length */ ret = nn_bitlen(q, &N); EG(ret, err); MUST_HAVE((siglen == (2 * BYTECEIL(N))), ret, err); /* Extract r and s */ ret = _os2ip(&r, sig, (siglen / 2)); EG(ret, err); ret = _os2ip(&s, sig + (siglen / 2), (siglen / 2)); EG(ret, err); /* Return an error if r = 0 or s = 0 */ ret = nn_iszero(&r, &iszero); EG(ret, err); MUST_HAVE((!iszero), ret, err); ret = nn_iszero(&s, &iszero); EG(ret, err); MUST_HAVE((!iszero), ret, err); /* Check thatt 0 < r′ < q and 0 < s′ < q */ ret = nn_cmp(&r, q, &cmp); EG(ret, err); MUST_HAVE((cmp < 0), ret, err); ret = nn_cmp(&s, q, &cmp); EG(ret, err); MUST_HAVE((cmp < 0), ret, err); /* Compute the hash */ ret = gen_hash_get_hash_sizes(dsa_hash, &hlen, &block_size); EG(ret, err); MUST_HAVE((hlen <= MAX_DIGEST_SIZE), ret, err); ret = gen_hash_hfunc(msg, msglen, hash, dsa_hash); EG(ret, err); /* z = the leftmost min(N, outlen) bits of Hash(M) */ rshift = 0; if ((hlen * 8) > N) { rshift = (bitcnt_t)((hlen * 8) - N); } ret = _os2ip(&z, hash, hlen); EG(ret, err); if (rshift) { ret = nn_rshift_fixedlen(&z, &z, rshift); EG(ret, err); } ret = nn_mod(&z, &z, q); EG(ret, err); /* Compute w = s**-1 mod (q) in s */ ret = nn_modinv(&s, &s, q); EG(ret, err); /* u1 = (zw) mod q */ ret = nn_mod_mul(&u1, &z, &s, q); EG(ret, err); /* u2 = ((r′)w) mod q */ ret = nn_mod_mul(&u2, &r, &s, q); EG(ret, err); /* Now compute v = = ((g**u1 y**u2) mod p) mod q */ /* NOTE: no need to use a secure exponentiation here as we only * manipulate public data. */ ret = _nn_mod_pow_insecure(&v, g, &u1, p); EG(ret, err); ret = _nn_mod_pow_insecure(&s, y, &u2, p); EG(ret, err); ret = nn_mod_mul(&v, &v, &s, p); EG(ret, err); ret = nn_mod(&v, &v, q); EG(ret, err); /* Check that v = r */ ret = nn_cmp(&v, &r, &cmp); EG(ret, err); ret = (cmp != 0) ? -1 : 0; err: nn_uninit(&r); nn_uninit(&s); nn_uninit(&z); nn_uninit(&u1); nn_uninit(&u2); nn_uninit(&v); PTR_NULLIFY(p); PTR_NULLIFY(q); PTR_NULLIFY(g); PTR_NULLIFY(y); return ret; } #ifdef DSA #include int main(int argc, char *argv[]) { int ret = 0; const u8 p[] = { 0x90, 0x06, 0x64, 0x55, 0xB5, 0xCF, 0xC3, 0x8F, 0x9C, 0xAA, 0x4A, 0x48, 0xB4, 0x28, 0x1F, 0x29, 0x2C, 0x26, 0x0F, 0xEE, 0xF0, 0x1F, 0xD6, 0x10, 0x37, 0xE5, 0x62, 0x58, 0xA7, 0x79, 0x5A, 0x1C, 0x7A, 0xD4, 0x60, 0x76, 0x98, 0x2C, 0xE6, 0xBB, 0x95, 0x69, 0x36, 0xC6, 0xAB, 0x4D, 0xCF, 0xE0, 0x5E, 0x67, 0x84, 0x58, 0x69, 0x40, 0xCA, 0x54, 0x4B, 0x9B, 0x21, 0x40, 0xE1, 0xEB, 0x52, 0x3F, 0x00, 0x9D, 0x20, 0xA7, 0xE7, 0x88, 0x0E, 0x4E, 0x5B, 0xFA, 0x69, 0x0F, 0x1B, 0x90, 0x04, 0xA2, 0x78, 0x11, 0xCD, 0x99, 0x04, 0xAF, 0x70, 0x42, 0x0E, 0xEF, 0xD6, 0xEA, 0x11, 0xEF, 0x7D, 0xA1, 0x29, 0xF5, 0x88, 0x35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xe6, 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, }; const u8 msg[] = "abc"; const u8 nonce[] = { 0xA6, 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, 0xB0, 0x67, 0x8B, 0xEE, }; dsa_priv_key priv; dsa_pub_key pub; dsa_pub_key pub2; u8 sig[32*2] = { 0 }; FORCE_USED_VAR(argc); FORCE_USED_VAR(argv); /* Sanity check on size for DSA. * NOTE: the double parentheses are here to handle -Wunreachable-code */ if((NN_USABLE_MAX_BIT_LEN) < (4096)){ ext_printf("Error: you seem to have compiled libecc with usable NN size < 4096, not suitable for DSA.\n"); ext_printf(" => Please recompile libecc with EXTRA_CFLAGS=\"-DUSER_NN_BIT_LEN=4096\"\n"); ext_printf(" This will increase usable NN for proper DSA up to 4096 bits.\n"); ext_printf(" Then recompile the current examples with the same EXTRA_CFLAGS=\"-DUSER_NN_BIT_LEN=4096\" flag and execute again!\n"); /* NOTE: ret = 0 here to pass self tests even if the library is not compatible */ ret = 0; goto err; } ret = dsa_import_priv_key(&priv, p, sizeof(p), q, sizeof(q), g, sizeof(g), x, sizeof(x)); EG(ret, err); ret = dsa_import_pub_key(&pub, p, sizeof(p), q, sizeof(q), g, sizeof(g), y, sizeof(y)); EG(ret, err); ret = dsa_compute_pub_from_priv(&pub2, &priv); EG(ret, err); nn_print("y", &(pub2.y)); ret = dsa_sign(&priv, msg, sizeof(msg)-1, nonce, sizeof(nonce), sig, sizeof(sig), HASH_SHA256); EG(ret, err); buf_print("sig", sig, sizeof(sig)); ret = dsa_verify(&pub, msg, sizeof(msg)-1, sig, sizeof(sig), HASH_SHA256); ext_printf("Signature result %d\n", ret); err: return ret; } #endif