Path: blob/main/contrib/bearssl/src/ec/ecdsa_i31_sign_raw.c
39488 views
/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526#define I31_LEN ((BR_MAX_EC_SIZE + 61) / 31)27#define POINT_LEN (1 + (((BR_MAX_EC_SIZE + 7) >> 3) << 1))28#define ORDER_LEN ((BR_MAX_EC_SIZE + 7) >> 3)2930/* see bearssl_ec.h */31size_t32br_ecdsa_i31_sign_raw(const br_ec_impl *impl,33const br_hash_class *hf, const void *hash_value,34const br_ec_private_key *sk, void *sig)35{36/*37* IMPORTANT: this code is fit only for curves with a prime38* order. This is needed so that modular reduction of the X39* coordinate of a point can be done with a simple subtraction.40* We also rely on the last byte of the curve order to be distinct41* from 0 and 1.42*/43const br_ec_curve_def *cd;44uint32_t n[I31_LEN], r[I31_LEN], s[I31_LEN], x[I31_LEN];45uint32_t m[I31_LEN], k[I31_LEN], t1[I31_LEN], t2[I31_LEN];46unsigned char tt[ORDER_LEN << 1];47unsigned char eU[POINT_LEN];48size_t hash_len, nlen, ulen;49uint32_t n0i, ctl;50br_hmac_drbg_context drbg;5152/*53* If the curve is not supported, then exit with an error.54*/55if (((impl->supported_curves >> sk->curve) & 1) == 0) {56return 0;57}5859/*60* Get the curve parameters (generator and order).61*/62switch (sk->curve) {63case BR_EC_secp256r1:64cd = &br_secp256r1;65break;66case BR_EC_secp384r1:67cd = &br_secp384r1;68break;69case BR_EC_secp521r1:70cd = &br_secp521r1;71break;72default:73return 0;74}7576/*77* Get modulus.78*/79nlen = cd->order_len;80br_i31_decode(n, cd->order, nlen);81n0i = br_i31_ninv31(n[1]);8283/*84* Get private key as an i31 integer. This also checks that the85* private key is well-defined (not zero, and less than the86* curve order).87*/88if (!br_i31_decode_mod(x, sk->x, sk->xlen, n)) {89return 0;90}91if (br_i31_iszero(x)) {92return 0;93}9495/*96* Get hash length.97*/98hash_len = (hf->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;99100/*101* Truncate and reduce the hash value modulo the curve order.102*/103br_ecdsa_i31_bits2int(m, hash_value, hash_len, n[0]);104br_i31_sub(m, n, br_i31_sub(m, n, 0) ^ 1);105106/*107* RFC 6979 generation of the "k" value.108*109* The process uses HMAC_DRBG (with the hash function used to110* process the message that is to be signed). The seed is the111* concatenation of the encodings of the private key and112* the hash value (after truncation and modular reduction).113*/114br_i31_encode(tt, nlen, x);115br_i31_encode(tt + nlen, nlen, m);116br_hmac_drbg_init(&drbg, hf, tt, nlen << 1);117for (;;) {118br_hmac_drbg_generate(&drbg, tt, nlen);119br_ecdsa_i31_bits2int(k, tt, nlen, n[0]);120if (br_i31_iszero(k)) {121continue;122}123if (br_i31_sub(k, n, 0)) {124break;125}126}127128/*129* Compute k*G and extract the X coordinate, then reduce it130* modulo the curve order. Since we support only curves with131* prime order, that reduction is only a matter of computing132* a subtraction.133*/134br_i31_encode(tt, nlen, k);135ulen = impl->mulgen(eU, tt, nlen, sk->curve);136br_i31_zero(r, n[0]);137br_i31_decode(r, &eU[1], ulen >> 1);138r[0] = n[0];139br_i31_sub(r, n, br_i31_sub(r, n, 0) ^ 1);140141/*142* Compute 1/k in double-Montgomery representation. We do so by143* first converting _from_ Montgomery representation (twice),144* then using a modular exponentiation.145*/146br_i31_from_monty(k, n, n0i);147br_i31_from_monty(k, n, n0i);148memcpy(tt, cd->order, nlen);149tt[nlen - 1] -= 2;150br_i31_modpow(k, tt, nlen, n, n0i, t1, t2);151152/*153* Compute s = (m+xr)/k (mod n).154* The k[] array contains R^2/k (double-Montgomery representation);155* we thus can use direct Montgomery multiplications and conversions156* from Montgomery, avoiding any call to br_i31_to_monty() (which157* is slower).158*/159br_i31_from_monty(m, n, n0i);160br_i31_montymul(t1, x, r, n, n0i);161ctl = br_i31_add(t1, m, 1);162ctl |= br_i31_sub(t1, n, 0) ^ 1;163br_i31_sub(t1, n, ctl);164br_i31_montymul(s, t1, k, n, n0i);165166/*167* Encode r and s in the signature.168*/169br_i31_encode(sig, nlen, r);170br_i31_encode((unsigned char *)sig + nlen, nlen, s);171return nlen << 1;172}173174175