summaryrefslogtreecommitdiff
path: root/arch/arm/cpu/armv8/crypto/sha2-ce-glue.c
blob: 31e436ef1dfef6c1f483ff1c09b618a9010ea8ab (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: GPL-2.0-only
/*
 * sha2-ce-glue.c - SHA-256 using ARMv8 Crypto Extensions
 *
 * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
 * Copyright 2022 NXP
 */

#include <linux/kernel.h>
#include <linux/linkage.h>
#include <crypto/sha256_base.h>

struct sha256_ce_state {
	struct sha256_state	sst;
	u32			finalize;
};

extern const u32 sha256_ce_offsetof_count;
extern const u32 sha256_ce_offsetof_finalize;

asmlinkage int sha2_ce_transform(struct sha256_ce_state *sst, u8 const *src,
				 int blocks);

static void __sha2_ce_transform(struct sha256_state *sst, u8 const *src,
				int blocks)
{
	while (blocks) {
		int rem;

		rem = sha2_ce_transform(container_of(sst, struct sha256_ce_state,
						     sst), src, blocks);
		src += (blocks - rem) * SHA256_BLOCK_SIZE;
		blocks = rem;
	}
}

const u32 sha256_ce_offsetof_count = offsetof(struct sha256_ce_state,
					      sst.count);
const u32 sha256_ce_offsetof_finalize = offsetof(struct sha256_ce_state,
						 finalize);

static void sha256_ce_update(struct sha256_ce_state *sctx, const u8 *data,
			     unsigned int len)
{
	sctx->finalize = 0;
	sha256_base_do_update(&sctx->sst, data, len, __sha2_ce_transform);
}

static void sha256_ce_final(struct sha256_ce_state *sctx, u8 *out)
{
	sctx->finalize = 0;
	sha256_base_do_finalize(&sctx->sst, __sha2_ce_transform);
	sha256_base_finish(&sctx->sst, out);
}

/*
 * Output = SHA-256( input buffer ).
 */
void sha256_ce(const unsigned char *input, unsigned int ilen, unsigned char *output)
{
	struct sha256_ce_state sctx;

	sha256_init(&sctx.sst);
	sha256_ce_update(&sctx, input, ilen);
	sha256_ce_final(&sctx, output);
}