]> git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/utilLib/rijndaelApi.h
Security-57740.31.2.tar.gz
[apple/security.git] / SecurityTests / cspxutils / utilLib / rijndaelApi.h
1 /*
2 * rijndaelApi.h - AES API layer
3 *
4 * Based on rijndael-api-ref.h v2.0 written by Paulo Barreto
5 * and Vincent Rijmen
6 */
7
8 #ifndef _RIJNDAEL_API_REF_H_
9 #define _RIJNDAEL_API_REF_H_
10
11 #include <stdio.h>
12 #include "rijndael-alg-ref.h"
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 #define DIR_ENCRYPT 0 /* Are we encrpyting? */
19 #define DIR_DECRYPT 1 /* Are we decrpyting? */
20 #define MODE_ECB 1 /* Are we ciphering in ECB mode? */
21 #define MODE_CBC 2 /* Are we ciphering in CBC mode? */
22
23 #define TRUE 1
24 #define FALSE 0
25
26 /* Error Codes */
27 #define BAD_KEY_DIR -1 /* Key direction is invalid, e.g.,
28 unknown value */
29 #define BAD_KEY_MAT -2 /* Key material not of correct
30 length */
31 #define BAD_KEY_INSTANCE -3 /* Key passed is not valid */
32 #define BAD_CIPHER_MODE -4 /* Params struct passed to
33 cipherInit invalid */
34 #define BAD_CIPHER_STATE -5 /* Cipher in wrong state (e.g., not
35 initialized) */
36 #define BAD_CIPHER_INSTANCE -7
37
38 #define MAX_AES_KEY_SIZE (MAX_AES_KEY_BITS / 8)
39 #define MAX_AES_BLOCK_SIZE (MAX_AES_BLOCK_BITS / 8)
40 #define MAX_AES_IV_SIZE MAX_AES_BLOCK_SIZE
41
42 typedef unsigned char BYTE;
43
44 /* The structure for key information */
45 typedef struct {
46 BYTE direction; /* Key used for encrypting or decrypting? */
47 int keyLen; /* Length of the key in bits */
48 int blockLen; /* Length of block in bits */
49 word8 keySched[MAXROUNDS+1][4][MAXBC]; /* key schedule */
50 } keyInstance;
51
52 /* The structure for cipher information */
53 typedef struct {
54 BYTE mode; /* MODE_ECB, MODE_CBC, or MODE_CFB1 */
55 word8 chainBlock[4][MAXBC];
56 int blockLen; /* block length in bits */
57 } cipherInstance;
58
59
60 int _makeKey(
61 keyInstance *key,
62 BYTE direction,
63 int keyLen, // in BITS
64 int blockLen, // in BITS
65 BYTE *keyMaterial);
66
67 int _cipherInit(
68 cipherInstance *cipher,
69 BYTE mode,
70 int blockLen, // in BITS
71 BYTE *IV);
72
73 int _blockEncrypt(
74 cipherInstance *cipher,
75 keyInstance *key,
76 BYTE *input,
77 int inputLen, // in BITS
78 BYTE *outBuffer);
79
80 int _blockDecrypt(
81 cipherInstance *cipher,
82 keyInstance *key,
83 BYTE *input,
84 int inputLen, // in BITS
85 BYTE *outBuffer);
86
87 /*
88 * Apple addenda 3/28/2001: simplified single-block encrypt/decrypt.
89 * Used when chaining and padding is done in elsewhere.
90 */
91 int _rijndaelBlockEncrypt(
92 cipherInstance *cipher,
93 keyInstance *key,
94 BYTE *input,
95 BYTE *outBuffer);
96 int _rijndaelBlockDecrypt(
97 cipherInstance *cipher,
98 keyInstance *key,
99 BYTE *input,
100 BYTE *outBuffer);
101
102 #ifdef __cplusplus
103 }
104 #endif // cplusplus
105
106 #endif // RIJNDAEL_API_REF
107
108