···11-/*
22- * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
33- *
44- * Permission is hereby granted, free of charge, to any person obtaining
55- * a copy of this software and associated documentation files (the
66- * "Software"), to deal in the Software without restriction, including
77- * without limitation the rights to use, copy, modify, merge, publish,
88- * distribute, sublicense, and/or sell copies of the Software, and to
99- * permit persons to whom the Software is furnished to do so, subject to
1010- * the following conditions:
1111- *
1212- * The above copyright notice and this permission notice shall be
1313- * included in all copies or substantial portions of the Software.
1414- *
1515- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1616- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1717- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1818- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
1919- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2020- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2121- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222- * SOFTWARE.
2323- */
2424-2525-#include "inner.h"
2626-2727-/* see inner.h */
2828-void
2929-br_aes_ct64_bitslice_invSbox(uint64_t *q)
3030-{
3131- /*
3232- * See br_aes_ct_bitslice_invSbox(). This is the natural extension
3333- * to 64-bit registers.
3434- */
3535- uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
3636-3737- q0 = ~q[0];
3838- q1 = ~q[1];
3939- q2 = q[2];
4040- q3 = q[3];
4141- q4 = q[4];
4242- q5 = ~q[5];
4343- q6 = ~q[6];
4444- q7 = q[7];
4545- q[7] = q1 ^ q4 ^ q6;
4646- q[6] = q0 ^ q3 ^ q5;
4747- q[5] = q7 ^ q2 ^ q4;
4848- q[4] = q6 ^ q1 ^ q3;
4949- q[3] = q5 ^ q0 ^ q2;
5050- q[2] = q4 ^ q7 ^ q1;
5151- q[1] = q3 ^ q6 ^ q0;
5252- q[0] = q2 ^ q5 ^ q7;
5353-5454- br_aes_ct64_bitslice_Sbox(q);
5555-5656- q0 = ~q[0];
5757- q1 = ~q[1];
5858- q2 = q[2];
5959- q3 = q[3];
6060- q4 = q[4];
6161- q5 = ~q[5];
6262- q6 = ~q[6];
6363- q7 = q[7];
6464- q[7] = q1 ^ q4 ^ q6;
6565- q[6] = q0 ^ q3 ^ q5;
6666- q[5] = q7 ^ q2 ^ q4;
6767- q[4] = q6 ^ q1 ^ q3;
6868- q[3] = q5 ^ q0 ^ q2;
6969- q[2] = q4 ^ q7 ^ q1;
7070- q[1] = q3 ^ q6 ^ q0;
7171- q[0] = q2 ^ q5 ^ q7;
7272-}
7373-7474-static void
7575-add_round_key(uint64_t *q, const uint64_t *sk)
7676-{
7777- int i;
7878-7979- for (i = 0; i < 8; i ++) {
8080- q[i] ^= sk[i];
8181- }
8282-}
8383-8484-static void
8585-inv_shift_rows(uint64_t *q)
8686-{
8787- int i;
8888-8989- for (i = 0; i < 8; i ++) {
9090- uint64_t x;
9191-9292- x = q[i];
9393- q[i] = (x & (uint64_t)0x000000000000FFFF)
9494- | ((x & (uint64_t)0x000000000FFF0000) << 4)
9595- | ((x & (uint64_t)0x00000000F0000000) >> 12)
9696- | ((x & (uint64_t)0x000000FF00000000) << 8)
9797- | ((x & (uint64_t)0x0000FF0000000000) >> 8)
9898- | ((x & (uint64_t)0x000F000000000000) << 12)
9999- | ((x & (uint64_t)0xFFF0000000000000) >> 4);
100100- }
101101-}
102102-103103-static inline uint64_t
104104-rotr32(uint64_t x)
105105-{
106106- return (x << 32) | (x >> 32);
107107-}
108108-109109-static void
110110-inv_mix_columns(uint64_t *q)
111111-{
112112- uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
113113- uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
114114-115115- q0 = q[0];
116116- q1 = q[1];
117117- q2 = q[2];
118118- q3 = q[3];
119119- q4 = q[4];
120120- q5 = q[5];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 ^ r7);
140140-}
141141-142142-/* see inner.h */
143143-void
144144-br_aes_ct64_bitslice_decrypt(unsigned num_rounds,
145145- const uint64_t *skey, uint64_t *q)
146146-{
147147- unsigned u;
148148-149149- add_round_key(q, skey + (num_rounds << 3));
150150- for (u = num_rounds - 1; u > 0; u --) {
151151- inv_shift_rows(q);
152152- br_aes_ct64_bitslice_invSbox(q);
153153- add_round_key(q, skey + (u << 3));
154154- inv_mix_columns(q);
155155- }
156156- inv_shift_rows(q);
157157- br_aes_ct64_bitslice_invSbox(q);
158158- add_round_key(q, skey);
159159-}
-115
src/c/bearssl/aes_ct64_enc.c
···11-/*
22- * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
33- *
44- * Permission is hereby granted, free of charge, to any person obtaining
55- * a copy of this software and associated documentation files (the
66- * "Software"), to deal in the Software without restriction, including
77- * without limitation the rights to use, copy, modify, merge, publish,
88- * distribute, sublicense, and/or sell copies of the Software, and to
99- * permit persons to whom the Software is furnished to do so, subject to
1010- * the following conditions:
1111- *
1212- * The above copyright notice and this permission notice shall be
1313- * included in all copies or substantial portions of the Software.
1414- *
1515- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1616- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1717- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1818- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
1919- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2020- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2121- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222- * SOFTWARE.
2323- */
2424-2525-#include "inner.h"
2626-2727-static inline void
2828-add_round_key(uint64_t *q, const uint64_t *sk)
2929-{
3030- q[0] ^= sk[0];
3131- q[1] ^= sk[1];
3232- q[2] ^= sk[2];
3333- q[3] ^= sk[3];
3434- q[4] ^= sk[4];
3535- q[5] ^= sk[5];
3636- q[6] ^= sk[6];
3737- q[7] ^= sk[7];
3838-}
3939-4040-static inline void
4141-shift_rows(uint64_t *q)
4242-{
4343- int i;
4444-4545- for (i = 0; i < 8; i ++) {
4646- uint64_t x;
4747-4848- x = q[i];
4949- q[i] = (x & (uint64_t)0x000000000000FFFF)
5050- | ((x & (uint64_t)0x00000000FFF00000) >> 4)
5151- | ((x & (uint64_t)0x00000000000F0000) << 12)
5252- | ((x & (uint64_t)0x0000FF0000000000) >> 8)
5353- | ((x & (uint64_t)0x000000FF00000000) << 8)
5454- | ((x & (uint64_t)0xF000000000000000) >> 12)
5555- | ((x & (uint64_t)0x0FFF000000000000) << 4);
5656- }
5757-}
5858-5959-static inline uint64_t
6060-rotr32(uint64_t x)
6161-{
6262- return (x << 32) | (x >> 32);
6363-}
6464-6565-static inline void
6666-mix_columns(uint64_t *q)
6767-{
6868- uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
6969- uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
7070-7171- q0 = q[0];
7272- q1 = q[1];
7373- q2 = q[2];
7474- q3 = q[3];
7575- q4 = q[4];
7676- q5 = q[5];
7777- q6 = q[6];
7878- q7 = q[7];
7979- r0 = (q0 >> 16) | (q0 << 48);
8080- r1 = (q1 >> 16) | (q1 << 48);
8181- r2 = (q2 >> 16) | (q2 << 48);
8282- r3 = (q3 >> 16) | (q3 << 48);
8383- r4 = (q4 >> 16) | (q4 << 48);
8484- r5 = (q5 >> 16) | (q5 << 48);
8585- r6 = (q6 >> 16) | (q6 << 48);
8686- r7 = (q7 >> 16) | (q7 << 48);
8787-8888- q[0] = q7 ^ r7 ^ r0 ^ rotr32(q0 ^ r0);
8989- q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr32(q1 ^ r1);
9090- q[2] = q1 ^ r1 ^ r2 ^ rotr32(q2 ^ r2);
9191- q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr32(q3 ^ r3);
9292- q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr32(q4 ^ r4);
9393- q[5] = q4 ^ r4 ^ r5 ^ rotr32(q5 ^ r5);
9494- q[6] = q5 ^ r5 ^ r6 ^ rotr32(q6 ^ r6);
9595- q[7] = q6 ^ r6 ^ r7 ^ rotr32(q7 ^ r7);
9696-}
9797-9898-/* see inner.h */
9999-void
100100-br_aes_ct64_bitslice_encrypt(unsigned num_rounds,
101101- const uint64_t *skey, uint64_t *q)
102102-{
103103- unsigned u;
104104-105105- add_round_key(q, skey);
106106- for (u = 1; u < num_rounds; u ++) {
107107- br_aes_ct64_bitslice_Sbox(q);
108108- shift_rows(q);
109109- mix_columns(q);
110110- add_round_key(q, skey + (u << 3));
111111- }
112112- br_aes_ct64_bitslice_Sbox(q);
113113- shift_rows(q);
114114- add_round_key(q, skey + (num_rounds << 3));
115115-}
-38
src/c/bearssl/dec32le.c
···11-/*
22- * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
33- *
44- * Permission is hereby granted, free of charge, to any person obtaining
55- * a copy of this software and associated documentation files (the
66- * "Software"), to deal in the Software without restriction, including
77- * without limitation the rights to use, copy, modify, merge, publish,
88- * distribute, sublicense, and/or sell copies of the Software, and to
99- * permit persons to whom the Software is furnished to do so, subject to
1010- * the following conditions:
1111- *
1212- * The above copyright notice and this permission notice shall be
1313- * included in all copies or substantial portions of the Software.
1414- *
1515- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1616- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1717- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1818- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
1919- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2020- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2121- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222- * SOFTWARE.
2323- */
2424-2525-#include "inner.h"
2626-2727-/* see inner.h */
2828-void
2929-br_range_dec32le(uint32_t *v, size_t num, const void *src)
3030-{
3131- const unsigned char *buf;
3232-3333- buf = src;
3434- while (num -- > 0) {
3535- *v ++ = br_dec32le(buf);
3636- buf += 4;
3737- }
3838-}
-38
src/c/bearssl/enc32le.c
···11-/*
22- * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
33- *
44- * Permission is hereby granted, free of charge, to any person obtaining
55- * a copy of this software and associated documentation files (the
66- * "Software"), to deal in the Software without restriction, including
77- * without limitation the rights to use, copy, modify, merge, publish,
88- * distribute, sublicense, and/or sell copies of the Software, and to
99- * permit persons to whom the Software is furnished to do so, subject to
1010- * the following conditions:
1111- *
1212- * The above copyright notice and this permission notice shall be
1313- * included in all copies or substantial portions of the Software.
1414- *
1515- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1616- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1717- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1818- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
1919- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2020- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2121- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222- * SOFTWARE.
2323- */
2424-2525-#include "inner.h"
2626-2727-/* see inner.h */
2828-void
2929-br_range_enc32le(void *dst, const uint32_t *v, size_t num)
3030-{
3131- unsigned char *buf;
3232-3333- buf = dst;
3434- while (num -- > 0) {
3535- br_enc32le(buf, *v ++);
3636- buf += 4;
3737- }
3838-}
-154
src/c/bearssl/ghash_ctmul64.c
···11-/*
22- * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
33- *
44- * Permission is hereby granted, free of charge, to any person obtaining
55- * a copy of this software and associated documentation files (the
66- * "Software"), to deal in the Software without restriction, including
77- * without limitation the rights to use, copy, modify, merge, publish,
88- * distribute, sublicense, and/or sell copies of the Software, and to
99- * permit persons to whom the Software is furnished to do so, subject to
1010- * the following conditions:
1111- *
1212- * The above copyright notice and this permission notice shall be
1313- * included in all copies or substantial portions of the Software.
1414- *
1515- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1616- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1717- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1818- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
1919- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2020- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2121- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222- * SOFTWARE.
2323- */
2424-2525-#include "inner.h"
2626-2727-/*
2828- * This is the 64-bit variant of br_ghash_ctmul32(), with 64-bit operands
2929- * and bit reversal of 64-bit words.
3030- */
3131-3232-static inline uint64_t
3333-bmul64(uint64_t x, uint64_t y)
3434-{
3535- uint64_t x0, x1, x2, x3;
3636- uint64_t y0, y1, y2, y3;
3737- uint64_t z0, z1, z2, z3;
3838-3939- x0 = x & (uint64_t)0x1111111111111111;
4040- x1 = x & (uint64_t)0x2222222222222222;
4141- x2 = x & (uint64_t)0x4444444444444444;
4242- x3 = x & (uint64_t)0x8888888888888888;
4343- y0 = y & (uint64_t)0x1111111111111111;
4444- y1 = y & (uint64_t)0x2222222222222222;
4545- y2 = y & (uint64_t)0x4444444444444444;
4646- y3 = y & (uint64_t)0x8888888888888888;
4747- z0 = (x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1);
4848- z1 = (x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2);
4949- z2 = (x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3);
5050- z3 = (x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0);
5151- z0 &= (uint64_t)0x1111111111111111;
5252- z1 &= (uint64_t)0x2222222222222222;
5353- z2 &= (uint64_t)0x4444444444444444;
5454- z3 &= (uint64_t)0x8888888888888888;
5555- return z0 | z1 | z2 | z3;
5656-}
5757-5858-static uint64_t
5959-rev64(uint64_t x)
6060-{
6161-#define RMS(m, s) do { \
6262- x = ((x & (uint64_t)(m)) << (s)) \
6363- | ((x >> (s)) & (uint64_t)(m)); \
6464- } while (0)
6565-6666- RMS(0x5555555555555555, 1);
6767- RMS(0x3333333333333333, 2);
6868- RMS(0x0F0F0F0F0F0F0F0F, 4);
6969- RMS(0x00FF00FF00FF00FF, 8);
7070- RMS(0x0000FFFF0000FFFF, 16);
7171- return (x << 32) | (x >> 32);
7272-7373-#undef RMS
7474-}
7575-7676-/* see bearssl_ghash.h */
7777-void
7878-br_ghash_ctmul64(void *y, const void *h, const void *data, size_t len)
7979-{
8080- const unsigned char *buf, *hb;
8181- unsigned char *yb;
8282- uint64_t y0, y1;
8383- uint64_t h0, h1, h2, h0r, h1r, h2r;
8484-8585- buf = data;
8686- yb = y;
8787- hb = h;
8888- y1 = br_dec64be(yb);
8989- y0 = br_dec64be(yb + 8);
9090- h1 = br_dec64be(hb);
9191- h0 = br_dec64be(hb + 8);
9292- h0r = rev64(h0);
9393- h1r = rev64(h1);
9494- h2 = h0 ^ h1;
9595- h2r = h0r ^ h1r;
9696- while (len > 0) {
9797- const unsigned char *src;
9898- unsigned char tmp[16];
9999- uint64_t y0r, y1r, y2, y2r;
100100- uint64_t z0, z1, z2, z0h, z1h, z2h;
101101- uint64_t v0, v1, v2, v3;
102102-103103- if (len >= 16) {
104104- src = buf;
105105- buf += 16;
106106- len -= 16;
107107- } else {
108108- memcpy(tmp, buf, len);
109109- memset(tmp + len, 0, (sizeof tmp) - len);
110110- src = tmp;
111111- len = 0;
112112- }
113113- y1 ^= br_dec64be(src);
114114- y0 ^= br_dec64be(src + 8);
115115-116116- y0r = rev64(y0);
117117- y1r = rev64(y1);
118118- y2 = y0 ^ y1;
119119- y2r = y0r ^ y1r;
120120-121121- z0 = bmul64(y0, h0);
122122- z1 = bmul64(y1, h1);
123123- z2 = bmul64(y2, h2);
124124- z0h = bmul64(y0r, h0r);
125125- z1h = bmul64(y1r, h1r);
126126- z2h = bmul64(y2r, h2r);
127127- z2 ^= z0 ^ z1;
128128- z2h ^= z0h ^ z1h;
129129- z0h = rev64(z0h) >> 1;
130130- z1h = rev64(z1h) >> 1;
131131- z2h = rev64(z2h) >> 1;
132132-133133- v0 = z0;
134134- v1 = z0h ^ z2;
135135- v2 = z1 ^ z2h;
136136- v3 = z1h;
137137-138138- v3 = (v3 << 1) | (v2 >> 63);
139139- v2 = (v2 << 1) | (v1 >> 63);
140140- v1 = (v1 << 1) | (v0 >> 63);
141141- v0 = (v0 << 1);
142142-143143- v2 ^= v0 ^ (v0 >> 1) ^ (v0 >> 2) ^ (v0 >> 7);
144144- v1 ^= (v0 << 63) ^ (v0 << 62) ^ (v0 << 57);
145145- v3 ^= v1 ^ (v1 >> 1) ^ (v1 >> 2) ^ (v1 >> 7);
146146- v2 ^= (v1 << 63) ^ (v1 << 62) ^ (v1 << 57);
147147-148148- y0 = v2;
149149- y1 = v3;
150150- }
151151-152152- br_enc64be(yb, y1);
153153- br_enc64be(yb + 8, y0);
154154-}