xref: /freebsd/sys/crypto/rijndael/rijndael-api-fst.h (revision 33841545909f4a4ee94aa148b3a9cbcdc1abb02a)
1 /*	$FreeBSD$	*/
2 /*	$KAME: rijndael-api-fst.h,v 1.6 2001/05/27 00:23:23 itojun Exp $	*/
3 
4 /*
5  * rijndael-api-fst.h   v2.3   April '2000
6  *
7  * Optimised ANSI C code
8  *
9  * #define INTERMEDIATE_VALUE_KAT to generate the Intermediate Value Known Answer Test.
10  */
11 
12 #ifndef __RIJNDAEL_API_FST_H
13 #define __RIJNDAEL_API_FST_H
14 
15 #include <crypto/rijndael/rijndael-alg-fst.h>
16 
17 /*  Defines:
18 	Add any additional defines you need
19 */
20 
21 #define     DIR_ENCRYPT           0 /*  Are we encrpyting?  */
22 #define     DIR_DECRYPT           1 /*  Are we decrpyting?  */
23 #define     MODE_ECB              1 /*  Are we ciphering in ECB mode?   */
24 #define     MODE_CBC              2 /*  Are we ciphering in CBC mode?   */
25 #define     MODE_CFB1             3 /*  Are we ciphering in 1-bit CFB mode? */
26 #define     TRUE                  1
27 #define     FALSE                 0
28 #define     BITSPERBLOCK        128 /* Default number of bits in a cipher block */
29 
30 /*  Error Codes - CHANGE POSSIBLE: inclusion of additional error codes  */
31 #define     BAD_KEY_DIR          -1 /*  Key direction is invalid, e.g., unknown value */
32 #define     BAD_KEY_MAT          -2 /*  Key material not of correct length */
33 #define     BAD_KEY_INSTANCE     -3 /*  Key passed is not valid */
34 #define     BAD_CIPHER_MODE      -4 /*  Params struct passed to cipherInit invalid */
35 #define     BAD_CIPHER_STATE     -5 /*  Cipher in wrong state (e.g., not initialized) */
36 #define     BAD_BLOCK_LENGTH     -6
37 #define     BAD_CIPHER_INSTANCE  -7
38 #define     BAD_DATA             -8 /*  Data contents are invalid, e.g., invalid padding */
39 #define     BAD_OTHER            -9 /*  Unknown error */
40 
41 /*  CHANGE POSSIBLE:  inclusion of algorithm specific defines  */
42 #define     MAX_KEY_SIZE         64 /* # of ASCII char's needed to represent a key */
43 #define     MAX_IV_SIZE          16 /* # bytes needed to represent an IV  */
44 
45 /*  Typedefs:
46 
47 	Typedef'ed data storage elements.  Add any algorithm specific
48 parameters at the bottom of the structs as appropriate.
49 */
50 
51 /*  The structure for key information */
52 typedef struct {
53     u_int8_t  direction;            /* Key used for encrypting or decrypting? */
54     int   keyLen;                   /* Length of the key  */
55     char  keyMaterial[MAX_KEY_SIZE+1];  /* Raw key data in ASCII, e.g., user input or KAT values */
56         /*  The following parameters are algorithm dependent, replace or add as necessary  */
57 	int   ROUNDS;                   /* key-length-dependent number of rounds */
58     int   blockLen;                 /* block length */
59     union {
60     	u_int8_t xkS8[RIJNDAEL_MAXROUNDS+1][4][4];	/* key schedule		*/
61     	u_int32_t xkS32[RIJNDAEL_MAXROUNDS+1][4];	/* key schedule		*/
62     } xKeySched;
63 #define	keySched	xKeySched.xkS8
64 } keyInstance;
65 
66 /*  The structure for cipher information */
67 typedef struct {                    /* changed order of the components */
68     u_int8_t mode;                  /* MODE_ECB, MODE_CBC, or MODE_CFB1 */
69     u_int8_t IV[MAX_IV_SIZE];       /* A possible Initialization Vector for ciphering */
70         /*  Add any algorithm specific parameters needed here  */
71     int   blockLen;                 /* Sample: Handles non-128 bit block sizes (if available) */
72 } cipherInstance;
73 
74 /*  Function prototypes  */
75 /*  CHANGED: nothing
76 	TODO: implement the following extensions to setup 192-bit and 256-bit block lengths:
77         makeKeyEx():    parameter blockLen added
78                         -- this parameter is absolutely necessary if you want to
79                         setup the round keys in a variable block length setting
80 	    cipherInitEx(): parameter blockLen added (for obvious reasons)
81  */
82 
83 int rijndael_makeKey(keyInstance *key, u_int8_t direction, int keyLen, char *keyMaterial);
84 
85 int rijndael_cipherInit(cipherInstance *cipher, u_int8_t mode, char *IV);
86 
87 int rijndael_blockEncrypt(cipherInstance *cipher, keyInstance *key,
88         u_int8_t *input, int inputLen, u_int8_t *outBuffer);
89 
90 int rijndael_padEncrypt(cipherInstance *cipher, keyInstance *key,
91 		u_int8_t *input, int inputOctets, u_int8_t *outBuffer);
92 
93 int rijndael_blockDecrypt(cipherInstance *cipher, keyInstance *key,
94         u_int8_t *input, int inputLen, u_int8_t *outBuffer);
95 
96 int rijndael_padDecrypt(cipherInstance *cipher, keyInstance *key,
97 		u_int8_t *input, int inputOctets, u_int8_t *outBuffer);
98 
99 #ifdef INTERMEDIATE_VALUE_KAT
100 int rijndael_cipherUpdateRounds(cipherInstance *cipher, keyInstance *key,
101         u_int8_t *input, int inputLen, u_int8_t *outBuffer, int Rounds);
102 #endif /* INTERMEDIATE_VALUE_KAT */
103 
104 #endif /*  __RIJNDAEL_API_FST_H */
105