Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: ML-DSA, ML-KEM #12

Merged
merged 7 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,17 @@ debug.log

# protobuf data -- included in package
*.dat
*.pb2
!crypto_condor/primitives/_testu01/examples/excel.dat

# objects
*.o

# compiled main -- included in package
main

# built docs
docs/build

# for importing NIST test vectors
*.imported

# Kyber executables -- included in package
**/_kyber/kyber512
**/_kyber/kyber512-90s
**/_kyber/kyber768
**/_kyber/kyber768-90s
**/_kyber/kyber1024
**/_kyber/kyber1024-90s

# Dilithium executables -- included in package
**/_dilithium/dilithium2
**/_dilithium/dilithium3
**/_dilithium/dilithium5

# built package
dist/
183 changes: 84 additions & 99 deletions crypto_condor/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
ECDH,
ECDSA,
HMAC,
MLDSA,
MLKEM,
RSAES,
RSASSA,
SHA,
SHAKE,
ChaCha20,
Dilithium,
Kyber,
)
from crypto_condor.primitives.common import Console

Expand Down Expand Up @@ -332,103 +332,6 @@ def ecdsa(
raise typer.Exit(1)


# TODO: expand.
_kyber_help = "Run a Kyber wrapper"


@app.command(name="Kyber", no_args_is_help=True, help=_kyber_help)
@app.command(name="kyber", no_args_is_help=True, help=_kyber_help, hidden=True)
def kyber(
language: Annotated[Kyber.Wrapper, _language],
parameter_set: Annotated[
Kyber.Paramset,
typer.Argument(help="The parameter set.", case_sensitive=False),
],
encapsulate: Annotated[
bool,
typer.Option(
"--encapsulate/--no-encapsulate",
help="Whether to test the encapsulation function.",
),
] = True,
decapsulate: Annotated[
bool,
typer.Option(
"--decapsulate/--no-decapsulate",
help="Whether to test the decapsulation function.",
),
] = True,
filename: Annotated[str, _filename] = "",
no_save: Annotated[bool, _no_save] = False,
debug: Annotated[Optional[bool], _debug] = None,
):
"""Runs a Kyber wrapper.

Args:
language: The language of the wrapper to run.
parameter_set: The Kyber parameter set to use.
encapsulate: Whether to test the encapsulation function.
decapsulate: Whether to test the decapsulation function.
filename: Name of the file to save results.
no_save: Do not save results or prompt the user.
debug: When saving the results to a file, whether to add the debug data.
"""
if not encapsulate and not decapsulate: # pragma: no cover (not needed)
console.print(
"--no-encapsulate and --no-decapsulate used, no function to test."
)
raise typer.Exit(1)
try:
results = Kyber.run_wrapper(language, parameter_set, encapsulate, decapsulate)
except Exception as error:
console.print(error)
raise typer.Exit(1) from error
if console.process_results(results, filename, no_save, debug):
raise typer.Exit(0)
else:
raise typer.Exit(1)


# TODO: expand.
_dilithium_help = "Run a Dilithium wrapper."


@app.command(name="Dilithium", no_args_is_help=True, help=_dilithium_help)
@app.command(name="dilithium", no_args_is_help=True, help=_dilithium_help, hidden=True)
def dilithium(
language: Annotated[Dilithium.Wrapper, _language],
parameter_set: Annotated[
Dilithium.Paramset,
typer.Argument(help="The parameter set.", case_sensitive=False),
],
sign: Annotated[bool, _sign] = True,
verify: Annotated[bool, _verify] = True,
filename: Annotated[str, _filename] = "",
no_save: Annotated[bool, _no_save] = False,
debug: Annotated[Optional[bool], _debug] = None,
):
"""Runs a Dilithium wrapper.

Args:
language: The language of the wrapper to run.
parameter_set: The Dilithium parameter set to use.
sign: Whether to test the signing function.
verify: Whether to test the verifying function.
filename: Name of the file to save results.
no_save: Do not save results or prompt the user.
debug: When saving the results to a file, whether to add the debug data.
"""
try:
results = Dilithium.run_wrapper(language, parameter_set, sign, verify)
except Exception as error:
console.print(error)
raise typer.Exit(1) from error
if console.process_results(results, filename, no_save, debug):
raise typer.Exit(0)
else:
raise typer.Exit(1)


# TODO: expand.
_sha_help = "Run a SHA wrapper."

Expand Down Expand Up @@ -794,3 +697,85 @@ def ecdh(
raise typer.Exit(0)
else:
raise typer.Exit(1)


# TODO: expand.
_mlkem_help = "Run a ML-KEM wrapper"


@app.command(name="MLKEM", no_args_is_help=True, help=_mlkem_help)
@app.command(name="mlkem", no_args_is_help=True, help=_mlkem_help, hidden=True)
def mlkem(
wrapper: Annotated[Path, typer.Argument()],
compliance: Annotated[bool, _compliance] = True,
resilience: Annotated[bool, _resilience] = False,
filename: Annotated[str, _filename] = "",
no_save: Annotated[bool, _no_save] = False,
debug: Annotated[Optional[bool], _debug] = None,
):
"""Runs a ML-KEM wrapper.

Args:
wrapper: The wrapper to test.
compliance: Whether to use compliance test vectors.
resilience: Whether to use resilience test vectors.
filename: Name of the file to save results.
no_save: Do not save results or prompt the user.
debug: When saving the results to a file, whether to add the debug data.
"""
if not wrapper.is_file():
raise FileNotFoundError(f"ML-KEM wrapper not found: {str(wrapper)}")

match wrapper.suffix:
case ".py":
results = MLKEM.run_python_wrapper(wrapper, compliance, resilience)
case _:
console.print(
"There is no ML-KEM runner defined for %s wrappers" % wrapper.suffix
)
raise typer.Exit(1)
if console.process_results(results, filename, no_save, debug):
raise typer.Exit(0)
else:
raise typer.Exit(1)


# TODO: expand.
_mldsa_help = "Run a ML-DSA wrapper."


@app.command(name="MLDSA", no_args_is_help=True, help=_mldsa_help)
@app.command(name="mldsa", no_args_is_help=True, help=_mldsa_help, hidden=True)
def mldsa(
wrapper: Annotated[Path, typer.Argument()],
compliance: Annotated[bool, _compliance] = True,
resilience: Annotated[bool, _resilience] = False,
filename: Annotated[str, _filename] = "",
no_save: Annotated[bool, _no_save] = False,
debug: Annotated[Optional[bool], _debug] = None,
):
"""Runs a ML-DSA wrapper.

Args:
wrapper: The wrapper to test.
compliance: Whether to use compliance test vectors.
resilience: Whether to use resilience test vectors.
filename: Name of the file to save results.
no_save: Do not save results or prompt the user.
debug: When saving the results to a file, whether to add the debug data.
"""
if not wrapper.is_file():
raise FileNotFoundError(f"ML-DSA wrapper not found: {str(wrapper)}")

match wrapper.suffix:
case ".py":
results = MLDSA.run_python_wrapper(wrapper, compliance, resilience)
case _:
console.print(
"There is no ML-DSA runner defined for %s wrappers" % wrapper.suffix
)
raise typer.Exit(1)
if console.process_results(results, filename, no_save, debug):
raise typer.Exit(0)
else:
raise typer.Exit(1)
48 changes: 24 additions & 24 deletions crypto_condor/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class Primitive(strenum.StrEnum):

AES = "AES"
CHACHA20 = "ChaCha20"
DILITHIUM = "Dilithium"
ECDH = "ECDH"
ECDSA = "ECDSA"
FALCON = "Falcon"
HMAC = "HMAC"
KYBER = "Kyber"
MLDSA = "MLDSA"
MLKEM = "MLKEM"
RSASSA = "RSASSA"
RSAES = "RSAES"
SHA = "SHA"
Expand All @@ -43,30 +43,30 @@ def get_languages(self):
ECDH,
ECDSA,
HMAC,
MLDSA,
MLKEM,
RSAES,
RSASSA,
SHA,
SHAKE,
ChaCha20,
Dilithium,
Kyber,
)

match self:
case Primitive.AES:
return AES.Wrapper
case Primitive.CHACHA20:
return ChaCha20.Wrapper
case Primitive.DILITHIUM:
return Dilithium.Wrapper
case Primitive.ECDH:
return ECDH.Wrapper
case Primitive.ECDSA:
return ECDSA.Wrapper
case Primitive.HMAC:
return HMAC.Wrapper
case Primitive.ECDH:
return ECDH.Wrapper
case Primitive.KYBER:
return Kyber.Wrapper
case Primitive.MLDSA:
return MLDSA.Wrapper
case Primitive.MLKEM:
return MLKEM.Wrapper
case Primitive.RSASSA:
return RSASSA.Wrapper
case Primitive.RSAES:
Expand Down Expand Up @@ -94,20 +94,6 @@ def get_languages(self):
"wrapper": True,
"harness": False,
},
Primitive.KYBER: {
"audit": False,
"method": True,
"output": None,
"wrapper": True,
"harness": True,
},
Primitive.DILITHIUM: {
"audit": False,
"method": True,
"output": None,
"wrapper": True,
"harness": True,
},
Primitive.FALCON: {
"audit": False,
"method": True,
Expand Down Expand Up @@ -171,6 +157,20 @@ def get_languages(self):
"wrapper": True,
"harness": False,
},
Primitive.MLDSA: {
"audit": False,
"method": True,
"output": True,
"wrapper": True,
"harness": True,
},
Primitive.MLKEM: {
"audit": False,
"method": True,
"output": True,
"wrapper": True,
"harness": True,
},
}
"""Primitives and their supported CLI modes."""

Expand Down
Loading