| 1 | /* $NetBSD: rijndael-api-fst.h,v 1.8 2007/01/21 23:00:08 cbiere Exp $ */ |
| 2 | |
| 3 | /** |
| 4 | * rijndael-api-fst.h |
| 5 | * |
| 6 | * @version 2.9 (December 2000) |
| 7 | * |
| 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> |
| 38 | */ |
| 39 | |
| 40 | #ifndef __RIJNDAEL_API_FST_H |
| 41 | #define __RIJNDAEL_API_FST_H |
| 42 | |
| 43 | #include "rijndael-alg-fst.h" |
| 44 | |
| 45 | /* Generic Defines */ |
| 46 | #define DIR_ENCRYPT 0 /* Are we encrpyting? */ |
| 47 | #define DIR_DECRYPT 1 /* Are we decrpyting? */ |
| 48 | #define MODE_ECB 1 /* Are we ciphering in ECB mode? */ |
| 49 | #define MODE_CBC 2 /* Are we ciphering in CBC mode? */ |
| 50 | #define MODE_CFB1 3 /* Are we ciphering in 1-bit CFB mode? */ |
| 51 | #define TRUE 1 |
| 52 | #define FALSE 0 |
| 53 | #define BITSPERBLOCK 128 /* Default number of bits in a cipher block */ |
| 54 | |
| 55 | /* Error Codes */ |
| 56 | #define BAD_KEY_DIR -1 /* Key direction is invalid, e.g., unknown value */ |
| 57 | #define BAD_KEY_MAT -2 /* Key material not of correct length */ |
| 58 | #define BAD_KEY_INSTANCE -3 /* Key passed is not valid */ |
| 59 | #define BAD_CIPHER_MODE -4 /* Params struct passed to cipherInit invalid */ |
| 60 | #define BAD_CIPHER_STATE -5 /* Cipher in wrong state (e.g., not initialized) */ |
| 61 | #define BAD_BLOCK_LENGTH -6 |
| 62 | #define BAD_CIPHER_INSTANCE -7 |
| 63 | #define BAD_DATA -8 /* Data contents are invalid, e.g., invalid padding */ |
| 64 | #define BAD_OTHER -9 /* Unknown error */ |
| 65 | |
| 66 | /* Algorithm-specific Defines */ |
| 67 | #define RIJNDAEL_MAX_KEY_SIZE 64 /* # of ASCII char's needed to represent a key */ |
| 68 | #define RIJNDAEL_MAX_IV_SIZE 16 /* # bytes needed to represent an IV */ |
| 69 | |
| 70 | /* Typedefs */ |
| 71 | |
| 72 | typedef unsigned char BYTE; |
| 73 | |
| 74 | /* The structure for key information */ |
| 75 | typedef struct { |
| 76 | BYTE direction; /* Key used for encrypting or decrypting? */ |
| 77 | int keyLen; /* Length of the key */ |
| 78 | char keyMaterial[RIJNDAEL_MAX_KEY_SIZE+1]; /* Raw key data in ASCII, e.g., user input or KAT values */ |
| 79 | int Nr; /* key-length-dependent number of rounds */ |
| 80 | u_int32_t rk[4*(RIJNDAEL_MAXNR + 1)]; /* key schedule */ |
| 81 | u_int32_t ek[4*(RIJNDAEL_MAXNR + 1)]; /* CFB1 key schedule (encryption only) */ |
| 82 | } keyInstance; |
| 83 | |
| 84 | /* The structure for cipher information */ |
| 85 | typedef struct { /* changed order of the components */ |
| 86 | u_int32_t IV[RIJNDAEL_MAX_IV_SIZE / sizeof(u_int32_t)]; |
| 87 | /* A possible Initialization Vector for ciphering */ |
| 88 | BYTE mode; /* MODE_ECB, MODE_CBC, or MODE_CFB1 */ |
| 89 | } cipherInstance; |
| 90 | |
| 91 | /* Function prototypes */ |
| 92 | |
| 93 | int rijndael_makeKey(keyInstance *, BYTE, int, const char *); |
| 94 | |
| 95 | int rijndael_cipherInit(cipherInstance *, BYTE, const char *); |
| 96 | |
| 97 | int rijndael_blockEncrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *); |
| 98 | |
| 99 | int rijndael_padEncrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *); |
| 100 | |
| 101 | int rijndael_blockDecrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *); |
| 102 | |
| 103 | int rijndael_padDecrypt(cipherInstance *, keyInstance *, const BYTE *, int, BYTE *); |
| 104 | |
| 105 | #endif /* __RIJNDAEL_API_FST_H */ |
| 106 | |