From 6b5b88bc0322ae61aa6ab38a32bc8edadca8e9a0 Mon Sep 17 00:00:00 2001 From: Lin Jinhan Date: Wed, 1 Apr 2020 11:27:57 +0800 Subject: [PATCH] dm: crypto: add zero hash support Change-Id: Ib0f74e9636a214918511715cdd4ef32ebe65463a Signed-off-by: Lin Jinhan --- drivers/crypto/crypto-uclass.c | 64 +++++++++++++++++++++++++++++ drivers/crypto/rockchip/crypto_v2.c | 33 --------------- include/rockchip/crypto_v2.h | 1 - 3 files changed, 64 insertions(+), 34 deletions(-) diff --git a/drivers/crypto/crypto-uclass.c b/drivers/crypto/crypto-uclass.c index 75b8a4c2db..c6e4c2dbdc 100644 --- a/drivers/crypto/crypto-uclass.c +++ b/drivers/crypto/crypto-uclass.c @@ -5,6 +5,35 @@ #include +static const u8 null_hash_sha1_value[] = { + 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, + 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, + 0xaf, 0xd8, 0x07, 0x09 +}; + +static const u8 null_hash_md5_value[] = { + 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, + 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e +}; + +static const u8 null_hash_sha256_value[] = { + 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, + 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, + 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, + 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 +}; + +static const u8 null_hash_sha512_value[] = { + 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, + 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07, + 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, + 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, + 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, + 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, + 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, + 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e +}; + u32 crypto_algo_nbits(u32 algo) { switch (algo) { @@ -64,6 +93,9 @@ int crypto_sha_init(struct udevice *dev, sha_context *ctx) { const struct dm_crypto_ops *ops = device_get_ops(dev); + if (ctx && !ctx->length) + return 0; + if (!ops || !ops->sha_init) return -ENOSYS; @@ -74,6 +106,9 @@ int crypto_sha_update(struct udevice *dev, u32 *input, u32 len) { const struct dm_crypto_ops *ops = device_get_ops(dev); + if (!len) + return 0; + if (!ops || !ops->sha_update) return -ENOSYS; @@ -83,6 +118,35 @@ int crypto_sha_update(struct udevice *dev, u32 *input, u32 len) int crypto_sha_final(struct udevice *dev, sha_context *ctx, u8 *output) { const struct dm_crypto_ops *ops = device_get_ops(dev); + const u8 *null_hash = NULL; + u32 hash_size = 0; + + if (ctx && !ctx->length && output) { + switch (ctx->algo) { + case CRYPTO_MD5: + null_hash = null_hash_md5_value; + hash_size = sizeof(null_hash_md5_value); + break; + case CRYPTO_SHA1: + null_hash = null_hash_sha1_value; + hash_size = sizeof(null_hash_sha1_value); + break; + case CRYPTO_SHA256: + null_hash = null_hash_sha256_value; + hash_size = sizeof(null_hash_sha256_value); + break; + case CRYPTO_SHA512: + null_hash = null_hash_sha512_value; + hash_size = sizeof(null_hash_sha512_value); + break; + default: + return -EINVAL; + } + + memcpy(output, null_hash, hash_size); + + return 0; + } if (!ops || !ops->sha_final) return -ENOSYS; diff --git a/drivers/crypto/rockchip/crypto_v2.c b/drivers/crypto/rockchip/crypto_v2.c index 1e4af55bb7..7836bfe9cb 100644 --- a/drivers/crypto/rockchip/crypto_v2.c +++ b/drivers/crypto/rockchip/crypto_v2.c @@ -48,35 +48,6 @@ typedef u32 paddr_t; #define virt_to_phys(addr) (((unsigned long)addr) & 0xffffffff) #define phys_to_virt(addr, area) ((unsigned long)addr) -static const u8 null_hash_sha1_value[] = { - 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, - 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, - 0xaf, 0xd8, 0x07, 0x09 -}; - -static const u8 null_hash_md5_value[] = { - 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, - 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e -}; - -static const u8 null_hash_sha256_value[] = { - 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, - 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, - 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, - 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 -}; - -static const u8 null_hash_sha512_value[] = { - 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, - 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07, - 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, - 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, - 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, - 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, - 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, - 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e -}; - fdt_addr_t crypto_base; static void word2byte(u32 word, u8 *ch, u32 endian) @@ -175,22 +146,18 @@ int rk_hash_init(void *hw_ctx, u32 algo, u32 length) case CRYPTO_MD5: reg_ctrl |= CRYPTO_MODE_MD5; tmp_ctx->digest_size = 16; - tmp_ctx->null_hash = null_hash_md5_value; break; case CRYPTO_SHA1: reg_ctrl |= CRYPTO_MODE_SHA1; tmp_ctx->digest_size = 20; - tmp_ctx->null_hash = null_hash_sha1_value; break; case CRYPTO_SHA256: reg_ctrl |= CRYPTO_MODE_SHA256; tmp_ctx->digest_size = 32; - tmp_ctx->null_hash = null_hash_sha256_value; break; case CRYPTO_SHA512: reg_ctrl |= CRYPTO_MODE_SHA512; tmp_ctx->digest_size = 64; - tmp_ctx->null_hash = null_hash_sha512_value; break; default: diff --git a/include/rockchip/crypto_v2.h b/include/rockchip/crypto_v2.h index 41d9720040..db37b4c748 100644 --- a/include/rockchip/crypto_v2.h +++ b/include/rockchip/crypto_v2.h @@ -581,7 +581,6 @@ struct crypto_lli_desc { struct rk_hash_ctx { struct crypto_lli_desc data_lli;/* lli desc */ - const u8 *null_hash; /* when length is zero */ void *cache; /* virt addr for hash src data*/ u32 cache_size; /* data in cached size */ u32 left_len; /* left data to calc */