Tom Lendacky | 7c18537 | 2013-11-12 11:46:34 -0600 | [diff] [blame] | 1 | /* |
| 2 | * AMD Cryptographic Coprocessor (CCP) AES CMAC crypto API support |
| 3 | * |
| 4 | * Copyright (C) 2013 Advanced Micro Devices, Inc. |
| 5 | * |
| 6 | * Author: Tom Lendacky <thomas.lendacky@amd.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/scatterlist.h> |
| 17 | #include <linux/crypto.h> |
| 18 | #include <crypto/algapi.h> |
| 19 | #include <crypto/aes.h> |
| 20 | #include <crypto/hash.h> |
| 21 | #include <crypto/internal/hash.h> |
| 22 | #include <crypto/scatterwalk.h> |
| 23 | |
| 24 | #include "ccp-crypto.h" |
| 25 | |
| 26 | |
| 27 | static int ccp_aes_cmac_complete(struct crypto_async_request *async_req, |
| 28 | int ret) |
| 29 | { |
| 30 | struct ahash_request *req = ahash_request_cast(async_req); |
| 31 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 32 | struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req); |
| 33 | unsigned int digest_size = crypto_ahash_digestsize(tfm); |
| 34 | |
| 35 | if (ret) |
| 36 | goto e_free; |
| 37 | |
| 38 | if (rctx->hash_rem) { |
| 39 | /* Save remaining data to buffer */ |
| 40 | scatterwalk_map_and_copy(rctx->buf, rctx->cmd.u.aes.src, |
| 41 | rctx->hash_cnt, rctx->hash_rem, 0); |
| 42 | rctx->buf_count = rctx->hash_rem; |
| 43 | } else |
| 44 | rctx->buf_count = 0; |
| 45 | |
Tom Lendacky | 393897c | 2014-01-06 13:34:11 -0600 | [diff] [blame^] | 46 | /* Update result area if supplied */ |
| 47 | if (req->result) |
| 48 | memcpy(req->result, rctx->iv, digest_size); |
Tom Lendacky | 7c18537 | 2013-11-12 11:46:34 -0600 | [diff] [blame] | 49 | |
| 50 | e_free: |
| 51 | sg_free_table(&rctx->data_sg); |
| 52 | |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | static int ccp_do_cmac_update(struct ahash_request *req, unsigned int nbytes, |
| 57 | unsigned int final) |
| 58 | { |
| 59 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 60 | struct ccp_ctx *ctx = crypto_ahash_ctx(tfm); |
| 61 | struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req); |
| 62 | struct scatterlist *sg, *cmac_key_sg = NULL; |
| 63 | unsigned int block_size = |
| 64 | crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm)); |
| 65 | unsigned int len, need_pad, sg_count; |
Tom Lendacky | 5258de8 | 2014-01-06 13:33:59 -0600 | [diff] [blame] | 66 | gfp_t gfp; |
Tom Lendacky | 7c18537 | 2013-11-12 11:46:34 -0600 | [diff] [blame] | 67 | int ret; |
| 68 | |
Tom Lendacky | 369f3da | 2013-12-10 10:38:44 -0600 | [diff] [blame] | 69 | if (!ctx->u.aes.key_len) |
Tom Lendacky | 7c18537 | 2013-11-12 11:46:34 -0600 | [diff] [blame] | 70 | return -EINVAL; |
Tom Lendacky | 7c18537 | 2013-11-12 11:46:34 -0600 | [diff] [blame] | 71 | |
| 72 | if (nbytes) |
| 73 | rctx->null_msg = 0; |
| 74 | |
| 75 | if (!final && ((nbytes + rctx->buf_count) <= block_size)) { |
| 76 | scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src, |
| 77 | 0, nbytes, 0); |
| 78 | rctx->buf_count += nbytes; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | len = rctx->buf_count + nbytes; |
| 84 | |
| 85 | rctx->final = final; |
| 86 | rctx->hash_cnt = final ? len : len & ~(block_size - 1); |
| 87 | rctx->hash_rem = final ? 0 : len & (block_size - 1); |
| 88 | if (!final && (rctx->hash_cnt == len)) { |
| 89 | /* CCP can't do zero length final, so keep some data around */ |
| 90 | rctx->hash_cnt -= block_size; |
| 91 | rctx->hash_rem = block_size; |
| 92 | } |
| 93 | |
| 94 | if (final && (rctx->null_msg || (len & (block_size - 1)))) |
| 95 | need_pad = 1; |
| 96 | else |
| 97 | need_pad = 0; |
| 98 | |
| 99 | sg_init_one(&rctx->iv_sg, rctx->iv, sizeof(rctx->iv)); |
| 100 | |
| 101 | /* Build the data scatterlist table - allocate enough entries for all |
| 102 | * possible data pieces (buffer, input data, padding) |
| 103 | */ |
| 104 | sg_count = (nbytes) ? sg_nents(req->src) + 2 : 2; |
Tom Lendacky | 5258de8 | 2014-01-06 13:33:59 -0600 | [diff] [blame] | 105 | gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? |
| 106 | GFP_KERNEL : GFP_ATOMIC; |
| 107 | ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp); |
Tom Lendacky | 7c18537 | 2013-11-12 11:46:34 -0600 | [diff] [blame] | 108 | if (ret) |
| 109 | return ret; |
| 110 | |
| 111 | sg = NULL; |
| 112 | if (rctx->buf_count) { |
| 113 | sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count); |
| 114 | sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg); |
| 115 | } |
| 116 | |
| 117 | if (nbytes) |
| 118 | sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src); |
| 119 | |
| 120 | if (need_pad) { |
| 121 | int pad_length = block_size - (len & (block_size - 1)); |
| 122 | |
| 123 | rctx->hash_cnt += pad_length; |
| 124 | |
| 125 | memset(rctx->pad, 0, sizeof(rctx->pad)); |
| 126 | rctx->pad[0] = 0x80; |
| 127 | sg_init_one(&rctx->pad_sg, rctx->pad, pad_length); |
| 128 | sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->pad_sg); |
| 129 | } |
Tom Lendacky | 77dc4a5 | 2014-01-06 13:34:05 -0600 | [diff] [blame] | 130 | if (sg) { |
Tom Lendacky | 7c18537 | 2013-11-12 11:46:34 -0600 | [diff] [blame] | 131 | sg_mark_end(sg); |
Tom Lendacky | 77dc4a5 | 2014-01-06 13:34:05 -0600 | [diff] [blame] | 132 | sg = rctx->data_sg.sgl; |
| 133 | } |
Tom Lendacky | 7c18537 | 2013-11-12 11:46:34 -0600 | [diff] [blame] | 134 | |
| 135 | /* Initialize the K1/K2 scatterlist */ |
| 136 | if (final) |
| 137 | cmac_key_sg = (need_pad) ? &ctx->u.aes.k2_sg |
| 138 | : &ctx->u.aes.k1_sg; |
| 139 | |
| 140 | memset(&rctx->cmd, 0, sizeof(rctx->cmd)); |
| 141 | INIT_LIST_HEAD(&rctx->cmd.entry); |
| 142 | rctx->cmd.engine = CCP_ENGINE_AES; |
| 143 | rctx->cmd.u.aes.type = ctx->u.aes.type; |
| 144 | rctx->cmd.u.aes.mode = ctx->u.aes.mode; |
| 145 | rctx->cmd.u.aes.action = CCP_AES_ACTION_ENCRYPT; |
| 146 | rctx->cmd.u.aes.key = &ctx->u.aes.key_sg; |
| 147 | rctx->cmd.u.aes.key_len = ctx->u.aes.key_len; |
| 148 | rctx->cmd.u.aes.iv = &rctx->iv_sg; |
| 149 | rctx->cmd.u.aes.iv_len = AES_BLOCK_SIZE; |
Tom Lendacky | 77dc4a5 | 2014-01-06 13:34:05 -0600 | [diff] [blame] | 150 | rctx->cmd.u.aes.src = sg; |
Tom Lendacky | 7c18537 | 2013-11-12 11:46:34 -0600 | [diff] [blame] | 151 | rctx->cmd.u.aes.src_len = rctx->hash_cnt; |
| 152 | rctx->cmd.u.aes.dst = NULL; |
| 153 | rctx->cmd.u.aes.cmac_key = cmac_key_sg; |
| 154 | rctx->cmd.u.aes.cmac_key_len = ctx->u.aes.kn_len; |
| 155 | rctx->cmd.u.aes.cmac_final = final; |
| 156 | |
| 157 | ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd); |
| 158 | |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | static int ccp_aes_cmac_init(struct ahash_request *req) |
| 163 | { |
| 164 | struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req); |
| 165 | |
| 166 | memset(rctx, 0, sizeof(*rctx)); |
| 167 | |
| 168 | rctx->null_msg = 1; |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int ccp_aes_cmac_update(struct ahash_request *req) |
| 174 | { |
| 175 | return ccp_do_cmac_update(req, req->nbytes, 0); |
| 176 | } |
| 177 | |
| 178 | static int ccp_aes_cmac_final(struct ahash_request *req) |
| 179 | { |
| 180 | return ccp_do_cmac_update(req, 0, 1); |
| 181 | } |
| 182 | |
| 183 | static int ccp_aes_cmac_finup(struct ahash_request *req) |
| 184 | { |
| 185 | return ccp_do_cmac_update(req, req->nbytes, 1); |
| 186 | } |
| 187 | |
| 188 | static int ccp_aes_cmac_digest(struct ahash_request *req) |
| 189 | { |
| 190 | int ret; |
| 191 | |
| 192 | ret = ccp_aes_cmac_init(req); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | |
| 196 | return ccp_do_cmac_update(req, req->nbytes, 1); |
| 197 | } |
| 198 | |
| 199 | static int ccp_aes_cmac_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 200 | unsigned int key_len) |
| 201 | { |
| 202 | struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm)); |
| 203 | struct ccp_crypto_ahash_alg *alg = |
| 204 | ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm)); |
| 205 | u64 k0_hi, k0_lo, k1_hi, k1_lo, k2_hi, k2_lo; |
| 206 | u64 rb_hi = 0x00, rb_lo = 0x87; |
| 207 | __be64 *gk; |
| 208 | int ret; |
| 209 | |
| 210 | switch (key_len) { |
| 211 | case AES_KEYSIZE_128: |
| 212 | ctx->u.aes.type = CCP_AES_TYPE_128; |
| 213 | break; |
| 214 | case AES_KEYSIZE_192: |
| 215 | ctx->u.aes.type = CCP_AES_TYPE_192; |
| 216 | break; |
| 217 | case AES_KEYSIZE_256: |
| 218 | ctx->u.aes.type = CCP_AES_TYPE_256; |
| 219 | break; |
| 220 | default: |
| 221 | crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | ctx->u.aes.mode = alg->mode; |
| 225 | |
| 226 | /* Set to zero until complete */ |
| 227 | ctx->u.aes.key_len = 0; |
| 228 | |
| 229 | /* Set the key for the AES cipher used to generate the keys */ |
| 230 | ret = crypto_cipher_setkey(ctx->u.aes.tfm_cipher, key, key_len); |
| 231 | if (ret) |
| 232 | return ret; |
| 233 | |
| 234 | /* Encrypt a block of zeroes - use key area in context */ |
| 235 | memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key)); |
| 236 | crypto_cipher_encrypt_one(ctx->u.aes.tfm_cipher, ctx->u.aes.key, |
| 237 | ctx->u.aes.key); |
| 238 | |
| 239 | /* Generate K1 and K2 */ |
| 240 | k0_hi = be64_to_cpu(*((__be64 *)ctx->u.aes.key)); |
| 241 | k0_lo = be64_to_cpu(*((__be64 *)ctx->u.aes.key + 1)); |
| 242 | |
| 243 | k1_hi = (k0_hi << 1) | (k0_lo >> 63); |
| 244 | k1_lo = k0_lo << 1; |
| 245 | if (ctx->u.aes.key[0] & 0x80) { |
| 246 | k1_hi ^= rb_hi; |
| 247 | k1_lo ^= rb_lo; |
| 248 | } |
| 249 | gk = (__be64 *)ctx->u.aes.k1; |
| 250 | *gk = cpu_to_be64(k1_hi); |
| 251 | gk++; |
| 252 | *gk = cpu_to_be64(k1_lo); |
| 253 | |
| 254 | k2_hi = (k1_hi << 1) | (k1_lo >> 63); |
| 255 | k2_lo = k1_lo << 1; |
| 256 | if (ctx->u.aes.k1[0] & 0x80) { |
| 257 | k2_hi ^= rb_hi; |
| 258 | k2_lo ^= rb_lo; |
| 259 | } |
| 260 | gk = (__be64 *)ctx->u.aes.k2; |
| 261 | *gk = cpu_to_be64(k2_hi); |
| 262 | gk++; |
| 263 | *gk = cpu_to_be64(k2_lo); |
| 264 | |
| 265 | ctx->u.aes.kn_len = sizeof(ctx->u.aes.k1); |
| 266 | sg_init_one(&ctx->u.aes.k1_sg, ctx->u.aes.k1, sizeof(ctx->u.aes.k1)); |
| 267 | sg_init_one(&ctx->u.aes.k2_sg, ctx->u.aes.k2, sizeof(ctx->u.aes.k2)); |
| 268 | |
| 269 | /* Save the supplied key */ |
| 270 | memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key)); |
| 271 | memcpy(ctx->u.aes.key, key, key_len); |
| 272 | ctx->u.aes.key_len = key_len; |
| 273 | sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len); |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static int ccp_aes_cmac_cra_init(struct crypto_tfm *tfm) |
| 279 | { |
| 280 | struct ccp_ctx *ctx = crypto_tfm_ctx(tfm); |
| 281 | struct crypto_ahash *ahash = __crypto_ahash_cast(tfm); |
| 282 | struct crypto_cipher *cipher_tfm; |
| 283 | |
| 284 | ctx->complete = ccp_aes_cmac_complete; |
| 285 | ctx->u.aes.key_len = 0; |
| 286 | |
| 287 | crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_aes_cmac_req_ctx)); |
| 288 | |
| 289 | cipher_tfm = crypto_alloc_cipher("aes", 0, |
| 290 | CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK); |
| 291 | if (IS_ERR(cipher_tfm)) { |
| 292 | pr_warn("could not load aes cipher driver\n"); |
| 293 | return PTR_ERR(cipher_tfm); |
| 294 | } |
| 295 | ctx->u.aes.tfm_cipher = cipher_tfm; |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static void ccp_aes_cmac_cra_exit(struct crypto_tfm *tfm) |
| 301 | { |
| 302 | struct ccp_ctx *ctx = crypto_tfm_ctx(tfm); |
| 303 | |
| 304 | if (ctx->u.aes.tfm_cipher) |
| 305 | crypto_free_cipher(ctx->u.aes.tfm_cipher); |
| 306 | ctx->u.aes.tfm_cipher = NULL; |
| 307 | } |
| 308 | |
| 309 | int ccp_register_aes_cmac_algs(struct list_head *head) |
| 310 | { |
| 311 | struct ccp_crypto_ahash_alg *ccp_alg; |
| 312 | struct ahash_alg *alg; |
| 313 | struct hash_alg_common *halg; |
| 314 | struct crypto_alg *base; |
| 315 | int ret; |
| 316 | |
| 317 | ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL); |
| 318 | if (!ccp_alg) |
| 319 | return -ENOMEM; |
| 320 | |
| 321 | INIT_LIST_HEAD(&ccp_alg->entry); |
| 322 | ccp_alg->mode = CCP_AES_MODE_CMAC; |
| 323 | |
| 324 | alg = &ccp_alg->alg; |
| 325 | alg->init = ccp_aes_cmac_init; |
| 326 | alg->update = ccp_aes_cmac_update; |
| 327 | alg->final = ccp_aes_cmac_final; |
| 328 | alg->finup = ccp_aes_cmac_finup; |
| 329 | alg->digest = ccp_aes_cmac_digest; |
| 330 | alg->setkey = ccp_aes_cmac_setkey; |
| 331 | |
| 332 | halg = &alg->halg; |
| 333 | halg->digestsize = AES_BLOCK_SIZE; |
| 334 | |
| 335 | base = &halg->base; |
| 336 | snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "cmac(aes)"); |
| 337 | snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "cmac-aes-ccp"); |
| 338 | base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC | |
| 339 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
| 340 | CRYPTO_ALG_NEED_FALLBACK; |
| 341 | base->cra_blocksize = AES_BLOCK_SIZE; |
| 342 | base->cra_ctxsize = sizeof(struct ccp_ctx); |
| 343 | base->cra_priority = CCP_CRA_PRIORITY; |
| 344 | base->cra_type = &crypto_ahash_type; |
| 345 | base->cra_init = ccp_aes_cmac_cra_init; |
| 346 | base->cra_exit = ccp_aes_cmac_cra_exit; |
| 347 | base->cra_module = THIS_MODULE; |
| 348 | |
| 349 | ret = crypto_register_ahash(alg); |
| 350 | if (ret) { |
| 351 | pr_err("%s ahash algorithm registration error (%d)\n", |
| 352 | base->cra_name, ret); |
| 353 | kfree(ccp_alg); |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | list_add(&ccp_alg->entry, head); |
| 358 | |
| 359 | return 0; |
| 360 | } |