2 * Copyright (C) 2000, 2004, 2005, 2008, 2010, 2011 Free Software Foundation,
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS.
9 * The GnuTLS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>
24 #include <gnutls_int.h>
25 #include <gnutls_errors.h>
26 #include <gnutls_cipher_int.h>
27 #include <gnutls_datum.h>
28 #include <gnutls/crypto.h>
34 * @handle: is a #gnutls_cipher_hd_t structure.
35 * @cipher: the encryption algorithm to use
36 * @key: The key to be used for encryption
37 * @iv: The IV to use (if not applicable set NULL)
39 * This function will initialize an context that can be used for
40 * encryption/decryption of data. This will effectively use the
41 * current crypto backend in use by gnutls or the cryptographic
44 * Returns: Zero or a negative error code on error.
49 gnutls_cipher_init (gnutls_cipher_hd_t
* handle
,
50 gnutls_cipher_algorithm_t cipher
,
51 const gnutls_datum_t
* key
, const gnutls_datum_t
* iv
)
53 *handle
= gnutls_malloc (sizeof (cipher_hd_st
));
57 return GNUTLS_E_MEMORY_ERROR
;
60 return _gnutls_cipher_init (((cipher_hd_st
*) * handle
), cipher
, key
, iv
);
65 * @handle: is a #gnutls_cipher_hd_t structure.
66 * @tag: will hold the tag
67 * @tag_size: The length of the tag to return
69 * This function operates on authenticated encryption with
70 * associated data (AEAD) ciphers and will return the
73 * Returns: Zero or a negative error code on error.
78 gnutls_cipher_tag (gnutls_cipher_hd_t handle
, void *tag
, size_t tag_size
)
80 if (_gnutls_cipher_is_aead( (cipher_hd_st
*)handle
)==0)
81 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
83 _gnutls_cipher_tag( (cipher_hd_st
*)handle
, tag
, tag_size
);
89 * gnutls_cipher_add_auth:
90 * @handle: is a #gnutls_cipher_hd_t structure.
91 * @text: the data to be authenticated
92 * @text_size: The length of the data
94 * This function operates on authenticated encryption with
95 * associated data (AEAD) ciphers and authenticate the
96 * input data. This function can only be called once
97 * and before any encryption operations.
99 * Returns: Zero or a negative error code on error.
104 gnutls_cipher_add_auth (gnutls_cipher_hd_t handle
, const void *text
, size_t text_size
)
106 if (_gnutls_cipher_is_aead( (cipher_hd_st
*)handle
)==0)
107 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
109 _gnutls_cipher_auth( (cipher_hd_st
*)handle
, text
, text_size
);
115 * gnutls_cipher_set_iv:
116 * @handle: is a #gnutls_cipher_hd_t structure.
118 * @ivlen: The length of the IV
120 * This function will set the IV to be used for the next
126 gnutls_cipher_set_iv (gnutls_cipher_hd_t handle
, void *iv
, size_t ivlen
)
128 _gnutls_cipher_setiv((cipher_hd_st
*)handle
, iv
, ivlen
);
132 * gnutls_cipher_encrypt:
133 * @handle: is a #gnutls_cipher_hd_t structure.
134 * @text: the data to encrypt
135 * @textlen: The length of data to encrypt
137 * This function will encrypt the given data using the algorithm
138 * specified by the context.
140 * Returns: Zero or a negative error code on error.
145 gnutls_cipher_encrypt (gnutls_cipher_hd_t handle
, void *text
, size_t textlen
)
147 return _gnutls_cipher_encrypt ((cipher_hd_st
*) handle
, text
, textlen
);
151 * gnutls_cipher_decrypt:
152 * @handle: is a #gnutls_cipher_hd_t structure.
153 * @ciphertext: the data to encrypt
154 * @ciphertextlen: The length of data to encrypt
156 * This function will decrypt the given data using the algorithm
157 * specified by the context.
159 * Returns: Zero or a negative error code on error.
164 gnutls_cipher_decrypt (gnutls_cipher_hd_t handle
, void *ciphertext
,
165 size_t ciphertextlen
)
167 return _gnutls_cipher_decrypt ((cipher_hd_st
*) handle
, ciphertext
,
172 * gnutls_cipher_encrypt2:
173 * @handle: is a #gnutls_cipher_hd_t structure.
174 * @text: the data to encrypt
175 * @textlen: The length of data to encrypt
176 * @ciphertext: the encrypted data
177 * @ciphertextlen: The available length for encrypted data
179 * This function will encrypt the given data using the algorithm
180 * specified by the context.
182 * Returns: Zero or a negative error code on error.
187 gnutls_cipher_encrypt2 (gnutls_cipher_hd_t handle
, const void *text
, size_t textlen
,
188 void *ciphertext
, size_t ciphertextlen
)
190 return _gnutls_cipher_encrypt2 ((cipher_hd_st
*) handle
, text
, textlen
,
191 ciphertext
, ciphertextlen
);
195 * gnutls_cipher_decrypt2:
196 * @handle: is a #gnutls_cipher_hd_t structure.
197 * @ciphertext: the data to encrypt
198 * @ciphertextlen: The length of data to encrypt
199 * @text: the decrypted data
200 * @textlen: The available length for decrypted data
202 * This function will decrypt the given data using the algorithm
203 * specified by the context.
205 * Returns: Zero or a negative error code on error.
210 gnutls_cipher_decrypt2 (gnutls_cipher_hd_t handle
, const void *ciphertext
,
211 size_t ciphertextlen
, void *text
, size_t textlen
)
213 return _gnutls_cipher_decrypt2 ((cipher_hd_st
*) handle
, ciphertext
,
214 ciphertextlen
, text
, textlen
);
218 * gnutls_cipher_deinit:
219 * @handle: is a #gnutls_cipher_hd_t structure.
221 * This function will deinitialize all resources occupied by the given
222 * encryption context.
227 gnutls_cipher_deinit (gnutls_cipher_hd_t handle
)
229 _gnutls_cipher_deinit ((cipher_hd_st
*) handle
);
230 gnutls_free (handle
);
238 * @dig: is a #gnutls_hmac_hd_t structure.
239 * @algorithm: the HMAC algorithm to use
240 * @key: The key to be used for encryption
241 * @keylen: The length of the key
243 * This function will initialize an context that can be used to
244 * produce a Message Authentication Code (MAC) of data. This will
245 * effectively use the current crypto backend in use by gnutls or the
246 * cryptographic accelerator in use.
248 * Returns: Zero or a negative error code on error.
253 gnutls_hmac_init (gnutls_hmac_hd_t
* dig
,
254 gnutls_digest_algorithm_t algorithm
,
255 const void *key
, size_t keylen
)
257 *dig
= gnutls_malloc (sizeof (digest_hd_st
));
261 return GNUTLS_E_MEMORY_ERROR
;
264 return _gnutls_hmac_init (((digest_hd_st
*) * dig
), algorithm
, key
, keylen
);
269 * @handle: is a #gnutls_cipher_hd_t structure.
270 * @text: the data to hash
271 * @textlen: The length of data to hash
273 * This function will hash the given data using the algorithm
274 * specified by the context.
276 * Returns: Zero or a negative error code on error.
281 gnutls_hmac (gnutls_hmac_hd_t handle
, const void *text
, size_t textlen
)
283 return _gnutls_hmac ((digest_hd_st
*) handle
, text
, textlen
);
287 * gnutls_hmac_output:
288 * @handle: is a #gnutls_hmac_hd_t structure.
289 * @digest: is the output value of the MAC
291 * This function will output the current MAC value.
296 gnutls_hmac_output (gnutls_hmac_hd_t handle
, void *digest
)
298 _gnutls_hmac_output ((digest_hd_st
*) handle
, digest
);
302 * gnutls_hmac_deinit:
303 * @handle: is a #gnutls_hmac_hd_t structure.
304 * @digest: is the output value of the MAC
306 * This function will deinitialize all resources occupied by
307 * the given hmac context.
312 gnutls_hmac_deinit (gnutls_hmac_hd_t handle
, void *digest
)
314 _gnutls_hmac_deinit ((digest_hd_st
*) handle
, digest
);
315 gnutls_free (handle
);
319 * gnutls_hmac_get_len:
320 * @algorithm: the hmac algorithm to use
322 * This function will return the length of the output data
323 * of the given hmac algorithm.
325 * Returns: The length or zero on error.
330 gnutls_hmac_get_len (gnutls_mac_algorithm_t algorithm
)
332 return _gnutls_hmac_get_algo_len (algorithm
);
337 * @algorithm: the hash algorithm to use
338 * @key: the key to use
339 * @keylen: The length of the key
340 * @text: the data to hash
341 * @textlen: The length of data to hash
342 * @digest: is the output value of the hash
344 * This convenience function will hash the given data and return output
347 * Returns: Zero or a negative error code on error.
352 gnutls_hmac_fast (gnutls_mac_algorithm_t algorithm
,
353 const void *key
, size_t keylen
,
354 const void *text
, size_t textlen
, void *digest
)
356 return _gnutls_hmac_fast (algorithm
, key
, keylen
, text
, textlen
, digest
);
363 * @dig: is a #gnutls_hash_hd_t structure.
364 * @algorithm: the hash algorithm to use
366 * This function will initialize an context that can be used to
367 * produce a Message Digest of data. This will effectively use the
368 * current crypto backend in use by gnutls or the cryptographic
369 * accelerator in use.
371 * Returns: Zero or a negative error code on error.
376 gnutls_hash_init (gnutls_hash_hd_t
* dig
, gnutls_digest_algorithm_t algorithm
)
378 *dig
= gnutls_malloc (sizeof (digest_hd_st
));
382 return GNUTLS_E_MEMORY_ERROR
;
385 return _gnutls_hash_init (((digest_hd_st
*) * dig
), algorithm
);
390 * @handle: is a #gnutls_cipher_hd_t structure.
391 * @text: the data to hash
392 * @textlen: The length of data to hash
394 * This function will hash the given data using the algorithm
395 * specified by the context.
397 * Returns: Zero or a negative error code on error.
402 gnutls_hash (gnutls_hash_hd_t handle
, const void *text
, size_t textlen
)
404 return _gnutls_hash ((digest_hd_st
*) handle
, text
, textlen
);
408 * gnutls_hash_output:
409 * @handle: is a #gnutls_hash_hd_t structure.
410 * @digest: is the output value of the hash
412 * This function will output the current hash value.
417 gnutls_hash_output (gnutls_hash_hd_t handle
, void *digest
)
419 _gnutls_hash_output ((digest_hd_st
*) handle
, digest
);
423 * gnutls_hash_deinit:
424 * @handle: is a #gnutls_hash_hd_t structure.
425 * @digest: is the output value of the hash
427 * This function will deinitialize all resources occupied by
428 * the given hash context.
433 gnutls_hash_deinit (gnutls_hash_hd_t handle
, void *digest
)
435 _gnutls_hash_deinit ((digest_hd_st
*) handle
, digest
);
436 gnutls_free (handle
);
440 * gnutls_hash_get_len:
441 * @algorithm: the hash algorithm to use
443 * This function will return the length of the output data
444 * of the given hash algorithm.
446 * Returns: The length or zero on error.
451 gnutls_hash_get_len (gnutls_digest_algorithm_t algorithm
)
453 return _gnutls_hash_get_algo_len (algorithm
);
458 * @algorithm: the hash algorithm to use
459 * @text: the data to hash
460 * @textlen: The length of data to hash
461 * @digest: is the output value of the hash
463 * This convenience function will hash the given data and return output
466 * Returns: Zero or a negative error code on error.
471 gnutls_hash_fast (gnutls_digest_algorithm_t algorithm
,
472 const void *text
, size_t textlen
, void *digest
)
474 return _gnutls_hash_fast (algorithm
, text
, textlen
, digest
);
478 * gnutls_key_generate:
479 * @key: is a pointer to a #gnutls_datum_t which will contain a newly
481 * @key_size: The number of bytes of the key.
483 * Generates a random key of @key_bytes size.
485 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, or an
491 gnutls_key_generate (gnutls_datum_t
* key
, unsigned int key_size
)
495 key
->size
= key_size
;
496 key
->data
= gnutls_malloc (key
->size
);
500 return GNUTLS_E_MEMORY_ERROR
;
503 ret
= gnutls_rnd (GNUTLS_RND_RANDOM
, key
->data
, key
->size
);
507 _gnutls_free_datum (key
);