Skip to content

Commit 095a7ce

Browse files
params_test: fix TestPSSParams skip and TestOAEPParams failure with latest SoftHSMv3
needMech now probes via GetMechanismList instead of GetMechanismInfo; newer SoftHSMv3 returns CKR_MECHANISM_INVALID from C_GetMechanismInfo for mechanisms that are present in C_GetMechanismList. TestPSSParams switches from CKM_RSA_PKCS_PSS (raw pre-hashed digest) to CKM_SHA256_RSA_PKCS_PSS (hash-then-sign); the raw PSS mechanism is no longer advertised by the latest SoftHSMv3. TestOAEPParams was failing with CKR_KEY_FUNCTION_NOT_PERMITTED at EncryptInit because getRSA delegated to generateRSAKeyPair which only sets CKA_VERIFY/CKA_SIGN. getRSA now generates the key pair inline with CKA_ENCRYPT and CKA_DECRYPT set. Also fixes a typo in getRSA where the private key lookup used CKO_PUBLIC_KEY. Tested with pqctoday-org/pqctoday-hsm@37bf66a Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com>
1 parent 2d9ce3d commit 095a7ce

1 file changed

Lines changed: 39 additions & 13 deletions

File tree

params_test.go

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ const notFound = 0xffffffff
1515

1616
// test whether mech is available; skip the test if it isn't
1717
func needMech(t *testing.T, p *Ctx, sh SessionHandle, mech uint) {
18+
t.Helper()
1819
slots, err := p.GetSlotList(true)
1920
if err != nil {
2021
t.Fatal("GetSlotList:", err)
2122
}
22-
_, err = p.GetMechanismInfo(slots[0], []*Mechanism{NewMechanism(mech, nil)})
23-
if err == nil {
24-
return
23+
mechs, err := p.GetMechanismList(slots[0])
24+
if err != nil {
25+
t.Fatal("GetMechanismList:", err)
2526
}
26-
e, ok := err.(Error)
27-
if !ok || e != CKR_MECHANISM_INVALID {
28-
t.Fatal("GetMechanismInfo:", err)
27+
for _, m := range mechs {
28+
if m.Mechanism == mech {
29+
return
30+
}
2931
}
3032
t.Skipf("skipping test; mech 0x%X not supported by softhsm", mech)
3133
}
@@ -54,9 +56,33 @@ func findObject(t *testing.T, p *Ctx, sh SessionHandle, class uint, label string
5456
// generate a rsa key if it doesn't exist
5557
func getRSA(t *testing.T, p *Ctx, sh SessionHandle) (pub, priv ObjectHandle) {
5658
pub = findObject(t, p, sh, CKO_PUBLIC_KEY, "paramstest")
57-
priv = findObject(t, p, sh, CKO_PUBLIC_KEY, "paramstest")
59+
priv = findObject(t, p, sh, CKO_PRIVATE_KEY, "paramstest")
5860
if pub == notFound || priv == notFound {
59-
pub, priv = generateRSAKeyPair(t, p, sh, "paramstest", false)
61+
pubTmpl := []*Attribute{
62+
NewAttribute(CKA_CLASS, CKO_PUBLIC_KEY),
63+
NewAttribute(CKA_KEY_TYPE, CKK_RSA),
64+
NewAttribute(CKA_TOKEN, false),
65+
NewAttribute(CKA_VERIFY, true),
66+
NewAttribute(CKA_ENCRYPT, true),
67+
NewAttribute(CKA_PUBLIC_EXPONENT, []byte{1, 0, 1}),
68+
NewAttribute(CKA_MODULUS_BITS, 2048),
69+
NewAttribute(CKA_LABEL, "paramstest"),
70+
}
71+
privTmpl := []*Attribute{
72+
NewAttribute(CKA_TOKEN, false),
73+
NewAttribute(CKA_SIGN, true),
74+
NewAttribute(CKA_DECRYPT, true),
75+
NewAttribute(CKA_LABEL, "paramstest"),
76+
NewAttribute(CKA_SENSITIVE, true),
77+
NewAttribute(CKA_EXTRACTABLE, true),
78+
}
79+
var err error
80+
pub, priv, err = p.GenerateKeyPair(sh,
81+
[]*Mechanism{NewMechanism(CKM_RSA_PKCS_KEY_PAIR_GEN, nil)},
82+
pubTmpl, privTmpl)
83+
if err != nil {
84+
t.Fatalf("GenerateKeyPair: %v", err)
85+
}
6086
}
6187
return
6288
}
@@ -65,23 +91,23 @@ func TestPSSParams(t *testing.T) {
6591
p := setenv(t)
6692
sh := getSession(p, t)
6793
defer finishSession(p, sh)
68-
needMech(t, p, sh, CKM_RSA_PKCS_PSS)
94+
needMech(t, p, sh, CKM_SHA256_RSA_PKCS_PSS)
6995
pub, priv := getRSA(t, p, sh)
7096

71-
sum := []byte("1234567890abcdef1234567890abcdef")
97+
msg := []byte("1234567890abcdef1234567890abcdef")
7298
params := NewPSSParams(CKM_SHA256, CKG_MGF1_SHA256, 32)
73-
mech := []*Mechanism{NewMechanism(CKM_RSA_PKCS_PSS, params)}
99+
mech := []*Mechanism{NewMechanism(CKM_SHA256_RSA_PKCS_PSS, params)}
74100
if err := p.SignInit(sh, mech, priv); err != nil {
75101
t.Fatal("SignInit:", err)
76102
}
77-
sig, err := p.Sign(sh, sum)
103+
sig, err := p.Sign(sh, msg)
78104
if err != nil {
79105
t.Fatal("Sign:", err)
80106
}
81107
if err := p.VerifyInit(sh, mech, pub); err != nil {
82108
t.Fatal("VerifyInit:")
83109
}
84-
if err := p.Verify(sh, sum, sig); err != nil {
110+
if err := p.Verify(sh, msg, sig); err != nil {
85111
t.Fatal("Verify:")
86112
}
87113
}

0 commit comments

Comments
 (0)