18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * sm3_base.h - core logic for SM3 implementations
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2017 ARM Limited or its affiliates.
68c2ecf20Sopenharmony_ci * Written by Gilad Ben-Yossef <gilad@benyossef.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef _CRYPTO_SM3_BASE_H
108c2ecf20Sopenharmony_ci#define _CRYPTO_SM3_BASE_H
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
138c2ecf20Sopenharmony_ci#include <crypto/sm3.h>
148c2ecf20Sopenharmony_ci#include <linux/crypto.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_citypedef void (sm3_block_fn)(struct sm3_state *sst, u8 const *src, int blocks);
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic inline int sm3_base_init(struct shash_desc *desc)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	struct sm3_state *sctx = shash_desc_ctx(desc);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	sctx->state[0] = SM3_IVA;
258c2ecf20Sopenharmony_ci	sctx->state[1] = SM3_IVB;
268c2ecf20Sopenharmony_ci	sctx->state[2] = SM3_IVC;
278c2ecf20Sopenharmony_ci	sctx->state[3] = SM3_IVD;
288c2ecf20Sopenharmony_ci	sctx->state[4] = SM3_IVE;
298c2ecf20Sopenharmony_ci	sctx->state[5] = SM3_IVF;
308c2ecf20Sopenharmony_ci	sctx->state[6] = SM3_IVG;
318c2ecf20Sopenharmony_ci	sctx->state[7] = SM3_IVH;
328c2ecf20Sopenharmony_ci	sctx->count = 0;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	return 0;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic inline int sm3_base_do_update(struct shash_desc *desc,
388c2ecf20Sopenharmony_ci				      const u8 *data,
398c2ecf20Sopenharmony_ci				      unsigned int len,
408c2ecf20Sopenharmony_ci				      sm3_block_fn *block_fn)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	struct sm3_state *sctx = shash_desc_ctx(desc);
438c2ecf20Sopenharmony_ci	unsigned int partial = sctx->count % SM3_BLOCK_SIZE;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	sctx->count += len;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	if (unlikely((partial + len) >= SM3_BLOCK_SIZE)) {
488c2ecf20Sopenharmony_ci		int blocks;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci		if (partial) {
518c2ecf20Sopenharmony_ci			int p = SM3_BLOCK_SIZE - partial;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci			memcpy(sctx->buffer + partial, data, p);
548c2ecf20Sopenharmony_ci			data += p;
558c2ecf20Sopenharmony_ci			len -= p;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci			block_fn(sctx, sctx->buffer, 1);
588c2ecf20Sopenharmony_ci		}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci		blocks = len / SM3_BLOCK_SIZE;
618c2ecf20Sopenharmony_ci		len %= SM3_BLOCK_SIZE;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci		if (blocks) {
648c2ecf20Sopenharmony_ci			block_fn(sctx, data, blocks);
658c2ecf20Sopenharmony_ci			data += blocks * SM3_BLOCK_SIZE;
668c2ecf20Sopenharmony_ci		}
678c2ecf20Sopenharmony_ci		partial = 0;
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci	if (len)
708c2ecf20Sopenharmony_ci		memcpy(sctx->buffer + partial, data, len);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	return 0;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic inline int sm3_base_do_finalize(struct shash_desc *desc,
768c2ecf20Sopenharmony_ci					sm3_block_fn *block_fn)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	const int bit_offset = SM3_BLOCK_SIZE - sizeof(__be64);
798c2ecf20Sopenharmony_ci	struct sm3_state *sctx = shash_desc_ctx(desc);
808c2ecf20Sopenharmony_ci	__be64 *bits = (__be64 *)(sctx->buffer + bit_offset);
818c2ecf20Sopenharmony_ci	unsigned int partial = sctx->count % SM3_BLOCK_SIZE;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	sctx->buffer[partial++] = 0x80;
848c2ecf20Sopenharmony_ci	if (partial > bit_offset) {
858c2ecf20Sopenharmony_ci		memset(sctx->buffer + partial, 0x0, SM3_BLOCK_SIZE - partial);
868c2ecf20Sopenharmony_ci		partial = 0;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		block_fn(sctx, sctx->buffer, 1);
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	memset(sctx->buffer + partial, 0x0, bit_offset - partial);
928c2ecf20Sopenharmony_ci	*bits = cpu_to_be64(sctx->count << 3);
938c2ecf20Sopenharmony_ci	block_fn(sctx, sctx->buffer, 1);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return 0;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic inline int sm3_base_finish(struct shash_desc *desc, u8 *out)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	struct sm3_state *sctx = shash_desc_ctx(desc);
1018c2ecf20Sopenharmony_ci	__be32 *digest = (__be32 *)out;
1028c2ecf20Sopenharmony_ci	int i;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	for (i = 0; i < SM3_DIGEST_SIZE / sizeof(__be32); i++)
1058c2ecf20Sopenharmony_ci		put_unaligned_be32(sctx->state[i], digest++);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	*sctx = (struct sm3_state){};
1088c2ecf20Sopenharmony_ci	return 0;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#endif /* _CRYPTO_SM3_BASE_H */
112