summaryrefslogtreecommitdiffstats
path: root/include/rijndael-api-fst.h
blob: e0a9834c2552a3c2cf6b598e8a7bb0bc9e060d90 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*	$NetBSD: rijndael-api-fst.h,v 1.8 2007/01/21 23:00:08 cbiere Exp $	*/

/**
 * rijndael-api-fst.h
 *
 * @version 2.9 (December 2000)
 *
 * Optimised ANSI C code for the Rijndael cipher (now AES)
 *
 * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
 * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
 * @author Paulo Barreto <paulo.barreto@terra.com.br>
 *
 * This code is hereby placed in the public domain.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Acknowledgements:
 *
 * We are deeply indebted to the following people for their bug reports,
 * fixes, and improvement suggestions to this implementation. Though we
 * tried to list all contributions, we apologise in advance for any
 * missing reference.
 *
 * Andrew Bales <Andrew.Bales@Honeywell.com>
 * Markus Friedl <markus.friedl@informatik.uni-erlangen.de>
 * John Skodon <skodonj@webquill.com>
 */

#ifndef __RIJNDAEL_API_FST_H
#define __RIJNDAEL_API_FST_H

/* Blocksize: 16 * 8 = 128; 128 * 1 = 128 */
#define     B_SIZ    16
#define     BNUM      1


#if defined(UINT32)
typedef unsigned char u8;
typedef UINT32 u32;
#else

typedef unsigned char u8;
#if defined(HAVE_INT_32)
typedef unsigned int u32;
#elif defined(HAVE_LONG_32)
typedef unsigned long u32;
#elif defined(HAVE_SHORT_32)
typedef unsigned short u32;
#else
#error "No 32 bit integer type found"
#endif

#endif

#include "rijndael-alg-fst.h"

/*  Generic Defines  */
#define     DIR_ENCRYPT           0 /*  Are we encrpyting?  */
#define     DIR_DECRYPT           1 /*  Are we decrpyting?  */
#define     MODE_ECB              1 /*  Are we ciphering in ECB mode?   */
#define     MODE_CBC              2 /*  Are we ciphering in CBC mode?   */
#define     MODE_CFB1             3 /*  Are we ciphering in 1-bit CFB mode? */
#ifndef     TRUE
#define     TRUE                  1
#endif
#ifndef     FALSE
#define     FALSE                 0
#endif
#define     BITSPERBLOCK        128 /* Default number of bits in a cipher block */

/*  Error Codes  */
#define     BAD_KEY_DIR          -1 /*  Key direction is invalid, e.g., unknown value */
#define     BAD_KEY_MAT          -2 /*  Key material not of correct length */
#define     BAD_KEY_INSTANCE     -3 /*  Key passed is not valid */
#define     BAD_CIPHER_MODE      -4 /*  Params struct passed to cipherInit invalid */
#define     BAD_CIPHER_STATE     -5 /*  Cipher in wrong state (e.g., not initialized) */
#define     BAD_BLOCK_LENGTH     -6
#define     BAD_CIPHER_INSTANCE  -7
#define     BAD_DATA             -8 /*  Data contents are invalid, e.g., invalid padding */
#define     BAD_OTHER            -9 /*  Unknown error */

/*  Algorithm-specific Defines  */
#define     RIJNDAEL_MAX_KEY_SIZE         64 /* # of ASCII char's needed to represent a key */
#define     RIJNDAEL_MAX_IV_SIZE          16 /* # bytes needed to represent an IV  */

#ifdef SH_ENCRYPT

/*  Typedefs  */

typedef unsigned char   BYTE;

/*  The structure for key information */
typedef struct {
  u32   rk[4*(RIJNDAEL_MAXNR + 1)];        /* key schedule */
  u32   ek[4*(RIJNDAEL_MAXNR + 1)];        /* CFB1 key schedule (encryption only) */
  BYTE  direction;                /* Key used for encrypting or decrypting? */
  int   keyLen;                   /* Length of the key  */
  char  keyMaterial[RIJNDAEL_MAX_KEY_SIZE+1];  /* Raw key data in ASCII, e.g., user input or KAT values */
  int   Nr;                       /* key-length-dependent number of rounds */
} keyInstance;

/*  The structure for cipher information */
typedef struct {                    /* changed order of the components */
    u32  IV[RIJNDAEL_MAX_IV_SIZE / sizeof(u32)];
			/* A possible Initialization Vector for ciphering */
    BYTE  mode;                     /* MODE_ECB, MODE_CBC, or MODE_CFB1 */
} cipherInstance;

/*  Function prototypes  */

int rijndael_makeKey(keyInstance *, BYTE, int, const char *);

int rijndael_cipherInit(cipherInstance *, BYTE, const char *);

int rijndael_blockEncrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *);

int rijndael_padEncrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *);

int rijndael_blockDecrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *);

int rijndael_padDecrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *);

/* SH_ENCRYPT */
#endif
#endif /* __RIJNDAEL_API_FST_H */