github.com/emmansun/gmsm@v0.29.1/drbg/common_test.go (about)

     1  package drbg
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/aes"
     6  	"crypto/sha256"
     7  	"testing"
     8  )
     9  
    10  func TestGmCtrDrbgPrng(t *testing.T) {
    11  	prng, err := NewGmCtrDrbgPrng(nil, 32, SECURITY_LEVEL_TEST, nil)
    12  	if err != nil {
    13  		t.Fatal(err)
    14  	}
    15  	data := make([]byte, 33)
    16  	for i := 0; i < int(DRBG_RESEED_COUNTER_INTERVAL_LEVEL_TEST+1); i++ {
    17  		n, err := prng.Read(data)
    18  		if err != nil {
    19  			t.Fatal(err)
    20  		}
    21  		if n != 33 {
    22  			t.Errorf("not got enough random bytes")
    23  		}
    24  	}
    25  }
    26  
    27  func TestGmCtrDrbgPrngReseedCase(t *testing.T) {
    28  	prng, err := NewGmCtrDrbgPrng(nil, 32, SECURITY_LEVEL_TEST, nil)
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	data := make([]byte, 64)
    34  	for i := 0; i < int(DRBG_RESEED_COUNTER_INTERVAL_LEVEL_TEST+1); i++ {
    35  		for j := 0; j < 64; j++ {
    36  			data[j] = 0
    37  		}
    38  		n, err := prng.Read(data)
    39  		if err != nil {
    40  			t.Fatal(err)
    41  		}
    42  		if n != 64 {
    43  			t.Errorf("not got enough random bytes")
    44  		}
    45  		if bytes.Contains(data, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) {
    46  			t.Fatal("failed, it's a bug")
    47  		}
    48  	}
    49  }
    50  
    51  func TestNistCtrDrbgPrng(t *testing.T) {
    52  	prng, err := NewNistCtrDrbgPrng(aes.NewCipher, 16, nil, 16, SECURITY_LEVEL_TEST, nil)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	data := make([]byte, MAX_BYTES_PER_GENERATE+1)
    57  	n, err := prng.Read(data)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	if n != MAX_BYTES_PER_GENERATE+1 {
    62  		t.Errorf("not got enough random bytes")
    63  	}
    64  }
    65  
    66  func TestGmHashDrbgPrng(t *testing.T) {
    67  	prng, err := NewGmHashDrbgPrng(nil, 32, SECURITY_LEVEL_TEST, nil)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	data := make([]byte, 33)
    72  	for i := 0; i < int(DRBG_RESEED_COUNTER_INTERVAL_LEVEL_TEST+1); i++ {
    73  		n, err := prng.Read(data)
    74  		if err != nil {
    75  			t.Fatal(err)
    76  		}
    77  		if n != 33 {
    78  			t.Errorf("not got enough random bytes")
    79  		}
    80  	}
    81  }
    82  
    83  func TestNistHashDrbgPrng(t *testing.T) {
    84  	prng, err := NewNistHashDrbgPrng(sha256.New, nil, 32, SECURITY_LEVEL_TEST, nil)
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	data := make([]byte, MAX_BYTES_PER_GENERATE+1)
    89  	n, err := prng.Read(data)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	if n != MAX_BYTES_PER_GENERATE+1 {
    94  		t.Errorf("not got enough random bytes")
    95  	}
    96  }
    97  
    98  
    99  func TestNistHmacDrbgPrng(t *testing.T) {
   100  	prng, err := NewNistHmacDrbgPrng(sha256.New, nil, 32, SECURITY_LEVEL_TEST, nil)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	data := make([]byte, MAX_BYTES_PER_GENERATE+1)
   105  	n, err := prng.Read(data)
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  	if n != MAX_BYTES_PER_GENERATE+1 {
   110  		t.Errorf("not got enough random bytes")
   111  	}
   112  }
   113  
   114  func TestGMSecurityStrengthValidation(t *testing.T) {
   115  	_, err := NewGmHashDrbgPrng(nil, 24, SECURITY_LEVEL_TEST, nil)
   116  	if err == nil {
   117  		t.Fatalf("expected error here")
   118  	}
   119  	_, err = NewGmCtrDrbgPrng(nil, 24, SECURITY_LEVEL_TEST, nil)
   120  	if err == nil {
   121  		t.Fatalf("expected error here")
   122  	}
   123  }