b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #include <common.h>
|
| 2 | #include <watchdog.h>
|
| 3 | #include <linux/string.h>
|
| 4 | #include <sha256.h>
|
| 5 | #include <tee.h>
|
| 6 | #include <asm/arch/cpu.h>
|
| 7 |
|
| 8 | void sha256_starts(sha256_context * ctx)
|
| 9 | {
|
| 10 | #ifdef CONFIG_ASR_TE200
|
| 11 | if (cpu_is_asr1903_b0()) {
|
| 12 | #ifdef CONFIG_TEE_OS
|
| 13 | sha256_starts_optee(ctx);
|
| 14 | if (ctx->tee_sha256 == TEE_SUCCESS)
|
| 15 | return;
|
| 16 | #else
|
| 17 | return sha256_starts_te200(ctx);
|
| 18 | #endif
|
| 19 | }
|
| 20 | #endif
|
| 21 |
|
| 22 | #ifdef CONFIG_ASR_BCM
|
| 23 | if (cpu_is_asr1901() || cpu_is_asr1906()) {
|
| 24 | #ifdef CONFIG_TEE_OS
|
| 25 | sha256_starts_optee(ctx);
|
| 26 | if (ctx->tee_sha256 == TEE_SUCCESS)
|
| 27 | return;
|
| 28 | #else
|
| 29 | return sha256_starts_bcm(ctx);
|
| 30 | #endif
|
| 31 | }
|
| 32 | #endif
|
| 33 |
|
| 34 | return sha256_starts_neon(ctx);
|
| 35 | }
|
| 36 |
|
| 37 | void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
|
| 38 | {
|
| 39 | #ifdef CONFIG_ASR_TE200
|
| 40 | if (cpu_is_asr1903_b0()) {
|
| 41 | #ifdef CONFIG_TEE_OS
|
| 42 | if (ctx->tee_sha256 == TEE_SUCCESS)
|
| 43 | return sha256_update_optee(ctx, input, length);
|
| 44 | #else
|
| 45 | return sha256_update_te200(ctx, input, length);
|
| 46 | #endif
|
| 47 | }
|
| 48 | #endif
|
| 49 |
|
| 50 | #ifdef CONFIG_ASR_BCM
|
| 51 | if (cpu_is_asr1901() || cpu_is_asr1906()) {
|
| 52 | #ifdef CONFIG_TEE_OS
|
| 53 | if (ctx->tee_sha256 == TEE_SUCCESS)
|
| 54 | return sha256_update_optee(ctx, input, length);
|
| 55 | #else
|
| 56 | return sha256_update_bcm(ctx, input, length);
|
| 57 | #endif
|
| 58 | }
|
| 59 | #endif
|
| 60 |
|
| 61 | return sha256_update_neon(ctx, input, length);
|
| 62 | }
|
| 63 |
|
| 64 | void sha256_finish(sha256_context * ctx, uint8_t digest[32])
|
| 65 | {
|
| 66 | #ifdef CONFIG_ASR_TE200
|
| 67 | if (cpu_is_asr1903_b0()) {
|
| 68 | #ifdef CONFIG_TEE_OS
|
| 69 | if (ctx->tee_sha256 == TEE_SUCCESS)
|
| 70 | return sha256_finish_optee(ctx, digest);
|
| 71 | #else
|
| 72 | return sha256_finish_te200(ctx, digest);
|
| 73 | #endif
|
| 74 | }
|
| 75 | #endif
|
| 76 |
|
| 77 | #ifdef CONFIG_ASR_BCM
|
| 78 | if (cpu_is_asr1901() || cpu_is_asr1906()) {
|
| 79 | #ifdef CONFIG_TEE_OS
|
| 80 | if (ctx->tee_sha256 == TEE_SUCCESS)
|
| 81 | return sha256_finish_optee(ctx, digest);
|
| 82 | #else
|
| 83 | return sha256_finish_bcm(ctx, digest);
|
| 84 | #endif
|
| 85 | }
|
| 86 | #endif
|
| 87 |
|
| 88 | return sha256_finish_neon(ctx, digest);
|
| 89 | }
|
| 90 |
|
| 91 |
|
| 92 | void sha256_csum_wd(const unsigned char *input, unsigned int ilen,
|
| 93 | unsigned char *output, unsigned int chunk_sz)
|
| 94 | {
|
| 95 | sha256_context ctx;
|
| 96 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
|
| 97 | unsigned char *end, *curr;
|
| 98 | int chunk;
|
| 99 | #endif
|
| 100 |
|
| 101 | sha256_starts(&ctx);
|
| 102 |
|
| 103 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
|
| 104 | curr = input;
|
| 105 | end = input + ilen;
|
| 106 | while (curr < end) {
|
| 107 | chunk = end - curr;
|
| 108 | if (chunk > chunk_sz)
|
| 109 | chunk = chunk_sz;
|
| 110 | sha256_update(&ctx, curr, chunk);
|
| 111 | curr += chunk;
|
| 112 | WATCHDOG_RESET();
|
| 113 | }
|
| 114 | #else
|
| 115 | sha256_update(&ctx, input, ilen);
|
| 116 | #endif
|
| 117 |
|
| 118 | sha256_finish(&ctx, output);
|
| 119 | }
|