Skip to content
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

add salt length calculation with PSSSaltLengthAuto #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,12 @@ func signPSS(session *pkcs11Session, key *pkcs11PrivateKeyRSA, digest []byte, op
return nil, err
}
switch opts.SaltLength {
case rsa.PSSSaltLengthAuto: // parseltongue constant
// TODO we could (in principle) work out the biggest
// possible size from the key, but until someone has
// the effort to do that...
return nil, errUnsupportedRSAOptions
case rsa.PSSSaltLengthAuto:
if k, ok := key.pkcs11PrivateKey.pubKey.(*rsa.PublicKey); ok {
sLen = uint(k.N.BitLen()-1+7)/8 - 2 - hLen
} else {
return nil, errUnsupportedRSAOptions
}
case rsa.PSSSaltLengthEqualsHash:
sLen = hLen
default:
Expand Down
25 changes: 15 additions & 10 deletions rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ func testRsaSigningPKCS1v15(t *testing.T, key crypto.Signer, hashFunction crypto
}

func testRsaSigningPSS(t *testing.T, key crypto.Signer, hashFunction crypto.Hash, native bool) {

if !native {
skipIfMechUnsupported(t, key.(*pkcs11PrivateKeyRSA).context, pkcs11.CKM_RSA_PKCS_PSS)
}
Expand All @@ -137,18 +136,24 @@ func testRsaSigningPSS(t *testing.T, key crypto.Signer, hashFunction crypto.Hash
_, err := h.Write(plaintext)
require.NoError(t, err)

plaintextHash := h.Sum([]byte{}) // weird API
pssOptions := &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthEqualsHash,
Hash: hashFunction,
plaintextHash := h.Sum(nil)
rsaPubkey := key.Public().(crypto.PublicKey).(*rsa.PublicKey)
saltLengths := []int{
rsa.PSSSaltLengthAuto,
rsa.PSSSaltLengthEqualsHash,
}
sig, err := key.Sign(rand.Reader, plaintextHash, pssOptions)
require.NoError(t, err)

rsaPubkey := key.Public().(crypto.PublicKey).(*rsa.PublicKey)
for _, sl := range saltLengths {
pssOptions := &rsa.PSSOptions{
SaltLength: sl,
Hash: hashFunction,
}
sig, err := key.Sign(rand.Reader, plaintextHash, pssOptions)
require.NoError(t, err)

err = rsa.VerifyPSS(rsaPubkey, hashFunction, plaintextHash, sig, pssOptions)
require.NoError(t, err)
err = rsa.VerifyPSS(rsaPubkey, hashFunction, plaintextHash, sig, pssOptions)
require.NoError(t, err)
}
}

func testRsaEncryption(t *testing.T, key crypto.Decrypter, native bool) {
Expand Down