add shared secret derivation and test for ECDH.

This commit is contained in:
Christopher Dudley 2017-12-15 10:54:33 -05:00 committed by Jeff
parent 64a7b8acc3
commit 7689615645
4 changed files with 121 additions and 0 deletions

70
dh.go Normal file
View File

@ -0,0 +1,70 @@
// Copyright (C) 2017. See AUTHORS.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openssl
// #include "shim.h"
import "C"
import (
"errors"
"unsafe"
)
// DeriveSharedSecret derives a shared secret using a private key and a peer's
// public key.
// The specific algorithm that is used depends on the types of the
// keys, but it is most commonly a variant of Diffie-Hellman.
func DeriveSharedSecret(private PrivateKey, public PublicKey) ([]byte, error) {
// Create context for the shared secret derivation
dhCtx := C.EVP_PKEY_CTX_new(private.evpPKey(), nil)
if dhCtx == nil {
return nil, errors.New("failed creating shared secret derivation context")
}
defer C.EVP_PKEY_CTX_free(dhCtx)
// Initialize the context
rc := C.EVP_PKEY_derive_init(dhCtx)
if rc != 1 {
return nil, errors.New("failed initializing shared secret derivation context")
}
// Provide the peer's public key
rc = C.EVP_PKEY_derive_set_peer(dhCtx, public.evpPKey())
if rc != 1 {
return nil, errors.New("failed adding peer public key to context")
}
// Determine how large of a buffer we need for the shared secret
var buffLen C.size_t
rc = C.EVP_PKEY_derive(dhCtx, nil, &buffLen)
if rc != 1 {
return nil, errors.New("failed determining shared secret length")
}
// Allocate a buffer
buffer := C.X_OPENSSL_malloc(buffLen)
if buffer == nil {
return nil, errors.New("failed allocating buffer for shared secret")
}
defer C.X_OPENSSL_free(buffer)
// Derive the shared secret
rc = C.EVP_PKEY_derive(dhCtx, (*C.uchar)(buffer), &buffLen)
if rc != 1 {
return nil, errors.New("failed deriving the shared secret")
}
secret := C.GoBytes(unsafe.Pointer(buffer), C.int(buffLen))
return secret, nil
}

46
dh_test.go Normal file
View File

@ -0,0 +1,46 @@
// Copyright (C) 2017. See AUTHORS.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openssl
import (
"bytes"
"testing"
)
func TestECDH(t *testing.T) {
t.Parallel()
myKey, err := GenerateECKey(Prime256v1)
if err != nil {
t.Fatal(err)
}
peerKey, err := GenerateECKey(Prime256v1)
if err != nil {
t.Fatal(err)
}
mySecret, err := DeriveSharedSecret(myKey, peerKey)
if err != nil {
t.Fatal(err)
}
theirSecret, err := DeriveSharedSecret(peerKey, myKey)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(mySecret, theirSecret) != 0 {
t.Fatal("shared secrets are different")
}
}

4
shim.c
View File

@ -312,6 +312,10 @@ int X_shim_init() {
return 0;
}
void * X_OPENSSL_malloc(size_t size) {
return OPENSSL_malloc(size);
}
void X_OPENSSL_free(void *ref) {
OPENSSL_free(ref);
}

1
shim.h
View File

@ -42,6 +42,7 @@ extern int X_shim_init();
/* Library methods */
extern void X_OPENSSL_free(void *ref);
extern void *X_OPENSSL_malloc(size_t size);
/* SSL methods */
extern long X_SSL_set_options(SSL* ssl, long options);