Adds KAT tests
This commit is contained in:
rodzic
a50c2a0591
commit
a3214a4e0f
121
sike_test.go
121
sike_test.go
@ -5,13 +5,16 @@ import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"math/big"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var tdata = struct {
|
||||
name string
|
||||
katFile string
|
||||
PrB_sidh string
|
||||
PkB_sidh string
|
||||
PkB_sike string
|
||||
@ -20,6 +23,7 @@ var tdata = struct {
|
||||
PkA_sike string
|
||||
}{
|
||||
name: "P-503",
|
||||
katFile: "etc/PQCkemKAT_434.rsp",
|
||||
PkB_sike: "68460C22466E95864CFEA7B5D9077E768FF4F9ED69AE56D7CF3F236FB06B31020EEE34B5B572CEA5DDF20B531966AA8F5F3ACC0C6D1CE04EEDC30FD1F1233E2D96FE60C6D638FC646EAF2E2246F1AEC96859CE874A1F029A78F9C978CD6B22114A0D5AB20101191FD923E80C76908B1498B9D0200065CCA09159A0C65A1E346CC6470314FE78388DAA89DD08EC67DBE63C1F606674ACC49EBF9FDBB2B898B3CE733113AA6F942DB401A76D629CE6EE6C0FDAF4CFB1A5E366DB66C17B3923A1B7FB26A3FF25B9018869C674D3DEF4AF269901D686FE4647F9D2CDB2CEB3AFA305B27C885F037ED167F595066C21E7DD467D8332B934A5102DA5F13332DFA356B82156A0BB2E7E91C6B85B7D1E381BC9E3F0FC4DB9C36016D9ECEC415D7E977E9AC29910D934BA2FE4EE49D3B387607A4E1AFABF495FB86A77194626589E802FF5167C7A25C542C1EAD25A6E0AA931D94F2F9AFD3DBDF222E651F729A90E77B20974905F1E65E041CE6C95AAB3E1F22D332E0A5DE9C5DB3D9C7A38",
|
||||
PrB_sike: "80FC55DA74DEFE3113487B80841E678AF9ED4E0599CF07353A4AB93971C090A0" +
|
||||
"A9402C9DC98AC6DC8F5FDE5E970AE22BA48A400EFC72851C",
|
||||
@ -451,26 +455,7 @@ func TestNegativeKEMSameWrongResult(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func readAndCheckLine(r *bufio.Reader) []byte {
|
||||
// Read next line from buffer
|
||||
line, isPrefix, err := r.ReadLine()
|
||||
if err != nil || isPrefix {
|
||||
panic("Wrong format of input file")
|
||||
}
|
||||
|
||||
// Function expects that line is in format "KEY = HEX_VALUE". Get
|
||||
// value, which should be a hex string
|
||||
hexst := strings.Split(string(line), "=")[1]
|
||||
hexst = strings.TrimSpace(hexst)
|
||||
// Convert value to byte string
|
||||
ret, err := hex.DecodeString(hexst)
|
||||
if err != nil {
|
||||
panic("Wrong format of input file")
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func testKeygenSIKE(pk, sk []byte, id uint8) bool {
|
||||
func testKeygen(t *testing.T, pk, sk []byte) {
|
||||
// Import provided private key
|
||||
var prvKey = NewPrivateKey(KeyVariant_SIKE)
|
||||
if prvKey.Import(sk) != nil {
|
||||
@ -479,25 +464,9 @@ func testKeygenSIKE(pk, sk []byte, id uint8) bool {
|
||||
|
||||
// Generate public key
|
||||
pubKey := prvKey.GeneratePublicKey()
|
||||
return bytes.Equal(pubKey.Export(), pk)
|
||||
}
|
||||
|
||||
func testDecapsulation(pk, sk, ct, ssExpected []byte, id uint8) bool {
|
||||
var pubKey = NewPublicKey(KeyVariant_SIKE)
|
||||
var prvKey = NewPrivateKey(KeyVariant_SIKE)
|
||||
if pubKey.Import(pk) != nil || prvKey.Import(sk) != nil {
|
||||
panic("sike test: can't load KAT")
|
||||
if !bytes.Equal(pubKey.Export(), pk) {
|
||||
t.Fatalf("KAT keygen form private failed\n")
|
||||
}
|
||||
|
||||
ssGot, err := Decapsulate(prvKey, pubKey, ct)
|
||||
if err != nil {
|
||||
panic("sike test: can't perform degcapsulation KAT")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return bytes.Equal(ssGot, ssExpected)
|
||||
}
|
||||
|
||||
func TestKeyAgreement(t *testing.T) {
|
||||
@ -614,6 +583,82 @@ func TestDecapsulation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func readAndCheckLine(r *bufio.Reader) []byte {
|
||||
// Read next line from buffer
|
||||
line, isPrefix, err := r.ReadLine()
|
||||
if err != nil || isPrefix {
|
||||
panic("Wrong format of input file")
|
||||
}
|
||||
|
||||
// Function expects that line is in format "KEY = HEX_VALUE". Get
|
||||
// value, which should be a hex string
|
||||
hexst := strings.Split(string(line), "=")[1]
|
||||
hexst = strings.TrimSpace(hexst)
|
||||
// Convert value to byte string
|
||||
ret, err := hex.DecodeString(hexst)
|
||||
if err != nil {
|
||||
panic("Wrong format of input file")
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func TestKAT(t *testing.T) {
|
||||
testDecapsulation := func(t *testing.T, pk, sk, ct, ssExpected []byte) {
|
||||
var pubKey = NewPublicKey(KeyVariant_SIKE)
|
||||
var prvKey = NewPrivateKey(KeyVariant_SIKE)
|
||||
if pubKey.Import(pk) != nil || prvKey.Import(sk) != nil {
|
||||
panic("sike test: can't load KAT")
|
||||
}
|
||||
|
||||
ssGot, err := Decapsulate(prvKey, pubKey, ct)
|
||||
if err != nil {
|
||||
panic("sike test: can't perform degcapsulation KAT")
|
||||
}
|
||||
|
||||
if (err != nil) || !bytes.Equal(ssGot, ssExpected) {
|
||||
t.Fatalf("KAT decapsulation failed\n")
|
||||
}
|
||||
}
|
||||
|
||||
f, err := os.Open(tdata.katFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := bufio.NewReader(f)
|
||||
for {
|
||||
line, isPrefix, err := r.ReadLine()
|
||||
if err != nil || isPrefix {
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if len(strings.TrimSpace(string(line))) == 0 || line[0] == '#' {
|
||||
continue
|
||||
}
|
||||
|
||||
// count
|
||||
_ = strings.Split(string(line), "=")[1]
|
||||
// seed
|
||||
_ = readAndCheckLine(r)
|
||||
// pk
|
||||
pk := readAndCheckLine(r)
|
||||
// sk (secret key in test vector is concatenation of
|
||||
// MSG + SECRET_BOB_KEY + PUBLIC_BOB_KEY. We use only MSG+SECRET_BOB_KEY
|
||||
sk := readAndCheckLine(r)
|
||||
sk = sk[:Params.MsgLen+int(Params.B.SecretByteLen)]
|
||||
// ct
|
||||
ct := readAndCheckLine(r)
|
||||
// ss
|
||||
ss := readAndCheckLine(r)
|
||||
testKeygen(t, pk, sk)
|
||||
testDecapsulation(t, pk, sk, ct, ss)
|
||||
testKEMRoundTrip(t, pk, sk)
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
Benchmarking
|
||||
-------------------------------------------------------------------------*/
|
||||
|
Ładowanie…
Reference in New Issue
Block a user