Commit graph

2700 commits

Author SHA1 Message Date
Karel Miko
e63dca1277 GCM + FreeBSD + HWCAP_PMULL 2026-04-24 09:34:50 +02:00
Steffen Jaeckel
cfb3ba8339
Merge pull request #726 from libtom/cleanup-and-fixes
Cleanup and fixes
2026-04-23 12:27:16 +02:00
Steffen Jaeckel
760d09031e Add Project Wycheproof testvectors for SIV.
Gemini proposed this to convert
```
import json
import requests

def to_c_hex(hex_str):
    if not hex_str: return "NULL"
    bytes_list = [f"0x{hex_str[i:i+2]}" for i in range(0, len(hex_str), 2)]
    return "{" + ",".join(bytes_list) + "}"

url = "https://raw.githubusercontent.com/C2SP/wycheproof/main/testvectors_v1/aes_siv_cmac_test.json"
data = requests.get(url).json()

print("#include <stdint.h>\n#include <stddef.h>\n")
print("typedef struct { int tcId; const char* comment; uint8_t key[64]; size_t keyLen; const uint8_t* aad; size_t aadLen; const uint8_t* msg; size_t msgLen; const uint8_t* ct; size_t ctLen; const char* result; } aes_siv_test_case;\n")

for group in data['testGroups']:
    for test in group['tests']:
        tid = test['tcId']
        if test['aad']: print(f"static const uint8_t aad_{tid}[] = {to_c_hex(test['aad'])};")
        if test['msg']: print(f"static const uint8_t msg_{tid}[] = {to_c_hex(test['msg'])};")
        if test['ct']:  print(f"static const uint8_t ct_{tid}[]  = {to_c_hex(test['ct'])};")

print("\nstatic const aes_siv_test_case aes_siv_tests[] = {")
for group in data['testGroups']:
    for test in group['tests']:
        tid = test['tcId']
        key_hex = to_c_hex(test['key'])
        aad_ptr = f"aad_{tid}" if test['aad'] else "NULL"
        msg_ptr = f"msg_{tid}" if test['msg'] else "NULL"
        ct_ptr  = f"ct_{tid}"  if test['ct']  else "NULL"

        print(f"    {{ {tid}, \"{test['comment']}\", {key_hex}, {len(test['key'])//2}, {aad_ptr}, {len(test['aad'])//2}, {msg_ptr}, {len(test['msg'])//2}, {ct_ptr}, {len(test['ct'])//2}, \"{test['result']}\" }},")
print("};")
```

I manually modified the result type to be an enum.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 10:30:32 +02:00
Steffen Jaeckel
098e0e3030 Explicitly use padding type.
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 10:30:32 +02:00
Steffen Jaeckel
72642a7e8c Make depadding better against timing attacks.
Fixes: 82482119df ("add padding module")
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 10:30:27 +02:00
Steffen Jaeckel
f700888231 Accept zero-length data in CTR mode.
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 10:30:27 +02:00
Steffen Jaeckel
b8c5ea8f18 Accept zero-length data in OMAC.
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 10:30:27 +02:00
Steffen Jaeckel
5f4287aced Fix SIV.
This also changes the API, since we need to know how many ADs are passed
and can't rely on a NULL element, as an empty element also changes the
tag.

Fixes: faa8cd71e5 ("add SIV")
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 10:29:48 +02:00
Steffen Jaeckel
5c7e0e9fb1 Allow pt as NULL if there's nothing to encrypt.
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
57b92e7351 Add a sentence about the origins of the CHC hash.
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
d7c650d398 Update PR template.
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
18cfd0f87a Suppress warnings emitted by Clang.
Fixes: 874e095a19 ("SHA-256 & SHA-224 x86")
Fixes: 7ac05df902 ("Add x86-optimized SHA1.")
Fixes: 46e0137e55 ("Optimize gcm_gf_mult using PCLMULQDQ and PMULL")
Fixes: 31c7f891aa ("add support for AES-NI instructions")
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
fe7bfb053a Branch once, not on each iteration.
Even though the branch predictor should get this, it's unnecessary.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
6be6de1ffa Replace int* recid hack in ECC.
Link: libtom/libtomcrypt#724
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
233f226108 Fix some ifdefs, include guards and a function call.
Fixes: 7ac05df902 ("Add x86-optimized SHA1.")
Fixes: 874e095a19 ("SHA-256 & SHA-224 x86")
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
e5df4f7ce8 Fix build with -DLTC_MINIMAL.
Fixes: 661109f660 ("re-factor modes to use internal ECB implementation")
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
e2544d4071 Further improve timing demo.
Filtering is now possible for all classes besides public key crypto.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
661dad10c7 Make HW-accel checkers always available.
Since the SHA-NI checker does the same for all three versions, we only have
to make one version public. This also now enables explicit testing of the
SHA-NI blocks.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
ec3804c452 Use s_x86_cpuid() in s_{aesni,pclmul}_is_supported().
Hopefully at one point we can get rid of its duplication. Why again don't
we use inline functions as they should be used?

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
e3bf539e5d Clean-up some defines.
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
4bdd0480f7 Introduce LTC_NO_ACCEL.
Related-to: libtom/libtomcrypt#727
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-23 09:13:02 +02:00
Steffen Jaeckel
a8236250a9
Merge pull request #737 from libtom/pr/sha3-missing-ltc_test
couple of missing: if defined(LTC_TEST)
2026-04-23 08:55:41 +02:00
Karel Miko
854749e2f4 couple of missing: if defined(LTC_TEST) 2026-04-23 08:16:12 +02:00
Steffen Jaeckel
ff52a70038
Merge pull request #734 from libtom/pr/fix-unused-parameter
fix unused-parameter warning
2026-04-22 14:41:44 +02:00
Karel Miko
a29e328b07 fix unused-parameter warning 2026-04-22 14:41:34 +02:00
Steffen Jaeckel
463431edc2
Merge pull request #735 from libtom/pr/hmac-zero-key
patch HMAC to accept zero-length keys and remove the scrypt workaround
2026-04-22 14:40:45 +02:00
Karel Miko
557e9e2678 patch HMAC to accept zero-length keys and remove the scrypt workaround 2026-04-21 23:35:10 +02:00
Steffen Jaeckel
2e441a17df
Merge pull request #717 from MarekKnapek/SHA-1x86
SHA-1, SHA-224 & SHA-256 with x86 instructions
2026-04-15 17:27:26 +02:00
Steffen Jaeckel
070d613d26 Update makefiles 2026-04-15 15:22:32 +02:00
Marek Knápek
874e095a19 SHA-256 & SHA-224 x86 2026-04-15 15:22:25 +02:00
Marek Knápek
7ac05df902 Add x86-optimized SHA1. 2026-04-15 15:20:28 +02:00
Steffen Jaeckel
68aae28a9d
Merge pull request #706 from libtom/new-rsa-api
New RSA API
2026-04-15 12:27:08 +02:00
Karel Miko
fa26d13016 RSA: update documentation (crypt.tex) to reflect the latest changes 2026-04-15 11:12:30 +02:00
Karel Miko
7ab625c090 RSA: fix demos/timing.c 2026-04-15 11:12:30 +02:00
Karel Miko
21d100d862 RSA: make rsa_decode_parameters work with rsa_key instead of ltc_rsa_parameters 2026-04-15 11:12:30 +02:00
Karel Miko
80de9b088b RSA: cosmetics - consistency tomcrypt_pk.h vs implementation files 2026-04-15 11:12:30 +02:00
Karel Miko
32ab39fb42 RSA: comment typo 2026-04-15 11:12:30 +02:00
Karel Miko
e4d3bd2fa5 RSA: replace hash_alg + mgf1_hash_alg with hash_idx + mgf1_hash_idx 2026-04-15 11:12:30 +02:00
Karel Miko
7fa7c2ad51 RSA: remove unnecessary #define LTC_DEPRECATED(x) 2026-04-15 11:12:30 +02:00
Karel Miko
557cb62b06 RSA: use consistently ltc_rsa_op_checked (+ cosmetic renaming op_check >> op_checked) 2026-04-15 11:12:30 +02:00
Karel Miko
c1b3281433 RSA: no more dual semantics of pss_oaep (removed from ltc_rsa_parameters; now lives on rsa_key where it belongs) 2026-04-15 11:12:30 +02:00
Karel Miko
d2829aa10f RSA: PSS + all encryption needs a PRNG 2026-04-15 11:12:30 +02:00
Steffen Jaeckel
66e677aedd Introduce new RSA API.
This also:
a) deprecates the old RSA and PKCS#1 API.
b) reverts the changes done to them in order to make the now deprecated API
   compatible again with the last release.

The fixes commit mentioned below is the testcase for the Bleichenbacher
attack, which works now again as expected.

Fixes: 9d03c38e ("add flags to `der_decode_sequence()`")
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-15 11:12:30 +02:00
Steffen Jaeckel
208ee2be4e Refactor SubjectPublicKeyInfo import
Slightly minimize both space and time when importing a
SubjectPublicKeyInfo. Time for ECC keys stays the same.

Those tests were done with X.509 support already available, but later these
commits were split up to be independent of the X.509 feature.

Running the entire set of pem files through `x509_verify` via [0]
resp. the timing app via [1] resulted in the following data:

Before this patch:

[0]
```
==1031519== HEAP SUMMARY:
==1031519==     in use at exit: 0 bytes in 0 blocks
==1031519==   total heap usage: 424,057 allocs, 424,057 frees, 73,527,730 bytes allocated
```

[1]
```
x509 cert-rsa-pss.pem    :     50021 cycles
x509 LTC_CA.pem          :     10335 cycles
x509 LTC_S0.pem          :     47284 cycles
x509 LTC_SS0.pem         :     36687 cycles
x509 secp384r1.pem       :   1985416 cycles
x509 secp521r1.pem       :   3287773 cycles
x509 LTC_SSS0.pem        :     25086 cycles
x509 secp224r1.pem       :    775807 cycles
```

After this patch:

[0]
```
==1043548== HEAP SUMMARY:
==1043548==     in use at exit: 0 bytes in 0 blocks
==1043548==   total heap usage: 337,244 allocs, 337,244 frees, 65,047,463 bytes allocated
```

[1]
```
x509 cert-rsa-pss.pem    :     32568 cycles
x509 LTC_CA.pem          :      5478 cycles
x509 LTC_S0.pem          :     36093 cycles
x509 LTC_SS0.pem         :     23351 cycles
x509 secp384r1.pem       :   1984030 cycles
x509 secp521r1.pem       :   3303396 cycles
x509 LTC_SSS0.pem        :     13220 cycles
x509 secp224r1.pem       :    781534 cycles
```

[0] find tests/x509 -name '*.pem' -exec valgrind --leak-check=full --show-leak-kinds=all './x509_verify' {} \+
[1] ./timing x509

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-15 11:12:30 +02:00
Steffen Jaeckel
d1ab64ec25 Add support for RSA-PSS keys
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-15 11:12:30 +02:00
Steffen Jaeckel
56f0e118dd Add support for separate MGF1 hashes
Update PKCS#1-PSS and RSA APIs that allow passing a separate hash index for
the MGF1 hash.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2026-04-15 11:12:30 +02:00
Steffen Jaeckel
33d8f8464d
Merge pull request #723 from cyanogilvie/fix/ocb-decrypt-aliasing
Fix s_ocb_done aliasing bug in decrypt path
2026-04-15 11:10:32 +02:00
Cyan Ogilvie
8c68d99221 Fix s_ocb_done aliasing bug in decrypt path
In decrypt mode (mode==1), s_ocb_done was XORing `ct[x]` into the
checksum before writing the output.  The function's parameter names are
misleading (the header comment notes pt/ct really mean in/out), so in
decrypt mode `ct` is the not-yet-written *output* buffer and `pt` is
the *input* ciphertext.  Reading from `ct` only worked when callers
aliased the input and output buffers (in-place decryption), as the
self-test does.  Callers passing distinct buffers got CRYPT_OK with
stat=0 -- correct plaintext but failed tag verification.

Fix by reading from `pt[x]` (the input).  Add a separate-buffer
regression case to ocb_test that runs against every existing test
vector and was confirmed to fail without the fix.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 19:32:17 -03:00
Steffen Jaeckel
e5bfb94f63
Merge pull request #719 from libtom/pr/scrypt-rfc7914
The scrypt Password-Based Key Derivation Function (RFC 7914)
2026-04-14 16:17:28 +02:00
Karel Miko
3aed12c410 The scrypt Password-Based Key Derivation Function (RFC 7914) 2026-04-14 13:59:38 +02:00