| 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 |
#include "common.h" |
| 33 |
#include "endianess.h" |
| 34 |
|
| 35 |
FAKE_INIT(chacha20) |
| 36 |
|
| 37 |
#define KEY_SIZE 32 |
| 38 |
|
| 39 |
typedef struct { |
| 40 |
|
| 41 |
uint32_t h[16]; |
| 42 |
size_t nonceSize; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
unsigned usedKeyStream; |
| 48 |
|
| 49 |
uint8_t keyStream[sizeof(uint32_t)*16]; |
| 50 |
} stream_state; |
| 51 |
|
| 52 |
#define ROTL(q, n) (((q) << (n)) | ((q) >> (32 - (n)))) |
| 53 |
|
| 54 |
#define QR(a, b, c, d) {\ |
| 55 |
a+=b; d^=a; d=ROTL(d,16); \ |
| 56 |
c+=d; b^=c; b=ROTL(b,12); \ |
| 57 |
a+=b; d^=a; d=ROTL(d,8); \ |
| 58 |
c+=d; b^=c; b=ROTL(b,7); \ |
| 59 |
} |
| 60 |
|
| 61 |
EXPORT_SYM int chacha20_init(stream_state **pState, |
| 62 |
const uint8_t *key, |
| 63 |
size_t keySize, |
| 64 |
const uint8_t *nonce, |
| 65 |
size_t nonceSize) |
| 66 |
{ |
| 67 |
stream_state *hs; |
| 68 |
unsigned i; |
| 69 |
|
| 70 |
if (NULL == pState || NULL == nonce) |
| 71 |
return ERR_NULL; |
| 72 |
|
| 73 |
if (NULL == key || keySize != KEY_SIZE) |
| 74 |
return ERR_KEY_SIZE; |
| 75 |
|
| 76 |
if (nonceSize != 8 && nonceSize != 12 && nonceSize != 16) |
| 77 |
return ERR_NONCE_SIZE; |
| 78 |
|
| 79 |
*pState = hs = (stream_state*) calloc(1, sizeof(stream_state)); |
| 80 |
if (NULL == hs) |
| 81 |
return ERR_MEMORY; |
| 82 |
|
| 83 |
hs->h[0] = 0x61707865; |
| 84 |
hs->h[1] = 0x3320646e; |
| 85 |
hs->h[2] = 0x79622d32; |
| 86 |
hs->h[3] = 0x6b206574; |
| 87 |
|
| 88 |
|
| 89 |
for (i=0; i<32/4; i++) { |
| 90 |
hs->h[4+i] = LOAD_U32_LITTLE(key + 4*i); |
| 91 |
} |
| 92 |
|
| 93 |
switch (nonceSize) { |
| 94 |
case 8: { |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
hs->h[14] = LOAD_U32_LITTLE(nonce + 0); |
| 107 |
hs->h[15] = LOAD_U32_LITTLE(nonce + 4); |
| 108 |
break; |
| 109 |
} |
| 110 |
case 12: { |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
hs->h[13] = LOAD_U32_LITTLE(nonce + 0); |
| 122 |
hs->h[14] = LOAD_U32_LITTLE(nonce + 4); |
| 123 |
hs->h[15] = LOAD_U32_LITTLE(nonce + 8); |
| 124 |
break; |
| 125 |
} |
| 126 |
case 16: { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
hs->h[12] = LOAD_U32_LITTLE(nonce + 0); |
| 137 |
hs->h[13] = LOAD_U32_LITTLE(nonce + 4); |
| 138 |
hs->h[14] = LOAD_U32_LITTLE(nonce + 8); |
| 139 |
hs->h[15] = LOAD_U32_LITTLE(nonce + 12); |
| 140 |
break; |
| 141 |
} |
| 142 |
default: |
| 143 |
return ERR_NONCE_SIZE; |
| 144 |
} |
| 145 |
|
| 146 |
hs->nonceSize = nonceSize; |
| 147 |
hs->usedKeyStream = sizeof hs->keyStream; |
| 148 |
|
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
EXPORT_SYM int chacha20_destroy(stream_state *state) |
| 153 |
{ |
| 154 |
if (NULL == state) |
| 155 |
return ERR_NULL; |
| 156 |
free(state); |
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
static int chacha20_core(stream_state *state, uint32_t h[16]) |
| 161 |
{ |
| 162 |
unsigned i; |
| 163 |
|
| 164 |
memcpy(h, state->h, sizeof state->h); |
| 165 |
|
| 166 |
for (i=0; i<10; i++) { |
| 167 |
|
| 168 |
QR(h[0], h[4], h[ 8], h[12]); |
| 169 |
QR(h[1], h[5], h[ 9], h[13]); |
| 170 |
QR(h[2], h[6], h[10], h[14]); |
| 171 |
QR(h[3], h[7], h[11], h[15]); |
| 172 |
|
| 173 |
QR(h[0], h[5], h[10], h[15]); |
| 174 |
QR(h[1], h[6], h[11], h[12]); |
| 175 |
QR(h[2], h[7], h[ 8], h[13]); |
| 176 |
QR(h[3], h[4], h[ 9], h[14]); |
| 177 |
} |
| 178 |
|
| 179 |
for (i=0; i<16; i++) { |
| 180 |
uint32_t sum; |
| 181 |
|
| 182 |
sum = h[i] + state->h[i]; |
| 183 |
STORE_U32_LITTLE(state->keyStream + 4*i, sum); |
| 184 |
} |
| 185 |
|
| 186 |
state->usedKeyStream = 0; |
| 187 |
|
| 188 |
switch (state->nonceSize) { |
| 189 |
case 8: { |
| 190 |
|
| 191 |
if (++state->h[12] == 0) { |
| 192 |
if (++state->h[13] == 0) { |
| 193 |
return ERR_MAX_DATA; |
| 194 |
} |
| 195 |
} |
| 196 |
break; |
| 197 |
} |
| 198 |
case 12: { |
| 199 |
|
| 200 |
if (++state->h[12] == 0) { |
| 201 |
return ERR_MAX_DATA; |
| 202 |
} |
| 203 |
break; |
| 204 |
} |
| 205 |
case 16: { |
| 206 |
|
| 207 |
|
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
EXPORT_SYM int chacha20_encrypt(stream_state *state, |
| 216 |
const uint8_t in[], |
| 217 |
uint8_t out[], |
| 218 |
size_t len) |
| 219 |
{ |
| 220 |
if (NULL == state || NULL == in || NULL == out) |
| 221 |
return ERR_NULL; |
| 222 |
|
| 223 |
if ((state->nonceSize != 8) && (state->nonceSize != 12)) |
| 224 |
return ERR_NONCE_SIZE; |
| 225 |
|
| 226 |
while (len>0) { |
| 227 |
unsigned keyStreamToUse; |
| 228 |
unsigned i; |
| 229 |
uint32_t h[16]; |
| 230 |
|
| 231 |
if (state->usedKeyStream == sizeof state->keyStream) { |
| 232 |
int result; |
| 233 |
|
| 234 |
result = chacha20_core(state, h); |
| 235 |
if (result) |
| 236 |
return result; |
| 237 |
} |
| 238 |
|
| 239 |
keyStreamToUse = (unsigned)MIN(len, sizeof state->keyStream - state->usedKeyStream); |
| 240 |
for (i=0; i<keyStreamToUse; i++) |
| 241 |
*out++ = *in++ ^ state->keyStream[i + state->usedKeyStream]; |
| 242 |
|
| 243 |
len -= keyStreamToUse; |
| 244 |
state->usedKeyStream += keyStreamToUse; |
| 245 |
} |
| 246 |
|
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
EXPORT_SYM int chacha20_seek(stream_state *state, |
| 251 |
unsigned long block_high, |
| 252 |
unsigned long block_low, |
| 253 |
unsigned offset) |
| 254 |
{ |
| 255 |
int result; |
| 256 |
uint32_t h[16]; |
| 257 |
|
| 258 |
if (NULL == state) |
| 259 |
return ERR_NULL; |
| 260 |
|
| 261 |
if ((state->nonceSize != 8) && (state->nonceSize != 12)) |
| 262 |
return ERR_NONCE_SIZE; |
| 263 |
|
| 264 |
if (offset >= sizeof state->keyStream) |
| 265 |
return ERR_MAX_OFFSET; |
| 266 |
|
| 267 |
if (state->nonceSize == 8) { |
| 268 |
|
| 269 |
state->h[12] = (uint32_t)block_low; |
| 270 |
state->h[13] = (uint32_t)block_high; |
| 271 |
} else { |
| 272 |
|
| 273 |
if (block_high > 0) { |
| 274 |
return ERR_MAX_OFFSET; |
| 275 |
} |
| 276 |
state->h[12] = (uint32_t)block_low; |
| 277 |
} |
| 278 |
|
| 279 |
result = chacha20_core(state, h); |
| 280 |
if (result) |
| 281 |
return result; |
| 282 |
|
| 283 |
state->usedKeyStream = offset; |
| 284 |
|
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
EXPORT_SYM int hchacha20(const uint8_t key[KEY_SIZE], |
| 292 |
const uint8_t nonce16[16], |
| 293 |
uint8_t subkey[KEY_SIZE]) |
| 294 |
{ |
| 295 |
stream_state *pState; |
| 296 |
uint32_t h[16]; |
| 297 |
|
| 298 |
if (NULL == key || NULL == nonce16 || NULL == subkey) { |
| 299 |
return ERR_NULL; |
| 300 |
} |
| 301 |
|
| 302 |
chacha20_init(&pState, key, KEY_SIZE, nonce16, 16); |
| 303 |
if (NULL == pState) |
| 304 |
return ERR_MEMORY; |
| 305 |
|
| 306 |
chacha20_core(pState, h); |
| 307 |
|
| 308 |
STORE_U32_LITTLE(subkey + 0, h[0]); |
| 309 |
STORE_U32_LITTLE(subkey + 4, h[1]); |
| 310 |
STORE_U32_LITTLE(subkey + 8, h[2]); |
| 311 |
STORE_U32_LITTLE(subkey + 12, h[3]); |
| 312 |
STORE_U32_LITTLE(subkey + 16, h[12]); |
| 313 |
STORE_U32_LITTLE(subkey + 20, h[13]); |
| 314 |
STORE_U32_LITTLE(subkey + 24, h[14]); |
| 315 |
STORE_U32_LITTLE(subkey + 28, h[15]); |
| 316 |
chacha20_destroy(pState); |
| 317 |
|
| 318 |
return 0; |
| 319 |
} |
| 320 |
|
| 321 |
#ifdef PROFILE |
| 322 |
int main(void) |
| 323 |
{ |
| 324 |
const unsigned data_size = 1024*1024; |
| 325 |
const uint8_t key[32] = "12345678901234561234567890123456"; |
| 326 |
const uint8_t nonce[8] = "12345678"; |
| 327 |
stream_state *state; |
| 328 |
uint8_t *data; |
| 329 |
|
| 330 |
data = malloc(data_size); |
| 331 |
for (int i=0; i<data_size; i++) { |
| 332 |
data[i] = (uint8_t) i; |
| 333 |
} |
| 334 |
|
| 335 |
chacha20_init(&state, key, 32, nonce, 8); |
| 336 |
|
| 337 |
for (int i=0; i<1024; i++) |
| 338 |
chacha20_encrypt(state, data, data, data_size); |
| 339 |
|
| 340 |
chacha20_destroy(state); |
| 341 |
free(data); |
| 342 |
} |
| 343 |
#endif |