Skip to content

Commit 7821a35

Browse files
committed
feat: add SHA3 hash support to RSA APIs
1 parent 5b9eb55 commit 7821a35

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

rsa.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ func hashToPKCS11(hashFunction crypto.Hash) (uint, uint, uint, error) {
244244
return pkcs11.CKM_SHA384, pkcs11.CKG_MGF1_SHA384, 48, nil
245245
case crypto.SHA512:
246246
return pkcs11.CKM_SHA512, pkcs11.CKG_MGF1_SHA512, 64, nil
247+
case crypto.SHA3_224:
248+
return pkcs11.CKM_SHA3_224, pkcs11.CKG_MGF1_SHA3_224, 28, nil
249+
case crypto.SHA3_256:
250+
return pkcs11.CKM_SHA3_256, pkcs11.CKG_MGF1_SHA3_256, 32, nil
251+
case crypto.SHA3_384:
252+
return pkcs11.CKM_SHA3_384, pkcs11.CKG_MGF1_SHA3_384, 48, nil
253+
case crypto.SHA3_512:
254+
return pkcs11.CKM_SHA3_512, pkcs11.CKG_MGF1_SHA3_512, 64, nil
247255
default:
248256
return 0, 0, 0, errUnsupportedRSAOptions
249257
}
@@ -279,11 +287,15 @@ func signPSS(session *pkcs11Session, key *pkcs11PrivateKeyRSA, digest []byte, op
279287
}
280288

281289
var pkcs1Prefix = map[crypto.Hash][]byte{
282-
crypto.SHA1: {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14},
283-
crypto.SHA224: {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
284-
crypto.SHA256: {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
285-
crypto.SHA384: {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
286-
crypto.SHA512: {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
290+
crypto.SHA1: {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14},
291+
crypto.SHA224: {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
292+
crypto.SHA256: {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
293+
crypto.SHA384: {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
294+
crypto.SHA512: {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
295+
crypto.SHA3_224: []byte{0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x07, 0x05, 0x00, 0x04, 0x1c},
296+
crypto.SHA3_256: []byte{0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x08, 0x05, 0x00, 0x04, 0x20},
297+
crypto.SHA3_384: []byte{0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x09, 0x05, 0x00, 0x04, 0x30},
298+
crypto.SHA3_512: []byte{0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0a, 0x05, 0x00, 0x04, 0x40},
287299
}
288300

289301
func signPKCS1v15(session *pkcs11Session, key *pkcs11PrivateKeyRSA, digest []byte, hash crypto.Hash) (signature []byte, err error) {

rsa_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ func testRsaSigning(t *testing.T, key crypto.Signer, nbits int, native bool) {
113113
t.Run("SHA256", func(t *testing.T) { testRsaSigningPKCS1v15(t, key, crypto.SHA256) })
114114
t.Run("SHA384", func(t *testing.T) { testRsaSigningPKCS1v15(t, key, crypto.SHA384) })
115115
t.Run("SHA512", func(t *testing.T) { testRsaSigningPKCS1v15(t, key, crypto.SHA512) })
116+
t.Run("SHA3-224", func(t *testing.T) { testRsaSigningPKCS1v15(t, key, crypto.SHA3_224) })
117+
t.Run("SHA3-256", func(t *testing.T) { testRsaSigningPKCS1v15(t, key, crypto.SHA3_256) })
118+
t.Run("SHA3-384", func(t *testing.T) { testRsaSigningPKCS1v15(t, key, crypto.SHA3_384) })
119+
t.Run("SHA3-512", func(t *testing.T) { testRsaSigningPKCS1v15(t, key, crypto.SHA3_512) })
116120
t.Run("PSSSHA1", func(t *testing.T) { testRsaSigningPSS(t, key, crypto.SHA1, native) })
117121
t.Run("PSSSHA224", func(t *testing.T) { testRsaSigningPSS(t, key, crypto.SHA224, native) })
118122
t.Run("PSSSHA256", func(t *testing.T) { testRsaSigningPSS(t, key, crypto.SHA256, native) })
@@ -124,6 +128,16 @@ func testRsaSigning(t *testing.T, key crypto.Signer, nbits int, native bool) {
124128
t.Skipf("key too smol for SHA512 with sLen=hLen")
125129
}
126130
})
131+
t.Run("PSSSHA3-224", func(t *testing.T) { testRsaSigningPSS(t, key, crypto.SHA3_224, native) })
132+
t.Run("PSSSHA3-256", func(t *testing.T) { testRsaSigningPSS(t, key, crypto.SHA3_256, native) })
133+
t.Run("PSSSHA3-384", func(t *testing.T) { testRsaSigningPSS(t, key, crypto.SHA3_384, native) })
134+
t.Run("PSSSHA3-512", func(t *testing.T) {
135+
if nbits > 1024 {
136+
testRsaSigningPSS(t, key, crypto.SHA3_512, native)
137+
} else {
138+
t.Skipf("key too smol for SHA512 with sLen=hLen")
139+
}
140+
})
127141
}
128142

129143
func testRsaSigningPKCS1v15(t *testing.T, key crypto.Signer, hashFunction crypto.Hash) {
@@ -192,6 +206,16 @@ func testRsaEncryption(t *testing.T, key crypto.Decrypter, nbits int, native boo
192206
t.Skipf("key too small for SHA512")
193207
}
194208
})
209+
t.Run("OAEPSHA3-224", func(t *testing.T) { testRsaEncryptionOAEP(t, key, crypto.SHA3_224, []byte{}, native) })
210+
t.Run("OAEPSHA3-256", func(t *testing.T) { testRsaEncryptionOAEP(t, key, crypto.SHA3_256, []byte{}, native) })
211+
t.Run("OAEPSHA3-384", func(t *testing.T) { testRsaEncryptionOAEP(t, key, crypto.SHA3_384, []byte{}, native) })
212+
t.Run("OAEPSHA3-512", func(t *testing.T) {
213+
if nbits > 1024 {
214+
testRsaEncryptionOAEP(t, key, crypto.SHA3_512, []byte{}, native)
215+
} else {
216+
t.Skipf("key too small for SHA512")
217+
}
218+
})
195219
}
196220

197221
func testRsaEncryptionPKCS1v15(t *testing.T, key crypto.Decrypter) {

0 commit comments

Comments
 (0)