Added AES+GHASH implementation using AES-NI opcodes; also ARM-Thumb assembly for...
[BearSSL] / src / symcipher / aes_x86ni_cbcenc.c
1 /*
2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "inner.h"
26
27 #if BR_AES_X86NI
28
29 #if BR_AES_X86NI_GCC
30 #include <wmmintrin.h>
31 #endif
32
33 #if BR_AES_X86NI_MSC
34 #include <intrin.h>
35 #endif
36
37 /* see bearssl_block.h */
38 void
39 br_aes_x86ni_cbcenc_init(br_aes_x86ni_cbcenc_keys *ctx,
40 const void *key, size_t len)
41 {
42 ctx->vtable = &br_aes_x86ni_cbcenc_vtable;
43 ctx->num_rounds = br_aes_x86ni_keysched_enc(ctx->skey.skni, key, len);
44 }
45
46 /* see bearssl_block.h */
47 BR_TARGET("sse2,aes")
48 void
49 br_aes_x86ni_cbcenc_run(const br_aes_x86ni_cbcenc_keys *ctx,
50 void *iv, void *data, size_t len)
51 {
52 unsigned char *buf;
53 unsigned num_rounds;
54 __m128i sk[15], ivx;
55 unsigned u;
56
57 buf = data;
58 ivx = _mm_loadu_si128(iv);
59 num_rounds = ctx->num_rounds;
60 for (u = 0; u <= num_rounds; u ++) {
61 sk[u] = _mm_loadu_si128((void *)(ctx->skey.skni + (u << 4)));
62 }
63 while (len > 0) {
64 __m128i x;
65
66 x = _mm_xor_si128(_mm_loadu_si128((void *)buf), ivx);
67 x = _mm_xor_si128(x, sk[0]);
68 x = _mm_aesenc_si128(x, sk[1]);
69 x = _mm_aesenc_si128(x, sk[2]);
70 x = _mm_aesenc_si128(x, sk[3]);
71 x = _mm_aesenc_si128(x, sk[4]);
72 x = _mm_aesenc_si128(x, sk[5]);
73 x = _mm_aesenc_si128(x, sk[6]);
74 x = _mm_aesenc_si128(x, sk[7]);
75 x = _mm_aesenc_si128(x, sk[8]);
76 x = _mm_aesenc_si128(x, sk[9]);
77 if (num_rounds == 10) {
78 x = _mm_aesenclast_si128(x, sk[10]);
79 } else if (num_rounds == 12) {
80 x = _mm_aesenc_si128(x, sk[10]);
81 x = _mm_aesenc_si128(x, sk[11]);
82 x = _mm_aesenclast_si128(x, sk[12]);
83 } else {
84 x = _mm_aesenc_si128(x, sk[10]);
85 x = _mm_aesenc_si128(x, sk[11]);
86 x = _mm_aesenc_si128(x, sk[12]);
87 x = _mm_aesenc_si128(x, sk[13]);
88 x = _mm_aesenclast_si128(x, sk[14]);
89 }
90 ivx = x;
91 _mm_storeu_si128((void *)buf, x);
92 buf += 16;
93 len -= 16;
94 }
95 _mm_storeu_si128(iv, ivx);
96 }
97
98 /* see bearssl_block.h */
99 const br_block_cbcenc_class br_aes_x86ni_cbcenc_vtable = {
100 sizeof(br_aes_x86ni_cbcenc_keys),
101 16,
102 4,
103 (void (*)(const br_block_cbcenc_class **, const void *, size_t))
104 &br_aes_x86ni_cbcenc_init,
105 (void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
106 &br_aes_x86ni_cbcenc_run
107 };
108
109 /* see bearssl_block.h */
110 const br_block_cbcenc_class *
111 br_aes_x86ni_cbcenc_get_vtable(void)
112 {
113 return br_aes_x86ni_supported() ? &br_aes_x86ni_cbcenc_vtable : NULL;
114 }
115
116 #else
117
118 /* see bearssl_block.h */
119 const br_block_cbcenc_class *
120 br_aes_x86ni_cbcenc_get_vtable(void)
121 {
122 return NULL;
123 }
124
125 #endif