1 /*
2  * Copyright (C) 2000-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
8  * The GnuTLS 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 #ifndef GNUTLS_LIB_PK_H
24 #define GNUTLS_LIB_PK_H
25 
26 extern int crypto_pk_prio;
27 extern gnutls_crypto_pk_st _gnutls_pk_ops;
28 
29 #define _gnutls_pk_encrypt( algo, ciphertext, plaintext, params) _gnutls_pk_ops.encrypt( algo, ciphertext, plaintext, params)
30 #define _gnutls_pk_decrypt( algo, ciphertext, plaintext, params) _gnutls_pk_ops.decrypt( algo, ciphertext, plaintext, params)
31 #define _gnutls_pk_decrypt2( algo, ciphertext, plaintext, size, params) _gnutls_pk_ops.decrypt2( algo, ciphertext, plaintext, size, params)
32 #define _gnutls_pk_sign( algo, sig, data, params, sign_params) _gnutls_pk_ops.sign( algo, sig, data, params, sign_params)
33 #define _gnutls_pk_verify( algo, data, sig, params, sign_params) _gnutls_pk_ops.verify( algo, data, sig, params, sign_params)
34 #define _gnutls_pk_verify_priv_params( algo, params) _gnutls_pk_ops.verify_priv_params( algo, params)
35 #define _gnutls_pk_verify_pub_params( algo, params) _gnutls_pk_ops.verify_pub_params( algo, params)
36 #define _gnutls_pk_derive( algo, out, pub, priv) _gnutls_pk_ops.derive( algo, out, pub, priv, NULL, 0)
37 #define _gnutls_pk_derive_nonce( algo, out, pub, priv, nonce) _gnutls_pk_ops.derive( algo, out, pub, priv, nonce, 0)
38 #define _gnutls_pk_derive_tls13( algo, out, pub, priv) _gnutls_pk_ops.derive( algo, out, pub, priv, NULL, PK_DERIVE_TLS13)
39 #define _gnutls_pk_generate_keys( algo, bits, params, temporal) _gnutls_pk_ops.generate_keys( algo, bits, params, temporal)
40 #define _gnutls_pk_generate_params( algo, bits, priv) _gnutls_pk_ops.generate_params( algo, bits, priv)
41 #define _gnutls_pk_hash_algorithm( pk, sig, params, hash) _gnutls_pk_ops.hash_algorithm(pk, sig, params, hash)
42 #define _gnutls_pk_curve_exists( curve) _gnutls_pk_ops.curve_exists(curve)
43 
44 inline static int
_gnutls_pk_fixup(gnutls_pk_algorithm_t algo,gnutls_direction_t direction,gnutls_pk_params_st * params)45 _gnutls_pk_fixup(gnutls_pk_algorithm_t algo, gnutls_direction_t direction,
46 		 gnutls_pk_params_st * params)
47 {
48 	if (_gnutls_pk_ops.pk_fixup_private_params)
49 		return _gnutls_pk_ops.pk_fixup_private_params(algo,
50 							      direction,
51 							      params);
52 	return 0;
53 }
54 
55 int _gnutls_pk_params_copy(gnutls_pk_params_st * dst,
56 			   const gnutls_pk_params_st * src);
57 
58 /* The internal PK interface */
59 int
60 _gnutls_encode_ber_rs(gnutls_datum_t * sig_value, bigint_t r, bigint_t s);
61 int
62 _gnutls_encode_ber_rs_raw(gnutls_datum_t * sig_value,
63 			  const gnutls_datum_t * r,
64 			  const gnutls_datum_t * s);
65 
66 int
67 _gnutls_decode_ber_rs(const gnutls_datum_t * sig_value, bigint_t * r,
68 		      bigint_t * s);
69 
70 int
71 _gnutls_decode_ber_rs_raw(const gnutls_datum_t * sig_value, gnutls_datum_t *r,
72 			  gnutls_datum_t *s);
73 
74 int
75 _gnutls_encode_gost_rs(gnutls_datum_t * sig_value, bigint_t r, bigint_t s,
76 		       size_t intsize);
77 
78 int
79 _gnutls_decode_gost_rs(const gnutls_datum_t * sig_value, bigint_t * r,
80 		       bigint_t * s);
81 
82 gnutls_digest_algorithm_t _gnutls_gost_digest(gnutls_pk_algorithm_t pk);
83 gnutls_pk_algorithm_t _gnutls_digest_gost(gnutls_digest_algorithm_t digest);
84 gnutls_gost_paramset_t _gnutls_gost_paramset_default(gnutls_pk_algorithm_t pk);
85 
86 int
87 encode_ber_digest_info(const mac_entry_st * e,
88 		       const gnutls_datum_t * digest,
89 		       gnutls_datum_t * output);
90 
91 #define decode_ber_digest_info gnutls_decode_ber_digest_info
92 
93 int
94 _gnutls_params_get_rsa_raw(const gnutls_pk_params_st* params,
95 				    gnutls_datum_t * m, gnutls_datum_t * e,
96 				    gnutls_datum_t * d, gnutls_datum_t * p,
97 				    gnutls_datum_t * q, gnutls_datum_t * u,
98 				    gnutls_datum_t * e1,
99 				    gnutls_datum_t * e2,
100 				    unsigned int flags);
101 
102 int
103 _gnutls_params_get_dsa_raw(const gnutls_pk_params_st* params,
104 			     gnutls_datum_t * p, gnutls_datum_t * q,
105 			     gnutls_datum_t * g, gnutls_datum_t * y,
106 			     gnutls_datum_t * x, unsigned int flags);
107 
108 int _gnutls_params_get_ecc_raw(const gnutls_pk_params_st* params,
109 				       gnutls_ecc_curve_t * curve,
110 				       gnutls_datum_t * x,
111 				       gnutls_datum_t * y,
112 				       gnutls_datum_t * k,
113 				       unsigned int flags);
114 
115 int _gnutls_params_get_gost_raw(const gnutls_pk_params_st* params,
116 				       gnutls_ecc_curve_t * curve,
117 				       gnutls_digest_algorithm_t * digest,
118 				       gnutls_gost_paramset_t * paramset,
119 				       gnutls_datum_t * x,
120 				       gnutls_datum_t * y,
121 				       gnutls_datum_t * k,
122 				       unsigned int flags);
123 
124 int pk_prepare_hash(gnutls_pk_algorithm_t pk, const mac_entry_st * hash,
125 		    gnutls_datum_t * output);
126 int pk_hash_data(gnutls_pk_algorithm_t pk, const mac_entry_st * hash,
127 		 gnutls_pk_params_st * params, const gnutls_datum_t * data,
128 		 gnutls_datum_t * digest);
129 
130 int _gnutls_find_rsa_pss_salt_size(unsigned bits, const mac_entry_st *me,
131 				   unsigned salt_size);
132 
133 #endif /* GNUTLS_LIB_PK_H */
134