Fix use of deprecated types, for now and the future.
[gnutls.git] / lib / gnutls_cipher_int.c
blobe8f7df7aa839c596d2655fb9828e8e58076d2454
1 /*
2 * Copyright (C) 2000, 2004, 2005, 2008 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library 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 gnutls_crypto_single_cipher_st *cc = NULL;
45 /* check if a cipher has been registered
47 cc = _gnutls_get_crypto_cipher (cipher);
48 if (cc != NULL)
50 handle->registered = 1;
51 handle->hd.rh.cc = cc;
52 SR (cc->init (&handle->hd.rh.ctx), cc_cleanup);
53 SR (cc->setkey (handle->hd.rh.ctx, key->data, key->size), cc_cleanup);
54 if (iv->data && iv->size && cc->setiv)
55 SR (cc->setiv (handle->hd.rh.ctx, iv->data, iv->size), cc_cleanup);
56 return 0;
59 handle->registered = 0;
61 /* otherwise use generic cipher interface
63 ret = _gnutls_cipher_ops.init (cipher, &handle->hd.gc);
64 if (ret < 0)
66 gnutls_assert ();
67 return ret;
70 ret = _gnutls_cipher_ops.setkey (handle->hd.gc, key->data, key->size);
71 if (ret < 0)
73 _gnutls_cipher_ops.deinit (handle->hd.gc);
74 gnutls_assert ();
75 return ret;
78 if (iv->data != NULL && iv->size > 0)
79 _gnutls_cipher_ops.setiv (handle->hd.gc, iv->data, iv->size);
81 return 0;
83 cc_cleanup:
85 if (handle->hd.rh.cc)
86 cc->deinit (handle->hd.rh.ctx);
88 return ret;
91 int
92 _gnutls_cipher_encrypt (const cipher_hd_st * handle, void *text, int textlen)
94 if (handle != NULL)
96 if (handle->registered)
98 if (handle->hd.rh.ctx == NULL)
99 return 0;
100 return handle->hd.rh.cc->encrypt (handle->hd.rh.ctx, text, textlen,
101 text, textlen);
104 if (handle->hd.gc == NULL)
105 return 0;
106 return _gnutls_cipher_ops.encrypt (handle->hd.gc, text, textlen, text,
107 textlen);
109 return 0;
113 _gnutls_cipher_decrypt (const cipher_hd_st * handle, void *ciphertext,
114 int ciphertextlen)
116 if (handle != NULL)
118 if (handle->registered)
120 if (handle->hd.rh.ctx == NULL)
121 return 0;
122 return handle->hd.rh.cc->decrypt (handle->hd.rh.ctx, ciphertext,
123 ciphertextlen, ciphertext,
124 ciphertextlen);
127 if (handle->hd.gc == NULL)
128 return 0;
129 return _gnutls_cipher_ops.decrypt (handle->hd.gc, ciphertext,
130 ciphertextlen, ciphertext,
131 ciphertextlen);
133 return 0;
136 void
137 _gnutls_cipher_deinit (cipher_hd_st * handle)
139 if (handle != NULL)
141 if (handle->registered && handle->hd.rh.ctx != NULL)
143 handle->hd.rh.cc->deinit (handle->hd.rh.ctx);
144 return;
146 _gnutls_cipher_ops.deinit (handle->hd.gc);