OpenZWave Library 1.2
aes.h
Go to the documentation of this file.
1/*
2---------------------------------------------------------------------------
3Copyright (c) 1998-2013, Brian Gladman, Worcester, UK. All rights reserved.
4
5The redistribution and use of this software (with or without changes)
6is allowed without the payment of fees or royalties provided that:
7
8 source code distributions include the above copyright notice, this
9 list of conditions and the following disclaimer;
10
11 binary distributions include the above copyright notice, this list
12 of conditions and the following disclaimer in their documentation.
13
14This software is provided 'as is' with no explicit or implied warranties
15in respect of its operation, including, but not limited to, correctness
16and fitness for purpose.
17---------------------------------------------------------------------------
18Issue Date: 20/12/2007
19
20 This file contains the definitions required to use AES in C. See aesopt.h
21 for optimisation details.
22*/
23
24#ifndef _AES_H
25#define _AES_H
26
27#include <stdlib.h>
28
29/* This include is used to find 8 & 32 bit unsigned integer types */
30#include "brg_types.h"
31
32#if defined(__cplusplus)
33extern "C"
34{
35#endif
36
37#define AES_128 /* if a fast 128 bit key scheduler is needed */
38#define AES_192 /* if a fast 192 bit key scheduler is needed */
39#define AES_256 /* if a fast 256 bit key scheduler is needed */
40#define AES_VAR /* if variable key size scheduler is needed */
41#define AES_MODES /* if support is needed for modes */
42
43/* The following must also be set in assembler files if being used */
44
45#define AES_ENCRYPT /* if support for encryption is needed */
46#define AES_DECRYPT /* if support for decryption is needed */
47
48#define AES_BLOCK_SIZE 16 /* the AES block size in bytes */
49#define N_COLS 4 /* the number of columns in the state */
50
51/* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */
52/* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */
53/* or 44, 52 or 60 32-bit words. */
54
55#if defined( AES_VAR ) || defined( AES_256 )
56#define KS_LENGTH 60
57#elif defined( AES_192 )
58#define KS_LENGTH 52
59#else
60#define KS_LENGTH 44
61#endif
62
63#define AES_RETURN INT_RETURN
64
65/* the character array 'inf' in the following structures is used */
66/* to hold AES context information. This AES code uses cx->inf.b[0] */
67/* to hold the number of rounds multiplied by 16. The other three */
68/* elements can be used by code that implements additional modes */
69
70typedef union
71{ uint32_t l;
72 uint8_t b[4];
73} aes_inf;
74
75#pragma warning( disable : 4324 )
76
77#ifdef _WIN64
78__declspec(align(16))
79#endif
80typedef struct
81{ uint32_t ks[KS_LENGTH];
84
85#ifdef _WIN64
86__declspec(align(16))
87#endif
88typedef struct
89{ uint32_t ks[KS_LENGTH];
92
93#pragma warning( default : 4324 )
94
95/* This routine must be called before first use if non-static */
96/* tables are being used */
97
99
100/* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */
101/* those in the range 128 <= key_len <= 256 are given in bits */
102
103#if defined( AES_ENCRYPT )
104
105#if defined( AES_128 ) || defined( AES_VAR)
106AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
107#endif
108
109#if defined( AES_192 ) || defined( AES_VAR)
110AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
111#endif
112
113#if defined( AES_256 ) || defined( AES_VAR)
114AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
115#endif
116
117#if defined( AES_VAR )
118AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]);
119#endif
120
121AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
122
123#endif
124
125#if defined( AES_DECRYPT )
126
127#if defined( AES_128 ) || defined( AES_VAR)
128AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
129#endif
130
131#if defined( AES_192 ) || defined( AES_VAR)
132AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
133#endif
134
135#if defined( AES_256 ) || defined( AES_VAR)
136AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
137#endif
138
139#if defined( AES_VAR )
140AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]);
141#endif
142
143AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
144
145#endif
146
147#if defined( AES_MODES )
148
149/* Multiple calls to the following subroutines for multiple block */
150/* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */
151/* long messages incrementally provided that the context AND the iv */
152/* are preserved between all such calls. For the ECB and CBC modes */
153/* each individual call within a series of incremental calls must */
154/* process only full blocks (i.e. len must be a multiple of 16) but */
155/* the CFB, OFB and CTR mode calls can handle multiple incremental */
156/* calls of any length. Each mode is reset when a new AES key is */
157/* set but ECB and CBC operations can be reset without setting a */
158/* new key by setting a new IV value. To reset CFB, OFB and CTR */
159/* without setting the key, aes_mode_reset() must be called and the */
160/* IV must be set. NOTE: All these calls update the IV on exit so */
161/* this has to be reset if a new operation with the same IV as the */
162/* previous one is required (or decryption follows encryption with */
163/* the same IV array). */
164
166
167AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
168 int len, const aes_encrypt_ctx cx[1]);
169
170AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
171 int len, const aes_decrypt_ctx cx[1]);
172
173AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
174 int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);
175
176AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
177 int len, unsigned char *iv, const aes_decrypt_ctx cx[1]);
178
180
181AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
182 int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
183
184AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
185 int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
186
187#define aes_ofb_encrypt aes_ofb_crypt
188#define aes_ofb_decrypt aes_ofb_crypt
189
190AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
191 int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
192
193typedef void cbuf_inc(unsigned char *cbuf);
194
195#define aes_ctr_encrypt aes_ctr_crypt
196#define aes_ctr_decrypt aes_ctr_crypt
197
198AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
199 int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]);
200
201#endif
202
203#if defined(__cplusplus)
204}
205#endif
206
207#endif
AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1])
AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *iv, aes_encrypt_ctx cx[1])
Definition: aes_modes.c:723
AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *iv, const aes_encrypt_ctx cx[1])
Definition: aes_modes.c:261
AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1])
Definition: aeskey.c:539
void cbuf_inc(unsigned char *cbuf)
Definition: aes.h:193
AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *iv, aes_encrypt_ctx cx[1])
Definition: aes_modes.c:457
AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1])
AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1])
AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *iv, const aes_decrypt_ctx cx[1])
Definition: aes_modes.c:358
AES_RETURN aes_test_alignment_detection(unsigned int n)
Definition: aes_modes.c:109
AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *iv, aes_encrypt_ctx cx[1])
Definition: aes_modes.c:582
AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1])
AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf, int len, const aes_decrypt_ctx cx[1])
Definition: aes_modes.c:199
AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf, int len, const aes_encrypt_ctx cx[1])
Definition: aes_modes.c:137
AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1])
AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1])
Definition: aes_modes.c:850
AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1])
Definition: aeskey.c:528
#define AES_RETURN
Definition: aes.h:63
AES_RETURN aes_init(void)
Definition: aestab.c:187
AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1])
AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1])
AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1])
Definition: aes_modes.c:131
AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1])
#define KS_LENGTH
Definition: aes.h:56
Definition: aes.h:89
aes_inf inf
Definition: aes.h:90
Definition: aes.h:81
aes_inf inf
Definition: aes.h:82
Definition: aes.h:71
uint32_t l
Definition: aes.h:71