Coverage Report

Created: 2026-10-28 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mbedtls/library/aesni.c
Line
Count
Source
1
/*
2
 *  AES-NI support functions
3
 *
4
 *  Copyright The Mbed TLS Contributors
5
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
 */
7
8
/*
9
 * [AES-WP] https://www.intel.com/content/www/us/en/developer/articles/tool/intel-advanced-encryption-standard-aes-instructions-set.html
10
 * [CLMUL-WP] https://www.intel.com/content/www/us/en/develop/download/intel-carry-less-multiplication-instruction-and-its-usage-for-computing-the-gcm-mode.html
11
 */
12
13
#include "common.h"
14
15
#if defined(MBEDTLS_AESNI_C)
16
17
#include "mbedtls/aesni.h"
18
19
#include <string.h>
20
21
/* *INDENT-OFF* */
22
#ifndef asm
23
1.75M
#define asm __asm
24
#endif
25
/* *INDENT-ON* */
26
27
#if defined(MBEDTLS_AESNI_HAVE_CODE)
28
29
#if MBEDTLS_AESNI_HAVE_CODE == 2
30
#if defined(__GNUC__)
31
#include <cpuid.h>
32
#elif defined(_MSC_VER)
33
#include <intrin.h>
34
#else
35
#error "`__cpuid` required by MBEDTLS_AESNI_C is not supported by the compiler"
36
#endif
37
#include <immintrin.h>
38
#endif
39
40
/*
41
 * AES-NI support detection routine
42
 */
43
int mbedtls_aesni_has_support(unsigned int what)
44
1.75M
{
45
1.75M
    static int done = 0;
46
1.75M
    static unsigned int c = 0;
47
48
1.75M
    if (!done) {
49
#if MBEDTLS_AESNI_HAVE_CODE == 2
50
        static int info[4] = { 0, 0, 0, 0 };
51
#if defined(_MSC_VER)
52
        __cpuid(info, 1);
53
#else
54
        __cpuid(1, info[0], info[1], info[2], info[3]);
55
#endif
56
        c = info[2];
57
#else /* AESNI using asm */
58
3
        asm ("movl  $1, %%eax   \n\t"
59
3
             "cpuid             \n\t"
60
3
             : "=c" (c)
61
3
             :
62
3
             : "eax", "ebx", "edx");
63
3
#endif /* MBEDTLS_AESNI_HAVE_CODE */
64
3
        done = 1;
65
3
    }
66
67
1.75M
    return (c & what) != 0;
68
1.75M
}
69
70
#if MBEDTLS_AESNI_HAVE_CODE == 2
71
72
/*
73
 * AES-NI AES-ECB block en(de)cryption
74
 */
75
int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
76
                            int mode,
77
                            const unsigned char input[16],
78
                            unsigned char output[16])
79
{
80
    const __m128i *rk = (const __m128i *) (ctx->rk);
81
    unsigned nr = ctx->nr; // Number of remaining rounds
82
83
    // Load round key 0
84
    __m128i state;
85
    memcpy(&state, input, 16);
86
    state = _mm_xor_si128(state, rk[0]);  // state ^= *rk;
87
    ++rk;
88
    --nr;
89
90
    if (mode == 0) {
91
        while (nr != 0) {
92
            state = _mm_aesdec_si128(state, *rk);
93
            ++rk;
94
            --nr;
95
        }
96
        state = _mm_aesdeclast_si128(state, *rk);
97
    } else {
98
        while (nr != 0) {
99
            state = _mm_aesenc_si128(state, *rk);
100
            ++rk;
101
            --nr;
102
        }
103
        state = _mm_aesenclast_si128(state, *rk);
104
    }
105
106
    memcpy(output, &state, 16);
107
    return 0;
108
}
109
110
/*
111
 * GCM multiplication: c = a times b in GF(2^128)
112
 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
113
 */
114
115
static void gcm_clmul(const __m128i aa, const __m128i bb,
116
                      __m128i *cc, __m128i *dd)
117
{
118
    /*
119
     * Caryless multiplication dd:cc = aa * bb
120
     * using [CLMUL-WP] algorithm 1 (p. 12).
121
     */
122
    *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
123
    *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
124
    __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
125
    __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
126
    ff = _mm_xor_si128(ff, ee);                      // e1+f1:e0+f0
127
    ee = ff;                                         // e1+f1:e0+f0
128
    ff = _mm_srli_si128(ff, 8);                      // 0:e1+f1
129
    ee = _mm_slli_si128(ee, 8);                      // e0+f0:0
130
    *dd = _mm_xor_si128(*dd, ff);                    // d1:d0+e1+f1
131
    *cc = _mm_xor_si128(*cc, ee);                    // c1+e0+f0:c0
132
}
133
134
static void gcm_shift(__m128i *cc, __m128i *dd)
135
{
136
    /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,
137
     * taking advantage of [CLMUL-WP] eq 27 (p. 18). */
138
    //                                        // *cc = r1:r0
139
    //                                        // *dd = r3:r2
140
    __m128i cc_lo = _mm_slli_epi64(*cc, 1);   // r1<<1:r0<<1
141
    __m128i dd_lo = _mm_slli_epi64(*dd, 1);   // r3<<1:r2<<1
142
    __m128i cc_hi = _mm_srli_epi64(*cc, 63);  // r1>>63:r0>>63
143
    __m128i dd_hi = _mm_srli_epi64(*dd, 63);  // r3>>63:r2>>63
144
    __m128i xmm5 = _mm_srli_si128(cc_hi, 8);  // 0:r1>>63
145
    cc_hi = _mm_slli_si128(cc_hi, 8);         // r0>>63:0
146
    dd_hi = _mm_slli_si128(dd_hi, 8);         // 0:r1>>63
147
148
    *cc = _mm_or_si128(cc_lo, cc_hi);         // r1<<1|r0>>63:r0<<1
149
    *dd = _mm_or_si128(_mm_or_si128(dd_lo, dd_hi), xmm5); // r3<<1|r2>>62:r2<<1|r1>>63
150
}
151
152
static __m128i gcm_reduce(__m128i xx)
153
{
154
    //                                            // xx = x1:x0
155
    /* [CLMUL-WP] Algorithm 5 Step 2 */
156
    __m128i aa = _mm_slli_epi64(xx, 63);          // x1<<63:x0<<63 = stuff:a
157
    __m128i bb = _mm_slli_epi64(xx, 62);          // x1<<62:x0<<62 = stuff:b
158
    __m128i cc = _mm_slli_epi64(xx, 57);          // x1<<57:x0<<57 = stuff:c
159
    __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0
160
    return _mm_xor_si128(dd, xx);                 // x1+a+b+c:x0 = d:x0
161
}
162
163
static __m128i gcm_mix(__m128i dx)
164
{
165
    /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
166
    __m128i ee = _mm_srli_epi64(dx, 1);           // e1:x0>>1 = e1:e0'
167
    __m128i ff = _mm_srli_epi64(dx, 2);           // f1:x0>>2 = f1:f0'
168
    __m128i gg = _mm_srli_epi64(dx, 7);           // g1:x0>>7 = g1:g0'
169
170
    // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
171
    // bits carried from d. Now get those bits back in.
172
    __m128i eh = _mm_slli_epi64(dx, 63);          // d<<63:stuff
173
    __m128i fh = _mm_slli_epi64(dx, 62);          // d<<62:stuff
174
    __m128i gh = _mm_slli_epi64(dx, 57);          // d<<57:stuff
175
    __m128i hh = _mm_srli_si128(_mm_xor_si128(_mm_xor_si128(eh, fh), gh), 8); // 0:missing bits of d
176
177
    return _mm_xor_si128(_mm_xor_si128(_mm_xor_si128(_mm_xor_si128(ee, ff), gg), hh), dx);
178
}
179
180
void mbedtls_aesni_gcm_mult(unsigned char c[16],
181
                            const unsigned char a[16],
182
                            const unsigned char b[16])
183
{
184
    __m128i aa = { 0 }, bb = { 0 }, cc, dd;
185
186
    /* The inputs are in big-endian order, so byte-reverse them */
187
    for (size_t i = 0; i < 16; i++) {
188
        ((uint8_t *) &aa)[i] = a[15 - i];
189
        ((uint8_t *) &bb)[i] = b[15 - i];
190
    }
191
192
    gcm_clmul(aa, bb, &cc, &dd);
193
    gcm_shift(&cc, &dd);
194
    /*
195
     * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
196
     * using [CLMUL-WP] algorithm 5 (p. 18).
197
     * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
198
     */
199
    __m128i dx = gcm_reduce(cc);
200
    __m128i xh = gcm_mix(dx);
201
    cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0
202
203
    /* Now byte-reverse the outputs */
204
    for (size_t i = 0; i < 16; i++) {
205
        c[i] = ((uint8_t *) &cc)[15 - i];
206
    }
207
208
    return;
209
}
210
211
/*
212
 * Compute decryption round keys from encryption round keys
213
 */
214
void mbedtls_aesni_inverse_key(unsigned char *invkey,
215
                               const unsigned char *fwdkey, int nr)
216
{
217
    __m128i *ik = (__m128i *) invkey;
218
    const __m128i *fk = (const __m128i *) fwdkey + nr;
219
220
    *ik = *fk;
221
    for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
222
        *ik = _mm_aesimc_si128(*fk);
223
    }
224
    *ik = *fk;
225
}
226
227
/*
228
 * Key expansion, 128-bit case
229
 */
230
static __m128i aesni_set_rk_128(__m128i state, __m128i xword)
231
{
232
    /*
233
     * Finish generating the next round key.
234
     *
235
     * On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff
236
     * with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST).
237
     *
238
     * On exit, xword is r7:r6:r5:r4
239
     * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
240
     * and this is returned, to be written to the round key buffer.
241
     */
242
    xword = _mm_shuffle_epi32(xword, 0xff);   // X:X:X:X
243
    xword = _mm_xor_si128(xword, state);      // X+r3:X+r2:X+r1:r4
244
    state = _mm_slli_si128(state, 4);         // r2:r1:r0:0
245
    xword = _mm_xor_si128(xword, state);      // X+r3+r2:X+r2+r1:r5:r4
246
    state = _mm_slli_si128(state, 4);         // r1:r0:0:0
247
    xword = _mm_xor_si128(xword, state);      // X+r3+r2+r1:r6:r5:r4
248
    state = _mm_slli_si128(state, 4);         // r0:0:0:0
249
    state = _mm_xor_si128(xword, state);      // r7:r6:r5:r4
250
    return state;
251
}
252
253
static void aesni_setkey_enc_128(unsigned char *rk_bytes,
254
                                 const unsigned char *key)
255
{
256
    __m128i *rk = (__m128i *) rk_bytes;
257
258
    memcpy(&rk[0], key, 16);
259
    rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
260
    rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
261
    rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
262
    rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
263
    rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
264
    rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
265
    rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
266
    rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
267
    rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
268
    rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
269
}
270
271
/*
272
 * Key expansion, 192-bit case
273
 */
274
static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword,
275
                             unsigned char *rk)
276
{
277
    /*
278
     * Finish generating the next 6 quarter-keys.
279
     *
280
     * On entry state0 is r3:r2:r1:r0, state1 is stuff:stuff:r5:r4
281
     * and xword is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON
282
     * (obtained with AESKEYGENASSIST).
283
     *
284
     * On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10
285
     * and those are written to the round key buffer.
286
     */
287
    xword = _mm_shuffle_epi32(xword, 0x55);   // X:X:X:X
288
    xword = _mm_xor_si128(xword, *state0);    // X+r3:X+r2:X+r1:X+r0
289
    *state0 = _mm_slli_si128(*state0, 4);     // r2:r1:r0:0
290
    xword = _mm_xor_si128(xword, *state0);    // X+r3+r2:X+r2+r1:X+r1+r0:X+r0
291
    *state0 = _mm_slli_si128(*state0, 4);     // r1:r0:0:0
292
    xword = _mm_xor_si128(xword, *state0);    // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0
293
    *state0 = _mm_slli_si128(*state0, 4);     // r0:0:0:0
294
    xword = _mm_xor_si128(xword, *state0);    // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0
295
    *state0 = xword;                          // = r9:r8:r7:r6
296
297
    xword = _mm_shuffle_epi32(xword, 0xff);   // r9:r9:r9:r9
298
    xword = _mm_xor_si128(xword, *state1);    // stuff:stuff:r9+r5:r9+r4
299
    *state1 = _mm_slli_si128(*state1, 4);     // stuff:stuff:r4:0
300
    xword = _mm_xor_si128(xword, *state1);    // stuff:stuff:r9+r5+r4:r9+r4
301
    *state1 = xword;                          // = stuff:stuff:r11:r10
302
303
    /* Store state0 and the low half of state1 into rk, which is conceptually
304
     * an array of 24-byte elements. Since 24 is not a multiple of 16,
305
     * rk is not necessarily aligned so just `*rk = *state0` doesn't work. */
306
    memcpy(rk, state0, 16);
307
    memcpy(rk + 16, state1, 8);
308
}
309
310
static void aesni_setkey_enc_192(unsigned char *rk,
311
                                 const unsigned char *key)
312
{
313
    /* First round: use original key */
314
    memcpy(rk, key, 24);
315
    /* aes.c guarantees that rk is aligned on a 16-byte boundary. */
316
    __m128i state0 = ((__m128i *) rk)[0];
317
    __m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
318
319
    aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x01), rk + 24 * 1);
320
    aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x02), rk + 24 * 2);
321
    aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x04), rk + 24 * 3);
322
    aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x08), rk + 24 * 4);
323
    aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x10), rk + 24 * 5);
324
    aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x20), rk + 24 * 6);
325
    aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x40), rk + 24 * 7);
326
    aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x80), rk + 24 * 8);
327
}
328
329
/*
330
 * Key expansion, 256-bit case
331
 */
332
static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword,
333
                             __m128i *rk0, __m128i *rk1)
334
{
335
    /*
336
     * Finish generating the next two round keys.
337
     *
338
     * On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and
339
     * xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
340
     * (obtained with AESKEYGENASSIST).
341
     *
342
     * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
343
     */
344
    xword = _mm_shuffle_epi32(xword, 0xff);
345
    xword = _mm_xor_si128(xword, state0);
346
    state0 = _mm_slli_si128(state0, 4);
347
    xword = _mm_xor_si128(xword, state0);
348
    state0 = _mm_slli_si128(state0, 4);
349
    xword = _mm_xor_si128(xword, state0);
350
    state0 = _mm_slli_si128(state0, 4);
351
    state0 = _mm_xor_si128(state0, xword);
352
    *rk0 = state0;
353
354
    /* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 )
355
     * and proceed to generate next round key from there */
356
    xword = _mm_aeskeygenassist_si128(state0, 0x00);
357
    xword = _mm_shuffle_epi32(xword, 0xaa);
358
    xword = _mm_xor_si128(xword, state1);
359
    state1 = _mm_slli_si128(state1, 4);
360
    xword = _mm_xor_si128(xword, state1);
361
    state1 = _mm_slli_si128(state1, 4);
362
    xword = _mm_xor_si128(xword, state1);
363
    state1 = _mm_slli_si128(state1, 4);
364
    state1 = _mm_xor_si128(state1, xword);
365
    *rk1 = state1;
366
}
367
368
static void aesni_setkey_enc_256(unsigned char *rk_bytes,
369
                                 const unsigned char *key)
370
{
371
    __m128i *rk = (__m128i *) rk_bytes;
372
373
    memcpy(&rk[0], key, 16);
374
    memcpy(&rk[1], key + 16, 16);
375
376
    /*
377
     * Main "loop" - Generating one more key than necessary,
378
     * see definition of mbedtls_aes_context.buf
379
     */
380
    aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
381
    aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
382
    aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
383
    aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
384
    aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
385
    aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
386
    aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
387
}
388
389
#else /* MBEDTLS_AESNI_HAVE_CODE == 1 */
390
391
#if defined(__has_feature)
392
#if __has_feature(memory_sanitizer)
393
#warning \
394
    "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
395
#endif
396
#endif
397
398
/*
399
 * Binutils needs to be at least 2.19 to support AES-NI instructions.
400
 * Unfortunately, a lot of users have a lower version now (2014-04).
401
 * Emit bytecode directly in order to support "old" version of gas.
402
 *
403
 * Opcodes from the Intel architecture reference manual, vol. 3.
404
 * We always use registers, so we don't need prefixes for memory operands.
405
 * Operand macros are in gas order (src, dst) as opposed to Intel order
406
 * (dst, src) in order to blend better into the surrounding assembly code.
407
 */
408
#define AESDEC(regs)      ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
409
#define AESDECLAST(regs)  ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
410
#define AESENC(regs)      ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
411
#define AESENCLAST(regs)  ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
412
#define AESIMC(regs)      ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
413
#define AESKEYGENA(regs, imm)  ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
414
#define PCLMULQDQ(regs, imm)   ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
415
416
#define xmm0_xmm0   "0xC0"
417
#define xmm0_xmm1   "0xC8"
418
#define xmm0_xmm2   "0xD0"
419
#define xmm0_xmm3   "0xD8"
420
#define xmm0_xmm4   "0xE0"
421
#define xmm1_xmm0   "0xC1"
422
#define xmm1_xmm2   "0xD1"
423
424
/*
425
 * AES-NI AES-ECB block en(de)cryption
426
 */
427
int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
428
                            int mode,
429
                            const unsigned char input[16],
430
                            unsigned char output[16])
431
953k
{
432
953k
    asm ("movdqu    (%3), %%xmm0    \n\t" // load input
433
953k
         "movdqu    (%1), %%xmm1    \n\t" // load round key 0
434
953k
         "pxor      %%xmm1, %%xmm0  \n\t" // round 0
435
953k
         "add       $16, %1         \n\t" // point to next round key
436
953k
         "subl      $1, %0          \n\t" // normal rounds = nr - 1
437
953k
         "test      %2, %2          \n\t" // mode?
438
953k
         "jz        2f              \n\t" // 0 = decrypt
439
440
953k
         "1:                        \n\t" // encryption loop
441
953k
         "movdqu    (%1), %%xmm1    \n\t" // load round key
442
         AESENC(xmm1_xmm0)                // do round
443
953k
         "add       $16, %1         \n\t" // point to next round key
444
953k
         "subl      $1, %0          \n\t" // loop
445
953k
         "jnz       1b              \n\t"
446
953k
         "movdqu    (%1), %%xmm1    \n\t" // load round key
447
         AESENCLAST(xmm1_xmm0)            // last round
448
953k
         "jmp       3f              \n\t"
449
450
953k
         "2:                        \n\t" // decryption loop
451
953k
         "movdqu    (%1), %%xmm1    \n\t"
452
         AESDEC(xmm1_xmm0)                // do round
453
953k
         "add       $16, %1         \n\t"
454
953k
         "subl      $1, %0          \n\t"
455
953k
         "jnz       2b              \n\t"
456
953k
         "movdqu    (%1), %%xmm1    \n\t" // load round key
457
         AESDECLAST(xmm1_xmm0)            // last round
458
459
953k
         "3:                        \n\t"
460
953k
         "movdqu    %%xmm0, (%4)    \n\t" // export output
461
953k
         :
462
953k
         : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
463
953k
         : "memory", "cc", "xmm0", "xmm1");
464
465
466
953k
    return 0;
467
953k
}
468
469
/*
470
 * GCM multiplication: c = a times b in GF(2^128)
471
 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
472
 */
473
void mbedtls_aesni_gcm_mult(unsigned char c[16],
474
                            const unsigned char a[16],
475
                            const unsigned char b[16])
476
0
{
477
0
    unsigned char aa[16], bb[16], cc[16];
478
0
    size_t i;
479
480
    /* The inputs are in big-endian order, so byte-reverse them */
481
0
    for (i = 0; i < 16; i++) {
482
0
        aa[i] = a[15 - i];
483
0
        bb[i] = b[15 - i];
484
0
    }
485
486
0
    asm ("movdqu (%0), %%xmm0               \n\t" // a1:a0
487
0
         "movdqu (%1), %%xmm1               \n\t" // b1:b0
488
489
         /*
490
          * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
491
          * using [CLMUL-WP] algorithm 1 (p. 12).
492
          */
493
0
         "movdqa %%xmm1, %%xmm2             \n\t" // copy of b1:b0
494
0
         "movdqa %%xmm1, %%xmm3             \n\t" // same
495
0
         "movdqa %%xmm1, %%xmm4             \n\t" // same
496
         PCLMULQDQ(xmm0_xmm1, "0x00")             // a0*b0 = c1:c0
497
         PCLMULQDQ(xmm0_xmm2, "0x11")             // a1*b1 = d1:d0
498
         PCLMULQDQ(xmm0_xmm3, "0x10")             // a0*b1 = e1:e0
499
         PCLMULQDQ(xmm0_xmm4, "0x01")             // a1*b0 = f1:f0
500
0
         "pxor %%xmm3, %%xmm4               \n\t" // e1+f1:e0+f0
501
0
         "movdqa %%xmm4, %%xmm3             \n\t" // same
502
0
         "psrldq $8, %%xmm4                 \n\t" // 0:e1+f1
503
0
         "pslldq $8, %%xmm3                 \n\t" // e0+f0:0
504
0
         "pxor %%xmm4, %%xmm2               \n\t" // d1:d0+e1+f1
505
0
         "pxor %%xmm3, %%xmm1               \n\t" // c1+e0+f1:c0
506
507
         /*
508
          * Now shift the result one bit to the left,
509
          * taking advantage of [CLMUL-WP] eq 27 (p. 18)
510
          */
511
0
         "movdqa %%xmm1, %%xmm3             \n\t" // r1:r0
512
0
         "movdqa %%xmm2, %%xmm4             \n\t" // r3:r2
513
0
         "psllq $1, %%xmm1                  \n\t" // r1<<1:r0<<1
514
0
         "psllq $1, %%xmm2                  \n\t" // r3<<1:r2<<1
515
0
         "psrlq $63, %%xmm3                 \n\t" // r1>>63:r0>>63
516
0
         "psrlq $63, %%xmm4                 \n\t" // r3>>63:r2>>63
517
0
         "movdqa %%xmm3, %%xmm5             \n\t" // r1>>63:r0>>63
518
0
         "pslldq $8, %%xmm3                 \n\t" // r0>>63:0
519
0
         "pslldq $8, %%xmm4                 \n\t" // r2>>63:0
520
0
         "psrldq $8, %%xmm5                 \n\t" // 0:r1>>63
521
0
         "por %%xmm3, %%xmm1                \n\t" // r1<<1|r0>>63:r0<<1
522
0
         "por %%xmm4, %%xmm2                \n\t" // r3<<1|r2>>62:r2<<1
523
0
         "por %%xmm5, %%xmm2                \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
524
525
         /*
526
          * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
527
          * using [CLMUL-WP] algorithm 5 (p. 18).
528
          * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
529
          */
530
         /* Step 2 (1) */
531
0
         "movdqa %%xmm1, %%xmm3             \n\t" // x1:x0
532
0
         "movdqa %%xmm1, %%xmm4             \n\t" // same
533
0
         "movdqa %%xmm1, %%xmm5             \n\t" // same
534
0
         "psllq $63, %%xmm3                 \n\t" // x1<<63:x0<<63 = stuff:a
535
0
         "psllq $62, %%xmm4                 \n\t" // x1<<62:x0<<62 = stuff:b
536
0
         "psllq $57, %%xmm5                 \n\t" // x1<<57:x0<<57 = stuff:c
537
538
         /* Step 2 (2) */
539
0
         "pxor %%xmm4, %%xmm3               \n\t" // stuff:a+b
540
0
         "pxor %%xmm5, %%xmm3               \n\t" // stuff:a+b+c
541
0
         "pslldq $8, %%xmm3                 \n\t" // a+b+c:0
542
0
         "pxor %%xmm3, %%xmm1               \n\t" // x1+a+b+c:x0 = d:x0
543
544
         /* Steps 3 and 4 */
545
0
         "movdqa %%xmm1,%%xmm0              \n\t" // d:x0
546
0
         "movdqa %%xmm1,%%xmm4              \n\t" // same
547
0
         "movdqa %%xmm1,%%xmm5              \n\t" // same
548
0
         "psrlq $1, %%xmm0                  \n\t" // e1:x0>>1 = e1:e0'
549
0
         "psrlq $2, %%xmm4                  \n\t" // f1:x0>>2 = f1:f0'
550
0
         "psrlq $7, %%xmm5                  \n\t" // g1:x0>>7 = g1:g0'
551
0
         "pxor %%xmm4, %%xmm0               \n\t" // e1+f1:e0'+f0'
552
0
         "pxor %%xmm5, %%xmm0               \n\t" // e1+f1+g1:e0'+f0'+g0'
553
         // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
554
         // bits carried from d. Now get those\t bits back in.
555
0
         "movdqa %%xmm1,%%xmm3              \n\t" // d:x0
556
0
         "movdqa %%xmm1,%%xmm4              \n\t" // same
557
0
         "movdqa %%xmm1,%%xmm5              \n\t" // same
558
0
         "psllq $63, %%xmm3                 \n\t" // d<<63:stuff
559
0
         "psllq $62, %%xmm4                 \n\t" // d<<62:stuff
560
0
         "psllq $57, %%xmm5                 \n\t" // d<<57:stuff
561
0
         "pxor %%xmm4, %%xmm3               \n\t" // d<<63+d<<62:stuff
562
0
         "pxor %%xmm5, %%xmm3               \n\t" // missing bits of d:stuff
563
0
         "psrldq $8, %%xmm3                 \n\t" // 0:missing bits of d
564
0
         "pxor %%xmm3, %%xmm0               \n\t" // e1+f1+g1:e0+f0+g0
565
0
         "pxor %%xmm1, %%xmm0               \n\t" // h1:h0
566
0
         "pxor %%xmm2, %%xmm0               \n\t" // x3+h1:x2+h0
567
568
0
         "movdqu %%xmm0, (%2)               \n\t" // done
569
0
         :
570
0
         : "r" (aa), "r" (bb), "r" (cc)
571
0
         : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
572
573
    /* Now byte-reverse the outputs */
574
0
    for (i = 0; i < 16; i++) {
575
0
        c[i] = cc[15 - i];
576
0
    }
577
578
0
    return;
579
0
}
580
581
/*
582
 * Compute decryption round keys from encryption round keys
583
 */
584
void mbedtls_aesni_inverse_key(unsigned char *invkey,
585
                               const unsigned char *fwdkey, int nr)
586
0
{
587
0
    unsigned char *ik = invkey;
588
0
    const unsigned char *fk = fwdkey + 16 * nr;
589
590
0
    memcpy(ik, fk, 16);
591
592
0
    for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
593
0
        asm ("movdqu (%0), %%xmm0       \n\t"
594
0
             AESIMC(xmm0_xmm0)
595
0
             "movdqu %%xmm0, (%1)       \n\t"
596
0
             :
597
0
             : "r" (fk), "r" (ik)
598
0
             : "memory", "xmm0");
599
0
    }
600
601
0
    memcpy(ik, fk, 16);
602
0
}
603
604
/*
605
 * Key expansion, 128-bit case
606
 */
607
static void aesni_setkey_enc_128(unsigned char *rk,
608
                                 const unsigned char *key)
609
803k
{
610
803k
    asm ("movdqu (%1), %%xmm0               \n\t" // copy the original key
611
803k
         "movdqu %%xmm0, (%0)               \n\t" // as round key 0
612
803k
         "jmp 2f                            \n\t" // skip auxiliary routine
613
614
         /*
615
          * Finish generating the next round key.
616
          *
617
          * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
618
          * with X = rot( sub( r3 ) ) ^ RCON.
619
          *
620
          * On exit, xmm0 is r7:r6:r5:r4
621
          * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
622
          * and those are written to the round key buffer.
623
          */
624
803k
         "1:                                \n\t"
625
803k
         "pshufd $0xff, %%xmm1, %%xmm1      \n\t" // X:X:X:X
626
803k
         "pxor %%xmm0, %%xmm1               \n\t" // X+r3:X+r2:X+r1:r4
627
803k
         "pslldq $4, %%xmm0                 \n\t" // r2:r1:r0:0
628
803k
         "pxor %%xmm0, %%xmm1               \n\t" // X+r3+r2:X+r2+r1:r5:r4
629
803k
         "pslldq $4, %%xmm0                 \n\t" // etc
630
803k
         "pxor %%xmm0, %%xmm1               \n\t"
631
803k
         "pslldq $4, %%xmm0                 \n\t"
632
803k
         "pxor %%xmm1, %%xmm0               \n\t" // update xmm0 for next time!
633
803k
         "add $16, %0                       \n\t" // point to next round key
634
803k
         "movdqu %%xmm0, (%0)               \n\t" // write it
635
803k
         "ret                               \n\t"
636
637
         /* Main "loop" */
638
803k
         "2:                                \n\t"
639
803k
         AESKEYGENA(xmm0_xmm1, "0x01")      "call 1b \n\t"
640
803k
         AESKEYGENA(xmm0_xmm1, "0x02")      "call 1b \n\t"
641
803k
         AESKEYGENA(xmm0_xmm1, "0x04")      "call 1b \n\t"
642
803k
         AESKEYGENA(xmm0_xmm1, "0x08")      "call 1b \n\t"
643
803k
         AESKEYGENA(xmm0_xmm1, "0x10")      "call 1b \n\t"
644
803k
         AESKEYGENA(xmm0_xmm1, "0x20")      "call 1b \n\t"
645
803k
         AESKEYGENA(xmm0_xmm1, "0x40")      "call 1b \n\t"
646
803k
         AESKEYGENA(xmm0_xmm1, "0x80")      "call 1b \n\t"
647
803k
         AESKEYGENA(xmm0_xmm1, "0x1B")      "call 1b \n\t"
648
803k
         AESKEYGENA(xmm0_xmm1, "0x36")      "call 1b \n\t"
649
803k
         :
650
803k
         : "r" (rk), "r" (key)
651
803k
         : "memory", "cc", "0");
652
803k
}
653
654
/*
655
 * Key expansion, 192-bit case
656
 */
657
static void aesni_setkey_enc_192(unsigned char *rk,
658
                                 const unsigned char *key)
659
0
{
660
0
    asm ("movdqu (%1), %%xmm0   \n\t" // copy original round key
661
0
         "movdqu %%xmm0, (%0)   \n\t"
662
0
         "add $16, %0           \n\t"
663
0
         "movq 16(%1), %%xmm1   \n\t"
664
0
         "movq %%xmm1, (%0)     \n\t"
665
0
         "add $8, %0            \n\t"
666
0
         "jmp 2f                \n\t" // skip auxiliary routine
667
668
         /*
669
          * Finish generating the next 6 quarter-keys.
670
          *
671
          * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
672
          * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
673
          *
674
          * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
675
          * and those are written to the round key buffer.
676
          */
677
0
         "1:                            \n\t"
678
0
         "pshufd $0x55, %%xmm2, %%xmm2  \n\t" // X:X:X:X
679
0
         "pxor %%xmm0, %%xmm2           \n\t" // X+r3:X+r2:X+r1:r4
680
0
         "pslldq $4, %%xmm0             \n\t" // etc
681
0
         "pxor %%xmm0, %%xmm2           \n\t"
682
0
         "pslldq $4, %%xmm0             \n\t"
683
0
         "pxor %%xmm0, %%xmm2           \n\t"
684
0
         "pslldq $4, %%xmm0             \n\t"
685
0
         "pxor %%xmm2, %%xmm0           \n\t" // update xmm0 = r9:r8:r7:r6
686
0
         "movdqu %%xmm0, (%0)           \n\t"
687
0
         "add $16, %0                   \n\t"
688
0
         "pshufd $0xff, %%xmm0, %%xmm2  \n\t" // r9:r9:r9:r9
689
0
         "pxor %%xmm1, %%xmm2           \n\t" // stuff:stuff:r9+r5:r10
690
0
         "pslldq $4, %%xmm1             \n\t" // r2:r1:r0:0
691
0
         "pxor %%xmm2, %%xmm1           \n\t" // xmm1 = stuff:stuff:r11:r10
692
0
         "movq %%xmm1, (%0)             \n\t"
693
0
         "add $8, %0                    \n\t"
694
0
         "ret                           \n\t"
695
696
0
         "2:                            \n\t"
697
0
         AESKEYGENA(xmm1_xmm2, "0x01")  "call 1b \n\t"
698
0
         AESKEYGENA(xmm1_xmm2, "0x02")  "call 1b \n\t"
699
0
         AESKEYGENA(xmm1_xmm2, "0x04")  "call 1b \n\t"
700
0
         AESKEYGENA(xmm1_xmm2, "0x08")  "call 1b \n\t"
701
0
         AESKEYGENA(xmm1_xmm2, "0x10")  "call 1b \n\t"
702
0
         AESKEYGENA(xmm1_xmm2, "0x20")  "call 1b \n\t"
703
0
         AESKEYGENA(xmm1_xmm2, "0x40")  "call 1b \n\t"
704
0
         AESKEYGENA(xmm1_xmm2, "0x80")  "call 1b \n\t"
705
706
0
         :
707
0
         : "r" (rk), "r" (key)
708
0
         : "memory", "cc", "0");
709
0
}
710
711
/*
712
 * Key expansion, 256-bit case
713
 */
714
static void aesni_setkey_enc_256(unsigned char *rk,
715
                                 const unsigned char *key)
716
0
{
717
0
    asm ("movdqu (%1), %%xmm0           \n\t"
718
0
         "movdqu %%xmm0, (%0)           \n\t"
719
0
         "add $16, %0                   \n\t"
720
0
         "movdqu 16(%1), %%xmm1         \n\t"
721
0
         "movdqu %%xmm1, (%0)           \n\t"
722
0
         "jmp 2f                        \n\t" // skip auxiliary routine
723
724
         /*
725
          * Finish generating the next two round keys.
726
          *
727
          * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
728
          * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
729
          *
730
          * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
731
          * and those have been written to the output buffer.
732
          */
733
0
         "1:                                \n\t"
734
0
         "pshufd $0xff, %%xmm2, %%xmm2      \n\t"
735
0
         "pxor %%xmm0, %%xmm2               \n\t"
736
0
         "pslldq $4, %%xmm0                 \n\t"
737
0
         "pxor %%xmm0, %%xmm2               \n\t"
738
0
         "pslldq $4, %%xmm0                 \n\t"
739
0
         "pxor %%xmm0, %%xmm2               \n\t"
740
0
         "pslldq $4, %%xmm0                 \n\t"
741
0
         "pxor %%xmm2, %%xmm0               \n\t"
742
0
         "add $16, %0                       \n\t"
743
0
         "movdqu %%xmm0, (%0)               \n\t"
744
745
         /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
746
          * and proceed to generate next round key from there */
747
0
         AESKEYGENA(xmm0_xmm2, "0x00")
748
0
         "pshufd $0xaa, %%xmm2, %%xmm2      \n\t"
749
0
         "pxor %%xmm1, %%xmm2               \n\t"
750
0
         "pslldq $4, %%xmm1                 \n\t"
751
0
         "pxor %%xmm1, %%xmm2               \n\t"
752
0
         "pslldq $4, %%xmm1                 \n\t"
753
0
         "pxor %%xmm1, %%xmm2               \n\t"
754
0
         "pslldq $4, %%xmm1                 \n\t"
755
0
         "pxor %%xmm2, %%xmm1               \n\t"
756
0
         "add $16, %0                       \n\t"
757
0
         "movdqu %%xmm1, (%0)               \n\t"
758
0
         "ret                               \n\t"
759
760
         /*
761
          * Main "loop" - Generating one more key than necessary,
762
          * see definition of mbedtls_aes_context.buf
763
          */
764
0
         "2:                                \n\t"
765
0
         AESKEYGENA(xmm1_xmm2, "0x01")      "call 1b \n\t"
766
0
         AESKEYGENA(xmm1_xmm2, "0x02")      "call 1b \n\t"
767
0
         AESKEYGENA(xmm1_xmm2, "0x04")      "call 1b \n\t"
768
0
         AESKEYGENA(xmm1_xmm2, "0x08")      "call 1b \n\t"
769
0
         AESKEYGENA(xmm1_xmm2, "0x10")      "call 1b \n\t"
770
0
         AESKEYGENA(xmm1_xmm2, "0x20")      "call 1b \n\t"
771
0
         AESKEYGENA(xmm1_xmm2, "0x40")      "call 1b \n\t"
772
0
         :
773
0
         : "r" (rk), "r" (key)
774
0
         : "memory", "cc", "0");
775
0
}
776
777
#endif  /* MBEDTLS_AESNI_HAVE_CODE */
778
779
/*
780
 * Key expansion, wrapper
781
 */
782
int mbedtls_aesni_setkey_enc(unsigned char *rk,
783
                             const unsigned char *key,
784
                             size_t bits)
785
803k
{
786
803k
    switch (bits) {
787
803k
        case 128: aesni_setkey_enc_128(rk, key); break;
788
0
        case 192: aesni_setkey_enc_192(rk, key); break;
789
0
        case 256: aesni_setkey_enc_256(rk, key); break;
790
0
        default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
791
803k
    }
792
793
803k
    return 0;
794
803k
}
795
796
#endif /* MBEDTLS_AESNI_HAVE_CODE */
797
798
#endif /* MBEDTLS_AESNI_C */