1 // SPDX-License-Identifier: GPL-2.0+
3 * Hash shim layer on MbedTLS Crypto library
5 * Copyright (c) 2024 Linaro Limited
10 #endif /* USE_HOSTCC */
12 #include <u-boot/sha1.h>
14 const u8 sha1_der_prefix[SHA1_DER_LEN] = {
15 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,
16 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14
19 void sha1_starts(sha1_context *ctx)
21 mbedtls_sha1_init(ctx);
22 mbedtls_sha1_starts(ctx);
25 void sha1_update(sha1_context *ctx, const unsigned char *input,
28 mbedtls_sha1_update(ctx, input, length);
31 void sha1_finish(sha1_context *ctx, unsigned char output[SHA1_SUM_LEN])
33 mbedtls_sha1_finish(ctx, output);
34 mbedtls_sha1_free(ctx);
37 void sha1_csum_wd(const unsigned char *input, unsigned int ilen,
38 unsigned char *output, unsigned int chunk_sz)
44 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
45 const unsigned char *curr = input;
46 const unsigned char *end = input + ilen;
53 sha1_update(&ctx, curr, chunk);
58 sha1_update(&ctx, input, ilen);
61 sha1_finish(&ctx, output);
64 void sha1_hmac(const unsigned char *key, int keylen,
65 const unsigned char *input, unsigned int ilen,
66 unsigned char *output)
70 unsigned char k_ipad[K_PAD_LEN];
71 unsigned char k_opad[K_PAD_LEN];
72 unsigned char tmpbuf[20];
74 if (keylen > K_PAD_LEN)
77 memset(k_ipad, K_IPAD_VAL, sizeof(k_ipad));
78 memset(k_opad, K_OPAD_VAL, sizeof(k_opad));
80 for (i = 0; i < keylen; i++) {
86 sha1_update(&ctx, k_ipad, sizeof(k_ipad));
87 sha1_update(&ctx, input, ilen);
88 sha1_finish(&ctx, tmpbuf);
91 sha1_update(&ctx, k_opad, sizeof(k_opad));
92 sha1_update(&ctx, tmpbuf, sizeof(tmpbuf));
93 sha1_finish(&ctx, output);
95 memset(k_ipad, 0, sizeof(k_ipad));
96 memset(k_opad, 0, sizeof(k_opad));
97 memset(tmpbuf, 0, sizeof(tmpbuf));
98 memset(&ctx, 0, sizeof(sha1_context));