| [481] | 1 | /*      $NetBSD: rijndael-api-fst.h,v 1.8 2007/01/21 23:00:08 cbiere Exp $      */ | 
|---|
|  | 2 |  | 
|---|
|  | 3 | /** | 
|---|
|  | 4 | * rijndael-api-fst.h | 
|---|
| [1] | 5 | * | 
|---|
| [481] | 6 | * @version 2.9 (December 2000) | 
|---|
| [1] | 7 | * | 
|---|
| [481] | 8 | * Optimised ANSI C code for the Rijndael cipher (now AES) | 
|---|
|  | 9 | * | 
|---|
|  | 10 | * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be> | 
|---|
|  | 11 | * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be> | 
|---|
|  | 12 | * @author Paulo Barreto <paulo.barreto@terra.com.br> | 
|---|
|  | 13 | * | 
|---|
|  | 14 | * This code is hereby placed in the public domain. | 
|---|
|  | 15 | * | 
|---|
|  | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS | 
|---|
|  | 17 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 
|---|
|  | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
|  | 19 | * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE | 
|---|
|  | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 
|---|
|  | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 
|---|
|  | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | 
|---|
|  | 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 
|---|
|  | 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | 
|---|
|  | 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | 
|---|
|  | 26 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
|  | 27 | * | 
|---|
|  | 28 | * Acknowledgements: | 
|---|
|  | 29 | * | 
|---|
|  | 30 | * We are deeply indebted to the following people for their bug reports, | 
|---|
|  | 31 | * fixes, and improvement suggestions to this implementation. Though we | 
|---|
|  | 32 | * tried to list all contributions, we apologise in advance for any | 
|---|
|  | 33 | * missing reference. | 
|---|
|  | 34 | * | 
|---|
|  | 35 | * Andrew Bales <Andrew.Bales@Honeywell.com> | 
|---|
|  | 36 | * Markus Friedl <markus.friedl@informatik.uni-erlangen.de> | 
|---|
|  | 37 | * John Skodon <skodonj@webquill.com> | 
|---|
| [1] | 38 | */ | 
|---|
|  | 39 |  | 
|---|
|  | 40 | #ifndef __RIJNDAEL_API_FST_H | 
|---|
|  | 41 | #define __RIJNDAEL_API_FST_H | 
|---|
|  | 42 |  | 
|---|
| [481] | 43 | /* Blocksize: 16 * 8 = 128; 128 * 1 = 128 */ | 
|---|
|  | 44 | #define     B_SIZ    16 | 
|---|
|  | 45 | #define     BNUM      1 | 
|---|
| [1] | 46 |  | 
|---|
|  | 47 |  | 
|---|
| [481] | 48 | #if defined(UINT32) | 
|---|
|  | 49 | typedef unsigned char u8; | 
|---|
|  | 50 | typedef UINT32 u32; | 
|---|
|  | 51 | #else | 
|---|
| [1] | 52 |  | 
|---|
| [481] | 53 | typedef unsigned char u8; | 
|---|
|  | 54 | #if defined(HAVE_INT_32) | 
|---|
|  | 55 | typedef unsigned int u32; | 
|---|
|  | 56 | #elif defined(HAVE_LONG_32) | 
|---|
|  | 57 | typedef unsigned long u32; | 
|---|
|  | 58 | #elif defined(HAVE_SHORT_32) | 
|---|
|  | 59 | typedef unsigned short u32; | 
|---|
|  | 60 | #else | 
|---|
|  | 61 | #error "No 32 bit integer type found" | 
|---|
|  | 62 | #endif | 
|---|
| [1] | 63 |  | 
|---|
| [481] | 64 | #endif | 
|---|
|  | 65 |  | 
|---|
|  | 66 | #include "rijndael-alg-fst.h" | 
|---|
|  | 67 |  | 
|---|
|  | 68 | /*  Generic Defines  */ | 
|---|
| [1] | 69 | #define     DIR_ENCRYPT           0 /*  Are we encrpyting?  */ | 
|---|
|  | 70 | #define     DIR_DECRYPT           1 /*  Are we decrpyting?  */ | 
|---|
|  | 71 | #define     MODE_ECB              1 /*  Are we ciphering in ECB mode?   */ | 
|---|
|  | 72 | #define     MODE_CBC              2 /*  Are we ciphering in CBC mode?   */ | 
|---|
|  | 73 | #define     MODE_CFB1             3 /*  Are we ciphering in 1-bit CFB mode? */ | 
|---|
| [68] | 74 | #ifndef     TRUE | 
|---|
| [1] | 75 | #define     TRUE                  1 | 
|---|
| [68] | 76 | #endif | 
|---|
|  | 77 | #ifndef     FALSE | 
|---|
| [1] | 78 | #define     FALSE                 0 | 
|---|
| [68] | 79 | #endif | 
|---|
| [1] | 80 | #define     BITSPERBLOCK        128 /* Default number of bits in a cipher block */ | 
|---|
|  | 81 |  | 
|---|
| [481] | 82 | /*  Error Codes  */ | 
|---|
| [1] | 83 | #define     BAD_KEY_DIR          -1 /*  Key direction is invalid, e.g., unknown value */ | 
|---|
|  | 84 | #define     BAD_KEY_MAT          -2 /*  Key material not of correct length */ | 
|---|
|  | 85 | #define     BAD_KEY_INSTANCE     -3 /*  Key passed is not valid */ | 
|---|
|  | 86 | #define     BAD_CIPHER_MODE      -4 /*  Params struct passed to cipherInit invalid */ | 
|---|
|  | 87 | #define     BAD_CIPHER_STATE     -5 /*  Cipher in wrong state (e.g., not initialized) */ | 
|---|
|  | 88 | #define     BAD_BLOCK_LENGTH     -6 | 
|---|
|  | 89 | #define     BAD_CIPHER_INSTANCE  -7 | 
|---|
|  | 90 | #define     BAD_DATA             -8 /*  Data contents are invalid, e.g., invalid padding */ | 
|---|
|  | 91 | #define     BAD_OTHER            -9 /*  Unknown error */ | 
|---|
|  | 92 |  | 
|---|
| [481] | 93 | /*  Algorithm-specific Defines  */ | 
|---|
|  | 94 | #define     RIJNDAEL_MAX_KEY_SIZE         64 /* # of ASCII char's needed to represent a key */ | 
|---|
|  | 95 | #define     RIJNDAEL_MAX_IV_SIZE          16 /* # bytes needed to represent an IV  */ | 
|---|
| [1] | 96 |  | 
|---|
|  | 97 | #ifdef SH_ENCRYPT | 
|---|
|  | 98 |  | 
|---|
| [481] | 99 | /*  Typedefs  */ | 
|---|
| [1] | 100 |  | 
|---|
| [481] | 101 | typedef unsigned char   BYTE; | 
|---|
| [1] | 102 |  | 
|---|
|  | 103 | /*  The structure for key information */ | 
|---|
|  | 104 | typedef struct { | 
|---|
| [481] | 105 | u32   rk[4*(RIJNDAEL_MAXNR + 1)];        /* key schedule */ | 
|---|
|  | 106 | u32   ek[4*(RIJNDAEL_MAXNR + 1)];        /* CFB1 key schedule (encryption only) */ | 
|---|
|  | 107 | BYTE  direction;                /* Key used for encrypting or decrypting? */ | 
|---|
|  | 108 | int   keyLen;                   /* Length of the key  */ | 
|---|
|  | 109 | char  keyMaterial[RIJNDAEL_MAX_KEY_SIZE+1];  /* Raw key data in ASCII, e.g., user input or KAT values */ | 
|---|
|  | 110 | int   Nr;                       /* key-length-dependent number of rounds */ | 
|---|
| [1] | 111 | } keyInstance; | 
|---|
|  | 112 |  | 
|---|
|  | 113 | /*  The structure for cipher information */ | 
|---|
|  | 114 | typedef struct {                    /* changed order of the components */ | 
|---|
| [481] | 115 | u32  IV[RIJNDAEL_MAX_IV_SIZE / sizeof(u32)]; | 
|---|
|  | 116 | /* A possible Initialization Vector for ciphering */ | 
|---|
|  | 117 | BYTE  mode;                     /* MODE_ECB, MODE_CBC, or MODE_CFB1 */ | 
|---|
| [1] | 118 | } cipherInstance; | 
|---|
|  | 119 |  | 
|---|
|  | 120 | /*  Function prototypes  */ | 
|---|
|  | 121 |  | 
|---|
| [481] | 122 | int rijndael_makeKey(keyInstance *, BYTE, int, const char *); | 
|---|
| [1] | 123 |  | 
|---|
| [481] | 124 | int rijndael_cipherInit(cipherInstance *, BYTE, const char *); | 
|---|
| [1] | 125 |  | 
|---|
| [481] | 126 | int rijndael_blockEncrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *); | 
|---|
| [1] | 127 |  | 
|---|
| [481] | 128 | int rijndael_padEncrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *); | 
|---|
| [1] | 129 |  | 
|---|
| [481] | 130 | int rijndael_blockDecrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *); | 
|---|
|  | 131 |  | 
|---|
|  | 132 | int rijndael_padDecrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *); | 
|---|
|  | 133 |  | 
|---|
| [1] | 134 | /* SH_ENCRYPT */ | 
|---|
|  | 135 | #endif | 
|---|
| [481] | 136 | #endif /* __RIJNDAEL_API_FST_H */ | 
|---|