Path: blob/main/crypto/libecc/src/sig/fuzzing_ecrdsa.c
39536 views
/*1* Copyright (C) 2017 - This file is part of libecc project2*3* Authors:4* Ryad BENADJILA <[email protected]>5* Arnaud EBALARD <[email protected]>6* Jean-Pierre FLORI <[email protected]>7*8* Contributors:9* Nicolas VIVET <[email protected]>10* Karim KHALFALLAH <[email protected]>11*12* This software is licensed under a dual BSD and GPL v2 license.13* See LICENSE file at the root folder of the project.14*/15#include <libecc/lib_ecc_config.h>16#if defined(WITH_SIG_ECRDSA) && defined(USE_CRYPTOFUZZ)1718#include <libecc/nn/nn_rand.h>19#include <libecc/nn/nn_mul.h>20#include <libecc/nn/nn_logical.h>2122#include <libecc/sig/sig_algs_internal.h>23#include <libecc/sig/ec_key.h>24#include <libecc/utils/utils.h>25#ifdef VERBOSE_INNER_VALUES26#define EC_SIG_ALG "ECRDSA"27#endif28#include <libecc/utils/dbg_sig.h>2930/* NOTE: the following versions of ECRDSA are "raw" with31* no hash functions and nonce override. They are DANGEROUS and32* should NOT be used in production mode! They are however useful33* for corner cases tests and fuzzing.34*/3536/*37* NOTE: ISO/IEC 14888-3 standard seems to diverge from the existing implementations38* of ECRDSA when treating the message hash, and from the examples of certificates provided39* in RFC 7091 and draft-deremin-rfc4491-bis. While in ISO/IEC 14888-3 it is explicitely asked40* to proceed with the hash of the message as big endian, the RFCs derived from the Russian41* standard expect the hash value to be treated as little endian when importing it as an integer42* (this discrepancy is exhibited and confirmed by test vectors present in ISO/IEC 14888-3, and43* by X.509 certificates present in the RFCs). This seems (to be confirmed) to be a discrepancy of44* ISO/IEC 14888-3 algorithm description that must be fixed there.45*46* In order to be conservative, libecc uses the Russian standard behavior as expected to be in line with47* other implemetations, but keeps the ISO/IEC 14888-3 behavior if forced/asked by the user using48* the USE_ISO14888_3_ECRDSA toggle. This allows to keep backward compatibility with previous versions of the49* library if needed.50*51*/52#ifndef USE_ISO14888_3_ECRDSA53/* Reverses the endiannes of a buffer in place */54ATTRIBUTE_WARN_UNUSED_RET static inline int _reverse_endianness(u8 *buf, u16 buf_size)55{56u16 i;57u8 tmp;58int ret;5960MUST_HAVE((buf != NULL), ret, err);6162if(buf_size > 1){63for(i = 0; i < (buf_size / 2); i++){64tmp = buf[i];65buf[i] = buf[buf_size - 1 - i];66buf[buf_size - 1 - i] = tmp;67}68}6970ret = 0;71err:72return ret;73}74#endif7576#define ECRDSA_SIGN_MAGIC ((word_t)(0xcc97bbc8ada8973cULL))77#define ECRDSA_SIGN_CHECK_INITIALIZED(A, ret, err) \78MUST_HAVE((((const void *)(A)) != NULL) && \79((A)->magic == ECRDSA_SIGN_MAGIC), ret, err)8081int ecrdsa_sign_raw(struct ec_sign_context *ctx, const u8 *input, u8 inputlen, u8 *sig, u8 siglen, const u8 *nonce, u8 noncelen)82{83bitcnt_t q_bit_len, p_bit_len;84const ec_priv_key *priv_key;85/* NOTE: hash here is not really a hash ... */86u8 h_buf[LOCAL_MIN(255, BIT_LEN_WORDS(NN_MAX_BIT_LEN) * (WORDSIZE / 8))];87prj_pt_src_t G;88prj_pt kG;89nn_src_t q, x;90u8 hsize, r_len, s_len;91int ret, iszero;92nn tmp, s, rx, ke, k, r, e;93#ifdef USE_SIG_BLINDING94/* b is the blinding mask */95nn b, binv;96b.magic = binv.magic = WORD(0);97#endif /* USE_SIG_BLINDING */9899tmp.magic = s.magic = rx.magic = ke.magic = WORD(0);100k.magic = r.magic = e.magic = WORD(0);101kG.magic = WORD(0);102103/*104* First, verify context has been initialized and private105* part too. This guarantees the context is an EC-RDSA106* signature one and we do not finalize() before init().107*/108ret = sig_sign_check_initialized(ctx); EG(ret, err);109ECRDSA_SIGN_CHECK_INITIALIZED(&(ctx->sign_data.ecrdsa), ret, err);110111/* Zero init points */112ret = local_memset(&kG, 0, sizeof(prj_pt)); EG(ret, err);113114/* Make things more readable */115priv_key = &(ctx->key_pair->priv_key);116G = &(priv_key->params->ec_gen);117q = &(priv_key->params->ec_gen_order);118p_bit_len = priv_key->params->ec_fp.p_bitlen;119q_bit_len = priv_key->params->ec_gen_order_bitlen;120x = &(priv_key->x);121r_len = (u8)ECRDSA_R_LEN(q_bit_len);122s_len = (u8)ECRDSA_S_LEN(q_bit_len);123hsize = inputlen;124125MUST_HAVE((NN_MAX_BIT_LEN >= p_bit_len), ret, err);126127MUST_HAVE((siglen == ECRDSA_SIGLEN(q_bit_len)), ret, err);128129dbg_nn_print("p", &(priv_key->params->ec_fp.p));130dbg_nn_print("q", q);131dbg_priv_key_print("x", priv_key);132dbg_pub_key_print("Y", &(ctx->key_pair->pub_key));133dbg_ec_point_print("G", G);134135/*136NOTE: the restart label is removed in CRYPTOFUZZ mode as137we trigger MUST_HAVE instead of restarting in this mode.138restart:139*/140/* 2. Get a random value k in ]0, q[ ... */141/* NOTE: copy our input nonce if not NULL */142if(nonce != NULL){143MUST_HAVE((noncelen <= (u8)(BYTECEIL(q_bit_len))), ret, err);144ret = nn_init_from_buf(&k, nonce, noncelen); EG(ret, err);145}146else{147ret = ctx->rand(&k, q); EG(ret, err);148}149150dbg_nn_print("k", &k);151#ifdef USE_SIG_BLINDING152/* Note: if we use blinding, k and e are multiplied by153* a random value b in ]0,q[ */154ret = nn_get_random_mod(&b, q); EG(ret, err);155dbg_nn_print("b", &b);156#endif /* USE_SIG_BLINDING */157158/* 3. Compute W = kG = (Wx, Wy) */159#ifdef USE_SIG_BLINDING160/* We use blinding for the scalar multiplication */161ret = prj_pt_mul_blind(&kG, &k, G); EG(ret, err);162#else163ret = prj_pt_mul(&kG, &k, G); EG(ret, err);164#endif /* USE_SIG_BLINDING */165ret = prj_pt_unique(&kG, &kG); EG(ret, err);166dbg_nn_print("W_x", &(kG.X.fp_val));167dbg_nn_print("W_y", &(kG.Y.fp_val));168169/* 4. Compute r = Wx mod q */170ret = nn_mod(&r, &(kG.X.fp_val), q); EG(ret, err);171172/* 5. If r is 0, restart the process at step 2. */173/* NOTE: for the CRYPTOFUZZ mode, we do not restart174* the procedure but throw an assert exception instead.175*/176ret = nn_iszero(&r, &iszero); EG(ret, err);177MUST_HAVE((!iszero), ret, err);178179dbg_nn_print("r", &r);180181/* Export r */182ret = nn_export_to_buf(sig, r_len, &r); EG(ret, err);183184/* 6. Compute e = OS2I(h) mod q. If e is 0, set e to 1. */185/* NOTE: here we have raw ECRDSA, this is the raw input */186MUST_HAVE((input != NULL), ret, err);187/* NOTE: the MUST_HAVE is protected by a preprocessing check188* to avoid -Werror=type-limits errors:189* "error: comparison is always true due to limited range of data type"190*/191#if LOCAL_MIN(255, BIT_LEN_WORDS(NN_MAX_BIT_LEN) * (WORDSIZE / 8)) < 255192MUST_HAVE(((u32)inputlen <= sizeof(h_buf)), ret, err);193#endif194ret = local_memset(h_buf, 0, sizeof(h_buf)); EG(ret, err);195ret = local_memcpy(h_buf, input, hsize); EG(ret, err);196dbg_buf_print("H(m)", h_buf, hsize);197/* NOTE: this handles a discrepancy between ISO/IEC 14888-3 and198* Russian standard based RFCs.199*/200#ifndef USE_ISO14888_3_ECRDSA201ret = _reverse_endianness(h_buf, hsize); EG(ret, err);202#endif203204ret = nn_init_from_buf(&tmp, h_buf, hsize); EG(ret, err);205ret = local_memset(h_buf, 0, hsize); EG(ret, err);206ret = nn_mod(&e, &tmp, q); EG(ret, err);207ret = nn_iszero(&e, &iszero); EG(ret, err);208if (iszero) {209ret = nn_inc(&e, &e); EG(ret, err);210}211dbg_nn_print("e", &e);212213#ifdef USE_SIG_BLINDING214/* In case of blinding, we blind r and e */215ret = nn_mod_mul(&r, &r, &b, q); EG(ret, err);216ret = nn_mod_mul(&e, &e, &b, q); EG(ret, err);217#endif /* USE_SIG_BLINDING */218219/* Compute s = (rx + ke) mod q */220ret = nn_mod_mul(&rx, &r, x, q); EG(ret, err);221ret = nn_mod_mul(&ke, &k, &e, q); EG(ret, err);222ret = nn_mod_add(&s, &rx, &ke, q); EG(ret, err);223#ifdef USE_SIG_BLINDING224/* Unblind s */225/* NOTE: we use Fermat's little theorem inversion for226* constant time here. This is possible since q is prime.227*/228ret = nn_modinv_fermat(&binv, &b, q); EG(ret, err);229ret = nn_mod_mul(&s, &s, &binv, q); EG(ret, err);230#endif /* USE_SIG_BLINDING */231232/* If s is 0, restart the process at step 2. */233/* 10. If s is 0, restart the process at step 4. */234/* NOTE: for the CRYPTOFUZZ mode, we do not restart235* the procedure but throw an assert exception instead.236*/237ret = nn_iszero(&s, &iszero); EG(ret, err);238MUST_HAVE((!iszero), ret, err);239240dbg_nn_print("s", &s);241242/* Return (r,s) */243ret = nn_export_to_buf(sig + r_len, s_len, &s); EG(ret, err);244245err:246nn_uninit(&r);247nn_uninit(&s);248nn_uninit(&tmp);249nn_uninit(&rx);250nn_uninit(&ke);251nn_uninit(&k);252nn_uninit(&e);253prj_pt_uninit(&kG);254255/*256* We can now clear data part of the context. This will clear257* magic and avoid further reuse of the whole context.258*/259if(ctx != NULL){260IGNORE_RET_VAL(local_memset(&(ctx->sign_data.ecrdsa), 0, sizeof(ecrdsa_sign_data)));261}262263/* Clean what remains on the stack */264VAR_ZEROIFY(r_len);265VAR_ZEROIFY(s_len);266VAR_ZEROIFY(q_bit_len);267VAR_ZEROIFY(p_bit_len);268VAR_ZEROIFY(hsize);269PTR_NULLIFY(priv_key);270PTR_NULLIFY(G);271PTR_NULLIFY(q);272PTR_NULLIFY(x);273274#ifdef USE_SIG_BLINDING275nn_uninit(&b);276nn_uninit(&binv);277#endif /* USE_SIG_BLINDING */278279return ret;280}281282/******************************/283#define ECRDSA_VERIFY_MAGIC ((word_t)(0xa8e16b7e8180cb9aULL))284#define ECRDSA_VERIFY_CHECK_INITIALIZED(A, ret, err) \285MUST_HAVE((((const void *)(A)) != NULL) && \286((A)->magic == ECRDSA_VERIFY_MAGIC), ret, err)287288int ecrdsa_verify_raw(struct ec_verify_context *ctx, const u8 *input, u8 inputlen)289{290prj_pt_src_t G, Y;291nn_src_t q;292nn tmp, h, r_prime, e, v, u;293prj_pt vY, uG;294prj_pt_t Wprime;295/* NOTE: hash here is not really a hash ... */296u8 h_buf[LOCAL_MIN(255, BIT_LEN_WORDS(NN_MAX_BIT_LEN) * (WORDSIZE / 8))];297nn *r, *s;298u8 hsize;299int ret, iszero, cmp;300301tmp.magic = h.magic = r_prime.magic = e.magic = WORD(0);302v.magic = u.magic = WORD(0);303vY.magic = uG.magic = WORD(0);304305/* NOTE: we reuse uG for Wprime to optimize local variables */306Wprime = &uG;307308/*309* First, verify context has been initialized and public310* part too. This guarantees the context is an EC-RDSA311* verification one and we do not finalize() before init().312*/313ret = sig_verify_check_initialized(ctx); EG(ret, err);314ECRDSA_VERIFY_CHECK_INITIALIZED(&(ctx->verify_data.ecrdsa), ret, err);315316/* Zero init points */317ret = local_memset(&uG, 0, sizeof(prj_pt)); EG(ret, err);318ret = local_memset(&vY, 0, sizeof(prj_pt)); EG(ret, err);319320/* Make things more readable */321G = &(ctx->pub_key->params->ec_gen);322Y = &(ctx->pub_key->y);323q = &(ctx->pub_key->params->ec_gen_order);324r = &(ctx->verify_data.ecrdsa.r);325s = &(ctx->verify_data.ecrdsa.s);326hsize = inputlen;327328/* 2. Compute h = H(m) */329/* NOTE: here we have raw ECRDSA, this is the raw input */330MUST_HAVE((input != NULL), ret, err);331/* NOTE: the MUST_HAVE is protected by a preprocessing check332* to avoid -Werror=type-limits errors:333* "error: comparison is always true due to limited range of data type"334*/335#if LOCAL_MIN(255, BIT_LEN_WORDS(NN_MAX_BIT_LEN) * (WORDSIZE / 8)) < 255336MUST_HAVE(((u32)inputlen <= sizeof(h_buf)), ret, err);337#endif338339ret = local_memset(h_buf, 0, sizeof(h_buf)); EG(ret, err);340ret = local_memcpy(h_buf, input, hsize); EG(ret, err);341dbg_buf_print("H(m)", h_buf, hsize);342/* NOTE: this handles a discrepancy between ISO/IEC 14888-3 and343* Russian standard based RFCs.344*/345#ifndef USE_ISO14888_3_ECRDSA346ret = _reverse_endianness(h_buf, hsize); EG(ret, err);347#endif348349/* 3. Compute e = OS2I(h)^-1 mod q */350ret = nn_init_from_buf(&tmp, h_buf, hsize); EG(ret, err);351ret = local_memset(h_buf, 0, hsize); EG(ret, err);352ret = nn_mod(&h, &tmp, q); EG(ret, err); /* h = OS2I(h) mod q */353ret = nn_iszero(&h, &iszero); EG(ret, err);354if (iszero) { /* If h is equal to 0, set it to 1 */355ret = nn_inc(&h, &h); EG(ret, err);356}357ret = nn_modinv(&e, &h, q); EG(ret, err); /* e = h^-1 mod q */358359/* 4. Compute u = es mod q */360ret = nn_mul(&tmp, &e, s); EG(ret, err);361ret = nn_mod(&u, &tmp, q); EG(ret, err);362363/* 5. Compute v = -er mod q364*365* Because we only support positive integers, we compute366* v = -er mod q = q - (er mod q) (except when er is 0).367*/368ret = nn_mul(&tmp, &e, r); EG(ret, err); /* tmp = er */369ret = nn_mod(&tmp, &tmp, q); EG(ret, err); /* tmp = er mod q */370ret = nn_mod_neg(&v, &tmp, q); EG(ret, err); /* negate tmp */371372/* 6. Compute W' = uG + vY = (W'_x, W'_y) */373ret = prj_pt_mul(&uG, &u, G); EG(ret, err);374ret = prj_pt_mul(&vY, &v, Y); EG(ret, err);375ret = prj_pt_add(Wprime, &uG, &vY); EG(ret, err);376ret = prj_pt_unique(Wprime, Wprime); EG(ret, err);377dbg_nn_print("W'_x", &(Wprime->X.fp_val));378dbg_nn_print("W'_y", &(Wprime->Y.fp_val));379380/* 7. Compute r' = W'_x mod q */381ret = nn_mod(&r_prime, &(Wprime->X.fp_val), q); EG(ret, err);382383/* 8. Check r and r' are the same */384ret = nn_cmp(r, &r_prime, &cmp); EG(ret, err);385ret = (cmp == 0) ? 0 : -1;386387err:388nn_uninit(&r_prime);389nn_uninit(&tmp);390nn_uninit(&h);391nn_uninit(&e);392nn_uninit(&u);393nn_uninit(&v);394prj_pt_uninit(&vY);395prj_pt_uninit(&uG);396397/*398* We can now clear data part of the context. This will clear399* magic and avoid further reuse of the whole context.400*/401if(ctx != NULL){402IGNORE_RET_VAL(local_memset(&(ctx->verify_data.ecrdsa), 0,403sizeof(ecrdsa_verify_data)));404}405406/* Clean what remains on the stack */407PTR_NULLIFY(Wprime);408PTR_NULLIFY(G);409PTR_NULLIFY(Y);410PTR_NULLIFY(q);411PTR_NULLIFY(r);412PTR_NULLIFY(s);413VAR_ZEROIFY(hsize);414415return ret;416}417418#else /* WITH_SIG_ECRDSA && USE_CRYPTOFUZZ */419420/*421* Dummy definition to avoid the empty translation unit ISO C warning422*/423typedef int dummy;424#endif /* WITH_SIG_ECRDSA */425426427