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
);
65 * gnutls_cipher_encrypt:
66 * @handle: is a #gnutls_cipher_hd_t structure.
67 * @text: the data to encrypt
68 * @textlen: The length of data to encrypt
70 * This function will encrypt the given data using the algorithm
71 * specified by the context.
73 * Returns: Zero or a negative value on error.
78 gnutls_cipher_encrypt (gnutls_cipher_hd_t handle
, void *text
, size_t textlen
)
80 return _gnutls_cipher_encrypt ((cipher_hd_st
*) handle
, text
, textlen
);
84 * gnutls_cipher_decrypt:
85 * @handle: is a #gnutls_cipher_hd_t structure.
86 * @ciphertext: the data to encrypt
87 * @ciphertextlen: The length of data to encrypt
89 * This function will decrypt the given data using the algorithm
90 * specified by the context.
92 * Returns: Zero or a negative value on error.
97 gnutls_cipher_decrypt (gnutls_cipher_hd_t handle
, void *ciphertext
,
100 return _gnutls_cipher_decrypt ((cipher_hd_st
*) handle
, ciphertext
,
105 * gnutls_cipher_encrypt2:
106 * @handle: is a #gnutls_cipher_hd_t structure.
107 * @text: the data to encrypt
108 * @textlen: The length of data to encrypt
109 * @ciphertext: the encrypted data
110 * @ciphertextlen: The available length for encrypted data
112 * This function will encrypt the given data using the algorithm
113 * specified by the context.
115 * Returns: Zero or a negative value on error.
120 gnutls_cipher_encrypt2 (gnutls_cipher_hd_t handle
, void *text
, size_t textlen
,
121 void *ciphertext
, size_t ciphertextlen
)
123 return _gnutls_cipher_encrypt2 ((cipher_hd_st
*) handle
, text
, textlen
,
124 ciphertext
, ciphertextlen
);
128 * gnutls_cipher_decrypt2:
129 * @handle: is a #gnutls_cipher_hd_t structure.
130 * @ciphertext: the data to encrypt
131 * @ciphertextlen: The length of data to encrypt
132 * @text: the decrypted data
133 * @textlen: The available length for decrypted data
135 * This function will decrypt the given data using the algorithm
136 * specified by the context.
138 * Returns: Zero or a negative value on error.
143 gnutls_cipher_decrypt2 (gnutls_cipher_hd_t handle
, const void *ciphertext
,
144 size_t ciphertextlen
, void *text
, size_t textlen
)
146 return _gnutls_cipher_decrypt2 ((cipher_hd_st
*) handle
, ciphertext
,
147 ciphertextlen
, text
, textlen
);
151 * gnutls_cipher_deinit:
152 * @handle: is a #gnutls_cipher_hd_t structure.
154 * This function will deinitialize all resources occupied by the given
155 * encryption context.
160 gnutls_cipher_deinit (gnutls_cipher_hd_t handle
)
162 _gnutls_cipher_deinit ((cipher_hd_st
*) handle
);
163 gnutls_free (handle
);
171 * @dig: is a #gnutls_hmac_hd_t structure.
172 * @algorithm: the HMAC algorithm to use
173 * @key: The key to be used for encryption
174 * @keylen: The length of the key
176 * This function will initialize an context that can be used to
177 * produce a Message Authentication Code (MAC) of data. This will
178 * effectively use the current crypto backend in use by gnutls or the
179 * cryptographic accelerator in use.
181 * Returns: Zero or a negative value on error.
186 gnutls_hmac_init (gnutls_hmac_hd_t
* dig
,
187 gnutls_digest_algorithm_t algorithm
,
188 const void *key
, size_t keylen
)
190 *dig
= gnutls_malloc (sizeof (digest_hd_st
));
194 return GNUTLS_E_MEMORY_ERROR
;
197 return _gnutls_hmac_init (((digest_hd_st
*) * dig
), algorithm
, key
, keylen
);
202 * @handle: is a #gnutls_cipher_hd_t structure.
203 * @text: the data to hash
204 * @textlen: The length of data to hash
206 * This function will hash the given data using the algorithm
207 * specified by the context.
209 * Returns: Zero or a negative value on error.
214 gnutls_hmac (gnutls_hmac_hd_t handle
, const void *text
, size_t textlen
)
216 return _gnutls_hmac ((digest_hd_st
*) handle
, text
, textlen
);
220 * gnutls_hmac_output:
221 * @handle: is a #gnutls_hmac_hd_t structure.
222 * @digest: is the output value of the MAC
224 * This function will output the current MAC value.
229 gnutls_hmac_output (gnutls_hmac_hd_t handle
, void *digest
)
231 _gnutls_hmac_output ((digest_hd_st
*) handle
, digest
);
235 * gnutls_hmac_deinit:
236 * @handle: is a #gnutls_hmac_hd_t structure.
237 * @digest: is the output value of the MAC
239 * This function will deinitialize all resources occupied by
240 * the given hmac context.
245 gnutls_hmac_deinit (gnutls_hmac_hd_t handle
, void *digest
)
247 _gnutls_hmac_deinit ((digest_hd_st
*) handle
, digest
);
248 gnutls_free (handle
);
252 * gnutls_hmac_get_len:
253 * @algorithm: the hmac algorithm to use
255 * This function will return the length of the output data
256 * of the given hmac algorithm.
258 * Returns: The length or zero on error.
263 gnutls_hmac_get_len (gnutls_mac_algorithm_t algorithm
)
265 return _gnutls_hmac_get_algo_len (algorithm
);
270 * @algorithm: the hash algorithm to use
271 * @key: the key to use
272 * @keylen: The length of the key
273 * @text: the data to hash
274 * @textlen: The length of data to hash
275 * @digest: is the output value of the hash
277 * This convenience function will hash the given data and return output
280 * Returns: Zero or a negative value on error.
285 gnutls_hmac_fast (gnutls_mac_algorithm_t algorithm
,
286 const void *key
, size_t keylen
,
287 const void *text
, size_t textlen
, void *digest
)
289 return _gnutls_hmac_fast (algorithm
, key
, keylen
, text
, textlen
, digest
);
296 * @dig: is a #gnutls_hash_hd_t structure.
297 * @algorithm: the hash algorithm to use
299 * This function will initialize an context that can be used to
300 * produce a Message Digest of data. This will effectively use the
301 * current crypto backend in use by gnutls or the cryptographic
302 * accelerator in use.
304 * Returns: Zero or a negative value on error.
309 gnutls_hash_init (gnutls_hash_hd_t
* dig
, gnutls_digest_algorithm_t algorithm
)
311 *dig
= gnutls_malloc (sizeof (digest_hd_st
));
315 return GNUTLS_E_MEMORY_ERROR
;
318 return _gnutls_hash_init (((digest_hd_st
*) * dig
), algorithm
);
323 * @handle: is a #gnutls_cipher_hd_t structure.
324 * @text: the data to hash
325 * @textlen: The length of data to hash
327 * This function will hash the given data using the algorithm
328 * specified by the context.
330 * Returns: Zero or a negative value on error.
335 gnutls_hash (gnutls_hash_hd_t handle
, const void *text
, size_t textlen
)
337 return _gnutls_hash ((digest_hd_st
*) handle
, text
, textlen
);
341 * gnutls_hash_output:
342 * @handle: is a #gnutls_hash_hd_t structure.
343 * @digest: is the output value of the hash
345 * This function will output the current hash value.
350 gnutls_hash_output (gnutls_hash_hd_t handle
, void *digest
)
352 _gnutls_hash_output ((digest_hd_st
*) handle
, digest
);
356 * gnutls_hash_deinit:
357 * @handle: is a #gnutls_hash_hd_t structure.
358 * @digest: is the output value of the hash
360 * This function will deinitialize all resources occupied by
361 * the given hash context.
366 gnutls_hash_deinit (gnutls_hash_hd_t handle
, void *digest
)
368 _gnutls_hash_deinit ((digest_hd_st
*) handle
, digest
);
369 gnutls_free (handle
);
373 * gnutls_hash_get_len:
374 * @algorithm: the hash algorithm to use
376 * This function will return the length of the output data
377 * of the given hash algorithm.
379 * Returns: The length or zero on error.
384 gnutls_hash_get_len (gnutls_digest_algorithm_t algorithm
)
386 return _gnutls_hash_get_algo_len (algorithm
);
391 * @algorithm: the hash algorithm to use
392 * @text: the data to hash
393 * @textlen: The length of data to hash
394 * @digest: is the output value of the hash
396 * This convenience function will hash the given data and return output
399 * Returns: Zero or a negative value on error.
404 gnutls_hash_fast (gnutls_digest_algorithm_t algorithm
,
405 const void *text
, size_t textlen
, void *digest
)
407 return _gnutls_hash_fast (algorithm
, text
, textlen
, digest
);