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,
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>
32 #define SR(x, cleanup) if ( (x)<0 ) { \
34 ret = GNUTLS_E_INTERNAL_ERROR; \
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
);
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
);
59 handle
->registered
= 0;
61 /* otherwise use generic cipher interface
63 ret
= _gnutls_cipher_ops
.init (cipher
, &handle
->hd
.gc
);
70 ret
= _gnutls_cipher_ops
.setkey (handle
->hd
.gc
, key
->data
, key
->size
);
73 _gnutls_cipher_ops
.deinit (handle
->hd
.gc
);
78 if (iv
->data
!= NULL
&& iv
->size
> 0)
79 _gnutls_cipher_ops
.setiv (handle
->hd
.gc
, iv
->data
, iv
->size
);
86 cc
->deinit (handle
->hd
.rh
.ctx
);
92 _gnutls_cipher_encrypt (const cipher_hd_st
* handle
, void *text
, int textlen
)
96 if (handle
->registered
)
98 if (handle
->hd
.rh
.ctx
== NULL
)
100 return handle
->hd
.rh
.cc
->encrypt (handle
->hd
.rh
.ctx
, text
, textlen
,
104 if (handle
->hd
.gc
== NULL
)
106 return _gnutls_cipher_ops
.encrypt (handle
->hd
.gc
, text
, textlen
, text
,
113 _gnutls_cipher_decrypt (const cipher_hd_st
* handle
, void *ciphertext
,
118 if (handle
->registered
)
120 if (handle
->hd
.rh
.ctx
== NULL
)
122 return handle
->hd
.rh
.cc
->decrypt (handle
->hd
.rh
.ctx
, ciphertext
,
123 ciphertextlen
, ciphertext
,
127 if (handle
->hd
.gc
== NULL
)
129 return _gnutls_cipher_ops
.decrypt (handle
->hd
.gc
, ciphertext
,
130 ciphertextlen
, ciphertext
,
137 _gnutls_cipher_deinit (cipher_hd_st
* handle
)
141 if (handle
->registered
&& handle
->hd
.rh
.ctx
!= NULL
)
143 handle
->hd
.rh
.cc
->deinit (handle
->hd
.rh
.ctx
);
146 _gnutls_cipher_ops
.deinit (handle
->hd
.gc
);