2 * Copyright (C) 2008, 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 /* Here lie everything that has to do with large numbers, libgcrypt and
26 * other stuff that didn't fit anywhere else.
29 #include <gnutls_int.h>
30 #include <gnutls_errors.h>
31 #include <gnutls_cipher_int.h>
34 /* Functions that refer to the libgcrypt library.
38 wrap_gcry_cipher_init (gnutls_cipher_algorithm_t algo
, void **ctx
)
44 case GNUTLS_CIPHER_AES_128_CBC
:
46 gcry_cipher_open ((gcry_cipher_hd_t
*) ctx
, GCRY_CIPHER_AES128
,
47 GCRY_CIPHER_MODE_CBC
, 0);
50 case GNUTLS_CIPHER_AES_192_CBC
:
52 gcry_cipher_open ((gcry_cipher_hd_t
*) ctx
, GCRY_CIPHER_AES192
,
53 GCRY_CIPHER_MODE_CBC
, 0);
56 case GNUTLS_CIPHER_AES_256_CBC
:
58 gcry_cipher_open ((gcry_cipher_hd_t
*) ctx
, GCRY_CIPHER_AES256
,
59 GCRY_CIPHER_MODE_CBC
, 0);
62 case GNUTLS_CIPHER_3DES_CBC
:
64 gcry_cipher_open ((gcry_cipher_hd_t
*) ctx
, GCRY_CIPHER_3DES
,
65 GCRY_CIPHER_MODE_CBC
, 0);
68 case GNUTLS_CIPHER_DES_CBC
:
70 gcry_cipher_open ((gcry_cipher_hd_t
*) ctx
, GCRY_CIPHER_DES
,
71 GCRY_CIPHER_MODE_CBC
, 0);
74 case GNUTLS_CIPHER_ARCFOUR_128
:
75 case GNUTLS_CIPHER_ARCFOUR_40
:
77 gcry_cipher_open ((gcry_cipher_hd_t
*) ctx
, GCRY_CIPHER_ARCFOUR
,
78 GCRY_CIPHER_MODE_STREAM
, 0);
81 case GNUTLS_CIPHER_RC2_40_CBC
:
83 gcry_cipher_open ((gcry_cipher_hd_t
*) ctx
, GCRY_CIPHER_RFC2268_40
,
84 GCRY_CIPHER_MODE_CBC
, 0);
87 #ifdef ENABLE_CAMELLIA
88 case GNUTLS_CIPHER_CAMELLIA_128_CBC
:
90 gcry_cipher_open ((gcry_cipher_hd_t
*) ctx
, GCRY_CIPHER_CAMELLIA128
,
91 GCRY_CIPHER_MODE_CBC
, 0);
94 case GNUTLS_CIPHER_CAMELLIA_256_CBC
:
96 gcry_cipher_open ((gcry_cipher_hd_t
*) ctx
, GCRY_CIPHER_CAMELLIA256
,
97 GCRY_CIPHER_MODE_CBC
, 0);
102 return GNUTLS_E_INVALID_REQUEST
;
109 return GNUTLS_E_ENCRYPTION_FAILED
;
113 wrap_gcry_cipher_setkey (void *ctx
, const void *key
, size_t keysize
)
115 gcry_cipher_setkey (ctx
, key
, keysize
);
120 wrap_gcry_cipher_setiv (void *ctx
, const void *iv
, size_t ivsize
)
122 gcry_cipher_setiv (ctx
, iv
, ivsize
);
127 wrap_gcry_cipher_decrypt (void *ctx
, const void *encr
, size_t encrsize
,
128 void *plain
, size_t plainsize
)
132 err
= gcry_cipher_decrypt (ctx
, plain
, plainsize
, encr
, encrsize
);
137 return GNUTLS_E_ENCRYPTION_FAILED
;
141 wrap_gcry_cipher_encrypt (void *ctx
, const void *plain
, size_t plainsize
,
142 void *encr
, size_t encrsize
)
146 err
= gcry_cipher_encrypt (ctx
, encr
, encrsize
, plain
, plainsize
);
151 return GNUTLS_E_ENCRYPTION_FAILED
;
155 wrap_gcry_cipher_close (void *h
)
157 gcry_cipher_close (h
);
161 gnutls_crypto_cipher_st _gnutls_cipher_ops
= {
162 .init
= wrap_gcry_cipher_init
,
163 .setkey
= wrap_gcry_cipher_setkey
,
164 .setiv
= wrap_gcry_cipher_setiv
,
165 .encrypt
= wrap_gcry_cipher_encrypt
,
166 .decrypt
= wrap_gcry_cipher_decrypt
,
167 .deinit
= wrap_gcry_cipher_close
,