aes_api.h
3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef __aes_h__
#define __aes_h__
#ifndef __uint32_t_defined
typedef unsigned int uint32_t;
#define __uint32_t_defined
typedef unsigned char uint8_t;
typedef int int_least16_t;
#endif
/* Generic Defines */
#define AES_DIR_ENCRYPT 0 /* Are we encrpyting? */
#define AES_DIR_DECRYPT 1 /* Are we decrpyting? */
#define AES_MODE_ECB 1 /* Are we ciphering in ECB mode? */
#define AES_MODE_CBC 2 /* Are we ciphering in CBC mode? */
//#define AES_MODE_CFB1 3 /* Are we ciphering in 1-bit CFB mode? */
#define AES_TRUE 1
#define AES_FALSE 0
#define AES_BITSPERBLOCK 128 /* Default number of bits in a cipher block */
/* Error Codes */
#define AES_BAD_KEY_DIR -1 /* Key direction is invalid, e.g., unknown value */
#define AES_BAD_KEY_MAT -2 /* Key material not of correct length */
#define AES_BAD_KEY_INSTANCE -3 /* Key passed is not valid */
#define AES_BAD_CIPHER_MODE -4 /* Params struct passed to cipherInit invalid */
#define AES_BAD_CIPHER_STATE -5 /* Cipher in wrong state (e.g., not initialized) */
#define AES_BAD_BLOCK_LENGTH -6
#define AES_BAD_CIPHER_INSTANCE -7
#define AES_BAD_DATA -8 /* Data contents are invalid, e.g., invalid padding */
#define AES_BAD_OTHER -9 /* Unknown error */
/* Algorithm-specific Defines */
#define AES_MAX_KEY_SIZE 64 /* # of ASCII char's needed to represent a key */
#define AES_MAX_IV_SIZE 16 /* # bytes needed to represent an IV */
#define __AES_MAXNR 14
/* Typedefs */
/* The structure for key information */
typedef struct {
unsigned char direction; /* Key used for encrypting or decrypting? */
int Nr; /* key-length-dependent number of rounds */
uint32_t rk[4*(__AES_MAXNR + 1)]; /* key schedule */
uint32_t ek[4*(__AES_MAXNR + 1)]; /* CFB1 key schedule (encryption only) */
} AesKeyInstance;
/* The structure for cipher information */
typedef struct { /* changed order of the components */
unsigned int mode; /* MODE_ECB, MODE_CBC, or MODE_CFB1 */
unsigned char IV[AES_MAX_IV_SIZE]; /* A possible Initialization Vector for ciphering */
} AesCipherInstance;
/* Function prototypes */
int aesMakeKey(AesKeyInstance *key, unsigned char direction, int keyLen, unsigned char *AesKeyMaterial);
int aesCipherInit(AesCipherInstance *cipher, unsigned char mode, unsigned char *IV);
int aesBlockEncrypt(AesCipherInstance *cipher, AesKeyInstance *key,
unsigned char *input, int inputLen, unsigned char *outBuffer);
#if 0
int aesPadEncrypt(AesCipherInstance *cipher, AesKeyInstance *key,
unsigned char *input, int inputOctets, unsigned char *outBuffer);
#endif
int aesBlockDecrypt(AesCipherInstance *cipher, AesKeyInstance *key,
unsigned char *input, int inputLen, unsigned char *outBuffer);
#if 0
int aesPadDecrypt(AesCipherInstance *cipher, AesKeyInstance *key,
unsigned char *input, int inputOctets, unsigned char *outBuffer);
#endif
#endif /* __aes_h__ */