2 * Copyright (C) 2000 Nikos Mavroyanopoulos
4 * This file is part of GNUTLS.
6 * The GNUTLS library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <gnutls_int.h>
23 #include <gnutls_errors.h>
24 #include <gnutls_cipher_int.h>
25 #include <gnutls_datum.h>
27 GNUTLS_CIPHER_HANDLE
_gnutls_cipher_init( BulkCipherAlgorithm cipher
, gnutls_datum key
, gnutls_datum iv
)
29 GNUTLS_CIPHER_HANDLE ret
;
32 case GNUTLS_CIPHER_NULL
:
33 ret
= GNUTLS_CIPHER_FAILED
;
35 case GNUTLS_CIPHER_RIJNDAEL_128_CBC
:
37 ret
= mcrypt_module_open( "rijndael-128", NULL
, "cbc", NULL
);
39 ret
= gcry_cipher_open(GCRY_CIPHER_RIJNDAEL
, GCRY_CIPHER_MODE_CBC
, 0);
42 case GNUTLS_CIPHER_RIJNDAEL_256_CBC
:
44 ret
= mcrypt_module_open( "rijndael-128", NULL
, "cbc", NULL
);
46 ret
= gcry_cipher_open(GCRY_CIPHER_RIJNDAEL256
, GCRY_CIPHER_MODE_CBC
, 0);
49 case GNUTLS_CIPHER_TWOFISH_128_CBC
:
51 ret
= mcrypt_module_open( "twofish", NULL
, "cbc", NULL
);
53 ret
= gcry_cipher_open(GCRY_CIPHER_TWOFISH
, GCRY_CIPHER_MODE_CBC
, 0);
56 case GNUTLS_CIPHER_3DES_CBC
:
58 ret
= mcrypt_module_open( "tripledes", NULL
, "cbc", NULL
);
60 ret
= gcry_cipher_open(GCRY_CIPHER_3DES
, GCRY_CIPHER_MODE_CBC
, 0);
63 case GNUTLS_CIPHER_ARCFOUR
:
65 ret
= mcrypt_module_open( "arcfour", NULL
, "stream", NULL
);
67 ret
= gcry_cipher_open(GCRY_CIPHER_ARCFOUR
, GCRY_CIPHER_MODE_STREAM
, 0);
71 ret
= GNUTLS_CIPHER_FAILED
;
73 if (ret
!=GNUTLS_CIPHER_FAILED
) {
75 /* ivsize is assumed to be blocksize */
76 if ( mcrypt_generic_init( ret
, key
.data
, key
.size
, iv
.data
) < 0) {
77 return GNUTLS_CIPHER_FAILED
;
80 gcry_cipher_setkey(ret
, key
.data
, key
.size
);
81 if (iv
.data
!=NULL
&& iv
.size
>0) gcry_cipher_setiv(ret
, iv
.data
, iv
.size
);
88 int _gnutls_cipher_encrypt(GNUTLS_CIPHER_HANDLE handle
, void* text
, int textlen
) {
89 if (handle
!=GNUTLS_CIPHER_FAILED
) {
91 mcrypt_generic( handle
, text
, textlen
);
93 if (gcry_cipher_encrypt( handle
, text
, textlen
, NULL
, textlen
)!=0) {
95 return GNUTLS_E_UNKNOWN_ERROR
;
102 int _gnutls_cipher_decrypt(GNUTLS_CIPHER_HANDLE handle
, void* ciphertext
, int ciphertextlen
) {
103 if (handle
!=GNUTLS_CIPHER_FAILED
) {
105 mdecrypt_generic( handle
, ciphertext
, ciphertextlen
);
107 if (gcry_cipher_decrypt( handle
, ciphertext
, ciphertextlen
, NULL
, ciphertextlen
)!=0) {
109 return GNUTLS_E_UNKNOWN_ERROR
;
116 void _gnutls_cipher_deinit(GNUTLS_CIPHER_HANDLE handle
) {
117 if (handle
!=GNUTLS_CIPHER_FAILED
) {
119 mcrypt_generic_end( handle
);
121 gcry_cipher_close(handle
);