1 /* rijndael-alg-fst.h v2.0 August '99 2 * Optimised ANSI C code 3 */ 4 5 #include <stdio.h> 6 7 #ifndef __RIJNDAEL_ALG_H 8 #define __RIJNDAEL_ALG_H 9 10 #define MAXKC (256/32) 11 #define MAXROUNDS 14 12 13 typedef unsigned char word8; 14 typedef unsigned short word16; 15 typedef unsigned int word32; 16 17 int ROUNDS; 18 19 int rijndaelKeySched (word8 k[MAXKC][4], int keyBits, 20 word8 rk[MAXROUNDS+1][4][4]); 21 int rijndaelKeyEnctoDec (int keyBits, word8 W[MAXROUNDS+1][4][4]); 22 int rijndaelEncrypt (word8 a[16], word8 b[16], 23 word8 rk[MAXROUNDS+1][4][4]); 24 int rijndaelEncryptRound (word8 a[4][4], 25 word8 rk[MAXROUNDS+1][4][4], int rounds); 26 int rijndaelDecrypt (word8 a[16], word8 b[16], 27 word8 rk[MAXROUNDS+1][4][4]); 28 int rijndaelDecryptRound (word8 a[4][4], 29 word8 rk[MAXROUNDS+1][4][4], int rounds); 30 31 #endif /* __RIJNDAEL_ALG_H */ 32 33 /* End of algorithm headers. begin the AES API header defs */ 34 35 36 #ifndef __RIJNDAEL_API_H 37 #define __RIJNDAEL_API_H 38 39 /* rijndael-api-fst.h v2.0 August '99 40 * Optimised ANSI C code 41 */ 42 43 /* Defines: 44 Add any additional defines you need 45 */ 46 47 #define DIR_ENCRYPT 0 /* Are we encrpyting? */ 48 #define DIR_DECRYPT 1 /* Are we decrpyting? */ 49 #define MODE_ECB 1 /* Are we ciphering in ECB mode? */ 50 #define MODE_CBC 2 /* Are we ciphering in CBC mode? */ 51 #define MODE_CFB1 3 /* Are we ciphering in 1-bit CFB mode? */ 52 #define TRUE 1 53 #define FALSE 0 54 #define BITSPERBLOCK 128 /* Default number of bits in a cipher block */ 55 56 /* Error Codes - CHANGE POSSIBLE: inclusion of additional error codes */ 57 #define BAD_KEY_DIR -1 /* Key direction is invalid, e.g., 58 unknown value */ 59 #define BAD_KEY_MAT -2 /* Key material not of correct 60 length */ 61 #define BAD_KEY_INSTANCE -3 /* Key passed is not valid */ 62 #define BAD_CIPHER_MODE -4 /* Params struct passed to 63 cipherInit invalid */ 64 #define BAD_CIPHER_STATE -5 /* Cipher in wrong state (e.g., not 65 initialized) */ 66 #define BAD_BLOCK_LENGTH -6 67 #define BAD_CIPHER_INSTANCE -7 68 69 70 /* CHANGE POSSIBLE: inclusion of algorithm specific defines */ 71 #define MAX_KEY_SIZE 64 /* # of ASCII char's needed to 72 represent a key */ 73 #define MAX_IV_SIZE 32 /* # bytes needed to 74 represent an IV */ 75 76 /* Typedefs: 77 78 Typedef'ed data storage elements. Add any algorithm specific 79 parameters at the bottom of the structs as appropriate. 80 */ 81 82 typedef unsigned char BYTE; 83 84 /* The structure for key information */ 85 typedef struct { 86 BYTE direction; /* Key used for encrypting or decrypting? */ 87 int keyLen; /* Length of the key */ 88 char keyMaterial[MAX_KEY_SIZE+1]; /* Raw key data in ASCII, 89 e.g., user input or KAT values */ 90 /* The following parameters are algorithm dependent, replace or 91 add as necessary */ 92 int blockLen; /* block length */ 93 word8 keySched[MAXROUNDS+1][4][4]; /* key schedule */ 94 } keyInstance; 95 96 /* The structure for cipher information */ 97 typedef struct { /* changed order of the components */ 98 BYTE mode; /* MODE_ECB, MODE_CBC, or MODE_CFB1 */ 99 BYTE IV[MAX_IV_SIZE]; /* A possible Initialization Vector for 100 ciphering */ 101 /* Add any algorithm specific parameters needed here */ 102 int blockLen; /* Sample: Handles non-128 bit block sizes 103 (if available) */ 104 } cipherInstance; 105 106 107 /* Function protoypes */ 108 /* CHANGED: makeKey(): parameter blockLen added 109 this parameter is absolutely necessary if you want to 110 setup the round keys in a variable block length setting 111 cipherInit(): parameter blockLen added (for obvious reasons) 112 */ 113 int makeKey(keyInstance *key, BYTE direction, int keyLen, char *keyMaterial); 114 115 int cipherInit(cipherInstance *cipher, BYTE mode, char *IV); 116 117 int blockEncrypt(cipherInstance *cipher, keyInstance *key, BYTE *input, 118 int inputLen, BYTE *outBuffer); 119 120 int blockDecrypt(cipherInstance *cipher, keyInstance *key, BYTE *input, 121 int inputLen, BYTE *outBuffer); 122 int cipherUpdateRounds(cipherInstance *cipher, keyInstance *key, BYTE *input, 123 int inputLen, BYTE *outBuffer, int Rounds); 124 125 126 #endif /* __RIJNDAEL_API_H */ 127