00001 /** 00002 * rijndael-api-fst.h 00003 * 00004 * @version 2.9 (December 2000) 00005 * 00006 * Optimised ANSI C code for the Rijndael cipher (now AES) 00007 * 00008 * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be> 00009 * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be> 00010 * @author Paulo Barreto <paulo.barreto@terra.com.br> 00011 * 00012 * This code is hereby placed in the public domain. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS 00015 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00016 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00017 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE 00018 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00019 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00020 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 00021 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00022 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00023 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00024 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00025 * 00026 * Acknowledgements: 00027 * 00028 * We are deeply indebted to the following people for their bug reports, 00029 * fixes, and improvement suggestions to this implementation. Though we 00030 * tried to list all contributions, we apologise in advance for any 00031 * missing reference. 00032 * 00033 * Andrew Bales <Andrew.Bales@Honeywell.com> 00034 * Markus Friedl <markus.friedl@informatik.uni-erlangen.de> 00035 * John Skodon <skodonj@webquill.com> 00036 */ 00037 00038 #ifndef __RIJNDAEL_API_FST_H 00039 #define __RIJNDAEL_API_FST_H 00040 00041 #include <stdio.h> 00042 #include "rijndael-alg-fst.h" 00043 00044 /* Generic Defines */ 00045 #define DIR_ENCRYPT 0 /* Are we encrpyting? */ 00046 #define DIR_DECRYPT 1 /* Are we decrpyting? */ 00047 #define MODE_ECB 1 /* Are we ciphering in ECB mode? */ 00048 #define MODE_CBC 2 /* Are we ciphering in CBC mode? */ 00049 #define MODE_CFB1 3 /* Are we ciphering in 1-bit CFB mode? */ 00050 #define TRUE 1 00051 #define FALSE 0 00052 #define BITSPERBLOCK 128 /* Default number of bits in a cipher block */ 00053 00054 /* Error Codes */ 00055 #define BAD_KEY_DIR -1 /* Key direction is invalid, e.g., unknown value */ 00056 #define BAD_KEY_MAT -2 /* Key material not of correct length */ 00057 #define BAD_KEY_INSTANCE -3 /* Key passed is not valid */ 00058 #define BAD_CIPHER_MODE -4 /* Params struct passed to cipherInit invalid */ 00059 #define BAD_CIPHER_STATE -5 /* Cipher in wrong state (e.g., not initialized) */ 00060 #define BAD_BLOCK_LENGTH -6 00061 #define BAD_CIPHER_INSTANCE -7 00062 #define BAD_DATA -8 /* Data contents are invalid, e.g., invalid padding */ 00063 #define BAD_OTHER -9 /* Unknown error */ 00064 00065 /* Algorithm-specific Defines */ 00066 #define MAX_KEY_SIZE 64 /* # of ASCII char's needed to represent a key */ 00067 #define MAX_IV_SIZE 16 /* # bytes needed to represent an IV */ 00068 00069 /* Typedefs */ 00070 00071 typedef unsigned char BYTE; 00072 00073 /* The structure for key information */ 00074 typedef struct { 00075 BYTE direction; /* Key used for encrypting or decrypting? */ 00076 int keyLen; /* Length of the key */ 00077 char keyMaterial[MAX_KEY_SIZE+1]; /* Raw key data in ASCII, e.g., user input or KAT values */ 00078 int Nr; /* key-length-dependent number of rounds */ 00079 u32 rk[4*(MAXNR + 1)]; /* key schedule */ 00080 u32 ek[4*(MAXNR + 1)]; /* CFB1 key schedule (encryption only) */ 00081 } keyInstance; 00082 00083 /* The structure for cipher information */ 00084 typedef struct { /* changed order of the components */ 00085 BYTE mode; /* MODE_ECB, MODE_CBC, or MODE_CFB1 */ 00086 BYTE IV[MAX_IV_SIZE]; /* A possible Initialization Vector for ciphering */ 00087 } cipherInstance; 00088 00089 /* Function prototypes */ 00090 00091 int makeKey(keyInstance *key, BYTE direction, int keyLen, char *keyMaterial); 00092 00093 int cipherInit(cipherInstance *cipher, BYTE mode, char *IV); 00094 00095 int blockEncrypt(cipherInstance *cipher, keyInstance *key, 00096 BYTE *input, int inputLen, BYTE *outBuffer); 00097 00098 int padEncrypt(cipherInstance *cipher, keyInstance *key, 00099 BYTE *input, int inputOctets, BYTE *outBuffer); 00100 00101 int blockDecrypt(cipherInstance *cipher, keyInstance *key, 00102 BYTE *input, int inputLen, BYTE *outBuffer); 00103 00104 int padDecrypt(cipherInstance *cipher, keyInstance *key, 00105 BYTE *input, int inputOctets, BYTE *outBuffer); 00106 00107 #ifdef INTERMEDIATE_VALUE_KAT 00108 int cipherUpdateRounds(cipherInstance *cipher, keyInstance *key, 00109 BYTE *input, int inputLen, BYTE *outBuffer, int Rounds); 00110 #endif /* INTERMEDIATE_VALUE_KAT */ 00111 00112 #endif /* __RIJNDAEL_API_FST_H */