2019-03-29 22:13:53 +00:00
|
|
|
package sike
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2019-05-05 11:30:32 +01:00
|
|
|
"io"
|
2019-03-29 22:13:53 +00:00
|
|
|
"math/big"
|
2019-05-05 11:30:32 +01:00
|
|
|
"os"
|
2019-03-29 22:13:53 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var tdata = struct {
|
|
|
|
name string
|
2019-05-05 11:30:32 +01:00
|
|
|
katFile string
|
2019-03-29 22:13:53 +00:00
|
|
|
PrB_sidh string
|
|
|
|
PkB_sidh string
|
|
|
|
PkB_sike string
|
|
|
|
PrB_sike string
|
|
|
|
PrA_sike string
|
|
|
|
PkA_sike string
|
|
|
|
}{
|
|
|
|
name: "P-503",
|
2019-05-05 11:30:32 +01:00
|
|
|
katFile: "etc/PQCkemKAT_434.rsp",
|
2019-03-29 22:13:53 +00:00
|
|
|
PkB_sike: "68460C22466E95864CFEA7B5D9077E768FF4F9ED69AE56D7CF3F236FB06B31020EEE34B5B572CEA5DDF20B531966AA8F5F3ACC0C6D1CE04EEDC30FD1F1233E2D96FE60C6D638FC646EAF2E2246F1AEC96859CE874A1F029A78F9C978CD6B22114A0D5AB20101191FD923E80C76908B1498B9D0200065CCA09159A0C65A1E346CC6470314FE78388DAA89DD08EC67DBE63C1F606674ACC49EBF9FDBB2B898B3CE733113AA6F942DB401A76D629CE6EE6C0FDAF4CFB1A5E366DB66C17B3923A1B7FB26A3FF25B9018869C674D3DEF4AF269901D686FE4647F9D2CDB2CEB3AFA305B27C885F037ED167F595066C21E7DD467D8332B934A5102DA5F13332DFA356B82156A0BB2E7E91C6B85B7D1E381BC9E3F0FC4DB9C36016D9ECEC415D7E977E9AC29910D934BA2FE4EE49D3B387607A4E1AFABF495FB86A77194626589E802FF5167C7A25C542C1EAD25A6E0AA931D94F2F9AFD3DBDF222E651F729A90E77B20974905F1E65E041CE6C95AAB3E1F22D332E0A5DE9C5DB3D9C7A38",
|
|
|
|
PrB_sike: "80FC55DA74DEFE3113487B80841E678AF9ED4E0599CF07353A4AB93971C090A0" +
|
|
|
|
"A9402C9DC98AC6DC8F5FDE5E970AE22BA48A400EFC72851C",
|
|
|
|
PrB_sidh: "A885A8B889520A6DBAD9FB33365E5B77FDED629440A16A533F259A510F63A822",
|
|
|
|
PrA_sike: "B0AD510708F4ABCF3E0D97DC2F2FF112D9D2AAE49D97FFD1E4267F21C6E71C03",
|
|
|
|
PkA_sike: "A6BADBA04518A924B20046B59AC197DCDF0EA48014C9E228C4994CCA432F360E" +
|
|
|
|
"2D527AFB06CA7C96EE5CEE19BAD53BF9218A3961CAD7EC092BD8D9EBB22A3D51" +
|
|
|
|
"33008895A3F1F6A023F91E0FE06A00A622FD6335DAC107F8EC4283DC2632F080" +
|
|
|
|
"4E64B390DAD8A2572F1947C67FDF4F8787D140CE2C6B24E752DA9A195040EDFA" +
|
|
|
|
"C27333FAE97DBDEB41DA9EEB2DB067AE7DA8C58C0EF57AEFC18A3D6BD0576FF2" +
|
|
|
|
"F1CFCAEC50C958331BF631F3D2E769790C7B6DF282B74BBC02998AD10F291D47" +
|
|
|
|
"C5A762FF84253D3B3278BDF20C8D4D4AA317BE401B884E26A1F02C7308AADB68" +
|
|
|
|
"20EBDB0D339F5A63346F3B40CACED72F544DAF51566C6E807D0E6E1E38514342" +
|
|
|
|
"432661DC9564DA07548570E256688CD9E8060D8775F95D501886D958588CACA0" +
|
|
|
|
"9F2D2AE1913F996E76AF63E31A179A7A7D2A46EDA03B2BCCF9020A5AA15F9A28" +
|
|
|
|
"9340B33F3AE7F97360D45F8AE1B9DD48779A57E8C45B50A02C00349CD1C58C55" +
|
|
|
|
"1D68BC2A75EAFED944E8C599C288037181E997471352E24C952B",
|
|
|
|
PkB_sidh: "244AF1F367C2C33912750A98497CC8214BC195BD52BD76513D32ACE4B75E31F0" +
|
|
|
|
"281755C265F5565C74E3C04182B9C244071859C8588CC7F09547CEFF8F7705D2" +
|
|
|
|
"60CE87D6BFF914EE7DBE4B9AF051CA420062EEBDF043AF58184495026949B068" +
|
|
|
|
"98A47046BFAE8DF3B447746184AF550553BB5D266D6E1967ACA33CAC5F399F90" +
|
|
|
|
"360D70867F2C71EF6F94FF915C7DA8BC9549FB7656E691DAEFC93CF56876E482" +
|
|
|
|
"CA2F8BE2D6CDCC374C31AD8833CABE997CC92305F38497BEC4DFD1821B004FEC" +
|
|
|
|
"E16448F9A24F965EFE409A8939EEA671633D9FFCF961283E59B8834BDF7EDDB3" +
|
|
|
|
"05D6275B61DA6692325432A0BAA074FC7C1F51E76208AB193A57520D40A76334" +
|
|
|
|
"EE5712BDC3E1EFB6103966F2329EDFF63082C4DFCDF6BE1C5A048630B81871B8" +
|
|
|
|
"83B735748A8FD4E2D9530C272163AB18105B10015CA7456202FE1C9B92CEB167" +
|
|
|
|
"5EAE1132E582C88E47ED87B363D45F05BEA714D5E9933D7AF4071CBB5D49008F" +
|
|
|
|
"3E3DAD7DFF935EE509D5DE561842B678CCEB133D62E270E9AC3E",
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------
|
|
|
|
Helpers
|
|
|
|
-------------------------------------------------------------------------*/
|
|
|
|
// Fail if err !=nil. Display msg as an error message
|
|
|
|
func checkErr(t testing.TB, err error, msg string) {
|
|
|
|
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) {
|
2019-05-06 11:42:26 +01:00
|
|
|
var keyExp, keyGot [63 * 2 * 3]byte
|
2019-03-29 22:13:53 +00:00
|
|
|
alicePrivate := convToPrv(tdata.PrA_sike, KeyVariant_SIDH_A)
|
|
|
|
bobPrivate := convToPrv(tdata.PrB_sidh, KeyVariant_SIDH_B)
|
|
|
|
expPubA := convToPub(tdata.PkA_sike, KeyVariant_SIDH_A)
|
|
|
|
expPubB := convToPub(tdata.PkB_sidh, KeyVariant_SIDH_B)
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
pubA := NewPublicKey(KeyVariant_SIDH_A)
|
|
|
|
alicePrivate.GeneratePublicKey(pubA)
|
|
|
|
pubB := NewPublicKey(KeyVariant_SIDH_B)
|
|
|
|
bobPrivate.GeneratePublicKey(pubB)
|
2019-03-29 22:13:53 +00:00
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
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)
|
2019-03-29 22:13:53 +00:00
|
|
|
}
|
2019-05-06 11:42:26 +01:00
|
|
|
|
|
|
|
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",
|
|
|
|
keyExp, keyGot)
|
2019-03-29 22:13:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImportExport(t *testing.T) {
|
|
|
|
var err error
|
2019-05-06 11:42:26 +01:00
|
|
|
var aBytes, bBytes [126 * 3]byte
|
|
|
|
|
2019-03-29 22:13:53 +00:00
|
|
|
a := NewPublicKey(KeyVariant_SIDH_A)
|
|
|
|
b := NewPublicKey(KeyVariant_SIDH_B)
|
|
|
|
|
|
|
|
// Import keys
|
2019-05-06 11:42:26 +01:00
|
|
|
aHex, err := hex.DecodeString(tdata.PkA_sike)
|
2019-03-29 22:13:53 +00:00
|
|
|
checkErr(t, err, "invalid hex-number provided")
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
err = a.Import(aHex)
|
2019-03-29 22:13:53 +00:00
|
|
|
checkErr(t, err, "import failed")
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
bHex, err := hex.DecodeString(tdata.PkB_sike)
|
2019-03-29 22:13:53 +00:00
|
|
|
checkErr(t, err, "invalid hex-number provided")
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
err = b.Import(bHex)
|
2019-03-29 22:13:53 +00:00
|
|
|
checkErr(t, err, "import failed")
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
a.Export(aBytes[:])
|
|
|
|
b.Export(bBytes[:])
|
|
|
|
|
2019-03-29 22:13:53 +00:00
|
|
|
// Export and check if same
|
2019-05-06 11:42:26 +01:00
|
|
|
if !bytes.Equal(bBytes[:], bHex) || !bytes.Equal(aBytes[:], aHex) {
|
2019-03-29 22:13:53 +00:00
|
|
|
t.Fatalf("export/import failed")
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
if (len(bBytes) != b.Size()) || (len(aBytes) != a.Size()) {
|
2019-03-29 22:13:53 +00:00
|
|
|
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)
|
2019-05-17 20:40:22 +01:00
|
|
|
var secretBytes = make([]byte, prv.Size())
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
// 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()
|
2019-05-17 20:40:22 +01:00
|
|
|
prv.Export(secretBytes)
|
2019-03-29 22:13:53 +00:00
|
|
|
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
|
2019-05-06 11:42:26 +01:00
|
|
|
var s1, s2 [126 * 3]byte
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
// 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
|
2019-05-06 11:42:26 +01:00
|
|
|
e = DeriveSecret(s1[:], bobPrivate, alicePublic)
|
2019-03-29 22:13:53 +00:00
|
|
|
checkErr(t, e, "derivation s1")
|
2019-05-06 11:42:26 +01:00
|
|
|
e = DeriveSecret(s2[:], alicePrivate, bobPublic)
|
2019-03-29 22:13:53 +00:00
|
|
|
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_sike)
|
|
|
|
if e != nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
dec[0] = ^dec[0]
|
|
|
|
e = alicePublic.Import(dec)
|
|
|
|
if e != nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
e = DeriveSecret(s1[:], bobPrivate, alicePublic)
|
2019-03-29 22:13:53 +00:00
|
|
|
checkErr(t, e, "derivation of s1 failed")
|
2019-05-06 11:42:26 +01:00
|
|
|
e = DeriveSecret(s2[:], alicePrivate, bobPublic)
|
2019-03-29 22:13:53 +00:00
|
|
|
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
|
2019-05-06 11:42:26 +01:00
|
|
|
var s1, s2 [126 * 3]byte
|
2019-03-29 22:13:53 +00:00
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
pubA := NewPublicKey(KeyVariant_SIDH_A)
|
2019-03-29 22:13:53 +00:00
|
|
|
prvA := NewPrivateKey(KeyVariant_SIDH_A)
|
2019-05-06 11:42:26 +01:00
|
|
|
pubB := NewPublicKey(KeyVariant_SIDH_B)
|
2019-03-29 22:13:53 +00:00
|
|
|
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
|
2019-05-06 11:42:26 +01:00
|
|
|
prvA.GeneratePublicKey(pubA)
|
|
|
|
prvB.GeneratePublicKey(pubB)
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
// Derive shared secret
|
2019-05-06 11:42:26 +01:00
|
|
|
err = DeriveSecret(s1[:], prvB, pubA)
|
2019-03-29 22:13:53 +00:00
|
|
|
checkErr(t, err, "")
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
err = DeriveSecret(s2[:], prvA, pubB)
|
2019-03-29 22:13:53 +00:00
|
|
|
checkErr(t, err, "")
|
|
|
|
|
|
|
|
if !bytes.Equal(s1[:], s2[:]) {
|
|
|
|
t.Fatalf("Two shared keys: \n%X, \n%X do not match", s1, s2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
// encrypt, decrypt, check if input/output plaintext is the same
|
2019-03-29 22:13:53 +00:00
|
|
|
func testPKERoundTrip(t testing.TB, id uint8) {
|
|
|
|
// Message to be encrypted
|
|
|
|
var msg = make([]byte, Params.MsgLen)
|
2019-05-06 11:42:26 +01:00
|
|
|
var ct = make([]byte, kemSikeP503.CiphertextSize())
|
2019-03-29 22:13:53 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
err = encrypt(ct, rand.Reader, pkB, msg[:])
|
2019-03-29 22:13:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-05-06 11:42:26 +01:00
|
|
|
var pt [40]byte
|
|
|
|
pt_len, err := decrypt(pt[:], skB, ct)
|
2019-03-29 22:13:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-05-06 11:42:26 +01:00
|
|
|
if !bytes.Equal(pt[:pt_len], msg[:]) {
|
2019-03-29 22:13:53 +00:00
|
|
|
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)
|
2019-05-06 11:42:26 +01:00
|
|
|
var ct = make([]byte, kemSikeP503.CiphertextSize())
|
2019-03-29 22:13:53 +00:00
|
|
|
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")
|
2019-05-06 11:42:26 +01:00
|
|
|
pk := NewPublicKey(KeyVariant_SIKE)
|
|
|
|
sk.GeneratePublicKey(pk)
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
// Try to encrypt
|
2019-05-06 11:42:26 +01:00
|
|
|
err = encrypt(ct, rand.Reader, pk, msg[:])
|
2019-03-29 22:13:53 +00:00
|
|
|
checkErr(t, err, "PEK encryption")
|
2019-05-06 11:42:26 +01:00
|
|
|
var pt [40]byte
|
|
|
|
pt_len, err := decrypt(pt[:], sk, ct)
|
2019-03-29 22:13:53 +00:00
|
|
|
checkErr(t, err, "PEK key decryption")
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
if !bytes.Equal(pt[:pt_len], msg[:]) {
|
2019-03-29 22:13:53 +00:00
|
|
|
t.Fatalf("Decryption failed \n got : %X\n exp : %X", pt, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNegativePKE(t *testing.T) {
|
|
|
|
var msg [40]byte
|
|
|
|
var err error
|
2019-05-06 11:42:26 +01:00
|
|
|
var ct = make([]byte, kemSikeP503.CiphertextSize())
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
// Generate key
|
2019-05-06 11:42:26 +01:00
|
|
|
pk := NewPublicKey(KeyVariant_SIKE)
|
2019-03-29 22:13:53 +00:00
|
|
|
sk := NewPrivateKey(KeyVariant_SIKE)
|
|
|
|
err = sk.Generate(rand.Reader)
|
|
|
|
checkErr(t, err, "key generation")
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
sk.GeneratePublicKey(pk)
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
// bytelen(msg) - 1
|
2019-05-06 11:42:26 +01:00
|
|
|
err = encrypt(ct, rand.Reader, pk, msg[:Params.KemSize+8-1])
|
2019-03-29 22:13:53 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Error hasn't been returned")
|
|
|
|
}
|
2019-05-06 11:42:26 +01:00
|
|
|
for _, v := range ct {
|
|
|
|
if v != 0 {
|
|
|
|
t.Fatal("Returned ciphertext must be not changed")
|
|
|
|
}
|
2019-03-29 22:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// KemSize - 1
|
2019-05-06 11:42:26 +01:00
|
|
|
var pt [40]byte
|
|
|
|
pt_len, err := decrypt(pt[:], sk, msg[:Params.KemSize+8-1])
|
2019-03-29 22:13:53 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Error hasn't been returned")
|
|
|
|
}
|
2019-05-06 11:42:26 +01:00
|
|
|
if pt_len != 0 {
|
2019-03-29 22:13:53 +00:00
|
|
|
t.Fatal("Ciphertext must be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testKEMRoundTrip(t *testing.T, pkB, skB []byte) {
|
2019-05-06 11:42:26 +01:00
|
|
|
ct := make([]byte, kemSikeP503.CiphertextSize())
|
|
|
|
ss_e := make([]byte, kemSikeP503.SharedSecretSize())
|
|
|
|
ss_d := make([]byte, kemSikeP503.SharedSecretSize())
|
2019-03-29 22:13:53 +00:00
|
|
|
// Import keys
|
|
|
|
pk := NewPublicKey(KeyVariant_SIKE)
|
|
|
|
sk := NewPrivateKey(KeyVariant_SIKE)
|
|
|
|
if pk.Import(pkB) != nil || sk.Import(skB) != nil {
|
|
|
|
t.Error("Import failed")
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
checkErr(t, kemSikeP503.Encapsulate(ct, ss_e, pk),
|
|
|
|
"error: Encapsulation during round-trip")
|
|
|
|
checkErr(t, kemSikeP503.Decapsulate(ss_d, sk, pk, ct),
|
|
|
|
"error: Decapsulation during round-trip")
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
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) {
|
2019-05-06 11:42:26 +01:00
|
|
|
ct := make([]byte, kemSikeP503.CiphertextSize())
|
|
|
|
ss_e := make([]byte, kemSikeP503.SharedSecretSize())
|
|
|
|
ss_d := make([]byte, kemSikeP503.SharedSecretSize())
|
|
|
|
|
2019-03-29 22:13:53 +00:00
|
|
|
// Generate key
|
2019-05-06 11:42:26 +01:00
|
|
|
pk := NewPublicKey(KeyVariant_SIKE)
|
2019-03-29 22:13:53 +00:00
|
|
|
sk := NewPrivateKey(KeyVariant_SIKE)
|
|
|
|
checkErr(t, sk.Generate(rand.Reader), "error: key generation")
|
2019-05-06 11:42:26 +01:00
|
|
|
sk.GeneratePublicKey(pk)
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
// calculated shared secret
|
2019-05-06 11:42:26 +01:00
|
|
|
checkErr(t, kemSikeP503.Encapsulate(ct, ss_e, pk),
|
|
|
|
"encapsulation failed")
|
|
|
|
checkErr(t, kemSikeP503.Decapsulate(ss_d, sk, pk, ct),
|
|
|
|
"decapsulation failed")
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
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) {
|
2019-05-06 11:42:26 +01:00
|
|
|
ct := make([]byte, kemSikeP503.CiphertextSize())
|
|
|
|
ss_e := make([]byte, kemSikeP503.SharedSecretSize())
|
|
|
|
ss_d := make([]byte, kemSikeP503.SharedSecretSize())
|
|
|
|
|
|
|
|
pk := NewPublicKey(KeyVariant_SIKE)
|
2019-03-29 22:13:53 +00:00
|
|
|
sk := NewPrivateKey(KeyVariant_SIKE)
|
|
|
|
checkErr(t, sk.Generate(rand.Reader), "error: key generation")
|
2019-05-06 11:42:26 +01:00
|
|
|
sk.GeneratePublicKey(pk)
|
2019-03-29 22:13:53 +00:00
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
checkErr(t, kemSikeP503.Encapsulate(ct, ss_e, pk),
|
|
|
|
"pre-requisite for a test failed")
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
ct[0] = ct[0] - 1
|
2019-05-06 11:42:26 +01:00
|
|
|
checkErr(t, kemSikeP503.Decapsulate(ss_d, sk, pk, ct),
|
|
|
|
"decapsulation returns error when invalid ciphertext provided")
|
2019-03-29 22:13:53 +00:00
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
if bytes.Equal(ss_e[:], ss_d) {
|
2019-03-29 22:13:53 +00:00
|
|
|
// 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)
|
2019-05-06 11:42:26 +01:00
|
|
|
err := kemSikeP503.Encapsulate(ct, ss_e, pkSidh)
|
2019-03-29 22:13:53 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Error("encapsulation accepts SIDH public key")
|
|
|
|
}
|
|
|
|
// Try decapsulating with SIDH key
|
2019-05-06 11:42:26 +01:00
|
|
|
err = kemSikeP503.Decapsulate(ss_e, prSidh, pk, ct)
|
2019-03-29 22:13:53 +00:00
|
|
|
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) {
|
2019-05-06 11:42:26 +01:00
|
|
|
ct := make([]byte, kemSikeP503.CiphertextSize())
|
|
|
|
ss_e := make([]byte, kemSikeP503.SharedSecretSize())
|
|
|
|
ss_d1 := make([]byte, kemSikeP503.SharedSecretSize())
|
|
|
|
ss_d2 := make([]byte, kemSikeP503.SharedSecretSize())
|
|
|
|
|
2019-03-29 22:13:53 +00:00
|
|
|
sk := NewPrivateKey(KeyVariant_SIKE)
|
2019-05-06 11:42:26 +01:00
|
|
|
pk := NewPublicKey(KeyVariant_SIKE)
|
|
|
|
|
|
|
|
// generate keys
|
2019-03-29 22:13:53 +00:00
|
|
|
checkErr(t, sk.Generate(rand.Reader), "error: key generation")
|
2019-05-06 11:42:26 +01:00
|
|
|
sk.GeneratePublicKey(pk)
|
2019-03-29 22:13:53 +00:00
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
checkErr(t, kemSikeP503.Encapsulate(ct, ss_e, pk),
|
|
|
|
"pre-requisite for a test failed")
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
// make ciphertext wrong
|
|
|
|
ct[0] = ct[0] - 1
|
2019-05-06 11:42:26 +01:00
|
|
|
checkErr(t, kemSikeP503.Decapsulate(ss_d1, sk, pk, ct),
|
|
|
|
"pre-requisite for a test failed")
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
// second decapsulation must be done with same, but imported private key
|
2019-05-17 20:40:22 +01:00
|
|
|
var expSk = make([]byte, sk.Size())
|
|
|
|
sk.Export(expSk)
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
// creat new private key
|
|
|
|
sk = NewPrivateKey(KeyVariant_SIKE)
|
2019-05-06 11:42:26 +01:00
|
|
|
checkErr(t, sk.Import(expSk),
|
|
|
|
"import failed")
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
// try decapsulating again. ss2 must be same as ss1 and different than
|
|
|
|
// original plaintext
|
2019-05-06 11:42:26 +01:00
|
|
|
checkErr(t, kemSikeP503.Decapsulate(ss_d2, sk, pk, ct),
|
|
|
|
"pre-requisite for a test failed")
|
2019-03-29 22:13:53 +00:00
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
if !bytes.Equal(ss_d1, ss_d2) {
|
2019-03-29 22:13:53 +00:00
|
|
|
t.Error("decapsulation is insecure")
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
if bytes.Equal(ss_e, ss_d1) || bytes.Equal(ss_e, ss_d2) {
|
2019-03-29 22:13:53 +00:00
|
|
|
// this test requires that decapsulation returns wrong result
|
|
|
|
t.Errorf("test implementation error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-05 11:30:32 +01:00
|
|
|
func testKeygen(t *testing.T, pk, sk []byte) {
|
2019-05-06 11:42:26 +01:00
|
|
|
var pubKeyBytes [126 * 3]byte
|
2019-03-29 22:13:53 +00:00
|
|
|
// Import provided private key
|
|
|
|
var prvKey = NewPrivateKey(KeyVariant_SIKE)
|
|
|
|
if prvKey.Import(sk) != nil {
|
|
|
|
panic("sike test: can't load KAT")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate public key
|
2019-05-06 11:42:26 +01:00
|
|
|
pubKey := NewPublicKey(KeyVariant_SIKE)
|
|
|
|
prvKey.GeneratePublicKey(pubKey)
|
|
|
|
pubKey.Export(pubKeyBytes[:])
|
|
|
|
if !bytes.Equal(pubKeyBytes[:], pk) {
|
2019-05-05 11:30:32 +01:00
|
|
|
t.Fatalf("KAT keygen form private failed\n")
|
2019-03-29 22:13:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyAgreement(t *testing.T) {
|
|
|
|
testKeyAgreement(t, tdata.PkA_sike, tdata.PrA_sike, tdata.PkB_sidh, tdata.PrB_sidh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same values as in sike_test.cc
|
|
|
|
func TestDecapsulation(t *testing.T) {
|
|
|
|
|
|
|
|
var sk = [56]byte{
|
|
|
|
0xDB, 0xAF, 0x2C, 0x89, 0xCA, 0x5A, 0xD4, 0x9D, 0x4F, 0x13,
|
|
|
|
0x40, 0xDF, 0x2D, 0xB1, 0x5F, 0x4C, 0x91, 0xA7, 0x1F, 0x0B,
|
|
|
|
0x29, 0x15, 0x01, 0x59, 0xBC, 0x5F, 0x0B, 0x4A, 0x03, 0x27,
|
|
|
|
0x6F, 0x18}
|
|
|
|
|
|
|
|
var pk = []byte{
|
|
|
|
0x07, 0xAA, 0x51, 0x45, 0x3E, 0x1F, 0x53, 0x2A, 0x0A, 0x05,
|
|
|
|
0x46, 0xF6, 0x54, 0x7F, 0x5D, 0x56, 0xD6, 0x76, 0xD3, 0xEA,
|
|
|
|
0x4B, 0x6B, 0x01, 0x9B, 0x11, 0x72, 0x6F, 0x75, 0xEA, 0x34,
|
|
|
|
0x3C, 0x28, 0x2C, 0x36, 0xFD, 0x77, 0xDA, 0xBE, 0xB6, 0x20,
|
|
|
|
0x18, 0xC1, 0x93, 0x98, 0x18, 0x86, 0x30, 0x2F, 0x2E, 0xD2,
|
|
|
|
0x00, 0x61, 0xFF, 0xAE, 0x78, 0xAE, 0xFB, 0x6F, 0x32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F4, 0x19, 0xC6, 0x25, 0x0B, 0x0A, 0x5E, 0x21, 0xB9,
|
|
|
|
0x91, 0xB8, 0x19, 0x84, 0x48, 0x78, 0xCE, 0x27, 0xBF, 0x41,
|
|
|
|
0x89, 0xF6, 0x30, 0xFD, 0x6B, 0xD9, 0xB8, 0x1D, 0x72, 0x8A,
|
|
|
|
0x56, 0xCC, 0x2F, 0x82, 0xE4, 0x46, 0x4D, 0x75, 0xD8, 0x92,
|
|
|
|
0xE6, 0x9C, 0xCC, 0xD2, 0xCD, 0x35, 0xE4, 0xFC, 0x2A, 0x85,
|
|
|
|
0x6B, 0xA9, 0xB2, 0x27, 0xC9, 0xA1, 0xFF, 0xB3, 0x96, 0x3E,
|
|
|
|
0x59, 0xF6, 0x4C, 0x66, 0x56, 0x2E, 0xF5, 0x1B, 0x97, 0x32,
|
|
|
|
0xB0, 0x71, 0x5A, 0x9C, 0x50, 0x4B, 0x6F, 0xC4, 0xCA, 0x94,
|
|
|
|
0x75, 0x37, 0x46, 0x10, 0x12, 0x2F, 0x4F, 0xA3, 0x82, 0xCD,
|
|
|
|
0xBD, 0x7C,
|
|
|
|
}
|
|
|
|
var ss_exp = []byte{
|
2019-05-04 23:29:36 +01:00
|
|
|
0xbe, 0x07, 0x1d, 0xa6, 0x95, 0x4b, 0x03, 0x49, 0x6b, 0x2a,
|
|
|
|
0x8e, 0x25, 0x80, 0xab, 0x9c, 0xdd}
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
res := make([]byte, kemSikeP503.SharedSecretSize())
|
|
|
|
checkErr(t, kemSikeP503.Decapsulate(res, prvObj, pubObj, ct),
|
|
|
|
"error: Decapsulation failed")
|
|
|
|
|
2019-03-29 22:13:53 +00:00
|
|
|
if !bytes.Equal(ss_exp, res) {
|
|
|
|
t.Error("Wrong decapsulation result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-05 11:30:32 +01:00
|
|
|
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) {
|
2019-05-06 11:42:26 +01:00
|
|
|
ssGot := make([]byte, kemSikeP503.SharedSecretSize())
|
2019-05-05 11:30:32 +01:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
err := kemSikeP503.Decapsulate(ssGot, prvKey, pubKey, ct)
|
2019-05-05 11:30:32 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 22:13:53 +00:00
|
|
|
/* -------------------------------------------------------------------------
|
2019-05-06 11:42:26 +01:00
|
|
|
Benchmarking
|
|
|
|
-------------------------------------------------------------------------*/
|
2019-03-29 22:13:53 +00:00
|
|
|
|
|
|
|
func BenchmarkKeygen(b *testing.B) {
|
2019-05-06 11:42:26 +01:00
|
|
|
pk := NewPublicKey(KeyVariant_SIKE)
|
2019-03-29 22:13:53 +00:00
|
|
|
prv := NewPrivateKey(KeyVariant_SIKE)
|
2019-03-29 22:16:10 +00:00
|
|
|
prv.Generate(rand.Reader)
|
|
|
|
|
2019-03-29 22:13:53 +00:00
|
|
|
for n := 0; n < b.N; n++ {
|
2019-05-06 11:42:26 +01:00
|
|
|
prv.GeneratePublicKey(pk)
|
2019-03-29 22:13:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncaps(b *testing.B) {
|
2019-05-06 11:42:26 +01:00
|
|
|
pub := NewPublicKey(KeyVariant_SIKE)
|
2019-03-29 22:13:53 +00:00
|
|
|
prv := NewPrivateKey(KeyVariant_SIKE)
|
2019-05-06 11:42:26 +01:00
|
|
|
|
2019-03-29 22:13:53 +00:00
|
|
|
if prv.Generate(rand.Reader) != nil {
|
|
|
|
b.FailNow()
|
|
|
|
}
|
2019-05-06 11:42:26 +01:00
|
|
|
prv.GeneratePublicKey(pub)
|
|
|
|
|
|
|
|
var ct [24 + 378]byte
|
|
|
|
var ss [16]byte
|
|
|
|
|
2019-03-29 22:13:53 +00:00
|
|
|
for n := 0; n < b.N; n++ {
|
2019-05-06 11:42:26 +01:00
|
|
|
kemSikeP503.Reset()
|
|
|
|
kemSikeP503.Encapsulate(ct[:], ss[:], pub)
|
2019-03-29 22:13:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDecaps(b *testing.B) {
|
2019-05-06 11:42:26 +01:00
|
|
|
var ct [24 + 378]byte
|
|
|
|
var ss [16]byte
|
|
|
|
|
|
|
|
pkA := NewPublicKey(KeyVariant_SIKE)
|
2019-03-29 22:13:53 +00:00
|
|
|
prvA := NewPrivateKey(KeyVariant_SIKE)
|
2019-05-06 11:42:26 +01:00
|
|
|
pkB := NewPublicKey(KeyVariant_SIKE)
|
2019-03-29 22:13:53 +00:00
|
|
|
prvB := NewPrivateKey(KeyVariant_SIKE)
|
|
|
|
|
|
|
|
if prvA.Generate(rand.Reader) != nil || prvB.Generate(rand.Reader) != nil {
|
|
|
|
b.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
prvA.GeneratePublicKey(pkA)
|
|
|
|
prvB.GeneratePublicKey(pkB)
|
2019-03-29 22:13:53 +00:00
|
|
|
|
2019-05-06 11:42:26 +01:00
|
|
|
err := kemSikeP503.Encapsulate(ct[:], ss[:], pkA)
|
2019-03-29 22:13:53 +00:00
|
|
|
if err != nil {
|
|
|
|
b.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
2019-05-06 11:42:26 +01:00
|
|
|
kemSikeP503.Decapsulate(ss[:], prvA, pkB, ct[:])
|
2019-03-29 22:13:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-06 11:42:26 +01:00
|
|
|
|
|
|
|
var kemSikeP503 KEM
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
kemSikeP503.Allocate(rand.Reader)
|
|
|
|
}
|