decrypt_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package template
  14. import (
  15. "crypto/rand"
  16. "crypto/rsa"
  17. "crypto/x509"
  18. "encoding/pem"
  19. "fmt"
  20. "testing"
  21. "github.com/stretchr/testify/assert"
  22. "github.com/stretchr/testify/require"
  23. )
  24. func generateRSAPrivateKeyPEM(t testing.TB) (string, *rsa.PrivateKey) {
  25. t.Helper()
  26. // Generate a new RSA private key
  27. priv, err := rsa.GenerateKey(rand.Reader, 2048)
  28. require.NoError(t, err, "failed to generate RSA key")
  29. privBytes := x509.MarshalPKCS1PrivateKey(priv)
  30. privPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: privBytes})
  31. return string(privPEM), priv
  32. }
  33. func TestRsaDecrypt_NoneScheme(t *testing.T) {
  34. input := "plaintext"
  35. privateKey := "irrelevant"
  36. out, err := rsaDecrypt("None", "SHA256", input, privateKey)
  37. assert.NoError(t, err)
  38. assert.Equal(t, input, out)
  39. }
  40. func TestRsaDecrypt_InvalidPEM(t *testing.T) {
  41. _, err := rsaDecrypt("RSA-OAEP", "SHA256", "data", "not-a-valid-pem")
  42. assert.Error(t, err)
  43. assert.Equal(t, errDecodePEM, err.Error())
  44. }
  45. func TestRsaDecrypt_InvalidPrivateKey(t *testing.T) {
  46. // Use a valid PEM block but not a private key
  47. pemBlock := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: []byte("invalid")})
  48. _, err := rsaDecrypt("RSA-OAEP", "SHA256", "data", string(pemBlock))
  49. assert.Error(t, err)
  50. assert.ErrorIs(t, err, errParsePK)
  51. }
  52. func TestRsaDecrypt_UnsupportedScheme(t *testing.T) {
  53. privateKey, _ := generateRSAPrivateKeyPEM(t)
  54. _, err := rsaDecrypt("Unsupported", "SHA256", "data", privateKey)
  55. assert.Error(t, err)
  56. assert.Equal(t, fmt.Errorf(errSchemeNotSupported, "Unsupported"), err)
  57. }
  58. func TestRsaDecrypt_RSAOAEP_Success(t *testing.T) {
  59. privateKeyPEM, priv := generateRSAPrivateKeyPEM(t)
  60. plaintext := []byte("secret-data")
  61. ciphertext, err := rsa.EncryptOAEP(getHash("SHA256"), rand.Reader, &priv.PublicKey, plaintext, nil)
  62. assert.NoError(t, err)
  63. out, err := rsaDecrypt("RSA-OAEP", "SHA256", string(ciphertext), privateKeyPEM)
  64. assert.NoError(t, err)
  65. assert.Equal(t, string(plaintext), out)
  66. }
  67. func TestRsaDecrypt_RSAOAEP_DecryptionError(t *testing.T) {
  68. privateKeyPEM, _ := generateRSAPrivateKeyPEM(t)
  69. // Pass random data as ciphertext
  70. _, err := rsaDecrypt("RSA-OAEP", "SHA256", "not-encrypted-data", privateKeyPEM)
  71. assert.Error(t, err)
  72. assert.ErrorIs(t, err, errRSADecrypt)
  73. }