added memory handling section
[gnutls.git] / lib / gnutls_cipher_int.c
blobbbbfefd93f41a0bc869fc19c9db824040a432e2a
1 /*
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;
31 switch (cipher) {
32 case GNUTLS_CIPHER_NULL:
33 ret = GNUTLS_CIPHER_FAILED;
34 break;
35 case GNUTLS_CIPHER_RIJNDAEL_128_CBC:
36 #ifdef USE_MCRYPT
37 ret = mcrypt_module_open( "rijndael-128", NULL, "cbc", NULL);
38 #else
39 ret = gcry_cipher_open(GCRY_CIPHER_RIJNDAEL, GCRY_CIPHER_MODE_CBC, 0);
40 #endif
41 break;
42 case GNUTLS_CIPHER_RIJNDAEL_256_CBC:
43 #ifdef USE_MCRYPT
44 ret = mcrypt_module_open( "rijndael-128", NULL, "cbc", NULL);
45 #else
46 ret = gcry_cipher_open(GCRY_CIPHER_RIJNDAEL256, GCRY_CIPHER_MODE_CBC, 0);
47 #endif
48 break;
49 case GNUTLS_CIPHER_TWOFISH_128_CBC:
50 #ifdef USE_MCRYPT
51 ret = mcrypt_module_open( "twofish", NULL, "cbc", NULL);
52 #else
53 ret = gcry_cipher_open(GCRY_CIPHER_TWOFISH, GCRY_CIPHER_MODE_CBC, 0);
54 #endif
55 break;
56 case GNUTLS_CIPHER_3DES_CBC:
57 #ifdef USE_MCRYPT
58 ret = mcrypt_module_open( "tripledes", NULL, "cbc", NULL);
59 #else
60 ret = gcry_cipher_open(GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_CBC, 0);
61 #endif
62 break;
63 case GNUTLS_CIPHER_ARCFOUR:
64 #ifdef USE_MCRYPT
65 ret = mcrypt_module_open( "arcfour", NULL, "stream", NULL);
66 #else
67 ret = gcry_cipher_open(GCRY_CIPHER_ARCFOUR, GCRY_CIPHER_MODE_STREAM, 0);
68 #endif
69 break;
70 default:
71 ret = GNUTLS_CIPHER_FAILED;
73 if (ret!=GNUTLS_CIPHER_FAILED) {
74 #ifdef USE_MCRYPT
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;
79 #else
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);
82 #endif
85 return ret;
88 int _gnutls_cipher_encrypt(GNUTLS_CIPHER_HANDLE handle, void* text, int textlen) {
89 if (handle!=GNUTLS_CIPHER_FAILED) {
90 #ifdef USE_MCRYPT
91 mcrypt_generic( handle, text, textlen);
92 #else
93 if (gcry_cipher_encrypt( handle, text, textlen, NULL, textlen)!=0) {
94 gnutls_assert();
95 return GNUTLS_E_UNKNOWN_ERROR;
97 #endif
99 return 0;
102 int _gnutls_cipher_decrypt(GNUTLS_CIPHER_HANDLE handle, void* ciphertext, int ciphertextlen) {
103 if (handle!=GNUTLS_CIPHER_FAILED) {
104 #ifdef USE_MCRYPT
105 mdecrypt_generic( handle, ciphertext, ciphertextlen);
106 #else
107 if (gcry_cipher_decrypt( handle, ciphertext, ciphertextlen, NULL, ciphertextlen)!=0) {
108 gnutls_assert();
109 return GNUTLS_E_UNKNOWN_ERROR;
111 #endif
113 return 0;
116 void _gnutls_cipher_deinit(GNUTLS_CIPHER_HANDLE handle) {
117 if (handle!=GNUTLS_CIPHER_FAILED) {
118 #ifdef USE_MCRYPT
119 mcrypt_generic_end( handle);
120 #else
121 gcry_cipher_close(handle);
122 #endif