mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 17:58:05 -05:00
perf(build): replace BouncyCastle with JDK crypto API as much as possible (#2988)
* perf(security): stop registering the BouncyCastle JCE provider ACM signs through the JDK and builds PBES2 there, and KMS PSS uses RSASSA-PSS with the parameters AWS specifies. Downsize Docker image from 88.7 to 83.9 MB gzipped. * test(compat): verify the native image's KMS and ACM crypto from outside Floci Floci verifying its own signature proves nothing. Signatures now check against GetPublicKey with an outside library, exported keys are decrypted, and issued certificates verify their own signature. * fix(kms): apply real PSS padding when signing a digest with RSASSA_PSS * fix(acm): set the PBES2 salt and iteration count explicitly * perf(kms): reuse the shared SecureRandom when signing a PSS digest * fix(kms): let AwsException pass through the sign and verify catch blocks * perf(native): drop the native-image config the BouncyCastle provider needed Five --initialize-at-run-time entries and 21 reflect-config entries existed for the BouncyCastle JCE provider. Nothing registers that provider any more, so they only kept dead initialization code in the image. The run-time initialization entries are what cost size. Removing them takes the binary from 204.6 to 203.1 MB and gzip -9 from 79.5 to 78.9 MB. The reflect-config entries measured as noise, because those classes stay registered through another path, but they still describe a provider Floci no longer installs. Verified on a native build. The Python suite passes 354 tests, the Java SDK suite passes 61 including CloudHSM v2 and ACM, and a TLS run generates its self-signed certificate and signs with secp256k1 over HTTPS.
This commit is contained in:
1 parent
21f165b2e9
commit
ffe8a92635
10 files changed
+313
-267
No files matched your search
@@ -2,9 +2,38 @@
|
||||
|
||||
import hashlib
|
||||
|
||||
import boto3
|
||||
import pytest
|
||||
from botocore.exceptions import ClientError
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives import hashes, serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import ec, padding
|
||||
|
||||
|
||||
# Every signing algorithm each key spec declares. RSA key generation is the only
|
||||
# real cost here and the fixture below creates one key per spec, so covering the
|
||||
# full matrix is cheaper than the per-case keys it replaced.
|
||||
RSA_SIGNING_ALGORITHMS = [
|
||||
("RSASSA_PSS_SHA_256", hashes.SHA256),
|
||||
("RSASSA_PSS_SHA_384", hashes.SHA384),
|
||||
("RSASSA_PSS_SHA_512", hashes.SHA512),
|
||||
("RSASSA_PKCS1_V1_5_SHA_256", hashes.SHA256),
|
||||
("RSASSA_PKCS1_V1_5_SHA_384", hashes.SHA384),
|
||||
("RSASSA_PKCS1_V1_5_SHA_512", hashes.SHA512),
|
||||
]
|
||||
|
||||
SIGNING_CASES = [
|
||||
(key_spec, algorithm, digest)
|
||||
for key_spec in ("RSA_2048", "RSA_3072", "RSA_4096")
|
||||
for algorithm, digest in RSA_SIGNING_ALGORITHMS
|
||||
] + [
|
||||
("ECC_NIST_P256", "ECDSA_SHA_256", hashes.SHA256),
|
||||
("ECC_NIST_P384", "ECDSA_SHA_384", hashes.SHA384),
|
||||
("ECC_NIST_P521", "ECDSA_SHA_512", hashes.SHA512),
|
||||
# secp256k1 is signed with BouncyCastle's lightweight signer, reached directly
|
||||
# rather than through a JCA provider, and the JDK dropped the curve. It is the
|
||||
# crypto path most likely to break in a native image.
|
||||
("ECC_SECG_P256K1", "ECDSA_SHA_256", hashes.SHA256),
|
||||
]
|
||||
|
||||
|
||||
class TestKMSKey:
|
||||
@@ -207,37 +236,73 @@ class TestKMSEncryption:
|
||||
class TestKMSSigning:
|
||||
"""Test KMS signing operations."""
|
||||
|
||||
def test_sign_and_verify(self, kms_client):
|
||||
"""Test Sign and Verify with RSA key."""
|
||||
# Create an asymmetric signing key
|
||||
response = kms_client.create_key(
|
||||
Description="pytest-signing-key",
|
||||
KeyUsage="SIGN_VERIFY",
|
||||
KeySpec="RSA_2048",
|
||||
)
|
||||
key_id = response["KeyMetadata"]["KeyId"]
|
||||
@pytest.fixture(scope="class")
|
||||
@staticmethod
|
||||
def signing_key(aws_config, client_config):
|
||||
"""Return one signing key per key spec, created on first use.
|
||||
|
||||
RSA key generation dominates the runtime of this class, so the key is
|
||||
shared across every algorithm tested against it.
|
||||
"""
|
||||
client = boto3.client("kms", config=client_config, **aws_config)
|
||||
created = {}
|
||||
|
||||
def key_for(key_spec):
|
||||
if key_spec not in created:
|
||||
created[key_spec] = client.create_key(
|
||||
Description="pytest-signing-key",
|
||||
KeyUsage="SIGN_VERIFY",
|
||||
KeySpec=key_spec,
|
||||
)["KeyMetadata"]["KeyId"]
|
||||
return created[key_spec]
|
||||
|
||||
yield key_for
|
||||
for key_id in created.values():
|
||||
client.schedule_key_deletion(KeyId=key_id, PendingWindowInDays=7)
|
||||
|
||||
@pytest.mark.parametrize("key_spec,signing_algorithm,digest", SIGNING_CASES)
|
||||
def test_sign_and_verify(
|
||||
self, kms_client, signing_key, key_spec, signing_algorithm, digest
|
||||
):
|
||||
"""Test Sign and Verify, and check the signature against an outside library."""
|
||||
key_id = signing_key(key_spec)
|
||||
message = b"message to sign"
|
||||
|
||||
try:
|
||||
# Sign the message
|
||||
sign_response = kms_client.sign(
|
||||
KeyId=key_id,
|
||||
Message=message,
|
||||
SigningAlgorithm="RSASSA_PSS_SHA_256",
|
||||
)
|
||||
signature = sign_response["Signature"]
|
||||
assert signature
|
||||
# Sign the message
|
||||
sign_response = kms_client.sign(
|
||||
KeyId=key_id,
|
||||
Message=message,
|
||||
SigningAlgorithm=signing_algorithm,
|
||||
)
|
||||
signature = sign_response["Signature"]
|
||||
assert signature
|
||||
|
||||
# Verify the signature
|
||||
verify_response = kms_client.verify(
|
||||
KeyId=key_id,
|
||||
Message=message,
|
||||
Signature=signature,
|
||||
SigningAlgorithm="RSASSA_PSS_SHA_256",
|
||||
)
|
||||
assert verify_response["SignatureValid"]
|
||||
finally:
|
||||
kms_client.schedule_key_deletion(KeyId=key_id, PendingWindowInDays=7)
|
||||
# Verify the signature
|
||||
verify_response = kms_client.verify(
|
||||
KeyId=key_id,
|
||||
Message=message,
|
||||
Signature=signature,
|
||||
SigningAlgorithm=signing_algorithm,
|
||||
)
|
||||
assert verify_response["SignatureValid"]
|
||||
|
||||
# Sign and Verify are both Floci, so agreeing with each other proves
|
||||
# nothing about the padding, the mask generation function or the salt
|
||||
# length. Check the signature against GetPublicKey with the parameters
|
||||
# AWS documents for this algorithm instead.
|
||||
public_key = serialization.load_der_public_key(
|
||||
kms_client.get_public_key(KeyId=key_id)["PublicKey"]
|
||||
)
|
||||
if signing_algorithm.startswith("ECDSA"):
|
||||
public_key.verify(signature, message, ec.ECDSA(digest()))
|
||||
else:
|
||||
if signing_algorithm.startswith("RSASSA_PSS"):
|
||||
scheme = padding.PSS(
|
||||
mgf=padding.MGF1(digest()), salt_length=digest.digest_size
|
||||
)
|
||||
else:
|
||||
scheme = padding.PKCS1v15()
|
||||
public_key.verify(signature, message, scheme, digest())
|
||||
|
||||
|
||||
def test_ed25519_sign_and_verify(self, kms_client):
|
||||
|
||||
Reference in new issue
Block a user