1 /*
2  * Copyright (C) 2019 Red Hat, Inc.
3  *
4  * Author: Daiki Ueno
5  *
6  * This file is part of GNUTLS.
7  *
8  * The GNUTLS library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>
20  *
21  */
22 
23 #if HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 
27 #include "ecdsa-compute-k.h"
28 
29 #include "dsa-compute-k.h"
30 #include "gnutls_int.h"
31 
32 static inline int
_gnutls_ecc_curve_to_dsa_q(mpz_t * q,gnutls_ecc_curve_t curve)33 _gnutls_ecc_curve_to_dsa_q(mpz_t *q, gnutls_ecc_curve_t curve)
34 {
35 	switch (curve) {
36 #ifdef ENABLE_NON_SUITEB_CURVES
37 	case GNUTLS_ECC_CURVE_SECP192R1:
38 		mpz_init_set_str(*q,
39 				 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836"
40 				 "146BC9B1B4D22831",
41 				 16);
42 		return 0;
43 	case GNUTLS_ECC_CURVE_SECP224R1:
44 		mpz_init_set_str(*q,
45 				 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2"
46 				 "E0B8F03E13DD29455C5C2A3D",
47 				 16);
48 		return 0;
49 #endif
50 	case GNUTLS_ECC_CURVE_SECP256R1:
51 		mpz_init_set_str(*q,
52 				 "FFFFFFFF00000000FFFFFFFFFFFFFFFF"
53 				 "BCE6FAADA7179E84F3B9CAC2FC632551",
54 				 16);
55 		return 0;
56 	case GNUTLS_ECC_CURVE_SECP384R1:
57 		mpz_init_set_str(*q,
58 				 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
59 				 "FFFFFFFFFFFFFFFFC7634D81F4372DDF"
60 				 "581A0DB248B0A77AECEC196ACCC52973",
61 				 16);
62 		return 0;
63 	case GNUTLS_ECC_CURVE_SECP521R1:
64 		mpz_init_set_str(*q,
65 				 "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
66 				 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
67 				 "FFA51868783BF2F966B7FCC0148F709A"
68 				 "5D03BB5C9B8899C47AEBB6FB71E91386"
69 				 "409",
70 				 16);
71 		return 0;
72 	default:
73 		return gnutls_assert_val(GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);
74 	}
75 }
76 
77 int
_gnutls_ecdsa_compute_k(mpz_t k,gnutls_ecc_curve_t curve,const mpz_t x,gnutls_mac_algorithm_t mac,const uint8_t * digest,size_t length)78 _gnutls_ecdsa_compute_k (mpz_t k,
79 			 gnutls_ecc_curve_t curve,
80 			 const mpz_t x,
81 			 gnutls_mac_algorithm_t mac,
82 			 const uint8_t *digest,
83 			 size_t length)
84 {
85 	mpz_t q;
86 	int ret;
87 
88 	ret = _gnutls_ecc_curve_to_dsa_q(&q, curve);
89 	if (ret < 0)
90 		return gnutls_assert_val(ret);
91 
92 	ret = _gnutls_dsa_compute_k (k, q, x, mac, digest, length);
93 	mpz_clear(q);
94 	return ret;
95 }
96