blob: 0b49b6e9869a9a04ec39e4ac065457d6fda861ec [file] [log] [blame]
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001/*
2 * Cryptographic API.
3 *
4 * Support for ATMEL AES HW acceleration.
5 *
6 * Copyright (c) 2012 Eukréa Electromatique - ATMEL
7 * Author: Nicolas Royer <nicolas@eukrea.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
12 *
13 * Some ideas are from omap-aes.c driver.
14 */
15
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/err.h>
21#include <linux/clk.h>
22#include <linux/io.h>
23#include <linux/hw_random.h>
24#include <linux/platform_device.h>
25
26#include <linux/device.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020027#include <linux/init.h>
28#include <linux/errno.h>
29#include <linux/interrupt.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020030#include <linux/irq.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020031#include <linux/scatterlist.h>
32#include <linux/dma-mapping.h>
Nicolas Ferrebe943c72013-10-14 17:52:38 +020033#include <linux/of_device.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020034#include <linux/delay.h>
35#include <linux/crypto.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020036#include <crypto/scatterwalk.h>
37#include <crypto/algapi.h>
38#include <crypto/aes.h>
Corentin LABBE219d51c2017-08-22 10:08:12 +020039#include <crypto/gcm.h>
Cyrille Pitchend52db512016-10-03 14:33:16 +020040#include <crypto/xts.h>
Cyrille Pitchend4419542015-12-17 18:13:07 +010041#include <crypto/internal/aead.h>
Nicolas Royercadc4ab2013-02-20 17:10:24 +010042#include <linux/platform_data/crypto-atmel.h>
Nicolas Ferrebe943c72013-10-14 17:52:38 +020043#include <dt-bindings/dma/at91.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020044#include "atmel-aes-regs.h"
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010045#include "atmel-authenc.h"
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020046
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +010047#define ATMEL_AES_PRIORITY 300
48
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +010049#define ATMEL_AES_BUFFER_ORDER 2
50#define ATMEL_AES_BUFFER_SIZE (PAGE_SIZE << ATMEL_AES_BUFFER_ORDER)
51
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020052#define CFB8_BLOCK_SIZE 1
53#define CFB16_BLOCK_SIZE 2
54#define CFB32_BLOCK_SIZE 4
55#define CFB64_BLOCK_SIZE 8
56
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +010057#define SIZE_IN_WORDS(x) ((x) >> 2)
58
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020059/* AES flags */
Cyrille Pitchend4419542015-12-17 18:13:07 +010060/* Reserve bits [18:16] [14:12] [1:0] for mode (same as for AES_MR) */
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010061#define AES_FLAGS_ENCRYPT AES_MR_CYPHER_ENC
Cyrille Pitchend4419542015-12-17 18:13:07 +010062#define AES_FLAGS_GTAGEN AES_MR_GTAGEN
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010063#define AES_FLAGS_OPMODE_MASK (AES_MR_OPMOD_MASK | AES_MR_CFBS_MASK)
64#define AES_FLAGS_ECB AES_MR_OPMOD_ECB
65#define AES_FLAGS_CBC AES_MR_OPMOD_CBC
66#define AES_FLAGS_OFB AES_MR_OPMOD_OFB
67#define AES_FLAGS_CFB128 (AES_MR_OPMOD_CFB | AES_MR_CFBS_128b)
68#define AES_FLAGS_CFB64 (AES_MR_OPMOD_CFB | AES_MR_CFBS_64b)
69#define AES_FLAGS_CFB32 (AES_MR_OPMOD_CFB | AES_MR_CFBS_32b)
70#define AES_FLAGS_CFB16 (AES_MR_OPMOD_CFB | AES_MR_CFBS_16b)
71#define AES_FLAGS_CFB8 (AES_MR_OPMOD_CFB | AES_MR_CFBS_8b)
72#define AES_FLAGS_CTR AES_MR_OPMOD_CTR
Cyrille Pitchend4419542015-12-17 18:13:07 +010073#define AES_FLAGS_GCM AES_MR_OPMOD_GCM
Cyrille Pitchend52db512016-10-03 14:33:16 +020074#define AES_FLAGS_XTS AES_MR_OPMOD_XTS
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020075
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010076#define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \
Cyrille Pitchend4419542015-12-17 18:13:07 +010077 AES_FLAGS_ENCRYPT | \
78 AES_FLAGS_GTAGEN)
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010079
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010080#define AES_FLAGS_BUSY BIT(3)
Cyrille Pitchen45379922015-12-17 18:13:08 +010081#define AES_FLAGS_DUMP_REG BIT(4)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010082#define AES_FLAGS_OWN_SHA BIT(5)
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010083
Romain Izard7a373fd2017-10-31 16:25:24 +010084#define AES_FLAGS_PERSISTENT AES_FLAGS_BUSY
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020085
Nicolas Royercadc4ab2013-02-20 17:10:24 +010086#define ATMEL_AES_QUEUE_LENGTH 50
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020087
Cyrille Pitchen129f8bb2015-12-17 18:13:06 +010088#define ATMEL_AES_DMA_THRESHOLD 256
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020089
90
Nicolas Royercadc4ab2013-02-20 17:10:24 +010091struct atmel_aes_caps {
Cyrille Pitchenafbac172015-12-17 18:13:02 +010092 bool has_dualbuff;
93 bool has_cfb64;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +010094 bool has_ctr32;
Cyrille Pitchend4419542015-12-17 18:13:07 +010095 bool has_gcm;
Cyrille Pitchend52db512016-10-03 14:33:16 +020096 bool has_xts;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010097 bool has_authenc;
Cyrille Pitchenafbac172015-12-17 18:13:02 +010098 u32 max_burst_size;
Nicolas Royercadc4ab2013-02-20 17:10:24 +010099};
100
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200101struct atmel_aes_dev;
102
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100103
104typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *);
105
106
107struct atmel_aes_base_ctx {
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100108 struct atmel_aes_dev *dd;
109 atmel_aes_fn_t start;
110 int keylen;
111 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
112 u16 block_size;
Romain Izard91308012017-10-31 16:25:23 +0100113 bool is_aead;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200114};
115
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100116struct atmel_aes_ctx {
117 struct atmel_aes_base_ctx base;
118};
119
Cyrille Pitchenfcac8362015-12-17 18:13:05 +0100120struct atmel_aes_ctr_ctx {
121 struct atmel_aes_base_ctx base;
122
123 u32 iv[AES_BLOCK_SIZE / sizeof(u32)];
124 size_t offset;
125 struct scatterlist src[2];
126 struct scatterlist dst[2];
127};
128
Cyrille Pitchend4419542015-12-17 18:13:07 +0100129struct atmel_aes_gcm_ctx {
130 struct atmel_aes_base_ctx base;
131
132 struct scatterlist src[2];
133 struct scatterlist dst[2];
134
135 u32 j0[AES_BLOCK_SIZE / sizeof(u32)];
136 u32 tag[AES_BLOCK_SIZE / sizeof(u32)];
137 u32 ghash[AES_BLOCK_SIZE / sizeof(u32)];
138 size_t textlen;
139
140 const u32 *ghash_in;
141 u32 *ghash_out;
142 atmel_aes_fn_t ghash_resume;
143};
144
Cyrille Pitchend52db512016-10-03 14:33:16 +0200145struct atmel_aes_xts_ctx {
146 struct atmel_aes_base_ctx base;
147
148 u32 key2[AES_KEYSIZE_256 / sizeof(u32)];
149};
150
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100151#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
152struct atmel_aes_authenc_ctx {
153 struct atmel_aes_base_ctx base;
154 struct atmel_sha_authenc_ctx *auth;
155};
156#endif
157
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200158struct atmel_aes_reqctx {
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100159 unsigned long mode;
Romain Izard91308012017-10-31 16:25:23 +0100160 u32 lastc[AES_BLOCK_SIZE / sizeof(u32)];
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200161};
162
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100163#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
164struct atmel_aes_authenc_reqctx {
165 struct atmel_aes_reqctx base;
166
167 struct scatterlist src[2];
168 struct scatterlist dst[2];
169 size_t textlen;
170 u32 digest[SHA512_DIGEST_SIZE / sizeof(u32)];
171
172 /* auth_req MUST be place last. */
173 struct ahash_request auth_req;
174};
175#endif
176
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200177struct atmel_aes_dma {
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100178 struct dma_chan *chan;
179 struct scatterlist *sg;
180 int nents;
181 unsigned int remainder;
182 unsigned int sg_len;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200183};
184
185struct atmel_aes_dev {
186 struct list_head list;
187 unsigned long phys_base;
188 void __iomem *io_base;
189
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100190 struct crypto_async_request *areq;
191 struct atmel_aes_base_ctx *ctx;
192
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100193 bool is_async;
194 atmel_aes_fn_t resume;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100195 atmel_aes_fn_t cpu_transfer_complete;
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100196
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200197 struct device *dev;
198 struct clk *iclk;
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100199 int irq;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200200
201 unsigned long flags;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200202
203 spinlock_t lock;
204 struct crypto_queue queue;
205
206 struct tasklet_struct done_task;
207 struct tasklet_struct queue_task;
208
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100209 size_t total;
210 size_t datalen;
211 u32 *data;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200212
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100213 struct atmel_aes_dma src;
214 struct atmel_aes_dma dst;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200215
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100216 size_t buflen;
217 void *buf;
218 struct scatterlist aligned_sg;
219 struct scatterlist *real_dst;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200220
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100221 struct atmel_aes_caps caps;
222
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100223 u32 hw_version;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200224};
225
226struct atmel_aes_drv {
227 struct list_head dev_list;
228 spinlock_t lock;
229};
230
231static struct atmel_aes_drv atmel_aes = {
232 .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list),
233 .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock),
234};
235
Cyrille Pitchen45379922015-12-17 18:13:08 +0100236#ifdef VERBOSE_DEBUG
237static const char *atmel_aes_reg_name(u32 offset, char *tmp, size_t sz)
238{
239 switch (offset) {
240 case AES_CR:
241 return "CR";
242
243 case AES_MR:
244 return "MR";
245
246 case AES_ISR:
247 return "ISR";
248
249 case AES_IMR:
250 return "IMR";
251
252 case AES_IER:
253 return "IER";
254
255 case AES_IDR:
256 return "IDR";
257
258 case AES_KEYWR(0):
259 case AES_KEYWR(1):
260 case AES_KEYWR(2):
261 case AES_KEYWR(3):
262 case AES_KEYWR(4):
263 case AES_KEYWR(5):
264 case AES_KEYWR(6):
265 case AES_KEYWR(7):
266 snprintf(tmp, sz, "KEYWR[%u]", (offset - AES_KEYWR(0)) >> 2);
267 break;
268
269 case AES_IDATAR(0):
270 case AES_IDATAR(1):
271 case AES_IDATAR(2):
272 case AES_IDATAR(3):
273 snprintf(tmp, sz, "IDATAR[%u]", (offset - AES_IDATAR(0)) >> 2);
274 break;
275
276 case AES_ODATAR(0):
277 case AES_ODATAR(1):
278 case AES_ODATAR(2):
279 case AES_ODATAR(3):
280 snprintf(tmp, sz, "ODATAR[%u]", (offset - AES_ODATAR(0)) >> 2);
281 break;
282
283 case AES_IVR(0):
284 case AES_IVR(1):
285 case AES_IVR(2):
286 case AES_IVR(3):
287 snprintf(tmp, sz, "IVR[%u]", (offset - AES_IVR(0)) >> 2);
288 break;
289
290 case AES_AADLENR:
291 return "AADLENR";
292
293 case AES_CLENR:
294 return "CLENR";
295
296 case AES_GHASHR(0):
297 case AES_GHASHR(1):
298 case AES_GHASHR(2):
299 case AES_GHASHR(3):
300 snprintf(tmp, sz, "GHASHR[%u]", (offset - AES_GHASHR(0)) >> 2);
301 break;
302
303 case AES_TAGR(0):
304 case AES_TAGR(1):
305 case AES_TAGR(2):
306 case AES_TAGR(3):
307 snprintf(tmp, sz, "TAGR[%u]", (offset - AES_TAGR(0)) >> 2);
308 break;
309
310 case AES_CTRR:
311 return "CTRR";
312
313 case AES_GCMHR(0):
314 case AES_GCMHR(1):
315 case AES_GCMHR(2):
316 case AES_GCMHR(3):
317 snprintf(tmp, sz, "GCMHR[%u]", (offset - AES_GCMHR(0)) >> 2);
Herbert Xue31835a2016-01-19 09:05:43 +0800318 break;
Cyrille Pitchen45379922015-12-17 18:13:08 +0100319
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100320 case AES_EMR:
321 return "EMR";
322
Cyrille Pitchend52db512016-10-03 14:33:16 +0200323 case AES_TWR(0):
324 case AES_TWR(1):
325 case AES_TWR(2):
326 case AES_TWR(3):
327 snprintf(tmp, sz, "TWR[%u]", (offset - AES_TWR(0)) >> 2);
328 break;
329
330 case AES_ALPHAR(0):
331 case AES_ALPHAR(1):
332 case AES_ALPHAR(2):
333 case AES_ALPHAR(3):
334 snprintf(tmp, sz, "ALPHAR[%u]", (offset - AES_ALPHAR(0)) >> 2);
335 break;
336
Cyrille Pitchen45379922015-12-17 18:13:08 +0100337 default:
338 snprintf(tmp, sz, "0x%02x", offset);
339 break;
340 }
341
342 return tmp;
343}
344#endif /* VERBOSE_DEBUG */
345
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100346/* Shared functions */
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100347
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200348static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset)
349{
Cyrille Pitchen45379922015-12-17 18:13:08 +0100350 u32 value = readl_relaxed(dd->io_base + offset);
351
352#ifdef VERBOSE_DEBUG
353 if (dd->flags & AES_FLAGS_DUMP_REG) {
354 char tmp[16];
355
356 dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
357 atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
358 }
359#endif /* VERBOSE_DEBUG */
360
361 return value;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200362}
363
364static inline void atmel_aes_write(struct atmel_aes_dev *dd,
365 u32 offset, u32 value)
366{
Cyrille Pitchen45379922015-12-17 18:13:08 +0100367#ifdef VERBOSE_DEBUG
368 if (dd->flags & AES_FLAGS_DUMP_REG) {
369 char tmp[16];
370
371 dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
Cyrille Pitchenf709dc82016-09-29 18:46:57 +0200372 atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
Cyrille Pitchen45379922015-12-17 18:13:08 +0100373 }
374#endif /* VERBOSE_DEBUG */
375
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200376 writel_relaxed(value, dd->io_base + offset);
377}
378
379static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset,
380 u32 *value, int count)
381{
382 for (; count--; value++, offset += 4)
383 *value = atmel_aes_read(dd, offset);
384}
385
386static void atmel_aes_write_n(struct atmel_aes_dev *dd, u32 offset,
Cyrille Pitchenc0b28d82015-12-17 17:48:33 +0100387 const u32 *value, int count)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200388{
389 for (; count--; value++, offset += 4)
390 atmel_aes_write(dd, offset, *value);
391}
392
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100393static inline void atmel_aes_read_block(struct atmel_aes_dev *dd, u32 offset,
394 u32 *value)
395{
396 atmel_aes_read_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
397}
398
399static inline void atmel_aes_write_block(struct atmel_aes_dev *dd, u32 offset,
400 const u32 *value)
401{
402 atmel_aes_write_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
403}
404
405static inline int atmel_aes_wait_for_data_ready(struct atmel_aes_dev *dd,
406 atmel_aes_fn_t resume)
407{
408 u32 isr = atmel_aes_read(dd, AES_ISR);
409
410 if (unlikely(isr & AES_INT_DATARDY))
411 return resume(dd);
412
413 dd->resume = resume;
414 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
415 return -EINPROGRESS;
416}
417
418static inline size_t atmel_aes_padlen(size_t len, size_t block_size)
419{
420 len &= block_size - 1;
421 return len ? block_size - len : 0;
422}
423
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100424static struct atmel_aes_dev *atmel_aes_find_dev(struct atmel_aes_base_ctx *ctx)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200425{
426 struct atmel_aes_dev *aes_dd = NULL;
427 struct atmel_aes_dev *tmp;
428
429 spin_lock_bh(&atmel_aes.lock);
430 if (!ctx->dd) {
431 list_for_each_entry(tmp, &atmel_aes.dev_list, list) {
432 aes_dd = tmp;
433 break;
434 }
435 ctx->dd = aes_dd;
436 } else {
437 aes_dd = ctx->dd;
438 }
439
440 spin_unlock_bh(&atmel_aes.lock);
441
442 return aes_dd;
443}
444
445static int atmel_aes_hw_init(struct atmel_aes_dev *dd)
446{
LABBE Corentin9d83d292015-10-02 14:12:58 +0200447 int err;
448
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100449 err = clk_enable(dd->iclk);
LABBE Corentin9d83d292015-10-02 14:12:58 +0200450 if (err)
451 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200452
Romain Izard7a373fd2017-10-31 16:25:24 +0100453 atmel_aes_write(dd, AES_CR, AES_CR_SWRST);
454 atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200455
456 return 0;
457}
458
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100459static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd)
460{
461 return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff;
462}
463
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100464static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200465{
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100466 int err;
467
468 err = atmel_aes_hw_init(dd);
469 if (err)
470 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200471
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100472 dd->hw_version = atmel_aes_get_version(dd);
473
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100474 dev_info(dd->dev, "version: 0x%x\n", dd->hw_version);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200475
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100476 clk_disable(dd->iclk);
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100477 return 0;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200478}
479
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100480static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd,
481 const struct atmel_aes_reqctx *rctx)
482{
483 /* Clear all but persistent flags and set request flags. */
484 dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode;
485}
486
Cyrille Pitchend4419542015-12-17 18:13:07 +0100487static inline bool atmel_aes_is_encrypt(const struct atmel_aes_dev *dd)
488{
489 return (dd->flags & AES_FLAGS_ENCRYPT);
490}
491
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100492#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
493static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err);
494#endif
495
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100496static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200497{
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100498#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
Romain Izard91308012017-10-31 16:25:23 +0100499 if (dd->ctx->is_aead)
500 atmel_aes_authenc_complete(dd, err);
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100501#endif
502
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100503 clk_disable(dd->iclk);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200504 dd->flags &= ~AES_FLAGS_BUSY;
505
Romain Izard91308012017-10-31 16:25:23 +0100506 if (!dd->ctx->is_aead) {
507 struct ablkcipher_request *req =
508 ablkcipher_request_cast(dd->areq);
509 struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
510 struct crypto_ablkcipher *ablkcipher =
511 crypto_ablkcipher_reqtfm(req);
512 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
513
514 if (rctx->mode & AES_FLAGS_ENCRYPT) {
515 scatterwalk_map_and_copy(req->info, req->dst,
516 req->nbytes - ivsize, ivsize, 0);
517 } else {
518 if (req->src == req->dst) {
519 memcpy(req->info, rctx->lastc, ivsize);
520 } else {
521 scatterwalk_map_and_copy(req->info, req->src,
522 req->nbytes - ivsize, ivsize, 0);
523 }
524 }
525 }
526
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100527 if (dd->is_async)
528 dd->areq->complete(dd->areq, err);
529
530 tasklet_schedule(&dd->queue_task);
531
532 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200533}
534
Cyrille Pitchend52db512016-10-03 14:33:16 +0200535static void atmel_aes_write_ctrl_key(struct atmel_aes_dev *dd, bool use_dma,
536 const u32 *iv, const u32 *key, int keylen)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100537{
538 u32 valmr = 0;
539
540 /* MR register must be set before IV registers */
Cyrille Pitchend52db512016-10-03 14:33:16 +0200541 if (keylen == AES_KEYSIZE_128)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100542 valmr |= AES_MR_KEYSIZE_128;
Cyrille Pitchend52db512016-10-03 14:33:16 +0200543 else if (keylen == AES_KEYSIZE_192)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100544 valmr |= AES_MR_KEYSIZE_192;
545 else
546 valmr |= AES_MR_KEYSIZE_256;
547
548 valmr |= dd->flags & AES_FLAGS_MODE_MASK;
549
550 if (use_dma) {
551 valmr |= AES_MR_SMOD_IDATAR0;
552 if (dd->caps.has_dualbuff)
553 valmr |= AES_MR_DUALBUFF;
554 } else {
555 valmr |= AES_MR_SMOD_AUTO;
556 }
557
558 atmel_aes_write(dd, AES_MR, valmr);
559
Cyrille Pitchend52db512016-10-03 14:33:16 +0200560 atmel_aes_write_n(dd, AES_KEYWR(0), key, SIZE_IN_WORDS(keylen));
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100561
562 if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB)
563 atmel_aes_write_block(dd, AES_IVR(0), iv);
564}
565
Cyrille Pitchend52db512016-10-03 14:33:16 +0200566static inline void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma,
567 const u32 *iv)
568
569{
570 atmel_aes_write_ctrl_key(dd, use_dma, iv,
571 dd->ctx->key, dd->ctx->keylen);
572}
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200573
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100574/* CPU transfer */
575
576static int atmel_aes_cpu_transfer(struct atmel_aes_dev *dd)
577{
578 int err = 0;
579 u32 isr;
580
581 for (;;) {
582 atmel_aes_read_block(dd, AES_ODATAR(0), dd->data);
583 dd->data += 4;
584 dd->datalen -= AES_BLOCK_SIZE;
585
586 if (dd->datalen < AES_BLOCK_SIZE)
587 break;
588
589 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
590
591 isr = atmel_aes_read(dd, AES_ISR);
592 if (!(isr & AES_INT_DATARDY)) {
593 dd->resume = atmel_aes_cpu_transfer;
594 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
595 return -EINPROGRESS;
596 }
597 }
598
599 if (!sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
600 dd->buf, dd->total))
601 err = -EINVAL;
602
603 if (err)
604 return atmel_aes_complete(dd, err);
605
606 return dd->cpu_transfer_complete(dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200607}
608
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100609static int atmel_aes_cpu_start(struct atmel_aes_dev *dd,
610 struct scatterlist *src,
611 struct scatterlist *dst,
612 size_t len,
613 atmel_aes_fn_t resume)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200614{
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100615 size_t padlen = atmel_aes_padlen(len, AES_BLOCK_SIZE);
616
617 if (unlikely(len == 0))
618 return -EINVAL;
619
620 sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
621
622 dd->total = len;
623 dd->real_dst = dst;
624 dd->cpu_transfer_complete = resume;
625 dd->datalen = len + padlen;
626 dd->data = (u32 *)dd->buf;
627 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
628 return atmel_aes_wait_for_data_ready(dd, atmel_aes_cpu_transfer);
629}
630
631
632/* DMA transfer */
633
634static void atmel_aes_dma_callback(void *data);
635
636static bool atmel_aes_check_aligned(struct atmel_aes_dev *dd,
637 struct scatterlist *sg,
638 size_t len,
639 struct atmel_aes_dma *dma)
640{
641 int nents;
642
643 if (!IS_ALIGNED(len, dd->ctx->block_size))
644 return false;
645
646 for (nents = 0; sg; sg = sg_next(sg), ++nents) {
647 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
648 return false;
649
650 if (len <= sg->length) {
651 if (!IS_ALIGNED(len, dd->ctx->block_size))
652 return false;
653
654 dma->nents = nents+1;
655 dma->remainder = sg->length - len;
656 sg->length = len;
657 return true;
658 }
659
660 if (!IS_ALIGNED(sg->length, dd->ctx->block_size))
661 return false;
662
663 len -= sg->length;
664 }
665
666 return false;
667}
668
669static inline void atmel_aes_restore_sg(const struct atmel_aes_dma *dma)
670{
671 struct scatterlist *sg = dma->sg;
672 int nents = dma->nents;
673
674 if (!dma->remainder)
675 return;
676
677 while (--nents > 0 && sg)
678 sg = sg_next(sg);
679
680 if (!sg)
681 return;
682
683 sg->length += dma->remainder;
684}
685
686static int atmel_aes_map(struct atmel_aes_dev *dd,
687 struct scatterlist *src,
688 struct scatterlist *dst,
689 size_t len)
690{
691 bool src_aligned, dst_aligned;
692 size_t padlen;
693
694 dd->total = len;
695 dd->src.sg = src;
696 dd->dst.sg = dst;
697 dd->real_dst = dst;
698
699 src_aligned = atmel_aes_check_aligned(dd, src, len, &dd->src);
700 if (src == dst)
701 dst_aligned = src_aligned;
702 else
703 dst_aligned = atmel_aes_check_aligned(dd, dst, len, &dd->dst);
704 if (!src_aligned || !dst_aligned) {
705 padlen = atmel_aes_padlen(len, dd->ctx->block_size);
706
707 if (dd->buflen < len + padlen)
708 return -ENOMEM;
709
710 if (!src_aligned) {
711 sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
712 dd->src.sg = &dd->aligned_sg;
713 dd->src.nents = 1;
714 dd->src.remainder = 0;
715 }
716
717 if (!dst_aligned) {
718 dd->dst.sg = &dd->aligned_sg;
719 dd->dst.nents = 1;
720 dd->dst.remainder = 0;
721 }
722
723 sg_init_table(&dd->aligned_sg, 1);
724 sg_set_buf(&dd->aligned_sg, dd->buf, len + padlen);
725 }
726
727 if (dd->src.sg == dd->dst.sg) {
728 dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
729 DMA_BIDIRECTIONAL);
730 dd->dst.sg_len = dd->src.sg_len;
731 if (!dd->src.sg_len)
732 return -EFAULT;
733 } else {
734 dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
735 DMA_TO_DEVICE);
736 if (!dd->src.sg_len)
737 return -EFAULT;
738
739 dd->dst.sg_len = dma_map_sg(dd->dev, dd->dst.sg, dd->dst.nents,
740 DMA_FROM_DEVICE);
741 if (!dd->dst.sg_len) {
742 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
743 DMA_TO_DEVICE);
744 return -EFAULT;
745 }
746 }
747
748 return 0;
749}
750
751static void atmel_aes_unmap(struct atmel_aes_dev *dd)
752{
753 if (dd->src.sg == dd->dst.sg) {
754 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
755 DMA_BIDIRECTIONAL);
756
757 if (dd->src.sg != &dd->aligned_sg)
758 atmel_aes_restore_sg(&dd->src);
759 } else {
760 dma_unmap_sg(dd->dev, dd->dst.sg, dd->dst.nents,
761 DMA_FROM_DEVICE);
762
763 if (dd->dst.sg != &dd->aligned_sg)
764 atmel_aes_restore_sg(&dd->dst);
765
766 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
767 DMA_TO_DEVICE);
768
769 if (dd->src.sg != &dd->aligned_sg)
770 atmel_aes_restore_sg(&dd->src);
771 }
772
773 if (dd->dst.sg == &dd->aligned_sg)
774 sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
775 dd->buf, dd->total);
776}
777
778static int atmel_aes_dma_transfer_start(struct atmel_aes_dev *dd,
779 enum dma_slave_buswidth addr_width,
780 enum dma_transfer_direction dir,
781 u32 maxburst)
782{
783 struct dma_async_tx_descriptor *desc;
784 struct dma_slave_config config;
785 dma_async_tx_callback callback;
786 struct atmel_aes_dma *dma;
787 int err;
788
789 memset(&config, 0, sizeof(config));
790 config.direction = dir;
791 config.src_addr_width = addr_width;
792 config.dst_addr_width = addr_width;
793 config.src_maxburst = maxburst;
794 config.dst_maxburst = maxburst;
795
796 switch (dir) {
797 case DMA_MEM_TO_DEV:
798 dma = &dd->src;
799 callback = NULL;
800 config.dst_addr = dd->phys_base + AES_IDATAR(0);
801 break;
802
803 case DMA_DEV_TO_MEM:
804 dma = &dd->dst;
805 callback = atmel_aes_dma_callback;
806 config.src_addr = dd->phys_base + AES_ODATAR(0);
807 break;
808
809 default:
810 return -EINVAL;
811 }
812
813 err = dmaengine_slave_config(dma->chan, &config);
814 if (err)
815 return err;
816
817 desc = dmaengine_prep_slave_sg(dma->chan, dma->sg, dma->sg_len, dir,
818 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
819 if (!desc)
820 return -ENOMEM;
821
822 desc->callback = callback;
823 desc->callback_param = dd;
824 dmaengine_submit(desc);
825 dma_async_issue_pending(dma->chan);
826
827 return 0;
828}
829
830static void atmel_aes_dma_transfer_stop(struct atmel_aes_dev *dd,
831 enum dma_transfer_direction dir)
832{
833 struct atmel_aes_dma *dma;
834
835 switch (dir) {
836 case DMA_MEM_TO_DEV:
837 dma = &dd->src;
838 break;
839
840 case DMA_DEV_TO_MEM:
841 dma = &dd->dst;
842 break;
843
844 default:
845 return;
846 }
847
848 dmaengine_terminate_all(dma->chan);
849}
850
851static int atmel_aes_dma_start(struct atmel_aes_dev *dd,
852 struct scatterlist *src,
853 struct scatterlist *dst,
854 size_t len,
855 atmel_aes_fn_t resume)
856{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100857 enum dma_slave_buswidth addr_width;
858 u32 maxburst;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100859 int err;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100860
861 switch (dd->ctx->block_size) {
862 case CFB8_BLOCK_SIZE:
863 addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
864 maxburst = 1;
865 break;
866
867 case CFB16_BLOCK_SIZE:
868 addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
869 maxburst = 1;
870 break;
871
872 case CFB32_BLOCK_SIZE:
873 case CFB64_BLOCK_SIZE:
874 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
875 maxburst = 1;
876 break;
877
878 case AES_BLOCK_SIZE:
879 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
880 maxburst = dd->caps.max_burst_size;
881 break;
882
883 default:
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100884 err = -EINVAL;
885 goto exit;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100886 }
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200887
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100888 err = atmel_aes_map(dd, src, dst, len);
889 if (err)
890 goto exit;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200891
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100892 dd->resume = resume;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200893
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100894 /* Set output DMA transfer first */
895 err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_DEV_TO_MEM,
896 maxburst);
897 if (err)
898 goto unmap;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100899
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100900 /* Then set input DMA transfer */
901 err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_MEM_TO_DEV,
902 maxburst);
903 if (err)
904 goto output_transfer_stop;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100905
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100906 return -EINPROGRESS;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100907
908output_transfer_stop:
909 atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
910unmap:
911 atmel_aes_unmap(dd);
912exit:
913 return atmel_aes_complete(dd, err);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200914}
915
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100916static void atmel_aes_dma_stop(struct atmel_aes_dev *dd)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200917{
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100918 atmel_aes_dma_transfer_stop(dd, DMA_MEM_TO_DEV);
919 atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
920 atmel_aes_unmap(dd);
921}
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200922
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100923static void atmel_aes_dma_callback(void *data)
924{
925 struct atmel_aes_dev *dd = data;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100926
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100927 atmel_aes_dma_stop(dd);
928 dd->is_async = true;
929 (void)dd->resume(dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200930}
931
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200932static int atmel_aes_handle_queue(struct atmel_aes_dev *dd,
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100933 struct crypto_async_request *new_areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200934{
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100935 struct crypto_async_request *areq, *backlog;
936 struct atmel_aes_base_ctx *ctx;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200937 unsigned long flags;
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100938 bool start_async;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200939 int err, ret = 0;
940
941 spin_lock_irqsave(&dd->lock, flags);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100942 if (new_areq)
943 ret = crypto_enqueue_request(&dd->queue, new_areq);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200944 if (dd->flags & AES_FLAGS_BUSY) {
945 spin_unlock_irqrestore(&dd->lock, flags);
946 return ret;
947 }
948 backlog = crypto_get_backlog(&dd->queue);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100949 areq = crypto_dequeue_request(&dd->queue);
950 if (areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200951 dd->flags |= AES_FLAGS_BUSY;
952 spin_unlock_irqrestore(&dd->lock, flags);
953
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100954 if (!areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200955 return ret;
956
957 if (backlog)
958 backlog->complete(backlog, -EINPROGRESS);
959
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100960 ctx = crypto_tfm_ctx(areq->tfm);
961
962 dd->areq = areq;
963 dd->ctx = ctx;
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100964 start_async = (areq != new_areq);
965 dd->is_async = start_async;
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100966
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100967 /* WARNING: ctx->start() MAY change dd->is_async. */
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100968 err = ctx->start(dd);
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100969 return (start_async) ? ret : err;
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100970}
971
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100972
973/* AES async block ciphers */
974
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100975static int atmel_aes_transfer_complete(struct atmel_aes_dev *dd)
976{
977 return atmel_aes_complete(dd, 0);
978}
979
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100980static int atmel_aes_start(struct atmel_aes_dev *dd)
981{
982 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100983 struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
984 bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD ||
985 dd->ctx->block_size != AES_BLOCK_SIZE);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100986 int err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200987
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100988 atmel_aes_set_mode(dd, rctx);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200989
Cyrille Pitchencdfab4a2015-12-17 17:48:38 +0100990 err = atmel_aes_hw_init(dd);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100991 if (err)
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100992 return atmel_aes_complete(dd, err);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200993
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100994 atmel_aes_write_ctrl(dd, use_dma, req->info);
995 if (use_dma)
996 return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes,
997 atmel_aes_transfer_complete);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200998
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100999 return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes,
1000 atmel_aes_transfer_complete);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001001}
1002
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01001003static inline struct atmel_aes_ctr_ctx *
1004atmel_aes_ctr_ctx_cast(struct atmel_aes_base_ctx *ctx)
1005{
1006 return container_of(ctx, struct atmel_aes_ctr_ctx, base);
1007}
1008
1009static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
1010{
1011 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
1012 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
1013 struct scatterlist *src, *dst;
1014 u32 ctr, blocks;
1015 size_t datalen;
1016 bool use_dma, fragmented = false;
1017
1018 /* Check for transfer completion. */
1019 ctx->offset += dd->total;
1020 if (ctx->offset >= req->nbytes)
1021 return atmel_aes_transfer_complete(dd);
1022
1023 /* Compute data length. */
1024 datalen = req->nbytes - ctx->offset;
1025 blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
1026 ctr = be32_to_cpu(ctx->iv[3]);
1027 if (dd->caps.has_ctr32) {
1028 /* Check 32bit counter overflow. */
1029 u32 start = ctr;
1030 u32 end = start + blocks - 1;
1031
1032 if (end < start) {
1033 ctr |= 0xffffffff;
1034 datalen = AES_BLOCK_SIZE * -start;
1035 fragmented = true;
1036 }
1037 } else {
1038 /* Check 16bit counter overflow. */
1039 u16 start = ctr & 0xffff;
1040 u16 end = start + (u16)blocks - 1;
1041
1042 if (blocks >> 16 || end < start) {
1043 ctr |= 0xffff;
1044 datalen = AES_BLOCK_SIZE * (0x10000-start);
1045 fragmented = true;
1046 }
1047 }
1048 use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD);
1049
1050 /* Jump to offset. */
1051 src = scatterwalk_ffwd(ctx->src, req->src, ctx->offset);
1052 dst = ((req->src == req->dst) ? src :
1053 scatterwalk_ffwd(ctx->dst, req->dst, ctx->offset));
1054
1055 /* Configure hardware. */
1056 atmel_aes_write_ctrl(dd, use_dma, ctx->iv);
1057 if (unlikely(fragmented)) {
1058 /*
1059 * Increment the counter manually to cope with the hardware
1060 * counter overflow.
1061 */
1062 ctx->iv[3] = cpu_to_be32(ctr);
1063 crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE);
1064 }
1065
1066 if (use_dma)
1067 return atmel_aes_dma_start(dd, src, dst, datalen,
1068 atmel_aes_ctr_transfer);
1069
1070 return atmel_aes_cpu_start(dd, src, dst, datalen,
1071 atmel_aes_ctr_transfer);
1072}
1073
1074static int atmel_aes_ctr_start(struct atmel_aes_dev *dd)
1075{
1076 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
1077 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
1078 struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
1079 int err;
1080
1081 atmel_aes_set_mode(dd, rctx);
1082
1083 err = atmel_aes_hw_init(dd);
1084 if (err)
1085 return atmel_aes_complete(dd, err);
1086
1087 memcpy(ctx->iv, req->info, AES_BLOCK_SIZE);
1088 ctx->offset = 0;
1089 dd->total = 0;
1090 return atmel_aes_ctr_transfer(dd);
1091}
1092
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001093static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
1094{
Romain Izard91308012017-10-31 16:25:23 +01001095 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
1096 struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001097 struct atmel_aes_reqctx *rctx;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001098 struct atmel_aes_dev *dd;
1099
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001100 switch (mode & AES_FLAGS_OPMODE_MASK) {
1101 case AES_FLAGS_CFB8:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001102 ctx->block_size = CFB8_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001103 break;
1104
1105 case AES_FLAGS_CFB16:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001106 ctx->block_size = CFB16_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001107 break;
1108
1109 case AES_FLAGS_CFB32:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001110 ctx->block_size = CFB32_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001111 break;
1112
1113 case AES_FLAGS_CFB64:
Leilei Zhao9f849512014-04-22 15:23:24 +08001114 ctx->block_size = CFB64_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001115 break;
1116
1117 default:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001118 ctx->block_size = AES_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001119 break;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001120 }
Romain Izard91308012017-10-31 16:25:23 +01001121 ctx->is_aead = false;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001122
1123 dd = atmel_aes_find_dev(ctx);
1124 if (!dd)
1125 return -ENODEV;
1126
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001127 rctx = ablkcipher_request_ctx(req);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001128 rctx->mode = mode;
1129
Romain Izard91308012017-10-31 16:25:23 +01001130 if (!(mode & AES_FLAGS_ENCRYPT) && (req->src == req->dst)) {
1131 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
1132
1133 scatterwalk_map_and_copy(rctx->lastc, req->src,
1134 (req->nbytes - ivsize), ivsize, 0);
1135 }
1136
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001137 return atmel_aes_handle_queue(dd, &req->base);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001138}
1139
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001140static int atmel_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
1141 unsigned int keylen)
1142{
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001143 struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001144
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001145 if (keylen != AES_KEYSIZE_128 &&
1146 keylen != AES_KEYSIZE_192 &&
1147 keylen != AES_KEYSIZE_256) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001148 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1149 return -EINVAL;
1150 }
1151
1152 memcpy(ctx->key, key, keylen);
1153 ctx->keylen = keylen;
1154
1155 return 0;
1156}
1157
1158static int atmel_aes_ecb_encrypt(struct ablkcipher_request *req)
1159{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001160 return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001161}
1162
1163static int atmel_aes_ecb_decrypt(struct ablkcipher_request *req)
1164{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001165 return atmel_aes_crypt(req, AES_FLAGS_ECB);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001166}
1167
1168static int atmel_aes_cbc_encrypt(struct ablkcipher_request *req)
1169{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001170 return atmel_aes_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001171}
1172
1173static int atmel_aes_cbc_decrypt(struct ablkcipher_request *req)
1174{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001175 return atmel_aes_crypt(req, AES_FLAGS_CBC);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001176}
1177
1178static int atmel_aes_ofb_encrypt(struct ablkcipher_request *req)
1179{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001180 return atmel_aes_crypt(req, AES_FLAGS_OFB | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001181}
1182
1183static int atmel_aes_ofb_decrypt(struct ablkcipher_request *req)
1184{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001185 return atmel_aes_crypt(req, AES_FLAGS_OFB);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001186}
1187
1188static int atmel_aes_cfb_encrypt(struct ablkcipher_request *req)
1189{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001190 return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001191}
1192
1193static int atmel_aes_cfb_decrypt(struct ablkcipher_request *req)
1194{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001195 return atmel_aes_crypt(req, AES_FLAGS_CFB128);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001196}
1197
1198static int atmel_aes_cfb64_encrypt(struct ablkcipher_request *req)
1199{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001200 return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001201}
1202
1203static int atmel_aes_cfb64_decrypt(struct ablkcipher_request *req)
1204{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001205 return atmel_aes_crypt(req, AES_FLAGS_CFB64);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001206}
1207
1208static int atmel_aes_cfb32_encrypt(struct ablkcipher_request *req)
1209{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001210 return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001211}
1212
1213static int atmel_aes_cfb32_decrypt(struct ablkcipher_request *req)
1214{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001215 return atmel_aes_crypt(req, AES_FLAGS_CFB32);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001216}
1217
1218static int atmel_aes_cfb16_encrypt(struct ablkcipher_request *req)
1219{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001220 return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001221}
1222
1223static int atmel_aes_cfb16_decrypt(struct ablkcipher_request *req)
1224{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001225 return atmel_aes_crypt(req, AES_FLAGS_CFB16);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001226}
1227
1228static int atmel_aes_cfb8_encrypt(struct ablkcipher_request *req)
1229{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001230 return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001231}
1232
1233static int atmel_aes_cfb8_decrypt(struct ablkcipher_request *req)
1234{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001235 return atmel_aes_crypt(req, AES_FLAGS_CFB8);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001236}
1237
1238static int atmel_aes_ctr_encrypt(struct ablkcipher_request *req)
1239{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001240 return atmel_aes_crypt(req, AES_FLAGS_CTR | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001241}
1242
1243static int atmel_aes_ctr_decrypt(struct ablkcipher_request *req)
1244{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001245 return atmel_aes_crypt(req, AES_FLAGS_CTR);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001246}
1247
1248static int atmel_aes_cra_init(struct crypto_tfm *tfm)
1249{
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001250 struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
1251
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001252 tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001253 ctx->base.start = atmel_aes_start;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001254
1255 return 0;
1256}
1257
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01001258static int atmel_aes_ctr_cra_init(struct crypto_tfm *tfm)
1259{
1260 struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
1261
1262 tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
1263 ctx->base.start = atmel_aes_ctr_start;
1264
1265 return 0;
1266}
1267
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001268static void atmel_aes_cra_exit(struct crypto_tfm *tfm)
1269{
1270}
1271
1272static struct crypto_alg aes_algs[] = {
1273{
1274 .cra_name = "ecb(aes)",
1275 .cra_driver_name = "atmel-ecb-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001276 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001277 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1278 .cra_blocksize = AES_BLOCK_SIZE,
1279 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001280 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001281 .cra_type = &crypto_ablkcipher_type,
1282 .cra_module = THIS_MODULE,
1283 .cra_init = atmel_aes_cra_init,
1284 .cra_exit = atmel_aes_cra_exit,
1285 .cra_u.ablkcipher = {
1286 .min_keysize = AES_MIN_KEY_SIZE,
1287 .max_keysize = AES_MAX_KEY_SIZE,
1288 .setkey = atmel_aes_setkey,
1289 .encrypt = atmel_aes_ecb_encrypt,
1290 .decrypt = atmel_aes_ecb_decrypt,
1291 }
1292},
1293{
1294 .cra_name = "cbc(aes)",
1295 .cra_driver_name = "atmel-cbc-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001296 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001297 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1298 .cra_blocksize = AES_BLOCK_SIZE,
1299 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001300 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001301 .cra_type = &crypto_ablkcipher_type,
1302 .cra_module = THIS_MODULE,
1303 .cra_init = atmel_aes_cra_init,
1304 .cra_exit = atmel_aes_cra_exit,
1305 .cra_u.ablkcipher = {
1306 .min_keysize = AES_MIN_KEY_SIZE,
1307 .max_keysize = AES_MAX_KEY_SIZE,
1308 .ivsize = AES_BLOCK_SIZE,
1309 .setkey = atmel_aes_setkey,
1310 .encrypt = atmel_aes_cbc_encrypt,
1311 .decrypt = atmel_aes_cbc_decrypt,
1312 }
1313},
1314{
1315 .cra_name = "ofb(aes)",
1316 .cra_driver_name = "atmel-ofb-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001317 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001318 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1319 .cra_blocksize = AES_BLOCK_SIZE,
1320 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001321 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001322 .cra_type = &crypto_ablkcipher_type,
1323 .cra_module = THIS_MODULE,
1324 .cra_init = atmel_aes_cra_init,
1325 .cra_exit = atmel_aes_cra_exit,
1326 .cra_u.ablkcipher = {
1327 .min_keysize = AES_MIN_KEY_SIZE,
1328 .max_keysize = AES_MAX_KEY_SIZE,
1329 .ivsize = AES_BLOCK_SIZE,
1330 .setkey = atmel_aes_setkey,
1331 .encrypt = atmel_aes_ofb_encrypt,
1332 .decrypt = atmel_aes_ofb_decrypt,
1333 }
1334},
1335{
1336 .cra_name = "cfb(aes)",
1337 .cra_driver_name = "atmel-cfb-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001338 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001339 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1340 .cra_blocksize = AES_BLOCK_SIZE,
1341 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001342 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001343 .cra_type = &crypto_ablkcipher_type,
1344 .cra_module = THIS_MODULE,
1345 .cra_init = atmel_aes_cra_init,
1346 .cra_exit = atmel_aes_cra_exit,
1347 .cra_u.ablkcipher = {
1348 .min_keysize = AES_MIN_KEY_SIZE,
1349 .max_keysize = AES_MAX_KEY_SIZE,
1350 .ivsize = AES_BLOCK_SIZE,
1351 .setkey = atmel_aes_setkey,
1352 .encrypt = atmel_aes_cfb_encrypt,
1353 .decrypt = atmel_aes_cfb_decrypt,
1354 }
1355},
1356{
1357 .cra_name = "cfb32(aes)",
1358 .cra_driver_name = "atmel-cfb32-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001359 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001360 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1361 .cra_blocksize = CFB32_BLOCK_SIZE,
1362 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001363 .cra_alignmask = 0x3,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001364 .cra_type = &crypto_ablkcipher_type,
1365 .cra_module = THIS_MODULE,
1366 .cra_init = atmel_aes_cra_init,
1367 .cra_exit = atmel_aes_cra_exit,
1368 .cra_u.ablkcipher = {
1369 .min_keysize = AES_MIN_KEY_SIZE,
1370 .max_keysize = AES_MAX_KEY_SIZE,
1371 .ivsize = AES_BLOCK_SIZE,
1372 .setkey = atmel_aes_setkey,
1373 .encrypt = atmel_aes_cfb32_encrypt,
1374 .decrypt = atmel_aes_cfb32_decrypt,
1375 }
1376},
1377{
1378 .cra_name = "cfb16(aes)",
1379 .cra_driver_name = "atmel-cfb16-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001380 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001381 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1382 .cra_blocksize = CFB16_BLOCK_SIZE,
1383 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001384 .cra_alignmask = 0x1,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001385 .cra_type = &crypto_ablkcipher_type,
1386 .cra_module = THIS_MODULE,
1387 .cra_init = atmel_aes_cra_init,
1388 .cra_exit = atmel_aes_cra_exit,
1389 .cra_u.ablkcipher = {
1390 .min_keysize = AES_MIN_KEY_SIZE,
1391 .max_keysize = AES_MAX_KEY_SIZE,
1392 .ivsize = AES_BLOCK_SIZE,
1393 .setkey = atmel_aes_setkey,
1394 .encrypt = atmel_aes_cfb16_encrypt,
1395 .decrypt = atmel_aes_cfb16_decrypt,
1396 }
1397},
1398{
1399 .cra_name = "cfb8(aes)",
1400 .cra_driver_name = "atmel-cfb8-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001401 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001402 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
Leilei Zhaoe5d8c962014-04-22 15:23:23 +08001403 .cra_blocksize = CFB8_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001404 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
1405 .cra_alignmask = 0x0,
1406 .cra_type = &crypto_ablkcipher_type,
1407 .cra_module = THIS_MODULE,
1408 .cra_init = atmel_aes_cra_init,
1409 .cra_exit = atmel_aes_cra_exit,
1410 .cra_u.ablkcipher = {
1411 .min_keysize = AES_MIN_KEY_SIZE,
1412 .max_keysize = AES_MAX_KEY_SIZE,
1413 .ivsize = AES_BLOCK_SIZE,
1414 .setkey = atmel_aes_setkey,
1415 .encrypt = atmel_aes_cfb8_encrypt,
1416 .decrypt = atmel_aes_cfb8_decrypt,
1417 }
1418},
1419{
1420 .cra_name = "ctr(aes)",
1421 .cra_driver_name = "atmel-ctr-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001422 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001423 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
Cyrille Pitchenda7b8502015-12-17 18:13:04 +01001424 .cra_blocksize = 1,
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01001425 .cra_ctxsize = sizeof(struct atmel_aes_ctr_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001426 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001427 .cra_type = &crypto_ablkcipher_type,
1428 .cra_module = THIS_MODULE,
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01001429 .cra_init = atmel_aes_ctr_cra_init,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001430 .cra_exit = atmel_aes_cra_exit,
1431 .cra_u.ablkcipher = {
1432 .min_keysize = AES_MIN_KEY_SIZE,
1433 .max_keysize = AES_MAX_KEY_SIZE,
1434 .ivsize = AES_BLOCK_SIZE,
1435 .setkey = atmel_aes_setkey,
1436 .encrypt = atmel_aes_ctr_encrypt,
1437 .decrypt = atmel_aes_ctr_decrypt,
1438 }
1439},
1440};
1441
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001442static struct crypto_alg aes_cfb64_alg = {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001443 .cra_name = "cfb64(aes)",
1444 .cra_driver_name = "atmel-cfb64-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001445 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001446 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1447 .cra_blocksize = CFB64_BLOCK_SIZE,
1448 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001449 .cra_alignmask = 0x7,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001450 .cra_type = &crypto_ablkcipher_type,
1451 .cra_module = THIS_MODULE,
1452 .cra_init = atmel_aes_cra_init,
1453 .cra_exit = atmel_aes_cra_exit,
1454 .cra_u.ablkcipher = {
1455 .min_keysize = AES_MIN_KEY_SIZE,
1456 .max_keysize = AES_MAX_KEY_SIZE,
1457 .ivsize = AES_BLOCK_SIZE,
1458 .setkey = atmel_aes_setkey,
1459 .encrypt = atmel_aes_cfb64_encrypt,
1460 .decrypt = atmel_aes_cfb64_decrypt,
1461 }
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001462};
1463
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01001464
Cyrille Pitchend4419542015-12-17 18:13:07 +01001465/* gcm aead functions */
1466
1467static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
1468 const u32 *data, size_t datalen,
1469 const u32 *ghash_in, u32 *ghash_out,
1470 atmel_aes_fn_t resume);
1471static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd);
1472static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd);
1473
1474static int atmel_aes_gcm_start(struct atmel_aes_dev *dd);
1475static int atmel_aes_gcm_process(struct atmel_aes_dev *dd);
1476static int atmel_aes_gcm_length(struct atmel_aes_dev *dd);
1477static int atmel_aes_gcm_data(struct atmel_aes_dev *dd);
1478static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd);
1479static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd);
1480static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd);
1481
1482static inline struct atmel_aes_gcm_ctx *
1483atmel_aes_gcm_ctx_cast(struct atmel_aes_base_ctx *ctx)
1484{
1485 return container_of(ctx, struct atmel_aes_gcm_ctx, base);
1486}
1487
1488static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
1489 const u32 *data, size_t datalen,
1490 const u32 *ghash_in, u32 *ghash_out,
1491 atmel_aes_fn_t resume)
1492{
1493 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1494
1495 dd->data = (u32 *)data;
1496 dd->datalen = datalen;
1497 ctx->ghash_in = ghash_in;
1498 ctx->ghash_out = ghash_out;
1499 ctx->ghash_resume = resume;
1500
1501 atmel_aes_write_ctrl(dd, false, NULL);
1502 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_ghash_init);
1503}
1504
1505static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd)
1506{
1507 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1508
1509 /* Set the data length. */
1510 atmel_aes_write(dd, AES_AADLENR, dd->total);
1511 atmel_aes_write(dd, AES_CLENR, 0);
1512
1513 /* If needed, overwrite the GCM Intermediate Hash Word Registers */
1514 if (ctx->ghash_in)
1515 atmel_aes_write_block(dd, AES_GHASHR(0), ctx->ghash_in);
1516
1517 return atmel_aes_gcm_ghash_finalize(dd);
1518}
1519
1520static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd)
1521{
1522 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1523 u32 isr;
1524
1525 /* Write data into the Input Data Registers. */
1526 while (dd->datalen > 0) {
1527 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
1528 dd->data += 4;
1529 dd->datalen -= AES_BLOCK_SIZE;
1530
1531 isr = atmel_aes_read(dd, AES_ISR);
1532 if (!(isr & AES_INT_DATARDY)) {
1533 dd->resume = atmel_aes_gcm_ghash_finalize;
1534 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
1535 return -EINPROGRESS;
1536 }
1537 }
1538
1539 /* Read the computed hash from GHASHRx. */
1540 atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash_out);
1541
1542 return ctx->ghash_resume(dd);
1543}
1544
1545
1546static int atmel_aes_gcm_start(struct atmel_aes_dev *dd)
1547{
1548 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1549 struct aead_request *req = aead_request_cast(dd->areq);
1550 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1551 struct atmel_aes_reqctx *rctx = aead_request_ctx(req);
1552 size_t ivsize = crypto_aead_ivsize(tfm);
1553 size_t datalen, padlen;
1554 const void *iv = req->iv;
1555 u8 *data = dd->buf;
1556 int err;
1557
1558 atmel_aes_set_mode(dd, rctx);
1559
1560 err = atmel_aes_hw_init(dd);
1561 if (err)
1562 return atmel_aes_complete(dd, err);
1563
Corentin LABBE219d51c2017-08-22 10:08:12 +02001564 if (likely(ivsize == GCM_AES_IV_SIZE)) {
Cyrille Pitchend4419542015-12-17 18:13:07 +01001565 memcpy(ctx->j0, iv, ivsize);
1566 ctx->j0[3] = cpu_to_be32(1);
1567 return atmel_aes_gcm_process(dd);
1568 }
1569
1570 padlen = atmel_aes_padlen(ivsize, AES_BLOCK_SIZE);
1571 datalen = ivsize + padlen + AES_BLOCK_SIZE;
1572 if (datalen > dd->buflen)
1573 return atmel_aes_complete(dd, -EINVAL);
1574
1575 memcpy(data, iv, ivsize);
1576 memset(data + ivsize, 0, padlen + sizeof(u64));
1577 ((u64 *)(data + datalen))[-1] = cpu_to_be64(ivsize * 8);
1578
1579 return atmel_aes_gcm_ghash(dd, (const u32 *)data, datalen,
1580 NULL, ctx->j0, atmel_aes_gcm_process);
1581}
1582
1583static int atmel_aes_gcm_process(struct atmel_aes_dev *dd)
1584{
1585 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1586 struct aead_request *req = aead_request_cast(dd->areq);
1587 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1588 bool enc = atmel_aes_is_encrypt(dd);
1589 u32 authsize;
1590
1591 /* Compute text length. */
1592 authsize = crypto_aead_authsize(tfm);
1593 ctx->textlen = req->cryptlen - (enc ? 0 : authsize);
1594
1595 /*
1596 * According to tcrypt test suite, the GCM Automatic Tag Generation
1597 * fails when both the message and its associated data are empty.
1598 */
1599 if (likely(req->assoclen != 0 || ctx->textlen != 0))
1600 dd->flags |= AES_FLAGS_GTAGEN;
1601
1602 atmel_aes_write_ctrl(dd, false, NULL);
1603 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_length);
1604}
1605
1606static int atmel_aes_gcm_length(struct atmel_aes_dev *dd)
1607{
1608 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1609 struct aead_request *req = aead_request_cast(dd->areq);
1610 u32 j0_lsw, *j0 = ctx->j0;
1611 size_t padlen;
1612
1613 /* Write incr32(J0) into IV. */
1614 j0_lsw = j0[3];
1615 j0[3] = cpu_to_be32(be32_to_cpu(j0[3]) + 1);
1616 atmel_aes_write_block(dd, AES_IVR(0), j0);
1617 j0[3] = j0_lsw;
1618
1619 /* Set aad and text lengths. */
1620 atmel_aes_write(dd, AES_AADLENR, req->assoclen);
1621 atmel_aes_write(dd, AES_CLENR, ctx->textlen);
1622
1623 /* Check whether AAD are present. */
1624 if (unlikely(req->assoclen == 0)) {
1625 dd->datalen = 0;
1626 return atmel_aes_gcm_data(dd);
1627 }
1628
1629 /* Copy assoc data and add padding. */
1630 padlen = atmel_aes_padlen(req->assoclen, AES_BLOCK_SIZE);
1631 if (unlikely(req->assoclen + padlen > dd->buflen))
1632 return atmel_aes_complete(dd, -EINVAL);
1633 sg_copy_to_buffer(req->src, sg_nents(req->src), dd->buf, req->assoclen);
1634
1635 /* Write assoc data into the Input Data register. */
1636 dd->data = (u32 *)dd->buf;
1637 dd->datalen = req->assoclen + padlen;
1638 return atmel_aes_gcm_data(dd);
1639}
1640
1641static int atmel_aes_gcm_data(struct atmel_aes_dev *dd)
1642{
1643 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1644 struct aead_request *req = aead_request_cast(dd->areq);
1645 bool use_dma = (ctx->textlen >= ATMEL_AES_DMA_THRESHOLD);
1646 struct scatterlist *src, *dst;
1647 u32 isr, mr;
1648
1649 /* Write AAD first. */
1650 while (dd->datalen > 0) {
1651 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
1652 dd->data += 4;
1653 dd->datalen -= AES_BLOCK_SIZE;
1654
1655 isr = atmel_aes_read(dd, AES_ISR);
1656 if (!(isr & AES_INT_DATARDY)) {
1657 dd->resume = atmel_aes_gcm_data;
1658 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
1659 return -EINPROGRESS;
1660 }
1661 }
1662
1663 /* GMAC only. */
1664 if (unlikely(ctx->textlen == 0))
1665 return atmel_aes_gcm_tag_init(dd);
1666
1667 /* Prepare src and dst scatter lists to transfer cipher/plain texts */
1668 src = scatterwalk_ffwd(ctx->src, req->src, req->assoclen);
1669 dst = ((req->src == req->dst) ? src :
1670 scatterwalk_ffwd(ctx->dst, req->dst, req->assoclen));
1671
1672 if (use_dma) {
1673 /* Update the Mode Register for DMA transfers. */
1674 mr = atmel_aes_read(dd, AES_MR);
1675 mr &= ~(AES_MR_SMOD_MASK | AES_MR_DUALBUFF);
1676 mr |= AES_MR_SMOD_IDATAR0;
1677 if (dd->caps.has_dualbuff)
1678 mr |= AES_MR_DUALBUFF;
1679 atmel_aes_write(dd, AES_MR, mr);
1680
1681 return atmel_aes_dma_start(dd, src, dst, ctx->textlen,
1682 atmel_aes_gcm_tag_init);
1683 }
1684
1685 return atmel_aes_cpu_start(dd, src, dst, ctx->textlen,
1686 atmel_aes_gcm_tag_init);
1687}
1688
1689static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd)
1690{
1691 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1692 struct aead_request *req = aead_request_cast(dd->areq);
1693 u64 *data = dd->buf;
1694
1695 if (likely(dd->flags & AES_FLAGS_GTAGEN)) {
1696 if (!(atmel_aes_read(dd, AES_ISR) & AES_INT_TAGRDY)) {
1697 dd->resume = atmel_aes_gcm_tag_init;
1698 atmel_aes_write(dd, AES_IER, AES_INT_TAGRDY);
1699 return -EINPROGRESS;
1700 }
1701
1702 return atmel_aes_gcm_finalize(dd);
1703 }
1704
1705 /* Read the GCM Intermediate Hash Word Registers. */
1706 atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash);
1707
1708 data[0] = cpu_to_be64(req->assoclen * 8);
1709 data[1] = cpu_to_be64(ctx->textlen * 8);
1710
1711 return atmel_aes_gcm_ghash(dd, (const u32 *)data, AES_BLOCK_SIZE,
1712 ctx->ghash, ctx->ghash, atmel_aes_gcm_tag);
1713}
1714
1715static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd)
1716{
1717 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1718 unsigned long flags;
1719
1720 /*
1721 * Change mode to CTR to complete the tag generation.
1722 * Use J0 as Initialization Vector.
1723 */
1724 flags = dd->flags;
1725 dd->flags &= ~(AES_FLAGS_OPMODE_MASK | AES_FLAGS_GTAGEN);
1726 dd->flags |= AES_FLAGS_CTR;
1727 atmel_aes_write_ctrl(dd, false, ctx->j0);
1728 dd->flags = flags;
1729
1730 atmel_aes_write_block(dd, AES_IDATAR(0), ctx->ghash);
1731 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_finalize);
1732}
1733
1734static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd)
1735{
1736 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1737 struct aead_request *req = aead_request_cast(dd->areq);
1738 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1739 bool enc = atmel_aes_is_encrypt(dd);
1740 u32 offset, authsize, itag[4], *otag = ctx->tag;
1741 int err;
1742
1743 /* Read the computed tag. */
1744 if (likely(dd->flags & AES_FLAGS_GTAGEN))
1745 atmel_aes_read_block(dd, AES_TAGR(0), ctx->tag);
1746 else
1747 atmel_aes_read_block(dd, AES_ODATAR(0), ctx->tag);
1748
1749 offset = req->assoclen + ctx->textlen;
1750 authsize = crypto_aead_authsize(tfm);
1751 if (enc) {
1752 scatterwalk_map_and_copy(otag, req->dst, offset, authsize, 1);
1753 err = 0;
1754 } else {
1755 scatterwalk_map_and_copy(itag, req->src, offset, authsize, 0);
1756 err = crypto_memneq(itag, otag, authsize) ? -EBADMSG : 0;
1757 }
1758
1759 return atmel_aes_complete(dd, err);
1760}
1761
1762static int atmel_aes_gcm_crypt(struct aead_request *req,
1763 unsigned long mode)
1764{
1765 struct atmel_aes_base_ctx *ctx;
1766 struct atmel_aes_reqctx *rctx;
1767 struct atmel_aes_dev *dd;
1768
1769 ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
1770 ctx->block_size = AES_BLOCK_SIZE;
Romain Izard91308012017-10-31 16:25:23 +01001771 ctx->is_aead = true;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001772
1773 dd = atmel_aes_find_dev(ctx);
1774 if (!dd)
1775 return -ENODEV;
1776
1777 rctx = aead_request_ctx(req);
1778 rctx->mode = AES_FLAGS_GCM | mode;
1779
1780 return atmel_aes_handle_queue(dd, &req->base);
1781}
1782
1783static int atmel_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
1784 unsigned int keylen)
1785{
1786 struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
1787
1788 if (keylen != AES_KEYSIZE_256 &&
1789 keylen != AES_KEYSIZE_192 &&
1790 keylen != AES_KEYSIZE_128) {
1791 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1792 return -EINVAL;
1793 }
1794
1795 memcpy(ctx->key, key, keylen);
1796 ctx->keylen = keylen;
1797
1798 return 0;
1799}
1800
1801static int atmel_aes_gcm_setauthsize(struct crypto_aead *tfm,
1802 unsigned int authsize)
1803{
1804 /* Same as crypto_gcm_authsize() from crypto/gcm.c */
1805 switch (authsize) {
1806 case 4:
1807 case 8:
1808 case 12:
1809 case 13:
1810 case 14:
1811 case 15:
1812 case 16:
1813 break;
1814 default:
1815 return -EINVAL;
1816 }
1817
1818 return 0;
1819}
1820
1821static int atmel_aes_gcm_encrypt(struct aead_request *req)
1822{
1823 return atmel_aes_gcm_crypt(req, AES_FLAGS_ENCRYPT);
1824}
1825
1826static int atmel_aes_gcm_decrypt(struct aead_request *req)
1827{
1828 return atmel_aes_gcm_crypt(req, 0);
1829}
1830
1831static int atmel_aes_gcm_init(struct crypto_aead *tfm)
1832{
1833 struct atmel_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm);
1834
1835 crypto_aead_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
1836 ctx->base.start = atmel_aes_gcm_start;
1837
1838 return 0;
1839}
1840
1841static void atmel_aes_gcm_exit(struct crypto_aead *tfm)
1842{
1843
1844}
1845
1846static struct aead_alg aes_gcm_alg = {
1847 .setkey = atmel_aes_gcm_setkey,
1848 .setauthsize = atmel_aes_gcm_setauthsize,
1849 .encrypt = atmel_aes_gcm_encrypt,
1850 .decrypt = atmel_aes_gcm_decrypt,
1851 .init = atmel_aes_gcm_init,
1852 .exit = atmel_aes_gcm_exit,
Corentin LABBE219d51c2017-08-22 10:08:12 +02001853 .ivsize = GCM_AES_IV_SIZE,
Cyrille Pitchend4419542015-12-17 18:13:07 +01001854 .maxauthsize = AES_BLOCK_SIZE,
1855
1856 .base = {
1857 .cra_name = "gcm(aes)",
1858 .cra_driver_name = "atmel-gcm-aes",
1859 .cra_priority = ATMEL_AES_PRIORITY,
1860 .cra_flags = CRYPTO_ALG_ASYNC,
1861 .cra_blocksize = 1,
1862 .cra_ctxsize = sizeof(struct atmel_aes_gcm_ctx),
1863 .cra_alignmask = 0xf,
1864 .cra_module = THIS_MODULE,
1865 },
1866};
1867
1868
Cyrille Pitchend52db512016-10-03 14:33:16 +02001869/* xts functions */
1870
1871static inline struct atmel_aes_xts_ctx *
1872atmel_aes_xts_ctx_cast(struct atmel_aes_base_ctx *ctx)
1873{
1874 return container_of(ctx, struct atmel_aes_xts_ctx, base);
1875}
1876
1877static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd);
1878
1879static int atmel_aes_xts_start(struct atmel_aes_dev *dd)
1880{
1881 struct atmel_aes_xts_ctx *ctx = atmel_aes_xts_ctx_cast(dd->ctx);
1882 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
1883 struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
1884 unsigned long flags;
1885 int err;
1886
1887 atmel_aes_set_mode(dd, rctx);
1888
1889 err = atmel_aes_hw_init(dd);
1890 if (err)
1891 return atmel_aes_complete(dd, err);
1892
1893 /* Compute the tweak value from req->info with ecb(aes). */
1894 flags = dd->flags;
1895 dd->flags &= ~AES_FLAGS_MODE_MASK;
1896 dd->flags |= (AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
1897 atmel_aes_write_ctrl_key(dd, false, NULL,
1898 ctx->key2, ctx->base.keylen);
1899 dd->flags = flags;
1900
1901 atmel_aes_write_block(dd, AES_IDATAR(0), req->info);
1902 return atmel_aes_wait_for_data_ready(dd, atmel_aes_xts_process_data);
1903}
1904
1905static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd)
1906{
1907 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
1908 bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD);
1909 u32 tweak[AES_BLOCK_SIZE / sizeof(u32)];
1910 static const u32 one[AES_BLOCK_SIZE / sizeof(u32)] = {cpu_to_le32(1), };
1911 u8 *tweak_bytes = (u8 *)tweak;
1912 int i;
1913
1914 /* Read the computed ciphered tweak value. */
1915 atmel_aes_read_block(dd, AES_ODATAR(0), tweak);
1916 /*
1917 * Hardware quirk:
1918 * the order of the ciphered tweak bytes need to be reversed before
1919 * writing them into the ODATARx registers.
1920 */
1921 for (i = 0; i < AES_BLOCK_SIZE/2; ++i) {
1922 u8 tmp = tweak_bytes[AES_BLOCK_SIZE - 1 - i];
1923
1924 tweak_bytes[AES_BLOCK_SIZE - 1 - i] = tweak_bytes[i];
1925 tweak_bytes[i] = tmp;
1926 }
1927
1928 /* Process the data. */
1929 atmel_aes_write_ctrl(dd, use_dma, NULL);
1930 atmel_aes_write_block(dd, AES_TWR(0), tweak);
1931 atmel_aes_write_block(dd, AES_ALPHAR(0), one);
1932 if (use_dma)
1933 return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes,
1934 atmel_aes_transfer_complete);
1935
1936 return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes,
1937 atmel_aes_transfer_complete);
1938}
1939
1940static int atmel_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
1941 unsigned int keylen)
1942{
1943 struct atmel_aes_xts_ctx *ctx = crypto_ablkcipher_ctx(tfm);
1944 int err;
1945
1946 err = xts_check_key(crypto_ablkcipher_tfm(tfm), key, keylen);
1947 if (err)
1948 return err;
1949
1950 memcpy(ctx->base.key, key, keylen/2);
1951 memcpy(ctx->key2, key + keylen/2, keylen/2);
1952 ctx->base.keylen = keylen/2;
1953
1954 return 0;
1955}
1956
1957static int atmel_aes_xts_encrypt(struct ablkcipher_request *req)
1958{
1959 return atmel_aes_crypt(req, AES_FLAGS_XTS | AES_FLAGS_ENCRYPT);
1960}
1961
1962static int atmel_aes_xts_decrypt(struct ablkcipher_request *req)
1963{
1964 return atmel_aes_crypt(req, AES_FLAGS_XTS);
1965}
1966
1967static int atmel_aes_xts_cra_init(struct crypto_tfm *tfm)
1968{
1969 struct atmel_aes_xts_ctx *ctx = crypto_tfm_ctx(tfm);
1970
1971 tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
1972 ctx->base.start = atmel_aes_xts_start;
1973
1974 return 0;
1975}
1976
1977static struct crypto_alg aes_xts_alg = {
1978 .cra_name = "xts(aes)",
1979 .cra_driver_name = "atmel-xts-aes",
1980 .cra_priority = ATMEL_AES_PRIORITY,
1981 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1982 .cra_blocksize = AES_BLOCK_SIZE,
1983 .cra_ctxsize = sizeof(struct atmel_aes_xts_ctx),
1984 .cra_alignmask = 0xf,
1985 .cra_type = &crypto_ablkcipher_type,
1986 .cra_module = THIS_MODULE,
1987 .cra_init = atmel_aes_xts_cra_init,
1988 .cra_exit = atmel_aes_cra_exit,
1989 .cra_u.ablkcipher = {
1990 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1991 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1992 .ivsize = AES_BLOCK_SIZE,
1993 .setkey = atmel_aes_xts_setkey,
1994 .encrypt = atmel_aes_xts_encrypt,
1995 .decrypt = atmel_aes_xts_decrypt,
1996 }
1997};
1998
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01001999#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2000/* authenc aead functions */
2001
2002static int atmel_aes_authenc_start(struct atmel_aes_dev *dd);
2003static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
2004 bool is_async);
2005static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
2006 bool is_async);
2007static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd);
2008static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
2009 bool is_async);
2010
2011static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err)
2012{
2013 struct aead_request *req = aead_request_cast(dd->areq);
2014 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2015
2016 if (err && (dd->flags & AES_FLAGS_OWN_SHA))
2017 atmel_sha_authenc_abort(&rctx->auth_req);
2018 dd->flags &= ~AES_FLAGS_OWN_SHA;
2019}
2020
2021static int atmel_aes_authenc_start(struct atmel_aes_dev *dd)
2022{
2023 struct aead_request *req = aead_request_cast(dd->areq);
2024 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2025 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2026 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2027 int err;
2028
2029 atmel_aes_set_mode(dd, &rctx->base);
2030
2031 err = atmel_aes_hw_init(dd);
2032 if (err)
2033 return atmel_aes_complete(dd, err);
2034
2035 return atmel_sha_authenc_schedule(&rctx->auth_req, ctx->auth,
2036 atmel_aes_authenc_init, dd);
2037}
2038
2039static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
2040 bool is_async)
2041{
2042 struct aead_request *req = aead_request_cast(dd->areq);
2043 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2044
2045 if (is_async)
2046 dd->is_async = true;
2047 if (err)
2048 return atmel_aes_complete(dd, err);
2049
2050 /* If here, we've got the ownership of the SHA device. */
2051 dd->flags |= AES_FLAGS_OWN_SHA;
2052
2053 /* Configure the SHA device. */
2054 return atmel_sha_authenc_init(&rctx->auth_req,
2055 req->src, req->assoclen,
2056 rctx->textlen,
2057 atmel_aes_authenc_transfer, dd);
2058}
2059
2060static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
2061 bool is_async)
2062{
2063 struct aead_request *req = aead_request_cast(dd->areq);
2064 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2065 bool enc = atmel_aes_is_encrypt(dd);
2066 struct scatterlist *src, *dst;
2067 u32 iv[AES_BLOCK_SIZE / sizeof(u32)];
2068 u32 emr;
2069
2070 if (is_async)
2071 dd->is_async = true;
2072 if (err)
2073 return atmel_aes_complete(dd, err);
2074
2075 /* Prepare src and dst scatter-lists to transfer cipher/plain texts. */
2076 src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
2077 dst = src;
2078
2079 if (req->src != req->dst)
2080 dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
2081
2082 /* Configure the AES device. */
2083 memcpy(iv, req->iv, sizeof(iv));
2084
2085 /*
2086 * Here we always set the 2nd parameter of atmel_aes_write_ctrl() to
2087 * 'true' even if the data transfer is actually performed by the CPU (so
2088 * not by the DMA) because we must force the AES_MR_SMOD bitfield to the
2089 * value AES_MR_SMOD_IDATAR0. Indeed, both AES_MR_SMOD and SHA_MR_SMOD
2090 * must be set to *_MR_SMOD_IDATAR0.
2091 */
2092 atmel_aes_write_ctrl(dd, true, iv);
2093 emr = AES_EMR_PLIPEN;
2094 if (!enc)
2095 emr |= AES_EMR_PLIPD;
2096 atmel_aes_write(dd, AES_EMR, emr);
2097
2098 /* Transfer data. */
2099 return atmel_aes_dma_start(dd, src, dst, rctx->textlen,
2100 atmel_aes_authenc_digest);
2101}
2102
2103static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd)
2104{
2105 struct aead_request *req = aead_request_cast(dd->areq);
2106 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2107
2108 /* atmel_sha_authenc_final() releases the SHA device. */
2109 dd->flags &= ~AES_FLAGS_OWN_SHA;
2110 return atmel_sha_authenc_final(&rctx->auth_req,
2111 rctx->digest, sizeof(rctx->digest),
2112 atmel_aes_authenc_final, dd);
2113}
2114
2115static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
2116 bool is_async)
2117{
2118 struct aead_request *req = aead_request_cast(dd->areq);
2119 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2120 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2121 bool enc = atmel_aes_is_encrypt(dd);
2122 u32 idigest[SHA512_DIGEST_SIZE / sizeof(u32)], *odigest = rctx->digest;
2123 u32 offs, authsize;
2124
2125 if (is_async)
2126 dd->is_async = true;
2127 if (err)
2128 goto complete;
2129
2130 offs = req->assoclen + rctx->textlen;
2131 authsize = crypto_aead_authsize(tfm);
2132 if (enc) {
2133 scatterwalk_map_and_copy(odigest, req->dst, offs, authsize, 1);
2134 } else {
2135 scatterwalk_map_and_copy(idigest, req->src, offs, authsize, 0);
2136 if (crypto_memneq(idigest, odigest, authsize))
2137 err = -EBADMSG;
2138 }
2139
2140complete:
2141 return atmel_aes_complete(dd, err);
2142}
2143
2144static int atmel_aes_authenc_setkey(struct crypto_aead *tfm, const u8 *key,
2145 unsigned int keylen)
2146{
2147 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2148 struct crypto_authenc_keys keys;
2149 u32 flags;
2150 int err;
2151
2152 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
2153 goto badkey;
2154
2155 if (keys.enckeylen > sizeof(ctx->base.key))
2156 goto badkey;
2157
2158 /* Save auth key. */
2159 flags = crypto_aead_get_flags(tfm);
2160 err = atmel_sha_authenc_setkey(ctx->auth,
2161 keys.authkey, keys.authkeylen,
2162 &flags);
2163 crypto_aead_set_flags(tfm, flags & CRYPTO_TFM_RES_MASK);
2164 if (err) {
2165 memzero_explicit(&keys, sizeof(keys));
2166 return err;
2167 }
2168
2169 /* Save enc key. */
2170 ctx->base.keylen = keys.enckeylen;
2171 memcpy(ctx->base.key, keys.enckey, keys.enckeylen);
2172
2173 memzero_explicit(&keys, sizeof(keys));
2174 return 0;
2175
2176badkey:
2177 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
2178 memzero_explicit(&key, sizeof(keys));
2179 return -EINVAL;
2180}
2181
2182static int atmel_aes_authenc_init_tfm(struct crypto_aead *tfm,
2183 unsigned long auth_mode)
2184{
2185 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2186 unsigned int auth_reqsize = atmel_sha_authenc_get_reqsize();
2187
2188 ctx->auth = atmel_sha_authenc_spawn(auth_mode);
2189 if (IS_ERR(ctx->auth))
2190 return PTR_ERR(ctx->auth);
2191
2192 crypto_aead_set_reqsize(tfm, (sizeof(struct atmel_aes_authenc_reqctx) +
2193 auth_reqsize));
2194 ctx->base.start = atmel_aes_authenc_start;
2195
2196 return 0;
2197}
2198
2199static int atmel_aes_authenc_hmac_sha1_init_tfm(struct crypto_aead *tfm)
2200{
2201 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA1);
2202}
2203
2204static int atmel_aes_authenc_hmac_sha224_init_tfm(struct crypto_aead *tfm)
2205{
2206 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA224);
2207}
2208
2209static int atmel_aes_authenc_hmac_sha256_init_tfm(struct crypto_aead *tfm)
2210{
2211 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA256);
2212}
2213
2214static int atmel_aes_authenc_hmac_sha384_init_tfm(struct crypto_aead *tfm)
2215{
2216 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA384);
2217}
2218
2219static int atmel_aes_authenc_hmac_sha512_init_tfm(struct crypto_aead *tfm)
2220{
2221 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA512);
2222}
2223
2224static void atmel_aes_authenc_exit_tfm(struct crypto_aead *tfm)
2225{
2226 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2227
2228 atmel_sha_authenc_free(ctx->auth);
2229}
2230
2231static int atmel_aes_authenc_crypt(struct aead_request *req,
2232 unsigned long mode)
2233{
2234 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2235 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2236 struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
2237 u32 authsize = crypto_aead_authsize(tfm);
2238 bool enc = (mode & AES_FLAGS_ENCRYPT);
2239 struct atmel_aes_dev *dd;
2240
2241 /* Compute text length. */
2242 if (!enc && req->cryptlen < authsize)
2243 return -EINVAL;
2244 rctx->textlen = req->cryptlen - (enc ? 0 : authsize);
2245
2246 /*
2247 * Currently, empty messages are not supported yet:
2248 * the SHA auto-padding can be used only on non-empty messages.
2249 * Hence a special case needs to be implemented for empty message.
2250 */
2251 if (!rctx->textlen && !req->assoclen)
2252 return -EINVAL;
2253
2254 rctx->base.mode = mode;
2255 ctx->block_size = AES_BLOCK_SIZE;
Romain Izard91308012017-10-31 16:25:23 +01002256 ctx->is_aead = true;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002257
2258 dd = atmel_aes_find_dev(ctx);
2259 if (!dd)
2260 return -ENODEV;
2261
2262 return atmel_aes_handle_queue(dd, &req->base);
2263}
2264
2265static int atmel_aes_authenc_cbc_aes_encrypt(struct aead_request *req)
2266{
2267 return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
2268}
2269
2270static int atmel_aes_authenc_cbc_aes_decrypt(struct aead_request *req)
2271{
2272 return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC);
2273}
2274
2275static struct aead_alg aes_authenc_algs[] = {
2276{
2277 .setkey = atmel_aes_authenc_setkey,
2278 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2279 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2280 .init = atmel_aes_authenc_hmac_sha1_init_tfm,
2281 .exit = atmel_aes_authenc_exit_tfm,
2282 .ivsize = AES_BLOCK_SIZE,
2283 .maxauthsize = SHA1_DIGEST_SIZE,
2284
2285 .base = {
2286 .cra_name = "authenc(hmac(sha1),cbc(aes))",
2287 .cra_driver_name = "atmel-authenc-hmac-sha1-cbc-aes",
2288 .cra_priority = ATMEL_AES_PRIORITY,
2289 .cra_flags = CRYPTO_ALG_ASYNC,
2290 .cra_blocksize = AES_BLOCK_SIZE,
2291 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
2292 .cra_alignmask = 0xf,
2293 .cra_module = THIS_MODULE,
2294 },
2295},
2296{
2297 .setkey = atmel_aes_authenc_setkey,
2298 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2299 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2300 .init = atmel_aes_authenc_hmac_sha224_init_tfm,
2301 .exit = atmel_aes_authenc_exit_tfm,
2302 .ivsize = AES_BLOCK_SIZE,
2303 .maxauthsize = SHA224_DIGEST_SIZE,
2304
2305 .base = {
2306 .cra_name = "authenc(hmac(sha224),cbc(aes))",
2307 .cra_driver_name = "atmel-authenc-hmac-sha224-cbc-aes",
2308 .cra_priority = ATMEL_AES_PRIORITY,
2309 .cra_flags = CRYPTO_ALG_ASYNC,
2310 .cra_blocksize = AES_BLOCK_SIZE,
2311 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
2312 .cra_alignmask = 0xf,
2313 .cra_module = THIS_MODULE,
2314 },
2315},
2316{
2317 .setkey = atmel_aes_authenc_setkey,
2318 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2319 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2320 .init = atmel_aes_authenc_hmac_sha256_init_tfm,
2321 .exit = atmel_aes_authenc_exit_tfm,
2322 .ivsize = AES_BLOCK_SIZE,
2323 .maxauthsize = SHA256_DIGEST_SIZE,
2324
2325 .base = {
2326 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2327 .cra_driver_name = "atmel-authenc-hmac-sha256-cbc-aes",
2328 .cra_priority = ATMEL_AES_PRIORITY,
2329 .cra_flags = CRYPTO_ALG_ASYNC,
2330 .cra_blocksize = AES_BLOCK_SIZE,
2331 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
2332 .cra_alignmask = 0xf,
2333 .cra_module = THIS_MODULE,
2334 },
2335},
2336{
2337 .setkey = atmel_aes_authenc_setkey,
2338 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2339 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2340 .init = atmel_aes_authenc_hmac_sha384_init_tfm,
2341 .exit = atmel_aes_authenc_exit_tfm,
2342 .ivsize = AES_BLOCK_SIZE,
2343 .maxauthsize = SHA384_DIGEST_SIZE,
2344
2345 .base = {
2346 .cra_name = "authenc(hmac(sha384),cbc(aes))",
2347 .cra_driver_name = "atmel-authenc-hmac-sha384-cbc-aes",
2348 .cra_priority = ATMEL_AES_PRIORITY,
2349 .cra_flags = CRYPTO_ALG_ASYNC,
2350 .cra_blocksize = AES_BLOCK_SIZE,
2351 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
2352 .cra_alignmask = 0xf,
2353 .cra_module = THIS_MODULE,
2354 },
2355},
2356{
2357 .setkey = atmel_aes_authenc_setkey,
2358 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2359 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2360 .init = atmel_aes_authenc_hmac_sha512_init_tfm,
2361 .exit = atmel_aes_authenc_exit_tfm,
2362 .ivsize = AES_BLOCK_SIZE,
2363 .maxauthsize = SHA512_DIGEST_SIZE,
2364
2365 .base = {
2366 .cra_name = "authenc(hmac(sha512),cbc(aes))",
2367 .cra_driver_name = "atmel-authenc-hmac-sha512-cbc-aes",
2368 .cra_priority = ATMEL_AES_PRIORITY,
2369 .cra_flags = CRYPTO_ALG_ASYNC,
2370 .cra_blocksize = AES_BLOCK_SIZE,
2371 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
2372 .cra_alignmask = 0xf,
2373 .cra_module = THIS_MODULE,
2374 },
2375},
2376};
2377#endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
Cyrille Pitchend52db512016-10-03 14:33:16 +02002378
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002379/* Probe functions */
2380
2381static int atmel_aes_buff_init(struct atmel_aes_dev *dd)
2382{
2383 dd->buf = (void *)__get_free_pages(GFP_KERNEL, ATMEL_AES_BUFFER_ORDER);
2384 dd->buflen = ATMEL_AES_BUFFER_SIZE;
2385 dd->buflen &= ~(AES_BLOCK_SIZE - 1);
2386
2387 if (!dd->buf) {
2388 dev_err(dd->dev, "unable to alloc pages.\n");
2389 return -ENOMEM;
2390 }
2391
2392 return 0;
2393}
2394
2395static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd)
2396{
2397 free_page((unsigned long)dd->buf);
2398}
2399
2400static bool atmel_aes_filter(struct dma_chan *chan, void *slave)
2401{
2402 struct at_dma_slave *sl = slave;
2403
2404 if (sl && sl->dma_dev == chan->device->dev) {
2405 chan->private = sl;
2406 return true;
2407 } else {
2408 return false;
2409 }
2410}
2411
2412static int atmel_aes_dma_init(struct atmel_aes_dev *dd,
2413 struct crypto_platform_data *pdata)
2414{
2415 struct at_dma_slave *slave;
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002416 dma_cap_mask_t mask;
2417
2418 dma_cap_zero(mask);
2419 dma_cap_set(DMA_SLAVE, mask);
2420
2421 /* Try to grab 2 DMA channels */
2422 slave = &pdata->dma_slave->rxdata;
2423 dd->src.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
2424 slave, dd->dev, "tx");
2425 if (!dd->src.chan)
2426 goto err_dma_in;
2427
2428 slave = &pdata->dma_slave->txdata;
2429 dd->dst.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
2430 slave, dd->dev, "rx");
2431 if (!dd->dst.chan)
2432 goto err_dma_out;
2433
2434 return 0;
2435
2436err_dma_out:
2437 dma_release_channel(dd->src.chan);
2438err_dma_in:
2439 dev_warn(dd->dev, "no DMA channel available\n");
Tudor-Dan Ambarus3c887612017-10-23 18:34:39 +03002440 return -ENODEV;
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002441}
2442
2443static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd)
2444{
2445 dma_release_channel(dd->dst.chan);
2446 dma_release_channel(dd->src.chan);
2447}
2448
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002449static void atmel_aes_queue_task(unsigned long data)
2450{
2451 struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
2452
2453 atmel_aes_handle_queue(dd, NULL);
2454}
2455
2456static void atmel_aes_done_task(unsigned long data)
2457{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01002458 struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
Cyrille Pitchen10f12c12015-12-17 17:48:42 +01002459
2460 dd->is_async = true;
2461 (void)dd->resume(dd);
2462}
2463
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002464static irqreturn_t atmel_aes_irq(int irq, void *dev_id)
2465{
2466 struct atmel_aes_dev *aes_dd = dev_id;
2467 u32 reg;
2468
2469 reg = atmel_aes_read(aes_dd, AES_ISR);
2470 if (reg & atmel_aes_read(aes_dd, AES_IMR)) {
2471 atmel_aes_write(aes_dd, AES_IDR, reg);
2472 if (AES_FLAGS_BUSY & aes_dd->flags)
2473 tasklet_schedule(&aes_dd->done_task);
2474 else
2475 dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n");
2476 return IRQ_HANDLED;
2477 }
2478
2479 return IRQ_NONE;
2480}
2481
2482static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd)
2483{
2484 int i;
2485
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002486#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2487 if (dd->caps.has_authenc)
2488 for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++)
2489 crypto_unregister_aead(&aes_authenc_algs[i]);
2490#endif
2491
Cyrille Pitchend52db512016-10-03 14:33:16 +02002492 if (dd->caps.has_xts)
2493 crypto_unregister_alg(&aes_xts_alg);
2494
Cyrille Pitchend4419542015-12-17 18:13:07 +01002495 if (dd->caps.has_gcm)
2496 crypto_unregister_aead(&aes_gcm_alg);
2497
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002498 if (dd->caps.has_cfb64)
2499 crypto_unregister_alg(&aes_cfb64_alg);
Cyrille Pitchen924a8bc2015-12-17 17:48:35 +01002500
2501 for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
2502 crypto_unregister_alg(&aes_algs[i]);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002503}
2504
2505static int atmel_aes_register_algs(struct atmel_aes_dev *dd)
2506{
2507 int err, i, j;
2508
2509 for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002510 err = crypto_register_alg(&aes_algs[i]);
2511 if (err)
2512 goto err_aes_algs;
2513 }
2514
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002515 if (dd->caps.has_cfb64) {
2516 err = crypto_register_alg(&aes_cfb64_alg);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002517 if (err)
2518 goto err_aes_cfb64_alg;
2519 }
2520
Cyrille Pitchend4419542015-12-17 18:13:07 +01002521 if (dd->caps.has_gcm) {
2522 err = crypto_register_aead(&aes_gcm_alg);
2523 if (err)
2524 goto err_aes_gcm_alg;
2525 }
2526
Cyrille Pitchend52db512016-10-03 14:33:16 +02002527 if (dd->caps.has_xts) {
2528 err = crypto_register_alg(&aes_xts_alg);
2529 if (err)
2530 goto err_aes_xts_alg;
2531 }
2532
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002533#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2534 if (dd->caps.has_authenc) {
2535 for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++) {
2536 err = crypto_register_aead(&aes_authenc_algs[i]);
2537 if (err)
2538 goto err_aes_authenc_alg;
2539 }
2540 }
2541#endif
2542
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002543 return 0;
2544
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002545#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2546 /* i = ARRAY_SIZE(aes_authenc_algs); */
2547err_aes_authenc_alg:
2548 for (j = 0; j < i; j++)
2549 crypto_unregister_aead(&aes_authenc_algs[j]);
2550 crypto_unregister_alg(&aes_xts_alg);
2551#endif
Cyrille Pitchend52db512016-10-03 14:33:16 +02002552err_aes_xts_alg:
2553 crypto_unregister_aead(&aes_gcm_alg);
Cyrille Pitchend4419542015-12-17 18:13:07 +01002554err_aes_gcm_alg:
2555 crypto_unregister_alg(&aes_cfb64_alg);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002556err_aes_cfb64_alg:
2557 i = ARRAY_SIZE(aes_algs);
2558err_aes_algs:
2559 for (j = 0; j < i; j++)
2560 crypto_unregister_alg(&aes_algs[j]);
2561
2562 return err;
2563}
2564
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002565static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
2566{
2567 dd->caps.has_dualbuff = 0;
2568 dd->caps.has_cfb64 = 0;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01002569 dd->caps.has_ctr32 = 0;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002570 dd->caps.has_gcm = 0;
Cyrille Pitchend52db512016-10-03 14:33:16 +02002571 dd->caps.has_xts = 0;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002572 dd->caps.has_authenc = 0;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002573 dd->caps.max_burst_size = 1;
2574
2575 /* keep only major version number */
2576 switch (dd->hw_version & 0xff0) {
Leilei Zhao973e2092015-12-17 17:48:32 +01002577 case 0x500:
2578 dd->caps.has_dualbuff = 1;
2579 dd->caps.has_cfb64 = 1;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01002580 dd->caps.has_ctr32 = 1;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002581 dd->caps.has_gcm = 1;
Cyrille Pitchend52db512016-10-03 14:33:16 +02002582 dd->caps.has_xts = 1;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002583 dd->caps.has_authenc = 1;
Leilei Zhao973e2092015-12-17 17:48:32 +01002584 dd->caps.max_burst_size = 4;
2585 break;
Leilei Zhaocf1f0d12015-04-07 17:45:02 +08002586 case 0x200:
2587 dd->caps.has_dualbuff = 1;
2588 dd->caps.has_cfb64 = 1;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01002589 dd->caps.has_ctr32 = 1;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002590 dd->caps.has_gcm = 1;
Leilei Zhaocf1f0d12015-04-07 17:45:02 +08002591 dd->caps.max_burst_size = 4;
2592 break;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002593 case 0x130:
2594 dd->caps.has_dualbuff = 1;
2595 dd->caps.has_cfb64 = 1;
2596 dd->caps.max_burst_size = 4;
2597 break;
2598 case 0x120:
2599 break;
2600 default:
2601 dev_warn(dd->dev,
2602 "Unmanaged aes version, set minimum capabilities\n");
2603 break;
2604 }
2605}
2606
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002607#if defined(CONFIG_OF)
2608static const struct of_device_id atmel_aes_dt_ids[] = {
2609 { .compatible = "atmel,at91sam9g46-aes" },
2610 { /* sentinel */ }
2611};
2612MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids);
2613
2614static struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
2615{
2616 struct device_node *np = pdev->dev.of_node;
2617 struct crypto_platform_data *pdata;
2618
2619 if (!np) {
2620 dev_err(&pdev->dev, "device node not found\n");
2621 return ERR_PTR(-EINVAL);
2622 }
2623
2624 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2625 if (!pdata) {
2626 dev_err(&pdev->dev, "could not allocate memory for pdata\n");
2627 return ERR_PTR(-ENOMEM);
2628 }
2629
2630 pdata->dma_slave = devm_kzalloc(&pdev->dev,
2631 sizeof(*(pdata->dma_slave)),
2632 GFP_KERNEL);
2633 if (!pdata->dma_slave) {
2634 dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
2635 devm_kfree(&pdev->dev, pdata);
2636 return ERR_PTR(-ENOMEM);
2637 }
2638
2639 return pdata;
2640}
2641#else
2642static inline struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
2643{
2644 return ERR_PTR(-EINVAL);
2645}
2646#endif
2647
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002648static int atmel_aes_probe(struct platform_device *pdev)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002649{
2650 struct atmel_aes_dev *aes_dd;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002651 struct crypto_platform_data *pdata;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002652 struct device *dev = &pdev->dev;
2653 struct resource *aes_res;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002654 int err;
2655
2656 pdata = pdev->dev.platform_data;
2657 if (!pdata) {
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002658 pdata = atmel_aes_of_init(pdev);
2659 if (IS_ERR(pdata)) {
2660 err = PTR_ERR(pdata);
2661 goto aes_dd_err;
2662 }
2663 }
2664
2665 if (!pdata->dma_slave) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002666 err = -ENXIO;
2667 goto aes_dd_err;
2668 }
2669
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002670 aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002671 if (aes_dd == NULL) {
2672 dev_err(dev, "unable to alloc data struct.\n");
2673 err = -ENOMEM;
2674 goto aes_dd_err;
2675 }
2676
2677 aes_dd->dev = dev;
2678
2679 platform_set_drvdata(pdev, aes_dd);
2680
2681 INIT_LIST_HEAD(&aes_dd->list);
Leilei Zhao8a10eb82015-04-07 17:45:09 +08002682 spin_lock_init(&aes_dd->lock);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002683
2684 tasklet_init(&aes_dd->done_task, atmel_aes_done_task,
2685 (unsigned long)aes_dd);
2686 tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task,
2687 (unsigned long)aes_dd);
2688
2689 crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH);
2690
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002691 /* Get the base address */
2692 aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2693 if (!aes_res) {
2694 dev_err(dev, "no MEM resource info\n");
2695 err = -ENODEV;
2696 goto res_err;
2697 }
2698 aes_dd->phys_base = aes_res->start;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002699
2700 /* Get the IRQ */
2701 aes_dd->irq = platform_get_irq(pdev, 0);
2702 if (aes_dd->irq < 0) {
2703 dev_err(dev, "no IRQ resource info\n");
2704 err = aes_dd->irq;
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002705 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002706 }
2707
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002708 err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq,
2709 IRQF_SHARED, "atmel-aes", aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002710 if (err) {
2711 dev_err(dev, "unable to request aes irq.\n");
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002712 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002713 }
2714
2715 /* Initializing the clock */
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002716 aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002717 if (IS_ERR(aes_dd->iclk)) {
Colin Ian Kingbe208352015-02-28 20:40:10 +00002718 dev_err(dev, "clock initialization failed.\n");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002719 err = PTR_ERR(aes_dd->iclk);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002720 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002721 }
2722
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002723 aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res);
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002724 if (IS_ERR(aes_dd->io_base)) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002725 dev_err(dev, "can't ioremap\n");
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002726 err = PTR_ERR(aes_dd->io_base);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002727 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002728 }
2729
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002730 err = clk_prepare(aes_dd->iclk);
Cyrille Pitchenaab0a392015-12-17 17:48:37 +01002731 if (err)
2732 goto res_err;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002733
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002734 err = atmel_aes_hw_version_init(aes_dd);
2735 if (err)
2736 goto iclk_unprepare;
2737
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002738 atmel_aes_get_cap(aes_dd);
2739
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002740#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2741 if (aes_dd->caps.has_authenc && !atmel_sha_authenc_is_ready()) {
2742 err = -EPROBE_DEFER;
2743 goto iclk_unprepare;
2744 }
2745#endif
2746
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002747 err = atmel_aes_buff_init(aes_dd);
2748 if (err)
2749 goto err_aes_buff;
2750
2751 err = atmel_aes_dma_init(aes_dd, pdata);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002752 if (err)
2753 goto err_aes_dma;
2754
2755 spin_lock(&atmel_aes.lock);
2756 list_add_tail(&aes_dd->list, &atmel_aes.dev_list);
2757 spin_unlock(&atmel_aes.lock);
2758
2759 err = atmel_aes_register_algs(aes_dd);
2760 if (err)
2761 goto err_algs;
2762
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002763 dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n",
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +01002764 dma_chan_name(aes_dd->src.chan),
2765 dma_chan_name(aes_dd->dst.chan));
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002766
2767 return 0;
2768
2769err_algs:
2770 spin_lock(&atmel_aes.lock);
2771 list_del(&aes_dd->list);
2772 spin_unlock(&atmel_aes.lock);
2773 atmel_aes_dma_cleanup(aes_dd);
2774err_aes_dma:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002775 atmel_aes_buff_cleanup(aes_dd);
2776err_aes_buff:
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002777iclk_unprepare:
2778 clk_unprepare(aes_dd->iclk);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002779res_err:
2780 tasklet_kill(&aes_dd->done_task);
2781 tasklet_kill(&aes_dd->queue_task);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002782aes_dd_err:
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002783 if (err != -EPROBE_DEFER)
2784 dev_err(dev, "initialization failed.\n");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002785
2786 return err;
2787}
2788
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002789static int atmel_aes_remove(struct platform_device *pdev)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002790{
Wei Yongjunfc783342016-10-24 14:51:22 +00002791 struct atmel_aes_dev *aes_dd;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002792
2793 aes_dd = platform_get_drvdata(pdev);
2794 if (!aes_dd)
2795 return -ENODEV;
2796 spin_lock(&atmel_aes.lock);
2797 list_del(&aes_dd->list);
2798 spin_unlock(&atmel_aes.lock);
2799
2800 atmel_aes_unregister_algs(aes_dd);
2801
2802 tasklet_kill(&aes_dd->done_task);
2803 tasklet_kill(&aes_dd->queue_task);
2804
2805 atmel_aes_dma_cleanup(aes_dd);
Cyrille Pitchen2a377822015-12-17 17:48:46 +01002806 atmel_aes_buff_cleanup(aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002807
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002808 clk_unprepare(aes_dd->iclk);
2809
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002810 return 0;
2811}
2812
2813static struct platform_driver atmel_aes_driver = {
2814 .probe = atmel_aes_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002815 .remove = atmel_aes_remove,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002816 .driver = {
2817 .name = "atmel_aes",
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002818 .of_match_table = of_match_ptr(atmel_aes_dt_ids),
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002819 },
2820};
2821
2822module_platform_driver(atmel_aes_driver);
2823
2824MODULE_DESCRIPTION("Atmel AES hw acceleration support.");
2825MODULE_LICENSE("GPL v2");
2826MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");