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