diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-11-23 18:02:44 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-11-23 18:02:44 +0100 |
commit | 5e4236d226309a32842a6928878fd0e1cd5937e7 (patch) | |
tree | eb41a6c5cbaaac79b22b8c200e0aabfe26ba7d15 /networking/tls_aes.c | |
parent | 83e5c627e1b2c7f34d694696d0c3d5a3ce25dc59 (diff) | |
download | busybox-w32-5e4236d226309a32842a6928878fd0e1cd5937e7.tar.gz busybox-w32-5e4236d226309a32842a6928878fd0e1cd5937e7.tar.bz2 busybox-w32-5e4236d226309a32842a6928878fd0e1cd5937e7.zip |
tls: in AES-CBC code, do not set key for every record - do it once
function old new delta
aes_setkey 16 212 +196
tls_handshake 1941 1977 +36
aes_encrypt_1 382 396 +14
xwrite_encrypted 605 604 -1
tls_xread_record 659 656 -3
aes_encrypt_one_block 65 59 -6
aes_cbc_encrypt 172 121 -51
aesgcm_setkey 58 - -58
aes_cbc_decrypt 958 881 -77
KeyExpansion 188 - -188
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 3/5 up/down: 246/-384) Total: -138 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/tls_aes.c')
-rw-r--r-- | networking/tls_aes.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/networking/tls_aes.c b/networking/tls_aes.c index 4d2b68975..cf6b5fe3d 100644 --- a/networking/tls_aes.c +++ b/networking/tls_aes.c | |||
@@ -326,8 +326,11 @@ static void InvMixColumns(unsigned astate[16]) | |||
326 | } | 326 | } |
327 | } | 327 | } |
328 | 328 | ||
329 | static void aes_encrypt_1(unsigned astate[16], unsigned rounds, const uint32_t *RoundKey) | 329 | static void aes_encrypt_1(struct tls_aes *aes, unsigned astate[16]) |
330 | { | 330 | { |
331 | unsigned rounds = aes->rounds; | ||
332 | const uint32_t *RoundKey = aes->key; | ||
333 | |||
331 | for (;;) { | 334 | for (;;) { |
332 | AddRoundKey(astate, RoundKey); | 335 | AddRoundKey(astate, RoundKey); |
333 | RoundKey += 4; | 336 | RoundKey += 4; |
@@ -355,22 +358,19 @@ void FAST_FUNC aes_encrypt_one_block(struct tls_aes *aes, const void *data, void | |||
355 | 358 | ||
356 | for (i = 0; i < 16; i++) | 359 | for (i = 0; i < 16; i++) |
357 | astate[i] = pt[i]; | 360 | astate[i] = pt[i]; |
358 | aes_encrypt_1(astate, aes->rounds, aes->key); | 361 | aes_encrypt_1(aes, astate); |
359 | for (i = 0; i < 16; i++) | 362 | for (i = 0; i < 16; i++) |
360 | ct[i] = astate[i]; | 363 | ct[i] = astate[i]; |
361 | } | 364 | } |
362 | 365 | ||
363 | void FAST_FUNC aes_cbc_encrypt(const void *key, int klen, void *iv, const void *data, size_t len, void *dst) | 366 | void FAST_FUNC aes_cbc_encrypt(struct tls_aes *aes, void *iv, const void *data, size_t len, void *dst) |
364 | { | 367 | { |
365 | uint32_t RoundKey[60]; | ||
366 | uint8_t iv2[16]; | 368 | uint8_t iv2[16]; |
367 | unsigned rounds; | ||
368 | 369 | ||
369 | const uint8_t *pt = data; | 370 | const uint8_t *pt = data; |
370 | uint8_t *ct = dst; | 371 | uint8_t *ct = dst; |
371 | 372 | ||
372 | memcpy(iv2, iv, 16); | 373 | memcpy(iv2, iv, 16); |
373 | rounds = KeyExpansion(RoundKey, key, klen); | ||
374 | while (len > 0) { | 374 | while (len > 0) { |
375 | { | 375 | { |
376 | /* almost aes_encrypt_one_block(rounds, RoundKey, pt, ct); | 376 | /* almost aes_encrypt_one_block(rounds, RoundKey, pt, ct); |
@@ -381,7 +381,7 @@ void FAST_FUNC aes_cbc_encrypt(const void *key, int klen, void *iv, const void * | |||
381 | unsigned astate[16]; | 381 | unsigned astate[16]; |
382 | for (i = 0; i < 16; i++) | 382 | for (i = 0; i < 16; i++) |
383 | astate[i] = pt[i] ^ iv2[i]; | 383 | astate[i] = pt[i] ^ iv2[i]; |
384 | aes_encrypt_1(astate, rounds, RoundKey); | 384 | aes_encrypt_1(aes, astate); |
385 | for (i = 0; i < 16; i++) | 385 | for (i = 0; i < 16; i++) |
386 | iv2[i] = ct[i] = astate[i]; | 386 | iv2[i] = ct[i] = astate[i]; |
387 | } | 387 | } |
@@ -391,8 +391,11 @@ void FAST_FUNC aes_cbc_encrypt(const void *key, int klen, void *iv, const void * | |||
391 | } | 391 | } |
392 | } | 392 | } |
393 | 393 | ||
394 | static void aes_decrypt_1(unsigned astate[16], unsigned rounds, const uint32_t *RoundKey) | 394 | static void aes_decrypt_1(struct tls_aes *aes, unsigned astate[16]) |
395 | { | 395 | { |
396 | unsigned rounds = aes->rounds; | ||
397 | const uint32_t *RoundKey = aes->key; | ||
398 | |||
396 | RoundKey += rounds * 4; | 399 | RoundKey += rounds * 4; |
397 | AddRoundKey(astate, RoundKey); | 400 | AddRoundKey(astate, RoundKey); |
398 | for (;;) { | 401 | for (;;) { |
@@ -407,8 +410,10 @@ static void aes_decrypt_1(unsigned astate[16], unsigned rounds, const uint32_t * | |||
407 | } | 410 | } |
408 | 411 | ||
409 | #if 0 //UNUSED | 412 | #if 0 //UNUSED |
410 | static void aes_decrypt_one_block(unsigned rounds, const uint32_t *RoundKey, const void *data, void *dst) | 413 | static void aes_decrypt_one_block(struct tls_aes *aes, const void *data, void *dst) |
411 | { | 414 | { |
415 | unsigned rounds = aes->rounds; | ||
416 | const uint32_t *RoundKey = aes->key; | ||
412 | unsigned astate[16]; | 417 | unsigned astate[16]; |
413 | unsigned i; | 418 | unsigned i; |
414 | 419 | ||
@@ -417,25 +422,22 @@ static void aes_decrypt_one_block(unsigned rounds, const uint32_t *RoundKey, con | |||
417 | 422 | ||
418 | for (i = 0; i < 16; i++) | 423 | for (i = 0; i < 16; i++) |
419 | astate[i] = ct[i]; | 424 | astate[i] = ct[i]; |
420 | aes_decrypt_1(astate, rounds, RoundKey); | 425 | aes_decrypt_1(aes, astate); |
421 | for (i = 0; i < 16; i++) | 426 | for (i = 0; i < 16; i++) |
422 | pt[i] = astate[i]; | 427 | pt[i] = astate[i]; |
423 | } | 428 | } |
424 | #endif | 429 | #endif |
425 | 430 | ||
426 | void FAST_FUNC aes_cbc_decrypt(const void *key, int klen, void *iv, const void *data, size_t len, void *dst) | 431 | void FAST_FUNC aes_cbc_decrypt(struct tls_aes *aes, void *iv, const void *data, size_t len, void *dst) |
427 | { | 432 | { |
428 | uint32_t RoundKey[60]; | ||
429 | uint8_t iv2[16]; | 433 | uint8_t iv2[16]; |
430 | uint8_t iv3[16]; | 434 | uint8_t iv3[16]; |
431 | unsigned rounds; | ||
432 | uint8_t *ivbuf; | 435 | uint8_t *ivbuf; |
433 | uint8_t *ivnext; | 436 | uint8_t *ivnext; |
434 | 437 | ||
435 | const uint8_t *ct = data; | 438 | const uint8_t *ct = data; |
436 | uint8_t *pt = dst; | 439 | uint8_t *pt = dst; |
437 | 440 | ||
438 | rounds = KeyExpansion(RoundKey, key, klen); | ||
439 | ivbuf = memcpy(iv2, iv, 16); | 441 | ivbuf = memcpy(iv2, iv, 16); |
440 | while (len) { | 442 | while (len) { |
441 | ivnext = (ivbuf==iv2) ? iv3 : iv2; | 443 | ivnext = (ivbuf==iv2) ? iv3 : iv2; |
@@ -447,7 +449,7 @@ void FAST_FUNC aes_cbc_decrypt(const void *key, int klen, void *iv, const void * | |||
447 | unsigned astate[16]; | 449 | unsigned astate[16]; |
448 | for (i = 0; i < 16; i++) | 450 | for (i = 0; i < 16; i++) |
449 | ivnext[i] = astate[i] = ct[i]; | 451 | ivnext[i] = astate[i] = ct[i]; |
450 | aes_decrypt_1(astate, rounds, RoundKey); | 452 | aes_decrypt_1(aes, astate); |
451 | for (i = 0; i < 16; i++) | 453 | for (i = 0; i < 16; i++) |
452 | pt[i] = astate[i] ^ ivbuf[i]; | 454 | pt[i] = astate[i] ^ ivbuf[i]; |
453 | } | 455 | } |