go-sike/p434/sike_test.go

754 líneas
23 KiB
Go

package sike
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/hex"
"io"
"math/big"
"os"
"strings"
"testing"
)
var tdata = struct {
name string
katFile string
PrA_sidh string
PkA_sidh string
PrB_sidh string
PkB_sidh string
PkB_sike string
PrB_sike string
}{
name: "SIKEp434",
katFile: "etc/PQCkemKAT_374.rsp",
PrA_sidh: "3A727E04EA9B7E2A766A6F846489E7E7B915263BCEED308BB10FC900",
PkA_sidh: "9E668D1E6750ED4B91EE052C32839CA9DD2E56D52BC24DECC950AAAD24CEED3F9049C77FE80F0B9B01E7F8DAD7833EEC2286544D6380009C379CDD3E7517CEF5E20EB01F8231D52FC30DC61D2F63FB357F85DC6396E8A95DB9740BD3A972C8DB7901B31F074CD3E45345CA78F900817130E688A29A7CF0073B5C00FF2C65FBE776918EF9BD8E75B29EF7FAB791969B60B0C5B37A8992EDEF95FA7BAC40A95DAFE02E237301FEE9A7A43FD0B73477E8035DD12B73FAFEF18D39904DDE3653A754F36BE1888F6607C6A7951349A414352CF31A29F2C40302DB406C48018C905EB9DC46AFBF42A9187A9BB9E51B587622A2862DC7D5CC598BF38ED6320FB51D8697AD3D7A72ABCC32A393F0133DA8DF5E253D9E00B760B2DF342FCE974DCFE946CFE4727783531882800F9E5DD594D6D5A6275EEFEF9713ED838F4A06BB34D7B8D46E0B385AAEA1C7963601",
PrB_sidh: "E37BFE55B43B32448F375903D8D226EC94ADBFEA1D2B3536EB987001",
PkB_sidh: "C9F73E4497AAA3FDF9EB688135866A8A83934BA10E273B8CC3808CF0C1F5FAB3E9BB295885881B73DEBC875670C0F51C4BB40DF5FEDE01B8AF32D1BF10508B8C17B2734EB93B2B7F5D84A4A0F2F816E9E2C32AC253C0B6025B124D05A87A9E2A8567930F44BAA14219B941B6B400B4AED1D796DA12A5A9F0B8F3F5EE9DD43F64CB24A3B1719DF278ADF56B5F3395187829DA2319DEABF6BBD6EDA244DE2B62CC5AC250C1009DD1CD4712B0B37406612AD002B5E51A62B51AC9C0374D143ABBBD58275FAFC4A5E959C54838C2D6D9FB43B7B2609061267B6A2E6C6D01D295C4223E0D3D7A4CDCFB28A7818A737935279751A6DD8290FD498D1F6AD5F4FFF6BDFA536713F509DCE8047252F1E7D0DD9FCC414C0070B5DCCE3665A21A032D7FBE749181032183AFAD240B7E671E87FBBEC3A8CA4C11AA7A9A23AC69AE2ACF54B664DECD27753D63508F1B02",
PrB_sike: "4B622DE1350119C45A9F2E2EF3DC5DF56A27FCDFCDDAF58CD69B903752D68C200934E160B234E49EDE247601",
PkB_sike: "1BD0A2E81307B6F96461317DDF535ACC0E59C742627BAE60D27605E10FAF722D22A73E184CB572A12E79DCD58C6B54FB01442114CBE9010B6CAEC25D04C16C5E42540C1524C545B8C67614ED4183C9FA5BD0BE45A7F89FBC770EE8E7E5E391C7EE6F35F74C29E6D9E35B1663DA01E48E9DEB2347512D366FDE505161677055E3EF23054D276E817E2C57025DA1C10D2461F68617F2D11256EEE4E2D7DBDF6C8E34F3A0FD00C625428CB41857002159DAB94267ABE42D630C6AAA91AF837C7A6740754EA6634C45454C51B0BB4D44C3CCCCE4B32C00901CF69C008D013348379B2F9837F428A01B6173584691F2A6F3A3C4CF487D20D261B36C8CDB1BC158E2A5162A9DA4F7A97AA0879B9897E2B6891B672201F9AEFBF799C27B2587120AC586A511360926FB7DA8EBF5CB5272F396AE06608422BE9792E2CE9BEF21BF55B7EFF8DC7EC8C99910D3F800",
}
/* -------------------------------------------------------------------------
Helpers
-------------------------------------------------------------------------*/
// Fail if err !=nil. Display msg as an error message
func checkErr(t testing.TB, err error, msg string) {
t.Helper()
if err != nil {
t.Error(msg)
}
}
// Utility used for running same test with all registered prime fields
type MultiIdTestingFunc func(testing.TB)
// Converts string to private key
func convToPrv(s string, v KeyVariant) *PrivateKey {
key := NewPrivateKey(v)
hex, e := hex.DecodeString(s)
if e != nil {
panic("non-hex number provided")
}
e = key.Import(hex)
if e != nil {
panic("Can't import private key")
}
return key
}
// Converts string to public key
func convToPub(s string, v KeyVariant) *PublicKey {
key := NewPublicKey(v)
hex, e := hex.DecodeString(s)
if e != nil {
panic("non-hex number provided")
}
e = key.Import(hex)
if e != nil {
panic("Can't import public key")
}
return key
}
/* -------------------------------------------------------------------------
Unit tests
-------------------------------------------------------------------------*/
func TestKeygen(t *testing.T) {
var keyExp, keyGot [55 * 2 * 3]byte
alicePrivate := convToPrv(tdata.PrA_sidh, KeyVariant_SIDH_A)
bobPrivate := convToPrv(tdata.PrB_sidh, KeyVariant_SIDH_B)
expPubA := convToPub(tdata.PkA_sidh, KeyVariant_SIDH_A)
expPubB := convToPub(tdata.PkB_sidh, KeyVariant_SIDH_B)
pubA := NewPublicKey(KeyVariant_SIDH_A)
alicePrivate.GeneratePublicKey(pubA)
pubB := NewPublicKey(KeyVariant_SIDH_B)
bobPrivate.GeneratePublicKey(pubB)
pubA.Export(keyExp[:])
expPubA.Export(keyGot[:])
if !bytes.Equal(keyExp[:], keyGot[:]) {
t.Fatalf("unexpected value of public key A [\nGot: %X\nExp: %X]\n",
keyExp, keyGot)
}
for i, _ := range keyExp {
keyExp[i] = 0
keyGot[i] = 0
}
pubB.Export(keyExp[:])
expPubB.Export(keyGot[:])
if !bytes.Equal(keyExp[:], keyGot[:]) {
t.Fatalf("unexpected value of public key B [\nGot: %X\nExp: %X]\n",
keyGot, keyExp)
}
}
func TestImportExport(t *testing.T) {
var err error
var aBytes, bBytes [110 * 3]byte
a := NewPublicKey(KeyVariant_SIDH_A)
b := NewPublicKey(KeyVariant_SIDH_B)
// Import keys
aHex, err := hex.DecodeString(tdata.PkA_sidh)
checkErr(t, err, "invalid hex-number provided")
err = a.Import(aHex)
checkErr(t, err, "import failed")
bHex, err := hex.DecodeString(tdata.PkB_sike)
checkErr(t, err, "invalid hex-number provided")
err = b.Import(bHex)
checkErr(t, err, "import failed")
a.Export(aBytes[:])
b.Export(bBytes[:])
// Export and check if same
if !bytes.Equal(bBytes[:], bHex) || !bytes.Equal(aBytes[:], aHex) {
t.Fatalf("export/import failed")
}
if (len(bBytes) != b.Size()) || (len(aBytes) != a.Size()) {
t.Fatalf("wrong size of exported keys")
}
}
func testPrivateKeyBelowMax(t testing.TB) {
for variant, keySz := range map[KeyVariant]*DomainParams{
KeyVariant_SIDH_A: &Params.A,
KeyVariant_SIDH_B: &Params.B} {
func(v KeyVariant, dp *DomainParams) {
var blen = int(dp.SecretByteLen)
var prv = NewPrivateKey(v)
var secretBytes = make([]byte, prv.Size())
// Calculate either (2^e2 - 1) or (2^s - 1); where s=ceil(log_2(3^e3)))
maxSecertVal := big.NewInt(int64(dp.SecretBitLen))
maxSecertVal.Exp(big.NewInt(int64(2)), maxSecertVal, nil)
maxSecertVal.Sub(maxSecertVal, big.NewInt(1))
// Do same test 1000 times
for i := 0; i < 1000; i++ {
err := prv.Generate(rand.Reader)
checkErr(t, err, "Private key generation")
// Convert to big-endian, as that's what expected by (*Int)SetBytes()
prv.Export(secretBytes)
for i := 0; i < int(blen/2); i++ {
tmp := secretBytes[i] ^ secretBytes[blen-i-1]
secretBytes[i] = tmp ^ secretBytes[i]
secretBytes[blen-i-1] = tmp ^ secretBytes[blen-i-1]
}
prvBig := new(big.Int).SetBytes(secretBytes)
// Check if generated key is bigger then acceptable
if prvBig.Cmp(maxSecertVal) == 1 {
t.Error("Generated private key is wrong")
}
}
}(variant, keySz)
}
}
func testKeyAgreement(t *testing.T, pkA, prA, pkB, prB string) {
var e error
var s1, s2 [110 * 3]byte
// KeyPairs
alicePublic := convToPub(pkA, KeyVariant_SIDH_A)
bobPublic := convToPub(pkB, KeyVariant_SIDH_B)
alicePrivate := convToPrv(prA, KeyVariant_SIDH_A)
bobPrivate := convToPrv(prB, KeyVariant_SIDH_B)
// Do actual test
e = DeriveSecret(s1[:], bobPrivate, alicePublic)
checkErr(t, e, "derivation s1")
e = DeriveSecret(s2[:], alicePrivate, bobPublic)
checkErr(t, e, "derivation s1")
if !bytes.Equal(s1[:], s2[:]) {
t.Fatalf("two shared keys: %d, %d do not match", s1, s2)
}
// Negative case
dec, e := hex.DecodeString(tdata.PkA_sidh)
if e != nil {
t.FailNow()
}
dec[0] = ^dec[0]
e = alicePublic.Import(dec)
if e != nil {
t.FailNow()
}
e = DeriveSecret(s1[:], bobPrivate, alicePublic)
checkErr(t, e, "derivation of s1 failed")
e = DeriveSecret(s2[:], alicePrivate, bobPublic)
checkErr(t, e, "derivation of s2 failed")
if bytes.Equal(s1[:], s2[:]) {
t.Fatalf("The two shared keys: %d, %d match", s1, s2)
}
}
func TestDerivationRoundTrip(t *testing.T) {
var err error
var s1, s2 [110 * 3]byte
pubA := NewPublicKey(KeyVariant_SIDH_A)
prvA := NewPrivateKey(KeyVariant_SIDH_A)
pubB := NewPublicKey(KeyVariant_SIDH_B)
prvB := NewPrivateKey(KeyVariant_SIDH_B)
// Generate private keys
err = prvA.Generate(rand.Reader)
checkErr(t, err, "key generation failed")
err = prvB.Generate(rand.Reader)
checkErr(t, err, "key generation failed")
// Generate public keys
prvA.GeneratePublicKey(pubA)
prvB.GeneratePublicKey(pubB)
// Derive shared secret
err = DeriveSecret(s1[:], prvB, pubA)
checkErr(t, err, "")
err = DeriveSecret(s2[:], prvA, pubB)
checkErr(t, err, "")
if !bytes.Equal(s1[:], s2[:]) {
t.Fatalf("Two shared keys: \n%X, \n%X do not match", s1, s2)
}
}
// encrypt, decrypt, check if input/output plaintext is the same
func TestPKERoundTrip(t *testing.T) {
// Message to be encrypted
var msg = make([]byte, Params.MsgLen)
var ct = make([]byte, kem.CiphertextSize())
for i, _ := range msg {
msg[i] = byte(i)
}
// Import keys
pkB := NewPublicKey(KeyVariant_SIKE)
skB := NewPrivateKey(KeyVariant_SIKE)
pk_hex, err := hex.DecodeString(tdata.PkB_sike)
if err != nil {
t.Fatal(err)
}
sk_hex, err := hex.DecodeString(tdata.PrB_sike)
if err != nil {
t.Fatal(err)
}
if pkB.Import(pk_hex) != nil || skB.Import(sk_hex) != nil {
t.Error("Import")
}
err = encrypt(ct, rand.Reader, pkB, msg[:])
if err != nil {
t.Fatal(err)
}
var pt [40]byte
pt_len, err := decrypt(pt[:], skB, ct)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(pt[:pt_len], msg[:]) {
t.Errorf("Decryption failed \n got : %X\n exp : %X", pt, msg)
}
}
// Generate key and check if can encrypt
func TestPKEKeyGeneration(t *testing.T) {
// Message to be encrypted
var msg = make([]byte, Params.MsgLen)
var ct = make([]byte, kem.CiphertextSize())
var err error
for i, _ := range msg {
msg[i] = byte(i)
}
sk := NewPrivateKey(KeyVariant_SIKE)
err = sk.Generate(rand.Reader)
checkErr(t, err, "PEK key generation")
pk := NewPublicKey(KeyVariant_SIKE)
sk.GeneratePublicKey(pk)
// Try to encrypt
err = encrypt(ct, rand.Reader, pk, msg[:])
checkErr(t, err, "PEK encryption")
var pt [40]byte
pt_len, err := decrypt(pt[:], sk, ct)
checkErr(t, err, "PEK key decryption")
if !bytes.Equal(pt[:pt_len], msg[:]) {
t.Fatalf("Decryption failed \n got : %X\n exp : %X", pt, msg)
}
}
func TestNegativePKE(t *testing.T) {
var msg [40]byte
var err error
var ct = make([]byte, kem.CiphertextSize())
// Generate key
pk := NewPublicKey(KeyVariant_SIKE)
sk := NewPrivateKey(KeyVariant_SIKE)
err = sk.Generate(rand.Reader)
checkErr(t, err, "key generation")
sk.GeneratePublicKey(pk)
// bytelen(msg) - 1
err = encrypt(ct, rand.Reader, pk, msg[:Params.KemSize+8-1])
if err == nil {
t.Fatal("Error hasn't been returned")
}
for _, v := range ct {
if v != 0 {
t.Fatal("Returned ciphertext must be not changed")
}
}
// KemSize - 1
var pt [40]byte
pt_len, err := decrypt(pt[:], sk, msg[:Params.KemSize+8-1])
if err == nil {
t.Fatal("Error hasn't been returned")
}
if pt_len != 0 {
t.Fatal("Ciphertext must be nil")
}
}
func testKEMRoundTrip(t *testing.T, pkB, skB []byte) {
ct := make([]byte, kem.CiphertextSize())
ss_e := make([]byte, kem.SharedSecretSize())
ss_d := make([]byte, kem.SharedSecretSize())
// Import keys
pk := NewPublicKey(KeyVariant_SIKE)
sk := NewPrivateKey(KeyVariant_SIKE)
if pk.Import(pkB) != nil || sk.Import(skB) != nil {
t.Error("Import failed")
}
checkErr(t, kem.Encapsulate(ct, ss_e, pk),
"error: Encapsulation during round-trip")
checkErr(t, kem.Decapsulate(ss_d, sk, pk, ct),
"error: Decapsulation during round-trip")
if !bytes.Equal(ss_e, ss_d) {
t.Error("Shared secrets from decapsulation and encapsulation differ")
}
}
func TestKEMRoundTrip(t *testing.T) {
pk, err := hex.DecodeString(tdata.PkB_sike)
checkErr(t, err, "public key B not a number")
sk, err := hex.DecodeString(tdata.PrB_sike)
checkErr(t, err, "private key B not a number")
testKEMRoundTrip(t, pk, sk)
}
func TestKEMKeyGeneration(t *testing.T) {
ct := make([]byte, kem.CiphertextSize())
ss_e := make([]byte, kem.SharedSecretSize())
ss_d := make([]byte, kem.SharedSecretSize())
// Generate key
pk := NewPublicKey(KeyVariant_SIKE)
sk := NewPrivateKey(KeyVariant_SIKE)
checkErr(t, sk.Generate(rand.Reader), "error: key generation")
sk.GeneratePublicKey(pk)
// calculated shared secret
checkErr(t, kem.Encapsulate(ct, ss_e, pk),
"encapsulation failed")
checkErr(t, kem.Decapsulate(ss_d, sk, pk, ct),
"decapsulation failed")
if !bytes.Equal(ss_e, ss_d) {
t.Fatalf("KEM failed \n encapsulated: %X\n decapsulated: %X", ss_d, ss_e)
}
}
func TestNegativeKEM(t *testing.T) {
ct := make([]byte, kem.CiphertextSize())
ss_e := make([]byte, kem.SharedSecretSize())
ss_d := make([]byte, kem.SharedSecretSize())
pk := NewPublicKey(KeyVariant_SIKE)
sk := NewPrivateKey(KeyVariant_SIKE)
checkErr(t, sk.Generate(rand.Reader), "error: key generation")
sk.GeneratePublicKey(pk)
checkErr(t, kem.Encapsulate(ct, ss_e, pk),
"pre-requisite for a test failed")
ct[0] = ct[0] - 1
checkErr(t, kem.Decapsulate(ss_d, sk, pk, ct),
"decapsulation returns error when invalid ciphertext provided")
if bytes.Equal(ss_e[:], ss_d) {
// no idea how this could ever happen, but it would be very bad
t.Error("critical error")
}
// Try encapsulating with SIDH key
pkSidh := NewPublicKey(KeyVariant_SIDH_B)
prSidh := NewPrivateKey(KeyVariant_SIDH_B)
err := kem.Encapsulate(ct, ss_e, pkSidh)
if err == nil {
t.Error("encapsulation accepts SIDH public key")
}
// Try decapsulating with SIDH key
err = kem.Decapsulate(ss_e, prSidh, pk, ct)
if err == nil {
t.Error("decapsulation accepts SIDH private key key")
}
}
// In case invalid ciphertext is provided, SIKE's decapsulation must
// return same (but unpredictable) result for a given key.
func TestNegativeKEMSameWrongResult(t *testing.T) {
ct := make([]byte, kem.CiphertextSize())
ss_e := make([]byte, kem.SharedSecretSize())
ss_d1 := make([]byte, kem.SharedSecretSize())
ss_d2 := make([]byte, kem.SharedSecretSize())
sk := NewPrivateKey(KeyVariant_SIKE)
pk := NewPublicKey(KeyVariant_SIKE)
// generate keys
checkErr(t, sk.Generate(rand.Reader), "error: key generation")
sk.GeneratePublicKey(pk)
checkErr(t, kem.Encapsulate(ct, ss_e, pk),
"pre-requisite for a test failed")
// make ciphertext wrong
ct[0] = ct[0] - 1
checkErr(t, kem.Decapsulate(ss_d1, sk, pk, ct),
"pre-requisite for a test failed")
// second decapsulation must be done with same, but imported private key
var expSk = make([]byte, sk.Size())
sk.Export(expSk)
// creat new private key
sk = NewPrivateKey(KeyVariant_SIKE)
checkErr(t, sk.Import(expSk),
"import failed")
// try decapsulating again. ss2 must be same as ss1 and different than
// original plaintext
checkErr(t, kem.Decapsulate(ss_d2, sk, pk, ct),
"pre-requisite for a test failed")
if !bytes.Equal(ss_d1, ss_d2) {
t.Error("decapsulation is insecure")
}
if bytes.Equal(ss_e, ss_d1) || bytes.Equal(ss_e, ss_d2) {
// this test requires that decapsulation returns wrong result
t.Errorf("test implementation error")
}
}
func testKeygen(t *testing.T, pk, sk []byte) {
var pubKeyBytes [110 * 3]byte
// Import provided private key
var prvKey = NewPrivateKey(KeyVariant_SIKE)
if prvKey.Import(sk) != nil {
panic("sike test: can't load KAT")
}
// Generate public key
pubKey := NewPublicKey(KeyVariant_SIKE)
prvKey.GeneratePublicKey(pubKey)
pubKey.Export(pubKeyBytes[:])
if !bytes.Equal(pubKeyBytes[:], pk) {
t.Fatalf("KAT keygen form private failed\nExp: %X\nGot: %X\n", pubKeyBytes[:], pk)
}
}
func TestKeyAgreement(t *testing.T) {
testKeyAgreement(t, tdata.PkA_sidh, tdata.PrA_sidh, tdata.PkB_sidh, tdata.PrB_sidh)
}
// Same values as in sike_test.cc
func TestDecapsulation(t *testing.T) {
var sk = [28 + 16]byte{
0x04, 0x5E, 0x01, 0x42, 0xB8, 0x2F, 0xE1, 0x9A, 0x38, 0x25,
0x92, 0xE7, 0xDC, 0xBA, 0xF7, 0x1B, 0xB1, 0xFD, 0x34, 0x42,
0xDB, 0x02, 0xBC, 0x9D, 0x4C, 0xD0, 0x72, 0x34, 0x4D, 0xBD,
0x06, 0xDF, 0x1C, 0x7D, 0x0A, 0x88, 0xB2, 0x50, 0xC4, 0xF6,
0xAE, 0xE8, 0x25, 0x01,
}
var pk = [330]byte{
0x6D, 0x8D, 0xF5, 0x7B, 0xCD, 0x47, 0xCA, 0xCB, 0x7A, 0x38,
0xB7, 0xA6, 0x90, 0xB7, 0x37, 0x03, 0xD4, 0x6F, 0x27, 0x73,
0x74, 0x17, 0x5A, 0xA4, 0x0D, 0xC6, 0x81, 0xAD, 0xDB, 0xF7,
0x18, 0xB2, 0x3C, 0x30, 0xCF, 0xAA, 0x08, 0x11, 0x91, 0xCC,
0x27, 0x4E, 0xF1, 0xA6, 0xB7, 0xDA, 0xD2, 0xCF, 0x99, 0x7F,
0xF7, 0xE1, 0xD0, 0xCE, 0x00, 0xD2, 0x4B, 0xA4, 0x33, 0xB4,
0x87, 0x01, 0x3F, 0x02, 0xF7, 0xF9, 0xDE, 0xC3, 0x60, 0x62,
0xDA, 0x3F, 0x74, 0xA9, 0x44, 0xBE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 0x86,
0x84, 0xC8, 0x84, 0x48, 0x89, 0x89, 0x5F, 0x7F, 0x92, 0x68,
0x2E, 0x9F, 0xBA, 0x46, 0x6D, 0x34, 0x93, 0x86, 0xE4, 0xEA,
0xE6, 0x1F, 0xF8, 0xE1, 0x94, 0xFF, 0x6E, 0xB6, 0x8D, 0x46,
0x64, 0xF0, 0xE3, 0xB7, 0x84, 0x0A, 0x5B, 0xE2, 0xC4, 0x01,
0x6E, 0xA5, 0xB8, 0xFA, 0xA5, 0x5A, 0x1C, 0x2D, 0xF6, 0x81,
0xC2, 0x44, 0x81, 0x5B, 0x00, 0x17,
}
var ss_exp = [16]byte{
0xCB, 0x9C, 0x6B, 0x0A, 0x64, 0x95, 0x32, 0xA7, 0x35, 0x4E,
0xE5, 0x14, 0xC8, 0xB7, 0x82, 0xB6,
}
var prvObj = NewPrivateKey(KeyVariant_SIKE)
var pubObj = NewPublicKey(KeyVariant_SIKE)
if pubObj.Import(pk[:]) != nil || prvObj.Import(sk[:]) != nil {
t.Error("Can't import one of the keys")
}
res := make([]byte, kem.SharedSecretSize())
checkErr(t, kem.Decapsulate(res, prvObj, pubObj, ct[:]),
"error: Decapsulation failed")
if !bytes.Equal(ss_exp[:], res) {
t.Error("Wrong decapsulation result")
}
}
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) {
ssGot := make([]byte, kem.SharedSecretSize())
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")
}
err := kem.Decapsulate(ssGot, 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
-------------------------------------------------------------------------*/
func BenchmarkKeygen(b *testing.B) {
pk := NewPublicKey(KeyVariant_SIKE)
prv := NewPrivateKey(KeyVariant_SIKE)
prv.Generate(rand.Reader)
for n := 0; n < b.N; n++ {
prv.GeneratePublicKey(pk)
}
}
func BenchmarkEncaps(b *testing.B) {
pub := NewPublicKey(KeyVariant_SIKE)
prv := NewPrivateKey(KeyVariant_SIKE)
if prv.Generate(rand.Reader) != nil {
b.FailNow()
}
prv.GeneratePublicKey(pub)
var ct [16 + 330]byte
var ss [16]byte
for n := 0; n < b.N; n++ {
kem.Reset()
kem.Encapsulate(ct[:], ss[:], pub)
}
}
func BenchmarkDecaps(b *testing.B) {
var ct [16 + 330]byte
var ss [16]byte
pkA := NewPublicKey(KeyVariant_SIKE)
prvA := NewPrivateKey(KeyVariant_SIKE)
pkB := NewPublicKey(KeyVariant_SIKE)
prvB := NewPrivateKey(KeyVariant_SIKE)
if prvA.Generate(rand.Reader) != nil || prvB.Generate(rand.Reader) != nil {
b.FailNow()
}
prvA.GeneratePublicKey(pkA)
prvB.GeneratePublicKey(pkB)
err := kem.Encapsulate(ct[:], ss[:], pkA)
if err != nil {
b.FailNow()
}
for n := 0; n < b.N; n++ {
kem.Decapsulate(ss[:], prvA, pkB, ct[:])
}
}
var kem KEM
func init() {
kem.Allocate(rand.Reader)
}