Skip to content
Snippets Groups Projects
Commit 2e7fb078 authored by David Stainton's avatar David Stainton
Browse files

Add GenerateKeyPairWithRNG

parent 051a023d
No related branches found
No related tags found
No related merge requests found
package ctidh package ctidh
// #include "binding.h" /*
// #include <csidh.h> #include "binding.h"
#include <csidh.h>
void custom_gen_private(private_key *priv) {
csidh_private_withrng(priv, fillrandom_custom);
}
void fillrandom_custom(
void *const outptr,
const size_t outsz,
const uintptr_t context)
{
(void) context;
go_fillrandom(outptr, outsz);
}
*/
import "C" import "C"
import ( import (
"bytes" "bytes"
"crypto/hmac" "crypto/hmac"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"sync"
"unsafe" "unsafe"
) )
...@@ -33,6 +50,9 @@ var ( ...@@ -33,6 +50,9 @@ var (
// ErrCTIDH indicates a group action failure. // ErrCTIDH indicates a group action failure.
ErrCTIDH error = fmt.Errorf("%s: group action failure", Name()) ErrCTIDH error = fmt.Errorf("%s: group action failure", Name())
privateKeyRNGLock sync.Mutex
privateKeyRNG io.Reader = nil
) )
// ErrPEMKeyTypeMismatch returns an error indicating that we tried // ErrPEMKeyTypeMismatch returns an error indicating that we tried
...@@ -300,6 +320,39 @@ func GenerateKeyPair() (*PrivateKey, *PublicKey) { ...@@ -300,6 +320,39 @@ func GenerateKeyPair() (*PrivateKey, *PublicKey) {
return privKey, DerivePublicKey(privKey) return privKey, DerivePublicKey(privKey)
} }
//export go_fillrandom
func go_fillrandom(outptr unsafe.Pointer, outsz C.size_t) {
buf := make([]byte, outsz)
privateKeyRNGLock.Lock()
rng := privateKeyRNG
privateKeyRNGLock.Unlock()
count, err := rng.Read(buf)
if err != nil {
panic(err)
}
if count != int(outsz) {
panic("rng fail")
}
p := uintptr(outptr)
for i := 0; i < int(outsz); i++ {
(*(*uint8)(unsafe.Pointer(p))) = uint8(buf[i])
p += 1
}
}
// GenerateKeyPairWithRNG uses the given RNG to derive a new keypair.
// HOWEVER, if GenerateKeyPairWithRNG is called by multiple threads
// then the rng is overwritten with each call which can unintentionally
// cause multiple RNGs to be used to generate the keypair.
func GenerateKeyPairWithRNG(rng io.Reader) (*PrivateKey, *PublicKey) {
privKey := new(PrivateKey)
privateKeyRNGLock.Lock()
privateKeyRNG = rng
privateKeyRNGLock.Unlock()
C.custom_gen_private(&privKey.privateKey)
return privKey, DerivePublicKey(privKey)
}
func groupAction(privateKey *PrivateKey, publicKey *PublicKey) *PublicKey { func groupAction(privateKey *PrivateKey, publicKey *PublicKey) *PublicKey {
sharedKey := new(PublicKey) sharedKey := new(PublicKey)
ok := C.csidh(&sharedKey.publicKey, &publicKey.publicKey, &privateKey.privateKey) ok := C.csidh(&sharedKey.publicKey, &publicKey.publicKey, &privateKey.privateKey)
......
package ctidh package ctidh
import ( import (
"crypto/rand"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
...@@ -8,6 +9,16 @@ import ( ...@@ -8,6 +9,16 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestGenerateKeyPairWithRNG(t *testing.T) {
privateKey, publicKey := GenerateKeyPairWithRNG(rand.Reader)
zeros := make([]byte, PublicKeySize)
require.NotEqual(t, privateKey.Bytes(), zeros)
require.NotEqual(t, publicKey.Bytes(), zeros)
t.Logf("privateKey.Bytes() %x", privateKey.Bytes())
}
func TestPrivateKeyPEMSerialization(t *testing.T) { func TestPrivateKeyPEMSerialization(t *testing.T) {
privateKey, _ := GenerateKeyPair() privateKey, _ := GenerateKeyPair()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment