blob: 02fb5345319592116dd34b025ca6a4ac317a7916 [file] [log] [blame]
Thomas Gleixner64d85cc2019-05-29 07:18:13 -07001// SPDX-License-Identifier: GPL-2.0-only
Kent Yoder528e3962012-05-14 11:06:20 +00002/**
3 * SHA-256 routines supporting the Power 7+ Nest Accelerators driver
4 *
5 * Copyright (C) 2011-2012 International Business Machines Inc.
6 *
Kent Yoder528e3962012-05-14 11:06:20 +00007 * Author: Kent Yoder <yoder1@us.ibm.com>
8 */
9
10#include <crypto/internal/hash.h>
11#include <crypto/sha.h>
12#include <linux/module.h>
13#include <asm/vio.h>
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020014#include <asm/byteorder.h>
Kent Yoder528e3962012-05-14 11:06:20 +000015
16#include "nx_csbcpb.h"
17#include "nx.h"
18
19
Herbert Xu030f4e92015-07-07 17:30:25 +080020static int nx_crypto_ctx_sha256_init(struct crypto_tfm *tfm)
Kent Yoder528e3962012-05-14 11:06:20 +000021{
Herbert Xu030f4e92015-07-07 17:30:25 +080022 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
23 int err;
24
25 err = nx_crypto_ctx_sha_init(tfm);
26 if (err)
27 return err;
Kent Yoder528e3962012-05-14 11:06:20 +000028
29 nx_ctx_init(nx_ctx, HCOP_FC_SHA);
30
Kent Yoder528e3962012-05-14 11:06:20 +000031 nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA256];
32
33 NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA256);
Kent Yoder528e3962012-05-14 11:06:20 +000034
Herbert Xu030f4e92015-07-07 17:30:25 +080035 return 0;
36}
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020037
Herbert Xu030f4e92015-07-07 17:30:25 +080038static int nx_sha256_init(struct shash_desc *desc) {
39 struct sha256_state *sctx = shash_desc_ctx(desc);
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030040
Herbert Xu030f4e92015-07-07 17:30:25 +080041 memset(sctx, 0, sizeof *sctx);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020042
43 sctx->state[0] = __cpu_to_be32(SHA256_H0);
44 sctx->state[1] = __cpu_to_be32(SHA256_H1);
45 sctx->state[2] = __cpu_to_be32(SHA256_H2);
46 sctx->state[3] = __cpu_to_be32(SHA256_H3);
47 sctx->state[4] = __cpu_to_be32(SHA256_H4);
48 sctx->state[5] = __cpu_to_be32(SHA256_H5);
49 sctx->state[6] = __cpu_to_be32(SHA256_H6);
50 sctx->state[7] = __cpu_to_be32(SHA256_H7);
51 sctx->count = 0;
52
Kent Yoder528e3962012-05-14 11:06:20 +000053 return 0;
54}
55
56static int nx_sha256_update(struct shash_desc *desc, const u8 *data,
57 unsigned int len)
58{
59 struct sha256_state *sctx = shash_desc_ctx(desc);
60 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
61 struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
Herbert Xu030f4e92015-07-07 17:30:25 +080062 struct nx_sg *out_sg;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020063 u64 to_process = 0, leftover, total;
Marcelo Cerric8491632013-08-12 18:49:37 -030064 unsigned long irq_flags;
Kent Yoder528e3962012-05-14 11:06:20 +000065 int rc = 0;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020066 int data_len;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030067 u32 max_sg_len;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020068 u64 buf_len = (sctx->count % SHA256_BLOCK_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +000069
Marcelo Cerric8491632013-08-12 18:49:37 -030070 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
71
Kent Yoder528e3962012-05-14 11:06:20 +000072 /* 2 cases for total data len:
Marcelo Cerrid3111492013-08-02 12:09:52 +000073 * 1: < SHA256_BLOCK_SIZE: copy into state, return 0
74 * 2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover
Kent Yoder528e3962012-05-14 11:06:20 +000075 */
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020076 total = (sctx->count % SHA256_BLOCK_SIZE) + len;
Marcelo Cerrid3111492013-08-02 12:09:52 +000077 if (total < SHA256_BLOCK_SIZE) {
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020078 memcpy(sctx->buf + buf_len, data, len);
Kent Yoder528e3962012-05-14 11:06:20 +000079 sctx->count += len;
80 goto out;
81 }
82
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020083 memcpy(csbcpb->cpb.sha256.message_digest, sctx->state, SHA256_DIGEST_SIZE);
84 NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
85 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
Kent Yoder528e3962012-05-14 11:06:20 +000086
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030087 max_sg_len = min_t(u64, nx_ctx->ap->sglen,
88 nx_driver.of.max_sg_len/sizeof(struct nx_sg));
89 max_sg_len = min_t(u64, max_sg_len,
90 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
91
Herbert Xu030f4e92015-07-07 17:30:25 +080092 data_len = SHA256_DIGEST_SIZE;
93 out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
94 &data_len, max_sg_len);
95 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
96
97 if (data_len != SHA256_DIGEST_SIZE) {
98 rc = -EINVAL;
99 goto out;
100 }
101
Marcelo Cerrid3111492013-08-02 12:09:52 +0000102 do {
Jan Stancekd3392f412015-08-08 08:47:28 +0200103 int used_sgs = 0;
104 struct nx_sg *in_sg = nx_ctx->in_sg;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200105
106 if (buf_len) {
107 data_len = buf_len;
Jan Stancekd3392f412015-08-08 08:47:28 +0200108 in_sg = nx_build_sg_list(in_sg,
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300109 (u8 *) sctx->buf,
110 &data_len,
111 max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200112
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300113 if (data_len != buf_len) {
114 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200115 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300116 }
Jan Stancekd3392f412015-08-08 08:47:28 +0200117 used_sgs = in_sg - nx_ctx->in_sg;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200118 }
119
Jan Stancekd3392f412015-08-08 08:47:28 +0200120 /* to_process: SHA256_BLOCK_SIZE aligned chunk to be
121 * processed in this iteration. This value is restricted
122 * by sg list limits and number of sgs we already used
123 * for leftover data. (see above)
124 * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len,
125 * but because data may not be aligned, we need to account
126 * for that too. */
127 to_process = min_t(u64, total,
128 (max_sg_len - 1 - used_sgs) * NX_PAGE_SIZE);
129 to_process = to_process & ~(SHA256_BLOCK_SIZE - 1);
130
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200131 data_len = to_process - buf_len;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300132 in_sg = nx_build_sg_list(in_sg, (u8 *) data,
133 &data_len, max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200134
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300135 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200136
Jan Stancekd3392f412015-08-08 08:47:28 +0200137 to_process = data_len + buf_len;
Marcelo Cerrid3111492013-08-02 12:09:52 +0000138 leftover = total - to_process;
139
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200140 /*
141 * we've hit the nx chip previously and we're updating
142 * again, so copy over the partial digest.
143 */
144 memcpy(csbcpb->cpb.sha256.input_partial_digest,
Marcelo Cerrid3111492013-08-02 12:09:52 +0000145 csbcpb->cpb.sha256.message_digest,
146 SHA256_DIGEST_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000147
Marcelo Cerrid3111492013-08-02 12:09:52 +0000148 if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
149 rc = -EINVAL;
150 goto out;
151 }
Kent Yoder528e3962012-05-14 11:06:20 +0000152
Eric Biggers75f22222019-04-14 17:37:08 -0700153 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
Marcelo Cerrid3111492013-08-02 12:09:52 +0000154 if (rc)
155 goto out;
Kent Yoder528e3962012-05-14 11:06:20 +0000156
Marcelo Cerrid3111492013-08-02 12:09:52 +0000157 atomic_inc(&(nx_ctx->stats->sha256_ops));
Marcelo Cerrid3111492013-08-02 12:09:52 +0000158
159 total -= to_process;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200160 data += to_process - buf_len;
161 buf_len = 0;
162
Marcelo Cerrid3111492013-08-02 12:09:52 +0000163 } while (leftover >= SHA256_BLOCK_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000164
165 /* copy the leftover back into the state struct */
Kent Yoder1ad936e2013-04-12 17:13:59 +0000166 if (leftover)
Marcelo Cerrid3111492013-08-02 12:09:52 +0000167 memcpy(sctx->buf, data, leftover);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200168
169 sctx->count += len;
170 memcpy(sctx->state, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000171out:
Marcelo Cerric8491632013-08-12 18:49:37 -0300172 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
Kent Yoder528e3962012-05-14 11:06:20 +0000173 return rc;
174}
175
176static int nx_sha256_final(struct shash_desc *desc, u8 *out)
177{
178 struct sha256_state *sctx = shash_desc_ctx(desc);
179 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
180 struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300181 struct nx_sg *in_sg, *out_sg;
Marcelo Cerric8491632013-08-12 18:49:37 -0300182 unsigned long irq_flags;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300183 u32 max_sg_len;
184 int rc = 0;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200185 int len;
Kent Yoder528e3962012-05-14 11:06:20 +0000186
Marcelo Cerric8491632013-08-12 18:49:37 -0300187 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
188
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300189 max_sg_len = min_t(u64, nx_ctx->ap->sglen,
190 nx_driver.of.max_sg_len/sizeof(struct nx_sg));
191 max_sg_len = min_t(u64, max_sg_len,
192 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
193
Kent Yoder528e3962012-05-14 11:06:20 +0000194 /* final is represented by continuing the operation and indicating that
195 * this is not an intermediate operation */
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200196 if (sctx->count >= SHA256_BLOCK_SIZE) {
197 /* we've hit the nx chip previously, now we're finalizing,
198 * so copy over the partial digest */
199 memcpy(csbcpb->cpb.sha256.input_partial_digest, sctx->state, SHA256_DIGEST_SIZE);
200 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
201 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
202 } else {
203 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
204 NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
205 }
Kent Yoder528e3962012-05-14 11:06:20 +0000206
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200207 csbcpb->cpb.sha256.message_bit_length = (u64) (sctx->count * 8);
Kent Yoder528e3962012-05-14 11:06:20 +0000208
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200209 len = sctx->count & (SHA256_BLOCK_SIZE - 1);
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300210 in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) sctx->buf,
211 &len, max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200212
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300213 if (len != (sctx->count & (SHA256_BLOCK_SIZE - 1))) {
214 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200215 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300216 }
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200217
218 len = SHA256_DIGEST_SIZE;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300219 out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200220
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300221 if (len != SHA256_DIGEST_SIZE) {
222 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200223 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300224 }
Kent Yoder528e3962012-05-14 11:06:20 +0000225
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300226 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
227 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
Kent Yoder528e3962012-05-14 11:06:20 +0000228 if (!nx_ctx->op.outlen) {
229 rc = -EINVAL;
230 goto out;
231 }
232
Eric Biggers75f22222019-04-14 17:37:08 -0700233 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
Kent Yoder528e3962012-05-14 11:06:20 +0000234 if (rc)
235 goto out;
236
237 atomic_inc(&(nx_ctx->stats->sha256_ops));
238
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200239 atomic64_add(sctx->count, &(nx_ctx->stats->sha256_bytes));
Kent Yoder528e3962012-05-14 11:06:20 +0000240 memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
241out:
Marcelo Cerric8491632013-08-12 18:49:37 -0300242 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
Kent Yoder528e3962012-05-14 11:06:20 +0000243 return rc;
244}
245
246static int nx_sha256_export(struct shash_desc *desc, void *out)
247{
248 struct sha256_state *sctx = shash_desc_ctx(desc);
Marcelo Cerric8491632013-08-12 18:49:37 -0300249
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200250 memcpy(out, sctx, sizeof(*sctx));
Kent Yoder528e3962012-05-14 11:06:20 +0000251
Kent Yoder528e3962012-05-14 11:06:20 +0000252 return 0;
253}
254
255static int nx_sha256_import(struct shash_desc *desc, const void *in)
256{
257 struct sha256_state *sctx = shash_desc_ctx(desc);
Marcelo Cerric8491632013-08-12 18:49:37 -0300258
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200259 memcpy(sctx, in, sizeof(*sctx));
Kent Yoder528e3962012-05-14 11:06:20 +0000260
Kent Yoder528e3962012-05-14 11:06:20 +0000261 return 0;
262}
263
264struct shash_alg nx_shash_sha256_alg = {
265 .digestsize = SHA256_DIGEST_SIZE,
266 .init = nx_sha256_init,
267 .update = nx_sha256_update,
268 .final = nx_sha256_final,
269 .export = nx_sha256_export,
270 .import = nx_sha256_import,
271 .descsize = sizeof(struct sha256_state),
272 .statesize = sizeof(struct sha256_state),
273 .base = {
274 .cra_name = "sha256",
275 .cra_driver_name = "sha256-nx",
276 .cra_priority = 300,
Kent Yoder528e3962012-05-14 11:06:20 +0000277 .cra_blocksize = SHA256_BLOCK_SIZE,
278 .cra_module = THIS_MODULE,
279 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
Herbert Xu030f4e92015-07-07 17:30:25 +0800280 .cra_init = nx_crypto_ctx_sha256_init,
Kent Yoder528e3962012-05-14 11:06:20 +0000281 .cra_exit = nx_crypto_ctx_exit,
282 }
283};