Raymond Mao | f51f355 | 2024-10-03 14:50:19 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Hash shim layer on MbedTLS Crypto library |
| 4 | * |
| 5 | * Copyright (c) 2024 Linaro Limited |
| 6 | * Author: Raymond Mao <raymond.mao@linaro.org> |
| 7 | */ |
| 8 | #ifndef USE_HOSTCC |
| 9 | #include <cyclic.h> |
| 10 | #endif /* USE_HOSTCC */ |
| 11 | #include <compiler.h> |
| 12 | #include <u-boot/sha512.h> |
| 13 | |
| 14 | const u8 sha384_der_prefix[SHA384_DER_LEN] = { |
| 15 | 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, |
| 16 | 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, |
| 17 | 0x00, 0x04, 0x30 |
| 18 | }; |
| 19 | |
| 20 | const u8 sha512_der_prefix[SHA512_DER_LEN] = { |
| 21 | 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, |
| 22 | 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, |
| 23 | 0x00, 0x04, 0x40 |
| 24 | }; |
| 25 | |
| 26 | void sha384_starts(sha512_context *ctx) |
| 27 | { |
| 28 | mbedtls_sha512_init(ctx); |
| 29 | mbedtls_sha512_starts(ctx, 1); |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | sha384_update(sha512_context *ctx, const uint8_t *input, uint32_t length) |
| 34 | { |
| 35 | mbedtls_sha512_update(ctx, input, length); |
| 36 | } |
| 37 | |
| 38 | void sha384_finish(sha512_context *ctx, uint8_t digest[SHA384_SUM_LEN]) |
| 39 | { |
| 40 | mbedtls_sha512_finish(ctx, digest); |
| 41 | mbedtls_sha512_free(ctx); |
| 42 | } |
| 43 | |
| 44 | void sha384_csum_wd(const unsigned char *input, unsigned int length, |
| 45 | unsigned char *output, unsigned int chunk_sz) |
| 46 | { |
| 47 | mbedtls_sha512(input, length, output, 1); |
| 48 | } |
| 49 | |
| 50 | void sha512_starts(sha512_context *ctx) |
| 51 | { |
| 52 | mbedtls_sha512_init(ctx); |
| 53 | mbedtls_sha512_starts(ctx, 0); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | sha512_update(sha512_context *ctx, const uint8_t *input, uint32_t length) |
| 58 | { |
| 59 | mbedtls_sha512_update(ctx, input, length); |
| 60 | } |
| 61 | |
| 62 | void sha512_finish(sha512_context *ctx, uint8_t digest[SHA512_SUM_LEN]) |
| 63 | { |
| 64 | mbedtls_sha512_finish(ctx, digest); |
| 65 | mbedtls_sha512_free(ctx); |
| 66 | } |
| 67 | |
| 68 | void sha512_csum_wd(const unsigned char *input, unsigned int ilen, |
| 69 | unsigned char *output, unsigned int chunk_sz) |
| 70 | { |
| 71 | sha512_context ctx; |
| 72 | |
| 73 | sha512_starts(&ctx); |
| 74 | |
| 75 | if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) { |
| 76 | const unsigned char *curr = input; |
| 77 | const unsigned char *end = input + ilen; |
| 78 | int chunk; |
| 79 | |
| 80 | while (curr < end) { |
| 81 | chunk = end - curr; |
| 82 | if (chunk > chunk_sz) |
| 83 | chunk = chunk_sz; |
| 84 | sha512_update(&ctx, curr, chunk); |
| 85 | curr += chunk; |
| 86 | schedule(); |
| 87 | } |
| 88 | } else { |
| 89 | sha512_update(&ctx, input, ilen); |
| 90 | } |
| 91 | |
| 92 | sha512_finish(&ctx, output); |
| 93 | } |