Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
crypto
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
crypto
Commits
8d13b999
Commit
8d13b999
authored
Aug 19, 2020
by
Benjamin Wenger
Browse files
Options
Downloads
Patches
Plain Diff
minor changes per comments
parent
e1ab565c
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
diffieHellman/dhkx.go
+7
-6
7 additions, 6 deletions
diffieHellman/dhkx.go
diffieHellman/dhkx_test.go
+7
-7
7 additions, 7 deletions
diffieHellman/dhkx_test.go
with
14 additions
and
13 deletions
diffieHellman/dhkx.go
+
7
−
6
View file @
8d13b999
...
@@ -9,29 +9,30 @@
...
@@ -9,29 +9,30 @@
package
diffieHellman
package
diffieHellman
import
(
import
(
"
github.com/pkg/errors
"
"
fmt
"
"gitlab.com/elixxir/crypto/csprng"
"gitlab.com/elixxir/crypto/csprng"
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/cyclic"
)
)
const
DefaultPrivateKeyLength
=
256
const
DefaultPrivateKeyLengthBits
=
256
const
DefaultPrivateKeyLength
=
DefaultPrivateKeyLengthBits
/
8
// Creates a private key of the passed length in bits in the given group using
// Creates a private key of the passed length in bits in the given group using
// the passed csprng. The length of the key must be within the prime of the
// the passed csprng. The length of the key must be within the prime of the
// group. It is recommended to use the "DefaultPrivateKeyLength"
// group. It is recommended to use the "DefaultPrivateKeyLength"
// for most use cases.
// for most use cases.
// key size must be divisible by 8
// key size must be divisible by 8
func
GeneratePrivateKey
(
size
u
int
,
group
*
cyclic
.
Group
,
source
csprng
.
Source
)
(
*
cyclic
.
Int
,
error
)
{
func
GeneratePrivateKey
(
size
int
,
group
*
cyclic
.
Group
,
source
csprng
.
Source
)
*
cyclic
.
Int
{
k1
,
err
:=
csprng
.
GenerateInGroup
(
group
.
GetPBytes
(),
int
(
size
/
8
)
,
source
)
k1
,
err
:=
csprng
.
GenerateInGroup
(
group
.
GetPBytes
(),
size
,
source
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
errors
.
Error
f
(
"Failed to generate key: %s"
,
err
.
Error
())
panic
(
fmt
.
Sprint
f
(
"Failed to generate key: %s"
,
err
.
Error
())
)
}
}
privateKey
:=
group
.
NewIntFromBytes
(
k1
)
privateKey
:=
group
.
NewIntFromBytes
(
k1
)
return
privateKey
,
nil
return
privateKey
}
}
// Computes a public key for the given private key. The private key must be
// Computes a public key for the given private key. The private key must be
...
...
This diff is collapsed.
Click to expand it.
diffieHellman/dhkx_test.go
+
7
−
7
View file @
8d13b999
...
@@ -43,13 +43,13 @@ func TestGeneratePrivateKey(t *testing.T) {
...
@@ -43,13 +43,13 @@ func TestGeneratePrivateKey(t *testing.T) {
maxSize
:=
0
maxSize
:=
0
for
i
:=
0
;
i
<
numGenerations
;
i
++
{
for
i
:=
0
;
i
<
numGenerations
;
i
++
{
privKey
,
_
:=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
privKey
:=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
if
privKey
.
BitLen
()
>
maxSize
{
if
privKey
.
BitLen
()
>
maxSize
{
maxSize
=
privKey
.
BitLen
()
maxSize
=
privKey
.
BitLen
()
}
}
}
}
if
maxSize
!=
DefaultPrivateKeyLength
{
if
maxSize
!=
DefaultPrivateKeyLength
Bits
{
t
.
Errorf
(
"Generated Private Keys never met correct length: "
+
t
.
Errorf
(
"Generated Private Keys never met correct length: "
+
"Expected :%v, Received: %v"
,
DefaultPrivateKeyLength
,
maxSize
)
"Expected :%v, Received: %v"
,
DefaultPrivateKeyLength
,
maxSize
)
}
}
...
@@ -79,7 +79,7 @@ func TestGeneratePublicKey(t *testing.T) {
...
@@ -79,7 +79,7 @@ func TestGeneratePublicKey(t *testing.T) {
for
i
:=
0
;
i
<
numTests
;
i
++
{
for
i
:=
0
;
i
<
numTests
;
i
++
{
//create public key
//create public key
privKey
,
_
:=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
privKey
:=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
publicKey
:=
GeneratePublicKey
(
privKey
,
grp
)
publicKey
:=
GeneratePublicKey
(
privKey
,
grp
)
//create public key manually
//create public key manually
...
@@ -120,7 +120,7 @@ func TestGenerateSessionKey(t *testing.T) {
...
@@ -120,7 +120,7 @@ func TestGenerateSessionKey(t *testing.T) {
for
i
:=
0
;
i
<
numTests
;
i
++
{
for
i
:=
0
;
i
<
numTests
;
i
++
{
//create session key
//create session key
privKey
,
_
:=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
privKey
:=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
publicKey
:=
GeneratePublicKey
(
privKey
,
grp
)
publicKey
:=
GeneratePublicKey
(
privKey
,
grp
)
session
:=
GenerateSessionKey
(
privKey
,
publicKey
,
grp
)
session
:=
GenerateSessionKey
(
privKey
,
publicKey
,
grp
)
...
@@ -161,7 +161,7 @@ func TestCheckPublicKey(t *testing.T) {
...
@@ -161,7 +161,7 @@ func TestCheckPublicKey(t *testing.T) {
rng
:=
csprng
.
NewSystemRNG
()
rng
:=
csprng
.
NewSystemRNG
()
// Creation of a DH Key Pair with valid parameters
// Creation of a DH Key Pair with valid parameters
privKey
,
_
:=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
privKey
:=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
publicKey
:=
GeneratePublicKey
(
privKey
,
grp
)
publicKey
:=
GeneratePublicKey
(
privKey
,
grp
)
// Random 2048 bit number that is not a quadratic residue
// Random 2048 bit number that is not a quadratic residue
...
@@ -220,8 +220,8 @@ func BenchmarkCreateDHSessionKey(b *testing.B) {
...
@@ -220,8 +220,8 @@ func BenchmarkCreateDHSessionKey(b *testing.B) {
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
// Creation of two different DH Key Pairs with valid parameters
// Creation of two different DH Key Pairs with valid parameters
privkeys
[
i
]
,
_
=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
privkeys
[
i
]
=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
tmpPrivKey
,
_
:=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
tmpPrivKey
:=
GeneratePrivateKey
(
DefaultPrivateKeyLength
,
grp
,
rng
)
pubkeys
[
i
]
=
GeneratePublicKey
(
tmpPrivKey
,
grp
)
pubkeys
[
i
]
=
GeneratePublicKey
(
tmpPrivKey
,
grp
)
}
}
...
...
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