Path: blob/main/external/libecc/src/examples/sig/sdsa/sdsa.c
2066 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 "sdsa.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 Schnorr signature24* scheme (aka SDSA for Schnorr DSA) based on libecc arithmetic primitives.25* Many "variants" of Schnorr signature schemes exist, we implement here the26* one corresponding to SDSA as described in the ISO14888-3 standard.27*28* XXX: Please be aware that libecc has been designed for Elliptic29* Curve cryptography, and as so the arithmetic primitives are30* not optimized for big numbers >= 1024 bits usually used for SDSA.31* Additionnaly, a hard limit of our NN values makes it impossible32* to exceed ~5300 bits in the best case (words of size 64 bits).33*34* All in all, please see this as a proof of concept.35* Use it at your own risk!36*37* !! DISCLAIMER !!38* ================39*40* Althoug some efforts have been made to secure this implementation41* of Schnorr DSA (e.g. by protecting the private key and nonces using constant42* time and blinding WHEN activated with BLINDING=1), please consider this43* code as a proof of concept and use it at your own risk.44*45* All-in-all, this piece of code can be useful in some contexts, or risky to46* use in other sensitive ones where advanced side-channels or fault attacks47* have to be considered. Use this SDSA code knowingly and at your own risk!48*49*/5051/* NOTE: since SDSA is very similar to DSA, we reuse some of our DSA52* primitives to factorize some code. Also, SDSA private and public keys53* have the exact same type as DSA keys.54*/5556/* Import a SDSA private key from buffers */57int sdsa_import_priv_key(sdsa_priv_key *priv, const u8 *p, u16 plen,58const u8 *q, u16 qlen,59const u8 *g, u16 glen,60const u8 *x, u16 xlen)61{62return dsa_import_priv_key(priv, p, plen, q, qlen, g, glen, x, xlen);63}6465/* Import a SDSA public key from buffers */66int sdsa_import_pub_key(sdsa_pub_key *pub, const u8 *p, u16 plen,67const u8 *q, u16 qlen,68const u8 *g, u16 glen,69const u8 *y, u16 ylen)70{71return dsa_import_pub_key(pub, p, plen, q, qlen, g, glen, y, ylen);72}73747576/* Compute a SDSA public key from a private key.77* The public key is computed using modular exponentiation of the generator78* with the private key.79*/80int sdsa_compute_pub_from_priv(sdsa_pub_key *pub, const sdsa_priv_key *priv)81{82return dsa_compute_pub_from_priv(pub, priv);83}8485/* Generate a SDSA signature86*/87int sdsa_sign(const sdsa_priv_key *priv, const u8 *msg, u32 msglen,88const u8 *nonce, u16 noncelen,89u8 *sig, u16 siglen, gen_hash_alg_type sdsa_hash)90{91int ret, iszero;92/* alpha is the bit length of p, beta is the bit length of q */93bitcnt_t alpha, beta;94/* Length of the hash function (hlen is "gamma") */95u8 hlen, block_size;96nn_src_t p, q, g, x;97/* The nonce and its protected version */98nn k, k_;99/* r, s, pi */100nn r, s;101nn_t pi;102/* This is a bit too much for stack space, but we need it for103* the computation of "pi" I2BS representation ...104*/105u8 pi_buf[NN_USABLE_MAX_BYTE_LEN];106/* hash context */107gen_hash_context hash_ctx;108#ifdef USE_SIG_BLINDING109/* b is the blinding mask */110nn b;111b.magic = WORD(0);112#endif /* USE_SIG_BLINDING */113k.magic = k_.magic = r.magic = s.magic = WORD(0);114115/* Sanity checks */116MUST_HAVE((priv != NULL) && (msg != NULL) && (sig != NULL), ret, err);117118ret = local_memset(pi_buf, 0, sizeof(pi_buf)); EG(ret, err);119120/* Make things more readable */121p = &(priv->p);122q = &(priv->q);123g = &(priv->g);124x = &(priv->x);125126/* Sanity checks */127ret = nn_check_initialized(p); EG(ret, err);128ret = nn_check_initialized(q); EG(ret, err);129ret = nn_check_initialized(g); EG(ret, err);130ret = nn_check_initialized(x); EG(ret, err);131132/* Let alpha be the bit length of p */133ret = nn_bitlen(p, &alpha); EG(ret, err);134/* Let beta be the bit length of q */135ret = nn_bitlen(q, &beta); EG(ret, err);136/* Get the hash sizes (8*"gamma") */137ret = gen_hash_get_hash_sizes(sdsa_hash, &hlen, &block_size); EG(ret, err);138MUST_HAVE((hlen <= MAX_DIGEST_SIZE), ret, err);139140/* Sanity check on the signature length:141* the signature is of size hash function plus an integer modulo q142* "gamma" + beta143*/144MUST_HAVE((siglen == (hlen + BYTECEIL(beta))), ret, err);145146restart:147/* If the nonce is imposed, use it. Else get a random modulo q */148if(nonce != NULL){149ret = _os2ip(&k, nonce, noncelen); EG(ret, err);150}151else{152ret = nn_get_random_mod(&k, q); EG(ret, err);153}154155/* Fix the MSB of our scalar */156ret = nn_copy(&k_, &k); EG(ret, err);157#ifdef USE_SIG_BLINDING158/* Blind the scalar */159ret = _blind_scalar(&k_, q, &k_); EG(ret, err);160#endif /* USE_SIG_BLINDING */161ret = _fix_scalar_msb(&k_, q, &k_); EG(ret, err);162/* Use r as aliasing for pi to save some space */163pi = &r;164/* pi = (g**k mod p) */165ret = nn_init(pi, 0); EG(ret, err);166/* Exponentiation modulo p */167ret = nn_mod_pow(pi, g, &k_, p); EG(ret, err);168169/* Compute I2BS(alpha, pi)170*/171ret = _i2osp(pi, pi_buf, (u16)BYTECEIL(alpha)); EG(ret, err);172173/* r = h(I2BS(alpha, pi) || M) */174ret = gen_hash_init(&hash_ctx, sdsa_hash); EG(ret, err);175ret = gen_hash_update(&hash_ctx, pi_buf, (u16)BYTECEIL(alpha), sdsa_hash); EG(ret, err);176ret = gen_hash_update(&hash_ctx, msg, msglen, sdsa_hash); EG(ret, err);177/* Export r result of the hash function in sig */178ret = gen_hash_final(&hash_ctx, sig, sdsa_hash); EG(ret, err);179180/* Import r as an integer modulo q */181ret = _os2ip(&r, sig, hlen); EG(ret, err);182ret = nn_mod(&r, &r, q); EG(ret, err);183184/* If r is 0, restart the process */185ret = nn_iszero(&r, &iszero); EG(ret, err);186if (iszero) {187IGNORE_RET_VAL(local_memset(sig, 0, hlen));188goto restart;189}190191#ifdef USE_SIG_BLINDING192/* Note: if we use blinding, r and k are multiplied by193* a random value b in ]0,q[ */194ret = nn_get_random_mod(&b, q); EG(ret, err);195/* Blind r with b */196ret = nn_mod_mul(&r, &r, &b, q); EG(ret, err);197/* Blind k with b */198ret = nn_mod_mul(&k, &k, &b, q); EG(ret, err);199/*200* In case of blinding, we compute b^-1 with201* little Fermat theorem. This will be used to202* unblind s.203*/204ret = nn_modinv_fermat(&b, &b, q); EG(ret, err);205#endif /* USE_SIG_BLINDING */206207/* Compute s = (k + r x) mod q */208ret = nn_mod_mul(&s, &r, x, q); EG(ret, err);209ret = nn_mod_add(&s, &s, &k, q); EG(ret, err);210211#ifdef USE_SIG_BLINDING212/* In case of blinding, unblind s */213ret = nn_mod_mul(&s, &s, &b, q); EG(ret, err);214#endif /* USE_SIG_BLINDING */215/* If s is 0, restart the process */216ret = nn_iszero(&s, &iszero); EG(ret, err);217if (iszero) {218goto restart;219}220221/* Export s */222ret = _i2osp(&s, sig + hlen, (u16)(siglen - hlen)); EG(ret, err);223224err:225if(ret && (sig != NULL)){226IGNORE_RET_VAL(local_memset(sig, 0, siglen));227}228229nn_uninit(&k);230nn_uninit(&k_);231#ifdef USE_SIG_BLINDING232nn_uninit(&b);233#endif234nn_uninit(&r);235nn_uninit(&s);236237PTR_NULLIFY(pi);238239PTR_NULLIFY(p);240PTR_NULLIFY(q);241PTR_NULLIFY(g);242PTR_NULLIFY(x);243244return ret;245}246247248249/* Verify a SDSA signature250*/251int sdsa_verify(const sdsa_pub_key *pub, const u8 *msg, u32 msglen,252const u8 *sig, u16 siglen, gen_hash_alg_type sdsa_hash)253{254int ret, iszero, cmp;255/* alpha is the bit length of p, beta is the bit length of q */256bitcnt_t alpha, beta;257/* Length of the hash function */258u8 hlen, block_size;259nn_src_t p, q, g, y;260/* r, s */261nn r, s;262/* u, and pi */263nn u, pi;264/* This is a bit too much for stack space, but we need it for265* the computation of "pi" I2BS representation ...266*/267u8 pi_buf[NN_USABLE_MAX_BYTE_LEN];268/* Hash */269u8 hash[MAX_DIGEST_SIZE];270/* hash context */271gen_hash_context hash_ctx;272r.magic = s.magic = u.magic = pi.magic = WORD(0);273274/* Sanity checks */275MUST_HAVE((pub != NULL) && (msg != NULL) && (sig != NULL), ret, err);276277ret = local_memset(pi_buf, 0, sizeof(pi_buf)); EG(ret, err);278ret = local_memset(hash, 0, sizeof(hash)); EG(ret, err);279280/* Make things more readable */281p = &(pub->p);282q = &(pub->q);283g = &(pub->g);284y = &(pub->y);285286/* Sanity checks */287ret = nn_check_initialized(p); EG(ret, err);288ret = nn_check_initialized(q); EG(ret, err);289ret = nn_check_initialized(g); EG(ret, err);290ret = nn_check_initialized(y); EG(ret, err);291292/* Let alpha be the bit length of p */293ret = nn_bitlen(p, &alpha); EG(ret, err);294/* Let beta be the bit length of q */295ret = nn_bitlen(q, &beta); EG(ret, err);296/* Get the hash sizes (8*"gamma") */297ret = gen_hash_get_hash_sizes(sdsa_hash, &hlen, &block_size); EG(ret, err);298MUST_HAVE((hlen <= MAX_DIGEST_SIZE), ret, err);299300/* Sanity check on the signature length */301MUST_HAVE((siglen == (hlen + BYTECEIL(beta))), ret, err);302303/* Extract r and s */304ret = _os2ip(&r, sig, hlen); EG(ret, err);305ret = _os2ip(&s, sig + hlen, (u16)(siglen - hlen)); EG(ret, err);306307/* Return an error if r = 0 or s = 0 */308ret = nn_iszero(&r, &iszero); EG(ret, err);309MUST_HAVE((!iszero), ret, err);310ret = nn_iszero(&s, &iszero); EG(ret, err);311MUST_HAVE((!iszero), ret, err);312/* Check that 0 < s < q */313ret = nn_cmp(&s, q, &cmp); EG(ret, err);314MUST_HAVE((cmp < 0), ret, err);315316/* Take r modulo q */317ret = nn_mod(&r, &r, q); EG(ret, err);318319/* Initialize internal variables */320ret = nn_init(&u, 0); EG(ret, err);321ret = nn_init(&pi, 0); EG(ret, err);322323/* NOTE: no need to use a secure exponentiation here as we only324* manipulate public data.325*/326/* Compute (y ** -r) mod (p) */327ret = nn_sub(&r, q, &r); EG(ret, err); /* compute -r = (q - r) mod q */328ret = _nn_mod_pow_insecure(&u, y, &r, p); EG(ret, err);329/* Compute (g ** s) mod (p) */330ret = _nn_mod_pow_insecure(&pi, g, &s, p); EG(ret, err);331/* Compute (y ** -r) * (g ** s) mod (p) */332ret = nn_mod_mul(&pi, &pi, &u, p); EG(ret, err);333334/* Compute r' */335/* I2BS(alpha, pi)336*/337ret = _i2osp(&pi, pi_buf, (u16)BYTECEIL(alpha)); EG(ret, err);338/* r' = h(I2BS(alpha, pi) || M) */339ret = gen_hash_init(&hash_ctx, sdsa_hash); EG(ret, err);340ret = gen_hash_update(&hash_ctx, pi_buf, (u16)BYTECEIL(alpha), sdsa_hash); EG(ret, err);341ret = gen_hash_update(&hash_ctx, msg, msglen, sdsa_hash); EG(ret, err);342ret = gen_hash_final(&hash_ctx, hash, sdsa_hash); EG(ret, err);343344/* Check that hash values r' == r */345ret = are_equal(sig, hash, hlen, &cmp); EG(ret, err);346ret = (cmp != 1) ? -1 : 0;347348err:349nn_uninit(&r);350nn_uninit(&s);351nn_uninit(&u);352nn_uninit(&pi);353354PTR_NULLIFY(p);355PTR_NULLIFY(q);356PTR_NULLIFY(g);357PTR_NULLIFY(y);358359return ret;360}361362#ifdef SDSA363#include <libecc/utils/print_buf.h>364int main(int argc, char *argv[])365{366int ret = 0;367368/* This example is taken from ISO14888-3 SDSA (Appendix F "Numerical examples" */369const u8 p[] = {3700x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C, 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,3710x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1, 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,3720x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D, 0x83, 0x0E, 0x9A, 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0xCA, 0xDB, 0x09, 0x4A, 0xE9, 0x1E, 0x1A, 0x15, 0x97,381};382383const u8 q[] = {3840x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76, 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,3850xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3,386};387388const u8 g[] = {3890x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66, 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,3900x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50, 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,3910xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF, 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,3920xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31, 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,3930x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2, 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,3940x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2, 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,3950xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85, 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,3960xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2, 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,3970xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37, 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,3980x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84, 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,3990x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F, 0x6C, 0xC4, 0x16, 0x59,400};401402const u8 x[] = {4030x73, 0x01, 0x88, 0x95, 0x20, 0xD4, 0x7A, 0xA0, 0x55, 0x99, 0x5B, 0xA1, 0xD8, 0xFC, 0xD7, 0x01, 0x6E, 0xA6, 0x2E, 0x09, 0x18, 0x89, 0x2E, 0x07,4040xB7, 0xDC, 0x23, 0xAF, 0x69, 0x00, 0x6B, 0x88,405};406407const u8 y[] = {4080x57, 0xA1, 0x72, 0x58, 0xD4, 0xA3, 0xF4, 0x7C, 0x45, 0x45, 0xAD, 0x51, 0xF3, 0x10, 0x9C, 0x5D, 0xB4, 0x1B, 0x78, 0x78, 0x79, 0xFC, 0xFE, 0x53,4090x8D, 0xC1, 0xDD, 0x5D, 0x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x82, 0x03, 0x2A,4170xFB, 0x63, 0xC6, 0x24, 0xF3, 0x2E, 0x66, 0xB0, 0x5F, 0xC3, 0x1C, 0x5D, 0xFF, 0xB2, 0x5F, 0xA9, 0x2D, 0x4D, 0x00, 0xE2, 0xB0, 0xD4, 0xF7, 0x21,4180xE8, 0x8C, 0x41, 0x7D, 0x2E, 0x57, 0x79, 0x7B, 0x8F, 0x55, 0xA2, 0xFF, 0xC6, 0xEE, 0x4D, 0xDB,419};420421const u8 msg[] = "abc";422423const u8 nonce[] = {4240x2B, 0x73, 0xE8, 0xFF, 0x3A, 0x7C, 0x01, 0x68, 0x6C, 0xA5, 0x56, 0xE0, 0xFA, 0xBF, 0xD7, 0x4A, 0xC8, 0xD1, 0xFD, 0xA4, 0xAD, 0x3D, 0x50, 0x3F,4250x23, 0xB8, 0xEB, 0x8A, 0xEE, 0xC6, 0x33, 0x05,426};427428sdsa_priv_key priv;429sdsa_pub_key pub;430sdsa_pub_key pub2;431u8 sig[32*2] = { 0 };432433FORCE_USED_VAR(argc);434FORCE_USED_VAR(argv);435436/* Sanity check on size for DSA.437* NOTE: the double parentheses are here to handle -Wunreachable-code438*/439if((NN_USABLE_MAX_BIT_LEN) < (4096)){440ext_printf("Error: you seem to have compiled libecc with usable NN size < 4096, not suitable for DSA.\n");441ext_printf(" => Please recompile libecc with EXTRA_CFLAGS=\"-DUSER_NN_BIT_LEN=4096\"\n");442ext_printf(" This will increase usable NN for proper DSA up to 4096 bits.\n");443ext_printf(" Then recompile the current examples with the same EXTRA_CFLAGS=\"-DUSER_NN_BIT_LEN=4096\" flag and execute again!\n");444/* NOTE: ret = 0 here to pass self tests even if the library is not compatible */445ret = 0;446goto err;447}448449450ret = sdsa_import_priv_key(&priv, p, sizeof(p), q, sizeof(q), g, sizeof(g), x, sizeof(x)); EG(ret, err);451ret = sdsa_import_pub_key(&pub, p, sizeof(p), q, sizeof(q), g, sizeof(g), y, sizeof(y)); EG(ret, err);452ret = sdsa_compute_pub_from_priv(&pub2, &priv); EG(ret, err);453454nn_print("y", &(pub2.y));455456ret = sdsa_sign(&priv, msg, sizeof(msg)-1, nonce, sizeof(nonce), sig, sizeof(sig), HASH_SHA256); EG(ret, err);457458buf_print("sig", sig, sizeof(sig));459460ret = sdsa_verify(&pub, msg, sizeof(msg)-1, sig, sizeof(sig), HASH_SHA256);461ext_printf("Signature result %d\n", ret);462463err:464return ret;465}466#endif467468469