documented fix
[gnutls.git] / lib / gnutls_cipher_int.c
blobd3b7698a94e1c9e72e2b6f3c8b80de942778878c
1 /*
2 * Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
25 #include <gnutls_int.h>
26 #include <gnutls_errors.h>
27 #include <gnutls_cipher_int.h>
28 #include <gnutls_datum.h>
29 #include <gnutls/crypto.h>
30 #include <crypto.h>
32 #define SR(x, cleanup) if ( (x)<0 ) { \
33 gnutls_assert(); \
34 ret = GNUTLS_E_INTERNAL_ERROR; \
35 goto cleanup; \
38 int
39 _gnutls_cipher_init (cipher_hd_st * handle, gnutls_cipher_algorithm_t cipher,
40 const gnutls_datum_t * key, const gnutls_datum_t * iv)
42 int ret = GNUTLS_E_INTERNAL_ERROR;
43 const gnutls_crypto_cipher_st *cc = NULL;
45 /* check if a cipher has been registered
47 cc = _gnutls_get_crypto_cipher (cipher);
48 if (cc != NULL)
50 SR (cc->init (cipher, &handle->handle), cc_cleanup);
51 SR (cc->setkey (handle->handle, key->data, key->size), cc_cleanup);
53 handle->encrypt = cc->encrypt;
54 handle->decrypt = cc->decrypt;
55 handle->deinit = cc->deinit;
57 if (iv && iv->data && iv->size && cc->setiv)
58 SR (cc->setiv (handle->handle, iv->data, iv->size), cc_cleanup);
59 return 0;
62 /* otherwise use generic cipher interface
64 ret = _gnutls_cipher_ops.init (cipher, &handle->handle);
65 if (ret < 0)
67 gnutls_assert ();
68 return ret;
71 ret = _gnutls_cipher_ops.setkey (handle->handle, key->data, key->size);
72 if (ret < 0)
74 _gnutls_cipher_ops.deinit (handle->handle);
75 gnutls_assert ();
76 return ret;
79 handle->encrypt = _gnutls_cipher_ops.encrypt;
80 handle->decrypt = _gnutls_cipher_ops.decrypt;
81 handle->deinit = _gnutls_cipher_ops.deinit;
83 if (iv && iv->data != NULL && iv->size > 0)
84 _gnutls_cipher_ops.setiv (handle->handle, iv->data, iv->size);
86 return 0;
88 cc_cleanup:
90 if (handle->handle)
91 cc->deinit (handle->handle);
93 return ret;
96 int
97 _gnutls_cipher_encrypt (const cipher_hd_st * handle, void *text, int textlen)
99 if (handle != NULL && handle->handle != NULL)
101 return handle->encrypt (handle->handle, text, textlen, text, textlen);
103 return 0;
107 _gnutls_cipher_decrypt (const cipher_hd_st * handle, void *ciphertext,
108 int ciphertextlen)
110 if (handle != NULL && handle->handle != NULL)
112 return handle->decrypt (handle->handle, ciphertext, ciphertextlen,
113 ciphertext, ciphertextlen);
115 return 0;
119 _gnutls_cipher_encrypt2 (const cipher_hd_st * handle, const void *text,
120 int textlen, void *ciphertext, int ciphertextlen)
122 if (handle != NULL && handle->handle != NULL)
124 return handle->encrypt (handle->handle, text, textlen, ciphertext,
125 ciphertextlen);
127 return 0;
131 _gnutls_cipher_decrypt2 (const cipher_hd_st * handle, const void *ciphertext,
132 int ciphertextlen, void *text, int textlen)
134 if (handle != NULL && handle->handle != NULL)
136 return handle->decrypt (handle->handle, ciphertext, ciphertextlen,
137 text, textlen);
139 return 0;
142 void
143 _gnutls_cipher_deinit (cipher_hd_st * handle)
145 if (handle != NULL && handle->handle != NULL)
147 handle->deinit (handle->handle);
148 handle->handle = NULL;