corrected copyright notices
[gnutls.git] / lib / crypto-api.c
blob3bf2efefbf0ee91cbc70e91f94c185276943cbc2
1 /*
2 * Copyright (C) 2000-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 3 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 #include <gnutls_int.h>
24 #include <gnutls_errors.h>
25 #include <gnutls_cipher_int.h>
26 #include <gnutls_datum.h>
27 #include <gnutls/crypto.h>
28 #include <random.h>
29 #include <crypto.h>
31 typedef struct api_cipher_hd_st
33 cipher_hd_st ctx_enc;
34 cipher_hd_st ctx_dec;
35 } api_cipher_hd_st;
37 /**
38 * gnutls_cipher_init:
39 * @handle: is a #gnutls_cipher_hd_t structure.
40 * @cipher: the encryption algorithm to use
41 * @key: The key to be used for encryption
42 * @iv: The IV to use (if not applicable set NULL)
44 * This function will initialize an context that can be used for
45 * encryption/decryption of data. This will effectively use the
46 * current crypto backend in use by gnutls or the cryptographic
47 * accelerator in use.
49 * Returns: Zero or a negative error code on error.
51 * Since: 2.10.0
52 **/
53 int
54 gnutls_cipher_init (gnutls_cipher_hd_t * handle,
55 gnutls_cipher_algorithm_t cipher,
56 const gnutls_datum_t * key, const gnutls_datum_t * iv)
58 api_cipher_hd_st * h;
59 int ret;
61 *handle = gnutls_calloc (1, sizeof (api_cipher_hd_st));
62 if (*handle == NULL)
64 gnutls_assert ();
65 return GNUTLS_E_MEMORY_ERROR;
68 h = *handle;
69 ret = _gnutls_cipher_init (&h->ctx_enc, cipher, key, iv, 1);
71 if (ret >= 0 && _gnutls_cipher_is_aead( &h->ctx_enc) == 0) /* AEAD ciphers are stream - so far */
72 ret = _gnutls_cipher_init (&h->ctx_dec, cipher, key, iv, 0);
74 return ret;
77 /**
78 * gnutls_cipher_tag:
79 * @handle: is a #gnutls_cipher_hd_t structure.
80 * @tag: will hold the tag
81 * @tag_size: The length of the tag to return
83 * This function operates on authenticated encryption with
84 * associated data (AEAD) ciphers and will return the
85 * output tag.
87 * Returns: Zero or a negative error code on error.
89 * Since: 3.0
90 **/
91 int
92 gnutls_cipher_tag (gnutls_cipher_hd_t handle, void *tag, size_t tag_size)
94 api_cipher_hd_st * h = handle;
96 if (_gnutls_cipher_is_aead( &h->ctx_enc)==0)
97 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
99 _gnutls_cipher_tag( &h->ctx_enc, tag, tag_size);
101 return 0;
105 * gnutls_cipher_add_auth:
106 * @handle: is a #gnutls_cipher_hd_t structure.
107 * @text: the data to be authenticated
108 * @text_size: The length of the data
110 * This function operates on authenticated encryption with
111 * associated data (AEAD) ciphers and authenticate the
112 * input data. This function can only be called once
113 * and before any encryption operations.
115 * Returns: Zero or a negative error code on error.
117 * Since: 3.0
120 gnutls_cipher_add_auth (gnutls_cipher_hd_t handle, const void *text, size_t text_size)
122 api_cipher_hd_st * h = handle;
124 if (_gnutls_cipher_is_aead( &h->ctx_enc)==0)
125 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
127 _gnutls_cipher_auth( &h->ctx_enc, text, text_size);
129 return 0;
133 * gnutls_cipher_set_iv:
134 * @handle: is a #gnutls_cipher_hd_t structure.
135 * @iv: the IV to set
136 * @ivlen: The length of the IV
138 * This function will set the IV to be used for the next
139 * encryption block.
141 * Since: 3.0
143 void
144 gnutls_cipher_set_iv (gnutls_cipher_hd_t handle, void *iv, size_t ivlen)
146 api_cipher_hd_st * h = handle;
148 _gnutls_cipher_setiv( &h->ctx_enc, iv, ivlen);
150 if (_gnutls_cipher_is_aead( &h->ctx_enc)==0)
151 _gnutls_cipher_setiv( &h->ctx_dec, iv, ivlen);
155 * gnutls_cipher_encrypt:
156 * @handle: is a #gnutls_cipher_hd_t structure.
157 * @text: the data to encrypt
158 * @textlen: The length of data to encrypt
160 * This function will encrypt the given data using the algorithm
161 * specified by the context.
163 * Returns: Zero or a negative error code on error.
165 * Since: 2.10.0
168 gnutls_cipher_encrypt (gnutls_cipher_hd_t handle, void *text, size_t textlen)
170 api_cipher_hd_st * h = handle;
172 return _gnutls_cipher_encrypt (&h->ctx_enc, text, textlen);
176 * gnutls_cipher_decrypt:
177 * @handle: is a #gnutls_cipher_hd_t structure.
178 * @ciphertext: the data to encrypt
179 * @ciphertextlen: The length of data to encrypt
181 * This function will decrypt the given data using the algorithm
182 * specified by the context.
184 * Returns: Zero or a negative error code on error.
186 * Since: 2.10.0
189 gnutls_cipher_decrypt (gnutls_cipher_hd_t handle, void *ciphertext,
190 size_t ciphertextlen)
192 api_cipher_hd_st * h = handle;
194 if (_gnutls_cipher_is_aead( &h->ctx_enc)!=0)
195 return _gnutls_cipher_decrypt (&h->ctx_enc, ciphertext, ciphertextlen);
196 else
197 return _gnutls_cipher_decrypt (&h->ctx_dec, ciphertext, ciphertextlen);
201 * gnutls_cipher_encrypt2:
202 * @handle: is a #gnutls_cipher_hd_t structure.
203 * @text: the data to encrypt
204 * @textlen: The length of data to encrypt
205 * @ciphertext: the encrypted data
206 * @ciphertextlen: The available length for encrypted data
208 * This function will encrypt the given data using the algorithm
209 * specified by the context.
211 * Returns: Zero or a negative error code on error.
213 * Since: 2.12.0
216 gnutls_cipher_encrypt2 (gnutls_cipher_hd_t handle, const void *text, size_t textlen,
217 void *ciphertext, size_t ciphertextlen)
219 api_cipher_hd_st * h = handle;
221 return _gnutls_cipher_encrypt2 (&h->ctx_enc, text, textlen,
222 ciphertext, ciphertextlen);
226 * gnutls_cipher_decrypt2:
227 * @handle: is a #gnutls_cipher_hd_t structure.
228 * @ciphertext: the data to encrypt
229 * @ciphertextlen: The length of data to encrypt
230 * @text: the decrypted data
231 * @textlen: The available length for decrypted data
233 * This function will decrypt the given data using the algorithm
234 * specified by the context.
236 * Returns: Zero or a negative error code on error.
238 * Since: 2.12.0
241 gnutls_cipher_decrypt2 (gnutls_cipher_hd_t handle, const void *ciphertext,
242 size_t ciphertextlen, void *text, size_t textlen)
244 api_cipher_hd_st * h = handle;
246 return _gnutls_cipher_decrypt2 (&h->ctx_dec, ciphertext,
247 ciphertextlen, text, textlen);
251 * gnutls_cipher_deinit:
252 * @handle: is a #gnutls_cipher_hd_t structure.
254 * This function will deinitialize all resources occupied by the given
255 * encryption context.
257 * Since: 2.10.0
259 void
260 gnutls_cipher_deinit (gnutls_cipher_hd_t handle)
262 api_cipher_hd_st * h = handle;
264 _gnutls_cipher_deinit (&h->ctx_enc);
265 if (_gnutls_cipher_is_aead( &h->ctx_enc)==0)
266 _gnutls_cipher_deinit (&h->ctx_dec);
267 gnutls_free (handle);
271 /* HMAC */
274 * gnutls_hmac_init:
275 * @dig: is a #gnutls_hmac_hd_t structure.
276 * @algorithm: the HMAC algorithm to use
277 * @key: The key to be used for encryption
278 * @keylen: The length of the key
280 * This function will initialize an context that can be used to
281 * produce a Message Authentication Code (MAC) of data. This will
282 * effectively use the current crypto backend in use by gnutls or the
283 * cryptographic accelerator in use.
285 * Returns: Zero or a negative error code on error.
287 * Since: 2.10.0
290 gnutls_hmac_init (gnutls_hmac_hd_t * dig,
291 gnutls_mac_algorithm_t algorithm,
292 const void *key, size_t keylen)
294 *dig = gnutls_malloc (sizeof (digest_hd_st));
295 if (*dig == NULL)
297 gnutls_assert ();
298 return GNUTLS_E_MEMORY_ERROR;
301 return _gnutls_hmac_init (((digest_hd_st *) * dig), algorithm, key, keylen);
305 * gnutls_hmac:
306 * @handle: is a #gnutls_cipher_hd_t structure.
307 * @text: the data to hash
308 * @textlen: The length of data to hash
310 * This function will hash the given data using the algorithm
311 * specified by the context.
313 * Returns: Zero or a negative error code on error.
315 * Since: 2.10.0
318 gnutls_hmac (gnutls_hmac_hd_t handle, const void *text, size_t textlen)
320 return _gnutls_hmac ((digest_hd_st *) handle, text, textlen);
324 * gnutls_hmac_output:
325 * @handle: is a #gnutls_hmac_hd_t structure.
326 * @digest: is the output value of the MAC
328 * This function will output the current MAC value.
330 * Since: 2.10.0
332 void
333 gnutls_hmac_output (gnutls_hmac_hd_t handle, void *digest)
335 _gnutls_hmac_output ((digest_hd_st *) handle, digest);
339 * gnutls_hmac_deinit:
340 * @handle: is a #gnutls_hmac_hd_t structure.
341 * @digest: is the output value of the MAC
343 * This function will deinitialize all resources occupied by
344 * the given hmac context.
346 * Since: 2.10.0
348 void
349 gnutls_hmac_deinit (gnutls_hmac_hd_t handle, void *digest)
351 _gnutls_hmac_deinit ((digest_hd_st *) handle, digest);
352 gnutls_free (handle);
356 * gnutls_hmac_get_len:
357 * @algorithm: the hmac algorithm to use
359 * This function will return the length of the output data
360 * of the given hmac algorithm.
362 * Returns: The length or zero on error.
364 * Since: 2.10.0
367 gnutls_hmac_get_len (gnutls_mac_algorithm_t algorithm)
369 return _gnutls_hmac_get_algo_len (algorithm);
373 * gnutls_hmac_fast:
374 * @algorithm: the hash algorithm to use
375 * @key: the key to use
376 * @keylen: The length of the key
377 * @text: the data to hash
378 * @textlen: The length of data to hash
379 * @digest: is the output value of the hash
381 * This convenience function will hash the given data and return output
382 * on a single call.
384 * Returns: Zero or a negative error code on error.
386 * Since: 2.10.0
389 gnutls_hmac_fast (gnutls_mac_algorithm_t algorithm,
390 const void *key, size_t keylen,
391 const void *text, size_t textlen, void *digest)
393 return _gnutls_hmac_fast (algorithm, key, keylen, text, textlen, digest);
396 /* HASH */
399 * gnutls_hash_init:
400 * @dig: is a #gnutls_hash_hd_t structure.
401 * @algorithm: the hash algorithm to use
403 * This function will initialize an context that can be used to
404 * produce a Message Digest of data. This will effectively use the
405 * current crypto backend in use by gnutls or the cryptographic
406 * accelerator in use.
408 * Returns: Zero or a negative error code on error.
410 * Since: 2.10.0
413 gnutls_hash_init (gnutls_hash_hd_t * dig, gnutls_digest_algorithm_t algorithm)
415 *dig = gnutls_malloc (sizeof (digest_hd_st));
416 if (*dig == NULL)
418 gnutls_assert ();
419 return GNUTLS_E_MEMORY_ERROR;
422 return _gnutls_hash_init (((digest_hd_st *) * dig), algorithm);
426 * gnutls_hash:
427 * @handle: is a #gnutls_cipher_hd_t structure.
428 * @text: the data to hash
429 * @textlen: The length of data to hash
431 * This function will hash the given data using the algorithm
432 * specified by the context.
434 * Returns: Zero or a negative error code on error.
436 * Since: 2.10.0
439 gnutls_hash (gnutls_hash_hd_t handle, const void *text, size_t textlen)
441 return _gnutls_hash ((digest_hd_st *) handle, text, textlen);
445 * gnutls_hash_output:
446 * @handle: is a #gnutls_hash_hd_t structure.
447 * @digest: is the output value of the hash
449 * This function will output the current hash value.
451 * Since: 2.10.0
453 void
454 gnutls_hash_output (gnutls_hash_hd_t handle, void *digest)
456 _gnutls_hash_output ((digest_hd_st *) handle, digest);
460 * gnutls_hash_deinit:
461 * @handle: is a #gnutls_hash_hd_t structure.
462 * @digest: is the output value of the hash
464 * This function will deinitialize all resources occupied by
465 * the given hash context.
467 * Since: 2.10.0
469 void
470 gnutls_hash_deinit (gnutls_hash_hd_t handle, void *digest)
472 _gnutls_hash_deinit ((digest_hd_st *) handle, digest);
473 gnutls_free (handle);
477 * gnutls_hash_get_len:
478 * @algorithm: the hash algorithm to use
480 * This function will return the length of the output data
481 * of the given hash algorithm.
483 * Returns: The length or zero on error.
485 * Since: 2.10.0
488 gnutls_hash_get_len (gnutls_digest_algorithm_t algorithm)
490 return _gnutls_hash_get_algo_len (algorithm);
494 * gnutls_hash_fast:
495 * @algorithm: the hash algorithm to use
496 * @text: the data to hash
497 * @textlen: The length of data to hash
498 * @digest: is the output value of the hash
500 * This convenience function will hash the given data and return output
501 * on a single call.
503 * Returns: Zero or a negative error code on error.
505 * Since: 2.10.0
508 gnutls_hash_fast (gnutls_digest_algorithm_t algorithm,
509 const void *text, size_t textlen, void *digest)
511 return _gnutls_hash_fast (algorithm, text, textlen, digest);
515 * gnutls_key_generate:
516 * @key: is a pointer to a #gnutls_datum_t which will contain a newly
517 * created key.
518 * @key_size: The number of bytes of the key.
520 * Generates a random key of @key_bytes size.
522 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, or an
523 * error code.
525 * Since: 3.0
528 gnutls_key_generate (gnutls_datum_t * key, unsigned int key_size)
530 int ret;
532 key->size = key_size;
533 key->data = gnutls_malloc (key->size);
534 if (!key->data)
536 gnutls_assert ();
537 return GNUTLS_E_MEMORY_ERROR;
540 ret = _gnutls_rnd (GNUTLS_RND_RANDOM, key->data, key->size);
541 if (ret < 0)
543 gnutls_assert ();
544 _gnutls_free_datum (key);
545 return ret;
548 return 0;