Upgrade libtomcrypt

This commit is contained in:
2025-10-24 19:33:21 +03:00
parent d770dd8df3
commit acc69bb8ad
157 changed files with 922 additions and 761 deletions

View File

@@ -295,6 +295,14 @@ typedef unsigned long ltc_mp_digit;
#define LTC_HAVE_ROTATE_BUILTIN
#endif
#if __has_builtin(__builtin_clzl)
#define LTC_HAVE_CLZL_BUILTIN
#endif
#if __has_builtin(__builtin_ctzl)
#define LTC_HAVE_CTZL_BUILTIN
#endif
#if defined(__GNUC__)
#define LTC_ALIGN(n) __attribute__((aligned(n)))
#else

View File

@@ -274,18 +274,14 @@ typedef struct {
#ifdef LTC_CFB_MODE
/** A block cipher CFB structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE],
/** The pad used to encrypt/decrypt */
pad[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The width of the mode: 1, 8, 64, or 128 */
width,
int width,
/** The padding offset */
padlen;
} symmetric_CFB;
@@ -294,30 +290,23 @@ typedef struct {
#ifdef LTC_OFB_MODE
/** A block cipher OFB structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The padding offset */
padlen;
int padlen;
} symmetric_OFB;
#endif
#ifdef LTC_CBC_MODE
/** A block cipher CBC structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen;
} symmetric_CBC;
#endif
@@ -325,19 +314,15 @@ typedef struct {
#ifdef LTC_CTR_MODE
/** A block cipher CTR structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The counter */
unsigned char ctr[MAXBLOCKSIZE];
/** The pad used to encrypt/decrypt */
unsigned char pad[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The padding offset */
padlen,
int padlen,
/** The mode (endianess) of the CTR, 0==little, 1==big */
mode,
/** counter width */
@@ -349,18 +334,14 @@ typedef struct {
#ifdef LTC_LRW_MODE
/** A LRW structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The current IV */
unsigned char IV[16],
/** the tweak key */
tweak[16],
/** The current pad, it's the product of the first 15 bytes against the tweak key */
pad[16];
/** The scheduled symmetric key */
symmetric_key key;
#ifdef LTC_LRW_TABLES
/** The pre-computed multiplication table */
unsigned char PC[16][256][16];
@@ -374,17 +355,13 @@ typedef struct {
#ifdef LTC_F8_MODE
/** A block cipher F8 structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE],
MIV[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The padding offset */
padlen;
int padlen;
/** Current block count */
ulong32 blockcnt;
} symmetric_F8;
@@ -451,7 +428,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, symmetric_key *skey);
int (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, const symmetric_key *skey);
/** Accelerated ECB decryption
@param pt Plaintext
@@ -460,7 +437,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, symmetric_key *skey);
int (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, const symmetric_key *skey);
/** Accelerated CBC encryption
@param pt Plaintext
@@ -470,7 +447,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
int (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const symmetric_key *skey);
/** Accelerated CBC decryption
@param pt Plaintext
@@ -480,7 +457,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
int (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const symmetric_key *skey);
/** Accelerated CTR encryption
@param pt Plaintext
@@ -491,7 +468,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, symmetric_key *skey);
int (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, const symmetric_key *skey);
/** Accelerated LRW
@param pt Plaintext
@@ -502,7 +479,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_lrw_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);
int (*accel_lrw_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, const symmetric_key *skey);
/** Accelerated LRW
@param ct Ciphertext
@@ -513,7 +490,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_lrw_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);
int (*accel_lrw_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, const symmetric_key *skey);
/** Accelerated CCM packet (one-shot)
@param key The secret key to use
@@ -533,7 +510,7 @@ extern struct ltc_cipher_descriptor {
*/
int (*accel_ccm_memory)(
const unsigned char *key, unsigned long keylen,
symmetric_key *uskey,
const symmetric_key *uskey,
const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen,
unsigned char *pt, unsigned long ptlen,
@@ -923,8 +900,8 @@ extern const struct ltc_cipher_descriptor tea_desc;
#ifdef LTC_ECB_MODE
int ecb_start(int cipher, const unsigned char *key,
int keylen, int num_rounds, symmetric_ECB *ecb);
int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_ECB *ecb);
int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb);
int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, const symmetric_ECB *ecb);
int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, const symmetric_ECB *ecb);
int ecb_done(symmetric_ECB *ecb);
#endif
@@ -1016,7 +993,7 @@ int f8_test_mode(void);
#ifdef LTC_XTS_MODE
typedef struct {
symmetric_key key1, key2;
symmetric_ECB key1, key2;
int cipher;
} symmetric_xts;

View File

@@ -710,6 +710,18 @@
#error LTC_NO_MATH defined, but also a math descriptor
#endif
#if !defined(LTC_ECB_MODE)
#if defined(LTC_CFB_MODE) || defined(LTC_OFB_MODE) || defined(LTC_CBC_MODE) || defined(LTC_CTR_MODE) || \
defined(LTC_F8_MODE) || defined(LTC_LRW_MODE) || defined(LTC_XTS_MODE) )
#error LTC_ECB_MODE not defined, but all other modes depend on it
#endif
#if defined(LTC_OMAC) || defined(LTC_PMAC) || defined(LTC_XCBC) || defined(LTC_F9_MODE) || defined(LTC_EAX_MODE) || \
defined(LTC_OCB_MODE) || defined(LTC_OCB3_MODE) || defined(LTC_CCM_MODE) || defined(LTC_GCM_MODE) )
#error LTC_ECB_MODE not defined, but most MAC and AEAD modes depend on it
#endif
#endif
/* THREAD management */
#ifdef LTC_PTHREAD

View File

@@ -29,13 +29,12 @@ int hmac_file(int hash, const char *fname, const unsigned char *key,
#ifdef LTC_OMAC
typedef struct {
int cipher_idx,
buflen,
int buflen,
blklen;
unsigned char block[MAXBLOCKSIZE],
prev[MAXBLOCKSIZE],
Lu[2][MAXBLOCKSIZE];
symmetric_key key;
symmetric_ECB key;
} omac_state;
int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen);
@@ -66,10 +65,9 @@ typedef struct {
block[MAXBLOCKSIZE], /* currently accumulated block */
checksum[MAXBLOCKSIZE]; /* current checksum */
symmetric_key key; /* scheduled key for cipher */
symmetric_ECB key; /* scheduled key for cipher */
unsigned long block_index; /* index # for current block */
int cipher_idx, /* cipher idx */
block_len, /* length of block */
int block_len, /* length of block */
buflen; /* number of bytes in the buffer */
} pmac_state;
@@ -181,10 +179,9 @@ typedef struct {
unsigned char K[3][MAXBLOCKSIZE],
IV[MAXBLOCKSIZE];
symmetric_key key;
symmetric_ECB key;
int cipher,
buflen,
int buflen,
blocksize;
} xcbc_state;
@@ -215,7 +212,7 @@ typedef struct {
ACC[MAXBLOCKSIZE],
IV[MAXBLOCKSIZE];
symmetric_key key;
symmetric_ECB key;
int cipher,
buflen,
@@ -297,10 +294,9 @@ typedef struct {
R[MAXBLOCKSIZE], /* R value */
checksum[MAXBLOCKSIZE]; /* current checksum */
symmetric_key key; /* scheduled key for cipher */
symmetric_ECB key; /* scheduled key for cipher */
unsigned long block_index; /* index # for current block */
int cipher, /* cipher idx */
block_len; /* length of block */
int block_len; /* length of block */
} ocb_state;
int ocb_init(ocb_state *ocb, int cipher,
@@ -359,12 +355,11 @@ typedef struct {
aOffset_current[MAXBLOCKSIZE], /* AAD related helper variable */
adata_buffer[MAXBLOCKSIZE]; /* AAD buffer */
symmetric_key key; /* scheduled key for cipher */
symmetric_ECB key; /* scheduled key for cipher */
int adata_buffer_bytes; /* bytes in AAD buffer */
unsigned long ablock_index; /* index # for current adata (AAD) block */
unsigned long block_index; /* index # for current data block */
int cipher, /* cipher idx */
tag_len, /* length of tag */
int tag_len, /* length of tag */
block_len; /* length of block */
} ocb3_state;
@@ -407,14 +402,13 @@ int ocb3_test(void);
#define CCM_DECRYPT LTC_DECRYPT
typedef struct {
symmetric_ECB K;
unsigned char PAD[16], /* flags | Nonce N | l(m) */
ctr[16],
CTRPAD[16];
symmetric_key K;
int cipher, /* which cipher */
taglen, /* length of the tag (encoded in M value) */
int taglen, /* length of the tag (encoded in M value) */
x; /* index in PAD */
unsigned long L, /* L value */
@@ -448,7 +442,7 @@ int ccm_done(ccm_state *ccm,
int ccm_memory(int cipher,
const unsigned char *key, unsigned long keylen,
symmetric_key *uskey,
symmetric_ECB *uskey,
const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen,
unsigned char *pt, unsigned long ptlen,
@@ -480,6 +474,7 @@ extern const unsigned char gcm_shift_table[];
#define LTC_GCM_MODE_TEXT 2
typedef struct {
symmetric_ECB K;
unsigned char H[16], /* multiplier */
X[16], /* accumulator */
Y[16], /* counter */
@@ -489,11 +484,7 @@ typedef struct {
#ifdef LTC_GCM_TABLES
unsigned char PC[16][256][16]; /* 16 tables of 8x128 */
#endif
symmetric_key K;
int cipher, /* which cipher */
ivmode, /* Which mode is the IV in? */
int ivmode, /* Which mode is the IV in? */
mode, /* mode the GCM code is in */
buflen; /* length of data in buf */

View File

@@ -281,8 +281,18 @@ typedef struct {
/** The private key */
void *k;
/** The hash algorithm to use when creating a signature.
* Setting this will enable RFC6979 compatible signature generation.
* The macro ECC_SET_RFC6979_HASH_ALG() is provided as a helper
* to set this.*/
const char *rfc6979_hash_alg;
} ecc_key;
#define ECC_SET_RFC6979_HASH_ALG(key, alg) do { \
(key)->rfc6979_hash_alg = (alg); \
} while(0)
/** Formats of ECC signatures */
typedef enum ecc_signature_type_ {
/* ASN.1 encoded, ANSI X9.62 */
@@ -834,7 +844,6 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long nwor
int der_decode_object_identifier(const unsigned char *in, unsigned long inlen,
unsigned long *words, unsigned long *outlen);
int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen);
unsigned long der_object_identifier_bits(unsigned long x);
/* IA5 STRING */
int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
@@ -843,9 +852,6 @@ int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
int der_ia5_char_encode(int c);
int der_ia5_value_decode(int v);
/* TELETEX STRING */
int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
@@ -858,9 +864,6 @@ int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
int der_printable_char_encode(int c);
int der_printable_value_decode(int v);
/* UTF-8 */
#if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(__WCHAR_MAX__) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
#if defined(__WCHAR_MAX__)
@@ -880,7 +883,6 @@ int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
wchar_t *out, unsigned long *outlen);
unsigned long der_utf8_charsize(const wchar_t c);
int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);

View File

@@ -45,6 +45,8 @@ LTC_STATIC_ASSERT(correct_ltc_uintptr_size, sizeof(ltc_uintptr) == sizeof(void*)
#define LTC_NULL ((void *)0)
#endif
#define LTC_ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
/*
* Internal Enums
*/
@@ -122,6 +124,10 @@ typedef struct {
/* tomcrypt_cipher.h */
int ecb_encrypt_block(const unsigned char *pt, unsigned char *ct, const symmetric_ECB *ecb);
int ecb_decrypt_block(const unsigned char *ct, unsigned char *pt, const symmetric_ECB *ecb);
void blowfish_enc(ulong32 *data, unsigned long blocks, const symmetric_key *skey);
int blowfish_expand(const unsigned char *key, int keylen,
const unsigned char *data, int datalen,
@@ -357,6 +363,7 @@ struct get_char {
} data;
struct str unget_buf;
char unget_buf_[LTC_PEM_DECODE_BUFSZ];
int prev_get;
};
#endif
@@ -383,7 +390,7 @@ int pem_decrypt(unsigned char *data, unsigned long *datalen,
int pem_get_char_from_file(struct get_char *g);
#endif /* LTC_NO_FILE */
int pem_get_char_from_buf(struct get_char *g);
int pem_read(void *pem, unsigned long *w, struct pem_headers *hdr, struct get_char *g);
int pem_read(void *asn1_cert, unsigned long *asn1_len, struct pem_headers *hdr, struct get_char *g);
#endif
/* tomcrypt_pk.h */
@@ -441,6 +448,8 @@ int ecc_verify_hash_internal(void *r, void *s,
const unsigned char *hash, unsigned long hashlen,
int *stat, const ecc_key *key);
int ecc_rfc6979_key(const ecc_key *priv, const unsigned char *in, unsigned long inlen, ecc_key *key);
#ifdef LTC_SSH
int ecc_ssh_ecdsa_encode_name(char *buffer, unsigned long *buflen, const ecc_key *key);
#endif
@@ -579,6 +588,17 @@ int der_length_asn1_length(unsigned long len, unsigned long *outlen);
int der_length_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
unsigned long *outlen, unsigned long *payloadlen);
int der_length_object_identifier_full(const unsigned long *words, unsigned long nwords,
unsigned long *outlen, unsigned long *datalen);
int der_ia5_char_encode(int c);
int der_ia5_value_decode(int v);
int der_printable_char_encode(int c);
int der_printable_value_decode(int v);
unsigned long der_utf8_charsize(const wchar_t c);
typedef struct {
ltc_asn1_type t;
ltc_asn1_list **pp;