summaryrefslogtreecommitdiff
path: root/cipher/rijndael-armv8-ce.c
blob: 6e46830ee45bfeb88d83a8cbcd8798b762cc5aac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* ARMv8 Crypto Extension AES for Libgcrypt
 * Copyright (C) 2016 Jussi Kivilinna <jussi.kivilinna@iki.fi>
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* for memcmp() */

#include "types.h"  /* for byte and u32 typedefs */
#include "g10lib.h"
#include "cipher.h"
#include "bufhelp.h"
#include "cipher-selftest.h"
#include "rijndael-internal.h"
#include "./cipher-internal.h"


#ifdef USE_ARM_CE


typedef struct u128_s { u32 a, b, c, d; } u128_t;

extern u32 _gcry_aes_sbox4_armv8_ce(u32 in4b);
extern void _gcry_aes_invmixcol_armv8_ce(u128_t *dst, const u128_t *src);

extern unsigned int _gcry_aes_enc_armv8_ce(const void *keysched, byte *dst,
                                           const byte *src,
                                           unsigned int nrounds);
extern unsigned int _gcry_aes_dec_armv8_ce(const void *keysched, byte *dst,
                                           const byte *src,
                                           unsigned int nrounds);

extern void _gcry_aes_cbc_enc_armv8_ce (const void *keysched,
                                        unsigned char *outbuf,
                                        const unsigned char *inbuf,
                                        unsigned char *iv, size_t nblocks,
                                        int cbc_mac, unsigned int nrounds);
extern void _gcry_aes_cbc_dec_armv8_ce (const void *keysched,
                                        unsigned char *outbuf,
                                        const unsigned char *inbuf,
                                        unsigned char *iv, size_t nblocks,
                                        unsigned int nrounds);

extern void _gcry_aes_cfb_enc_armv8_ce (const void *keysched,
                                        unsigned char *outbuf,
                                        const unsigned char *inbuf,
                                        unsigned char *iv, size_t nblocks,
                                        unsigned int nrounds);
extern void _gcry_aes_cfb_dec_armv8_ce (const void *keysched,
                                        unsigned char *outbuf,
                                        const unsigned char *inbuf,
                                        unsigned char *iv, size_t nblocks,
                                        unsigned int nrounds);

extern void _gcry_aes_ctr_enc_armv8_ce (const void *keysched,
                                        unsigned char *outbuf,
                                        const unsigned char *inbuf,
                                        unsigned char *iv, size_t nblocks,
                                        unsigned int nrounds);

extern void _gcry_aes_ocb_enc_armv8_ce (const void *keysched,
                                        unsigned char *outbuf,
                                        const unsigned char *inbuf,
                                        unsigned char *offset,
                                        unsigned char *checksum,
                                        unsigned char *L_table,
                                        size_t nblocks,
                                        unsigned int nrounds,
                                        unsigned int blkn);
extern void _gcry_aes_ocb_dec_armv8_ce (const void *keysched,
                                        unsigned char *outbuf,
                                        const unsigned char *inbuf,
                                        unsigned char *offset,
                                        unsigned char *checksum,
                                        unsigned char *L_table,
                                        size_t nblocks,
                                        unsigned int nrounds,
                                        unsigned int blkn);
extern void _gcry_aes_ocb_auth_armv8_ce (const void *keysched,
                                         const unsigned char *abuf,
                                         unsigned char *offset,
                                         unsigned char *checksum,
                                         unsigned char *L_table,
                                         size_t nblocks,
                                         unsigned int nrounds,
                                         unsigned int blkn);
extern void _gcry_aes_xts_enc_armv8_ce (const void *keysched,
                                        unsigned char *outbuf,
                                        const unsigned char *inbuf,
                                        unsigned char *tweak,
                                        size_t nblocks, unsigned int nrounds);
extern void _gcry_aes_xts_dec_armv8_ce (const void *keysched,
                                        unsigned char *outbuf,
                                        const unsigned char *inbuf,
                                        unsigned char *tweak,
                                        size_t nblocks, unsigned int nrounds);

typedef void (*ocb_crypt_fn_t) (const void *keysched, unsigned char *outbuf,
                                const unsigned char *inbuf,
                                unsigned char *offset, unsigned char *checksum,
                                unsigned char *L_table, size_t nblocks,
                                unsigned int nrounds, unsigned int blkn);

typedef void (*xts_crypt_fn_t) (const void *keysched, unsigned char *outbuf,
                                const unsigned char *inbuf,
                                unsigned char *tweak, size_t nblocks,
                                unsigned int nrounds);

void
_gcry_aes_armv8_ce_setkey (RIJNDAEL_context *ctx, const byte *key)
{
  union
    {
      PROPERLY_ALIGNED_TYPE dummy;
      byte data[MAXKC][4];
      u32 data32[MAXKC];
    } tkk[2];
  unsigned int rounds = ctx->rounds;
  int KC = rounds - 6;
  unsigned int keylen = KC * 4;
  unsigned int i, r, t;
  byte rcon = 1;
  int j;
#define k      tkk[0].data
#define k_u32  tkk[0].data32
#define tk     tkk[1].data
#define tk_u32 tkk[1].data32
#define W      (ctx->keyschenc)
#define W_u32  (ctx->keyschenc32)

  for (i = 0; i < keylen; i++)
    {
      k[i >> 2][i & 3] = key[i];
    }

  for (j = KC-1; j >= 0; j--)
    {
      tk_u32[j] = k_u32[j];
    }
  r = 0;
  t = 0;
  /* Copy values into round key array.  */
  for (j = 0; (j < KC) && (r < rounds + 1); )
    {
      for (; (j < KC) && (t < 4); j++, t++)
        {
          W_u32[r][t] = le_bswap32(tk_u32[j]);
        }
      if (t == 4)
        {
          r++;
          t = 0;
        }
    }

  while (r < rounds + 1)
    {
      tk_u32[0] ^= _gcry_aes_sbox4_armv8_ce(rol(tk_u32[KC - 1], 24)) ^ rcon;

      if (KC != 8)
        {
          for (j = 1; j < KC; j++)
            {
              tk_u32[j] ^= tk_u32[j-1];
            }
        }
      else
        {
          for (j = 1; j < KC/2; j++)
            {
              tk_u32[j] ^= tk_u32[j-1];
            }

          tk_u32[KC/2] ^= _gcry_aes_sbox4_armv8_ce(tk_u32[KC/2 - 1]);

          for (j = KC/2 + 1; j < KC; j++)
            {
              tk_u32[j] ^= tk_u32[j-1];
            }
        }

      /* Copy values into round key array.  */
      for (j = 0; (j < KC) && (r < rounds + 1); )
        {
          for (; (j < KC) && (t < 4); j++, t++)
            {
              W_u32[r][t] = le_bswap32(tk_u32[j]);
            }
          if (t == 4)
            {
              r++;
              t = 0;
            }
        }

      rcon = (rcon << 1) ^ ((rcon >> 7) * 0x1b);
    }

#undef W
#undef tk
#undef k
#undef W_u32
#undef tk_u32
#undef k_u32
  wipememory(&tkk, sizeof(tkk));
}

/* Make a decryption key from an encryption key. */
void
_gcry_aes_armv8_ce_prepare_decryption (RIJNDAEL_context *ctx)
{
  u128_t *ekey = (u128_t *)(void *)ctx->keyschenc;
  u128_t *dkey = (u128_t *)(void *)ctx->keyschdec;
  int rounds = ctx->rounds;
  int rr;
  int r;

#define DO_AESIMC() _gcry_aes_invmixcol_armv8_ce(&dkey[r], &ekey[rr])

  dkey[0] = ekey[rounds];
  r = 1;
  rr = rounds-1;
  DO_AESIMC(); r++; rr--; /* round 1 */
  DO_AESIMC(); r++; rr--; /* round 2 */
  DO_AESIMC(); r++; rr--; /* round 3 */
  DO_AESIMC(); r++; rr--; /* round 4 */
  DO_AESIMC(); r++; rr--; /* round 5 */
  DO_AESIMC(); r++; rr--; /* round 6 */
  DO_AESIMC(); r++; rr--; /* round 7 */
  DO_AESIMC(); r++; rr--; /* round 8 */
  DO_AESIMC(); r++; rr--; /* round 9 */
  if (rounds >= 12)
    {
      if (rounds > 12)
        {
          DO_AESIMC(); r++; rr--; /* round 10 */
          DO_AESIMC(); r++; rr--; /* round 11 */
        }

      DO_AESIMC(); r++; rr--; /* round 12 / 10 */
      DO_AESIMC(); r++; rr--; /* round 13 / 11 */
    }

  dkey[r] = ekey[0];

#undef DO_AESIMC
}

unsigned int
_gcry_aes_armv8_ce_encrypt (const RIJNDAEL_context *ctx, unsigned char *dst,
                            const unsigned char *src)
{
  const void *keysched = ctx->keyschenc32;
  unsigned int nrounds = ctx->rounds;

  return _gcry_aes_enc_armv8_ce(keysched, dst, src, nrounds);
}

unsigned int
_gcry_aes_armv8_ce_decrypt (const RIJNDAEL_context *ctx, unsigned char *dst,
                            const unsigned char *src)
{
  const void *keysched = ctx->keyschdec32;
  unsigned int nrounds = ctx->rounds;

  return _gcry_aes_dec_armv8_ce(keysched, dst, src, nrounds);
}

void
_gcry_aes_armv8_ce_cbc_enc (const RIJNDAEL_context *ctx, unsigned char *iv,
                            unsigned char *outbuf, const unsigned char *inbuf,
                            size_t nblocks, int cbc_mac)
{
  const void *keysched = ctx->keyschenc32;
  unsigned int nrounds = ctx->rounds;

  _gcry_aes_cbc_enc_armv8_ce(keysched, outbuf, inbuf, iv, nblocks, cbc_mac,
                             nrounds);
}

void
_gcry_aes_armv8_ce_cbc_dec (RIJNDAEL_context *ctx, unsigned char *iv,
                            unsigned char *outbuf, const unsigned char *inbuf,
                            size_t nblocks)
{
  const void *keysched = ctx->keyschdec32;
  unsigned int nrounds = ctx->rounds;

  if ( !ctx->decryption_prepared )
    {
      _gcry_aes_armv8_ce_prepare_decryption ( ctx );
      ctx->decryption_prepared = 1;
    }

  _gcry_aes_cbc_dec_armv8_ce(keysched, outbuf, inbuf, iv, nblocks, nrounds);
}

void
_gcry_aes_armv8_ce_cfb_enc (RIJNDAEL_context *ctx, unsigned char *iv,
                            unsigned char *outbuf, const unsigned char *inbuf,
                            size_t nblocks)
{
  const void *keysched = ctx->keyschenc32;
  unsigned int nrounds = ctx->rounds;

  _gcry_aes_cfb_enc_armv8_ce(keysched, outbuf, inbuf, iv, nblocks, nrounds);
}

void
_gcry_aes_armv8_ce_cfb_dec (RIJNDAEL_context *ctx, unsigned char *iv,
                            unsigned char *outbuf, const unsigned char *inbuf,
                            size_t nblocks)
{
  const void *keysched = ctx->keyschenc32;
  unsigned int nrounds = ctx->rounds;

  _gcry_aes_cfb_dec_armv8_ce(keysched, outbuf, inbuf, iv, nblocks, nrounds);
}

void
_gcry_aes_armv8_ce_ctr_enc (RIJNDAEL_context *ctx, unsigned char *iv,
                            unsigned char *outbuf, const unsigned char *inbuf,
                            size_t nblocks)
{
  const void *keysched = ctx->keyschenc32;
  unsigned int nrounds = ctx->rounds;

  _gcry_aes_ctr_enc_armv8_ce(keysched, outbuf, inbuf, iv, nblocks, nrounds);
}

size_t
_gcry_aes_armv8_ce_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
                              const void *inbuf_arg, size_t nblocks,
                              int encrypt)
{
  RIJNDAEL_context *ctx = (void *)&c->context.c;
  const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32;
  ocb_crypt_fn_t crypt_fn = encrypt ? _gcry_aes_ocb_enc_armv8_ce
                                    : _gcry_aes_ocb_dec_armv8_ce;
  unsigned char *outbuf = outbuf_arg;
  const unsigned char *inbuf = inbuf_arg;
  unsigned int nrounds = ctx->rounds;
  u64 blkn = c->u_mode.ocb.data_nblocks;

  if ( !encrypt && !ctx->decryption_prepared )
    {
      _gcry_aes_armv8_ce_prepare_decryption ( ctx );
      ctx->decryption_prepared = 1;
    }

  c->u_mode.ocb.data_nblocks = blkn + nblocks;

  crypt_fn(keysched, outbuf, inbuf, c->u_iv.iv, c->u_ctr.ctr,
           c->u_mode.ocb.L[0], nblocks, nrounds, (unsigned int)blkn);

  return 0;
}

size_t
_gcry_aes_armv8_ce_ocb_auth (gcry_cipher_hd_t c, void *abuf_arg,
                             size_t nblocks)
{
  RIJNDAEL_context *ctx = (void *)&c->context.c;
  const void *keysched = ctx->keyschenc32;
  const unsigned char *abuf = abuf_arg;
  unsigned int nrounds = ctx->rounds;
  u64 blkn = c->u_mode.ocb.aad_nblocks;

  c->u_mode.ocb.aad_nblocks = blkn + nblocks;

  _gcry_aes_ocb_auth_armv8_ce(keysched, abuf, c->u_mode.ocb.aad_offset,
			      c->u_mode.ocb.aad_sum, c->u_mode.ocb.L[0],
			      nblocks, nrounds, (unsigned int)blkn);

  return 0;
}

void
_gcry_aes_armv8_ce_xts_crypt (RIJNDAEL_context *ctx, unsigned char *tweak,
			      unsigned char *outbuf, const unsigned char *inbuf,
			      size_t nblocks, int encrypt)
{
  const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32;
  xts_crypt_fn_t crypt_fn = encrypt ? _gcry_aes_xts_enc_armv8_ce
                                    : _gcry_aes_xts_dec_armv8_ce;
  unsigned int nrounds = ctx->rounds;

  if ( !encrypt && !ctx->decryption_prepared )
    {
      _gcry_aes_armv8_ce_prepare_decryption ( ctx );
      ctx->decryption_prepared = 1;
    }

  crypt_fn(keysched, outbuf, inbuf, tweak, nblocks, nrounds);
}

#endif /* USE_ARM_CE */