crypto: Make aes_unwrap look similar to aes_wrap
Running basic performance testing on x86 shows new and old code within a few percent of each other. The main penalty is the AES AF_ALG invocation.
This commit is contained in:
parent
4f865ba9f8
commit
6d6fa57550
20
src/crypto.c
20
src/crypto.c
|
@ -125,11 +125,12 @@ bool cmac_aes(const void *key, size_t key_len,
|
||||||
bool aes_unwrap(const uint8_t *kek, const uint8_t *in, size_t len,
|
bool aes_unwrap(const uint8_t *kek, const uint8_t *in, size_t len,
|
||||||
uint8_t *out)
|
uint8_t *out)
|
||||||
{
|
{
|
||||||
uint8_t b[16];
|
uint64_t b[2];
|
||||||
uint8_t *r;
|
uint64_t *r;
|
||||||
size_t n = (len - 8) >> 3;
|
size_t n = (len - 8) >> 3;
|
||||||
int i, j;
|
int i, j;
|
||||||
struct l_cipher *cipher;
|
struct l_cipher *cipher;
|
||||||
|
uint64_t t = n * 6;
|
||||||
|
|
||||||
cipher = l_cipher_new(L_CIPHER_AES, kek, 16);
|
cipher = l_cipher_new(L_CIPHER_AES, kek, 16);
|
||||||
if (!cipher)
|
if (!cipher)
|
||||||
|
@ -141,22 +142,21 @@ bool aes_unwrap(const uint8_t *kek, const uint8_t *in, size_t len,
|
||||||
|
|
||||||
/* Unwrap */
|
/* Unwrap */
|
||||||
for (j = 5; j >= 0; j--) {
|
for (j = 5; j >= 0; j--) {
|
||||||
r = out + (n - 1) * 8;
|
r = (uint64_t *) out + n - 1;
|
||||||
|
|
||||||
for (i = n; i >= 1; i--) {
|
for (i = n; i >= 1; i--, t--) {
|
||||||
memcpy(b + 8, r, 8);
|
b[0] ^= L_CPU_TO_BE64(t);
|
||||||
b[7] ^= n * j + i;
|
b[1] = L_GET_UNALIGNED(r);
|
||||||
l_cipher_decrypt(cipher, b, b, 16);
|
l_cipher_decrypt(cipher, b, b, 16);
|
||||||
memcpy(r, b + 8, 8);
|
L_PUT_UNALIGNED(b[1], r);
|
||||||
r -= 8;
|
r -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
l_cipher_free(cipher);
|
l_cipher_free(cipher);
|
||||||
|
|
||||||
/* Check IV */
|
/* Check IV */
|
||||||
for (i = 0; i < 8; i++)
|
if (b[0] != 0xa6a6a6a6a6a6a6a6)
|
||||||
if (b[i] != 0xA6)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Reference in New Issue