Open fastchain opened 1 day ago
How is the key generated?
Edit: seems an outdated version of OpenSC. Try to use version 0.26
@polhenarejos
How is the key generated?
It was imported following the method described in this comment
Here is key itself, if needed
7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6
Edit: seems an outdated version of OpenSC. Try to use version 0.26
didn't help.
I am using this script and it works:
from picohsm import PicoHSM
from cryptography.hazmat.primitives.asymmetric import ec
from binascii import unhexlify
curve=ec.SECP256K1
secret_key=unhexlify('7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6')
pkey = ec.derive_private_key(
int.from_bytes(secret_key, byteorder='big'),
curve(), # Curve used in Ethereum
)
DEFAULT_DKEK = bytes([0x1] * 32)
print(pkey)
device = PicoHSM()
device.initialize(dkek_shares=1)
device.import_dkek(DEFAULT_DKEK)
key_id = device.import_key(pkey, dkek=DEFAULT_DKEK)
print(key_id)
pubkey = device.public_key(key_id, param=curve().name)
print(pubkey)
Also pkcs11-tool
:
~/Devel/pico/pico-hsm/build_pico_2040 % pkcs11-tool -O
Using slot 2 with a present token (0x8)
Public Key Object; EC EC_POINT 256 bits
EC_POINT: 04410420b871f3ced029e14472ec4ebc3c0448164942b123aa6af91a3386c1c403e0ebd3b4a5752a2b6c49e574619e6aa0549eb9ccd036b9bbc507e1f7f9712a236092
EC_PARAMS: 06052b8104000a (OID 1.3.132.0.10)
label:
ID: 31
Usage: verify, derive
Access: none
uri: pkcs11:model=PKCS%2315%20emulated;manufacturer=Pol%20Henarejos;serial=ESPICOHSMTR;token=Pico-HSM;id=%31;object=;type=public
Profile object 16073104
profile_id: CKP_PUBLIC_CERTIFICATES_TOKEN (4)
~/Devel/pico/pico-hsm/build_pico_2040 % pkcs11-tool --read-object --pin 648219 --id 31 --type pubkey
Using slot 2 with a present token (0x8)
0V0*?H?=+?
B ?q???)?Dr?N?<HIB?#?j?3?????Ӵ?u*+lI?ta?j?T????6??????q*#`?%
@polhenarejos interesting. Was the SecureLock enabled on init and the and unlocked before read?
No, it wasn't. Can you try the snippet? To isolate the problem. Perhaps it's the securelock.
I updated the script with SecureLock2 and still works. Can you try it? It is the same as the other comment.
from picohsm import PicoHSM
from cryptography.hazmat.primitives.asymmetric import ec
from binascii import unhexlify
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
import platform
import sys
class SecureLock2:
def __init__(self, picohsm, secretkey):
self.picohsm = picohsm
self.secretkey = secretkey
def mse(self):
sk = ec.generate_private_key(ec.SECP256R1())
pn = sk.public_key().public_numbers()
self.__pb = sk.public_key().public_bytes(Encoding.X962, PublicFormat.UncompressedPoint)
ret = self.picohsm.send(cla=0x80, command=0x64, p1=0x3A, p2=0x01, data=list(self.__pb))
pk = ec.EllipticCurvePublicKey.from_encoded_point(ec.SECP256R1(), bytes(ret))
shared_key = sk.exchange(ec.ECDH(), pk)
xkdf = HKDF(
algorithm=hashes.SHA256(),
length=12+32,
salt=None,
info=self.__pb
)
kdf_out = xkdf.derive(shared_key)
self.__key_enc = kdf_out[12:]
self.__iv = kdf_out[:12]
def encrypt_chacha(self, data):
chacha = ChaCha20Poly1305(self.__key_enc)
ct = chacha.encrypt(self.__iv, data, self.__pb)
return ct
def unlock_device(self):
ct = self.get_skey()
self.picohsm.send(cla=0x80, command=0x64, p1=0x3A, p2=0x03, data=list(ct))
def _get_key_device(self):
return self.secretkey
def get_skey(self):
self.mse()
ct = self.encrypt_chacha(self._get_key_device())
return ct
def enable_device_aut(self):
ct = self.get_skey()
self.picohsm.send(cla=0x80, command=0x64, p1=0x3A, p2=0x02, data=list(ct))
def disable_device_aut(self):
ct = self.get_skey()
self.picohsm.send(cla=0x80, command=0x64, p1=0x3A, p2=0x04, p3=list(ct))
curve=ec.SECP256K1
secret_key=unhexlify('7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6')
pkey = ec.derive_private_key(
int.from_bytes(secret_key, byteorder='big'),
curve(), # Curve used in Ethereum
)
DEFAULT_DKEK = bytes([0x1] * 32)
print(pkey)
device = PicoHSM()
device.initialize(dkek_shares=1)
device.import_dkek(DEFAULT_DKEK)
key_id = device.import_key(pkey, dkek=DEFAULT_DKEK)
print(key_id)
pubkey = device.public_key(key_id, param=curve().name)
print(pubkey)
slck = SecureLock2(device,secret_key)
slck.enable_device_aut()
slck.unlock_device()
pubkey = device.public_key(key_id, param=curve().name)
print(pubkey)
BTW, note that the log you posted is doing an ECDSA signature (4 attempts), nothing related with getting the pubkey. So what is failing in the log is the signature command.
hw | Using signature algorithm ECDSA
hw | 00000911 APDU: 80 68 01 70 00 00 20 33 F4 34 0C E7 2D CB 30 E5 E9 11 49 F6 22 C5 CA 5A A5 8C E8 FF A3 6A 75 8D 31 2C 72 73 9A CD 71 02 00
hw | 00001648 SW: 64 00
hw | 00000139 APDU: 80 68 01 70 00 00 20 33 F4 34 0C E7 2D CB 30 E5 E9 11 49 F6 22 C5 CA 5A A5 8C E8 FF A3 6A 75 8D 31 2C 72 73 9A CD 71 02 00
hw | 00001047 SW: 64 00
hw | 00000204 APDU: 80 68 01 70 00 00 20 33 F4 34 0C E7 2D CB 30 E5 E9 11 49 F6 22 C5 CA 5A A5 8C E8 FF A3 6A 75 8D 31 2C 72 73 9A CD 71 02 00
hw | 00001099 SW: 64 00
hw | 00000071 APDU: 80 68 01 70 00 00 20 33 F4 34 0C E7 2D CB 30 E5 E9 11 49 F6 22 C5 CA 5A A5 8C E8 FF A3 6A 75 8D 31 2C 72 73 9A CD 71 02 00
hw | 00000819 SW: 64 00
hw | error: PKCS11 function C_SignFinal failed: rv = CKR_GENERAL_ERROR (0x5)
hw | Aborting.
If you want the log generated by pkcs11-tool
, prepend OPENSC_DEBUG=9
:
OPENSC_DEBUG=9 pkcs11-tool --read-object --pin 648219 --id 31 --type pubkey
Version: current state of development branch Board: Pico
Pico-hsm with enabled SecureLock and unlocked.
Output on
/usr/local/bin/pkcs11-tool -O
when I try to read public key with
I get this (log with APDU commands)