1 /* aes.h */ 2 3 /* AES Cipher header file for ANSI C Submissions 4 Lawrence E. Bassham III 5 Computer Security Division 6 National Institute of Standards and Technology 7 8 April 15, 1998 9 10 This sample is to assist implementers developing to the Cryptographic 11 API Profile for AES Candidate Algorithm Submissions. Please consult this 12 document as a cross-reference. 13 14 ANY CHANGES, WHERE APPROPRIATE, TO INFORMATION PROVIDED IN THIS FILE 15 MUST BE DOCUMENTED. CHANGES ARE ONLY APPROPRIATE WHERE SPECIFIED WITH 16 THE STRING "CHANGE POSSIBLE". FUNCTION CALLS AND THEIR PARAMETERS CANNOT 17 BE CHANGED. STRUCTURES CAN BE ALTERED TO ALLOW IMPLEMENTERS TO INCLUDE 18 IMPLEMENTATION SPECIFIC INFORMATION. 19 */ 20 21 /* Includes: 22 Standard include files 23 */ 24 25 #include <stdio.h> 26 27 /* Defines: 28 Add any additional defines you need 29 */ 30 31 #define DIR_ENCRYPT 0 /* Are we encrpyting? */ 32 #define DIR_DECRYPT 1 /* Are we decrpyting? */ 33 #define MODE_ECB 1 /* Are we ciphering in ECB mode? */ 34 #define MODE_CBC 2 /* Are we ciphering in CBC mode? */ 35 #define MODE_CFB1 3 /* Are we ciphering in 1-bit CFB mode? */ 36 #define TRUE 1 37 #define FALSE 0 38 39 /* Error Codes - CHANGE POSSIBLE: inclusion of additional error codes */ 40 #define BAD_KEY_DIR -1 /* Key direction is invalid, e;g;, 41 unknown value */ 42 #define BAD_KEY_MAT -2 /* Key material not of correct 43 length */ 44 #define BAD_KEY_INSTANCE -3 /* Key passed is not valid */ 45 #define BAD_CIPHER_MODE -4 /* Params struct passed to 46 cipherInit invalid */ 47 #define BAD_CIPHER_STATE -5 /* Cipher in wrong state (e.g., not 48 initialized) */ 49 50 /* CHANGE POSSIBLE: inclusion of algorithm specific defines */ 51 #define MAX_KEY_SIZE 64 /* # of ASCII char's needed to 52 represent a key */ 53 #define MAX_IV_SIZE 32 /* # of ASCII char's needed to 54 represent an IV */ 55 56 /* Typedefs: 57 58 Typedef'ed data storage elements. Add any algorithm specific 59 parameters at the bottom of the structs as appropriate. 60 */ 61 62 typedef unsigned char BYTE; 63 64 /* The structure for key information */ 65 typedef struct { 66 BYTE direction; /* Key used for encrypting or decrypting? */ 67 int keyLen; /* Length of the key */ 68 char keyMaterial[MAX_KEY_SIZE+1]; /* Raw key data in ASCII, e.g., 69 what the user types or KAT values)*/ 70 /* The following parameters are algorithm dependent, replace or 71 add as necessary */ 72 unsigned long key[8]; /* The key in binary */ 73 unsigned long subkeys[33][4]; /* Serpent subkeys */ 74 } keyInstance; 75 76 /* The structure for cipher information */ 77 typedef struct { 78 BYTE mode; /* MODE_ECB, MODE_CBC, or MODE_CFB1 */ 79 char IV[MAX_IV_SIZE]; /* A possible Initialization Vector for 80 ciphering */ 81 /* Add any algorithm specific parameters needed here */ 82 int blockSize; /* Sample: Handles non-128 bit block sizes 83 (if available) */ 84 } cipherInstance; 85 86 87 /* Function protoypes */ 88 int makeKey(keyInstance *key, BYTE direction, int keyLen, 89 char *keyMaterial); 90 91 int cipherInit(cipherInstance *cipher, BYTE mode, char *IV); 92 93 int blockEncrypt(cipherInstance *cipher, keyInstance *key, BYTE *input, 94 int inputLen, BYTE *outBuffer); 95 96 int blockDecrypt(cipherInstance *cipher, keyInstance *key, BYTE *input, 97 int inputLen, BYTE *outBuffer); 98