gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/gmtls/key_schedule_test.go (about)

     1  // Copyright (c) 2022 zhaochun
     2  // core-gm is licensed under Mulan PSL v2.
     3  // You can use this software according to the terms and conditions of the Mulan PSL v2.
     4  // You may obtain a copy of Mulan PSL v2 at:
     5  //          http://license.coscl.org.cn/MulanPSL2
     6  // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
     7  // See the Mulan PSL v2 for more details.
     8  
     9  /*
    10  gmtls是基于`golang/go`的`tls`包实现的国密改造版本。
    11  对应版权声明: thrid_licenses/github.com/golang/go/LICENSE
    12  */
    13  
    14  package gmtls
    15  
    16  import (
    17  	"bytes"
    18  	"encoding/hex"
    19  	"hash"
    20  	"strings"
    21  	"testing"
    22  	"unicode"
    23  )
    24  
    25  // This file contains tests derived from draft-ietf-tls-tls13-vectors-07.
    26  
    27  func parseVector(v string) []byte {
    28  	v = strings.Map(func(c rune) rune {
    29  		if unicode.IsSpace(c) {
    30  			return -1
    31  		}
    32  		return c
    33  	}, v)
    34  	parts := strings.Split(v, ":")
    35  	v = parts[len(parts)-1]
    36  	res, err := hex.DecodeString(v)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	return res
    41  }
    42  
    43  func TestDeriveSecret(t *testing.T) {
    44  	chTranscript := cipherSuitesTLS13[0].hash.New()
    45  	chTranscript.Write(parseVector(`
    46  	payload (512 octets):  01 00 01 fc 03 03 1b c3 ce b6 bb e3 9c ff
    47  	93 83 55 b5 a5 0a db 6d b2 1b 7a 6a f6 49 d7 b4 bc 41 9d 78 76
    48  	48 7d 95 00 00 06 13 01 13 03 13 02 01 00 01 cd 00 00 00 0b 00
    49  	09 00 00 06 73 65 72 76 65 72 ff 01 00 01 00 00 0a 00 14 00 12
    50  	00 1d 00 17 00 18 00 19 01 00 01 01 01 02 01 03 01 04 00 33 00
    51  	26 00 24 00 1d 00 20 e4 ff b6 8a c0 5f 8d 96 c9 9d a2 66 98 34
    52  	6c 6b e1 64 82 ba dd da fe 05 1a 66 b4 f1 8d 66 8f 0b 00 2a 00
    53  	00 00 2b 00 03 02 03 04 00 0d 00 20 00 1e 04 03 05 03 06 03 02
    54  	03 08 04 08 05 08 06 04 01 05 01 06 01 02 01 04 02 05 02 06 02
    55  	02 02 00 2d 00 02 01 01 00 1c 00 02 40 01 00 15 00 57 00 00 00
    56  	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    57  	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    58  	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    59  	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    60  	00 29 00 dd 00 b8 00 b2 2c 03 5d 82 93 59 ee 5f f7 af 4e c9 00
    61  	00 00 00 26 2a 64 94 dc 48 6d 2c 8a 34 cb 33 fa 90 bf 1b 00 70
    62  	ad 3c 49 88 83 c9 36 7c 09 a2 be 78 5a bc 55 cd 22 60 97 a3 a9
    63  	82 11 72 83 f8 2a 03 a1 43 ef d3 ff 5d d3 6d 64 e8 61 be 7f d6
    64  	1d 28 27 db 27 9c ce 14 50 77 d4 54 a3 66 4d 4e 6d a4 d2 9e e0
    65  	37 25 a6 a4 da fc d0 fc 67 d2 ae a7 05 29 51 3e 3d a2 67 7f a5
    66  	90 6c 5b 3f 7d 8f 92 f2 28 bd a4 0d da 72 14 70 f9 fb f2 97 b5
    67  	ae a6 17 64 6f ac 5c 03 27 2e 97 07 27 c6 21 a7 91 41 ef 5f 7d
    68  	e6 50 5e 5b fb c3 88 e9 33 43 69 40 93 93 4a e4 d3 57 fa d6 aa
    69  	cb 00 21 20 3a dd 4f b2 d8 fd f8 22 a0 ca 3c f7 67 8e f5 e8 8d
    70  	ae 99 01 41 c5 92 4d 57 bb 6f a3 1b 9e 5f 9d`))
    71  
    72  	type args struct {
    73  		secret     []byte
    74  		label      string
    75  		transcript hash.Hash
    76  	}
    77  	tests := []struct {
    78  		name string
    79  		args args
    80  		want []byte
    81  	}{
    82  		{
    83  			`derive secret for handshake "tls13 derived"`,
    84  			args{
    85  				parseVector(`PRK (32 octets):  33 ad 0a 1c 60 7e c0 3b 09 e6 cd 98 93 68 0c e2
    86  				10 ad f3 00 aa 1f 26 60 e1 b2 2e 10 f1 70 f9 2a`),
    87  				"derived",
    88  				nil,
    89  			},
    90  			parseVector(`expanded (32 octets):  6f 26 15 a1 08 c7 02 c5 67 8f 54 fc 9d ba
    91  			b6 97 16 c0 76 18 9c 48 25 0c eb ea c3 57 6c 36 11 ba`),
    92  		},
    93  		{
    94  			`derive secret "tls13 c e traffic"`,
    95  			args{
    96  				parseVector(`PRK (32 octets):  9b 21 88 e9 b2 fc 6d 64 d7 1d c3 29 90 0e 20 bb
    97  				41 91 50 00 f6 78 aa 83 9c bb 79 7c b7 d8 33 2c`),
    98  				"c e traffic",
    99  				chTranscript,
   100  			},
   101  			parseVector(`expanded (32 octets):  3f bb e6 a6 0d eb 66 c3 0a 32 79 5a ba 0e
   102  			ff 7e aa 10 10 55 86 e7 be 5c 09 67 8d 63 b6 ca ab 62`),
   103  		},
   104  	}
   105  	for _, tt := range tests {
   106  		t.Run(tt.name, func(t *testing.T) {
   107  			c := cipherSuitesTLS13[0]
   108  			if got := c.deriveSecret(tt.args.secret, tt.args.label, tt.args.transcript); !bytes.Equal(got, tt.want) {
   109  				t.Errorf("cipherSuiteTLS13.deriveSecret() = % x, want % x", got, tt.want)
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  func TestTrafficKey(t *testing.T) {
   116  	trafficSecret := parseVector(
   117  		`PRK (32 octets):  b6 7b 7d 69 0c c1 6c 4e 75 e5 42 13 cb 2d 37 b4
   118  		e9 c9 12 bc de d9 10 5d 42 be fd 59 d3 91 ad 38`)
   119  	wantKey := parseVector(
   120  		`key expanded (16 octets):  3f ce 51 60 09 c2 17 27 d0 f2 e4 e8 6e
   121  		e4 03 bc`)
   122  	wantIV := parseVector(
   123  		`iv expanded (12 octets):  5d 31 3e b2 67 12 76 ee 13 00 0b 30`)
   124  
   125  	c := cipherSuitesTLS13[0]
   126  	gotKey, gotIV := c.trafficKey(trafficSecret)
   127  	if !bytes.Equal(gotKey, wantKey) {
   128  		t.Errorf("cipherSuiteTLS13.trafficKey() gotKey = % x, want % x", gotKey, wantKey)
   129  	}
   130  	if !bytes.Equal(gotIV, wantIV) {
   131  		t.Errorf("cipherSuiteTLS13.trafficKey() gotIV = % x, want % x", gotIV, wantIV)
   132  	}
   133  }
   134  
   135  func TestExtract(t *testing.T) {
   136  	type args struct {
   137  		newSecret     []byte
   138  		currentSecret []byte
   139  	}
   140  	tests := []struct {
   141  		name string
   142  		args args
   143  		want []byte
   144  	}{
   145  		{
   146  			`extract secret "early"`,
   147  			args{
   148  				nil,
   149  				nil,
   150  			},
   151  			parseVector(`secret (32 octets):  33 ad 0a 1c 60 7e c0 3b 09 e6 cd 98 93 68 0c
   152  			e2 10 ad f3 00 aa 1f 26 60 e1 b2 2e 10 f1 70 f9 2a`),
   153  		},
   154  		{
   155  			`extract secret "master"`,
   156  			args{
   157  				nil,
   158  				parseVector(`salt (32 octets):  43 de 77 e0 c7 77 13 85 9a 94 4d b9 db 25 90 b5
   159  				31 90 a6 5b 3e e2 e4 f1 2d d7 a0 bb 7c e2 54 b4`),
   160  			},
   161  			parseVector(`secret (32 octets):  18 df 06 84 3d 13 a0 8b f2 a4 49 84 4c 5f 8a
   162  			47 80 01 bc 4d 4c 62 79 84 d5 a4 1d a8 d0 40 29 19`),
   163  		},
   164  		{
   165  			`extract secret "handshake"`,
   166  			args{
   167  				parseVector(`IKM (32 octets):  8b d4 05 4f b5 5b 9d 63 fd fb ac f9 f0 4b 9f 0d
   168  				35 e6 d6 3f 53 75 63 ef d4 62 72 90 0f 89 49 2d`),
   169  				parseVector(`salt (32 octets):  6f 26 15 a1 08 c7 02 c5 67 8f 54 fc 9d ba b6 97
   170  				16 c0 76 18 9c 48 25 0c eb ea c3 57 6c 36 11 ba`),
   171  			},
   172  			parseVector(`secret (32 octets):  1d c8 26 e9 36 06 aa 6f dc 0a ad c1 2f 74 1b
   173  			01 04 6a a6 b9 9f 69 1e d2 21 a9 f0 ca 04 3f be ac`),
   174  		},
   175  	}
   176  	for _, tt := range tests {
   177  		t.Run(tt.name, func(t *testing.T) {
   178  			c := cipherSuitesTLS13[0]
   179  			if got := c.extract(tt.args.newSecret, tt.args.currentSecret); !bytes.Equal(got, tt.want) {
   180  				t.Errorf("cipherSuiteTLS13.extract() = % x, want % x", got, tt.want)
   181  			}
   182  		})
   183  	}
   184  }