blob: 08f8d5cd633491e3ff0e28ca8204d7f51be2b05b [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;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030074 struct nx_sg *in_sg;
Herbert Xu030f4e92015-07-07 17:30:25 +080075 struct nx_sg *out_sg;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020076 u64 to_process = 0, leftover, total;
Marcelo Cerric8491632013-08-12 18:49:37 -030077 unsigned long irq_flags;
Kent Yoder528e3962012-05-14 11:06:20 +000078 int rc = 0;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020079 int data_len;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -030080 u32 max_sg_len;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020081 u64 buf_len = (sctx->count % SHA256_BLOCK_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +000082
Marcelo Cerric8491632013-08-12 18:49:37 -030083 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
84
Kent Yoder528e3962012-05-14 11:06:20 +000085 /* 2 cases for total data len:
Marcelo Cerrid3111492013-08-02 12:09:52 +000086 * 1: < SHA256_BLOCK_SIZE: copy into state, return 0
87 * 2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover
Kent Yoder528e3962012-05-14 11:06:20 +000088 */
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020089 total = (sctx->count % SHA256_BLOCK_SIZE) + len;
Marcelo Cerrid3111492013-08-02 12:09:52 +000090 if (total < SHA256_BLOCK_SIZE) {
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020091 memcpy(sctx->buf + buf_len, data, len);
Kent Yoder528e3962012-05-14 11:06:20 +000092 sctx->count += len;
93 goto out;
94 }
95
Leonidas S. Barbosa00085112014-10-28 15:49:46 -020096 memcpy(csbcpb->cpb.sha256.message_digest, sctx->state, SHA256_DIGEST_SIZE);
97 NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
98 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
Kent Yoder528e3962012-05-14 11:06:20 +000099
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300100 in_sg = nx_ctx->in_sg;
101 max_sg_len = min_t(u64, nx_ctx->ap->sglen,
102 nx_driver.of.max_sg_len/sizeof(struct nx_sg));
103 max_sg_len = min_t(u64, max_sg_len,
104 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
105
Herbert Xu030f4e92015-07-07 17:30:25 +0800106 data_len = SHA256_DIGEST_SIZE;
107 out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
108 &data_len, max_sg_len);
109 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
110
111 if (data_len != SHA256_DIGEST_SIZE) {
112 rc = -EINVAL;
113 goto out;
114 }
115
Marcelo Cerrid3111492013-08-02 12:09:52 +0000116 do {
117 /*
118 * to_process: the SHA256_BLOCK_SIZE data chunk to process in
119 * this update. This value is also restricted by the sg list
120 * limits.
121 */
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200122 to_process = total - to_process;
Marcelo Cerrid3111492013-08-02 12:09:52 +0000123 to_process = to_process & ~(SHA256_BLOCK_SIZE - 1);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200124
125 if (buf_len) {
126 data_len = buf_len;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300127 in_sg = nx_build_sg_list(nx_ctx->in_sg,
128 (u8 *) sctx->buf,
129 &data_len,
130 max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200131
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300132 if (data_len != buf_len) {
133 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200134 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300135 }
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200136 }
137
138 data_len = to_process - buf_len;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300139 in_sg = nx_build_sg_list(in_sg, (u8 *) data,
140 &data_len, max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200141
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300142 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200143
144 to_process = (data_len + buf_len);
Marcelo Cerrid3111492013-08-02 12:09:52 +0000145 leftover = total - to_process;
146
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200147 /*
148 * we've hit the nx chip previously and we're updating
149 * again, so copy over the partial digest.
150 */
151 memcpy(csbcpb->cpb.sha256.input_partial_digest,
Marcelo Cerrid3111492013-08-02 12:09:52 +0000152 csbcpb->cpb.sha256.message_digest,
153 SHA256_DIGEST_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000154
Marcelo Cerrid3111492013-08-02 12:09:52 +0000155 if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
156 rc = -EINVAL;
157 goto out;
158 }
Kent Yoder528e3962012-05-14 11:06:20 +0000159
Marcelo Cerrid3111492013-08-02 12:09:52 +0000160 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
161 desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
162 if (rc)
163 goto out;
Kent Yoder528e3962012-05-14 11:06:20 +0000164
Marcelo Cerrid3111492013-08-02 12:09:52 +0000165 atomic_inc(&(nx_ctx->stats->sha256_ops));
Marcelo Cerrid3111492013-08-02 12:09:52 +0000166
167 total -= to_process;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200168 data += to_process - buf_len;
169 buf_len = 0;
170
Marcelo Cerrid3111492013-08-02 12:09:52 +0000171 } while (leftover >= SHA256_BLOCK_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000172
173 /* copy the leftover back into the state struct */
Kent Yoder1ad936e2013-04-12 17:13:59 +0000174 if (leftover)
Marcelo Cerrid3111492013-08-02 12:09:52 +0000175 memcpy(sctx->buf, data, leftover);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200176
177 sctx->count += len;
178 memcpy(sctx->state, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
Kent Yoder528e3962012-05-14 11:06:20 +0000179out:
Marcelo Cerric8491632013-08-12 18:49:37 -0300180 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
Kent Yoder528e3962012-05-14 11:06:20 +0000181 return rc;
182}
183
184static int nx_sha256_final(struct shash_desc *desc, u8 *out)
185{
186 struct sha256_state *sctx = shash_desc_ctx(desc);
187 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
188 struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300189 struct nx_sg *in_sg, *out_sg;
Marcelo Cerric8491632013-08-12 18:49:37 -0300190 unsigned long irq_flags;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300191 u32 max_sg_len;
192 int rc = 0;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200193 int len;
Kent Yoder528e3962012-05-14 11:06:20 +0000194
Marcelo Cerric8491632013-08-12 18:49:37 -0300195 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
196
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300197 max_sg_len = min_t(u64, nx_ctx->ap->sglen,
198 nx_driver.of.max_sg_len/sizeof(struct nx_sg));
199 max_sg_len = min_t(u64, max_sg_len,
200 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
201
Kent Yoder528e3962012-05-14 11:06:20 +0000202 /* final is represented by continuing the operation and indicating that
203 * this is not an intermediate operation */
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200204 if (sctx->count >= SHA256_BLOCK_SIZE) {
205 /* we've hit the nx chip previously, now we're finalizing,
206 * so copy over the partial digest */
207 memcpy(csbcpb->cpb.sha256.input_partial_digest, sctx->state, SHA256_DIGEST_SIZE);
208 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
209 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
210 } else {
211 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
212 NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
213 }
Kent Yoder528e3962012-05-14 11:06:20 +0000214
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200215 csbcpb->cpb.sha256.message_bit_length = (u64) (sctx->count * 8);
Kent Yoder528e3962012-05-14 11:06:20 +0000216
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200217 len = sctx->count & (SHA256_BLOCK_SIZE - 1);
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300218 in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) sctx->buf,
219 &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 != (sctx->count & (SHA256_BLOCK_SIZE - 1))) {
222 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200223 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300224 }
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200225
226 len = SHA256_DIGEST_SIZE;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300227 out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, max_sg_len);
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200228
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300229 if (len != SHA256_DIGEST_SIZE) {
230 rc = -EINVAL;
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200231 goto out;
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300232 }
Kent Yoder528e3962012-05-14 11:06:20 +0000233
Leonidas Da Silva Barbosa10d87b72015-04-23 17:41:43 -0300234 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
235 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
Kent Yoder528e3962012-05-14 11:06:20 +0000236 if (!nx_ctx->op.outlen) {
237 rc = -EINVAL;
238 goto out;
239 }
240
241 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
242 desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
243 if (rc)
244 goto out;
245
246 atomic_inc(&(nx_ctx->stats->sha256_ops));
247
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200248 atomic64_add(sctx->count, &(nx_ctx->stats->sha256_bytes));
Kent Yoder528e3962012-05-14 11:06:20 +0000249 memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
250out:
Marcelo Cerric8491632013-08-12 18:49:37 -0300251 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
Kent Yoder528e3962012-05-14 11:06:20 +0000252 return rc;
253}
254
255static int nx_sha256_export(struct shash_desc *desc, void *out)
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(out, sctx, sizeof(*sctx));
Kent Yoder528e3962012-05-14 11:06:20 +0000260
Kent Yoder528e3962012-05-14 11:06:20 +0000261 return 0;
262}
263
264static int nx_sha256_import(struct shash_desc *desc, const void *in)
265{
266 struct sha256_state *sctx = shash_desc_ctx(desc);
Marcelo Cerric8491632013-08-12 18:49:37 -0300267
Leonidas S. Barbosa00085112014-10-28 15:49:46 -0200268 memcpy(sctx, in, sizeof(*sctx));
Kent Yoder528e3962012-05-14 11:06:20 +0000269
Kent Yoder528e3962012-05-14 11:06:20 +0000270 return 0;
271}
272
273struct shash_alg nx_shash_sha256_alg = {
274 .digestsize = SHA256_DIGEST_SIZE,
275 .init = nx_sha256_init,
276 .update = nx_sha256_update,
277 .final = nx_sha256_final,
278 .export = nx_sha256_export,
279 .import = nx_sha256_import,
280 .descsize = sizeof(struct sha256_state),
281 .statesize = sizeof(struct sha256_state),
282 .base = {
283 .cra_name = "sha256",
284 .cra_driver_name = "sha256-nx",
285 .cra_priority = 300,
286 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
287 .cra_blocksize = SHA256_BLOCK_SIZE,
288 .cra_module = THIS_MODULE,
289 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
Herbert Xu030f4e92015-07-07 17:30:25 +0800290 .cra_init = nx_crypto_ctx_sha256_init,
Kent Yoder528e3962012-05-14 11:06:20 +0000291 .cra_exit = nx_crypto_ctx_exit,
292 }
293};