1/* $NetBSD: aes_sse2_4x32_enc.c,v 1.1 2025/11/23 22:48:26 riastradh Exp $ */ 2 3/* 4 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 * SOFTWARE. 25 */ 26 27#include <sys/cdefs.h> 28__KERNEL_RCSID(1, "$NetBSD: aes_sse2_4x32_enc.c,v 1.1 2025/11/23 22:48:26 riastradh Exp $"); 29 30#include <sys/types.h> 31 32#include "aes_sse2_4x32_impl.h" 33 34static inline void 35add_round_key(__m128i q[static 8], const uint32_t sk[static 8]) 36{ 37 38 q[0] ^= _mm_set1_epi32(sk[0]); 39 q[1] ^= _mm_set1_epi32(sk[1]); 40 q[2] ^= _mm_set1_epi32(sk[2]); 41 q[3] ^= _mm_set1_epi32(sk[3]); 42 q[4] ^= _mm_set1_epi32(sk[4]); 43 q[5] ^= _mm_set1_epi32(sk[5]); 44 q[6] ^= _mm_set1_epi32(sk[6]); 45 q[7] ^= _mm_set1_epi32(sk[7]); 46} 47 48static inline __m128i 49shift_row(__m128i q) 50{ 51 __m128i x, y0, y1, y2, y3, y4, y5, y6; 52 53 x = q; 54 y0 = x & _mm_set1_epi32(0x000000FF); 55 y1 = _mm_srli_epi32(x & _mm_set1_epi32(0x0000FC00), 2); 56 y2 = _mm_slli_epi32(x & _mm_set1_epi32(0x00000300), 6); 57 y3 = _mm_srli_epi32(x & _mm_set1_epi32(0x00F00000), 4); 58 y4 = _mm_slli_epi32(x & _mm_set1_epi32(0x000F0000), 4); 59 y5 = _mm_srli_epi32(x & _mm_set1_epi32(0xC0000000), 6); 60 y6 = _mm_slli_epi32(x & _mm_set1_epi32(0x3F000000), 2); 61 return y0 | y1 | y2 | y3 | y4 | y5 | y6; 62} 63 64static inline void 65shift_rows(__m128i q[static 8]) 66{ 67 68 q[0] = shift_row(q[0]); 69 q[1] = shift_row(q[1]); 70 q[2] = shift_row(q[2]); 71 q[3] = shift_row(q[3]); 72 q[4] = shift_row(q[4]); 73 q[5] = shift_row(q[5]); 74 q[6] = shift_row(q[6]); 75 q[7] = shift_row(q[7]); 76} 77 78static inline __m128i 79rotr16(__m128i x) 80{ 81 return _mm_slli_epi32(x, 16) | _mm_srli_epi32(x, 16); 82} 83 84static inline void 85mix_columns(__m128i q[static 8]) 86{ 87 __m128i q0, q1, q2, q3, q4, q5, q6, q7; 88 __m128i r0, r1, r2, r3, r4, r5, r6, r7; 89 90 q0 = q[0]; 91 q1 = q[1]; 92 q2 = q[2]; 93 q3 = q[3]; 94 q4 = q[4]; 95 q5 = q[5]; 96 q6 = q[6]; 97 q7 = q[7]; 98 r0 = _mm_srli_epi32(q0, 8) | _mm_slli_epi32(q0, 24); 99 r1 = _mm_srli_epi32(q1, 8) | _mm_slli_epi32(q1, 24); 100 r2 = _mm_srli_epi32(q2, 8) | _mm_slli_epi32(q2, 24); 101 r3 = _mm_srli_epi32(q3, 8) | _mm_slli_epi32(q3, 24); 102 r4 = _mm_srli_epi32(q4, 8) | _mm_slli_epi32(q4, 24); 103 r5 = _mm_srli_epi32(q5, 8) | _mm_slli_epi32(q5, 24); 104 r6 = _mm_srli_epi32(q6, 8) | _mm_slli_epi32(q6, 24); 105 r7 = _mm_srli_epi32(q7, 8) | _mm_slli_epi32(q7, 24); 106 107 q[0] = q7 ^ r7 ^ r0 ^ rotr16(q0 ^ r0); 108 q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr16(q1 ^ r1); 109 q[2] = q1 ^ r1 ^ r2 ^ rotr16(q2 ^ r2); 110 q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr16(q3 ^ r3); 111 q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr16(q4 ^ r4); 112 q[5] = q4 ^ r4 ^ r5 ^ rotr16(q5 ^ r5); 113 q[6] = q5 ^ r5 ^ r6 ^ rotr16(q6 ^ r6); 114 q[7] = q6 ^ r6 ^ r7 ^ rotr16(q7 ^ r7); 115} 116 117/* see inner.h */ 118void 119aes_sse2_4x32_bitslice_encrypt(unsigned num_rounds, 120 const uint32_t skey[static 120], __m128i q[static 8]) 121{ 122 unsigned u; 123 124 add_round_key(q, skey); 125 for (u = 1; u < num_rounds; u ++) { 126 aes_sse2_4x32_bitslice_Sbox(q); 127 shift_rows(q); 128 mix_columns(q); 129 add_round_key(q, skey + (u << 3)); 130 } 131 aes_sse2_4x32_bitslice_Sbox(q); 132 shift_rows(q); 133 add_round_key(q, skey + (num_rounds << 3)); 134} 135