Tudor Ambarus | 820684c | 2018-08-21 16:36:09 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2 | /* |
| 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 Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 10 | * 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 Ambarus | b46f36c | 2020-01-15 12:53:53 +0000 | [diff] [blame] | 24 | #include <linux/dmaengine.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 25 | #include <linux/init.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/interrupt.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 28 | #include <linux/irq.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 29 | #include <linux/scatterlist.h> |
| 30 | #include <linux/dma-mapping.h> |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 31 | #include <linux/of_device.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 32 | #include <linux/delay.h> |
| 33 | #include <linux/crypto.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 34 | #include <crypto/scatterwalk.h> |
| 35 | #include <crypto/algapi.h> |
| 36 | #include <crypto/aes.h> |
Corentin LABBE | 219d51c | 2017-08-22 10:08:12 +0200 | [diff] [blame] | 37 | #include <crypto/gcm.h> |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 38 | #include <crypto/xts.h> |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 39 | #include <crypto/internal/aead.h> |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 40 | #include <crypto/internal/skcipher.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 41 | #include "atmel-aes-regs.h" |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 42 | #include "atmel-authenc.h" |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 43 | |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 44 | #define ATMEL_AES_PRIORITY 300 |
| 45 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 46 | #define ATMEL_AES_BUFFER_ORDER 2 |
| 47 | #define ATMEL_AES_BUFFER_SIZE (PAGE_SIZE << ATMEL_AES_BUFFER_ORDER) |
| 48 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 49 | #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 Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 54 | #define SIZE_IN_WORDS(x) ((x) >> 2) |
| 55 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 56 | /* AES flags */ |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 57 | /* Reserve bits [18:16] [14:12] [1:0] for mode (same as for AES_MR) */ |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 58 | #define AES_FLAGS_ENCRYPT AES_MR_CYPHER_ENC |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 59 | #define AES_FLAGS_GTAGEN AES_MR_GTAGEN |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 60 | #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 Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 70 | #define AES_FLAGS_GCM AES_MR_OPMOD_GCM |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 71 | #define AES_FLAGS_XTS AES_MR_OPMOD_XTS |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 72 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 73 | #define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \ |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 74 | AES_FLAGS_ENCRYPT | \ |
| 75 | AES_FLAGS_GTAGEN) |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 76 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 77 | #define AES_FLAGS_BUSY BIT(3) |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 78 | #define AES_FLAGS_DUMP_REG BIT(4) |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 79 | #define AES_FLAGS_OWN_SHA BIT(5) |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 80 | |
Romain Izard | 7a373fd | 2017-10-31 16:25:24 +0100 | [diff] [blame] | 81 | #define AES_FLAGS_PERSISTENT AES_FLAGS_BUSY |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 82 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 83 | #define ATMEL_AES_QUEUE_LENGTH 50 |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 84 | |
Cyrille Pitchen | 129f8bb | 2015-12-17 18:13:06 +0100 | [diff] [blame] | 85 | #define ATMEL_AES_DMA_THRESHOLD 256 |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 86 | |
| 87 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 88 | struct atmel_aes_caps { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 89 | bool has_dualbuff; |
| 90 | bool has_cfb64; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 91 | bool has_gcm; |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 92 | bool has_xts; |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 93 | bool has_authenc; |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 94 | u32 max_burst_size; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 97 | struct atmel_aes_dev; |
| 98 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 99 | |
| 100 | typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *); |
| 101 | |
| 102 | |
| 103 | struct atmel_aes_base_ctx { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 104 | 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 Izard | 9130801 | 2017-10-31 16:25:23 +0100 | [diff] [blame] | 109 | bool is_aead; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 112 | struct atmel_aes_ctx { |
| 113 | struct atmel_aes_base_ctx base; |
| 114 | }; |
| 115 | |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 116 | struct atmel_aes_ctr_ctx { |
| 117 | struct atmel_aes_base_ctx base; |
| 118 | |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 119 | __be32 iv[AES_BLOCK_SIZE / sizeof(u32)]; |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 120 | size_t offset; |
| 121 | struct scatterlist src[2]; |
| 122 | struct scatterlist dst[2]; |
Tudor Ambarus | 3907ccf | 2019-12-13 14:45:44 +0000 | [diff] [blame] | 123 | u32 blocks; |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 124 | }; |
| 125 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 126 | struct 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) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 132 | __be32 j0[AES_BLOCK_SIZE / sizeof(u32)]; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 133 | u32 tag[AES_BLOCK_SIZE / sizeof(u32)]; |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 134 | __be32 ghash[AES_BLOCK_SIZE / sizeof(u32)]; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 135 | size_t textlen; |
| 136 | |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 137 | const __be32 *ghash_in; |
| 138 | __be32 *ghash_out; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 139 | atmel_aes_fn_t ghash_resume; |
| 140 | }; |
| 141 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 142 | struct atmel_aes_xts_ctx { |
| 143 | struct atmel_aes_base_ctx base; |
| 144 | |
| 145 | u32 key2[AES_KEYSIZE_256 / sizeof(u32)]; |
| 146 | }; |
| 147 | |
Herbert Xu | 1520c72 | 2019-10-28 15:39:07 +0800 | [diff] [blame] | 148 | #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC) |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 149 | struct atmel_aes_authenc_ctx { |
| 150 | struct atmel_aes_base_ctx base; |
| 151 | struct atmel_sha_authenc_ctx *auth; |
| 152 | }; |
| 153 | #endif |
| 154 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 155 | struct atmel_aes_reqctx { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 156 | unsigned long mode; |
Tudor Ambarus | 57d8154 | 2019-11-15 13:49:09 +0000 | [diff] [blame] | 157 | u8 lastc[AES_BLOCK_SIZE]; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 158 | }; |
| 159 | |
Herbert Xu | 1520c72 | 2019-10-28 15:39:07 +0800 | [diff] [blame] | 160 | #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC) |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 161 | struct atmel_aes_authenc_reqctx { |
| 162 | struct atmel_aes_reqctx base; |
| 163 | |
| 164 | struct scatterlist src[2]; |
| 165 | struct scatterlist dst[2]; |
| 166 | size_t textlen; |
| 167 | u32 digest[SHA512_DIGEST_SIZE / sizeof(u32)]; |
| 168 | |
| 169 | /* auth_req MUST be place last. */ |
| 170 | struct ahash_request auth_req; |
| 171 | }; |
| 172 | #endif |
| 173 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 174 | struct atmel_aes_dma { |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 175 | struct dma_chan *chan; |
| 176 | struct scatterlist *sg; |
| 177 | int nents; |
| 178 | unsigned int remainder; |
| 179 | unsigned int sg_len; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | struct atmel_aes_dev { |
| 183 | struct list_head list; |
| 184 | unsigned long phys_base; |
| 185 | void __iomem *io_base; |
| 186 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 187 | struct crypto_async_request *areq; |
| 188 | struct atmel_aes_base_ctx *ctx; |
| 189 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 190 | bool is_async; |
| 191 | atmel_aes_fn_t resume; |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 192 | atmel_aes_fn_t cpu_transfer_complete; |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 193 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 194 | struct device *dev; |
| 195 | struct clk *iclk; |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 196 | int irq; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 197 | |
| 198 | unsigned long flags; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 199 | |
| 200 | spinlock_t lock; |
| 201 | struct crypto_queue queue; |
| 202 | |
| 203 | struct tasklet_struct done_task; |
| 204 | struct tasklet_struct queue_task; |
| 205 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 206 | size_t total; |
| 207 | size_t datalen; |
| 208 | u32 *data; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 209 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 210 | struct atmel_aes_dma src; |
| 211 | struct atmel_aes_dma dst; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 212 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 213 | size_t buflen; |
| 214 | void *buf; |
| 215 | struct scatterlist aligned_sg; |
| 216 | struct scatterlist *real_dst; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 217 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 218 | struct atmel_aes_caps caps; |
| 219 | |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 220 | u32 hw_version; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | struct atmel_aes_drv { |
| 224 | struct list_head dev_list; |
| 225 | spinlock_t lock; |
| 226 | }; |
| 227 | |
| 228 | static struct atmel_aes_drv atmel_aes = { |
| 229 | .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list), |
| 230 | .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock), |
| 231 | }; |
| 232 | |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 233 | #ifdef VERBOSE_DEBUG |
| 234 | static const char *atmel_aes_reg_name(u32 offset, char *tmp, size_t sz) |
| 235 | { |
| 236 | switch (offset) { |
| 237 | case AES_CR: |
| 238 | return "CR"; |
| 239 | |
| 240 | case AES_MR: |
| 241 | return "MR"; |
| 242 | |
| 243 | case AES_ISR: |
| 244 | return "ISR"; |
| 245 | |
| 246 | case AES_IMR: |
| 247 | return "IMR"; |
| 248 | |
| 249 | case AES_IER: |
| 250 | return "IER"; |
| 251 | |
| 252 | case AES_IDR: |
| 253 | return "IDR"; |
| 254 | |
| 255 | case AES_KEYWR(0): |
| 256 | case AES_KEYWR(1): |
| 257 | case AES_KEYWR(2): |
| 258 | case AES_KEYWR(3): |
| 259 | case AES_KEYWR(4): |
| 260 | case AES_KEYWR(5): |
| 261 | case AES_KEYWR(6): |
| 262 | case AES_KEYWR(7): |
| 263 | snprintf(tmp, sz, "KEYWR[%u]", (offset - AES_KEYWR(0)) >> 2); |
| 264 | break; |
| 265 | |
| 266 | case AES_IDATAR(0): |
| 267 | case AES_IDATAR(1): |
| 268 | case AES_IDATAR(2): |
| 269 | case AES_IDATAR(3): |
| 270 | snprintf(tmp, sz, "IDATAR[%u]", (offset - AES_IDATAR(0)) >> 2); |
| 271 | break; |
| 272 | |
| 273 | case AES_ODATAR(0): |
| 274 | case AES_ODATAR(1): |
| 275 | case AES_ODATAR(2): |
| 276 | case AES_ODATAR(3): |
| 277 | snprintf(tmp, sz, "ODATAR[%u]", (offset - AES_ODATAR(0)) >> 2); |
| 278 | break; |
| 279 | |
| 280 | case AES_IVR(0): |
| 281 | case AES_IVR(1): |
| 282 | case AES_IVR(2): |
| 283 | case AES_IVR(3): |
| 284 | snprintf(tmp, sz, "IVR[%u]", (offset - AES_IVR(0)) >> 2); |
| 285 | break; |
| 286 | |
| 287 | case AES_AADLENR: |
| 288 | return "AADLENR"; |
| 289 | |
| 290 | case AES_CLENR: |
| 291 | return "CLENR"; |
| 292 | |
| 293 | case AES_GHASHR(0): |
| 294 | case AES_GHASHR(1): |
| 295 | case AES_GHASHR(2): |
| 296 | case AES_GHASHR(3): |
| 297 | snprintf(tmp, sz, "GHASHR[%u]", (offset - AES_GHASHR(0)) >> 2); |
| 298 | break; |
| 299 | |
| 300 | case AES_TAGR(0): |
| 301 | case AES_TAGR(1): |
| 302 | case AES_TAGR(2): |
| 303 | case AES_TAGR(3): |
| 304 | snprintf(tmp, sz, "TAGR[%u]", (offset - AES_TAGR(0)) >> 2); |
| 305 | break; |
| 306 | |
| 307 | case AES_CTRR: |
| 308 | return "CTRR"; |
| 309 | |
| 310 | case AES_GCMHR(0): |
| 311 | case AES_GCMHR(1): |
| 312 | case AES_GCMHR(2): |
| 313 | case AES_GCMHR(3): |
| 314 | snprintf(tmp, sz, "GCMHR[%u]", (offset - AES_GCMHR(0)) >> 2); |
Herbert Xu | e31835a | 2016-01-19 09:05:43 +0800 | [diff] [blame] | 315 | break; |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 316 | |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 317 | case AES_EMR: |
| 318 | return "EMR"; |
| 319 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 320 | case AES_TWR(0): |
| 321 | case AES_TWR(1): |
| 322 | case AES_TWR(2): |
| 323 | case AES_TWR(3): |
| 324 | snprintf(tmp, sz, "TWR[%u]", (offset - AES_TWR(0)) >> 2); |
| 325 | break; |
| 326 | |
| 327 | case AES_ALPHAR(0): |
| 328 | case AES_ALPHAR(1): |
| 329 | case AES_ALPHAR(2): |
| 330 | case AES_ALPHAR(3): |
| 331 | snprintf(tmp, sz, "ALPHAR[%u]", (offset - AES_ALPHAR(0)) >> 2); |
| 332 | break; |
| 333 | |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 334 | default: |
| 335 | snprintf(tmp, sz, "0x%02x", offset); |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | return tmp; |
| 340 | } |
| 341 | #endif /* VERBOSE_DEBUG */ |
| 342 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 343 | /* Shared functions */ |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 344 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 345 | static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset) |
| 346 | { |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 347 | u32 value = readl_relaxed(dd->io_base + offset); |
| 348 | |
| 349 | #ifdef VERBOSE_DEBUG |
| 350 | if (dd->flags & AES_FLAGS_DUMP_REG) { |
| 351 | char tmp[16]; |
| 352 | |
| 353 | dev_vdbg(dd->dev, "read 0x%08x from %s\n", value, |
| 354 | atmel_aes_reg_name(offset, tmp, sizeof(tmp))); |
| 355 | } |
| 356 | #endif /* VERBOSE_DEBUG */ |
| 357 | |
| 358 | return value; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static inline void atmel_aes_write(struct atmel_aes_dev *dd, |
| 362 | u32 offset, u32 value) |
| 363 | { |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 364 | #ifdef VERBOSE_DEBUG |
| 365 | if (dd->flags & AES_FLAGS_DUMP_REG) { |
| 366 | char tmp[16]; |
| 367 | |
| 368 | dev_vdbg(dd->dev, "write 0x%08x into %s\n", value, |
Cyrille Pitchen | f709dc8 | 2016-09-29 18:46:57 +0200 | [diff] [blame] | 369 | atmel_aes_reg_name(offset, tmp, sizeof(tmp))); |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 370 | } |
| 371 | #endif /* VERBOSE_DEBUG */ |
| 372 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 373 | writel_relaxed(value, dd->io_base + offset); |
| 374 | } |
| 375 | |
| 376 | static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset, |
| 377 | u32 *value, int count) |
| 378 | { |
| 379 | for (; count--; value++, offset += 4) |
| 380 | *value = atmel_aes_read(dd, offset); |
| 381 | } |
| 382 | |
| 383 | static void atmel_aes_write_n(struct atmel_aes_dev *dd, u32 offset, |
Cyrille Pitchen | c0b28d8 | 2015-12-17 17:48:33 +0100 | [diff] [blame] | 384 | const u32 *value, int count) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 385 | { |
| 386 | for (; count--; value++, offset += 4) |
| 387 | atmel_aes_write(dd, offset, *value); |
| 388 | } |
| 389 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 390 | static inline void atmel_aes_read_block(struct atmel_aes_dev *dd, u32 offset, |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 391 | void *value) |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 392 | { |
| 393 | atmel_aes_read_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE)); |
| 394 | } |
| 395 | |
| 396 | static inline void atmel_aes_write_block(struct atmel_aes_dev *dd, u32 offset, |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 397 | const void *value) |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 398 | { |
| 399 | atmel_aes_write_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE)); |
| 400 | } |
| 401 | |
| 402 | static inline int atmel_aes_wait_for_data_ready(struct atmel_aes_dev *dd, |
| 403 | atmel_aes_fn_t resume) |
| 404 | { |
| 405 | u32 isr = atmel_aes_read(dd, AES_ISR); |
| 406 | |
| 407 | if (unlikely(isr & AES_INT_DATARDY)) |
| 408 | return resume(dd); |
| 409 | |
| 410 | dd->resume = resume; |
| 411 | atmel_aes_write(dd, AES_IER, AES_INT_DATARDY); |
| 412 | return -EINPROGRESS; |
| 413 | } |
| 414 | |
| 415 | static inline size_t atmel_aes_padlen(size_t len, size_t block_size) |
| 416 | { |
| 417 | len &= block_size - 1; |
| 418 | return len ? block_size - len : 0; |
| 419 | } |
| 420 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 421 | static struct atmel_aes_dev *atmel_aes_find_dev(struct atmel_aes_base_ctx *ctx) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 422 | { |
| 423 | struct atmel_aes_dev *aes_dd = NULL; |
| 424 | struct atmel_aes_dev *tmp; |
| 425 | |
| 426 | spin_lock_bh(&atmel_aes.lock); |
| 427 | if (!ctx->dd) { |
| 428 | list_for_each_entry(tmp, &atmel_aes.dev_list, list) { |
| 429 | aes_dd = tmp; |
| 430 | break; |
| 431 | } |
| 432 | ctx->dd = aes_dd; |
| 433 | } else { |
| 434 | aes_dd = ctx->dd; |
| 435 | } |
| 436 | |
| 437 | spin_unlock_bh(&atmel_aes.lock); |
| 438 | |
| 439 | return aes_dd; |
| 440 | } |
| 441 | |
| 442 | static int atmel_aes_hw_init(struct atmel_aes_dev *dd) |
| 443 | { |
LABBE Corentin | 9d83d29 | 2015-10-02 14:12:58 +0200 | [diff] [blame] | 444 | int err; |
| 445 | |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 446 | err = clk_enable(dd->iclk); |
LABBE Corentin | 9d83d29 | 2015-10-02 14:12:58 +0200 | [diff] [blame] | 447 | if (err) |
| 448 | return err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 449 | |
Romain Izard | 7a373fd | 2017-10-31 16:25:24 +0100 | [diff] [blame] | 450 | atmel_aes_write(dd, AES_CR, AES_CR_SWRST); |
| 451 | atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 456 | static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd) |
| 457 | { |
| 458 | return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff; |
| 459 | } |
| 460 | |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 461 | static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 462 | { |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 463 | int err; |
| 464 | |
| 465 | err = atmel_aes_hw_init(dd); |
| 466 | if (err) |
| 467 | return err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 468 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 469 | dd->hw_version = atmel_aes_get_version(dd); |
| 470 | |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 471 | dev_info(dd->dev, "version: 0x%x\n", dd->hw_version); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 472 | |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 473 | clk_disable(dd->iclk); |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 474 | return 0; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 477 | static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd, |
| 478 | const struct atmel_aes_reqctx *rctx) |
| 479 | { |
| 480 | /* Clear all but persistent flags and set request flags. */ |
| 481 | dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode; |
| 482 | } |
| 483 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 484 | static inline bool atmel_aes_is_encrypt(const struct atmel_aes_dev *dd) |
| 485 | { |
| 486 | return (dd->flags & AES_FLAGS_ENCRYPT); |
| 487 | } |
| 488 | |
Herbert Xu | 1520c72 | 2019-10-28 15:39:07 +0800 | [diff] [blame] | 489 | #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC) |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 490 | static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err); |
| 491 | #endif |
| 492 | |
Tudor Ambarus | 86ef1df | 2019-10-04 08:55:37 +0000 | [diff] [blame] | 493 | static void atmel_aes_set_iv_as_last_ciphertext_block(struct atmel_aes_dev *dd) |
| 494 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 495 | struct skcipher_request *req = skcipher_request_cast(dd->areq); |
| 496 | struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req); |
| 497 | struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); |
| 498 | unsigned int ivsize = crypto_skcipher_ivsize(skcipher); |
Tudor Ambarus | 86ef1df | 2019-10-04 08:55:37 +0000 | [diff] [blame] | 499 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 500 | if (req->cryptlen < ivsize) |
Tudor Ambarus | 86ef1df | 2019-10-04 08:55:37 +0000 | [diff] [blame] | 501 | return; |
| 502 | |
| 503 | if (rctx->mode & AES_FLAGS_ENCRYPT) { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 504 | scatterwalk_map_and_copy(req->iv, req->dst, |
| 505 | req->cryptlen - ivsize, ivsize, 0); |
Tudor Ambarus | 86ef1df | 2019-10-04 08:55:37 +0000 | [diff] [blame] | 506 | } else { |
| 507 | if (req->src == req->dst) |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 508 | memcpy(req->iv, rctx->lastc, ivsize); |
Tudor Ambarus | 86ef1df | 2019-10-04 08:55:37 +0000 | [diff] [blame] | 509 | else |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 510 | scatterwalk_map_and_copy(req->iv, req->src, |
| 511 | req->cryptlen - ivsize, |
Tudor Ambarus | 86ef1df | 2019-10-04 08:55:37 +0000 | [diff] [blame] | 512 | ivsize, 0); |
| 513 | } |
| 514 | } |
| 515 | |
Tudor Ambarus | 371731e | 2019-12-05 09:54:03 +0000 | [diff] [blame] | 516 | static inline struct atmel_aes_ctr_ctx * |
| 517 | atmel_aes_ctr_ctx_cast(struct atmel_aes_base_ctx *ctx) |
| 518 | { |
| 519 | return container_of(ctx, struct atmel_aes_ctr_ctx, base); |
| 520 | } |
| 521 | |
| 522 | static void atmel_aes_ctr_update_req_iv(struct atmel_aes_dev *dd) |
| 523 | { |
| 524 | struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx); |
| 525 | struct skcipher_request *req = skcipher_request_cast(dd->areq); |
| 526 | struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); |
| 527 | unsigned int ivsize = crypto_skcipher_ivsize(skcipher); |
| 528 | int i; |
| 529 | |
Tudor Ambarus | 3907ccf | 2019-12-13 14:45:44 +0000 | [diff] [blame] | 530 | /* |
| 531 | * The CTR transfer works in fragments of data of maximum 1 MByte |
| 532 | * because of the 16 bit CTR counter embedded in the IP. When reaching |
| 533 | * here, ctx->blocks contains the number of blocks of the last fragment |
| 534 | * processed, there is no need to explicit cast it to u16. |
| 535 | */ |
Tudor Ambarus | 371731e | 2019-12-05 09:54:03 +0000 | [diff] [blame] | 536 | for (i = 0; i < ctx->blocks; i++) |
| 537 | crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE); |
| 538 | |
| 539 | memcpy(req->iv, ctx->iv, ivsize); |
| 540 | } |
| 541 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 542 | static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 543 | { |
Tudor Ambarus | c65d123 | 2019-12-05 09:54:00 +0000 | [diff] [blame] | 544 | struct skcipher_request *req = skcipher_request_cast(dd->areq); |
| 545 | struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req); |
| 546 | |
Herbert Xu | 1520c72 | 2019-10-28 15:39:07 +0800 | [diff] [blame] | 547 | #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC) |
Romain Izard | 9130801 | 2017-10-31 16:25:23 +0100 | [diff] [blame] | 548 | if (dd->ctx->is_aead) |
| 549 | atmel_aes_authenc_complete(dd, err); |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 550 | #endif |
| 551 | |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 552 | clk_disable(dd->iclk); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 553 | dd->flags &= ~AES_FLAGS_BUSY; |
| 554 | |
Tudor Ambarus | 27f4adf | 2019-12-13 09:54:56 +0000 | [diff] [blame] | 555 | if (!err && !dd->ctx->is_aead && |
Tudor Ambarus | 371731e | 2019-12-05 09:54:03 +0000 | [diff] [blame] | 556 | (rctx->mode & AES_FLAGS_OPMODE_MASK) != AES_FLAGS_ECB) { |
| 557 | if ((rctx->mode & AES_FLAGS_OPMODE_MASK) != AES_FLAGS_CTR) |
| 558 | atmel_aes_set_iv_as_last_ciphertext_block(dd); |
| 559 | else |
| 560 | atmel_aes_ctr_update_req_iv(dd); |
| 561 | } |
Romain Izard | 9130801 | 2017-10-31 16:25:23 +0100 | [diff] [blame] | 562 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 563 | if (dd->is_async) |
| 564 | dd->areq->complete(dd->areq, err); |
| 565 | |
| 566 | tasklet_schedule(&dd->queue_task); |
| 567 | |
| 568 | return err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 571 | static void atmel_aes_write_ctrl_key(struct atmel_aes_dev *dd, bool use_dma, |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 572 | const __be32 *iv, const u32 *key, int keylen) |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 573 | { |
| 574 | u32 valmr = 0; |
| 575 | |
| 576 | /* MR register must be set before IV registers */ |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 577 | if (keylen == AES_KEYSIZE_128) |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 578 | valmr |= AES_MR_KEYSIZE_128; |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 579 | else if (keylen == AES_KEYSIZE_192) |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 580 | valmr |= AES_MR_KEYSIZE_192; |
| 581 | else |
| 582 | valmr |= AES_MR_KEYSIZE_256; |
| 583 | |
| 584 | valmr |= dd->flags & AES_FLAGS_MODE_MASK; |
| 585 | |
| 586 | if (use_dma) { |
| 587 | valmr |= AES_MR_SMOD_IDATAR0; |
| 588 | if (dd->caps.has_dualbuff) |
| 589 | valmr |= AES_MR_DUALBUFF; |
| 590 | } else { |
| 591 | valmr |= AES_MR_SMOD_AUTO; |
| 592 | } |
| 593 | |
| 594 | atmel_aes_write(dd, AES_MR, valmr); |
| 595 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 596 | atmel_aes_write_n(dd, AES_KEYWR(0), key, SIZE_IN_WORDS(keylen)); |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 597 | |
| 598 | if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB) |
| 599 | atmel_aes_write_block(dd, AES_IVR(0), iv); |
| 600 | } |
| 601 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 602 | static inline void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma, |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 603 | const __be32 *iv) |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 604 | |
| 605 | { |
| 606 | atmel_aes_write_ctrl_key(dd, use_dma, iv, |
| 607 | dd->ctx->key, dd->ctx->keylen); |
| 608 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 609 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 610 | /* CPU transfer */ |
| 611 | |
| 612 | static int atmel_aes_cpu_transfer(struct atmel_aes_dev *dd) |
| 613 | { |
| 614 | int err = 0; |
| 615 | u32 isr; |
| 616 | |
| 617 | for (;;) { |
| 618 | atmel_aes_read_block(dd, AES_ODATAR(0), dd->data); |
| 619 | dd->data += 4; |
| 620 | dd->datalen -= AES_BLOCK_SIZE; |
| 621 | |
| 622 | if (dd->datalen < AES_BLOCK_SIZE) |
| 623 | break; |
| 624 | |
| 625 | atmel_aes_write_block(dd, AES_IDATAR(0), dd->data); |
| 626 | |
| 627 | isr = atmel_aes_read(dd, AES_ISR); |
| 628 | if (!(isr & AES_INT_DATARDY)) { |
| 629 | dd->resume = atmel_aes_cpu_transfer; |
| 630 | atmel_aes_write(dd, AES_IER, AES_INT_DATARDY); |
| 631 | return -EINPROGRESS; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | if (!sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst), |
| 636 | dd->buf, dd->total)) |
| 637 | err = -EINVAL; |
| 638 | |
| 639 | if (err) |
| 640 | return atmel_aes_complete(dd, err); |
| 641 | |
| 642 | return dd->cpu_transfer_complete(dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 643 | } |
| 644 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 645 | static int atmel_aes_cpu_start(struct atmel_aes_dev *dd, |
| 646 | struct scatterlist *src, |
| 647 | struct scatterlist *dst, |
| 648 | size_t len, |
| 649 | atmel_aes_fn_t resume) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 650 | { |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 651 | size_t padlen = atmel_aes_padlen(len, AES_BLOCK_SIZE); |
| 652 | |
| 653 | if (unlikely(len == 0)) |
| 654 | return -EINVAL; |
| 655 | |
| 656 | sg_copy_to_buffer(src, sg_nents(src), dd->buf, len); |
| 657 | |
| 658 | dd->total = len; |
| 659 | dd->real_dst = dst; |
| 660 | dd->cpu_transfer_complete = resume; |
| 661 | dd->datalen = len + padlen; |
| 662 | dd->data = (u32 *)dd->buf; |
| 663 | atmel_aes_write_block(dd, AES_IDATAR(0), dd->data); |
| 664 | return atmel_aes_wait_for_data_ready(dd, atmel_aes_cpu_transfer); |
| 665 | } |
| 666 | |
| 667 | |
| 668 | /* DMA transfer */ |
| 669 | |
| 670 | static void atmel_aes_dma_callback(void *data); |
| 671 | |
| 672 | static bool atmel_aes_check_aligned(struct atmel_aes_dev *dd, |
| 673 | struct scatterlist *sg, |
| 674 | size_t len, |
| 675 | struct atmel_aes_dma *dma) |
| 676 | { |
| 677 | int nents; |
| 678 | |
| 679 | if (!IS_ALIGNED(len, dd->ctx->block_size)) |
| 680 | return false; |
| 681 | |
| 682 | for (nents = 0; sg; sg = sg_next(sg), ++nents) { |
| 683 | if (!IS_ALIGNED(sg->offset, sizeof(u32))) |
| 684 | return false; |
| 685 | |
| 686 | if (len <= sg->length) { |
| 687 | if (!IS_ALIGNED(len, dd->ctx->block_size)) |
| 688 | return false; |
| 689 | |
| 690 | dma->nents = nents+1; |
| 691 | dma->remainder = sg->length - len; |
| 692 | sg->length = len; |
| 693 | return true; |
| 694 | } |
| 695 | |
| 696 | if (!IS_ALIGNED(sg->length, dd->ctx->block_size)) |
| 697 | return false; |
| 698 | |
| 699 | len -= sg->length; |
| 700 | } |
| 701 | |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | static inline void atmel_aes_restore_sg(const struct atmel_aes_dma *dma) |
| 706 | { |
| 707 | struct scatterlist *sg = dma->sg; |
| 708 | int nents = dma->nents; |
| 709 | |
| 710 | if (!dma->remainder) |
| 711 | return; |
| 712 | |
| 713 | while (--nents > 0 && sg) |
| 714 | sg = sg_next(sg); |
| 715 | |
| 716 | if (!sg) |
| 717 | return; |
| 718 | |
| 719 | sg->length += dma->remainder; |
| 720 | } |
| 721 | |
| 722 | static int atmel_aes_map(struct atmel_aes_dev *dd, |
| 723 | struct scatterlist *src, |
| 724 | struct scatterlist *dst, |
| 725 | size_t len) |
| 726 | { |
| 727 | bool src_aligned, dst_aligned; |
| 728 | size_t padlen; |
| 729 | |
| 730 | dd->total = len; |
| 731 | dd->src.sg = src; |
| 732 | dd->dst.sg = dst; |
| 733 | dd->real_dst = dst; |
| 734 | |
| 735 | src_aligned = atmel_aes_check_aligned(dd, src, len, &dd->src); |
| 736 | if (src == dst) |
| 737 | dst_aligned = src_aligned; |
| 738 | else |
| 739 | dst_aligned = atmel_aes_check_aligned(dd, dst, len, &dd->dst); |
| 740 | if (!src_aligned || !dst_aligned) { |
| 741 | padlen = atmel_aes_padlen(len, dd->ctx->block_size); |
| 742 | |
| 743 | if (dd->buflen < len + padlen) |
| 744 | return -ENOMEM; |
| 745 | |
| 746 | if (!src_aligned) { |
| 747 | sg_copy_to_buffer(src, sg_nents(src), dd->buf, len); |
| 748 | dd->src.sg = &dd->aligned_sg; |
| 749 | dd->src.nents = 1; |
| 750 | dd->src.remainder = 0; |
| 751 | } |
| 752 | |
| 753 | if (!dst_aligned) { |
| 754 | dd->dst.sg = &dd->aligned_sg; |
| 755 | dd->dst.nents = 1; |
| 756 | dd->dst.remainder = 0; |
| 757 | } |
| 758 | |
| 759 | sg_init_table(&dd->aligned_sg, 1); |
| 760 | sg_set_buf(&dd->aligned_sg, dd->buf, len + padlen); |
| 761 | } |
| 762 | |
| 763 | if (dd->src.sg == dd->dst.sg) { |
| 764 | dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 765 | DMA_BIDIRECTIONAL); |
| 766 | dd->dst.sg_len = dd->src.sg_len; |
| 767 | if (!dd->src.sg_len) |
| 768 | return -EFAULT; |
| 769 | } else { |
| 770 | dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 771 | DMA_TO_DEVICE); |
| 772 | if (!dd->src.sg_len) |
| 773 | return -EFAULT; |
| 774 | |
| 775 | dd->dst.sg_len = dma_map_sg(dd->dev, dd->dst.sg, dd->dst.nents, |
| 776 | DMA_FROM_DEVICE); |
| 777 | if (!dd->dst.sg_len) { |
| 778 | dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 779 | DMA_TO_DEVICE); |
| 780 | return -EFAULT; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static void atmel_aes_unmap(struct atmel_aes_dev *dd) |
| 788 | { |
| 789 | if (dd->src.sg == dd->dst.sg) { |
| 790 | dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 791 | DMA_BIDIRECTIONAL); |
| 792 | |
| 793 | if (dd->src.sg != &dd->aligned_sg) |
| 794 | atmel_aes_restore_sg(&dd->src); |
| 795 | } else { |
| 796 | dma_unmap_sg(dd->dev, dd->dst.sg, dd->dst.nents, |
| 797 | DMA_FROM_DEVICE); |
| 798 | |
| 799 | if (dd->dst.sg != &dd->aligned_sg) |
| 800 | atmel_aes_restore_sg(&dd->dst); |
| 801 | |
| 802 | dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 803 | DMA_TO_DEVICE); |
| 804 | |
| 805 | if (dd->src.sg != &dd->aligned_sg) |
| 806 | atmel_aes_restore_sg(&dd->src); |
| 807 | } |
| 808 | |
| 809 | if (dd->dst.sg == &dd->aligned_sg) |
| 810 | sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst), |
| 811 | dd->buf, dd->total); |
| 812 | } |
| 813 | |
| 814 | static int atmel_aes_dma_transfer_start(struct atmel_aes_dev *dd, |
| 815 | enum dma_slave_buswidth addr_width, |
| 816 | enum dma_transfer_direction dir, |
| 817 | u32 maxburst) |
| 818 | { |
| 819 | struct dma_async_tx_descriptor *desc; |
| 820 | struct dma_slave_config config; |
| 821 | dma_async_tx_callback callback; |
| 822 | struct atmel_aes_dma *dma; |
| 823 | int err; |
| 824 | |
| 825 | memset(&config, 0, sizeof(config)); |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 826 | config.src_addr_width = addr_width; |
| 827 | config.dst_addr_width = addr_width; |
| 828 | config.src_maxburst = maxburst; |
| 829 | config.dst_maxburst = maxburst; |
| 830 | |
| 831 | switch (dir) { |
| 832 | case DMA_MEM_TO_DEV: |
| 833 | dma = &dd->src; |
| 834 | callback = NULL; |
| 835 | config.dst_addr = dd->phys_base + AES_IDATAR(0); |
| 836 | break; |
| 837 | |
| 838 | case DMA_DEV_TO_MEM: |
| 839 | dma = &dd->dst; |
| 840 | callback = atmel_aes_dma_callback; |
| 841 | config.src_addr = dd->phys_base + AES_ODATAR(0); |
| 842 | break; |
| 843 | |
| 844 | default: |
| 845 | return -EINVAL; |
| 846 | } |
| 847 | |
| 848 | err = dmaengine_slave_config(dma->chan, &config); |
| 849 | if (err) |
| 850 | return err; |
| 851 | |
| 852 | desc = dmaengine_prep_slave_sg(dma->chan, dma->sg, dma->sg_len, dir, |
| 853 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 854 | if (!desc) |
| 855 | return -ENOMEM; |
| 856 | |
| 857 | desc->callback = callback; |
| 858 | desc->callback_param = dd; |
| 859 | dmaengine_submit(desc); |
| 860 | dma_async_issue_pending(dma->chan); |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 865 | static int atmel_aes_dma_start(struct atmel_aes_dev *dd, |
| 866 | struct scatterlist *src, |
| 867 | struct scatterlist *dst, |
| 868 | size_t len, |
| 869 | atmel_aes_fn_t resume) |
| 870 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 871 | enum dma_slave_buswidth addr_width; |
| 872 | u32 maxburst; |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 873 | int err; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 874 | |
| 875 | switch (dd->ctx->block_size) { |
| 876 | case CFB8_BLOCK_SIZE: |
| 877 | addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 878 | maxburst = 1; |
| 879 | break; |
| 880 | |
| 881 | case CFB16_BLOCK_SIZE: |
| 882 | addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 883 | maxburst = 1; |
| 884 | break; |
| 885 | |
| 886 | case CFB32_BLOCK_SIZE: |
| 887 | case CFB64_BLOCK_SIZE: |
| 888 | addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 889 | maxburst = 1; |
| 890 | break; |
| 891 | |
| 892 | case AES_BLOCK_SIZE: |
| 893 | addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 894 | maxburst = dd->caps.max_burst_size; |
| 895 | break; |
| 896 | |
| 897 | default: |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 898 | err = -EINVAL; |
| 899 | goto exit; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 900 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 901 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 902 | err = atmel_aes_map(dd, src, dst, len); |
| 903 | if (err) |
| 904 | goto exit; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 905 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 906 | dd->resume = resume; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 907 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 908 | /* Set output DMA transfer first */ |
| 909 | err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_DEV_TO_MEM, |
| 910 | maxburst); |
| 911 | if (err) |
| 912 | goto unmap; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 913 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 914 | /* Then set input DMA transfer */ |
| 915 | err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_MEM_TO_DEV, |
| 916 | maxburst); |
| 917 | if (err) |
| 918 | goto output_transfer_stop; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 919 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 920 | return -EINPROGRESS; |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 921 | |
| 922 | output_transfer_stop: |
Tudor Ambarus | 0e69378 | 2019-12-13 09:54:42 +0000 | [diff] [blame] | 923 | dmaengine_terminate_sync(dd->dst.chan); |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 924 | unmap: |
| 925 | atmel_aes_unmap(dd); |
| 926 | exit: |
| 927 | return atmel_aes_complete(dd, err); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 928 | } |
| 929 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 930 | static void atmel_aes_dma_callback(void *data) |
| 931 | { |
| 932 | struct atmel_aes_dev *dd = data; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 933 | |
Tudor Ambarus | 0e69378 | 2019-12-13 09:54:42 +0000 | [diff] [blame] | 934 | atmel_aes_unmap(dd); |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 935 | dd->is_async = true; |
| 936 | (void)dd->resume(dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 937 | } |
| 938 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 939 | static int atmel_aes_handle_queue(struct atmel_aes_dev *dd, |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 940 | struct crypto_async_request *new_areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 941 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 942 | struct crypto_async_request *areq, *backlog; |
| 943 | struct atmel_aes_base_ctx *ctx; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 944 | unsigned long flags; |
Cyrille Pitchen | a1f613f | 2017-01-26 17:07:55 +0100 | [diff] [blame] | 945 | bool start_async; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 946 | int err, ret = 0; |
| 947 | |
| 948 | spin_lock_irqsave(&dd->lock, flags); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 949 | if (new_areq) |
| 950 | ret = crypto_enqueue_request(&dd->queue, new_areq); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 951 | if (dd->flags & AES_FLAGS_BUSY) { |
| 952 | spin_unlock_irqrestore(&dd->lock, flags); |
| 953 | return ret; |
| 954 | } |
| 955 | backlog = crypto_get_backlog(&dd->queue); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 956 | areq = crypto_dequeue_request(&dd->queue); |
| 957 | if (areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 958 | dd->flags |= AES_FLAGS_BUSY; |
| 959 | spin_unlock_irqrestore(&dd->lock, flags); |
| 960 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 961 | if (!areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 962 | return ret; |
| 963 | |
| 964 | if (backlog) |
| 965 | backlog->complete(backlog, -EINPROGRESS); |
| 966 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 967 | ctx = crypto_tfm_ctx(areq->tfm); |
| 968 | |
| 969 | dd->areq = areq; |
| 970 | dd->ctx = ctx; |
Cyrille Pitchen | a1f613f | 2017-01-26 17:07:55 +0100 | [diff] [blame] | 971 | start_async = (areq != new_areq); |
| 972 | dd->is_async = start_async; |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 973 | |
Cyrille Pitchen | a1f613f | 2017-01-26 17:07:55 +0100 | [diff] [blame] | 974 | /* WARNING: ctx->start() MAY change dd->is_async. */ |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 975 | err = ctx->start(dd); |
Cyrille Pitchen | a1f613f | 2017-01-26 17:07:55 +0100 | [diff] [blame] | 976 | return (start_async) ? ret : err; |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 977 | } |
| 978 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 979 | |
| 980 | /* AES async block ciphers */ |
| 981 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 982 | static int atmel_aes_transfer_complete(struct atmel_aes_dev *dd) |
| 983 | { |
| 984 | return atmel_aes_complete(dd, 0); |
| 985 | } |
| 986 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 987 | static int atmel_aes_start(struct atmel_aes_dev *dd) |
| 988 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 989 | struct skcipher_request *req = skcipher_request_cast(dd->areq); |
| 990 | struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req); |
| 991 | bool use_dma = (req->cryptlen >= ATMEL_AES_DMA_THRESHOLD || |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 992 | dd->ctx->block_size != AES_BLOCK_SIZE); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 993 | int err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 994 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 995 | atmel_aes_set_mode(dd, rctx); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 996 | |
Cyrille Pitchen | cdfab4a | 2015-12-17 17:48:38 +0100 | [diff] [blame] | 997 | err = atmel_aes_hw_init(dd); |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 998 | if (err) |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 999 | return atmel_aes_complete(dd, err); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1000 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1001 | atmel_aes_write_ctrl(dd, use_dma, (void *)req->iv); |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 1002 | if (use_dma) |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1003 | return atmel_aes_dma_start(dd, req->src, req->dst, |
| 1004 | req->cryptlen, |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 1005 | atmel_aes_transfer_complete); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1006 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1007 | return atmel_aes_cpu_start(dd, req->src, req->dst, req->cryptlen, |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 1008 | atmel_aes_transfer_complete); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1009 | } |
| 1010 | |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1011 | static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd) |
| 1012 | { |
| 1013 | struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx); |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1014 | struct skcipher_request *req = skcipher_request_cast(dd->areq); |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1015 | struct scatterlist *src, *dst; |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1016 | size_t datalen; |
Tudor Ambarus | 781a08d | 2019-12-05 09:54:01 +0000 | [diff] [blame] | 1017 | u32 ctr; |
Tudor Ambarus | 371731e | 2019-12-05 09:54:03 +0000 | [diff] [blame] | 1018 | u16 start, end; |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1019 | bool use_dma, fragmented = false; |
| 1020 | |
| 1021 | /* Check for transfer completion. */ |
| 1022 | ctx->offset += dd->total; |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1023 | if (ctx->offset >= req->cryptlen) |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1024 | return atmel_aes_transfer_complete(dd); |
| 1025 | |
| 1026 | /* Compute data length. */ |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1027 | datalen = req->cryptlen - ctx->offset; |
Tudor Ambarus | 371731e | 2019-12-05 09:54:03 +0000 | [diff] [blame] | 1028 | ctx->blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE); |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1029 | ctr = be32_to_cpu(ctx->iv[3]); |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1030 | |
Tudor Ambarus | 781a08d | 2019-12-05 09:54:01 +0000 | [diff] [blame] | 1031 | /* Check 16bit counter overflow. */ |
| 1032 | start = ctr & 0xffff; |
Tudor Ambarus | 371731e | 2019-12-05 09:54:03 +0000 | [diff] [blame] | 1033 | end = start + ctx->blocks - 1; |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1034 | |
Tudor Ambarus | 371731e | 2019-12-05 09:54:03 +0000 | [diff] [blame] | 1035 | if (ctx->blocks >> 16 || end < start) { |
Tudor Ambarus | 781a08d | 2019-12-05 09:54:01 +0000 | [diff] [blame] | 1036 | ctr |= 0xffff; |
| 1037 | datalen = AES_BLOCK_SIZE * (0x10000 - start); |
| 1038 | fragmented = true; |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1039 | } |
Tudor Ambarus | 781a08d | 2019-12-05 09:54:01 +0000 | [diff] [blame] | 1040 | |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1041 | use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD); |
| 1042 | |
| 1043 | /* Jump to offset. */ |
| 1044 | src = scatterwalk_ffwd(ctx->src, req->src, ctx->offset); |
| 1045 | dst = ((req->src == req->dst) ? src : |
| 1046 | scatterwalk_ffwd(ctx->dst, req->dst, ctx->offset)); |
| 1047 | |
| 1048 | /* Configure hardware. */ |
| 1049 | atmel_aes_write_ctrl(dd, use_dma, ctx->iv); |
| 1050 | if (unlikely(fragmented)) { |
| 1051 | /* |
| 1052 | * Increment the counter manually to cope with the hardware |
| 1053 | * counter overflow. |
| 1054 | */ |
| 1055 | ctx->iv[3] = cpu_to_be32(ctr); |
| 1056 | crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE); |
| 1057 | } |
| 1058 | |
| 1059 | if (use_dma) |
| 1060 | return atmel_aes_dma_start(dd, src, dst, datalen, |
| 1061 | atmel_aes_ctr_transfer); |
| 1062 | |
| 1063 | return atmel_aes_cpu_start(dd, src, dst, datalen, |
| 1064 | atmel_aes_ctr_transfer); |
| 1065 | } |
| 1066 | |
| 1067 | static int atmel_aes_ctr_start(struct atmel_aes_dev *dd) |
| 1068 | { |
| 1069 | struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx); |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1070 | struct skcipher_request *req = skcipher_request_cast(dd->areq); |
| 1071 | struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req); |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1072 | int err; |
| 1073 | |
| 1074 | atmel_aes_set_mode(dd, rctx); |
| 1075 | |
| 1076 | err = atmel_aes_hw_init(dd); |
| 1077 | if (err) |
| 1078 | return atmel_aes_complete(dd, err); |
| 1079 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1080 | memcpy(ctx->iv, req->iv, AES_BLOCK_SIZE); |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1081 | ctx->offset = 0; |
| 1082 | dd->total = 0; |
| 1083 | return atmel_aes_ctr_transfer(dd); |
| 1084 | } |
| 1085 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1086 | static int atmel_aes_crypt(struct skcipher_request *req, unsigned long mode) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1087 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1088 | struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); |
| 1089 | struct atmel_aes_base_ctx *ctx = crypto_skcipher_ctx(skcipher); |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1090 | struct atmel_aes_reqctx *rctx; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1091 | struct atmel_aes_dev *dd; |
Tudor Ambarus | 534b32a | 2021-07-20 11:55:29 +0300 | [diff] [blame] | 1092 | u32 opmode = mode & AES_FLAGS_OPMODE_MASK; |
| 1093 | |
Tudor Ambarus | 26d769a | 2021-07-20 11:55:30 +0300 | [diff] [blame] | 1094 | if (opmode == AES_FLAGS_XTS && req->cryptlen < XTS_BLOCK_SIZE) |
| 1095 | return -EINVAL; |
| 1096 | |
Tudor Ambarus | 0d04335 | 2021-07-20 11:55:31 +0300 | [diff] [blame] | 1097 | /* |
| 1098 | * ECB, CBC, CFB, OFB or CTR mode require the plaintext and ciphertext |
| 1099 | * to have a positve integer length. |
| 1100 | */ |
| 1101 | if (!req->cryptlen && opmode != AES_FLAGS_XTS) |
| 1102 | return 0; |
| 1103 | |
Tudor Ambarus | 534b32a | 2021-07-20 11:55:29 +0300 | [diff] [blame] | 1104 | if ((opmode == AES_FLAGS_ECB || opmode == AES_FLAGS_CBC) && |
| 1105 | !IS_ALIGNED(req->cryptlen, crypto_skcipher_blocksize(skcipher))) |
| 1106 | return -EINVAL; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1107 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1108 | switch (mode & AES_FLAGS_OPMODE_MASK) { |
| 1109 | case AES_FLAGS_CFB8: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1110 | ctx->block_size = CFB8_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1111 | break; |
| 1112 | |
| 1113 | case AES_FLAGS_CFB16: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1114 | ctx->block_size = CFB16_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1115 | break; |
| 1116 | |
| 1117 | case AES_FLAGS_CFB32: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1118 | ctx->block_size = CFB32_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1119 | break; |
| 1120 | |
| 1121 | case AES_FLAGS_CFB64: |
Leilei Zhao | 9f84951 | 2014-04-22 15:23:24 +0800 | [diff] [blame] | 1122 | ctx->block_size = CFB64_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1123 | break; |
| 1124 | |
| 1125 | default: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1126 | ctx->block_size = AES_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1127 | break; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1128 | } |
Romain Izard | 9130801 | 2017-10-31 16:25:23 +0100 | [diff] [blame] | 1129 | ctx->is_aead = false; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1130 | |
| 1131 | dd = atmel_aes_find_dev(ctx); |
| 1132 | if (!dd) |
| 1133 | return -ENODEV; |
| 1134 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1135 | rctx = skcipher_request_ctx(req); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1136 | rctx->mode = mode; |
| 1137 | |
Tudor Ambarus | 534b32a | 2021-07-20 11:55:29 +0300 | [diff] [blame] | 1138 | if (opmode != AES_FLAGS_ECB && |
Tudor Ambarus | c65d123 | 2019-12-05 09:54:00 +0000 | [diff] [blame] | 1139 | !(mode & AES_FLAGS_ENCRYPT) && req->src == req->dst) { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1140 | unsigned int ivsize = crypto_skcipher_ivsize(skcipher); |
Romain Izard | 9130801 | 2017-10-31 16:25:23 +0100 | [diff] [blame] | 1141 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1142 | if (req->cryptlen >= ivsize) |
Tudor Ambarus | 86ef1df | 2019-10-04 08:55:37 +0000 | [diff] [blame] | 1143 | scatterwalk_map_and_copy(rctx->lastc, req->src, |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1144 | req->cryptlen - ivsize, |
Tudor Ambarus | 86ef1df | 2019-10-04 08:55:37 +0000 | [diff] [blame] | 1145 | ivsize, 0); |
Romain Izard | 9130801 | 2017-10-31 16:25:23 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 1148 | return atmel_aes_handle_queue(dd, &req->base); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1151 | static int atmel_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1152 | unsigned int keylen) |
| 1153 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1154 | struct atmel_aes_base_ctx *ctx = crypto_skcipher_ctx(tfm); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1155 | |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1156 | if (keylen != AES_KEYSIZE_128 && |
| 1157 | keylen != AES_KEYSIZE_192 && |
Eric Biggers | 674f368 | 2019-12-30 21:19:36 -0600 | [diff] [blame] | 1158 | keylen != AES_KEYSIZE_256) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1159 | return -EINVAL; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1160 | |
| 1161 | memcpy(ctx->key, key, keylen); |
| 1162 | ctx->keylen = keylen; |
| 1163 | |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1167 | static int atmel_aes_ecb_encrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1168 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1169 | return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1170 | } |
| 1171 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1172 | static int atmel_aes_ecb_decrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1173 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1174 | return atmel_aes_crypt(req, AES_FLAGS_ECB); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1177 | static int atmel_aes_cbc_encrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1178 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1179 | return atmel_aes_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1180 | } |
| 1181 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1182 | static int atmel_aes_cbc_decrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1183 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1184 | return atmel_aes_crypt(req, AES_FLAGS_CBC); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1185 | } |
| 1186 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1187 | static int atmel_aes_ofb_encrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1188 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1189 | return atmel_aes_crypt(req, AES_FLAGS_OFB | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1190 | } |
| 1191 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1192 | static int atmel_aes_ofb_decrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1193 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1194 | return atmel_aes_crypt(req, AES_FLAGS_OFB); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1195 | } |
| 1196 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1197 | static int atmel_aes_cfb_encrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1198 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1199 | return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1200 | } |
| 1201 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1202 | static int atmel_aes_cfb_decrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1203 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1204 | return atmel_aes_crypt(req, AES_FLAGS_CFB128); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1205 | } |
| 1206 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1207 | static int atmel_aes_cfb64_encrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1208 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1209 | return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1210 | } |
| 1211 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1212 | static int atmel_aes_cfb64_decrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1213 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1214 | return atmel_aes_crypt(req, AES_FLAGS_CFB64); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1215 | } |
| 1216 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1217 | static int atmel_aes_cfb32_encrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1218 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1219 | return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1220 | } |
| 1221 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1222 | static int atmel_aes_cfb32_decrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1223 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1224 | return atmel_aes_crypt(req, AES_FLAGS_CFB32); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1225 | } |
| 1226 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1227 | static int atmel_aes_cfb16_encrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1228 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1229 | return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1230 | } |
| 1231 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1232 | static int atmel_aes_cfb16_decrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1233 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1234 | return atmel_aes_crypt(req, AES_FLAGS_CFB16); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1235 | } |
| 1236 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1237 | static int atmel_aes_cfb8_encrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1238 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1239 | return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1242 | static int atmel_aes_cfb8_decrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1243 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1244 | return atmel_aes_crypt(req, AES_FLAGS_CFB8); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1247 | static int atmel_aes_ctr_encrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1248 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1249 | return atmel_aes_crypt(req, AES_FLAGS_CTR | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1252 | static int atmel_aes_ctr_decrypt(struct skcipher_request *req) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1253 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1254 | return atmel_aes_crypt(req, AES_FLAGS_CTR); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1257 | static int atmel_aes_init_tfm(struct crypto_skcipher *tfm) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1258 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1259 | struct atmel_aes_ctx *ctx = crypto_skcipher_ctx(tfm); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 1260 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1261 | crypto_skcipher_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx)); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 1262 | ctx->base.start = atmel_aes_start; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1263 | |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1267 | static int atmel_aes_ctr_init_tfm(struct crypto_skcipher *tfm) |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1268 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1269 | struct atmel_aes_ctx *ctx = crypto_skcipher_ctx(tfm); |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1270 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1271 | crypto_skcipher_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx)); |
Cyrille Pitchen | fcac8365 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1272 | ctx->base.start = atmel_aes_ctr_start; |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1277 | static struct skcipher_alg aes_algs[] = { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1278 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1279 | .base.cra_name = "ecb(aes)", |
| 1280 | .base.cra_driver_name = "atmel-ecb-aes", |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1281 | .base.cra_blocksize = AES_BLOCK_SIZE, |
| 1282 | .base.cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1283 | |
| 1284 | .init = atmel_aes_init_tfm, |
| 1285 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1286 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1287 | .setkey = atmel_aes_setkey, |
| 1288 | .encrypt = atmel_aes_ecb_encrypt, |
| 1289 | .decrypt = atmel_aes_ecb_decrypt, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1290 | }, |
| 1291 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1292 | .base.cra_name = "cbc(aes)", |
| 1293 | .base.cra_driver_name = "atmel-cbc-aes", |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1294 | .base.cra_blocksize = AES_BLOCK_SIZE, |
| 1295 | .base.cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1296 | |
| 1297 | .init = atmel_aes_init_tfm, |
| 1298 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1299 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1300 | .setkey = atmel_aes_setkey, |
| 1301 | .encrypt = atmel_aes_cbc_encrypt, |
| 1302 | .decrypt = atmel_aes_cbc_decrypt, |
| 1303 | .ivsize = AES_BLOCK_SIZE, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1304 | }, |
| 1305 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1306 | .base.cra_name = "ofb(aes)", |
| 1307 | .base.cra_driver_name = "atmel-ofb-aes", |
Tudor Ambarus | 76d579f | 2021-07-20 11:55:33 +0300 | [diff] [blame^] | 1308 | .base.cra_blocksize = 1, |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1309 | .base.cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1310 | |
| 1311 | .init = atmel_aes_init_tfm, |
| 1312 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1313 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1314 | .setkey = atmel_aes_setkey, |
| 1315 | .encrypt = atmel_aes_ofb_encrypt, |
| 1316 | .decrypt = atmel_aes_ofb_decrypt, |
| 1317 | .ivsize = AES_BLOCK_SIZE, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1318 | }, |
| 1319 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1320 | .base.cra_name = "cfb(aes)", |
| 1321 | .base.cra_driver_name = "atmel-cfb-aes", |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1322 | .base.cra_blocksize = AES_BLOCK_SIZE, |
| 1323 | .base.cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1324 | |
| 1325 | .init = atmel_aes_init_tfm, |
| 1326 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1327 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1328 | .setkey = atmel_aes_setkey, |
| 1329 | .encrypt = atmel_aes_cfb_encrypt, |
| 1330 | .decrypt = atmel_aes_cfb_decrypt, |
| 1331 | .ivsize = AES_BLOCK_SIZE, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1332 | }, |
| 1333 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1334 | .base.cra_name = "cfb32(aes)", |
| 1335 | .base.cra_driver_name = "atmel-cfb32-aes", |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1336 | .base.cra_blocksize = CFB32_BLOCK_SIZE, |
| 1337 | .base.cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1338 | |
| 1339 | .init = atmel_aes_init_tfm, |
| 1340 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1341 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1342 | .setkey = atmel_aes_setkey, |
| 1343 | .encrypt = atmel_aes_cfb32_encrypt, |
| 1344 | .decrypt = atmel_aes_cfb32_decrypt, |
| 1345 | .ivsize = AES_BLOCK_SIZE, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1346 | }, |
| 1347 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1348 | .base.cra_name = "cfb16(aes)", |
| 1349 | .base.cra_driver_name = "atmel-cfb16-aes", |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1350 | .base.cra_blocksize = CFB16_BLOCK_SIZE, |
| 1351 | .base.cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1352 | |
| 1353 | .init = atmel_aes_init_tfm, |
| 1354 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1355 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1356 | .setkey = atmel_aes_setkey, |
| 1357 | .encrypt = atmel_aes_cfb16_encrypt, |
| 1358 | .decrypt = atmel_aes_cfb16_decrypt, |
| 1359 | .ivsize = AES_BLOCK_SIZE, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1360 | }, |
| 1361 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1362 | .base.cra_name = "cfb8(aes)", |
| 1363 | .base.cra_driver_name = "atmel-cfb8-aes", |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1364 | .base.cra_blocksize = CFB8_BLOCK_SIZE, |
| 1365 | .base.cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1366 | |
| 1367 | .init = atmel_aes_init_tfm, |
| 1368 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1369 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1370 | .setkey = atmel_aes_setkey, |
| 1371 | .encrypt = atmel_aes_cfb8_encrypt, |
| 1372 | .decrypt = atmel_aes_cfb8_decrypt, |
| 1373 | .ivsize = AES_BLOCK_SIZE, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1374 | }, |
| 1375 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1376 | .base.cra_name = "ctr(aes)", |
| 1377 | .base.cra_driver_name = "atmel-ctr-aes", |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1378 | .base.cra_blocksize = 1, |
| 1379 | .base.cra_ctxsize = sizeof(struct atmel_aes_ctr_ctx), |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1380 | |
| 1381 | .init = atmel_aes_ctr_init_tfm, |
| 1382 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1383 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1384 | .setkey = atmel_aes_setkey, |
| 1385 | .encrypt = atmel_aes_ctr_encrypt, |
| 1386 | .decrypt = atmel_aes_ctr_decrypt, |
| 1387 | .ivsize = AES_BLOCK_SIZE, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1388 | }, |
| 1389 | }; |
| 1390 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1391 | static struct skcipher_alg aes_cfb64_alg = { |
| 1392 | .base.cra_name = "cfb64(aes)", |
| 1393 | .base.cra_driver_name = "atmel-cfb64-aes", |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1394 | .base.cra_blocksize = CFB64_BLOCK_SIZE, |
| 1395 | .base.cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1396 | |
| 1397 | .init = atmel_aes_init_tfm, |
| 1398 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1399 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1400 | .setkey = atmel_aes_setkey, |
| 1401 | .encrypt = atmel_aes_cfb64_encrypt, |
| 1402 | .decrypt = atmel_aes_cfb64_decrypt, |
| 1403 | .ivsize = AES_BLOCK_SIZE, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1404 | }; |
| 1405 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 1406 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1407 | /* gcm aead functions */ |
| 1408 | |
| 1409 | static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd, |
| 1410 | const u32 *data, size_t datalen, |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 1411 | const __be32 *ghash_in, __be32 *ghash_out, |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1412 | atmel_aes_fn_t resume); |
| 1413 | static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd); |
| 1414 | static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd); |
| 1415 | |
| 1416 | static int atmel_aes_gcm_start(struct atmel_aes_dev *dd); |
| 1417 | static int atmel_aes_gcm_process(struct atmel_aes_dev *dd); |
| 1418 | static int atmel_aes_gcm_length(struct atmel_aes_dev *dd); |
| 1419 | static int atmel_aes_gcm_data(struct atmel_aes_dev *dd); |
| 1420 | static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd); |
| 1421 | static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd); |
| 1422 | static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd); |
| 1423 | |
| 1424 | static inline struct atmel_aes_gcm_ctx * |
| 1425 | atmel_aes_gcm_ctx_cast(struct atmel_aes_base_ctx *ctx) |
| 1426 | { |
| 1427 | return container_of(ctx, struct atmel_aes_gcm_ctx, base); |
| 1428 | } |
| 1429 | |
| 1430 | static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd, |
| 1431 | const u32 *data, size_t datalen, |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 1432 | const __be32 *ghash_in, __be32 *ghash_out, |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1433 | atmel_aes_fn_t resume) |
| 1434 | { |
| 1435 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1436 | |
| 1437 | dd->data = (u32 *)data; |
| 1438 | dd->datalen = datalen; |
| 1439 | ctx->ghash_in = ghash_in; |
| 1440 | ctx->ghash_out = ghash_out; |
| 1441 | ctx->ghash_resume = resume; |
| 1442 | |
| 1443 | atmel_aes_write_ctrl(dd, false, NULL); |
| 1444 | return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_ghash_init); |
| 1445 | } |
| 1446 | |
| 1447 | static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd) |
| 1448 | { |
| 1449 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1450 | |
| 1451 | /* Set the data length. */ |
| 1452 | atmel_aes_write(dd, AES_AADLENR, dd->total); |
| 1453 | atmel_aes_write(dd, AES_CLENR, 0); |
| 1454 | |
| 1455 | /* If needed, overwrite the GCM Intermediate Hash Word Registers */ |
| 1456 | if (ctx->ghash_in) |
| 1457 | atmel_aes_write_block(dd, AES_GHASHR(0), ctx->ghash_in); |
| 1458 | |
| 1459 | return atmel_aes_gcm_ghash_finalize(dd); |
| 1460 | } |
| 1461 | |
| 1462 | static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd) |
| 1463 | { |
| 1464 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1465 | u32 isr; |
| 1466 | |
| 1467 | /* Write data into the Input Data Registers. */ |
| 1468 | while (dd->datalen > 0) { |
| 1469 | atmel_aes_write_block(dd, AES_IDATAR(0), dd->data); |
| 1470 | dd->data += 4; |
| 1471 | dd->datalen -= AES_BLOCK_SIZE; |
| 1472 | |
| 1473 | isr = atmel_aes_read(dd, AES_ISR); |
| 1474 | if (!(isr & AES_INT_DATARDY)) { |
| 1475 | dd->resume = atmel_aes_gcm_ghash_finalize; |
| 1476 | atmel_aes_write(dd, AES_IER, AES_INT_DATARDY); |
| 1477 | return -EINPROGRESS; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | /* Read the computed hash from GHASHRx. */ |
| 1482 | atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash_out); |
| 1483 | |
| 1484 | return ctx->ghash_resume(dd); |
| 1485 | } |
| 1486 | |
| 1487 | |
| 1488 | static int atmel_aes_gcm_start(struct atmel_aes_dev *dd) |
| 1489 | { |
| 1490 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1491 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1492 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 1493 | struct atmel_aes_reqctx *rctx = aead_request_ctx(req); |
| 1494 | size_t ivsize = crypto_aead_ivsize(tfm); |
| 1495 | size_t datalen, padlen; |
| 1496 | const void *iv = req->iv; |
| 1497 | u8 *data = dd->buf; |
| 1498 | int err; |
| 1499 | |
| 1500 | atmel_aes_set_mode(dd, rctx); |
| 1501 | |
| 1502 | err = atmel_aes_hw_init(dd); |
| 1503 | if (err) |
| 1504 | return atmel_aes_complete(dd, err); |
| 1505 | |
Corentin LABBE | 219d51c | 2017-08-22 10:08:12 +0200 | [diff] [blame] | 1506 | if (likely(ivsize == GCM_AES_IV_SIZE)) { |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1507 | memcpy(ctx->j0, iv, ivsize); |
| 1508 | ctx->j0[3] = cpu_to_be32(1); |
| 1509 | return atmel_aes_gcm_process(dd); |
| 1510 | } |
| 1511 | |
| 1512 | padlen = atmel_aes_padlen(ivsize, AES_BLOCK_SIZE); |
| 1513 | datalen = ivsize + padlen + AES_BLOCK_SIZE; |
| 1514 | if (datalen > dd->buflen) |
| 1515 | return atmel_aes_complete(dd, -EINVAL); |
| 1516 | |
| 1517 | memcpy(data, iv, ivsize); |
| 1518 | memset(data + ivsize, 0, padlen + sizeof(u64)); |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 1519 | ((__be64 *)(data + datalen))[-1] = cpu_to_be64(ivsize * 8); |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1520 | |
| 1521 | return atmel_aes_gcm_ghash(dd, (const u32 *)data, datalen, |
| 1522 | NULL, ctx->j0, atmel_aes_gcm_process); |
| 1523 | } |
| 1524 | |
| 1525 | static int atmel_aes_gcm_process(struct atmel_aes_dev *dd) |
| 1526 | { |
| 1527 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1528 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1529 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 1530 | bool enc = atmel_aes_is_encrypt(dd); |
| 1531 | u32 authsize; |
| 1532 | |
| 1533 | /* Compute text length. */ |
| 1534 | authsize = crypto_aead_authsize(tfm); |
| 1535 | ctx->textlen = req->cryptlen - (enc ? 0 : authsize); |
| 1536 | |
| 1537 | /* |
| 1538 | * According to tcrypt test suite, the GCM Automatic Tag Generation |
| 1539 | * fails when both the message and its associated data are empty. |
| 1540 | */ |
| 1541 | if (likely(req->assoclen != 0 || ctx->textlen != 0)) |
| 1542 | dd->flags |= AES_FLAGS_GTAGEN; |
| 1543 | |
| 1544 | atmel_aes_write_ctrl(dd, false, NULL); |
| 1545 | return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_length); |
| 1546 | } |
| 1547 | |
| 1548 | static int atmel_aes_gcm_length(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); |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 1552 | __be32 j0_lsw, *j0 = ctx->j0; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1553 | size_t padlen; |
| 1554 | |
| 1555 | /* Write incr32(J0) into IV. */ |
| 1556 | j0_lsw = j0[3]; |
Liu Shixin | fb7c2f4 | 2020-09-14 12:17:46 +0800 | [diff] [blame] | 1557 | be32_add_cpu(&j0[3], 1); |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1558 | atmel_aes_write_block(dd, AES_IVR(0), j0); |
| 1559 | j0[3] = j0_lsw; |
| 1560 | |
| 1561 | /* Set aad and text lengths. */ |
| 1562 | atmel_aes_write(dd, AES_AADLENR, req->assoclen); |
| 1563 | atmel_aes_write(dd, AES_CLENR, ctx->textlen); |
| 1564 | |
| 1565 | /* Check whether AAD are present. */ |
| 1566 | if (unlikely(req->assoclen == 0)) { |
| 1567 | dd->datalen = 0; |
| 1568 | return atmel_aes_gcm_data(dd); |
| 1569 | } |
| 1570 | |
| 1571 | /* Copy assoc data and add padding. */ |
| 1572 | padlen = atmel_aes_padlen(req->assoclen, AES_BLOCK_SIZE); |
| 1573 | if (unlikely(req->assoclen + padlen > dd->buflen)) |
| 1574 | return atmel_aes_complete(dd, -EINVAL); |
| 1575 | sg_copy_to_buffer(req->src, sg_nents(req->src), dd->buf, req->assoclen); |
| 1576 | |
| 1577 | /* Write assoc data into the Input Data register. */ |
| 1578 | dd->data = (u32 *)dd->buf; |
| 1579 | dd->datalen = req->assoclen + padlen; |
| 1580 | return atmel_aes_gcm_data(dd); |
| 1581 | } |
| 1582 | |
| 1583 | static int atmel_aes_gcm_data(struct atmel_aes_dev *dd) |
| 1584 | { |
| 1585 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1586 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1587 | bool use_dma = (ctx->textlen >= ATMEL_AES_DMA_THRESHOLD); |
| 1588 | struct scatterlist *src, *dst; |
| 1589 | u32 isr, mr; |
| 1590 | |
| 1591 | /* Write AAD first. */ |
| 1592 | while (dd->datalen > 0) { |
| 1593 | atmel_aes_write_block(dd, AES_IDATAR(0), dd->data); |
| 1594 | dd->data += 4; |
| 1595 | dd->datalen -= AES_BLOCK_SIZE; |
| 1596 | |
| 1597 | isr = atmel_aes_read(dd, AES_ISR); |
| 1598 | if (!(isr & AES_INT_DATARDY)) { |
| 1599 | dd->resume = atmel_aes_gcm_data; |
| 1600 | atmel_aes_write(dd, AES_IER, AES_INT_DATARDY); |
| 1601 | return -EINPROGRESS; |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | /* GMAC only. */ |
| 1606 | if (unlikely(ctx->textlen == 0)) |
| 1607 | return atmel_aes_gcm_tag_init(dd); |
| 1608 | |
| 1609 | /* Prepare src and dst scatter lists to transfer cipher/plain texts */ |
| 1610 | src = scatterwalk_ffwd(ctx->src, req->src, req->assoclen); |
| 1611 | dst = ((req->src == req->dst) ? src : |
| 1612 | scatterwalk_ffwd(ctx->dst, req->dst, req->assoclen)); |
| 1613 | |
| 1614 | if (use_dma) { |
| 1615 | /* Update the Mode Register for DMA transfers. */ |
| 1616 | mr = atmel_aes_read(dd, AES_MR); |
| 1617 | mr &= ~(AES_MR_SMOD_MASK | AES_MR_DUALBUFF); |
| 1618 | mr |= AES_MR_SMOD_IDATAR0; |
| 1619 | if (dd->caps.has_dualbuff) |
| 1620 | mr |= AES_MR_DUALBUFF; |
| 1621 | atmel_aes_write(dd, AES_MR, mr); |
| 1622 | |
| 1623 | return atmel_aes_dma_start(dd, src, dst, ctx->textlen, |
| 1624 | atmel_aes_gcm_tag_init); |
| 1625 | } |
| 1626 | |
| 1627 | return atmel_aes_cpu_start(dd, src, dst, ctx->textlen, |
| 1628 | atmel_aes_gcm_tag_init); |
| 1629 | } |
| 1630 | |
| 1631 | static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd) |
| 1632 | { |
| 1633 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1634 | struct aead_request *req = aead_request_cast(dd->areq); |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 1635 | __be64 *data = dd->buf; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1636 | |
| 1637 | if (likely(dd->flags & AES_FLAGS_GTAGEN)) { |
| 1638 | if (!(atmel_aes_read(dd, AES_ISR) & AES_INT_TAGRDY)) { |
| 1639 | dd->resume = atmel_aes_gcm_tag_init; |
| 1640 | atmel_aes_write(dd, AES_IER, AES_INT_TAGRDY); |
| 1641 | return -EINPROGRESS; |
| 1642 | } |
| 1643 | |
| 1644 | return atmel_aes_gcm_finalize(dd); |
| 1645 | } |
| 1646 | |
| 1647 | /* Read the GCM Intermediate Hash Word Registers. */ |
| 1648 | atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash); |
| 1649 | |
| 1650 | data[0] = cpu_to_be64(req->assoclen * 8); |
| 1651 | data[1] = cpu_to_be64(ctx->textlen * 8); |
| 1652 | |
| 1653 | return atmel_aes_gcm_ghash(dd, (const u32 *)data, AES_BLOCK_SIZE, |
| 1654 | ctx->ghash, ctx->ghash, atmel_aes_gcm_tag); |
| 1655 | } |
| 1656 | |
| 1657 | static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd) |
| 1658 | { |
| 1659 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1660 | unsigned long flags; |
| 1661 | |
| 1662 | /* |
| 1663 | * Change mode to CTR to complete the tag generation. |
| 1664 | * Use J0 as Initialization Vector. |
| 1665 | */ |
| 1666 | flags = dd->flags; |
| 1667 | dd->flags &= ~(AES_FLAGS_OPMODE_MASK | AES_FLAGS_GTAGEN); |
| 1668 | dd->flags |= AES_FLAGS_CTR; |
| 1669 | atmel_aes_write_ctrl(dd, false, ctx->j0); |
| 1670 | dd->flags = flags; |
| 1671 | |
| 1672 | atmel_aes_write_block(dd, AES_IDATAR(0), ctx->ghash); |
| 1673 | return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_finalize); |
| 1674 | } |
| 1675 | |
| 1676 | static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd) |
| 1677 | { |
| 1678 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1679 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1680 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 1681 | bool enc = atmel_aes_is_encrypt(dd); |
| 1682 | u32 offset, authsize, itag[4], *otag = ctx->tag; |
| 1683 | int err; |
| 1684 | |
| 1685 | /* Read the computed tag. */ |
| 1686 | if (likely(dd->flags & AES_FLAGS_GTAGEN)) |
| 1687 | atmel_aes_read_block(dd, AES_TAGR(0), ctx->tag); |
| 1688 | else |
| 1689 | atmel_aes_read_block(dd, AES_ODATAR(0), ctx->tag); |
| 1690 | |
| 1691 | offset = req->assoclen + ctx->textlen; |
| 1692 | authsize = crypto_aead_authsize(tfm); |
| 1693 | if (enc) { |
| 1694 | scatterwalk_map_and_copy(otag, req->dst, offset, authsize, 1); |
| 1695 | err = 0; |
| 1696 | } else { |
| 1697 | scatterwalk_map_and_copy(itag, req->src, offset, authsize, 0); |
| 1698 | err = crypto_memneq(itag, otag, authsize) ? -EBADMSG : 0; |
| 1699 | } |
| 1700 | |
| 1701 | return atmel_aes_complete(dd, err); |
| 1702 | } |
| 1703 | |
| 1704 | static int atmel_aes_gcm_crypt(struct aead_request *req, |
| 1705 | unsigned long mode) |
| 1706 | { |
| 1707 | struct atmel_aes_base_ctx *ctx; |
| 1708 | struct atmel_aes_reqctx *rctx; |
| 1709 | struct atmel_aes_dev *dd; |
| 1710 | |
| 1711 | ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
| 1712 | ctx->block_size = AES_BLOCK_SIZE; |
Romain Izard | 9130801 | 2017-10-31 16:25:23 +0100 | [diff] [blame] | 1713 | ctx->is_aead = true; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1714 | |
| 1715 | dd = atmel_aes_find_dev(ctx); |
| 1716 | if (!dd) |
| 1717 | return -ENODEV; |
| 1718 | |
| 1719 | rctx = aead_request_ctx(req); |
| 1720 | rctx->mode = AES_FLAGS_GCM | mode; |
| 1721 | |
| 1722 | return atmel_aes_handle_queue(dd, &req->base); |
| 1723 | } |
| 1724 | |
| 1725 | static int atmel_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key, |
| 1726 | unsigned int keylen) |
| 1727 | { |
| 1728 | struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm); |
| 1729 | |
| 1730 | if (keylen != AES_KEYSIZE_256 && |
| 1731 | keylen != AES_KEYSIZE_192 && |
Eric Biggers | 674f368 | 2019-12-30 21:19:36 -0600 | [diff] [blame] | 1732 | keylen != AES_KEYSIZE_128) |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1733 | return -EINVAL; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1734 | |
| 1735 | memcpy(ctx->key, key, keylen); |
| 1736 | ctx->keylen = keylen; |
| 1737 | |
| 1738 | return 0; |
| 1739 | } |
| 1740 | |
| 1741 | static int atmel_aes_gcm_setauthsize(struct crypto_aead *tfm, |
| 1742 | unsigned int authsize) |
| 1743 | { |
Tudor Ambarus | 7db15aa | 2019-12-05 09:54:08 +0000 | [diff] [blame] | 1744 | return crypto_gcm_check_authsize(authsize); |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | static int atmel_aes_gcm_encrypt(struct aead_request *req) |
| 1748 | { |
| 1749 | return atmel_aes_gcm_crypt(req, AES_FLAGS_ENCRYPT); |
| 1750 | } |
| 1751 | |
| 1752 | static int atmel_aes_gcm_decrypt(struct aead_request *req) |
| 1753 | { |
| 1754 | return atmel_aes_gcm_crypt(req, 0); |
| 1755 | } |
| 1756 | |
| 1757 | static int atmel_aes_gcm_init(struct crypto_aead *tfm) |
| 1758 | { |
| 1759 | struct atmel_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm); |
| 1760 | |
| 1761 | crypto_aead_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx)); |
| 1762 | ctx->base.start = atmel_aes_gcm_start; |
| 1763 | |
| 1764 | return 0; |
| 1765 | } |
| 1766 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1767 | static struct aead_alg aes_gcm_alg = { |
| 1768 | .setkey = atmel_aes_gcm_setkey, |
| 1769 | .setauthsize = atmel_aes_gcm_setauthsize, |
| 1770 | .encrypt = atmel_aes_gcm_encrypt, |
| 1771 | .decrypt = atmel_aes_gcm_decrypt, |
| 1772 | .init = atmel_aes_gcm_init, |
Corentin LABBE | 219d51c | 2017-08-22 10:08:12 +0200 | [diff] [blame] | 1773 | .ivsize = GCM_AES_IV_SIZE, |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1774 | .maxauthsize = AES_BLOCK_SIZE, |
| 1775 | |
| 1776 | .base = { |
| 1777 | .cra_name = "gcm(aes)", |
| 1778 | .cra_driver_name = "atmel-gcm-aes", |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1779 | .cra_blocksize = 1, |
| 1780 | .cra_ctxsize = sizeof(struct atmel_aes_gcm_ctx), |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1781 | }, |
| 1782 | }; |
| 1783 | |
| 1784 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1785 | /* xts functions */ |
| 1786 | |
| 1787 | static inline struct atmel_aes_xts_ctx * |
| 1788 | atmel_aes_xts_ctx_cast(struct atmel_aes_base_ctx *ctx) |
| 1789 | { |
| 1790 | return container_of(ctx, struct atmel_aes_xts_ctx, base); |
| 1791 | } |
| 1792 | |
| 1793 | static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd); |
| 1794 | |
| 1795 | static int atmel_aes_xts_start(struct atmel_aes_dev *dd) |
| 1796 | { |
| 1797 | struct atmel_aes_xts_ctx *ctx = atmel_aes_xts_ctx_cast(dd->ctx); |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1798 | struct skcipher_request *req = skcipher_request_cast(dd->areq); |
| 1799 | struct atmel_aes_reqctx *rctx = skcipher_request_ctx(req); |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1800 | unsigned long flags; |
| 1801 | int err; |
| 1802 | |
| 1803 | atmel_aes_set_mode(dd, rctx); |
| 1804 | |
| 1805 | err = atmel_aes_hw_init(dd); |
| 1806 | if (err) |
| 1807 | return atmel_aes_complete(dd, err); |
| 1808 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1809 | /* Compute the tweak value from req->iv with ecb(aes). */ |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1810 | flags = dd->flags; |
| 1811 | dd->flags &= ~AES_FLAGS_MODE_MASK; |
| 1812 | dd->flags |= (AES_FLAGS_ECB | AES_FLAGS_ENCRYPT); |
| 1813 | atmel_aes_write_ctrl_key(dd, false, NULL, |
| 1814 | ctx->key2, ctx->base.keylen); |
| 1815 | dd->flags = flags; |
| 1816 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1817 | atmel_aes_write_block(dd, AES_IDATAR(0), req->iv); |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1818 | return atmel_aes_wait_for_data_ready(dd, atmel_aes_xts_process_data); |
| 1819 | } |
| 1820 | |
| 1821 | static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd) |
| 1822 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1823 | struct skcipher_request *req = skcipher_request_cast(dd->areq); |
| 1824 | bool use_dma = (req->cryptlen >= ATMEL_AES_DMA_THRESHOLD); |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1825 | u32 tweak[AES_BLOCK_SIZE / sizeof(u32)]; |
Ben Dooks (Codethink) | 49c4cd8 | 2019-10-16 13:26:33 +0100 | [diff] [blame] | 1826 | static const __le32 one[AES_BLOCK_SIZE / sizeof(u32)] = {cpu_to_le32(1), }; |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1827 | u8 *tweak_bytes = (u8 *)tweak; |
| 1828 | int i; |
| 1829 | |
| 1830 | /* Read the computed ciphered tweak value. */ |
| 1831 | atmel_aes_read_block(dd, AES_ODATAR(0), tweak); |
| 1832 | /* |
| 1833 | * Hardware quirk: |
| 1834 | * the order of the ciphered tweak bytes need to be reversed before |
| 1835 | * writing them into the ODATARx registers. |
| 1836 | */ |
| 1837 | for (i = 0; i < AES_BLOCK_SIZE/2; ++i) { |
| 1838 | u8 tmp = tweak_bytes[AES_BLOCK_SIZE - 1 - i]; |
| 1839 | |
| 1840 | tweak_bytes[AES_BLOCK_SIZE - 1 - i] = tweak_bytes[i]; |
| 1841 | tweak_bytes[i] = tmp; |
| 1842 | } |
| 1843 | |
| 1844 | /* Process the data. */ |
| 1845 | atmel_aes_write_ctrl(dd, use_dma, NULL); |
| 1846 | atmel_aes_write_block(dd, AES_TWR(0), tweak); |
| 1847 | atmel_aes_write_block(dd, AES_ALPHAR(0), one); |
| 1848 | if (use_dma) |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1849 | return atmel_aes_dma_start(dd, req->src, req->dst, |
| 1850 | req->cryptlen, |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1851 | atmel_aes_transfer_complete); |
| 1852 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1853 | return atmel_aes_cpu_start(dd, req->src, req->dst, req->cryptlen, |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1854 | atmel_aes_transfer_complete); |
| 1855 | } |
| 1856 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1857 | static int atmel_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key, |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1858 | unsigned int keylen) |
| 1859 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1860 | struct atmel_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1861 | int err; |
| 1862 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1863 | err = xts_check_key(crypto_skcipher_tfm(tfm), key, keylen); |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1864 | if (err) |
| 1865 | return err; |
| 1866 | |
| 1867 | memcpy(ctx->base.key, key, keylen/2); |
| 1868 | memcpy(ctx->key2, key + keylen/2, keylen/2); |
| 1869 | ctx->base.keylen = keylen/2; |
| 1870 | |
| 1871 | return 0; |
| 1872 | } |
| 1873 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1874 | static int atmel_aes_xts_encrypt(struct skcipher_request *req) |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1875 | { |
| 1876 | return atmel_aes_crypt(req, AES_FLAGS_XTS | AES_FLAGS_ENCRYPT); |
| 1877 | } |
| 1878 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1879 | static int atmel_aes_xts_decrypt(struct skcipher_request *req) |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1880 | { |
| 1881 | return atmel_aes_crypt(req, AES_FLAGS_XTS); |
| 1882 | } |
| 1883 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1884 | static int atmel_aes_xts_init_tfm(struct crypto_skcipher *tfm) |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1885 | { |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1886 | struct atmel_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1887 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1888 | crypto_skcipher_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx)); |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1889 | ctx->base.start = atmel_aes_xts_start; |
| 1890 | |
| 1891 | return 0; |
| 1892 | } |
| 1893 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1894 | static struct skcipher_alg aes_xts_alg = { |
| 1895 | .base.cra_name = "xts(aes)", |
| 1896 | .base.cra_driver_name = "atmel-xts-aes", |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1897 | .base.cra_blocksize = AES_BLOCK_SIZE, |
| 1898 | .base.cra_ctxsize = sizeof(struct atmel_aes_xts_ctx), |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 1899 | |
| 1900 | .min_keysize = 2 * AES_MIN_KEY_SIZE, |
| 1901 | .max_keysize = 2 * AES_MAX_KEY_SIZE, |
| 1902 | .ivsize = AES_BLOCK_SIZE, |
| 1903 | .setkey = atmel_aes_xts_setkey, |
| 1904 | .encrypt = atmel_aes_xts_encrypt, |
| 1905 | .decrypt = atmel_aes_xts_decrypt, |
| 1906 | .init = atmel_aes_xts_init_tfm, |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1907 | }; |
| 1908 | |
Herbert Xu | 1520c72 | 2019-10-28 15:39:07 +0800 | [diff] [blame] | 1909 | #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC) |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 1910 | /* authenc aead functions */ |
| 1911 | |
| 1912 | static int atmel_aes_authenc_start(struct atmel_aes_dev *dd); |
| 1913 | static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err, |
| 1914 | bool is_async); |
| 1915 | static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err, |
| 1916 | bool is_async); |
| 1917 | static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd); |
| 1918 | static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err, |
| 1919 | bool is_async); |
| 1920 | |
| 1921 | static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err) |
| 1922 | { |
| 1923 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1924 | struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req); |
| 1925 | |
| 1926 | if (err && (dd->flags & AES_FLAGS_OWN_SHA)) |
| 1927 | atmel_sha_authenc_abort(&rctx->auth_req); |
| 1928 | dd->flags &= ~AES_FLAGS_OWN_SHA; |
| 1929 | } |
| 1930 | |
| 1931 | static int atmel_aes_authenc_start(struct atmel_aes_dev *dd) |
| 1932 | { |
| 1933 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1934 | struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req); |
| 1935 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 1936 | struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm); |
| 1937 | int err; |
| 1938 | |
| 1939 | atmel_aes_set_mode(dd, &rctx->base); |
| 1940 | |
| 1941 | err = atmel_aes_hw_init(dd); |
| 1942 | if (err) |
| 1943 | return atmel_aes_complete(dd, err); |
| 1944 | |
| 1945 | return atmel_sha_authenc_schedule(&rctx->auth_req, ctx->auth, |
| 1946 | atmel_aes_authenc_init, dd); |
| 1947 | } |
| 1948 | |
| 1949 | static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err, |
| 1950 | bool is_async) |
| 1951 | { |
| 1952 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1953 | struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req); |
| 1954 | |
| 1955 | if (is_async) |
| 1956 | dd->is_async = true; |
| 1957 | if (err) |
| 1958 | return atmel_aes_complete(dd, err); |
| 1959 | |
| 1960 | /* If here, we've got the ownership of the SHA device. */ |
| 1961 | dd->flags |= AES_FLAGS_OWN_SHA; |
| 1962 | |
| 1963 | /* Configure the SHA device. */ |
| 1964 | return atmel_sha_authenc_init(&rctx->auth_req, |
| 1965 | req->src, req->assoclen, |
| 1966 | rctx->textlen, |
| 1967 | atmel_aes_authenc_transfer, dd); |
| 1968 | } |
| 1969 | |
| 1970 | static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err, |
| 1971 | bool is_async) |
| 1972 | { |
| 1973 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1974 | struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req); |
| 1975 | bool enc = atmel_aes_is_encrypt(dd); |
| 1976 | struct scatterlist *src, *dst; |
Herbert Xu | 427e6e3 | 2019-10-28 15:45:02 +0800 | [diff] [blame] | 1977 | __be32 iv[AES_BLOCK_SIZE / sizeof(u32)]; |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 1978 | u32 emr; |
| 1979 | |
| 1980 | if (is_async) |
| 1981 | dd->is_async = true; |
| 1982 | if (err) |
| 1983 | return atmel_aes_complete(dd, err); |
| 1984 | |
| 1985 | /* Prepare src and dst scatter-lists to transfer cipher/plain texts. */ |
| 1986 | src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen); |
| 1987 | dst = src; |
| 1988 | |
| 1989 | if (req->src != req->dst) |
| 1990 | dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen); |
| 1991 | |
| 1992 | /* Configure the AES device. */ |
| 1993 | memcpy(iv, req->iv, sizeof(iv)); |
| 1994 | |
| 1995 | /* |
| 1996 | * Here we always set the 2nd parameter of atmel_aes_write_ctrl() to |
| 1997 | * 'true' even if the data transfer is actually performed by the CPU (so |
| 1998 | * not by the DMA) because we must force the AES_MR_SMOD bitfield to the |
| 1999 | * value AES_MR_SMOD_IDATAR0. Indeed, both AES_MR_SMOD and SHA_MR_SMOD |
| 2000 | * must be set to *_MR_SMOD_IDATAR0. |
| 2001 | */ |
| 2002 | atmel_aes_write_ctrl(dd, true, iv); |
| 2003 | emr = AES_EMR_PLIPEN; |
| 2004 | if (!enc) |
| 2005 | emr |= AES_EMR_PLIPD; |
| 2006 | atmel_aes_write(dd, AES_EMR, emr); |
| 2007 | |
| 2008 | /* Transfer data. */ |
| 2009 | return atmel_aes_dma_start(dd, src, dst, rctx->textlen, |
| 2010 | atmel_aes_authenc_digest); |
| 2011 | } |
| 2012 | |
| 2013 | static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd) |
| 2014 | { |
| 2015 | struct aead_request *req = aead_request_cast(dd->areq); |
| 2016 | struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req); |
| 2017 | |
| 2018 | /* atmel_sha_authenc_final() releases the SHA device. */ |
| 2019 | dd->flags &= ~AES_FLAGS_OWN_SHA; |
| 2020 | return atmel_sha_authenc_final(&rctx->auth_req, |
| 2021 | rctx->digest, sizeof(rctx->digest), |
| 2022 | atmel_aes_authenc_final, dd); |
| 2023 | } |
| 2024 | |
| 2025 | static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err, |
| 2026 | bool is_async) |
| 2027 | { |
| 2028 | struct aead_request *req = aead_request_cast(dd->areq); |
| 2029 | struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req); |
| 2030 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 2031 | bool enc = atmel_aes_is_encrypt(dd); |
| 2032 | u32 idigest[SHA512_DIGEST_SIZE / sizeof(u32)], *odigest = rctx->digest; |
| 2033 | u32 offs, authsize; |
| 2034 | |
| 2035 | if (is_async) |
| 2036 | dd->is_async = true; |
| 2037 | if (err) |
| 2038 | goto complete; |
| 2039 | |
| 2040 | offs = req->assoclen + rctx->textlen; |
| 2041 | authsize = crypto_aead_authsize(tfm); |
| 2042 | if (enc) { |
| 2043 | scatterwalk_map_and_copy(odigest, req->dst, offs, authsize, 1); |
| 2044 | } else { |
| 2045 | scatterwalk_map_and_copy(idigest, req->src, offs, authsize, 0); |
| 2046 | if (crypto_memneq(idigest, odigest, authsize)) |
| 2047 | err = -EBADMSG; |
| 2048 | } |
| 2049 | |
| 2050 | complete: |
| 2051 | return atmel_aes_complete(dd, err); |
| 2052 | } |
| 2053 | |
| 2054 | static int atmel_aes_authenc_setkey(struct crypto_aead *tfm, const u8 *key, |
| 2055 | unsigned int keylen) |
| 2056 | { |
| 2057 | struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm); |
| 2058 | struct crypto_authenc_keys keys; |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2059 | int err; |
| 2060 | |
| 2061 | if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) |
| 2062 | goto badkey; |
| 2063 | |
| 2064 | if (keys.enckeylen > sizeof(ctx->base.key)) |
| 2065 | goto badkey; |
| 2066 | |
| 2067 | /* Save auth key. */ |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2068 | err = atmel_sha_authenc_setkey(ctx->auth, |
| 2069 | keys.authkey, keys.authkeylen, |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 2070 | crypto_aead_get_flags(tfm)); |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2071 | if (err) { |
| 2072 | memzero_explicit(&keys, sizeof(keys)); |
| 2073 | return err; |
| 2074 | } |
| 2075 | |
| 2076 | /* Save enc key. */ |
| 2077 | ctx->base.keylen = keys.enckeylen; |
| 2078 | memcpy(ctx->base.key, keys.enckey, keys.enckeylen); |
| 2079 | |
| 2080 | memzero_explicit(&keys, sizeof(keys)); |
| 2081 | return 0; |
| 2082 | |
| 2083 | badkey: |
Antoine Tenart | 5d804a51 | 2018-02-23 10:01:40 +0100 | [diff] [blame] | 2084 | memzero_explicit(&keys, sizeof(keys)); |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2085 | return -EINVAL; |
| 2086 | } |
| 2087 | |
| 2088 | static int atmel_aes_authenc_init_tfm(struct crypto_aead *tfm, |
| 2089 | unsigned long auth_mode) |
| 2090 | { |
| 2091 | struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm); |
| 2092 | unsigned int auth_reqsize = atmel_sha_authenc_get_reqsize(); |
| 2093 | |
| 2094 | ctx->auth = atmel_sha_authenc_spawn(auth_mode); |
| 2095 | if (IS_ERR(ctx->auth)) |
| 2096 | return PTR_ERR(ctx->auth); |
| 2097 | |
| 2098 | crypto_aead_set_reqsize(tfm, (sizeof(struct atmel_aes_authenc_reqctx) + |
| 2099 | auth_reqsize)); |
| 2100 | ctx->base.start = atmel_aes_authenc_start; |
| 2101 | |
| 2102 | return 0; |
| 2103 | } |
| 2104 | |
| 2105 | static int atmel_aes_authenc_hmac_sha1_init_tfm(struct crypto_aead *tfm) |
| 2106 | { |
| 2107 | return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA1); |
| 2108 | } |
| 2109 | |
| 2110 | static int atmel_aes_authenc_hmac_sha224_init_tfm(struct crypto_aead *tfm) |
| 2111 | { |
| 2112 | return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA224); |
| 2113 | } |
| 2114 | |
| 2115 | static int atmel_aes_authenc_hmac_sha256_init_tfm(struct crypto_aead *tfm) |
| 2116 | { |
| 2117 | return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA256); |
| 2118 | } |
| 2119 | |
| 2120 | static int atmel_aes_authenc_hmac_sha384_init_tfm(struct crypto_aead *tfm) |
| 2121 | { |
| 2122 | return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA384); |
| 2123 | } |
| 2124 | |
| 2125 | static int atmel_aes_authenc_hmac_sha512_init_tfm(struct crypto_aead *tfm) |
| 2126 | { |
| 2127 | return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA512); |
| 2128 | } |
| 2129 | |
| 2130 | static void atmel_aes_authenc_exit_tfm(struct crypto_aead *tfm) |
| 2131 | { |
| 2132 | struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm); |
| 2133 | |
| 2134 | atmel_sha_authenc_free(ctx->auth); |
| 2135 | } |
| 2136 | |
| 2137 | static int atmel_aes_authenc_crypt(struct aead_request *req, |
| 2138 | unsigned long mode) |
| 2139 | { |
| 2140 | struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req); |
| 2141 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 2142 | struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm); |
| 2143 | u32 authsize = crypto_aead_authsize(tfm); |
| 2144 | bool enc = (mode & AES_FLAGS_ENCRYPT); |
| 2145 | struct atmel_aes_dev *dd; |
| 2146 | |
| 2147 | /* Compute text length. */ |
| 2148 | if (!enc && req->cryptlen < authsize) |
| 2149 | return -EINVAL; |
| 2150 | rctx->textlen = req->cryptlen - (enc ? 0 : authsize); |
| 2151 | |
| 2152 | /* |
| 2153 | * Currently, empty messages are not supported yet: |
| 2154 | * the SHA auto-padding can be used only on non-empty messages. |
| 2155 | * Hence a special case needs to be implemented for empty message. |
| 2156 | */ |
| 2157 | if (!rctx->textlen && !req->assoclen) |
| 2158 | return -EINVAL; |
| 2159 | |
| 2160 | rctx->base.mode = mode; |
| 2161 | ctx->block_size = AES_BLOCK_SIZE; |
Romain Izard | 9130801 | 2017-10-31 16:25:23 +0100 | [diff] [blame] | 2162 | ctx->is_aead = true; |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2163 | |
| 2164 | dd = atmel_aes_find_dev(ctx); |
| 2165 | if (!dd) |
| 2166 | return -ENODEV; |
| 2167 | |
| 2168 | return atmel_aes_handle_queue(dd, &req->base); |
| 2169 | } |
| 2170 | |
| 2171 | static int atmel_aes_authenc_cbc_aes_encrypt(struct aead_request *req) |
| 2172 | { |
| 2173 | return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT); |
| 2174 | } |
| 2175 | |
| 2176 | static int atmel_aes_authenc_cbc_aes_decrypt(struct aead_request *req) |
| 2177 | { |
| 2178 | return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC); |
| 2179 | } |
| 2180 | |
| 2181 | static struct aead_alg aes_authenc_algs[] = { |
| 2182 | { |
| 2183 | .setkey = atmel_aes_authenc_setkey, |
| 2184 | .encrypt = atmel_aes_authenc_cbc_aes_encrypt, |
| 2185 | .decrypt = atmel_aes_authenc_cbc_aes_decrypt, |
| 2186 | .init = atmel_aes_authenc_hmac_sha1_init_tfm, |
| 2187 | .exit = atmel_aes_authenc_exit_tfm, |
| 2188 | .ivsize = AES_BLOCK_SIZE, |
| 2189 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 2190 | |
| 2191 | .base = { |
| 2192 | .cra_name = "authenc(hmac(sha1),cbc(aes))", |
| 2193 | .cra_driver_name = "atmel-authenc-hmac-sha1-cbc-aes", |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2194 | .cra_blocksize = AES_BLOCK_SIZE, |
| 2195 | .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx), |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2196 | }, |
| 2197 | }, |
| 2198 | { |
| 2199 | .setkey = atmel_aes_authenc_setkey, |
| 2200 | .encrypt = atmel_aes_authenc_cbc_aes_encrypt, |
| 2201 | .decrypt = atmel_aes_authenc_cbc_aes_decrypt, |
| 2202 | .init = atmel_aes_authenc_hmac_sha224_init_tfm, |
| 2203 | .exit = atmel_aes_authenc_exit_tfm, |
| 2204 | .ivsize = AES_BLOCK_SIZE, |
| 2205 | .maxauthsize = SHA224_DIGEST_SIZE, |
| 2206 | |
| 2207 | .base = { |
| 2208 | .cra_name = "authenc(hmac(sha224),cbc(aes))", |
| 2209 | .cra_driver_name = "atmel-authenc-hmac-sha224-cbc-aes", |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2210 | .cra_blocksize = AES_BLOCK_SIZE, |
| 2211 | .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx), |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2212 | }, |
| 2213 | }, |
| 2214 | { |
| 2215 | .setkey = atmel_aes_authenc_setkey, |
| 2216 | .encrypt = atmel_aes_authenc_cbc_aes_encrypt, |
| 2217 | .decrypt = atmel_aes_authenc_cbc_aes_decrypt, |
| 2218 | .init = atmel_aes_authenc_hmac_sha256_init_tfm, |
| 2219 | .exit = atmel_aes_authenc_exit_tfm, |
| 2220 | .ivsize = AES_BLOCK_SIZE, |
| 2221 | .maxauthsize = SHA256_DIGEST_SIZE, |
| 2222 | |
| 2223 | .base = { |
| 2224 | .cra_name = "authenc(hmac(sha256),cbc(aes))", |
| 2225 | .cra_driver_name = "atmel-authenc-hmac-sha256-cbc-aes", |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2226 | .cra_blocksize = AES_BLOCK_SIZE, |
| 2227 | .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx), |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2228 | }, |
| 2229 | }, |
| 2230 | { |
| 2231 | .setkey = atmel_aes_authenc_setkey, |
| 2232 | .encrypt = atmel_aes_authenc_cbc_aes_encrypt, |
| 2233 | .decrypt = atmel_aes_authenc_cbc_aes_decrypt, |
| 2234 | .init = atmel_aes_authenc_hmac_sha384_init_tfm, |
| 2235 | .exit = atmel_aes_authenc_exit_tfm, |
| 2236 | .ivsize = AES_BLOCK_SIZE, |
| 2237 | .maxauthsize = SHA384_DIGEST_SIZE, |
| 2238 | |
| 2239 | .base = { |
| 2240 | .cra_name = "authenc(hmac(sha384),cbc(aes))", |
| 2241 | .cra_driver_name = "atmel-authenc-hmac-sha384-cbc-aes", |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2242 | .cra_blocksize = AES_BLOCK_SIZE, |
| 2243 | .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx), |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2244 | }, |
| 2245 | }, |
| 2246 | { |
| 2247 | .setkey = atmel_aes_authenc_setkey, |
| 2248 | .encrypt = atmel_aes_authenc_cbc_aes_encrypt, |
| 2249 | .decrypt = atmel_aes_authenc_cbc_aes_decrypt, |
| 2250 | .init = atmel_aes_authenc_hmac_sha512_init_tfm, |
| 2251 | .exit = atmel_aes_authenc_exit_tfm, |
| 2252 | .ivsize = AES_BLOCK_SIZE, |
| 2253 | .maxauthsize = SHA512_DIGEST_SIZE, |
| 2254 | |
| 2255 | .base = { |
| 2256 | .cra_name = "authenc(hmac(sha512),cbc(aes))", |
| 2257 | .cra_driver_name = "atmel-authenc-hmac-sha512-cbc-aes", |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2258 | .cra_blocksize = AES_BLOCK_SIZE, |
| 2259 | .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx), |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2260 | }, |
| 2261 | }, |
| 2262 | }; |
| 2263 | #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */ |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2264 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 2265 | /* Probe functions */ |
| 2266 | |
| 2267 | static int atmel_aes_buff_init(struct atmel_aes_dev *dd) |
| 2268 | { |
| 2269 | dd->buf = (void *)__get_free_pages(GFP_KERNEL, ATMEL_AES_BUFFER_ORDER); |
| 2270 | dd->buflen = ATMEL_AES_BUFFER_SIZE; |
| 2271 | dd->buflen &= ~(AES_BLOCK_SIZE - 1); |
| 2272 | |
| 2273 | if (!dd->buf) { |
| 2274 | dev_err(dd->dev, "unable to alloc pages.\n"); |
| 2275 | return -ENOMEM; |
| 2276 | } |
| 2277 | |
| 2278 | return 0; |
| 2279 | } |
| 2280 | |
| 2281 | static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd) |
| 2282 | { |
| 2283 | free_page((unsigned long)dd->buf); |
| 2284 | } |
| 2285 | |
Tudor Ambarus | 827a98d | 2019-12-13 09:54:49 +0000 | [diff] [blame] | 2286 | static int atmel_aes_dma_init(struct atmel_aes_dev *dd) |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 2287 | { |
Peter Ujfalusi | 62f72cb | 2019-11-21 12:16:00 +0200 | [diff] [blame] | 2288 | int ret; |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 2289 | |
| 2290 | /* Try to grab 2 DMA channels */ |
Peter Ujfalusi | 62f72cb | 2019-11-21 12:16:00 +0200 | [diff] [blame] | 2291 | dd->src.chan = dma_request_chan(dd->dev, "tx"); |
| 2292 | if (IS_ERR(dd->src.chan)) { |
| 2293 | ret = PTR_ERR(dd->src.chan); |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 2294 | goto err_dma_in; |
Peter Ujfalusi | 62f72cb | 2019-11-21 12:16:00 +0200 | [diff] [blame] | 2295 | } |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 2296 | |
Peter Ujfalusi | 62f72cb | 2019-11-21 12:16:00 +0200 | [diff] [blame] | 2297 | dd->dst.chan = dma_request_chan(dd->dev, "rx"); |
| 2298 | if (IS_ERR(dd->dst.chan)) { |
| 2299 | ret = PTR_ERR(dd->dst.chan); |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 2300 | goto err_dma_out; |
Peter Ujfalusi | 62f72cb | 2019-11-21 12:16:00 +0200 | [diff] [blame] | 2301 | } |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 2302 | |
| 2303 | return 0; |
| 2304 | |
| 2305 | err_dma_out: |
| 2306 | dma_release_channel(dd->src.chan); |
| 2307 | err_dma_in: |
Tudor Ambarus | e9ce6ae | 2019-12-13 09:54:54 +0000 | [diff] [blame] | 2308 | dev_err(dd->dev, "no DMA channel available\n"); |
Peter Ujfalusi | 62f72cb | 2019-11-21 12:16:00 +0200 | [diff] [blame] | 2309 | return ret; |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd) |
| 2313 | { |
| 2314 | dma_release_channel(dd->dst.chan); |
| 2315 | dma_release_channel(dd->src.chan); |
| 2316 | } |
| 2317 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2318 | static void atmel_aes_queue_task(unsigned long data) |
| 2319 | { |
| 2320 | struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data; |
| 2321 | |
| 2322 | atmel_aes_handle_queue(dd, NULL); |
| 2323 | } |
| 2324 | |
| 2325 | static void atmel_aes_done_task(unsigned long data) |
| 2326 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 2327 | struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data; |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 2328 | |
| 2329 | dd->is_async = true; |
| 2330 | (void)dd->resume(dd); |
| 2331 | } |
| 2332 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2333 | static irqreturn_t atmel_aes_irq(int irq, void *dev_id) |
| 2334 | { |
| 2335 | struct atmel_aes_dev *aes_dd = dev_id; |
| 2336 | u32 reg; |
| 2337 | |
| 2338 | reg = atmel_aes_read(aes_dd, AES_ISR); |
| 2339 | if (reg & atmel_aes_read(aes_dd, AES_IMR)) { |
| 2340 | atmel_aes_write(aes_dd, AES_IDR, reg); |
| 2341 | if (AES_FLAGS_BUSY & aes_dd->flags) |
| 2342 | tasklet_schedule(&aes_dd->done_task); |
| 2343 | else |
| 2344 | dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n"); |
| 2345 | return IRQ_HANDLED; |
| 2346 | } |
| 2347 | |
| 2348 | return IRQ_NONE; |
| 2349 | } |
| 2350 | |
| 2351 | static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd) |
| 2352 | { |
| 2353 | int i; |
| 2354 | |
Herbert Xu | 1520c72 | 2019-10-28 15:39:07 +0800 | [diff] [blame] | 2355 | #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC) |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2356 | if (dd->caps.has_authenc) |
| 2357 | for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++) |
| 2358 | crypto_unregister_aead(&aes_authenc_algs[i]); |
| 2359 | #endif |
| 2360 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2361 | if (dd->caps.has_xts) |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 2362 | crypto_unregister_skcipher(&aes_xts_alg); |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2363 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2364 | if (dd->caps.has_gcm) |
| 2365 | crypto_unregister_aead(&aes_gcm_alg); |
| 2366 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2367 | if (dd->caps.has_cfb64) |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 2368 | crypto_unregister_skcipher(&aes_cfb64_alg); |
Cyrille Pitchen | 924a8bc | 2015-12-17 17:48:35 +0100 | [diff] [blame] | 2369 | |
| 2370 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 2371 | crypto_unregister_skcipher(&aes_algs[i]); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2372 | } |
| 2373 | |
Tudor Ambarus | aebe5bd | 2019-12-05 13:48:39 +0000 | [diff] [blame] | 2374 | static void atmel_aes_crypto_alg_init(struct crypto_alg *alg) |
| 2375 | { |
| 2376 | alg->cra_flags = CRYPTO_ALG_ASYNC; |
| 2377 | alg->cra_alignmask = 0xf; |
| 2378 | alg->cra_priority = ATMEL_AES_PRIORITY; |
| 2379 | alg->cra_module = THIS_MODULE; |
| 2380 | } |
| 2381 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2382 | static int atmel_aes_register_algs(struct atmel_aes_dev *dd) |
| 2383 | { |
| 2384 | int err, i, j; |
| 2385 | |
| 2386 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) { |
Tudor Ambarus | aebe5bd | 2019-12-05 13:48:39 +0000 | [diff] [blame] | 2387 | atmel_aes_crypto_alg_init(&aes_algs[i].base); |
| 2388 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 2389 | err = crypto_register_skcipher(&aes_algs[i]); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2390 | if (err) |
| 2391 | goto err_aes_algs; |
| 2392 | } |
| 2393 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2394 | if (dd->caps.has_cfb64) { |
Tudor Ambarus | aebe5bd | 2019-12-05 13:48:39 +0000 | [diff] [blame] | 2395 | atmel_aes_crypto_alg_init(&aes_cfb64_alg.base); |
| 2396 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 2397 | err = crypto_register_skcipher(&aes_cfb64_alg); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2398 | if (err) |
| 2399 | goto err_aes_cfb64_alg; |
| 2400 | } |
| 2401 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2402 | if (dd->caps.has_gcm) { |
Tudor Ambarus | aebe5bd | 2019-12-05 13:48:39 +0000 | [diff] [blame] | 2403 | atmel_aes_crypto_alg_init(&aes_gcm_alg.base); |
| 2404 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2405 | err = crypto_register_aead(&aes_gcm_alg); |
| 2406 | if (err) |
| 2407 | goto err_aes_gcm_alg; |
| 2408 | } |
| 2409 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2410 | if (dd->caps.has_xts) { |
Tudor Ambarus | aebe5bd | 2019-12-05 13:48:39 +0000 | [diff] [blame] | 2411 | atmel_aes_crypto_alg_init(&aes_xts_alg.base); |
| 2412 | |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 2413 | err = crypto_register_skcipher(&aes_xts_alg); |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2414 | if (err) |
| 2415 | goto err_aes_xts_alg; |
| 2416 | } |
| 2417 | |
Herbert Xu | 1520c72 | 2019-10-28 15:39:07 +0800 | [diff] [blame] | 2418 | #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC) |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2419 | if (dd->caps.has_authenc) { |
| 2420 | for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++) { |
Tudor Ambarus | aebe5bd | 2019-12-05 13:48:39 +0000 | [diff] [blame] | 2421 | atmel_aes_crypto_alg_init(&aes_authenc_algs[i].base); |
| 2422 | |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2423 | err = crypto_register_aead(&aes_authenc_algs[i]); |
| 2424 | if (err) |
| 2425 | goto err_aes_authenc_alg; |
| 2426 | } |
| 2427 | } |
| 2428 | #endif |
| 2429 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2430 | return 0; |
| 2431 | |
Herbert Xu | 1520c72 | 2019-10-28 15:39:07 +0800 | [diff] [blame] | 2432 | #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC) |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2433 | /* i = ARRAY_SIZE(aes_authenc_algs); */ |
| 2434 | err_aes_authenc_alg: |
| 2435 | for (j = 0; j < i; j++) |
| 2436 | crypto_unregister_aead(&aes_authenc_algs[j]); |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 2437 | crypto_unregister_skcipher(&aes_xts_alg); |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2438 | #endif |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2439 | err_aes_xts_alg: |
| 2440 | crypto_unregister_aead(&aes_gcm_alg); |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2441 | err_aes_gcm_alg: |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 2442 | crypto_unregister_skcipher(&aes_cfb64_alg); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2443 | err_aes_cfb64_alg: |
| 2444 | i = ARRAY_SIZE(aes_algs); |
| 2445 | err_aes_algs: |
| 2446 | for (j = 0; j < i; j++) |
Ard Biesheuvel | 7ada42d | 2019-11-09 18:09:33 +0100 | [diff] [blame] | 2447 | crypto_unregister_skcipher(&aes_algs[j]); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2448 | |
| 2449 | return err; |
| 2450 | } |
| 2451 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2452 | static void atmel_aes_get_cap(struct atmel_aes_dev *dd) |
| 2453 | { |
| 2454 | dd->caps.has_dualbuff = 0; |
| 2455 | dd->caps.has_cfb64 = 0; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2456 | dd->caps.has_gcm = 0; |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2457 | dd->caps.has_xts = 0; |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2458 | dd->caps.has_authenc = 0; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2459 | dd->caps.max_burst_size = 1; |
| 2460 | |
| 2461 | /* keep only major version number */ |
| 2462 | switch (dd->hw_version & 0xff0) { |
Leilei Zhao | 973e209 | 2015-12-17 17:48:32 +0100 | [diff] [blame] | 2463 | case 0x500: |
| 2464 | dd->caps.has_dualbuff = 1; |
| 2465 | dd->caps.has_cfb64 = 1; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2466 | dd->caps.has_gcm = 1; |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2467 | dd->caps.has_xts = 1; |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2468 | dd->caps.has_authenc = 1; |
Leilei Zhao | 973e209 | 2015-12-17 17:48:32 +0100 | [diff] [blame] | 2469 | dd->caps.max_burst_size = 4; |
| 2470 | break; |
Leilei Zhao | cf1f0d1 | 2015-04-07 17:45:02 +0800 | [diff] [blame] | 2471 | case 0x200: |
| 2472 | dd->caps.has_dualbuff = 1; |
| 2473 | dd->caps.has_cfb64 = 1; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2474 | dd->caps.has_gcm = 1; |
Leilei Zhao | cf1f0d1 | 2015-04-07 17:45:02 +0800 | [diff] [blame] | 2475 | dd->caps.max_burst_size = 4; |
| 2476 | break; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2477 | case 0x130: |
| 2478 | dd->caps.has_dualbuff = 1; |
| 2479 | dd->caps.has_cfb64 = 1; |
| 2480 | dd->caps.max_burst_size = 4; |
| 2481 | break; |
| 2482 | case 0x120: |
| 2483 | break; |
| 2484 | default: |
| 2485 | dev_warn(dd->dev, |
| 2486 | "Unmanaged aes version, set minimum capabilities\n"); |
| 2487 | break; |
| 2488 | } |
| 2489 | } |
| 2490 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 2491 | #if defined(CONFIG_OF) |
| 2492 | static const struct of_device_id atmel_aes_dt_ids[] = { |
| 2493 | { .compatible = "atmel,at91sam9g46-aes" }, |
| 2494 | { /* sentinel */ } |
| 2495 | }; |
| 2496 | MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids); |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 2497 | #endif |
| 2498 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 2499 | static int atmel_aes_probe(struct platform_device *pdev) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2500 | { |
| 2501 | struct atmel_aes_dev *aes_dd; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2502 | struct device *dev = &pdev->dev; |
| 2503 | struct resource *aes_res; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2504 | int err; |
| 2505 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2506 | aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL); |
Tudor Ambarus | c9063a0 | 2019-12-05 09:53:51 +0000 | [diff] [blame] | 2507 | if (!aes_dd) |
| 2508 | return -ENOMEM; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2509 | |
| 2510 | aes_dd->dev = dev; |
| 2511 | |
| 2512 | platform_set_drvdata(pdev, aes_dd); |
| 2513 | |
| 2514 | INIT_LIST_HEAD(&aes_dd->list); |
Leilei Zhao | 8a10eb8 | 2015-04-07 17:45:09 +0800 | [diff] [blame] | 2515 | spin_lock_init(&aes_dd->lock); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2516 | |
| 2517 | tasklet_init(&aes_dd->done_task, atmel_aes_done_task, |
| 2518 | (unsigned long)aes_dd); |
| 2519 | tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task, |
| 2520 | (unsigned long)aes_dd); |
| 2521 | |
| 2522 | crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH); |
| 2523 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2524 | /* Get the base address */ |
| 2525 | aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2526 | if (!aes_res) { |
| 2527 | dev_err(dev, "no MEM resource info\n"); |
| 2528 | err = -ENODEV; |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2529 | goto err_tasklet_kill; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2530 | } |
| 2531 | aes_dd->phys_base = aes_res->start; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2532 | |
| 2533 | /* Get the IRQ */ |
| 2534 | aes_dd->irq = platform_get_irq(pdev, 0); |
| 2535 | if (aes_dd->irq < 0) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2536 | err = aes_dd->irq; |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2537 | goto err_tasklet_kill; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2538 | } |
| 2539 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2540 | err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq, |
| 2541 | IRQF_SHARED, "atmel-aes", aes_dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2542 | if (err) { |
| 2543 | dev_err(dev, "unable to request aes irq.\n"); |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2544 | goto err_tasklet_kill; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | /* Initializing the clock */ |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2548 | aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk"); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2549 | if (IS_ERR(aes_dd->iclk)) { |
Colin Ian King | be20835 | 2015-02-28 20:40:10 +0000 | [diff] [blame] | 2550 | dev_err(dev, "clock initialization failed.\n"); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2551 | err = PTR_ERR(aes_dd->iclk); |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2552 | goto err_tasklet_kill; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2553 | } |
| 2554 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2555 | aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res); |
Vladimir Zapolskiy | 9b52d55 | 2016-03-06 03:21:52 +0200 | [diff] [blame] | 2556 | if (IS_ERR(aes_dd->io_base)) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2557 | dev_err(dev, "can't ioremap\n"); |
Vladimir Zapolskiy | 9b52d55 | 2016-03-06 03:21:52 +0200 | [diff] [blame] | 2558 | err = PTR_ERR(aes_dd->io_base); |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2559 | goto err_tasklet_kill; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2560 | } |
| 2561 | |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 2562 | err = clk_prepare(aes_dd->iclk); |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 2563 | if (err) |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2564 | goto err_tasklet_kill; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2565 | |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 2566 | err = atmel_aes_hw_version_init(aes_dd); |
| 2567 | if (err) |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2568 | goto err_iclk_unprepare; |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 2569 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2570 | atmel_aes_get_cap(aes_dd); |
| 2571 | |
Herbert Xu | 1520c72 | 2019-10-28 15:39:07 +0800 | [diff] [blame] | 2572 | #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC) |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2573 | if (aes_dd->caps.has_authenc && !atmel_sha_authenc_is_ready()) { |
| 2574 | err = -EPROBE_DEFER; |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2575 | goto err_iclk_unprepare; |
Cyrille Pitchen | 89a82ef | 2017-01-26 17:07:56 +0100 | [diff] [blame] | 2576 | } |
| 2577 | #endif |
| 2578 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2579 | err = atmel_aes_buff_init(aes_dd); |
| 2580 | if (err) |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2581 | goto err_iclk_unprepare; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2582 | |
Tudor Ambarus | 827a98d | 2019-12-13 09:54:49 +0000 | [diff] [blame] | 2583 | err = atmel_aes_dma_init(aes_dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2584 | if (err) |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2585 | goto err_buff_cleanup; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2586 | |
| 2587 | spin_lock(&atmel_aes.lock); |
| 2588 | list_add_tail(&aes_dd->list, &atmel_aes.dev_list); |
| 2589 | spin_unlock(&atmel_aes.lock); |
| 2590 | |
| 2591 | err = atmel_aes_register_algs(aes_dd); |
| 2592 | if (err) |
| 2593 | goto err_algs; |
| 2594 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 2595 | dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n", |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 2596 | dma_chan_name(aes_dd->src.chan), |
| 2597 | dma_chan_name(aes_dd->dst.chan)); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2598 | |
| 2599 | return 0; |
| 2600 | |
| 2601 | err_algs: |
| 2602 | spin_lock(&atmel_aes.lock); |
| 2603 | list_del(&aes_dd->list); |
| 2604 | spin_unlock(&atmel_aes.lock); |
| 2605 | atmel_aes_dma_cleanup(aes_dd); |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2606 | err_buff_cleanup: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2607 | atmel_aes_buff_cleanup(aes_dd); |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2608 | err_iclk_unprepare: |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 2609 | clk_unprepare(aes_dd->iclk); |
Tudor Ambarus | e783651 | 2019-12-05 09:53:53 +0000 | [diff] [blame] | 2610 | err_tasklet_kill: |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2611 | tasklet_kill(&aes_dd->done_task); |
| 2612 | tasklet_kill(&aes_dd->queue_task); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2613 | |
| 2614 | return err; |
| 2615 | } |
| 2616 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 2617 | static int atmel_aes_remove(struct platform_device *pdev) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2618 | { |
Wei Yongjun | fc78334 | 2016-10-24 14:51:22 +0000 | [diff] [blame] | 2619 | struct atmel_aes_dev *aes_dd; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2620 | |
| 2621 | aes_dd = platform_get_drvdata(pdev); |
| 2622 | if (!aes_dd) |
| 2623 | return -ENODEV; |
| 2624 | spin_lock(&atmel_aes.lock); |
| 2625 | list_del(&aes_dd->list); |
| 2626 | spin_unlock(&atmel_aes.lock); |
| 2627 | |
| 2628 | atmel_aes_unregister_algs(aes_dd); |
| 2629 | |
| 2630 | tasklet_kill(&aes_dd->done_task); |
| 2631 | tasklet_kill(&aes_dd->queue_task); |
| 2632 | |
| 2633 | atmel_aes_dma_cleanup(aes_dd); |
Cyrille Pitchen | 2a37782 | 2015-12-17 17:48:46 +0100 | [diff] [blame] | 2634 | atmel_aes_buff_cleanup(aes_dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2635 | |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 2636 | clk_unprepare(aes_dd->iclk); |
| 2637 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2638 | return 0; |
| 2639 | } |
| 2640 | |
| 2641 | static struct platform_driver atmel_aes_driver = { |
| 2642 | .probe = atmel_aes_probe, |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 2643 | .remove = atmel_aes_remove, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2644 | .driver = { |
| 2645 | .name = "atmel_aes", |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 2646 | .of_match_table = of_match_ptr(atmel_aes_dt_ids), |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2647 | }, |
| 2648 | }; |
| 2649 | |
| 2650 | module_platform_driver(atmel_aes_driver); |
| 2651 | |
| 2652 | MODULE_DESCRIPTION("Atmel AES hw acceleration support."); |
| 2653 | MODULE_LICENSE("GPL v2"); |
| 2654 | MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique"); |