Small improvement to tolerate PEM files missing the terminating newline in the brssl...
[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 #if BR_AES_X86NI_GCC_OLD
31 #pragma GCC target("sse2,sse4.1,aes,pclmul")
32 #endif
33 #include <wmmintrin.h>
34 #endif
35
36 #if BR_AES_X86NI_MSC
37 #include <intrin.h>
38 #endif
39
40 /* see bearssl_block.h */
41 void
42 br_aes_x86ni_cbcenc_init(br_aes_x86ni_cbcenc_keys *ctx,
43 const void *key, size_t len)
44 {
45 ctx->vtable = &br_aes_x86ni_cbcenc_vtable;
46 ctx->num_rounds = br_aes_x86ni_keysched_enc(ctx->skey.skni, key, len);
47 }
48
49 /* see bearssl_block.h */
50 BR_TARGET("sse2,aes")
51 void
52 br_aes_x86ni_cbcenc_run(const br_aes_x86ni_cbcenc_keys *ctx,
53 void *iv, void *data, size_t len)
54 {
55 unsigned char *buf;
56 unsigned num_rounds;
57 __m128i sk[15], ivx;
58 unsigned u;
59
60 buf = data;
61 ivx = _mm_loadu_si128(iv);
62 num_rounds = ctx->num_rounds;
63 for (u = 0; u <= num_rounds; u ++) {
64 sk[u] = _mm_loadu_si128((void *)(ctx->skey.skni + (u << 4)));
65 }
66 while (len > 0) {
67 __m128i x;
68
69 x = _mm_xor_si128(_mm_loadu_si128((void *)buf), ivx);
70 x = _mm_xor_si128(x, sk[0]);
71 x = _mm_aesenc_si128(x, sk[1]);
72 x = _mm_aesenc_si128(x, sk[2]);
73 x = _mm_aesenc_si128(x, sk[3]);
74 x = _mm_aesenc_si128(x, sk[4]);
75 x = _mm_aesenc_si128(x, sk[5]);
76 x = _mm_aesenc_si128(x, sk[6]);
77 x = _mm_aesenc_si128(x, sk[7]);
78 x = _mm_aesenc_si128(x, sk[8]);
79 x = _mm_aesenc_si128(x, sk[9]);
80 if (num_rounds == 10) {
81 x = _mm_aesenclast_si128(x, sk[10]);
82 } else if (num_rounds == 12) {
83 x = _mm_aesenc_si128(x, sk[10]);
84 x = _mm_aesenc_si128(x, sk[11]);
85 x = _mm_aesenclast_si128(x, sk[12]);
86 } else {
87 x = _mm_aesenc_si128(x, sk[10]);
88 x = _mm_aesenc_si128(x, sk[11]);
89 x = _mm_aesenc_si128(x, sk[12]);
90 x = _mm_aesenc_si128(x, sk[13]);
91 x = _mm_aesenclast_si128(x, sk[14]);
92 }
93 ivx = x;
94 _mm_storeu_si128((void *)buf, x);
95 buf += 16;
96 len -= 16;
97 }
98 _mm_storeu_si128(iv, ivx);
99 }
100
101 /* see bearssl_block.h */
102 const br_block_cbcenc_class br_aes_x86ni_cbcenc_vtable = {
103 sizeof(br_aes_x86ni_cbcenc_keys),
104 16,
105 4,
106 (void (*)(const br_block_cbcenc_class **, const void *, size_t))
107 &br_aes_x86ni_cbcenc_init,
108 (void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
109 &br_aes_x86ni_cbcenc_run
110 };
111
112 /* see bearssl_block.h */
113 const br_block_cbcenc_class *
114 br_aes_x86ni_cbcenc_get_vtable(void)
115 {
116 return br_aes_x86ni_supported() ? &br_aes_x86ni_cbcenc_vtable : NULL;
117 }
118
119 #else
120
121 /* see bearssl_block.h */
122 const br_block_cbcenc_class *
123 br_aes_x86ni_cbcenc_get_vtable(void)
124 {
125 return NULL;
126 }
127
128 #endif