1 /*
2  * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stdbool.h>
7 #include <string.h>
8 #include <sys/param.h>
9 #include "rom/ecdsa.h"
10 
11 #define ROM_FUNC_TYPECAST  int(*)(const uint8_t*, const uint8_t*, int, const uint8_t*, uint8_t*)
12 
13 extern uint32_t _rom_eco_version;
14 int (*_rom_ets_ecdsa_verify)(const uint8_t*, const uint8_t*, int, const uint8_t*, uint8_t*);
15 
16 /* On ESP32-C6 ECO 0, the ROM interface hasn't exposed ets_ecdsa_verify symbol, so for that we have defined
17  * the function here and then jump to the absolute address in ROM.
18  *
19  * There is a possibility of updating the ROM in the future chip revisions without any major upgrades,
20  * in that case, the same binary should work as is on the new chip revision. For that, we check the _rom_eco_version
21  * and if its a newer one, we jump to the new ROM interface. These addresses won't change in the future
22  *
23  * ets_ecdsa_verify symbol will be present in the upcoming ROM ECO versions so even though we have defined it here,
24  * linker will pick the symbol address from rom.ld file
25  */
ets_ecdsa_verify(const uint8_t * key,const uint8_t * sig,ECDSA_CURVE curve_id,const uint8_t * image_digest,uint8_t * verified_digest)26 int ets_ecdsa_verify(const uint8_t *key, const uint8_t *sig, ECDSA_CURVE curve_id, const uint8_t *image_digest, uint8_t *verified_digest)
27 {
28     if (_rom_eco_version == 0) {
29         _rom_ets_ecdsa_verify = (ROM_FUNC_TYPECAST)0x4001a824;
30         return _rom_ets_ecdsa_verify(key, sig, curve_id, image_digest, verified_digest);
31     } else {
32         _rom_ets_ecdsa_verify = (ROM_FUNC_TYPECAST)0x40001490;
33         return _rom_ets_ecdsa_verify(key, sig, curve_id, image_digest, verified_digest);
34     }
35 }
36