Skip to content

Commit

Permalink
add ECDSA verification interface (#10)
Browse files Browse the repository at this point in the history
* add ECDSA verification interface

* update firmware version to 1.5.0
  • Loading branch information
lihuanhuan authored Jan 16, 2024
1 parent 1d4f1c9 commit a491a6a
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 25 deletions.
6 changes: 6 additions & 0 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $(OUTPUT_DIRECTORY)/nrf52832_xxaa.out: \
SRC_FILES += \
$(PROJ_DIR)/main.c \
$(PROJ_DIR)/nfc.c \
$(PROJ_DIR)/ecdsa.c \
$(SDK_ROOT)/components/ble/ble_advertising/ble_advertising.c \
$(SDK_ROOT)/components/ble/ble_link_ctx_manager/ble_link_ctx_manager.c \
$(SDK_ROOT)/components/ble/ble_racp/ble_racp.c \
Expand Down Expand Up @@ -54,6 +55,9 @@ SRC_FILES += \
$(SDK_ROOT)/components/libraries/bsp/bsp_btn_ble.c \
$(SDK_ROOT)/components/libraries/button/app_button.c \
$(SDK_ROOT)/components/libraries/crc16/crc16.c \
$(SDK_ROOT)/components/libraries/crypto/backend/micro_ecc/micro_ecc_backend_ecc.c \
$(SDK_ROOT)/components/libraries/crypto/backend/micro_ecc/micro_ecc_backend_ecdh.c \
$(SDK_ROOT)/components/libraries/crypto/backend/micro_ecc/micro_ecc_backend_ecdsa.c \
$(SDK_ROOT)/components/libraries/crypto/backend/nrf_hw/nrf_hw_backend_init.c \
$(SDK_ROOT)/components/libraries/crypto/backend/nrf_hw/nrf_hw_backend_rng.c \
$(SDK_ROOT)/components/libraries/crypto/backend/nrf_hw/nrf_hw_backend_rng_mbedtls.c \
Expand Down Expand Up @@ -285,6 +289,7 @@ INC_FOLDERS += \
$(SDK_ROOT)/external/nrf_tls/mbedtls/nrf_crypto/config \
$(SDK_ROOT)/external/segger_rtt \
$(SDK_ROOT)/external/utf_converter \
$(SDK_ROOT)/external/micro-ecc/micro-ecc \
$(SDK_ROOT)/integration/nrfx \
$(SDK_ROOT)/integration/nrfx/legacy \
$(SDK_ROOT)/modules/nrfx \
Expand All @@ -297,6 +302,7 @@ LIB_FILES += \
$(SDK_ROOT)/components/nfc/t4t_lib/nfc_t4t_lib_gcc.a \
$(SDK_ROOT)/external/nrf_cc310/lib/cortex-m4/hard-float/libnrf_cc310_0.9.12.a \
$(SDK_ROOT)/external/nrf_oberon/lib/cortex-m4/hard-float/liboberon_3.0.1.a \
$(SDK_ROOT)/external/micro-ecc/nrf52hf_armgcc/armgcc/micro_ecc_lib_nrf52.a \

# Optimization flags
OPT = -O3 -g3
Expand Down
101 changes: 101 additions & 0 deletions app/ecdsa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "nrf_crypto.h"
#include "nrf_crypto_ecc.h"
#include "nrf_crypto_error.h"
#include "nrf_crypto_ecdsa.h"

ret_code_t generate_ecdsa_keypair(uint8_t *pri_key, uint8_t *pubkey){
ret_code_t err_code = NRF_SUCCESS;
nrf_crypto_ecc_key_pair_generate_context_t context;

size_t private_key_size = 32;
size_t public_key_size = 64;

uint8_t sk[32],pk[64];

nrf_crypto_ecc_private_key_t private_key;
nrf_crypto_ecc_public_key_t public_key;

err_code = nrf_crypto_ecc_key_pair_generate(&context,
&g_nrf_crypto_ecc_secp256k1_curve_info,
&private_key,
&public_key);
if(err_code != NRF_SUCCESS){
return err_code;
}

err_code = nrf_crypto_ecc_private_key_to_raw(&private_key,
sk,
&private_key_size);
if(err_code != NRF_SUCCESS){
return err_code;
}

err_code = nrf_crypto_ecc_public_key_to_raw(&public_key,
pk,
&public_key_size);
if(err_code != NRF_SUCCESS){
return err_code;
}
nrf_crypto_internal_swap_endian(pri_key,sk,32);
nrf_crypto_internal_double_swap_endian(pubkey,pk,32);
return NRF_SUCCESS;

}

ret_code_t sign_ecdsa(uint8_t *pri_key, uint8_t *hash, uint8_t *signature){
ret_code_t err_code = NRF_SUCCESS;
nrf_crypto_ecdsa_sign_context_t context;
size_t signature_size = 64;
nrf_crypto_ecc_private_key_t private_key;

uint8_t sk[32],sign[64],hash1[32];

nrf_crypto_internal_swap_endian(sk,pri_key,32);
nrf_crypto_internal_swap_endian(hash1,hash,32);

err_code = nrf_crypto_ecc_private_key_from_raw(&g_nrf_crypto_ecc_secp256k1_curve_info,
&private_key,
sk,
32);
if(err_code != NRF_SUCCESS){
return err_code;
}

err_code = nrf_crypto_ecdsa_sign(&context,
&private_key,
hash1,
32,
sign,
&signature_size);
if(err_code != NRF_SUCCESS){
return err_code;
}
nrf_crypto_internal_double_swap_endian(signature,sign,32);
return NRF_SUCCESS;
}

ret_code_t sign_ecdsa_msg(uint8_t *pri_key, uint8_t *msg, uint32_t msg_len, uint8_t *signature){
ret_code_t err_code = NRF_SUCCESS;
nrf_crypto_hash_context_t hash_context = {0};
uint8_t hash[32];
size_t hash_len = 32;

err_code = nrf_crypto_hash_calculate(&hash_context,
&g_nrf_crypto_hash_sha256_info,
msg,
msg_len,
hash,
&hash_len);
if(err_code != NRF_SUCCESS){
return err_code;
}

err_code = sign_ecdsa(pri_key,hash,signature);
if(err_code != NRF_SUCCESS){
return err_code;
}
return NRF_SUCCESS;
}
15 changes: 15 additions & 0 deletions app/ecdsa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef __NORDIC_52832_ECDSA_
#define __NORDIC_52832_ECDSA_

typedef struct {
uint32_t key_lock_flag;
uint32_t key_flag;
uint8_t private_key[32];
uint8_t public_key[64];
} ecdsa_key_info_t;

ret_code_t generate_ecdsa_keypair(uint8_t *pri_key, uint8_t *pubkey);
ret_code_t sign_ecdsa(uint8_t *pri_key, uint8_t *hash, uint8_t *signature);
ret_code_t sign_ecdsa_msg(uint8_t *pri_key, uint8_t *msg, uint32_t msg_len, uint8_t *signature);

#endif
2 changes: 1 addition & 1 deletion app/firmware_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define MANUFACTURER_ID 0x55AA55AA55 /**< DUMMY Manufacturer ID. Will be passed to Device Information Service. You shall use the ID for your Company*/
#define ORG_UNIQUE_ID 0xEEBBEE /**< DUMMY Organisation Unique ID. Will be passed to Device Information Service. You shall use the Organisation Unique ID relevant for your Company */
#define HW_REVISION "1.0.0"
#define FW_REVISION "1.4.2"
#define FW_REVISION "1.5.0"
#define SW_REVISION "s132_nrf52_7.0.1"

#endif //FIRMWARE_CONFIG
Loading

0 comments on commit a491a6a

Please sign in to comment.