Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 1 | /* |
| 2 | * AMD Cryptographic Coprocessor (CCP) SHA crypto API support |
| 3 | * |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 4 | * Copyright (C) 2013,2016 Advanced Micro Devices, Inc. |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 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/hash.h> |
| 20 | #include <crypto/internal/hash.h> |
| 21 | #include <crypto/sha.h> |
| 22 | #include <crypto/scatterwalk.h> |
| 23 | |
| 24 | #include "ccp-crypto.h" |
| 25 | |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 26 | static int ccp_sha_complete(struct crypto_async_request *async_req, int ret) |
| 27 | { |
| 28 | struct ahash_request *req = ahash_request_cast(async_req); |
| 29 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 30 | struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 31 | unsigned int digest_size = crypto_ahash_digestsize(tfm); |
| 32 | |
| 33 | if (ret) |
| 34 | goto e_free; |
| 35 | |
| 36 | if (rctx->hash_rem) { |
| 37 | /* Save remaining data to buffer */ |
Tom Lendacky | 81a59f0 | 2014-01-06 13:34:17 -0600 | [diff] [blame] | 38 | unsigned int offset = rctx->nbytes - rctx->hash_rem; |
Tom Lendacky | 8db8846 | 2015-02-03 13:07:05 -0600 | [diff] [blame] | 39 | |
Tom Lendacky | 81a59f0 | 2014-01-06 13:34:17 -0600 | [diff] [blame] | 40 | scatterwalk_map_and_copy(rctx->buf, rctx->src, |
| 41 | offset, rctx->hash_rem, 0); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 42 | rctx->buf_count = rctx->hash_rem; |
Tom Lendacky | 8db8846 | 2015-02-03 13:07:05 -0600 | [diff] [blame] | 43 | } else { |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 44 | rctx->buf_count = 0; |
Tom Lendacky | 8db8846 | 2015-02-03 13:07:05 -0600 | [diff] [blame] | 45 | } |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 46 | |
Tom Lendacky | 393897c | 2014-01-06 13:34:11 -0600 | [diff] [blame] | 47 | /* Update result area if supplied */ |
| 48 | if (req->result) |
| 49 | memcpy(req->result, rctx->ctx, digest_size); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 50 | |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 51 | e_free: |
| 52 | sg_free_table(&rctx->data_sg); |
| 53 | |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | static int ccp_do_sha_update(struct ahash_request *req, unsigned int nbytes, |
| 58 | unsigned int final) |
| 59 | { |
| 60 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
Tom Lendacky | c11baa0 | 2014-01-24 16:18:02 -0600 | [diff] [blame] | 61 | struct ccp_ctx *ctx = crypto_ahash_ctx(tfm); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 62 | struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 63 | struct scatterlist *sg; |
| 64 | unsigned int block_size = |
| 65 | crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm)); |
Tom Lendacky | 81a59f0 | 2014-01-06 13:34:17 -0600 | [diff] [blame] | 66 | unsigned int sg_count; |
Tom Lendacky | 5258de8 | 2014-01-06 13:33:59 -0600 | [diff] [blame] | 67 | gfp_t gfp; |
Tom Lendacky | 81a59f0 | 2014-01-06 13:34:17 -0600 | [diff] [blame] | 68 | u64 len; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 69 | int ret; |
| 70 | |
Tom Lendacky | 81a59f0 | 2014-01-06 13:34:17 -0600 | [diff] [blame] | 71 | len = (u64)rctx->buf_count + (u64)nbytes; |
| 72 | |
| 73 | if (!final && (len <= block_size)) { |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 74 | scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src, |
| 75 | 0, nbytes, 0); |
| 76 | rctx->buf_count += nbytes; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Tom Lendacky | 81a59f0 | 2014-01-06 13:34:17 -0600 | [diff] [blame] | 81 | rctx->src = req->src; |
| 82 | rctx->nbytes = nbytes; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 83 | |
| 84 | rctx->final = final; |
Tom Lendacky | 81a59f0 | 2014-01-06 13:34:17 -0600 | [diff] [blame] | 85 | rctx->hash_rem = final ? 0 : len & (block_size - 1); |
| 86 | rctx->hash_cnt = len - rctx->hash_rem; |
| 87 | if (!final && !rctx->hash_rem) { |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 88 | /* CCP can't do zero length final, so keep some data around */ |
| 89 | rctx->hash_cnt -= block_size; |
| 90 | rctx->hash_rem = block_size; |
| 91 | } |
| 92 | |
| 93 | /* Initialize the context scatterlist */ |
| 94 | sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx)); |
| 95 | |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 96 | sg = NULL; |
Tom Lendacky | 77dc4a5 | 2014-01-06 13:34:05 -0600 | [diff] [blame] | 97 | if (rctx->buf_count && nbytes) { |
| 98 | /* Build the data scatterlist table - allocate enough entries |
| 99 | * for both data pieces (buffer and input data) |
| 100 | */ |
| 101 | gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? |
| 102 | GFP_KERNEL : GFP_ATOMIC; |
| 103 | sg_count = sg_nents(req->src) + 1; |
| 104 | ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp); |
| 105 | if (ret) |
| 106 | return ret; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 107 | |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 108 | sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count); |
| 109 | sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg); |
Tom Lendacky | 355eba5 | 2015-10-01 16:32:31 -0500 | [diff] [blame] | 110 | if (!sg) { |
| 111 | ret = -EINVAL; |
| 112 | goto e_free; |
| 113 | } |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 114 | sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src); |
Tom Lendacky | 355eba5 | 2015-10-01 16:32:31 -0500 | [diff] [blame] | 115 | if (!sg) { |
| 116 | ret = -EINVAL; |
| 117 | goto e_free; |
| 118 | } |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 119 | sg_mark_end(sg); |
| 120 | |
Tom Lendacky | 77dc4a5 | 2014-01-06 13:34:05 -0600 | [diff] [blame] | 121 | sg = rctx->data_sg.sgl; |
| 122 | } else if (rctx->buf_count) { |
| 123 | sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count); |
| 124 | |
| 125 | sg = &rctx->buf_sg; |
| 126 | } else if (nbytes) { |
| 127 | sg = req->src; |
| 128 | } |
| 129 | |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 130 | rctx->msg_bits += (rctx->hash_cnt << 3); /* Total in bits */ |
| 131 | |
| 132 | memset(&rctx->cmd, 0, sizeof(rctx->cmd)); |
| 133 | INIT_LIST_HEAD(&rctx->cmd.entry); |
| 134 | rctx->cmd.engine = CCP_ENGINE_SHA; |
| 135 | rctx->cmd.u.sha.type = rctx->type; |
| 136 | rctx->cmd.u.sha.ctx = &rctx->ctx_sg; |
| 137 | rctx->cmd.u.sha.ctx_len = sizeof(rctx->ctx); |
Tom Lendacky | 77dc4a5 | 2014-01-06 13:34:05 -0600 | [diff] [blame] | 138 | rctx->cmd.u.sha.src = sg; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 139 | rctx->cmd.u.sha.src_len = rctx->hash_cnt; |
Tom Lendacky | c11baa0 | 2014-01-24 16:18:02 -0600 | [diff] [blame] | 140 | rctx->cmd.u.sha.opad = ctx->u.sha.key_len ? |
| 141 | &ctx->u.sha.opad_sg : NULL; |
| 142 | rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ? |
| 143 | ctx->u.sha.opad_count : 0; |
| 144 | rctx->cmd.u.sha.first = rctx->first; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 145 | rctx->cmd.u.sha.final = rctx->final; |
| 146 | rctx->cmd.u.sha.msg_bits = rctx->msg_bits; |
| 147 | |
| 148 | rctx->first = 0; |
| 149 | |
| 150 | ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd); |
| 151 | |
| 152 | return ret; |
Tom Lendacky | 355eba5 | 2015-10-01 16:32:31 -0500 | [diff] [blame] | 153 | |
| 154 | e_free: |
| 155 | sg_free_table(&rctx->data_sg); |
| 156 | |
| 157 | return ret; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int ccp_sha_init(struct ahash_request *req) |
| 161 | { |
| 162 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
Tom Lendacky | 77dc4a5 | 2014-01-06 13:34:05 -0600 | [diff] [blame] | 163 | struct ccp_ctx *ctx = crypto_ahash_ctx(tfm); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 164 | struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 165 | struct ccp_crypto_ahash_alg *alg = |
| 166 | ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm)); |
Tom Lendacky | 77dc4a5 | 2014-01-06 13:34:05 -0600 | [diff] [blame] | 167 | unsigned int block_size = |
| 168 | crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm)); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 169 | |
| 170 | memset(rctx, 0, sizeof(*rctx)); |
| 171 | |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 172 | rctx->type = alg->type; |
| 173 | rctx->first = 1; |
| 174 | |
Tom Lendacky | 77dc4a5 | 2014-01-06 13:34:05 -0600 | [diff] [blame] | 175 | if (ctx->u.sha.key_len) { |
| 176 | /* Buffer the HMAC key for first update */ |
| 177 | memcpy(rctx->buf, ctx->u.sha.ipad, block_size); |
| 178 | rctx->buf_count = block_size; |
| 179 | } |
| 180 | |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int ccp_sha_update(struct ahash_request *req) |
| 185 | { |
| 186 | return ccp_do_sha_update(req, req->nbytes, 0); |
| 187 | } |
| 188 | |
| 189 | static int ccp_sha_final(struct ahash_request *req) |
| 190 | { |
| 191 | return ccp_do_sha_update(req, 0, 1); |
| 192 | } |
| 193 | |
| 194 | static int ccp_sha_finup(struct ahash_request *req) |
| 195 | { |
| 196 | return ccp_do_sha_update(req, req->nbytes, 1); |
| 197 | } |
| 198 | |
| 199 | static int ccp_sha_digest(struct ahash_request *req) |
| 200 | { |
Tom Lendacky | 82d1585 | 2014-01-06 13:34:23 -0600 | [diff] [blame] | 201 | int ret; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 202 | |
Tom Lendacky | 82d1585 | 2014-01-06 13:34:23 -0600 | [diff] [blame] | 203 | ret = ccp_sha_init(req); |
| 204 | if (ret) |
| 205 | return ret; |
| 206 | |
| 207 | return ccp_sha_finup(req); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 208 | } |
| 209 | |
Tom Lendacky | 952bce9 | 2016-01-12 11:17:38 -0600 | [diff] [blame] | 210 | static int ccp_sha_export(struct ahash_request *req, void *out) |
| 211 | { |
| 212 | struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req); |
Tom Lendacky | b31dde2 | 2016-02-02 11:38:21 -0600 | [diff] [blame] | 213 | struct ccp_sha_exp_ctx state; |
Tom Lendacky | 952bce9 | 2016-01-12 11:17:38 -0600 | [diff] [blame] | 214 | |
Tom Lendacky | f709b45 | 2016-04-13 10:52:25 -0500 | [diff] [blame] | 215 | /* Don't let anything leak to 'out' */ |
| 216 | memset(&state, 0, sizeof(state)); |
| 217 | |
Tom Lendacky | b31dde2 | 2016-02-02 11:38:21 -0600 | [diff] [blame] | 218 | state.type = rctx->type; |
| 219 | state.msg_bits = rctx->msg_bits; |
| 220 | state.first = rctx->first; |
| 221 | memcpy(state.ctx, rctx->ctx, sizeof(state.ctx)); |
| 222 | state.buf_count = rctx->buf_count; |
| 223 | memcpy(state.buf, rctx->buf, sizeof(state.buf)); |
| 224 | |
| 225 | /* 'out' may not be aligned so memcpy from local variable */ |
| 226 | memcpy(out, &state, sizeof(state)); |
Tom Lendacky | 952bce9 | 2016-01-12 11:17:38 -0600 | [diff] [blame] | 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int ccp_sha_import(struct ahash_request *req, const void *in) |
| 232 | { |
| 233 | struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req); |
Tom Lendacky | b31dde2 | 2016-02-02 11:38:21 -0600 | [diff] [blame] | 234 | struct ccp_sha_exp_ctx state; |
Tom Lendacky | 952bce9 | 2016-01-12 11:17:38 -0600 | [diff] [blame] | 235 | |
Tom Lendacky | b31dde2 | 2016-02-02 11:38:21 -0600 | [diff] [blame] | 236 | /* 'in' may not be aligned so memcpy to local variable */ |
| 237 | memcpy(&state, in, sizeof(state)); |
| 238 | |
Tom Lendacky | ce0ae26 | 2016-02-25 16:48:13 -0600 | [diff] [blame] | 239 | memset(rctx, 0, sizeof(*rctx)); |
Tom Lendacky | b31dde2 | 2016-02-02 11:38:21 -0600 | [diff] [blame] | 240 | rctx->type = state.type; |
| 241 | rctx->msg_bits = state.msg_bits; |
| 242 | rctx->first = state.first; |
| 243 | memcpy(rctx->ctx, state.ctx, sizeof(rctx->ctx)); |
| 244 | rctx->buf_count = state.buf_count; |
| 245 | memcpy(rctx->buf, state.buf, sizeof(rctx->buf)); |
Tom Lendacky | 952bce9 | 2016-01-12 11:17:38 -0600 | [diff] [blame] | 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 250 | static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 251 | unsigned int key_len) |
| 252 | { |
| 253 | struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm)); |
Tom Lendacky | c11baa0 | 2014-01-24 16:18:02 -0600 | [diff] [blame] | 254 | struct crypto_shash *shash = ctx->u.sha.hmac_tfm; |
Jan-Simon Möller | 61ded52 | 2014-09-04 20:39:24 +0200 | [diff] [blame] | 255 | |
| 256 | SHASH_DESC_ON_STACK(sdesc, shash); |
| 257 | |
Tom Lendacky | c11baa0 | 2014-01-24 16:18:02 -0600 | [diff] [blame] | 258 | unsigned int block_size = crypto_shash_blocksize(shash); |
| 259 | unsigned int digest_size = crypto_shash_digestsize(shash); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 260 | int i, ret; |
| 261 | |
| 262 | /* Set to zero until complete */ |
| 263 | ctx->u.sha.key_len = 0; |
| 264 | |
| 265 | /* Clear key area to provide zero padding for keys smaller |
| 266 | * than the block size |
| 267 | */ |
| 268 | memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key)); |
| 269 | |
| 270 | if (key_len > block_size) { |
| 271 | /* Must hash the input key */ |
Jan-Simon Möller | 61ded52 | 2014-09-04 20:39:24 +0200 | [diff] [blame] | 272 | sdesc->tfm = shash; |
| 273 | sdesc->flags = crypto_ahash_get_flags(tfm) & |
Tom Lendacky | c11baa0 | 2014-01-24 16:18:02 -0600 | [diff] [blame] | 274 | CRYPTO_TFM_REQ_MAY_SLEEP; |
| 275 | |
Jan-Simon Möller | 61ded52 | 2014-09-04 20:39:24 +0200 | [diff] [blame] | 276 | ret = crypto_shash_digest(sdesc, key, key_len, |
Tom Lendacky | c11baa0 | 2014-01-24 16:18:02 -0600 | [diff] [blame] | 277 | ctx->u.sha.key); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 278 | if (ret) { |
| 279 | crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 280 | return -EINVAL; |
| 281 | } |
| 282 | |
| 283 | key_len = digest_size; |
Tom Lendacky | 8db8846 | 2015-02-03 13:07:05 -0600 | [diff] [blame] | 284 | } else { |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 285 | memcpy(ctx->u.sha.key, key, key_len); |
Tom Lendacky | 8db8846 | 2015-02-03 13:07:05 -0600 | [diff] [blame] | 286 | } |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 287 | |
| 288 | for (i = 0; i < block_size; i++) { |
| 289 | ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ 0x36; |
| 290 | ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ 0x5c; |
| 291 | } |
| 292 | |
Tom Lendacky | c11baa0 | 2014-01-24 16:18:02 -0600 | [diff] [blame] | 293 | sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size); |
| 294 | ctx->u.sha.opad_count = block_size; |
| 295 | |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 296 | ctx->u.sha.key_len = key_len; |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int ccp_sha_cra_init(struct crypto_tfm *tfm) |
| 302 | { |
| 303 | struct ccp_ctx *ctx = crypto_tfm_ctx(tfm); |
| 304 | struct crypto_ahash *ahash = __crypto_ahash_cast(tfm); |
| 305 | |
| 306 | ctx->complete = ccp_sha_complete; |
| 307 | ctx->u.sha.key_len = 0; |
| 308 | |
| 309 | crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx)); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static void ccp_sha_cra_exit(struct crypto_tfm *tfm) |
| 315 | { |
| 316 | } |
| 317 | |
| 318 | static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm) |
| 319 | { |
| 320 | struct ccp_ctx *ctx = crypto_tfm_ctx(tfm); |
| 321 | struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm); |
Tom Lendacky | c11baa0 | 2014-01-24 16:18:02 -0600 | [diff] [blame] | 322 | struct crypto_shash *hmac_tfm; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 323 | |
Tom Lendacky | c11baa0 | 2014-01-24 16:18:02 -0600 | [diff] [blame] | 324 | hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 325 | if (IS_ERR(hmac_tfm)) { |
| 326 | pr_warn("could not load driver %s need for HMAC support\n", |
| 327 | alg->child_alg); |
| 328 | return PTR_ERR(hmac_tfm); |
| 329 | } |
| 330 | |
| 331 | ctx->u.sha.hmac_tfm = hmac_tfm; |
| 332 | |
| 333 | return ccp_sha_cra_init(tfm); |
| 334 | } |
| 335 | |
| 336 | static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm) |
| 337 | { |
| 338 | struct ccp_ctx *ctx = crypto_tfm_ctx(tfm); |
| 339 | |
| 340 | if (ctx->u.sha.hmac_tfm) |
Tom Lendacky | c11baa0 | 2014-01-24 16:18:02 -0600 | [diff] [blame] | 341 | crypto_free_shash(ctx->u.sha.hmac_tfm); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 342 | |
| 343 | ccp_sha_cra_exit(tfm); |
| 344 | } |
| 345 | |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 346 | struct ccp_sha_def { |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 347 | unsigned int version; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 348 | const char *name; |
| 349 | const char *drv_name; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 350 | enum ccp_sha_type type; |
| 351 | u32 digest_size; |
| 352 | u32 block_size; |
| 353 | }; |
| 354 | |
| 355 | static struct ccp_sha_def sha_algs[] = { |
| 356 | { |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 357 | .version = CCP_VERSION(3, 0), |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 358 | .name = "sha1", |
| 359 | .drv_name = "sha1-ccp", |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 360 | .type = CCP_SHA_TYPE_1, |
| 361 | .digest_size = SHA1_DIGEST_SIZE, |
| 362 | .block_size = SHA1_BLOCK_SIZE, |
| 363 | }, |
| 364 | { |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 365 | .version = CCP_VERSION(3, 0), |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 366 | .name = "sha224", |
| 367 | .drv_name = "sha224-ccp", |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 368 | .type = CCP_SHA_TYPE_224, |
| 369 | .digest_size = SHA224_DIGEST_SIZE, |
| 370 | .block_size = SHA224_BLOCK_SIZE, |
| 371 | }, |
| 372 | { |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 373 | .version = CCP_VERSION(3, 0), |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 374 | .name = "sha256", |
| 375 | .drv_name = "sha256-ccp", |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 376 | .type = CCP_SHA_TYPE_256, |
| 377 | .digest_size = SHA256_DIGEST_SIZE, |
| 378 | .block_size = SHA256_BLOCK_SIZE, |
| 379 | }, |
| 380 | }; |
| 381 | |
| 382 | static int ccp_register_hmac_alg(struct list_head *head, |
| 383 | const struct ccp_sha_def *def, |
| 384 | const struct ccp_crypto_ahash_alg *base_alg) |
| 385 | { |
| 386 | struct ccp_crypto_ahash_alg *ccp_alg; |
| 387 | struct ahash_alg *alg; |
| 388 | struct hash_alg_common *halg; |
| 389 | struct crypto_alg *base; |
| 390 | int ret; |
| 391 | |
| 392 | ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL); |
| 393 | if (!ccp_alg) |
| 394 | return -ENOMEM; |
| 395 | |
| 396 | /* Copy the base algorithm and only change what's necessary */ |
Fengguang Wu | d1dd206 | 2013-12-09 20:08:19 +0800 | [diff] [blame] | 397 | *ccp_alg = *base_alg; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 398 | INIT_LIST_HEAD(&ccp_alg->entry); |
| 399 | |
| 400 | strncpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME); |
| 401 | |
| 402 | alg = &ccp_alg->alg; |
| 403 | alg->setkey = ccp_sha_setkey; |
| 404 | |
| 405 | halg = &alg->halg; |
| 406 | |
| 407 | base = &halg->base; |
| 408 | snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name); |
| 409 | snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s", |
| 410 | def->drv_name); |
| 411 | base->cra_init = ccp_hmac_sha_cra_init; |
| 412 | base->cra_exit = ccp_hmac_sha_cra_exit; |
| 413 | |
| 414 | ret = crypto_register_ahash(alg); |
| 415 | if (ret) { |
| 416 | pr_err("%s ahash algorithm registration error (%d)\n", |
Tom Lendacky | 8db8846 | 2015-02-03 13:07:05 -0600 | [diff] [blame] | 417 | base->cra_name, ret); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 418 | kfree(ccp_alg); |
| 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | list_add(&ccp_alg->entry, head); |
| 423 | |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | static int ccp_register_sha_alg(struct list_head *head, |
| 428 | const struct ccp_sha_def *def) |
| 429 | { |
| 430 | struct ccp_crypto_ahash_alg *ccp_alg; |
| 431 | struct ahash_alg *alg; |
| 432 | struct hash_alg_common *halg; |
| 433 | struct crypto_alg *base; |
| 434 | int ret; |
| 435 | |
| 436 | ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL); |
| 437 | if (!ccp_alg) |
| 438 | return -ENOMEM; |
| 439 | |
| 440 | INIT_LIST_HEAD(&ccp_alg->entry); |
| 441 | |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 442 | ccp_alg->type = def->type; |
| 443 | |
| 444 | alg = &ccp_alg->alg; |
| 445 | alg->init = ccp_sha_init; |
| 446 | alg->update = ccp_sha_update; |
| 447 | alg->final = ccp_sha_final; |
| 448 | alg->finup = ccp_sha_finup; |
| 449 | alg->digest = ccp_sha_digest; |
Tom Lendacky | 952bce9 | 2016-01-12 11:17:38 -0600 | [diff] [blame] | 450 | alg->export = ccp_sha_export; |
| 451 | alg->import = ccp_sha_import; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 452 | |
| 453 | halg = &alg->halg; |
| 454 | halg->digestsize = def->digest_size; |
Tom Lendacky | d166216 | 2016-01-29 12:45:14 -0600 | [diff] [blame] | 455 | halg->statesize = sizeof(struct ccp_sha_exp_ctx); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 456 | |
| 457 | base = &halg->base; |
| 458 | snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name); |
| 459 | snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", |
| 460 | def->drv_name); |
| 461 | base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC | |
| 462 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
| 463 | CRYPTO_ALG_NEED_FALLBACK; |
| 464 | base->cra_blocksize = def->block_size; |
| 465 | base->cra_ctxsize = sizeof(struct ccp_ctx); |
| 466 | base->cra_priority = CCP_CRA_PRIORITY; |
| 467 | base->cra_type = &crypto_ahash_type; |
| 468 | base->cra_init = ccp_sha_cra_init; |
| 469 | base->cra_exit = ccp_sha_cra_exit; |
| 470 | base->cra_module = THIS_MODULE; |
| 471 | |
| 472 | ret = crypto_register_ahash(alg); |
| 473 | if (ret) { |
| 474 | pr_err("%s ahash algorithm registration error (%d)\n", |
Tom Lendacky | 8db8846 | 2015-02-03 13:07:05 -0600 | [diff] [blame] | 475 | base->cra_name, ret); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 476 | kfree(ccp_alg); |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | list_add(&ccp_alg->entry, head); |
| 481 | |
| 482 | ret = ccp_register_hmac_alg(head, def, ccp_alg); |
| 483 | |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | int ccp_register_sha_algs(struct list_head *head) |
| 488 | { |
| 489 | int i, ret; |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 490 | unsigned int ccpversion = ccp_version(); |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 491 | |
| 492 | for (i = 0; i < ARRAY_SIZE(sha_algs); i++) { |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 493 | if (sha_algs[i].version > ccpversion) |
| 494 | continue; |
Tom Lendacky | 0ab0a1d | 2013-11-12 11:46:45 -0600 | [diff] [blame] | 495 | ret = ccp_register_sha_alg(head, &sha_algs[i]); |
| 496 | if (ret) |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | return 0; |
| 501 | } |