2018-11-23 21:51:38 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Denys Vlasenko
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tls.h"
|
|
|
|
|
|
|
|
typedef uint8_t byte;
|
|
|
|
typedef uint32_t word32;
|
|
|
|
#define XMEMSET memset
|
|
|
|
#define XMEMCPY memcpy
|
|
|
|
|
2018-11-23 23:01:26 +05:30
|
|
|
/* from wolfssl-3.15.3/wolfcrypt/src/aes.c */
|
2018-11-23 21:51:38 +05:30
|
|
|
|
2019-10-25 16:35:15 +05:30
|
|
|
#ifdef UNUSED
|
2018-11-23 23:01:26 +05:30
|
|
|
static ALWAYS_INLINE void FlattenSzInBits(byte* buf, word32 sz)
|
2018-11-23 21:51:38 +05:30
|
|
|
{
|
|
|
|
/* Multiply the sz by 8 */
|
2018-11-23 23:01:26 +05:30
|
|
|
//bbox: these sizes are never even close to 2^32/8
|
|
|
|
// word32 szHi = (sz >> (8*sizeof(sz) - 3));
|
2018-11-23 21:51:38 +05:30
|
|
|
sz <<= 3;
|
|
|
|
|
|
|
|
/* copy over the words of the sz into the destination buffer */
|
2018-11-23 23:01:26 +05:30
|
|
|
// buf[0] = (szHi >> 24) & 0xff;
|
|
|
|
// buf[1] = (szHi >> 16) & 0xff;
|
|
|
|
// buf[2] = (szHi >> 8) & 0xff;
|
|
|
|
// buf[3] = szHi & 0xff;
|
2018-11-23 23:25:15 +05:30
|
|
|
*(uint32_t*)(buf + 0) = 0;
|
2018-11-23 23:01:26 +05:30
|
|
|
// buf[4] = (sz >> 24) & 0xff;
|
|
|
|
// buf[5] = (sz >> 16) & 0xff;
|
|
|
|
// buf[6] = (sz >> 8) & 0xff;
|
|
|
|
// buf[7] = sz & 0xff;
|
2018-11-23 23:25:15 +05:30
|
|
|
*(uint32_t*)(buf + 4) = SWAP_BE32(sz);
|
2018-11-23 21:51:38 +05:30
|
|
|
}
|
2019-10-25 16:35:15 +05:30
|
|
|
#endif
|
2018-11-23 21:51:38 +05:30
|
|
|
|
|
|
|
static void RIGHTSHIFTX(byte* x)
|
|
|
|
{
|
2018-12-08 18:04:43 +05:30
|
|
|
#define l ((unsigned long*)x)
|
|
|
|
#if 0
|
2018-11-23 21:51:38 +05:30
|
|
|
|
2018-12-08 18:04:43 +05:30
|
|
|
// Generic byte-at-a-time algorithm
|
|
|
|
int i;
|
|
|
|
byte carryIn = (x[15] & 0x01) ? 0xE1 : 0;
|
2018-11-23 21:51:38 +05:30
|
|
|
for (i = 0; i < AES_BLOCK_SIZE; i++) {
|
2018-12-08 18:04:43 +05:30
|
|
|
byte carryOut = (x[i] << 7); // zero, or 0x80
|
|
|
|
x[i] = (x[i] >> 1) ^ carryIn;
|
|
|
|
carryIn = carryOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif BB_BIG_ENDIAN
|
|
|
|
|
|
|
|
// Big-endian can shift-right in larger than byte chunks
|
|
|
|
// (we use the fact that 'x' is long-aligned)
|
|
|
|
unsigned long carryIn = (x[15] & 0x01)
|
|
|
|
? ((unsigned long)0xE1 << (LONG_BIT-8))
|
|
|
|
: 0;
|
|
|
|
# if ULONG_MAX <= 0xffffffff
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < AES_BLOCK_SIZE/sizeof(long); i++) {
|
|
|
|
unsigned long carryOut = l[i] << (LONG_BIT-1); // zero, or 0x800..00
|
|
|
|
l[i] = (l[i] >> 1) ^ carryIn;
|
2018-11-23 21:51:38 +05:30
|
|
|
carryIn = carryOut;
|
|
|
|
}
|
2018-12-08 18:04:43 +05:30
|
|
|
# else
|
|
|
|
// 64-bit code: need to process only 2 words
|
|
|
|
unsigned long carryOut = l[0] << (LONG_BIT-1); // zero, or 0x800..00
|
|
|
|
l[0] = (l[0] >> 1) ^ carryIn;
|
|
|
|
l[1] = (l[1] >> 1) ^ carryOut;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#else /* LITTLE_ENDIAN */
|
|
|
|
|
|
|
|
// In order to use word-sized ops, little-endian needs to byteswap.
|
|
|
|
// On x86, code size increase is ~10 bytes compared to byte-by-byte.
|
|
|
|
unsigned long carryIn = (x[15] & 0x01)
|
|
|
|
? ((unsigned long)0xE1 << (LONG_BIT-8))
|
|
|
|
: 0;
|
|
|
|
# if ULONG_MAX <= 0xffffffff
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < AES_BLOCK_SIZE/sizeof(long); i++) {
|
|
|
|
unsigned long ti = SWAP_BE32(l[i]);
|
|
|
|
unsigned long carryOut = ti << (LONG_BIT-1); // zero, or 0x800..00
|
|
|
|
ti = (ti >> 1) ^ carryIn;
|
|
|
|
l[i] = SWAP_BE32(ti);
|
|
|
|
carryIn = carryOut;
|
|
|
|
}
|
|
|
|
# else
|
|
|
|
// 64-bit code: need to process only 2 words
|
|
|
|
unsigned long tt = SWAP_BE64(l[0]);
|
|
|
|
unsigned long carryOut = tt << (LONG_BIT-1); // zero, or 0x800..00
|
|
|
|
tt = (tt >> 1) ^ carryIn; l[0] = SWAP_BE64(tt);
|
|
|
|
tt = SWAP_BE64(l[1]);
|
|
|
|
tt = (tt >> 1) ^ carryOut; l[1] = SWAP_BE64(tt);
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#endif /* LITTLE_ENDIAN */
|
|
|
|
#undef l
|
2018-11-23 21:51:38 +05:30
|
|
|
}
|
|
|
|
|
2018-12-09 01:54:38 +05:30
|
|
|
// Caller guarantees X is aligned
|
2018-11-23 21:51:38 +05:30
|
|
|
static void GMULT(byte* X, byte* Y)
|
|
|
|
{
|
2018-11-24 18:38:29 +05:30
|
|
|
byte Z[AES_BLOCK_SIZE] ALIGNED_long;
|
2018-12-09 01:54:38 +05:30
|
|
|
//byte V[AES_BLOCK_SIZE] ALIGNED_long;
|
|
|
|
int i;
|
2018-11-23 21:51:38 +05:30
|
|
|
|
|
|
|
XMEMSET(Z, 0, AES_BLOCK_SIZE);
|
2018-12-09 01:54:38 +05:30
|
|
|
//XMEMCPY(V, X, AES_BLOCK_SIZE);
|
|
|
|
for (i = 0; i < AES_BLOCK_SIZE; i++) {
|
|
|
|
uint32_t y = 0x800000 | Y[i];
|
|
|
|
for (;;) { // for every bit in Y[i], from msb to lsb
|
2018-11-23 21:51:38 +05:30
|
|
|
if (y & 0x80) {
|
2018-12-09 01:54:38 +05:30
|
|
|
xorbuf_aligned_AES_BLOCK_SIZE(Z, X); // was V, not X
|
2018-11-23 21:51:38 +05:30
|
|
|
}
|
2018-12-09 01:54:38 +05:30
|
|
|
RIGHTSHIFTX(X); // was V, not X
|
2018-11-23 21:51:38 +05:30
|
|
|
y = y << 1;
|
2018-12-09 01:54:38 +05:30
|
|
|
if ((int32_t)y < 0) // if bit 0x80000000 set = if 8 iterations done
|
|
|
|
break;
|
2018-11-23 21:51:38 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
XMEMCPY(X, Z, AES_BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
2018-11-23 23:01:26 +05:30
|
|
|
//bbox:
|
2018-11-23 23:30:12 +05:30
|
|
|
// for TLS AES-GCM, a (which is AAD) is always 13 bytes long, and bbox code provides
|
2018-11-23 23:01:26 +05:30
|
|
|
// extra 3 zeroed bytes, making it a[16], or a[AES_BLOCK_SIZE].
|
2018-11-23 23:30:12 +05:30
|
|
|
// Resulting auth tag in s[] is also always AES_BLOCK_SIZE bytes.
|
2018-11-23 23:01:26 +05:30
|
|
|
//
|
|
|
|
// This allows some simplifications.
|
2018-11-24 18:17:44 +05:30
|
|
|
#define aSz 13
|
2018-11-23 23:01:26 +05:30
|
|
|
#define sSz AES_BLOCK_SIZE
|
|
|
|
void FAST_FUNC aesgcm_GHASH(byte* h,
|
|
|
|
const byte* a, //unsigned aSz,
|
|
|
|
const byte* c, unsigned cSz,
|
|
|
|
byte* s //, unsigned sSz
|
|
|
|
)
|
2018-11-23 21:51:38 +05:30
|
|
|
{
|
2018-11-24 18:38:29 +05:30
|
|
|
byte x[AES_BLOCK_SIZE] ALIGNED_long;
|
2018-11-25 18:33:59 +05:30
|
|
|
// byte scratch[AES_BLOCK_SIZE] ALIGNED_long;
|
|
|
|
unsigned blocks, partial;
|
2018-11-23 21:51:38 +05:30
|
|
|
//was: byte* h = aes->H;
|
|
|
|
|
2018-11-23 23:01:26 +05:30
|
|
|
//XMEMSET(x, 0, AES_BLOCK_SIZE);
|
2018-11-23 21:51:38 +05:30
|
|
|
|
|
|
|
/* Hash in A, the Additional Authentication Data */
|
2018-11-23 23:01:26 +05:30
|
|
|
// if (aSz != 0 && a != NULL) {
|
|
|
|
// blocks = aSz / AES_BLOCK_SIZE;
|
|
|
|
// partial = aSz % AES_BLOCK_SIZE;
|
|
|
|
// while (blocks--) {
|
|
|
|
//xorbuf(x, a, AES_BLOCK_SIZE);
|
|
|
|
XMEMCPY(x, a, AES_BLOCK_SIZE);// memcpy(x,a) = memset(x,0)+xorbuf(x,a)
|
2018-11-23 21:51:38 +05:30
|
|
|
GMULT(x, h);
|
2018-11-23 23:01:26 +05:30
|
|
|
// a += AES_BLOCK_SIZE;
|
|
|
|
// }
|
|
|
|
// if (partial != 0) {
|
|
|
|
// XMEMSET(scratch, 0, AES_BLOCK_SIZE);
|
|
|
|
// XMEMCPY(scratch, a, partial);
|
|
|
|
// xorbuf(x, scratch, AES_BLOCK_SIZE);
|
|
|
|
// GMULT(x, h);
|
|
|
|
// }
|
|
|
|
// }
|
2018-11-23 21:51:38 +05:30
|
|
|
|
|
|
|
/* Hash in C, the Ciphertext */
|
2018-11-23 23:01:26 +05:30
|
|
|
if (cSz != 0 /*&& c != NULL*/) {
|
2018-11-23 21:51:38 +05:30
|
|
|
blocks = cSz / AES_BLOCK_SIZE;
|
|
|
|
partial = cSz % AES_BLOCK_SIZE;
|
|
|
|
while (blocks--) {
|
2018-11-25 16:31:44 +05:30
|
|
|
if (BB_UNALIGNED_MEMACCESS_OK) // c is not guaranteed to be aligned
|
|
|
|
xorbuf_aligned_AES_BLOCK_SIZE(x, c);
|
|
|
|
else
|
|
|
|
xorbuf(x, c, AES_BLOCK_SIZE);
|
2018-11-23 21:51:38 +05:30
|
|
|
GMULT(x, h);
|
|
|
|
c += AES_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
if (partial != 0) {
|
2018-11-23 23:37:05 +05:30
|
|
|
//XMEMSET(scratch, 0, AES_BLOCK_SIZE);
|
|
|
|
//XMEMCPY(scratch, c, partial);
|
|
|
|
//xorbuf(x, scratch, AES_BLOCK_SIZE);
|
2018-11-24 18:38:29 +05:30
|
|
|
xorbuf(x, c, partial);//same result as above
|
2018-11-23 21:51:38 +05:30
|
|
|
GMULT(x, h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hash in the lengths of A and C in bits */
|
2018-11-25 18:33:59 +05:30
|
|
|
//FlattenSzInBits(&scratch[0], aSz);
|
|
|
|
//FlattenSzInBits(&scratch[8], cSz);
|
|
|
|
//xorbuf_aligned_AES_BLOCK_SIZE(x, scratch);
|
|
|
|
// simpler:
|
|
|
|
#define P32(v) ((uint32_t*)v)
|
|
|
|
//P32(x)[0] ^= 0;
|
|
|
|
P32(x)[1] ^= SWAP_BE32(aSz * 8);
|
|
|
|
//P32(x)[2] ^= 0;
|
|
|
|
P32(x)[3] ^= SWAP_BE32(cSz * 8);
|
|
|
|
#undef P32
|
|
|
|
|
2018-11-23 21:51:38 +05:30
|
|
|
GMULT(x, h);
|
|
|
|
|
|
|
|
/* Copy the result into s. */
|
|
|
|
XMEMCPY(s, x, sSz);
|
|
|
|
}
|