rijndael.c 2.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* rijndael.c
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1999, 2000 Niels Mller, Rafael R. Sevilla
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "crypto.h"

#include "werror.h"
#include "xalloc.h"
Niels Möller's avatar
Niels Möller committed
27

28
29
#include "nettle/aes.h"

30
#include <assert.h>
Niels Möller's avatar
Niels Möller committed
31

32
33
34
35
36
37
38
39
40
#include "rijndael.c.x"

/* Rijndael */

/* GABA:
   (class
     (name rijndael_instance)
     (super crypto_instance)
     (vars
41
       (ctx . "struct aes_ctx")))
42
43
*/

Niels Möller's avatar
Niels Möller committed
44
45
46
static void
do_rijndael_encrypt(struct crypto_instance *s,
		    UINT32 length, const UINT8 *src, UINT8 *dst)
47
48
49
{
  CAST(rijndael_instance, self, s);

50
  aes_encrypt(&self->ctx, length, dst, src);
51
52
}

Niels Möller's avatar
Niels Möller committed
53
54
55
static void
do_rijndael_decrypt(struct crypto_instance *s,
		    UINT32 length, const UINT8 *src, UINT8 *dst)
56
57
58
{
  CAST(rijndael_instance, self, s);

59
  aes_decrypt(&self->ctx, length, dst, src);
60
61
62
63
64
65
66
67
}

static struct crypto_instance *
make_rijndael_instance(struct crypto_algorithm *algorithm, int mode,
		       const UINT8 *key, const UINT8 *iv UNUSED)
{
  NEW(rijndael_instance, self);

68
  self->super.block_size = AES_BLOCK_SIZE;
69
70
71
72
  self->super.crypt = ( (mode == CRYPTO_ENCRYPT)
			? do_rijndael_encrypt
			: do_rijndael_decrypt);

73
  aes_set_key(&self->ctx, algorithm->key_size, key);
74
75
76
77
78
79
80
81
82

  return(&self->super);
}

struct crypto_algorithm *
make_rijndael_algorithm(UINT32 key_size)
{
  NEW(crypto_algorithm, algorithm);

83
84
  assert(key_size <= AES_MAX_KEY_SIZE);
  assert(key_size >= AES_MIN_KEY_SIZE);
85

86
  algorithm->block_size = AES_BLOCK_SIZE;
87
88
89
90
91
92
93
  algorithm->key_size = key_size;
  algorithm->iv_size = 0;
  algorithm->make_crypt = make_rijndael_instance;

  return algorithm;
}

94
struct crypto_algorithm rijndael128_algorithm =
95
{ STATIC_HEADER, AES_BLOCK_SIZE, 16, 0, make_rijndael_instance};
96
97

struct crypto_algorithm rijndael192_algorithm =
98
{ STATIC_HEADER, AES_BLOCK_SIZE, 24, 0, make_rijndael_instance};
99
100

struct crypto_algorithm rijndael256_algorithm =
101
{ STATIC_HEADER, AES_BLOCK_SIZE, 32, 0, make_rijndael_instance};