Use libtasn1 v2.4.
[gnutls.git] / lib / crypto-api.c
bloba88ab94722dc5cb656ebc9094b2bdda51ff150aa
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 library 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 - initializes a crypto context
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
41 * be used for encryption/decryption of data. This will effectively
42 * use the current crypto backend in use by gnutls or the
43 * cryptographic accelerator in use.
45 * Returns: Zero or a negative value on error.
46 **/
47 int
48 gnutls_cipher_init (gnutls_cipher_hd_t * handle, gnutls_cipher_algorithm_t cipher,
49 const gnutls_datum_t * key, const gnutls_datum_t * iv)
51 *handle = gnutls_malloc(sizeof(cipher_hd_st));
52 if (*handle == NULL) {
53 gnutls_assert();
54 return GNUTLS_E_MEMORY_ERROR;
57 return _gnutls_cipher_init(((cipher_hd_st*)*handle), cipher, key, iv);
60 /**
61 * gnutls_cipher_encrypt - encrypts the given data
62 * @handle: is a #gnutls_cipher_hd_t structure.
63 * @text: the data to encrypt
64 * @textlen: The length of data to encrypt
66 * This function will encrypt the given data using the algorithm
67 * specified by the context.
69 * Returns: Zero or a negative value on error.
70 **/
71 int gnutls_cipher_encrypt (gnutls_cipher_hd_t handle, void *text, int textlen)
73 return _gnutls_cipher_encrypt((cipher_hd_st*)handle, text, textlen);
76 /**
77 * gnutls_cipher_decrypt - decrypts the given data
78 * @handle: is a #gnutls_cipher_hd_t structure.
79 * @text: the data to encrypt
80 * @textlen: The length of data to encrypt
82 * This function will decrypt the given data using the algorithm
83 * specified by the context.
85 * Returns: Zero or a negative value on error.
86 **/
87 int
88 gnutls_cipher_decrypt (gnutls_cipher_hd_t handle, void *ciphertext,
89 int ciphertextlen)
91 return _gnutls_cipher_decrypt((cipher_hd_st*)handle, ciphertext, ciphertextlen);
94 /**
95 * gnutls_cipher_deinit - deinitializes resources by a context
96 * @handle: is a #gnutls_cipher_hd_t structure.
98 * This function will deinitialize all resources occupied by
99 * the given encryption context.
101 * Returns: This function has no return value.
103 void
104 gnutls_cipher_deinit (gnutls_cipher_hd_t handle)
106 return _gnutls_cipher_deinit((cipher_hd_st*)handle);
110 /* HMAC */
113 * gnutls_hmac_init - initializes an HMAC context
114 * @handle: is a #gnutls_hmac_hd_t structure.
115 * @algorithm: the HMAC algorithm to use
116 * @key: The key to be used for encryption
117 * @keylen: The length of the key
119 * This function will initialize an context that can
120 * be used to produce a Message Authentication Code (MAC) of data.
121 * This will effectively use the current crypto backend in use by gnutls or
122 * the cryptographic accelerator in use.
124 * Returns: Zero or a negative value on error.
127 gnutls_hmac_init (gnutls_hmac_hd_t * dig, gnutls_digest_algorithm_t algorithm,
128 const void *key, int keylen)
130 *dig = gnutls_malloc(sizeof(digest_hd_st));
131 if (*dig == NULL) {
132 gnutls_assert();
133 return GNUTLS_E_MEMORY_ERROR;
136 return _gnutls_hmac_init(((digest_hd_st*)*dig), algorithm, key, keylen);
140 * gnutls_hmac - hashes the given data
141 * @handle: is a #gnutls_cipher_hd_t structure.
142 * @text: the data to hash
143 * @textlen: The length of data to hash
145 * This function will hash the given data using the algorithm
146 * specified by the context.
148 * Returns: Zero or a negative value on error.
150 int gnutls_hmac (gnutls_hmac_hd_t handle, const void *text, size_t textlen)
152 return _gnutls_hmac((digest_hd_st*)handle, text, textlen);
156 * gnutls_hmac_output - Outputs the current MAC value
157 * @handle: is a #gnutls_hmac_hd_t structure.
158 * @digest: is the output value of the MAC
160 * This function will output the current MAC value.
162 * Returns: This function has no return value.
164 void
165 gnutls_hmac_output (gnutls_hmac_hd_t handle, void *digest)
167 return _gnutls_hmac_output((digest_hd_st*)handle, digest);
171 * gnutls_hmac_deinit - deinitializes resources by a context
172 * @handle: is a #gnutls_hmac_hd_t structure.
173 * @digest: is the output value of the MAC
175 * This function will deinitialize all resources occupied by
176 * the given hmac context.
178 * Returns: This function has no return value.
180 void gnutls_hmac_deinit (gnutls_hmac_hd_t handle, void *digest)
182 _gnutls_hmac_deinit((digest_hd_st*)handle, digest);
186 * gnutls_hmac_get_len - returns the length of the output data
187 * @algorithm: the hmac algorithm to use
189 * This function will return the length of the output data
190 * of the given hmac algorithm.
192 * Returns: The length or zero on error.
195 int gnutls_hmac_get_len( gnutls_mac_algorithm_t algorithm)
197 return _gnutls_hmac_get_algo_len(algorithm);
201 * gnutls_hmac_fast - hashes data
202 * @algorithm: the hash algorithm to use
203 * @key: the key to use
204 * @keylen: The length of the key
205 * @text: the data to hash
206 * @textlen: The length of data to hash
207 * @digest: is the output value of the hash
209 * This convenience function will hash the given data and return output
210 * on a single call.
212 * Returns: Zero or a negative value on error.
214 int gnutls_hmac_fast( gnutls_mac_algorithm_t algorithm, const void* key, int keylen,
215 const void* text, size_t textlen, void* digest)
217 return _gnutls_hmac_fast(algorithm, key, keylen, text, textlen, digest);
220 /* HASH */
223 * gnutls_hash_init - initializes an hash context
224 * @handle: is a #gnutls_hash_hd_t structure.
225 * @algorithm: the hash algorithm to use
226 * @key: The key to be used for encryption
227 * @keylen: The length of the key
229 * This function will initialize an context that can
230 * be used to produce a Message Digest of data.
231 * This will effectively use the current crypto backend in use by gnutls or
232 * the cryptographic accelerator in use.
234 * Returns: Zero or a negative value on error.
236 int gnutls_hash_init (gnutls_hash_hd_t * dig, gnutls_digest_algorithm_t algorithm)
238 *dig = gnutls_malloc(sizeof(digest_hd_st));
239 if (*dig == NULL) {
240 gnutls_assert();
241 return GNUTLS_E_MEMORY_ERROR;
244 return _gnutls_hash_init(((digest_hd_st*)*dig), algorithm);
248 * gnutls_hash - hashes the given data
249 * @handle: is a #gnutls_cipher_hd_t structure.
250 * @text: the data to hash
251 * @textlen: The length of data to hash
253 * This function will hash the given data using the algorithm
254 * specified by the context.
256 * Returns: Zero or a negative value on error.
258 int gnutls_hash (gnutls_hash_hd_t handle, const void *text, size_t textlen)
260 return _gnutls_hash((digest_hd_st*)handle, text, textlen);
264 * gnutls_hash_output - Outputs the current hash value
265 * @handle: is a #gnutls_hash_hd_t structure.
266 * @digest: is the output value of the hash
268 * This function will output the current hash value.
270 * Returns: This function has no return value.
272 void gnutls_hash_output (gnutls_hash_hd_t handle, void *digest)
274 return _gnutls_hash_output((digest_hd_st*)handle, digest);
278 * gnutls_hash_deinit - deinitializes resources by a context
279 * @handle: is a #gnutls_hash_hd_t structure.
280 * @digest: is the output value of the hash
282 * This function will deinitialize all resources occupied by
283 * the given hash context.
285 * Returns: This function has no return value.
287 void
288 gnutls_hash_deinit (gnutls_hash_hd_t handle, void *digest)
290 _gnutls_hash_deinit((digest_hd_st*)handle, digest);
294 * gnutls_hash_get_len - returns the length of the output data
295 * @algorithm: the hash algorithm to use
297 * This function will return the length of the output data
298 * of the given hash algorithm.
300 * Returns: The length or zero on error.
303 int gnutls_hash_get_len( gnutls_digest_algorithm_t algorithm)
305 return _gnutls_hash_get_algo_len(algorithm);
309 * gnutls_hash_fast - initializes an hash context
310 * @algorithm: the hash algorithm to use
311 * @text: the data to hash
312 * @textlen: The length of data to hash
313 * @digest: is the output value of the hash
315 * This convenience function will hash the given data and return output
316 * on a single call.
318 * Returns: Zero or a negative value on error.
320 int gnutls_hash_fast (gnutls_digest_algorithm_t algorithm,
321 const void *text, size_t textlen, void *digest)
323 return _gnutls_hash_fast(algorithm, text, textlen, digest);