Skip to content

Commit 758238f

Browse files
authored
Merge pull request #110 from golang-fips/tlsprf1
Support PRF for TLS1.1
2 parents 8cbc4d7 + 0d84c8e commit 758238f

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

tls1prf.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@ package openssl
55
// #include "goopenssl.h"
66
import "C"
77
import (
8+
"crypto"
89
"errors"
910
"hash"
1011
"unsafe"
1112
)
1213

1314
func SupportsTLS1PRF() bool {
1415
return vMajor > 1 ||
15-
(vMajor >= 1 && vMinor > 1) ||
16-
(vMajor >= 1 && vMinor >= 1 && vPatch >= 1)
16+
(vMajor >= 1 && vMinor > 1)
1717
}
1818

1919
func TLS1PRF(secret, label, seed []byte, keyLen int, h func() hash.Hash) ([]byte, error) {
20-
ch := h()
21-
md := hashToMD(ch)
20+
var md C.GO_EVP_MD_PTR
21+
if h == nil {
22+
// TLS 1.0/1.1 PRF doesn't allow to specify the hash function,
23+
// it always uses MD5SHA1. If h is nil, then assume
24+
// that the caller wants to use TLS 1.0/1.1 PRF.
25+
// OpenSSL detects this case by checking if the hash
26+
// function is MD5SHA1.
27+
md = cryptoHashToMD(crypto.MD5SHA1)
28+
} else {
29+
md = hashToMD(h())
30+
}
2231
if md == nil {
2332
return nil, errors.New("unsupported hash function")
2433
}

tls1prf_test.go

+38-13
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,43 @@ package openssl_test
44

55
import (
66
"bytes"
7-
"hash"
7+
"crypto"
88
"testing"
99

1010
"github.com/golang-fips/openssl/v2"
1111
)
1212

1313
type tls1prfTest struct {
14-
hash func() hash.Hash
14+
hash crypto.Hash
1515
secret []byte
1616
label []byte
1717
seed []byte
1818
out []byte
1919
}
2020

2121
var tls1prfTests = []tls1prfTest{
22+
// TLS 1.0/1.1 test generated with OpenSSL and cross-validated
23+
// with Windows CNG.
24+
{
25+
crypto.MD5SHA1,
26+
[]byte{
27+
0x9b, 0xbe, 0x43, 0x6b, 0xa9, 0x40, 0xf0, 0x17,
28+
0xb1, 0x76, 0x52, 0x84, 0x9a, 0x71, 0xdb, 0x35,
29+
},
30+
[]byte{
31+
0x74, 0x65, 0x73, 0x74, 0x20, 0x6c, 0x61, 0x62,
32+
0x65, 0x6c},
33+
[]byte{
34+
0xa0, 0xba, 0x9f, 0x93, 0x6c, 0xda, 0x31, 0x18,
35+
0x27, 0xa6, 0xf7, 0x96, 0xff, 0xd5, 0x19, 0x8c,
36+
},
37+
[]byte{
38+
0x66, 0x17, 0x40, 0xe6, 0xf9, 0x8b, 0xc9, 0x01,
39+
},
40+
},
2241
// Tests from https://mailarchive.ietf.org/arch/msg/tls/fzVCzk-z3FShgGJ6DOXqM1ydxms/
2342
{
24-
openssl.NewSHA256,
43+
crypto.SHA256,
2544
[]byte{
2645
0x9b, 0xbe, 0x43, 0x6b, 0xa9, 0x40, 0xf0, 0x17,
2746
0xb1, 0x76, 0x52, 0x84, 0x9a, 0x71, 0xdb, 0x35,
@@ -50,7 +69,7 @@ var tls1prfTests = []tls1prfTest{
5069
},
5170
},
5271
{
53-
openssl.NewSHA384,
72+
crypto.SHA384,
5473
[]byte{
5574
0xb8, 0x0b, 0x73, 0x3d, 0x6c, 0xee, 0xfc, 0xdc,
5675
0x71, 0x56, 0x6e, 0xa4, 0x8e, 0x55, 0x67, 0xdf,
@@ -85,7 +104,7 @@ var tls1prfTests = []tls1prfTest{
85104
},
86105
},
87106
{
88-
openssl.NewSHA512,
107+
crypto.SHA512,
89108
[]byte{
90109
0xb0, 0x32, 0x35, 0x23, 0xc1, 0x85, 0x35, 0x99,
91110
0x58, 0x4d, 0x88, 0x56, 0x8b, 0xbb, 0x05, 0xeb,
@@ -132,13 +151,19 @@ func TestTLS1PRF(t *testing.T) {
132151
if !openssl.SupportsTLS1PRF() {
133152
t.Skip("TLS 1.2 PRF is not supported")
134153
}
135-
for i, tt := range tls1prfTests {
136-
out, err := openssl.TLS1PRF(tt.secret, tt.label, tt.seed, len(tt.out), tt.hash)
137-
if err != nil {
138-
t.Errorf("test %d: error deriving TLS 1.2 PRF: %v.", i, err)
139-
}
140-
if !bytes.Equal(out, tt.out) {
141-
t.Errorf("test %d: incorrect key output: have %v, need %v.", i, out, tt.out)
142-
}
154+
for _, tt := range tls1prfTests {
155+
tt := tt
156+
t.Run(tt.hash.String(), func(t *testing.T) {
157+
if !openssl.SupportsHash(tt.hash) {
158+
t.Skip("skipping: hash not supported")
159+
}
160+
out, err := openssl.TLS1PRF(tt.secret, tt.label, tt.seed, len(tt.out), cryptoToHash(tt.hash))
161+
if err != nil {
162+
t.Fatalf("error deriving TLS 1.2 PRF: %v.", err)
163+
}
164+
if !bytes.Equal(out, tt.out) {
165+
t.Errorf("incorrect key output: have %v, need %v.", out, tt.out)
166+
}
167+
})
143168
}
144169
}

0 commit comments

Comments
 (0)