~nutznboltz-deactivatedaccount/ubuntu/precise/gnutls26/fix-lp926350

« back to all changes in this revision

Viewing changes to lib/nettle/cipher.c

  • Committer: Package Import Robot
  • Author(s): Andreas Metzler
  • Date: 2011-10-01 15:28:13 UTC
  • mfrom: (12.1.20 sid)
  • Revision ID: package-import@ubuntu.com-20111001152813-yygm1c4cxonfxhzy
Tags: 2.12.11-1
* New upstream version.
  + Allow CA importing of 0 certificates to succeed. Closes: #640639
* Add libp11-kit-dev to libgnutls-dev dependencies. (see #643811)
* [20_guiledocstring.diff] guile: Fix docstring extraction with CPP 4.5+.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Free Software Foundation, Inc.
 
3
 *
 
4
 * Author: Nikos Mavrogiannopoulos
 
5
 *
 
6
 * This file is part of GNUTLS.
 
7
 *
 
8
 * The GNUTLS library 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.
 
12
 *
 
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.
 
17
 *
 
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
 
22
 *
 
23
 */
 
24
 
 
25
/* Here lie nettle's wrappers for cipher support.
 
26
 */
 
27
 
 
28
#include <gnutls_int.h>
 
29
#include <gnutls_errors.h>
 
30
#include <gnutls_cipher_int.h>
 
31
#include <nettle/aes.h>
 
32
#include <nettle/camellia.h>
 
33
#include <nettle/arcfour.h>
 
34
#include <nettle/arctwo.h>
 
35
#include <nettle/des.h>
 
36
#include <nettle/nettle-meta.h>
 
37
#include <nettle/cbc.h>
 
38
 
 
39
/* Functions that refer to the libgcrypt library.
 
40
 */
 
41
 
 
42
#define MAX_BLOCK_SIZE 32
 
43
 
 
44
typedef void (*encrypt_func) (void *, nettle_crypt_func, unsigned, uint8_t *,
 
45
                              unsigned, uint8_t *, const uint8_t *);
 
46
typedef void (*decrypt_func) (void *, nettle_crypt_func, unsigned, uint8_t *,
 
47
                              unsigned, uint8_t *, const uint8_t *);
 
48
typedef void (*setkey_func) (void *, unsigned, const uint8_t *);
 
49
 
 
50
static void
 
51
stream_encrypt (void *ctx, nettle_crypt_func func, unsigned block_size,
 
52
                uint8_t * iv, unsigned length, uint8_t * dst,
 
53
                const uint8_t * src)
 
54
{
 
55
  func (ctx, length, dst, src);
 
56
}
 
57
 
 
58
struct aes_bidi_ctx
 
59
{
 
60
  struct aes_ctx encrypt;
 
61
  struct aes_ctx decrypt;
 
62
};
 
63
 
 
64
static void
 
65
aes_bidi_setkey (struct aes_bidi_ctx *ctx, unsigned length,
 
66
                 const uint8_t * key)
 
67
{
 
68
  aes_set_encrypt_key (&ctx->encrypt, length, key);
 
69
  aes_invert_key (&ctx->decrypt, &ctx->encrypt);
 
70
}
 
71
 
 
72
static void
 
73
aes_bidi_encrypt (struct aes_bidi_ctx *ctx,
 
74
                  unsigned length, uint8_t * dst, const uint8_t * src)
 
75
{
 
76
  aes_encrypt (&ctx->encrypt, length, dst, src);
 
77
}
 
78
 
 
79
static void
 
80
aes_bidi_decrypt (struct aes_bidi_ctx *ctx,
 
81
                  unsigned length, uint8_t * dst, const uint8_t * src)
 
82
{
 
83
  aes_decrypt (&ctx->decrypt, length, dst, src);
 
84
}
 
85
 
 
86
struct camellia_bidi_ctx
 
87
{
 
88
  struct camellia_ctx encrypt;
 
89
  struct camellia_ctx decrypt;
 
90
};
 
91
 
 
92
static void
 
93
camellia_bidi_setkey (struct camellia_bidi_ctx *ctx, unsigned length,
 
94
                      const uint8_t * key)
 
95
{
 
96
  camellia_set_encrypt_key (&ctx->encrypt, length, key);
 
97
  camellia_invert_key (&ctx->decrypt, &ctx->encrypt);
 
98
}
 
99
 
 
100
static void
 
101
camellia_bidi_encrypt (struct camellia_bidi_ctx *ctx,
 
102
                       unsigned length, uint8_t * dst, const uint8_t * src)
 
103
{
 
104
  camellia_crypt (&ctx->encrypt, length, dst, src);
 
105
}
 
106
 
 
107
static void
 
108
camellia_bidi_decrypt (struct camellia_bidi_ctx *ctx,
 
109
                       unsigned length, uint8_t * dst, const uint8_t * src)
 
110
{
 
111
  camellia_crypt (&ctx->decrypt, length, dst, src);
 
112
}
 
113
 
 
114
struct nettle_cipher_ctx
 
115
{
 
116
  union
 
117
  {
 
118
    struct aes_bidi_ctx aes_bidi;
 
119
    struct camellia_bidi_ctx camellia_bidi;
 
120
    struct arcfour_ctx arcfour;
 
121
    struct arctwo_ctx arctwo;
 
122
    struct des3_ctx des3;
 
123
    struct des_ctx des;
 
124
  } ctx;
 
125
  void *ctx_ptr;
 
126
  uint8_t iv[MAX_BLOCK_SIZE];
 
127
  gnutls_cipher_algorithm_t algo;
 
128
  size_t block_size;
 
129
  nettle_crypt_func *i_encrypt;
 
130
  nettle_crypt_func *i_decrypt;
 
131
  encrypt_func encrypt;
 
132
  decrypt_func decrypt;
 
133
};
 
134
 
 
135
 
 
136
 
 
137
static int
 
138
wrap_nettle_cipher_init (gnutls_cipher_algorithm_t algo, void **_ctx)
 
139
{
 
140
  struct nettle_cipher_ctx *ctx;
 
141
 
 
142
  ctx = gnutls_calloc (1, sizeof (*ctx));
 
143
  if (ctx == NULL)
 
144
    {
 
145
      gnutls_assert ();
 
146
      return GNUTLS_E_MEMORY_ERROR;
 
147
    }
 
148
 
 
149
  ctx->algo = algo;
 
150
 
 
151
  switch (algo)
 
152
    {
 
153
    case GNUTLS_CIPHER_CAMELLIA_128_CBC:
 
154
    case GNUTLS_CIPHER_CAMELLIA_256_CBC:
 
155
      ctx->encrypt = cbc_encrypt;
 
156
      ctx->decrypt = cbc_decrypt;
 
157
      ctx->i_encrypt = (nettle_crypt_func *) camellia_bidi_encrypt;
 
158
      ctx->i_decrypt = (nettle_crypt_func *) camellia_bidi_decrypt;
 
159
      ctx->ctx_ptr = &ctx->ctx.camellia_bidi;
 
160
      ctx->block_size = CAMELLIA_BLOCK_SIZE;
 
161
      break;
 
162
    case GNUTLS_CIPHER_AES_128_CBC:
 
163
    case GNUTLS_CIPHER_AES_192_CBC:
 
164
    case GNUTLS_CIPHER_AES_256_CBC:
 
165
      ctx->encrypt = cbc_encrypt;
 
166
      ctx->decrypt = cbc_decrypt;
 
167
      ctx->i_encrypt = (nettle_crypt_func *) aes_bidi_encrypt;
 
168
      ctx->i_decrypt = (nettle_crypt_func *) aes_bidi_decrypt;
 
169
      ctx->ctx_ptr = &ctx->ctx.aes_bidi;
 
170
      ctx->block_size = AES_BLOCK_SIZE;
 
171
      break;
 
172
    case GNUTLS_CIPHER_3DES_CBC:
 
173
      ctx->encrypt = cbc_encrypt;
 
174
      ctx->decrypt = cbc_decrypt;
 
175
      ctx->i_encrypt = (nettle_crypt_func *) des3_encrypt;
 
176
      ctx->i_decrypt = (nettle_crypt_func *) des3_decrypt;
 
177
      ctx->ctx_ptr = &ctx->ctx.des3;
 
178
      ctx->block_size = DES3_BLOCK_SIZE;
 
179
      break;
 
180
    case GNUTLS_CIPHER_DES_CBC:
 
181
      ctx->encrypt = cbc_encrypt;
 
182
      ctx->decrypt = cbc_decrypt;
 
183
      ctx->i_encrypt = (nettle_crypt_func *) des_encrypt;
 
184
      ctx->i_decrypt = (nettle_crypt_func *) des_decrypt;
 
185
      ctx->ctx_ptr = &ctx->ctx.des;
 
186
      ctx->block_size = DES_BLOCK_SIZE;
 
187
      break;
 
188
    case GNUTLS_CIPHER_ARCFOUR_128:
 
189
    case GNUTLS_CIPHER_ARCFOUR_40:
 
190
      ctx->encrypt = stream_encrypt;
 
191
      ctx->decrypt = stream_encrypt;
 
192
      ctx->i_encrypt = (nettle_crypt_func *) arcfour_crypt;
 
193
      ctx->i_decrypt = (nettle_crypt_func *) arcfour_crypt;
 
194
      ctx->ctx_ptr = &ctx->ctx.arcfour;
 
195
      ctx->block_size = 1;
 
196
      break;
 
197
    case GNUTLS_CIPHER_RC2_40_CBC:
 
198
      ctx->encrypt = cbc_encrypt;
 
199
      ctx->decrypt = cbc_decrypt;
 
200
      ctx->i_encrypt = (nettle_crypt_func *) arctwo_encrypt;
 
201
      ctx->i_decrypt = (nettle_crypt_func *) arctwo_decrypt;
 
202
      ctx->ctx_ptr = &ctx->ctx.arctwo;
 
203
      ctx->block_size = ARCTWO_BLOCK_SIZE;
 
204
      break;
 
205
    default:
 
206
      gnutls_assert ();
 
207
      return GNUTLS_E_INVALID_REQUEST;
 
208
    }
 
209
 
 
210
  *_ctx = ctx;
 
211
 
 
212
  return 0;
 
213
}
 
214
 
 
215
static int
 
216
wrap_nettle_cipher_setkey (void *_ctx, const void *key, size_t keysize)
 
217
{
 
218
  struct nettle_cipher_ctx *ctx = _ctx;
 
219
  opaque des_key[DES3_KEY_SIZE];
 
220
 
 
221
  switch (ctx->algo)
 
222
    {
 
223
    case GNUTLS_CIPHER_AES_128_CBC:
 
224
    case GNUTLS_CIPHER_AES_192_CBC:
 
225
    case GNUTLS_CIPHER_AES_256_CBC:
 
226
      aes_bidi_setkey (ctx->ctx_ptr, keysize, key);
 
227
      break;
 
228
    case GNUTLS_CIPHER_CAMELLIA_128_CBC:
 
229
    case GNUTLS_CIPHER_CAMELLIA_256_CBC:
 
230
      camellia_bidi_setkey (ctx->ctx_ptr, keysize, key);
 
231
      break;
 
232
    case GNUTLS_CIPHER_3DES_CBC:
 
233
      if (keysize != DES3_KEY_SIZE)
 
234
        {
 
235
          gnutls_assert ();
 
236
          return GNUTLS_E_INTERNAL_ERROR;
 
237
        }
 
238
 
 
239
      des_fix_parity (keysize, des_key, key);
 
240
 
 
241
      /* this fails on weak keys */
 
242
      if (des3_set_key (ctx->ctx_ptr, des_key) != 1)
 
243
        {
 
244
          gnutls_assert ();
 
245
          return GNUTLS_E_INTERNAL_ERROR;
 
246
        }
 
247
      break;
 
248
    case GNUTLS_CIPHER_DES_CBC:
 
249
      if (keysize != DES_KEY_SIZE)
 
250
        {
 
251
          gnutls_assert ();
 
252
          return GNUTLS_E_INTERNAL_ERROR;
 
253
        }
 
254
 
 
255
      des_fix_parity (keysize, des_key, key);
 
256
 
 
257
      if (des_set_key (ctx->ctx_ptr, des_key) != 1)
 
258
        {
 
259
          gnutls_assert ();
 
260
          return GNUTLS_E_INTERNAL_ERROR;
 
261
        }
 
262
      break;
 
263
    case GNUTLS_CIPHER_ARCFOUR_128:
 
264
    case GNUTLS_CIPHER_ARCFOUR_40:
 
265
      arcfour_set_key (ctx->ctx_ptr, keysize, key);
 
266
      break;
 
267
    case GNUTLS_CIPHER_RC2_40_CBC:
 
268
      arctwo_set_key (ctx->ctx_ptr, keysize, key);
 
269
      break;
 
270
    default:
 
271
      gnutls_assert ();
 
272
      return GNUTLS_E_INVALID_REQUEST;
 
273
    }
 
274
 
 
275
  return 0;
 
276
}
 
277
 
 
278
static int
 
279
wrap_nettle_cipher_setiv (void *_ctx, const void *iv, size_t ivsize)
 
280
{
 
281
  struct nettle_cipher_ctx *ctx = _ctx;
 
282
 
 
283
  if (ivsize > ctx->block_size)
 
284
    {
 
285
      gnutls_assert ();
 
286
      return GNUTLS_E_INVALID_REQUEST;
 
287
    }
 
288
  memcpy (ctx->iv, iv, ivsize);
 
289
 
 
290
  return 0;
 
291
}
 
292
 
 
293
static int
 
294
wrap_nettle_cipher_decrypt (void *_ctx, const void *encr, size_t encrsize,
 
295
                            void *plain, size_t plainsize)
 
296
{
 
297
  struct nettle_cipher_ctx *ctx = _ctx;
 
298
 
 
299
  ctx->decrypt (ctx->ctx_ptr, ctx->i_decrypt, ctx->block_size, ctx->iv,
 
300
                encrsize, plain, encr);
 
301
 
 
302
  return 0;
 
303
}
 
304
 
 
305
static int
 
306
wrap_nettle_cipher_encrypt (void *_ctx, const void *plain, size_t plainsize,
 
307
                            void *encr, size_t encrsize)
 
308
{
 
309
  struct nettle_cipher_ctx *ctx = _ctx;
 
310
 
 
311
  ctx->encrypt (ctx->ctx_ptr, ctx->i_encrypt, ctx->block_size, ctx->iv,
 
312
                plainsize, encr, plain);
 
313
 
 
314
  return 0;
 
315
}
 
316
 
 
317
static void
 
318
wrap_nettle_cipher_close (void *h)
 
319
{
 
320
  gnutls_free (h);
 
321
}
 
322
 
 
323
gnutls_crypto_cipher_st _gnutls_cipher_ops = {
 
324
  .init = wrap_nettle_cipher_init,
 
325
  .setkey = wrap_nettle_cipher_setkey,
 
326
  .setiv = wrap_nettle_cipher_setiv,
 
327
  .encrypt = wrap_nettle_cipher_encrypt,
 
328
  .decrypt = wrap_nettle_cipher_decrypt,
 
329
  .deinit = wrap_nettle_cipher_close,
 
330
};