Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
ctidh_cgo
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
ctidh_cgo
Commits
2e7fb078
Commit
2e7fb078
authored
2 years ago
by
David Stainton
Browse files
Options
Downloads
Patches
Plain Diff
Add GenerateKeyPairWithRNG
parent
051a023d
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
binding.go
+55
-2
55 additions, 2 deletions
binding.go
binding_test.go
+11
-0
11 additions, 0 deletions
binding_test.go
with
66 additions
and
2 deletions
binding.go
+
55
−
2
View file @
2e7fb078
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
)
...
...
This diff is collapsed.
Click to expand it.
binding_test.go
+
11
−
0
View file @
2e7fb078
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
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment