18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * sha256_base.h - core logic for SHA-256 implementations 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#ifndef _CRYPTO_SHA256_BASE_H 98c2ecf20Sopenharmony_ci#define _CRYPTO_SHA256_BASE_H 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h> 128c2ecf20Sopenharmony_ci#include <crypto/sha.h> 138c2ecf20Sopenharmony_ci#include <linux/crypto.h> 148c2ecf20Sopenharmony_ci#include <linux/module.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_citypedef void (sha256_block_fn)(struct sha256_state *sst, u8 const *src, 198c2ecf20Sopenharmony_ci int blocks); 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic inline int sha224_base_init(struct shash_desc *desc) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci struct sha256_state *sctx = shash_desc_ctx(desc); 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci sha224_init(sctx); 268c2ecf20Sopenharmony_ci return 0; 278c2ecf20Sopenharmony_ci} 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic inline int sha256_base_init(struct shash_desc *desc) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci struct sha256_state *sctx = shash_desc_ctx(desc); 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci sha256_init(sctx); 348c2ecf20Sopenharmony_ci return 0; 358c2ecf20Sopenharmony_ci} 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistatic inline int sha256_base_do_update(struct shash_desc *desc, 388c2ecf20Sopenharmony_ci const u8 *data, 398c2ecf20Sopenharmony_ci unsigned int len, 408c2ecf20Sopenharmony_ci sha256_block_fn *block_fn) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci struct sha256_state *sctx = shash_desc_ctx(desc); 438c2ecf20Sopenharmony_ci unsigned int partial = sctx->count % SHA256_BLOCK_SIZE; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci sctx->count += len; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci if (unlikely((partial + len) >= SHA256_BLOCK_SIZE)) { 488c2ecf20Sopenharmony_ci int blocks; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci if (partial) { 518c2ecf20Sopenharmony_ci int p = SHA256_BLOCK_SIZE - partial; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci memcpy(sctx->buf + partial, data, p); 548c2ecf20Sopenharmony_ci data += p; 558c2ecf20Sopenharmony_ci len -= p; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci block_fn(sctx, sctx->buf, 1); 588c2ecf20Sopenharmony_ci } 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci blocks = len / SHA256_BLOCK_SIZE; 618c2ecf20Sopenharmony_ci len %= SHA256_BLOCK_SIZE; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci if (blocks) { 648c2ecf20Sopenharmony_ci block_fn(sctx, data, blocks); 658c2ecf20Sopenharmony_ci data += blocks * SHA256_BLOCK_SIZE; 668c2ecf20Sopenharmony_ci } 678c2ecf20Sopenharmony_ci partial = 0; 688c2ecf20Sopenharmony_ci } 698c2ecf20Sopenharmony_ci if (len) 708c2ecf20Sopenharmony_ci memcpy(sctx->buf + partial, data, len); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci return 0; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistatic inline int sha256_base_do_finalize(struct shash_desc *desc, 768c2ecf20Sopenharmony_ci sha256_block_fn *block_fn) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci const int bit_offset = SHA256_BLOCK_SIZE - sizeof(__be64); 798c2ecf20Sopenharmony_ci struct sha256_state *sctx = shash_desc_ctx(desc); 808c2ecf20Sopenharmony_ci __be64 *bits = (__be64 *)(sctx->buf + bit_offset); 818c2ecf20Sopenharmony_ci unsigned int partial = sctx->count % SHA256_BLOCK_SIZE; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci sctx->buf[partial++] = 0x80; 848c2ecf20Sopenharmony_ci if (partial > bit_offset) { 858c2ecf20Sopenharmony_ci memset(sctx->buf + partial, 0x0, SHA256_BLOCK_SIZE - partial); 868c2ecf20Sopenharmony_ci partial = 0; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci block_fn(sctx, sctx->buf, 1); 898c2ecf20Sopenharmony_ci } 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci memset(sctx->buf + partial, 0x0, bit_offset - partial); 928c2ecf20Sopenharmony_ci *bits = cpu_to_be64(sctx->count << 3); 938c2ecf20Sopenharmony_ci block_fn(sctx, sctx->buf, 1); 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci return 0; 968c2ecf20Sopenharmony_ci} 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic inline int sha256_base_finish(struct shash_desc *desc, u8 *out) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci unsigned int digest_size = crypto_shash_digestsize(desc->tfm); 1018c2ecf20Sopenharmony_ci struct sha256_state *sctx = shash_desc_ctx(desc); 1028c2ecf20Sopenharmony_ci __be32 *digest = (__be32 *)out; 1038c2ecf20Sopenharmony_ci int i; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be32)) 1068c2ecf20Sopenharmony_ci put_unaligned_be32(sctx->state[i], digest++); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci *sctx = (struct sha256_state){}; 1098c2ecf20Sopenharmony_ci return 0; 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci#endif /* _CRYPTO_SHA256_BASE_H */ 113