2 * Copyright (C) 2010-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
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 3 of
11 * the License, or (at your option) any later version.
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.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 /* This file contains the functions needed for RSA/DSA public key
24 * encryption and signatures.
27 #include <gnutls_int.h>
28 #include <gnutls_mpi.h>
29 #include <gnutls_pk.h>
30 #include <gnutls_errors.h>
31 #include <gnutls_datum.h>
32 #include <gnutls_global.h>
33 #include <gnutls_sig.h>
34 #include <gnutls_num.h>
35 #include <x509/x509_int.h>
36 #include <x509/common.h>
38 #include <gnutls_pk.h>
39 #include <nettle/dsa.h>
40 #include <nettle/rsa.h>
42 #include <gnutls/crypto.h>
45 #define TOMPZ(x) (*((mpz_t*)(x)))
47 static inline int is_supported_curve(int curve
);
50 rnd_func (void *_ctx
, unsigned length
, uint8_t * data
)
52 _gnutls_rnd (GNUTLS_RND_RANDOM
, data
, length
);
56 _dsa_params_to_pubkey (const gnutls_pk_params_st
* pk_params
,
57 struct dsa_public_key
*pub
)
59 memcpy (&pub
->p
, pk_params
->params
[0], sizeof (mpz_t
));
60 memcpy (&pub
->q
, pk_params
->params
[1], sizeof (mpz_t
));
61 memcpy (&pub
->g
, pk_params
->params
[2], sizeof (mpz_t
));
62 memcpy (&pub
->y
, pk_params
->params
[3], sizeof (mpz_t
));
66 _dsa_params_to_privkey (const gnutls_pk_params_st
* pk_params
,
67 struct dsa_private_key
*pub
)
69 memcpy (&pub
->x
, pk_params
->params
[4], sizeof (mpz_t
));
73 _rsa_params_to_privkey (const gnutls_pk_params_st
* pk_params
,
74 struct rsa_private_key
*priv
)
76 memcpy (&priv
->d
, pk_params
->params
[2], sizeof (mpz_t
));
77 memcpy (&priv
->p
, pk_params
->params
[3], sizeof (mpz_t
));
78 memcpy (&priv
->q
, pk_params
->params
[4], sizeof (mpz_t
));
79 memcpy (&priv
->c
, pk_params
->params
[5], sizeof (mpz_t
));
80 memcpy (&priv
->a
, pk_params
->params
[6], sizeof (mpz_t
));
81 memcpy (&priv
->b
, pk_params
->params
[7], sizeof (mpz_t
));
82 priv
->size
= nettle_mpz_sizeinbase_256_u(TOMPZ(pk_params
->params
[RSA_MODULUS
]));
86 _rsa_params_to_pubkey (const gnutls_pk_params_st
* pk_params
,
87 struct rsa_public_key
*pub
)
89 memcpy (&pub
->n
, pk_params
->params
[RSA_MODULUS
], sizeof (mpz_t
));
90 memcpy (&pub
->e
, pk_params
->params
[RSA_PUB
], sizeof (mpz_t
));
91 pub
->size
= nettle_mpz_sizeinbase_256_u(pub
->n
);
95 _ecc_params_to_privkey(const gnutls_pk_params_st
* pk_params
,
98 priv
->type
= PK_PRIVATE
;
99 memcpy(&priv
->prime
, pk_params
->params
[ECC_PRIME
], sizeof(mpz_t
));
100 memcpy(&priv
->order
, pk_params
->params
[ECC_ORDER
], sizeof(mpz_t
));
101 memcpy(&priv
->A
, pk_params
->params
[ECC_A
], sizeof(mpz_t
));
102 memcpy(&priv
->B
, pk_params
->params
[ECC_B
], sizeof(mpz_t
));
103 memcpy(&priv
->Gx
, pk_params
->params
[ECC_GX
], sizeof(mpz_t
));
104 memcpy(&priv
->Gy
, pk_params
->params
[ECC_GY
], sizeof(mpz_t
));
105 memcpy(&priv
->pubkey
.x
, pk_params
->params
[ECC_X
], sizeof(mpz_t
));
106 memcpy(&priv
->pubkey
.y
, pk_params
->params
[ECC_Y
], sizeof(mpz_t
));
107 memcpy(&priv
->k
, pk_params
->params
[ECC_K
], sizeof(mpz_t
));
108 mpz_init_set_ui(priv
->pubkey
.z
, 1);
111 static void _ecc_params_clear(ecc_key
* key
)
113 mpz_clear(key
->pubkey
.z
);
117 _ecc_params_to_pubkey(const gnutls_pk_params_st
* pk_params
,
120 pub
->type
= PK_PUBLIC
;
121 memcpy(&pub
->prime
, pk_params
->params
[ECC_PRIME
], sizeof(mpz_t
));
122 memcpy(&pub
->order
, pk_params
->params
[ECC_ORDER
], sizeof(mpz_t
));
123 memcpy(&pub
->A
, pk_params
->params
[ECC_A
], sizeof(mpz_t
));
124 memcpy(&pub
->B
, pk_params
->params
[ECC_B
], sizeof(mpz_t
));
125 memcpy(&pub
->Gx
, pk_params
->params
[ECC_GX
], sizeof(mpz_t
));
126 memcpy(&pub
->Gy
, pk_params
->params
[ECC_GY
], sizeof(mpz_t
));
127 memcpy(&pub
->pubkey
.x
, pk_params
->params
[ECC_X
], sizeof(mpz_t
));
128 memcpy(&pub
->pubkey
.y
, pk_params
->params
[ECC_Y
], sizeof(mpz_t
));
129 mpz_init_set_ui(pub
->pubkey
.z
, 1);
132 static int _wrap_nettle_pk_derive(gnutls_pk_algorithm_t algo
, gnutls_datum_t
* out
,
133 const gnutls_pk_params_st
* priv
,
134 const gnutls_pk_params_st
* pub
)
142 ecc_key ecc_pub
, ecc_priv
;
143 int curve
= priv
->flags
;
148 if (is_supported_curve(curve
) == 0)
149 return gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE
);
151 _ecc_params_to_pubkey(pub
, &ecc_pub
);
152 _ecc_params_to_privkey(priv
, &ecc_priv
);
155 if (ecc_projective_check_point(&ecc_pub
.pubkey
, pub
->params
[ECC_B
], pub
->params
[ECC_PRIME
]) != 0)
157 ret
= gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER
);
161 out
->data
= gnutls_malloc(sz
);
162 if (out
->data
== NULL
)
164 ret
= gnutls_assert_val(GNUTLS_E_MEMORY_ERROR
);
168 ret
= ecc_shared_secret(&ecc_priv
, &ecc_pub
, out
->data
, &sz
);
170 ret
= gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR
);
173 _ecc_params_clear(&ecc_pub
);
174 _ecc_params_clear(&ecc_priv
);
178 gnutls_free(out
->data
);
187 ret
= GNUTLS_E_INTERNAL_ERROR
;
199 _wrap_nettle_pk_encrypt (gnutls_pk_algorithm_t algo
,
200 gnutls_datum_t
* ciphertext
,
201 const gnutls_datum_t
* plaintext
,
202 const gnutls_pk_params_st
* pk_params
)
213 struct rsa_public_key pub
;
215 _rsa_params_to_pubkey (pk_params
, &pub
);
217 ret
= rsa_encrypt(&pub
, NULL
, rnd_func
, plaintext
->size
, plaintext
->data
, p
);
220 ret
= gnutls_assert_val(GNUTLS_E_ENCRYPTION_FAILED
);
224 ret
= _gnutls_mpi_dprint_size (p
, ciphertext
, plaintext
->size
);
235 ret
= GNUTLS_E_INTERNAL_ERROR
;
248 _wrap_nettle_pk_decrypt (gnutls_pk_algorithm_t algo
,
249 gnutls_datum_t
* plaintext
,
250 const gnutls_datum_t
* ciphertext
,
251 const gnutls_pk_params_st
* pk_params
)
255 plaintext
->data
= NULL
;
257 /* make a sexp from pkey */
262 struct rsa_private_key priv
;
263 struct rsa_public_key pub
;
267 _rsa_params_to_privkey (pk_params
, &priv
);
268 _rsa_params_to_pubkey (pk_params
, &pub
);
270 if (_gnutls_mpi_scan_nz (&c
, ciphertext
->data
, ciphertext
->size
) != 0)
272 ret
= gnutls_assert_val(GNUTLS_E_MPI_SCAN_FAILED
);
277 plaintext
->data
= gnutls_malloc(length
);
278 if (plaintext
->data
== NULL
)
280 ret
= gnutls_assert_val(GNUTLS_E_MEMORY_ERROR
);
284 ret
= rsa_decrypt_tr(&pub
, &priv
, NULL
, rnd_func
, &length
, plaintext
->data
,
286 _gnutls_mpi_release (&c
);
287 plaintext
->size
= length
;
291 ret
= gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED
);
299 ret
= GNUTLS_E_INTERNAL_ERROR
;
307 gnutls_free(plaintext
->data
);
312 /* in case of DSA puts into data, r,s
315 _wrap_nettle_pk_sign (gnutls_pk_algorithm_t algo
,
316 gnutls_datum_t
* signature
,
317 const gnutls_datum_t
* vdata
,
318 const gnutls_pk_params_st
* pk_params
)
322 unsigned int hash_len
;
326 case GNUTLS_PK_EC
: /* we do ECDSA */
329 struct dsa_signature sig
;
330 int curve_id
= pk_params
->flags
;
332 if (is_supported_curve(curve_id
) == 0)
333 return gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE
);
335 _ecc_params_to_privkey(pk_params
, &priv
);
337 dsa_signature_init (&sig
);
339 hash
= _gnutls_dsa_q_to_hash (algo
, pk_params
, &hash_len
);
340 if (hash_len
> vdata
->size
)
343 _gnutls_debug_log("Security level of algorithm requires hash %s(%d) or better\n", gnutls_mac_get_name(hash
), hash_len
);
344 hash_len
= vdata
->size
;
347 ret
= ecc_sign_hash(vdata
->data
, hash_len
,
348 &sig
, NULL
, rnd_func
, &priv
, curve_id
);
352 ret
= GNUTLS_E_PK_SIGN_FAILED
;
356 ret
= _gnutls_encode_ber_rs (signature
, &sig
.r
, &sig
.s
);
359 dsa_signature_clear (&sig
);
360 _ecc_params_clear( &priv
);
371 struct dsa_public_key pub
;
372 struct dsa_private_key priv
;
373 struct dsa_signature sig
;
375 memset(&priv
, 0, sizeof(priv
));
376 memset(&pub
, 0, sizeof(pub
));
377 _dsa_params_to_pubkey (pk_params
, &pub
);
378 _dsa_params_to_privkey (pk_params
, &priv
);
380 dsa_signature_init (&sig
);
382 hash
= _gnutls_dsa_q_to_hash (algo
, pk_params
, &hash_len
);
383 if (hash_len
> vdata
->size
)
386 _gnutls_debug_log("Security level of algorithm requires hash %s(%d) or better\n", gnutls_mac_get_name(hash
), hash_len
);
387 hash_len
= vdata
->size
;
391 _dsa_sign (&pub
, &priv
, NULL
, rnd_func
,
392 hash_len
, vdata
->data
, &sig
);
396 ret
= GNUTLS_E_PK_SIGN_FAILED
;
400 ret
= _gnutls_encode_ber_rs (signature
, &sig
.r
, &sig
.s
);
403 dsa_signature_clear (&sig
);
414 struct rsa_private_key priv
;
415 struct rsa_public_key pub
;
418 _rsa_params_to_privkey (pk_params
, &priv
);
419 _rsa_params_to_pubkey (pk_params
, &pub
);
423 ret
= rsa_pkcs1_sign_tr(&pub
, &priv
, NULL
, rnd_func
,
424 vdata
->size
, vdata
->data
, s
);
428 ret
= GNUTLS_E_PK_SIGN_FAILED
;
432 ret
= _gnutls_mpi_dprint (s
, signature
);
447 ret
= GNUTLS_E_INTERNAL_ERROR
;
459 _wrap_nettle_pk_verify (gnutls_pk_algorithm_t algo
,
460 const gnutls_datum_t
* vdata
,
461 const gnutls_datum_t
* signature
,
462 const gnutls_pk_params_st
* pk_params
)
465 unsigned int hash_len
;
466 bigint_t tmp
[2] = { NULL
, NULL
};
470 case GNUTLS_PK_EC
: /* ECDSA */
473 struct dsa_signature sig
;
475 int curve_id
= pk_params
->flags
;
477 if (is_supported_curve(curve_id
) == 0)
478 return gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE
);
480 ret
= _gnutls_decode_ber_rs (signature
, &tmp
[0], &tmp
[1]);
487 _ecc_params_to_pubkey(pk_params
, &pub
);
488 memcpy (&sig
.r
, tmp
[0], sizeof (sig
.r
));
489 memcpy (&sig
.s
, tmp
[1], sizeof (sig
.s
));
491 _gnutls_dsa_q_to_hash (algo
, pk_params
, &hash_len
);
492 if (hash_len
> vdata
->size
)
493 hash_len
= vdata
->size
;
495 ret
= ecc_verify_hash(&sig
, vdata
->data
, hash_len
, &stat
, &pub
, curve_id
);
496 if (ret
!= 0 || stat
!= 1)
499 ret
= GNUTLS_E_PK_SIG_VERIFY_FAILED
;
504 _gnutls_mpi_release (&tmp
[0]);
505 _gnutls_mpi_release (&tmp
[1]);
506 _ecc_params_clear( &pub
);
511 struct dsa_public_key pub
;
512 struct dsa_signature sig
;
514 ret
= _gnutls_decode_ber_rs (signature
, &tmp
[0], &tmp
[1]);
520 memset(&pub
, 0, sizeof(pub
));
521 _dsa_params_to_pubkey (pk_params
, &pub
);
522 memcpy (&sig
.r
, tmp
[0], sizeof (sig
.r
));
523 memcpy (&sig
.s
, tmp
[1], sizeof (sig
.s
));
525 _gnutls_dsa_q_to_hash (algo
, pk_params
, &hash_len
);
526 if (hash_len
> vdata
->size
)
527 hash_len
= vdata
->size
;
529 ret
= _dsa_verify (&pub
, hash_len
, vdata
->data
, &sig
);
533 ret
= GNUTLS_E_PK_SIG_VERIFY_FAILED
;
538 _gnutls_mpi_release (&tmp
[0]);
539 _gnutls_mpi_release (&tmp
[1]);
544 struct rsa_public_key pub
;
546 _rsa_params_to_pubkey (pk_params
, &pub
);
548 ret
= _gnutls_mpi_scan_nz (&tmp
[0], signature
->data
, signature
->size
);
555 ret
= rsa_pkcs1_verify (&pub
, vdata
->size
, vdata
->data
, TOMPZ(tmp
[0]));
557 ret
= gnutls_assert_val(GNUTLS_E_PK_SIG_VERIFY_FAILED
);
560 _gnutls_mpi_release (&tmp
[0]);
565 ret
= GNUTLS_E_INTERNAL_ERROR
;
574 static inline int is_supported_curve(int curve
)
576 if (gnutls_ecc_curve_get_name(curve
) != NULL
)
584 wrap_nettle_pk_generate_params (gnutls_pk_algorithm_t algo
,
585 unsigned int level
/*bits */ ,
586 gnutls_pk_params_st
* params
)
589 unsigned int i
, q_bits
;
591 memset(params
, 0, sizeof(*params
));
598 struct dsa_public_key pub
;
599 struct dsa_private_key priv
;
601 dsa_public_key_init (&pub
);
602 dsa_private_key_init (&priv
);
604 /* the best would be to use _gnutls_pk_bits_to_subgroup_bits()
605 * but we do NIST DSA here */
612 dsa_generate_keypair (&pub
, &priv
, NULL
,
613 rnd_func
, NULL
, NULL
, level
, q_bits
);
617 ret
= GNUTLS_E_INTERNAL_ERROR
;
621 params
->params_nr
= 0;
622 for (i
= 0; i
< DSA_PRIVATE_PARAMS
; i
++)
624 params
->params
[i
] = _gnutls_mpi_alloc_like (&pub
.p
);
625 if (params
->params
[i
] == NULL
)
627 ret
= GNUTLS_E_MEMORY_ERROR
;
634 _gnutls_mpi_set (params
->params
[0], pub
.p
);
635 _gnutls_mpi_set (params
->params
[1], pub
.q
);
636 _gnutls_mpi_set (params
->params
[2], pub
.g
);
637 _gnutls_mpi_set (params
->params
[3], pub
.y
);
638 _gnutls_mpi_set (params
->params
[4], priv
.x
);
641 dsa_private_key_clear (&priv
);
642 dsa_public_key_clear (&pub
);
651 struct rsa_public_key pub
;
652 struct rsa_private_key priv
;
654 rsa_public_key_init (&pub
);
655 rsa_private_key_init (&priv
);
657 _gnutls_mpi_set_ui (&pub
.e
, 65537);
660 rsa_generate_keypair (&pub
, &priv
, NULL
,
661 rnd_func
, NULL
, NULL
, level
, 0);
665 ret
= GNUTLS_E_INTERNAL_ERROR
;
669 params
->params_nr
= 0;
670 for (i
= 0; i
< RSA_PRIVATE_PARAMS
; i
++)
672 params
->params
[i
] = _gnutls_mpi_alloc_like (&pub
.n
);
673 if (params
->params
[i
] == NULL
)
675 ret
= GNUTLS_E_MEMORY_ERROR
;
684 _gnutls_mpi_set (params
->params
[0], pub
.n
);
685 _gnutls_mpi_set (params
->params
[1], pub
.e
);
686 _gnutls_mpi_set (params
->params
[2], priv
.d
);
687 _gnutls_mpi_set (params
->params
[3], priv
.p
);
688 _gnutls_mpi_set (params
->params
[4], priv
.q
);
689 _gnutls_mpi_set (params
->params
[5], priv
.c
);
690 _gnutls_mpi_set (params
->params
[6], priv
.a
);
691 _gnutls_mpi_set (params
->params
[7], priv
.b
);
694 rsa_private_key_clear (&priv
);
695 rsa_public_key_clear (&pub
);
705 ecc_set_type tls_ecc_set
;
706 const gnutls_ecc_curve_entry_st
*st
;
708 st
= _gnutls_ecc_curve_get_params(level
);
710 return gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE
);
712 tls_ecc_set
.size
= st
->size
;
713 tls_ecc_set
.prime
= st
->prime
;
714 tls_ecc_set
.order
= st
->order
;
715 tls_ecc_set
.Gx
= st
->Gx
;
716 tls_ecc_set
.Gy
= st
->Gy
;
717 tls_ecc_set
.A
= st
->A
;
718 tls_ecc_set
.B
= st
->B
;
720 ret
= ecc_make_key(NULL
, rnd_func
, &key
, &tls_ecc_set
, st
->id
);
722 return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR
);
724 params
->params_nr
= 0;
725 for (i
= 0; i
< ECC_PRIVATE_PARAMS
; i
++)
727 params
->params
[i
] = _gnutls_mpi_alloc_like(&key
.prime
);
728 if (params
->params
[i
] == NULL
)
730 ret
= GNUTLS_E_MEMORY_ERROR
;
735 params
->flags
= level
;
737 mpz_set(TOMPZ(params
->params
[ECC_PRIME
]), key
.prime
);
738 mpz_set(TOMPZ(params
->params
[ECC_ORDER
]), key
.order
);
739 mpz_set(TOMPZ(params
->params
[ECC_A
]), key
.A
);
740 mpz_set(TOMPZ(params
->params
[ECC_B
]), key
.B
);
741 mpz_set(TOMPZ(params
->params
[ECC_GX
]), key
.Gx
);
742 mpz_set(TOMPZ(params
->params
[ECC_GY
]), key
.Gy
);
743 mpz_set(TOMPZ(params
->params
[ECC_X
]), key
.pubkey
.x
);
744 mpz_set(TOMPZ(params
->params
[ECC_Y
]), key
.pubkey
.y
);
745 mpz_set(TOMPZ(params
->params
[ECC_K
]), key
.k
);
757 return GNUTLS_E_INVALID_REQUEST
;
764 for (i
= 0; i
< params
->params_nr
; i
++)
766 _gnutls_mpi_release (¶ms
->params
[i
]);
768 params
->params_nr
= 0;
774 wrap_nettle_pk_verify_params (gnutls_pk_algorithm_t algo
,
775 const gnutls_pk_params_st
* params
)
783 bigint_t t1
= NULL
, t2
= NULL
;
785 if (params
->params_nr
!= RSA_PRIVATE_PARAMS
)
786 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
788 t1
= _gnutls_mpi_new (256);
790 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR
);
792 _gnutls_mpi_mulm (t1
, params
->params
[RSA_PRIME1
], params
->params
[RSA_PRIME2
], params
->params
[RSA_MODULUS
]);
793 if (_gnutls_mpi_cmp_ui(t1
, 0) != 0)
795 ret
= gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER
);
799 mpz_invert (TOMPZ(t1
), TOMPZ (params
->params
[RSA_PRIME2
]), TOMPZ (params
->params
[RSA_PRIME1
]));
800 if (_gnutls_mpi_cmp(t1
, params
->params
[RSA_COEF
]) != 0)
802 ret
= gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER
);
806 /* [RSA_PRIME1] = d % p-1, [RSA_PRIME2] = d % q-1 */
807 _gnutls_mpi_sub_ui (t1
, params
->params
[RSA_PRIME1
], 1);
808 t2
= _gnutls_mpi_mod (params
->params
[RSA_PRIV
], t1
);
811 ret
= gnutls_assert_val(GNUTLS_E_MEMORY_ERROR
);
815 if (_gnutls_mpi_cmp(params
->params
[RSA_E1
], t2
) != 0)
817 ret
= gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER
);
821 _gnutls_mpi_sub_ui (t1
, params
->params
[RSA_PRIME2
], 1);
822 _gnutls_mpi_release(&t2
);
824 t2
= _gnutls_mpi_mod (params
->params
[RSA_PRIV
], t1
);
827 ret
= gnutls_assert_val(GNUTLS_E_MEMORY_ERROR
);
831 if (_gnutls_mpi_cmp(params
->params
[RSA_E2
], t2
) != 0)
833 ret
= gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER
);
840 _gnutls_mpi_release(&t1
);
841 _gnutls_mpi_release(&t2
);
849 if (params
->params_nr
!= DSA_PRIVATE_PARAMS
)
850 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
852 t1
= _gnutls_mpi_new (256);
854 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR
);
856 _gnutls_mpi_powm (t1
, params
->params
[DSA_G
], params
->params
[DSA_X
], params
->params
[DSA_P
]);
858 if (_gnutls_mpi_cmp(t1
, params
->params
[DSA_Y
]) != 0)
860 ret
= gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER
);
867 _gnutls_mpi_release(&t1
);
873 int curve
= params
->flags
;
878 if (params
->params_nr
!= ECC_PRIVATE_PARAMS
)
879 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
881 if (is_supported_curve(curve
) == 0)
882 return gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE
);
884 _ecc_params_to_privkey(params
, &ecc_priv
);
887 /* verify that x,y lie on the curve */
888 ret
= ecc_projective_check_point(&ecc_priv
.pubkey
, TOMPZ(params
->params
[ECC_B
]), params
->params
[ECC_PRIME
]);
891 ret
= gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER
);
895 memcpy(&zero
.x
, ecc_priv
.Gx
, sizeof(mpz_t
));
896 memcpy(&zero
.y
, ecc_priv
.Gy
, sizeof(mpz_t
));
897 memcpy(&zero
.z
, ecc_priv
.pubkey
.z
, sizeof(mpz_t
)); /* z = 1 */
899 /* verify that k*(Gx,Gy)=(x,y) */
900 ret
= ecc_mulmod_cached(ecc_priv
.k
, curve
, R
, TOMPZ(params
->params
[ECC_A
]), TOMPZ(params
->params
[ECC_PRIME
]), 1);
903 ret
= gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER
);
907 if (mpz_cmp(ecc_priv
.pubkey
.x
, R
->x
) != 0 || mpz_cmp(ecc_priv
.pubkey
.y
, R
->y
) != 0)
909 ret
= gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER
);
916 _ecc_params_clear(&ecc_priv
);
921 ret
= gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
927 static int calc_rsa_exp (gnutls_pk_params_st
* params
)
929 bigint_t tmp
= _gnutls_mpi_alloc_like (params
->params
[0]);
931 if (params
->params_nr
< RSA_PRIVATE_PARAMS
- 2)
934 return GNUTLS_E_INTERNAL_ERROR
;
940 return GNUTLS_E_MEMORY_ERROR
;
943 /* [6] = d % p-1, [7] = d % q-1 */
944 _gnutls_mpi_sub_ui (tmp
, params
->params
[3], 1);
945 params
->params
[6] = _gnutls_mpi_mod (params
->params
[2] /*d */ , tmp
);
947 _gnutls_mpi_sub_ui (tmp
, params
->params
[4], 1);
948 params
->params
[7] = _gnutls_mpi_mod (params
->params
[2] /*d */ , tmp
);
950 _gnutls_mpi_release (&tmp
);
952 if (params
->params
[7] == NULL
|| params
->params
[6] == NULL
)
955 return GNUTLS_E_MEMORY_ERROR
;
963 wrap_nettle_pk_fixup (gnutls_pk_algorithm_t algo
,
964 gnutls_direction_t direction
,
965 gnutls_pk_params_st
* params
)
969 if (direction
== GNUTLS_IMPORT
&& algo
== GNUTLS_PK_RSA
)
971 /* do not trust the generated values. Some old private keys
972 * generated by us have mess on the values. Those were very
973 * old but it seemed some of the shipped example private
976 mpz_invert (TOMPZ (params
->params
[RSA_COEF
]),
977 TOMPZ (params
->params
[RSA_PRIME2
]), TOMPZ (params
->params
[RSA_PRIME1
]));
979 /* calculate exp1 [6] and exp2 [7] */
980 _gnutls_mpi_release (¶ms
->params
[RSA_E1
]);
981 _gnutls_mpi_release (¶ms
->params
[RSA_E2
]);
983 result
= calc_rsa_exp (params
);
989 params
->params_nr
= RSA_PRIVATE_PARAMS
;
996 extract_digest_info(const struct rsa_public_key
*key
,
997 unsigned *length
, uint8_t *digest_info
,
998 const mpz_t signature
)
1005 if (key
->size
== 0 || *length
< key
->size
)
1008 em
= gnutls_malloc(key
->size
);
1014 mpz_powm(m
, signature
, key
->e
, key
->n
);
1016 nettle_mpz_get_str_256(key
->size
, em
, m
);
1019 if (em
[0] != 0 || em
[1] != 1)
1025 for (i
= 2; i
< key
->size
; i
++)
1027 if (em
[i
] == 0 && i
> 2)
1038 memcpy(digest_info
, &em
[i
], key
->size
-i
);
1039 *length
= key
->size
-i
;
1048 /* Given a signature and parameters, it should return
1049 * the hash algorithm used in the signature. This is a kludge
1050 * but until we deprecate gnutls_pubkey_get_verify_algorithm()
1053 static int wrap_nettle_hash_algorithm (gnutls_pk_algorithm_t pk
,
1054 const gnutls_datum_t
* sig
, gnutls_pk_params_st
* issuer_params
,
1055 gnutls_digest_algorithm_t
* hash_algo
)
1057 uint8_t digest
[MAX_HASH_SIZE
];
1058 uint8_t digest_info
[MAX_HASH_SIZE
*3];
1060 unsigned digest_size
;
1062 struct rsa_public_key pub
;
1073 *hash_algo
= _gnutls_dsa_q_to_hash (pk
, issuer_params
, NULL
);
1079 { /* return a sensible algorithm */
1081 *hash_algo
= GNUTLS_DIG_SHA256
;
1085 _rsa_params_to_pubkey (issuer_params
, &pub
);
1087 digest_size
= sizeof(digest
);
1089 nettle_mpz_set_str_256_u(s
, sig
->size
, sig
->data
);
1091 digest_size
= sizeof (digest_info
);
1092 ret
= extract_digest_info( &pub
, &digest_size
, digest_info
, s
);
1095 ret
= GNUTLS_E_PK_SIG_VERIFY_FAILED
;
1100 di
.data
= digest_info
;
1101 di
.size
= digest_size
;
1103 digest_size
= sizeof(digest
);
1105 decode_ber_digest_info (&di
, hash_algo
, digest
,
1112 if (digest_size
!= _gnutls_hash_get_algo_len (*hash_algo
))
1115 ret
= GNUTLS_E_PK_SIG_VERIFY_FAILED
;
1124 ret
= GNUTLS_E_INTERNAL_ERROR
;
1134 int crypto_pk_prio
= INT_MAX
;
1136 gnutls_crypto_pk_st _gnutls_pk_ops
= {
1137 .hash_algorithm
= wrap_nettle_hash_algorithm
,
1138 .encrypt
= _wrap_nettle_pk_encrypt
,
1139 .decrypt
= _wrap_nettle_pk_decrypt
,
1140 .sign
= _wrap_nettle_pk_sign
,
1141 .verify
= _wrap_nettle_pk_verify
,
1142 .verify_params
= wrap_nettle_pk_verify_params
,
1143 .generate
= wrap_nettle_pk_generate_params
,
1144 .pk_fixup_private_params
= wrap_nettle_pk_fixup
,
1145 .derive
= _wrap_nettle_pk_derive
,