2 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
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.
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 #ifndef GNUTLS_CRYPTO_BACKEND_H
24 #define GNUTLS_CRYPTO_BACKEND_H
26 #include <gnutls/crypto.h>
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
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);
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;
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);
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;
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);
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;
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;
87 typedef void *bigint_t;
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
94 * @GNUTLS_MPI_FORMAT_PGP: The pgp integer format.
96 * Enumeration of different bignum integer encoding formats.
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;
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);
116 int (*bigint_cmp_ui) (const bigint_t m1, unsigned long m2);
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,
140 /* w = a - b */ int (*bigint_sub) (bigint_t w,
144 int (*bigint_mul) (bigint_t w, const bigint_t a,
147 int (*bigint_add_ui) (bigint_t w, const bigint_t a,
150 int (*bigint_sub_ui) (bigint_t w, const bigint_t a,
153 int (*bigint_mul_ui) (bigint_t w, const bigint_t a,
156 int (*bigint_div) (bigint_t q, const bigint_t a,
159 int (*bigint_prime_check) (const bigint_t pp);
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;
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,
170 gnutls_bigint_format_t format);
171 } gnutls_crypto_bigint_st;
173 #define GNUTLS_MAX_PK_PARAMS 16
176 bigint_t params[GNUTLS_MAX_PK_PARAMS];
177 unsigned int params_nr; /* the number of parameters */
179 gnutls_pk_algorithm_t algo;
180 } gnutls_pk_params_st;
184 * @GNUTLS_PK_FLAG_NONE: No flag.
186 * Enumeration of public-key flag.
189 GNUTLS_PK_FLAG_NONE = 0
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);
198 #define MAX_PUBLIC_PARAMS_SIZE 4 /* ok for RSA and DSA */
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
207 #define MAX_PRIV_PARAMS_SIZE GNUTLS_MAX_PK_PARAMS /* ok for RSA and DSA */
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
215 #if MAX_PRIV_PARAMS_SIZE - RSA_PRIVATE_PARAMS < 0
216 #error INCREASE MAX_PRIV_PARAMS
219 #if MAX_PRIV_PARAMS_SIZE - ECC_PRIVATE_PARAMS < 0
220 #error INCREASE MAX_PRIV_PARAMS
223 #if MAX_PRIV_PARAMS_SIZE - DSA_PRIVATE_PARAMS < 0
224 #error INCREASE MAX_PRIV_PARAMS
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)
239 * note that for libgcrypt that does not use the inverse of q mod p,
240 * we need to perform conversions using fixup_params().
246 * [3] is y (public key)
247 * [4] is x (private key only)
260 * [8] is k (private key)
279 #define RSA_MODULUS 0
289 * gnutls_direction_t:
290 * @GNUTLS_IMPORT: Import direction.
291 * @GNUTLS_EXPORT: Export direction.
293 * Enumeration of different directions.
298 } gnutls_direction_t;
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);
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
335 int (*pk_fixup_private_params) (gnutls_pk_algorithm_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);
342 int (*curve_exists) (gnutls_ecc_curve_t); /* true/false */
343 } gnutls_crypto_pk_st;
345 /* priority: infinity for backend algorithms, 90 for kernel
346 algorithms, lowest wins
348 int gnutls_crypto_single_cipher_register(gnutls_cipher_algorithm_t
349 algorithm, int priority,
351 gnutls_crypto_single_cipher_st *
353 int gnutls_crypto_single_mac_register(gnutls_mac_algorithm_t algorithm,
355 const gnutls_crypto_single_mac_st *
357 int gnutls_crypto_single_digest_register(gnutls_digest_algorithm_t
358 algorithm, int priority,
360 gnutls_crypto_single_digest_st *
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);
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);