11.1Sriastrad/*	$NetBSD: aes_ct64_enc.c,v 1.1 2025/11/23 22:44:14 riastradh Exp $	*/
21.1Sriastrad
31.1Sriastrad/*
41.1Sriastrad * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
51.1Sriastrad *
61.1Sriastrad * Permission is hereby granted, free of charge, to any person obtaining
71.1Sriastrad * a copy of this software and associated documentation files (the
81.1Sriastrad * "Software"), to deal in the Software without restriction, including
91.1Sriastrad * without limitation the rights to use, copy, modify, merge, publish,
101.1Sriastrad * distribute, sublicense, and/or sell copies of the Software, and to
111.1Sriastrad * permit persons to whom the Software is furnished to do so, subject to
121.1Sriastrad * the following conditions:
131.1Sriastrad *
141.1Sriastrad * The above copyright notice and this permission notice shall be
151.1Sriastrad * included in all copies or substantial portions of the Software.
161.1Sriastrad *
171.1Sriastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
181.1Sriastrad * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
191.1Sriastrad * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
201.1Sriastrad * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
211.1Sriastrad * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
221.1Sriastrad * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
231.1Sriastrad * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
241.1Sriastrad * SOFTWARE.
251.1Sriastrad */
261.1Sriastrad
271.1Sriastrad#include <sys/cdefs.h>
281.1Sriastrad__KERNEL_RCSID(1, "$NetBSD: aes_ct64_enc.c,v 1.1 2025/11/23 22:44:14 riastradh Exp $");
291.1Sriastrad
301.1Sriastrad#include <sys/types.h>
311.1Sriastrad
321.1Sriastrad#include <crypto/aes/aes_bear64.h>
331.1Sriastrad
341.1Sriastradstatic inline void
351.1Sriastradadd_round_key(uint64_t q[static 8], const uint64_t sk[static 8])
361.1Sriastrad{
371.1Sriastrad	q[0] ^= sk[0];
381.1Sriastrad	q[1] ^= sk[1];
391.1Sriastrad	q[2] ^= sk[2];
401.1Sriastrad	q[3] ^= sk[3];
411.1Sriastrad	q[4] ^= sk[4];
421.1Sriastrad	q[5] ^= sk[5];
431.1Sriastrad	q[6] ^= sk[6];
441.1Sriastrad	q[7] ^= sk[7];
451.1Sriastrad}
461.1Sriastrad
471.1Sriastradstatic inline void
481.1Sriastradshift_rows(uint64_t q[static 8])
491.1Sriastrad{
501.1Sriastrad	int i;
511.1Sriastrad
521.1Sriastrad	for (i = 0; i < 8; i ++) {
531.1Sriastrad		uint64_t x;
541.1Sriastrad
551.1Sriastrad		x = q[i];
561.1Sriastrad		q[i] = (x & (uint64_t)0x000000000000FFFF)
571.1Sriastrad			| ((x & (uint64_t)0x00000000FFF00000) >> 4)
581.1Sriastrad			| ((x & (uint64_t)0x00000000000F0000) << 12)
591.1Sriastrad			| ((x & (uint64_t)0x0000FF0000000000) >> 8)
601.1Sriastrad			| ((x & (uint64_t)0x000000FF00000000) << 8)
611.1Sriastrad			| ((x & (uint64_t)0xF000000000000000) >> 12)
621.1Sriastrad			| ((x & (uint64_t)0x0FFF000000000000) << 4);
631.1Sriastrad	}
641.1Sriastrad}
651.1Sriastrad
661.1Sriastradstatic inline uint64_t
671.1Sriastradrotr32(uint64_t x)
681.1Sriastrad{
691.1Sriastrad	return (x << 32) | (x >> 32);
701.1Sriastrad}
711.1Sriastrad
721.1Sriastradstatic inline void
731.1Sriastradmix_columns(uint64_t q[static 8])
741.1Sriastrad{
751.1Sriastrad	uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
761.1Sriastrad	uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
771.1Sriastrad
781.1Sriastrad	q0 = q[0];
791.1Sriastrad	q1 = q[1];
801.1Sriastrad	q2 = q[2];
811.1Sriastrad	q3 = q[3];
821.1Sriastrad	q4 = q[4];
831.1Sriastrad	q5 = q[5];
841.1Sriastrad	q6 = q[6];
851.1Sriastrad	q7 = q[7];
861.1Sriastrad	r0 = (q0 >> 16) | (q0 << 48);
871.1Sriastrad	r1 = (q1 >> 16) | (q1 << 48);
881.1Sriastrad	r2 = (q2 >> 16) | (q2 << 48);
891.1Sriastrad	r3 = (q3 >> 16) | (q3 << 48);
901.1Sriastrad	r4 = (q4 >> 16) | (q4 << 48);
911.1Sriastrad	r5 = (q5 >> 16) | (q5 << 48);
921.1Sriastrad	r6 = (q6 >> 16) | (q6 << 48);
931.1Sriastrad	r7 = (q7 >> 16) | (q7 << 48);
941.1Sriastrad
951.1Sriastrad	q[0] = q7 ^ r7 ^ r0 ^ rotr32(q0 ^ r0);
961.1Sriastrad	q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr32(q1 ^ r1);
971.1Sriastrad	q[2] = q1 ^ r1 ^ r2 ^ rotr32(q2 ^ r2);
981.1Sriastrad	q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr32(q3 ^ r3);
991.1Sriastrad	q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr32(q4 ^ r4);
1001.1Sriastrad	q[5] = q4 ^ r4 ^ r5 ^ rotr32(q5 ^ r5);
1011.1Sriastrad	q[6] = q5 ^ r5 ^ r6 ^ rotr32(q6 ^ r6);
1021.1Sriastrad	q[7] = q6 ^ r6 ^ r7 ^ rotr32(q7 ^ r7);
1031.1Sriastrad}
1041.1Sriastrad
1051.1Sriastrad/* see inner.h */
1061.1Sriastradvoid
1071.1Sriastradbr_aes_ct64_bitslice_encrypt(unsigned num_rounds,
1081.1Sriastrad	const uint64_t skey[static 120], uint64_t q[static 8])
1091.1Sriastrad{
1101.1Sriastrad	unsigned u;
1111.1Sriastrad
1121.1Sriastrad	add_round_key(q, skey);
1131.1Sriastrad	for (u = 1; u < num_rounds; u ++) {
1141.1Sriastrad		br_aes_ct64_bitslice_Sbox(q);
1151.1Sriastrad		shift_rows(q);
1161.1Sriastrad		mix_columns(q);
1171.1Sriastrad		add_round_key(q, skey + (u << 3));
1181.1Sriastrad	}
1191.1Sriastrad	br_aes_ct64_bitslice_Sbox(q);
1201.1Sriastrad	shift_rows(q);
1211.1Sriastrad	add_round_key(q, skey + (num_rounds << 3));
1221.1Sriastrad}
123