N-of-m authentication failling with Nitrokey HSM2 and SmartCardShell

Hi, when following this guide0 I am hitting the very issue described in this thread on CardContact’s support forum1. So I tried disabling smartcard plug and play and answering “No” to “Automatically access card with Key Manager”, it’s still failing with the same error:

>GPError: Card (CARD_INVALID_SW/25344) - "Unexpected SW1/SW2=6300 (Warning processing: State of non-volatile memory changed) received" in /home/smartcard/scsh-withJRE-3.18.39/scsh/sc-hsm/ManagePKA.js#142                                         at /home/smartcard/scsh-withJRE-3.18.39/scsh/sc-hsm/ManagePKA.js#142                                                    at /home/smartcard/scsh-withJRE-3.18.39/keymanager/keymanager.js#1785                                                   at /home/smartcard/scsh-withJRE-3.18.39/keymanager/keymanager.js#3022

I am using Nitrokey HSM2 from a desktop computer. My SmartCard Shell instance runs in a Debian 12 virtual machine, and I connect HSM to the remote host through VMware Remote Console. So I tried running SmartCard Shell right on the desktop computer, and got the exact same error.

Is there something I am missing? How can I get n-of-m authentication working?

1 Like

63 00 shall mean “Signature verification failed”. Which firmware are you on?

The signature will obviously fail, if the private key used for signing does not match the public key imported during n-of-m setup. Are you sure that public and private key match ?

Can you post the last 10 APDUs from the trace tab in the Smart Card Shell up until the error happens ?

Usually it should look like

49 C: 00 84 00 00 - GET CHALLENGE      Le=8 
   R: SW1/SW2=9000 (Normal processing: No error) Lr=8
      0000  1B 32 00 D2 F4 79 11 CF                          .2...y..
C4 C: 80 68 01 73 - SIGN Lc=24 Extended
      0007  44 45 43 43 31 32 30 30 32 39 31 30 30 30 30 30  DECC120029100000
      0017  1B 32 00 D2 F4 79 11 CF                          .2...y..
      Le=0 Extended
   R: SW1/SW2=9000 (Normal processing: No error) Lr=71
      0000  30 45 02 20 41 AA 7B 0B 6E 6E 0B A7 3F 3A 1F 01  0E. A.{.nn..?:..
      0010  01 43 A3 F4 3B 63 8D B9 30 2D 9D 80 21 C5 32 37  .C..;c..0-..!.27
      0020  7C D7 08 10 02 21 00 91 82 EC 57 38 A3 BD 4A 46  |....!....W8..JF
      0030  5A BD 4B 0D 20 83 A2 3D C9 93 64 82 E8 81 BB 19  Z.K. ..=..d.....
      0040  46 3D F6 35 8D 6C F8                             F=.5.l.
49 C: 00 82 00 00 - EXTERNAL AUTHENTICATE Lc=64 
      0005  41 AA 7B 0B 6E 6E 0B A7 3F 3A 1F 01 01 43 A3 F4  A.{.nn..?:...C..
      0015  3B 63 8D B9 30 2D 9D 80 21 C5 32 37 7C D7 08 10  ;c..0-..!.27|...
      0025  91 82 EC 57 38 A3 BD 4A 46 5A BD 4B 0D 20 83 A2  ...W8..JFZ.K. ..
      0035  3D C9 93 64 82 E8 81 BB 19 46 3D F6 35 8D 6C F8  =..d.....F=.5.l.
   R: SW1/SW2=9000 (Normal processing: No error) Lr=0

Firmware version is 4.0.

I am sure the key match, as 3 keys were registered and I tried to authenticate with 2 of them.

Please find the trace below:

4D C: 00 20 00 81 - VERIFY
   R: SW1/SW2=63C3 (Warning processing: Counter at 3) Lr=0
4D C: 00 A4 04 00 - SELECT Lc=11
      0005  E8 2B 06 01 04 01 81 C3 1F 02 01                 .+.........
   R: SW1/SW2=9000 (Normal processing: No error) Lr=12
      0000  6F 0A 82 01 78 85 05 00 01 05 03 04              o...x.......
4D C: 00 20 00 81 - VERIFY
   R: SW1/SW2=63C3 (Warning processing: Counter at 3) Lr=0
4D C: 00 20 00 81 - VERIFY
   R: SW1/SW2=63C3 (Warning processing: Counter at 3) Lr=0
4D C: 00 20 00 85 - VERIFY
   R: SW1/SW2=6A88 (Checking error: Reference data not found) Lr=0
4D C: 00 20 00 81 - VERIFY
   R: SW1/SW2=63C3 (Warning processing: Counter at 3) Lr=0
4D C: 00 20 00 81 - VERIFY Lc=6
      *** Sensitive Information Removed ***
   R: SW1/SW2=9000 (Normal processing: No error) Lr=0
4D C: 00 20 00 81 - VERIFY      Le=0
   R: SW1/SW2=9000 (Normal processing: No error) Lr=0
EE C: 00 84 00 00 - GET CHALLENGE      Le=8
   R: SW1/SW2=9000 (Normal processing: No error) Lr=8
      0000  F0 21 21 8C 4E 1F BA 05                          .!!.N...
4D C: 80 68 01 73 - SIGN Lc=24 Extended
      0007  44 45 4E 4B 30 33 30 31 33 36 34 30 30 30 30 30  DENK030136400000
      0017  F0 21 21 8C 4E 1F BA 05                          .!!.N...
      Le=0 Extended
   R: SW1/SW2=9000 (Normal processing: No error) Lr=102
      0000  30 64 02 30 18 47 BB BE E0 83 45 F1 D9 00 4D F2  0d.0.G....E...M.
      0010  24 D8 36 7B FC 50 A1 5E DB 38 DB B5 24 20 C2 58  $.6{.P.^.8..$ .X
      0020  85 04 8F E0 C1 BD 9A 89 90 D5 96 9B 0E E9 B2 BB  ................
      0030  F4 50 B8 C4 02 30 7D 64 E3 EB C0 B0 C4 2B E6 0E  .P...0}d.....+..
      0040  61 09 88 02 11 13 94 4B 6C 6F 1B 23 8E E9 71 72  a......Klo.#..qr
      0050  E6 97 2E 00 00 16 87 94 B9 54 75 01 2E D5 83 9D  .........Tu.....
      0060  5A 46 0A FE AB 4E                                ZF...N
EE C: 00 82 00 00 - EXTERNAL AUTHENTICATE Lc=64
      0005  FC 50 A1 5E DB 38 DB B5 24 20 C2 58 85 04 8F E0  .P.^.8..$ .X....
      0015  C1 BD 9A 89 90 D5 96 9B 0E E9 B2 BB F4 50 B8 C4  .............P..
      0025  94 4B 6C 6F 1B 23 8E E9 71 72 E6 97 2E 00 00 16  .Klo.#..qr......
      0035  87 94 B9 54 75 01 2E D5 83 9D 5A 46 0A FE AB 4E  ...Tu.....ZF...N
   R: SW1/SW2=6300 (Warning processing: State of non-volatile memory changed) Lr=0

Which domain parameter did you select for the authentication key ?

The signature passed into the EXTERNAL AUTHENTICATE (256 bit) is smaller than the signature returned in SIGN (384).

Try an authentication key with brainpoolP256r1.

I shall add that I followed the aforementioned guide only for the CA part. The authentication keys were generated with the following commands:

 sc-hsm-tool --create-dkek-share hsm_equipe_systeme_dkek.pbe
 sc-hsm-tool  --initialize  --dkek-shares 1  --label "hsm_equipe_reseau"
 sc-hsm-tool --import-dkek-share hsm_equipe_reseau_dkek.pbe
 pkcs11-tool --login --keypairgen --key-type "EC:secp384r1" --label "cle_equipe_reseau"
 sc-hsm-tool --export-for-pub-key-auth cle_equipe_reseau_pub.asn1 --key-reference 1

So i tried to reinitialize one hsm using smartcard shell, with “resetting pin with so-pin not allowed”, and brainpool256r1 as a curve (instead of p384r1 as before). Then I exported the publick key, and tried to replace in the CA HSM one of the previous allowed key with the newer one. It failed with “Security condition not satisfied”. The log and trace are in the zip file attached.
card_replace_public_key_2024-09-30.zip (7.1 KB)

Perhaps that helps to elucidate what’s going on?

You need to be authenticated to replace the registered public keys. As you can not authenticate using the 384 bit key, you need to re-initialize the target device and import the brainpoolP256r1 public keys.

I will need to check, if we can support authentication keys that are not brainpoolP256r1 keys. The current code expects a 256 bit signature, which is why the ManagePKA code assumes a fixed signature length.

At least we should prevent users from importing public keys that are not brainpoolP256r1 for PKA.

You can also use a secp256r1 key for authentication. The only limitation is that it must be a 256 bit key.

1 Like

Alright, thank you! Before I re-initialize the target device, are there best practices I should be aware of, regarding key backups in particular?
As a reinit of the CA HSM is required in case there is no possibility to reach the quorum of authentication keys, is it safe to say that each authentication key should be set with a (different) DKEK, and that the target should have its DKEK too?

Public key authentication and key domains are unrelated. The former is one of the authentication mechanisms, while the later allows encrypted key backup. There is no dependency of one on the other.

It’s quite common to have a different regime for managing DKEK shares and PKA. There is no need to assign the same key custodians for n-of-m in PKA and n-of-m when accessing DKEK shares.

Obviously both mechanisms control access to sensitive key material. While PKA controls access to the key on the device, DKEK share control access to keys while they are not on the device, but in transit or backup.

You probably know, that there is a third mechanism, XKEK key domains, where control over key distribution is even more strict, with a group signer that entitles devices to be part of a key domain. While in DKEK key domains the key could be disclosed outside the HSM, if the DKEK share is disclosed, in XKEK key domains that is not possible (unless someone compromises the Scheme PKI).

1 Like

We aimed to split the dkek shares across the same key custodians for simplicity sake, but indeed that may not be the most common approach.

Thanks for your help, I confirm I could successfully authenticate to the HSM. So I thought I was close to have my root CA signed, seems I was wrong. So maybe I’ll ask for help again soon.

1 Like