github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/gmtls/key_schedule_test.go (about)

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