1255187Sjmg/*- 2255187Sjmg * Copyright 2013 John-Mark Gurney <jmg@FreeBSD.org> 3285254Sjmg * Copyright 2015 Netflix, Inc. 4255187Sjmg * All rights reserved. 5255187Sjmg * 6255187Sjmg * Redistribution and use in source and binary forms, with or without 7255187Sjmg * modification, are permitted provided that the following conditions 8255187Sjmg * are met: 9255187Sjmg * 1. Redistributions of source code must retain the above copyright 10255187Sjmg * notice, this list of conditions and the following disclaimer. 11255187Sjmg * 2. Redistributions in binary form must reproduce the above copyright 12255187Sjmg * notice, this list of conditions and the following disclaimer in the 13255187Sjmg * documentation and/or other materials provided with the distribution. 14255187Sjmg * 15255187Sjmg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16255187Sjmg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17255187Sjmg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18255187Sjmg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19255187Sjmg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20255187Sjmg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21255187Sjmg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22255187Sjmg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23255187Sjmg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24255187Sjmg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25255187Sjmg * SUCH DAMAGE. 26255187Sjmg * 27255187Sjmg * $FreeBSD$ 28255187Sjmg * 29255187Sjmg */ 30255187Sjmg 31285254Sjmg#ifndef _AESENCDEC_H_ 32285254Sjmg#define _AESENCDEC_H_ 33285254Sjmg 34281606Srodrigc#include <crypto/aesni/aesni_os.h> 35281606Srodrigc 36255187Sjmg#include <wmmintrin.h> 37255187Sjmg 38255187Sjmgstatic inline void 39257757Sjmgaesni_enc8(int rounds, const __m128i *keysched, __m128i a, 40255187Sjmg __m128i b, __m128i c, __m128i d, __m128i e, __m128i f, __m128i g, 41255187Sjmg __m128i h, __m128i out[8]) 42255187Sjmg{ 43255187Sjmg int i; 44255187Sjmg 45255187Sjmg a ^= keysched[0]; 46255187Sjmg b ^= keysched[0]; 47255187Sjmg c ^= keysched[0]; 48255187Sjmg d ^= keysched[0]; 49255187Sjmg e ^= keysched[0]; 50255187Sjmg f ^= keysched[0]; 51255187Sjmg g ^= keysched[0]; 52255187Sjmg h ^= keysched[0]; 53255187Sjmg 54255187Sjmg for (i = 0; i < rounds; i++) { 55255187Sjmg a = _mm_aesenc_si128(a, keysched[i + 1]); 56255187Sjmg b = _mm_aesenc_si128(b, keysched[i + 1]); 57255187Sjmg c = _mm_aesenc_si128(c, keysched[i + 1]); 58255187Sjmg d = _mm_aesenc_si128(d, keysched[i + 1]); 59255187Sjmg e = _mm_aesenc_si128(e, keysched[i + 1]); 60255187Sjmg f = _mm_aesenc_si128(f, keysched[i + 1]); 61255187Sjmg g = _mm_aesenc_si128(g, keysched[i + 1]); 62255187Sjmg h = _mm_aesenc_si128(h, keysched[i + 1]); 63255187Sjmg } 64255187Sjmg 65255187Sjmg out[0] = _mm_aesenclast_si128(a, keysched[i + 1]); 66255187Sjmg out[1] = _mm_aesenclast_si128(b, keysched[i + 1]); 67255187Sjmg out[2] = _mm_aesenclast_si128(c, keysched[i + 1]); 68255187Sjmg out[3] = _mm_aesenclast_si128(d, keysched[i + 1]); 69255187Sjmg out[4] = _mm_aesenclast_si128(e, keysched[i + 1]); 70255187Sjmg out[5] = _mm_aesenclast_si128(f, keysched[i + 1]); 71255187Sjmg out[6] = _mm_aesenclast_si128(g, keysched[i + 1]); 72255187Sjmg out[7] = _mm_aesenclast_si128(h, keysched[i + 1]); 73255187Sjmg} 74255187Sjmg 75255187Sjmgstatic inline void 76257757Sjmgaesni_dec8(int rounds, const __m128i *keysched, __m128i a, 77255187Sjmg __m128i b, __m128i c, __m128i d, __m128i e, __m128i f, __m128i g, 78255187Sjmg __m128i h, __m128i out[8]) 79255187Sjmg{ 80255187Sjmg int i; 81255187Sjmg 82255187Sjmg a ^= keysched[0]; 83255187Sjmg b ^= keysched[0]; 84255187Sjmg c ^= keysched[0]; 85255187Sjmg d ^= keysched[0]; 86255187Sjmg e ^= keysched[0]; 87255187Sjmg f ^= keysched[0]; 88255187Sjmg g ^= keysched[0]; 89255187Sjmg h ^= keysched[0]; 90255187Sjmg 91255187Sjmg for (i = 0; i < rounds; i++) { 92255187Sjmg a = _mm_aesdec_si128(a, keysched[i + 1]); 93255187Sjmg b = _mm_aesdec_si128(b, keysched[i + 1]); 94255187Sjmg c = _mm_aesdec_si128(c, keysched[i + 1]); 95255187Sjmg d = _mm_aesdec_si128(d, keysched[i + 1]); 96255187Sjmg e = _mm_aesdec_si128(e, keysched[i + 1]); 97255187Sjmg f = _mm_aesdec_si128(f, keysched[i + 1]); 98255187Sjmg g = _mm_aesdec_si128(g, keysched[i + 1]); 99255187Sjmg h = _mm_aesdec_si128(h, keysched[i + 1]); 100255187Sjmg } 101255187Sjmg 102255187Sjmg out[0] = _mm_aesdeclast_si128(a, keysched[i + 1]); 103255187Sjmg out[1] = _mm_aesdeclast_si128(b, keysched[i + 1]); 104255187Sjmg out[2] = _mm_aesdeclast_si128(c, keysched[i + 1]); 105255187Sjmg out[3] = _mm_aesdeclast_si128(d, keysched[i + 1]); 106255187Sjmg out[4] = _mm_aesdeclast_si128(e, keysched[i + 1]); 107255187Sjmg out[5] = _mm_aesdeclast_si128(f, keysched[i + 1]); 108255187Sjmg out[6] = _mm_aesdeclast_si128(g, keysched[i + 1]); 109255187Sjmg out[7] = _mm_aesdeclast_si128(h, keysched[i + 1]); 110255187Sjmg} 111255187Sjmg 112285254Sjmg/* rounds is passed in as rounds - 1 */ 113255187Sjmgstatic inline __m128i 114257757Sjmgaesni_enc(int rounds, const __m128i *keysched, const __m128i from) 115255187Sjmg{ 116255187Sjmg __m128i tmp; 117255187Sjmg int i; 118255187Sjmg 119255187Sjmg tmp = from ^ keysched[0]; 120285254Sjmg for (i = 1; i < rounds; i += 2) { 121285254Sjmg tmp = _mm_aesenc_si128(tmp, keysched[i]); 122255187Sjmg tmp = _mm_aesenc_si128(tmp, keysched[i + 1]); 123285254Sjmg } 124255187Sjmg 125285254Sjmg tmp = _mm_aesenc_si128(tmp, keysched[rounds]); 126285254Sjmg return _mm_aesenclast_si128(tmp, keysched[rounds + 1]); 127255187Sjmg} 128255187Sjmg 129255187Sjmgstatic inline __m128i 130257757Sjmgaesni_dec(int rounds, const __m128i *keysched, const __m128i from) 131255187Sjmg{ 132255187Sjmg __m128i tmp; 133255187Sjmg int i; 134255187Sjmg 135255187Sjmg tmp = from ^ keysched[0]; 136255187Sjmg 137285254Sjmg for (i = 1; i < rounds; i += 2) { 138285254Sjmg tmp = _mm_aesdec_si128(tmp, keysched[i]); 139255187Sjmg tmp = _mm_aesdec_si128(tmp, keysched[i + 1]); 140285254Sjmg } 141255187Sjmg 142285254Sjmg tmp = _mm_aesdec_si128(tmp, keysched[rounds]); 143285254Sjmg return _mm_aesdeclast_si128(tmp, keysched[rounds + 1]); 144255187Sjmg} 145285254Sjmg 146285254Sjmg#endif /* _AESENCDEC_H_ */ 147