Add `gnutls/dtls.h' to the distribution.
[gnutls.git] / lib / gcrypt / cipher.c
blob965d8db093da210b7223319132e6a1ab824d7fc1
1 /*
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,
21 * USA
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>
32 #include <gcrypt.h>
34 /* Functions that refer to the libgcrypt library.
37 static int
38 wrap_gcry_cipher_init (gnutls_cipher_algorithm_t algo, void **ctx)
40 int err;
42 switch (algo)
44 case GNUTLS_CIPHER_AES_128_CBC:
45 err =
46 gcry_cipher_open ((gcry_cipher_hd_t *) ctx, GCRY_CIPHER_AES128,
47 GCRY_CIPHER_MODE_CBC, 0);
48 break;
50 case GNUTLS_CIPHER_AES_192_CBC:
51 err =
52 gcry_cipher_open ((gcry_cipher_hd_t *) ctx, GCRY_CIPHER_AES192,
53 GCRY_CIPHER_MODE_CBC, 0);
54 break;
56 case GNUTLS_CIPHER_AES_256_CBC:
57 err =
58 gcry_cipher_open ((gcry_cipher_hd_t *) ctx, GCRY_CIPHER_AES256,
59 GCRY_CIPHER_MODE_CBC, 0);
60 break;
62 case GNUTLS_CIPHER_3DES_CBC:
63 err =
64 gcry_cipher_open ((gcry_cipher_hd_t *) ctx, GCRY_CIPHER_3DES,
65 GCRY_CIPHER_MODE_CBC, 0);
66 break;
68 case GNUTLS_CIPHER_DES_CBC:
69 err =
70 gcry_cipher_open ((gcry_cipher_hd_t *) ctx, GCRY_CIPHER_DES,
71 GCRY_CIPHER_MODE_CBC, 0);
72 break;
74 case GNUTLS_CIPHER_ARCFOUR_128:
75 case GNUTLS_CIPHER_ARCFOUR_40:
76 err =
77 gcry_cipher_open ((gcry_cipher_hd_t *) ctx, GCRY_CIPHER_ARCFOUR,
78 GCRY_CIPHER_MODE_STREAM, 0);
79 break;
81 case GNUTLS_CIPHER_RC2_40_CBC:
82 err =
83 gcry_cipher_open ((gcry_cipher_hd_t *) ctx, GCRY_CIPHER_RFC2268_40,
84 GCRY_CIPHER_MODE_CBC, 0);
85 break;
87 #ifdef ENABLE_CAMELLIA
88 case GNUTLS_CIPHER_CAMELLIA_128_CBC:
89 err =
90 gcry_cipher_open ((gcry_cipher_hd_t *) ctx, GCRY_CIPHER_CAMELLIA128,
91 GCRY_CIPHER_MODE_CBC, 0);
92 break;
94 case GNUTLS_CIPHER_CAMELLIA_256_CBC:
95 err =
96 gcry_cipher_open ((gcry_cipher_hd_t *) ctx, GCRY_CIPHER_CAMELLIA256,
97 GCRY_CIPHER_MODE_CBC, 0);
98 break;
99 #endif
100 default:
101 gnutls_assert ();
102 return GNUTLS_E_INVALID_REQUEST;
105 if (err == 0)
106 return 0;
108 gnutls_assert ();
109 return GNUTLS_E_ENCRYPTION_FAILED;
112 static int
113 wrap_gcry_cipher_setkey (void *ctx, const void *key, size_t keysize)
115 gcry_cipher_setkey (ctx, key, keysize);
116 return 0;
119 static int
120 wrap_gcry_cipher_setiv (void *ctx, const void *iv, size_t ivsize)
122 gcry_cipher_setiv (ctx, iv, ivsize);
123 return 0;
126 static int
127 wrap_gcry_cipher_decrypt (void *ctx, const void *encr, size_t encrsize,
128 void *plain, size_t plainsize)
130 int err;
132 err = gcry_cipher_decrypt (ctx, plain, plainsize, encr, encrsize);
133 if (err == 0)
134 return 0;
136 gnutls_assert ();
137 return GNUTLS_E_ENCRYPTION_FAILED;
140 static int
141 wrap_gcry_cipher_encrypt (void *ctx, const void *plain, size_t plainsize,
142 void *encr, size_t encrsize)
144 int err;
146 err = gcry_cipher_encrypt (ctx, encr, encrsize, plain, plainsize);
147 if (err == 0)
148 return 0;
150 gnutls_assert ();
151 return GNUTLS_E_ENCRYPTION_FAILED;
154 static void
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,