blob: 4e91bdb83c594c3491bcb183cddd4021e8d8c596 [file] [log] [blame]
Kent Yoder528e3962012-05-14 11:06:20 +00001/**
2 * SHA-256 routines supporting the Power 7+ Nest Accelerators driver
3 *
4 * Copyright (C) 2011-2012 International Business Machines Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 only.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * Author: Kent Yoder <yoder1@us.ibm.com>
20 */
21
22#include <crypto/internal/hash.h>
23#include <crypto/sha.h>
24#include <linux/module.h>
25#include <asm/vio.h>
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020026#include <asm/byteorder.h>
Kent Yoder528e3962012-05-14 11:06:20 +000027
28#include "nx_csbcpb.h"
29#include "nx.h"
30
31
32static int nx_sha256_init(struct shash_desc *desc)
33{
34 struct sha256_state *sctx = shash_desc_ctx(desc);
35 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030036 struct nx_sg *out_sg;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020037 int len;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030038 u32 max_sg_len;
Kent Yoder528e3962012-05-14 11:06:20 +000039
40 nx_ctx_init(nx_ctx, HCOP_FC_SHA);
41
42 memset(sctx, 0, sizeof *sctx);
43
44 nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA256];
45
46 NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA256);
Kent Yoder528e3962012-05-14 11:06:20 +000047
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030048 max_sg_len = min_t(u64, nx_ctx->ap->sglen,
49 nx_driver.of.max_sg_len/sizeof(struct nx_sg));
50 max_sg_len = min_t(u64, max_sg_len,
51 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020052
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030053 len = SHA256_DIGEST_SIZE;
54 out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
55 &len, max_sg_len);
56 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
57
58 if (len != SHA256_DIGEST_SIZE)
59 return -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020060
61 sctx->state[0] = __cpu_to_be32(SHA256_H0);
62 sctx->state[1] = __cpu_to_be32(SHA256_H1);
63 sctx->state[2] = __cpu_to_be32(SHA256_H2);
64 sctx->state[3] = __cpu_to_be32(SHA256_H3);
65 sctx->state[4] = __cpu_to_be32(SHA256_H4);
66 sctx->state[5] = __cpu_to_be32(SHA256_H5);
67 sctx->state[6] = __cpu_to_be32(SHA256_H6);
68 sctx->state[7] = __cpu_to_be32(SHA256_H7);
69 sctx->count = 0;
70
Kent Yoder528e3962012-05-14 11:06:20 +000071 return 0;
72}
73
74static int nx_sha256_update(struct shash_desc *desc, const u8 *data,
75 unsigned int len)
76{
77 struct sha256_state *sctx = shash_desc_ctx(desc);
78 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
79 struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030080 struct nx_sg *in_sg;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020081 u64 to_process = 0, leftover, total;
Marcelo Cerric8491632013-08-12 18:49:37 -030082 unsigned long irq_flags;
Kent Yoder528e3962012-05-14 11:06:20 +000083 int rc = 0;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020084 int data_len;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030085 u32 max_sg_len;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020086 u64 buf_len = (sctx->count % SHA256_BLOCK_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +000087
Marcelo Cerric8491632013-08-12 18:49:37 -030088 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
89
Kent Yoder528e3962012-05-14 11:06:20 +000090 /* 2 cases for total data len:
Marcelo Cerrid3111492013-08-02 12:09:52 +000091 * 1: < SHA256_BLOCK_SIZE: copy into state, return 0
92 * 2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover
Kent Yoder528e3962012-05-14 11:06:20 +000093 */
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020094 total = (sctx->count % SHA256_BLOCK_SIZE) + len;
Marcelo Cerrid3111492013-08-02 12:09:52 +000095 if (total < SHA256_BLOCK_SIZE) {
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020096 memcpy(sctx->buf + buf_len, data, len);
Kent Yoder528e3962012-05-14 11:06:20 +000097 sctx->count += len;
98 goto out;
99 }
100
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200101 memcpy(csbcpb->cpb.sha256.message_digest, sctx->state, SHA256_DIGEST_SIZE);
102 NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
103 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
Kent Yoder528e3962012-05-14 11:06:20 +0000104
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300105 in_sg = nx_ctx->in_sg;
106 max_sg_len = min_t(u64, nx_ctx->ap->sglen,
107 nx_driver.of.max_sg_len/sizeof(struct nx_sg));
108 max_sg_len = min_t(u64, max_sg_len,
109 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
110
Marcelo Cerrid3111492013-08-02 12:09:52 +0000111 do {
112 /*
113 * to_process: the SHA256_BLOCK_SIZE data chunk to process in
114 * this update. This value is also restricted by the sg list
115 * limits.
116 */
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200117 to_process = total - to_process;
Marcelo Cerrid3111492013-08-02 12:09:52 +0000118 to_process = to_process & ~(SHA256_BLOCK_SIZE - 1);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200119
120 if (buf_len) {
121 data_len = buf_len;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300122 in_sg = nx_build_sg_list(nx_ctx->in_sg,
123 (u8 *) sctx->buf,
124 &data_len,
125 max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200126
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300127 if (data_len != buf_len) {
128 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200129 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300130 }
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200131 }
132
133 data_len = to_process - buf_len;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300134 in_sg = nx_build_sg_list(in_sg, (u8 *) data,
135 &data_len, max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200136
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300137 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200138
139 to_process = (data_len + buf_len);
Marcelo Cerrid3111492013-08-02 12:09:52 +0000140 leftover = total - to_process;
141
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200142 /*
143 * we've hit the nx chip previously and we're updating
144 * again, so copy over the partial digest.
145 */
146 memcpy(csbcpb->cpb.sha256.input_partial_digest,
Marcelo Cerrid3111492013-08-02 12:09:52 +0000147 csbcpb->cpb.sha256.message_digest,
148 SHA256_DIGEST_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000149
Marcelo Cerrid3111492013-08-02 12:09:52 +0000150 if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
151 rc = -EINVAL;
152 goto out;
153 }
Kent Yoder528e3962012-05-14 11:06:20 +0000154
Marcelo Cerrid3111492013-08-02 12:09:52 +0000155 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
156 desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
157 if (rc)
158 goto out;
Kent Yoder528e3962012-05-14 11:06:20 +0000159
Marcelo Cerrid3111492013-08-02 12:09:52 +0000160 atomic_inc(&(nx_ctx->stats->sha256_ops));
Marcelo Cerrid3111492013-08-02 12:09:52 +0000161
162 total -= to_process;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200163 data += to_process - buf_len;
164 buf_len = 0;
165
Marcelo Cerrid3111492013-08-02 12:09:52 +0000166 } while (leftover >= SHA256_BLOCK_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000167
168 /* copy the leftover back into the state struct */
Kent Yoder1ad936e2013-04-12 17:13:59 +0000169 if (leftover)
Marcelo Cerrid3111492013-08-02 12:09:52 +0000170 memcpy(sctx->buf, data, leftover);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200171
172 sctx->count += len;
173 memcpy(sctx->state, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000174out:
Marcelo Cerric8491632013-08-12 18:49:37 -0300175 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
Kent Yoder528e3962012-05-14 11:06:20 +0000176 return rc;
177}
178
179static int nx_sha256_final(struct shash_desc *desc, u8 *out)
180{
181 struct sha256_state *sctx = shash_desc_ctx(desc);
182 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
183 struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300184 struct nx_sg *in_sg, *out_sg;
Marcelo Cerric8491632013-08-12 18:49:37 -0300185 unsigned long irq_flags;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300186 u32 max_sg_len;
187 int rc = 0;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200188 int len;
Kent Yoder528e3962012-05-14 11:06:20 +0000189
Marcelo Cerric8491632013-08-12 18:49:37 -0300190 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
191
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300192 max_sg_len = min_t(u64, nx_ctx->ap->sglen,
193 nx_driver.of.max_sg_len/sizeof(struct nx_sg));
194 max_sg_len = min_t(u64, max_sg_len,
195 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
196
Kent Yoder528e3962012-05-14 11:06:20 +0000197 /* final is represented by continuing the operation and indicating that
198 * this is not an intermediate operation */
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200199 if (sctx->count >= SHA256_BLOCK_SIZE) {
200 /* we've hit the nx chip previously, now we're finalizing,
201 * so copy over the partial digest */
202 memcpy(csbcpb->cpb.sha256.input_partial_digest, sctx->state, SHA256_DIGEST_SIZE);
203 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
204 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
205 } else {
206 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
207 NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
208 }
Kent Yoder528e3962012-05-14 11:06:20 +0000209
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200210 csbcpb->cpb.sha256.message_bit_length = (u64) (sctx->count * 8);
Kent Yoder528e3962012-05-14 11:06:20 +0000211
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200212 len = sctx->count & (SHA256_BLOCK_SIZE - 1);
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300213 in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) sctx->buf,
214 &len, max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200215
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300216 if (len != (sctx->count & (SHA256_BLOCK_SIZE - 1))) {
217 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200218 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300219 }
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200220
221 len = SHA256_DIGEST_SIZE;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300222 out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200223
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300224 if (len != SHA256_DIGEST_SIZE) {
225 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200226 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300227 }
Kent Yoder528e3962012-05-14 11:06:20 +0000228
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300229 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
230 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
Kent Yoder528e3962012-05-14 11:06:20 +0000231 if (!nx_ctx->op.outlen) {
232 rc = -EINVAL;
233 goto out;
234 }
235
236 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
237 desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
238 if (rc)
239 goto out;
240
241 atomic_inc(&(nx_ctx->stats->sha256_ops));
242
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200243 atomic64_add(sctx->count, &(nx_ctx->stats->sha256_bytes));
Kent Yoder528e3962012-05-14 11:06:20 +0000244 memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
245out:
Marcelo Cerric8491632013-08-12 18:49:37 -0300246 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
Kent Yoder528e3962012-05-14 11:06:20 +0000247 return rc;
248}
249
250static int nx_sha256_export(struct shash_desc *desc, void *out)
251{
252 struct sha256_state *sctx = shash_desc_ctx(desc);
Marcelo Cerric8491632013-08-12 18:49:37 -0300253
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200254 memcpy(out, sctx, sizeof(*sctx));
Kent Yoder528e3962012-05-14 11:06:20 +0000255
Kent Yoder528e3962012-05-14 11:06:20 +0000256 return 0;
257}
258
259static int nx_sha256_import(struct shash_desc *desc, const void *in)
260{
261 struct sha256_state *sctx = shash_desc_ctx(desc);
Marcelo Cerric8491632013-08-12 18:49:37 -0300262
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200263 memcpy(sctx, in, sizeof(*sctx));
Kent Yoder528e3962012-05-14 11:06:20 +0000264
Kent Yoder528e3962012-05-14 11:06:20 +0000265 return 0;
266}
267
268struct shash_alg nx_shash_sha256_alg = {
269 .digestsize = SHA256_DIGEST_SIZE,
270 .init = nx_sha256_init,
271 .update = nx_sha256_update,
272 .final = nx_sha256_final,
273 .export = nx_sha256_export,
274 .import = nx_sha256_import,
275 .descsize = sizeof(struct sha256_state),
276 .statesize = sizeof(struct sha256_state),
277 .base = {
278 .cra_name = "sha256",
279 .cra_driver_name = "sha256-nx",
280 .cra_priority = 300,
281 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
282 .cra_blocksize = SHA256_BLOCK_SIZE,
283 .cra_module = THIS_MODULE,
284 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
285 .cra_init = nx_crypto_ctx_sha_init,
286 .cra_exit = nx_crypto_ctx_exit,
287 }
288};