Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / lib / crypto-backend.h
1 /*
2  * Copyright (C) 2011-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 <http://www.gnu.org/licenses/>
20  *
21  */
22
23 #ifndef GNUTLS_CRYPTO_BACKEND_H
24 #define GNUTLS_CRYPTO_BACKEND_H
25
26 #include <gnutls/crypto.h>
27
28 #define gnutls_crypto_single_cipher_st gnutls_crypto_cipher_st
29 #define gnutls_crypto_single_mac_st gnutls_crypto_mac_st
30 #define gnutls_crypto_single_digest_st gnutls_crypto_digest_st
31
32 typedef struct {
33         int (*init) (gnutls_cipher_algorithm_t, void **ctx, int enc);
34         int (*setkey) (void *ctx, const void *key, size_t keysize);
35         int (*setiv) (void *ctx, const void *iv, size_t ivsize);
36         int (*encrypt) (void *ctx, const void *plain, size_t plainsize,
37                         void *encr, size_t encrsize);
38         int (*decrypt) (void *ctx, const void *encr, size_t encrsize,
39                         void *plain, size_t plainsize);
40         int (*auth) (void *ctx, const void *data, size_t datasize);
41         void (*tag) (void *ctx, void *tag, size_t tagsize);
42         void (*deinit) (void *ctx);
43
44         /* Not needed for registered on run-time. Only included
45          * should define it. */
46         int (*exists) (gnutls_cipher_algorithm_t);      /* true/false */
47 } gnutls_crypto_cipher_st;
48
49 typedef struct {
50         int (*init) (gnutls_mac_algorithm_t, void **ctx);
51         int (*setkey) (void *ctx, const void *key, size_t keysize);
52         int (*setnonce) (void *ctx, const void *nonce, size_t noncesize);
53         int (*hash) (void *ctx, const void *text, size_t textsize);
54         int (*output) (void *src_ctx, void *digest, size_t digestsize);
55         void (*deinit) (void *ctx);
56         int (*fast) (gnutls_mac_algorithm_t, const void *nonce,
57                      size_t nonce_size, const void *key, size_t keysize,
58                      const void *text, size_t textsize, void *digest);
59
60         /* Not needed for registered on run-time. Only included
61          * should define it. */
62         int (*exists) (gnutls_mac_algorithm_t);
63 } gnutls_crypto_mac_st;
64
65 typedef struct {
66         int (*init) (gnutls_digest_algorithm_t, void **ctx);
67         int (*hash) (void *ctx, const void *src, size_t srcsize);
68         int (*output) (void *src_ctx, void *digest, size_t digestsize);
69         void (*deinit) (void *ctx);
70         int (*fast) (gnutls_digest_algorithm_t, const void *src,
71                      size_t srcsize, void *digest);
72
73         /* Not needed for registered on run-time. Only included
74          * should define it. */
75         int (*exists) (gnutls_digest_algorithm_t);
76 } gnutls_crypto_digest_st;
77
78 typedef struct gnutls_crypto_rnd {
79         int (*init) (void **ctx);
80         int (*check) (void **ctx);
81         int (*rnd) (void *ctx, int level, void *data, size_t datasize);
82         void (*rnd_refresh) (void *ctx);
83         void (*deinit) (void *ctx);
84         int (*self_test) (void);
85 } gnutls_crypto_rnd_st;
86
87 typedef void *bigint_t;
88
89 /**
90  * gnutls_bigint_format_t:
91  * @GNUTLS_MPI_FORMAT_USG: Raw unsigned integer format.
92  * @GNUTLS_MPI_FORMAT_STD: Raw signed integer format, always a leading
93  *   zero when positive.
94  * @GNUTLS_MPI_FORMAT_PGP: The pgp integer format.
95  *
96  * Enumeration of different bignum integer encoding formats.
97  */
98 typedef enum {
99         /* raw unsigned integer format */
100         GNUTLS_MPI_FORMAT_USG = 0,
101         /* raw signed integer format - always a leading zero when positive */
102         GNUTLS_MPI_FORMAT_STD = 1,
103         /* the pgp integer format */
104         GNUTLS_MPI_FORMAT_PGP = 2
105 } gnutls_bigint_format_t;
106
107 /* Multi precision integer arithmetic */
108 typedef struct gnutls_crypto_bigint {
109         int (*bigint_init) (bigint_t*);
110         int (*bigint_init_multi) (bigint_t*, ...);
111         void (*bigint_release) (bigint_t n);
112         void (*bigint_clear) (bigint_t n);      /* zeros the int */
113         /* 0 for equality, > 0 for m1>m2, < 0 for m1<m2 */
114         int (*bigint_cmp) (const bigint_t m1, const bigint_t m2);
115         /* as bigint_cmp */
116         int (*bigint_cmp_ui) (const bigint_t m1, unsigned long m2);
117         /* r = a % b */
118         int (*bigint_modm) (bigint_t r, const bigint_t a, const bigint_t b);
119         /* a = b -> ret == a */
120         int (*bigint_set) (bigint_t a, const bigint_t b);
121         bigint_t (*bigint_copy) (const bigint_t a);
122         /* a = b -> ret == a */
123         int (*bigint_set_ui) (bigint_t a, unsigned long b);
124         unsigned int (*bigint_get_nbits) (const bigint_t a);
125         /* w = b ^ e mod m */
126         int (*bigint_powm) (bigint_t w, const bigint_t b,
127                                  const bigint_t e, const bigint_t m);
128         /* w = a + b mod m */
129         int (*bigint_addm) (bigint_t w, const bigint_t a,
130                                  const bigint_t b, const bigint_t m);
131         /* w = a - b mod m */
132         int (*bigint_subm) (bigint_t w, const bigint_t a,
133                                  const bigint_t b, const bigint_t m);
134         /* w = a * b mod m */
135         int (*bigint_mulm) (bigint_t w, const bigint_t a,
136                                  const bigint_t b, const bigint_t m);
137         /* w = a + b */ int (*bigint_add) (bigint_t w,
138                                                const bigint_t a,
139                                                const bigint_t b);
140         /* w = a - b */ int (*bigint_sub) (bigint_t w,
141                                                const bigint_t a,
142                                                const bigint_t b);
143         /* w = a * b */
144         int (*bigint_mul) (bigint_t w, const bigint_t a,
145                                 const bigint_t b);
146         /* w = a + b */
147         int (*bigint_add_ui) (bigint_t w, const bigint_t a,
148                                    unsigned long b);
149         /* w = a - b */
150         int (*bigint_sub_ui) (bigint_t w, const bigint_t a,
151                                    unsigned long b);
152         /* w = a * b */
153         int (*bigint_mul_ui) (bigint_t w, const bigint_t a,
154                                    unsigned long b);
155         /* q = a / b */
156         int (*bigint_div) (bigint_t q, const bigint_t a,
157                                 const bigint_t b);
158         /* 0 if prime */
159         int (*bigint_prime_check) (const bigint_t pp);
160
161         /* reads a bigint from a buffer */
162         /* stores a bigint into the buffer.  returns
163          * GNUTLS_E_SHORT_MEMORY_BUFFER if buf_size is not sufficient to
164          * store this integer, and updates the buf_size;
165          */
166         int (*bigint_scan) (bigint_t m, const void *buf, size_t buf_size,
167                                  gnutls_bigint_format_t format);
168         int (*bigint_print) (const bigint_t a, void *buf,
169                              size_t * buf_size,
170                              gnutls_bigint_format_t format);
171 } gnutls_crypto_bigint_st;
172
173 #define GNUTLS_MAX_PK_PARAMS 16
174
175 typedef struct {
176         bigint_t params[GNUTLS_MAX_PK_PARAMS];
177         unsigned int params_nr; /* the number of parameters */
178         unsigned int flags;
179         gnutls_pk_algorithm_t algo;
180 } gnutls_pk_params_st;
181
182 /**
183  * gnutls_pk_flag_t:
184  * @GNUTLS_PK_FLAG_NONE: No flag.
185  *
186  * Enumeration of public-key flag.
187  */
188 typedef enum {
189         GNUTLS_PK_FLAG_NONE = 0
190 } gnutls_pk_flag_t;
191
192
193 void gnutls_pk_params_release(gnutls_pk_params_st * p);
194 void gnutls_pk_params_clear(gnutls_pk_params_st * p);
195 void gnutls_pk_params_init(gnutls_pk_params_st * p);
196
197
198 #define MAX_PUBLIC_PARAMS_SIZE 4        /* ok for RSA and DSA */
199
200 /* parameters should not be larger than this limit */
201 #define DSA_PUBLIC_PARAMS 4
202 #define DH_PUBLIC_PARAMS 4
203 #define RSA_PUBLIC_PARAMS 2
204 #define ECC_PUBLIC_PARAMS 2
205
206
207 #define MAX_PRIV_PARAMS_SIZE GNUTLS_MAX_PK_PARAMS       /* ok for RSA and DSA */
208
209 /* parameters should not be larger than this limit */
210 #define DSA_PRIVATE_PARAMS 5
211 #define DH_PRIVATE_PARAMS 5
212 #define RSA_PRIVATE_PARAMS 8
213 #define ECC_PRIVATE_PARAMS 3
214
215 #if MAX_PRIV_PARAMS_SIZE - RSA_PRIVATE_PARAMS < 0
216 #error INCREASE MAX_PRIV_PARAMS
217 #endif
218
219 #if MAX_PRIV_PARAMS_SIZE - ECC_PRIVATE_PARAMS < 0
220 #error INCREASE MAX_PRIV_PARAMS
221 #endif
222
223 #if MAX_PRIV_PARAMS_SIZE - DSA_PRIVATE_PARAMS < 0
224 #error INCREASE MAX_PRIV_PARAMS
225 #endif
226
227
228 /* params are:
229  * RSA:
230  *  [0] is modulus
231  *  [1] is public exponent
232  *  [2] is private exponent (private key only)
233  *  [3] is prime1 (p) (private key only)
234  *  [4] is prime2 (q) (private key only)
235  *  [5] is coefficient (u == inverse of p mod q) (private key only)
236  *  [6] e1 == d mod (p-1)
237  *  [7] e2 == d mod (q-1)
238  *
239  *  note that for libgcrypt that does not use the inverse of q mod p,
240  *  we need to perform conversions using fixup_params().
241  *
242  * DSA:
243  *  [0] is p
244  *  [1] is q
245  *  [2] is g
246  *  [3] is y (public key)
247  *  [4] is x (private key only)
248  *
249  * DH: as DSA
250  *
251  * ECC:
252  *  [0] is prime
253  *  [1] is order
254  *  [2] is A
255  *  [3] is B
256  *  [4] is Gx
257  *  [5] is Gy
258  *  [6] is x
259  *  [7] is y
260  *  [8] is k (private key)
261  */
262
263 #define ECC_X 0
264 #define ECC_Y 1
265 #define ECC_K 2
266
267 #define DSA_P 0
268 #define DSA_Q 1
269 #define DSA_G 2
270 #define DSA_Y 3
271 #define DSA_X 4
272
273 #define DH_P 0
274 #define DH_Q 1
275 #define DH_G 2
276 #define DH_Y 3
277 #define DH_X 4
278
279 #define RSA_MODULUS 0
280 #define RSA_PUB 1
281 #define RSA_PRIV 2
282 #define RSA_PRIME1 3
283 #define RSA_PRIME2 4
284 #define RSA_COEF 5
285 #define RSA_E1 6
286 #define RSA_E2 7
287
288 /**
289  * gnutls_direction_t:
290  * @GNUTLS_IMPORT: Import direction.
291  * @GNUTLS_EXPORT: Export direction.
292  *
293  * Enumeration of different directions.
294  */
295 typedef enum {
296         GNUTLS_IMPORT = 0,
297         GNUTLS_EXPORT = 1
298 } gnutls_direction_t;
299
300 /* Public key algorithms */
301 typedef struct gnutls_crypto_pk {
302         /* The params structure should contain the private or public key
303          * parameters, depending on the operation */
304         int (*encrypt) (gnutls_pk_algorithm_t, gnutls_datum_t * ciphertext,
305                         const gnutls_datum_t * plaintext,
306                         const gnutls_pk_params_st * pub);
307         int (*decrypt) (gnutls_pk_algorithm_t, gnutls_datum_t * plaintext,
308                         const gnutls_datum_t * ciphertext,
309                         const gnutls_pk_params_st * priv);
310
311         int (*sign) (gnutls_pk_algorithm_t, gnutls_datum_t * signature,
312                      const gnutls_datum_t * data,
313                      const gnutls_pk_params_st * priv);
314         int (*verify) (gnutls_pk_algorithm_t, const gnutls_datum_t * data,
315                        const gnutls_datum_t * sig,
316                        const gnutls_pk_params_st * pub);
317         /* given a signature and the public parameters,
318          * suggest a hash algorithm */
319         int (*hash_algorithm) (gnutls_pk_algorithm_t,
320                                const gnutls_datum_t * sig,
321                                gnutls_pk_params_st * issuer_params,
322                                gnutls_digest_algorithm_t *);
323         /* sanity checks the public key parameters */
324         int (*verify_priv_params) (gnutls_pk_algorithm_t,
325                               const gnutls_pk_params_st * priv);
326         int (*verify_pub_params) (gnutls_pk_algorithm_t,
327                               const gnutls_pk_params_st * pub);
328         int (*generate_keys) (gnutls_pk_algorithm_t, unsigned int nbits,
329                          gnutls_pk_params_st *);
330         int (*generate_params) (gnutls_pk_algorithm_t, unsigned int nbits,
331                          gnutls_pk_params_st *);
332         /* this function should convert params to ones suitable
333          * for the above functions
334          */
335         int (*pk_fixup_private_params) (gnutls_pk_algorithm_t,
336                                         gnutls_direction_t,
337                                         gnutls_pk_params_st *);
338         int (*derive) (gnutls_pk_algorithm_t, gnutls_datum_t * out,
339                        const gnutls_pk_params_st * priv,
340                        const gnutls_pk_params_st * pub);
341
342         int (*curve_exists) (gnutls_ecc_curve_t);       /* true/false */
343 } gnutls_crypto_pk_st;
344
345 /* priority: infinity for backend algorithms, 90 for kernel
346    algorithms, lowest wins
347  */
348 int gnutls_crypto_single_cipher_register(gnutls_cipher_algorithm_t
349                                          algorithm, int priority,
350                                          const
351                                          gnutls_crypto_single_cipher_st *
352                                          s);
353 int gnutls_crypto_single_mac_register(gnutls_mac_algorithm_t algorithm,
354                                       int priority,
355                                       const gnutls_crypto_single_mac_st *
356                                       s);
357 int gnutls_crypto_single_digest_register(gnutls_digest_algorithm_t
358                                          algorithm, int priority,
359                                          const
360                                          gnutls_crypto_single_digest_st *
361                                          s);
362
363 int gnutls_crypto_cipher_register(int priority,
364                                   const gnutls_crypto_cipher_st * s);
365 int gnutls_crypto_mac_register(int priority,
366                                const gnutls_crypto_mac_st * s);
367 int gnutls_crypto_digest_register(int priority,
368                                   const gnutls_crypto_digest_st * s);
369
370 int gnutls_crypto_rnd_register(int priority,
371                                const gnutls_crypto_rnd_st * s);
372 int gnutls_crypto_pk_register(int priority, const gnutls_crypto_pk_st * s);
373 int gnutls_crypto_bigint_register(int priority,
374                                   const gnutls_crypto_bigint_st * s);
375
376 #endif