Formatting
This commit is contained in:
parent
16f0f5b588
commit
f143e36c59
@ -1,41 +1,44 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/henrydcase/nobs/dh/csidh"
|
"github.com/henrydcase/nobs/dh/csidh"
|
||||||
"github.com/henrydcase/sidh_torture/csidh/ref/go-wrapper"
|
"github.com/henrydcase/sidh_torture/csidh/ref/go-wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Possible values for "Status"
|
// Possible values for "Status"
|
||||||
const (
|
const (
|
||||||
Valid = iota // Indicates that shared secret must be agreed correctly
|
Valid = iota // Indicates that shared secret must be agreed correctly
|
||||||
InvalidSharedSecret // Calculated shared secret must be different than test vector
|
ValidPublicKey2 // Public key 2 must succeed validation
|
||||||
InvalidPublicKey1 // Public key 1 generated from private key must be different than test vector
|
InvalidSharedSecret // Calculated shared secret must be different than test vector
|
||||||
InvalidPublicKey2 // Public key 2 must fail validation
|
InvalidPublicKey1 // Public key 1 generated from private key must be different than test vector
|
||||||
|
InvalidPublicKey2 // Public key 2 must fail validation
|
||||||
)
|
)
|
||||||
|
|
||||||
var StatusValues = map[int]string{
|
var StatusValues = map[int]string{
|
||||||
Valid: "valid",
|
Valid: "valid",
|
||||||
InvalidSharedSecret: "invalid_shared_secret",
|
ValidPublicKey2: "valid_public_key2",
|
||||||
InvalidPublicKey1: "invalid_public_key1",
|
InvalidSharedSecret: "invalid_shared_secret",
|
||||||
InvalidPublicKey2: "invalid_public_key2",
|
InvalidPublicKey1: "invalid_public_key1",
|
||||||
|
InvalidPublicKey2: "invalid_public_key2",
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestVector struct {
|
type TestVector struct {
|
||||||
Id int `json:"Id"`
|
Id int `json:"Id"`
|
||||||
Pk1 string `json:"Pk1"`
|
Pk1 string `json:"Pk1"`
|
||||||
Pr1 string `json:"Pr1"`
|
Pr1 string `json:"Pr1"`
|
||||||
Pk2 string `json:"Pk2"`
|
Pk2 string `json:"Pk2"`
|
||||||
Ss string `json:"Ss"`
|
Ss string `json:"Ss"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
|
Comment string `json:"comment"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestVectors struct {
|
type TestVectors struct {
|
||||||
Vectors []TestVector `json:"Vectors"`
|
Vectors []TestVector `json:"Vectors"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// R is a reference to C implementation.
|
// R is a reference to C implementation.
|
||||||
@ -44,139 +47,142 @@ var R wrapper.Ref
|
|||||||
// createValid creates 'num' of TestVector's. Each vector contains
|
// createValid creates 'num' of TestVector's. Each vector contains
|
||||||
// valid data.
|
// valid data.
|
||||||
func createValid(num int) TestVector {
|
func createValid(num int) TestVector {
|
||||||
var tv TestVector
|
var tv TestVector
|
||||||
var ss [csidh.SharedSecretSize]byte
|
var ss [csidh.SharedSecretSize]byte
|
||||||
var pk [csidh.PublicKeySize]byte
|
var pk [csidh.PublicKeySize]byte
|
||||||
var pr [csidh.PrivateKeySize]byte
|
var pr [csidh.PrivateKeySize]byte
|
||||||
|
|
||||||
prA := R.KeygenPrv()
|
prA := R.KeygenPrv()
|
||||||
pkA := R.KeygenPub(&prA)
|
pkA := R.KeygenPub(&prA)
|
||||||
prB := R.KeygenPrv()
|
prB := R.KeygenPrv()
|
||||||
pkB := R.KeygenPub(&prB)
|
pkB := R.KeygenPub(&prB)
|
||||||
|
|
||||||
R.Derive(ss[:], &pkB, &prA)
|
R.Derive(ss[:], &pkB, &prA)
|
||||||
|
|
||||||
tv.Id = num
|
tv.Id = num
|
||||||
tv.Status = StatusValues[Valid]
|
tv.Status = StatusValues[Valid]
|
||||||
|
|
||||||
tv.Ss = hex.EncodeToString(ss[:])
|
tv.Ss = hex.EncodeToString(ss[:])
|
||||||
|
|
||||||
prA.Export(pr[:])
|
prA.Export(pr[:])
|
||||||
tv.Pr1 = hex.EncodeToString(pr[:])
|
tv.Pr1 = hex.EncodeToString(pr[:])
|
||||||
|
|
||||||
pkA.Export(pk[:])
|
pkA.Export(pk[:])
|
||||||
tv.Pk1 = hex.EncodeToString(pk[:])
|
tv.Pk1 = hex.EncodeToString(pk[:])
|
||||||
|
|
||||||
for i, _ := range pk {
|
for i, _ := range pk {
|
||||||
pk[i] = 0
|
pk[i] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
pkB.Export(pk[:])
|
pkB.Export(pk[:])
|
||||||
tv.Pk2 = hex.EncodeToString(pk[:])
|
tv.Pk2 = hex.EncodeToString(pk[:])
|
||||||
|
|
||||||
return tv
|
return tv
|
||||||
}
|
}
|
||||||
|
|
||||||
func createNegativeSharedSecret(vectors *TestVectors) {
|
func createNegativeSharedSecret(vectors *TestVectors) {
|
||||||
n := len(vectors.Vectors)
|
n := len(vectors.Vectors)
|
||||||
tv := createValid(n)
|
tv := createValid(n)
|
||||||
ss, err := hex.DecodeString(tv.Ss)
|
ss, err := hex.DecodeString(tv.Ss)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Can't decode shared secret")
|
panic("Can't decode shared secret")
|
||||||
}
|
}
|
||||||
|
|
||||||
for i:=0; i<csidh.SharedSecretSize; i++ {
|
for i := 0; i < csidh.SharedSecretSize; i++ {
|
||||||
var newSs [csidh.SharedSecretSize]byte
|
var newSs [csidh.SharedSecretSize]byte
|
||||||
copy(newSs[:], ss[:])
|
copy(newSs[:], ss[:])
|
||||||
newSs[i] = ss[i]^ss[(i+1)%csidh.SharedSecretSize]
|
newSs[i] = ss[i] ^ ss[(i+1)%csidh.SharedSecretSize]
|
||||||
|
|
||||||
if bytes.Equal(newSs[:], ss) {
|
if bytes.Equal(newSs[:], ss) {
|
||||||
tv.Status = StatusValues[Valid]
|
tv.Status = StatusValues[Valid]
|
||||||
} else {
|
} else {
|
||||||
tv.Status = StatusValues[InvalidSharedSecret]
|
tv.Status = StatusValues[InvalidSharedSecret]
|
||||||
}
|
}
|
||||||
tv.Ss = hex.EncodeToString(newSs[:])
|
tv.Ss = hex.EncodeToString(newSs[:])
|
||||||
tv.Id = n + i
|
tv.Id = n + i
|
||||||
vectors.Vectors = append(vectors.Vectors, tv)
|
vectors.Vectors = append(vectors.Vectors, tv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Public key validation fails
|
// Public key validation fails
|
||||||
func createNegativePk2(vectors *TestVectors) {
|
func createNegativePk2(vectors *TestVectors) {
|
||||||
n := len(vectors.Vectors)
|
n := len(vectors.Vectors)
|
||||||
tv := createValid(n)
|
tv := createValid(n)
|
||||||
pk, err := hex.DecodeString(tv.Pk2)
|
pk, err := hex.DecodeString(tv.Pk2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Can't decode public key 2")
|
panic("Can't decode public key 2")
|
||||||
}
|
}
|
||||||
|
|
||||||
for i:=0; i<csidh.PublicKeySize; i++ {
|
for i := 0; i < csidh.PublicKeySize; i++ {
|
||||||
var newPk [csidh.PublicKeySize]byte
|
var newPk [csidh.PublicKeySize]byte
|
||||||
|
|
||||||
// Modify good public key so it's probably no longer valid
|
// Modify good public key so it's probably no longer valid
|
||||||
copy(newPk[:], pk[:])
|
copy(newPk[:], pk[:])
|
||||||
newPk[i] = pk[i]^pk[(i+1)%csidh.PublicKeySize]
|
newPk[i] = pk[i] ^ pk[(i+1)%csidh.PublicKeySize]
|
||||||
|
|
||||||
// Try to validate and set the status
|
// Try to validate and set the status
|
||||||
if R.Validate(newPk[:]) {
|
if R.Validate(newPk[:]) {
|
||||||
tv.Status = StatusValues[Valid]
|
tv.Status = StatusValues[Valid]
|
||||||
} else {
|
} else {
|
||||||
tv.Status = StatusValues[InvalidPublicKey2]
|
tv.Status = StatusValues[InvalidPublicKey2]
|
||||||
}
|
}
|
||||||
tv.Pk2 = hex.EncodeToString(newPk[:])
|
tv.Pk2 = hex.EncodeToString(newPk[:])
|
||||||
tv.Id = n + i
|
tv.Id = n + i
|
||||||
vectors.Vectors = append(vectors.Vectors, tv)
|
vectors.Vectors = append(vectors.Vectors, tv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private key doesn't correspond to public key
|
// Private key doesn't correspond to public key
|
||||||
func createNegativePrivateKey(vectors *TestVectors, num int) {
|
func createNegativePrivateKey(vectors *TestVectors, num int) {
|
||||||
n := len(vectors.Vectors)
|
n := len(vectors.Vectors)
|
||||||
for i:=0; i<num; i++ {
|
for i := 0; i < num; i++ {
|
||||||
var tv TestVector
|
var tv TestVector
|
||||||
var pkBytes1 [csidh.PublicKeySize]byte
|
var pkBytes1 [csidh.PublicKeySize]byte
|
||||||
var pkBytes2 [csidh.PublicKeySize]byte
|
var pkBytes2 [csidh.PublicKeySize]byte
|
||||||
var prBytes [csidh.PrivateKeySize]byte
|
var prBytes [csidh.PrivateKeySize]byte
|
||||||
|
|
||||||
pr1 := R.KeygenPrv()
|
pr1 := R.KeygenPrv()
|
||||||
pk1 := R.KeygenPub(&pr1)
|
pk1 := R.KeygenPub(&pr1)
|
||||||
|
|
||||||
// Store private key 1
|
// Store private key 1
|
||||||
pr1.Export(prBytes[:])
|
pr1.Export(prBytes[:])
|
||||||
tv.Pr1 = hex.EncodeToString(prBytes[:])
|
tv.Pr1 = hex.EncodeToString(prBytes[:])
|
||||||
|
|
||||||
// Generate public key which doesn't correspond to pr1
|
// Generate public key which doesn't correspond to pr1
|
||||||
pr2 := R.KeygenPrv()
|
pr2 := R.KeygenPrv()
|
||||||
pk2 := R.KeygenPub(&pr2)
|
pk2 := R.KeygenPub(&pr2)
|
||||||
|
|
||||||
pk1.Export(pkBytes1[:])
|
pk1.Export(pkBytes1[:])
|
||||||
pk2.Export(pkBytes2[:])
|
pk2.Export(pkBytes2[:])
|
||||||
if bytes.Equal(pkBytes1[:], pkBytes2[:]) {
|
if bytes.Equal(pkBytes1[:], pkBytes2[:]) {
|
||||||
tv.Status = StatusValues[Valid]
|
tv.Status = StatusValues[Valid]
|
||||||
} else {
|
} else {
|
||||||
tv.Status = StatusValues[InvalidPublicKey1]
|
tv.Status = StatusValues[InvalidPublicKey1]
|
||||||
}
|
}
|
||||||
|
|
||||||
tv.Id = n + i
|
tv.Id = n + i
|
||||||
tv.Pk1 = hex.EncodeToString(pkBytes2[:])
|
tv.Pk1 = hex.EncodeToString(pkBytes2[:])
|
||||||
|
|
||||||
vectors.Vectors = append(vectors.Vectors, tv)
|
vectors.Vectors = append(vectors.Vectors, tv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Produce test case for a Pk2 with all zeros and status valid_public_key2.
|
||||||
|
// comment should be "Zero key should validate correctly"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var vectors TestVectors
|
var vectors TestVectors
|
||||||
|
|
||||||
for i:=0; i<10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
vectors.Vectors = append(vectors.Vectors, createValid(i))
|
vectors.Vectors = append(vectors.Vectors, createValid(i))
|
||||||
}
|
}
|
||||||
createNegativeSharedSecret(&vectors)
|
createNegativeSharedSecret(&vectors)
|
||||||
createNegativePrivateKey(&vectors, 10)
|
createNegativePrivateKey(&vectors, 10)
|
||||||
createNegativePk2(&vectors)
|
createNegativePk2(&vectors)
|
||||||
|
|
||||||
marshalled, err := json.MarshalIndent(vectors, "", " ")
|
marshalled, err := json.MarshalIndent(vectors, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Error occured while Marshalling")
|
panic("Error occured while Marshalling")
|
||||||
}
|
}
|
||||||
ioutil.WriteFile("testvectors.dat", marshalled, 0644)
|
ioutil.WriteFile("testvectors.dat", marshalled, 0644)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user