@ -14,6 +14,12 @@
# include <unistd.h>
# include <sys/time.h>
# include <stdio.h>
// Simple debug macros
# define DEBUG_CATEGORY_CRYPTO 1
# define DEBUG_ERROR(category, fmt, ...) fprintf(stderr, "ERROR: " fmt "\n", ##__VA_ARGS__)
# define DEBUG_INFO(category, fmt, ...) fprintf(stdout, "INFO: " fmt "\n", ##__VA_ARGS__)
# include <stdio.h>
# include <fcntl.h>
# include "crc32.h"
@ -66,7 +72,9 @@ static int sc_validate_key(const uint8_t *public_key)
if ( ! curve ) {
curve = uECC_secp256r1 ( ) ;
}
return uECC_valid_public_key ( public_key , curve ) ;
int result = uECC_valid_public_key ( public_key , curve ) ;
DEBUG_INFO ( DEBUG_CATEGORY_CRYPTO , " sc_validate_key: uECC_valid_public_key returned %d " , result ) ;
return result ;
}
sc_status_t sc_generate_keypair ( struct SC_MYKEYS * pk )
@ -102,6 +110,7 @@ static int hex_to_binary(const char *hex_str, uint8_t *binary, size_t binary_len
sc_status_t sc_init_local_keys ( struct SC_MYKEYS * mykeys , const char * public_key , const char * private_key ) {
if ( ! mykeys | | ! public_key | | ! private_key ) {
DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " sc_init_local_keys: invalid arguments " ) ;
return SC_ERR_INVALID_ARG ;
}
@ -109,13 +118,26 @@ sc_status_t sc_init_local_keys(struct SC_MYKEYS *mykeys, const char *public_key,
curve = uECC_secp256r1 ( ) ;
}
/* Validate public key */
if ( sc_validate_key ( public_key ) ! = 0 ) {
DEBUG_INFO ( DEBUG_CATEGORY_CRYPTO , " sc_init_local_keys: public_key len=%zu, private_key len=%zu " ,
strlen ( public_key ) , strlen ( private_key ) ) ;
/* Convert hex to binary first */
if ( hex_to_binary ( public_key , mykeys - > public_key , SC_PUBKEY_SIZE ) ) {
DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " sc_init_local_keys: failed to convert public key from hex " ) ;
return SC_ERR_INVALID_ARG ;
}
if ( hex_to_binary ( private_key , mykeys - > private_key , SC_PRIVKEY_SIZE ) ) {
DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " sc_init_local_keys: failed to convert private key from hex " ) ;
return SC_ERR_INVALID_ARG ;
}
/* Validate the converted binary public key */
if ( sc_validate_key ( mykeys - > public_key ) ! = 0 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " sc_init_local_keys: public key validation failed " ) ;
return SC_ERR_INVALID_ARG ;
}
if ( hex_to_binary ( public_key , mykeys - > public_key , SC_PUBKEY_SIZE ) ) return SC_ERR_INVALID_ARG ;
if ( hex_to_binary ( private_key , mykeys - > private_key , SC_PRIVKEY_SIZE ) ) return SC_ERR_INVALID_ARG ;
DEBUG_INFO ( DEBUG_CATEGORY_CRYPTO , " sc_init_local_keys: keys initialized successfully " ) ;
return SC_OK ;
}