github.com/emmansun/gmsm@v0.29.1/pkcs7/envelope_test.go (about)

     1  package pkcs7
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/x509"
     6  	"encoding/pem"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/emmansun/gmsm/pkcs"
    11  	"github.com/emmansun/gmsm/smx509"
    12  )
    13  
    14  func TestEncrypt(t *testing.T) {
    15  	ciphers := []pkcs.Cipher{
    16  		pkcs.DESCBC,
    17  		pkcs.TripleDESCBC,
    18  		pkcs.SM4CBC,
    19  		pkcs.SM4GCM,
    20  		pkcs.AES128CBC,
    21  		pkcs.AES192CBC,
    22  		pkcs.AES256CBC,
    23  		pkcs.AES128GCM,
    24  		pkcs.AES192GCM,
    25  		pkcs.AES256GCM,
    26  	}
    27  	sigalgs := []x509.SignatureAlgorithm{
    28  		x509.SHA1WithRSA,
    29  		x509.SHA256WithRSA,
    30  		x509.SHA512WithRSA,
    31  		smx509.SM2WithSM3,
    32  	}
    33  	for _, cipher := range ciphers {
    34  		for _, sigalg := range sigalgs {
    35  			plaintext := []byte("Hello Secret World!")
    36  			cert, err := createTestCertificate(sigalg)
    37  			if err != nil {
    38  				t.Fatal(err)
    39  			}
    40  			encrypted, err := Encrypt(cipher, plaintext, []*smx509.Certificate{cert.Certificate})
    41  			if err != nil {
    42  				t.Fatal(err)
    43  			}
    44  			p7, err := Parse(encrypted)
    45  			if err != nil {
    46  				t.Fatalf("cannot Parse encrypted result: %s", err)
    47  			}
    48  			result, err := p7.Decrypt(cert.Certificate, *cert.PrivateKey)
    49  			if err != nil {
    50  				t.Fatalf("cannot Decrypt encrypted result: %s", err)
    51  			}
    52  			if !bytes.Equal(plaintext, result) {
    53  				t.Errorf("encrypted data does not match plaintext:\n\tExpected: %s\n\tActual: %s", plaintext, result)
    54  			}
    55  		}
    56  	}
    57  }
    58  
    59  func TestEncryptSM(t *testing.T) {
    60  	ciphers := []pkcs.Cipher{
    61  		pkcs.SM4CBC,
    62  		pkcs.SM4GCM,
    63  	}
    64  	sigalgs := []x509.SignatureAlgorithm{
    65  		smx509.SM2WithSM3,
    66  	}
    67  	for _, cipher := range ciphers {
    68  		for _, sigalg := range sigalgs {
    69  			plaintext := []byte("Hello Secret World!")
    70  			cert, err := createTestCertificate(sigalg)
    71  			if err != nil {
    72  				t.Fatal(err)
    73  			}
    74  			encrypted, err := EncryptSM(cipher, plaintext, []*smx509.Certificate{cert.Certificate})
    75  			if err != nil {
    76  				t.Fatal(err)
    77  			}
    78  			pem.Encode(os.Stdout, &pem.Block{Type: "PKCS7", Bytes: encrypted})
    79  			p7, err := Parse(encrypted)
    80  			if err != nil {
    81  				t.Fatalf("cannot Parse encrypted result: %s", err)
    82  			}
    83  			result, err := p7.Decrypt(cert.Certificate, *cert.PrivateKey)
    84  			if err != nil {
    85  				t.Fatalf("cannot Decrypt encrypted result: %s", err)
    86  			}
    87  			if !bytes.Equal(plaintext, result) {
    88  				t.Errorf("encrypted data does not match plaintext:\n\tExpected: %s\n\tActual: %s", plaintext, result)
    89  			}
    90  		}
    91  	}
    92  }
    93  
    94  func TestEncryptCFCA(t *testing.T) {
    95  	ciphers := []pkcs.Cipher{
    96  		pkcs.SM4,
    97  		pkcs.SM4CBC,
    98  		pkcs.SM4GCM,
    99  	}
   100  	sigalgs := []x509.SignatureAlgorithm{
   101  		smx509.SM2WithSM3,
   102  	}
   103  	for _, cipher := range ciphers {
   104  		for _, sigalg := range sigalgs {
   105  			plaintext := []byte("Hello Secret World!")
   106  			cert, err := createTestCertificate(sigalg)
   107  			if err != nil {
   108  				t.Fatal(err)
   109  			}
   110  			encrypted, err := EncryptCFCA(cipher, plaintext, []*smx509.Certificate{cert.Certificate})
   111  			if err != nil {
   112  				t.Fatal(err)
   113  			}
   114  			pem.Encode(os.Stdout, &pem.Block{Type: "PKCS7", Bytes: encrypted})
   115  			p7, err := Parse(encrypted)
   116  			if err != nil {
   117  				t.Fatalf("cannot Parse encrypted result: %s", err)
   118  			}
   119  			result, err := p7.DecryptCFCA(cert.Certificate, *cert.PrivateKey)
   120  			if err != nil {
   121  				t.Fatalf("cannot Decrypt encrypted result: %s", err)
   122  			}
   123  			if !bytes.Equal(plaintext, result) {
   124  				t.Errorf("encrypted data does not match plaintext:\n\tExpected: %s\n\tActual: %s", plaintext, result)
   125  			}
   126  		}
   127  	}
   128  }