nx-sha256.c 7.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/**
 * SHA-256 routines supporting the Power 7+ Nest Accelerators driver
 *
 * Copyright (C) 2011-2012 International Business Machines Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 only.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Author: Kent Yoder <yoder1@us.ibm.com>
 */

#include <crypto/internal/hash.h>
#include <crypto/sha.h>
#include <linux/module.h>
#include <asm/vio.h>
26
#include <asm/byteorder.h>
27 28 29 30 31

#include "nx_csbcpb.h"
#include "nx.h"


H
Herbert Xu 已提交
32
static int nx_crypto_ctx_sha256_init(struct crypto_tfm *tfm)
33
{
H
Herbert Xu 已提交
34 35
	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
	int err;
36

H
Herbert Xu 已提交
37 38 39
	err = nx_crypto_ctx_sha_init(tfm);
	if (err)
		return err;
40

H
Herbert Xu 已提交
41
	nx_ctx_init(nx_ctx, HCOP_FC_SHA);
42 43 44 45 46

	nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA256];

	NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA256);

H
Herbert Xu 已提交
47 48
	return 0;
}
49

H
Herbert Xu 已提交
50 51
static int nx_sha256_init(struct shash_desc *desc) {
	struct sha256_state *sctx = shash_desc_ctx(desc);
52

H
Herbert Xu 已提交
53
	memset(sctx, 0, sizeof *sctx);
54 55 56 57 58 59 60 61 62 63 64

	sctx->state[0] = __cpu_to_be32(SHA256_H0);
	sctx->state[1] = __cpu_to_be32(SHA256_H1);
	sctx->state[2] = __cpu_to_be32(SHA256_H2);
	sctx->state[3] = __cpu_to_be32(SHA256_H3);
	sctx->state[4] = __cpu_to_be32(SHA256_H4);
	sctx->state[5] = __cpu_to_be32(SHA256_H5);
	sctx->state[6] = __cpu_to_be32(SHA256_H6);
	sctx->state[7] = __cpu_to_be32(SHA256_H7);
	sctx->count = 0;

65 66 67 68 69 70 71 72 73
	return 0;
}

static int nx_sha256_update(struct shash_desc *desc, const u8 *data,
			    unsigned int len)
{
	struct sha256_state *sctx = shash_desc_ctx(desc);
	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
	struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
74
	struct nx_sg *in_sg;
H
Herbert Xu 已提交
75
	struct nx_sg *out_sg;
76
	u64 to_process = 0, leftover, total;
77
	unsigned long irq_flags;
78
	int rc = 0;
79
	int data_len;
80
	u32 max_sg_len;
81
	u64 buf_len = (sctx->count % SHA256_BLOCK_SIZE);
82

83 84
	spin_lock_irqsave(&nx_ctx->lock, irq_flags);

85
	/* 2 cases for total data len:
86 87
	 *  1: < SHA256_BLOCK_SIZE: copy into state, return 0
	 *  2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover
88
	 */
89
	total = (sctx->count % SHA256_BLOCK_SIZE) + len;
90
	if (total < SHA256_BLOCK_SIZE) {
91
		memcpy(sctx->buf + buf_len, data, len);
92 93 94 95
		sctx->count += len;
		goto out;
	}

96 97 98
	memcpy(csbcpb->cpb.sha256.message_digest, sctx->state, SHA256_DIGEST_SIZE);
	NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
	NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
99

100 101 102 103 104 105
	in_sg = nx_ctx->in_sg;
	max_sg_len = min_t(u64, nx_ctx->ap->sglen,
			nx_driver.of.max_sg_len/sizeof(struct nx_sg));
	max_sg_len = min_t(u64, max_sg_len,
			nx_ctx->ap->databytelen/NX_PAGE_SIZE);

H
Herbert Xu 已提交
106 107 108 109 110 111 112 113 114 115
	data_len = SHA256_DIGEST_SIZE;
	out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
				  &data_len, max_sg_len);
	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);

	if (data_len != SHA256_DIGEST_SIZE) {
		rc = -EINVAL;
		goto out;
	}

116 117 118 119 120 121
	do {
		/*
		 * to_process: the SHA256_BLOCK_SIZE data chunk to process in
		 * this update. This value is also restricted by the sg list
		 * limits.
		 */
122
		to_process = total - to_process;
123 124
		to_process = to_process & ~(SHA256_BLOCK_SIZE - 1);

125 126
		if (buf_len) {
			data_len = buf_len;
127 128 129 130
			in_sg = nx_build_sg_list(nx_ctx->in_sg,
						 (u8 *) sctx->buf,
						 &data_len,
						 max_sg_len);
131

132 133
			if (data_len != buf_len) {
				rc = -EINVAL;
134
				goto out;
135
			}
136
		}
137 138

		data_len = to_process - buf_len;
139 140
		in_sg = nx_build_sg_list(in_sg, (u8 *) data,
					 &data_len, max_sg_len);
141

142
		nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
143 144 145 146 147 148 149 150 151

		to_process = (data_len + buf_len);
		leftover = total - to_process;

		/*
		 * we've hit the nx chip previously and we're updating
		 * again, so copy over the partial digest.
		 */
		memcpy(csbcpb->cpb.sha256.input_partial_digest,
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
			       csbcpb->cpb.sha256.message_digest,
			       SHA256_DIGEST_SIZE);

		if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
			rc = -EINVAL;
			goto out;
		}

		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
				   desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
		if (rc)
			goto out;

		atomic_inc(&(nx_ctx->stats->sha256_ops));

		total -= to_process;
168 169 170
		data += to_process - buf_len;
		buf_len = 0;

171
	} while (leftover >= SHA256_BLOCK_SIZE);
172 173

	/* copy the leftover back into the state struct */
174
	if (leftover)
175
		memcpy(sctx->buf, data, leftover);
176 177 178

	sctx->count += len;
	memcpy(sctx->state, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
179
out:
180
	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
181 182 183 184 185 186 187 188
	return rc;
}

static int nx_sha256_final(struct shash_desc *desc, u8 *out)
{
	struct sha256_state *sctx = shash_desc_ctx(desc);
	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
	struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
189
	struct nx_sg *in_sg, *out_sg;
190
	unsigned long irq_flags;
191 192
	u32 max_sg_len;
	int rc = 0;
193
	int len;
194

195 196
	spin_lock_irqsave(&nx_ctx->lock, irq_flags);

197 198 199 200 201
	max_sg_len = min_t(u64, nx_ctx->ap->sglen,
			nx_driver.of.max_sg_len/sizeof(struct nx_sg));
	max_sg_len = min_t(u64, max_sg_len,
			nx_ctx->ap->databytelen/NX_PAGE_SIZE);

202 203 204
	/* final is represented by continuing the operation and indicating that
	 * this is not an intermediate operation */
	if (sctx->count >= SHA256_BLOCK_SIZE) {
205 206
		/* we've hit the nx chip previously, now we're finalizing,
		 * so copy over the partial digest */
207 208 209 210 211 212
		memcpy(csbcpb->cpb.sha256.input_partial_digest, sctx->state, SHA256_DIGEST_SIZE);
		NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
		NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
	} else {
		NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
		NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
213 214
	}

215
	csbcpb->cpb.sha256.message_bit_length = (u64) (sctx->count * 8);
216

217
	len = sctx->count & (SHA256_BLOCK_SIZE - 1);
218 219
	in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) sctx->buf,
				 &len, max_sg_len);
220

221 222
	if (len != (sctx->count & (SHA256_BLOCK_SIZE - 1))) {
		rc = -EINVAL;
223
		goto out;
224
	}
225 226

	len = SHA256_DIGEST_SIZE;
227
	out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, max_sg_len);
228

229 230
	if (len != SHA256_DIGEST_SIZE) {
		rc = -EINVAL;
231
		goto out;
232
	}
233

234 235
	nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
236 237 238 239 240 241 242 243 244 245 246 247
	if (!nx_ctx->op.outlen) {
		rc = -EINVAL;
		goto out;
	}

	rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
			   desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
	if (rc)
		goto out;

	atomic_inc(&(nx_ctx->stats->sha256_ops));

248
	atomic64_add(sctx->count, &(nx_ctx->stats->sha256_bytes));
249 250
	memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
out:
251
	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
252 253 254 255 256 257 258
	return rc;
}

static int nx_sha256_export(struct shash_desc *desc, void *out)
{
	struct sha256_state *sctx = shash_desc_ctx(desc);

259
	memcpy(out, sctx, sizeof(*sctx));
260 261 262 263 264 265 266 267

	return 0;
}

static int nx_sha256_import(struct shash_desc *desc, const void *in)
{
	struct sha256_state *sctx = shash_desc_ctx(desc);

268
	memcpy(sctx, in, sizeof(*sctx));
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

	return 0;
}

struct shash_alg nx_shash_sha256_alg = {
	.digestsize = SHA256_DIGEST_SIZE,
	.init       = nx_sha256_init,
	.update     = nx_sha256_update,
	.final      = nx_sha256_final,
	.export     = nx_sha256_export,
	.import     = nx_sha256_import,
	.descsize   = sizeof(struct sha256_state),
	.statesize  = sizeof(struct sha256_state),
	.base       = {
		.cra_name        = "sha256",
		.cra_driver_name = "sha256-nx",
		.cra_priority    = 300,
		.cra_flags       = CRYPTO_ALG_TYPE_SHASH,
		.cra_blocksize   = SHA256_BLOCK_SIZE,
		.cra_module      = THIS_MODULE,
		.cra_ctxsize     = sizeof(struct nx_crypto_ctx),
H
Herbert Xu 已提交
290
		.cra_init        = nx_crypto_ctx_sha256_init,
291 292 293
		.cra_exit        = nx_crypto_ctx_exit,
	}
};