blob: 9391ccc03382d13649a8c07ce26ad2758725941f [file] [log] [blame]
Tudor Ambarus820684c2018-08-21 16:36:09 +03001// SPDX-License-Identifier: GPL-2.0
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002/*
3 * Cryptographic API.
4 *
5 * Support for ATMEL AES HW acceleration.
6 *
7 * Copyright (c) 2012 Eukréa Electromatique - ATMEL
8 * Author: Nicolas Royer <nicolas@eukrea.com>
9 *
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020010 * Some ideas are from omap-aes.c driver.
11 */
12
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/err.h>
18#include <linux/clk.h>
19#include <linux/io.h>
20#include <linux/hw_random.h>
21#include <linux/platform_device.h>
22
23#include <linux/device.h>
Tudor Ambarusb46f36c2020-01-15 12:53:53 +000024#include <linux/dmaengine.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020025#include <linux/init.h>
26#include <linux/errno.h>
27#include <linux/interrupt.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020028#include <linux/irq.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020029#include <linux/scatterlist.h>
30#include <linux/dma-mapping.h>
Nicolas Ferrebe943c72013-10-14 17:52:38 +020031#include <linux/of_device.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020032#include <linux/delay.h>
33#include <linux/crypto.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020034#include <crypto/scatterwalk.h>
35#include <crypto/algapi.h>
36#include <crypto/aes.h>
Corentin LABBE219d51c2017-08-22 10:08:12 +020037#include <crypto/gcm.h>
Cyrille Pitchend52db512016-10-03 14:33:16 +020038#include <crypto/xts.h>
Cyrille Pitchend4419542015-12-17 18:13:07 +010039#include <crypto/internal/aead.h>
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +010040#include <crypto/internal/skcipher.h>
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020041#include "atmel-aes-regs.h"
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010042#include "atmel-authenc.h"
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020043
Cyrille Pitchen88efd9a2015-12-17 17:48:34 +010044#define ATMEL_AES_PRIORITY 300
45
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +010046#define ATMEL_AES_BUFFER_ORDER 2
47#define ATMEL_AES_BUFFER_SIZE (PAGE_SIZE << ATMEL_AES_BUFFER_ORDER)
48
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020049#define CFB8_BLOCK_SIZE 1
50#define CFB16_BLOCK_SIZE 2
51#define CFB32_BLOCK_SIZE 4
52#define CFB64_BLOCK_SIZE 8
53
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +010054#define SIZE_IN_WORDS(x) ((x) >> 2)
55
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020056/* AES flags */
Cyrille Pitchend4419542015-12-17 18:13:07 +010057/* Reserve bits [18:16] [14:12] [1:0] for mode (same as for AES_MR) */
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010058#define AES_FLAGS_ENCRYPT AES_MR_CYPHER_ENC
Cyrille Pitchend4419542015-12-17 18:13:07 +010059#define AES_FLAGS_GTAGEN AES_MR_GTAGEN
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010060#define AES_FLAGS_OPMODE_MASK (AES_MR_OPMOD_MASK | AES_MR_CFBS_MASK)
61#define AES_FLAGS_ECB AES_MR_OPMOD_ECB
62#define AES_FLAGS_CBC AES_MR_OPMOD_CBC
63#define AES_FLAGS_OFB AES_MR_OPMOD_OFB
64#define AES_FLAGS_CFB128 (AES_MR_OPMOD_CFB | AES_MR_CFBS_128b)
65#define AES_FLAGS_CFB64 (AES_MR_OPMOD_CFB | AES_MR_CFBS_64b)
66#define AES_FLAGS_CFB32 (AES_MR_OPMOD_CFB | AES_MR_CFBS_32b)
67#define AES_FLAGS_CFB16 (AES_MR_OPMOD_CFB | AES_MR_CFBS_16b)
68#define AES_FLAGS_CFB8 (AES_MR_OPMOD_CFB | AES_MR_CFBS_8b)
69#define AES_FLAGS_CTR AES_MR_OPMOD_CTR
Cyrille Pitchend4419542015-12-17 18:13:07 +010070#define AES_FLAGS_GCM AES_MR_OPMOD_GCM
Cyrille Pitchend52db512016-10-03 14:33:16 +020071#define AES_FLAGS_XTS AES_MR_OPMOD_XTS
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020072
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010073#define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \
Cyrille Pitchend4419542015-12-17 18:13:07 +010074 AES_FLAGS_ENCRYPT | \
75 AES_FLAGS_GTAGEN)
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010076
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010077#define AES_FLAGS_BUSY BIT(3)
Cyrille Pitchen45379922015-12-17 18:13:08 +010078#define AES_FLAGS_DUMP_REG BIT(4)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010079#define AES_FLAGS_OWN_SHA BIT(5)
Cyrille Pitchen77dacf52015-12-17 17:48:41 +010080
Romain Izard7a373fd2017-10-31 16:25:24 +010081#define AES_FLAGS_PERSISTENT AES_FLAGS_BUSY
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020082
Nicolas Royercadc4ab2013-02-20 17:10:24 +010083#define ATMEL_AES_QUEUE_LENGTH 50
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020084
Cyrille Pitchen129f8bb2015-12-17 18:13:06 +010085#define ATMEL_AES_DMA_THRESHOLD 256
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020086
87
Nicolas Royercadc4ab2013-02-20 17:10:24 +010088struct atmel_aes_caps {
Cyrille Pitchenafbac172015-12-17 18:13:02 +010089 bool has_dualbuff;
90 bool has_cfb64;
Cyrille Pitchend4419542015-12-17 18:13:07 +010091 bool has_gcm;
Cyrille Pitchend52db512016-10-03 14:33:16 +020092 bool has_xts;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +010093 bool has_authenc;
Cyrille Pitchenafbac172015-12-17 18:13:02 +010094 u32 max_burst_size;
Nicolas Royercadc4ab2013-02-20 17:10:24 +010095};
96
Nicolas Royerbd3c7b52012-07-01 19:19:44 +020097struct atmel_aes_dev;
98
Cyrille Pitchenccbf7292015-12-17 17:48:39 +010099
100typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *);
101
102
103struct atmel_aes_base_ctx {
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100104 struct atmel_aes_dev *dd;
105 atmel_aes_fn_t start;
106 int keylen;
107 u32 key[AES_KEYSIZE_256 / sizeof(u32)];
108 u16 block_size;
Romain Izard91308012017-10-31 16:25:23 +0100109 bool is_aead;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200110};
111
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100112struct atmel_aes_ctx {
113 struct atmel_aes_base_ctx base;
114};
115
Cyrille Pitchenfcac83652015-12-17 18:13:05 +0100116struct atmel_aes_ctr_ctx {
117 struct atmel_aes_base_ctx base;
118
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100119 __be32 iv[AES_BLOCK_SIZE / sizeof(u32)];
Cyrille Pitchenfcac83652015-12-17 18:13:05 +0100120 size_t offset;
121 struct scatterlist src[2];
122 struct scatterlist dst[2];
Tudor Ambarus3907ccf2019-12-13 14:45:44 +0000123 u32 blocks;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +0100124};
125
Cyrille Pitchend4419542015-12-17 18:13:07 +0100126struct atmel_aes_gcm_ctx {
127 struct atmel_aes_base_ctx base;
128
129 struct scatterlist src[2];
130 struct scatterlist dst[2];
131
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100132 __be32 j0[AES_BLOCK_SIZE / sizeof(u32)];
Cyrille Pitchend4419542015-12-17 18:13:07 +0100133 u32 tag[AES_BLOCK_SIZE / sizeof(u32)];
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100134 __be32 ghash[AES_BLOCK_SIZE / sizeof(u32)];
Cyrille Pitchend4419542015-12-17 18:13:07 +0100135 size_t textlen;
136
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100137 const __be32 *ghash_in;
138 __be32 *ghash_out;
Cyrille Pitchend4419542015-12-17 18:13:07 +0100139 atmel_aes_fn_t ghash_resume;
140};
141
Cyrille Pitchend52db512016-10-03 14:33:16 +0200142struct atmel_aes_xts_ctx {
143 struct atmel_aes_base_ctx base;
144
145 u32 key2[AES_KEYSIZE_256 / sizeof(u32)];
Tudor Ambarusbf2db8e2021-07-20 11:55:34 +0300146 struct crypto_skcipher *fallback_tfm;
Cyrille Pitchend52db512016-10-03 14:33:16 +0200147};
148
Herbert Xu1520c722019-10-28 15:39:07 +0800149#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100150struct atmel_aes_authenc_ctx {
151 struct atmel_aes_base_ctx base;
152 struct atmel_sha_authenc_ctx *auth;
153};
154#endif
155
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200156struct atmel_aes_reqctx {
Cyrille Pitchenafbac172015-12-17 18:13:02 +0100157 unsigned long mode;
Tudor Ambarus57d81542019-11-15 13:49:09 +0000158 u8 lastc[AES_BLOCK_SIZE];
Tudor Ambarusbf2db8e2021-07-20 11:55:34 +0300159 struct skcipher_request fallback_req;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200160};
161
Herbert Xu1520c722019-10-28 15:39:07 +0800162#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100163struct 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,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100393 void *value)
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100394{
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,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100399 const void *value)
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100400{
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
Tudor Ambarusec2088b2021-07-20 11:55:35 +0300423static struct atmel_aes_dev *atmel_aes_dev_alloc(struct atmel_aes_base_ctx *ctx)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200424{
Tudor Ambarusec2088b2021-07-20 11:55:35 +0300425 struct atmel_aes_dev *aes_dd;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200426
427 spin_lock_bh(&atmel_aes.lock);
Tudor Ambarusec2088b2021-07-20 11:55:35 +0300428 /* One AES IP per SoC. */
429 aes_dd = list_first_entry_or_null(&atmel_aes.dev_list,
430 struct atmel_aes_dev, list);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200431 spin_unlock_bh(&atmel_aes.lock);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200432 return aes_dd;
433}
434
435static int atmel_aes_hw_init(struct atmel_aes_dev *dd)
436{
LABBE Corentin9d83d292015-10-02 14:12:58 +0200437 int err;
438
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100439 err = clk_enable(dd->iclk);
LABBE Corentin9d83d292015-10-02 14:12:58 +0200440 if (err)
441 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200442
Romain Izard7a373fd2017-10-31 16:25:24 +0100443 atmel_aes_write(dd, AES_CR, AES_CR_SWRST);
444 atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200445
446 return 0;
447}
448
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100449static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd)
450{
451 return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff;
452}
453
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100454static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200455{
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100456 int err;
457
458 err = atmel_aes_hw_init(dd);
459 if (err)
460 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200461
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100462 dd->hw_version = atmel_aes_get_version(dd);
463
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100464 dev_info(dd->dev, "version: 0x%x\n", dd->hw_version);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200465
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100466 clk_disable(dd->iclk);
Cyrille Pitchenaab0a392015-12-17 17:48:37 +0100467 return 0;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200468}
469
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100470static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd,
471 const struct atmel_aes_reqctx *rctx)
472{
473 /* Clear all but persistent flags and set request flags. */
474 dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode;
475}
476
Cyrille Pitchend4419542015-12-17 18:13:07 +0100477static inline bool atmel_aes_is_encrypt(const struct atmel_aes_dev *dd)
478{
479 return (dd->flags & AES_FLAGS_ENCRYPT);
480}
481
Herbert Xu1520c722019-10-28 15:39:07 +0800482#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100483static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err);
484#endif
485
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000486static void atmel_aes_set_iv_as_last_ciphertext_block(struct atmel_aes_dev *dd)
487{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100488 struct skcipher_request *req = skcipher_request_cast(dd->areq);
489 struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req);
490 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
491 unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000492
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100493 if (req->cryptlen < ivsize)
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000494 return;
495
496 if (rctx->mode & AES_FLAGS_ENCRYPT) {
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100497 scatterwalk_map_and_copy(req->iv, req->dst,
498 req->cryptlen - ivsize, ivsize, 0);
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000499 } else {
500 if (req->src == req->dst)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100501 memcpy(req->iv, rctx->lastc, ivsize);
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000502 else
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100503 scatterwalk_map_and_copy(req->iv, req->src,
504 req->cryptlen - ivsize,
Tudor Ambarus86ef1df2019-10-04 08:55:37 +0000505 ivsize, 0);
506 }
507}
508
Tudor Ambarus371731e2019-12-05 09:54:03 +0000509static inline struct atmel_aes_ctr_ctx *
510atmel_aes_ctr_ctx_cast(struct atmel_aes_base_ctx *ctx)
511{
512 return container_of(ctx, struct atmel_aes_ctr_ctx, base);
513}
514
515static void atmel_aes_ctr_update_req_iv(struct atmel_aes_dev *dd)
516{
517 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
518 struct skcipher_request *req = skcipher_request_cast(dd->areq);
519 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
520 unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
521 int i;
522
Tudor Ambarus3907ccf2019-12-13 14:45:44 +0000523 /*
524 * The CTR transfer works in fragments of data of maximum 1 MByte
525 * because of the 16 bit CTR counter embedded in the IP. When reaching
526 * here, ctx->blocks contains the number of blocks of the last fragment
527 * processed, there is no need to explicit cast it to u16.
528 */
Tudor Ambarus371731e2019-12-05 09:54:03 +0000529 for (i = 0; i < ctx->blocks; i++)
530 crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE);
531
532 memcpy(req->iv, ctx->iv, ivsize);
533}
534
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100535static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200536{
Tudor Ambarusc65d1232019-12-05 09:54:00 +0000537 struct skcipher_request *req = skcipher_request_cast(dd->areq);
538 struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req);
539
Herbert Xu1520c722019-10-28 15:39:07 +0800540#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Romain Izard91308012017-10-31 16:25:23 +0100541 if (dd->ctx->is_aead)
542 atmel_aes_authenc_complete(dd, err);
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +0100543#endif
544
Cyrille Pitchen49a20452016-01-29 17:53:33 +0100545 clk_disable(dd->iclk);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200546 dd->flags &= ~AES_FLAGS_BUSY;
547
Tudor Ambarus27f4adf2019-12-13 09:54:56 +0000548 if (!err && !dd->ctx->is_aead &&
Tudor Ambarus371731e2019-12-05 09:54:03 +0000549 (rctx->mode & AES_FLAGS_OPMODE_MASK) != AES_FLAGS_ECB) {
550 if ((rctx->mode & AES_FLAGS_OPMODE_MASK) != AES_FLAGS_CTR)
551 atmel_aes_set_iv_as_last_ciphertext_block(dd);
552 else
553 atmel_aes_ctr_update_req_iv(dd);
554 }
Romain Izard91308012017-10-31 16:25:23 +0100555
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100556 if (dd->is_async)
557 dd->areq->complete(dd->areq, err);
558
559 tasklet_schedule(&dd->queue_task);
560
561 return err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200562}
563
Cyrille Pitchend52db512016-10-03 14:33:16 +0200564static void atmel_aes_write_ctrl_key(struct atmel_aes_dev *dd, bool use_dma,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100565 const __be32 *iv, const u32 *key, int keylen)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100566{
567 u32 valmr = 0;
568
569 /* MR register must be set before IV registers */
Cyrille Pitchend52db512016-10-03 14:33:16 +0200570 if (keylen == AES_KEYSIZE_128)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100571 valmr |= AES_MR_KEYSIZE_128;
Cyrille Pitchend52db512016-10-03 14:33:16 +0200572 else if (keylen == AES_KEYSIZE_192)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100573 valmr |= AES_MR_KEYSIZE_192;
574 else
575 valmr |= AES_MR_KEYSIZE_256;
576
577 valmr |= dd->flags & AES_FLAGS_MODE_MASK;
578
579 if (use_dma) {
580 valmr |= AES_MR_SMOD_IDATAR0;
581 if (dd->caps.has_dualbuff)
582 valmr |= AES_MR_DUALBUFF;
583 } else {
584 valmr |= AES_MR_SMOD_AUTO;
585 }
586
587 atmel_aes_write(dd, AES_MR, valmr);
588
Cyrille Pitchend52db512016-10-03 14:33:16 +0200589 atmel_aes_write_n(dd, AES_KEYWR(0), key, SIZE_IN_WORDS(keylen));
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100590
591 if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB)
592 atmel_aes_write_block(dd, AES_IVR(0), iv);
593}
594
Cyrille Pitchend52db512016-10-03 14:33:16 +0200595static inline void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +0100596 const __be32 *iv)
Cyrille Pitchend52db512016-10-03 14:33:16 +0200597
598{
599 atmel_aes_write_ctrl_key(dd, use_dma, iv,
600 dd->ctx->key, dd->ctx->keylen);
601}
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200602
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100603/* CPU transfer */
604
605static int atmel_aes_cpu_transfer(struct atmel_aes_dev *dd)
606{
607 int err = 0;
608 u32 isr;
609
610 for (;;) {
611 atmel_aes_read_block(dd, AES_ODATAR(0), dd->data);
612 dd->data += 4;
613 dd->datalen -= AES_BLOCK_SIZE;
614
615 if (dd->datalen < AES_BLOCK_SIZE)
616 break;
617
618 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
619
620 isr = atmel_aes_read(dd, AES_ISR);
621 if (!(isr & AES_INT_DATARDY)) {
622 dd->resume = atmel_aes_cpu_transfer;
623 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
624 return -EINPROGRESS;
625 }
626 }
627
628 if (!sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
629 dd->buf, dd->total))
630 err = -EINVAL;
631
632 if (err)
633 return atmel_aes_complete(dd, err);
634
635 return dd->cpu_transfer_complete(dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200636}
637
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100638static int atmel_aes_cpu_start(struct atmel_aes_dev *dd,
639 struct scatterlist *src,
640 struct scatterlist *dst,
641 size_t len,
642 atmel_aes_fn_t resume)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200643{
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100644 size_t padlen = atmel_aes_padlen(len, AES_BLOCK_SIZE);
645
646 if (unlikely(len == 0))
647 return -EINVAL;
648
649 sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
650
651 dd->total = len;
652 dd->real_dst = dst;
653 dd->cpu_transfer_complete = resume;
654 dd->datalen = len + padlen;
655 dd->data = (u32 *)dd->buf;
656 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
657 return atmel_aes_wait_for_data_ready(dd, atmel_aes_cpu_transfer);
658}
659
660
661/* DMA transfer */
662
663static void atmel_aes_dma_callback(void *data);
664
665static bool atmel_aes_check_aligned(struct atmel_aes_dev *dd,
666 struct scatterlist *sg,
667 size_t len,
668 struct atmel_aes_dma *dma)
669{
670 int nents;
671
672 if (!IS_ALIGNED(len, dd->ctx->block_size))
673 return false;
674
675 for (nents = 0; sg; sg = sg_next(sg), ++nents) {
676 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
677 return false;
678
679 if (len <= sg->length) {
680 if (!IS_ALIGNED(len, dd->ctx->block_size))
681 return false;
682
683 dma->nents = nents+1;
684 dma->remainder = sg->length - len;
685 sg->length = len;
686 return true;
687 }
688
689 if (!IS_ALIGNED(sg->length, dd->ctx->block_size))
690 return false;
691
692 len -= sg->length;
693 }
694
695 return false;
696}
697
698static inline void atmel_aes_restore_sg(const struct atmel_aes_dma *dma)
699{
700 struct scatterlist *sg = dma->sg;
701 int nents = dma->nents;
702
703 if (!dma->remainder)
704 return;
705
706 while (--nents > 0 && sg)
707 sg = sg_next(sg);
708
709 if (!sg)
710 return;
711
712 sg->length += dma->remainder;
713}
714
715static int atmel_aes_map(struct atmel_aes_dev *dd,
716 struct scatterlist *src,
717 struct scatterlist *dst,
718 size_t len)
719{
720 bool src_aligned, dst_aligned;
721 size_t padlen;
722
723 dd->total = len;
724 dd->src.sg = src;
725 dd->dst.sg = dst;
726 dd->real_dst = dst;
727
728 src_aligned = atmel_aes_check_aligned(dd, src, len, &dd->src);
729 if (src == dst)
730 dst_aligned = src_aligned;
731 else
732 dst_aligned = atmel_aes_check_aligned(dd, dst, len, &dd->dst);
733 if (!src_aligned || !dst_aligned) {
734 padlen = atmel_aes_padlen(len, dd->ctx->block_size);
735
736 if (dd->buflen < len + padlen)
737 return -ENOMEM;
738
739 if (!src_aligned) {
740 sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
741 dd->src.sg = &dd->aligned_sg;
742 dd->src.nents = 1;
743 dd->src.remainder = 0;
744 }
745
746 if (!dst_aligned) {
747 dd->dst.sg = &dd->aligned_sg;
748 dd->dst.nents = 1;
749 dd->dst.remainder = 0;
750 }
751
752 sg_init_table(&dd->aligned_sg, 1);
753 sg_set_buf(&dd->aligned_sg, dd->buf, len + padlen);
754 }
755
756 if (dd->src.sg == dd->dst.sg) {
757 dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
758 DMA_BIDIRECTIONAL);
759 dd->dst.sg_len = dd->src.sg_len;
760 if (!dd->src.sg_len)
761 return -EFAULT;
762 } else {
763 dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
764 DMA_TO_DEVICE);
765 if (!dd->src.sg_len)
766 return -EFAULT;
767
768 dd->dst.sg_len = dma_map_sg(dd->dev, dd->dst.sg, dd->dst.nents,
769 DMA_FROM_DEVICE);
770 if (!dd->dst.sg_len) {
771 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
772 DMA_TO_DEVICE);
773 return -EFAULT;
774 }
775 }
776
777 return 0;
778}
779
780static void atmel_aes_unmap(struct atmel_aes_dev *dd)
781{
782 if (dd->src.sg == dd->dst.sg) {
783 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
784 DMA_BIDIRECTIONAL);
785
786 if (dd->src.sg != &dd->aligned_sg)
787 atmel_aes_restore_sg(&dd->src);
788 } else {
789 dma_unmap_sg(dd->dev, dd->dst.sg, dd->dst.nents,
790 DMA_FROM_DEVICE);
791
792 if (dd->dst.sg != &dd->aligned_sg)
793 atmel_aes_restore_sg(&dd->dst);
794
795 dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
796 DMA_TO_DEVICE);
797
798 if (dd->src.sg != &dd->aligned_sg)
799 atmel_aes_restore_sg(&dd->src);
800 }
801
802 if (dd->dst.sg == &dd->aligned_sg)
803 sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
804 dd->buf, dd->total);
805}
806
807static int atmel_aes_dma_transfer_start(struct atmel_aes_dev *dd,
808 enum dma_slave_buswidth addr_width,
809 enum dma_transfer_direction dir,
810 u32 maxburst)
811{
812 struct dma_async_tx_descriptor *desc;
813 struct dma_slave_config config;
814 dma_async_tx_callback callback;
815 struct atmel_aes_dma *dma;
816 int err;
817
818 memset(&config, 0, sizeof(config));
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100819 config.src_addr_width = addr_width;
820 config.dst_addr_width = addr_width;
821 config.src_maxburst = maxburst;
822 config.dst_maxburst = maxburst;
823
824 switch (dir) {
825 case DMA_MEM_TO_DEV:
826 dma = &dd->src;
827 callback = NULL;
828 config.dst_addr = dd->phys_base + AES_IDATAR(0);
829 break;
830
831 case DMA_DEV_TO_MEM:
832 dma = &dd->dst;
833 callback = atmel_aes_dma_callback;
834 config.src_addr = dd->phys_base + AES_ODATAR(0);
835 break;
836
837 default:
838 return -EINVAL;
839 }
840
841 err = dmaengine_slave_config(dma->chan, &config);
842 if (err)
843 return err;
844
845 desc = dmaengine_prep_slave_sg(dma->chan, dma->sg, dma->sg_len, dir,
846 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
847 if (!desc)
848 return -ENOMEM;
849
850 desc->callback = callback;
851 desc->callback_param = dd;
852 dmaengine_submit(desc);
853 dma_async_issue_pending(dma->chan);
854
855 return 0;
856}
857
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100858static int atmel_aes_dma_start(struct atmel_aes_dev *dd,
859 struct scatterlist *src,
860 struct scatterlist *dst,
861 size_t len,
862 atmel_aes_fn_t resume)
863{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100864 enum dma_slave_buswidth addr_width;
865 u32 maxburst;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100866 int err;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100867
868 switch (dd->ctx->block_size) {
869 case CFB8_BLOCK_SIZE:
870 addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
871 maxburst = 1;
872 break;
873
874 case CFB16_BLOCK_SIZE:
875 addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
876 maxburst = 1;
877 break;
878
879 case CFB32_BLOCK_SIZE:
880 case CFB64_BLOCK_SIZE:
881 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
882 maxburst = 1;
883 break;
884
885 case AES_BLOCK_SIZE:
886 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
887 maxburst = dd->caps.max_burst_size;
888 break;
889
890 default:
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100891 err = -EINVAL;
892 goto exit;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100893 }
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200894
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100895 err = atmel_aes_map(dd, src, dst, len);
896 if (err)
897 goto exit;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200898
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100899 dd->resume = resume;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200900
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100901 /* Set output DMA transfer first */
902 err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_DEV_TO_MEM,
903 maxburst);
904 if (err)
905 goto unmap;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100906
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100907 /* Then set input DMA transfer */
908 err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_MEM_TO_DEV,
909 maxburst);
910 if (err)
911 goto output_transfer_stop;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100912
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100913 return -EINPROGRESS;
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100914
915output_transfer_stop:
Tudor Ambarus0e693782019-12-13 09:54:42 +0000916 dmaengine_terminate_sync(dd->dst.chan);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100917unmap:
918 atmel_aes_unmap(dd);
919exit:
920 return atmel_aes_complete(dd, err);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200921}
922
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100923static void atmel_aes_dma_callback(void *data)
924{
925 struct atmel_aes_dev *dd = data;
Nicolas Royercadc4ab2013-02-20 17:10:24 +0100926
Tudor Ambarus0e693782019-12-13 09:54:42 +0000927 atmel_aes_unmap(dd);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100928 dd->is_async = true;
929 (void)dd->resume(dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200930}
931
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200932static int atmel_aes_handle_queue(struct atmel_aes_dev *dd,
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100933 struct crypto_async_request *new_areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200934{
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100935 struct crypto_async_request *areq, *backlog;
936 struct atmel_aes_base_ctx *ctx;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200937 unsigned long flags;
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100938 bool start_async;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200939 int err, ret = 0;
940
941 spin_lock_irqsave(&dd->lock, flags);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100942 if (new_areq)
943 ret = crypto_enqueue_request(&dd->queue, new_areq);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200944 if (dd->flags & AES_FLAGS_BUSY) {
945 spin_unlock_irqrestore(&dd->lock, flags);
946 return ret;
947 }
948 backlog = crypto_get_backlog(&dd->queue);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100949 areq = crypto_dequeue_request(&dd->queue);
950 if (areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200951 dd->flags |= AES_FLAGS_BUSY;
952 spin_unlock_irqrestore(&dd->lock, flags);
953
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100954 if (!areq)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200955 return ret;
956
957 if (backlog)
958 backlog->complete(backlog, -EINPROGRESS);
959
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100960 ctx = crypto_tfm_ctx(areq->tfm);
961
962 dd->areq = areq;
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100963 start_async = (areq != new_areq);
964 dd->is_async = start_async;
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100965
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100966 /* WARNING: ctx->start() MAY change dd->is_async. */
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100967 err = ctx->start(dd);
Cyrille Pitchena1f613f2017-01-26 17:07:55 +0100968 return (start_async) ? ret : err;
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100969}
970
Cyrille Pitchene37a7e52015-12-17 18:13:03 +0100971
972/* AES async block ciphers */
973
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100974static int atmel_aes_transfer_complete(struct atmel_aes_dev *dd)
975{
976 return atmel_aes_complete(dd, 0);
977}
978
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100979static int atmel_aes_start(struct atmel_aes_dev *dd)
980{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100981 struct skcipher_request *req = skcipher_request_cast(dd->areq);
982 struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req);
983 bool use_dma = (req->cryptlen >= ATMEL_AES_DMA_THRESHOLD ||
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100984 dd->ctx->block_size != AES_BLOCK_SIZE);
Cyrille Pitchenccbf7292015-12-17 17:48:39 +0100985 int err;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200986
Cyrille Pitchen77dacf52015-12-17 17:48:41 +0100987 atmel_aes_set_mode(dd, rctx);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200988
Cyrille Pitchencdfab4a2015-12-17 17:48:38 +0100989 err = atmel_aes_hw_init(dd);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100990 if (err)
Cyrille Pitchen10f12c12015-12-17 17:48:42 +0100991 return atmel_aes_complete(dd, err);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200992
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100993 atmel_aes_write_ctrl(dd, use_dma, (void *)req->iv);
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100994 if (use_dma)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100995 return atmel_aes_dma_start(dd, req->src, req->dst,
996 req->cryptlen,
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +0100997 atmel_aes_transfer_complete);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +0200998
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +0100999 return atmel_aes_cpu_start(dd, req->src, req->dst, req->cryptlen,
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +01001000 atmel_aes_transfer_complete);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001001}
1002
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001003static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
1004{
1005 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001006 struct skcipher_request *req = skcipher_request_cast(dd->areq);
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001007 struct scatterlist *src, *dst;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001008 size_t datalen;
Tudor Ambarus781a08d2019-12-05 09:54:01 +00001009 u32 ctr;
Tudor Ambarus371731e2019-12-05 09:54:03 +00001010 u16 start, end;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001011 bool use_dma, fragmented = false;
1012
1013 /* Check for transfer completion. */
1014 ctx->offset += dd->total;
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001015 if (ctx->offset >= req->cryptlen)
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001016 return atmel_aes_transfer_complete(dd);
1017
1018 /* Compute data length. */
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001019 datalen = req->cryptlen - ctx->offset;
Tudor Ambarus371731e2019-12-05 09:54:03 +00001020 ctx->blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001021 ctr = be32_to_cpu(ctx->iv[3]);
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001022
Tudor Ambarus781a08d2019-12-05 09:54:01 +00001023 /* Check 16bit counter overflow. */
1024 start = ctr & 0xffff;
Tudor Ambarus371731e2019-12-05 09:54:03 +00001025 end = start + ctx->blocks - 1;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001026
Tudor Ambarus371731e2019-12-05 09:54:03 +00001027 if (ctx->blocks >> 16 || end < start) {
Tudor Ambarus781a08d2019-12-05 09:54:01 +00001028 ctr |= 0xffff;
1029 datalen = AES_BLOCK_SIZE * (0x10000 - start);
1030 fragmented = true;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001031 }
Tudor Ambarus781a08d2019-12-05 09:54:01 +00001032
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001033 use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD);
1034
1035 /* Jump to offset. */
1036 src = scatterwalk_ffwd(ctx->src, req->src, ctx->offset);
1037 dst = ((req->src == req->dst) ? src :
1038 scatterwalk_ffwd(ctx->dst, req->dst, ctx->offset));
1039
1040 /* Configure hardware. */
1041 atmel_aes_write_ctrl(dd, use_dma, ctx->iv);
1042 if (unlikely(fragmented)) {
1043 /*
1044 * Increment the counter manually to cope with the hardware
1045 * counter overflow.
1046 */
1047 ctx->iv[3] = cpu_to_be32(ctr);
1048 crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE);
1049 }
1050
1051 if (use_dma)
1052 return atmel_aes_dma_start(dd, src, dst, datalen,
1053 atmel_aes_ctr_transfer);
1054
1055 return atmel_aes_cpu_start(dd, src, dst, datalen,
1056 atmel_aes_ctr_transfer);
1057}
1058
1059static int atmel_aes_ctr_start(struct atmel_aes_dev *dd)
1060{
1061 struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001062 struct skcipher_request *req = skcipher_request_cast(dd->areq);
1063 struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req);
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001064 int err;
1065
1066 atmel_aes_set_mode(dd, rctx);
1067
1068 err = atmel_aes_hw_init(dd);
1069 if (err)
1070 return atmel_aes_complete(dd, err);
1071
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001072 memcpy(ctx->iv, req->iv, AES_BLOCK_SIZE);
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001073 ctx->offset = 0;
1074 dd->total = 0;
1075 return atmel_aes_ctr_transfer(dd);
1076}
1077
Tudor Ambarusbf2db8e2021-07-20 11:55:34 +03001078static int atmel_aes_xts_fallback(struct skcipher_request *req, bool enc)
1079{
1080 struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req);
1081 struct atmel_aes_xts_ctx *ctx = crypto_skcipher_ctx(
1082 crypto_skcipher_reqtfm(req));
1083
1084 skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
1085 skcipher_request_set_callback(&rctx->fallback_req, req->base.flags,
1086 req->base.complete, req->base.data);
1087 skcipher_request_set_crypt(&rctx->fallback_req, req->src, req->dst,
1088 req->cryptlen, req->iv);
1089
1090 return enc ? crypto_skcipher_encrypt(&rctx->fallback_req) :
1091 crypto_skcipher_decrypt(&rctx->fallback_req);
1092}
1093
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001094static int atmel_aes_crypt(struct skcipher_request *req, unsigned long mode)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001095{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001096 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
1097 struct atmel_aes_base_ctx *ctx = crypto_skcipher_ctx(skcipher);
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001098 struct atmel_aes_reqctx *rctx;
Tudor Ambarus534b32a2021-07-20 11:55:29 +03001099 u32 opmode = mode & AES_FLAGS_OPMODE_MASK;
1100
Tudor Ambarusbf2db8e2021-07-20 11:55:34 +03001101 if (opmode == AES_FLAGS_XTS) {
1102 if (req->cryptlen < XTS_BLOCK_SIZE)
1103 return -EINVAL;
1104
1105 if (!IS_ALIGNED(req->cryptlen, XTS_BLOCK_SIZE))
1106 return atmel_aes_xts_fallback(req,
1107 mode & AES_FLAGS_ENCRYPT);
1108 }
Tudor Ambarus26d769a2021-07-20 11:55:30 +03001109
Tudor Ambarus0d043352021-07-20 11:55:31 +03001110 /*
1111 * ECB, CBC, CFB, OFB or CTR mode require the plaintext and ciphertext
1112 * to have a positve integer length.
1113 */
1114 if (!req->cryptlen && opmode != AES_FLAGS_XTS)
1115 return 0;
1116
Tudor Ambarus534b32a2021-07-20 11:55:29 +03001117 if ((opmode == AES_FLAGS_ECB || opmode == AES_FLAGS_CBC) &&
1118 !IS_ALIGNED(req->cryptlen, crypto_skcipher_blocksize(skcipher)))
1119 return -EINVAL;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001120
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001121 switch (mode & AES_FLAGS_OPMODE_MASK) {
1122 case AES_FLAGS_CFB8:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001123 ctx->block_size = CFB8_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001124 break;
1125
1126 case AES_FLAGS_CFB16:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001127 ctx->block_size = CFB16_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001128 break;
1129
1130 case AES_FLAGS_CFB32:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001131 ctx->block_size = CFB32_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001132 break;
1133
1134 case AES_FLAGS_CFB64:
Leilei Zhao9f849512014-04-22 15:23:24 +08001135 ctx->block_size = CFB64_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001136 break;
1137
1138 default:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01001139 ctx->block_size = AES_BLOCK_SIZE;
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001140 break;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001141 }
Romain Izard91308012017-10-31 16:25:23 +01001142 ctx->is_aead = false;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001143
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001144 rctx = skcipher_request_ctx(req);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001145 rctx->mode = mode;
1146
Tudor Ambarus534b32a2021-07-20 11:55:29 +03001147 if (opmode != AES_FLAGS_ECB &&
Tudor Ambarusc65d1232019-12-05 09:54:00 +00001148 !(mode & AES_FLAGS_ENCRYPT) && req->src == req->dst) {
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001149 unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
Romain Izard91308012017-10-31 16:25:23 +01001150
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001151 if (req->cryptlen >= ivsize)
Tudor Ambarus86ef1df2019-10-04 08:55:37 +00001152 scatterwalk_map_and_copy(rctx->lastc, req->src,
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001153 req->cryptlen - ivsize,
Tudor Ambarus86ef1df2019-10-04 08:55:37 +00001154 ivsize, 0);
Romain Izard91308012017-10-31 16:25:23 +01001155 }
1156
Tudor Ambarusec2088b2021-07-20 11:55:35 +03001157 return atmel_aes_handle_queue(ctx->dd, &req->base);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001158}
1159
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001160static int atmel_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001161 unsigned int keylen)
1162{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001163 struct atmel_aes_base_ctx *ctx = crypto_skcipher_ctx(tfm);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001164
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001165 if (keylen != AES_KEYSIZE_128 &&
1166 keylen != AES_KEYSIZE_192 &&
Eric Biggers674f3682019-12-30 21:19:36 -06001167 keylen != AES_KEYSIZE_256)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001168 return -EINVAL;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001169
1170 memcpy(ctx->key, key, keylen);
1171 ctx->keylen = keylen;
1172
1173 return 0;
1174}
1175
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001176static int atmel_aes_ecb_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001177{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001178 return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001179}
1180
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001181static int atmel_aes_ecb_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001182{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001183 return atmel_aes_crypt(req, AES_FLAGS_ECB);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001184}
1185
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001186static int atmel_aes_cbc_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001187{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001188 return atmel_aes_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001189}
1190
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001191static int atmel_aes_cbc_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001192{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001193 return atmel_aes_crypt(req, AES_FLAGS_CBC);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001194}
1195
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001196static int atmel_aes_ofb_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001197{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001198 return atmel_aes_crypt(req, AES_FLAGS_OFB | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001199}
1200
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001201static int atmel_aes_ofb_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001202{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001203 return atmel_aes_crypt(req, AES_FLAGS_OFB);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001204}
1205
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001206static int atmel_aes_cfb_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001207{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001208 return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001209}
1210
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001211static int atmel_aes_cfb_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001212{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001213 return atmel_aes_crypt(req, AES_FLAGS_CFB128);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001214}
1215
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001216static int atmel_aes_cfb64_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001217{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001218 return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001219}
1220
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001221static int atmel_aes_cfb64_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001222{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001223 return atmel_aes_crypt(req, AES_FLAGS_CFB64);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001224}
1225
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001226static int atmel_aes_cfb32_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001227{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001228 return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001229}
1230
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001231static int atmel_aes_cfb32_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001232{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001233 return atmel_aes_crypt(req, AES_FLAGS_CFB32);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001234}
1235
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001236static int atmel_aes_cfb16_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001237{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001238 return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001239}
1240
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001241static int atmel_aes_cfb16_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001242{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001243 return atmel_aes_crypt(req, AES_FLAGS_CFB16);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001244}
1245
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001246static int atmel_aes_cfb8_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001247{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001248 return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001249}
1250
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001251static int atmel_aes_cfb8_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001252{
Cyrille Pitchen77dacf52015-12-17 17:48:41 +01001253 return atmel_aes_crypt(req, AES_FLAGS_CFB8);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001254}
1255
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001256static int atmel_aes_ctr_encrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001257{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001258 return atmel_aes_crypt(req, AES_FLAGS_CTR | AES_FLAGS_ENCRYPT);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001259}
1260
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001261static int atmel_aes_ctr_decrypt(struct skcipher_request *req)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001262{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01001263 return atmel_aes_crypt(req, AES_FLAGS_CTR);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001264}
1265
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001266static int atmel_aes_init_tfm(struct crypto_skcipher *tfm)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001267{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001268 struct atmel_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
Tudor Ambarusec2088b2021-07-20 11:55:35 +03001269 struct atmel_aes_dev *dd;
1270
1271 dd = atmel_aes_dev_alloc(&ctx->base);
1272 if (!dd)
1273 return -ENODEV;
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001274
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001275 crypto_skcipher_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
Tudor Ambarusec2088b2021-07-20 11:55:35 +03001276 ctx->base.dd = dd;
1277 ctx->base.dd->ctx = &ctx->base;
Cyrille Pitchenccbf7292015-12-17 17:48:39 +01001278 ctx->base.start = atmel_aes_start;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001279
1280 return 0;
1281}
1282
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001283static int atmel_aes_ctr_init_tfm(struct crypto_skcipher *tfm)
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001284{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001285 struct atmel_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
Tudor Ambarusec2088b2021-07-20 11:55:35 +03001286 struct atmel_aes_dev *dd;
1287
1288 dd = atmel_aes_dev_alloc(&ctx->base);
1289 if (!dd)
1290 return -ENODEV;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001291
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001292 crypto_skcipher_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
Tudor Ambarusec2088b2021-07-20 11:55:35 +03001293 ctx->base.dd = dd;
1294 ctx->base.dd->ctx = &ctx->base;
Cyrille Pitchenfcac83652015-12-17 18:13:05 +01001295 ctx->base.start = atmel_aes_ctr_start;
1296
1297 return 0;
1298}
1299
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001300static struct skcipher_alg aes_algs[] = {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001301{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001302 .base.cra_name = "ecb(aes)",
1303 .base.cra_driver_name = "atmel-ecb-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001304 .base.cra_blocksize = AES_BLOCK_SIZE,
1305 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001306
1307 .init = atmel_aes_init_tfm,
1308 .min_keysize = AES_MIN_KEY_SIZE,
1309 .max_keysize = AES_MAX_KEY_SIZE,
1310 .setkey = atmel_aes_setkey,
1311 .encrypt = atmel_aes_ecb_encrypt,
1312 .decrypt = atmel_aes_ecb_decrypt,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001313},
1314{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001315 .base.cra_name = "cbc(aes)",
1316 .base.cra_driver_name = "atmel-cbc-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001317 .base.cra_blocksize = AES_BLOCK_SIZE,
1318 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001319
1320 .init = atmel_aes_init_tfm,
1321 .min_keysize = AES_MIN_KEY_SIZE,
1322 .max_keysize = AES_MAX_KEY_SIZE,
1323 .setkey = atmel_aes_setkey,
1324 .encrypt = atmel_aes_cbc_encrypt,
1325 .decrypt = atmel_aes_cbc_decrypt,
1326 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001327},
1328{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001329 .base.cra_name = "ofb(aes)",
1330 .base.cra_driver_name = "atmel-ofb-aes",
Tudor Ambarus76d579f2021-07-20 11:55:33 +03001331 .base.cra_blocksize = 1,
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001332 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001333
1334 .init = atmel_aes_init_tfm,
1335 .min_keysize = AES_MIN_KEY_SIZE,
1336 .max_keysize = AES_MAX_KEY_SIZE,
1337 .setkey = atmel_aes_setkey,
1338 .encrypt = atmel_aes_ofb_encrypt,
1339 .decrypt = atmel_aes_ofb_decrypt,
1340 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001341},
1342{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001343 .base.cra_name = "cfb(aes)",
1344 .base.cra_driver_name = "atmel-cfb-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001345 .base.cra_blocksize = AES_BLOCK_SIZE,
1346 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001347
1348 .init = atmel_aes_init_tfm,
1349 .min_keysize = AES_MIN_KEY_SIZE,
1350 .max_keysize = AES_MAX_KEY_SIZE,
1351 .setkey = atmel_aes_setkey,
1352 .encrypt = atmel_aes_cfb_encrypt,
1353 .decrypt = atmel_aes_cfb_decrypt,
1354 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001355},
1356{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001357 .base.cra_name = "cfb32(aes)",
1358 .base.cra_driver_name = "atmel-cfb32-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001359 .base.cra_blocksize = CFB32_BLOCK_SIZE,
1360 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001361
1362 .init = atmel_aes_init_tfm,
1363 .min_keysize = AES_MIN_KEY_SIZE,
1364 .max_keysize = AES_MAX_KEY_SIZE,
1365 .setkey = atmel_aes_setkey,
1366 .encrypt = atmel_aes_cfb32_encrypt,
1367 .decrypt = atmel_aes_cfb32_decrypt,
1368 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001369},
1370{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001371 .base.cra_name = "cfb16(aes)",
1372 .base.cra_driver_name = "atmel-cfb16-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001373 .base.cra_blocksize = CFB16_BLOCK_SIZE,
1374 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001375
1376 .init = atmel_aes_init_tfm,
1377 .min_keysize = AES_MIN_KEY_SIZE,
1378 .max_keysize = AES_MAX_KEY_SIZE,
1379 .setkey = atmel_aes_setkey,
1380 .encrypt = atmel_aes_cfb16_encrypt,
1381 .decrypt = atmel_aes_cfb16_decrypt,
1382 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001383},
1384{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001385 .base.cra_name = "cfb8(aes)",
1386 .base.cra_driver_name = "atmel-cfb8-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001387 .base.cra_blocksize = CFB8_BLOCK_SIZE,
1388 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001389
1390 .init = atmel_aes_init_tfm,
1391 .min_keysize = AES_MIN_KEY_SIZE,
1392 .max_keysize = AES_MAX_KEY_SIZE,
1393 .setkey = atmel_aes_setkey,
1394 .encrypt = atmel_aes_cfb8_encrypt,
1395 .decrypt = atmel_aes_cfb8_decrypt,
1396 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001397},
1398{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001399 .base.cra_name = "ctr(aes)",
1400 .base.cra_driver_name = "atmel-ctr-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001401 .base.cra_blocksize = 1,
1402 .base.cra_ctxsize = sizeof(struct atmel_aes_ctr_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001403
1404 .init = atmel_aes_ctr_init_tfm,
1405 .min_keysize = AES_MIN_KEY_SIZE,
1406 .max_keysize = AES_MAX_KEY_SIZE,
1407 .setkey = atmel_aes_setkey,
1408 .encrypt = atmel_aes_ctr_encrypt,
1409 .decrypt = atmel_aes_ctr_decrypt,
1410 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001411},
1412};
1413
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001414static struct skcipher_alg aes_cfb64_alg = {
1415 .base.cra_name = "cfb64(aes)",
1416 .base.cra_driver_name = "atmel-cfb64-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001417 .base.cra_blocksize = CFB64_BLOCK_SIZE,
1418 .base.cra_ctxsize = sizeof(struct atmel_aes_ctx),
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001419
1420 .init = atmel_aes_init_tfm,
1421 .min_keysize = AES_MIN_KEY_SIZE,
1422 .max_keysize = AES_MAX_KEY_SIZE,
1423 .setkey = atmel_aes_setkey,
1424 .encrypt = atmel_aes_cfb64_encrypt,
1425 .decrypt = atmel_aes_cfb64_decrypt,
1426 .ivsize = AES_BLOCK_SIZE,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02001427};
1428
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01001429
Cyrille Pitchend4419542015-12-17 18:13:07 +01001430/* gcm aead functions */
1431
1432static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
1433 const u32 *data, size_t datalen,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001434 const __be32 *ghash_in, __be32 *ghash_out,
Cyrille Pitchend4419542015-12-17 18:13:07 +01001435 atmel_aes_fn_t resume);
1436static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd);
1437static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd);
1438
1439static int atmel_aes_gcm_start(struct atmel_aes_dev *dd);
1440static int atmel_aes_gcm_process(struct atmel_aes_dev *dd);
1441static int atmel_aes_gcm_length(struct atmel_aes_dev *dd);
1442static int atmel_aes_gcm_data(struct atmel_aes_dev *dd);
1443static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd);
1444static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd);
1445static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd);
1446
1447static inline struct atmel_aes_gcm_ctx *
1448atmel_aes_gcm_ctx_cast(struct atmel_aes_base_ctx *ctx)
1449{
1450 return container_of(ctx, struct atmel_aes_gcm_ctx, base);
1451}
1452
1453static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
1454 const u32 *data, size_t datalen,
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001455 const __be32 *ghash_in, __be32 *ghash_out,
Cyrille Pitchend4419542015-12-17 18:13:07 +01001456 atmel_aes_fn_t resume)
1457{
1458 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1459
1460 dd->data = (u32 *)data;
1461 dd->datalen = datalen;
1462 ctx->ghash_in = ghash_in;
1463 ctx->ghash_out = ghash_out;
1464 ctx->ghash_resume = resume;
1465
1466 atmel_aes_write_ctrl(dd, false, NULL);
1467 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_ghash_init);
1468}
1469
1470static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd)
1471{
1472 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1473
1474 /* Set the data length. */
1475 atmel_aes_write(dd, AES_AADLENR, dd->total);
1476 atmel_aes_write(dd, AES_CLENR, 0);
1477
1478 /* If needed, overwrite the GCM Intermediate Hash Word Registers */
1479 if (ctx->ghash_in)
1480 atmel_aes_write_block(dd, AES_GHASHR(0), ctx->ghash_in);
1481
1482 return atmel_aes_gcm_ghash_finalize(dd);
1483}
1484
1485static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd)
1486{
1487 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1488 u32 isr;
1489
1490 /* Write data into the Input Data Registers. */
1491 while (dd->datalen > 0) {
1492 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
1493 dd->data += 4;
1494 dd->datalen -= AES_BLOCK_SIZE;
1495
1496 isr = atmel_aes_read(dd, AES_ISR);
1497 if (!(isr & AES_INT_DATARDY)) {
1498 dd->resume = atmel_aes_gcm_ghash_finalize;
1499 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
1500 return -EINPROGRESS;
1501 }
1502 }
1503
1504 /* Read the computed hash from GHASHRx. */
1505 atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash_out);
1506
1507 return ctx->ghash_resume(dd);
1508}
1509
1510
1511static int atmel_aes_gcm_start(struct atmel_aes_dev *dd)
1512{
1513 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1514 struct aead_request *req = aead_request_cast(dd->areq);
1515 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1516 struct atmel_aes_reqctx *rctx = aead_request_ctx(req);
1517 size_t ivsize = crypto_aead_ivsize(tfm);
1518 size_t datalen, padlen;
1519 const void *iv = req->iv;
1520 u8 *data = dd->buf;
1521 int err;
1522
1523 atmel_aes_set_mode(dd, rctx);
1524
1525 err = atmel_aes_hw_init(dd);
1526 if (err)
1527 return atmel_aes_complete(dd, err);
1528
Corentin LABBE219d51c2017-08-22 10:08:12 +02001529 if (likely(ivsize == GCM_AES_IV_SIZE)) {
Cyrille Pitchend4419542015-12-17 18:13:07 +01001530 memcpy(ctx->j0, iv, ivsize);
1531 ctx->j0[3] = cpu_to_be32(1);
1532 return atmel_aes_gcm_process(dd);
1533 }
1534
1535 padlen = atmel_aes_padlen(ivsize, AES_BLOCK_SIZE);
1536 datalen = ivsize + padlen + AES_BLOCK_SIZE;
1537 if (datalen > dd->buflen)
1538 return atmel_aes_complete(dd, -EINVAL);
1539
1540 memcpy(data, iv, ivsize);
1541 memset(data + ivsize, 0, padlen + sizeof(u64));
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001542 ((__be64 *)(data + datalen))[-1] = cpu_to_be64(ivsize * 8);
Cyrille Pitchend4419542015-12-17 18:13:07 +01001543
1544 return atmel_aes_gcm_ghash(dd, (const u32 *)data, datalen,
1545 NULL, ctx->j0, atmel_aes_gcm_process);
1546}
1547
1548static int atmel_aes_gcm_process(struct atmel_aes_dev *dd)
1549{
1550 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1551 struct aead_request *req = aead_request_cast(dd->areq);
1552 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1553 bool enc = atmel_aes_is_encrypt(dd);
1554 u32 authsize;
1555
1556 /* Compute text length. */
1557 authsize = crypto_aead_authsize(tfm);
1558 ctx->textlen = req->cryptlen - (enc ? 0 : authsize);
1559
1560 /*
1561 * According to tcrypt test suite, the GCM Automatic Tag Generation
1562 * fails when both the message and its associated data are empty.
1563 */
1564 if (likely(req->assoclen != 0 || ctx->textlen != 0))
1565 dd->flags |= AES_FLAGS_GTAGEN;
1566
1567 atmel_aes_write_ctrl(dd, false, NULL);
1568 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_length);
1569}
1570
1571static int atmel_aes_gcm_length(struct atmel_aes_dev *dd)
1572{
1573 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1574 struct aead_request *req = aead_request_cast(dd->areq);
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001575 __be32 j0_lsw, *j0 = ctx->j0;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001576 size_t padlen;
1577
1578 /* Write incr32(J0) into IV. */
1579 j0_lsw = j0[3];
Liu Shixinfb7c2f42020-09-14 12:17:46 +08001580 be32_add_cpu(&j0[3], 1);
Cyrille Pitchend4419542015-12-17 18:13:07 +01001581 atmel_aes_write_block(dd, AES_IVR(0), j0);
1582 j0[3] = j0_lsw;
1583
1584 /* Set aad and text lengths. */
1585 atmel_aes_write(dd, AES_AADLENR, req->assoclen);
1586 atmel_aes_write(dd, AES_CLENR, ctx->textlen);
1587
1588 /* Check whether AAD are present. */
1589 if (unlikely(req->assoclen == 0)) {
1590 dd->datalen = 0;
1591 return atmel_aes_gcm_data(dd);
1592 }
1593
1594 /* Copy assoc data and add padding. */
1595 padlen = atmel_aes_padlen(req->assoclen, AES_BLOCK_SIZE);
1596 if (unlikely(req->assoclen + padlen > dd->buflen))
1597 return atmel_aes_complete(dd, -EINVAL);
1598 sg_copy_to_buffer(req->src, sg_nents(req->src), dd->buf, req->assoclen);
1599
1600 /* Write assoc data into the Input Data register. */
1601 dd->data = (u32 *)dd->buf;
1602 dd->datalen = req->assoclen + padlen;
1603 return atmel_aes_gcm_data(dd);
1604}
1605
1606static int atmel_aes_gcm_data(struct atmel_aes_dev *dd)
1607{
1608 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1609 struct aead_request *req = aead_request_cast(dd->areq);
1610 bool use_dma = (ctx->textlen >= ATMEL_AES_DMA_THRESHOLD);
1611 struct scatterlist *src, *dst;
1612 u32 isr, mr;
1613
1614 /* Write AAD first. */
1615 while (dd->datalen > 0) {
1616 atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
1617 dd->data += 4;
1618 dd->datalen -= AES_BLOCK_SIZE;
1619
1620 isr = atmel_aes_read(dd, AES_ISR);
1621 if (!(isr & AES_INT_DATARDY)) {
1622 dd->resume = atmel_aes_gcm_data;
1623 atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
1624 return -EINPROGRESS;
1625 }
1626 }
1627
1628 /* GMAC only. */
1629 if (unlikely(ctx->textlen == 0))
1630 return atmel_aes_gcm_tag_init(dd);
1631
1632 /* Prepare src and dst scatter lists to transfer cipher/plain texts */
1633 src = scatterwalk_ffwd(ctx->src, req->src, req->assoclen);
1634 dst = ((req->src == req->dst) ? src :
1635 scatterwalk_ffwd(ctx->dst, req->dst, req->assoclen));
1636
1637 if (use_dma) {
1638 /* Update the Mode Register for DMA transfers. */
1639 mr = atmel_aes_read(dd, AES_MR);
1640 mr &= ~(AES_MR_SMOD_MASK | AES_MR_DUALBUFF);
1641 mr |= AES_MR_SMOD_IDATAR0;
1642 if (dd->caps.has_dualbuff)
1643 mr |= AES_MR_DUALBUFF;
1644 atmel_aes_write(dd, AES_MR, mr);
1645
1646 return atmel_aes_dma_start(dd, src, dst, ctx->textlen,
1647 atmel_aes_gcm_tag_init);
1648 }
1649
1650 return atmel_aes_cpu_start(dd, src, dst, ctx->textlen,
1651 atmel_aes_gcm_tag_init);
1652}
1653
1654static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd)
1655{
1656 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1657 struct aead_request *req = aead_request_cast(dd->areq);
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001658 __be64 *data = dd->buf;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001659
1660 if (likely(dd->flags & AES_FLAGS_GTAGEN)) {
1661 if (!(atmel_aes_read(dd, AES_ISR) & AES_INT_TAGRDY)) {
1662 dd->resume = atmel_aes_gcm_tag_init;
1663 atmel_aes_write(dd, AES_IER, AES_INT_TAGRDY);
1664 return -EINPROGRESS;
1665 }
1666
1667 return atmel_aes_gcm_finalize(dd);
1668 }
1669
1670 /* Read the GCM Intermediate Hash Word Registers. */
1671 atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash);
1672
1673 data[0] = cpu_to_be64(req->assoclen * 8);
1674 data[1] = cpu_to_be64(ctx->textlen * 8);
1675
1676 return atmel_aes_gcm_ghash(dd, (const u32 *)data, AES_BLOCK_SIZE,
1677 ctx->ghash, ctx->ghash, atmel_aes_gcm_tag);
1678}
1679
1680static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd)
1681{
1682 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1683 unsigned long flags;
1684
1685 /*
1686 * Change mode to CTR to complete the tag generation.
1687 * Use J0 as Initialization Vector.
1688 */
1689 flags = dd->flags;
1690 dd->flags &= ~(AES_FLAGS_OPMODE_MASK | AES_FLAGS_GTAGEN);
1691 dd->flags |= AES_FLAGS_CTR;
1692 atmel_aes_write_ctrl(dd, false, ctx->j0);
1693 dd->flags = flags;
1694
1695 atmel_aes_write_block(dd, AES_IDATAR(0), ctx->ghash);
1696 return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_finalize);
1697}
1698
1699static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd)
1700{
1701 struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
1702 struct aead_request *req = aead_request_cast(dd->areq);
1703 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1704 bool enc = atmel_aes_is_encrypt(dd);
1705 u32 offset, authsize, itag[4], *otag = ctx->tag;
1706 int err;
1707
1708 /* Read the computed tag. */
1709 if (likely(dd->flags & AES_FLAGS_GTAGEN))
1710 atmel_aes_read_block(dd, AES_TAGR(0), ctx->tag);
1711 else
1712 atmel_aes_read_block(dd, AES_ODATAR(0), ctx->tag);
1713
1714 offset = req->assoclen + ctx->textlen;
1715 authsize = crypto_aead_authsize(tfm);
1716 if (enc) {
1717 scatterwalk_map_and_copy(otag, req->dst, offset, authsize, 1);
1718 err = 0;
1719 } else {
1720 scatterwalk_map_and_copy(itag, req->src, offset, authsize, 0);
1721 err = crypto_memneq(itag, otag, authsize) ? -EBADMSG : 0;
1722 }
1723
1724 return atmel_aes_complete(dd, err);
1725}
1726
1727static int atmel_aes_gcm_crypt(struct aead_request *req,
1728 unsigned long mode)
1729{
1730 struct atmel_aes_base_ctx *ctx;
1731 struct atmel_aes_reqctx *rctx;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001732
1733 ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
1734 ctx->block_size = AES_BLOCK_SIZE;
Romain Izard91308012017-10-31 16:25:23 +01001735 ctx->is_aead = true;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001736
Cyrille Pitchend4419542015-12-17 18:13:07 +01001737 rctx = aead_request_ctx(req);
1738 rctx->mode = AES_FLAGS_GCM | mode;
1739
Tudor Ambarusec2088b2021-07-20 11:55:35 +03001740 return atmel_aes_handle_queue(ctx->dd, &req->base);
Cyrille Pitchend4419542015-12-17 18:13:07 +01001741}
1742
1743static int atmel_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
1744 unsigned int keylen)
1745{
1746 struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
1747
1748 if (keylen != AES_KEYSIZE_256 &&
1749 keylen != AES_KEYSIZE_192 &&
Eric Biggers674f3682019-12-30 21:19:36 -06001750 keylen != AES_KEYSIZE_128)
Cyrille Pitchend4419542015-12-17 18:13:07 +01001751 return -EINVAL;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001752
1753 memcpy(ctx->key, key, keylen);
1754 ctx->keylen = keylen;
1755
1756 return 0;
1757}
1758
1759static int atmel_aes_gcm_setauthsize(struct crypto_aead *tfm,
1760 unsigned int authsize)
1761{
Tudor Ambarus7db15aa2019-12-05 09:54:08 +00001762 return crypto_gcm_check_authsize(authsize);
Cyrille Pitchend4419542015-12-17 18:13:07 +01001763}
1764
1765static int atmel_aes_gcm_encrypt(struct aead_request *req)
1766{
1767 return atmel_aes_gcm_crypt(req, AES_FLAGS_ENCRYPT);
1768}
1769
1770static int atmel_aes_gcm_decrypt(struct aead_request *req)
1771{
1772 return atmel_aes_gcm_crypt(req, 0);
1773}
1774
1775static int atmel_aes_gcm_init(struct crypto_aead *tfm)
1776{
1777 struct atmel_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm);
Tudor Ambarusec2088b2021-07-20 11:55:35 +03001778 struct atmel_aes_dev *dd;
1779
1780 dd = atmel_aes_dev_alloc(&ctx->base);
1781 if (!dd)
1782 return -ENODEV;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001783
1784 crypto_aead_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
Tudor Ambarusec2088b2021-07-20 11:55:35 +03001785 ctx->base.dd = dd;
1786 ctx->base.dd->ctx = &ctx->base;
Cyrille Pitchend4419542015-12-17 18:13:07 +01001787 ctx->base.start = atmel_aes_gcm_start;
1788
1789 return 0;
1790}
1791
Cyrille Pitchend4419542015-12-17 18:13:07 +01001792static struct aead_alg aes_gcm_alg = {
1793 .setkey = atmel_aes_gcm_setkey,
1794 .setauthsize = atmel_aes_gcm_setauthsize,
1795 .encrypt = atmel_aes_gcm_encrypt,
1796 .decrypt = atmel_aes_gcm_decrypt,
1797 .init = atmel_aes_gcm_init,
Corentin LABBE219d51c2017-08-22 10:08:12 +02001798 .ivsize = GCM_AES_IV_SIZE,
Cyrille Pitchend4419542015-12-17 18:13:07 +01001799 .maxauthsize = AES_BLOCK_SIZE,
1800
1801 .base = {
1802 .cra_name = "gcm(aes)",
1803 .cra_driver_name = "atmel-gcm-aes",
Cyrille Pitchend4419542015-12-17 18:13:07 +01001804 .cra_blocksize = 1,
1805 .cra_ctxsize = sizeof(struct atmel_aes_gcm_ctx),
Cyrille Pitchend4419542015-12-17 18:13:07 +01001806 },
1807};
1808
1809
Cyrille Pitchend52db512016-10-03 14:33:16 +02001810/* xts functions */
1811
1812static inline struct atmel_aes_xts_ctx *
1813atmel_aes_xts_ctx_cast(struct atmel_aes_base_ctx *ctx)
1814{
1815 return container_of(ctx, struct atmel_aes_xts_ctx, base);
1816}
1817
1818static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd);
1819
1820static int atmel_aes_xts_start(struct atmel_aes_dev *dd)
1821{
1822 struct atmel_aes_xts_ctx *ctx = atmel_aes_xts_ctx_cast(dd->ctx);
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001823 struct skcipher_request *req = skcipher_request_cast(dd->areq);
1824 struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001825 unsigned long flags;
1826 int err;
1827
1828 atmel_aes_set_mode(dd, rctx);
1829
1830 err = atmel_aes_hw_init(dd);
1831 if (err)
1832 return atmel_aes_complete(dd, err);
1833
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001834 /* Compute the tweak value from req->iv with ecb(aes). */
Cyrille Pitchend52db512016-10-03 14:33:16 +02001835 flags = dd->flags;
1836 dd->flags &= ~AES_FLAGS_MODE_MASK;
1837 dd->flags |= (AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
1838 atmel_aes_write_ctrl_key(dd, false, NULL,
1839 ctx->key2, ctx->base.keylen);
1840 dd->flags = flags;
1841
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001842 atmel_aes_write_block(dd, AES_IDATAR(0), req->iv);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001843 return atmel_aes_wait_for_data_ready(dd, atmel_aes_xts_process_data);
1844}
1845
1846static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd)
1847{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001848 struct skcipher_request *req = skcipher_request_cast(dd->areq);
1849 bool use_dma = (req->cryptlen >= ATMEL_AES_DMA_THRESHOLD);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001850 u32 tweak[AES_BLOCK_SIZE / sizeof(u32)];
Ben Dooks (Codethink)49c4cd82019-10-16 13:26:33 +01001851 static const __le32 one[AES_BLOCK_SIZE / sizeof(u32)] = {cpu_to_le32(1), };
Cyrille Pitchend52db512016-10-03 14:33:16 +02001852 u8 *tweak_bytes = (u8 *)tweak;
1853 int i;
1854
1855 /* Read the computed ciphered tweak value. */
1856 atmel_aes_read_block(dd, AES_ODATAR(0), tweak);
1857 /*
1858 * Hardware quirk:
1859 * the order of the ciphered tweak bytes need to be reversed before
1860 * writing them into the ODATARx registers.
1861 */
Salah Triki089015d2021-07-22 18:08:27 +01001862 for (i = 0; i < AES_BLOCK_SIZE/2; ++i)
1863 swap(tweak_bytes[i], tweak_bytes[AES_BLOCK_SIZE - 1 - i]);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001864
1865 /* Process the data. */
1866 atmel_aes_write_ctrl(dd, use_dma, NULL);
1867 atmel_aes_write_block(dd, AES_TWR(0), tweak);
1868 atmel_aes_write_block(dd, AES_ALPHAR(0), one);
1869 if (use_dma)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001870 return atmel_aes_dma_start(dd, req->src, req->dst,
1871 req->cryptlen,
Cyrille Pitchend52db512016-10-03 14:33:16 +02001872 atmel_aes_transfer_complete);
1873
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001874 return atmel_aes_cpu_start(dd, req->src, req->dst, req->cryptlen,
Cyrille Pitchend52db512016-10-03 14:33:16 +02001875 atmel_aes_transfer_complete);
1876}
1877
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001878static int atmel_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key,
Cyrille Pitchend52db512016-10-03 14:33:16 +02001879 unsigned int keylen)
1880{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001881 struct atmel_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001882 int err;
1883
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001884 err = xts_check_key(crypto_skcipher_tfm(tfm), key, keylen);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001885 if (err)
1886 return err;
1887
Tudor Ambarusbf2db8e2021-07-20 11:55:34 +03001888 crypto_skcipher_clear_flags(ctx->fallback_tfm, CRYPTO_TFM_REQ_MASK);
1889 crypto_skcipher_set_flags(ctx->fallback_tfm, tfm->base.crt_flags &
1890 CRYPTO_TFM_REQ_MASK);
1891 err = crypto_skcipher_setkey(ctx->fallback_tfm, key, keylen);
1892 if (err)
1893 return err;
1894
Cyrille Pitchend52db512016-10-03 14:33:16 +02001895 memcpy(ctx->base.key, key, keylen/2);
1896 memcpy(ctx->key2, key + keylen/2, keylen/2);
1897 ctx->base.keylen = keylen/2;
1898
1899 return 0;
1900}
1901
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001902static int atmel_aes_xts_encrypt(struct skcipher_request *req)
Cyrille Pitchend52db512016-10-03 14:33:16 +02001903{
1904 return atmel_aes_crypt(req, AES_FLAGS_XTS | AES_FLAGS_ENCRYPT);
1905}
1906
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001907static int atmel_aes_xts_decrypt(struct skcipher_request *req)
Cyrille Pitchend52db512016-10-03 14:33:16 +02001908{
1909 return atmel_aes_crypt(req, AES_FLAGS_XTS);
1910}
1911
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001912static int atmel_aes_xts_init_tfm(struct crypto_skcipher *tfm)
Cyrille Pitchend52db512016-10-03 14:33:16 +02001913{
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001914 struct atmel_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Tudor Ambarusec2088b2021-07-20 11:55:35 +03001915 struct atmel_aes_dev *dd;
Tudor Ambarusbf2db8e2021-07-20 11:55:34 +03001916 const char *tfm_name = crypto_tfm_alg_name(&tfm->base);
Cyrille Pitchend52db512016-10-03 14:33:16 +02001917
Tudor Ambarusec2088b2021-07-20 11:55:35 +03001918 dd = atmel_aes_dev_alloc(&ctx->base);
1919 if (!dd)
1920 return -ENODEV;
1921
Tudor Ambarusbf2db8e2021-07-20 11:55:34 +03001922 ctx->fallback_tfm = crypto_alloc_skcipher(tfm_name, 0,
1923 CRYPTO_ALG_NEED_FALLBACK);
1924 if (IS_ERR(ctx->fallback_tfm))
1925 return PTR_ERR(ctx->fallback_tfm);
1926
1927 crypto_skcipher_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx) +
1928 crypto_skcipher_reqsize(ctx->fallback_tfm));
Tudor Ambarusec2088b2021-07-20 11:55:35 +03001929 ctx->base.dd = dd;
1930 ctx->base.dd->ctx = &ctx->base;
Cyrille Pitchend52db512016-10-03 14:33:16 +02001931 ctx->base.start = atmel_aes_xts_start;
1932
1933 return 0;
1934}
1935
Tudor Ambarusbf2db8e2021-07-20 11:55:34 +03001936static void atmel_aes_xts_exit_tfm(struct crypto_skcipher *tfm)
1937{
1938 struct atmel_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
1939
1940 crypto_free_skcipher(ctx->fallback_tfm);
1941}
1942
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001943static struct skcipher_alg aes_xts_alg = {
1944 .base.cra_name = "xts(aes)",
1945 .base.cra_driver_name = "atmel-xts-aes",
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001946 .base.cra_blocksize = AES_BLOCK_SIZE,
1947 .base.cra_ctxsize = sizeof(struct atmel_aes_xts_ctx),
Tudor Ambarusbf2db8e2021-07-20 11:55:34 +03001948 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01001949
1950 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1951 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1952 .ivsize = AES_BLOCK_SIZE,
1953 .setkey = atmel_aes_xts_setkey,
1954 .encrypt = atmel_aes_xts_encrypt,
1955 .decrypt = atmel_aes_xts_decrypt,
1956 .init = atmel_aes_xts_init_tfm,
Tudor Ambarusbf2db8e2021-07-20 11:55:34 +03001957 .exit = atmel_aes_xts_exit_tfm,
Cyrille Pitchend52db512016-10-03 14:33:16 +02001958};
1959
Herbert Xu1520c722019-10-28 15:39:07 +08001960#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01001961/* authenc aead functions */
1962
1963static int atmel_aes_authenc_start(struct atmel_aes_dev *dd);
1964static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
1965 bool is_async);
1966static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
1967 bool is_async);
1968static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd);
1969static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
1970 bool is_async);
1971
1972static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err)
1973{
1974 struct aead_request *req = aead_request_cast(dd->areq);
1975 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
1976
1977 if (err && (dd->flags & AES_FLAGS_OWN_SHA))
1978 atmel_sha_authenc_abort(&rctx->auth_req);
1979 dd->flags &= ~AES_FLAGS_OWN_SHA;
1980}
1981
1982static int atmel_aes_authenc_start(struct atmel_aes_dev *dd)
1983{
1984 struct aead_request *req = aead_request_cast(dd->areq);
1985 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
1986 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1987 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
1988 int err;
1989
1990 atmel_aes_set_mode(dd, &rctx->base);
1991
1992 err = atmel_aes_hw_init(dd);
1993 if (err)
1994 return atmel_aes_complete(dd, err);
1995
1996 return atmel_sha_authenc_schedule(&rctx->auth_req, ctx->auth,
1997 atmel_aes_authenc_init, dd);
1998}
1999
2000static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
2001 bool is_async)
2002{
2003 struct aead_request *req = aead_request_cast(dd->areq);
2004 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2005
2006 if (is_async)
2007 dd->is_async = true;
2008 if (err)
2009 return atmel_aes_complete(dd, err);
2010
2011 /* If here, we've got the ownership of the SHA device. */
2012 dd->flags |= AES_FLAGS_OWN_SHA;
2013
2014 /* Configure the SHA device. */
2015 return atmel_sha_authenc_init(&rctx->auth_req,
2016 req->src, req->assoclen,
2017 rctx->textlen,
2018 atmel_aes_authenc_transfer, dd);
2019}
2020
2021static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
2022 bool is_async)
2023{
2024 struct aead_request *req = aead_request_cast(dd->areq);
2025 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2026 bool enc = atmel_aes_is_encrypt(dd);
2027 struct scatterlist *src, *dst;
Herbert Xu427e6e32019-10-28 15:45:02 +08002028 __be32 iv[AES_BLOCK_SIZE / sizeof(u32)];
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002029 u32 emr;
2030
2031 if (is_async)
2032 dd->is_async = true;
2033 if (err)
2034 return atmel_aes_complete(dd, err);
2035
2036 /* Prepare src and dst scatter-lists to transfer cipher/plain texts. */
2037 src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
2038 dst = src;
2039
2040 if (req->src != req->dst)
2041 dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
2042
2043 /* Configure the AES device. */
2044 memcpy(iv, req->iv, sizeof(iv));
2045
2046 /*
2047 * Here we always set the 2nd parameter of atmel_aes_write_ctrl() to
2048 * 'true' even if the data transfer is actually performed by the CPU (so
2049 * not by the DMA) because we must force the AES_MR_SMOD bitfield to the
2050 * value AES_MR_SMOD_IDATAR0. Indeed, both AES_MR_SMOD and SHA_MR_SMOD
2051 * must be set to *_MR_SMOD_IDATAR0.
2052 */
2053 atmel_aes_write_ctrl(dd, true, iv);
2054 emr = AES_EMR_PLIPEN;
2055 if (!enc)
2056 emr |= AES_EMR_PLIPD;
2057 atmel_aes_write(dd, AES_EMR, emr);
2058
2059 /* Transfer data. */
2060 return atmel_aes_dma_start(dd, src, dst, rctx->textlen,
2061 atmel_aes_authenc_digest);
2062}
2063
2064static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd)
2065{
2066 struct aead_request *req = aead_request_cast(dd->areq);
2067 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2068
2069 /* atmel_sha_authenc_final() releases the SHA device. */
2070 dd->flags &= ~AES_FLAGS_OWN_SHA;
2071 return atmel_sha_authenc_final(&rctx->auth_req,
2072 rctx->digest, sizeof(rctx->digest),
2073 atmel_aes_authenc_final, dd);
2074}
2075
2076static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
2077 bool is_async)
2078{
2079 struct aead_request *req = aead_request_cast(dd->areq);
2080 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2081 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2082 bool enc = atmel_aes_is_encrypt(dd);
2083 u32 idigest[SHA512_DIGEST_SIZE / sizeof(u32)], *odigest = rctx->digest;
2084 u32 offs, authsize;
2085
2086 if (is_async)
2087 dd->is_async = true;
2088 if (err)
2089 goto complete;
2090
2091 offs = req->assoclen + rctx->textlen;
2092 authsize = crypto_aead_authsize(tfm);
2093 if (enc) {
2094 scatterwalk_map_and_copy(odigest, req->dst, offs, authsize, 1);
2095 } else {
2096 scatterwalk_map_and_copy(idigest, req->src, offs, authsize, 0);
2097 if (crypto_memneq(idigest, odigest, authsize))
2098 err = -EBADMSG;
2099 }
2100
2101complete:
2102 return atmel_aes_complete(dd, err);
2103}
2104
2105static int atmel_aes_authenc_setkey(struct crypto_aead *tfm, const u8 *key,
2106 unsigned int keylen)
2107{
2108 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2109 struct crypto_authenc_keys keys;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002110 int err;
2111
2112 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
2113 goto badkey;
2114
2115 if (keys.enckeylen > sizeof(ctx->base.key))
2116 goto badkey;
2117
2118 /* Save auth key. */
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002119 err = atmel_sha_authenc_setkey(ctx->auth,
2120 keys.authkey, keys.authkeylen,
Eric Biggersaf5034e2019-12-30 21:19:38 -06002121 crypto_aead_get_flags(tfm));
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002122 if (err) {
2123 memzero_explicit(&keys, sizeof(keys));
2124 return err;
2125 }
2126
2127 /* Save enc key. */
2128 ctx->base.keylen = keys.enckeylen;
2129 memcpy(ctx->base.key, keys.enckey, keys.enckeylen);
2130
2131 memzero_explicit(&keys, sizeof(keys));
2132 return 0;
2133
2134badkey:
Antoine Tenart5d804a512018-02-23 10:01:40 +01002135 memzero_explicit(&keys, sizeof(keys));
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002136 return -EINVAL;
2137}
2138
2139static int atmel_aes_authenc_init_tfm(struct crypto_aead *tfm,
2140 unsigned long auth_mode)
2141{
2142 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2143 unsigned int auth_reqsize = atmel_sha_authenc_get_reqsize();
Tudor Ambarusec2088b2021-07-20 11:55:35 +03002144 struct atmel_aes_dev *dd;
2145
2146 dd = atmel_aes_dev_alloc(&ctx->base);
2147 if (!dd)
2148 return -ENODEV;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002149
2150 ctx->auth = atmel_sha_authenc_spawn(auth_mode);
2151 if (IS_ERR(ctx->auth))
2152 return PTR_ERR(ctx->auth);
2153
2154 crypto_aead_set_reqsize(tfm, (sizeof(struct atmel_aes_authenc_reqctx) +
2155 auth_reqsize));
Tudor Ambarusec2088b2021-07-20 11:55:35 +03002156 ctx->base.dd = dd;
2157 ctx->base.dd->ctx = &ctx->base;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002158 ctx->base.start = atmel_aes_authenc_start;
2159
2160 return 0;
2161}
2162
2163static int atmel_aes_authenc_hmac_sha1_init_tfm(struct crypto_aead *tfm)
2164{
2165 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA1);
2166}
2167
2168static int atmel_aes_authenc_hmac_sha224_init_tfm(struct crypto_aead *tfm)
2169{
2170 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA224);
2171}
2172
2173static int atmel_aes_authenc_hmac_sha256_init_tfm(struct crypto_aead *tfm)
2174{
2175 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA256);
2176}
2177
2178static int atmel_aes_authenc_hmac_sha384_init_tfm(struct crypto_aead *tfm)
2179{
2180 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA384);
2181}
2182
2183static int atmel_aes_authenc_hmac_sha512_init_tfm(struct crypto_aead *tfm)
2184{
2185 return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA512);
2186}
2187
2188static void atmel_aes_authenc_exit_tfm(struct crypto_aead *tfm)
2189{
2190 struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
2191
2192 atmel_sha_authenc_free(ctx->auth);
2193}
2194
2195static int atmel_aes_authenc_crypt(struct aead_request *req,
2196 unsigned long mode)
2197{
2198 struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
2199 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2200 struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
2201 u32 authsize = crypto_aead_authsize(tfm);
2202 bool enc = (mode & AES_FLAGS_ENCRYPT);
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002203
2204 /* Compute text length. */
2205 if (!enc && req->cryptlen < authsize)
2206 return -EINVAL;
2207 rctx->textlen = req->cryptlen - (enc ? 0 : authsize);
2208
2209 /*
2210 * Currently, empty messages are not supported yet:
2211 * the SHA auto-padding can be used only on non-empty messages.
2212 * Hence a special case needs to be implemented for empty message.
2213 */
2214 if (!rctx->textlen && !req->assoclen)
2215 return -EINVAL;
2216
2217 rctx->base.mode = mode;
2218 ctx->block_size = AES_BLOCK_SIZE;
Romain Izard91308012017-10-31 16:25:23 +01002219 ctx->is_aead = true;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002220
Tudor Ambarusec2088b2021-07-20 11:55:35 +03002221 return atmel_aes_handle_queue(ctx->dd, &req->base);
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002222}
2223
2224static int atmel_aes_authenc_cbc_aes_encrypt(struct aead_request *req)
2225{
2226 return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
2227}
2228
2229static int atmel_aes_authenc_cbc_aes_decrypt(struct aead_request *req)
2230{
2231 return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC);
2232}
2233
2234static struct aead_alg aes_authenc_algs[] = {
2235{
2236 .setkey = atmel_aes_authenc_setkey,
2237 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2238 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2239 .init = atmel_aes_authenc_hmac_sha1_init_tfm,
2240 .exit = atmel_aes_authenc_exit_tfm,
2241 .ivsize = AES_BLOCK_SIZE,
2242 .maxauthsize = SHA1_DIGEST_SIZE,
2243
2244 .base = {
2245 .cra_name = "authenc(hmac(sha1),cbc(aes))",
2246 .cra_driver_name = "atmel-authenc-hmac-sha1-cbc-aes",
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002247 .cra_blocksize = AES_BLOCK_SIZE,
2248 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002249 },
2250},
2251{
2252 .setkey = atmel_aes_authenc_setkey,
2253 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2254 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2255 .init = atmel_aes_authenc_hmac_sha224_init_tfm,
2256 .exit = atmel_aes_authenc_exit_tfm,
2257 .ivsize = AES_BLOCK_SIZE,
2258 .maxauthsize = SHA224_DIGEST_SIZE,
2259
2260 .base = {
2261 .cra_name = "authenc(hmac(sha224),cbc(aes))",
2262 .cra_driver_name = "atmel-authenc-hmac-sha224-cbc-aes",
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002263 .cra_blocksize = AES_BLOCK_SIZE,
2264 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002265 },
2266},
2267{
2268 .setkey = atmel_aes_authenc_setkey,
2269 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2270 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2271 .init = atmel_aes_authenc_hmac_sha256_init_tfm,
2272 .exit = atmel_aes_authenc_exit_tfm,
2273 .ivsize = AES_BLOCK_SIZE,
2274 .maxauthsize = SHA256_DIGEST_SIZE,
2275
2276 .base = {
2277 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2278 .cra_driver_name = "atmel-authenc-hmac-sha256-cbc-aes",
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002279 .cra_blocksize = AES_BLOCK_SIZE,
2280 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002281 },
2282},
2283{
2284 .setkey = atmel_aes_authenc_setkey,
2285 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2286 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2287 .init = atmel_aes_authenc_hmac_sha384_init_tfm,
2288 .exit = atmel_aes_authenc_exit_tfm,
2289 .ivsize = AES_BLOCK_SIZE,
2290 .maxauthsize = SHA384_DIGEST_SIZE,
2291
2292 .base = {
2293 .cra_name = "authenc(hmac(sha384),cbc(aes))",
2294 .cra_driver_name = "atmel-authenc-hmac-sha384-cbc-aes",
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002295 .cra_blocksize = AES_BLOCK_SIZE,
2296 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002297 },
2298},
2299{
2300 .setkey = atmel_aes_authenc_setkey,
2301 .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
2302 .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
2303 .init = atmel_aes_authenc_hmac_sha512_init_tfm,
2304 .exit = atmel_aes_authenc_exit_tfm,
2305 .ivsize = AES_BLOCK_SIZE,
2306 .maxauthsize = SHA512_DIGEST_SIZE,
2307
2308 .base = {
2309 .cra_name = "authenc(hmac(sha512),cbc(aes))",
2310 .cra_driver_name = "atmel-authenc-hmac-sha512-cbc-aes",
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002311 .cra_blocksize = AES_BLOCK_SIZE,
2312 .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002313 },
2314},
2315};
2316#endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
Cyrille Pitchend52db512016-10-03 14:33:16 +02002317
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002318/* Probe functions */
2319
2320static int atmel_aes_buff_init(struct atmel_aes_dev *dd)
2321{
2322 dd->buf = (void *)__get_free_pages(GFP_KERNEL, ATMEL_AES_BUFFER_ORDER);
2323 dd->buflen = ATMEL_AES_BUFFER_SIZE;
2324 dd->buflen &= ~(AES_BLOCK_SIZE - 1);
2325
2326 if (!dd->buf) {
2327 dev_err(dd->dev, "unable to alloc pages.\n");
2328 return -ENOMEM;
2329 }
2330
2331 return 0;
2332}
2333
2334static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd)
2335{
2336 free_page((unsigned long)dd->buf);
2337}
2338
Tudor Ambarus827a98d2019-12-13 09:54:49 +00002339static int atmel_aes_dma_init(struct atmel_aes_dev *dd)
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002340{
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002341 int ret;
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002342
2343 /* Try to grab 2 DMA channels */
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002344 dd->src.chan = dma_request_chan(dd->dev, "tx");
2345 if (IS_ERR(dd->src.chan)) {
2346 ret = PTR_ERR(dd->src.chan);
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002347 goto err_dma_in;
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002348 }
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002349
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002350 dd->dst.chan = dma_request_chan(dd->dev, "rx");
2351 if (IS_ERR(dd->dst.chan)) {
2352 ret = PTR_ERR(dd->dst.chan);
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002353 goto err_dma_out;
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002354 }
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002355
2356 return 0;
2357
2358err_dma_out:
2359 dma_release_channel(dd->src.chan);
2360err_dma_in:
Tudor Ambaruse9ce6ae2019-12-13 09:54:54 +00002361 dev_err(dd->dev, "no DMA channel available\n");
Peter Ujfalusi62f72cb2019-11-21 12:16:00 +02002362 return ret;
Cyrille Pitchene37a7e52015-12-17 18:13:03 +01002363}
2364
2365static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd)
2366{
2367 dma_release_channel(dd->dst.chan);
2368 dma_release_channel(dd->src.chan);
2369}
2370
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002371static void atmel_aes_queue_task(unsigned long data)
2372{
2373 struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
2374
2375 atmel_aes_handle_queue(dd, NULL);
2376}
2377
2378static void atmel_aes_done_task(unsigned long data)
2379{
Cyrille Pitchenafbac172015-12-17 18:13:02 +01002380 struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
Cyrille Pitchen10f12c12015-12-17 17:48:42 +01002381
2382 dd->is_async = true;
2383 (void)dd->resume(dd);
2384}
2385
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002386static irqreturn_t atmel_aes_irq(int irq, void *dev_id)
2387{
2388 struct atmel_aes_dev *aes_dd = dev_id;
2389 u32 reg;
2390
2391 reg = atmel_aes_read(aes_dd, AES_ISR);
2392 if (reg & atmel_aes_read(aes_dd, AES_IMR)) {
2393 atmel_aes_write(aes_dd, AES_IDR, reg);
2394 if (AES_FLAGS_BUSY & aes_dd->flags)
2395 tasklet_schedule(&aes_dd->done_task);
2396 else
2397 dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n");
2398 return IRQ_HANDLED;
2399 }
2400
2401 return IRQ_NONE;
2402}
2403
2404static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd)
2405{
2406 int i;
2407
Herbert Xu1520c722019-10-28 15:39:07 +08002408#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002409 if (dd->caps.has_authenc)
2410 for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++)
2411 crypto_unregister_aead(&aes_authenc_algs[i]);
2412#endif
2413
Cyrille Pitchend52db512016-10-03 14:33:16 +02002414 if (dd->caps.has_xts)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002415 crypto_unregister_skcipher(&aes_xts_alg);
Cyrille Pitchend52db512016-10-03 14:33:16 +02002416
Cyrille Pitchend4419542015-12-17 18:13:07 +01002417 if (dd->caps.has_gcm)
2418 crypto_unregister_aead(&aes_gcm_alg);
2419
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002420 if (dd->caps.has_cfb64)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002421 crypto_unregister_skcipher(&aes_cfb64_alg);
Cyrille Pitchen924a8bc2015-12-17 17:48:35 +01002422
2423 for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002424 crypto_unregister_skcipher(&aes_algs[i]);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002425}
2426
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002427static void atmel_aes_crypto_alg_init(struct crypto_alg *alg)
2428{
Tudor Ambarusbf2db8e2021-07-20 11:55:34 +03002429 alg->cra_flags |= CRYPTO_ALG_ASYNC;
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002430 alg->cra_alignmask = 0xf;
2431 alg->cra_priority = ATMEL_AES_PRIORITY;
2432 alg->cra_module = THIS_MODULE;
2433}
2434
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002435static int atmel_aes_register_algs(struct atmel_aes_dev *dd)
2436{
2437 int err, i, j;
2438
2439 for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002440 atmel_aes_crypto_alg_init(&aes_algs[i].base);
2441
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002442 err = crypto_register_skcipher(&aes_algs[i]);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002443 if (err)
2444 goto err_aes_algs;
2445 }
2446
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002447 if (dd->caps.has_cfb64) {
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002448 atmel_aes_crypto_alg_init(&aes_cfb64_alg.base);
2449
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002450 err = crypto_register_skcipher(&aes_cfb64_alg);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002451 if (err)
2452 goto err_aes_cfb64_alg;
2453 }
2454
Cyrille Pitchend4419542015-12-17 18:13:07 +01002455 if (dd->caps.has_gcm) {
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002456 atmel_aes_crypto_alg_init(&aes_gcm_alg.base);
2457
Cyrille Pitchend4419542015-12-17 18:13:07 +01002458 err = crypto_register_aead(&aes_gcm_alg);
2459 if (err)
2460 goto err_aes_gcm_alg;
2461 }
2462
Cyrille Pitchend52db512016-10-03 14:33:16 +02002463 if (dd->caps.has_xts) {
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002464 atmel_aes_crypto_alg_init(&aes_xts_alg.base);
2465
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002466 err = crypto_register_skcipher(&aes_xts_alg);
Cyrille Pitchend52db512016-10-03 14:33:16 +02002467 if (err)
2468 goto err_aes_xts_alg;
2469 }
2470
Herbert Xu1520c722019-10-28 15:39:07 +08002471#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002472 if (dd->caps.has_authenc) {
2473 for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++) {
Tudor Ambarusaebe5bd2019-12-05 13:48:39 +00002474 atmel_aes_crypto_alg_init(&aes_authenc_algs[i].base);
2475
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002476 err = crypto_register_aead(&aes_authenc_algs[i]);
2477 if (err)
2478 goto err_aes_authenc_alg;
2479 }
2480 }
2481#endif
2482
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002483 return 0;
2484
Herbert Xu1520c722019-10-28 15:39:07 +08002485#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002486 /* i = ARRAY_SIZE(aes_authenc_algs); */
2487err_aes_authenc_alg:
2488 for (j = 0; j < i; j++)
2489 crypto_unregister_aead(&aes_authenc_algs[j]);
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002490 crypto_unregister_skcipher(&aes_xts_alg);
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002491#endif
Cyrille Pitchend52db512016-10-03 14:33:16 +02002492err_aes_xts_alg:
2493 crypto_unregister_aead(&aes_gcm_alg);
Cyrille Pitchend4419542015-12-17 18:13:07 +01002494err_aes_gcm_alg:
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002495 crypto_unregister_skcipher(&aes_cfb64_alg);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002496err_aes_cfb64_alg:
2497 i = ARRAY_SIZE(aes_algs);
2498err_aes_algs:
2499 for (j = 0; j < i; j++)
Ard Biesheuvel7ada42d2019-11-09 18:09:33 +01002500 crypto_unregister_skcipher(&aes_algs[j]);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002501
2502 return err;
2503}
2504
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002505static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
2506{
2507 dd->caps.has_dualbuff = 0;
2508 dd->caps.has_cfb64 = 0;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002509 dd->caps.has_gcm = 0;
Cyrille Pitchend52db512016-10-03 14:33:16 +02002510 dd->caps.has_xts = 0;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002511 dd->caps.has_authenc = 0;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002512 dd->caps.max_burst_size = 1;
2513
2514 /* keep only major version number */
2515 switch (dd->hw_version & 0xff0) {
Leilei Zhao973e2092015-12-17 17:48:32 +01002516 case 0x500:
2517 dd->caps.has_dualbuff = 1;
2518 dd->caps.has_cfb64 = 1;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002519 dd->caps.has_gcm = 1;
Cyrille Pitchend52db512016-10-03 14:33:16 +02002520 dd->caps.has_xts = 1;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002521 dd->caps.has_authenc = 1;
Leilei Zhao973e2092015-12-17 17:48:32 +01002522 dd->caps.max_burst_size = 4;
2523 break;
Leilei Zhaocf1f0d12015-04-07 17:45:02 +08002524 case 0x200:
2525 dd->caps.has_dualbuff = 1;
2526 dd->caps.has_cfb64 = 1;
Cyrille Pitchend4419542015-12-17 18:13:07 +01002527 dd->caps.has_gcm = 1;
Leilei Zhaocf1f0d12015-04-07 17:45:02 +08002528 dd->caps.max_burst_size = 4;
2529 break;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002530 case 0x130:
2531 dd->caps.has_dualbuff = 1;
2532 dd->caps.has_cfb64 = 1;
2533 dd->caps.max_burst_size = 4;
2534 break;
2535 case 0x120:
2536 break;
2537 default:
2538 dev_warn(dd->dev,
2539 "Unmanaged aes version, set minimum capabilities\n");
2540 break;
2541 }
2542}
2543
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002544#if defined(CONFIG_OF)
2545static const struct of_device_id atmel_aes_dt_ids[] = {
2546 { .compatible = "atmel,at91sam9g46-aes" },
2547 { /* sentinel */ }
2548};
2549MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids);
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002550#endif
2551
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002552static int atmel_aes_probe(struct platform_device *pdev)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002553{
2554 struct atmel_aes_dev *aes_dd;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002555 struct device *dev = &pdev->dev;
2556 struct resource *aes_res;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002557 int err;
2558
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002559 aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL);
Tudor Ambarusc9063a02019-12-05 09:53:51 +00002560 if (!aes_dd)
2561 return -ENOMEM;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002562
2563 aes_dd->dev = dev;
2564
2565 platform_set_drvdata(pdev, aes_dd);
2566
2567 INIT_LIST_HEAD(&aes_dd->list);
Leilei Zhao8a10eb82015-04-07 17:45:09 +08002568 spin_lock_init(&aes_dd->lock);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002569
2570 tasklet_init(&aes_dd->done_task, atmel_aes_done_task,
2571 (unsigned long)aes_dd);
2572 tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task,
2573 (unsigned long)aes_dd);
2574
2575 crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH);
2576
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002577 /* Get the base address */
2578 aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2579 if (!aes_res) {
2580 dev_err(dev, "no MEM resource info\n");
2581 err = -ENODEV;
Tudor Ambaruse7836512019-12-05 09:53:53 +00002582 goto err_tasklet_kill;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002583 }
2584 aes_dd->phys_base = aes_res->start;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002585
2586 /* Get the IRQ */
2587 aes_dd->irq = platform_get_irq(pdev, 0);
2588 if (aes_dd->irq < 0) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002589 err = aes_dd->irq;
Tudor Ambaruse7836512019-12-05 09:53:53 +00002590 goto err_tasklet_kill;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002591 }
2592
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002593 err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq,
2594 IRQF_SHARED, "atmel-aes", aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002595 if (err) {
2596 dev_err(dev, "unable to request aes irq.\n");
Tudor Ambaruse7836512019-12-05 09:53:53 +00002597 goto err_tasklet_kill;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002598 }
2599
2600 /* Initializing the clock */
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002601 aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002602 if (IS_ERR(aes_dd->iclk)) {
Colin Ian Kingbe208352015-02-28 20:40:10 +00002603 dev_err(dev, "clock initialization failed.\n");
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002604 err = PTR_ERR(aes_dd->iclk);
Tudor Ambaruse7836512019-12-05 09:53:53 +00002605 goto err_tasklet_kill;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002606 }
2607
LABBE Corentinb0e8b342015-10-12 19:47:03 +02002608 aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res);
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002609 if (IS_ERR(aes_dd->io_base)) {
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002610 dev_err(dev, "can't ioremap\n");
Vladimir Zapolskiy9b52d552016-03-06 03:21:52 +02002611 err = PTR_ERR(aes_dd->io_base);
Tudor Ambaruse7836512019-12-05 09:53:53 +00002612 goto err_tasklet_kill;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002613 }
2614
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002615 err = clk_prepare(aes_dd->iclk);
Cyrille Pitchenaab0a392015-12-17 17:48:37 +01002616 if (err)
Tudor Ambaruse7836512019-12-05 09:53:53 +00002617 goto err_tasklet_kill;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002618
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002619 err = atmel_aes_hw_version_init(aes_dd);
2620 if (err)
Tudor Ambaruse7836512019-12-05 09:53:53 +00002621 goto err_iclk_unprepare;
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002622
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002623 atmel_aes_get_cap(aes_dd);
2624
Herbert Xu1520c722019-10-28 15:39:07 +08002625#if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002626 if (aes_dd->caps.has_authenc && !atmel_sha_authenc_is_ready()) {
2627 err = -EPROBE_DEFER;
Tudor Ambaruse7836512019-12-05 09:53:53 +00002628 goto err_iclk_unprepare;
Cyrille Pitchen89a82ef2017-01-26 17:07:56 +01002629 }
2630#endif
2631
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002632 err = atmel_aes_buff_init(aes_dd);
2633 if (err)
Tudor Ambaruse7836512019-12-05 09:53:53 +00002634 goto err_iclk_unprepare;
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002635
Tudor Ambarus827a98d2019-12-13 09:54:49 +00002636 err = atmel_aes_dma_init(aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002637 if (err)
Tudor Ambaruse7836512019-12-05 09:53:53 +00002638 goto err_buff_cleanup;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002639
2640 spin_lock(&atmel_aes.lock);
2641 list_add_tail(&aes_dd->list, &atmel_aes.dev_list);
2642 spin_unlock(&atmel_aes.lock);
2643
2644 err = atmel_aes_register_algs(aes_dd);
2645 if (err)
2646 goto err_algs;
2647
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002648 dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n",
Cyrille Pitchenbbe628e2015-12-17 18:13:00 +01002649 dma_chan_name(aes_dd->src.chan),
2650 dma_chan_name(aes_dd->dst.chan));
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002651
2652 return 0;
2653
2654err_algs:
2655 spin_lock(&atmel_aes.lock);
2656 list_del(&aes_dd->list);
2657 spin_unlock(&atmel_aes.lock);
2658 atmel_aes_dma_cleanup(aes_dd);
Tudor Ambaruse7836512019-12-05 09:53:53 +00002659err_buff_cleanup:
Nicolas Royercadc4ab2013-02-20 17:10:24 +01002660 atmel_aes_buff_cleanup(aes_dd);
Tudor Ambaruse7836512019-12-05 09:53:53 +00002661err_iclk_unprepare:
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002662 clk_unprepare(aes_dd->iclk);
Tudor Ambaruse7836512019-12-05 09:53:53 +00002663err_tasklet_kill:
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002664 tasklet_kill(&aes_dd->done_task);
2665 tasklet_kill(&aes_dd->queue_task);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002666
2667 return err;
2668}
2669
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002670static int atmel_aes_remove(struct platform_device *pdev)
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002671{
Wei Yongjunfc783342016-10-24 14:51:22 +00002672 struct atmel_aes_dev *aes_dd;
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002673
2674 aes_dd = platform_get_drvdata(pdev);
2675 if (!aes_dd)
2676 return -ENODEV;
2677 spin_lock(&atmel_aes.lock);
2678 list_del(&aes_dd->list);
2679 spin_unlock(&atmel_aes.lock);
2680
2681 atmel_aes_unregister_algs(aes_dd);
2682
2683 tasklet_kill(&aes_dd->done_task);
2684 tasklet_kill(&aes_dd->queue_task);
2685
2686 atmel_aes_dma_cleanup(aes_dd);
Cyrille Pitchen2a377822015-12-17 17:48:46 +01002687 atmel_aes_buff_cleanup(aes_dd);
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002688
Cyrille Pitchen49a20452016-01-29 17:53:33 +01002689 clk_unprepare(aes_dd->iclk);
2690
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002691 return 0;
2692}
2693
2694static struct platform_driver atmel_aes_driver = {
2695 .probe = atmel_aes_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002696 .remove = atmel_aes_remove,
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002697 .driver = {
2698 .name = "atmel_aes",
Nicolas Ferrebe943c72013-10-14 17:52:38 +02002699 .of_match_table = of_match_ptr(atmel_aes_dt_ids),
Nicolas Royerbd3c7b52012-07-01 19:19:44 +02002700 },
2701};
2702
2703module_platform_driver(atmel_aes_driver);
2704
2705MODULE_DESCRIPTION("Atmel AES hw acceleration support.");
2706MODULE_LICENSE("GPL v2");
2707MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");