Skip to content

Commit

Permalink
Make libcrux depend on libcrux-kem
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneider-bensch committed Jun 13, 2024
1 parent debd4e7 commit 3044792
Show file tree
Hide file tree
Showing 27 changed files with 18,026 additions and 1,067 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ libcrux-platform = { version = "=0.0.2-pre.2", path = "sys/platform" }
libcrux-hkdf = { version = "=0.0.2-pre.2", path = "libcrux-hkdf" }
libcrux-hmac = { version = "=0.0.2-pre.2", path = "libcrux-hmac" }
libcrux-ecdh = { version = "=0.0.2-pre.2", path = "libcrux-ecdh" }
libcrux-ml-kem = { version = "=0.0.2-pre.2", path = "libcrux-ml-kem" }
libcrux-kem = { version = "=0.0.2-pre.2", path = "libcrux-kem" }
rand = { version = "0.8" }
log = { version = "0.4", optional = true }
# WASM API
Expand Down
2 changes: 2 additions & 0 deletions libcrux-kem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ rand = { version = "0.8" }
tests = [] # Expose functions for testing.

[dev-dependencies]
libcrux-kem = { version = "0.0.2-pre.2", path = "./", features = ["tests"] }
libcrux = { version = "0.0.2-pre.2", path = "../", features = ["rand"] }
hex = { version = "0.4.3", features = ["serde"] }
1 change: 1 addition & 0 deletions libcrux-kem/tests/kats/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In order to regenerate the JSON KAT files for all parameter sets, simply run `./generate_kats.py`.
91 changes: 91 additions & 0 deletions libcrux-kem/tests/kats/generate_kats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#! /usr/bin/env python3

# This file is a modified version of:
# https://github.com/bwesterb/draft-schwabe-cfrg-kyber/blob/main/kyber_test.py

from kyber import *

import hashlib
import json

import Crypto
from Crypto.Cipher import AES


class NistDRBG:
"""NIST's DRBG used to generate NIST's Known Answer Tests (KATs),
see PQCgenKAT.c."""

def __init__(self, seed):
self.key = b"\0" * 32
self.v = 0
assert len(seed) == 48
self._update(seed)

def _update(self, seed):
b = AES.new(self.key, AES.MODE_ECB)
buf = b""
for i in range(3):
self.v += 1
buf += b.encrypt(self.v.to_bytes(16, "big"))
if seed is not None:
buf = bytes([x ^ y for x, y in zip(seed, buf)])
self.key = buf[:32]
self.v = int.from_bytes(buf[32:], "big")

def read(self, length):
b = AES.new(self.key, AES.MODE_ECB)
ret = b""
while len(ret) < length:
self.v += 1
block = b.encrypt(self.v.to_bytes(16, "big"))
ret += block
self._update(None)
return ret[:length]


for params in [params512, params768, params1024]:
kats_formatted = []
seed = bytes(range(48))
g = NistDRBG(seed)

print("Generating KATs for {} parameter set.".format(params))

for i in range(100):
seed = g.read(48)
g2 = NistDRBG(seed)

kseed = g2.read(32) + g2.read(32)
eseed = g2.read(32)

pk, sk = KeyGen(kseed, params)
ct, ss = Enc(pk, eseed, params)

Dec(sk, ct, params)

kats_formatted.append(
{
"key_generation_seed": bytes(kseed).hex(),
"sha3_256_hash_of_public_key": bytes(
hashlib.sha3_256(pk).digest()
).hex(),
"sha3_256_hash_of_secret_key": bytes(
hashlib.sha3_256(sk).digest()
).hex(),
"encapsulation_seed": bytes(eseed).hex(),
"sha3_256_hash_of_ciphertext": bytes(
hashlib.sha3_256(ct).digest()
).hex(),
"shared_secret": bytes(ss).hex(),
}
)

if params == params512:
output_suffix = "512"
elif params == params768:
output_suffix = "768"
else:
output_suffix = "1024"

with open("nistkats_{}.json".format(output_suffix), "w") as f:
json.dump(kats_formatted, f, ensure_ascii=False, indent=4)
1,040 changes: 1,040 additions & 0 deletions libcrux-kem/tests/kats/invalid_modulus/ML-KEM-1024.txt

Large diffs are not rendered by default.

775 changes: 775 additions & 0 deletions libcrux-kem/tests/kats/invalid_modulus/ML-KEM-512.txt

Large diffs are not rendered by default.

780 changes: 780 additions & 0 deletions libcrux-kem/tests/kats/invalid_modulus/ML-KEM-768.txt

Large diffs are not rendered by default.

Loading

0 comments on commit 3044792

Please sign in to comment.