Philippe Reynes | 0836a2a | 2024-12-19 14:05:49 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * FIPS-180-2 compliant SHA-256 implementation |
| 4 | * |
| 5 | * Copyright (C) 2001-2003 Christophe Devine |
| 6 | */ |
| 7 | |
| 8 | #ifndef USE_HOSTCC |
| 9 | #include <u-boot/schedule.h> |
| 10 | #endif /* USE_HOSTCC */ |
| 11 | #include <string.h> |
| 12 | #include <u-boot/sha256.h> |
| 13 | |
| 14 | #include <linux/compiler_attributes.h> |
| 15 | |
| 16 | /* |
| 17 | * Output = SHA-256( input buffer ). Trigger the watchdog every 'chunk_sz' |
| 18 | * bytes of input processed. |
| 19 | */ |
| 20 | void sha256_csum_wd(const unsigned char *input, unsigned int ilen, |
| 21 | unsigned char *output, unsigned int chunk_sz) |
| 22 | { |
| 23 | sha256_context ctx; |
| 24 | #if !defined(USE_HOSTCC) && \ |
| 25 | (defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)) |
| 26 | const unsigned char *end; |
| 27 | unsigned char *curr; |
| 28 | int chunk; |
| 29 | #endif |
| 30 | |
| 31 | sha256_starts(&ctx); |
| 32 | |
| 33 | #if !defined(USE_HOSTCC) && \ |
| 34 | (defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)) |
| 35 | curr = (unsigned char *)input; |
| 36 | end = input + ilen; |
| 37 | while (curr < end) { |
| 38 | chunk = end - curr; |
| 39 | if (chunk > chunk_sz) |
| 40 | chunk = chunk_sz; |
| 41 | sha256_update(&ctx, curr, chunk); |
| 42 | curr += chunk; |
| 43 | schedule(); |
| 44 | } |
| 45 | #else |
| 46 | sha256_update(&ctx, input, ilen); |
| 47 | #endif |
| 48 | |
| 49 | sha256_finish(&ctx, output); |
| 50 | } |