| 1 |
|
| 2 |
#include "utilmem.h" |
| 3 |
#include "utilcrypt.h" |
| 4 |
#include "utilcryptdef.h" |
| 5 |
#include "utilmd5.h" |
| 6 |
|
| 7 |
/* rc4 */ |
| 8 |
|
| 9 |
/* |
| 10 |
Initializer arguments: |
| 11 |
- state - crypt state |
| 12 |
- map - a space for rc4 bytes map; may be left NULL in which case will be allocated |
| 13 |
- vkey - crypt key; may be left NULL iff map is provided and properly initialized |
| 14 |
- keylength - the length of crypt key (from 5 to 16 bytes) |
| 15 |
*/ |
| 16 |
|
| 17 |
rc4_state * rc4_state_initialize (rc4_state *state, rc4_map *map, const void *vkey, size_t keylength) |
| 18 |
{ |
| 19 |
int i, j; |
| 20 |
uint8_t tmp; |
| 21 |
const uint8_t *key; |
| 22 |
key = (const uint8_t *)vkey; |
| 23 |
if (keylength == 0 || keylength > 256) |
| 24 |
return NULL; |
| 25 |
state->flags = 0; |
| 26 |
if (map != NULL) |
| 27 |
{ |
| 28 |
state->map = map; |
| 29 |
} |
| 30 |
else |
| 31 |
{ |
| 32 |
state->map = (rc4_map *)util_malloc(sizeof(rc4_map)); |
| 33 |
state->flags |= RC4_STATE_ALLOC; |
| 34 |
} |
| 35 |
|
| 36 |
if (key != NULL) |
| 37 |
{ |
| 38 |
for (i = 0; i < 256; ++i) |
| 39 |
state->smap[i] = (uint8_t)i; |
| 40 |
for (i = 0, j = 0; i < 256; ++i) |
| 41 |
{ |
| 42 |
j = (j + state->smap[i] + key[i % keylength]) & 255; |
| 43 |
tmp = state->smap[i]; |
| 44 |
state->smap[i] = state->smap[j]; |
| 45 |
state->smap[j] = tmp; |
| 46 |
} |
| 47 |
} |
| 48 |
state->i = 0; |
| 49 |
state->j = 0; |
| 50 |
state->flush = 0; /* caller is responsible to override if necessary */ |
| 51 |
return state; |
| 52 |
} |
| 53 |
|
| 54 |
void rc4_map_save (rc4_state *state, rc4_map *map) |
| 55 |
{ |
| 56 |
memcpy(map, state->map, sizeof(rc4_map)); |
| 57 |
} |
| 58 |
|
| 59 |
void rc4_map_restore (rc4_state *state, rc4_map *map) |
| 60 |
{ |
| 61 |
memcpy(state->map, map, sizeof(rc4_map)); |
| 62 |
//state->flags = 0; |
| 63 |
//state->flush = 0; |
| 64 |
state->i = 0; |
| 65 |
state->j = 0; |
| 66 |
} |
| 67 |
|
| 68 |
static uint8_t rc4_next_random_byte (rc4_state *state) |
| 69 |
{ |
| 70 |
uint8_t tmp; |
| 71 |
state->i = (state->i + 1) & 255; |
| 72 |
state->j = (state->j + state->smap[state->i]) & 255; |
| 73 |
tmp = state->smap[state->i]; |
| 74 |
state->smap[state->i] = state->smap[state->j]; |
| 75 |
state->smap[state->j] = tmp; |
| 76 |
return state->smap[(state->smap[state->i] + state->smap[state->j]) & 255]; |
| 77 |
} |
| 78 |
|
| 79 |
iof_status rc4_crypt_state (iof *I, iof *O, rc4_state *state) |
| 80 |
{ |
| 81 |
uint8_t r; |
| 82 |
int c; |
| 83 |
while (iof_ensure(O, 1)) |
| 84 |
{ |
| 85 |
if ((c = iof_get(I)) < 0) |
| 86 |
return c == IOFERR ? IOFERR : (state->flush ? IOFEOF : IOFEMPTY); |
| 87 |
r = rc4_next_random_byte(state); |
| 88 |
//r = r ^ ((uint8_t)c); |
| 89 |
//iof_set(O, r); |
| 90 |
iof_set(O, r ^ ((uint8_t)c)); |
| 91 |
} |
| 92 |
return IOFFULL; |
| 93 |
} |
| 94 |
|
| 95 |
iof_status rc4_crypt (iof *I, iof *O, const void *key, size_t keylength) |
| 96 |
{ |
| 97 |
int ret; |
| 98 |
rc4_state state; |
| 99 |
rc4_map map; |
| 100 |
if (rc4_state_initialize(&state, &map, key, keylength) == NULL) |
| 101 |
return IOFERR; |
| 102 |
state.flush = 1; |
| 103 |
ret = rc4_crypt_state(I, O, &state); |
| 104 |
rc4_state_close(&state); |
| 105 |
return ret; |
| 106 |
} |
| 107 |
|
| 108 |
/* |
| 109 |
Variants that operates on c-strings can worn inplace, so output and input can be the same address. |
| 110 |
Variant that takes rc4_state pointer expects the state properly initialized. Keep in mind |
| 111 |
the crypt procedure modifies rc4 bytes map. All returns the size of encrypted/decrypted |
| 112 |
data, which is the same as input data length for rc4. |
| 113 |
*/ |
| 114 |
|
| 115 |
size_t rc4_crypt_data (const void *input, size_t length, void *output, const void *key, size_t keylength) |
| 116 |
{ |
| 117 |
rc4_state state; |
| 118 |
rc4_map map; |
| 119 |
if (rc4_state_initialize(&state, &map, key, keylength) == NULL) |
| 120 |
return 0; |
| 121 |
return rc4_crypt_state_data(&state, input, length, output); |
| 122 |
// no need to call rc4_state_close() |
| 123 |
} |
| 124 |
|
| 125 |
size_t rc4_crypt_state_data (rc4_state *state, const void *input, size_t length, void *output) |
| 126 |
{ /* state assumed to be initialized and with the proper state of smap */ |
| 127 |
const uint8_t *inp; |
| 128 |
uint8_t r, *out; |
| 129 |
size_t size; |
| 130 |
inp = (const uint8_t *)input; |
| 131 |
out = (uint8_t *)output; |
| 132 |
for (size = 0; size < length; ++size, ++inp, ++out) |
| 133 |
{ |
| 134 |
r = rc4_next_random_byte(state); |
| 135 |
*out = r ^ *inp; |
| 136 |
} |
| 137 |
return length; |
| 138 |
} |
| 139 |
|
| 140 |
void rc4_state_close (rc4_state *state) |
| 141 |
{ |
| 142 |
if (state->smap != NULL && (state->flags & RC4_STATE_ALLOC)) |
| 143 |
{ |
| 144 |
util_free(state->smap); |
| 145 |
state->smap = NULL; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/* aes; parts of code excerpted from https://github.com/kokke/tiny-AES128-C */ |
| 150 |
|
| 151 |
static const uint8_t sbox[256] = { |
| 152 |
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
| 153 |
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
| 154 |
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
| 155 |
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
| 156 |
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
| 157 |
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
| 158 |
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
| 159 |
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
| 160 |
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
| 161 |
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
| 162 |
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
| 163 |
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
| 164 |
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
| 165 |
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
| 166 |
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
| 167 |
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; |
| 168 |
|
| 169 |
static const uint8_t rsbox[256] = |
| 170 |
{ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, |
| 171 |
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, |
| 172 |
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, |
| 173 |
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, |
| 174 |
0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, |
| 175 |
0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, |
| 176 |
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, |
| 177 |
0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, |
| 178 |
0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, |
| 179 |
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, |
| 180 |
0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, |
| 181 |
0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, |
| 182 |
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, |
| 183 |
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, |
| 184 |
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, |
| 185 |
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; |
| 186 |
|
| 187 |
/* |
| 188 |
The round constant word array, rcon[i], contains the values given by |
| 189 |
x to th e power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8) |
| 190 |
Note that i starts at 1, not 0). |
| 191 |
*/ |
| 192 |
|
| 193 |
static const uint8_t rcon[255] = { |
| 194 |
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, |
| 195 |
0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, |
| 196 |
0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, |
| 197 |
0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, |
| 198 |
0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, |
| 199 |
0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, |
| 200 |
0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, |
| 201 |
0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, |
| 202 |
0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, |
| 203 |
0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, |
| 204 |
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, |
| 205 |
0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, |
| 206 |
0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, |
| 207 |
0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, |
| 208 |
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, |
| 209 |
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb }; |
| 210 |
|
| 211 |
/* block copying */ |
| 212 |
|
| 213 |
#define aes_copy_block(output, input) memcpy(output, input, 16) |
| 214 |
|
| 215 |
static void aes_copy_cbc (uint8_t *data, const uint8_t *input) |
| 216 |
{ |
| 217 |
uint8_t i; |
| 218 |
for (i = 0; i < 16; ++i) |
| 219 |
data[i] ^= input[i]; |
| 220 |
} |
| 221 |
|
| 222 |
static void aes_copy_xor (uint8_t *data, const uint8_t *input, const uint8_t *iv) |
| 223 |
{ |
| 224 |
uint8_t i; |
| 225 |
for (i = 0; i < 16; ++i) |
| 226 |
data[i] = input[i] ^ iv[i]; |
| 227 |
} |
| 228 |
|
| 229 |
/* key expansion */ |
| 230 |
|
| 231 |
#define AES_COLUMNS 4 // constant in aes |
| 232 |
|
| 233 |
static void key_expansion (aes_state *state, const uint8_t *key) |
| 234 |
{ |
| 235 |
uint32_t i, j; |
| 236 |
uint8_t t[4], temp; |
| 237 |
uint8_t *keydata, keywords, columns; |
| 238 |
|
| 239 |
keywords = (uint8_t)(state->keylength >> 2); |
| 240 |
keydata = (uint8_t *)state->keyblock; |
| 241 |
|
| 242 |
/* the first round key is the key itself */ |
| 243 |
for(i = 0; i < keywords; ++i) |
| 244 |
{ |
| 245 |
keydata[(i * 4) + 0] = key[(i * 4) + 0]; |
| 246 |
keydata[(i * 4) + 1] = key[(i * 4) + 1]; |
| 247 |
keydata[(i * 4) + 2] = key[(i * 4) + 2]; |
| 248 |
keydata[(i * 4) + 3] = key[(i * 4) + 3]; |
| 249 |
} |
| 250 |
|
| 251 |
/* others derived from the first */ |
| 252 |
for(columns = AES_COLUMNS * (state->rounds + 1); i < columns; ++i) |
| 253 |
{ |
| 254 |
for(j = 0; j < 4; ++j) |
| 255 |
t[j] = keydata[(i - 1) * 4 + j]; |
| 256 |
if (i % keywords == 0) |
| 257 |
{ |
| 258 |
/* rotate the 4 bytes in a word to the left once; [a0,a1,a2,a3] becomes [a1,a2,a3,a0] */ |
| 259 |
temp = t[0]; |
| 260 |
t[0] = t[1]; |
| 261 |
t[1] = t[2]; |
| 262 |
t[2] = t[3]; |
| 263 |
t[3] = temp; |
| 264 |
|
| 265 |
/* take a four-byte input word and apply the S-box to each of the four bytes to produce an output word */ |
| 266 |
t[0] = sbox[t[0]]; |
| 267 |
t[1] = sbox[t[1]]; |
| 268 |
t[2] = sbox[t[2]]; |
| 269 |
t[3] = sbox[t[3]]; |
| 270 |
|
| 271 |
t[0] = t[0] ^ rcon[i / keywords]; |
| 272 |
} |
| 273 |
else if (keywords > 6 && i % keywords == 4) |
| 274 |
{ |
| 275 |
t[0] = sbox[t[0]]; |
| 276 |
t[1] = sbox[t[1]]; |
| 277 |
t[2] = sbox[t[2]]; |
| 278 |
t[3] = sbox[t[3]]; |
| 279 |
} |
| 280 |
keydata[i * 4 + 0] = keydata[(i - keywords) * 4 + 0] ^ t[0]; |
| 281 |
keydata[i * 4 + 1] = keydata[(i - keywords) * 4 + 1] ^ t[1]; |
| 282 |
keydata[i * 4 + 2] = keydata[(i - keywords) * 4 + 2] ^ t[2]; |
| 283 |
keydata[i * 4 + 3] = keydata[(i - keywords) * 4 + 3] ^ t[3]; |
| 284 |
} |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
/* |
| 289 |
An original implementation uses no private buffers except a keyblock. We need private buffers to |
| 290 |
keep a CBC vector between calls and to be able to read input data not necessarily in 16-bytes blocks. |
| 291 |
Encrypter would actually require only one such buffer, as CBC vector is applied on input data before |
| 292 |
the actual cipher procedure. And CBC for the next chunk is simply the output from the previous. |
| 293 |
Decrypter, however, applies the cipher first, then applies CBC to the output with a buffered init |
| 294 |
vector, and the vector for the next call is the row input before cipher. Hence we need two 16-bytes |
| 295 |
buffers for decrypter. |
| 296 |
*/ |
| 297 |
|
| 298 |
/* |
| 299 |
aes_state * aes_state_initialize_ecb (aes_state *State, uint8_t *keyblock, const uint8_t *key) |
| 300 |
{ |
| 301 |
state->flags = 0; |
| 302 |
|
| 303 |
state->flags |= AES_ECB_MODE; |
| 304 |
|
| 305 |
if (keyblock == NULL) |
| 306 |
{ |
| 307 |
keyblock = util_malloc(sizeof(aes_keyblock)); |
| 308 |
state->flags |= AES_STATE_ALLOC; |
| 309 |
} |
| 310 |
state->keyblock = keyblock; |
| 311 |
key_expansion(state, key); |
| 312 |
state->flush = 0; |
| 313 |
return state; |
| 314 |
} |
| 315 |
*/ |
| 316 |
|
| 317 |
void aes_pdf_mode (aes_state *state) |
| 318 |
{ |
| 319 |
state->flags |= AES_INLINE_IV; |
| 320 |
state->flags &= ~AES_NULL_PADDING; |
| 321 |
} |
| 322 |
|
| 323 |
/* |
| 324 |
Initialize arguments: |
| 325 |
- state - crypt state |
| 326 |
- keyblock - a space for aes key expansion; can be left NULL in which case will be allocated |
| 327 |
- key - crypt key; can be left NULL iff keyblock is given and properly initialized |
| 328 |
- keylength - the length of the key (16 or 32 bytes) |
| 329 |
- iv - 16-bytes CBC initialization vector; |
| 330 |
- if left NULL for encoder, one is generated and stored as state->iv |
| 331 |
- can also be left NULL for decorer, but then AES_INLINE_IV must be set, as this informs decoder to take |
| 332 |
an initialization vector from the beginning of the encrypted stream |
| 333 |
|
| 334 |
At the first approach, an initialization vector was copied to state block during initialization and encoders |
| 335 |
assumed that the state block is the current initialization vector. This simplifies encrypting procedure, |
| 336 |
as the output from every 16-bytes chunk encryption is an initialization vector for the next chunk. However, |
| 337 |
it makes api usage cumbersome, as the user has to know that iv may need to be copied to state block |
| 338 |
before each call. |
| 339 |
*/ |
| 340 |
|
| 341 |
static int aes_key_length (aes_state *state, size_t keylength) |
| 342 |
{ |
| 343 |
state->keylength = keylength; |
| 344 |
switch (keylength) |
| 345 |
{ |
| 346 |
case 16: |
| 347 |
state->rounds = 10; |
| 348 |
break; |
| 349 |
case 24: |
| 350 |
state->rounds = 12; |
| 351 |
break; |
| 352 |
case 32: |
| 353 |
state->rounds = 14; |
| 354 |
break; |
| 355 |
default: |
| 356 |
return 0; |
| 357 |
} |
| 358 |
return 1; |
| 359 |
} |
| 360 |
|
| 361 |
aes_state * aes_encode_initialize (aes_state *state, aes_keyblock *keyblock, const void *key, size_t keylength, const void *iv) |
| 362 |
{ |
| 363 |
state->flags = 0; |
| 364 |
if (!aes_key_length(state, keylength)) |
| 365 |
return NULL; |
| 366 |
if (iv != NULL) |
| 367 |
aes_copy_block(state->iv, iv); |
| 368 |
else |
| 369 |
aes_generate_iv(state->iv); |
| 370 |
state->flags |= AES_HAS_IV; |
| 371 |
|
| 372 |
if (keyblock == NULL) |
| 373 |
{ |
| 374 |
keyblock = (aes_keyblock *)util_malloc(sizeof(aes_keyblock)); |
| 375 |
state->flags |= AES_STATE_ALLOC; |
| 376 |
} |
| 377 |
state->keyblock = keyblock; |
| 378 |
if (key != NULL) /* if NULL we assume keyblock is given and already expanded */ |
| 379 |
key_expansion(state, (const uint8_t *)key); |
| 380 |
state->flush = 0; |
| 381 |
return state; |
| 382 |
} |
| 383 |
|
| 384 |
aes_state * aes_decode_initialize (aes_state *state, aes_keyblock *keyblock, const void *key, size_t keylength, const void *iv) |
| 385 |
{ |
| 386 |
state->flags = 0; |
| 387 |
if (!aes_key_length(state, keylength)) |
| 388 |
return NULL; |
| 389 |
if (iv != NULL) |
| 390 |
{ |
| 391 |
aes_copy_block(state->iv, iv); |
| 392 |
state->flags |= AES_HAS_IV; |
| 393 |
} |
| 394 |
/* else if AES_INLINE_IV flag is set will be read from input */ |
| 395 |
|
| 396 |
if (keyblock == NULL) |
| 397 |
{ |
| 398 |
keyblock = (aes_keyblock *)util_malloc(sizeof(aes_keyblock)); |
| 399 |
state->flags |= AES_STATE_ALLOC; |
| 400 |
} |
| 401 |
state->keyblock = keyblock; |
| 402 |
if (key != NULL) /* otherwise keyblock is assumed present and properly initialized */ |
| 403 |
key_expansion(state, (const uint8_t *)key); |
| 404 |
state->flush = 0; |
| 405 |
return state; |
| 406 |
} |
| 407 |
|
| 408 |
void aes_state_close (aes_state *state) |
| 409 |
{ |
| 410 |
if (state->keyblock != NULL && (state->flags & AES_STATE_ALLOC)) |
| 411 |
util_free(state->keyblock); |
| 412 |
} |
| 413 |
|
| 414 |
/* add round key */ |
| 415 |
|
| 416 |
static void aes_round_key (aes_block block, aes_block keyblock) |
| 417 |
{ |
| 418 |
uint8_t i, j; |
| 419 |
for(i = 0; i < 4; ++i) |
| 420 |
for(j = 0; j < 4; ++j) |
| 421 |
block[i][j] ^= keyblock[i][j]; |
| 422 |
} |
| 423 |
|
| 424 |
#define aes_add_key(block, keyblock, round) aes_round_key(block, (*keyblock)[round]) |
| 425 |
|
| 426 |
/* substitution */ |
| 427 |
|
| 428 |
static void aes_encode_sub (aes_block block) |
| 429 |
{ |
| 430 |
uint8_t i, j, v; |
| 431 |
for(i = 0; i < 4; ++i) |
| 432 |
for(j = 0; j < 4; ++j) |
| 433 |
v = block[i][j], block[i][j] = sbox[v]; |
| 434 |
} |
| 435 |
|
| 436 |
/* rows shift; the row index is the shift offset, the first order is not shifted */ |
| 437 |
|
| 438 |
static void aes_encode_shift (aes_block block) |
| 439 |
{ |
| 440 |
uint8_t tmp; |
| 441 |
|
| 442 |
/* 1st row rotated once */ |
| 443 |
tmp = block[0][1]; |
| 444 |
block[0][1] = block[1][1]; |
| 445 |
block[1][1] = block[2][1]; |
| 446 |
block[2][1] = block[3][1]; |
| 447 |
block[3][1] = tmp; |
| 448 |
|
| 449 |
/* 2nd row rotated twice */ |
| 450 |
tmp = block[0][2]; |
| 451 |
block[0][2] = block[2][2]; |
| 452 |
block[2][2] = tmp; |
| 453 |
tmp = block[1][2]; |
| 454 |
block[1][2] = block[3][2]; |
| 455 |
block[3][2] = tmp; |
| 456 |
|
| 457 |
/* 3rd row rotated 3 times */ |
| 458 |
tmp = block[0][3]; |
| 459 |
block[0][3] = block[3][3]; |
| 460 |
block[3][3] = block[2][3]; |
| 461 |
block[2][3] = block[1][3]; |
| 462 |
block[1][3] = tmp; |
| 463 |
} |
| 464 |
|
| 465 |
static uint8_t xtime (uint8_t x) |
| 466 |
{ |
| 467 |
return ((x << 1) ^ (((x >> 7) & 1) * 0x1b)); |
| 468 |
} |
| 469 |
|
| 470 |
/* mix columns */ |
| 471 |
|
| 472 |
static void aes_encode_mix (aes_block block) |
| 473 |
{ |
| 474 |
uint8_t i, tmp, tm, t; |
| 475 |
|
| 476 |
for(i = 0; i < 4; ++i) |
| 477 |
{ |
| 478 |
t = block[i][0]; |
| 479 |
tmp = block[i][0] ^ block[i][1] ^ block[i][2] ^ block[i][3] ; |
| 480 |
tm = block[i][0] ^ block[i][1]; tm = xtime(tm); block[i][0] ^= tm ^ tmp; |
| 481 |
tm = block[i][1] ^ block[i][2]; tm = xtime(tm); block[i][1] ^= tm ^ tmp; |
| 482 |
tm = block[i][2] ^ block[i][3]; tm = xtime(tm); block[i][2] ^= tm ^ tmp; |
| 483 |
tm = block[i][3] ^ t ; tm = xtime(tm); block[i][3] ^= tm ^ tmp; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
/* multiply is used to multiply numbers in the field GF(2^8) */ |
| 488 |
|
| 489 |
#define multiply(x, y) \ |
| 490 |
( ((y & 1) * x) ^ \ |
| 491 |
((y>>1 & 1) * xtime(x)) ^ \ |
| 492 |
((y>>2 & 1) * xtime(xtime(x))) ^ \ |
| 493 |
((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \ |
| 494 |
((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \ |
| 495 |
|
| 496 |
/* mix columns */ |
| 497 |
|
| 498 |
static void aes_decode_mix (aes_block block) |
| 499 |
{ |
| 500 |
int i; |
| 501 |
uint8_t a, b, c, d; |
| 502 |
|
| 503 |
for(i = 0; i < 4; ++i) |
| 504 |
{ |
| 505 |
a = block[i][0]; |
| 506 |
b = block[i][1]; |
| 507 |
c = block[i][2]; |
| 508 |
d = block[i][3]; |
| 509 |
block[i][0] = multiply(a, 0x0e) ^ multiply(b, 0x0b) ^ multiply(c, 0x0d) ^ multiply(d, 0x09); |
| 510 |
block[i][1] = multiply(a, 0x09) ^ multiply(b, 0x0e) ^ multiply(c, 0x0b) ^ multiply(d, 0x0d); |
| 511 |
block[i][2] = multiply(a, 0x0d) ^ multiply(b, 0x09) ^ multiply(c, 0x0e) ^ multiply(d, 0x0b); |
| 512 |
block[i][3] = multiply(a, 0x0b) ^ multiply(b, 0x0d) ^ multiply(c, 0x09) ^ multiply(d, 0x0e); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
/* inverse substitution */ |
| 517 |
|
| 518 |
static void aes_decode_sub (aes_block block) |
| 519 |
{ |
| 520 |
uint8_t i, j, v; |
| 521 |
for(i = 0; i < 4; ++i) |
| 522 |
for(j = 0; j < 4; ++j) |
| 523 |
v = block[i][j], block[i][j] = rsbox[v]; |
| 524 |
} |
| 525 |
|
| 526 |
/* inverse shift rows */ |
| 527 |
|
| 528 |
static void aes_decode_shift (aes_block block) |
| 529 |
{ |
| 530 |
uint8_t tmp; |
| 531 |
|
| 532 |
/* 1st row rotated once right */ |
| 533 |
tmp = block[3][1]; |
| 534 |
block[3][1] = block[2][1]; |
| 535 |
block[2][1] = block[1][1]; |
| 536 |
block[1][1] = block[0][1]; |
| 537 |
block[0][1] = tmp; |
| 538 |
|
| 539 |
/* 2st row rotated twice right */ |
| 540 |
tmp = block[0][2]; |
| 541 |
block[0][2] = block[2][2]; |
| 542 |
block[2][2] = tmp; |
| 543 |
tmp = block[1][2]; |
| 544 |
block[1][2] = block[3][2]; |
| 545 |
block[3][2] = tmp; |
| 546 |
|
| 547 |
/* 3rd row rotated 3 times right */ |
| 548 |
tmp = block[0][3]; |
| 549 |
block[0][3] = block[1][3]; |
| 550 |
block[1][3] = block[2][3]; |
| 551 |
block[2][3] = block[3][3]; |
| 552 |
block[3][3] = tmp; |
| 553 |
} |
| 554 |
|
| 555 |
/* aes block encoder */ |
| 556 |
|
| 557 |
static void aes_encode_cipher (aes_state *state) |
| 558 |
{ |
| 559 |
uint8_t round; |
| 560 |
aes_add_key(state->block, state->keyblock, 0); |
| 561 |
for (round = 1; round < state->rounds; ++round) |
| 562 |
{ |
| 563 |
aes_encode_sub(state->block); |
| 564 |
aes_encode_shift(state->block); |
| 565 |
aes_encode_mix(state->block); |
| 566 |
aes_add_key(state->block, state->keyblock, round); |
| 567 |
} |
| 568 |
aes_encode_sub(state->block); |
| 569 |
aes_encode_shift(state->block); |
| 570 |
aes_add_key(state->block, state->keyblock, state->rounds); |
| 571 |
} |
| 572 |
|
| 573 |
/* aes block decoder */ |
| 574 |
|
| 575 |
static void aes_decode_cipher (aes_state *state) |
| 576 |
{ |
| 577 |
uint8_t round; |
| 578 |
aes_add_key(state->block, state->keyblock, state->rounds); |
| 579 |
for(round = state->rounds - 1; round > 0; --round) |
| 580 |
{ |
| 581 |
aes_decode_shift(state->block); |
| 582 |
aes_decode_sub(state->block); |
| 583 |
aes_add_key(state->block, state->keyblock, round); |
| 584 |
aes_decode_mix(state->block); |
| 585 |
} |
| 586 |
aes_decode_shift(state->block); |
| 587 |
aes_decode_sub(state->block); |
| 588 |
aes_add_key(state->block, state->keyblock, 0); |
| 589 |
} |
| 590 |
|
| 591 |
/* tail block padding; RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0; pdf spec p. 119 */ |
| 592 |
|
| 593 |
#define aes_padding(state) ((state->flags & AES_NULL_PADDING) == 0) |
| 594 |
|
| 595 |
static void aes_put_padding (aes_state *state, uint8_t length) |
| 596 |
{ |
| 597 |
uint8_t pad; |
| 598 |
pad = (aes_padding(state)) ? 16 - length : 0; |
| 599 |
for (; length < 16; ++length) |
| 600 |
state->data[length] = state->iv[length] ^ pad; |
| 601 |
} |
| 602 |
|
| 603 |
static int aes_remove_padding (aes_state *state, uint8_t *data, uint8_t *length) |
| 604 |
{ |
| 605 |
uint8_t pad; |
| 606 |
*length = 16; /* block length 16 means leave intact */ |
| 607 |
if (aes_padding(state)) |
| 608 |
{ |
| 609 |
pad = data[16 - 1]; |
| 610 |
if (pad > 16) |
| 611 |
return IOFERR; |
| 612 |
for ( ; *length > 16 - pad; --(*length)) |
| 613 |
if (data[*length - 1] != pad) |
| 614 |
return IOFERR; |
| 615 |
} |
| 616 |
else |
| 617 |
{ |
| 618 |
for ( ; *length > 0; --(*length)) |
| 619 |
if (data[*length - 1] != '\0') |
| 620 |
break; |
| 621 |
} |
| 622 |
return IOFEOF; |
| 623 |
} |
| 624 |
|
| 625 |
/* aes codec */ |
| 626 |
|
| 627 |
/* make the cipher on input xor-ed with iv, save the output as a new iv, write the output */ |
| 628 |
#define aes_encode_output(state, output) \ |
| 629 |
(aes_encode_cipher(state), aes_copy_block(state->iv, state->data), aes_copy_block(output, state->data), output += 16) |
| 630 |
|
| 631 |
iof_status aes_encode_state (iof *I, iof *O, aes_state *state) |
| 632 |
{ |
| 633 |
int c; |
| 634 |
|
| 635 |
if (!(state->flags & AES_HAS_IV)) // weird |
| 636 |
return IOFERR; |
| 637 |
if ((state->flags & AES_INLINE_IV) && !(state->flags & AES_CONTINUE)) |
| 638 |
{ /* write iv at the beginning of encrypted data */ |
| 639 |
if (!iof_ensure(O, 16)) |
| 640 |
return IOFFULL; |
| 641 |
aes_copy_block(O->pos, state->iv); |
| 642 |
O->pos += 16; |
| 643 |
state->flags |= AES_CONTINUE; |
| 644 |
} |
| 645 |
while (iof_ensure(O, 16)) |
| 646 |
{ |
| 647 |
while (state->buffered < 16) |
| 648 |
{ |
| 649 |
if ((c = iof_get(I)) != IOFEOF) |
| 650 |
{ /* get input byte XORed with iv */ |
| 651 |
state->data[state->buffered] = state->iv[state->buffered] ^ ((uint8_t)c); |
| 652 |
++state->buffered; |
| 653 |
} |
| 654 |
else |
| 655 |
{ |
| 656 |
if (state->flush) |
| 657 |
{ |
| 658 |
if (state->buffered > 0 || aes_padding(state)) |
| 659 |
{ /* pad the last input chunk; for input divisable by 16, add 16 bytes 0x0f */ |
| 660 |
aes_put_padding(state, state->buffered); |
| 661 |
state->buffered = 16; |
| 662 |
aes_encode_output(state, O->pos); |
| 663 |
} |
| 664 |
return IOFEOF; |
| 665 |
} |
| 666 |
else |
| 667 |
return IOFEMPTY; |
| 668 |
} |
| 669 |
} |
| 670 |
aes_encode_output(state, O->pos); |
| 671 |
state->buffered = 0; |
| 672 |
} |
| 673 |
return IOFFULL; |
| 674 |
} |
| 675 |
|
| 676 |
/* write iv to the output, save the raw input just buffered as iv for the next chunk, make the cipher, write out xoring with iv */ |
| 677 |
#define aes_decode_output(state, output) \ |
| 678 |
(aes_copy_block(output, state->iv), aes_copy_block(state->iv, state->data), aes_decode_cipher(state), aes_copy_cbc(output, state->data), output += 16) |
| 679 |
|
| 680 |
iof_status aes_decode_state (iof *I, iof *O, aes_state *state) |
| 681 |
{ |
| 682 |
int c, ret; |
| 683 |
uint8_t lastlength; |
| 684 |
|
| 685 |
if ((state->flags & AES_INLINE_IV) && !(state->flags & AES_CONTINUE)) |
| 686 |
{ |
| 687 |
while (state->buffered < 16) |
| 688 |
{ |
| 689 |
if ((c = iof_get(I)) != IOFEOF) |
| 690 |
state->iv[state->buffered++] = (uint8_t)c; |
| 691 |
else |
| 692 |
return state->flush ? IOFERR : IOFEMPTY; |
| 693 |
} |
| 694 |
state->flags |= AES_CONTINUE|AES_HAS_IV; |
| 695 |
state->buffered = 0; |
| 696 |
} |
| 697 |
while (iof_ensure(O, 16)) |
| 698 |
{ |
| 699 |
while (state->buffered < 16) |
| 700 |
{ |
| 701 |
if ((c = iof_get(I)) != IOFEOF) |
| 702 |
state->data[state->buffered++] = (uint8_t)c; |
| 703 |
else |
| 704 |
return state->flush ? IOFERR : IOFEMPTY; |
| 705 |
} |
| 706 |
aes_decode_output(state, O->pos); |
| 707 |
if (state->flush) |
| 708 |
{ /* we have to check for EOF here, to remove eventual padding */ |
| 709 |
if ((c = iof_get(I)) < 0) |
| 710 |
{ /* end of input at 16-bytes boundary; remove padding and quit */ |
| 711 |
ret = aes_remove_padding(state, O->pos - 16, &lastlength); |
| 712 |
O->pos -= 16 - lastlength; |
| 713 |
return ret; |
| 714 |
} |
| 715 |
else |
| 716 |
{ /* beginning of the next block */ |
| 717 |
state->buffered = 1; |
| 718 |
state->data[0] = (uint8_t)c; |
| 719 |
} |
| 720 |
} |
| 721 |
else |
| 722 |
state->buffered = 0; |
| 723 |
} |
| 724 |
return IOFFULL; |
| 725 |
} |
| 726 |
|
| 727 |
/* variants that works on c-strings; can work inplace (output==input) except encoder in pdf flavour */ |
| 728 |
|
| 729 |
/* |
| 730 |
Codecs operating on c-string can generally work inplace (output==input), except encoder with AES_INLINE_IV flag set, |
| 731 |
which outputs 16 bytes of initialization vector at the beginning of encrypted data. All return the size of encrypted/decrypted |
| 732 |
data. Encoders output is the original length padded to a complete 16 bytes (plus eventual 16 bytes of initialization |
| 733 |
vector, if AES_INLINE_IV is used). Default padding is unambiguously removed during decryption. AES_NULL_PADDING flag |
| 734 |
forces using (ambiguous) NULL-byte padding, only if input length module 16 is greater then zero. |
| 735 |
|
| 736 |
An input data is supposed to be a complete data to be encrypted or decrypted. It is possible, however, to use those |
| 737 |
codecs for scaterred data chunks by manipulating AES_INLINE_IV, AES_NULL_PADDING, AES_CONTINUE flags and data length. |
| 738 |
Caller may assume that c-string codecs do not modify state flags. |
| 739 |
|
| 740 |
Encoder could actually be optimized by writing an initialization vector to a state block once. After every chunk encryption, |
| 741 |
the output is the initialization vector for the next chunk. Since we use c-string codec variants on short strings, |
| 742 |
the gain is neglectable in comparison with the weight of the aes crypt procedure. |
| 743 |
*/ |
| 744 |
|
| 745 |
size_t aes_encode_data (const void *input, size_t length, void *output, const void *key, size_t keylength, const void *iv, int flags) |
| 746 |
{ |
| 747 |
aes_state state; |
| 748 |
aes_keyblock keyblock; |
| 749 |
|
| 750 |
if (aes_encode_initialize(&state, &keyblock, key, keylength, iv) == NULL) |
| 751 |
return 0; |
| 752 |
state.flags |= flags; |
| 753 |
return aes_encode_state_data(&state, input, length, output); |
| 754 |
// aes_state_close(&state); |
| 755 |
} |
| 756 |
|
| 757 |
size_t aes_encode_state_data (aes_state *state, const void *input, size_t length, void *output) |
| 758 |
{ |
| 759 |
const uint8_t *inp; |
| 760 |
uint8_t *out, tail, t; |
| 761 |
size_t size; |
| 762 |
|
| 763 |
inp = (const uint8_t *)input; |
| 764 |
out = (uint8_t *)output; |
| 765 |
|
| 766 |
if (!(state->flags & AES_HAS_IV)) |
| 767 |
return 0; |
| 768 |
if ((state->flags & AES_INLINE_IV) && !(state->flags & AES_CONTINUE)) |
| 769 |
{ |
| 770 |
aes_copy_block(out, state->iv); |
| 771 |
out += 16; |
| 772 |
} |
| 773 |
// state->flags |= AES_CONTINUE; // do not modify state flags |
| 774 |
|
| 775 |
for (size = 0; size + 16 <= length; size += 16) |
| 776 |
{ |
| 777 |
aes_copy_xor(state->data, inp, state->iv); |
| 778 |
aes_encode_output(state, out); |
| 779 |
inp += 16; |
| 780 |
} |
| 781 |
|
| 782 |
if ((tail = (length % 16)) > 0 || aes_padding(state)) |
| 783 |
{ |
| 784 |
for (t = 0; t < tail; ++t) |
| 785 |
state->data[t] = inp[t] ^ state->iv[t]; |
| 786 |
aes_put_padding(state, tail); |
| 787 |
aes_encode_output(state, out); |
| 788 |
size += 16; |
| 789 |
} |
| 790 |
if (state->flags & AES_INLINE_IV) |
| 791 |
size += 16; /* iv written at the beginning of encoded data */ |
| 792 |
|
| 793 |
return size; |
| 794 |
} |
| 795 |
|
| 796 |
size_t aes_decode_data (const void *input, size_t length, void *output, const void *key, size_t keylength, const void *iv, int flags) |
| 797 |
{ |
| 798 |
aes_state state; |
| 799 |
aes_keyblock keyblock; |
| 800 |
|
| 801 |
if (aes_decode_initialize(&state, &keyblock, key, keylength, iv) == NULL) |
| 802 |
return 0; |
| 803 |
state.flags |= flags; |
| 804 |
return aes_decode_state_data(&state, input, length, output); |
| 805 |
// aes_state_close(&state); |
| 806 |
} |
| 807 |
|
| 808 |
size_t aes_decode_state_data (aes_state *state, const void *input, size_t length, void *output) |
| 809 |
{ |
| 810 |
const uint8_t *inp; |
| 811 |
uint8_t *out, lastlength; |
| 812 |
size_t size; |
| 813 |
|
| 814 |
inp = (const uint8_t *)input; |
| 815 |
out = (uint8_t *)output; |
| 816 |
|
| 817 |
if ((state->flags & AES_INLINE_IV) && !(state->flags & AES_CONTINUE)) |
| 818 |
{ |
| 819 |
aes_copy_block(state->iv, inp); |
| 820 |
// state->flags |= AES_HAS_IV; // do not modify state flags |
| 821 |
inp += 16; |
| 822 |
length = length >= 16 ? length - 16 : 0; |
| 823 |
} |
| 824 |
else if (!(state->flags & AES_HAS_IV)) |
| 825 |
return 0; |
| 826 |
// state->flags |= AES_CONTINUE; // do not modify state flags |
| 827 |
for (size = 0; size + 16 <= length; size += 16) |
| 828 |
{ |
| 829 |
aes_copy_block(state->data, inp); |
| 830 |
aes_decode_output(state, out); |
| 831 |
inp += 16; |
| 832 |
} |
| 833 |
|
| 834 |
if (size >= 16) |
| 835 |
{ |
| 836 |
aes_remove_padding(state, out - 16, &lastlength); |
| 837 |
size = size - 16 + lastlength; |
| 838 |
} |
| 839 |
|
| 840 |
return size; |
| 841 |
} |
| 842 |
|
| 843 |
/* |
| 844 |
pseudo-random bytes chain exceprted from eexec; not expected to have strong cryptographic properties |
| 845 |
we only expect that it is (reasonably) unique and different for each call (not only function call, but also |
| 846 |
a program call). A current trick with mangling pointer value gives satisfactory results, generally different |
| 847 |
for every function call and a programm call. Note that the pseudo-input bytes starts from some inner address |
| 848 |
bits, as they vary better; without that, the first byte tends to be "lazy". |
| 849 |
*/ |
| 850 |
|
| 851 |
void random_bytes (uint8_t *output, size_t size) |
| 852 |
{ |
| 853 |
size_t i; |
| 854 |
uint8_t p; |
| 855 |
static uint16_t k = 55665; |
| 856 |
for (i = 0; i < size; ++i) |
| 857 |
{ |
| 858 |
p = ((uint8_t *)(&output))[(i + 2) % sizeof(uint8_t *)] ^ (uint8_t)size; // pseudo input byte ;) |
| 859 |
k = (((p + k) * 52845 + 22719) & 65535); // xor-ed with pseudo-random sequence (kept between calls) |
| 860 |
output[i] = p ^ (k >> 8); |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
void aes_generate_iv (uint8_t output[16]) |
| 865 |
{ |
| 866 |
random_bytes(output, 16); |
| 867 |
} |
| 868 |
|
| 869 |
/* filters */ |
| 870 |
|
| 871 |
// rc4 decoder function |
| 872 |
|
| 873 |
static size_t rc4_decoder (iof *F, iof_mode mode) |
| 874 |
{ |
| 875 |
rc4_state *state; |
| 876 |
iof_status status; |
| 877 |
size_t tail; |
| 878 |
|
| 879 |
state = iof_filter_state(rc4_state *, F); |
| 880 |
switch(mode) |
| 881 |
{ |
| 882 |
case IOFLOAD: |
| 883 |
case IOFREAD: |
| 884 |
if (F->flags & IOF_STOPPED) |
| 885 |
return 0; |
| 886 |
tail = iof_tail(F); |
| 887 |
F->pos = F->buf + tail; |
| 888 |
F->end = F->buf + F->space; |
| 889 |
do { |
| 890 |
status = rc4_decode_state(F->next, F, state); |
| 891 |
} while (mode == IOFLOAD && status == IOFFULL && iof_resize_buffer(F)); |
| 892 |
return iof_decoder_retval(F, "rc4", status); |
| 893 |
case IOFCLOSE: |
| 894 |
rc4_state_close(state); |
| 895 |
iof_free(F); |
| 896 |
return 0; |
| 897 |
default: |
| 898 |
break; |
| 899 |
} |
| 900 |
return 0; |
| 901 |
} |
| 902 |
|
| 903 |
// rc4 encoder function |
| 904 |
|
| 905 |
static size_t rc4_encoder (iof *F, iof_mode mode) |
| 906 |
{ |
| 907 |
rc4_state *state; |
| 908 |
iof_status status; |
| 909 |
|
| 910 |
state = iof_filter_state(rc4_state *, F); |
| 911 |
switch (mode) |
| 912 |
{ |
| 913 |
case IOFFLUSH: |
| 914 |
state->flush = 1; |
| 915 |
FALLTHRU // fall through |
| 916 |
case IOFWRITE: |
| 917 |
F->end = F->pos; |
| 918 |
F->pos = F->buf; |
| 919 |
status = rc4_encode_state(F, F->next, state); |
| 920 |
return iof_encoder_retval(F, "rc4", status); |
| 921 |
case IOFCLOSE: |
| 922 |
if (!state->flush) |
| 923 |
rc4_encoder(F, IOFFLUSH); |
| 924 |
rc4_state_close(state); |
| 925 |
iof_free(F); |
| 926 |
return 0; |
| 927 |
default: |
| 928 |
break; |
| 929 |
} |
| 930 |
return 0; |
| 931 |
} |
| 932 |
|
| 933 |
// aes decoder function |
| 934 |
|
| 935 |
static size_t aes_decoder (iof *F, iof_mode mode) |
| 936 |
{ |
| 937 |
aes_state *state; |
| 938 |
iof_status status; |
| 939 |
size_t tail; |
| 940 |
|
| 941 |
state = iof_filter_state(aes_state *, F); |
| 942 |
switch(mode) |
| 943 |
{ |
| 944 |
case IOFLOAD: |
| 945 |
case IOFREAD: |
| 946 |
if (F->flags & IOF_STOPPED) |
| 947 |
return 0; |
| 948 |
tail = iof_tail(F); |
| 949 |
F->pos = F->buf + tail; |
| 950 |
F->end = F->buf + F->space; |
| 951 |
do { |
| 952 |
status = aes_decode_state(F->next, F, state); |
| 953 |
} while (mode == IOFLOAD && status == IOFFULL && iof_resize_buffer(F)); |
| 954 |
return iof_decoder_retval(F, "aes", status); |
| 955 |
case IOFCLOSE: |
| 956 |
aes_state_close(state); |
| 957 |
iof_free(F); |
| 958 |
return 0; |
| 959 |
default: |
| 960 |
break; |
| 961 |
} |
| 962 |
return 0; |
| 963 |
} |
| 964 |
|
| 965 |
// aes encoder function |
| 966 |
|
| 967 |
static size_t aes_encoder (iof *F, iof_mode mode) |
| 968 |
{ |
| 969 |
aes_state *state; |
| 970 |
iof_status status; |
| 971 |
|
| 972 |
state = iof_filter_state(aes_state *, F); |
| 973 |
switch (mode) |
| 974 |
{ |
| 975 |
case IOFFLUSH: |
| 976 |
state->flush = 1; |
| 977 |
FALLTHRU // fall through |
| 978 |
case IOFWRITE: |
| 979 |
F->end = F->pos; |
| 980 |
F->pos = F->buf; |
| 981 |
status = aes_encode_state(F, F->next, state); |
| 982 |
return iof_encoder_retval(F, "aes", status); |
| 983 |
case IOFCLOSE: |
| 984 |
if (!state->flush) |
| 985 |
aes_encoder(F, IOFFLUSH); |
| 986 |
aes_state_close(state); |
| 987 |
iof_free(F); |
| 988 |
return 0; |
| 989 |
default: |
| 990 |
break; |
| 991 |
} |
| 992 |
return 0; |
| 993 |
} |
| 994 |
|
| 995 |
iof * iof_filter_rc4_decoder (iof *N, const void *key, size_t keylength) |
| 996 |
{ |
| 997 |
iof *I; |
| 998 |
crypt_state_pointer P; |
| 999 |
|
| 1000 |
I = iof_filter_reader(rc4_decoder, sizeof(rc4_state), &P.voidstate); |
| 1001 |
iof_setup_next(I, N); |
| 1002 |
if (rc4_state_init(P.rc4state, key, keylength) == NULL) |
| 1003 |
{ |
| 1004 |
iof_discard(I); |
| 1005 |
return NULL; |
| 1006 |
} |
| 1007 |
P.rc4state->flush = 1; |
| 1008 |
return I; |
| 1009 |
} |
| 1010 |
|
| 1011 |
iof * iof_filter_rc4_encoder (iof *N, const void *key, size_t keylength) |
| 1012 |
{ |
| 1013 |
iof *O; |
| 1014 |
crypt_state_pointer P; |
| 1015 |
|
| 1016 |
O = iof_filter_writer(rc4_encoder, sizeof(rc4_state), &P.voidstate); |
| 1017 |
iof_setup_next(O, N); |
| 1018 |
if (rc4_state_init(P.rc4state, key, keylength) == NULL) |
| 1019 |
{ |
| 1020 |
iof_discard(O); |
| 1021 |
return NULL; |
| 1022 |
} |
| 1023 |
// P.rc4state->flush = 1; |
| 1024 |
return O; |
| 1025 |
} |
| 1026 |
|
| 1027 |
/* aes crypt filters */ |
| 1028 |
|
| 1029 |
iof * iof_filter_aes_decoder (iof *N, const void *key, size_t keylength) |
| 1030 |
{ |
| 1031 |
iof *I; |
| 1032 |
crypt_state_pointer P; |
| 1033 |
|
| 1034 |
I = iof_filter_reader(aes_decoder, sizeof(aes_state), &P.voidstate); |
| 1035 |
iof_setup_next(I, N); |
| 1036 |
if (aes_decode_init(P.aesstate, key, keylength) == NULL) |
| 1037 |
{ |
| 1038 |
iof_discard(I); |
| 1039 |
return NULL; |
| 1040 |
} |
| 1041 |
aes_pdf_mode(P.aesstate); |
| 1042 |
P.aesstate->flush = 1; |
| 1043 |
return I; |
| 1044 |
} |
| 1045 |
|
| 1046 |
iof * iof_filter_aes_encoder (iof *N, const void *key, size_t keylength) |
| 1047 |
{ |
| 1048 |
iof *O; |
| 1049 |
crypt_state_pointer P; |
| 1050 |
|
| 1051 |
O = iof_filter_writer(aes_encoder, sizeof(aes_state), &P.voidstate); |
| 1052 |
iof_setup_next(O, N); |
| 1053 |
if (aes_encode_init(P.aesstate, key, keylength) == NULL) |
| 1054 |
{ |
| 1055 |
iof_discard(O); |
| 1056 |
return NULL; |
| 1057 |
} |
| 1058 |
aes_pdf_mode(P.aesstate); |
| 1059 |
// P.aesstate->flush = 1; |
| 1060 |
return O; |
| 1061 |
} |
| 1062 |
|
| 1063 |
/* test */ |
| 1064 |
|
| 1065 |
/* |
| 1066 |
static void show (void *p, size_t size, uint8_t round, uint8_t sym) |
| 1067 |
{ |
| 1068 |
uint8_t i; |
| 1069 |
printf("%c%c:", round, sym); |
| 1070 |
for (i = 0; i < size; ++i) |
| 1071 |
printf("%02x", ((uint8_t *)p)[i]); |
| 1072 |
printf("\n"); |
| 1073 |
} |
| 1074 |
|
| 1075 |
void aes_test (void) |
| 1076 |
{ |
| 1077 |
const uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; |
| 1078 |
const uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; |
| 1079 |
const uint8_t inp[] = { |
| 1080 |
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, |
| 1081 |
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, |
| 1082 |
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, |
| 1083 |
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 }; |
| 1084 |
const uint8_t out[] = { |
| 1085 |
0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d, |
| 1086 |
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2, |
| 1087 |
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16, |
| 1088 |
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 }; |
| 1089 |
|
| 1090 |
uint8_t input[64], output[64]; |
| 1091 |
size_t inpsize, outsize; |
| 1092 |
int flags = AES_NULL_PADDING; |
| 1093 |
|
| 1094 |
//////////////////////////////////////////////////////////////////////////// |
| 1095 |
|
| 1096 |
//#define ENCODETO output |
| 1097 |
#define ENCODETO input // inplace |
| 1098 |
|
| 1099 |
inpsize = 64; |
| 1100 |
memcpy(input, inp, inpsize); |
| 1101 |
show(input, inpsize, '>', '>'); |
| 1102 |
outsize = aes_encode_data(input, inpsize, ENCODETO, key, 16, iv, flags); |
| 1103 |
show(ENCODETO, outsize, '<', '<'); |
| 1104 |
if (outsize == inpsize && memcmp(ENCODETO, out, outsize) == 0) |
| 1105 |
printf("ENCODER SUCCESS\n"); |
| 1106 |
else |
| 1107 |
printf("ENCODER FAILURE\n"); |
| 1108 |
|
| 1109 |
//////////////////////////////////////////////////////////////////////////// |
| 1110 |
|
| 1111 |
//#define DECODETO input |
| 1112 |
#define DECODETO output // in place |
| 1113 |
|
| 1114 |
outsize = 64; |
| 1115 |
memcpy(output, out, outsize); |
| 1116 |
show(output, outsize, '<', '<'); |
| 1117 |
inpsize = aes_decode_data(output, outsize, DECODETO, key, 16, iv, flags); |
| 1118 |
show(DECODETO, inpsize, '>', '>'); |
| 1119 |
if (inpsize == outsize && memcmp(DECODETO, inp, inpsize) == 0) |
| 1120 |
printf("DECODER SUCCESS\n"); |
| 1121 |
else |
| 1122 |
printf("DECODER FAILURE\n"); |
| 1123 |
} |
| 1124 |
*/ |
| 1125 |
|
| 1126 |
/* |
| 1127 |
Some example vectors |
| 1128 |
|
| 1129 |
================================ AES ECB 128-bit encryption mode ================================ |
| 1130 |
|
| 1131 |
Encryption key: 2b7e151628aed2a6abf7158809cf4f3c |
| 1132 |
|
| 1133 |
Test vector Cipher text |
| 1134 |
6bc1bee22e409f96e93d7e117393172a 3ad77bb40d7a3660a89ecaf32466ef97 |
| 1135 |
ae2d8a571e03ac9c9eb76fac45af8e51 f5d3d58503b9699de785895a96fdbaaf |
| 1136 |
30c81c46a35ce411e5fbc1191a0a52ef 43b1cd7f598ece23881b00e3ed030688 |
| 1137 |
f69f2445df4f9b17ad2b417be66c3710 7b0c785e27e8ad3f8223207104725dd4 |
| 1138 |
|
| 1139 |
|
| 1140 |
================================ AES ECB 192-bit encryption mode ================================ |
| 1141 |
|
| 1142 |
Encryption key: 8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b |
| 1143 |
|
| 1144 |
Test vector Cipher text |
| 1145 |
6bc1bee22e409f96e93d7e117393172a bd334f1d6e45f25ff712a214571fa5cc |
| 1146 |
ae2d8a571e03ac9c9eb76fac45af8e51 974104846d0ad3ad7734ecb3ecee4eef |
| 1147 |
30c81c46a35ce411e5fbc1191a0a52ef ef7afd2270e2e60adce0ba2face6444e |
| 1148 |
f69f2445df4f9b17ad2b417be66c3710 9a4b41ba738d6c72fb16691603c18e0e |
| 1149 |
|
| 1150 |
|
| 1151 |
================================ AES ECB 256-bit encryption mode ================================ |
| 1152 |
|
| 1153 |
Encryption key: 603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4 |
| 1154 |
|
| 1155 |
Test vector Cipher text |
| 1156 |
6bc1bee22e409f96e93d7e117393172a f3eed1bdb5d2a03c064b5a7e3db181f8 |
| 1157 |
ae2d8a571e03ac9c9eb76fac45af8e51 591ccb10d410ed26dc5ba74a31362870 |
| 1158 |
30c81c46a35ce411e5fbc1191a0a52ef b6ed21b99ca6f4f9f153e7b1beafed1d |
| 1159 |
f69f2445df4f9b17ad2b417be66c3710 23304b7a39f9f3ff067d8d8f9e24ecc7 |
| 1160 |
|
| 1161 |
================================ AES CBC 128-bit encryption mode ================================ |
| 1162 |
|
| 1163 |
Encryption key: 2b7e151628aed2a6abf7158809cf4f3c |
| 1164 |
|
| 1165 |
Initialization vector Test vector Cipher text |
| 1166 |
000102030405060708090A0B0C0D0E0F 6bc1bee22e409f96e93d7e117393172a 7649abac8119b246cee98e9b12e9197d |
| 1167 |
7649ABAC8119B246CEE98E9B12E9197D ae2d8a571e03ac9c9eb76fac45af8e51 5086cb9b507219ee95db113a917678b2 |
| 1168 |
5086CB9B507219EE95DB113A917678B2 30c81c46a35ce411e5fbc1191a0a52ef 73bed6b8e3c1743b7116e69e22229516 |
| 1169 |
73BED6B8E3C1743B7116E69E22229516 f69f2445df4f9b17ad2b417be66c3710 3ff1caa1681fac09120eca307586e1a7 |
| 1170 |
|
| 1171 |
================================ AES CBC 192-bit encryption mode ================================ |
| 1172 |
|
| 1173 |
Encryption key: 8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b |
| 1174 |
|
| 1175 |
Initialization vector Test vector Cipher text |
| 1176 |
000102030405060708090A0B0C0D0E0F 6bc1bee22e409f96e93d7e117393172a 4f021db243bc633d7178183a9fa071e8 |
| 1177 |
4F021DB243BC633D7178183A9FA071E8 ae2d8a571e03ac9c9eb76fac45af8e51 b4d9ada9ad7dedf4e5e738763f69145a |
| 1178 |
B4D9ADA9AD7DEDF4E5E738763F69145A 30c81c46a35ce411e5fbc1191a0a52ef 571b242012fb7ae07fa9baac3df102e0 |
| 1179 |
571B242012FB7AE07FA9BAAC3DF102E0 f69f2445df4f9b17ad2b417be66c3710 08b0e27988598881d920a9e64f5615cd |
| 1180 |
|
| 1181 |
================================ AES CBC 256-bit encryption mode ================================ |
| 1182 |
|
| 1183 |
Encryption key: 603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4 |
| 1184 |
|
| 1185 |
Initialization vector Test vector Cipher text |
| 1186 |
000102030405060708090A0B0C0D0E0F 6bc1bee22e409f96e93d7e117393172a f58c4c04d6e5f1ba779eabfb5f7bfbd6 |
| 1187 |
F58C4C04D6E5F1BA779EABFB5F7BFBD6 ae2d8a571e03ac9c9eb76fac45af8e51 9cfc4e967edb808d679f777bc6702c7d |
| 1188 |
9CFC4E967EDB808D679F777BC6702C7D 30c81c46a35ce411e5fbc1191a0a52ef 39f23369a9d9bacfa530e26304231461 |
| 1189 |
39F23369A9D9BACFA530E26304231461 f69f2445df4f9b17ad2b417be66c3710 b2eb05e2c39be9fcda6c19078c6a9d1b |
| 1190 |
*/ |