-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ECDSA verification interface (#10)
* add ECDSA verification interface * update firmware version to 1.5.0
- Loading branch information
1 parent
1d4f1c9
commit a491a6a
Showing
6 changed files
with
233 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.