Coda Distributed File System
rijndael-mikescott.h
Go to the documentation of this file.
1/* BLURB lgpl
2 Coda File System
3 Release 6
4
5 Copyright (c) 2006 Carnegie Mellon University
6 Additional copyrights listed below
7
8This code is distributed "AS IS" without warranty of any kind under
9the terms of the GNU Library General Public Licence Version 2, as
10shown in the file LICENSE. The technical and financial contributors to
11Coda are listed in the file CREDITS.
12
13 Additional copyrights
14#*/
15
16/* Wrapper around the rijndael (AES) implementation by Mike Scott */
17#ifndef _AES_H_
18#define _AES_H_
19
20#include <stdint.h>
21
22#define AES_MAXROUNDS MAXNR
23#define AES_BLOCK_SIZE 16
24
25typedef struct {
26 uint32_t context[120 * sizeof(uint32_t)];
28 uint32_t rounds;
30#define aes_encrypt_ctx aes_context
31#define aes_decrypt_ctx aes_context
32
33/* Define this to the function used to setup tables during initialization */
34#define AES_INIT_FUNC gentables()
35
36/* arghh. this code uses globals... */
37extern int Nb, Nr, Nk;
38extern unsigned int fkey[120], rkey[120];
39
40static inline int aes_encrypt_key(const uint8_t *key, int keylen,
41 aes_encrypt_ctx *ctx)
42{
43 ctx->rounds = (keylen == 128) ? 10 : (keylen == 192) ? 12 : 14;
44 gkey(AES_BLOCK_SIZE / sizeof(uint32_t), keylen / 8 / sizeof(uint32_t), key);
45
46 ctx->Nk = Nk;
47 ctx->Nb = Nb;
48 ctx->Nr = Nr;
49 memcpy(ctx->context, fkey, 120 * sizeof(uint32_t));
50 return 0;
51}
52
53static inline int aes_decrypt_key(const uint8_t *key, int keylen,
54 aes_decrypt_ctx *ctx)
55{
56 ctx->rounds = (keylen == 128) ? 10 : (keylen == 192) ? 12 : 14;
57 gkey(AES_BLOCK_SIZE / sizeof(uint32_t), keylen / 8 / sizeof(uint32_t), key);
58
59 ctx->Nk = Nk;
60 ctx->Nb = Nb;
61 ctx->Nr = Nr;
62 memcpy(ctx->context, rkey, 120 * sizeof(uint32_t));
63 return 0;
64}
65
66static inline int aes_encrypt(const uint8_t in[AES_BLOCK_SIZE],
68 const aes_encrypt_ctx *ctx)
69{
70 Nk = ctx->Nk;
71 Nb = ctx->Nb;
72 Nr = ctx->Nr;
73 memcpy(fkey, ctx->context, 120 * sizeof(uint32_t));
74
75 /* and I guess it also only supports in-place encryption/decryption */
76 if (out != in)
77 memcpy(out, in, AES_BLOCK_SIZE);
78 encrypt(out);
79 return 0;
80}
81
82static inline int aes_decrypt(const uint8_t in[AES_BLOCK_SIZE],
84 const aes_decrypt_ctx *ctx)
85{
86 Nk = ctx->Nk;
87 Nb = ctx->Nb;
88 Nr = ctx->Nr;
89 memcpy(rkey, ctx->context, 120 * sizeof(uint32_t));
90
91 if (out != in)
92 memcpy(out, in, AES_BLOCK_SIZE);
93 decrypt(out);
94 return 0;
95}
96
97#endif /* _AES_H_ */
int context
unsigned int uint32_t
Definition: coda.h:105
unsigned char uint8_t
Definition: coda.h:101
void encrypt(char *buff)
Definition: rijndael-mikescott.c:267
void decrypt(char *buff)
Definition: rijndael-mikescott.c:314
void gkey(int nb, int nk, char *key)
Definition: rijndael-mikescott.c:195
#define aes_decrypt_ctx
Definition: rijndael-mikescott.h:31
#define aes_encrypt_ctx
Definition: rijndael-mikescott.h:30
unsigned int fkey[120]
Definition: rijndael-mikescott.c:69
int Nk
Definition: rijndael-mikescott.h:37
int Nr
Definition: rijndael-mikescott.h:37
unsigned int rkey[120]
Definition: rijndael-mikescott.h:38
#define AES_BLOCK_SIZE
Definition: rijndael-mikescott.h:23
int Nb
Definition: rijndael-mikescott.c:67
Definition: aes.h:34
uint8_t Nb
Definition: rijndael-mikescott.h:27