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