Skip to content

Commit

Permalink
Merge pull request #103 from ueno/wip/tls1-prf
Browse files Browse the repository at this point in the history
Support TLS 1.2 PRF
  • Loading branch information
qmuntal authored Aug 24, 2023
2 parents bd1e6e7 + 7056514 commit de9ae4f
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 0 deletions.
7 changes: 7 additions & 0 deletions shims.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum {
GO_EVP_PKEY_CTRL_MD = 1,
GO_EVP_PKEY_RSA = 6,
GO_EVP_PKEY_EC = 408,
GO_EVP_PKEY_TLS1_PRF = 1021,
GO_EVP_PKEY_HKDF = 1036,
/* This is defined differently in OpenSSL 3 (1 << 11), but in our
* code it is only used in OpenSSL 1.
Expand All @@ -43,6 +44,9 @@ enum {
GO_EVP_KDF_HKDF_MODE_EXTRACT_ONLY = 1,
GO_EVP_KDF_HKDF_MODE_EXPAND_ONLY = 2,

GO_EVP_PKEY_CTRL_TLS_MD = 0x1000,
GO_EVP_PKEY_CTRL_TLS_SECRET = 0x1001,
GO_EVP_PKEY_CTRL_TLS_SEED = 0x1002,
GO_EVP_PKEY_CTRL_HKDF_MD = 0x1003,
GO_EVP_PKEY_CTRL_HKDF_SALT = 0x1004,
GO_EVP_PKEY_CTRL_HKDF_KEY = 0x1005,
Expand Down Expand Up @@ -341,4 +345,7 @@ DEFINEFUNC_3_0(int, EVP_PKEY_up_ref, (GO_EVP_PKEY_PTR key), (key)) \
DEFINEFUNC_LEGACY_1(int, EVP_PKEY_set1_EC_KEY, (GO_EVP_PKEY_PTR pkey, GO_EC_KEY_PTR key), (pkey, key)) \
DEFINEFUNC_3_0(int, EVP_PKEY_CTX_set0_rsa_oaep_label, (GO_EVP_PKEY_CTX_PTR ctx, void *label, int len), (ctx, label, len)) \
DEFINEFUNC(int, PKCS5_PBKDF2_HMAC, (const char *pass, int passlen, const unsigned char *salt, int saltlen, int iter, const GO_EVP_MD_PTR digest, int keylen, unsigned char *out), (pass, passlen, salt, saltlen, iter, digest, keylen, out)) \
DEFINEFUNC_3_0(int, EVP_PKEY_CTX_set_tls1_prf_md, (GO_EVP_PKEY_CTX_PTR arg0, const GO_EVP_MD_PTR arg1), (arg0, arg1)) \
DEFINEFUNC_3_0(int, EVP_PKEY_CTX_set1_tls1_prf_secret, (GO_EVP_PKEY_CTX_PTR arg0, const unsigned char *arg1, int arg2), (arg0, arg1, arg2)) \
DEFINEFUNC_3_0(int, EVP_PKEY_CTX_add1_tls1_prf_seed, (GO_EVP_PKEY_CTX_PTR arg0, const unsigned char *arg1, int arg2), (arg0, arg1, arg2)) \

79 changes: 79 additions & 0 deletions tls1prf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//go:build linux && !cmd_go_bootstrap

package openssl

// #include "goopenssl.h"
import "C"
import (
"errors"
"hash"
"unsafe"
)

func SupportsTLS1PRF() bool {
return vMajor > 1 ||
(vMajor >= 1 && vMinor > 1) ||
(vMajor >= 1 && vMinor >= 1 && vPatch >= 1)
}

func TLS1PRF(secret, seed []byte, keyLen int, h func() hash.Hash) ([]byte, error) {
ch := h()
md := hashToMD(ch)
if md == nil {
return nil, errors.New("unsupported hash function")
}

ctx := C.go_openssl_EVP_PKEY_CTX_new_id(C.GO_EVP_PKEY_TLS1_PRF, nil)
if ctx == nil {
return nil, newOpenSSLError("EVP_PKEY_CTX_new_id")
}
defer func() {
C.go_openssl_EVP_PKEY_CTX_free(ctx)
}()

if C.go_openssl_EVP_PKEY_derive_init(ctx) != 1 {
return nil, newOpenSSLError("EVP_PKEY_derive_init")
}
switch vMajor {
case 3:
if C.go_openssl_EVP_PKEY_CTX_set_tls1_prf_md(ctx, md) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_set_tls1_prf_md")
}
if C.go_openssl_EVP_PKEY_CTX_set1_tls1_prf_secret(ctx,
base(secret), C.int(len(secret))) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_set1_tls1_prf_secret")
}
if C.go_openssl_EVP_PKEY_CTX_add1_tls1_prf_seed(ctx,
base(seed), C.int(len(seed))) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_add1_tls1_prf_seed")
}
case 1:
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, -1,
C.GO1_EVP_PKEY_OP_DERIVE,
C.GO_EVP_PKEY_CTRL_TLS_MD,
0, unsafe.Pointer(md)) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_set_tls1_prf_md")
}
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, -1,
C.GO1_EVP_PKEY_OP_DERIVE,
C.GO_EVP_PKEY_CTRL_TLS_SECRET,
C.int(len(secret)), unsafe.Pointer(base(secret))) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_set1_tls1_prf_secret")
}
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, -1,
C.GO1_EVP_PKEY_OP_DERIVE,
C.GO_EVP_PKEY_CTRL_TLS_SEED,
C.int(len(seed)), unsafe.Pointer(base(seed))) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_add1_tls1_prf_seed")
}
}
outLen := C.size_t(keyLen)
out := make([]byte, outLen)
if C.go_openssl_EVP_PKEY_derive(ctx, base(out), &outLen) != 1 {
return nil, newOpenSSLError("EVP_PKEY_derive")
}
if outLen != C.size_t(keyLen) {
return nil, errors.New("tls1-prf: entropy limit reached")
}
return out[:outLen], nil
}
139 changes: 139 additions & 0 deletions tls1prf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//go:build linux

package openssl_test

import (
"bytes"
"hash"
"testing"

"github.com/golang-fips/openssl/v2"
)

type tls1prfTest struct {
hash func() hash.Hash
secret []byte
seed []byte
out []byte
}

var tls1prfTests = []tls1prfTest{
// Tests from https://mailarchive.ietf.org/arch/msg/tls/fzVCzk-z3FShgGJ6DOXqM1ydxms/
{
openssl.NewSHA256,
[]byte{
0x9b, 0xbe, 0x43, 0x6b, 0xa9, 0x40, 0xf0, 0x17,
0xb1, 0x76, 0x52, 0x84, 0x9a, 0x71, 0xdb, 0x35,
},
[]byte{
0x74, 0x65, 0x73, 0x74, 0x20, 0x6c, 0x61, 0x62,
0x65, 0x6c,
0xa0, 0xba, 0x9f, 0x93, 0x6c, 0xda, 0x31, 0x18,
0x27, 0xa6, 0xf7, 0x96, 0xff, 0xd5, 0x19, 0x8c,
},
[]byte{
0xe3, 0xf2, 0x29, 0xba, 0x72, 0x7b, 0xe1, 0x7b,
0x8d, 0x12, 0x26, 0x20, 0x55, 0x7c, 0xd4, 0x53,
0xc2, 0xaa, 0xb2, 0x1d, 0x07, 0xc3, 0xd4, 0x95,
0x32, 0x9b, 0x52, 0xd4, 0xe6, 0x1e, 0xdb, 0x5a,
0x6b, 0x30, 0x17, 0x91, 0xe9, 0x0d, 0x35, 0xc9,
0xc9, 0xa4, 0x6b, 0x4e, 0x14, 0xba, 0xf9, 0xaf,
0x0f, 0xa0, 0x22, 0xf7, 0x07, 0x7d, 0xef, 0x17,
0xab, 0xfd, 0x37, 0x97, 0xc0, 0x56, 0x4b, 0xab,
0x4f, 0xbc, 0x91, 0x66, 0x6e, 0x9d, 0xef, 0x9b,
0x97, 0xfc, 0xe3, 0x4f, 0x79, 0x67, 0x89, 0xba,
0xa4, 0x80, 0x82, 0xd1, 0x22, 0xee, 0x42, 0xc5,
0xa7, 0x2e, 0x5a, 0x51, 0x10, 0xff, 0xf7, 0x01,
0x87, 0x34, 0x7b, 0x66,
},
},
{
openssl.NewSHA384,
[]byte{
0xb8, 0x0b, 0x73, 0x3d, 0x6c, 0xee, 0xfc, 0xdc,
0x71, 0x56, 0x6e, 0xa4, 0x8e, 0x55, 0x67, 0xdf,
},
[]byte{
0x74, 0x65, 0x73, 0x74, 0x20, 0x6c, 0x61, 0x62,
0x65, 0x6c,
0xcd, 0x66, 0x5c, 0xf6, 0xa8, 0x44, 0x7d, 0xd6,
0xff, 0x8b, 0x27, 0x55, 0x5e, 0xdb, 0x74, 0x65,
},
[]byte{
0x7b, 0x0c, 0x18,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, 0x42, 0xfb,
0x8d, 0xa4, 0x19, 0x76, 0x32, 0xbd, 0xb5, 0x4f,
0xf6, 0x63, 0x3f, 0x86, 0xbb, 0xc8, 0x36, 0xe6,
0x40, 0xd4, 0xd8, 0x98,
},
},
}

func TestTLS1PRF(t *testing.T) {
if !openssl.SupportsTLS1PRF() {
t.Skip("TLS 1.2 PRF is not supported")
}
for i, tt := range tls1prfTests {
out, err := openssl.TLS1PRF(tt.secret, tt.seed, len(tt.out), tt.hash)
if err != nil {
t.Errorf("test %d: error deriving TLS 1.2 PRF: %v.", i, err)
}
if !bytes.Equal(out, tt.out) {
t.Errorf("test %d: incorrect key output: have %v, need %v.", i, out, tt.out)
}
}
}

0 comments on commit de9ae4f

Please sign in to comment.