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,
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 const gnutls_crypto_cipher_st
*cc
= NULL
;
45 /* check if a cipher has been registered
47 cc
= _gnutls_get_crypto_cipher (cipher
);
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
);
62 /* otherwise use generic cipher interface
64 ret
= _gnutls_cipher_ops
.init (cipher
, &handle
->handle
);
71 ret
= _gnutls_cipher_ops
.setkey (handle
->handle
, key
->data
, key
->size
);
74 _gnutls_cipher_ops
.deinit (handle
->handle
);
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
);
91 cc
->deinit (handle
->handle
);
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
);
107 _gnutls_cipher_decrypt (const cipher_hd_st
* handle
, void *ciphertext
,
110 if (handle
!= NULL
&& handle
->handle
!= NULL
)
112 return handle
->decrypt (handle
->handle
, ciphertext
, ciphertextlen
,
113 ciphertext
, ciphertextlen
);
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
,
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
,
143 _gnutls_cipher_deinit (cipher_hd_st
* handle
)
145 if (handle
!= NULL
&& handle
->handle
!= NULL
)
147 handle
->deinit (handle
->handle
);
148 handle
->handle
= NULL
;