2 * Copyright (C) 2000, 2004, 2005, 2008, 2010 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 2.1 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
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 #include <gnutls_int.h>
27 #include <gnutls_errors.h>
28 #include <gnutls_cipher_int.h>
29 #include <gnutls_datum.h>
30 #include <gnutls/crypto.h>
35 * @handle: is a #gnutls_cipher_hd_t structure.
36 * @cipher: the encryption algorithm to use
37 * @key: The key to be used for encryption
38 * @iv: The IV to use (if not applicable set NULL)
40 * This function will initialize an context that can be used for
41 * encryption/decryption of data. This will effectively use the
42 * current crypto backend in use by gnutls or the cryptographic
45 * Returns: Zero or a negative value on error.
50 gnutls_cipher_init (gnutls_cipher_hd_t
* handle
,
51 gnutls_cipher_algorithm_t cipher
,
52 const gnutls_datum_t
* key
, const gnutls_datum_t
* iv
)
54 *handle
= gnutls_malloc (sizeof (cipher_hd_st
));
58 return GNUTLS_E_MEMORY_ERROR
;
61 return _gnutls_cipher_init (((cipher_hd_st
*) * handle
), cipher
, key
, iv
);
66 * @handle: is a #gnutls_cipher_hd_t structure.
67 * @tag: will hold the tag
68 * @tag_size: The length of the tag to return
70 * This function operates on authenticated encryption with
71 * associated data (AEAD) ciphers and will return the
74 * Returns: Zero or a negative value on error.
79 gnutls_cipher_tag (gnutls_cipher_hd_t handle
, void *tag
, size_t tag_size
)
81 if (_gnutls_cipher_is_aead( (cipher_hd_st
*)handle
)==0)
82 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
84 _gnutls_cipher_tag( (cipher_hd_st
*)handle
, tag
, tag_size
);
90 * gnutls_cipher_add_auth:
91 * @handle: is a #gnutls_cipher_hd_t structure.
92 * @text: the data to be authenticated
93 * @text_size: The length of the data
95 * This function operates on authenticated encryption with
96 * associated data (AEAD) ciphers and authenticate the
97 * input data. This function can only be called before
98 * encryption operations.
100 * Returns: Zero or a negative value on error.
105 gnutls_cipher_add_auth (gnutls_cipher_hd_t handle
, const void *text
, size_t text_size
)
107 if (_gnutls_cipher_is_aead( (cipher_hd_st
*)handle
)==0)
108 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
110 _gnutls_cipher_auth( (cipher_hd_st
*)handle
, text
, text_size
);
116 * gnutls_cipher_encrypt:
117 * @handle: is a #gnutls_cipher_hd_t structure.
118 * @text: the data to encrypt
119 * @textlen: The length of data to encrypt
121 * This function will encrypt the given data using the algorithm
122 * specified by the context.
124 * Returns: Zero or a negative value on error.
129 gnutls_cipher_encrypt (gnutls_cipher_hd_t handle
, void *text
, size_t textlen
)
131 return _gnutls_cipher_encrypt ((cipher_hd_st
*) handle
, text
, textlen
);
135 * gnutls_cipher_decrypt:
136 * @handle: is a #gnutls_cipher_hd_t structure.
137 * @ciphertext: the data to encrypt
138 * @ciphertextlen: The length of data to encrypt
140 * This function will decrypt the given data using the algorithm
141 * specified by the context.
143 * Returns: Zero or a negative value on error.
148 gnutls_cipher_decrypt (gnutls_cipher_hd_t handle
, void *ciphertext
,
149 size_t ciphertextlen
)
151 return _gnutls_cipher_decrypt ((cipher_hd_st
*) handle
, ciphertext
,
156 * gnutls_cipher_encrypt2:
157 * @handle: is a #gnutls_cipher_hd_t structure.
158 * @text: the data to encrypt
159 * @textlen: The length of data to encrypt
160 * @ciphertext: the encrypted data
161 * @ciphertextlen: The available length for encrypted data
163 * This function will encrypt the given data using the algorithm
164 * specified by the context.
166 * Returns: Zero or a negative value on error.
171 gnutls_cipher_encrypt2 (gnutls_cipher_hd_t handle
, void *text
, size_t textlen
,
172 void *ciphertext
, size_t ciphertextlen
)
174 return _gnutls_cipher_encrypt2 ((cipher_hd_st
*) handle
, text
, textlen
,
175 ciphertext
, ciphertextlen
);
179 * gnutls_cipher_decrypt2:
180 * @handle: is a #gnutls_cipher_hd_t structure.
181 * @ciphertext: the data to encrypt
182 * @ciphertextlen: The length of data to encrypt
183 * @text: the decrypted data
184 * @textlen: The available length for decrypted data
186 * This function will decrypt the given data using the algorithm
187 * specified by the context.
189 * Returns: Zero or a negative value on error.
194 gnutls_cipher_decrypt2 (gnutls_cipher_hd_t handle
, const void *ciphertext
,
195 size_t ciphertextlen
, void *text
, size_t textlen
)
197 return _gnutls_cipher_decrypt2 ((cipher_hd_st
*) handle
, ciphertext
,
198 ciphertextlen
, text
, textlen
);
202 * gnutls_cipher_deinit:
203 * @handle: is a #gnutls_cipher_hd_t structure.
205 * This function will deinitialize all resources occupied by the given
206 * encryption context.
211 gnutls_cipher_deinit (gnutls_cipher_hd_t handle
)
213 _gnutls_cipher_deinit ((cipher_hd_st
*) handle
);
214 gnutls_free (handle
);
222 * @dig: is a #gnutls_hmac_hd_t structure.
223 * @algorithm: the HMAC algorithm to use
224 * @key: The key to be used for encryption
225 * @keylen: The length of the key
227 * This function will initialize an context that can be used to
228 * produce a Message Authentication Code (MAC) of data. This will
229 * effectively use the current crypto backend in use by gnutls or the
230 * cryptographic accelerator in use.
232 * Returns: Zero or a negative value on error.
237 gnutls_hmac_init (gnutls_hmac_hd_t
* dig
,
238 gnutls_digest_algorithm_t algorithm
,
239 const void *key
, size_t keylen
)
241 *dig
= gnutls_malloc (sizeof (digest_hd_st
));
245 return GNUTLS_E_MEMORY_ERROR
;
248 return _gnutls_hmac_init (((digest_hd_st
*) * dig
), algorithm
, key
, keylen
);
253 * @handle: is a #gnutls_cipher_hd_t structure.
254 * @text: the data to hash
255 * @textlen: The length of data to hash
257 * This function will hash the given data using the algorithm
258 * specified by the context.
260 * Returns: Zero or a negative value on error.
265 gnutls_hmac (gnutls_hmac_hd_t handle
, const void *text
, size_t textlen
)
267 return _gnutls_hmac ((digest_hd_st
*) handle
, text
, textlen
);
271 * gnutls_hmac_output:
272 * @handle: is a #gnutls_hmac_hd_t structure.
273 * @digest: is the output value of the MAC
275 * This function will output the current MAC value.
280 gnutls_hmac_output (gnutls_hmac_hd_t handle
, void *digest
)
282 _gnutls_hmac_output ((digest_hd_st
*) handle
, digest
);
286 * gnutls_hmac_deinit:
287 * @handle: is a #gnutls_hmac_hd_t structure.
288 * @digest: is the output value of the MAC
290 * This function will deinitialize all resources occupied by
291 * the given hmac context.
296 gnutls_hmac_deinit (gnutls_hmac_hd_t handle
, void *digest
)
298 _gnutls_hmac_deinit ((digest_hd_st
*) handle
, digest
);
299 gnutls_free (handle
);
303 * gnutls_hmac_get_len:
304 * @algorithm: the hmac algorithm to use
306 * This function will return the length of the output data
307 * of the given hmac algorithm.
309 * Returns: The length or zero on error.
314 gnutls_hmac_get_len (gnutls_mac_algorithm_t algorithm
)
316 return _gnutls_hmac_get_algo_len (algorithm
);
321 * @algorithm: the hash algorithm to use
322 * @key: the key to use
323 * @keylen: The length of the key
324 * @text: the data to hash
325 * @textlen: The length of data to hash
326 * @digest: is the output value of the hash
328 * This convenience function will hash the given data and return output
331 * Returns: Zero or a negative value on error.
336 gnutls_hmac_fast (gnutls_mac_algorithm_t algorithm
,
337 const void *key
, size_t keylen
,
338 const void *text
, size_t textlen
, void *digest
)
340 return _gnutls_hmac_fast (algorithm
, key
, keylen
, text
, textlen
, digest
);
347 * @dig: is a #gnutls_hash_hd_t structure.
348 * @algorithm: the hash algorithm to use
350 * This function will initialize an context that can be used to
351 * produce a Message Digest of data. This will effectively use the
352 * current crypto backend in use by gnutls or the cryptographic
353 * accelerator in use.
355 * Returns: Zero or a negative value on error.
360 gnutls_hash_init (gnutls_hash_hd_t
* dig
, gnutls_digest_algorithm_t algorithm
)
362 *dig
= gnutls_malloc (sizeof (digest_hd_st
));
366 return GNUTLS_E_MEMORY_ERROR
;
369 return _gnutls_hash_init (((digest_hd_st
*) * dig
), algorithm
);
374 * @handle: is a #gnutls_cipher_hd_t structure.
375 * @text: the data to hash
376 * @textlen: The length of data to hash
378 * This function will hash the given data using the algorithm
379 * specified by the context.
381 * Returns: Zero or a negative value on error.
386 gnutls_hash (gnutls_hash_hd_t handle
, const void *text
, size_t textlen
)
388 return _gnutls_hash ((digest_hd_st
*) handle
, text
, textlen
);
392 * gnutls_hash_output:
393 * @handle: is a #gnutls_hash_hd_t structure.
394 * @digest: is the output value of the hash
396 * This function will output the current hash value.
401 gnutls_hash_output (gnutls_hash_hd_t handle
, void *digest
)
403 _gnutls_hash_output ((digest_hd_st
*) handle
, digest
);
407 * gnutls_hash_deinit:
408 * @handle: is a #gnutls_hash_hd_t structure.
409 * @digest: is the output value of the hash
411 * This function will deinitialize all resources occupied by
412 * the given hash context.
417 gnutls_hash_deinit (gnutls_hash_hd_t handle
, void *digest
)
419 _gnutls_hash_deinit ((digest_hd_st
*) handle
, digest
);
420 gnutls_free (handle
);
424 * gnutls_hash_get_len:
425 * @algorithm: the hash algorithm to use
427 * This function will return the length of the output data
428 * of the given hash algorithm.
430 * Returns: The length or zero on error.
435 gnutls_hash_get_len (gnutls_digest_algorithm_t algorithm
)
437 return _gnutls_hash_get_algo_len (algorithm
);
442 * @algorithm: the hash algorithm to use
443 * @text: the data to hash
444 * @textlen: The length of data to hash
445 * @digest: is the output value of the hash
447 * This convenience function will hash the given data and return output
450 * Returns: Zero or a negative value on error.
455 gnutls_hash_fast (gnutls_digest_algorithm_t algorithm
,
456 const void *text
, size_t textlen
, void *digest
)
458 return _gnutls_hash_fast (algorithm
, text
, textlen
, digest
);