diff --git a/src/keys.c b/src/keys.c index 6ee8f828..6730a3b3 100644 --- a/src/keys.c +++ b/src/keys.c @@ -21,6 +21,7 @@ #include "crypto_helpers.h" +#include "crypto.h" #include "keys.h" /***** Bip32 path *****/ @@ -79,6 +80,14 @@ cx_err_t generate_public_key(cx_ecfp_public_key_t *public_key, CX_SHA512)); break; } + case DERIVATION_TYPE_BLS12_381: { + public_key->curve = CX_CURVE_BLS12_381_G1; + public_key->W_len = 97; + CX_CHECK(bip32_derive_get_pubkey_bls(bip32_path->components, + bip32_path->length, + ((cx_ecfp_384_public_key_t *) public_key)->W)); + break; + } default: return CX_INVALID_PARAMETER; } @@ -132,6 +141,12 @@ static cx_err_t public_key_hash(uint8_t *const hash_out, compressed->W[0] = 0x02 + (public_key->W[64] & 0x01); break; } + case DERIVATION_TYPE_BLS12_381: { + compressed->curve = public_key->curve; + compressed->W_len = 48; + memcpy(compressed->W, public_key->W + 1, compressed->W_len); + break; + } default: return CX_INVALID_PARAMETER; } @@ -232,6 +247,16 @@ cx_err_t sign(uint8_t *const out, out[0] |= 0x01; } } break; +#ifndef TARGET_NANOS + case DERIVATION_TYPE_BLS12_381: { + CX_CHECK(bip32_derive_with_seed_bls_sign_hash(bip32_path->components, + bip32_path->length, + (uint8_t const *) PIC(in), + in_size, + out, + out_size)); + } break; +#endif default: error = CX_INVALID_PARAMETER; } diff --git a/src/keys.h b/src/keys.h index 2f069acb..f4f3eb4b 100644 --- a/src/keys.h +++ b/src/keys.h @@ -45,14 +45,16 @@ typedef enum { DERIVATION_TYPE_SECP256K1 = 0x01, DERIVATION_TYPE_SECP256R1 = 0x02, DERIVATION_TYPE_BIP32_ED25519 = 0x03, - DERIVATION_TYPE_UNSET = 0x04 + DERIVATION_TYPE_BLS12_381 = 0x04, + DERIVATION_TYPE_UNSET = 0x05 } derivation_type_t; typedef enum { SIGNATURE_TYPE_ED25519 = 0, SIGNATURE_TYPE_SECP256K1 = 1, SIGNATURE_TYPE_SECP256R1 = 2, - SIGNATURE_TYPE_UNSET = 3 + SIGNATURE_TYPE_BLS12_381 = 3, + SIGNATURE_TYPE_UNSET = 4 } signature_type_t; #define DERIVATION_TYPE_IS_SET(type) \ @@ -77,6 +79,8 @@ static inline signature_type_t derivation_type_to_signature_type( case DERIVATION_TYPE_ED25519: case DERIVATION_TYPE_BIP32_ED25519: return SIGNATURE_TYPE_ED25519; + case DERIVATION_TYPE_BLS12_381: + return SIGNATURE_TYPE_BLS12_381; default: return SIGNATURE_TYPE_UNSET; } @@ -188,6 +192,7 @@ static inline bool bip32_path_with_curve_eq(bip32_path_with_curve_t volatile con */ typedef union { cx_ecfp_256_public_key_t pk_256; ///< edpk, sppk and p2pk keys + cx_ecfp_384_public_key_t pk_384; ///< BLpk keys } tz_ecfp_public_key_t; /** @@ -212,12 +217,19 @@ typedef struct { size_t W_len; ///< Compressed public key length in bytes uint8_t W[33]; ///< Compressed public key value } tz_ecfp_secp256_compressed_public_key_t; +/** BLS compressed public key */ +typedef struct { + cx_curve_t curve; ///< Curve identifier + size_t W_len; ///< Compressed public key length in bytes + uint8_t W[48]; ///< Compressed public key value +} tz_ecfp_bls_compressed_public_key_t; /** * @brief This structure represents elliptic curve compressed public key handled */ typedef union { tz_ecfp_ed25519_compressed_public_key_t pk_ed25519; ///< edpk keys tz_ecfp_secp256_compressed_public_key_t pk_secp256; ///< sppk and p2pk keys + tz_ecfp_bls_compressed_public_key_t pk_bls; ///< BLpk keys } tz_ecfp_compressed_public_key_t; /** diff --git a/src/operations.h b/src/operations.h index a13bf888..1306d740 100644 --- a/src/operations.h +++ b/src/operations.h @@ -64,6 +64,7 @@ union public_key { uint8_t edpk[32]; ///< raw public key for a edpk key uint8_t sppk[33]; ///< raw public key for a sppk key uint8_t p2pk[33]; ///< raw public key for a p2pk key + uint8_t blpk[48]; ///< raw public key for a BLpk key } __attribute__((packed)); /** diff --git a/src/to_string.c b/src/to_string.c index c636a727..312a632e 100644 --- a/src/to_string.c +++ b/src/to_string.c @@ -121,6 +121,11 @@ static int pkh_to_string(char *const dest, data.prefix[1] = 161u; data.prefix[2] = 164u; break; + case SIGNATURE_TYPE_BLS12_381: + data.prefix[0] = 6u; + data.prefix[1] = 161u; + data.prefix[2] = 166u; + break; default: return -1; }