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