3

I bought a NitroKey HSM and manged to create an EC on it.

Now I want to derive a shared secret.
I found Pkcs11Interop, which seems to be the right tool for the job, but the handling is unclear.

A code snipplet or link to a working project would be very appreciated.

jariq
  • 11,681
  • 3
  • 33
  • 52
hdev
  • 6,097
  • 1
  • 45
  • 62

1 Answers1

4

You can take a look at Pkcs11Admin application for a real world sample using Pkcs11Interop library.

As far as I remember NitroKey HSM should be used with OpenSC middleware so your unmanaged library implementing PKCS#11 API will be opensc-pkcs11.dll.

I believe these resources might be helpful for you:

  1. Getting started with Pkcs11Interop
  2. Pkcs11Interop code samples which contain also key derivation sample
  3. PKCS#11 specification
jariq
  • 11,681
  • 3
  • 33
  • 52
  • Thx for your contribution to Pkcs11Interop! I learned a lot from the Pkcs11Interop samples and Pkcs11Admin, but stuck at the point where I have an ObjectHandle of my private EC-Key and a byte array of a public EC-Key. I found this implementation https://www.cryptsoft.com/pkcs11doc/v220/group__SEC__11__14__KEY__MANAGEMENT__FUNCTIONS.html but I was not able to adopt this. For example how so I get prime and base for my brainpool EC. – hdev Feb 11 '17 at 19:06
  • @dh_cgn Could I see your code and that byte array with public key? – jariq Feb 11 '17 at 19:31
  • @dh_cgn I am little confused from the sample but in general you first need to choose a suitable mechanism/algorithm for your use case. I guess you have chosen `CKM_DH_PKCS_DERIVE` but I am not sure why did you choose this particular one or whether it is the correct one for your use case. Anyway as a second step in general you need to read about the parameters of selected mechanism in PKCS#11 spec. It's chapter 12.4.10 in this case. If needed read also algorithm specs mentioned in PKCS#11 spec. It's PKCS#3 in this case. – jariq Feb 12 '17 at 14:53
  • As a third step when you understand the algorithm and its parameters, you just need to use the correct constructor of `Mechanism` class with the correct parameters and everything should be fine. BTW did you pick `CKM_DH_PKCS_DERIVE` intentionally? – jariq Feb 12 '17 at 14:53
  • I did choose CKM_DH_PKCS_DERIVE because the docu says: The PKCS #3 Diffie-Hellman key derivation mechanism, denoted CKM_DH_PKCS_DERIVE, is a mechanism for key derivation based on Diffie-Hellman key agreement. – hdev Feb 12 '17 at 16:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135508/discussion-between-dh-cgn-and-jariq). – hdev Feb 12 '17 at 16:04