Path: blob/main/contrib/bearssl/src/symcipher/aes_ct.c
39482 views
/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/* see inner.h */27void28br_aes_ct_bitslice_Sbox(uint32_t *q)29{30/*31* This S-box implementation is a straightforward translation of32* the circuit described by Boyar and Peralta in "A new33* combinational logic minimization technique with applications34* to cryptology" (https://eprint.iacr.org/2009/191.pdf).35*36* Note that variables x* (input) and s* (output) are numbered37* in "reverse" order (x0 is the high bit, x7 is the low bit).38*/3940uint32_t x0, x1, x2, x3, x4, x5, x6, x7;41uint32_t y1, y2, y3, y4, y5, y6, y7, y8, y9;42uint32_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;43uint32_t y20, y21;44uint32_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;45uint32_t z10, z11, z12, z13, z14, z15, z16, z17;46uint32_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;47uint32_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;48uint32_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;49uint32_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;50uint32_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;51uint32_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;52uint32_t t60, t61, t62, t63, t64, t65, t66, t67;53uint32_t s0, s1, s2, s3, s4, s5, s6, s7;5455x0 = q[7];56x1 = q[6];57x2 = q[5];58x3 = q[4];59x4 = q[3];60x5 = q[2];61x6 = q[1];62x7 = q[0];6364/*65* Top linear transformation.66*/67y14 = x3 ^ x5;68y13 = x0 ^ x6;69y9 = x0 ^ x3;70y8 = x0 ^ x5;71t0 = x1 ^ x2;72y1 = t0 ^ x7;73y4 = y1 ^ x3;74y12 = y13 ^ y14;75y2 = y1 ^ x0;76y5 = y1 ^ x6;77y3 = y5 ^ y8;78t1 = x4 ^ y12;79y15 = t1 ^ x5;80y20 = t1 ^ x1;81y6 = y15 ^ x7;82y10 = y15 ^ t0;83y11 = y20 ^ y9;84y7 = x7 ^ y11;85y17 = y10 ^ y11;86y19 = y10 ^ y8;87y16 = t0 ^ y11;88y21 = y13 ^ y16;89y18 = x0 ^ y16;9091/*92* Non-linear section.93*/94t2 = y12 & y15;95t3 = y3 & y6;96t4 = t3 ^ t2;97t5 = y4 & x7;98t6 = t5 ^ t2;99t7 = y13 & y16;100t8 = y5 & y1;101t9 = t8 ^ t7;102t10 = y2 & y7;103t11 = t10 ^ t7;104t12 = y9 & y11;105t13 = y14 & y17;106t14 = t13 ^ t12;107t15 = y8 & y10;108t16 = t15 ^ t12;109t17 = t4 ^ t14;110t18 = t6 ^ t16;111t19 = t9 ^ t14;112t20 = t11 ^ t16;113t21 = t17 ^ y20;114t22 = t18 ^ y19;115t23 = t19 ^ y21;116t24 = t20 ^ y18;117118t25 = t21 ^ t22;119t26 = t21 & t23;120t27 = t24 ^ t26;121t28 = t25 & t27;122t29 = t28 ^ t22;123t30 = t23 ^ t24;124t31 = t22 ^ t26;125t32 = t31 & t30;126t33 = t32 ^ t24;127t34 = t23 ^ t33;128t35 = t27 ^ t33;129t36 = t24 & t35;130t37 = t36 ^ t34;131t38 = t27 ^ t36;132t39 = t29 & t38;133t40 = t25 ^ t39;134135t41 = t40 ^ t37;136t42 = t29 ^ t33;137t43 = t29 ^ t40;138t44 = t33 ^ t37;139t45 = t42 ^ t41;140z0 = t44 & y15;141z1 = t37 & y6;142z2 = t33 & x7;143z3 = t43 & y16;144z4 = t40 & y1;145z5 = t29 & y7;146z6 = t42 & y11;147z7 = t45 & y17;148z8 = t41 & y10;149z9 = t44 & y12;150z10 = t37 & y3;151z11 = t33 & y4;152z12 = t43 & y13;153z13 = t40 & y5;154z14 = t29 & y2;155z15 = t42 & y9;156z16 = t45 & y14;157z17 = t41 & y8;158159/*160* Bottom linear transformation.161*/162t46 = z15 ^ z16;163t47 = z10 ^ z11;164t48 = z5 ^ z13;165t49 = z9 ^ z10;166t50 = z2 ^ z12;167t51 = z2 ^ z5;168t52 = z7 ^ z8;169t53 = z0 ^ z3;170t54 = z6 ^ z7;171t55 = z16 ^ z17;172t56 = z12 ^ t48;173t57 = t50 ^ t53;174t58 = z4 ^ t46;175t59 = z3 ^ t54;176t60 = t46 ^ t57;177t61 = z14 ^ t57;178t62 = t52 ^ t58;179t63 = t49 ^ t58;180t64 = z4 ^ t59;181t65 = t61 ^ t62;182t66 = z1 ^ t63;183s0 = t59 ^ t63;184s6 = t56 ^ ~t62;185s7 = t48 ^ ~t60;186t67 = t64 ^ t65;187s3 = t53 ^ t66;188s4 = t51 ^ t66;189s5 = t47 ^ t65;190s1 = t64 ^ ~s3;191s2 = t55 ^ ~t67;192193q[7] = s0;194q[6] = s1;195q[5] = s2;196q[4] = s3;197q[3] = s4;198q[2] = s5;199q[1] = s6;200q[0] = s7;201}202203/* see inner.h */204void205br_aes_ct_ortho(uint32_t *q)206{207#define SWAPN(cl, ch, s, x, y) do { \208uint32_t a, b; \209a = (x); \210b = (y); \211(x) = (a & (uint32_t)cl) | ((b & (uint32_t)cl) << (s)); \212(y) = ((a & (uint32_t)ch) >> (s)) | (b & (uint32_t)ch); \213} while (0)214215#define SWAP2(x, y) SWAPN(0x55555555, 0xAAAAAAAA, 1, x, y)216#define SWAP4(x, y) SWAPN(0x33333333, 0xCCCCCCCC, 2, x, y)217#define SWAP8(x, y) SWAPN(0x0F0F0F0F, 0xF0F0F0F0, 4, x, y)218219SWAP2(q[0], q[1]);220SWAP2(q[2], q[3]);221SWAP2(q[4], q[5]);222SWAP2(q[6], q[7]);223224SWAP4(q[0], q[2]);225SWAP4(q[1], q[3]);226SWAP4(q[4], q[6]);227SWAP4(q[5], q[7]);228229SWAP8(q[0], q[4]);230SWAP8(q[1], q[5]);231SWAP8(q[2], q[6]);232SWAP8(q[3], q[7]);233}234235static const unsigned char Rcon[] = {2360x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36237};238239static uint32_t240sub_word(uint32_t x)241{242uint32_t q[8];243int i;244245for (i = 0; i < 8; i ++) {246q[i] = x;247}248br_aes_ct_ortho(q);249br_aes_ct_bitslice_Sbox(q);250br_aes_ct_ortho(q);251return q[0];252}253254/* see inner.h */255unsigned256br_aes_ct_keysched(uint32_t *comp_skey, const void *key, size_t key_len)257{258unsigned num_rounds;259int i, j, k, nk, nkf;260uint32_t tmp;261uint32_t skey[120];262263switch (key_len) {264case 16:265num_rounds = 10;266break;267case 24:268num_rounds = 12;269break;270case 32:271num_rounds = 14;272break;273default:274/* abort(); */275return 0;276}277nk = (int)(key_len >> 2);278nkf = (int)((num_rounds + 1) << 2);279tmp = 0;280for (i = 0; i < nk; i ++) {281tmp = br_dec32le((const unsigned char *)key + (i << 2));282skey[(i << 1) + 0] = tmp;283skey[(i << 1) + 1] = tmp;284}285for (i = nk, j = 0, k = 0; i < nkf; i ++) {286if (j == 0) {287tmp = (tmp << 24) | (tmp >> 8);288tmp = sub_word(tmp) ^ Rcon[k];289} else if (nk > 6 && j == 4) {290tmp = sub_word(tmp);291}292tmp ^= skey[(i - nk) << 1];293skey[(i << 1) + 0] = tmp;294skey[(i << 1) + 1] = tmp;295if (++ j == nk) {296j = 0;297k ++;298}299}300for (i = 0; i < nkf; i += 4) {301br_aes_ct_ortho(skey + (i << 1));302}303for (i = 0, j = 0; i < nkf; i ++, j += 2) {304comp_skey[i] = (skey[j + 0] & 0x55555555)305| (skey[j + 1] & 0xAAAAAAAA);306}307return num_rounds;308}309310/* see inner.h */311void312br_aes_ct_skey_expand(uint32_t *skey,313unsigned num_rounds, const uint32_t *comp_skey)314{315unsigned u, v, n;316317n = (num_rounds + 1) << 2;318for (u = 0, v = 0; u < n; u ++, v += 2) {319uint32_t x, y;320321x = y = comp_skey[u];322x &= 0x55555555;323skey[v + 0] = x | (x << 1);324y &= 0xAAAAAAAA;325skey[v + 1] = y | (y >> 1);326}327}328329330