Sigvart Hovland | 25ec746 | 2019-03-11 15:57:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 Nordic Semiconductor ASA |
| 3 | * |
Dominik Kilian | 421730e | 2022-12-15 15:37:19 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
Sigvart Hovland | 25ec746 | 2019-03-11 15:57:21 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include "cc310_glue.h" |
| 8 | |
| 9 | int cc310_init(void) |
| 10 | { |
| 11 | /* Only initialize once */ |
| 12 | static bool initialized; |
| 13 | |
| 14 | if (!initialized) { |
| 15 | nrf_cc310_enable(); |
| 16 | if (nrf_cc310_bl_init() != 0) { |
| 17 | return -1; |
| 18 | } |
| 19 | initialized = true; |
| 20 | nrf_cc310_disable(); |
| 21 | } |
| 22 | |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | void cc310_sha256_update(nrf_cc310_bl_hash_context_sha256_t *ctx, |
| 27 | const void *data, |
| 28 | uint32_t data_len) |
| 29 | { |
| 30 | /* |
| 31 | * NRF Cryptocell can only read from RAM this allocates a buffer on the stack |
| 32 | * if the data provided is not located in RAM. |
| 33 | */ |
| 34 | |
| 35 | if ((uint32_t) data < CONFIG_SRAM_BASE_ADDRESS) { |
| 36 | uint8_t stack_buffer[data_len]; |
| 37 | uint32_t block_len = data_len; |
| 38 | memcpy(stack_buffer, data, block_len); |
| 39 | nrf_cc310_bl_hash_sha256_update(ctx, stack_buffer, block_len); |
| 40 | } else { |
| 41 | nrf_cc310_bl_hash_sha256_update(ctx, data, data_len); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | int cc310_ecdsa_verify_secp256r1(uint8_t *hash, |
| 46 | uint8_t *public_key, |
| 47 | uint8_t *signature, |
| 48 | size_t hash_len) |
| 49 | { |
| 50 | int rc; |
| 51 | nrf_cc310_bl_ecdsa_verify_context_secp256r1_t ctx; |
| 52 | cc310_init(); |
| 53 | nrf_cc310_enable(); |
| 54 | rc = nrf_cc310_bl_ecdsa_verify_secp256r1(&ctx, |
| 55 | (nrf_cc310_bl_ecc_public_key_secp256r1_t *) public_key, |
| 56 | (nrf_cc310_bl_ecc_signature_secp256r1_t *) signature, |
| 57 | hash, |
| 58 | hash_len); |
| 59 | nrf_cc310_disable(); |
| 60 | return rc; |
| 61 | } |
| 62 | |