blob: 9659759dcba9d4b8c88e8b0a50edb75bed2deab0 [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
80#define AES_FLAGS_INIT BIT(2)
81#define AES_FLAGS_BUSY BIT(3)
Cyrille Pitchen45379922015-12-17 18:13:08 +010082#define AES_FLAGS_DUMP_REG BIT(4)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010083#define AES_FLAGS_OWN_SHA BIT(5)
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010084
85#define AES_FLAGS_PERSISTENT (AES_FLAGS_INIT | AES_FLAGS_BUSY)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020086
Nicolas Royercadc4ab2013-02-20 17:10:24 +010087#define ATMEL_AES_QUEUE_LENGTH 50
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020088
Cyrille Pitchen129f8bb2015-12-17 18:13:06 +010089#define ATMEL_AES_DMA_THRESHOLD 256
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020090
91
Nicolas Royercadc4ab2013-02-20 17:10:24 +010092struct atmel_aes_caps {
Cyrille Pitchenafbac172015-12-17 18:13:02 +010093 bool has_dualbuff;
94 bool has_cfb64;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +010095 bool has_ctr32;
Cyrille Pitchend4419542015-12-17 18:13:07 +010096 bool has_gcm;
Cyrille Pitchend52db512016-10-03 14:33:16 +020097 bool has_xts;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010098 bool has_authenc;
Cyrille Pitchenafbac172015-12-17 18:13:02 +010099 u32 max_burst_size;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100100};
101
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200102struct atmel_aes_dev;
103
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100104
105typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *);
106
107
108struct atmel_aes_base_ctx {
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100109 struct atmel_aes_dev *dd;
110 atmel_aes_fn_t start;
111 int keylen;
112 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
113 u16 block_size;
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;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200160};
161
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100162#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
163struct atmel_aes_authenc_reqctx {
164 struct atmel_aes_reqctx base;
165
166 struct scatterlist src[2];
167 struct scatterlist dst[2];
168 size_t textlen;
169 u32 digest[SHA512_DIGEST_SIZE / sizeof(u32)];
170
171 /* auth_req MUST be place last. */
172 struct ahash_request auth_req;
173};
174#endif
175
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200176struct atmel_aes_dma {
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100177 struct dma_chan *chan;
178 struct scatterlist *sg;
179 int nents;
180 unsigned int remainder;
181 unsigned int sg_len;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200182};
183
184struct atmel_aes_dev {
185 struct list_head list;
186 unsigned long phys_base;
187 void __iomem *io_base;
188
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100189 struct crypto_async_request *areq;
190 struct atmel_aes_base_ctx *ctx;
191
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100192 bool is_async;
193 atmel_aes_fn_t resume;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100194 atmel_aes_fn_t cpu_transfer_complete;
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100195
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200196 struct device *dev;
197 struct clk *iclk;
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100198 int irq;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200199
200 unsigned long flags;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200201
202 spinlock_t lock;
203 struct crypto_queue queue;
204
205 struct tasklet_struct done_task;
206 struct tasklet_struct queue_task;
207
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100208 size_t total;
209 size_t datalen;
210 u32 *data;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200211
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100212 struct atmel_aes_dma src;
213 struct atmel_aes_dma dst;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200214
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100215 size_t buflen;
216 void *buf;
217 struct scatterlist aligned_sg;
218 struct scatterlist *real_dst;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200219
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100220 struct atmel_aes_caps caps;
221
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100222 u32 hw_version;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200223};
224
225struct atmel_aes_drv {
226 struct list_head dev_list;
227 spinlock_t lock;
228};
229
230static struct atmel_aes_drv atmel_aes = {
231 .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list),
232 .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock),
233};
234
Cyrille Pitchen45379922015-12-17 18:13:08 +0100235#ifdef VERBOSE_DEBUG
236static const char *atmel_aes_reg_name(u32 offset, char *tmp, size_t sz)
237{
238 switch (offset) {
239 case AES_CR:
240 return "CR";
241
242 case AES_MR:
243 return "MR";
244
245 case AES_ISR:
246 return "ISR";
247
248 case AES_IMR:
249 return "IMR";
250
251 case AES_IER:
252 return "IER";
253
254 case AES_IDR:
255 return "IDR";
256
257 case AES_KEYWR(0):
258 case AES_KEYWR(1):
259 case AES_KEYWR(2):
260 case AES_KEYWR(3):
261 case AES_KEYWR(4):
262 case AES_KEYWR(5):
263 case AES_KEYWR(6):
264 case AES_KEYWR(7):
265 snprintf(tmp, sz, "KEYWR[%u]", (offset - AES_KEYWR(0)) >> 2);
266 break;
267
268 case AES_IDATAR(0):
269 case AES_IDATAR(1):
270 case AES_IDATAR(2):
271 case AES_IDATAR(3):
272 snprintf(tmp, sz, "IDATAR[%u]", (offset - AES_IDATAR(0)) >> 2);
273 break;
274
275 case AES_ODATAR(0):
276 case AES_ODATAR(1):
277 case AES_ODATAR(2):
278 case AES_ODATAR(3):
279 snprintf(tmp, sz, "ODATAR[%u]", (offset - AES_ODATAR(0)) >> 2);
280 break;
281
282 case AES_IVR(0):
283 case AES_IVR(1):
284 case AES_IVR(2):
285 case AES_IVR(3):
286 snprintf(tmp, sz, "IVR[%u]", (offset - AES_IVR(0)) >> 2);
287 break;
288
289 case AES_AADLENR:
290 return "AADLENR";
291
292 case AES_CLENR:
293 return "CLENR";
294
295 case AES_GHASHR(0):
296 case AES_GHASHR(1):
297 case AES_GHASHR(2):
298 case AES_GHASHR(3):
299 snprintf(tmp, sz, "GHASHR[%u]", (offset - AES_GHASHR(0)) >> 2);
300 break;
301
302 case AES_TAGR(0):
303 case AES_TAGR(1):
304 case AES_TAGR(2):
305 case AES_TAGR(3):
306 snprintf(tmp, sz, "TAGR[%u]", (offset - AES_TAGR(0)) >> 2);
307 break;
308
309 case AES_CTRR:
310 return "CTRR";
311
312 case AES_GCMHR(0):
313 case AES_GCMHR(1):
314 case AES_GCMHR(2):
315 case AES_GCMHR(3):
316 snprintf(tmp, sz, "GCMHR[%u]", (offset - AES_GCMHR(0)) >> 2);
Herbert Xue31835a2016-01-19 09:05:43 +0800317 break;
Cyrille Pitchen45379922015-12-17 18:13:08 +0100318
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100319 case AES_EMR:
320 return "EMR";
321
Cyrille Pitchend52db512016-10-03 14:33:16 +0200322 case AES_TWR(0):
323 case AES_TWR(1):
324 case AES_TWR(2):
325 case AES_TWR(3):
326 snprintf(tmp, sz, "TWR[%u]", (offset - AES_TWR(0)) >> 2);
327 break;
328
329 case AES_ALPHAR(0):
330 case AES_ALPHAR(1):
331 case AES_ALPHAR(2):
332 case AES_ALPHAR(3):
333 snprintf(tmp, sz, "ALPHAR[%u]", (offset - AES_ALPHAR(0)) >> 2);
334 break;
335
Cyrille Pitchen45379922015-12-17 18:13:08 +0100336 default:
337 snprintf(tmp, sz, "0x%02x", offset);
338 break;
339 }
340
341 return tmp;
342}
343#endif /* VERBOSE_DEBUG */
344
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100345/* Shared functions */
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100346
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200347static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset)
348{
Cyrille Pitchen45379922015-12-17 18:13:08 +0100349 u32 value = readl_relaxed(dd->io_base + offset);
350
351#ifdef VERBOSE_DEBUG
352 if (dd->flags & AES_FLAGS_DUMP_REG) {
353 char tmp[16];
354
355 dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
356 atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
357 }
358#endif /* VERBOSE_DEBUG */
359
360 return value;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200361}
362
363static inline void atmel_aes_write(struct atmel_aes_dev *dd,
364 u32 offset, u32 value)
365{
Cyrille Pitchen45379922015-12-17 18:13:08 +0100366#ifdef VERBOSE_DEBUG
367 if (dd->flags & AES_FLAGS_DUMP_REG) {
368 char tmp[16];
369
370 dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
Cyrille Pitchenf709dc82016-09-29 18:46:57 +0200371 atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
Cyrille Pitchen45379922015-12-17 18:13:08 +0100372 }
373#endif /* VERBOSE_DEBUG */
374
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200375 writel_relaxed(value, dd->io_base + offset);
376}
377
378static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset,
379 u32 *value, int count)
380{
381 for (; count--; value++, offset += 4)
382 *value = atmel_aes_read(dd, offset);
383}
384
385static void atmel_aes_write_n(struct atmel_aes_dev *dd, u32 offset,
Cyrille Pitchenc0b28d82015-12-17 17:48:33 +0100386 const u32 *value, int count)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200387{
388 for (; count--; value++, offset += 4)
389 atmel_aes_write(dd, offset, *value);
390}
391
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100392static inline void atmel_aes_read_block(struct atmel_aes_dev *dd, u32 offset,
393 u32 *value)
394{
395 atmel_aes_read_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
396}
397
398static inline void atmel_aes_write_block(struct atmel_aes_dev *dd, u32 offset,
399 const u32 *value)
400{
401 atmel_aes_write_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
402}
403
404static inline int atmel_aes_wait_for_data_ready(struct atmel_aes_dev *dd,
405 atmel_aes_fn_t resume)
406{
407 u32 isr = atmel_aes_read(dd, AES_ISR);
408
409 if (unlikely(isr & AES_INT_DATARDY))
410 return resume(dd);
411
412 dd->resume = resume;
413 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
414 return -EINPROGRESS;
415}
416
417static inline size_t atmel_aes_padlen(size_t len, size_t block_size)
418{
419 len &= block_size - 1;
420 return len ? block_size - len : 0;
421}
422
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100423static struct atmel_aes_dev *atmel_aes_find_dev(struct atmel_aes_base_ctx *ctx)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200424{
425 struct atmel_aes_dev *aes_dd = NULL;
426 struct atmel_aes_dev *tmp;
427
428 spin_lock_bh(&atmel_aes.lock);
429 if (!ctx->dd) {
430 list_for_each_entry(tmp, &atmel_aes.dev_list, list) {
431 aes_dd = tmp;
432 break;
433 }
434 ctx->dd = aes_dd;
435 } else {
436 aes_dd = ctx->dd;
437 }
438
439 spin_unlock_bh(&atmel_aes.lock);
440
441 return aes_dd;
442}
443
444static int atmel_aes_hw_init(struct atmel_aes_dev *dd)
445{
LABBE Corentin9d83d292015-10-02 14:12:58 +0200446 int err;
447
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100448 err = clk_enable(dd->iclk);
LABBE Corentin9d83d292015-10-02 14:12:58 +0200449 if (err)
450 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200451
452 if (!(dd->flags & AES_FLAGS_INIT)) {
453 atmel_aes_write(dd, AES_CR, AES_CR_SWRST);
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100454 atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200455 dd->flags |= AES_FLAGS_INIT;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200456 }
457
458 return 0;
459}
460
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100461static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd)
462{
463 return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff;
464}
465
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100466static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200467{
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100468 int err;
469
470 err = atmel_aes_hw_init(dd);
471 if (err)
472 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200473
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100474 dd->hw_version = atmel_aes_get_version(dd);
475
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100476 dev_info(dd->dev, "version: 0x%x\n", dd->hw_version);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200477
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100478 clk_disable(dd->iclk);
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100479 return 0;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200480}
481
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100482static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd,
483 const struct atmel_aes_reqctx *rctx)
484{
485 /* Clear all but persistent flags and set request flags. */
486 dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode;
487}
488
Cyrille Pitchend4419542015-12-17 18:13:07 +0100489static inline bool atmel_aes_is_encrypt(const struct atmel_aes_dev *dd)
490{
491 return (dd->flags & AES_FLAGS_ENCRYPT);
492}
493
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100494#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
495static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err);
496#endif
497
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100498static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200499{
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100500#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
501 atmel_aes_authenc_complete(dd, err);
502#endif
503
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100504 clk_disable(dd->iclk);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200505 dd->flags &= ~AES_FLAGS_BUSY;
506
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100507 if (dd->is_async)
508 dd->areq->complete(dd->areq, err);
509
510 tasklet_schedule(&dd->queue_task);
511
512 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200513}
514
Cyrille Pitchend52db512016-10-03 14:33:16 +0200515static void atmel_aes_write_ctrl_key(struct atmel_aes_dev *dd, bool use_dma,
516 const u32 *iv, const u32 *key, int keylen)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100517{
518 u32 valmr = 0;
519
520 /* MR register must be set before IV registers */
Cyrille Pitchend52db512016-10-03 14:33:16 +0200521 if (keylen == AES_KEYSIZE_128)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100522 valmr |= AES_MR_KEYSIZE_128;
Cyrille Pitchend52db512016-10-03 14:33:16 +0200523 else if (keylen == AES_KEYSIZE_192)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100524 valmr |= AES_MR_KEYSIZE_192;
525 else
526 valmr |= AES_MR_KEYSIZE_256;
527
528 valmr |= dd->flags & AES_FLAGS_MODE_MASK;
529
530 if (use_dma) {
531 valmr |= AES_MR_SMOD_IDATAR0;
532 if (dd->caps.has_dualbuff)
533 valmr |= AES_MR_DUALBUFF;
534 } else {
535 valmr |= AES_MR_SMOD_AUTO;
536 }
537
538 atmel_aes_write(dd, AES_MR, valmr);
539
Cyrille Pitchend52db512016-10-03 14:33:16 +0200540 atmel_aes_write_n(dd, AES_KEYWR(0), key, SIZE_IN_WORDS(keylen));
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100541
542 if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB)
543 atmel_aes_write_block(dd, AES_IVR(0), iv);
544}
545
Cyrille Pitchend52db512016-10-03 14:33:16 +0200546static inline void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma,
547 const u32 *iv)
548
549{
550 atmel_aes_write_ctrl_key(dd, use_dma, iv,
551 dd->ctx->key, dd->ctx->keylen);
552}
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200553
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100554/* CPU transfer */
555
556static int atmel_aes_cpu_transfer(struct atmel_aes_dev *dd)
557{
558 int err = 0;
559 u32 isr;
560
561 for (;;) {
562 atmel_aes_read_block(dd, AES_ODATAR(0), dd->data);
563 dd->data += 4;
564 dd->datalen -= AES_BLOCK_SIZE;
565
566 if (dd->datalen < AES_BLOCK_SIZE)
567 break;
568
569 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
570
571 isr = atmel_aes_read(dd, AES_ISR);
572 if (!(isr & AES_INT_DATARDY)) {
573 dd->resume = atmel_aes_cpu_transfer;
574 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
575 return -EINPROGRESS;
576 }
577 }
578
579 if (!sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
580 dd->buf, dd->total))
581 err = -EINVAL;
582
583 if (err)
584 return atmel_aes_complete(dd, err);
585
586 return dd->cpu_transfer_complete(dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200587}
588
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100589static int atmel_aes_cpu_start(struct atmel_aes_dev *dd,
590 struct scatterlist *src,
591 struct scatterlist *dst,
592 size_t len,
593 atmel_aes_fn_t resume)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200594{
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100595 size_t padlen = atmel_aes_padlen(len, AES_BLOCK_SIZE);
596
597 if (unlikely(len == 0))
598 return -EINVAL;
599
600 sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
601
602 dd->total = len;
603 dd->real_dst = dst;
604 dd->cpu_transfer_complete = resume;
605 dd->datalen = len + padlen;
606 dd->data = (u32 *)dd->buf;
607 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
608 return atmel_aes_wait_for_data_ready(dd, atmel_aes_cpu_transfer);
609}
610
611
612/* DMA transfer */
613
614static void atmel_aes_dma_callback(void *data);
615
616static bool atmel_aes_check_aligned(struct atmel_aes_dev *dd,
617 struct scatterlist *sg,
618 size_t len,
619 struct atmel_aes_dma *dma)
620{
621 int nents;
622
623 if (!IS_ALIGNED(len, dd->ctx->block_size))
624 return false;
625
626 for (nents = 0; sg; sg = sg_next(sg), ++nents) {
627 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
628 return false;
629
630 if (len <= sg->length) {
631 if (!IS_ALIGNED(len, dd->ctx->block_size))
632 return false;
633
634 dma->nents = nents+1;
635 dma->remainder = sg->length - len;
636 sg->length = len;
637 return true;
638 }
639
640 if (!IS_ALIGNED(sg->length, dd->ctx->block_size))
641 return false;
642
643 len -= sg->length;
644 }
645
646 return false;
647}
648
649static inline void atmel_aes_restore_sg(const struct atmel_aes_dma *dma)
650{
651 struct scatterlist *sg = dma->sg;
652 int nents = dma->nents;
653
654 if (!dma->remainder)
655 return;
656
657 while (--nents > 0 && sg)
658 sg = sg_next(sg);
659
660 if (!sg)
661 return;
662
663 sg->length += dma->remainder;
664}
665
666static int atmel_aes_map(struct atmel_aes_dev *dd,
667 struct scatterlist *src,
668 struct scatterlist *dst,
669 size_t len)
670{
671 bool src_aligned, dst_aligned;
672 size_t padlen;
673
674 dd->total = len;
675 dd->src.sg = src;
676 dd->dst.sg = dst;
677 dd->real_dst = dst;
678
679 src_aligned = atmel_aes_check_aligned(dd, src, len, &dd->src);
680 if (src == dst)
681 dst_aligned = src_aligned;
682 else
683 dst_aligned = atmel_aes_check_aligned(dd, dst, len, &dd->dst);
684 if (!src_aligned || !dst_aligned) {
685 padlen = atmel_aes_padlen(len, dd->ctx->block_size);
686
687 if (dd->buflen < len + padlen)
688 return -ENOMEM;
689
690 if (!src_aligned) {
691 sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
692 dd->src.sg = &dd->aligned_sg;
693 dd->src.nents = 1;
694 dd->src.remainder = 0;
695 }
696
697 if (!dst_aligned) {
698 dd->dst.sg = &dd->aligned_sg;
699 dd->dst.nents = 1;
700 dd->dst.remainder = 0;
701 }
702
703 sg_init_table(&dd->aligned_sg, 1);
704 sg_set_buf(&dd->aligned_sg, dd->buf, len + padlen);
705 }
706
707 if (dd->src.sg == dd->dst.sg) {
708 dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
709 DMA_BIDIRECTIONAL);
710 dd->dst.sg_len = dd->src.sg_len;
711 if (!dd->src.sg_len)
712 return -EFAULT;
713 } else {
714 dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
715 DMA_TO_DEVICE);
716 if (!dd->src.sg_len)
717 return -EFAULT;
718
719 dd->dst.sg_len = dma_map_sg(dd->dev, dd->dst.sg, dd->dst.nents,
720 DMA_FROM_DEVICE);
721 if (!dd->dst.sg_len) {
722 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
723 DMA_TO_DEVICE);
724 return -EFAULT;
725 }
726 }
727
728 return 0;
729}
730
731static void atmel_aes_unmap(struct atmel_aes_dev *dd)
732{
733 if (dd->src.sg == dd->dst.sg) {
734 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
735 DMA_BIDIRECTIONAL);
736
737 if (dd->src.sg != &dd->aligned_sg)
738 atmel_aes_restore_sg(&dd->src);
739 } else {
740 dma_unmap_sg(dd->dev, dd->dst.sg, dd->dst.nents,
741 DMA_FROM_DEVICE);
742
743 if (dd->dst.sg != &dd->aligned_sg)
744 atmel_aes_restore_sg(&dd->dst);
745
746 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
747 DMA_TO_DEVICE);
748
749 if (dd->src.sg != &dd->aligned_sg)
750 atmel_aes_restore_sg(&dd->src);
751 }
752
753 if (dd->dst.sg == &dd->aligned_sg)
754 sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
755 dd->buf, dd->total);
756}
757
758static int atmel_aes_dma_transfer_start(struct atmel_aes_dev *dd,
759 enum dma_slave_buswidth addr_width,
760 enum dma_transfer_direction dir,
761 u32 maxburst)
762{
763 struct dma_async_tx_descriptor *desc;
764 struct dma_slave_config config;
765 dma_async_tx_callback callback;
766 struct atmel_aes_dma *dma;
767 int err;
768
769 memset(&config, 0, sizeof(config));
770 config.direction = dir;
771 config.src_addr_width = addr_width;
772 config.dst_addr_width = addr_width;
773 config.src_maxburst = maxburst;
774 config.dst_maxburst = maxburst;
775
776 switch (dir) {
777 case DMA_MEM_TO_DEV:
778 dma = &dd->src;
779 callback = NULL;
780 config.dst_addr = dd->phys_base + AES_IDATAR(0);
781 break;
782
783 case DMA_DEV_TO_MEM:
784 dma = &dd->dst;
785 callback = atmel_aes_dma_callback;
786 config.src_addr = dd->phys_base + AES_ODATAR(0);
787 break;
788
789 default:
790 return -EINVAL;
791 }
792
793 err = dmaengine_slave_config(dma->chan, &config);
794 if (err)
795 return err;
796
797 desc = dmaengine_prep_slave_sg(dma->chan, dma->sg, dma->sg_len, dir,
798 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
799 if (!desc)
800 return -ENOMEM;
801
802 desc->callback = callback;
803 desc->callback_param = dd;
804 dmaengine_submit(desc);
805 dma_async_issue_pending(dma->chan);
806
807 return 0;
808}
809
810static void atmel_aes_dma_transfer_stop(struct atmel_aes_dev *dd,
811 enum dma_transfer_direction dir)
812{
813 struct atmel_aes_dma *dma;
814
815 switch (dir) {
816 case DMA_MEM_TO_DEV:
817 dma = &dd->src;
818 break;
819
820 case DMA_DEV_TO_MEM:
821 dma = &dd->dst;
822 break;
823
824 default:
825 return;
826 }
827
828 dmaengine_terminate_all(dma->chan);
829}
830
831static int atmel_aes_dma_start(struct atmel_aes_dev *dd,
832 struct scatterlist *src,
833 struct scatterlist *dst,
834 size_t len,
835 atmel_aes_fn_t resume)
836{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100837 enum dma_slave_buswidth addr_width;
838 u32 maxburst;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100839 int err;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100840
841 switch (dd->ctx->block_size) {
842 case CFB8_BLOCK_SIZE:
843 addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
844 maxburst = 1;
845 break;
846
847 case CFB16_BLOCK_SIZE:
848 addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
849 maxburst = 1;
850 break;
851
852 case CFB32_BLOCK_SIZE:
853 case CFB64_BLOCK_SIZE:
854 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
855 maxburst = 1;
856 break;
857
858 case AES_BLOCK_SIZE:
859 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
860 maxburst = dd->caps.max_burst_size;
861 break;
862
863 default:
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100864 err = -EINVAL;
865 goto exit;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100866 }
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200867
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100868 err = atmel_aes_map(dd, src, dst, len);
869 if (err)
870 goto exit;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200871
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100872 dd->resume = resume;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200873
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100874 /* Set output DMA transfer first */
875 err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_DEV_TO_MEM,
876 maxburst);
877 if (err)
878 goto unmap;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100879
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100880 /* Then set input DMA transfer */
881 err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_MEM_TO_DEV,
882 maxburst);
883 if (err)
884 goto output_transfer_stop;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100885
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100886 return -EINPROGRESS;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100887
888output_transfer_stop:
889 atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
890unmap:
891 atmel_aes_unmap(dd);
892exit:
893 return atmel_aes_complete(dd, err);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200894}
895
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100896static void atmel_aes_dma_stop(struct atmel_aes_dev *dd)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200897{
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100898 atmel_aes_dma_transfer_stop(dd, DMA_MEM_TO_DEV);
899 atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
900 atmel_aes_unmap(dd);
901}
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200902
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100903static void atmel_aes_dma_callback(void *data)
904{
905 struct atmel_aes_dev *dd = data;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100906
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100907 atmel_aes_dma_stop(dd);
908 dd->is_async = true;
909 (void)dd->resume(dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200910}
911
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200912static int atmel_aes_handle_queue(struct atmel_aes_dev *dd,
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100913 struct crypto_async_request *new_areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200914{
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100915 struct crypto_async_request *areq, *backlog;
916 struct atmel_aes_base_ctx *ctx;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200917 unsigned long flags;
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100918 bool start_async;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200919 int err, ret = 0;
920
921 spin_lock_irqsave(&dd->lock, flags);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100922 if (new_areq)
923 ret = crypto_enqueue_request(&dd->queue, new_areq);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200924 if (dd->flags & AES_FLAGS_BUSY) {
925 spin_unlock_irqrestore(&dd->lock, flags);
926 return ret;
927 }
928 backlog = crypto_get_backlog(&dd->queue);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100929 areq = crypto_dequeue_request(&dd->queue);
930 if (areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200931 dd->flags |= AES_FLAGS_BUSY;
932 spin_unlock_irqrestore(&dd->lock, flags);
933
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100934 if (!areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200935 return ret;
936
937 if (backlog)
938 backlog->complete(backlog, -EINPROGRESS);
939
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100940 ctx = crypto_tfm_ctx(areq->tfm);
941
942 dd->areq = areq;
943 dd->ctx = ctx;
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100944 start_async = (areq != new_areq);
945 dd->is_async = start_async;
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100946
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100947 /* WARNING: ctx->start() MAY change dd->is_async. */
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100948 err = ctx->start(dd);
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100949 return (start_async) ? ret : err;
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100950}
951
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100952
953/* AES async block ciphers */
954
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100955static int atmel_aes_transfer_complete(struct atmel_aes_dev *dd)
956{
957 return atmel_aes_complete(dd, 0);
958}
959
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100960static int atmel_aes_start(struct atmel_aes_dev *dd)
961{
962 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100963 struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
964 bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD ||
965 dd->ctx->block_size != AES_BLOCK_SIZE);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100966 int err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200967
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100968 atmel_aes_set_mode(dd, rctx);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200969
Cyrille Pitchencdfab4a2015-12-17 17:48:38 +0100970 err = atmel_aes_hw_init(dd);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100971 if (err)
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100972 return atmel_aes_complete(dd, err);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200973
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100974 atmel_aes_write_ctrl(dd, use_dma, req->info);
975 if (use_dma)
976 return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes,
977 atmel_aes_transfer_complete);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200978
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100979 return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes,
980 atmel_aes_transfer_complete);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200981}
982
Cyrille Pitchenfcac8362015-12-17 18:13:05 +0100983static inline struct atmel_aes_ctr_ctx *
984atmel_aes_ctr_ctx_cast(struct atmel_aes_base_ctx *ctx)
985{
986 return container_of(ctx, struct atmel_aes_ctr_ctx, base);
987}
988
989static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
990{
991 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
992 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
993 struct scatterlist *src, *dst;
994 u32 ctr, blocks;
995 size_t datalen;
996 bool use_dma, fragmented = false;
997
998 /* Check for transfer completion. */
999 ctx->offset += dd->total;
1000 if (ctx->offset >= req->nbytes)
1001 return atmel_aes_transfer_complete(dd);
1002
1003 /* Compute data length. */
1004 datalen = req->nbytes - ctx->offset;
1005 blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
1006 ctr = be32_to_cpu(ctx->iv[3]);
1007 if (dd->caps.has_ctr32) {
1008 /* Check 32bit counter overflow. */
1009 u32 start = ctr;
1010 u32 end = start + blocks - 1;
1011
1012 if (end < start) {
1013 ctr |= 0xffffffff;
1014 datalen = AES_BLOCK_SIZE * -start;
1015 fragmented = true;
1016 }
1017 } else {
1018 /* Check 16bit counter overflow. */
1019 u16 start = ctr & 0xffff;
1020 u16 end = start + (u16)blocks - 1;
1021
1022 if (blocks >> 16 || end < start) {
1023 ctr |= 0xffff;
1024 datalen = AES_BLOCK_SIZE * (0x10000-start);
1025 fragmented = true;
1026 }
1027 }
1028 use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD);
1029
1030 /* Jump to offset. */
1031 src = scatterwalk_ffwd(ctx->src, req->src, ctx->offset);
1032 dst = ((req->src == req->dst) ? src :
1033 scatterwalk_ffwd(ctx->dst, req->dst, ctx->offset));
1034
1035 /* Configure hardware. */
1036 atmel_aes_write_ctrl(dd, use_dma, ctx->iv);
1037 if (unlikely(fragmented)) {
1038 /*
1039 * Increment the counter manually to cope with the hardware
1040 * counter overflow.
1041 */
1042 ctx->iv[3] = cpu_to_be32(ctr);
1043 crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE);
1044 }
1045
1046 if (use_dma)
1047 return atmel_aes_dma_start(dd, src, dst, datalen,
1048 atmel_aes_ctr_transfer);
1049
1050 return atmel_aes_cpu_start(dd, src, dst, datalen,
1051 atmel_aes_ctr_transfer);
1052}
1053
1054static int atmel_aes_ctr_start(struct atmel_aes_dev *dd)
1055{
1056 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
1057 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
1058 struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
1059 int err;
1060
1061 atmel_aes_set_mode(dd, rctx);
1062
1063 err = atmel_aes_hw_init(dd);
1064 if (err)
1065 return atmel_aes_complete(dd, err);
1066
1067 memcpy(ctx->iv, req->info, AES_BLOCK_SIZE);
1068 ctx->offset = 0;
1069 dd->total = 0;
1070 return atmel_aes_ctr_transfer(dd);
1071}
1072
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001073static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
1074{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001075 struct atmel_aes_base_ctx *ctx;
1076 struct atmel_aes_reqctx *rctx;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001077 struct atmel_aes_dev *dd;
1078
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001079 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001080 switch (mode & AES_FLAGS_OPMODE_MASK) {
1081 case AES_FLAGS_CFB8:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001082 ctx->block_size = CFB8_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001083 break;
1084
1085 case AES_FLAGS_CFB16:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001086 ctx->block_size = CFB16_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001087 break;
1088
1089 case AES_FLAGS_CFB32:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001090 ctx->block_size = CFB32_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001091 break;
1092
1093 case AES_FLAGS_CFB64:
Leilei Zhao9f849512014-04-22 15:23:24 +08001094 ctx->block_size = CFB64_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001095 break;
1096
1097 default:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001098 ctx->block_size = AES_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001099 break;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001100 }
1101
1102 dd = atmel_aes_find_dev(ctx);
1103 if (!dd)
1104 return -ENODEV;
1105
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001106 rctx = ablkcipher_request_ctx(req);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001107 rctx->mode = mode;
1108
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001109 return atmel_aes_handle_queue(dd, &req->base);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001110}
1111
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001112static int atmel_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
1113 unsigned int keylen)
1114{
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001115 struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001116
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001117 if (keylen != AES_KEYSIZE_128 &&
1118 keylen != AES_KEYSIZE_192 &&
1119 keylen != AES_KEYSIZE_256) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001120 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1121 return -EINVAL;
1122 }
1123
1124 memcpy(ctx->key, key, keylen);
1125 ctx->keylen = keylen;
1126
1127 return 0;
1128}
1129
1130static int atmel_aes_ecb_encrypt(struct ablkcipher_request *req)
1131{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001132 return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001133}
1134
1135static int atmel_aes_ecb_decrypt(struct ablkcipher_request *req)
1136{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001137 return atmel_aes_crypt(req, AES_FLAGS_ECB);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001138}
1139
1140static int atmel_aes_cbc_encrypt(struct ablkcipher_request *req)
1141{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001142 return atmel_aes_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001143}
1144
1145static int atmel_aes_cbc_decrypt(struct ablkcipher_request *req)
1146{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001147 return atmel_aes_crypt(req, AES_FLAGS_CBC);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001148}
1149
1150static int atmel_aes_ofb_encrypt(struct ablkcipher_request *req)
1151{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001152 return atmel_aes_crypt(req, AES_FLAGS_OFB | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001153}
1154
1155static int atmel_aes_ofb_decrypt(struct ablkcipher_request *req)
1156{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001157 return atmel_aes_crypt(req, AES_FLAGS_OFB);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001158}
1159
1160static int atmel_aes_cfb_encrypt(struct ablkcipher_request *req)
1161{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001162 return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001163}
1164
1165static int atmel_aes_cfb_decrypt(struct ablkcipher_request *req)
1166{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001167 return atmel_aes_crypt(req, AES_FLAGS_CFB128);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001168}
1169
1170static int atmel_aes_cfb64_encrypt(struct ablkcipher_request *req)
1171{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001172 return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001173}
1174
1175static int atmel_aes_cfb64_decrypt(struct ablkcipher_request *req)
1176{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001177 return atmel_aes_crypt(req, AES_FLAGS_CFB64);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001178}
1179
1180static int atmel_aes_cfb32_encrypt(struct ablkcipher_request *req)
1181{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001182 return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001183}
1184
1185static int atmel_aes_cfb32_decrypt(struct ablkcipher_request *req)
1186{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001187 return atmel_aes_crypt(req, AES_FLAGS_CFB32);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001188}
1189
1190static int atmel_aes_cfb16_encrypt(struct ablkcipher_request *req)
1191{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001192 return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001193}
1194
1195static int atmel_aes_cfb16_decrypt(struct ablkcipher_request *req)
1196{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001197 return atmel_aes_crypt(req, AES_FLAGS_CFB16);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001198}
1199
1200static int atmel_aes_cfb8_encrypt(struct ablkcipher_request *req)
1201{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001202 return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001203}
1204
1205static int atmel_aes_cfb8_decrypt(struct ablkcipher_request *req)
1206{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001207 return atmel_aes_crypt(req, AES_FLAGS_CFB8);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001208}
1209
1210static int atmel_aes_ctr_encrypt(struct ablkcipher_request *req)
1211{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001212 return atmel_aes_crypt(req, AES_FLAGS_CTR | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001213}
1214
1215static int atmel_aes_ctr_decrypt(struct ablkcipher_request *req)
1216{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001217 return atmel_aes_crypt(req, AES_FLAGS_CTR);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001218}
1219
1220static int atmel_aes_cra_init(struct crypto_tfm *tfm)
1221{
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001222 struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
1223
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001224 tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001225 ctx->base.start = atmel_aes_start;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001226
1227 return 0;
1228}
1229
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01001230static int atmel_aes_ctr_cra_init(struct crypto_tfm *tfm)
1231{
1232 struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
1233
1234 tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
1235 ctx->base.start = atmel_aes_ctr_start;
1236
1237 return 0;
1238}
1239
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001240static void atmel_aes_cra_exit(struct crypto_tfm *tfm)
1241{
1242}
1243
1244static struct crypto_alg aes_algs[] = {
1245{
1246 .cra_name = "ecb(aes)",
1247 .cra_driver_name = "atmel-ecb-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001248 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001249 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1250 .cra_blocksize = AES_BLOCK_SIZE,
1251 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001252 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001253 .cra_type = &crypto_ablkcipher_type,
1254 .cra_module = THIS_MODULE,
1255 .cra_init = atmel_aes_cra_init,
1256 .cra_exit = atmel_aes_cra_exit,
1257 .cra_u.ablkcipher = {
1258 .min_keysize = AES_MIN_KEY_SIZE,
1259 .max_keysize = AES_MAX_KEY_SIZE,
1260 .setkey = atmel_aes_setkey,
1261 .encrypt = atmel_aes_ecb_encrypt,
1262 .decrypt = atmel_aes_ecb_decrypt,
1263 }
1264},
1265{
1266 .cra_name = "cbc(aes)",
1267 .cra_driver_name = "atmel-cbc-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001268 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001269 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1270 .cra_blocksize = AES_BLOCK_SIZE,
1271 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001272 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001273 .cra_type = &crypto_ablkcipher_type,
1274 .cra_module = THIS_MODULE,
1275 .cra_init = atmel_aes_cra_init,
1276 .cra_exit = atmel_aes_cra_exit,
1277 .cra_u.ablkcipher = {
1278 .min_keysize = AES_MIN_KEY_SIZE,
1279 .max_keysize = AES_MAX_KEY_SIZE,
1280 .ivsize = AES_BLOCK_SIZE,
1281 .setkey = atmel_aes_setkey,
1282 .encrypt = atmel_aes_cbc_encrypt,
1283 .decrypt = atmel_aes_cbc_decrypt,
1284 }
1285},
1286{
1287 .cra_name = "ofb(aes)",
1288 .cra_driver_name = "atmel-ofb-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001289 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001290 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1291 .cra_blocksize = AES_BLOCK_SIZE,
1292 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001293 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001294 .cra_type = &crypto_ablkcipher_type,
1295 .cra_module = THIS_MODULE,
1296 .cra_init = atmel_aes_cra_init,
1297 .cra_exit = atmel_aes_cra_exit,
1298 .cra_u.ablkcipher = {
1299 .min_keysize = AES_MIN_KEY_SIZE,
1300 .max_keysize = AES_MAX_KEY_SIZE,
1301 .ivsize = AES_BLOCK_SIZE,
1302 .setkey = atmel_aes_setkey,
1303 .encrypt = atmel_aes_ofb_encrypt,
1304 .decrypt = atmel_aes_ofb_decrypt,
1305 }
1306},
1307{
1308 .cra_name = "cfb(aes)",
1309 .cra_driver_name = "atmel-cfb-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001310 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001311 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1312 .cra_blocksize = AES_BLOCK_SIZE,
1313 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001314 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001315 .cra_type = &crypto_ablkcipher_type,
1316 .cra_module = THIS_MODULE,
1317 .cra_init = atmel_aes_cra_init,
1318 .cra_exit = atmel_aes_cra_exit,
1319 .cra_u.ablkcipher = {
1320 .min_keysize = AES_MIN_KEY_SIZE,
1321 .max_keysize = AES_MAX_KEY_SIZE,
1322 .ivsize = AES_BLOCK_SIZE,
1323 .setkey = atmel_aes_setkey,
1324 .encrypt = atmel_aes_cfb_encrypt,
1325 .decrypt = atmel_aes_cfb_decrypt,
1326 }
1327},
1328{
1329 .cra_name = "cfb32(aes)",
1330 .cra_driver_name = "atmel-cfb32-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001331 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001332 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1333 .cra_blocksize = CFB32_BLOCK_SIZE,
1334 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001335 .cra_alignmask = 0x3,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001336 .cra_type = &crypto_ablkcipher_type,
1337 .cra_module = THIS_MODULE,
1338 .cra_init = atmel_aes_cra_init,
1339 .cra_exit = atmel_aes_cra_exit,
1340 .cra_u.ablkcipher = {
1341 .min_keysize = AES_MIN_KEY_SIZE,
1342 .max_keysize = AES_MAX_KEY_SIZE,
1343 .ivsize = AES_BLOCK_SIZE,
1344 .setkey = atmel_aes_setkey,
1345 .encrypt = atmel_aes_cfb32_encrypt,
1346 .decrypt = atmel_aes_cfb32_decrypt,
1347 }
1348},
1349{
1350 .cra_name = "cfb16(aes)",
1351 .cra_driver_name = "atmel-cfb16-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001352 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001353 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1354 .cra_blocksize = CFB16_BLOCK_SIZE,
1355 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001356 .cra_alignmask = 0x1,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001357 .cra_type = &crypto_ablkcipher_type,
1358 .cra_module = THIS_MODULE,
1359 .cra_init = atmel_aes_cra_init,
1360 .cra_exit = atmel_aes_cra_exit,
1361 .cra_u.ablkcipher = {
1362 .min_keysize = AES_MIN_KEY_SIZE,
1363 .max_keysize = AES_MAX_KEY_SIZE,
1364 .ivsize = AES_BLOCK_SIZE,
1365 .setkey = atmel_aes_setkey,
1366 .encrypt = atmel_aes_cfb16_encrypt,
1367 .decrypt = atmel_aes_cfb16_decrypt,
1368 }
1369},
1370{
1371 .cra_name = "cfb8(aes)",
1372 .cra_driver_name = "atmel-cfb8-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001373 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001374 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
Leilei Zhaoe5d8c962014-04-22 15:23:23 +08001375 .cra_blocksize = CFB8_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001376 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
1377 .cra_alignmask = 0x0,
1378 .cra_type = &crypto_ablkcipher_type,
1379 .cra_module = THIS_MODULE,
1380 .cra_init = atmel_aes_cra_init,
1381 .cra_exit = atmel_aes_cra_exit,
1382 .cra_u.ablkcipher = {
1383 .min_keysize = AES_MIN_KEY_SIZE,
1384 .max_keysize = AES_MAX_KEY_SIZE,
1385 .ivsize = AES_BLOCK_SIZE,
1386 .setkey = atmel_aes_setkey,
1387 .encrypt = atmel_aes_cfb8_encrypt,
1388 .decrypt = atmel_aes_cfb8_decrypt,
1389 }
1390},
1391{
1392 .cra_name = "ctr(aes)",
1393 .cra_driver_name = "atmel-ctr-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001394 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001395 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
Cyrille Pitchenda7b8502015-12-17 18:13:04 +01001396 .cra_blocksize = 1,
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01001397 .cra_ctxsize = sizeof(struct atmel_aes_ctr_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001398 .cra_alignmask = 0xf,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001399 .cra_type = &crypto_ablkcipher_type,
1400 .cra_module = THIS_MODULE,
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01001401 .cra_init = atmel_aes_ctr_cra_init,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001402 .cra_exit = atmel_aes_cra_exit,
1403 .cra_u.ablkcipher = {
1404 .min_keysize = AES_MIN_KEY_SIZE,
1405 .max_keysize = AES_MAX_KEY_SIZE,
1406 .ivsize = AES_BLOCK_SIZE,
1407 .setkey = atmel_aes_setkey,
1408 .encrypt = atmel_aes_ctr_encrypt,
1409 .decrypt = atmel_aes_ctr_decrypt,
1410 }
1411},
1412};
1413
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001414static struct crypto_alg aes_cfb64_alg = {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001415 .cra_name = "cfb64(aes)",
1416 .cra_driver_name = "atmel-cfb64-aes",
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +01001417 .cra_priority = ATMEL_AES_PRIORITY,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001418 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1419 .cra_blocksize = CFB64_BLOCK_SIZE,
1420 .cra_ctxsize = sizeof(struct atmel_aes_ctx),
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001421 .cra_alignmask = 0x7,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001422 .cra_type = &crypto_ablkcipher_type,
1423 .cra_module = THIS_MODULE,
1424 .cra_init = atmel_aes_cra_init,
1425 .cra_exit = atmel_aes_cra_exit,
1426 .cra_u.ablkcipher = {
1427 .min_keysize = AES_MIN_KEY_SIZE,
1428 .max_keysize = AES_MAX_KEY_SIZE,
1429 .ivsize = AES_BLOCK_SIZE,
1430 .setkey = atmel_aes_setkey,
1431 .encrypt = atmel_aes_cfb64_encrypt,
1432 .decrypt = atmel_aes_cfb64_decrypt,
1433 }
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001434};
1435
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01001436
Cyrille Pitchend4419542015-12-17 18:13:07 +01001437/* gcm aead functions */
1438
1439static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
1440 const u32 *data, size_t datalen,
1441 const u32 *ghash_in, u32 *ghash_out,
1442 atmel_aes_fn_t resume);
1443static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd);
1444static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd);
1445
1446static int atmel_aes_gcm_start(struct atmel_aes_dev *dd);
1447static int atmel_aes_gcm_process(struct atmel_aes_dev *dd);
1448static int atmel_aes_gcm_length(struct atmel_aes_dev *dd);
1449static int atmel_aes_gcm_data(struct atmel_aes_dev *dd);
1450static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd);
1451static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd);
1452static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd);
1453
1454static inline struct atmel_aes_gcm_ctx *
1455atmel_aes_gcm_ctx_cast(struct atmel_aes_base_ctx *ctx)
1456{
1457 return container_of(ctx, struct atmel_aes_gcm_ctx, base);
1458}
1459
1460static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
1461 const u32 *data, size_t datalen,
1462 const u32 *ghash_in, u32 *ghash_out,
1463 atmel_aes_fn_t resume)
1464{
1465 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1466
1467 dd->data = (u32 *)data;
1468 dd->datalen = datalen;
1469 ctx->ghash_in = ghash_in;
1470 ctx->ghash_out = ghash_out;
1471 ctx->ghash_resume = resume;
1472
1473 atmel_aes_write_ctrl(dd, false, NULL);
1474 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_ghash_init);
1475}
1476
1477static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd)
1478{
1479 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1480
1481 /* Set the data length. */
1482 atmel_aes_write(dd, AES_AADLENR, dd->total);
1483 atmel_aes_write(dd, AES_CLENR, 0);
1484
1485 /* If needed, overwrite the GCM Intermediate Hash Word Registers */
1486 if (ctx->ghash_in)
1487 atmel_aes_write_block(dd, AES_GHASHR(0), ctx->ghash_in);
1488
1489 return atmel_aes_gcm_ghash_finalize(dd);
1490}
1491
1492static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd)
1493{
1494 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1495 u32 isr;
1496
1497 /* Write data into the Input Data Registers. */
1498 while (dd->datalen > 0) {
1499 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
1500 dd->data += 4;
1501 dd->datalen -= AES_BLOCK_SIZE;
1502
1503 isr = atmel_aes_read(dd, AES_ISR);
1504 if (!(isr & AES_INT_DATARDY)) {
1505 dd->resume = atmel_aes_gcm_ghash_finalize;
1506 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
1507 return -EINPROGRESS;
1508 }
1509 }
1510
1511 /* Read the computed hash from GHASHRx. */
1512 atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash_out);
1513
1514 return ctx->ghash_resume(dd);
1515}
1516
1517
1518static int atmel_aes_gcm_start(struct atmel_aes_dev *dd)
1519{
1520 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1521 struct aead_request *req = aead_request_cast(dd->areq);
1522 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1523 struct atmel_aes_reqctx *rctx = aead_request_ctx(req);
1524 size_t ivsize = crypto_aead_ivsize(tfm);
1525 size_t datalen, padlen;
1526 const void *iv = req->iv;
1527 u8 *data = dd->buf;
1528 int err;
1529
1530 atmel_aes_set_mode(dd, rctx);
1531
1532 err = atmel_aes_hw_init(dd);
1533 if (err)
1534 return atmel_aes_complete(dd, err);
1535
Corentin LABBE219d51c2017-08-22 10:08:12 +02001536 if (likely(ivsize == GCM_AES_IV_SIZE)) {
Cyrille Pitchend4419542015-12-17 18:13:07 +01001537 memcpy(ctx->j0, iv, ivsize);
1538 ctx->j0[3] = cpu_to_be32(1);
1539 return atmel_aes_gcm_process(dd);
1540 }
1541
1542 padlen = atmel_aes_padlen(ivsize, AES_BLOCK_SIZE);
1543 datalen = ivsize + padlen + AES_BLOCK_SIZE;
1544 if (datalen > dd->buflen)
1545 return atmel_aes_complete(dd, -EINVAL);
1546
1547 memcpy(data, iv, ivsize);
1548 memset(data + ivsize, 0, padlen + sizeof(u64));
1549 ((u64 *)(data + datalen))[-1] = cpu_to_be64(ivsize * 8);
1550
1551 return atmel_aes_gcm_ghash(dd, (const u32 *)data, datalen,
1552 NULL, ctx->j0, atmel_aes_gcm_process);
1553}
1554
1555static int atmel_aes_gcm_process(struct atmel_aes_dev *dd)
1556{
1557 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1558 struct aead_request *req = aead_request_cast(dd->areq);
1559 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1560 bool enc = atmel_aes_is_encrypt(dd);
1561 u32 authsize;
1562
1563 /* Compute text length. */
1564 authsize = crypto_aead_authsize(tfm);
1565 ctx->textlen = req->cryptlen - (enc ? 0 : authsize);
1566
1567 /*
1568 * According to tcrypt test suite, the GCM Automatic Tag Generation
1569 * fails when both the message and its associated data are empty.
1570 */
1571 if (likely(req->assoclen != 0 || ctx->textlen != 0))
1572 dd->flags |= AES_FLAGS_GTAGEN;
1573
1574 atmel_aes_write_ctrl(dd, false, NULL);
1575 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_length);
1576}
1577
1578static int atmel_aes_gcm_length(struct atmel_aes_dev *dd)
1579{
1580 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1581 struct aead_request *req = aead_request_cast(dd->areq);
1582 u32 j0_lsw, *j0 = ctx->j0;
1583 size_t padlen;
1584
1585 /* Write incr32(J0) into IV. */
1586 j0_lsw = j0[3];
1587 j0[3] = cpu_to_be32(be32_to_cpu(j0[3]) + 1);
1588 atmel_aes_write_block(dd, AES_IVR(0), j0);
1589 j0[3] = j0_lsw;
1590
1591 /* Set aad and text lengths. */
1592 atmel_aes_write(dd, AES_AADLENR, req->assoclen);
1593 atmel_aes_write(dd, AES_CLENR, ctx->textlen);
1594
1595 /* Check whether AAD are present. */
1596 if (unlikely(req->assoclen == 0)) {
1597 dd->datalen = 0;
1598 return atmel_aes_gcm_data(dd);
1599 }
1600
1601 /* Copy assoc data and add padding. */
1602 padlen = atmel_aes_padlen(req->assoclen, AES_BLOCK_SIZE);
1603 if (unlikely(req->assoclen + padlen > dd->buflen))
1604 return atmel_aes_complete(dd, -EINVAL);
1605 sg_copy_to_buffer(req->src, sg_nents(req->src), dd->buf, req->assoclen);
1606
1607 /* Write assoc data into the Input Data register. */
1608 dd->data = (u32 *)dd->buf;
1609 dd->datalen = req->assoclen + padlen;
1610 return atmel_aes_gcm_data(dd);
1611}
1612
1613static int atmel_aes_gcm_data(struct atmel_aes_dev *dd)
1614{
1615 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1616 struct aead_request *req = aead_request_cast(dd->areq);
1617 bool use_dma = (ctx->textlen >= ATMEL_AES_DMA_THRESHOLD);
1618 struct scatterlist *src, *dst;
1619 u32 isr, mr;
1620
1621 /* Write AAD first. */
1622 while (dd->datalen > 0) {
1623 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
1624 dd->data += 4;
1625 dd->datalen -= AES_BLOCK_SIZE;
1626
1627 isr = atmel_aes_read(dd, AES_ISR);
1628 if (!(isr & AES_INT_DATARDY)) {
1629 dd->resume = atmel_aes_gcm_data;
1630 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
1631 return -EINPROGRESS;
1632 }
1633 }
1634
1635 /* GMAC only. */
1636 if (unlikely(ctx->textlen == 0))
1637 return atmel_aes_gcm_tag_init(dd);
1638
1639 /* Prepare src and dst scatter lists to transfer cipher/plain texts */
1640 src = scatterwalk_ffwd(ctx->src, req->src, req->assoclen);
1641 dst = ((req->src == req->dst) ? src :
1642 scatterwalk_ffwd(ctx->dst, req->dst, req->assoclen));
1643
1644 if (use_dma) {
1645 /* Update the Mode Register for DMA transfers. */
1646 mr = atmel_aes_read(dd, AES_MR);
1647 mr &= ~(AES_MR_SMOD_MASK | AES_MR_DUALBUFF);
1648 mr |= AES_MR_SMOD_IDATAR0;
1649 if (dd->caps.has_dualbuff)
1650 mr |= AES_MR_DUALBUFF;
1651 atmel_aes_write(dd, AES_MR, mr);
1652
1653 return atmel_aes_dma_start(dd, src, dst, ctx->textlen,
1654 atmel_aes_gcm_tag_init);
1655 }
1656
1657 return atmel_aes_cpu_start(dd, src, dst, ctx->textlen,
1658 atmel_aes_gcm_tag_init);
1659}
1660
1661static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd)
1662{
1663 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1664 struct aead_request *req = aead_request_cast(dd->areq);
1665 u64 *data = dd->buf;
1666
1667 if (likely(dd->flags & AES_FLAGS_GTAGEN)) {
1668 if (!(atmel_aes_read(dd, AES_ISR) & AES_INT_TAGRDY)) {
1669 dd->resume = atmel_aes_gcm_tag_init;
1670 atmel_aes_write(dd, AES_IER, AES_INT_TAGRDY);
1671 return -EINPROGRESS;
1672 }
1673
1674 return atmel_aes_gcm_finalize(dd);
1675 }
1676
1677 /* Read the GCM Intermediate Hash Word Registers. */
1678 atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash);
1679
1680 data[0] = cpu_to_be64(req->assoclen * 8);
1681 data[1] = cpu_to_be64(ctx->textlen * 8);
1682
1683 return atmel_aes_gcm_ghash(dd, (const u32 *)data, AES_BLOCK_SIZE,
1684 ctx->ghash, ctx->ghash, atmel_aes_gcm_tag);
1685}
1686
1687static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd)
1688{
1689 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1690 unsigned long flags;
1691
1692 /*
1693 * Change mode to CTR to complete the tag generation.
1694 * Use J0 as Initialization Vector.
1695 */
1696 flags = dd->flags;
1697 dd->flags &= ~(AES_FLAGS_OPMODE_MASK | AES_FLAGS_GTAGEN);
1698 dd->flags |= AES_FLAGS_CTR;
1699 atmel_aes_write_ctrl(dd, false, ctx->j0);
1700 dd->flags = flags;
1701
1702 atmel_aes_write_block(dd, AES_IDATAR(0), ctx->ghash);
1703 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_finalize);
1704}
1705
1706static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd)
1707{
1708 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1709 struct aead_request *req = aead_request_cast(dd->areq);
1710 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1711 bool enc = atmel_aes_is_encrypt(dd);
1712 u32 offset, authsize, itag[4], *otag = ctx->tag;
1713 int err;
1714
1715 /* Read the computed tag. */
1716 if (likely(dd->flags & AES_FLAGS_GTAGEN))
1717 atmel_aes_read_block(dd, AES_TAGR(0), ctx->tag);
1718 else
1719 atmel_aes_read_block(dd, AES_ODATAR(0), ctx->tag);
1720
1721 offset = req->assoclen + ctx->textlen;
1722 authsize = crypto_aead_authsize(tfm);
1723 if (enc) {
1724 scatterwalk_map_and_copy(otag, req->dst, offset, authsize, 1);
1725 err = 0;
1726 } else {
1727 scatterwalk_map_and_copy(itag, req->src, offset, authsize, 0);
1728 err = crypto_memneq(itag, otag, authsize) ? -EBADMSG : 0;
1729 }
1730
1731 return atmel_aes_complete(dd, err);
1732}
1733
1734static int atmel_aes_gcm_crypt(struct aead_request *req,
1735 unsigned long mode)
1736{
1737 struct atmel_aes_base_ctx *ctx;
1738 struct atmel_aes_reqctx *rctx;
1739 struct atmel_aes_dev *dd;
1740
1741 ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
1742 ctx->block_size = AES_BLOCK_SIZE;
1743
1744 dd = atmel_aes_find_dev(ctx);
1745 if (!dd)
1746 return -ENODEV;
1747
1748 rctx = aead_request_ctx(req);
1749 rctx->mode = AES_FLAGS_GCM | mode;
1750
1751 return atmel_aes_handle_queue(dd, &req->base);
1752}
1753
1754static int atmel_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
1755 unsigned int keylen)
1756{
1757 struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
1758
1759 if (keylen != AES_KEYSIZE_256 &&
1760 keylen != AES_KEYSIZE_192 &&
1761 keylen != AES_KEYSIZE_128) {
1762 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1763 return -EINVAL;
1764 }
1765
1766 memcpy(ctx->key, key, keylen);
1767 ctx->keylen = keylen;
1768
1769 return 0;
1770}
1771
1772static int atmel_aes_gcm_setauthsize(struct crypto_aead *tfm,
1773 unsigned int authsize)
1774{
1775 /* Same as crypto_gcm_authsize() from crypto/gcm.c */
1776 switch (authsize) {
1777 case 4:
1778 case 8:
1779 case 12:
1780 case 13:
1781 case 14:
1782 case 15:
1783 case 16:
1784 break;
1785 default:
1786 return -EINVAL;
1787 }
1788
1789 return 0;
1790}
1791
1792static int atmel_aes_gcm_encrypt(struct aead_request *req)
1793{
1794 return atmel_aes_gcm_crypt(req, AES_FLAGS_ENCRYPT);
1795}
1796
1797static int atmel_aes_gcm_decrypt(struct aead_request *req)
1798{
1799 return atmel_aes_gcm_crypt(req, 0);
1800}
1801
1802static int atmel_aes_gcm_init(struct crypto_aead *tfm)
1803{
1804 struct atmel_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm);
1805
1806 crypto_aead_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
1807 ctx->base.start = atmel_aes_gcm_start;
1808
1809 return 0;
1810}
1811
1812static void atmel_aes_gcm_exit(struct crypto_aead *tfm)
1813{
1814
1815}
1816
1817static struct aead_alg aes_gcm_alg = {
1818 .setkey = atmel_aes_gcm_setkey,
1819 .setauthsize = atmel_aes_gcm_setauthsize,
1820 .encrypt = atmel_aes_gcm_encrypt,
1821 .decrypt = atmel_aes_gcm_decrypt,
1822 .init = atmel_aes_gcm_init,
1823 .exit = atmel_aes_gcm_exit,
Corentin LABBE219d51c2017-08-22 10:08:12 +02001824 .ivsize = GCM_AES_IV_SIZE,
Cyrille Pitchend4419542015-12-17 18:13:07 +01001825 .maxauthsize = AES_BLOCK_SIZE,
1826
1827 .base = {
1828 .cra_name = "gcm(aes)",
1829 .cra_driver_name = "atmel-gcm-aes",
1830 .cra_priority = ATMEL_AES_PRIORITY,
1831 .cra_flags = CRYPTO_ALG_ASYNC,
1832 .cra_blocksize = 1,
1833 .cra_ctxsize = sizeof(struct atmel_aes_gcm_ctx),
1834 .cra_alignmask = 0xf,
1835 .cra_module = THIS_MODULE,
1836 },
1837};
1838
1839
Cyrille Pitchend52db512016-10-03 14:33:16 +02001840/* xts functions */
1841
1842static inline struct atmel_aes_xts_ctx *
1843atmel_aes_xts_ctx_cast(struct atmel_aes_base_ctx *ctx)
1844{
1845 return container_of(ctx, struct atmel_aes_xts_ctx, base);
1846}
1847
1848static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd);
1849
1850static int atmel_aes_xts_start(struct atmel_aes_dev *dd)
1851{
1852 struct atmel_aes_xts_ctx *ctx = atmel_aes_xts_ctx_cast(dd->ctx);
1853 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
1854 struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
1855 unsigned long flags;
1856 int err;
1857
1858 atmel_aes_set_mode(dd, rctx);
1859
1860 err = atmel_aes_hw_init(dd);
1861 if (err)
1862 return atmel_aes_complete(dd, err);
1863
1864 /* Compute the tweak value from req->info with ecb(aes). */
1865 flags = dd->flags;
1866 dd->flags &= ~AES_FLAGS_MODE_MASK;
1867 dd->flags |= (AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
1868 atmel_aes_write_ctrl_key(dd, false, NULL,
1869 ctx->key2, ctx->base.keylen);
1870 dd->flags = flags;
1871
1872 atmel_aes_write_block(dd, AES_IDATAR(0), req->info);
1873 return atmel_aes_wait_for_data_ready(dd, atmel_aes_xts_process_data);
1874}
1875
1876static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd)
1877{
1878 struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
1879 bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD);
1880 u32 tweak[AES_BLOCK_SIZE / sizeof(u32)];
1881 static const u32 one[AES_BLOCK_SIZE / sizeof(u32)] = {cpu_to_le32(1), };
1882 u8 *tweak_bytes = (u8 *)tweak;
1883 int i;
1884
1885 /* Read the computed ciphered tweak value. */
1886 atmel_aes_read_block(dd, AES_ODATAR(0), tweak);
1887 /*
1888 * Hardware quirk:
1889 * the order of the ciphered tweak bytes need to be reversed before
1890 * writing them into the ODATARx registers.
1891 */
1892 for (i = 0; i < AES_BLOCK_SIZE/2; ++i) {
1893 u8 tmp = tweak_bytes[AES_BLOCK_SIZE - 1 - i];
1894
1895 tweak_bytes[AES_BLOCK_SIZE - 1 - i] = tweak_bytes[i];
1896 tweak_bytes[i] = tmp;
1897 }
1898
1899 /* Process the data. */
1900 atmel_aes_write_ctrl(dd, use_dma, NULL);
1901 atmel_aes_write_block(dd, AES_TWR(0), tweak);
1902 atmel_aes_write_block(dd, AES_ALPHAR(0), one);
1903 if (use_dma)
1904 return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes,
1905 atmel_aes_transfer_complete);
1906
1907 return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes,
1908 atmel_aes_transfer_complete);
1909}
1910
1911static int atmel_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
1912 unsigned int keylen)
1913{
1914 struct atmel_aes_xts_ctx *ctx = crypto_ablkcipher_ctx(tfm);
1915 int err;
1916
1917 err = xts_check_key(crypto_ablkcipher_tfm(tfm), key, keylen);
1918 if (err)
1919 return err;
1920
1921 memcpy(ctx->base.key, key, keylen/2);
1922 memcpy(ctx->key2, key + keylen/2, keylen/2);
1923 ctx->base.keylen = keylen/2;
1924
1925 return 0;
1926}
1927
1928static int atmel_aes_xts_encrypt(struct ablkcipher_request *req)
1929{
1930 return atmel_aes_crypt(req, AES_FLAGS_XTS | AES_FLAGS_ENCRYPT);
1931}
1932
1933static int atmel_aes_xts_decrypt(struct ablkcipher_request *req)
1934{
1935 return atmel_aes_crypt(req, AES_FLAGS_XTS);
1936}
1937
1938static int atmel_aes_xts_cra_init(struct crypto_tfm *tfm)
1939{
1940 struct atmel_aes_xts_ctx *ctx = crypto_tfm_ctx(tfm);
1941
1942 tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
1943 ctx->base.start = atmel_aes_xts_start;
1944
1945 return 0;
1946}
1947
1948static struct crypto_alg aes_xts_alg = {
1949 .cra_name = "xts(aes)",
1950 .cra_driver_name = "atmel-xts-aes",
1951 .cra_priority = ATMEL_AES_PRIORITY,
1952 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1953 .cra_blocksize = AES_BLOCK_SIZE,
1954 .cra_ctxsize = sizeof(struct atmel_aes_xts_ctx),
1955 .cra_alignmask = 0xf,
1956 .cra_type = &crypto_ablkcipher_type,
1957 .cra_module = THIS_MODULE,
1958 .cra_init = atmel_aes_xts_cra_init,
1959 .cra_exit = atmel_aes_cra_exit,
1960 .cra_u.ablkcipher = {
1961 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1962 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1963 .ivsize = AES_BLOCK_SIZE,
1964 .setkey = atmel_aes_xts_setkey,
1965 .encrypt = atmel_aes_xts_encrypt,
1966 .decrypt = atmel_aes_xts_decrypt,
1967 }
1968};
1969
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01001970#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
1971/* authenc aead functions */
1972
1973static int atmel_aes_authenc_start(struct atmel_aes_dev *dd);
1974static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
1975 bool is_async);
1976static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
1977 bool is_async);
1978static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd);
1979static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
1980 bool is_async);
1981
1982static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err)
1983{
1984 struct aead_request *req = aead_request_cast(dd->areq);
1985 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
1986
1987 if (err && (dd->flags & AES_FLAGS_OWN_SHA))
1988 atmel_sha_authenc_abort(&rctx->auth_req);
1989 dd->flags &= ~AES_FLAGS_OWN_SHA;
1990}
1991
1992static int atmel_aes_authenc_start(struct atmel_aes_dev *dd)
1993{
1994 struct aead_request *req = aead_request_cast(dd->areq);
1995 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
1996 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1997 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
1998 int err;
1999
2000 atmel_aes_set_mode(dd, &rctx->base);
2001
2002 err = atmel_aes_hw_init(dd);
2003 if (err)
2004 return atmel_aes_complete(dd, err);
2005
2006 return atmel_sha_authenc_schedule(&rctx->auth_req, ctx->auth,
2007 atmel_aes_authenc_init, dd);
2008}
2009
2010static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
2011 bool is_async)
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 (is_async)
2017 dd->is_async = true;
2018 if (err)
2019 return atmel_aes_complete(dd, err);
2020
2021 /* If here, we've got the ownership of the SHA device. */
2022 dd->flags |= AES_FLAGS_OWN_SHA;
2023
2024 /* Configure the SHA device. */
2025 return atmel_sha_authenc_init(&rctx->auth_req,
2026 req->src, req->assoclen,
2027 rctx->textlen,
2028 atmel_aes_authenc_transfer, dd);
2029}
2030
2031static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
2032 bool is_async)
2033{
2034 struct aead_request *req = aead_request_cast(dd->areq);
2035 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2036 bool enc = atmel_aes_is_encrypt(dd);
2037 struct scatterlist *src, *dst;
2038 u32 iv[AES_BLOCK_SIZE / sizeof(u32)];
2039 u32 emr;
2040
2041 if (is_async)
2042 dd->is_async = true;
2043 if (err)
2044 return atmel_aes_complete(dd, err);
2045
2046 /* Prepare src and dst scatter-lists to transfer cipher/plain texts. */
2047 src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
2048 dst = src;
2049
2050 if (req->src != req->dst)
2051 dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
2052
2053 /* Configure the AES device. */
2054 memcpy(iv, req->iv, sizeof(iv));
2055
2056 /*
2057 * Here we always set the 2nd parameter of atmel_aes_write_ctrl() to
2058 * 'true' even if the data transfer is actually performed by the CPU (so
2059 * not by the DMA) because we must force the AES_MR_SMOD bitfield to the
2060 * value AES_MR_SMOD_IDATAR0. Indeed, both AES_MR_SMOD and SHA_MR_SMOD
2061 * must be set to *_MR_SMOD_IDATAR0.
2062 */
2063 atmel_aes_write_ctrl(dd, true, iv);
2064 emr = AES_EMR_PLIPEN;
2065 if (!enc)
2066 emr |= AES_EMR_PLIPD;
2067 atmel_aes_write(dd, AES_EMR, emr);
2068
2069 /* Transfer data. */
2070 return atmel_aes_dma_start(dd, src, dst, rctx->textlen,
2071 atmel_aes_authenc_digest);
2072}
2073
2074static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd)
2075{
2076 struct aead_request *req = aead_request_cast(dd->areq);
2077 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2078
2079 /* atmel_sha_authenc_final() releases the SHA device. */
2080 dd->flags &= ~AES_FLAGS_OWN_SHA;
2081 return atmel_sha_authenc_final(&rctx->auth_req,
2082 rctx->digest, sizeof(rctx->digest),
2083 atmel_aes_authenc_final, dd);
2084}
2085
2086static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
2087 bool is_async)
2088{
2089 struct aead_request *req = aead_request_cast(dd->areq);
2090 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2091 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2092 bool enc = atmel_aes_is_encrypt(dd);
2093 u32 idigest[SHA512_DIGEST_SIZE / sizeof(u32)], *odigest = rctx->digest;
2094 u32 offs, authsize;
2095
2096 if (is_async)
2097 dd->is_async = true;
2098 if (err)
2099 goto complete;
2100
2101 offs = req->assoclen + rctx->textlen;
2102 authsize = crypto_aead_authsize(tfm);
2103 if (enc) {
2104 scatterwalk_map_and_copy(odigest, req->dst, offs, authsize, 1);
2105 } else {
2106 scatterwalk_map_and_copy(idigest, req->src, offs, authsize, 0);
2107 if (crypto_memneq(idigest, odigest, authsize))
2108 err = -EBADMSG;
2109 }
2110
2111complete:
2112 return atmel_aes_complete(dd, err);
2113}
2114
2115static int atmel_aes_authenc_setkey(struct crypto_aead *tfm, const u8 *key,
2116 unsigned int keylen)
2117{
2118 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2119 struct crypto_authenc_keys keys;
2120 u32 flags;
2121 int err;
2122
2123 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
2124 goto badkey;
2125
2126 if (keys.enckeylen > sizeof(ctx->base.key))
2127 goto badkey;
2128
2129 /* Save auth key. */
2130 flags = crypto_aead_get_flags(tfm);
2131 err = atmel_sha_authenc_setkey(ctx->auth,
2132 keys.authkey, keys.authkeylen,
2133 &flags);
2134 crypto_aead_set_flags(tfm, flags & CRYPTO_TFM_RES_MASK);
2135 if (err) {
2136 memzero_explicit(&keys, sizeof(keys));
2137 return err;
2138 }
2139
2140 /* Save enc key. */
2141 ctx->base.keylen = keys.enckeylen;
2142 memcpy(ctx->base.key, keys.enckey, keys.enckeylen);
2143
2144 memzero_explicit(&keys, sizeof(keys));
2145 return 0;
2146
2147badkey:
2148 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
2149 memzero_explicit(&key, sizeof(keys));
2150 return -EINVAL;
2151}
2152
2153static int atmel_aes_authenc_init_tfm(struct crypto_aead *tfm,
2154 unsigned long auth_mode)
2155{
2156 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2157 unsigned int auth_reqsize = atmel_sha_authenc_get_reqsize();
2158
2159 ctx->auth = atmel_sha_authenc_spawn(auth_mode);
2160 if (IS_ERR(ctx->auth))
2161 return PTR_ERR(ctx->auth);
2162
2163 crypto_aead_set_reqsize(tfm, (sizeof(struct atmel_aes_authenc_reqctx) +
2164 auth_reqsize));
2165 ctx->base.start = atmel_aes_authenc_start;
2166
2167 return 0;
2168}
2169
2170static int atmel_aes_authenc_hmac_sha1_init_tfm(struct crypto_aead *tfm)
2171{
2172 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA1);
2173}
2174
2175static int atmel_aes_authenc_hmac_sha224_init_tfm(struct crypto_aead *tfm)
2176{
2177 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA224);
2178}
2179
2180static int atmel_aes_authenc_hmac_sha256_init_tfm(struct crypto_aead *tfm)
2181{
2182 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA256);
2183}
2184
2185static int atmel_aes_authenc_hmac_sha384_init_tfm(struct crypto_aead *tfm)
2186{
2187 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA384);
2188}
2189
2190static int atmel_aes_authenc_hmac_sha512_init_tfm(struct crypto_aead *tfm)
2191{
2192 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA512);
2193}
2194
2195static void atmel_aes_authenc_exit_tfm(struct crypto_aead *tfm)
2196{
2197 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2198
2199 atmel_sha_authenc_free(ctx->auth);
2200}
2201
2202static int atmel_aes_authenc_crypt(struct aead_request *req,
2203 unsigned long mode)
2204{
2205 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2206 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2207 struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
2208 u32 authsize = crypto_aead_authsize(tfm);
2209 bool enc = (mode & AES_FLAGS_ENCRYPT);
2210 struct atmel_aes_dev *dd;
2211
2212 /* Compute text length. */
2213 if (!enc && req->cryptlen < authsize)
2214 return -EINVAL;
2215 rctx->textlen = req->cryptlen - (enc ? 0 : authsize);
2216
2217 /*
2218 * Currently, empty messages are not supported yet:
2219 * the SHA auto-padding can be used only on non-empty messages.
2220 * Hence a special case needs to be implemented for empty message.
2221 */
2222 if (!rctx->textlen && !req->assoclen)
2223 return -EINVAL;
2224
2225 rctx->base.mode = mode;
2226 ctx->block_size = AES_BLOCK_SIZE;
2227
2228 dd = atmel_aes_find_dev(ctx);
2229 if (!dd)
2230 return -ENODEV;
2231
2232 return atmel_aes_handle_queue(dd, &req->base);
2233}
2234
2235static int atmel_aes_authenc_cbc_aes_encrypt(struct aead_request *req)
2236{
2237 return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
2238}
2239
2240static int atmel_aes_authenc_cbc_aes_decrypt(struct aead_request *req)
2241{
2242 return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC);
2243}
2244
2245static struct aead_alg aes_authenc_algs[] = {
2246{
2247 .setkey = atmel_aes_authenc_setkey,
2248 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2249 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2250 .init = atmel_aes_authenc_hmac_sha1_init_tfm,
2251 .exit = atmel_aes_authenc_exit_tfm,
2252 .ivsize = AES_BLOCK_SIZE,
2253 .maxauthsize = SHA1_DIGEST_SIZE,
2254
2255 .base = {
2256 .cra_name = "authenc(hmac(sha1),cbc(aes))",
2257 .cra_driver_name = "atmel-authenc-hmac-sha1-cbc-aes",
2258 .cra_priority = ATMEL_AES_PRIORITY,
2259 .cra_flags = CRYPTO_ALG_ASYNC,
2260 .cra_blocksize = AES_BLOCK_SIZE,
2261 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
2262 .cra_alignmask = 0xf,
2263 .cra_module = THIS_MODULE,
2264 },
2265},
2266{
2267 .setkey = atmel_aes_authenc_setkey,
2268 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2269 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2270 .init = atmel_aes_authenc_hmac_sha224_init_tfm,
2271 .exit = atmel_aes_authenc_exit_tfm,
2272 .ivsize = AES_BLOCK_SIZE,
2273 .maxauthsize = SHA224_DIGEST_SIZE,
2274
2275 .base = {
2276 .cra_name = "authenc(hmac(sha224),cbc(aes))",
2277 .cra_driver_name = "atmel-authenc-hmac-sha224-cbc-aes",
2278 .cra_priority = ATMEL_AES_PRIORITY,
2279 .cra_flags = CRYPTO_ALG_ASYNC,
2280 .cra_blocksize = AES_BLOCK_SIZE,
2281 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
2282 .cra_alignmask = 0xf,
2283 .cra_module = THIS_MODULE,
2284 },
2285},
2286{
2287 .setkey = atmel_aes_authenc_setkey,
2288 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2289 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2290 .init = atmel_aes_authenc_hmac_sha256_init_tfm,
2291 .exit = atmel_aes_authenc_exit_tfm,
2292 .ivsize = AES_BLOCK_SIZE,
2293 .maxauthsize = SHA256_DIGEST_SIZE,
2294
2295 .base = {
2296 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2297 .cra_driver_name = "atmel-authenc-hmac-sha256-cbc-aes",
2298 .cra_priority = ATMEL_AES_PRIORITY,
2299 .cra_flags = CRYPTO_ALG_ASYNC,
2300 .cra_blocksize = AES_BLOCK_SIZE,
2301 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
2302 .cra_alignmask = 0xf,
2303 .cra_module = THIS_MODULE,
2304 },
2305},
2306{
2307 .setkey = atmel_aes_authenc_setkey,
2308 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2309 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2310 .init = atmel_aes_authenc_hmac_sha384_init_tfm,
2311 .exit = atmel_aes_authenc_exit_tfm,
2312 .ivsize = AES_BLOCK_SIZE,
2313 .maxauthsize = SHA384_DIGEST_SIZE,
2314
2315 .base = {
2316 .cra_name = "authenc(hmac(sha384),cbc(aes))",
2317 .cra_driver_name = "atmel-authenc-hmac-sha384-cbc-aes",
2318 .cra_priority = ATMEL_AES_PRIORITY,
2319 .cra_flags = CRYPTO_ALG_ASYNC,
2320 .cra_blocksize = AES_BLOCK_SIZE,
2321 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
2322 .cra_alignmask = 0xf,
2323 .cra_module = THIS_MODULE,
2324 },
2325},
2326{
2327 .setkey = atmel_aes_authenc_setkey,
2328 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2329 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2330 .init = atmel_aes_authenc_hmac_sha512_init_tfm,
2331 .exit = atmel_aes_authenc_exit_tfm,
2332 .ivsize = AES_BLOCK_SIZE,
2333 .maxauthsize = SHA512_DIGEST_SIZE,
2334
2335 .base = {
2336 .cra_name = "authenc(hmac(sha512),cbc(aes))",
2337 .cra_driver_name = "atmel-authenc-hmac-sha512-cbc-aes",
2338 .cra_priority = ATMEL_AES_PRIORITY,
2339 .cra_flags = CRYPTO_ALG_ASYNC,
2340 .cra_blocksize = AES_BLOCK_SIZE,
2341 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
2342 .cra_alignmask = 0xf,
2343 .cra_module = THIS_MODULE,
2344 },
2345},
2346};
2347#endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
Cyrille Pitchend52db512016-10-03 14:33:16 +02002348
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002349/* Probe functions */
2350
2351static int atmel_aes_buff_init(struct atmel_aes_dev *dd)
2352{
2353 dd->buf = (void *)__get_free_pages(GFP_KERNEL, ATMEL_AES_BUFFER_ORDER);
2354 dd->buflen = ATMEL_AES_BUFFER_SIZE;
2355 dd->buflen &= ~(AES_BLOCK_SIZE - 1);
2356
2357 if (!dd->buf) {
2358 dev_err(dd->dev, "unable to alloc pages.\n");
2359 return -ENOMEM;
2360 }
2361
2362 return 0;
2363}
2364
2365static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd)
2366{
2367 free_page((unsigned long)dd->buf);
2368}
2369
2370static bool atmel_aes_filter(struct dma_chan *chan, void *slave)
2371{
2372 struct at_dma_slave *sl = slave;
2373
2374 if (sl && sl->dma_dev == chan->device->dev) {
2375 chan->private = sl;
2376 return true;
2377 } else {
2378 return false;
2379 }
2380}
2381
2382static int atmel_aes_dma_init(struct atmel_aes_dev *dd,
2383 struct crypto_platform_data *pdata)
2384{
2385 struct at_dma_slave *slave;
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002386 dma_cap_mask_t mask;
2387
2388 dma_cap_zero(mask);
2389 dma_cap_set(DMA_SLAVE, mask);
2390
2391 /* Try to grab 2 DMA channels */
2392 slave = &pdata->dma_slave->rxdata;
2393 dd->src.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
2394 slave, dd->dev, "tx");
2395 if (!dd->src.chan)
2396 goto err_dma_in;
2397
2398 slave = &pdata->dma_slave->txdata;
2399 dd->dst.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
2400 slave, dd->dev, "rx");
2401 if (!dd->dst.chan)
2402 goto err_dma_out;
2403
2404 return 0;
2405
2406err_dma_out:
2407 dma_release_channel(dd->src.chan);
2408err_dma_in:
2409 dev_warn(dd->dev, "no DMA channel available\n");
Tudor-Dan Ambarus3c887612017-10-23 18:34:39 +03002410 return -ENODEV;
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002411}
2412
2413static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd)
2414{
2415 dma_release_channel(dd->dst.chan);
2416 dma_release_channel(dd->src.chan);
2417}
2418
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002419static void atmel_aes_queue_task(unsigned long data)
2420{
2421 struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
2422
2423 atmel_aes_handle_queue(dd, NULL);
2424}
2425
2426static void atmel_aes_done_task(unsigned long data)
2427{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01002428 struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
Cyrille Pitchen10f12c12015-12-17 17:48:42 +01002429
2430 dd->is_async = true;
2431 (void)dd->resume(dd);
2432}
2433
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002434static irqreturn_t atmel_aes_irq(int irq, void *dev_id)
2435{
2436 struct atmel_aes_dev *aes_dd = dev_id;
2437 u32 reg;
2438
2439 reg = atmel_aes_read(aes_dd, AES_ISR);
2440 if (reg & atmel_aes_read(aes_dd, AES_IMR)) {
2441 atmel_aes_write(aes_dd, AES_IDR, reg);
2442 if (AES_FLAGS_BUSY & aes_dd->flags)
2443 tasklet_schedule(&aes_dd->done_task);
2444 else
2445 dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n");
2446 return IRQ_HANDLED;
2447 }
2448
2449 return IRQ_NONE;
2450}
2451
2452static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd)
2453{
2454 int i;
2455
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002456#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2457 if (dd->caps.has_authenc)
2458 for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++)
2459 crypto_unregister_aead(&aes_authenc_algs[i]);
2460#endif
2461
Cyrille Pitchend52db512016-10-03 14:33:16 +02002462 if (dd->caps.has_xts)
2463 crypto_unregister_alg(&aes_xts_alg);
2464
Cyrille Pitchend4419542015-12-17 18:13:07 +01002465 if (dd->caps.has_gcm)
2466 crypto_unregister_aead(&aes_gcm_alg);
2467
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002468 if (dd->caps.has_cfb64)
2469 crypto_unregister_alg(&aes_cfb64_alg);
Cyrille Pitchen924a8bc2015-12-17 17:48:35 +01002470
2471 for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
2472 crypto_unregister_alg(&aes_algs[i]);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002473}
2474
2475static int atmel_aes_register_algs(struct atmel_aes_dev *dd)
2476{
2477 int err, i, j;
2478
2479 for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002480 err = crypto_register_alg(&aes_algs[i]);
2481 if (err)
2482 goto err_aes_algs;
2483 }
2484
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002485 if (dd->caps.has_cfb64) {
2486 err = crypto_register_alg(&aes_cfb64_alg);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002487 if (err)
2488 goto err_aes_cfb64_alg;
2489 }
2490
Cyrille Pitchend4419542015-12-17 18:13:07 +01002491 if (dd->caps.has_gcm) {
2492 err = crypto_register_aead(&aes_gcm_alg);
2493 if (err)
2494 goto err_aes_gcm_alg;
2495 }
2496
Cyrille Pitchend52db512016-10-03 14:33:16 +02002497 if (dd->caps.has_xts) {
2498 err = crypto_register_alg(&aes_xts_alg);
2499 if (err)
2500 goto err_aes_xts_alg;
2501 }
2502
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002503#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2504 if (dd->caps.has_authenc) {
2505 for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++) {
2506 err = crypto_register_aead(&aes_authenc_algs[i]);
2507 if (err)
2508 goto err_aes_authenc_alg;
2509 }
2510 }
2511#endif
2512
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002513 return 0;
2514
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002515#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2516 /* i = ARRAY_SIZE(aes_authenc_algs); */
2517err_aes_authenc_alg:
2518 for (j = 0; j < i; j++)
2519 crypto_unregister_aead(&aes_authenc_algs[j]);
2520 crypto_unregister_alg(&aes_xts_alg);
2521#endif
Cyrille Pitchend52db512016-10-03 14:33:16 +02002522err_aes_xts_alg:
2523 crypto_unregister_aead(&aes_gcm_alg);
Cyrille Pitchend4419542015-12-17 18:13:07 +01002524err_aes_gcm_alg:
2525 crypto_unregister_alg(&aes_cfb64_alg);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002526err_aes_cfb64_alg:
2527 i = ARRAY_SIZE(aes_algs);
2528err_aes_algs:
2529 for (j = 0; j < i; j++)
2530 crypto_unregister_alg(&aes_algs[j]);
2531
2532 return err;
2533}
2534
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002535static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
2536{
2537 dd->caps.has_dualbuff = 0;
2538 dd->caps.has_cfb64 = 0;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01002539 dd->caps.has_ctr32 = 0;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002540 dd->caps.has_gcm = 0;
Cyrille Pitchend52db512016-10-03 14:33:16 +02002541 dd->caps.has_xts = 0;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002542 dd->caps.has_authenc = 0;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002543 dd->caps.max_burst_size = 1;
2544
2545 /* keep only major version number */
2546 switch (dd->hw_version & 0xff0) {
Leilei Zhao973e2092015-12-17 17:48:32 +01002547 case 0x500:
2548 dd->caps.has_dualbuff = 1;
2549 dd->caps.has_cfb64 = 1;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01002550 dd->caps.has_ctr32 = 1;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002551 dd->caps.has_gcm = 1;
Cyrille Pitchend52db512016-10-03 14:33:16 +02002552 dd->caps.has_xts = 1;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002553 dd->caps.has_authenc = 1;
Leilei Zhao973e2092015-12-17 17:48:32 +01002554 dd->caps.max_burst_size = 4;
2555 break;
Leilei Zhaocf1f0d12015-04-07 17:45:02 +08002556 case 0x200:
2557 dd->caps.has_dualbuff = 1;
2558 dd->caps.has_cfb64 = 1;
Cyrille Pitchenfcac8362015-12-17 18:13:05 +01002559 dd->caps.has_ctr32 = 1;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002560 dd->caps.has_gcm = 1;
Leilei Zhaocf1f0d12015-04-07 17:45:02 +08002561 dd->caps.max_burst_size = 4;
2562 break;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002563 case 0x130:
2564 dd->caps.has_dualbuff = 1;
2565 dd->caps.has_cfb64 = 1;
2566 dd->caps.max_burst_size = 4;
2567 break;
2568 case 0x120:
2569 break;
2570 default:
2571 dev_warn(dd->dev,
2572 "Unmanaged aes version, set minimum capabilities\n");
2573 break;
2574 }
2575}
2576
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002577#if defined(CONFIG_OF)
2578static const struct of_device_id atmel_aes_dt_ids[] = {
2579 { .compatible = "atmel,at91sam9g46-aes" },
2580 { /* sentinel */ }
2581};
2582MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids);
2583
2584static struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
2585{
2586 struct device_node *np = pdev->dev.of_node;
2587 struct crypto_platform_data *pdata;
2588
2589 if (!np) {
2590 dev_err(&pdev->dev, "device node not found\n");
2591 return ERR_PTR(-EINVAL);
2592 }
2593
2594 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2595 if (!pdata) {
2596 dev_err(&pdev->dev, "could not allocate memory for pdata\n");
2597 return ERR_PTR(-ENOMEM);
2598 }
2599
2600 pdata->dma_slave = devm_kzalloc(&pdev->dev,
2601 sizeof(*(pdata->dma_slave)),
2602 GFP_KERNEL);
2603 if (!pdata->dma_slave) {
2604 dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
2605 devm_kfree(&pdev->dev, pdata);
2606 return ERR_PTR(-ENOMEM);
2607 }
2608
2609 return pdata;
2610}
2611#else
2612static inline struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
2613{
2614 return ERR_PTR(-EINVAL);
2615}
2616#endif
2617
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002618static int atmel_aes_probe(struct platform_device *pdev)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002619{
2620 struct atmel_aes_dev *aes_dd;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002621 struct crypto_platform_data *pdata;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002622 struct device *dev = &pdev->dev;
2623 struct resource *aes_res;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002624 int err;
2625
2626 pdata = pdev->dev.platform_data;
2627 if (!pdata) {
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002628 pdata = atmel_aes_of_init(pdev);
2629 if (IS_ERR(pdata)) {
2630 err = PTR_ERR(pdata);
2631 goto aes_dd_err;
2632 }
2633 }
2634
2635 if (!pdata->dma_slave) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002636 err = -ENXIO;
2637 goto aes_dd_err;
2638 }
2639
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002640 aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002641 if (aes_dd == NULL) {
2642 dev_err(dev, "unable to alloc data struct.\n");
2643 err = -ENOMEM;
2644 goto aes_dd_err;
2645 }
2646
2647 aes_dd->dev = dev;
2648
2649 platform_set_drvdata(pdev, aes_dd);
2650
2651 INIT_LIST_HEAD(&aes_dd->list);
Leilei Zhao8a10eb82015-04-07 17:45:09 +08002652 spin_lock_init(&aes_dd->lock);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002653
2654 tasklet_init(&aes_dd->done_task, atmel_aes_done_task,
2655 (unsigned long)aes_dd);
2656 tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task,
2657 (unsigned long)aes_dd);
2658
2659 crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH);
2660
2661 aes_dd->irq = -1;
2662
2663 /* Get the base address */
2664 aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2665 if (!aes_res) {
2666 dev_err(dev, "no MEM resource info\n");
2667 err = -ENODEV;
2668 goto res_err;
2669 }
2670 aes_dd->phys_base = aes_res->start;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002671
2672 /* Get the IRQ */
2673 aes_dd->irq = platform_get_irq(pdev, 0);
2674 if (aes_dd->irq < 0) {
2675 dev_err(dev, "no IRQ resource info\n");
2676 err = aes_dd->irq;
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002677 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002678 }
2679
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002680 err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq,
2681 IRQF_SHARED, "atmel-aes", aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002682 if (err) {
2683 dev_err(dev, "unable to request aes irq.\n");
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002684 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002685 }
2686
2687 /* Initializing the clock */
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002688 aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002689 if (IS_ERR(aes_dd->iclk)) {
Colin Ian Kingbe208352015-02-28 20:40:10 +00002690 dev_err(dev, "clock initialization failed.\n");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002691 err = PTR_ERR(aes_dd->iclk);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002692 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002693 }
2694
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002695 aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res);
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002696 if (IS_ERR(aes_dd->io_base)) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002697 dev_err(dev, "can't ioremap\n");
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002698 err = PTR_ERR(aes_dd->io_base);
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002699 goto res_err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002700 }
2701
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002702 err = clk_prepare(aes_dd->iclk);
Cyrille Pitchenaab0a392015-12-17 17:48:37 +01002703 if (err)
2704 goto res_err;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002705
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002706 err = atmel_aes_hw_version_init(aes_dd);
2707 if (err)
2708 goto iclk_unprepare;
2709
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002710 atmel_aes_get_cap(aes_dd);
2711
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002712#ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2713 if (aes_dd->caps.has_authenc && !atmel_sha_authenc_is_ready()) {
2714 err = -EPROBE_DEFER;
2715 goto iclk_unprepare;
2716 }
2717#endif
2718
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002719 err = atmel_aes_buff_init(aes_dd);
2720 if (err)
2721 goto err_aes_buff;
2722
2723 err = atmel_aes_dma_init(aes_dd, pdata);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002724 if (err)
2725 goto err_aes_dma;
2726
2727 spin_lock(&atmel_aes.lock);
2728 list_add_tail(&aes_dd->list, &atmel_aes.dev_list);
2729 spin_unlock(&atmel_aes.lock);
2730
2731 err = atmel_aes_register_algs(aes_dd);
2732 if (err)
2733 goto err_algs;
2734
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002735 dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n",
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +01002736 dma_chan_name(aes_dd->src.chan),
2737 dma_chan_name(aes_dd->dst.chan));
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002738
2739 return 0;
2740
2741err_algs:
2742 spin_lock(&atmel_aes.lock);
2743 list_del(&aes_dd->list);
2744 spin_unlock(&atmel_aes.lock);
2745 atmel_aes_dma_cleanup(aes_dd);
2746err_aes_dma:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002747 atmel_aes_buff_cleanup(aes_dd);
2748err_aes_buff:
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002749iclk_unprepare:
2750 clk_unprepare(aes_dd->iclk);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002751res_err:
2752 tasklet_kill(&aes_dd->done_task);
2753 tasklet_kill(&aes_dd->queue_task);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002754aes_dd_err:
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002755 if (err != -EPROBE_DEFER)
2756 dev_err(dev, "initialization failed.\n");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002757
2758 return err;
2759}
2760
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002761static int atmel_aes_remove(struct platform_device *pdev)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002762{
Wei Yongjunfc783342016-10-24 14:51:22 +00002763 struct atmel_aes_dev *aes_dd;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002764
2765 aes_dd = platform_get_drvdata(pdev);
2766 if (!aes_dd)
2767 return -ENODEV;
2768 spin_lock(&atmel_aes.lock);
2769 list_del(&aes_dd->list);
2770 spin_unlock(&atmel_aes.lock);
2771
2772 atmel_aes_unregister_algs(aes_dd);
2773
2774 tasklet_kill(&aes_dd->done_task);
2775 tasklet_kill(&aes_dd->queue_task);
2776
2777 atmel_aes_dma_cleanup(aes_dd);
Cyrille Pitchen2a377822015-12-17 17:48:46 +01002778 atmel_aes_buff_cleanup(aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002779
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002780 clk_unprepare(aes_dd->iclk);
2781
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002782 return 0;
2783}
2784
2785static struct platform_driver atmel_aes_driver = {
2786 .probe = atmel_aes_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002787 .remove = atmel_aes_remove,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002788 .driver = {
2789 .name = "atmel_aes",
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002790 .of_match_table = of_match_ptr(atmel_aes_dt_ids),
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002791 },
2792};
2793
2794module_platform_driver(atmel_aes_driver);
2795
2796MODULE_DESCRIPTION("Atmel AES hw acceleration support.");
2797MODULE_LICENSE("GPL v2");
2798MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");