Skip to content

Commit ba5dd98

Browse files
committed
feature: bufferoverflow protection for liboqs
1 parent c67caa7 commit ba5dd98

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

core/constants.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
ML_DSA_87_PK_LEN = 2592
2525
ML_DSA_87_SIGN_LEN = 4595
2626

27+
ML_BUFFER_LIMITS = {
28+
ML_KEM_1024_NAME: {
29+
"SK_LEN": ML_KEM_1024_SK_LEN,
30+
"PK_LEN": ML_KEM_1024_PK_LEN
31+
},
32+
ML_DSA_87_NAME: {
33+
"SK_LEN": ML_DSA_87_SK_LEN,
34+
"PK_LEN": ML_DSA_87_PK_LEN
35+
}
36+
}
37+
2738
# hash parameters
2839
ARGON2_MEMORY = 256 * 1024 # KB
2940
ARGON2_ITERS = 3

core/crypto.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
ML_DSA_87_NAME,
2828
ML_DSA_87_SK_LEN,
2929
ML_DSA_87_PK_LEN,
30-
ML_DSA_87_SIGN_LEN
30+
ML_DSA_87_SIGN_LEN,
31+
ML_BUFFER_LIMITS
3132
)
3233

34+
3335
def create_signature(algorithm: str, message: bytes, private_key: bytes) -> bytes:
3436
"""
3537
Creates a digital signature for a message using a post-quantum signature scheme.
@@ -42,7 +44,7 @@ def create_signature(algorithm: str, message: bytes, private_key: bytes) -> byte
4244
Returns:
4345
Signature bytes of fixed size defined by the algorithm.
4446
"""
45-
with oqs.Signature(algorithm, secret_key=private_key) as signer:
47+
with oqs.Signature(algorithm, secret_key = private_key[:ML_BUFFER_LIMITS[algorithm]["SK_LEN"]]) as signer:
4648
return signer.sign(message)
4749

4850
def verify_signature(algorithm: str, message: bytes, signature: bytes, public_key: bytes) -> bool:
@@ -59,7 +61,7 @@ def verify_signature(algorithm: str, message: bytes, signature: bytes, public_ke
5961
True if valid, False if invalid.
6062
"""
6163
with oqs.Signature(algorithm) as verifier:
62-
return verifier.verify(message, signature, public_key)
64+
return verifier.verify(message, signature, public_key[:ML_BUFFER_LIMITS[algorithm]["PK_LEN"]])
6365

6466
def generate_sign_keys(algorithm: str = ML_DSA_87_NAME):
6567
"""
@@ -166,7 +168,7 @@ def decrypt_kyber_shared_secrets(ciphertext_blob: bytes, private_key: bytes, otp
166168
shared_secrets = b''
167169
cursor = 0
168170

169-
with oqs.KeyEncapsulation(ML_KEM_1024_NAME, secret_key=private_key) as kem:
171+
with oqs.KeyEncapsulation(ML_KEM_1024_NAME, secret_key=private_key[:ML_BUFFER_LIMITS[ML_KEM_1024_NAME]["SK_LEN"]]) as kem:
170172
while len(shared_secrets) < otp_pad_size:
171173
ciphertext = ciphertext_blob[cursor:cursor + cipher_size]
172174
if len(ciphertext) != cipher_size:
@@ -193,7 +195,7 @@ def generate_kyber_shared_secrets(public_key: bytes, otp_pad_size: int = OTP_PAD
193195

194196
with oqs.KeyEncapsulation(ML_KEM_1024_NAME) as kem:
195197
while len(shared_secrets) < otp_pad_size:
196-
ciphertext, shared_secret = kem.encap_secret(public_key)
198+
ciphertext, shared_secret = kem.encap_secret(public_key[:ML_BUFFER_LIMITS[ML_KEM_1024_NAME]["PK_LEN"]])
197199
ciphertexts_blob += ciphertext
198200
shared_secrets += shared_secret
199201

0 commit comments

Comments
 (0)