Path: blob/main/contrib/bearssl/src/symcipher/aes_ct64.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_ct64_bitslice_Sbox(uint64_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*/3940uint64_t x0, x1, x2, x3, x4, x5, x6, x7;41uint64_t y1, y2, y3, y4, y5, y6, y7, y8, y9;42uint64_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;43uint64_t y20, y21;44uint64_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;45uint64_t z10, z11, z12, z13, z14, z15, z16, z17;46uint64_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;47uint64_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;48uint64_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;49uint64_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;50uint64_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;51uint64_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;52uint64_t t60, t61, t62, t63, t64, t65, t66, t67;53uint64_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_ct64_ortho(uint64_t *q)206{207#define SWAPN(cl, ch, s, x, y) do { \208uint64_t a, b; \209a = (x); \210b = (y); \211(x) = (a & (uint64_t)cl) | ((b & (uint64_t)cl) << (s)); \212(y) = ((a & (uint64_t)ch) >> (s)) | (b & (uint64_t)ch); \213} while (0)214215#define SWAP2(x, y) SWAPN(0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 1, x, y)216#define SWAP4(x, y) SWAPN(0x3333333333333333, 0xCCCCCCCCCCCCCCCC, 2, x, y)217#define SWAP8(x, y) SWAPN(0x0F0F0F0F0F0F0F0F, 0xF0F0F0F0F0F0F0F0, 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}234235/* see inner.h */236void237br_aes_ct64_interleave_in(uint64_t *q0, uint64_t *q1, const uint32_t *w)238{239uint64_t x0, x1, x2, x3;240241x0 = w[0];242x1 = w[1];243x2 = w[2];244x3 = w[3];245x0 |= (x0 << 16);246x1 |= (x1 << 16);247x2 |= (x2 << 16);248x3 |= (x3 << 16);249x0 &= (uint64_t)0x0000FFFF0000FFFF;250x1 &= (uint64_t)0x0000FFFF0000FFFF;251x2 &= (uint64_t)0x0000FFFF0000FFFF;252x3 &= (uint64_t)0x0000FFFF0000FFFF;253x0 |= (x0 << 8);254x1 |= (x1 << 8);255x2 |= (x2 << 8);256x3 |= (x3 << 8);257x0 &= (uint64_t)0x00FF00FF00FF00FF;258x1 &= (uint64_t)0x00FF00FF00FF00FF;259x2 &= (uint64_t)0x00FF00FF00FF00FF;260x3 &= (uint64_t)0x00FF00FF00FF00FF;261*q0 = x0 | (x2 << 8);262*q1 = x1 | (x3 << 8);263}264265/* see inner.h */266void267br_aes_ct64_interleave_out(uint32_t *w, uint64_t q0, uint64_t q1)268{269uint64_t x0, x1, x2, x3;270271x0 = q0 & (uint64_t)0x00FF00FF00FF00FF;272x1 = q1 & (uint64_t)0x00FF00FF00FF00FF;273x2 = (q0 >> 8) & (uint64_t)0x00FF00FF00FF00FF;274x3 = (q1 >> 8) & (uint64_t)0x00FF00FF00FF00FF;275x0 |= (x0 >> 8);276x1 |= (x1 >> 8);277x2 |= (x2 >> 8);278x3 |= (x3 >> 8);279x0 &= (uint64_t)0x0000FFFF0000FFFF;280x1 &= (uint64_t)0x0000FFFF0000FFFF;281x2 &= (uint64_t)0x0000FFFF0000FFFF;282x3 &= (uint64_t)0x0000FFFF0000FFFF;283w[0] = (uint32_t)x0 | (uint32_t)(x0 >> 16);284w[1] = (uint32_t)x1 | (uint32_t)(x1 >> 16);285w[2] = (uint32_t)x2 | (uint32_t)(x2 >> 16);286w[3] = (uint32_t)x3 | (uint32_t)(x3 >> 16);287}288289static const unsigned char Rcon[] = {2900x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36291};292293static uint32_t294sub_word(uint32_t x)295{296uint64_t q[8];297298memset(q, 0, sizeof q);299q[0] = x;300br_aes_ct64_ortho(q);301br_aes_ct64_bitslice_Sbox(q);302br_aes_ct64_ortho(q);303return (uint32_t)q[0];304}305306/* see inner.h */307unsigned308br_aes_ct64_keysched(uint64_t *comp_skey, const void *key, size_t key_len)309{310unsigned num_rounds;311int i, j, k, nk, nkf;312uint32_t tmp;313uint32_t skey[60];314315switch (key_len) {316case 16:317num_rounds = 10;318break;319case 24:320num_rounds = 12;321break;322case 32:323num_rounds = 14;324break;325default:326/* abort(); */327return 0;328}329nk = (int)(key_len >> 2);330nkf = (int)((num_rounds + 1) << 2);331br_range_dec32le(skey, (key_len >> 2), key);332tmp = skey[(key_len >> 2) - 1];333for (i = nk, j = 0, k = 0; i < nkf; i ++) {334if (j == 0) {335tmp = (tmp << 24) | (tmp >> 8);336tmp = sub_word(tmp) ^ Rcon[k];337} else if (nk > 6 && j == 4) {338tmp = sub_word(tmp);339}340tmp ^= skey[i - nk];341skey[i] = tmp;342if (++ j == nk) {343j = 0;344k ++;345}346}347348for (i = 0, j = 0; i < nkf; i += 4, j += 2) {349uint64_t q[8];350351br_aes_ct64_interleave_in(&q[0], &q[4], skey + i);352q[1] = q[0];353q[2] = q[0];354q[3] = q[0];355q[5] = q[4];356q[6] = q[4];357q[7] = q[4];358br_aes_ct64_ortho(q);359comp_skey[j + 0] =360(q[0] & (uint64_t)0x1111111111111111)361| (q[1] & (uint64_t)0x2222222222222222)362| (q[2] & (uint64_t)0x4444444444444444)363| (q[3] & (uint64_t)0x8888888888888888);364comp_skey[j + 1] =365(q[4] & (uint64_t)0x1111111111111111)366| (q[5] & (uint64_t)0x2222222222222222)367| (q[6] & (uint64_t)0x4444444444444444)368| (q[7] & (uint64_t)0x8888888888888888);369}370return num_rounds;371}372373/* see inner.h */374void375br_aes_ct64_skey_expand(uint64_t *skey,376unsigned num_rounds, const uint64_t *comp_skey)377{378unsigned u, v, n;379380n = (num_rounds + 1) << 1;381for (u = 0, v = 0; u < n; u ++, v += 4) {382uint64_t x0, x1, x2, x3;383384x0 = x1 = x2 = x3 = comp_skey[u];385x0 &= (uint64_t)0x1111111111111111;386x1 &= (uint64_t)0x2222222222222222;387x2 &= (uint64_t)0x4444444444444444;388x3 &= (uint64_t)0x8888888888888888;389x1 >>= 1;390x2 >>= 2;391x3 >>= 3;392skey[v + 0] = (x0 << 4) - x0;393skey[v + 1] = (x1 << 4) - x1;394skey[v + 2] = (x2 << 4) - x2;395skey[v + 3] = (x3 << 4) - x3;396}397}398399400