summaryrefslogtreecommitdiffstats
path: root/rijndael.h
blob: a1694d25e1fc5613802b57391c09ac6719c18570 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Highly portable ANSI C implementation of Rijndael cipher.
 * Written by Kaz Kylheku.
 */

#ifndef RIJNDAEL_H
#define RIJNDAEL_H

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

#define RIJN_MAX_ROUNDS 14

/*
 * Constants representing choice of Rijndael key or block size,
 * either of which can be 128 bits, 192 bits or 256 bits.
 */

typedef enum {
	rijn_128 = 4, 	/* number of rows */
	rijn_192 = 6,
	rijn_256 = 8
} rijn_size_t;

/*
 * Rijndael parameters.
 */

typedef struct {
	rijn_size_t rijn_blockrows;
	rijn_size_t rijn_keyrows;
} rijn_param_t;

#define RIJN_PARAM_DEFAULT_INITIALIZER { rijn_128, rijn_256 }
#define RIJN_PARAM_INITIALIZER(BLOCKSZ, KEYSZ) { (BLOCKSZ), (KEYSZ) }

void rijn_param_init(rijn_param_t *, rijn_size_t blocksz, rijn_size_t keysz);

/*
 * Rijndael keys are 128, 192 or 256 bits wide.  There are up to
 * RIJN_MAX_ROUNDS + 1 round keys needed, so the schedule is that big.
 */

typedef unsigned char rijn_unit_t[4];
typedef rijn_unit_t rijn_key_t[8];
typedef rijn_unit_t rijn_block_t[8];
typedef unsigned char rijn_flatblock_t[sizeof (rijn_block_t)];

typedef struct {
	rijn_param_t rijn_param;
	int rijn_nrounds;
	rijn_block_t rijn_roundkey[RIJN_MAX_ROUNDS];
} rijn_keysched_t;

void rijn_sched_key(rijn_keysched_t *, rijn_key_t *, const rijn_param_t *);
void rijn_encrypt(rijn_keysched_t *, unsigned char *, const unsigned char *);
void rijn_decrypt(rijn_keysched_t *, unsigned char *, const unsigned char *);
void rijn_cbc_encrypt(rijn_keysched_t *, unsigned char *iv, unsigned char *out, 
		const unsigned char *in, size_t nblocks);
void rijn_cbc_decrypt(rijn_keysched_t *, unsigned char *iv, unsigned char *out,
		const unsigned char *in, size_t nblocks);
void rijn_cfb_encrypt(rijn_keysched_t *, unsigned char *iv, unsigned char *out,
		const unsigned char *pt, size_t nbytes);
void rijn_cfb_decrypt(rijn_keysched_t *, unsigned char *iv, unsigned char *out,
		const unsigned char *in, size_t nbytes);

#ifdef __cplusplus
}
#endif

#endif