Skip to content
Snippets Groups Projects
Commit e2486651 authored by Bernardo Cardoso's avatar Bernardo Cardoso
Browse files

Refactor crypto tests using Random

parent 05761e4d
No related branches found
No related tags found
No related merge requests found
...@@ -35,11 +35,7 @@ func TestDHKX(t *testing.T) { ...@@ -35,11 +35,7 @@ func TestDHKX(t *testing.T) {
p.SetString(primeString, 16) p.SetString(primeString, 16)
g := large.NewInt(2) g := large.NewInt(2)
q := large.NewInt(3) q := large.NewInt(3)
s := large.NewInt(0) grp := cyclic.NewGroup(p, g, q)
min := large.NewInt(2)
max := p
rng := cyclic.NewRandom(min, max)
grp := cyclic.NewGroup(p, s, g, q, rng)
testGroup := &grp testGroup := &grp
// Creation of two different DH Key Pairs with valid parameters // Creation of two different DH Key Pairs with valid parameters
...@@ -89,12 +85,7 @@ func TestCreateDHKeyPair(t *testing.T) { ...@@ -89,12 +85,7 @@ func TestCreateDHKeyPair(t *testing.T) {
p := large.NewInt(4) p := large.NewInt(4)
g := large.NewInt(2) g := large.NewInt(2)
q := large.NewInt(3) q := large.NewInt(3)
s := large.NewInt(0) grp := cyclic.NewGroup(p, g, q)
min := large.NewInt(2)
max := p
rng := cyclic.NewRandom(min, max)
grp := cyclic.NewGroup(p, s, g, q, rng)
testGroup := &grp testGroup := &grp
defer Catch("TestCreateDHKeyPair():", t) defer Catch("TestCreateDHKeyPair():", t)
...@@ -121,12 +112,7 @@ func TestCheckPublicKey(t *testing.T) { ...@@ -121,12 +112,7 @@ func TestCheckPublicKey(t *testing.T) {
p.SetString(primeString, 16) p.SetString(primeString, 16)
g := large.NewInt(2) g := large.NewInt(2)
q := large.NewInt(3) q := large.NewInt(3)
s := large.NewInt(0) grp := cyclic.NewGroup(p, g, q)
min := large.NewInt(2)
max := p
rng := cyclic.NewRandom(min, max)
grp := cyclic.NewGroup(p, s, g, q, rng)
testGroup := &grp testGroup := &grp
// Creation of a DH Key Pair with valid parameters // Creation of a DH Key Pair with valid parameters
...@@ -191,12 +177,7 @@ func TestDHNodeKeys(t *testing.T) { ...@@ -191,12 +177,7 @@ func TestDHNodeKeys(t *testing.T) {
p.SetString(primeString, 16) p.SetString(primeString, 16)
g := large.NewInt(2) g := large.NewInt(2)
q := large.NewInt(3) q := large.NewInt(3)
s := large.NewInt(0) grp := cyclic.NewGroup(p, g, q)
min := large.NewInt(2)
max := p
rng := cyclic.NewRandom(min, max)
grp := cyclic.NewGroup(p, s, g, q, rng)
testGroup := &grp testGroup := &grp
// This is a map key(string) -> value (hex string) // This is a map key(string) -> value (hex string)
......
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"gitlab.com/elixxir/crypto/csprng" "gitlab.com/elixxir/crypto/csprng"
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/large" "gitlab.com/elixxir/crypto/large"
"testing" "testing"
) )
...@@ -396,27 +395,16 @@ func TestAESEnc_Hash(t *testing.T) { ...@@ -396,27 +395,16 @@ func TestAESEnc_Hash(t *testing.T) {
// Loop test AES encryption/decryption with random inputs // Loop test AES encryption/decryption with random inputs
func TestEncDecAES_Random(t *testing.T) { func TestEncDecAES_Random(t *testing.T) {
keyGen := cyclic.NewRandom(large.NewInt(28), large.NewInt(512)) rng := csprng.NewSystemRNG()
textGen := cyclic.NewRandom(large.NewInt(1), large.NewInt(4096)) key := make([]byte, 256)
randSize := large.NewInt(1) plaintext := make([]byte, 2048)
tests := NUM_TESTS tests := NUM_TESTS
pass := 0 pass := 0
for i := 0; i < tests; i++ { for i := 0; i < tests; i++ {
keyGen.Rand(randSize) rng.Read(key)
rng.Read(plaintext)
csprig := csprng.NewSystemRNG()
key := make([]byte, randSize.Int64())
csprig.Read(key)
textGen.Rand(randSize)
plaintext := make([]byte, randSize.Int64())
csprig.Read(plaintext)
ciphertext, err := EncryptAES256(key, plaintext) ciphertext, err := EncryptAES256(key, plaintext)
...@@ -444,13 +432,12 @@ func TestEncDecAES_AllPaddings(t *testing.T) { ...@@ -444,13 +432,12 @@ func TestEncDecAES_AllPaddings(t *testing.T) {
tests := NUM_TESTS tests := NUM_TESTS
pass := 0 pass := 0
for i := 1; i <= NUM_TESTS; i++ { rng := csprng.NewSystemRNG()
csprig := csprng.NewSystemRNG() for i := 1; i <= NUM_TESTS; i++ {
plaintext := make([]byte, i) plaintext := make([]byte, i)
rng.Read(plaintext)
csprig.Read(plaintext)
ciphertext, err := EncryptAES256(key, plaintext) ciphertext, err := EncryptAES256(key, plaintext)
......
...@@ -28,14 +28,9 @@ func TestMain(m *testing.M) { ...@@ -28,14 +28,9 @@ func TestMain(m *testing.M) {
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" +
"15728E5A8AACAA68FFFFFFFFFFFFFFFF" "15728E5A8AACAA68FFFFFFFFFFFFFFFF"
p := large.NewIntFromString(primeString, 16) p := large.NewIntFromString(primeString, 16)
min := large.NewInt(2)
max := large.NewInt(0)
max.Mul(p, large.NewInt(1000))
seed := large.NewInt(42)
rng := cyclic.NewRandom(min, max)
g := large.NewInt(2) g := large.NewInt(2)
q := large.NewInt(3) q := large.NewInt(3)
grp = cyclic.NewGroup(p, seed, g, q, rng) grp = cyclic.NewGroup(p, g, q)
os.Exit(m.Run()) os.Exit(m.Run())
} }
......
...@@ -26,14 +26,9 @@ func TestKeygen(t *testing.T) { ...@@ -26,14 +26,9 @@ func TestKeygen(t *testing.T) {
"15728E5A8AACAA68FFFFFFFFFFFFFFFF" "15728E5A8AACAA68FFFFFFFFFFFFFFFF"
p := large.NewIntFromString(primeString, 16) p := large.NewIntFromString(primeString, 16)
min := large.NewInt(2)
max := large.NewInt(0)
max.Mul(p, large.NewInt(1000))
seed := large.NewInt(42)
rng := cyclic.NewRandom(min, max)
g := large.NewInt(2) g := large.NewInt(2)
q := large.NewInt(3) q := large.NewInt(3)
grp := cyclic.NewGroup(p, seed, g, q, rng) grp := cyclic.NewGroup(p, g, q)
testGroup := &grp testGroup := &grp
salt := grp.NewInt(1) salt := grp.NewInt(1)
......
...@@ -27,13 +27,9 @@ var primeString = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + ...@@ -27,13 +27,9 @@ var primeString = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" +
"15728E5A8AACAA68FFFFFFFFFFFFFFFF" "15728E5A8AACAA68FFFFFFFFFFFFFFFF"
var p = large.NewIntFromString(primeString, 16) var p = large.NewIntFromString(primeString, 16)
var min = large.NewInt(2)
var max = large.NewInt(0)
var seed = large.NewInt(42)
var rng = cyclic.NewRandom(min, max.Mul(p, large.NewInt(1000)))
var g = large.NewInt(2) var g = large.NewInt(2)
var q = large.NewInt(3) var q = large.NewInt(3)
var grp = cyclic.NewGroup(p, seed, g, q, rng) var grp = cyclic.NewGroup(p, g, q)
//TestExpandKey verifies ExpandKey with two different hashes //TestExpandKey verifies ExpandKey with two different hashes
func TestExpandKey(t *testing.T) { func TestExpandKey(t *testing.T) {
......
...@@ -35,10 +35,7 @@ var primeStrng = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + ...@@ -35,10 +35,7 @@ var primeStrng = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" +
"93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" +
"FFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFF"
var prime = large.NewIntFromString(primeStrng, 16) var prime = large.NewIntFromString(primeStrng, 16)
var rng = cyclic.NewRandom(large.NewInt(0), var grp = cyclic.NewGroup(prime, large.NewInt(4), large.NewInt(3))
large.NewIntFromString(primeStrng, 16))
var grp = cyclic.NewGroup(prime, large.NewInt(5), large.NewInt(4), large.NewInt(3),
rng)
var baseKey = grp.NewIntFromString("a906df88f30d6afbfa6165a50cc9e208d16b34e70b367068dc5d6bd6e155b2c3", 16) var baseKey = grp.NewIntFromString("a906df88f30d6afbfa6165a50cc9e208d16b34e70b367068dc5d6bd6e155b2c3", 16)
var salt = []byte("fdecfa52a8ad1688dbfa7d16df74ebf27e535903c469cefc007ebbe1ee895064") var salt = []byte("fdecfa52a8ad1688dbfa7d16df74ebf27e535903c469cefc007ebbe1ee895064")
var expectStr = "2e4c99e14e0b1cd18c08467c395a4d5c0eb594507595041a5cfa83eb2f5791f3" + var expectStr = "2e4c99e14e0b1cd18c08467c395a4d5c0eb594507595041a5cfa83eb2f5791f3" +
......
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
"gitlab.com/elixxir/crypto/csprng" "gitlab.com/elixxir/crypto/csprng"
"gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/hash" "gitlab.com/elixxir/crypto/hash"
"gitlab.com/elixxir/crypto/large"
"gitlab.com/elixxir/crypto/signature" "gitlab.com/elixxir/crypto/signature"
"testing" "testing"
) )
...@@ -25,15 +24,10 @@ var grp cyclic.Group ...@@ -25,15 +24,10 @@ var grp cyclic.Group
// Also confirm that base transmission and reception keys are different // Also confirm that base transmission and reception keys are different
func TestGenerateBaseKey(t *testing.T) { func TestGenerateBaseKey(t *testing.T) {
dsaParams := signature.GetDefaultDSAParams() dsaParams := signature.GetDefaultDSAParams()
p := dsaParams.GetP() grp = cyclic.NewGroup(
min := large.NewInt(2) dsaParams.GetP(),
max := large.NewInt(0)
max.Mul(p, large.NewInt(1000))
seed := large.NewInt(42)
grp = cyclic.NewGroup(p, seed,
dsaParams.GetG(), dsaParams.GetG(),
dsaParams.GetQ(), dsaParams.GetQ())
cyclic.NewRandom(min, max))
rng := csprng.NewSystemRNG() rng := csprng.NewSystemRNG()
ownPrivKey := dsaParams.PrivateKeyGen(rng) ownPrivKey := dsaParams.PrivateKeyGen(rng)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment