xref: /openbmc/linux/include/crypto/sm3_base.h (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
1caab277bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
24f0fc160SGilad Ben-Yossef /*
34f0fc160SGilad Ben-Yossef  * sm3_base.h - core logic for SM3 implementations
44f0fc160SGilad Ben-Yossef  *
54f0fc160SGilad Ben-Yossef  * Copyright (C) 2017 ARM Limited or its affiliates.
64f0fc160SGilad Ben-Yossef  * Written by Gilad Ben-Yossef <gilad@benyossef.com>
74f0fc160SGilad Ben-Yossef  */
84f0fc160SGilad Ben-Yossef 
968289c63SMasahiro Yamada #ifndef _CRYPTO_SM3_BASE_H
1068289c63SMasahiro Yamada #define _CRYPTO_SM3_BASE_H
1168289c63SMasahiro Yamada 
124f0fc160SGilad Ben-Yossef #include <crypto/internal/hash.h>
134f0fc160SGilad Ben-Yossef #include <crypto/sm3.h>
144f0fc160SGilad Ben-Yossef #include <linux/crypto.h>
154f0fc160SGilad Ben-Yossef #include <linux/module.h>
16*458c0480SArvind Sankar #include <linux/string.h>
174f0fc160SGilad Ben-Yossef #include <asm/unaligned.h>
184f0fc160SGilad Ben-Yossef 
194f0fc160SGilad Ben-Yossef typedef void (sm3_block_fn)(struct sm3_state *sst, u8 const *src, int blocks);
204f0fc160SGilad Ben-Yossef 
sm3_base_init(struct shash_desc * desc)214f0fc160SGilad Ben-Yossef static inline int sm3_base_init(struct shash_desc *desc)
224f0fc160SGilad Ben-Yossef {
234f0fc160SGilad Ben-Yossef 	struct sm3_state *sctx = shash_desc_ctx(desc);
244f0fc160SGilad Ben-Yossef 
254f0fc160SGilad Ben-Yossef 	sctx->state[0] = SM3_IVA;
264f0fc160SGilad Ben-Yossef 	sctx->state[1] = SM3_IVB;
274f0fc160SGilad Ben-Yossef 	sctx->state[2] = SM3_IVC;
284f0fc160SGilad Ben-Yossef 	sctx->state[3] = SM3_IVD;
294f0fc160SGilad Ben-Yossef 	sctx->state[4] = SM3_IVE;
304f0fc160SGilad Ben-Yossef 	sctx->state[5] = SM3_IVF;
314f0fc160SGilad Ben-Yossef 	sctx->state[6] = SM3_IVG;
324f0fc160SGilad Ben-Yossef 	sctx->state[7] = SM3_IVH;
334f0fc160SGilad Ben-Yossef 	sctx->count = 0;
344f0fc160SGilad Ben-Yossef 
354f0fc160SGilad Ben-Yossef 	return 0;
364f0fc160SGilad Ben-Yossef }
374f0fc160SGilad Ben-Yossef 
sm3_base_do_update(struct shash_desc * desc,const u8 * data,unsigned int len,sm3_block_fn * block_fn)384f0fc160SGilad Ben-Yossef static inline int sm3_base_do_update(struct shash_desc *desc,
394f0fc160SGilad Ben-Yossef 				      const u8 *data,
404f0fc160SGilad Ben-Yossef 				      unsigned int len,
414f0fc160SGilad Ben-Yossef 				      sm3_block_fn *block_fn)
424f0fc160SGilad Ben-Yossef {
434f0fc160SGilad Ben-Yossef 	struct sm3_state *sctx = shash_desc_ctx(desc);
444f0fc160SGilad Ben-Yossef 	unsigned int partial = sctx->count % SM3_BLOCK_SIZE;
454f0fc160SGilad Ben-Yossef 
464f0fc160SGilad Ben-Yossef 	sctx->count += len;
474f0fc160SGilad Ben-Yossef 
484f0fc160SGilad Ben-Yossef 	if (unlikely((partial + len) >= SM3_BLOCK_SIZE)) {
494f0fc160SGilad Ben-Yossef 		int blocks;
504f0fc160SGilad Ben-Yossef 
514f0fc160SGilad Ben-Yossef 		if (partial) {
524f0fc160SGilad Ben-Yossef 			int p = SM3_BLOCK_SIZE - partial;
534f0fc160SGilad Ben-Yossef 
544f0fc160SGilad Ben-Yossef 			memcpy(sctx->buffer + partial, data, p);
554f0fc160SGilad Ben-Yossef 			data += p;
564f0fc160SGilad Ben-Yossef 			len -= p;
574f0fc160SGilad Ben-Yossef 
584f0fc160SGilad Ben-Yossef 			block_fn(sctx, sctx->buffer, 1);
594f0fc160SGilad Ben-Yossef 		}
604f0fc160SGilad Ben-Yossef 
614f0fc160SGilad Ben-Yossef 		blocks = len / SM3_BLOCK_SIZE;
624f0fc160SGilad Ben-Yossef 		len %= SM3_BLOCK_SIZE;
634f0fc160SGilad Ben-Yossef 
644f0fc160SGilad Ben-Yossef 		if (blocks) {
654f0fc160SGilad Ben-Yossef 			block_fn(sctx, data, blocks);
664f0fc160SGilad Ben-Yossef 			data += blocks * SM3_BLOCK_SIZE;
674f0fc160SGilad Ben-Yossef 		}
684f0fc160SGilad Ben-Yossef 		partial = 0;
694f0fc160SGilad Ben-Yossef 	}
704f0fc160SGilad Ben-Yossef 	if (len)
714f0fc160SGilad Ben-Yossef 		memcpy(sctx->buffer + partial, data, len);
724f0fc160SGilad Ben-Yossef 
734f0fc160SGilad Ben-Yossef 	return 0;
744f0fc160SGilad Ben-Yossef }
754f0fc160SGilad Ben-Yossef 
sm3_base_do_finalize(struct shash_desc * desc,sm3_block_fn * block_fn)764f0fc160SGilad Ben-Yossef static inline int sm3_base_do_finalize(struct shash_desc *desc,
774f0fc160SGilad Ben-Yossef 					sm3_block_fn *block_fn)
784f0fc160SGilad Ben-Yossef {
794f0fc160SGilad Ben-Yossef 	const int bit_offset = SM3_BLOCK_SIZE - sizeof(__be64);
804f0fc160SGilad Ben-Yossef 	struct sm3_state *sctx = shash_desc_ctx(desc);
814f0fc160SGilad Ben-Yossef 	__be64 *bits = (__be64 *)(sctx->buffer + bit_offset);
824f0fc160SGilad Ben-Yossef 	unsigned int partial = sctx->count % SM3_BLOCK_SIZE;
834f0fc160SGilad Ben-Yossef 
844f0fc160SGilad Ben-Yossef 	sctx->buffer[partial++] = 0x80;
854f0fc160SGilad Ben-Yossef 	if (partial > bit_offset) {
864f0fc160SGilad Ben-Yossef 		memset(sctx->buffer + partial, 0x0, SM3_BLOCK_SIZE - partial);
874f0fc160SGilad Ben-Yossef 		partial = 0;
884f0fc160SGilad Ben-Yossef 
894f0fc160SGilad Ben-Yossef 		block_fn(sctx, sctx->buffer, 1);
904f0fc160SGilad Ben-Yossef 	}
914f0fc160SGilad Ben-Yossef 
924f0fc160SGilad Ben-Yossef 	memset(sctx->buffer + partial, 0x0, bit_offset - partial);
934f0fc160SGilad Ben-Yossef 	*bits = cpu_to_be64(sctx->count << 3);
944f0fc160SGilad Ben-Yossef 	block_fn(sctx, sctx->buffer, 1);
954f0fc160SGilad Ben-Yossef 
964f0fc160SGilad Ben-Yossef 	return 0;
974f0fc160SGilad Ben-Yossef }
984f0fc160SGilad Ben-Yossef 
sm3_base_finish(struct shash_desc * desc,u8 * out)994f0fc160SGilad Ben-Yossef static inline int sm3_base_finish(struct shash_desc *desc, u8 *out)
1004f0fc160SGilad Ben-Yossef {
1014f0fc160SGilad Ben-Yossef 	struct sm3_state *sctx = shash_desc_ctx(desc);
1024f0fc160SGilad Ben-Yossef 	__be32 *digest = (__be32 *)out;
1034f0fc160SGilad Ben-Yossef 	int i;
1044f0fc160SGilad Ben-Yossef 
1054f0fc160SGilad Ben-Yossef 	for (i = 0; i < SM3_DIGEST_SIZE / sizeof(__be32); i++)
1064f0fc160SGilad Ben-Yossef 		put_unaligned_be32(sctx->state[i], digest++);
1074f0fc160SGilad Ben-Yossef 
108*458c0480SArvind Sankar 	memzero_explicit(sctx, sizeof(*sctx));
1094f0fc160SGilad Ben-Yossef 	return 0;
1104f0fc160SGilad Ben-Yossef }
11168289c63SMasahiro Yamada 
11268289c63SMasahiro Yamada #endif /* _CRYPTO_SM3_BASE_H */
113