diff --git a/aes/aes.go b/aes/aes.go index 0f8a537..7f6f689 100644 --- a/aes/aes.go +++ b/aes/aes.go @@ -179,7 +179,6 @@ func DecryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs } // EncryptGCM uses gcm mode to encrypt bs. -// NOTICE: This is an experimental function, and we haven't tested it enough yet, so be careful when using it. func EncryptGCM(key cryptox.Bytes, nonce cryptox.Bytes, additional cryptox.Bytes, bs cryptox.Bytes) (cryptox.Bytes, error) { block, _, err := newBlock(key) if err != nil { @@ -199,7 +198,6 @@ func EncryptGCM(key cryptox.Bytes, nonce cryptox.Bytes, additional cryptox.Bytes } // DecryptGCM uses gcm mode to decrypt bs. -// NOTICE: This is an experimental function, and we haven't tested it enough yet, so be careful when using it. func DecryptGCM(key cryptox.Bytes, nonce cryptox.Bytes, additional cryptox.Bytes, bs cryptox.Bytes) (cryptox.Bytes, error) { block, _, err := newBlock(key) if err != nil { diff --git a/bytes_test.go b/bytes_test.go index 43aaed7..f3a6eb3 100644 --- a/bytes_test.go +++ b/bytes_test.go @@ -113,8 +113,8 @@ func TestParseHex(t *testing.T) { } } -// go test -v -cover -count=1 -test.cpu=1 -run=^TestFromBase64$ -func TestFromBase64(t *testing.T) { +// go test -v -cover -count=1 -test.cpu=1 -run=^TestParseBase64$ +func TestParseBase64(t *testing.T) { cases := map[string]string{ "": "", "MTIz": "123", diff --git a/rsa/private_test.go b/rsa/private_test.go index 52402f4..af06e97 100644 --- a/rsa/private_test.go +++ b/rsa/private_test.go @@ -93,6 +93,89 @@ func TestPrivateKey(t *testing.T) { } } +// go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeyDecryptPKCS1v15$ +func TestPrivateKeyDecryptPKCS1v15(t *testing.T) { + publicKey := newTestPublicKey(t) + privateKey := newTestPrivateKey(t) + + cases := []string{ + "", "123", "你好,世界", + } + + for _, msg := range cases { + encrypted, err := publicKey.EncryptPKCS1v15(cryptox.Bytes(msg)) + if err != nil { + t.Fatal(err) + } + + decrypted, err := privateKey.DecryptPKCS1v15(encrypted) + if err != nil { + t.Fatal(err) + } + + if string(decrypted) != msg { + t.Fatalf("decrypted %s != msg %s", decrypted, msg) + } + } +} + +// go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeyDecryptPKCS1v15SessionKey$ +func TestPrivateKeyDecryptPKCS1v15SessionKey(t *testing.T) { + publicKey := newTestPublicKey(t) + privateKey := newTestPrivateKey(t) + + cases := []string{ + "", "123", "你好,世界", + } + + for _, msg := range cases { + encrypted, err := publicKey.EncryptPKCS1v15(cryptox.Bytes(msg)) + if err != nil { + t.Fatal(err) + } + + sessionKey := cryptox.GenerateBytes(32) + if err = privateKey.DecryptPKCS1v15SessionKey(encrypted, sessionKey); err != nil { + t.Fatal(err) + } + + decrypted, err := privateKey.DecryptPKCS1v15(encrypted) + if err != nil { + t.Fatal(err) + } + + if string(decrypted) != msg { + t.Fatalf("decrypted %s != msg %s", decrypted, msg) + } + } +} + +// go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeyDecryptOAEP$ +func TestPrivateKeyDecryptOAEP(t *testing.T) { + publicKey := newTestPublicKey(t) + privateKey := newTestPrivateKey(t) + + cases := []string{ + "", "123", "你好,世界", + } + + for _, msg := range cases { + encrypted, err := publicKey.EncryptOAEP(cryptox.Bytes(msg), cryptox.Bytes(msg)) + if err != nil { + t.Fatal(err) + } + + decrypted, err := privateKey.DecryptOAEP(encrypted, cryptox.Bytes(msg)) + if err != nil { + t.Fatal(err) + } + + if string(decrypted) != msg { + t.Fatalf("decrypted %s != msg %s", decrypted, msg) + } + } +} + // go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeySignPKCS1v15$ func TestPrivateKeySignPKCS1v15(t *testing.T) { publicKey := newTestPublicKey(t) diff --git a/rsa/public_test.go b/rsa/public_test.go index 0bd0e5e..85b943b 100644 --- a/rsa/public_test.go +++ b/rsa/public_test.go @@ -128,3 +128,47 @@ func TestPublicKeyEncryptOAEP(t *testing.T) { } } } + +// go test -v -cover -count=1 -test.cpu=1 -run=^TestPublicKeyVerifyPKCS1v15$ +func TestPublicKeyVerifyPKCS1v15(t *testing.T) { + publicKey := newTestPublicKey(t) + privateKey := newTestPrivateKey(t) + + cases := []string{ + "d41d8cd98f00b204e9800998ecf8427e", "202cb962ac59075b964b07152d234b70", "dbefd3ada018615b35588a01e216ae6e", + } + + for _, msg := range cases { + signature, err := privateKey.SignPKCS1v15(cryptox.Bytes(msg)) + if err != nil { + t.Fatal(err) + } + + err = publicKey.VerifyPKCS1v15(cryptox.Bytes(msg), signature) + if err != nil { + t.Fatal(err) + } + } +} + +// go test -v -cover -count=1 -test.cpu=1 -run=^TestPublicKeyVerifyPSS$ +func TestPublicKeyVerifyPSS(t *testing.T) { + publicKey := newTestPublicKey(t) + privateKey := newTestPrivateKey(t) + + cases := []string{ + "d41d8cd98f00b204e9800998ecf8427e", "202cb962ac59075b964b07152d234b70", "dbefd3ada018615b35588a01e216ae6e", + } + + for _, msg := range cases { + signature, err := privateKey.SignPSS(cryptox.Bytes(msg), 0) + if err != nil { + t.Fatal(err) + } + + err = publicKey.VerifyPSS(cryptox.Bytes(msg), signature, 0) + if err != nil { + t.Fatal(err) + } + } +}