2 * Copyright (C) 2010-2012 Free Software Foundation, Inc.
3 * Copyright (C) 2013 Nikos Mavrogiannopoulos
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GNUTLS.
9 * The GNUTLS library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>
24 /* This file contains the functions needed for RSA/DSA public key
25 * encryption and signatures.
28 #include <gnutls_int.h>
29 #include <gnutls_mpi.h>
30 #include <gnutls_pk.h>
31 #include <gnutls_errors.h>
32 #include <gnutls_datum.h>
33 #include <gnutls_global.h>
34 #include <gnutls_sig.h>
35 #include <gnutls_num.h>
36 #include <x509/x509_int.h>
37 #include <x509/common.h>
39 #include <gnutls_pk.h>
40 #include <nettle/dsa.h>
42 #include <nettle/rsa.h>
43 #include <gnutls/crypto.h>
44 #include <nettle/bignum.h>
45 #include <nettle/ecc.h>
46 #include <nettle/ecdsa.h>
47 #include <nettle/ecc-curve.h>
50 #define TOMPZ(x) (*((mpz_t*)(x)))
52 static inline const struct ecc_curve *get_supported_curve(int curve);
54 static void rnd_func(void *_ctx, unsigned length, uint8_t * data)
56 if (_gnutls_rnd(GNUTLS_RND_RANDOM, data, length) < 0) {
58 _gnutls_switch_fips_state(FIPS_STATE_ERROR);
66 ecc_scalar_zclear (struct ecc_scalar *s)
68 memset(s->p, 0, ecc_size(s->ecc)*sizeof(mp_limb_t));
73 ecc_point_zclear (struct ecc_point *p)
75 memset(p->p, 0, ecc_size_a(p->ecc)*sizeof(mp_limb_t));
81 _dsa_params_to_pubkey(const gnutls_pk_params_st * pk_params,
82 struct dsa_public_key *pub)
84 memcpy(&pub->p, pk_params->params[DSA_P], sizeof(mpz_t));
86 if (pk_params->params[DSA_Q])
87 memcpy(&pub->q, pk_params->params[DSA_Q], sizeof(mpz_t));
88 memcpy(&pub->g, pk_params->params[DSA_G], sizeof(mpz_t));
90 if (pk_params->params[DSA_Y] != NULL)
91 memcpy(&pub->y, pk_params->params[DSA_Y], sizeof(mpz_t));
95 _dsa_params_to_privkey(const gnutls_pk_params_st * pk_params,
96 struct dsa_private_key *pub)
98 memcpy(&pub->x, pk_params->params[4], sizeof(mpz_t));
102 _rsa_params_to_privkey(const gnutls_pk_params_st * pk_params,
103 struct rsa_private_key *priv)
105 memcpy(&priv->d, pk_params->params[2], sizeof(mpz_t));
106 memcpy(&priv->p, pk_params->params[3], sizeof(mpz_t));
107 memcpy(&priv->q, pk_params->params[4], sizeof(mpz_t));
108 memcpy(&priv->c, pk_params->params[5], sizeof(mpz_t));
109 memcpy(&priv->a, pk_params->params[6], sizeof(mpz_t));
110 memcpy(&priv->b, pk_params->params[7], sizeof(mpz_t));
112 nettle_mpz_sizeinbase_256_u(TOMPZ
113 (pk_params->params[RSA_MODULUS]));
117 _rsa_params_to_pubkey(const gnutls_pk_params_st * pk_params,
118 struct rsa_public_key *pub)
120 memcpy(&pub->n, pk_params->params[RSA_MODULUS], sizeof(mpz_t));
121 memcpy(&pub->e, pk_params->params[RSA_PUB], sizeof(mpz_t));
122 pub->size = nettle_mpz_sizeinbase_256_u(pub->n);
126 _ecc_params_to_privkey(const gnutls_pk_params_st * pk_params,
127 struct ecc_scalar *priv,
128 const struct ecc_curve *curve)
130 ecc_scalar_init(priv, curve);
131 if (ecc_scalar_set(priv, pk_params->params[ECC_K]) == 0)
132 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
138 _ecc_params_to_pubkey(const gnutls_pk_params_st * pk_params,
139 struct ecc_point *pub, const struct ecc_curve *curve)
141 ecc_point_init(pub, curve);
143 (pub, pk_params->params[ECC_X], pk_params->params[ECC_Y]) == 0)
144 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
150 ecc_shared_secret(struct ecc_scalar *private_key,
151 struct ecc_point *public_key, void *out, unsigned size)
157 ecc_point_init(&r, public_key->ecc);
159 ecc_point_mul(&r, private_key, public_key);
161 ecc_point_get(&r, x, NULL);
162 nettle_mpz_get_str_256(size, out, x);
170 #define MAX_DH_BITS 16*1024
172 /* This is used for DH or ECDH key derivation. In DH for example
173 * it is given the peers Y and our x, and calculates Y^x
175 static int _wrap_nettle_pk_derive(gnutls_pk_algorithm_t algo,
176 gnutls_datum_t * out,
177 const gnutls_pk_params_st * priv,
178 const gnutls_pk_params_st * pub)
184 bigint_t f, x, prime;
185 bigint_t k = NULL, ff;
188 f = pub->params[DH_Y];
189 x = priv->params[DH_X];
190 prime = priv->params[DH_P];
192 ff = _gnutls_mpi_modm(NULL, f, prime);
193 _gnutls_mpi_add_ui(ff, ff, 1);
195 /* check if f==0,1,p-1.
196 * or (ff=f+1) equivalently ff==1,2,p */
197 if ((_gnutls_mpi_cmp_ui(ff, 2) == 0)
198 || (_gnutls_mpi_cmp_ui(ff, 1) == 0)
199 || (_gnutls_mpi_cmp(ff, prime) == 0)) {
201 ret = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
205 /* prevent denial of service */
206 bits = _gnutls_mpi_get_nbits(prime);
207 if (bits == 0 || bits > MAX_DH_BITS) {
209 ret = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
213 k = _gnutls_mpi_alloc_like(prime);
216 ret = GNUTLS_E_MEMORY_ERROR;
220 _gnutls_mpi_powm(k, f, x, prime);
222 ret = _gnutls_mpi_dprint(k, out);
230 _gnutls_mpi_release(&ff);
231 zrelease_temp_mpi_key(&k);
239 struct ecc_scalar ecc_priv;
240 struct ecc_point ecc_pub;
241 const struct ecc_curve *curve;
245 curve = get_supported_curve(priv->flags);
249 (GNUTLS_E_ECC_UNSUPPORTED_CURVE);
251 ret = _ecc_params_to_pubkey(pub, &ecc_pub, curve);
253 return gnutls_assert_val(ret);
256 _ecc_params_to_privkey(priv, &ecc_priv, curve);
258 ecc_point_clear(&ecc_pub);
259 return gnutls_assert_val(ret);
262 out->size = gnutls_ecc_curve_get_size(priv->flags);
263 /*ecc_size(curve)*sizeof(mp_limb_t); */
264 out->data = gnutls_malloc(out->size);
265 if (out->data == NULL) {
268 (GNUTLS_E_MEMORY_ERROR);
272 ecc_shared_secret(&ecc_priv, &ecc_pub, out->data,
276 ecc_point_clear(&ecc_pub);
277 ecc_scalar_zclear(&ecc_priv);
284 ret = GNUTLS_E_INTERNAL_ERROR;
296 _wrap_nettle_pk_encrypt(gnutls_pk_algorithm_t algo,
297 gnutls_datum_t * ciphertext,
298 const gnutls_datum_t * plaintext,
299 const gnutls_pk_params_st * pk_params)
309 struct rsa_public_key pub;
311 _rsa_params_to_pubkey(pk_params, &pub);
314 rsa_encrypt(&pub, NULL, rnd_func,
315 plaintext->size, plaintext->data,
320 (GNUTLS_E_ENCRYPTION_FAILED);
325 _gnutls_mpi_dprint_size(p, ciphertext,
336 ret = GNUTLS_E_INTERNAL_ERROR;
350 _wrap_nettle_pk_decrypt(gnutls_pk_algorithm_t algo,
351 gnutls_datum_t * plaintext,
352 const gnutls_datum_t * ciphertext,
353 const gnutls_pk_params_st * pk_params)
357 plaintext->data = NULL;
359 /* make a sexp from pkey */
363 struct rsa_private_key priv;
364 struct rsa_public_key pub;
368 _rsa_params_to_privkey(pk_params, &priv);
369 _rsa_params_to_pubkey(pk_params, &pub);
371 if (ciphertext->size != pub.size)
374 (GNUTLS_E_DECRYPTION_FAILED);
376 if (_gnutls_mpi_scan_nz
377 (&c, ciphertext->data,
378 ciphertext->size) != 0) {
381 (GNUTLS_E_MPI_SCAN_FAILED);
386 plaintext->data = gnutls_malloc(length);
387 if (plaintext->data == NULL) {
390 (GNUTLS_E_MEMORY_ERROR);
395 rsa_decrypt_tr(&pub, &priv, NULL, rnd_func,
396 &length, plaintext->data,
398 _gnutls_mpi_release(&c);
399 plaintext->size = length;
404 (GNUTLS_E_DECRYPTION_FAILED);
412 ret = GNUTLS_E_INTERNAL_ERROR;
420 gnutls_free(plaintext->data);
426 /* in case of DSA puts into data, r,s
429 _wrap_nettle_pk_sign(gnutls_pk_algorithm_t algo,
430 gnutls_datum_t * signature,
431 const gnutls_datum_t * vdata,
432 const gnutls_pk_params_st * pk_params)
435 unsigned int hash_len;
436 const mac_entry_st *me;
439 case GNUTLS_PK_EC: /* we do ECDSA */
441 struct ecc_scalar priv;
442 struct dsa_signature sig;
443 int curve_id = pk_params->flags;
444 const struct ecc_curve *curve;
446 curve = get_supported_curve(curve_id);
450 (GNUTLS_E_ECC_UNSUPPORTED_CURVE);
453 _ecc_params_to_privkey(pk_params, &priv,
456 return gnutls_assert_val(ret);
458 dsa_signature_init(&sig);
460 me = _gnutls_dsa_q_to_hash(algo, pk_params,
463 if (hash_len > vdata->size) {
466 ("Security level of algorithm requires hash %s(%d) or better\n",
467 _gnutls_mac_get_name(me), hash_len);
468 hash_len = vdata->size;
471 ecdsa_sign(&priv, NULL, rnd_func, hash_len,
475 _gnutls_encode_ber_rs(signature, &sig.r,
478 dsa_signature_clear(&sig);
479 ecc_scalar_zclear(&priv);
489 struct dsa_public_key pub;
490 struct dsa_private_key priv;
491 struct dsa_signature sig;
493 memset(&priv, 0, sizeof(priv));
494 memset(&pub, 0, sizeof(pub));
495 _dsa_params_to_pubkey(pk_params, &pub);
496 _dsa_params_to_privkey(pk_params, &priv);
498 dsa_signature_init(&sig);
500 me = _gnutls_dsa_q_to_hash(algo, pk_params,
503 if (hash_len > vdata->size) {
506 ("Security level of algorithm requires hash %s(%d) or better (have: %d)\n",
507 _gnutls_mac_get_name(me), hash_len, (int)vdata->size);
508 hash_len = vdata->size;
512 _dsa_sign(&pub, &priv, NULL, rnd_func,
513 hash_len, vdata->data, &sig);
516 ret = GNUTLS_E_PK_SIGN_FAILED;
521 _gnutls_encode_ber_rs(signature, &sig.r,
525 dsa_signature_clear(&sig);
535 struct rsa_private_key priv;
536 struct rsa_public_key pub;
539 _rsa_params_to_privkey(pk_params, &priv);
540 _rsa_params_to_pubkey(pk_params, &pub);
545 rsa_pkcs1_sign_tr(&pub, &priv, NULL, rnd_func,
546 vdata->size, vdata->data, s);
549 ret = GNUTLS_E_PK_SIGN_FAILED;
554 _gnutls_mpi_dprint_size(s, signature,
569 ret = GNUTLS_E_INTERNAL_ERROR;
582 _wrap_nettle_pk_verify(gnutls_pk_algorithm_t algo,
583 const gnutls_datum_t * vdata,
584 const gnutls_datum_t * signature,
585 const gnutls_pk_params_st * pk_params)
588 unsigned int hash_len;
589 bigint_t tmp[2] = { NULL, NULL };
592 case GNUTLS_PK_EC: /* ECDSA */
594 struct ecc_point pub;
595 struct dsa_signature sig;
596 int curve_id = pk_params->flags;
597 const struct ecc_curve *curve;
599 curve = get_supported_curve(curve_id);
603 (GNUTLS_E_ECC_UNSUPPORTED_CURVE);
606 _gnutls_decode_ber_rs(signature, &tmp[0],
609 return gnutls_assert_val(ret);
612 _ecc_params_to_pubkey(pk_params, &pub, curve);
618 memcpy(&sig.r, tmp[0], sizeof(sig.r));
619 memcpy(&sig.s, tmp[1], sizeof(sig.s));
621 _gnutls_dsa_q_to_hash(algo, pk_params, &hash_len);
623 if (hash_len > vdata->size)
624 hash_len = vdata->size;
627 ecdsa_verify(&pub, hash_len, vdata->data,
631 ret = GNUTLS_E_PK_SIG_VERIFY_FAILED;
635 ecc_point_clear(&pub);
640 struct dsa_public_key pub;
641 struct dsa_signature sig;
644 _gnutls_decode_ber_rs(signature, &tmp[0],
650 memset(&pub, 0, sizeof(pub));
651 _dsa_params_to_pubkey(pk_params, &pub);
652 memcpy(&sig.r, tmp[0], sizeof(sig.r));
653 memcpy(&sig.s, tmp[1], sizeof(sig.s));
655 _gnutls_dsa_q_to_hash(algo, pk_params, &hash_len);
657 if (hash_len > vdata->size)
658 hash_len = vdata->size;
661 _dsa_verify(&pub, hash_len, vdata->data, &sig);
664 ret = GNUTLS_E_PK_SIG_VERIFY_FAILED;
672 struct rsa_public_key pub;
674 _rsa_params_to_pubkey(pk_params, &pub);
676 if (signature->size != pub.size)
679 (GNUTLS_E_PK_SIG_VERIFY_FAILED);
682 _gnutls_mpi_scan_nz(&tmp[0], signature->data,
690 rsa_pkcs1_verify(&pub, vdata->size,
691 vdata->data, TOMPZ(tmp[0]));
695 (GNUTLS_E_PK_SIG_VERIFY_FAILED);
703 ret = GNUTLS_E_INTERNAL_ERROR;
709 _gnutls_mpi_release(&tmp[0]);
710 _gnutls_mpi_release(&tmp[1]);
714 static inline const struct ecc_curve *get_supported_curve(int curve)
717 #ifdef ENABLE_NON_SUITEB_CURVES
718 case GNUTLS_ECC_CURVE_SECP192R1:
719 return &nettle_secp_192r1;
720 case GNUTLS_ECC_CURVE_SECP224R1:
721 return &nettle_secp_224r1;
723 case GNUTLS_ECC_CURVE_SECP256R1:
724 return &nettle_secp_256r1;
725 case GNUTLS_ECC_CURVE_SECP384R1:
726 return &nettle_secp_384r1;
727 case GNUTLS_ECC_CURVE_SECP521R1:
728 return &nettle_secp_521r1;
734 static int _wrap_nettle_pk_curve_exists(gnutls_ecc_curve_t curve)
736 return ((get_supported_curve(curve)!=NULL)?1:0);
739 /* Generates algorithm's parameters. That is:
740 * For DSA: p, q, and g are generated.
742 * For ECDSA: just checks the curve is ok
745 wrap_nettle_pk_generate_params(gnutls_pk_algorithm_t algo,
746 unsigned int level /*bits or curve*/ ,
747 gnutls_pk_params_st * params)
750 unsigned int i, q_bits;
758 struct dsa_public_key pub;
759 struct dsa_private_key priv;
760 struct dss_params_validation_seeds cert;
763 dsa_public_key_init(&pub);
764 dsa_private_key_init(&priv);
766 q_bits = _gnutls_pk_bits_to_subgroup_bits(level);
768 return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
770 #ifdef ENABLE_FIPS140
771 if (algo==GNUTLS_PK_DSA)
777 dsa_generate_dss_keypair(&pub, &priv, &cert,
784 ret = GNUTLS_E_PK_GENERATION_ERROR;
788 /* verify the generated parameters */
789 ret = dsa_validate_dss_keypair(&pub, &cert, index);
792 ret = GNUTLS_E_PK_GENERATION_ERROR;
797 dsa_generate_keypair(&pub, &priv,
803 ret = GNUTLS_E_PK_GENERATION_ERROR;
808 params->params_nr = 0;
809 for (i = 0; i < DSA_PRIVATE_PARAMS-2; i++) {
811 _gnutls_mpi_alloc_like(&pub.p);
812 if (params->params[i] == NULL) {
813 ret = GNUTLS_E_MEMORY_ERROR;
820 _gnutls_mpi_set(params->params[0], pub.p);
821 _gnutls_mpi_set(params->params[1], pub.q);
822 _gnutls_mpi_set(params->params[2], pub.g);
825 dsa_private_key_clear(&priv);
826 dsa_public_key_clear(&pub);
839 return GNUTLS_E_INVALID_REQUEST;
847 for (i = 0; i < params->params_nr; i++) {
848 _gnutls_mpi_release(¶ms->params[i]);
850 params->params_nr = 0;
856 /* To generate a DH key either q must be set in the params or
857 * level should be set to the number of required bits.
860 wrap_nettle_pk_generate_keys(gnutls_pk_algorithm_t algo,
861 unsigned int level /*bits */ ,
862 gnutls_pk_params_st * params)
871 struct dsa_public_key pub;
877 if (algo != params->algo)
878 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
880 _dsa_params_to_pubkey(params, &pub);
882 if (params->params[DSA_Q] != NULL)
885 if (algo == GNUTLS_PK_DSA && have_q == 0)
886 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
897 nettle_mpz_random(x, NULL, rnd_func, r);
901 level = mpz_sizeinbase(pub.p, 2);
902 nettle_mpz_random_size(x, NULL, rnd_func, level);
904 if (level >= mpz_sizeinbase(pub.p, 2))
905 mpz_mod(x, x, pub.p);
908 mpz_powm(y, pub.g, x, pub.p);
911 if (max_tries <= 0) {
913 ret = GNUTLS_E_RANDOM_FAILED;
916 } while(mpz_cmp_ui(y, 1) == 0);
918 params->params[DSA_Y] = _gnutls_mpi_alloc_like(&pub.p);
919 if (params->params[DSA_Y] == NULL) {
920 ret = GNUTLS_E_MEMORY_ERROR;
924 params->params[DSA_X] = _gnutls_mpi_alloc_like(&pub.p);
925 if (params->params[DSA_X] == NULL) {
926 _gnutls_mpi_release(¶ms->params[DSA_Y]);
927 ret = GNUTLS_E_MEMORY_ERROR;
931 _gnutls_mpi_set(params->params[DSA_Y], y);
932 _gnutls_mpi_set(params->params[DSA_X], x);
933 params->params_nr += 2;
949 struct rsa_public_key pub;
950 struct rsa_private_key priv;
952 rsa_public_key_init(&pub);
953 rsa_private_key_init(&priv);
955 _gnutls_mpi_set_ui(&pub.e, 65537);
958 rsa_generate_keypair(&pub, &priv, NULL,
959 rnd_func, NULL, NULL,
963 ret = GNUTLS_E_INTERNAL_ERROR;
967 params->params_nr = 0;
968 for (i = 0; i < RSA_PRIVATE_PARAMS; i++) {
970 _gnutls_mpi_alloc_like(&pub.n);
971 if (params->params[i] == NULL) {
972 ret = GNUTLS_E_MEMORY_ERROR;
981 _gnutls_mpi_set(params->params[0], pub.n);
982 _gnutls_mpi_set(params->params[1], pub.e);
983 _gnutls_mpi_set(params->params[2], priv.d);
984 _gnutls_mpi_set(params->params[3], priv.p);
985 _gnutls_mpi_set(params->params[4], priv.q);
986 _gnutls_mpi_set(params->params[5], priv.c);
987 _gnutls_mpi_set(params->params[6], priv.a);
988 _gnutls_mpi_set(params->params[7], priv.b);
991 rsa_private_key_clear(&priv);
992 rsa_public_key_clear(&pub);
1001 struct ecc_scalar key;
1002 struct ecc_point pub;
1003 const struct ecc_curve *curve;
1005 curve = get_supported_curve(level);
1009 (GNUTLS_E_ECC_UNSUPPORTED_CURVE);
1011 ecc_scalar_init(&key, curve);
1012 ecc_point_init(&pub, curve);
1014 ecdsa_generate_keypair(&pub, &key, NULL, rnd_func);
1016 params->params[ECC_X] = _gnutls_mpi_new(0);
1017 params->params[ECC_Y] = _gnutls_mpi_new(0);
1018 params->params[ECC_K] = _gnutls_mpi_new(0);
1020 if (params->params[ECC_X] == NULL
1021 || params->params[ECC_Y] == NULL
1022 || params->params[ECC_K] == NULL) {
1023 _gnutls_mpi_release(¶ms->
1025 _gnutls_mpi_release(¶ms->
1027 _gnutls_mpi_release(¶ms->
1029 ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
1033 params->flags = level;
1034 params->params_nr = ECC_PRIVATE_PARAMS;
1036 ecc_point_get(&pub, TOMPZ(params->params[ECC_X]),
1037 TOMPZ(params->params[ECC_Y]));
1038 ecc_scalar_get(&key, TOMPZ(params->params[ECC_K]));
1043 ecc_point_clear(&pub);
1044 ecc_scalar_clear(&key);
1053 return GNUTLS_E_INVALID_REQUEST;
1061 for (i = 0; i < params->params_nr; i++) {
1062 _gnutls_mpi_release(¶ms->params[i]);
1064 params->params_nr = 0;
1071 wrap_nettle_pk_verify_params(gnutls_pk_algorithm_t algo,
1072 const gnutls_pk_params_st * params)
1079 bigint_t t1 = NULL, t2 = NULL;
1081 if (params->params_nr != RSA_PRIVATE_PARAMS)
1084 (GNUTLS_E_INVALID_REQUEST);
1086 t1 = _gnutls_mpi_new(256);
1090 (GNUTLS_E_MEMORY_ERROR);
1092 _gnutls_mpi_mulm(t1, params->params[RSA_PRIME1],
1093 params->params[RSA_PRIME2],
1094 params->params[RSA_MODULUS]);
1095 if (_gnutls_mpi_cmp_ui(t1, 0) != 0) {
1098 (GNUTLS_E_ILLEGAL_PARAMETER);
1102 mpz_invert(TOMPZ(t1),
1103 TOMPZ(params->params[RSA_PRIME2]),
1104 TOMPZ(params->params[RSA_PRIME1]));
1105 if (_gnutls_mpi_cmp(t1, params->params[RSA_COEF])
1109 (GNUTLS_E_ILLEGAL_PARAMETER);
1113 /* [RSA_PRIME1] = d % p-1, [RSA_PRIME2] = d % q-1 */
1114 _gnutls_mpi_sub_ui(t1, params->params[RSA_PRIME1],
1116 t2 = _gnutls_mpi_modm(NULL, params->params[RSA_PRIV], t1);
1120 (GNUTLS_E_MEMORY_ERROR);
1124 if (_gnutls_mpi_cmp(params->params[RSA_E1], t2) !=
1128 (GNUTLS_E_ILLEGAL_PARAMETER);
1132 _gnutls_mpi_sub_ui(t1, params->params[RSA_PRIME2],
1134 zrelease_mpi_key(&t2);
1136 t2 = _gnutls_mpi_modm(NULL, params->params[RSA_PRIV], t1);
1140 (GNUTLS_E_MEMORY_ERROR);
1144 if (_gnutls_mpi_cmp(params->params[RSA_E2], t2) !=
1148 (GNUTLS_E_ILLEGAL_PARAMETER);
1155 zrelease_mpi_key(&t1);
1156 zrelease_mpi_key(&t2);
1164 if (params->params_nr != DSA_PRIVATE_PARAMS)
1167 (GNUTLS_E_INVALID_REQUEST);
1169 t1 = _gnutls_mpi_new(256);
1173 (GNUTLS_E_MEMORY_ERROR);
1175 _gnutls_mpi_powm(t1, params->params[DSA_G],
1176 params->params[DSA_X],
1177 params->params[DSA_P]);
1179 if (_gnutls_mpi_cmp(t1, params->params[DSA_Y]) !=
1183 (GNUTLS_E_ILLEGAL_PARAMETER);
1190 zrelease_mpi_key(&t1);
1196 struct ecc_point r, pub;
1197 struct ecc_scalar priv;
1198 mpz_t x1, y1, x2, y2;
1199 const struct ecc_curve *curve;
1201 if (params->params_nr != ECC_PRIVATE_PARAMS)
1204 (GNUTLS_E_INVALID_REQUEST);
1206 curve = get_supported_curve(params->flags);
1210 (GNUTLS_E_ECC_UNSUPPORTED_CURVE);
1212 ret = _ecc_params_to_pubkey(params, &pub, curve);
1214 return gnutls_assert_val(ret);
1216 ret = _ecc_params_to_privkey(params, &priv, curve);
1218 ecc_point_clear(&pub);
1219 return gnutls_assert_val(ret);
1222 ecc_point_init(&r, curve);
1223 /* verify that x,y lie on the curve */
1225 ecc_point_set(&r, TOMPZ(params->params[ECC_X]),
1226 TOMPZ(params->params[ECC_Y]));
1230 (GNUTLS_E_ILLEGAL_PARAMETER);
1233 ecc_point_clear(&r);
1235 ecc_point_init(&r, curve);
1236 ecc_point_mul_g(&r, &priv);
1240 ecc_point_get(&r, x1, y1);
1241 ecc_point_zclear(&r);
1245 ecc_point_get(&pub, x2, y2);
1247 /* verify that k*(Gx,Gy)=(x,y) */
1248 if (mpz_cmp(x1, x2) != 0 || mpz_cmp(y1, y2) != 0) {
1251 (GNUTLS_E_ILLEGAL_PARAMETER);
1258 ecc_scalar_zclear(&priv);
1259 ecc_point_clear(&pub);
1263 ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1269 static int calc_rsa_exp(gnutls_pk_params_st * params)
1271 bigint_t tmp = _gnutls_mpi_alloc_like(params->params[0]);
1273 if (params->params_nr < RSA_PRIVATE_PARAMS - 2) {
1275 return GNUTLS_E_INTERNAL_ERROR;
1280 return GNUTLS_E_MEMORY_ERROR;
1283 /* [6] = d % p-1, [7] = d % q-1 */
1284 _gnutls_mpi_sub_ui(tmp, params->params[3], 1);
1286 _gnutls_mpi_modm(NULL, params->params[2] /*d */ , tmp);
1288 _gnutls_mpi_sub_ui(tmp, params->params[4], 1);
1290 _gnutls_mpi_modm(NULL, params->params[2] /*d */ , tmp);
1292 zrelease_mpi_key(&tmp);
1294 if (params->params[7] == NULL || params->params[6] == NULL) {
1296 return GNUTLS_E_MEMORY_ERROR;
1304 wrap_nettle_pk_fixup(gnutls_pk_algorithm_t algo,
1305 gnutls_direction_t direction,
1306 gnutls_pk_params_st * params)
1310 if (direction == GNUTLS_IMPORT && algo == GNUTLS_PK_RSA) {
1311 /* do not trust the generated values. Some old private keys
1312 * generated by us have mess on the values. Those were very
1313 * old but it seemed some of the shipped example private
1316 mpz_invert(TOMPZ(params->params[RSA_COEF]),
1317 TOMPZ(params->params[RSA_PRIME2]),
1318 TOMPZ(params->params[RSA_PRIME1]));
1320 /* calculate exp1 [6] and exp2 [7] */
1321 zrelease_mpi_key(¶ms->params[RSA_E1]);
1322 zrelease_mpi_key(¶ms->params[RSA_E2]);
1324 result = calc_rsa_exp(params);
1329 params->params_nr = RSA_PRIVATE_PARAMS;
1336 extract_digest_info(const struct rsa_public_key *key,
1337 gnutls_datum_t * di, uint8_t ** rdi,
1338 const mpz_t signature)
1348 em = gnutls_malloc(key->size);
1354 mpz_powm(m, signature, key->e, key->n);
1356 nettle_mpz_get_str_256(key->size, em, m);
1359 if (em[0] != 0 || em[1] != 1) {
1364 for (i = 2; i < key->size; i++) {
1365 if (em[i] == 0 && i > 2)
1368 if (em[i] != 0xff) {
1379 di->size = key->size - i;
1384 memset(em, 0, sizeof(key->size));
1390 /* Given a signature and parameters, it should return
1391 * the hash algorithm used in the signature. This is a kludge
1392 * but until we deprecate gnutls_pubkey_get_verify_algorithm()
1395 static int wrap_nettle_hash_algorithm(gnutls_pk_algorithm_t pk,
1396 const gnutls_datum_t * sig,
1397 gnutls_pk_params_st * issuer_params,
1398 gnutls_digest_algorithm_t *
1401 uint8_t digest[MAX_HASH_SIZE];
1402 uint8_t *rdi = NULL;
1404 unsigned digest_size;
1406 struct rsa_public_key pub;
1407 const mac_entry_st *me;
1416 me = _gnutls_dsa_q_to_hash(pk, issuer_params, NULL);
1418 *hash_algo = me->id;
1423 if (sig == NULL) { /* return a sensible algorithm */
1425 *hash_algo = GNUTLS_DIG_SHA256;
1429 _rsa_params_to_pubkey(issuer_params, &pub);
1431 digest_size = sizeof(digest);
1433 nettle_mpz_set_str_256_u(s, sig->size, sig->data);
1435 ret = extract_digest_info(&pub, &di, &rdi, s);
1437 ret = GNUTLS_E_PK_SIG_VERIFY_FAILED;
1442 digest_size = sizeof(digest);
1444 decode_ber_digest_info(&di, hash_algo, digest,
1445 &digest_size)) < 0) {
1451 _gnutls_hash_get_algo_len(mac_to_entry(*hash_algo))) {
1453 ret = GNUTLS_E_PK_SIG_VERIFY_FAILED;
1462 ret = GNUTLS_E_INTERNAL_ERROR;
1473 int crypto_pk_prio = INT_MAX;
1475 gnutls_crypto_pk_st _gnutls_pk_ops = {
1476 .hash_algorithm = wrap_nettle_hash_algorithm,
1477 .encrypt = _wrap_nettle_pk_encrypt,
1478 .decrypt = _wrap_nettle_pk_decrypt,
1479 .sign = _wrap_nettle_pk_sign,
1480 .verify = _wrap_nettle_pk_verify,
1481 .verify_params = wrap_nettle_pk_verify_params,
1482 .generate_params = wrap_nettle_pk_generate_params,
1483 .generate_keys = wrap_nettle_pk_generate_keys,
1484 .pk_fixup_private_params = wrap_nettle_pk_fixup,
1485 .derive = _wrap_nettle_pk_derive,
1486 .curve_exists = _wrap_nettle_pk_curve_exists,