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 (*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;
86 typedef void *bigint_t;
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
93 * @GNUTLS_MPI_FORMAT_PGP: The pgp integer format.
95 * Enumeration of different bignum integer encoding formats.
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;
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);
115 int (*bigint_cmp_ui) (const bigint_t m1, unsigned long m2);
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,
139 /* w = a - b */ int (*bigint_sub) (bigint_t w,
143 int (*bigint_mul) (bigint_t w, const bigint_t a,
146 int (*bigint_add_ui) (bigint_t w, const bigint_t a,
149 int (*bigint_sub_ui) (bigint_t w, const bigint_t a,
152 int (*bigint_mul_ui) (bigint_t w, const bigint_t a,
155 int (*bigint_div) (bigint_t q, const bigint_t a,
158 int (*bigint_prime_check) (const bigint_t pp);
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;
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,
169 gnutls_bigint_format_t format);
170 } gnutls_crypto_bigint_st;
172 #define GNUTLS_MAX_PK_PARAMS 16
175 bigint_t params[GNUTLS_MAX_PK_PARAMS];
176 unsigned int params_nr; /* the number of parameters */
178 gnutls_pk_algorithm_t algo;
179 } gnutls_pk_params_st;
183 * @GNUTLS_PK_FLAG_NONE: No flag.
185 * Enumeration of public-key flag.
188 GNUTLS_PK_FLAG_NONE = 0
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);
197 #define MAX_PUBLIC_PARAMS_SIZE 4 /* ok for RSA and DSA */
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
206 #define MAX_PRIV_PARAMS_SIZE GNUTLS_MAX_PK_PARAMS /* ok for RSA and DSA */
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
214 #if MAX_PRIV_PARAMS_SIZE - RSA_PRIVATE_PARAMS < 0
215 #error INCREASE MAX_PRIV_PARAMS
218 #if MAX_PRIV_PARAMS_SIZE - ECC_PRIVATE_PARAMS < 0
219 #error INCREASE MAX_PRIV_PARAMS
222 #if MAX_PRIV_PARAMS_SIZE - DSA_PRIVATE_PARAMS < 0
223 #error INCREASE MAX_PRIV_PARAMS
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)
238 * note that for libgcrypt that does not use the inverse of q mod p,
239 * we need to perform conversions using fixup_params().
245 * [3] is y (public key)
246 * [4] is x (private key only)
259 * [8] is k (private key)
278 #define RSA_MODULUS 0
288 * gnutls_direction_t:
289 * @GNUTLS_IMPORT: Import direction.
290 * @GNUTLS_EXPORT: Export direction.
292 * Enumeration of different directions.
297 } gnutls_direction_t;
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);
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
334 int (*pk_fixup_private_params) (gnutls_pk_algorithm_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);
341 int (*curve_exists) (gnutls_ecc_curve_t); /* true/false */
342 } gnutls_crypto_pk_st;
344 /* priority: infinity for backend algorithms, 90 for kernel
345 algorithms, lowest wins
347 int gnutls_crypto_single_cipher_register(gnutls_cipher_algorithm_t
348 algorithm, int priority,
350 gnutls_crypto_single_cipher_st *
352 int gnutls_crypto_single_mac_register(gnutls_mac_algorithm_t algorithm,
354 const gnutls_crypto_single_mac_st *
356 int gnutls_crypto_single_digest_register(gnutls_digest_algorithm_t
357 algorithm, int priority,
359 gnutls_crypto_single_digest_st *
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);
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);