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