Skip to content

Commit

Permalink
add swap endian for 32-byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
dangfan committed Nov 7, 2022
1 parent 7f74deb commit 1713aa0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/ecc.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ typedef struct {
uint8_t pub[MAX_EC_PUBLIC_KEY];
} ecc_key_t;

void swap_big_number_endian(uint8_t buf[32]);

/**
* Generate an ECDSA key pair
*
Expand Down
14 changes: 13 additions & 1 deletion src/ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ static const uint8_t grp_id[] = {
static const K__ed25519_public_key gx = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9};

void swap_big_number_endian(uint8_t buf[32]) {
for (int i = 0; i < 16; ++i) {
uint8_t tmp = buf[31 - i];
buf[31 - i] = buf[i];
buf[i] = tmp;
}
}

__attribute__((weak)) int ecc_generate(key_type_t type, ecc_key_t *key) {
#ifdef USE_MBEDCRYPTO
if (!IS_ECC(type)) return -1;
Expand Down Expand Up @@ -189,7 +197,11 @@ __attribute__((weak)) int ecdh(key_type_t type, const uint8_t *priv_key, const u
mbedtls_ecp_point_free(&pnt);
} else { // ed25519 & x25519
if (type == ED25519) return -1;
K__x25519(out, priv_key, receiver_pub_key);
uint8_t pub[32];
memcpy(pub, receiver_pub_key, 32);
swap_big_number_endian(pub);
K__x25519(out, priv_key, pub);
swap_big_number_endian(out);
}
#else
(void)type;
Expand Down

0 comments on commit 1713aa0

Please sign in to comment.