-
-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BIP32 Mechanism is missing? #116
Comments
I am trying to implement CKM_BIP32_MASTER_DERIVE |
What version of PkCS11 implements this method? |
PKCS#11 Definitions #define CKK_BIP32 (CKK_VENDOR_DEFINED |
These are not standard mechanisms, they come from a vendors proprietary implementation. What device is this? What version of its middleware supports it? What are the values of those constants? You can use proprietary device methods with PKCS11js, I’m not sure about Graphene. We will need to ask Stepan |
@anubnair Do you have PKCS#11 specification of those mechanisms using? |
Yes, if you provide us the vendor make/model and link to the documentation for it we will be able to help more. |
Using 'Luna_PCIe_HSM' from Thales. It is a part of extension to PKCS11 PKCS#11 Definitions |
BIP32 Mechanism Support and Implementation Curve Support Key Type and Form Extended Keys and Hardened Keys Hardened private keys create a firewall through which multi-level key derivation compromises cannot happen. For normal (non-hardened) keys one can derive child public keys of a given parent key without knowing any private key. So if an attacker gets a normal parent chain code and parent public key, he can brute-force all chain codes deriving from it. If the attacker also obtains a child, grandchild, or further-descended private key, he can use the chain code to generate all of the extended private keys descending from that private key. The formula for creating hardened keys makes it impossible to create child public keys without knowing the parent private key. Key Derivation CKM_BIP32_MASTER_DERIVE typedef struct CK_BIP32_MASTER_DERIVE_PARAMS { CKM_BIP32_CHILD_DERIVE The BIP32 and BIP44 specifications recommend wallet structures and use cases. The specifications provide a good reference for deciding how a key tree should be organized and if a particular key should be hardened or not. Follow the specifications to avoid potential security holes. This mechanism can be used to generate keys that are several levels deep in the key hierarchy. The path of the key is specified with pulPath and ulPathLen. The path is an array of integers (key indices). The path is relative to the input key. For example, if the path is [5, 1, 4] and the path of the input key is m/0 then the resulting path is m/0/5/1/4. typedef struct CK_BIP32_CHILD_DERIVE_PARAMS { Error Codes CKR_BIP32_CHILD_INDEX_INVALID: This error is returned on the rare occurrence (1 / 2^127) that a child derivation returns an all-zero private key, a private key bigger than or equal to the curve order parameter n, or a point at infinity. This error signifies that the child key index cannot be used to derive keys. Choose a different index and try the derivation again. The problematic child index is indicated by ulPathErrorIndex. PCKS#11 does not have fixed width integers. This error can also be returned on platforms where CK_ULONG is bigger than 32 bits and a child index is bigger than 2^32 – 1. CKR_BIP32_INVALID_HARDENED_DERIVATION: This error is returned from an attempt to derive a hardened key from a public key. The BIP32 specification does not support such a derivation. CKR_BIP32_MASTER_SEED_LEN_INVALID: The BIP32 specification recommends deriving the master key from a seed that is between 128 and 512 bits long. This error is returned if the seed length is outside of that range. CKR_BIP32_MASTER_SEED_INVALID: This error is returned on the rare occurrence (1 / 2^127) that the master derivation returns an all zero private key, a private key bigger than or equal to the curve order parameter n, or a point at infinity. This error signifies that the master seed cannot be used for BIP32. Generate a new master seed and retry the derivation. CKR_BIP32_INVALID_KEY_PATH_LEN: This error is returned when ulPathLen is 0 or greater than 255. The BIP44 standard only requires paths of length 5 so this limit should be acceptable for all customers. Key Attributes CKA_BIP32_CHAIN_CODE: The chain code is essential for BIP32 keys and is used to derive future keys. The public and private key share this value. Read only. CKA_BIP32_VERSION_BYTES: Version bytes are used to further identify BIP32 keys. The version bytes help determine if a key is used on the main bitcoin network or the test network. This attribute defaults to CKG_BIP32_VERSION_MAINNET_PUB/PRIV if it was not specified at key creation time. You can set this value to CKG_BIP32_VERSION_TESTNET_PUB/PRIV if applicable. CKA_BIP32_CHILD_INDEX: The child index stores which index was used to derive this key. An index with the CKF_BIP32_HARDENED bit set is considered a hardened child. The child index is 0 for the master key. The public and private key share this value. Read only. CKA_BIP32_CHILD_DEPTH: The depth of the child key in the key tree. The master key has a depth of 0. The public and private key share this value. Read only. CKA_BIP32_ID: The unique identifier for the key. This value is derived from the HASH160 of the compressed public key. The first 32 bits of this value is known as the fingerprint. (CKA_ID is not used for this purpose because it is writable by the user.) The public and private key share this value. Read only. NOTE No attribute is included for the parent ID because it should not be required. The anticipated use-case is to derive a key, use it and then delete it. In general, there should not be a need to discover how keys are organized based on the fingerprints or IDs. The parent fingerprint is available in case there is need to rediscover a key tree, but the wallet software must deal with any collisions. The BIP32 designers considered the parent ID not sufficiently important to include in serialized keys; therefore we exclude it as well. CKA_BIP32_FINGERPRINT and CKA_BIP32_PARENT_FINGERPRINT: The fingerprints for the key and parent key are the first 32 bits of the BIP32 key identifier. These can be used to identify keys but the wallet software must handle any collisions. For identifying keys, it is better to use CKA_BIP32_ID because it is long enough that collisions should not be an issue. The public and private key share this value. The master key has a parent fingerprint of 0. Read only. Public Key Import/Export CK_RV CA_Bip32ImportPubKey( NOTE When importing a serialized extended public key, implementations must verify whether the X coordinate in the public key data corresponds to a point on the curve. If not, the extended public key is invalid. Exporting is done with CA_Bip32ExportPubKey(). The specified object is extracted from the HSM and encoded in the BIP32 format. The result is a NULL-terminated string and is placed in the pKey parameter. The length of pKey has a maximum of 112 characters. This constant is defined as CKG_BIP32_MAX_SERIALIZED_LEN. It’s possible that not all characters are needed to serialize the key. Any unused characters are set to 0. See Code Samples for code examples. BIP32 Serialization Format
This 78 byte structure is encoded like other Bitcoin data in Base58, by first adding 32 checksum bits (derived from the double SHA-256 checksum), and then converting to the Base58 representation. This results in a Base58-encoded string of up to CKG_BIP32_MAX_SERIALIZED_LEN characters. Because of the choice of the version bytes, the Base58 representation will start with "xprv" or "xpub" on mainnet, "tprv" or "tpub" on testnet. Private Key Import/Export See Code Samples for code examples. Key Backup and Cloning Non-FIPS Algorithm Host Tools Code Samples CK_ATTRIBUTE pubTemplate[] = CK_BIP32_MASTER_DERIVE_PARAMS mechParams; CK_RV rv = C_DeriveKey(hSession, &mechanism, hSeedKey, NULL, 0, NULL); CK_OBJECT_HANDLE pubKey = mechanism.hPublicKey; Deriving a child leaf key CK_ATTRIBUTE pubTemplate[] = CK_ULONG path[] = { CK_BIP32_MASTER_DERIVE_PARAMS mechParams; CK_RV rv = C_DeriveKey(hSession, &mechanism, hMasterPrivKey, NULL, 0, NULL); CK_OBJECT_HANDLE pubKey = mechanism.hPublicKey; Importing a public extended key CK_CHAR_PTR encodedKey = “xpub661MyMwAqRbcFtXgS5…”; //BIP32 serialization format CK_RV rv = CA_Bip32ImportKey(hSession, template, ARRAY_SIZE(template), encodedKey, &pubKey); Exporting a public extended key CK_RV rv = CA_Bip32ExportPubKey(hSession, hObject, encodedKey, &ulEncodedKeySize ); Importing a private extended key CK_CHAR_PTR encodedKey = “xprv9s21ZrQH143K3QTDL4LXw2F…”; CK_RV rv = C_EncryptInit(hSession, &mechanism, hWrappingKey); rv = C_Encrypt(hSession, encodedKey, sizeof(encodedKey), wrappedKey, &wrappedKeyLen); rv = C_UnwrapKey(hSession, &mechanism, hWrappingKey, wrappedKey, wrappedKeyLen, template, ARRAY_SIZE(template), &hUnwrappedKey); Exporting a private extended key CK_RV rv = C_WrapKey(hSession, &mechanism, hWrappingKey, hKeyToWrap, key, &keyLen); rv = C_DecryptInit(hSession, &mechanism, hWrappingKey); rv = C_Decrypt(hSession, key, keyLen, key, &keyLen); key[keyLen] = 0 // The key isn’t NULL terminated after C_Decrypt(). The serialized key is stored in key if there were no errors. PKCS#11 Definitions |
The version of firmware I have for my Luna does not support this and I do not have a current support contract so I can’t get new firmware. PRs are welcome, and if you can provide access to a SafeNet time permitting we would consider adding it but things are busy and can’t commit to when we would be able to do it. |
the nodejs implementation is available please? any link |
No description provided.
The text was updated successfully, but these errors were encountered: