blob: becb738c897b1b5d93b632e3ab80ed2b146ead5a [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
Herbert Xu030f4e92015-07-07 17:30:25 +080032static int nx_crypto_ctx_sha256_init(struct crypto_tfm *tfm)
Kent Yoder528e3962012-05-14 11:06:20 +000033{
Herbert Xu030f4e92015-07-07 17:30:25 +080034 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
35 int err;
36
37 err = nx_crypto_ctx_sha_init(tfm);
38 if (err)
39 return err;
Kent Yoder528e3962012-05-14 11:06:20 +000040
41 nx_ctx_init(nx_ctx, HCOP_FC_SHA);
42
Kent Yoder528e3962012-05-14 11:06:20 +000043 nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA256];
44
45 NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA256);
Kent Yoder528e3962012-05-14 11:06:20 +000046
Herbert Xu030f4e92015-07-07 17:30:25 +080047 return 0;
48}
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020049
Herbert Xu030f4e92015-07-07 17:30:25 +080050static int nx_sha256_init(struct shash_desc *desc) {
51 struct sha256_state *sctx = shash_desc_ctx(desc);
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030052
Herbert Xu030f4e92015-07-07 17:30:25 +080053 memset(sctx, 0, sizeof *sctx);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020054
55 sctx->state[0] = __cpu_to_be32(SHA256_H0);
56 sctx->state[1] = __cpu_to_be32(SHA256_H1);
57 sctx->state[2] = __cpu_to_be32(SHA256_H2);
58 sctx->state[3] = __cpu_to_be32(SHA256_H3);
59 sctx->state[4] = __cpu_to_be32(SHA256_H4);
60 sctx->state[5] = __cpu_to_be32(SHA256_H5);
61 sctx->state[6] = __cpu_to_be32(SHA256_H6);
62 sctx->state[7] = __cpu_to_be32(SHA256_H7);
63 sctx->count = 0;
64
Kent Yoder528e3962012-05-14 11:06:20 +000065 return 0;
66}
67
68static int nx_sha256_update(struct shash_desc *desc, const u8 *data,
69 unsigned int len)
70{
71 struct sha256_state *sctx = shash_desc_ctx(desc);
72 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
73 struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
Herbert Xu030f4e92015-07-07 17:30:25 +080074 struct nx_sg *out_sg;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020075 u64 to_process = 0, leftover, total;
Marcelo Cerric8491632013-08-12 18:49:37 -030076 unsigned long irq_flags;
Kent Yoder528e3962012-05-14 11:06:20 +000077 int rc = 0;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020078 int data_len;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030079 u32 max_sg_len;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020080 u64 buf_len = (sctx->count % SHA256_BLOCK_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +000081
Marcelo Cerric8491632013-08-12 18:49:37 -030082 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
83
Kent Yoder528e3962012-05-14 11:06:20 +000084 /* 2 cases for total data len:
Marcelo Cerrid3111492013-08-02 12:09:52 +000085 * 1: < SHA256_BLOCK_SIZE: copy into state, return 0
86 * 2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover
Kent Yoder528e3962012-05-14 11:06:20 +000087 */
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020088 total = (sctx->count % SHA256_BLOCK_SIZE) + len;
Marcelo Cerrid3111492013-08-02 12:09:52 +000089 if (total < SHA256_BLOCK_SIZE) {
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020090 memcpy(sctx->buf + buf_len, data, len);
Kent Yoder528e3962012-05-14 11:06:20 +000091 sctx->count += len;
92 goto out;
93 }
94
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020095 memcpy(csbcpb->cpb.sha256.message_digest, sctx->state, SHA256_DIGEST_SIZE);
96 NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
97 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
Kent Yoder528e3962012-05-14 11:06:20 +000098
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030099 max_sg_len = min_t(u64, nx_ctx->ap->sglen,
100 nx_driver.of.max_sg_len/sizeof(struct nx_sg));
101 max_sg_len = min_t(u64, max_sg_len,
102 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
103
Herbert Xu030f4e92015-07-07 17:30:25 +0800104 data_len = SHA256_DIGEST_SIZE;
105 out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
106 &data_len, max_sg_len);
107 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
108
109 if (data_len != SHA256_DIGEST_SIZE) {
110 rc = -EINVAL;
111 goto out;
112 }
113
Marcelo Cerrid3111492013-08-02 12:09:52 +0000114 do {
Jan Stancekd3392f412015-08-08 08:47:28 +0200115 int used_sgs = 0;
116 struct nx_sg *in_sg = nx_ctx->in_sg;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200117
118 if (buf_len) {
119 data_len = buf_len;
Jan Stancekd3392f412015-08-08 08:47:28 +0200120 in_sg = nx_build_sg_list(in_sg,
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300121 (u8 *) sctx->buf,
122 &data_len,
123 max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200124
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300125 if (data_len != buf_len) {
126 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200127 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300128 }
Jan Stancekd3392f412015-08-08 08:47:28 +0200129 used_sgs = in_sg - nx_ctx->in_sg;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200130 }
131
Jan Stancekd3392f412015-08-08 08:47:28 +0200132 /* to_process: SHA256_BLOCK_SIZE aligned chunk to be
133 * processed in this iteration. This value is restricted
134 * by sg list limits and number of sgs we already used
135 * for leftover data. (see above)
136 * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len,
137 * but because data may not be aligned, we need to account
138 * for that too. */
139 to_process = min_t(u64, total,
140 (max_sg_len - 1 - used_sgs) * NX_PAGE_SIZE);
141 to_process = to_process & ~(SHA256_BLOCK_SIZE - 1);
142
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200143 data_len = to_process - buf_len;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300144 in_sg = nx_build_sg_list(in_sg, (u8 *) data,
145 &data_len, max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200146
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300147 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200148
Jan Stancekd3392f412015-08-08 08:47:28 +0200149 to_process = data_len + buf_len;
Marcelo Cerrid3111492013-08-02 12:09:52 +0000150 leftover = total - to_process;
151
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200152 /*
153 * we've hit the nx chip previously and we're updating
154 * again, so copy over the partial digest.
155 */
156 memcpy(csbcpb->cpb.sha256.input_partial_digest,
Marcelo Cerrid3111492013-08-02 12:09:52 +0000157 csbcpb->cpb.sha256.message_digest,
158 SHA256_DIGEST_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000159
Marcelo Cerrid3111492013-08-02 12:09:52 +0000160 if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
161 rc = -EINVAL;
162 goto out;
163 }
Kent Yoder528e3962012-05-14 11:06:20 +0000164
Marcelo Cerrid3111492013-08-02 12:09:52 +0000165 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
166 desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
167 if (rc)
168 goto out;
Kent Yoder528e3962012-05-14 11:06:20 +0000169
Marcelo Cerrid3111492013-08-02 12:09:52 +0000170 atomic_inc(&(nx_ctx->stats->sha256_ops));
Marcelo Cerrid3111492013-08-02 12:09:52 +0000171
172 total -= to_process;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200173 data += to_process - buf_len;
174 buf_len = 0;
175
Marcelo Cerrid3111492013-08-02 12:09:52 +0000176 } while (leftover >= SHA256_BLOCK_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000177
178 /* copy the leftover back into the state struct */
Kent Yoder1ad936e2013-04-12 17:13:59 +0000179 if (leftover)
Marcelo Cerrid3111492013-08-02 12:09:52 +0000180 memcpy(sctx->buf, data, leftover);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200181
182 sctx->count += len;
183 memcpy(sctx->state, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000184out:
Marcelo Cerric8491632013-08-12 18:49:37 -0300185 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
Kent Yoder528e3962012-05-14 11:06:20 +0000186 return rc;
187}
188
189static int nx_sha256_final(struct shash_desc *desc, u8 *out)
190{
191 struct sha256_state *sctx = shash_desc_ctx(desc);
192 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
193 struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300194 struct nx_sg *in_sg, *out_sg;
Marcelo Cerric8491632013-08-12 18:49:37 -0300195 unsigned long irq_flags;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300196 u32 max_sg_len;
197 int rc = 0;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200198 int len;
Kent Yoder528e3962012-05-14 11:06:20 +0000199
Marcelo Cerric8491632013-08-12 18:49:37 -0300200 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
201
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300202 max_sg_len = min_t(u64, nx_ctx->ap->sglen,
203 nx_driver.of.max_sg_len/sizeof(struct nx_sg));
204 max_sg_len = min_t(u64, max_sg_len,
205 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
206
Kent Yoder528e3962012-05-14 11:06:20 +0000207 /* final is represented by continuing the operation and indicating that
208 * this is not an intermediate operation */
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200209 if (sctx->count >= SHA256_BLOCK_SIZE) {
210 /* we've hit the nx chip previously, now we're finalizing,
211 * so copy over the partial digest */
212 memcpy(csbcpb->cpb.sha256.input_partial_digest, sctx->state, SHA256_DIGEST_SIZE);
213 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
214 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
215 } else {
216 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
217 NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
218 }
Kent Yoder528e3962012-05-14 11:06:20 +0000219
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200220 csbcpb->cpb.sha256.message_bit_length = (u64) (sctx->count * 8);
Kent Yoder528e3962012-05-14 11:06:20 +0000221
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200222 len = sctx->count & (SHA256_BLOCK_SIZE - 1);
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300223 in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) sctx->buf,
224 &len, max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200225
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300226 if (len != (sctx->count & (SHA256_BLOCK_SIZE - 1))) {
227 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200228 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300229 }
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200230
231 len = SHA256_DIGEST_SIZE;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300232 out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200233
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300234 if (len != SHA256_DIGEST_SIZE) {
235 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200236 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300237 }
Kent Yoder528e3962012-05-14 11:06:20 +0000238
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300239 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
240 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
Kent Yoder528e3962012-05-14 11:06:20 +0000241 if (!nx_ctx->op.outlen) {
242 rc = -EINVAL;
243 goto out;
244 }
245
246 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
247 desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
248 if (rc)
249 goto out;
250
251 atomic_inc(&(nx_ctx->stats->sha256_ops));
252
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200253 atomic64_add(sctx->count, &(nx_ctx->stats->sha256_bytes));
Kent Yoder528e3962012-05-14 11:06:20 +0000254 memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
255out:
Marcelo Cerric8491632013-08-12 18:49:37 -0300256 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
Kent Yoder528e3962012-05-14 11:06:20 +0000257 return rc;
258}
259
260static int nx_sha256_export(struct shash_desc *desc, void *out)
261{
262 struct sha256_state *sctx = shash_desc_ctx(desc);
Marcelo Cerric8491632013-08-12 18:49:37 -0300263
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200264 memcpy(out, sctx, sizeof(*sctx));
Kent Yoder528e3962012-05-14 11:06:20 +0000265
Kent Yoder528e3962012-05-14 11:06:20 +0000266 return 0;
267}
268
269static int nx_sha256_import(struct shash_desc *desc, const void *in)
270{
271 struct sha256_state *sctx = shash_desc_ctx(desc);
Marcelo Cerric8491632013-08-12 18:49:37 -0300272
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200273 memcpy(sctx, in, sizeof(*sctx));
Kent Yoder528e3962012-05-14 11:06:20 +0000274
Kent Yoder528e3962012-05-14 11:06:20 +0000275 return 0;
276}
277
278struct shash_alg nx_shash_sha256_alg = {
279 .digestsize = SHA256_DIGEST_SIZE,
280 .init = nx_sha256_init,
281 .update = nx_sha256_update,
282 .final = nx_sha256_final,
283 .export = nx_sha256_export,
284 .import = nx_sha256_import,
285 .descsize = sizeof(struct sha256_state),
286 .statesize = sizeof(struct sha256_state),
287 .base = {
288 .cra_name = "sha256",
289 .cra_driver_name = "sha256-nx",
290 .cra_priority = 300,
291 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
292 .cra_blocksize = SHA256_BLOCK_SIZE,
293 .cra_module = THIS_MODULE,
294 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
Herbert Xu030f4e92015-07-07 17:30:25 +0800295 .cra_init = nx_crypto_ctx_sha256_init,
Kent Yoder528e3962012-05-14 11:06:20 +0000296 .cra_exit = nx_crypto_ctx_exit,
297 }
298};