github.com/emmansun/gmsm@v0.29.1/kdf/kdf_test.go (about)

     1  package kdf
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"hash"
     8  	"math/big"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/emmansun/gmsm/sm3"
    13  )
    14  
    15  func TestKdf(t *testing.T) {
    16  	type args struct {
    17  		md  hash.Hash
    18  		z   []byte
    19  		len int
    20  	}
    21  	tests := []struct {
    22  		name string
    23  		args args
    24  		want string
    25  	}{
    26  		{"sm3 case 1", args{sm3.New(), []byte("emmansun"), 16}, "708993ef1388a0ae4245a19bb6c02554"},
    27  		{"sm3 case 2", args{sm3.New(), []byte("emmansun"), 32}, "708993ef1388a0ae4245a19bb6c02554c632633e356ddb989beb804fda96cfd4"},
    28  		{"sm3 case 3", args{sm3.New(), []byte("emmansun"), 48}, "708993ef1388a0ae4245a19bb6c02554c632633e356ddb989beb804fda96cfd47eba4fa460e7b277bc6b4ce4d07ed493"},
    29  		{"sm3 case 4", args{sm3.New(), []byte("708993ef1388a0ae4245a19bb6c02554c632633e356ddb989beb804fda96cfd47eba4fa460e7b277bc6b4ce4d07ed493708993ef1388a0ae4245a19bb6c02554c632633e356ddb989beb804fda96cfd47eba4fa460e7b277bc6b4ce4d07ed493"), 48}, "49cf14649f324a07e0d5bb2a00f7f05d5f5bdd6d14dff028e071327ec031104590eddb18f98b763e18bf382ff7c3875f"},
    30  		{"sm3 case 5", args{sm3.New(), []byte("708993ef1388a0ae4245a19bb6c02554c632633e356ddb989beb804fda96cfd47eba4fa460e7b277bc6b4ce4d07ed493708993ef1388a0ae4245a19bb6c02554c632633e356ddb989beb804fda96cfd47eba4fa460e7b277bc6b4ce4d07ed493"), 128}, "49cf14649f324a07e0d5bb2a00f7f05d5f5bdd6d14dff028e071327ec031104590eddb18f98b763e18bf382ff7c3875f30277f3179baebd795e7853fa643fdf280d8d7b81a2ab7829f615e132ab376d32194cd315908d27090e1180ce442d9be99322523db5bfac40ac5acb03550f5c93e5b01b1d71f2630868909a6a1250edb"},
    31  	}
    32  	for _, tt := range tests {
    33  		wantBytes, _ := hex.DecodeString(tt.want)
    34  		t.Run(tt.name, func(t *testing.T) {
    35  			if got := Kdf(sm3.New, tt.args.z, tt.args.len); !reflect.DeepEqual(got, wantBytes) {
    36  				t.Errorf("Kdf(%v) = %x, want %v", tt.name, got, tt.want)
    37  			}
    38  		})
    39  	}
    40  }
    41  
    42  func TestKdfOldCase(t *testing.T) {
    43  	x2, _ := new(big.Int).SetString("64D20D27D0632957F8028C1E024F6B02EDF23102A566C932AE8BD613A8E865FE", 16)
    44  	y2, _ := new(big.Int).SetString("58D225ECA784AE300A81A2D48281A828E1CEDF11C4219099840265375077BF78", 16)
    45  
    46  	expected := "006e30dae231b071dfad8aa379e90264491603"
    47  
    48  	result := Kdf(sm3.New, append(x2.Bytes(), y2.Bytes()...), 19)
    49  
    50  	resultStr := hex.EncodeToString(result)
    51  
    52  	if expected != resultStr {
    53  		t.Fatalf("expected %s, real value %s", expected, resultStr)
    54  	}
    55  }
    56  
    57  func shouldPanic(t *testing.T, f func()) {
    58  	t.Helper()
    59  	defer func() { 
    60  		t.Helper()
    61  		err := recover()
    62  		if err == nil {
    63  			t.Errorf("should have panicked")
    64  		}
    65  	}()
    66  	f()
    67  }
    68  
    69  func TestKdfWithSHA256(t *testing.T) {
    70  	type args struct {
    71  		z   []byte
    72  		len int
    73  	}
    74  	tests := []struct {
    75  		name string
    76  		args args
    77  		want string
    78  	}{
    79  		{"sha256 case 1", args{[]byte("emmansun"), 16}, "1bca7e7d05a858f5852a6e0ce7e99852"},
    80  		{"sha256 case 2", args{[]byte("emmansun"), 32}, "1bca7e7d05a858f5852a6e0ce7e9985294ebdc82c7f1c6539f89356d9c0a2856"},
    81  		{"sha256 case 3", args{[]byte("emmansun"), 48}, "1bca7e7d05a858f5852a6e0ce7e9985294ebdc82c7f1c6539f89356d9c0a28569500417f9b74de4ea18a85813b8968ba"},
    82  		{"sha256 case 4", args{[]byte("708993ef1388a0ae4245a19bb6c02554c632633e356ddb989beb804fda96cfd47eba4fa460e7b277bc6b4ce4d07ed493708993ef1388a0ae4245a19bb6c02554c632633e356ddb989beb804fda96cfd47eba4fa460e7b277bc6b4ce4d07ed493"), 48}, "61cc5b862a0a6511b3558536112c7ba4f21c9d65025505c0099bbba7196a35ed34d7805e5c4d779fcd0d950f693ec0f8"},
    83  		{"sha256 case 5", args{[]byte("708993ef1388a0ae4245a19bb6c02554c632633e356ddb989beb804fda96cfd47eba4fa460e7b277bc6b4ce4d07ed493708993ef1388a0ae4245a19bb6c02554c632633e356ddb989beb804fda96cfd47eba4fa460e7b277bc6b4ce4d07ed493"), 128}, "61cc5b862a0a6511b3558536112c7ba4f21c9d65025505c0099bbba7196a35ed34d7805e5c4d779fcd0d950f693ec0f8b1fdc996e97eadb5b7bee7ac44dd1a7954a44dd92c71c465f4ab20479c92748f179bd03bdad1768c65b59d62a0735dcf08837a04f32f53d45b5bdb00f5fd1bee003f6fcc01c003594d33014161862030"},
    84  	}
    85  	for _, tt := range tests {
    86  		wantBytes, _ := hex.DecodeString(tt.want)
    87  		t.Run(tt.name, func(t *testing.T) {
    88  			if got := Kdf(sha256.New, tt.args.z, tt.args.len); !reflect.DeepEqual(got, wantBytes) {
    89  				t.Errorf("Kdf(%v) = %x, want %v", tt.name, got, tt.want)
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func BenchmarkKdf(b *testing.B) {
    96  	tests := []struct {
    97  		zLen int
    98  		kLen int
    99  	}{
   100  		{32, 32},
   101  		{32, 64},
   102  		{32, 128},
   103  		{64, 32},
   104  		{64, 64},
   105  		{64, 128},
   106  		{64, 256},
   107  		{64, 512},
   108  		{64, 1024},
   109  	}
   110  	z := make([]byte, 512)
   111  	for _, tt := range tests {
   112  		b.Run(fmt.Sprintf("zLen=%v-kLen=%v", tt.zLen, tt.kLen), func(b *testing.B) {
   113  			b.ReportAllocs()
   114  			b.ResetTimer()
   115  			for i := 0; i < b.N; i++ {
   116  				Kdf(sm3.New, z[:tt.zLen], tt.kLen)
   117  			}
   118  		})
   119  	}
   120  }