Corrected functionality of gnutls_record_get_direction(). Reported by Philip Allison.
[gnutls.git] / lib / crypto-api.c
blobd89b7a2b53008035bbf534249e53045c54d230cb
1 /*
2 * Copyright (C) 2000, 2004, 2005, 2008, 2010 Free Software Foundation,
3 * Inc.
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,
22 * USA
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>
31 #include <crypto.h>
33 /**
34 * gnutls_cipher_init:
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
43 * accelerator in use.
45 * Returns: Zero or a negative value on error.
47 * Since: 2.10.0
48 **/
49 int
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));
55 if (*handle == NULL)
57 gnutls_assert ();
58 return GNUTLS_E_MEMORY_ERROR;
61 return _gnutls_cipher_init (((cipher_hd_st *) * handle), cipher, key, iv);
64 /**
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.
75 * Since: 2.10.0
76 **/
77 int
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);
83 /**
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.
94 * Since: 2.10.0
95 **/
96 int
97 gnutls_cipher_decrypt (gnutls_cipher_hd_t handle, void *ciphertext,
98 size_t ciphertextlen)
100 return _gnutls_cipher_decrypt ((cipher_hd_st *) handle, ciphertext,
101 ciphertextlen);
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.
117 * Since: 2.10.0
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.
140 * Since: 2.10.0
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.
157 * Since: 2.10.0
159 void
160 gnutls_cipher_deinit (gnutls_cipher_hd_t handle)
162 _gnutls_cipher_deinit ((cipher_hd_st *) handle);
163 gnutls_free (handle);
167 /* HMAC */
170 * gnutls_hmac_init:
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.
183 * Since: 2.10.0
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));
191 if (*dig == NULL)
193 gnutls_assert ();
194 return GNUTLS_E_MEMORY_ERROR;
197 return _gnutls_hmac_init (((digest_hd_st *) * dig), algorithm, key, keylen);
201 * gnutls_hmac:
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.
211 * Since: 2.10.0
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.
226 * Since: 2.10.0
228 void
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.
242 * Since: 2.10.0
244 void
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.
260 * Since: 2.10.0
263 gnutls_hmac_get_len (gnutls_mac_algorithm_t algorithm)
265 return _gnutls_hmac_get_algo_len (algorithm);
269 * gnutls_hmac_fast:
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
278 * on a single call.
280 * Returns: Zero or a negative value on error.
282 * Since: 2.10.0
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);
292 /* HASH */
295 * gnutls_hash_init:
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.
306 * Since: 2.10.0
309 gnutls_hash_init (gnutls_hash_hd_t * dig, gnutls_digest_algorithm_t algorithm)
311 *dig = gnutls_malloc (sizeof (digest_hd_st));
312 if (*dig == NULL)
314 gnutls_assert ();
315 return GNUTLS_E_MEMORY_ERROR;
318 return _gnutls_hash_init (((digest_hd_st *) * dig), algorithm);
322 * gnutls_hash:
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.
332 * Since: 2.10.0
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.
347 * Since: 2.10.0
349 void
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.
363 * Since: 2.10.0
365 void
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.
381 * Since: 2.10.0
384 gnutls_hash_get_len (gnutls_digest_algorithm_t algorithm)
386 return _gnutls_hash_get_algo_len (algorithm);
390 * gnutls_hash_fast:
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
397 * on a single call.
399 * Returns: Zero or a negative value on error.
401 * Since: 2.10.0
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);