Initial import.
This commit is contained in:
commit
3210f38e04
292 changed files with 58526 additions and 0 deletions
69
src/symcipher/aes_big_cbcdec.c
Normal file
69
src/symcipher/aes_big_cbcdec.c
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_big_cbcdec_init(br_aes_big_cbcdec_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_big_cbcdec_vtable;
|
||||
ctx->num_rounds = br_aes_big_keysched_inv(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_big_cbcdec_run(const br_aes_big_cbcdec_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
|
||||
ivbuf = iv;
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
unsigned char tmp[16];
|
||||
int i;
|
||||
|
||||
memcpy(tmp, buf, 16);
|
||||
br_aes_big_decrypt(ctx->num_rounds, ctx->skey, buf);
|
||||
for (i = 0; i < 16; i ++) {
|
||||
buf[i] ^= ivbuf[i];
|
||||
}
|
||||
memcpy(ivbuf, tmp, 16);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcdec_class br_aes_big_cbcdec_vtable = {
|
||||
sizeof(br_aes_big_cbcdec_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcdec_class **, const void *, size_t))
|
||||
&br_aes_big_cbcdec_init,
|
||||
(void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
|
||||
&br_aes_big_cbcdec_run
|
||||
};
|
||||
67
src/symcipher/aes_big_cbcenc.c
Normal file
67
src/symcipher/aes_big_cbcenc.c
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_big_cbcenc_init(br_aes_big_cbcenc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_big_cbcenc_vtable;
|
||||
ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_big_cbcenc_run(const br_aes_big_cbcenc_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
|
||||
ivbuf = iv;
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i ++) {
|
||||
buf[i] ^= ivbuf[i];
|
||||
}
|
||||
br_aes_big_encrypt(ctx->num_rounds, ctx->skey, buf);
|
||||
memcpy(ivbuf, buf, 16);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcenc_class br_aes_big_cbcenc_vtable = {
|
||||
sizeof(br_aes_big_cbcenc_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcenc_class **, const void *, size_t))
|
||||
&br_aes_big_cbcenc_init,
|
||||
(void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
|
||||
&br_aes_big_cbcenc_run
|
||||
};
|
||||
84
src/symcipher/aes_big_ctr.c
Normal file
84
src/symcipher/aes_big_ctr.c
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_big_ctr_init(br_aes_big_ctr_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_big_ctr_vtable;
|
||||
ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
static void
|
||||
xorbuf(void *dst, const void *src, size_t len)
|
||||
{
|
||||
unsigned char *d;
|
||||
const unsigned char *s;
|
||||
|
||||
d = dst;
|
||||
s = src;
|
||||
while (len -- > 0) {
|
||||
*d ++ ^= *s ++;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
uint32_t
|
||||
br_aes_big_ctr_run(const br_aes_big_ctr_keys *ctx,
|
||||
const void *iv, uint32_t cc, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
unsigned char tmp[16];
|
||||
|
||||
memcpy(tmp, iv, 12);
|
||||
br_enc32be(tmp + 12, cc ++);
|
||||
br_aes_big_encrypt(ctx->num_rounds, ctx->skey, tmp);
|
||||
if (len <= 16) {
|
||||
xorbuf(buf, tmp, len);
|
||||
break;
|
||||
}
|
||||
xorbuf(buf, tmp, 16);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
return cc;
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_ctr_class br_aes_big_ctr_vtable = {
|
||||
sizeof(br_aes_big_ctr_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_ctr_class **, const void *, size_t))
|
||||
&br_aes_big_ctr_init,
|
||||
(uint32_t (*)(const br_block_ctr_class *const *,
|
||||
const void *, uint32_t, void *, size_t))
|
||||
&br_aes_big_ctr_run
|
||||
};
|
||||
254
src/symcipher/aes_big_dec.c
Normal file
254
src/symcipher/aes_big_dec.c
Normal file
|
|
@ -0,0 +1,254 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/*
|
||||
* Inverse S-box (used in key schedule for decryption).
|
||||
*/
|
||||
static const unsigned char iS[] = {
|
||||
0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E,
|
||||
0x81, 0xF3, 0xD7, 0xFB, 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
|
||||
0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 0x54, 0x7B, 0x94, 0x32,
|
||||
0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
|
||||
0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49,
|
||||
0x6D, 0x8B, 0xD1, 0x25, 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16,
|
||||
0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 0x6C, 0x70, 0x48, 0x50,
|
||||
0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
|
||||
0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05,
|
||||
0xB8, 0xB3, 0x45, 0x06, 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02,
|
||||
0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 0x3A, 0x91, 0x11, 0x41,
|
||||
0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
|
||||
0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8,
|
||||
0x1C, 0x75, 0xDF, 0x6E, 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89,
|
||||
0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 0xFC, 0x56, 0x3E, 0x4B,
|
||||
0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
|
||||
0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59,
|
||||
0x27, 0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D,
|
||||
0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0, 0xE0, 0x3B, 0x4D,
|
||||
0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
|
||||
0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63,
|
||||
0x55, 0x21, 0x0C, 0x7D
|
||||
};
|
||||
|
||||
static const uint32_t iSsm0[] = {
|
||||
0x51F4A750, 0x7E416553, 0x1A17A4C3, 0x3A275E96, 0x3BAB6BCB, 0x1F9D45F1,
|
||||
0xACFA58AB, 0x4BE30393, 0x2030FA55, 0xAD766DF6, 0x88CC7691, 0xF5024C25,
|
||||
0x4FE5D7FC, 0xC52ACBD7, 0x26354480, 0xB562A38F, 0xDEB15A49, 0x25BA1B67,
|
||||
0x45EA0E98, 0x5DFEC0E1, 0xC32F7502, 0x814CF012, 0x8D4697A3, 0x6BD3F9C6,
|
||||
0x038F5FE7, 0x15929C95, 0xBF6D7AEB, 0x955259DA, 0xD4BE832D, 0x587421D3,
|
||||
0x49E06929, 0x8EC9C844, 0x75C2896A, 0xF48E7978, 0x99583E6B, 0x27B971DD,
|
||||
0xBEE14FB6, 0xF088AD17, 0xC920AC66, 0x7DCE3AB4, 0x63DF4A18, 0xE51A3182,
|
||||
0x97513360, 0x62537F45, 0xB16477E0, 0xBB6BAE84, 0xFE81A01C, 0xF9082B94,
|
||||
0x70486858, 0x8F45FD19, 0x94DE6C87, 0x527BF8B7, 0xAB73D323, 0x724B02E2,
|
||||
0xE31F8F57, 0x6655AB2A, 0xB2EB2807, 0x2FB5C203, 0x86C57B9A, 0xD33708A5,
|
||||
0x302887F2, 0x23BFA5B2, 0x02036ABA, 0xED16825C, 0x8ACF1C2B, 0xA779B492,
|
||||
0xF307F2F0, 0x4E69E2A1, 0x65DAF4CD, 0x0605BED5, 0xD134621F, 0xC4A6FE8A,
|
||||
0x342E539D, 0xA2F355A0, 0x058AE132, 0xA4F6EB75, 0x0B83EC39, 0x4060EFAA,
|
||||
0x5E719F06, 0xBD6E1051, 0x3E218AF9, 0x96DD063D, 0xDD3E05AE, 0x4DE6BD46,
|
||||
0x91548DB5, 0x71C45D05, 0x0406D46F, 0x605015FF, 0x1998FB24, 0xD6BDE997,
|
||||
0x894043CC, 0x67D99E77, 0xB0E842BD, 0x07898B88, 0xE7195B38, 0x79C8EEDB,
|
||||
0xA17C0A47, 0x7C420FE9, 0xF8841EC9, 0x00000000, 0x09808683, 0x322BED48,
|
||||
0x1E1170AC, 0x6C5A724E, 0xFD0EFFFB, 0x0F853856, 0x3DAED51E, 0x362D3927,
|
||||
0x0A0FD964, 0x685CA621, 0x9B5B54D1, 0x24362E3A, 0x0C0A67B1, 0x9357E70F,
|
||||
0xB4EE96D2, 0x1B9B919E, 0x80C0C54F, 0x61DC20A2, 0x5A774B69, 0x1C121A16,
|
||||
0xE293BA0A, 0xC0A02AE5, 0x3C22E043, 0x121B171D, 0x0E090D0B, 0xF28BC7AD,
|
||||
0x2DB6A8B9, 0x141EA9C8, 0x57F11985, 0xAF75074C, 0xEE99DDBB, 0xA37F60FD,
|
||||
0xF701269F, 0x5C72F5BC, 0x44663BC5, 0x5BFB7E34, 0x8B432976, 0xCB23C6DC,
|
||||
0xB6EDFC68, 0xB8E4F163, 0xD731DCCA, 0x42638510, 0x13972240, 0x84C61120,
|
||||
0x854A247D, 0xD2BB3DF8, 0xAEF93211, 0xC729A16D, 0x1D9E2F4B, 0xDCB230F3,
|
||||
0x0D8652EC, 0x77C1E3D0, 0x2BB3166C, 0xA970B999, 0x119448FA, 0x47E96422,
|
||||
0xA8FC8CC4, 0xA0F03F1A, 0x567D2CD8, 0x223390EF, 0x87494EC7, 0xD938D1C1,
|
||||
0x8CCAA2FE, 0x98D40B36, 0xA6F581CF, 0xA57ADE28, 0xDAB78E26, 0x3FADBFA4,
|
||||
0x2C3A9DE4, 0x5078920D, 0x6A5FCC9B, 0x547E4662, 0xF68D13C2, 0x90D8B8E8,
|
||||
0x2E39F75E, 0x82C3AFF5, 0x9F5D80BE, 0x69D0937C, 0x6FD52DA9, 0xCF2512B3,
|
||||
0xC8AC993B, 0x10187DA7, 0xE89C636E, 0xDB3BBB7B, 0xCD267809, 0x6E5918F4,
|
||||
0xEC9AB701, 0x834F9AA8, 0xE6956E65, 0xAAFFE67E, 0x21BCCF08, 0xEF15E8E6,
|
||||
0xBAE79BD9, 0x4A6F36CE, 0xEA9F09D4, 0x29B07CD6, 0x31A4B2AF, 0x2A3F2331,
|
||||
0xC6A59430, 0x35A266C0, 0x744EBC37, 0xFC82CAA6, 0xE090D0B0, 0x33A7D815,
|
||||
0xF104984A, 0x41ECDAF7, 0x7FCD500E, 0x1791F62F, 0x764DD68D, 0x43EFB04D,
|
||||
0xCCAA4D54, 0xE49604DF, 0x9ED1B5E3, 0x4C6A881B, 0xC12C1FB8, 0x4665517F,
|
||||
0x9D5EEA04, 0x018C355D, 0xFA877473, 0xFB0B412E, 0xB3671D5A, 0x92DBD252,
|
||||
0xE9105633, 0x6DD64713, 0x9AD7618C, 0x37A10C7A, 0x59F8148E, 0xEB133C89,
|
||||
0xCEA927EE, 0xB761C935, 0xE11CE5ED, 0x7A47B13C, 0x9CD2DF59, 0x55F2733F,
|
||||
0x1814CE79, 0x73C737BF, 0x53F7CDEA, 0x5FFDAA5B, 0xDF3D6F14, 0x7844DB86,
|
||||
0xCAAFF381, 0xB968C43E, 0x3824342C, 0xC2A3405F, 0x161DC372, 0xBCE2250C,
|
||||
0x283C498B, 0xFF0D9541, 0x39A80171, 0x080CB3DE, 0xD8B4E49C, 0x6456C190,
|
||||
0x7BCB8461, 0xD532B670, 0x486C5C74, 0xD0B85742
|
||||
};
|
||||
|
||||
static unsigned
|
||||
mul2(unsigned x)
|
||||
{
|
||||
x <<= 1;
|
||||
return x ^ ((unsigned)(-(int)(x >> 8)) & 0x11B);
|
||||
}
|
||||
|
||||
static unsigned
|
||||
mul9(unsigned x)
|
||||
{
|
||||
return x ^ mul2(mul2(mul2(x)));
|
||||
}
|
||||
|
||||
static unsigned
|
||||
mulb(unsigned x)
|
||||
{
|
||||
unsigned x2;
|
||||
|
||||
x2 = mul2(x);
|
||||
return x ^ x2 ^ mul2(mul2(x2));
|
||||
}
|
||||
|
||||
static unsigned
|
||||
muld(unsigned x)
|
||||
{
|
||||
unsigned x4;
|
||||
|
||||
x4 = mul2(mul2(x));
|
||||
return x ^ x4 ^ mul2(x4);
|
||||
}
|
||||
|
||||
static unsigned
|
||||
mule(unsigned x)
|
||||
{
|
||||
unsigned x2, x4;
|
||||
|
||||
x2 = mul2(x);
|
||||
x4 = mul2(x2);
|
||||
return x2 ^ x4 ^ mul2(x4);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
unsigned
|
||||
br_aes_big_keysched_inv(uint32_t *skey, const void *key, size_t key_len)
|
||||
{
|
||||
unsigned num_rounds;
|
||||
int i, m;
|
||||
|
||||
/*
|
||||
* Sub-keys for decryption are distinct from encryption sub-keys
|
||||
* in that InvMixColumns() is already applied for the inner
|
||||
* rounds.
|
||||
*/
|
||||
num_rounds = br_aes_keysched(skey, key, key_len);
|
||||
m = (int)(num_rounds << 2);
|
||||
for (i = 4; i < m; i ++) {
|
||||
uint32_t p;
|
||||
unsigned p0, p1, p2, p3;
|
||||
uint32_t q0, q1, q2, q3;
|
||||
|
||||
p = skey[i];
|
||||
p0 = p >> 24;
|
||||
p1 = (p >> 16) & 0xFF;
|
||||
p2 = (p >> 8) & 0xFF;
|
||||
p3 = p & 0xFF;
|
||||
q0 = mule(p0) ^ mulb(p1) ^ muld(p2) ^ mul9(p3);
|
||||
q1 = mul9(p0) ^ mule(p1) ^ mulb(p2) ^ muld(p3);
|
||||
q2 = muld(p0) ^ mul9(p1) ^ mule(p2) ^ mulb(p3);
|
||||
q3 = mulb(p0) ^ muld(p1) ^ mul9(p2) ^ mule(p3);
|
||||
skey[i] = (q0 << 24) | (q1 << 16) | (q2 << 8) | q3;
|
||||
}
|
||||
return num_rounds;
|
||||
}
|
||||
|
||||
static inline uint32_t
|
||||
rotr(uint32_t x, int n)
|
||||
{
|
||||
return (x << (32 - n)) | (x >> n);
|
||||
}
|
||||
|
||||
#define iSboxExt0(x) (iSsm0[x])
|
||||
#define iSboxExt1(x) (rotr(iSsm0[x], 8))
|
||||
#define iSboxExt2(x) (rotr(iSsm0[x], 16))
|
||||
#define iSboxExt3(x) (rotr(iSsm0[x], 24))
|
||||
|
||||
/* see bearssl.h */
|
||||
void
|
||||
br_aes_big_decrypt(unsigned num_rounds, const uint32_t *skey, void *data)
|
||||
{
|
||||
unsigned char *buf;
|
||||
uint32_t s0, s1, s2, s3;
|
||||
uint32_t t0, t1, t2, t3;
|
||||
unsigned u;
|
||||
|
||||
buf = data;
|
||||
s0 = br_dec32be(buf);
|
||||
s1 = br_dec32be(buf + 4);
|
||||
s2 = br_dec32be(buf + 8);
|
||||
s3 = br_dec32be(buf + 12);
|
||||
s0 ^= skey[(num_rounds << 2) + 0];
|
||||
s1 ^= skey[(num_rounds << 2) + 1];
|
||||
s2 ^= skey[(num_rounds << 2) + 2];
|
||||
s3 ^= skey[(num_rounds << 2) + 3];
|
||||
for (u = num_rounds - 1; u > 0; u --) {
|
||||
uint32_t v0 = iSboxExt0(s0 >> 24)
|
||||
^ iSboxExt1((s3 >> 16) & 0xFF)
|
||||
^ iSboxExt2((s2 >> 8) & 0xFF)
|
||||
^ iSboxExt3(s1 & 0xFF);
|
||||
uint32_t v1 = iSboxExt0(s1 >> 24)
|
||||
^ iSboxExt1((s0 >> 16) & 0xFF)
|
||||
^ iSboxExt2((s3 >> 8) & 0xFF)
|
||||
^ iSboxExt3(s2 & 0xFF);
|
||||
uint32_t v2 = iSboxExt0(s2 >> 24)
|
||||
^ iSboxExt1((s1 >> 16) & 0xFF)
|
||||
^ iSboxExt2((s0 >> 8) & 0xFF)
|
||||
^ iSboxExt3(s3 & 0xFF);
|
||||
uint32_t v3 = iSboxExt0(s3 >> 24)
|
||||
^ iSboxExt1((s2 >> 16) & 0xFF)
|
||||
^ iSboxExt2((s1 >> 8) & 0xFF)
|
||||
^ iSboxExt3(s0 & 0xFF);
|
||||
s0 = v0;
|
||||
s1 = v1;
|
||||
s2 = v2;
|
||||
s3 = v3;
|
||||
s0 ^= skey[u << 2];
|
||||
s1 ^= skey[(u << 2) + 1];
|
||||
s2 ^= skey[(u << 2) + 2];
|
||||
s3 ^= skey[(u << 2) + 3];
|
||||
}
|
||||
t0 = ((uint32_t)iS[s0 >> 24] << 24)
|
||||
| ((uint32_t)iS[(s3 >> 16) & 0xFF] << 16)
|
||||
| ((uint32_t)iS[(s2 >> 8) & 0xFF] << 8)
|
||||
| (uint32_t)iS[s1 & 0xFF];
|
||||
t1 = ((uint32_t)iS[s1 >> 24] << 24)
|
||||
| ((uint32_t)iS[(s0 >> 16) & 0xFF] << 16)
|
||||
| ((uint32_t)iS[(s3 >> 8) & 0xFF] << 8)
|
||||
| (uint32_t)iS[s2 & 0xFF];
|
||||
t2 = ((uint32_t)iS[s2 >> 24] << 24)
|
||||
| ((uint32_t)iS[(s1 >> 16) & 0xFF] << 16)
|
||||
| ((uint32_t)iS[(s0 >> 8) & 0xFF] << 8)
|
||||
| (uint32_t)iS[s3 & 0xFF];
|
||||
t3 = ((uint32_t)iS[s3 >> 24] << 24)
|
||||
| ((uint32_t)iS[(s2 >> 16) & 0xFF] << 16)
|
||||
| ((uint32_t)iS[(s1 >> 8) & 0xFF] << 8)
|
||||
| (uint32_t)iS[s0 & 0xFF];
|
||||
s0 = t0 ^ skey[0];
|
||||
s1 = t1 ^ skey[1];
|
||||
s2 = t2 ^ skey[2];
|
||||
s3 = t3 ^ skey[3];
|
||||
br_enc32be(buf, s0);
|
||||
br_enc32be(buf + 4, s1);
|
||||
br_enc32be(buf + 8, s2);
|
||||
br_enc32be(buf + 12, s3);
|
||||
}
|
||||
157
src/symcipher/aes_big_enc.c
Normal file
157
src/symcipher/aes_big_enc.c
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
#define S br_aes_S
|
||||
|
||||
static const uint32_t Ssm0[] = {
|
||||
0xC66363A5, 0xF87C7C84, 0xEE777799, 0xF67B7B8D, 0xFFF2F20D, 0xD66B6BBD,
|
||||
0xDE6F6FB1, 0x91C5C554, 0x60303050, 0x02010103, 0xCE6767A9, 0x562B2B7D,
|
||||
0xE7FEFE19, 0xB5D7D762, 0x4DABABE6, 0xEC76769A, 0x8FCACA45, 0x1F82829D,
|
||||
0x89C9C940, 0xFA7D7D87, 0xEFFAFA15, 0xB25959EB, 0x8E4747C9, 0xFBF0F00B,
|
||||
0x41ADADEC, 0xB3D4D467, 0x5FA2A2FD, 0x45AFAFEA, 0x239C9CBF, 0x53A4A4F7,
|
||||
0xE4727296, 0x9BC0C05B, 0x75B7B7C2, 0xE1FDFD1C, 0x3D9393AE, 0x4C26266A,
|
||||
0x6C36365A, 0x7E3F3F41, 0xF5F7F702, 0x83CCCC4F, 0x6834345C, 0x51A5A5F4,
|
||||
0xD1E5E534, 0xF9F1F108, 0xE2717193, 0xABD8D873, 0x62313153, 0x2A15153F,
|
||||
0x0804040C, 0x95C7C752, 0x46232365, 0x9DC3C35E, 0x30181828, 0x379696A1,
|
||||
0x0A05050F, 0x2F9A9AB5, 0x0E070709, 0x24121236, 0x1B80809B, 0xDFE2E23D,
|
||||
0xCDEBEB26, 0x4E272769, 0x7FB2B2CD, 0xEA75759F, 0x1209091B, 0x1D83839E,
|
||||
0x582C2C74, 0x341A1A2E, 0x361B1B2D, 0xDC6E6EB2, 0xB45A5AEE, 0x5BA0A0FB,
|
||||
0xA45252F6, 0x763B3B4D, 0xB7D6D661, 0x7DB3B3CE, 0x5229297B, 0xDDE3E33E,
|
||||
0x5E2F2F71, 0x13848497, 0xA65353F5, 0xB9D1D168, 0x00000000, 0xC1EDED2C,
|
||||
0x40202060, 0xE3FCFC1F, 0x79B1B1C8, 0xB65B5BED, 0xD46A6ABE, 0x8DCBCB46,
|
||||
0x67BEBED9, 0x7239394B, 0x944A4ADE, 0x984C4CD4, 0xB05858E8, 0x85CFCF4A,
|
||||
0xBBD0D06B, 0xC5EFEF2A, 0x4FAAAAE5, 0xEDFBFB16, 0x864343C5, 0x9A4D4DD7,
|
||||
0x66333355, 0x11858594, 0x8A4545CF, 0xE9F9F910, 0x04020206, 0xFE7F7F81,
|
||||
0xA05050F0, 0x783C3C44, 0x259F9FBA, 0x4BA8A8E3, 0xA25151F3, 0x5DA3A3FE,
|
||||
0x804040C0, 0x058F8F8A, 0x3F9292AD, 0x219D9DBC, 0x70383848, 0xF1F5F504,
|
||||
0x63BCBCDF, 0x77B6B6C1, 0xAFDADA75, 0x42212163, 0x20101030, 0xE5FFFF1A,
|
||||
0xFDF3F30E, 0xBFD2D26D, 0x81CDCD4C, 0x180C0C14, 0x26131335, 0xC3ECEC2F,
|
||||
0xBE5F5FE1, 0x359797A2, 0x884444CC, 0x2E171739, 0x93C4C457, 0x55A7A7F2,
|
||||
0xFC7E7E82, 0x7A3D3D47, 0xC86464AC, 0xBA5D5DE7, 0x3219192B, 0xE6737395,
|
||||
0xC06060A0, 0x19818198, 0x9E4F4FD1, 0xA3DCDC7F, 0x44222266, 0x542A2A7E,
|
||||
0x3B9090AB, 0x0B888883, 0x8C4646CA, 0xC7EEEE29, 0x6BB8B8D3, 0x2814143C,
|
||||
0xA7DEDE79, 0xBC5E5EE2, 0x160B0B1D, 0xADDBDB76, 0xDBE0E03B, 0x64323256,
|
||||
0x743A3A4E, 0x140A0A1E, 0x924949DB, 0x0C06060A, 0x4824246C, 0xB85C5CE4,
|
||||
0x9FC2C25D, 0xBDD3D36E, 0x43ACACEF, 0xC46262A6, 0x399191A8, 0x319595A4,
|
||||
0xD3E4E437, 0xF279798B, 0xD5E7E732, 0x8BC8C843, 0x6E373759, 0xDA6D6DB7,
|
||||
0x018D8D8C, 0xB1D5D564, 0x9C4E4ED2, 0x49A9A9E0, 0xD86C6CB4, 0xAC5656FA,
|
||||
0xF3F4F407, 0xCFEAEA25, 0xCA6565AF, 0xF47A7A8E, 0x47AEAEE9, 0x10080818,
|
||||
0x6FBABAD5, 0xF0787888, 0x4A25256F, 0x5C2E2E72, 0x381C1C24, 0x57A6A6F1,
|
||||
0x73B4B4C7, 0x97C6C651, 0xCBE8E823, 0xA1DDDD7C, 0xE874749C, 0x3E1F1F21,
|
||||
0x964B4BDD, 0x61BDBDDC, 0x0D8B8B86, 0x0F8A8A85, 0xE0707090, 0x7C3E3E42,
|
||||
0x71B5B5C4, 0xCC6666AA, 0x904848D8, 0x06030305, 0xF7F6F601, 0x1C0E0E12,
|
||||
0xC26161A3, 0x6A35355F, 0xAE5757F9, 0x69B9B9D0, 0x17868691, 0x99C1C158,
|
||||
0x3A1D1D27, 0x279E9EB9, 0xD9E1E138, 0xEBF8F813, 0x2B9898B3, 0x22111133,
|
||||
0xD26969BB, 0xA9D9D970, 0x078E8E89, 0x339494A7, 0x2D9B9BB6, 0x3C1E1E22,
|
||||
0x15878792, 0xC9E9E920, 0x87CECE49, 0xAA5555FF, 0x50282878, 0xA5DFDF7A,
|
||||
0x038C8C8F, 0x59A1A1F8, 0x09898980, 0x1A0D0D17, 0x65BFBFDA, 0xD7E6E631,
|
||||
0x844242C6, 0xD06868B8, 0x824141C3, 0x299999B0, 0x5A2D2D77, 0x1E0F0F11,
|
||||
0x7BB0B0CB, 0xA85454FC, 0x6DBBBBD6, 0x2C16163A
|
||||
};
|
||||
|
||||
static inline uint32_t
|
||||
rotr(uint32_t x, int n)
|
||||
{
|
||||
return (x << (32 - n)) | (x >> n);
|
||||
}
|
||||
|
||||
#define SboxExt0(x) (Ssm0[x])
|
||||
#define SboxExt1(x) (rotr(Ssm0[x], 8))
|
||||
#define SboxExt2(x) (rotr(Ssm0[x], 16))
|
||||
#define SboxExt3(x) (rotr(Ssm0[x], 24))
|
||||
|
||||
|
||||
/* see bearssl.h */
|
||||
void
|
||||
br_aes_big_encrypt(unsigned num_rounds, const uint32_t *skey, void *data)
|
||||
{
|
||||
unsigned char *buf;
|
||||
uint32_t s0, s1, s2, s3;
|
||||
uint32_t t0, t1, t2, t3;
|
||||
unsigned u;
|
||||
|
||||
buf = data;
|
||||
s0 = br_dec32be(buf);
|
||||
s1 = br_dec32be(buf + 4);
|
||||
s2 = br_dec32be(buf + 8);
|
||||
s3 = br_dec32be(buf + 12);
|
||||
s0 ^= skey[0];
|
||||
s1 ^= skey[1];
|
||||
s2 ^= skey[2];
|
||||
s3 ^= skey[3];
|
||||
for (u = 1; u < num_rounds; u ++) {
|
||||
uint32_t v0, v1, v2, v3;
|
||||
|
||||
v0 = SboxExt0(s0 >> 24)
|
||||
^ SboxExt1((s1 >> 16) & 0xFF)
|
||||
^ SboxExt2((s2 >> 8) & 0xFF)
|
||||
^ SboxExt3(s3 & 0xFF);
|
||||
v1 = SboxExt0(s1 >> 24)
|
||||
^ SboxExt1((s2 >> 16) & 0xFF)
|
||||
^ SboxExt2((s3 >> 8) & 0xFF)
|
||||
^ SboxExt3(s0 & 0xFF);
|
||||
v2 = SboxExt0(s2 >> 24)
|
||||
^ SboxExt1((s3 >> 16) & 0xFF)
|
||||
^ SboxExt2((s0 >> 8) & 0xFF)
|
||||
^ SboxExt3(s1 & 0xFF);
|
||||
v3 = SboxExt0(s3 >> 24)
|
||||
^ SboxExt1((s0 >> 16) & 0xFF)
|
||||
^ SboxExt2((s1 >> 8) & 0xFF)
|
||||
^ SboxExt3(s2 & 0xFF);
|
||||
s0 = v0;
|
||||
s1 = v1;
|
||||
s2 = v2;
|
||||
s3 = v3;
|
||||
s0 ^= skey[u << 2];
|
||||
s1 ^= skey[(u << 2) + 1];
|
||||
s2 ^= skey[(u << 2) + 2];
|
||||
s3 ^= skey[(u << 2) + 3];
|
||||
}
|
||||
t0 = ((uint32_t)S[s0 >> 24] << 24)
|
||||
| ((uint32_t)S[(s1 >> 16) & 0xFF] << 16)
|
||||
| ((uint32_t)S[(s2 >> 8) & 0xFF] << 8)
|
||||
| (uint32_t)S[s3 & 0xFF];
|
||||
t1 = ((uint32_t)S[s1 >> 24] << 24)
|
||||
| ((uint32_t)S[(s2 >> 16) & 0xFF] << 16)
|
||||
| ((uint32_t)S[(s3 >> 8) & 0xFF] << 8)
|
||||
| (uint32_t)S[s0 & 0xFF];
|
||||
t2 = ((uint32_t)S[s2 >> 24] << 24)
|
||||
| ((uint32_t)S[(s3 >> 16) & 0xFF] << 16)
|
||||
| ((uint32_t)S[(s0 >> 8) & 0xFF] << 8)
|
||||
| (uint32_t)S[s1 & 0xFF];
|
||||
t3 = ((uint32_t)S[s3 >> 24] << 24)
|
||||
| ((uint32_t)S[(s0 >> 16) & 0xFF] << 16)
|
||||
| ((uint32_t)S[(s1 >> 8) & 0xFF] << 8)
|
||||
| (uint32_t)S[s2 & 0xFF];
|
||||
s0 = t0 ^ skey[num_rounds << 2];
|
||||
s1 = t1 ^ skey[(num_rounds << 2) + 1];
|
||||
s2 = t2 ^ skey[(num_rounds << 2) + 2];
|
||||
s3 = t3 ^ skey[(num_rounds << 2) + 3];
|
||||
br_enc32be(buf, s0);
|
||||
br_enc32be(buf + 4, s1);
|
||||
br_enc32be(buf + 8, s2);
|
||||
br_enc32be(buf + 12, s3);
|
||||
}
|
||||
112
src/symcipher/aes_common.c
Normal file
112
src/symcipher/aes_common.c
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
static const uint32_t Rcon[] = {
|
||||
0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000,
|
||||
0x40000000, 0x80000000, 0x1B000000, 0x36000000
|
||||
};
|
||||
|
||||
#define S br_aes_S
|
||||
|
||||
/* see inner.h */
|
||||
const unsigned char br_aes_S[] = {
|
||||
0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B,
|
||||
0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
|
||||
0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26,
|
||||
0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
|
||||
0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2,
|
||||
0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0,
|
||||
0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED,
|
||||
0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
|
||||
0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F,
|
||||
0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5,
|
||||
0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC,
|
||||
0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
|
||||
0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14,
|
||||
0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C,
|
||||
0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D,
|
||||
0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
|
||||
0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F,
|
||||
0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E,
|
||||
0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11,
|
||||
0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
|
||||
0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F,
|
||||
0xB0, 0x54, 0xBB, 0x16
|
||||
};
|
||||
|
||||
static uint32_t
|
||||
SubWord(uint32_t x)
|
||||
{
|
||||
return ((uint32_t)S[x >> 24] << 24)
|
||||
| ((uint32_t)S[(x >> 16) & 0xFF] << 16)
|
||||
| ((uint32_t)S[(x >> 8) & 0xFF] << 8)
|
||||
| (uint32_t)S[x & 0xFF];
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
unsigned
|
||||
br_aes_keysched(uint32_t *skey, const void *key, size_t key_len)
|
||||
{
|
||||
unsigned num_rounds;
|
||||
int i, j, k, nk, nkf;
|
||||
|
||||
switch (key_len) {
|
||||
case 16:
|
||||
num_rounds = 10;
|
||||
break;
|
||||
case 24:
|
||||
num_rounds = 12;
|
||||
break;
|
||||
case 32:
|
||||
num_rounds = 14;
|
||||
break;
|
||||
default:
|
||||
/* abort(); */
|
||||
return 0;
|
||||
}
|
||||
nk = (int)(key_len >> 2);
|
||||
nkf = (int)((num_rounds + 1) << 2);
|
||||
for (i = 0; i < nk; i ++) {
|
||||
skey[i] = br_dec32be((const unsigned char *)key + (i << 2));
|
||||
}
|
||||
for (i = nk, j = 0, k = 0; i < nkf; i ++) {
|
||||
uint32_t tmp;
|
||||
|
||||
tmp = skey[i - 1];
|
||||
if (j == 0) {
|
||||
tmp = (tmp << 8) | (tmp >> 24);
|
||||
tmp = SubWord(tmp) ^ Rcon[k];
|
||||
} else if (nk > 6 && j == 4) {
|
||||
tmp = SubWord(tmp);
|
||||
}
|
||||
skey[i] = skey[i - nk] ^ tmp;
|
||||
if (++ j == nk) {
|
||||
j = 0;
|
||||
k ++;
|
||||
}
|
||||
}
|
||||
return num_rounds;
|
||||
}
|
||||
328
src/symcipher/aes_ct.c
Normal file
328
src/symcipher/aes_ct.c
Normal file
|
|
@ -0,0 +1,328 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_bitslice_Sbox(uint32_t *q)
|
||||
{
|
||||
/*
|
||||
* This S-box implementation is a straightforward translation of
|
||||
* the circuit described by Boyar and Peralta in "A new
|
||||
* combinational logic minimization technique with applications
|
||||
* to cryptology" (https://eprint.iacr.org/2009/191.pdf).
|
||||
*
|
||||
* Note that variables x* (input) and s* (output) are numbered
|
||||
* in "reverse" order (x0 is the high bit, x7 is the low bit).
|
||||
*/
|
||||
|
||||
uint32_t x0, x1, x2, x3, x4, x5, x6, x7;
|
||||
uint32_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
|
||||
uint32_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
|
||||
uint32_t y20, y21;
|
||||
uint32_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
|
||||
uint32_t z10, z11, z12, z13, z14, z15, z16, z17;
|
||||
uint32_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
|
||||
uint32_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
|
||||
uint32_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
|
||||
uint32_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
|
||||
uint32_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
|
||||
uint32_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
|
||||
uint32_t t60, t61, t62, t63, t64, t65, t66, t67;
|
||||
uint32_t s0, s1, s2, s3, s4, s5, s6, s7;
|
||||
|
||||
x0 = q[7];
|
||||
x1 = q[6];
|
||||
x2 = q[5];
|
||||
x3 = q[4];
|
||||
x4 = q[3];
|
||||
x5 = q[2];
|
||||
x6 = q[1];
|
||||
x7 = q[0];
|
||||
|
||||
/*
|
||||
* Top linear transformation.
|
||||
*/
|
||||
y14 = x3 ^ x5;
|
||||
y13 = x0 ^ x6;
|
||||
y9 = x0 ^ x3;
|
||||
y8 = x0 ^ x5;
|
||||
t0 = x1 ^ x2;
|
||||
y1 = t0 ^ x7;
|
||||
y4 = y1 ^ x3;
|
||||
y12 = y13 ^ y14;
|
||||
y2 = y1 ^ x0;
|
||||
y5 = y1 ^ x6;
|
||||
y3 = y5 ^ y8;
|
||||
t1 = x4 ^ y12;
|
||||
y15 = t1 ^ x5;
|
||||
y20 = t1 ^ x1;
|
||||
y6 = y15 ^ x7;
|
||||
y10 = y15 ^ t0;
|
||||
y11 = y20 ^ y9;
|
||||
y7 = x7 ^ y11;
|
||||
y17 = y10 ^ y11;
|
||||
y19 = y10 ^ y8;
|
||||
y16 = t0 ^ y11;
|
||||
y21 = y13 ^ y16;
|
||||
y18 = x0 ^ y16;
|
||||
|
||||
/*
|
||||
* Non-linear section.
|
||||
*/
|
||||
t2 = y12 & y15;
|
||||
t3 = y3 & y6;
|
||||
t4 = t3 ^ t2;
|
||||
t5 = y4 & x7;
|
||||
t6 = t5 ^ t2;
|
||||
t7 = y13 & y16;
|
||||
t8 = y5 & y1;
|
||||
t9 = t8 ^ t7;
|
||||
t10 = y2 & y7;
|
||||
t11 = t10 ^ t7;
|
||||
t12 = y9 & y11;
|
||||
t13 = y14 & y17;
|
||||
t14 = t13 ^ t12;
|
||||
t15 = y8 & y10;
|
||||
t16 = t15 ^ t12;
|
||||
t17 = t4 ^ t14;
|
||||
t18 = t6 ^ t16;
|
||||
t19 = t9 ^ t14;
|
||||
t20 = t11 ^ t16;
|
||||
t21 = t17 ^ y20;
|
||||
t22 = t18 ^ y19;
|
||||
t23 = t19 ^ y21;
|
||||
t24 = t20 ^ y18;
|
||||
|
||||
t25 = t21 ^ t22;
|
||||
t26 = t21 & t23;
|
||||
t27 = t24 ^ t26;
|
||||
t28 = t25 & t27;
|
||||
t29 = t28 ^ t22;
|
||||
t30 = t23 ^ t24;
|
||||
t31 = t22 ^ t26;
|
||||
t32 = t31 & t30;
|
||||
t33 = t32 ^ t24;
|
||||
t34 = t23 ^ t33;
|
||||
t35 = t27 ^ t33;
|
||||
t36 = t24 & t35;
|
||||
t37 = t36 ^ t34;
|
||||
t38 = t27 ^ t36;
|
||||
t39 = t29 & t38;
|
||||
t40 = t25 ^ t39;
|
||||
|
||||
t41 = t40 ^ t37;
|
||||
t42 = t29 ^ t33;
|
||||
t43 = t29 ^ t40;
|
||||
t44 = t33 ^ t37;
|
||||
t45 = t42 ^ t41;
|
||||
z0 = t44 & y15;
|
||||
z1 = t37 & y6;
|
||||
z2 = t33 & x7;
|
||||
z3 = t43 & y16;
|
||||
z4 = t40 & y1;
|
||||
z5 = t29 & y7;
|
||||
z6 = t42 & y11;
|
||||
z7 = t45 & y17;
|
||||
z8 = t41 & y10;
|
||||
z9 = t44 & y12;
|
||||
z10 = t37 & y3;
|
||||
z11 = t33 & y4;
|
||||
z12 = t43 & y13;
|
||||
z13 = t40 & y5;
|
||||
z14 = t29 & y2;
|
||||
z15 = t42 & y9;
|
||||
z16 = t45 & y14;
|
||||
z17 = t41 & y8;
|
||||
|
||||
/*
|
||||
* Bottom linear transformation.
|
||||
*/
|
||||
t46 = z15 ^ z16;
|
||||
t47 = z10 ^ z11;
|
||||
t48 = z5 ^ z13;
|
||||
t49 = z9 ^ z10;
|
||||
t50 = z2 ^ z12;
|
||||
t51 = z2 ^ z5;
|
||||
t52 = z7 ^ z8;
|
||||
t53 = z0 ^ z3;
|
||||
t54 = z6 ^ z7;
|
||||
t55 = z16 ^ z17;
|
||||
t56 = z12 ^ t48;
|
||||
t57 = t50 ^ t53;
|
||||
t58 = z4 ^ t46;
|
||||
t59 = z3 ^ t54;
|
||||
t60 = t46 ^ t57;
|
||||
t61 = z14 ^ t57;
|
||||
t62 = t52 ^ t58;
|
||||
t63 = t49 ^ t58;
|
||||
t64 = z4 ^ t59;
|
||||
t65 = t61 ^ t62;
|
||||
t66 = z1 ^ t63;
|
||||
s0 = t59 ^ t63;
|
||||
s6 = t56 ^ ~t62;
|
||||
s7 = t48 ^ ~t60;
|
||||
t67 = t64 ^ t65;
|
||||
s3 = t53 ^ t66;
|
||||
s4 = t51 ^ t66;
|
||||
s5 = t47 ^ t65;
|
||||
s1 = t64 ^ ~s3;
|
||||
s2 = t55 ^ ~t67;
|
||||
|
||||
q[7] = s0;
|
||||
q[6] = s1;
|
||||
q[5] = s2;
|
||||
q[4] = s3;
|
||||
q[3] = s4;
|
||||
q[2] = s5;
|
||||
q[1] = s6;
|
||||
q[0] = s7;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_ortho(uint32_t *q)
|
||||
{
|
||||
#define SWAPN(cl, ch, s, x, y) do { \
|
||||
uint32_t a, b; \
|
||||
a = (x); \
|
||||
b = (y); \
|
||||
(x) = (a & (uint32_t)cl) | ((b & (uint32_t)cl) << (s)); \
|
||||
(y) = ((a & (uint32_t)ch) >> (s)) | (b & (uint32_t)ch); \
|
||||
} while (0)
|
||||
|
||||
#define SWAP2(x, y) SWAPN(0x55555555, 0xAAAAAAAA, 1, x, y)
|
||||
#define SWAP4(x, y) SWAPN(0x33333333, 0xCCCCCCCC, 2, x, y)
|
||||
#define SWAP8(x, y) SWAPN(0x0F0F0F0F, 0xF0F0F0F0, 4, x, y)
|
||||
|
||||
SWAP2(q[0], q[1]);
|
||||
SWAP2(q[2], q[3]);
|
||||
SWAP2(q[4], q[5]);
|
||||
SWAP2(q[6], q[7]);
|
||||
|
||||
SWAP4(q[0], q[2]);
|
||||
SWAP4(q[1], q[3]);
|
||||
SWAP4(q[4], q[6]);
|
||||
SWAP4(q[5], q[7]);
|
||||
|
||||
SWAP8(q[0], q[4]);
|
||||
SWAP8(q[1], q[5]);
|
||||
SWAP8(q[2], q[6]);
|
||||
SWAP8(q[3], q[7]);
|
||||
}
|
||||
|
||||
static const unsigned char Rcon[] = {
|
||||
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
|
||||
};
|
||||
|
||||
static uint32_t
|
||||
sub_word(uint32_t x)
|
||||
{
|
||||
uint32_t q[8];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
q[i] = x;
|
||||
}
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_Sbox(q);
|
||||
br_aes_ct_ortho(q);
|
||||
return q[0];
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
unsigned
|
||||
br_aes_ct_keysched(uint32_t *comp_skey, const void *key, size_t key_len)
|
||||
{
|
||||
unsigned num_rounds;
|
||||
int i, j, k, nk, nkf;
|
||||
uint32_t tmp;
|
||||
uint32_t skey[120];
|
||||
|
||||
switch (key_len) {
|
||||
case 16:
|
||||
num_rounds = 10;
|
||||
break;
|
||||
case 24:
|
||||
num_rounds = 12;
|
||||
break;
|
||||
case 32:
|
||||
num_rounds = 14;
|
||||
break;
|
||||
default:
|
||||
/* abort(); */
|
||||
return 0;
|
||||
}
|
||||
nk = (int)(key_len >> 2);
|
||||
nkf = (int)((num_rounds + 1) << 2);
|
||||
tmp = 0;
|
||||
for (i = 0; i < nk; i ++) {
|
||||
tmp = br_dec32le((const unsigned char *)key + (i << 2));
|
||||
skey[(i << 1) + 0] = tmp;
|
||||
skey[(i << 1) + 1] = tmp;
|
||||
}
|
||||
for (i = nk, j = 0, k = 0; i < nkf; i ++) {
|
||||
if (j == 0) {
|
||||
tmp = (tmp << 24) | (tmp >> 8);
|
||||
tmp = sub_word(tmp) ^ Rcon[k];
|
||||
} else if (nk > 6 && j == 4) {
|
||||
tmp = sub_word(tmp);
|
||||
}
|
||||
tmp ^= skey[(i - nk) << 1];
|
||||
skey[(i << 1) + 0] = tmp;
|
||||
skey[(i << 1) + 1] = tmp;
|
||||
if (++ j == nk) {
|
||||
j = 0;
|
||||
k ++;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < nkf; i += 4) {
|
||||
br_aes_ct_ortho(skey + (i << 1));
|
||||
}
|
||||
for (i = 0, j = 0; i < nkf; i ++, j += 2) {
|
||||
comp_skey[i] = (skey[j + 0] & 0x55555555)
|
||||
| (skey[j + 1] & 0xAAAAAAAA);
|
||||
}
|
||||
return num_rounds;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_skey_expand(uint32_t *skey,
|
||||
unsigned num_rounds, const uint32_t *comp_skey)
|
||||
{
|
||||
unsigned u, v, n;
|
||||
|
||||
n = (num_rounds + 1) << 2;
|
||||
for (u = 0, v = 0; u < n; u ++, v += 2) {
|
||||
uint32_t x, y;
|
||||
|
||||
x = y = comp_skey[u];
|
||||
x &= 0x55555555;
|
||||
skey[v + 0] = x | (x << 1);
|
||||
y &= 0xAAAAAAAA;
|
||||
skey[v + 1] = y | (y >> 1);
|
||||
}
|
||||
}
|
||||
398
src/symcipher/aes_ct64.c
Normal file
398
src/symcipher/aes_ct64.c
Normal file
|
|
@ -0,0 +1,398 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_bitslice_Sbox(uint64_t *q)
|
||||
{
|
||||
/*
|
||||
* This S-box implementation is a straightforward translation of
|
||||
* the circuit described by Boyar and Peralta in "A new
|
||||
* combinational logic minimization technique with applications
|
||||
* to cryptology" (https://eprint.iacr.org/2009/191.pdf).
|
||||
*
|
||||
* Note that variables x* (input) and s* (output) are numbered
|
||||
* in "reverse" order (x0 is the high bit, x7 is the low bit).
|
||||
*/
|
||||
|
||||
uint64_t x0, x1, x2, x3, x4, x5, x6, x7;
|
||||
uint64_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
|
||||
uint64_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
|
||||
uint64_t y20, y21;
|
||||
uint64_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
|
||||
uint64_t z10, z11, z12, z13, z14, z15, z16, z17;
|
||||
uint64_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
|
||||
uint64_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
|
||||
uint64_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
|
||||
uint64_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
|
||||
uint64_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
|
||||
uint64_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
|
||||
uint64_t t60, t61, t62, t63, t64, t65, t66, t67;
|
||||
uint64_t s0, s1, s2, s3, s4, s5, s6, s7;
|
||||
|
||||
x0 = q[7];
|
||||
x1 = q[6];
|
||||
x2 = q[5];
|
||||
x3 = q[4];
|
||||
x4 = q[3];
|
||||
x5 = q[2];
|
||||
x6 = q[1];
|
||||
x7 = q[0];
|
||||
|
||||
/*
|
||||
* Top linear transformation.
|
||||
*/
|
||||
y14 = x3 ^ x5;
|
||||
y13 = x0 ^ x6;
|
||||
y9 = x0 ^ x3;
|
||||
y8 = x0 ^ x5;
|
||||
t0 = x1 ^ x2;
|
||||
y1 = t0 ^ x7;
|
||||
y4 = y1 ^ x3;
|
||||
y12 = y13 ^ y14;
|
||||
y2 = y1 ^ x0;
|
||||
y5 = y1 ^ x6;
|
||||
y3 = y5 ^ y8;
|
||||
t1 = x4 ^ y12;
|
||||
y15 = t1 ^ x5;
|
||||
y20 = t1 ^ x1;
|
||||
y6 = y15 ^ x7;
|
||||
y10 = y15 ^ t0;
|
||||
y11 = y20 ^ y9;
|
||||
y7 = x7 ^ y11;
|
||||
y17 = y10 ^ y11;
|
||||
y19 = y10 ^ y8;
|
||||
y16 = t0 ^ y11;
|
||||
y21 = y13 ^ y16;
|
||||
y18 = x0 ^ y16;
|
||||
|
||||
/*
|
||||
* Non-linear section.
|
||||
*/
|
||||
t2 = y12 & y15;
|
||||
t3 = y3 & y6;
|
||||
t4 = t3 ^ t2;
|
||||
t5 = y4 & x7;
|
||||
t6 = t5 ^ t2;
|
||||
t7 = y13 & y16;
|
||||
t8 = y5 & y1;
|
||||
t9 = t8 ^ t7;
|
||||
t10 = y2 & y7;
|
||||
t11 = t10 ^ t7;
|
||||
t12 = y9 & y11;
|
||||
t13 = y14 & y17;
|
||||
t14 = t13 ^ t12;
|
||||
t15 = y8 & y10;
|
||||
t16 = t15 ^ t12;
|
||||
t17 = t4 ^ t14;
|
||||
t18 = t6 ^ t16;
|
||||
t19 = t9 ^ t14;
|
||||
t20 = t11 ^ t16;
|
||||
t21 = t17 ^ y20;
|
||||
t22 = t18 ^ y19;
|
||||
t23 = t19 ^ y21;
|
||||
t24 = t20 ^ y18;
|
||||
|
||||
t25 = t21 ^ t22;
|
||||
t26 = t21 & t23;
|
||||
t27 = t24 ^ t26;
|
||||
t28 = t25 & t27;
|
||||
t29 = t28 ^ t22;
|
||||
t30 = t23 ^ t24;
|
||||
t31 = t22 ^ t26;
|
||||
t32 = t31 & t30;
|
||||
t33 = t32 ^ t24;
|
||||
t34 = t23 ^ t33;
|
||||
t35 = t27 ^ t33;
|
||||
t36 = t24 & t35;
|
||||
t37 = t36 ^ t34;
|
||||
t38 = t27 ^ t36;
|
||||
t39 = t29 & t38;
|
||||
t40 = t25 ^ t39;
|
||||
|
||||
t41 = t40 ^ t37;
|
||||
t42 = t29 ^ t33;
|
||||
t43 = t29 ^ t40;
|
||||
t44 = t33 ^ t37;
|
||||
t45 = t42 ^ t41;
|
||||
z0 = t44 & y15;
|
||||
z1 = t37 & y6;
|
||||
z2 = t33 & x7;
|
||||
z3 = t43 & y16;
|
||||
z4 = t40 & y1;
|
||||
z5 = t29 & y7;
|
||||
z6 = t42 & y11;
|
||||
z7 = t45 & y17;
|
||||
z8 = t41 & y10;
|
||||
z9 = t44 & y12;
|
||||
z10 = t37 & y3;
|
||||
z11 = t33 & y4;
|
||||
z12 = t43 & y13;
|
||||
z13 = t40 & y5;
|
||||
z14 = t29 & y2;
|
||||
z15 = t42 & y9;
|
||||
z16 = t45 & y14;
|
||||
z17 = t41 & y8;
|
||||
|
||||
/*
|
||||
* Bottom linear transformation.
|
||||
*/
|
||||
t46 = z15 ^ z16;
|
||||
t47 = z10 ^ z11;
|
||||
t48 = z5 ^ z13;
|
||||
t49 = z9 ^ z10;
|
||||
t50 = z2 ^ z12;
|
||||
t51 = z2 ^ z5;
|
||||
t52 = z7 ^ z8;
|
||||
t53 = z0 ^ z3;
|
||||
t54 = z6 ^ z7;
|
||||
t55 = z16 ^ z17;
|
||||
t56 = z12 ^ t48;
|
||||
t57 = t50 ^ t53;
|
||||
t58 = z4 ^ t46;
|
||||
t59 = z3 ^ t54;
|
||||
t60 = t46 ^ t57;
|
||||
t61 = z14 ^ t57;
|
||||
t62 = t52 ^ t58;
|
||||
t63 = t49 ^ t58;
|
||||
t64 = z4 ^ t59;
|
||||
t65 = t61 ^ t62;
|
||||
t66 = z1 ^ t63;
|
||||
s0 = t59 ^ t63;
|
||||
s6 = t56 ^ ~t62;
|
||||
s7 = t48 ^ ~t60;
|
||||
t67 = t64 ^ t65;
|
||||
s3 = t53 ^ t66;
|
||||
s4 = t51 ^ t66;
|
||||
s5 = t47 ^ t65;
|
||||
s1 = t64 ^ ~s3;
|
||||
s2 = t55 ^ ~t67;
|
||||
|
||||
q[7] = s0;
|
||||
q[6] = s1;
|
||||
q[5] = s2;
|
||||
q[4] = s3;
|
||||
q[3] = s4;
|
||||
q[2] = s5;
|
||||
q[1] = s6;
|
||||
q[0] = s7;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_ortho(uint64_t *q)
|
||||
{
|
||||
#define SWAPN(cl, ch, s, x, y) do { \
|
||||
uint64_t a, b; \
|
||||
a = (x); \
|
||||
b = (y); \
|
||||
(x) = (a & (uint64_t)cl) | ((b & (uint64_t)cl) << (s)); \
|
||||
(y) = ((a & (uint64_t)ch) >> (s)) | (b & (uint64_t)ch); \
|
||||
} while (0)
|
||||
|
||||
#define SWAP2(x, y) SWAPN(0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 1, x, y)
|
||||
#define SWAP4(x, y) SWAPN(0x3333333333333333, 0xCCCCCCCCCCCCCCCC, 2, x, y)
|
||||
#define SWAP8(x, y) SWAPN(0x0F0F0F0F0F0F0F0F, 0xF0F0F0F0F0F0F0F0, 4, x, y)
|
||||
|
||||
SWAP2(q[0], q[1]);
|
||||
SWAP2(q[2], q[3]);
|
||||
SWAP2(q[4], q[5]);
|
||||
SWAP2(q[6], q[7]);
|
||||
|
||||
SWAP4(q[0], q[2]);
|
||||
SWAP4(q[1], q[3]);
|
||||
SWAP4(q[4], q[6]);
|
||||
SWAP4(q[5], q[7]);
|
||||
|
||||
SWAP8(q[0], q[4]);
|
||||
SWAP8(q[1], q[5]);
|
||||
SWAP8(q[2], q[6]);
|
||||
SWAP8(q[3], q[7]);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_interleave_in(uint64_t *q0, uint64_t *q1, const uint32_t *w)
|
||||
{
|
||||
uint64_t x0, x1, x2, x3;
|
||||
|
||||
x0 = w[0];
|
||||
x1 = w[1];
|
||||
x2 = w[2];
|
||||
x3 = w[3];
|
||||
x0 |= (x0 << 16);
|
||||
x1 |= (x1 << 16);
|
||||
x2 |= (x2 << 16);
|
||||
x3 |= (x3 << 16);
|
||||
x0 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x1 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x2 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x3 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x0 |= (x0 << 8);
|
||||
x1 |= (x1 << 8);
|
||||
x2 |= (x2 << 8);
|
||||
x3 |= (x3 << 8);
|
||||
x0 &= (uint64_t)0x00FF00FF00FF00FF;
|
||||
x1 &= (uint64_t)0x00FF00FF00FF00FF;
|
||||
x2 &= (uint64_t)0x00FF00FF00FF00FF;
|
||||
x3 &= (uint64_t)0x00FF00FF00FF00FF;
|
||||
*q0 = x0 | (x2 << 8);
|
||||
*q1 = x1 | (x3 << 8);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_interleave_out(uint32_t *w, uint64_t q0, uint64_t q1)
|
||||
{
|
||||
uint64_t x0, x1, x2, x3;
|
||||
|
||||
x0 = q0 & (uint64_t)0x00FF00FF00FF00FF;
|
||||
x1 = q1 & (uint64_t)0x00FF00FF00FF00FF;
|
||||
x2 = (q0 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
|
||||
x3 = (q1 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
|
||||
x0 |= (x0 >> 8);
|
||||
x1 |= (x1 >> 8);
|
||||
x2 |= (x2 >> 8);
|
||||
x3 |= (x3 >> 8);
|
||||
x0 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x1 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x2 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x3 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
w[0] = (uint32_t)x0 | (uint32_t)(x0 >> 16);
|
||||
w[1] = (uint32_t)x1 | (uint32_t)(x1 >> 16);
|
||||
w[2] = (uint32_t)x2 | (uint32_t)(x2 >> 16);
|
||||
w[3] = (uint32_t)x3 | (uint32_t)(x3 >> 16);
|
||||
}
|
||||
|
||||
static const unsigned char Rcon[] = {
|
||||
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
|
||||
};
|
||||
|
||||
static uint32_t
|
||||
sub_word(uint32_t x)
|
||||
{
|
||||
uint64_t q[8];
|
||||
|
||||
memset(q, 0, sizeof q);
|
||||
q[0] = x;
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_Sbox(q);
|
||||
br_aes_ct64_ortho(q);
|
||||
return (uint32_t)q[0];
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
unsigned
|
||||
br_aes_ct64_keysched(uint64_t *comp_skey, const void *key, size_t key_len)
|
||||
{
|
||||
unsigned num_rounds;
|
||||
int i, j, k, nk, nkf;
|
||||
uint32_t tmp;
|
||||
uint32_t skey[60];
|
||||
|
||||
switch (key_len) {
|
||||
case 16:
|
||||
num_rounds = 10;
|
||||
break;
|
||||
case 24:
|
||||
num_rounds = 12;
|
||||
break;
|
||||
case 32:
|
||||
num_rounds = 14;
|
||||
break;
|
||||
default:
|
||||
/* abort(); */
|
||||
return 0;
|
||||
}
|
||||
nk = (int)(key_len >> 2);
|
||||
nkf = (int)((num_rounds + 1) << 2);
|
||||
br_range_dec32le(skey, (key_len >> 2), key);
|
||||
tmp = skey[(key_len >> 2) - 1];
|
||||
for (i = nk, j = 0, k = 0; i < nkf; i ++) {
|
||||
if (j == 0) {
|
||||
tmp = (tmp << 24) | (tmp >> 8);
|
||||
tmp = sub_word(tmp) ^ Rcon[k];
|
||||
} else if (nk > 6 && j == 4) {
|
||||
tmp = sub_word(tmp);
|
||||
}
|
||||
tmp ^= skey[i - nk];
|
||||
skey[i] = tmp;
|
||||
if (++ j == nk) {
|
||||
j = 0;
|
||||
k ++;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0, j = 0; i < nkf; i += 4, j += 2) {
|
||||
uint64_t q[8];
|
||||
|
||||
br_aes_ct64_interleave_in(&q[0], &q[4], skey + i);
|
||||
q[1] = q[0];
|
||||
q[2] = q[0];
|
||||
q[3] = q[0];
|
||||
q[5] = q[4];
|
||||
q[6] = q[4];
|
||||
q[7] = q[4];
|
||||
br_aes_ct64_ortho(q);
|
||||
comp_skey[j + 0] =
|
||||
(q[0] & (uint64_t)0x1111111111111111)
|
||||
| (q[1] & (uint64_t)0x2222222222222222)
|
||||
| (q[2] & (uint64_t)0x4444444444444444)
|
||||
| (q[3] & (uint64_t)0x8888888888888888);
|
||||
comp_skey[j + 1] =
|
||||
(q[4] & (uint64_t)0x1111111111111111)
|
||||
| (q[5] & (uint64_t)0x2222222222222222)
|
||||
| (q[6] & (uint64_t)0x4444444444444444)
|
||||
| (q[7] & (uint64_t)0x8888888888888888);
|
||||
}
|
||||
return num_rounds;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_skey_expand(uint64_t *skey,
|
||||
unsigned num_rounds, const uint64_t *comp_skey)
|
||||
{
|
||||
unsigned u, v, n;
|
||||
|
||||
n = (num_rounds + 1) << 2;
|
||||
for (u = 0, v = 0; u < n; u ++, v += 4) {
|
||||
uint64_t x0, x1, x2, x3;
|
||||
|
||||
x0 = x1 = x2 = x3 = comp_skey[u];
|
||||
x0 &= (uint64_t)0x1111111111111111;
|
||||
x1 &= (uint64_t)0x2222222222222222;
|
||||
x2 &= (uint64_t)0x4444444444444444;
|
||||
x3 &= (uint64_t)0x8888888888888888;
|
||||
x1 >>= 1;
|
||||
x2 >>= 2;
|
||||
x3 >>= 3;
|
||||
skey[v + 0] = (x0 << 4) - x0;
|
||||
skey[v + 1] = (x1 << 4) - x1;
|
||||
skey[v + 2] = (x2 << 4) - x2;
|
||||
skey[v + 3] = (x3 << 4) - x3;
|
||||
}
|
||||
}
|
||||
104
src/symcipher/aes_ct64_cbcdec.c
Normal file
104
src/symcipher/aes_ct64_cbcdec.c
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_cbcdec_init(br_aes_ct64_cbcdec_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct64_cbcdec_vtable;
|
||||
ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_cbcdec_run(const br_aes_ct64_cbcdec_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
uint64_t sk_exp[240];
|
||||
uint32_t ivw[4];
|
||||
|
||||
br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
br_range_dec32le(ivw, 4, iv);
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint64_t q[8];
|
||||
uint32_t w1[16], w2[16];
|
||||
int i;
|
||||
|
||||
if (len >= 64) {
|
||||
br_range_dec32le(w1, 16, buf);
|
||||
} else {
|
||||
br_range_dec32le(w1, len >> 2, buf);
|
||||
}
|
||||
for (i = 0; i < 4; i ++) {
|
||||
br_aes_ct64_interleave_in(
|
||||
&q[i], &q[i + 4], w1 + (i << 2));
|
||||
}
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_decrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct64_ortho(q);
|
||||
for (i = 0; i < 4; i ++) {
|
||||
br_aes_ct64_interleave_out(
|
||||
w2 + (i << 2), q[i], q[i + 4]);
|
||||
}
|
||||
for (i = 0; i < 4; i ++) {
|
||||
w2[i] ^= ivw[i];
|
||||
}
|
||||
if (len >= 64) {
|
||||
for (i = 4; i < 16; i ++) {
|
||||
w2[i] ^= w1[i - 4];
|
||||
}
|
||||
memcpy(ivw, w1 + 12, sizeof ivw);
|
||||
br_range_enc32le(buf, w2, 16);
|
||||
} else {
|
||||
int j;
|
||||
|
||||
j = (int)(len >> 2);
|
||||
for (i = 4; i < j; i ++) {
|
||||
w2[i] ^= w1[i - 4];
|
||||
}
|
||||
memcpy(ivw, w1 + j - 4, sizeof ivw);
|
||||
br_range_enc32le(buf, w2, j);
|
||||
break;
|
||||
}
|
||||
buf += 64;
|
||||
len -= 64;
|
||||
}
|
||||
br_range_enc32le(iv, ivw, 4);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcdec_class br_aes_ct64_cbcdec_vtable = {
|
||||
sizeof(br_aes_ct64_cbcdec_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcdec_class **, const void *, size_t))
|
||||
&br_aes_ct64_cbcdec_init,
|
||||
(void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
|
||||
&br_aes_ct64_cbcdec_run
|
||||
};
|
||||
81
src/symcipher/aes_ct64_cbcenc.c
Normal file
81
src/symcipher/aes_ct64_cbcenc.c
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_cbcenc_init(br_aes_ct64_cbcenc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct64_cbcenc_vtable;
|
||||
ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_cbcenc_run(const br_aes_ct64_cbcenc_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
uint64_t sk_exp[240];
|
||||
uint32_t ivw[4];
|
||||
|
||||
br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
br_range_dec32le(ivw, 4, iv);
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint32_t w[4];
|
||||
uint64_t q[8];
|
||||
|
||||
w[0] = ivw[0] ^ br_dec32le(buf);
|
||||
w[1] = ivw[1] ^ br_dec32le(buf + 4);
|
||||
w[2] = ivw[2] ^ br_dec32le(buf + 8);
|
||||
w[3] = ivw[3] ^ br_dec32le(buf + 12);
|
||||
br_aes_ct64_interleave_in(&q[0], &q[4], w);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_interleave_out(w, q[0], q[4]);
|
||||
memcpy(ivw, w, sizeof w);
|
||||
br_enc32le(buf, w[0]);
|
||||
br_enc32le(buf + 4, w[1]);
|
||||
br_enc32le(buf + 8, w[2]);
|
||||
br_enc32le(buf + 12, w[3]);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
br_range_enc32le(iv, ivw, 4);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcenc_class br_aes_ct64_cbcenc_vtable = {
|
||||
sizeof(br_aes_ct64_cbcenc_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcenc_class **, const void *, size_t))
|
||||
&br_aes_ct64_cbcenc_init,
|
||||
(void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
|
||||
&br_aes_ct64_cbcenc_run
|
||||
};
|
||||
114
src/symcipher/aes_ct64_ctr.c
Normal file
114
src/symcipher/aes_ct64_ctr.c
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_ctr_init(br_aes_ct64_ctr_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct64_ctr_vtable;
|
||||
ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
static void
|
||||
xorbuf(void *dst, const void *src, size_t len)
|
||||
{
|
||||
unsigned char *d;
|
||||
const unsigned char *s;
|
||||
|
||||
d = dst;
|
||||
s = src;
|
||||
while (len -- > 0) {
|
||||
*d ++ ^= *s ++;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
uint32_t
|
||||
br_aes_ct64_ctr_run(const br_aes_ct64_ctr_keys *ctx,
|
||||
const void *iv, uint32_t cc, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
uint32_t ivw[16];
|
||||
uint64_t sk_exp[240];
|
||||
|
||||
br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
br_range_dec32le(ivw, 3, iv);
|
||||
memcpy(ivw + 4, ivw, 3 * sizeof(uint32_t));
|
||||
memcpy(ivw + 8, ivw, 3 * sizeof(uint32_t));
|
||||
memcpy(ivw + 12, ivw, 3 * sizeof(uint32_t));
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint64_t q[8];
|
||||
uint32_t w[16];
|
||||
unsigned char tmp[64];
|
||||
int i;
|
||||
|
||||
/*
|
||||
* TODO: see if we can save on the first br_aes_ct64_ortho()
|
||||
* call, since iv0/iv1/iv2 are constant for the whole run.
|
||||
*/
|
||||
memcpy(w, ivw, sizeof ivw);
|
||||
w[3] = br_swap32(cc);
|
||||
w[7] = br_swap32(cc + 1);
|
||||
w[11] = br_swap32(cc + 2);
|
||||
w[15] = br_swap32(cc + 3);
|
||||
for (i = 0; i < 4; i ++) {
|
||||
br_aes_ct64_interleave_in(
|
||||
&q[i], &q[i + 4], w + (i << 2));
|
||||
}
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct64_ortho(q);
|
||||
for (i = 0; i < 4; i ++) {
|
||||
br_aes_ct64_interleave_out(
|
||||
w + (i << 2), q[i], q[i + 4]);
|
||||
}
|
||||
br_range_enc32le(tmp, w, 16);
|
||||
if (len <= 64) {
|
||||
xorbuf(buf, tmp, len);
|
||||
cc += (uint32_t)len >> 4;
|
||||
break;
|
||||
}
|
||||
xorbuf(buf, tmp, 64);
|
||||
buf += 64;
|
||||
len -= 64;
|
||||
cc += 4;
|
||||
}
|
||||
return cc;
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_ctr_class br_aes_ct64_ctr_vtable = {
|
||||
sizeof(br_aes_ct64_ctr_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_ctr_class **, const void *, size_t))
|
||||
&br_aes_ct64_ctr_init,
|
||||
(uint32_t (*)(const br_block_ctr_class *const *,
|
||||
const void *, uint32_t, void *, size_t))
|
||||
&br_aes_ct64_ctr_run
|
||||
};
|
||||
159
src/symcipher/aes_ct64_dec.c
Normal file
159
src/symcipher/aes_ct64_dec.c
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_bitslice_invSbox(uint64_t *q)
|
||||
{
|
||||
/*
|
||||
* See br_aes_ct_bitslice_invSbox(). This is the natural extension
|
||||
* to 64-bit registers.
|
||||
*/
|
||||
uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
|
||||
q0 = ~q[0];
|
||||
q1 = ~q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = ~q[5];
|
||||
q6 = ~q[6];
|
||||
q7 = q[7];
|
||||
q[7] = q1 ^ q4 ^ q6;
|
||||
q[6] = q0 ^ q3 ^ q5;
|
||||
q[5] = q7 ^ q2 ^ q4;
|
||||
q[4] = q6 ^ q1 ^ q3;
|
||||
q[3] = q5 ^ q0 ^ q2;
|
||||
q[2] = q4 ^ q7 ^ q1;
|
||||
q[1] = q3 ^ q6 ^ q0;
|
||||
q[0] = q2 ^ q5 ^ q7;
|
||||
|
||||
br_aes_ct64_bitslice_Sbox(q);
|
||||
|
||||
q0 = ~q[0];
|
||||
q1 = ~q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = ~q[5];
|
||||
q6 = ~q[6];
|
||||
q7 = q[7];
|
||||
q[7] = q1 ^ q4 ^ q6;
|
||||
q[6] = q0 ^ q3 ^ q5;
|
||||
q[5] = q7 ^ q2 ^ q4;
|
||||
q[4] = q6 ^ q1 ^ q3;
|
||||
q[3] = q5 ^ q0 ^ q2;
|
||||
q[2] = q4 ^ q7 ^ q1;
|
||||
q[1] = q3 ^ q6 ^ q0;
|
||||
q[0] = q2 ^ q5 ^ q7;
|
||||
}
|
||||
|
||||
static void
|
||||
add_round_key(uint64_t *q, const uint64_t *sk)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
q[i] ^= sk[i];
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
inv_shift_rows(uint64_t *q)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
uint64_t x;
|
||||
|
||||
x = q[i];
|
||||
q[i] = (x & (uint64_t)0x000000000000FFFF)
|
||||
| ((x & (uint64_t)0x000000000FFF0000) << 4)
|
||||
| ((x & (uint64_t)0x00000000F0000000) >> 12)
|
||||
| ((x & (uint64_t)0x000000FF00000000) << 8)
|
||||
| ((x & (uint64_t)0x0000FF0000000000) >> 8)
|
||||
| ((x & (uint64_t)0x000F000000000000) << 12)
|
||||
| ((x & (uint64_t)0xFFF0000000000000) >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint64_t
|
||||
rotr32(uint64_t x)
|
||||
{
|
||||
return (x << 32) | (x >> 32);
|
||||
}
|
||||
|
||||
static void
|
||||
inv_mix_columns(uint64_t *q)
|
||||
{
|
||||
uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
|
||||
|
||||
q0 = q[0];
|
||||
q1 = q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = q[5];
|
||||
q6 = q[6];
|
||||
q7 = q[7];
|
||||
r0 = (q0 >> 16) | (q0 << 48);
|
||||
r1 = (q1 >> 16) | (q1 << 48);
|
||||
r2 = (q2 >> 16) | (q2 << 48);
|
||||
r3 = (q3 >> 16) | (q3 << 48);
|
||||
r4 = (q4 >> 16) | (q4 << 48);
|
||||
r5 = (q5 >> 16) | (q5 << 48);
|
||||
r6 = (q6 >> 16) | (q6 << 48);
|
||||
r7 = (q7 >> 16) | (q7 << 48);
|
||||
|
||||
q[0] = q5 ^ q6 ^ q7 ^ r0 ^ r5 ^ r7 ^ rotr32(q0 ^ q5 ^ q6 ^ r0 ^ r5);
|
||||
q[1] = q0 ^ q5 ^ r0 ^ r1 ^ r5 ^ r6 ^ r7 ^ rotr32(q1 ^ q5 ^ q7 ^ r1 ^ r5 ^ r6);
|
||||
q[2] = q0 ^ q1 ^ q6 ^ r1 ^ r2 ^ r6 ^ r7 ^ rotr32(q0 ^ q2 ^ q6 ^ r2 ^ r6 ^ r7);
|
||||
q[3] = q0 ^ q1 ^ q2 ^ q5 ^ q6 ^ r0 ^ r2 ^ r3 ^ r5 ^ rotr32(q0 ^ q1 ^ q3 ^ q5 ^ q6 ^ q7 ^ r0 ^ r3 ^ r5 ^ r7);
|
||||
q[4] = q1 ^ q2 ^ q3 ^ q5 ^ r1 ^ r3 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr32(q1 ^ q2 ^ q4 ^ q5 ^ q7 ^ r1 ^ r4 ^ r5 ^ r6);
|
||||
q[5] = q2 ^ q3 ^ q4 ^ q6 ^ r2 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr32(q2 ^ q3 ^ q5 ^ q6 ^ r2 ^ r5 ^ r6 ^ r7);
|
||||
q[6] = q3 ^ q4 ^ q5 ^ q7 ^ r3 ^ r5 ^ r6 ^ r7 ^ rotr32(q3 ^ q4 ^ q6 ^ q7 ^ r3 ^ r6 ^ r7);
|
||||
q[7] = q4 ^ q5 ^ q6 ^ r4 ^ r6 ^ r7 ^ rotr32(q4 ^ q5 ^ q7 ^ r4 ^ r7);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_bitslice_decrypt(unsigned num_rounds,
|
||||
const uint64_t *skey, uint64_t *q)
|
||||
{
|
||||
unsigned u;
|
||||
|
||||
add_round_key(q, skey + (num_rounds << 3));
|
||||
for (u = num_rounds - 1; u > 0; u --) {
|
||||
inv_shift_rows(q);
|
||||
br_aes_ct64_bitslice_invSbox(q);
|
||||
add_round_key(q, skey + (u << 3));
|
||||
inv_mix_columns(q);
|
||||
}
|
||||
inv_shift_rows(q);
|
||||
br_aes_ct64_bitslice_invSbox(q);
|
||||
add_round_key(q, skey);
|
||||
}
|
||||
115
src/symcipher/aes_ct64_enc.c
Normal file
115
src/symcipher/aes_ct64_enc.c
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
static inline void
|
||||
add_round_key(uint64_t *q, const uint64_t *sk)
|
||||
{
|
||||
q[0] ^= sk[0];
|
||||
q[1] ^= sk[1];
|
||||
q[2] ^= sk[2];
|
||||
q[3] ^= sk[3];
|
||||
q[4] ^= sk[4];
|
||||
q[5] ^= sk[5];
|
||||
q[6] ^= sk[6];
|
||||
q[7] ^= sk[7];
|
||||
}
|
||||
|
||||
static inline void
|
||||
shift_rows(uint64_t *q)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
uint64_t x;
|
||||
|
||||
x = q[i];
|
||||
q[i] = (x & (uint64_t)0x000000000000FFFF)
|
||||
| ((x & (uint64_t)0x00000000FFF00000) >> 4)
|
||||
| ((x & (uint64_t)0x00000000000F0000) << 12)
|
||||
| ((x & (uint64_t)0x0000FF0000000000) >> 8)
|
||||
| ((x & (uint64_t)0x000000FF00000000) << 8)
|
||||
| ((x & (uint64_t)0xF000000000000000) >> 12)
|
||||
| ((x & (uint64_t)0x0FFF000000000000) << 4);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint64_t
|
||||
rotr32(uint64_t x)
|
||||
{
|
||||
return (x << 32) | (x >> 32);
|
||||
}
|
||||
|
||||
static inline void
|
||||
mix_columns(uint64_t *q)
|
||||
{
|
||||
uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
|
||||
|
||||
q0 = q[0];
|
||||
q1 = q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = q[5];
|
||||
q6 = q[6];
|
||||
q7 = q[7];
|
||||
r0 = (q0 >> 16) | (q0 << 48);
|
||||
r1 = (q1 >> 16) | (q1 << 48);
|
||||
r2 = (q2 >> 16) | (q2 << 48);
|
||||
r3 = (q3 >> 16) | (q3 << 48);
|
||||
r4 = (q4 >> 16) | (q4 << 48);
|
||||
r5 = (q5 >> 16) | (q5 << 48);
|
||||
r6 = (q6 >> 16) | (q6 << 48);
|
||||
r7 = (q7 >> 16) | (q7 << 48);
|
||||
|
||||
q[0] = q7 ^ r7 ^ r0 ^ rotr32(q0 ^ r0);
|
||||
q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr32(q1 ^ r1);
|
||||
q[2] = q1 ^ r1 ^ r2 ^ rotr32(q2 ^ r2);
|
||||
q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr32(q3 ^ r3);
|
||||
q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr32(q4 ^ r4);
|
||||
q[5] = q4 ^ r4 ^ r5 ^ rotr32(q5 ^ r5);
|
||||
q[6] = q5 ^ r5 ^ r6 ^ rotr32(q6 ^ r6);
|
||||
q[7] = q6 ^ r6 ^ r7 ^ rotr32(q7 ^ r7);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_bitslice_encrypt(unsigned num_rounds,
|
||||
const uint64_t *skey, uint64_t *q)
|
||||
{
|
||||
unsigned u;
|
||||
|
||||
add_round_key(q, skey);
|
||||
for (u = 1; u < num_rounds; u ++) {
|
||||
br_aes_ct64_bitslice_Sbox(q);
|
||||
shift_rows(q);
|
||||
mix_columns(q);
|
||||
add_round_key(q, skey + (u << 3));
|
||||
}
|
||||
br_aes_ct64_bitslice_Sbox(q);
|
||||
shift_rows(q);
|
||||
add_round_key(q, skey + (num_rounds << 3));
|
||||
}
|
||||
111
src/symcipher/aes_ct_cbcdec.c
Normal file
111
src/symcipher/aes_ct_cbcdec.c
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_cbcdec_init(br_aes_ct_cbcdec_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct_cbcdec_vtable;
|
||||
ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_cbcdec_run(const br_aes_ct_cbcdec_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
uint32_t iv0, iv1, iv2, iv3;
|
||||
uint32_t sk_exp[120];
|
||||
|
||||
br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
ivbuf = iv;
|
||||
iv0 = br_dec32le(ivbuf);
|
||||
iv1 = br_dec32le(ivbuf + 4);
|
||||
iv2 = br_dec32le(ivbuf + 8);
|
||||
iv3 = br_dec32le(ivbuf + 12);
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint32_t q[8], sq[8];
|
||||
|
||||
q[0] = br_dec32le(buf);
|
||||
q[2] = br_dec32le(buf + 4);
|
||||
q[4] = br_dec32le(buf + 8);
|
||||
q[6] = br_dec32le(buf + 12);
|
||||
if (len >= 32) {
|
||||
q[1] = br_dec32le(buf + 16);
|
||||
q[3] = br_dec32le(buf + 20);
|
||||
q[5] = br_dec32le(buf + 24);
|
||||
q[7] = br_dec32le(buf + 28);
|
||||
} else {
|
||||
q[1] = 0;
|
||||
q[3] = 0;
|
||||
q[5] = 0;
|
||||
q[7] = 0;
|
||||
}
|
||||
memcpy(sq, q, sizeof q);
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_decrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct_ortho(q);
|
||||
br_enc32le(buf, q[0] ^ iv0);
|
||||
br_enc32le(buf + 4, q[2] ^ iv1);
|
||||
br_enc32le(buf + 8, q[4] ^ iv2);
|
||||
br_enc32le(buf + 12, q[6] ^ iv3);
|
||||
if (len < 32) {
|
||||
iv0 = sq[0];
|
||||
iv1 = sq[2];
|
||||
iv2 = sq[4];
|
||||
iv3 = sq[6];
|
||||
break;
|
||||
}
|
||||
br_enc32le(buf + 16, q[1] ^ sq[0]);
|
||||
br_enc32le(buf + 20, q[3] ^ sq[2]);
|
||||
br_enc32le(buf + 24, q[5] ^ sq[4]);
|
||||
br_enc32le(buf + 28, q[7] ^ sq[6]);
|
||||
iv0 = sq[1];
|
||||
iv1 = sq[3];
|
||||
iv2 = sq[5];
|
||||
iv3 = sq[7];
|
||||
buf += 32;
|
||||
len -= 32;
|
||||
}
|
||||
br_enc32le(ivbuf, iv0);
|
||||
br_enc32le(ivbuf + 4, iv1);
|
||||
br_enc32le(ivbuf + 8, iv2);
|
||||
br_enc32le(ivbuf + 12, iv3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcdec_class br_aes_ct_cbcdec_vtable = {
|
||||
sizeof(br_aes_ct_cbcdec_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcdec_class **, const void *, size_t))
|
||||
&br_aes_ct_cbcdec_init,
|
||||
(void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
|
||||
&br_aes_ct_cbcdec_run
|
||||
};
|
||||
91
src/symcipher/aes_ct_cbcenc.c
Normal file
91
src/symcipher/aes_ct_cbcenc.c
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_cbcenc_init(br_aes_ct_cbcenc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct_cbcenc_vtable;
|
||||
ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_cbcenc_run(const br_aes_ct_cbcenc_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
uint32_t q[8];
|
||||
uint32_t iv0, iv1, iv2, iv3;
|
||||
uint32_t sk_exp[120];
|
||||
|
||||
q[1] = 0;
|
||||
q[3] = 0;
|
||||
q[5] = 0;
|
||||
q[7] = 0;
|
||||
br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
ivbuf = iv;
|
||||
iv0 = br_dec32le(ivbuf);
|
||||
iv1 = br_dec32le(ivbuf + 4);
|
||||
iv2 = br_dec32le(ivbuf + 8);
|
||||
iv3 = br_dec32le(ivbuf + 12);
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
q[0] = iv0 ^ br_dec32le(buf);
|
||||
q[2] = iv1 ^ br_dec32le(buf + 4);
|
||||
q[4] = iv2 ^ br_dec32le(buf + 8);
|
||||
q[6] = iv3 ^ br_dec32le(buf + 12);
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct_ortho(q);
|
||||
iv0 = q[0];
|
||||
iv1 = q[2];
|
||||
iv2 = q[4];
|
||||
iv3 = q[6];
|
||||
br_enc32le(buf, iv0);
|
||||
br_enc32le(buf + 4, iv1);
|
||||
br_enc32le(buf + 8, iv2);
|
||||
br_enc32le(buf + 12, iv3);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
br_enc32le(ivbuf, iv0);
|
||||
br_enc32le(ivbuf + 4, iv1);
|
||||
br_enc32le(ivbuf + 8, iv2);
|
||||
br_enc32le(ivbuf + 12, iv3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcenc_class br_aes_ct_cbcenc_vtable = {
|
||||
sizeof(br_aes_ct_cbcenc_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcenc_class **, const void *, size_t))
|
||||
&br_aes_ct_cbcenc_init,
|
||||
(void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
|
||||
&br_aes_ct_cbcenc_run
|
||||
};
|
||||
116
src/symcipher/aes_ct_ctr.c
Normal file
116
src/symcipher/aes_ct_ctr.c
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_ctr_init(br_aes_ct_ctr_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct_ctr_vtable;
|
||||
ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
static void
|
||||
xorbuf(void *dst, const void *src, size_t len)
|
||||
{
|
||||
unsigned char *d;
|
||||
const unsigned char *s;
|
||||
|
||||
d = dst;
|
||||
s = src;
|
||||
while (len -- > 0) {
|
||||
*d ++ ^= *s ++;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
uint32_t
|
||||
br_aes_ct_ctr_run(const br_aes_ct_ctr_keys *ctx,
|
||||
const void *iv, uint32_t cc, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
const unsigned char *ivbuf;
|
||||
uint32_t iv0, iv1, iv2;
|
||||
uint32_t sk_exp[120];
|
||||
|
||||
br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
ivbuf = iv;
|
||||
iv0 = br_dec32le(ivbuf);
|
||||
iv1 = br_dec32le(ivbuf + 4);
|
||||
iv2 = br_dec32le(ivbuf + 8);
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint32_t q[8];
|
||||
unsigned char tmp[32];
|
||||
|
||||
/*
|
||||
* TODO: see if we can save on the first br_aes_ct_ortho()
|
||||
* call, since iv0/iv1/iv2 are constant for the whole run.
|
||||
*/
|
||||
q[0] = q[1] = iv0;
|
||||
q[2] = q[3] = iv1;
|
||||
q[4] = q[5] = iv2;
|
||||
q[6] = br_swap32(cc);
|
||||
q[7] = br_swap32(cc + 1);
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct_ortho(q);
|
||||
br_enc32le(tmp, q[0]);
|
||||
br_enc32le(tmp + 4, q[2]);
|
||||
br_enc32le(tmp + 8, q[4]);
|
||||
br_enc32le(tmp + 12, q[6]);
|
||||
br_enc32le(tmp + 16, q[1]);
|
||||
br_enc32le(tmp + 20, q[3]);
|
||||
br_enc32le(tmp + 24, q[5]);
|
||||
br_enc32le(tmp + 28, q[7]);
|
||||
|
||||
if (len <= 32) {
|
||||
xorbuf(buf, tmp, len);
|
||||
cc ++;
|
||||
if (len > 16) {
|
||||
cc ++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
xorbuf(buf, tmp, 32);
|
||||
buf += 32;
|
||||
len -= 32;
|
||||
cc += 2;
|
||||
}
|
||||
return cc;
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_ctr_class br_aes_ct_ctr_vtable = {
|
||||
sizeof(br_aes_ct_ctr_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_ctr_class **, const void *, size_t))
|
||||
&br_aes_ct_ctr_init,
|
||||
(uint32_t (*)(const br_block_ctr_class *const *,
|
||||
const void *, uint32_t, void *, size_t))
|
||||
&br_aes_ct_ctr_run
|
||||
};
|
||||
170
src/symcipher/aes_ct_dec.c
Normal file
170
src/symcipher/aes_ct_dec.c
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_bitslice_invSbox(uint32_t *q)
|
||||
{
|
||||
/*
|
||||
* AES S-box is:
|
||||
* S(x) = A(I(x)) ^ 0x63
|
||||
* where I() is inversion in GF(256), and A() is a linear
|
||||
* transform (0 is formally defined to be its own inverse).
|
||||
* Since inversion is an involution, the inverse S-box can be
|
||||
* computed from the S-box as:
|
||||
* iS(x) = B(S(B(x ^ 0x63)) ^ 0x63)
|
||||
* where B() is the inverse of A(). Indeed, for any y in GF(256):
|
||||
* iS(S(y)) = B(A(I(B(A(I(y)) ^ 0x63 ^ 0x63))) ^ 0x63 ^ 0x63) = y
|
||||
*
|
||||
* Note: we reuse the implementation of the forward S-box,
|
||||
* instead of duplicating it here, so that total code size is
|
||||
* lower. By merging the B() transforms into the S-box circuit
|
||||
* we could make faster CBC decryption, but CBC decryption is
|
||||
* already quite faster than CBC encryption because we can
|
||||
* process two blocks in parallel.
|
||||
*/
|
||||
uint32_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
|
||||
q0 = ~q[0];
|
||||
q1 = ~q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = ~q[5];
|
||||
q6 = ~q[6];
|
||||
q7 = q[7];
|
||||
q[7] = q1 ^ q4 ^ q6;
|
||||
q[6] = q0 ^ q3 ^ q5;
|
||||
q[5] = q7 ^ q2 ^ q4;
|
||||
q[4] = q6 ^ q1 ^ q3;
|
||||
q[3] = q5 ^ q0 ^ q2;
|
||||
q[2] = q4 ^ q7 ^ q1;
|
||||
q[1] = q3 ^ q6 ^ q0;
|
||||
q[0] = q2 ^ q5 ^ q7;
|
||||
|
||||
br_aes_ct_bitslice_Sbox(q);
|
||||
|
||||
q0 = ~q[0];
|
||||
q1 = ~q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = ~q[5];
|
||||
q6 = ~q[6];
|
||||
q7 = q[7];
|
||||
q[7] = q1 ^ q4 ^ q6;
|
||||
q[6] = q0 ^ q3 ^ q5;
|
||||
q[5] = q7 ^ q2 ^ q4;
|
||||
q[4] = q6 ^ q1 ^ q3;
|
||||
q[3] = q5 ^ q0 ^ q2;
|
||||
q[2] = q4 ^ q7 ^ q1;
|
||||
q[1] = q3 ^ q6 ^ q0;
|
||||
q[0] = q2 ^ q5 ^ q7;
|
||||
}
|
||||
|
||||
static void
|
||||
add_round_key(uint32_t *q, const uint32_t *sk)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
q[i] ^= sk[i];
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
inv_shift_rows(uint32_t *q)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
uint32_t x;
|
||||
|
||||
x = q[i];
|
||||
q[i] = (x & 0x000000FF)
|
||||
| ((x & 0x00003F00) << 2) | ((x & 0x0000C000) >> 6)
|
||||
| ((x & 0x000F0000) << 4) | ((x & 0x00F00000) >> 4)
|
||||
| ((x & 0x03000000) << 6) | ((x & 0xFC000000) >> 2);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t
|
||||
rotr16(uint32_t x)
|
||||
{
|
||||
return (x << 16) | (x >> 16);
|
||||
}
|
||||
|
||||
static void
|
||||
inv_mix_columns(uint32_t *q)
|
||||
{
|
||||
uint32_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
uint32_t r0, r1, r2, r3, r4, r5, r6, r7;
|
||||
|
||||
q0 = q[0];
|
||||
q1 = q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = q[5];
|
||||
q6 = q[6];
|
||||
q7 = q[7];
|
||||
r0 = (q0 >> 8) | (q0 << 24);
|
||||
r1 = (q1 >> 8) | (q1 << 24);
|
||||
r2 = (q2 >> 8) | (q2 << 24);
|
||||
r3 = (q3 >> 8) | (q3 << 24);
|
||||
r4 = (q4 >> 8) | (q4 << 24);
|
||||
r5 = (q5 >> 8) | (q5 << 24);
|
||||
r6 = (q6 >> 8) | (q6 << 24);
|
||||
r7 = (q7 >> 8) | (q7 << 24);
|
||||
|
||||
q[0] = q5 ^ q6 ^ q7 ^ r0 ^ r5 ^ r7 ^ rotr16(q0 ^ q5 ^ q6 ^ r0 ^ r5);
|
||||
q[1] = q0 ^ q5 ^ r0 ^ r1 ^ r5 ^ r6 ^ r7 ^ rotr16(q1 ^ q5 ^ q7 ^ r1 ^ r5 ^ r6);
|
||||
q[2] = q0 ^ q1 ^ q6 ^ r1 ^ r2 ^ r6 ^ r7 ^ rotr16(q0 ^ q2 ^ q6 ^ r2 ^ r6 ^ r7);
|
||||
q[3] = q0 ^ q1 ^ q2 ^ q5 ^ q6 ^ r0 ^ r2 ^ r3 ^ r5 ^ rotr16(q0 ^ q1 ^ q3 ^ q5 ^ q6 ^ q7 ^ r0 ^ r3 ^ r5 ^ r7);
|
||||
q[4] = q1 ^ q2 ^ q3 ^ q5 ^ r1 ^ r3 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr16(q1 ^ q2 ^ q4 ^ q5 ^ q7 ^ r1 ^ r4 ^ r5 ^ r6);
|
||||
q[5] = q2 ^ q3 ^ q4 ^ q6 ^ r2 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr16(q2 ^ q3 ^ q5 ^ q6 ^ r2 ^ r5 ^ r6 ^ r7);
|
||||
q[6] = q3 ^ q4 ^ q5 ^ q7 ^ r3 ^ r5 ^ r6 ^ r7 ^ rotr16(q3 ^ q4 ^ q6 ^ q7 ^ r3 ^ r6 ^ r7);
|
||||
q[7] = q4 ^ q5 ^ q6 ^ r4 ^ r6 ^ r7 ^ rotr16(q4 ^ q5 ^ q7 ^ r4 ^ r7);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_bitslice_decrypt(unsigned num_rounds,
|
||||
const uint32_t *skey, uint32_t *q)
|
||||
{
|
||||
unsigned u;
|
||||
|
||||
add_round_key(q, skey + (num_rounds << 3));
|
||||
for (u = num_rounds - 1; u > 0; u --) {
|
||||
inv_shift_rows(q);
|
||||
br_aes_ct_bitslice_invSbox(q);
|
||||
add_round_key(q, skey + (u << 3));
|
||||
inv_mix_columns(q);
|
||||
}
|
||||
inv_shift_rows(q);
|
||||
br_aes_ct_bitslice_invSbox(q);
|
||||
add_round_key(q, skey);
|
||||
}
|
||||
112
src/symcipher/aes_ct_enc.c
Normal file
112
src/symcipher/aes_ct_enc.c
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
static inline void
|
||||
add_round_key(uint32_t *q, const uint32_t *sk)
|
||||
{
|
||||
q[0] ^= sk[0];
|
||||
q[1] ^= sk[1];
|
||||
q[2] ^= sk[2];
|
||||
q[3] ^= sk[3];
|
||||
q[4] ^= sk[4];
|
||||
q[5] ^= sk[5];
|
||||
q[6] ^= sk[6];
|
||||
q[7] ^= sk[7];
|
||||
}
|
||||
|
||||
static inline void
|
||||
shift_rows(uint32_t *q)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
uint32_t x;
|
||||
|
||||
x = q[i];
|
||||
q[i] = (x & 0x000000FF)
|
||||
| ((x & 0x0000FC00) >> 2) | ((x & 0x00000300) << 6)
|
||||
| ((x & 0x00F00000) >> 4) | ((x & 0x000F0000) << 4)
|
||||
| ((x & 0xC0000000) >> 6) | ((x & 0x3F000000) << 2);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t
|
||||
rotr16(uint32_t x)
|
||||
{
|
||||
return (x << 16) | (x >> 16);
|
||||
}
|
||||
|
||||
static inline void
|
||||
mix_columns(uint32_t *q)
|
||||
{
|
||||
uint32_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
uint32_t r0, r1, r2, r3, r4, r5, r6, r7;
|
||||
|
||||
q0 = q[0];
|
||||
q1 = q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = q[5];
|
||||
q6 = q[6];
|
||||
q7 = q[7];
|
||||
r0 = (q0 >> 8) | (q0 << 24);
|
||||
r1 = (q1 >> 8) | (q1 << 24);
|
||||
r2 = (q2 >> 8) | (q2 << 24);
|
||||
r3 = (q3 >> 8) | (q3 << 24);
|
||||
r4 = (q4 >> 8) | (q4 << 24);
|
||||
r5 = (q5 >> 8) | (q5 << 24);
|
||||
r6 = (q6 >> 8) | (q6 << 24);
|
||||
r7 = (q7 >> 8) | (q7 << 24);
|
||||
|
||||
q[0] = q7 ^ r7 ^ r0 ^ rotr16(q0 ^ r0);
|
||||
q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr16(q1 ^ r1);
|
||||
q[2] = q1 ^ r1 ^ r2 ^ rotr16(q2 ^ r2);
|
||||
q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr16(q3 ^ r3);
|
||||
q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr16(q4 ^ r4);
|
||||
q[5] = q4 ^ r4 ^ r5 ^ rotr16(q5 ^ r5);
|
||||
q[6] = q5 ^ r5 ^ r6 ^ rotr16(q6 ^ r6);
|
||||
q[7] = q6 ^ r6 ^ r7 ^ rotr16(q7 ^ r7);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_bitslice_encrypt(unsigned num_rounds,
|
||||
const uint32_t *skey, uint32_t *q)
|
||||
{
|
||||
unsigned u;
|
||||
|
||||
add_round_key(q, skey);
|
||||
for (u = 1; u < num_rounds; u ++) {
|
||||
br_aes_ct_bitslice_Sbox(q);
|
||||
shift_rows(q);
|
||||
mix_columns(q);
|
||||
add_round_key(q, skey + (u << 3));
|
||||
}
|
||||
br_aes_ct_bitslice_Sbox(q);
|
||||
shift_rows(q);
|
||||
add_round_key(q, skey + (num_rounds << 3));
|
||||
}
|
||||
69
src/symcipher/aes_small_cbcdec.c
Normal file
69
src/symcipher/aes_small_cbcdec.c
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_cbcdec_init(br_aes_small_cbcdec_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_small_cbcdec_vtable;
|
||||
ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_cbcdec_run(const br_aes_small_cbcdec_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
|
||||
ivbuf = iv;
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
unsigned char tmp[16];
|
||||
int i;
|
||||
|
||||
memcpy(tmp, buf, 16);
|
||||
br_aes_small_decrypt(ctx->num_rounds, ctx->skey, buf);
|
||||
for (i = 0; i < 16; i ++) {
|
||||
buf[i] ^= ivbuf[i];
|
||||
}
|
||||
memcpy(ivbuf, tmp, 16);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcdec_class br_aes_small_cbcdec_vtable = {
|
||||
sizeof(br_aes_small_cbcdec_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcdec_class **, const void *, size_t))
|
||||
&br_aes_small_cbcdec_init,
|
||||
(void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
|
||||
&br_aes_small_cbcdec_run
|
||||
};
|
||||
67
src/symcipher/aes_small_cbcenc.c
Normal file
67
src/symcipher/aes_small_cbcenc.c
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_cbcenc_init(br_aes_small_cbcenc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_small_cbcenc_vtable;
|
||||
ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_cbcenc_run(const br_aes_small_cbcenc_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
|
||||
ivbuf = iv;
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i ++) {
|
||||
buf[i] ^= ivbuf[i];
|
||||
}
|
||||
br_aes_small_encrypt(ctx->num_rounds, ctx->skey, buf);
|
||||
memcpy(ivbuf, buf, 16);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcenc_class br_aes_small_cbcenc_vtable = {
|
||||
sizeof(br_aes_small_cbcenc_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcenc_class **, const void *, size_t))
|
||||
&br_aes_small_cbcenc_init,
|
||||
(void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
|
||||
&br_aes_small_cbcenc_run
|
||||
};
|
||||
84
src/symcipher/aes_small_ctr.c
Normal file
84
src/symcipher/aes_small_ctr.c
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_ctr_init(br_aes_small_ctr_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_small_ctr_vtable;
|
||||
ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
static void
|
||||
xorbuf(void *dst, const void *src, size_t len)
|
||||
{
|
||||
unsigned char *d;
|
||||
const unsigned char *s;
|
||||
|
||||
d = dst;
|
||||
s = src;
|
||||
while (len -- > 0) {
|
||||
*d ++ ^= *s ++;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
uint32_t
|
||||
br_aes_small_ctr_run(const br_aes_small_ctr_keys *ctx,
|
||||
const void *iv, uint32_t cc, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
unsigned char tmp[16];
|
||||
|
||||
memcpy(tmp, iv, 12);
|
||||
br_enc32be(tmp + 12, cc ++);
|
||||
br_aes_small_encrypt(ctx->num_rounds, ctx->skey, tmp);
|
||||
if (len <= 16) {
|
||||
xorbuf(buf, tmp, len);
|
||||
break;
|
||||
}
|
||||
xorbuf(buf, tmp, 16);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
return cc;
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_ctr_class br_aes_small_ctr_vtable = {
|
||||
sizeof(br_aes_small_ctr_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_ctr_class **, const void *, size_t))
|
||||
&br_aes_small_ctr_init,
|
||||
(uint32_t (*)(const br_block_ctr_class *const *,
|
||||
const void *, uint32_t, void *, size_t))
|
||||
&br_aes_small_ctr_run
|
||||
};
|
||||
176
src/symcipher/aes_small_dec.c
Normal file
176
src/symcipher/aes_small_dec.c
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/*
|
||||
* Inverse S-box.
|
||||
*/
|
||||
static const unsigned char iS[] = {
|
||||
0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E,
|
||||
0x81, 0xF3, 0xD7, 0xFB, 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
|
||||
0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 0x54, 0x7B, 0x94, 0x32,
|
||||
0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
|
||||
0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49,
|
||||
0x6D, 0x8B, 0xD1, 0x25, 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16,
|
||||
0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 0x6C, 0x70, 0x48, 0x50,
|
||||
0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
|
||||
0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05,
|
||||
0xB8, 0xB3, 0x45, 0x06, 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02,
|
||||
0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 0x3A, 0x91, 0x11, 0x41,
|
||||
0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
|
||||
0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8,
|
||||
0x1C, 0x75, 0xDF, 0x6E, 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89,
|
||||
0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 0xFC, 0x56, 0x3E, 0x4B,
|
||||
0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
|
||||
0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59,
|
||||
0x27, 0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D,
|
||||
0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0, 0xE0, 0x3B, 0x4D,
|
||||
0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
|
||||
0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63,
|
||||
0x55, 0x21, 0x0C, 0x7D
|
||||
};
|
||||
|
||||
static void
|
||||
add_round_key(unsigned *state, const uint32_t *skeys)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i += 4) {
|
||||
uint32_t k;
|
||||
|
||||
k = *skeys ++;
|
||||
state[i + 0] ^= (unsigned)(k >> 24);
|
||||
state[i + 1] ^= (unsigned)(k >> 16) & 0xFF;
|
||||
state[i + 2] ^= (unsigned)(k >> 8) & 0xFF;
|
||||
state[i + 3] ^= (unsigned)k & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
inv_sub_bytes(unsigned *state)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i ++) {
|
||||
state[i] = iS[state[i]];
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
inv_shift_rows(unsigned *state)
|
||||
{
|
||||
unsigned tmp;
|
||||
|
||||
tmp = state[13];
|
||||
state[13] = state[9];
|
||||
state[9] = state[5];
|
||||
state[5] = state[1];
|
||||
state[1] = tmp;
|
||||
|
||||
tmp = state[2];
|
||||
state[2] = state[10];
|
||||
state[10] = tmp;
|
||||
tmp = state[6];
|
||||
state[6] = state[14];
|
||||
state[14] = tmp;
|
||||
|
||||
tmp = state[3];
|
||||
state[3] = state[7];
|
||||
state[7] = state[11];
|
||||
state[11] = state[15];
|
||||
state[15] = tmp;
|
||||
}
|
||||
|
||||
static inline unsigned
|
||||
gf256red(unsigned x)
|
||||
{
|
||||
unsigned y;
|
||||
|
||||
y = x >> 8;
|
||||
return (x ^ y ^ (y << 1) ^ (y << 3) ^ (y << 4)) & 0xFF;
|
||||
}
|
||||
|
||||
static void
|
||||
inv_mix_columns(unsigned *state)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i += 4) {
|
||||
unsigned s0, s1, s2, s3;
|
||||
unsigned t0, t1, t2, t3;
|
||||
|
||||
s0 = state[i + 0];
|
||||
s1 = state[i + 1];
|
||||
s2 = state[i + 2];
|
||||
s3 = state[i + 3];
|
||||
t0 = (s0 << 1) ^ (s0 << 2) ^ (s0 << 3)
|
||||
^ s1 ^ (s1 << 1) ^ (s1 << 3)
|
||||
^ s2 ^ (s2 << 2) ^ (s2 << 3)
|
||||
^ s3 ^ (s3 << 3);
|
||||
t1 = s0 ^ (s0 << 3)
|
||||
^ (s1 << 1) ^ (s1 << 2) ^ (s1 << 3)
|
||||
^ s2 ^ (s2 << 1) ^ (s2 << 3)
|
||||
^ s3 ^ (s3 << 2) ^ (s3 << 3);
|
||||
t2 = s0 ^ (s0 << 2) ^ (s0 << 3)
|
||||
^ s1 ^ (s1 << 3)
|
||||
^ (s2 << 1) ^ (s2 << 2) ^ (s2 << 3)
|
||||
^ s3 ^ (s3 << 1) ^ (s3 << 3);
|
||||
t3 = s0 ^ (s0 << 1) ^ (s0 << 3)
|
||||
^ s1 ^ (s1 << 2) ^ (s1 << 3)
|
||||
^ s2 ^ (s2 << 3)
|
||||
^ (s3 << 1) ^ (s3 << 2) ^ (s3 << 3);
|
||||
state[i + 0] = gf256red(t0);
|
||||
state[i + 1] = gf256red(t1);
|
||||
state[i + 2] = gf256red(t2);
|
||||
state[i + 3] = gf256red(t3);
|
||||
}
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_small_decrypt(unsigned num_rounds, const uint32_t *skey, void *data)
|
||||
{
|
||||
unsigned char *buf;
|
||||
unsigned state[16];
|
||||
unsigned u;
|
||||
|
||||
buf = data;
|
||||
for (u = 0; u < 16; u ++) {
|
||||
state[u] = buf[u];
|
||||
}
|
||||
add_round_key(state, skey + (num_rounds << 2));
|
||||
for (u = num_rounds - 1; u > 0; u --) {
|
||||
inv_shift_rows(state);
|
||||
inv_sub_bytes(state);
|
||||
add_round_key(state, skey + (u << 2));
|
||||
inv_mix_columns(state);
|
||||
}
|
||||
inv_shift_rows(state);
|
||||
inv_sub_bytes(state);
|
||||
add_round_key(state, skey);
|
||||
for (u = 0; u < 16; u ++) {
|
||||
buf[u] = state[u];
|
||||
}
|
||||
}
|
||||
129
src/symcipher/aes_small_enc.c
Normal file
129
src/symcipher/aes_small_enc.c
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
#define S br_aes_S
|
||||
|
||||
static void
|
||||
add_round_key(unsigned *state, const uint32_t *skeys)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i += 4) {
|
||||
uint32_t k;
|
||||
|
||||
k = *skeys ++;
|
||||
state[i + 0] ^= (unsigned)(k >> 24);
|
||||
state[i + 1] ^= (unsigned)(k >> 16) & 0xFF;
|
||||
state[i + 2] ^= (unsigned)(k >> 8) & 0xFF;
|
||||
state[i + 3] ^= (unsigned)k & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sub_bytes(unsigned *state)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i ++) {
|
||||
state[i] = S[state[i]];
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
shift_rows(unsigned *state)
|
||||
{
|
||||
unsigned tmp;
|
||||
|
||||
tmp = state[1];
|
||||
state[1] = state[5];
|
||||
state[5] = state[9];
|
||||
state[9] = state[13];
|
||||
state[13] = tmp;
|
||||
|
||||
tmp = state[2];
|
||||
state[2] = state[10];
|
||||
state[10] = tmp;
|
||||
tmp = state[6];
|
||||
state[6] = state[14];
|
||||
state[14] = tmp;
|
||||
|
||||
tmp = state[15];
|
||||
state[15] = state[11];
|
||||
state[11] = state[7];
|
||||
state[7] = state[3];
|
||||
state[3] = tmp;
|
||||
}
|
||||
|
||||
static void
|
||||
mix_columns(unsigned *state)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i += 4) {
|
||||
unsigned s0, s1, s2, s3;
|
||||
unsigned t0, t1, t2, t3;
|
||||
|
||||
s0 = state[i + 0];
|
||||
s1 = state[i + 1];
|
||||
s2 = state[i + 2];
|
||||
s3 = state[i + 3];
|
||||
t0 = (s0 << 1) ^ s1 ^ (s1 << 1) ^ s2 ^ s3;
|
||||
t1 = s0 ^ (s1 << 1) ^ s2 ^ (s2 << 1) ^ s3;
|
||||
t2 = s0 ^ s1 ^ (s2 << 1) ^ s3 ^ (s3 << 1);
|
||||
t3 = s0 ^ (s0 << 1) ^ s1 ^ s2 ^ (s3 << 1);
|
||||
state[i + 0] = t0 ^ ((unsigned)(-(int)(t0 >> 8)) & 0x11B);
|
||||
state[i + 1] = t1 ^ ((unsigned)(-(int)(t1 >> 8)) & 0x11B);
|
||||
state[i + 2] = t2 ^ ((unsigned)(-(int)(t2 >> 8)) & 0x11B);
|
||||
state[i + 3] = t3 ^ ((unsigned)(-(int)(t3 >> 8)) & 0x11B);
|
||||
}
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_small_encrypt(unsigned num_rounds, const uint32_t *skey, void *data)
|
||||
{
|
||||
unsigned char *buf;
|
||||
unsigned state[16];
|
||||
unsigned u;
|
||||
|
||||
buf = data;
|
||||
for (u = 0; u < 16; u ++) {
|
||||
state[u] = buf[u];
|
||||
}
|
||||
add_round_key(state, skey);
|
||||
for (u = 1; u < num_rounds; u ++) {
|
||||
sub_bytes(state);
|
||||
shift_rows(state);
|
||||
mix_columns(state);
|
||||
add_round_key(state, skey + (u << 2));
|
||||
}
|
||||
sub_bytes(state);
|
||||
shift_rows(state);
|
||||
add_round_key(state, skey + (num_rounds << 2));
|
||||
for (u = 0; u < 16; u ++) {
|
||||
buf[u] = state[u];
|
||||
}
|
||||
}
|
||||
411
src/symcipher/des_ct.c
Normal file
411
src/symcipher/des_ct.c
Normal file
|
|
@ -0,0 +1,411 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/*
|
||||
* During key schedule, we need to apply bit extraction PC-2 then permute
|
||||
* things into our bitslice representation. PC-2 extracts 48 bits out
|
||||
* of two 28-bit words (kl and kr), and we store these bits into two
|
||||
* 32-bit words sk0 and sk1.
|
||||
*
|
||||
* -- bit 16+x of sk0 comes from bit QL0[x] of kl
|
||||
* -- bit x of sk0 comes from bit QR0[x] of kr
|
||||
* -- bit 16+x of sk1 comes from bit QL1[x] of kl
|
||||
* -- bit x of sk1 comes from bit QR1[x] of kr
|
||||
*/
|
||||
|
||||
static const unsigned char QL0[] = {
|
||||
17, 4, 27, 23, 13, 22, 7, 18,
|
||||
16, 24, 2, 20, 1, 8, 15, 26
|
||||
};
|
||||
|
||||
static const unsigned char QR0[] = {
|
||||
25, 19, 9, 1, 5, 11, 23, 8,
|
||||
17, 0, 22, 3, 6, 20, 27, 24
|
||||
};
|
||||
|
||||
static const unsigned char QL1[] = {
|
||||
28, 28, 14, 11, 28, 28, 25, 0,
|
||||
28, 28, 5, 9, 28, 28, 12, 21
|
||||
};
|
||||
|
||||
static const unsigned char QR1[] = {
|
||||
28, 28, 15, 4, 28, 28, 26, 16,
|
||||
28, 28, 12, 7, 28, 28, 10, 14
|
||||
};
|
||||
|
||||
/*
|
||||
* 32-bit rotation. The C compiler is supposed to recognize it as a
|
||||
* rotation and use the local architecture rotation opcode (if available).
|
||||
*/
|
||||
static inline uint32_t
|
||||
rotl(uint32_t x, int n)
|
||||
{
|
||||
return (x << n) | (x >> (32 - n));
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute key schedule for 8 key bytes (produces 32 subkey words).
|
||||
*/
|
||||
static void
|
||||
keysched_unit(uint32_t *skey, const void *key)
|
||||
{
|
||||
int i;
|
||||
|
||||
br_des_keysched_unit(skey, key);
|
||||
|
||||
/*
|
||||
* Apply PC-2 + bitslicing.
|
||||
*/
|
||||
for (i = 0; i < 16; i ++) {
|
||||
uint32_t kl, kr, sk0, sk1;
|
||||
int j;
|
||||
|
||||
kl = skey[(i << 1) + 0];
|
||||
kr = skey[(i << 1) + 1];
|
||||
sk0 = 0;
|
||||
sk1 = 0;
|
||||
for (j = 0; j < 16; j ++) {
|
||||
sk0 <<= 1;
|
||||
sk1 <<= 1;
|
||||
sk0 |= ((kl >> QL0[j]) & (uint32_t)1) << 16;
|
||||
sk0 |= (kr >> QR0[j]) & (uint32_t)1;
|
||||
sk1 |= ((kl >> QL1[j]) & (uint32_t)1) << 16;
|
||||
sk1 |= (kr >> QR1[j]) & (uint32_t)1;
|
||||
}
|
||||
|
||||
skey[(i << 1) + 0] = sk0;
|
||||
skey[(i << 1) + 1] = sk1;
|
||||
}
|
||||
|
||||
#if 0
|
||||
/*
|
||||
* Speed-optimized version for PC-2 + bitslicing.
|
||||
* (Unused. Kept for reference only.)
|
||||
*/
|
||||
sk0 = kl & (uint32_t)0x00100000;
|
||||
sk0 |= (kl & (uint32_t)0x08008000) << 2;
|
||||
sk0 |= (kl & (uint32_t)0x00400000) << 4;
|
||||
sk0 |= (kl & (uint32_t)0x00800000) << 5;
|
||||
sk0 |= (kl & (uint32_t)0x00040000) << 6;
|
||||
sk0 |= (kl & (uint32_t)0x00010000) << 7;
|
||||
sk0 |= (kl & (uint32_t)0x00000100) << 10;
|
||||
sk0 |= (kl & (uint32_t)0x00022000) << 14;
|
||||
sk0 |= (kl & (uint32_t)0x00000082) << 18;
|
||||
sk0 |= (kl & (uint32_t)0x00000004) << 19;
|
||||
sk0 |= (kl & (uint32_t)0x04000000) >> 10;
|
||||
sk0 |= (kl & (uint32_t)0x00000010) << 26;
|
||||
sk0 |= (kl & (uint32_t)0x01000000) >> 2;
|
||||
|
||||
sk0 |= kr & (uint32_t)0x00000100;
|
||||
sk0 |= (kr & (uint32_t)0x00000008) << 1;
|
||||
sk0 |= (kr & (uint32_t)0x00000200) << 4;
|
||||
sk0 |= rotl(kr & (uint32_t)0x08000021, 6);
|
||||
sk0 |= (kr & (uint32_t)0x01000000) >> 24;
|
||||
sk0 |= (kr & (uint32_t)0x00000002) << 11;
|
||||
sk0 |= (kr & (uint32_t)0x00100000) >> 18;
|
||||
sk0 |= (kr & (uint32_t)0x00400000) >> 17;
|
||||
sk0 |= (kr & (uint32_t)0x00800000) >> 14;
|
||||
sk0 |= (kr & (uint32_t)0x02020000) >> 10;
|
||||
sk0 |= (kr & (uint32_t)0x00080000) >> 5;
|
||||
sk0 |= (kr & (uint32_t)0x00000040) >> 3;
|
||||
sk0 |= (kr & (uint32_t)0x00000800) >> 1;
|
||||
|
||||
sk1 = kl & (uint32_t)0x02000000;
|
||||
sk1 |= (kl & (uint32_t)0x00001000) << 5;
|
||||
sk1 |= (kl & (uint32_t)0x00000200) << 11;
|
||||
sk1 |= (kl & (uint32_t)0x00004000) << 15;
|
||||
sk1 |= (kl & (uint32_t)0x00000020) << 16;
|
||||
sk1 |= (kl & (uint32_t)0x00000800) << 17;
|
||||
sk1 |= (kl & (uint32_t)0x00000001) << 24;
|
||||
sk1 |= (kl & (uint32_t)0x00200000) >> 5;
|
||||
|
||||
sk1 |= (kr & (uint32_t)0x00000010) << 8;
|
||||
sk1 |= (kr & (uint32_t)0x04000000) >> 17;
|
||||
sk1 |= (kr & (uint32_t)0x00004000) >> 14;
|
||||
sk1 |= (kr & (uint32_t)0x00000400) >> 9;
|
||||
sk1 |= (kr & (uint32_t)0x00010000) >> 8;
|
||||
sk1 |= (kr & (uint32_t)0x00001000) >> 7;
|
||||
sk1 |= (kr & (uint32_t)0x00000080) >> 3;
|
||||
sk1 |= (kr & (uint32_t)0x00008000) >> 2;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
unsigned
|
||||
br_des_ct_keysched(uint32_t *skey, const void *key, size_t key_len)
|
||||
{
|
||||
switch (key_len) {
|
||||
case 8:
|
||||
keysched_unit(skey, key);
|
||||
return 1;
|
||||
case 16:
|
||||
keysched_unit(skey, key);
|
||||
keysched_unit(skey + 32, (const unsigned char *)key + 8);
|
||||
br_des_rev_skey(skey + 32);
|
||||
memcpy(skey + 64, skey, 32 * sizeof *skey);
|
||||
return 3;
|
||||
default:
|
||||
keysched_unit(skey, key);
|
||||
keysched_unit(skey + 32, (const unsigned char *)key + 8);
|
||||
br_des_rev_skey(skey + 32);
|
||||
keysched_unit(skey + 64, (const unsigned char *)key + 16);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* DES confusion function. This function performs expansion E (32 to
|
||||
* 48 bits), XOR with subkey, S-boxes, and permutation P.
|
||||
*/
|
||||
static inline uint32_t
|
||||
Fconf(uint32_t r0, const uint32_t *sk)
|
||||
{
|
||||
/*
|
||||
* Each 6->4 S-box is virtually turned into four 6->1 boxes; we
|
||||
* thus end up with 32 boxes that we call "T-boxes" here. We will
|
||||
* evaluate them with bitslice code.
|
||||
*
|
||||
* Each T-box is a circuit of multiplexers (sort of) and thus
|
||||
* takes 70 inputs: the 6 actual T-box inputs, and 64 constants
|
||||
* that describe the T-box output for all combinations of the
|
||||
* 6 inputs. With this model, all T-boxes are identical (with
|
||||
* distinct inputs) and thus can be executed in parallel with
|
||||
* bitslice code.
|
||||
*
|
||||
* T-boxes are numbered from 0 to 31, in least-to-most
|
||||
* significant order. Thus, S-box S1 corresponds to T-boxes 31,
|
||||
* 30, 29 and 28, in that order. T-box 'n' is computed with the
|
||||
* bits at rank 'n' in the 32-bit words.
|
||||
*
|
||||
* Words x0 to x5 contain the T-box inputs 0 to 5.
|
||||
*/
|
||||
uint32_t x0, x1, x2, x3, x4, x5, z0;
|
||||
uint32_t y0, y1, y2, y3, y4, y5, y6, y7, y8, y9;
|
||||
uint32_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
|
||||
uint32_t y20, y21, y22, y23, y24, y25, y26, y27, y28, y29;
|
||||
uint32_t y30;
|
||||
|
||||
/*
|
||||
* Spread input bits over the 6 input words x*.
|
||||
*/
|
||||
x1 = r0 & (uint32_t)0x11111111;
|
||||
x2 = (r0 >> 1) & (uint32_t)0x11111111;
|
||||
x3 = (r0 >> 2) & (uint32_t)0x11111111;
|
||||
x4 = (r0 >> 3) & (uint32_t)0x11111111;
|
||||
x1 = (x1 << 4) - x1;
|
||||
x2 = (x2 << 4) - x2;
|
||||
x3 = (x3 << 4) - x3;
|
||||
x4 = (x4 << 4) - x4;
|
||||
x0 = (x4 << 4) | (x4 >> 28);
|
||||
x5 = (x1 >> 4) | (x1 << 28);
|
||||
|
||||
/*
|
||||
* XOR with the subkey for this round.
|
||||
*/
|
||||
x0 ^= sk[0];
|
||||
x1 ^= sk[1];
|
||||
x2 ^= sk[2];
|
||||
x3 ^= sk[3];
|
||||
x4 ^= sk[4];
|
||||
x5 ^= sk[5];
|
||||
|
||||
/*
|
||||
* The T-boxes are done in parallel, since they all use a
|
||||
* "tree of multiplexer". We use "fake multiplexers":
|
||||
*
|
||||
* y = a ^ (x & b)
|
||||
*
|
||||
* computes y as either 'a' (if x == 0) or 'a ^ b' (if x == 1).
|
||||
*/
|
||||
y0 = (uint32_t)0xEFA72C4D ^ (x0 & (uint32_t)0xEC7AC69C);
|
||||
y1 = (uint32_t)0xAEAAEDFF ^ (x0 & (uint32_t)0x500FB821);
|
||||
y2 = (uint32_t)0x37396665 ^ (x0 & (uint32_t)0x40EFA809);
|
||||
y3 = (uint32_t)0x68D7B833 ^ (x0 & (uint32_t)0xA5EC0B28);
|
||||
y4 = (uint32_t)0xC9C755BB ^ (x0 & (uint32_t)0x252CF820);
|
||||
y5 = (uint32_t)0x73FC3606 ^ (x0 & (uint32_t)0x40205801);
|
||||
y6 = (uint32_t)0xA2A0A918 ^ (x0 & (uint32_t)0xE220F929);
|
||||
y7 = (uint32_t)0x8222BD90 ^ (x0 & (uint32_t)0x44A3F9E1);
|
||||
y8 = (uint32_t)0xD6B6AC77 ^ (x0 & (uint32_t)0x794F104A);
|
||||
y9 = (uint32_t)0x3069300C ^ (x0 & (uint32_t)0x026F320B);
|
||||
y10 = (uint32_t)0x6CE0D5CC ^ (x0 & (uint32_t)0x7640B01A);
|
||||
y11 = (uint32_t)0x59A9A22D ^ (x0 & (uint32_t)0x238F1572);
|
||||
y12 = (uint32_t)0xAC6D0BD4 ^ (x0 & (uint32_t)0x7A63C083);
|
||||
y13 = (uint32_t)0x21C83200 ^ (x0 & (uint32_t)0x11CCA000);
|
||||
y14 = (uint32_t)0xA0E62188 ^ (x0 & (uint32_t)0x202F69AA);
|
||||
/* y15 = (uint32_t)0x00000000 ^ (x0 & (uint32_t)0x00000000); */
|
||||
y16 = (uint32_t)0xAF7D655A ^ (x0 & (uint32_t)0x51B33BE9);
|
||||
y17 = (uint32_t)0xF0168AA3 ^ (x0 & (uint32_t)0x3B0FE8AE);
|
||||
y18 = (uint32_t)0x90AA30C6 ^ (x0 & (uint32_t)0x90BF8816);
|
||||
y19 = (uint32_t)0x5AB2750A ^ (x0 & (uint32_t)0x09E34F9B);
|
||||
y20 = (uint32_t)0x5391BE65 ^ (x0 & (uint32_t)0x0103BE88);
|
||||
y21 = (uint32_t)0x93372BAF ^ (x0 & (uint32_t)0x49AC8E25);
|
||||
y22 = (uint32_t)0xF288210C ^ (x0 & (uint32_t)0x922C313D);
|
||||
y23 = (uint32_t)0x920AF5C0 ^ (x0 & (uint32_t)0x70EF31B0);
|
||||
y24 = (uint32_t)0x63D312C0 ^ (x0 & (uint32_t)0x6A707100);
|
||||
y25 = (uint32_t)0x537B3006 ^ (x0 & (uint32_t)0xB97C9011);
|
||||
y26 = (uint32_t)0xA2EFB0A5 ^ (x0 & (uint32_t)0xA320C959);
|
||||
y27 = (uint32_t)0xBC8F96A5 ^ (x0 & (uint32_t)0x6EA0AB4A);
|
||||
y28 = (uint32_t)0xFAD176A5 ^ (x0 & (uint32_t)0x6953DDF8);
|
||||
y29 = (uint32_t)0x665A14A3 ^ (x0 & (uint32_t)0xF74F3E2B);
|
||||
y30 = (uint32_t)0xF2EFF0CC ^ (x0 & (uint32_t)0xF0306CAD);
|
||||
/* y31 = (uint32_t)0x00000000 ^ (x0 & (uint32_t)0x00000000); */
|
||||
|
||||
y0 = y0 ^ (x1 & y1);
|
||||
y1 = y2 ^ (x1 & y3);
|
||||
y2 = y4 ^ (x1 & y5);
|
||||
y3 = y6 ^ (x1 & y7);
|
||||
y4 = y8 ^ (x1 & y9);
|
||||
y5 = y10 ^ (x1 & y11);
|
||||
y6 = y12 ^ (x1 & y13);
|
||||
y7 = y14; /* was: y14 ^ (x1 & y15) */
|
||||
y8 = y16 ^ (x1 & y17);
|
||||
y9 = y18 ^ (x1 & y19);
|
||||
y10 = y20 ^ (x1 & y21);
|
||||
y11 = y22 ^ (x1 & y23);
|
||||
y12 = y24 ^ (x1 & y25);
|
||||
y13 = y26 ^ (x1 & y27);
|
||||
y14 = y28 ^ (x1 & y29);
|
||||
y15 = y30; /* was: y30 ^ (x1 & y31) */
|
||||
|
||||
y0 = y0 ^ (x2 & y1);
|
||||
y1 = y2 ^ (x2 & y3);
|
||||
y2 = y4 ^ (x2 & y5);
|
||||
y3 = y6 ^ (x2 & y7);
|
||||
y4 = y8 ^ (x2 & y9);
|
||||
y5 = y10 ^ (x2 & y11);
|
||||
y6 = y12 ^ (x2 & y13);
|
||||
y7 = y14 ^ (x2 & y15);
|
||||
|
||||
y0 = y0 ^ (x3 & y1);
|
||||
y1 = y2 ^ (x3 & y3);
|
||||
y2 = y4 ^ (x3 & y5);
|
||||
y3 = y6 ^ (x3 & y7);
|
||||
|
||||
y0 = y0 ^ (x4 & y1);
|
||||
y1 = y2 ^ (x4 & y3);
|
||||
|
||||
y0 = y0 ^ (x5 & y1);
|
||||
|
||||
/*
|
||||
* The P permutation:
|
||||
* -- Each bit move is converted into a mask + left rotation.
|
||||
* -- Rotations that use the same movement are coalesced together.
|
||||
* -- Left and right shifts are used as alternatives to a rotation
|
||||
* where appropriate (this will help architectures that do not have
|
||||
* a rotation opcode).
|
||||
*/
|
||||
z0 = (y0 & (uint32_t)0x00000004) << 3;
|
||||
z0 |= (y0 & (uint32_t)0x00004000) << 4;
|
||||
z0 |= rotl(y0 & 0x12020120, 5);
|
||||
z0 |= (y0 & (uint32_t)0x00100000) << 6;
|
||||
z0 |= (y0 & (uint32_t)0x00008000) << 9;
|
||||
z0 |= (y0 & (uint32_t)0x04000000) >> 22;
|
||||
z0 |= (y0 & (uint32_t)0x00000001) << 11;
|
||||
z0 |= rotl(y0 & 0x20000200, 12);
|
||||
z0 |= (y0 & (uint32_t)0x00200000) >> 19;
|
||||
z0 |= (y0 & (uint32_t)0x00000040) << 14;
|
||||
z0 |= (y0 & (uint32_t)0x00010000) << 15;
|
||||
z0 |= (y0 & (uint32_t)0x00000002) << 16;
|
||||
z0 |= rotl(y0 & 0x40801800, 17);
|
||||
z0 |= (y0 & (uint32_t)0x00080000) >> 13;
|
||||
z0 |= (y0 & (uint32_t)0x00000010) << 21;
|
||||
z0 |= (y0 & (uint32_t)0x01000000) >> 10;
|
||||
z0 |= rotl(y0 & 0x88000008, 24);
|
||||
z0 |= (y0 & (uint32_t)0x00000480) >> 7;
|
||||
z0 |= (y0 & (uint32_t)0x00442000) >> 6;
|
||||
return z0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Process one block through 16 successive rounds, omitting the swap
|
||||
* in the final round.
|
||||
*/
|
||||
static void
|
||||
process_block_unit(uint32_t *pl, uint32_t *pr, const uint32_t *sk_exp)
|
||||
{
|
||||
int i;
|
||||
uint32_t l, r;
|
||||
|
||||
l = *pl;
|
||||
r = *pr;
|
||||
for (i = 0; i < 16; i ++) {
|
||||
uint32_t t;
|
||||
|
||||
t = l ^ Fconf(r, sk_exp);
|
||||
l = r;
|
||||
r = t;
|
||||
sk_exp += 6;
|
||||
}
|
||||
*pl = r;
|
||||
*pr = l;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_des_ct_process_block(unsigned num_rounds,
|
||||
const uint32_t *sk_exp, void *block)
|
||||
{
|
||||
unsigned char *buf;
|
||||
uint32_t l, r;
|
||||
|
||||
buf = block;
|
||||
l = br_dec32be(buf);
|
||||
r = br_dec32be(buf + 4);
|
||||
br_des_do_IP(&l, &r);
|
||||
while (num_rounds -- > 0) {
|
||||
process_block_unit(&l, &r, sk_exp);
|
||||
sk_exp += 96;
|
||||
}
|
||||
br_des_do_invIP(&l, &r);
|
||||
br_enc32be(buf, l);
|
||||
br_enc32be(buf + 4, r);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_des_ct_skey_expand(uint32_t *sk_exp,
|
||||
unsigned num_rounds, const uint32_t *skey)
|
||||
{
|
||||
num_rounds <<= 4;
|
||||
while (num_rounds -- > 0) {
|
||||
uint32_t v, w0, w1, w2, w3;
|
||||
|
||||
v = *skey ++;
|
||||
w0 = v & 0x11111111;
|
||||
w1 = (v >> 1) & 0x11111111;
|
||||
w2 = (v >> 2) & 0x11111111;
|
||||
w3 = (v >> 3) & 0x11111111;
|
||||
*sk_exp ++ = (w0 << 4) - w0;
|
||||
*sk_exp ++ = (w1 << 4) - w1;
|
||||
*sk_exp ++ = (w2 << 4) - w2;
|
||||
*sk_exp ++ = (w3 << 4) - w3;
|
||||
v = *skey ++;
|
||||
w0 = v & 0x11111111;
|
||||
w1 = (v >> 1) & 0x11111111;
|
||||
*sk_exp ++ = (w0 << 4) - w0;
|
||||
*sk_exp ++ = (w1 << 4) - w1;
|
||||
}
|
||||
}
|
||||
87
src/symcipher/des_ct_cbcdec.c
Normal file
87
src/symcipher/des_ct_cbcdec.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_des_ct_cbcdec_init(br_des_ct_cbcdec_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_des_ct_cbcdec_vtable;
|
||||
ctx->num_rounds = br_des_ct_keysched(ctx->skey, key, len);
|
||||
if (len == 8) {
|
||||
br_des_rev_skey(ctx->skey);
|
||||
} else {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 48; i += 2) {
|
||||
uint32_t t;
|
||||
|
||||
t = ctx->skey[i];
|
||||
ctx->skey[i] = ctx->skey[94 - i];
|
||||
ctx->skey[94 - i] = t;
|
||||
t = ctx->skey[i + 1];
|
||||
ctx->skey[i + 1] = ctx->skey[95 - i];
|
||||
ctx->skey[95 - i] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_des_ct_cbcdec_run(const br_des_ct_cbcdec_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
uint32_t sk_exp[288];
|
||||
|
||||
br_des_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
ivbuf = iv;
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
unsigned char tmp[8];
|
||||
int i;
|
||||
|
||||
memcpy(tmp, buf, 8);
|
||||
br_des_ct_process_block(ctx->num_rounds, sk_exp, buf);
|
||||
for (i = 0; i < 8; i ++) {
|
||||
buf[i] ^= ivbuf[i];
|
||||
}
|
||||
memcpy(ivbuf, tmp, 8);
|
||||
buf += 8;
|
||||
len -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcdec_class br_des_ct_cbcdec_vtable = {
|
||||
sizeof(br_des_ct_cbcdec_keys),
|
||||
8,
|
||||
3,
|
||||
(void (*)(const br_block_cbcdec_class **, const void *, size_t))
|
||||
&br_des_ct_cbcdec_init,
|
||||
(void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
|
||||
&br_des_ct_cbcdec_run
|
||||
};
|
||||
69
src/symcipher/des_ct_cbcenc.c
Normal file
69
src/symcipher/des_ct_cbcenc.c
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_des_ct_cbcenc_init(br_des_ct_cbcenc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_des_ct_cbcenc_vtable;
|
||||
ctx->num_rounds = br_des_ct_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_des_ct_cbcenc_run(const br_des_ct_cbcenc_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
uint32_t sk_exp[288];
|
||||
|
||||
br_des_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
ivbuf = iv;
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
buf[i] ^= ivbuf[i];
|
||||
}
|
||||
br_des_ct_process_block(ctx->num_rounds, sk_exp, buf);
|
||||
memcpy(ivbuf, buf, 8);
|
||||
buf += 8;
|
||||
len -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcenc_class br_des_ct_cbcenc_vtable = {
|
||||
sizeof(br_des_ct_cbcenc_keys),
|
||||
8,
|
||||
3,
|
||||
(void (*)(const br_block_cbcenc_class **, const void *, size_t))
|
||||
&br_des_ct_cbcenc_init,
|
||||
(void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
|
||||
&br_des_ct_cbcenc_run
|
||||
};
|
||||
166
src/symcipher/des_support.c
Normal file
166
src/symcipher/des_support.c
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_des_do_IP(uint32_t *xl, uint32_t *xr)
|
||||
{
|
||||
/*
|
||||
* Permutation algorithm is initially from Richard Outerbridge;
|
||||
* implementation here is adapted from Crypto++ "des.cpp" file
|
||||
* (which is in public domain).
|
||||
*/
|
||||
uint32_t l, r, t;
|
||||
|
||||
l = *xl;
|
||||
r = *xr;
|
||||
t = ((l >> 4) ^ r) & (uint32_t)0x0F0F0F0F;
|
||||
r ^= t;
|
||||
l ^= t << 4;
|
||||
t = ((l >> 16) ^ r) & (uint32_t)0x0000FFFF;
|
||||
r ^= t;
|
||||
l ^= t << 16;
|
||||
t = ((r >> 2) ^ l) & (uint32_t)0x33333333;
|
||||
l ^= t;
|
||||
r ^= t << 2;
|
||||
t = ((r >> 8) ^ l) & (uint32_t)0x00FF00FF;
|
||||
l ^= t;
|
||||
r ^= t << 8;
|
||||
t = ((l >> 1) ^ r) & (uint32_t)0x55555555;
|
||||
r ^= t;
|
||||
l ^= t << 1;
|
||||
*xl = l;
|
||||
*xr = r;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_des_do_invIP(uint32_t *xl, uint32_t *xr)
|
||||
{
|
||||
/*
|
||||
* See br_des_do_IP().
|
||||
*/
|
||||
uint32_t l, r, t;
|
||||
|
||||
l = *xl;
|
||||
r = *xr;
|
||||
t = ((l >> 1) ^ r) & 0x55555555;
|
||||
r ^= t;
|
||||
l ^= t << 1;
|
||||
t = ((r >> 8) ^ l) & 0x00FF00FF;
|
||||
l ^= t;
|
||||
r ^= t << 8;
|
||||
t = ((r >> 2) ^ l) & 0x33333333;
|
||||
l ^= t;
|
||||
r ^= t << 2;
|
||||
t = ((l >> 16) ^ r) & 0x0000FFFF;
|
||||
r ^= t;
|
||||
l ^= t << 16;
|
||||
t = ((l >> 4) ^ r) & 0x0F0F0F0F;
|
||||
r ^= t;
|
||||
l ^= t << 4;
|
||||
*xl = l;
|
||||
*xr = r;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_des_keysched_unit(uint32_t *skey, const void *key)
|
||||
{
|
||||
uint32_t xl, xr, kl, kr;
|
||||
int i;
|
||||
|
||||
xl = br_dec32be(key);
|
||||
xr = br_dec32be((const unsigned char *)key + 4);
|
||||
|
||||
/*
|
||||
* Permutation PC-1 is quite similar to the IP permutation.
|
||||
* Definition of IP (in FIPS 46-3 notations) is:
|
||||
* 58 50 42 34 26 18 10 2
|
||||
* 60 52 44 36 28 20 12 4
|
||||
* 62 54 46 38 30 22 14 6
|
||||
* 64 56 48 40 32 24 16 8
|
||||
* 57 49 41 33 25 17 9 1
|
||||
* 59 51 43 35 27 19 11 3
|
||||
* 61 53 45 37 29 21 13 5
|
||||
* 63 55 47 39 31 23 15 7
|
||||
*
|
||||
* Definition of PC-1 is:
|
||||
* 57 49 41 33 25 17 9 1
|
||||
* 58 50 42 34 26 18 10 2
|
||||
* 59 51 43 35 27 19 11 3
|
||||
* 60 52 44 36
|
||||
* 63 55 47 39 31 23 15 7
|
||||
* 62 54 46 38 30 22 14 6
|
||||
* 61 53 45 37 29 21 13 5
|
||||
* 28 20 12 4
|
||||
*/
|
||||
br_des_do_IP(&xl, &xr);
|
||||
kl = ((xr & (uint32_t)0xFF000000) >> 4)
|
||||
| ((xl & (uint32_t)0xFF000000) >> 12)
|
||||
| ((xr & (uint32_t)0x00FF0000) >> 12)
|
||||
| ((xl & (uint32_t)0x00FF0000) >> 20);
|
||||
kr = ((xr & (uint32_t)0x000000FF) << 20)
|
||||
| ((xl & (uint32_t)0x0000FF00) << 4)
|
||||
| ((xr & (uint32_t)0x0000FF00) >> 4)
|
||||
| ((xl & (uint32_t)0x000F0000) >> 16);
|
||||
|
||||
/*
|
||||
* For each round, rotate the two 28-bit words kl and kr.
|
||||
* The extraction of the 48-bit subkey (PC-2) is not done yet.
|
||||
*/
|
||||
for (i = 0; i < 16; i ++) {
|
||||
if ((1 << i) & 0x8103) {
|
||||
kl = (kl << 1) | (kl >> 27);
|
||||
kr = (kr << 1) | (kr >> 27);
|
||||
} else {
|
||||
kl = (kl << 2) | (kl >> 26);
|
||||
kr = (kr << 2) | (kr >> 26);
|
||||
}
|
||||
kl &= (uint32_t)0x0FFFFFFF;
|
||||
kr &= (uint32_t)0x0FFFFFFF;
|
||||
skey[(i << 1) + 0] = kl;
|
||||
skey[(i << 1) + 1] = kr;
|
||||
}
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_des_rev_skey(uint32_t *skey)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i += 2) {
|
||||
uint32_t t;
|
||||
|
||||
t = skey[i + 0];
|
||||
skey[i + 0] = skey[30 - i];
|
||||
skey[30 - i] = t;
|
||||
t = skey[i + 1];
|
||||
skey[i + 1] = skey[31 - i];
|
||||
skey[31 - i] = t;
|
||||
}
|
||||
}
|
||||
310
src/symcipher/des_tab.c
Normal file
310
src/symcipher/des_tab.c
Normal file
|
|
@ -0,0 +1,310 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/*
|
||||
* PC2left[x] tells where bit x goes when applying PC-2. 'x' is a bit
|
||||
* position in the left rotated key word. Both position are in normal
|
||||
* order (rightmost bit is 0).
|
||||
*/
|
||||
static const unsigned char PC2left[] = {
|
||||
16, 3, 7, 24, 20, 11, 24,
|
||||
13, 2, 10, 24, 22, 5, 15,
|
||||
23, 1, 9, 21, 12, 24, 6,
|
||||
4, 14, 18, 8, 17, 0, 19
|
||||
};
|
||||
|
||||
/*
|
||||
* Similar to PC2left[x], for the right rotated key word.
|
||||
*/
|
||||
static const unsigned char PC2right[] = {
|
||||
8, 18, 24, 6, 22, 15, 3,
|
||||
10, 12, 19, 5, 14, 11, 24,
|
||||
4, 23, 16, 9, 24, 20, 2,
|
||||
24, 7, 13, 0, 21, 17, 1
|
||||
};
|
||||
|
||||
/*
|
||||
* S-boxes and PC-1 merged.
|
||||
*/
|
||||
static const uint32_t S1[] = {
|
||||
0x00808200, 0x00000000, 0x00008000, 0x00808202,
|
||||
0x00808002, 0x00008202, 0x00000002, 0x00008000,
|
||||
0x00000200, 0x00808200, 0x00808202, 0x00000200,
|
||||
0x00800202, 0x00808002, 0x00800000, 0x00000002,
|
||||
0x00000202, 0x00800200, 0x00800200, 0x00008200,
|
||||
0x00008200, 0x00808000, 0x00808000, 0x00800202,
|
||||
0x00008002, 0x00800002, 0x00800002, 0x00008002,
|
||||
0x00000000, 0x00000202, 0x00008202, 0x00800000,
|
||||
0x00008000, 0x00808202, 0x00000002, 0x00808000,
|
||||
0x00808200, 0x00800000, 0x00800000, 0x00000200,
|
||||
0x00808002, 0x00008000, 0x00008200, 0x00800002,
|
||||
0x00000200, 0x00000002, 0x00800202, 0x00008202,
|
||||
0x00808202, 0x00008002, 0x00808000, 0x00800202,
|
||||
0x00800002, 0x00000202, 0x00008202, 0x00808200,
|
||||
0x00000202, 0x00800200, 0x00800200, 0x00000000,
|
||||
0x00008002, 0x00008200, 0x00000000, 0x00808002
|
||||
};
|
||||
|
||||
static const uint32_t S2[] = {
|
||||
0x40084010, 0x40004000, 0x00004000, 0x00084010,
|
||||
0x00080000, 0x00000010, 0x40080010, 0x40004010,
|
||||
0x40000010, 0x40084010, 0x40084000, 0x40000000,
|
||||
0x40004000, 0x00080000, 0x00000010, 0x40080010,
|
||||
0x00084000, 0x00080010, 0x40004010, 0x00000000,
|
||||
0x40000000, 0x00004000, 0x00084010, 0x40080000,
|
||||
0x00080010, 0x40000010, 0x00000000, 0x00084000,
|
||||
0x00004010, 0x40084000, 0x40080000, 0x00004010,
|
||||
0x00000000, 0x00084010, 0x40080010, 0x00080000,
|
||||
0x40004010, 0x40080000, 0x40084000, 0x00004000,
|
||||
0x40080000, 0x40004000, 0x00000010, 0x40084010,
|
||||
0x00084010, 0x00000010, 0x00004000, 0x40000000,
|
||||
0x00004010, 0x40084000, 0x00080000, 0x40000010,
|
||||
0x00080010, 0x40004010, 0x40000010, 0x00080010,
|
||||
0x00084000, 0x00000000, 0x40004000, 0x00004010,
|
||||
0x40000000, 0x40080010, 0x40084010, 0x00084000
|
||||
};
|
||||
|
||||
static const uint32_t S3[] = {
|
||||
0x00000104, 0x04010100, 0x00000000, 0x04010004,
|
||||
0x04000100, 0x00000000, 0x00010104, 0x04000100,
|
||||
0x00010004, 0x04000004, 0x04000004, 0x00010000,
|
||||
0x04010104, 0x00010004, 0x04010000, 0x00000104,
|
||||
0x04000000, 0x00000004, 0x04010100, 0x00000100,
|
||||
0x00010100, 0x04010000, 0x04010004, 0x00010104,
|
||||
0x04000104, 0x00010100, 0x00010000, 0x04000104,
|
||||
0x00000004, 0x04010104, 0x00000100, 0x04000000,
|
||||
0x04010100, 0x04000000, 0x00010004, 0x00000104,
|
||||
0x00010000, 0x04010100, 0x04000100, 0x00000000,
|
||||
0x00000100, 0x00010004, 0x04010104, 0x04000100,
|
||||
0x04000004, 0x00000100, 0x00000000, 0x04010004,
|
||||
0x04000104, 0x00010000, 0x04000000, 0x04010104,
|
||||
0x00000004, 0x00010104, 0x00010100, 0x04000004,
|
||||
0x04010000, 0x04000104, 0x00000104, 0x04010000,
|
||||
0x00010104, 0x00000004, 0x04010004, 0x00010100
|
||||
};
|
||||
|
||||
static const uint32_t S4[] = {
|
||||
0x80401000, 0x80001040, 0x80001040, 0x00000040,
|
||||
0x00401040, 0x80400040, 0x80400000, 0x80001000,
|
||||
0x00000000, 0x00401000, 0x00401000, 0x80401040,
|
||||
0x80000040, 0x00000000, 0x00400040, 0x80400000,
|
||||
0x80000000, 0x00001000, 0x00400000, 0x80401000,
|
||||
0x00000040, 0x00400000, 0x80001000, 0x00001040,
|
||||
0x80400040, 0x80000000, 0x00001040, 0x00400040,
|
||||
0x00001000, 0x00401040, 0x80401040, 0x80000040,
|
||||
0x00400040, 0x80400000, 0x00401000, 0x80401040,
|
||||
0x80000040, 0x00000000, 0x00000000, 0x00401000,
|
||||
0x00001040, 0x00400040, 0x80400040, 0x80000000,
|
||||
0x80401000, 0x80001040, 0x80001040, 0x00000040,
|
||||
0x80401040, 0x80000040, 0x80000000, 0x00001000,
|
||||
0x80400000, 0x80001000, 0x00401040, 0x80400040,
|
||||
0x80001000, 0x00001040, 0x00400000, 0x80401000,
|
||||
0x00000040, 0x00400000, 0x00001000, 0x00401040
|
||||
};
|
||||
|
||||
static const uint32_t S5[] = {
|
||||
0x00000080, 0x01040080, 0x01040000, 0x21000080,
|
||||
0x00040000, 0x00000080, 0x20000000, 0x01040000,
|
||||
0x20040080, 0x00040000, 0x01000080, 0x20040080,
|
||||
0x21000080, 0x21040000, 0x00040080, 0x20000000,
|
||||
0x01000000, 0x20040000, 0x20040000, 0x00000000,
|
||||
0x20000080, 0x21040080, 0x21040080, 0x01000080,
|
||||
0x21040000, 0x20000080, 0x00000000, 0x21000000,
|
||||
0x01040080, 0x01000000, 0x21000000, 0x00040080,
|
||||
0x00040000, 0x21000080, 0x00000080, 0x01000000,
|
||||
0x20000000, 0x01040000, 0x21000080, 0x20040080,
|
||||
0x01000080, 0x20000000, 0x21040000, 0x01040080,
|
||||
0x20040080, 0x00000080, 0x01000000, 0x21040000,
|
||||
0x21040080, 0x00040080, 0x21000000, 0x21040080,
|
||||
0x01040000, 0x00000000, 0x20040000, 0x21000000,
|
||||
0x00040080, 0x01000080, 0x20000080, 0x00040000,
|
||||
0x00000000, 0x20040000, 0x01040080, 0x20000080
|
||||
};
|
||||
|
||||
static const uint32_t S6[] = {
|
||||
0x10000008, 0x10200000, 0x00002000, 0x10202008,
|
||||
0x10200000, 0x00000008, 0x10202008, 0x00200000,
|
||||
0x10002000, 0x00202008, 0x00200000, 0x10000008,
|
||||
0x00200008, 0x10002000, 0x10000000, 0x00002008,
|
||||
0x00000000, 0x00200008, 0x10002008, 0x00002000,
|
||||
0x00202000, 0x10002008, 0x00000008, 0x10200008,
|
||||
0x10200008, 0x00000000, 0x00202008, 0x10202000,
|
||||
0x00002008, 0x00202000, 0x10202000, 0x10000000,
|
||||
0x10002000, 0x00000008, 0x10200008, 0x00202000,
|
||||
0x10202008, 0x00200000, 0x00002008, 0x10000008,
|
||||
0x00200000, 0x10002000, 0x10000000, 0x00002008,
|
||||
0x10000008, 0x10202008, 0x00202000, 0x10200000,
|
||||
0x00202008, 0x10202000, 0x00000000, 0x10200008,
|
||||
0x00000008, 0x00002000, 0x10200000, 0x00202008,
|
||||
0x00002000, 0x00200008, 0x10002008, 0x00000000,
|
||||
0x10202000, 0x10000000, 0x00200008, 0x10002008
|
||||
};
|
||||
|
||||
static const uint32_t S7[] = {
|
||||
0x00100000, 0x02100001, 0x02000401, 0x00000000,
|
||||
0x00000400, 0x02000401, 0x00100401, 0x02100400,
|
||||
0x02100401, 0x00100000, 0x00000000, 0x02000001,
|
||||
0x00000001, 0x02000000, 0x02100001, 0x00000401,
|
||||
0x02000400, 0x00100401, 0x00100001, 0x02000400,
|
||||
0x02000001, 0x02100000, 0x02100400, 0x00100001,
|
||||
0x02100000, 0x00000400, 0x00000401, 0x02100401,
|
||||
0x00100400, 0x00000001, 0x02000000, 0x00100400,
|
||||
0x02000000, 0x00100400, 0x00100000, 0x02000401,
|
||||
0x02000401, 0x02100001, 0x02100001, 0x00000001,
|
||||
0x00100001, 0x02000000, 0x02000400, 0x00100000,
|
||||
0x02100400, 0x00000401, 0x00100401, 0x02100400,
|
||||
0x00000401, 0x02000001, 0x02100401, 0x02100000,
|
||||
0x00100400, 0x00000000, 0x00000001, 0x02100401,
|
||||
0x00000000, 0x00100401, 0x02100000, 0x00000400,
|
||||
0x02000001, 0x02000400, 0x00000400, 0x00100001
|
||||
};
|
||||
|
||||
static const uint32_t S8[] = {
|
||||
0x08000820, 0x00000800, 0x00020000, 0x08020820,
|
||||
0x08000000, 0x08000820, 0x00000020, 0x08000000,
|
||||
0x00020020, 0x08020000, 0x08020820, 0x00020800,
|
||||
0x08020800, 0x00020820, 0x00000800, 0x00000020,
|
||||
0x08020000, 0x08000020, 0x08000800, 0x00000820,
|
||||
0x00020800, 0x00020020, 0x08020020, 0x08020800,
|
||||
0x00000820, 0x00000000, 0x00000000, 0x08020020,
|
||||
0x08000020, 0x08000800, 0x00020820, 0x00020000,
|
||||
0x00020820, 0x00020000, 0x08020800, 0x00000800,
|
||||
0x00000020, 0x08020020, 0x00000800, 0x00020820,
|
||||
0x08000800, 0x00000020, 0x08000020, 0x08020000,
|
||||
0x08020020, 0x08000000, 0x00020000, 0x08000820,
|
||||
0x00000000, 0x08020820, 0x00020020, 0x08000020,
|
||||
0x08020000, 0x08000800, 0x08000820, 0x00000000,
|
||||
0x08020820, 0x00020800, 0x00020800, 0x00000820,
|
||||
0x00000820, 0x00020020, 0x08000000, 0x08020800
|
||||
};
|
||||
|
||||
static inline uint32_t
|
||||
Fconf(uint32_t r0, uint32_t skl, uint32_t skr)
|
||||
{
|
||||
uint32_t r1;
|
||||
|
||||
r1 = (r0 << 16) | (r0 >> 16);
|
||||
return
|
||||
S1[((r1 >> 11) ^ (skl >> 18)) & 0x3F]
|
||||
| S2[((r0 >> 23) ^ (skl >> 12)) & 0x3F]
|
||||
| S3[((r0 >> 19) ^ (skl >> 6)) & 0x3F]
|
||||
| S4[((r0 >> 15) ^ (skl )) & 0x3F]
|
||||
| S5[((r0 >> 11) ^ (skr >> 18)) & 0x3F]
|
||||
| S6[((r0 >> 7) ^ (skr >> 12)) & 0x3F]
|
||||
| S7[((r0 >> 3) ^ (skr >> 6)) & 0x3F]
|
||||
| S8[((r1 >> 15) ^ (skr )) & 0x3F];
|
||||
}
|
||||
|
||||
static void
|
||||
process_block_unit(uint32_t *pl, uint32_t *pr, const uint32_t *skey)
|
||||
{
|
||||
int i;
|
||||
uint32_t l, r;
|
||||
|
||||
l = *pl;
|
||||
r = *pr;
|
||||
for (i = 0; i < 16; i ++) {
|
||||
uint32_t t;
|
||||
|
||||
t = l ^ Fconf(r, skey[(i << 1) + 0], skey[(i << 1) + 1]);
|
||||
l = r;
|
||||
r = t;
|
||||
}
|
||||
*pl = r;
|
||||
*pr = l;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_des_tab_process_block(unsigned num_rounds, const uint32_t *skey, void *block)
|
||||
{
|
||||
unsigned char *buf;
|
||||
uint32_t l, r;
|
||||
|
||||
buf = block;
|
||||
l = br_dec32be(buf);
|
||||
r = br_dec32be(buf + 4);
|
||||
br_des_do_IP(&l, &r);
|
||||
while (num_rounds -- > 0) {
|
||||
process_block_unit(&l, &r, skey);
|
||||
skey += 32;
|
||||
}
|
||||
br_des_do_invIP(&l, &r);
|
||||
br_enc32be(buf, l);
|
||||
br_enc32be(buf + 4, r);
|
||||
}
|
||||
|
||||
static void
|
||||
keysched_unit(uint32_t *skey, const void *key)
|
||||
{
|
||||
int i;
|
||||
|
||||
br_des_keysched_unit(skey, key);
|
||||
|
||||
/*
|
||||
* Apply PC-2 to get the 48-bit subkeys.
|
||||
*/
|
||||
for (i = 0; i < 16; i ++) {
|
||||
uint32_t xl, xr, ul, ur;
|
||||
int j;
|
||||
|
||||
xl = skey[(i << 1) + 0];
|
||||
xr = skey[(i << 1) + 1];
|
||||
ul = 0;
|
||||
ur = 0;
|
||||
for (j = 0; j < 28; j ++) {
|
||||
ul |= (xl & 1) << PC2left[j];
|
||||
ur |= (xr & 1) << PC2right[j];
|
||||
xl >>= 1;
|
||||
xr >>= 1;
|
||||
}
|
||||
skey[(i << 1) + 0] = ul;
|
||||
skey[(i << 1) + 1] = ur;
|
||||
}
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
unsigned
|
||||
br_des_tab_keysched(uint32_t *skey, const void *key, size_t key_len)
|
||||
{
|
||||
switch (key_len) {
|
||||
case 8:
|
||||
keysched_unit(skey, key);
|
||||
return 1;
|
||||
case 16:
|
||||
keysched_unit(skey, key);
|
||||
keysched_unit(skey + 32, (const unsigned char *)key + 8);
|
||||
br_des_rev_skey(skey + 32);
|
||||
memcpy(skey + 64, skey, 32 * sizeof *skey);
|
||||
return 3;
|
||||
default:
|
||||
keysched_unit(skey, key);
|
||||
keysched_unit(skey + 32, (const unsigned char *)key + 8);
|
||||
br_des_rev_skey(skey + 32);
|
||||
keysched_unit(skey + 64, (const unsigned char *)key + 16);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
85
src/symcipher/des_tab_cbcdec.c
Normal file
85
src/symcipher/des_tab_cbcdec.c
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_des_tab_cbcdec_init(br_des_tab_cbcdec_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_des_tab_cbcdec_vtable;
|
||||
ctx->num_rounds = br_des_tab_keysched(ctx->skey, key, len);
|
||||
if (len == 8) {
|
||||
br_des_rev_skey(ctx->skey);
|
||||
} else {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 48; i += 2) {
|
||||
uint32_t t;
|
||||
|
||||
t = ctx->skey[i];
|
||||
ctx->skey[i] = ctx->skey[94 - i];
|
||||
ctx->skey[94 - i] = t;
|
||||
t = ctx->skey[i + 1];
|
||||
ctx->skey[i + 1] = ctx->skey[95 - i];
|
||||
ctx->skey[95 - i] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_des_tab_cbcdec_run(const br_des_tab_cbcdec_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
|
||||
ivbuf = iv;
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
unsigned char tmp[8];
|
||||
int i;
|
||||
|
||||
memcpy(tmp, buf, 8);
|
||||
br_des_tab_process_block(ctx->num_rounds, ctx->skey, buf);
|
||||
for (i = 0; i < 8; i ++) {
|
||||
buf[i] ^= ivbuf[i];
|
||||
}
|
||||
memcpy(ivbuf, tmp, 8);
|
||||
buf += 8;
|
||||
len -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcdec_class br_des_tab_cbcdec_vtable = {
|
||||
sizeof(br_des_tab_cbcdec_keys),
|
||||
8,
|
||||
3,
|
||||
(void (*)(const br_block_cbcdec_class **, const void *, size_t))
|
||||
&br_des_tab_cbcdec_init,
|
||||
(void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
|
||||
&br_des_tab_cbcdec_run
|
||||
};
|
||||
67
src/symcipher/des_tab_cbcenc.c
Normal file
67
src/symcipher/des_tab_cbcenc.c
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_des_tab_cbcenc_init(br_des_tab_cbcenc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_des_tab_cbcenc_vtable;
|
||||
ctx->num_rounds = br_des_tab_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_des_tab_cbcenc_run(const br_des_tab_cbcenc_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
|
||||
ivbuf = iv;
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
buf[i] ^= ivbuf[i];
|
||||
}
|
||||
br_des_tab_process_block(ctx->num_rounds, ctx->skey, buf);
|
||||
memcpy(ivbuf, buf, 8);
|
||||
buf += 8;
|
||||
len -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcenc_class br_des_tab_cbcenc_vtable = {
|
||||
sizeof(br_des_tab_cbcenc_keys),
|
||||
8,
|
||||
3,
|
||||
(void (*)(const br_block_cbcenc_class **, const void *, size_t))
|
||||
&br_des_tab_cbcenc_init,
|
||||
(void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
|
||||
&br_des_tab_cbcenc_run
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue