From acc69bb8adba958676ad427d48e5f45705dde9f0 Mon Sep 17 00:00:00 2001 From: Oleksii Zghurskyi Date: Fri, 24 Oct 2025 19:33:21 +0300 Subject: [PATCH] Upgrade libtomcrypt --- .../DataLiteC/libtomcrypt/ciphers/aes/aes.c | 2 +- .../libtomcrypt/ciphers/aes/aes_desc.c | 11 +- .../DataLiteC/libtomcrypt/ciphers/aes/aesni.c | 2 +- .../DataLiteC/libtomcrypt/ciphers/anubis.c | 2 +- .../DataLiteC/libtomcrypt/ciphers/camellia.c | 2 +- Sources/DataLiteC/libtomcrypt/ciphers/cast5.c | 6 +- Sources/DataLiteC/libtomcrypt/ciphers/des.c | 25 +-- Sources/DataLiteC/libtomcrypt/ciphers/idea.c | 2 +- .../DataLiteC/libtomcrypt/ciphers/kasumi.c | 2 +- .../DataLiteC/libtomcrypt/ciphers/khazad.c | 2 +- Sources/DataLiteC/libtomcrypt/ciphers/kseed.c | 2 +- .../DataLiteC/libtomcrypt/ciphers/multi2.c | 2 +- .../DataLiteC/libtomcrypt/ciphers/noekeon.c | 2 +- .../DataLiteC/libtomcrypt/ciphers/serpent.c | 2 +- Sources/DataLiteC/libtomcrypt/ciphers/sm4.c | 24 +-- Sources/DataLiteC/libtomcrypt/ciphers/tea.c | 6 +- .../libtomcrypt/ciphers/twofish/twofish.c | 2 +- Sources/DataLiteC/libtomcrypt/ciphers/xtea.c | 2 +- .../libtomcrypt/encauth/ccm/ccm_add_aad.c | 4 +- .../libtomcrypt/encauth/ccm/ccm_add_nonce.c | 2 +- .../libtomcrypt/encauth/ccm/ccm_done.c | 6 +- .../libtomcrypt/encauth/ccm/ccm_init.c | 3 +- .../libtomcrypt/encauth/ccm/ccm_memory.c | 37 ++-- .../libtomcrypt/encauth/ccm/ccm_process.c | 4 +- .../libtomcrypt/encauth/ccm/ccm_test.c | 22 +-- .../libtomcrypt/encauth/eax/eax_test.c | 2 +- .../libtomcrypt/encauth/gcm/gcm_add_aad.c | 5 - .../libtomcrypt/encauth/gcm/gcm_add_iv.c | 6 - .../libtomcrypt/encauth/gcm/gcm_done.c | 8 +- .../libtomcrypt/encauth/gcm/gcm_init.c | 5 +- .../libtomcrypt/encauth/gcm/gcm_process.c | 12 +- .../libtomcrypt/encauth/gcm/gcm_test.c | 2 +- .../libtomcrypt/encauth/ocb/ocb_decrypt.c | 14 +- .../libtomcrypt/encauth/ocb/ocb_encrypt.c | 8 +- .../libtomcrypt/encauth/ocb/ocb_init.c | 9 +- .../libtomcrypt/encauth/ocb/ocb_ntz.c | 6 + .../libtomcrypt/encauth/ocb/ocb_test.c | 2 +- .../libtomcrypt/encauth/ocb/s_ocb_done.c | 12 +- .../libtomcrypt/encauth/ocb3/ocb3_add_aad.c | 2 +- .../libtomcrypt/encauth/ocb3/ocb3_decrypt.c | 11 +- .../encauth/ocb3/ocb3_decrypt_last.c | 10 +- .../libtomcrypt/encauth/ocb3/ocb3_done.c | 5 +- .../libtomcrypt/encauth/ocb3/ocb3_encrypt.c | 11 +- .../encauth/ocb3/ocb3_encrypt_last.c | 10 +- .../libtomcrypt/encauth/ocb3/ocb3_init.c | 9 +- .../libtomcrypt/encauth/ocb3/ocb3_int_ntz.c | 6 + .../libtomcrypt/encauth/ocb3/ocb3_test.c | 2 +- .../DataLiteC/libtomcrypt/encauth/siv/siv.c | 2 +- .../DataLiteC/libtomcrypt/hashes/chc/chc.c | 2 +- Sources/DataLiteC/libtomcrypt/hashes/rmd128.c | 2 +- Sources/DataLiteC/libtomcrypt/hashes/rmd160.c | 2 +- Sources/DataLiteC/libtomcrypt/hashes/rmd256.c | 2 +- Sources/DataLiteC/libtomcrypt/hashes/rmd320.c | 2 +- Sources/DataLiteC/libtomcrypt/hashes/tiger.c | 18 +- .../libtomcrypt/hashes/whirl/whirl.c | 2 +- .../libtomcrypt/headers/tomcrypt_cfg.h | 8 + .../libtomcrypt/headers/tomcrypt_cipher.h | 79 +++------ .../libtomcrypt/headers/tomcrypt_custom.h | 12 ++ .../libtomcrypt/headers/tomcrypt_mac.h | 41 ++--- .../libtomcrypt/headers/tomcrypt_pk.h | 18 +- .../libtomcrypt/headers/tomcrypt_private.h | 22 ++- .../DataLiteC/libtomcrypt/mac/f9/f9_done.c | 8 +- .../DataLiteC/libtomcrypt/mac/f9/f9_init.c | 2 +- .../DataLiteC/libtomcrypt/mac/f9/f9_process.c | 4 +- .../DataLiteC/libtomcrypt/mac/f9/f9_test.c | 2 +- .../libtomcrypt/mac/omac/omac_done.c | 7 +- .../libtomcrypt/mac/omac/omac_init.c | 5 +- .../libtomcrypt/mac/omac/omac_process.c | 32 ++-- .../libtomcrypt/mac/omac/omac_test.c | 2 +- .../libtomcrypt/mac/pelican/pelican_test.c | 2 +- .../libtomcrypt/mac/pmac/pmac_done.c | 7 +- .../libtomcrypt/mac/pmac/pmac_init.c | 9 +- .../DataLiteC/libtomcrypt/mac/pmac/pmac_ntz.c | 6 + .../libtomcrypt/mac/pmac/pmac_process.c | 7 +- .../libtomcrypt/mac/pmac/pmac_test.c | 2 +- .../libtomcrypt/mac/xcbc/xcbc_done.c | 14 +- .../libtomcrypt/mac/xcbc/xcbc_init.c | 11 +- .../libtomcrypt/mac/xcbc/xcbc_process.c | 13 +- .../libtomcrypt/mac/xcbc/xcbc_test.c | 2 +- Sources/DataLiteC/libtomcrypt/math/ltm_desc.c | 2 +- Sources/DataLiteC/libtomcrypt/math/tfm_desc.c | 2 +- .../libtomcrypt/misc/base16/base16_encode.c | 7 +- .../DataLiteC/libtomcrypt/misc/crypt/crypt.c | 9 + .../misc/crypt/crypt_register_all_hashes.c | 9 +- .../libtomcrypt/misc/error_to_string.c | 2 +- .../DataLiteC/libtomcrypt/misc/pbes/pbes2.c | 4 +- Sources/DataLiteC/libtomcrypt/misc/pem/pem.c | 6 +- .../DataLiteC/libtomcrypt/misc/pem/pem_pkcs.c | 44 ++--- .../DataLiteC/libtomcrypt/misc/pem/pem_read.c | 77 ++++++--- .../DataLiteC/libtomcrypt/misc/pem/pem_ssh.c | 18 +- .../libtomcrypt/modes/cbc/cbc_decrypt.c | 24 +-- .../libtomcrypt/modes/cbc/cbc_done.c | 7 +- .../libtomcrypt/modes/cbc/cbc_encrypt.c | 28 +-- .../libtomcrypt/modes/cbc/cbc_getiv.c | 8 +- .../libtomcrypt/modes/cbc/cbc_setiv.c | 2 +- .../libtomcrypt/modes/cbc/cbc_start.c | 11 +- .../libtomcrypt/modes/cfb/cfb_decrypt.c | 18 +- .../libtomcrypt/modes/cfb/cfb_done.c | 7 +- .../libtomcrypt/modes/cfb/cfb_encrypt.c | 18 +- .../libtomcrypt/modes/cfb/cfb_getiv.c | 8 +- .../libtomcrypt/modes/cfb/cfb_setiv.c | 6 +- .../libtomcrypt/modes/cfb/cfb_start.c | 16 +- .../libtomcrypt/modes/ctr/ctr_done.c | 7 +- .../libtomcrypt/modes/ctr/ctr_encrypt.c | 40 ++--- .../libtomcrypt/modes/ctr/ctr_getiv.c | 8 +- .../libtomcrypt/modes/ctr/ctr_setiv.c | 6 +- .../libtomcrypt/modes/ctr/ctr_start.c | 10 +- .../libtomcrypt/modes/ctr/ctr_test.c | 2 +- .../libtomcrypt/modes/ecb/ecb_decrypt.c | 10 +- .../libtomcrypt/modes/ecb/ecb_encrypt.c | 10 +- .../DataLiteC/libtomcrypt/modes/f8/f8_done.c | 7 +- .../libtomcrypt/modes/f8/f8_encrypt.c | 28 +-- .../DataLiteC/libtomcrypt/modes/f8/f8_getiv.c | 8 +- .../DataLiteC/libtomcrypt/modes/f8/f8_setiv.c | 6 +- .../DataLiteC/libtomcrypt/modes/f8/f8_start.c | 14 +- .../libtomcrypt/modes/lrw/lrw_decrypt.c | 6 +- .../libtomcrypt/modes/lrw/lrw_done.c | 9 +- .../libtomcrypt/modes/lrw/lrw_encrypt.c | 6 +- .../libtomcrypt/modes/lrw/lrw_process.c | 4 +- .../libtomcrypt/modes/lrw/lrw_setiv.c | 4 +- .../libtomcrypt/modes/lrw/lrw_start.c | 4 +- .../libtomcrypt/modes/lrw/lrw_test.c | 2 +- .../libtomcrypt/modes/ofb/ofb_done.c | 7 +- .../libtomcrypt/modes/ofb/ofb_encrypt.c | 8 +- .../libtomcrypt/modes/ofb/ofb_getiv.c | 8 +- .../libtomcrypt/modes/ofb/ofb_setiv.c | 6 +- .../libtomcrypt/modes/ofb/ofb_start.c | 14 +- .../libtomcrypt/modes/xts/xts_decrypt.c | 10 +- .../libtomcrypt/modes/xts/xts_done.c | 4 +- .../libtomcrypt/modes/xts/xts_encrypt.c | 10 +- .../libtomcrypt/modes/xts/xts_init.c | 4 +- .../libtomcrypt/modes/xts/xts_test.c | 12 +- .../pk/asn1/der/general/der_asn1_maps.c | 10 +- .../der/general/der_decode_asn1_identifier.c | 2 +- .../pk/asn1/der/ia5/der_length_ia5_string.c | 4 +- .../der_encode_object_identifier.c | 15 +- .../der_length_object_identifier.c | 36 ++-- .../der_length_printable_string.c | 4 +- .../der/sequence/der_decode_sequence_flexi.c | 38 ++-- .../der_length_teletex_string.c | 4 +- .../libtomcrypt/pk/asn1/oid/pk_get.c | 4 +- .../libtomcrypt/pk/asn1/oid/pk_oid_str.c | 75 +++++--- .../x509_decode_subject_public_key_info.c | 2 +- .../x509_encode_subject_public_key_info.c | 4 +- .../libtomcrypt/pk/dsa/dsa_decrypt_key.c | 2 +- .../libtomcrypt/pk/dsa/dsa_generate_pqg.c | 2 +- .../libtomcrypt/pk/ec25519/ec25519_export.c | 2 +- .../libtomcrypt/pk/ecc/ecc_decrypt_key.c | 2 +- .../libtomcrypt/pk/ecc/ecc_export_openssl.c | 2 +- .../libtomcrypt/pk/ecc/ecc_make_key.c | 1 + .../libtomcrypt/pk/ecc/ecc_rfc6979_key.c | 162 ++++++++++++++++++ .../libtomcrypt/pk/ecc/ecc_set_curve.c | 2 + .../libtomcrypt/pk/ecc/ecc_set_key.c | 3 +- .../pk/ecc/ecc_sign_hash_internal.c | 10 +- .../pk/ecc/ecc_ssh_ecdsa_encode_name.c | 2 +- .../DataLiteC/libtomcrypt/pk/rsa/rsa_key.c | 6 +- .../libtomcrypt/pk/rsa/rsa_verify_hash.c | 2 +- 157 files changed, 922 insertions(+), 761 deletions(-) create mode 100644 Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_rfc6979_key.c diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/aes/aes.c b/Sources/DataLiteC/libtomcrypt/ciphers/aes/aes.c index 1a5e269..ea65a6b 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/aes/aes.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/aes/aes.c @@ -669,7 +669,7 @@ int ECB_TEST(void) unsigned char tmp[2][16]; int i, y; - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { zeromem(&key, sizeof(key)); if ((err = rijndael_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/aes/aes_desc.c b/Sources/DataLiteC/libtomcrypt/ciphers/aes/aes_desc.c index 7c537df..5b13dc2 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/aes/aes_desc.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/aes/aes_desc.c @@ -63,10 +63,19 @@ static LTC_INLINE int s_aesni_is_supported(void) a = 1; c = 0; +#if defined(_MSC_VER) && !defined(__clang__) + int arr[4]; + __cpuidex(arr, a, c); + a = arr[0]; + b = arr[1]; + c = arr[2]; + d = arr[3]; +#else __asm__ volatile ("cpuid" :"=a"(a), "=b"(b), "=c"(c), "=d"(d) :"a"(a), "c"(c) ); +#endif is_supported = ((c >> 19) & 1) && ((c >> 25) & 1); initialized = 1; @@ -189,7 +198,7 @@ int AES_TEST(void) int y; #endif - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { zeromem(&key, sizeof(key)); if ((err = AES_SETUP(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/aes/aesni.c b/Sources/DataLiteC/libtomcrypt/ciphers/aes/aesni.c index ecfc008..723ad27 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/aes/aesni.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/aes/aesni.c @@ -313,7 +313,7 @@ int aesni_test(void) unsigned char tmp[2][16]; int i, y; - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { zeromem(&key, sizeof(key)); if ((err = aesni_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/anubis.c b/Sources/DataLiteC/libtomcrypt/ciphers/anubis.c index 8338fa7..128b16c 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/anubis.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/anubis.c @@ -1498,7 +1498,7 @@ int anubis_test(void) unsigned char buf[2][16]; symmetric_key skey; - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { anubis_setup(tests[x].key, tests[x].keylen, 0, &skey); anubis_ecb_encrypt(tests[x].pt, buf[0], &skey); anubis_ecb_decrypt(buf[0], buf[1], &skey); diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/camellia.c b/Sources/DataLiteC/libtomcrypt/ciphers/camellia.c index 702c602..ee40284 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/camellia.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/camellia.c @@ -680,7 +680,7 @@ int camellia_test(void) int err; unsigned int x; - for (x = 0; x < sizeof(tests)/sizeof(tests[0]); x++) { + for (x = 0; x < LTC_ARRAY_SIZE(tests); x++) { zeromem(&skey, sizeof(skey)); if ((err = camellia_setup(tests[x].key, tests[x].keylen, 0, &skey)) != CRYPT_OK) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/cast5.c b/Sources/DataLiteC/libtomcrypt/ciphers/cast5.c index d8279b5..e55d8ab 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/cast5.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/cast5.c @@ -492,7 +492,7 @@ int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_ #endif #define FI cast5_FI -LTC_INLINE static ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr) +static LTC_INLINE ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr) { ulong32 I; I = (Km + R); @@ -500,7 +500,7 @@ LTC_INLINE static ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr) return ((S1[LTC_BYTE(I, 3)] ^ S2[LTC_BYTE(I,2)]) - S3[LTC_BYTE(I,1)]) + S4[LTC_BYTE(I,0)]; } -LTC_INLINE static ulong32 FII(ulong32 R, ulong32 Km, ulong32 Kr) +static LTC_INLINE ulong32 FII(ulong32 R, ulong32 Km, ulong32 Kr) { ulong32 I; I = (Km ^ R); @@ -508,7 +508,7 @@ LTC_INLINE static ulong32 FII(ulong32 R, ulong32 Km, ulong32 Kr) return ((S1[LTC_BYTE(I, 3)] - S2[LTC_BYTE(I,2)]) + S3[LTC_BYTE(I,1)]) ^ S4[LTC_BYTE(I,0)]; } -LTC_INLINE static ulong32 FIII(ulong32 R, ulong32 Km, ulong32 Kr) +static LTC_INLINE ulong32 FIII(ulong32 R, ulong32 Km, ulong32 Kr) { ulong32 I; I = (Km - R); diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/des.c b/Sources/DataLiteC/libtomcrypt/ciphers/des.c index d50e4de..2c03666 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/des.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/des.c @@ -2,6 +2,10 @@ /* SPDX-License-Identifier: Unlicense */ #include "tomcrypt_private.h" +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wconversion" +#pragma clang diagnostic ignored "-Wshorten-64-to-32" + /** @file des.c DES code submitted by Dobes Vandermeer @@ -1381,14 +1385,14 @@ static void cookey(const ulong32 *raw1, ulong32 *keyout) for(i=0; i < 16; i++, raw1++) { raw0 = raw1++; - *cook = (ulong32)((*raw0 & 0x00fc0000L) << 6); - *cook |= (ulong32)((*raw0 & 0x00000fc0L) << 10); - *cook |= (ulong32)((*raw1 & 0x00fc0000L) >> 10); - *cook++ |= (ulong32)((*raw1 & 0x00000fc0L) >> 6); - *cook = (ulong32)((*raw0 & 0x0003f000L) << 12); - *cook |= (ulong32)((*raw0 & 0x0000003fL) << 16); - *cook |= (ulong32)((*raw1 & 0x0003f000L) >> 4); - *cook++ |= (ulong32) (*raw1 & 0x0000003fL); + *cook = (*raw0 & 0x00fc0000L) << 6; + *cook |= (*raw0 & 0x00000fc0L) << 10; + *cook |= (*raw1 & 0x00fc0000L) >> 10; + *cook++ |= (*raw1 & 0x00000fc0L) >> 6; + *cook = (*raw0 & 0x0003f000L) << 12; + *cook |= (*raw0 & 0x0000003fL) << 16; + *cook |= (*raw1 & 0x0003f000L) >> 4; + *cook++ |= (*raw1 & 0x0000003fL); } XMEMCPY(keyout, dough, sizeof(dough)); @@ -2018,7 +2022,7 @@ int des_test(void) symmetric_key skey; int i, err; - for (i = 0; i < (int)(sizeof(cases)/sizeof(cases[0])); i++) + for (i = 0; i < (int)LTC_ARRAY_SIZE(cases); i++) { if ((err = des_setup(cases[i].key, 8, 0, &skey)) != CRYPT_OK) { return err; @@ -2125,7 +2129,7 @@ int des3_test(void) return err; } - for (i = 0; i < (int)(sizeof(cases)/sizeof(cases[0])); i++) + for (i = 0; i < (int)LTC_ARRAY_SIZE(cases); i++) { if ((err = des3_setup(cases[i].key, 16, 0, &skey)) != CRYPT_OK) { return err; @@ -2243,3 +2247,4 @@ int des3_keysize(int *keysize) #endif +#pragma clang diagnostic pop diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/idea.c b/Sources/DataLiteC/libtomcrypt/ciphers/idea.c index 40adc58..f663b48 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/idea.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/idea.c @@ -226,7 +226,7 @@ int idea_test(void) return CRYPT_FAIL_TESTVECTOR; } - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { if ((err = idea_setup(tests[x].key, 16, 8, &key)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/kasumi.c b/Sources/DataLiteC/libtomcrypt/ciphers/kasumi.c index de76fc7..7f25b34 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/kasumi.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/kasumi.c @@ -286,7 +286,7 @@ int kasumi_test(void) symmetric_key key; int err, x; - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { if ((err = kasumi_setup(tests[x].key, 16, 0, &key)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/khazad.c b/Sources/DataLiteC/libtomcrypt/ciphers/khazad.c index ccd1b70..0f1327d 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/khazad.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/khazad.c @@ -794,7 +794,7 @@ int khazad_test(void) unsigned char buf[2][8]; symmetric_key skey; - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { khazad_setup(tests[x].key, 16, 0, &skey); khazad_ecb_encrypt(tests[x].pt, buf[0], &skey); khazad_ecb_decrypt(buf[0], buf[1], &skey); diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/kseed.c b/Sources/DataLiteC/libtomcrypt/ciphers/kseed.c index 014b4e3..be8ec63 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/kseed.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/kseed.c @@ -334,7 +334,7 @@ int kseed_test(void) unsigned char buf[2][16]; symmetric_key skey; - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { kseed_setup(tests[x].key, 16, 0, &skey); kseed_ecb_encrypt(tests[x].pt, buf[0], &skey); kseed_ecb_decrypt(buf[0], buf[1], &skey); diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/multi2.c b/Sources/DataLiteC/libtomcrypt/ciphers/multi2.c index e1a84ac..2ef7ff0 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/multi2.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/multi2.c @@ -242,7 +242,7 @@ int multi2_test(void) symmetric_key skey; int err, x; - for (x = 1; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 1; x < (int)LTC_ARRAY_SIZE(tests); x++) { if ((err = multi2_setup(tests[x].key, 40, tests[x].rounds, &skey)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/noekeon.c b/Sources/DataLiteC/libtomcrypt/ciphers/noekeon.c index 3dfe91e..e5b4ded 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/noekeon.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/noekeon.c @@ -267,7 +267,7 @@ int noekeon_test(void) unsigned char tmp[2][16]; int err, i, y; - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { zeromem(&key, sizeof(key)); if ((err = noekeon_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/serpent.c b/Sources/DataLiteC/libtomcrypt/ciphers/serpent.c index 8f5b785..03661a1 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/serpent.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/serpent.c @@ -693,7 +693,7 @@ int serpent_test(void) symmetric_key key; int err, x; - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { if ((err = serpent_setup(tests[x].key, tests[x].keylen, 0, &key)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/sm4.c b/Sources/DataLiteC/libtomcrypt/ciphers/sm4.c index d21cdce..2fc68d0 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/sm4.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/sm4.c @@ -67,7 +67,7 @@ static const sm4_u8_t sm4_sbox_table[16][16] = { * S-box * defined in section 2.6 S-box */ -LTC_INLINE static sm4_u8_t s_sm4_sbox(sm4_u8_t a) +static LTC_INLINE sm4_u8_t s_sm4_sbox(sm4_u8_t a) { return sm4_sbox_table[(a >> 4) & 0x0f][a & 0x0f]; } @@ -80,7 +80,7 @@ LTC_INLINE static sm4_u8_t s_sm4_sbox(sm4_u8_t a) * But we just convert a 32bit word byte by byte. * So it's OK if we don't convert the endian order */ -LTC_INLINE static sm4_u32_t s_sm4_t(sm4_u32_t A) +static LTC_INLINE sm4_u32_t s_sm4_t(sm4_u32_t A) { sm4_u8_t a[4]; sm4_u8_t b[4]; @@ -98,7 +98,7 @@ LTC_INLINE static sm4_u32_t s_sm4_t(sm4_u32_t A) /* * defined in section 6.2 (2) Linear transformation L */ -LTC_INLINE static sm4_u32_t s_sm4_L62(sm4_u32_t B) +static LTC_INLINE sm4_u32_t s_sm4_L62(sm4_u32_t B) { return B ^ ROLc(B, 2) ^ ROLc(B, 10) ^ ROLc(B, 18) ^ ROLc(B, 24); } @@ -106,7 +106,7 @@ LTC_INLINE static sm4_u32_t s_sm4_L62(sm4_u32_t B) /* * defined in section 6.2 Permutation T */ -LTC_INLINE static sm4_u32_t s_sm4_T62(sm4_u32_t Z) +static LTC_INLINE sm4_u32_t s_sm4_T62(sm4_u32_t Z) { return s_sm4_L62(s_sm4_t(Z)); } @@ -137,7 +137,7 @@ static const sm4_u32_t sm4_CK[32] = /* * defined in section 7.3 (1) L' */ -LTC_INLINE static sm4_u32_t s_sm4_L73(sm4_u32_t B) +static LTC_INLINE sm4_u32_t s_sm4_L73(sm4_u32_t B) { return B ^ ROLc(B, 13) ^ ROLc(B, 23); } @@ -145,7 +145,7 @@ LTC_INLINE static sm4_u32_t s_sm4_L73(sm4_u32_t B) /* * defined in section 7.3 (1) T' */ -LTC_INLINE static sm4_u32_t s_sm4_T73(sm4_u32_t Z) +static LTC_INLINE sm4_u32_t s_sm4_T73(sm4_u32_t Z) { return s_sm4_L73(s_sm4_t(Z)); } @@ -153,7 +153,7 @@ LTC_INLINE static sm4_u32_t s_sm4_T73(sm4_u32_t Z) /* * defined in section 7.3 Key Expansion */ -LTC_INLINE static void s_sm4_mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16]) +static LTC_INLINE void s_sm4_mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16]) { sm4_u32_t MK[4] = { 0 }; sm4_u32_t K[4+32] = { 0 }; @@ -175,7 +175,7 @@ LTC_INLINE static void s_sm4_mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16]) /* * defined in section 6 Round Function F */ -LTC_INLINE static sm4_u32_t s_sm4_F(sm4_u32_t X[4], sm4_u32_t rk) +static LTC_INLINE sm4_u32_t s_sm4_F(sm4_u32_t X[4], sm4_u32_t rk) { return X[0] ^ s_sm4_T62(X[1] ^ X[2] ^ X[3] ^ rk); } @@ -183,7 +183,7 @@ LTC_INLINE static sm4_u32_t s_sm4_F(sm4_u32_t X[4], sm4_u32_t rk) /* * defined in section 7.1 (2) The reverse transformation */ -LTC_INLINE static void s_sm4_R(sm4_u32_t Y[4], sm4_u32_t X[32+4]) +static LTC_INLINE void s_sm4_R(sm4_u32_t Y[4], sm4_u32_t X[32+4]) { Y[0] = X[35]; Y[1] = X[34]; @@ -194,7 +194,7 @@ LTC_INLINE static void s_sm4_R(sm4_u32_t Y[4], sm4_u32_t X[32+4]) /* * defined in section 7.1 (En)cryption */ -LTC_INLINE static void s_sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_u32_t rk[32]) +static LTC_INLINE void s_sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_u32_t rk[32]) { int i; @@ -203,7 +203,7 @@ LTC_INLINE static void s_sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_ s_sm4_R(Y, X); } -LTC_INLINE static void s_sm4_setkey(struct sm4_key *sm4, const unsigned char *key) +static LTC_INLINE void s_sm4_setkey(struct sm4_key *sm4, const unsigned char *key) { int i; @@ -229,7 +229,7 @@ int sm4_setup(const unsigned char *key, int keylen, /* * SM4 encryption. */ -LTC_INLINE static void s_sm4_do(void *output, const void *input, const sm4_u32_t rk[32]) +static LTC_INLINE void s_sm4_do(void *output, const void *input, const sm4_u32_t rk[32]) { sm4_u32_t Y[4]; sm4_u32_t X[32+4]; diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/tea.c b/Sources/DataLiteC/libtomcrypt/ciphers/tea.c index 128d8a3..c5419bf 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/tea.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/tea.c @@ -150,7 +150,7 @@ int tea_test(void) symmetric_key skey; size_t i; int err, y; - for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) { + for (i = 0; i < LTC_ARRAY_SIZE(tests); i++) { zeromem(&skey, sizeof(skey)); l = sizeof(key); @@ -166,8 +166,8 @@ int tea_test(void) tea_ecb_encrypt(ptct[0], tmp[0], &skey); tea_ecb_decrypt(tmp[0], tmp[1], &skey); - if (compare_testvector(tmp[0], 8, ptct[1], 8, "TEA Encrypt", (int)i) != 0 || - compare_testvector(tmp[1], 8, ptct[0], 8, "TEA Decrypt", (int)i) != 0) { + if (compare_testvector(tmp[0], 8, ptct[1], 8, "TEA Encrypt", i) != 0 || + compare_testvector(tmp[1], 8, ptct[0], 8, "TEA Decrypt", i) != 0) { return CRYPT_FAIL_TESTVECTOR; } diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/twofish/twofish.c b/Sources/DataLiteC/libtomcrypt/ciphers/twofish/twofish.c index 48149a2..30478f0 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/twofish/twofish.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/twofish/twofish.c @@ -651,7 +651,7 @@ int twofish_test(void) unsigned char tmp[2][16]; int err, i, y; - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { if ((err = twofish_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/ciphers/xtea.c b/Sources/DataLiteC/libtomcrypt/ciphers/xtea.c index 95aaa1a..9e86e8d 100644 --- a/Sources/DataLiteC/libtomcrypt/ciphers/xtea.c +++ b/Sources/DataLiteC/libtomcrypt/ciphers/xtea.c @@ -197,7 +197,7 @@ int xtea_test(void) unsigned char tmp[2][8]; symmetric_key skey; int i, err, y; - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { zeromem(&skey, sizeof(skey)); if ((err = xtea_setup(tests[i].key, 16, 0, &skey)) != CRYPT_OK) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_add_aad.c b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_add_aad.c index 130d304..94d2e2b 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_add_aad.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_add_aad.c @@ -29,7 +29,7 @@ int ccm_add_aad(ccm_state *ccm, for (y = 0; y < adatalen; y++) { if (ccm->x == 16) { /* full block so let's encrypt it */ - if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { return err; } ccm->x = 0; @@ -40,7 +40,7 @@ int ccm_add_aad(ccm_state *ccm, /* remainder? */ if (ccm->aadlen == ccm->current_aadlen) { if (ccm->x != 0) { - if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { return err; } } diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_add_nonce.c b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_add_nonce.c index a56a43a..176836b 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_add_nonce.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_add_nonce.c @@ -66,7 +66,7 @@ int ccm_add_nonce(ccm_state *ccm, } /* encrypt PAD */ - if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_done.c b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_done.c index 965121d..51ce6cc 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_done.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_done.c @@ -28,7 +28,7 @@ int ccm_done(ccm_state *ccm, LTC_ARGCHK(taglen != NULL); if (ccm->x != 0) { - if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { return err; } } @@ -37,11 +37,11 @@ int ccm_done(ccm_state *ccm, for (y = 15; y > 15 - ccm->L; y--) { ccm->ctr[y] = 0x00; } - if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) { return err; } - cipher_descriptor[ccm->cipher].done(&ccm->K); + ecb_done(&ccm->K); /* store the TAG */ for (x = 0; x < 16 && x < *taglen; x++) { diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_init.c b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_init.c index 527c6af..3578bcf 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_init.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_init.c @@ -41,10 +41,9 @@ int ccm_init(ccm_state *ccm, int cipher, ccm->taglen = taglen; /* schedule key */ - if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ccm->K)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, 0, &ccm->K)) != CRYPT_OK) { return err; } - ccm->cipher = cipher; /* let's get the L value */ ccm->ptlen = ptlen; diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_memory.c b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_memory.c index a6498ce..d239044 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_memory.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_memory.c @@ -32,7 +32,7 @@ */ 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, @@ -42,7 +42,7 @@ int ccm_memory(int cipher, { unsigned char PAD[16], ctr[16], CTRPAD[16], ptTag[16], b, *pt_real; unsigned char *pt_work = NULL; - symmetric_key *skey; + symmetric_ECB *skey; int err; unsigned long len, L, x, y, z, CTRlen; @@ -78,12 +78,15 @@ int ccm_memory(int cipher, if (*taglen < 4 || *taglen > 16 || (*taglen % 2) == 1 || headerlen > 0x7fffffffu) { return CRYPT_INVALID_ARG; } + if (noncelen < 7) { + return CRYPT_INVALID_ARG; + } /* is there an accelerator? */ if (cipher_descriptor[cipher].accel_ccm_memory != NULL) { return cipher_descriptor[cipher].accel_ccm_memory( key, keylen, - uskey, + &uskey->key, nonce, noncelen, header, headerlen, pt, ptlen, @@ -120,7 +123,7 @@ int ccm_memory(int cipher, } /* initialize the cipher */ - if ((err = cipher_descriptor[cipher].setup(key, (int)keylen, 0, skey)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, 0, skey)) != CRYPT_OK) { XFREE(skey); return err; } @@ -144,7 +147,7 @@ int ccm_memory(int cipher, (L-1)); /* nonce */ - for (y = 0; y < 15 - L; y++) { + for (y = 0; y < noncelen; y++) { PAD[x++] = nonce[y]; } @@ -170,7 +173,7 @@ int ccm_memory(int cipher, } /* encrypt PAD */ - if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) { goto error; } @@ -195,7 +198,7 @@ int ccm_memory(int cipher, for (y = 0; y < headerlen; y++) { if (x == 16) { /* full block so let's encrypt it */ - if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) { goto error; } x = 0; @@ -204,7 +207,7 @@ int ccm_memory(int cipher, } /* remainder */ - if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) { goto error; } } @@ -239,7 +242,7 @@ int ccm_memory(int cipher, ctr[z] = (ctr[z] + 1) & 255; if (ctr[z]) break; } - if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ctr, CTRPAD, skey)) != CRYPT_OK) { goto error; } @@ -248,7 +251,7 @@ int ccm_memory(int cipher, *(LTC_FAST_TYPE_PTR_CAST(&PAD[z])) ^= *(LTC_FAST_TYPE_PTR_CAST(&pt[y+z])); *(LTC_FAST_TYPE_PTR_CAST(&ct[y+z])) = *(LTC_FAST_TYPE_PTR_CAST(&pt[y+z])) ^ *(LTC_FAST_TYPE_PTR_CAST(&CTRPAD[z])); } - if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) { goto error; } } @@ -259,7 +262,7 @@ int ccm_memory(int cipher, ctr[z] = (ctr[z] + 1) & 255; if (ctr[z]) break; } - if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ctr, CTRPAD, skey)) != CRYPT_OK) { goto error; } @@ -268,7 +271,7 @@ int ccm_memory(int cipher, *(LTC_FAST_TYPE_PTR_CAST(&pt[y+z])) = *(LTC_FAST_TYPE_PTR_CAST(&ct[y+z])) ^ *(LTC_FAST_TYPE_PTR_CAST(&CTRPAD[z])); *(LTC_FAST_TYPE_PTR_CAST(&PAD[z])) ^= *(LTC_FAST_TYPE_PTR_CAST(&pt[y+z])); } - if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) { goto error; } } @@ -283,7 +286,7 @@ int ccm_memory(int cipher, ctr[z] = (ctr[z] + 1) & 255; if (ctr[z]) break; } - if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ctr, CTRPAD, skey)) != CRYPT_OK) { goto error; } CTRlen = 0; @@ -299,7 +302,7 @@ int ccm_memory(int cipher, } if (x == 16) { - if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) { goto error; } x = 0; @@ -308,7 +311,7 @@ int ccm_memory(int cipher, } if (x != 0) { - if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) { goto error; } } @@ -318,12 +321,12 @@ int ccm_memory(int cipher, for (y = 15; y > 15 - L; y--) { ctr[y] = 0x00; } - if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ctr, CTRPAD, skey)) != CRYPT_OK) { goto error; } if (skey != uskey) { - cipher_descriptor[cipher].done(skey); + ecb_done(skey); #ifdef LTC_CLEAN_STACK zeromem(skey, sizeof(*skey)); #endif diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_process.c b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_process.c index 3d22480..b5f973d 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_process.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_process.c @@ -47,7 +47,7 @@ int ccm_process(ccm_state *ccm, ccm->ctr[z] = (ccm->ctr[z] + 1) & 255; if (ccm->ctr[z]) break; } - if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) { return err; } ccm->CTRlen = 0; @@ -63,7 +63,7 @@ int ccm_process(ccm_state *ccm, } if (ccm->x == 16) { - if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { return err; } ccm->x = 0; diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_test.c b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_test.c index e551c38..d725ecd 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_test.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ccm/ccm_test.c @@ -108,7 +108,7 @@ int ccm_test(void) unsigned long taglen, x, y; unsigned char buf[64], buf2[64], tag[16], tag2[16], tag3[16], zero[64]; int err, idx; - symmetric_key skey; + symmetric_ECB skey; ccm_state ccm; zeromem(zero, 64); @@ -121,11 +121,11 @@ int ccm_test(void) } } - for (x = 0; x < (sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < LTC_ARRAY_SIZE(tests); x++) { for (y = 0; y < 2; y++) { taglen = tests[x].taglen; if (y == 0) { - if ((err = cipher_descriptor[idx].setup(tests[x].key, 16, 0, &skey)) != CRYPT_OK) { + if ((err = ecb_start(idx, tests[x].key, 16, 0, &skey)) != CRYPT_OK) { return err; } @@ -151,7 +151,7 @@ int ccm_test(void) return err; } } else { - if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, (int)tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) { + if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) { return err; } if ((err = ccm_add_nonce(&ccm, tests[x].nonce, tests[x].noncelen)) != CRYPT_OK) { @@ -168,10 +168,10 @@ int ccm_test(void) } } - if (compare_testvector(buf, tests[x].ptlen, tests[x].ct, tests[x].ptlen, "CCM encrypt data", (int)x)) { + if (compare_testvector(buf, tests[x].ptlen, tests[x].ct, tests[x].ptlen, "CCM encrypt data", x)) { return CRYPT_FAIL_TESTVECTOR; } - if (compare_testvector(tag, taglen, tests[x].tag, tests[x].taglen, "CCM encrypt tag", (int)x)) { + if (compare_testvector(tag, taglen, tests[x].tag, tests[x].taglen, "CCM encrypt tag", x)) { return CRYPT_FAIL_TESTVECTOR; } @@ -189,7 +189,7 @@ int ccm_test(void) return err; } } else { - if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, (int)tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) { + if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) { return err; } if ((err = ccm_add_nonce(&ccm, tests[x].nonce, tests[x].noncelen)) != CRYPT_OK) { @@ -207,7 +207,7 @@ int ccm_test(void) } - if (compare_testvector(buf2, tests[x].ptlen, tests[x].pt, tests[x].ptlen, "CCM decrypt data", (int)x)) { + if (compare_testvector(buf2, tests[x].ptlen, tests[x].pt, tests[x].ptlen, "CCM decrypt data", x)) { return CRYPT_FAIL_TESTVECTOR; } if (y == 0) { @@ -225,17 +225,17 @@ int ccm_test(void) tag3, &taglen, 1 ) != CRYPT_ERROR) { return CRYPT_FAIL_TESTVECTOR; } - if (compare_testvector(buf2, tests[x].ptlen, zero, tests[x].ptlen, "CCM decrypt wrong tag", (int)x)) { + if (compare_testvector(buf2, tests[x].ptlen, zero, tests[x].ptlen, "CCM decrypt wrong tag", x)) { return CRYPT_FAIL_TESTVECTOR; } } else { - if (compare_testvector(tag2, taglen, tests[x].tag, tests[x].taglen, "CCM decrypt tag", (int)x)) { + if (compare_testvector(tag2, taglen, tests[x].tag, tests[x].taglen, "CCM decrypt tag", x)) { return CRYPT_FAIL_TESTVECTOR; } } if (y == 0) { - cipher_descriptor[idx].done(&skey); + ecb_done(&skey); } } } diff --git a/Sources/DataLiteC/libtomcrypt/encauth/eax/eax_test.c b/Sources/DataLiteC/libtomcrypt/encauth/eax/eax_test.c index c613e0d..adfa879 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/eax/eax_test.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/eax/eax_test.c @@ -216,7 +216,7 @@ int eax_test(void) } } - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { len = sizeof(outtag); if ((err = eax_encrypt_authenticate_memory(idx, tests[x].key, tests[x].keylen, tests[x].nonce, tests[x].noncelen, tests[x].header, tests[x].headerlen, diff --git a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_add_aad.c b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_add_aad.c index 5c3793e..67a86fe 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_add_aad.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_add_aad.c @@ -20,7 +20,6 @@ int gcm_add_aad(gcm_state *gcm, const unsigned char *adata, unsigned long adatalen) { unsigned long x; - int err; #ifdef LTC_FAST unsigned long y; #endif @@ -34,10 +33,6 @@ int gcm_add_aad(gcm_state *gcm, return CRYPT_INVALID_ARG; } - if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) { - return err; - } - /* in IV mode? */ if (gcm->mode == LTC_GCM_MODE_IV) { /* IV length must be > 0 */ diff --git a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_add_iv.c b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_add_iv.c index 33a2444..b37a55b 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_add_iv.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_add_iv.c @@ -20,7 +20,6 @@ int gcm_add_iv(gcm_state *gcm, const unsigned char *IV, unsigned long IVlen) { unsigned long x, y; - int err; LTC_ARGCHK(gcm != NULL); if (IVlen > 0) { @@ -36,11 +35,6 @@ int gcm_add_iv(gcm_state *gcm, return CRYPT_INVALID_ARG; } - if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) { - return err; - } - - /* trip the ivmode flag */ if (IVlen + gcm->buflen > 12) { gcm->ivmode |= 1; diff --git a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_done.c b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_done.c index 464f87a..5e135d1 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_done.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_done.c @@ -30,10 +30,6 @@ int gcm_done(gcm_state *gcm, return CRYPT_INVALID_ARG; } - if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) { - return err; - } - if (gcm->mode == LTC_GCM_MODE_IV) { /* let's process the IV */ if ((err = gcm_add_aad(gcm, NULL, 0)) != CRYPT_OK) return err; @@ -63,7 +59,7 @@ int gcm_done(gcm_state *gcm, gcm_mult_h(gcm, gcm->X); /* encrypt original counter */ - if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y_0, gcm->buf, &gcm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(gcm->Y_0, gcm->buf, &gcm->K)) != CRYPT_OK) { return err; } for (x = 0; x < 16 && x < *taglen; x++) { @@ -71,7 +67,7 @@ int gcm_done(gcm_state *gcm, } *taglen = x; - cipher_descriptor[gcm->cipher].done(&gcm->K); + ecb_done(&gcm->K); return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_init.c b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_init.c index 1822bdc..96abfe2 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_init.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_init.c @@ -44,20 +44,19 @@ int gcm_init(gcm_state *gcm, int cipher, } /* schedule key */ - if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &gcm->K)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, 0, &gcm->K)) != CRYPT_OK) { return err; } /* H = E(0) */ zeromem(B, 16); - if ((err = cipher_descriptor[cipher].ecb_encrypt(B, gcm->H, &gcm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(B, gcm->H, &gcm->K)) != CRYPT_OK) { return err; } /* setup state */ zeromem(gcm->buf, sizeof(gcm->buf)); zeromem(gcm->X, sizeof(gcm->X)); - gcm->cipher = cipher; gcm->mode = LTC_GCM_MODE_IV; gcm->ivmode = 0; gcm->buflen = 0; diff --git a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_process.c b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_process.c index e3c956c..b75c1d0 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_process.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_process.c @@ -37,10 +37,6 @@ int gcm_process(gcm_state *gcm, return CRYPT_INVALID_ARG; } - if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) { - return err; - } - /* 0xFFFFFFFE0 = ((2^39)-256)/8 */ if (gcm->pttotlen / 8 + (ulong64)gcm->buflen + (ulong64)ptlen >= CONST64(0xFFFFFFFE0)) { return CRYPT_INVALID_ARG; @@ -64,7 +60,7 @@ int gcm_process(gcm_state *gcm, if (++gcm->Y[y] & 255) { break; } } /* encrypt the counter */ - if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) { return err; } @@ -93,7 +89,7 @@ int gcm_process(gcm_state *gcm, for (y = 15; y >= 12; y--) { if (++gcm->Y[y] & 255) { break; } } - if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) { return err; } } @@ -111,7 +107,7 @@ int gcm_process(gcm_state *gcm, for (y = 15; y >= 12; y--) { if (++gcm->Y[y] & 255) { break; } } - if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) { return err; } } @@ -129,7 +125,7 @@ int gcm_process(gcm_state *gcm, for (y = 15; y >= 12; y--) { if (++gcm->Y[y] & 255) { break; } } - if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) { return err; } gcm->buflen = 0; diff --git a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_test.c b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_test.c index 228db57..aa24122 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_test.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/gcm/gcm_test.c @@ -338,7 +338,7 @@ int gcm_test(void) if ((err = gcm_done(&gcm, T[0], &y)) != CRYPT_OK) return err; if (compare_testvector(T[0], y, tests[0].T, 16, "GCM Encrypt Tag-special", 0)) return CRYPT_FAIL_TESTVECTOR; - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { y = sizeof(T[0]); if ((err = gcm_memory(idx, tests[x].K, tests[x].keylen, tests[x].IV, tests[x].IVlen, diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_decrypt.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_decrypt.c index dd512ce..98f36e4 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_decrypt.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_decrypt.c @@ -25,16 +25,8 @@ int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt) LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); - /* check if valid cipher */ - if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) { - return err; - } - LTC_ARGCHK(cipher_descriptor[ocb->cipher].ecb_decrypt != NULL); - - /* check length */ - if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) { - return CRYPT_INVALID_ARG; - } + /* can't use a encrypt-only descriptor */ + LTC_ARGCHK(cipher_descriptor[ocb->key.cipher].ecb_decrypt != NULL); /* Get Z[i] value */ ocb_shift_xor(ocb, Z); @@ -43,7 +35,7 @@ int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt) for (x = 0; x < ocb->block_len; x++) { tmp[x] = ct[x] ^ Z[x]; } - if ((err = cipher_descriptor[ocb->cipher].ecb_decrypt(tmp, pt, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_decrypt_block(tmp, pt, &ocb->key)) != CRYPT_OK) { return err; } for (x = 0; x < ocb->block_len; x++) { diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_encrypt.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_encrypt.c index ad6260f..a38765a 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_encrypt.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_encrypt.c @@ -24,12 +24,6 @@ int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct) LTC_ARGCHK(ocb != NULL); LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); - if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) { - return err; - } - if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) { - return CRYPT_INVALID_ARG; - } /* compute checksum */ for (x = 0; x < ocb->block_len; x++) { @@ -43,7 +37,7 @@ int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct) for (x = 0; x < ocb->block_len; x++) { tmp[x] = pt[x] ^ Z[x]; } - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, ct, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(tmp, ct, &ocb->key)) != CRYPT_OK) { return err; } for (x = 0; x < ocb->block_len; x++) { diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_init.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_init.c index c10e3c3..056605c 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_init.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_init.c @@ -57,7 +57,7 @@ int ocb_init(ocb_state *ocb, int cipher, /* determine which polys to use */ ocb->block_len = cipher_descriptor[cipher].block_length; - x = (int)(sizeof(polys)/sizeof(polys[0])); + x = (int)LTC_ARRAY_SIZE(polys); for (poly = 0; poly < x; poly++) { if (polys[poly].len == ocb->block_len) { break; @@ -71,13 +71,13 @@ int ocb_init(ocb_state *ocb, int cipher, } /* schedule the key */ - if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, 0, &ocb->key)) != CRYPT_OK) { return err; } /* find L = E[0] */ zeromem(ocb->L, ocb->block_len); - if ((err = cipher_descriptor[cipher].ecb_encrypt(ocb->L, ocb->L, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ocb->L, ocb->L, &ocb->key)) != CRYPT_OK) { return err; } @@ -85,7 +85,7 @@ int ocb_init(ocb_state *ocb, int cipher, for (x = 0; x < ocb->block_len; x++) { ocb->R[x] = ocb->L[x] ^ nonce[x]; } - if ((err = cipher_descriptor[cipher].ecb_encrypt(ocb->R, ocb->R, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ocb->R, ocb->R, &ocb->key)) != CRYPT_OK) { return err; } @@ -126,7 +126,6 @@ int ocb_init(ocb_state *ocb, int cipher, /* set other params */ ocb->block_index = 1; - ocb->cipher = cipher; return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_ntz.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_ntz.c index b0f5570..bf933fd 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_ntz.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_ntz.c @@ -17,6 +17,11 @@ */ int ocb_ntz(unsigned long x) { +#if defined(LTC_HAVE_CTZL_BUILTIN) + if (x == 0) + return sizeof(unsigned long) * CHAR_BIT; + return __builtin_ctzl(x); +#else int c; x &= 0xFFFFFFFFUL; c = 0; @@ -25,6 +30,7 @@ int ocb_ntz(unsigned long x) x >>= 1; } return c; +#endif } #endif diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_test.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_test.c index b03c2fd..12942b5 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_test.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb/ocb_test.c @@ -167,7 +167,7 @@ int ocb_test(void) } } - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { len = sizeof(outtag); if ((err = ocb_encrypt_authenticate_memory(idx, tests[x].key, 16, tests[x].nonce, tests[x].pt, tests[x].ptlen, outct, outtag, &len)) != CRYPT_OK) { diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb/s_ocb_done.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb/s_ocb_done.c index c5987b9..9336b75 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb/s_ocb_done.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb/s_ocb_done.c @@ -40,11 +40,7 @@ int s_ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen, LTC_ARGCHK(ct != NULL); LTC_ARGCHK(tag != NULL); LTC_ARGCHK(taglen != NULL); - if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) { - return err; - } - if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length || - (int)ptlen > ocb->block_len || (int)ptlen < 0) { + if ((int)ptlen > ocb->block_len || (int)ptlen < 0) { return CRYPT_INVALID_ARG; } @@ -76,7 +72,7 @@ int s_ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen, } /* Y[m] = E(X[m])) */ - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(X, Y, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(X, Y, &ocb->key)) != CRYPT_OK) { goto error; } @@ -107,10 +103,10 @@ int s_ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen, } /* encrypt checksum, er... tag!! */ - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->checksum, X, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ocb->checksum, X, &ocb->key)) != CRYPT_OK) { goto error; } - cipher_descriptor[ocb->cipher].done(&ocb->key); + ecb_done(&ocb->key); /* now store it */ for (x = 0; x < ocb->block_len && x < (int)*taglen; x++) { diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_add_aad.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_add_aad.c index 6215e9d..1c08423 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_add_aad.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_add_aad.c @@ -29,7 +29,7 @@ static int s_ocb3_int_aad_add_block(ocb3_state *ocb, const unsigned char *aad_bl /* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */ ocb3_int_xor_blocks(tmp, aad_block, ocb->aOffset_current, ocb->block_len); - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(tmp, tmp, &ocb->key)) != CRYPT_OK) { return err; } ocb3_int_xor_blocks(ocb->aSum_current, ocb->aSum_current, tmp, ocb->block_len); diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_decrypt.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_decrypt.c index 6d85036..15a3175 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_decrypt.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_decrypt.c @@ -32,14 +32,7 @@ int ocb3_decrypt(ocb3_state *ocb, const unsigned char *ct, unsigned long ctlen, LTC_ARGCHK(ct != NULL); LTC_ARGCHK(pt != NULL); - if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) { - return err; - } - if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) { - return CRYPT_INVALID_ARG; - } - - if (ctlen % ocb->block_len) { /* ctlen has to bu multiple of block_len */ + if (ctlen % ocb->block_len) { /* ctlen has to be multiple of block_len */ return CRYPT_INVALID_ARG; } @@ -55,7 +48,7 @@ int ocb3_decrypt(ocb3_state *ocb, const unsigned char *ct, unsigned long ctlen, ocb3_int_xor_blocks(tmp, ct_b, ocb->Offset_current, ocb->block_len); /* decrypt */ - if ((err = cipher_descriptor[ocb->cipher].ecb_decrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_decrypt_block(tmp, tmp, &ocb->key)) != CRYPT_OK) { goto LBL_ERR; } diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_decrypt_last.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_decrypt_last.c index 6a1ea9d..9ef69f7 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_decrypt_last.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_decrypt_last.c @@ -34,10 +34,6 @@ int ocb3_decrypt_last(ocb3_state *ocb, const unsigned char *ct, unsigned long ct LTC_ARGCHK(pt != NULL); } - if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) { - goto LBL_ERR; - } - full_blocks = ctlen/ocb->block_len; full_blocks_len = full_blocks * ocb->block_len; last_block_len = ctlen - full_blocks_len; @@ -54,7 +50,7 @@ int ocb3_decrypt_last(ocb3_state *ocb, const unsigned char *ct, unsigned long ct ocb3_int_xor_blocks(iOffset_star, ocb->Offset_current, ocb->L_star, ocb->block_len); /* Pad = ENCIPHER(K, Offset_*) */ - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(iOffset_star, iPad, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(iOffset_star, iPad, &ocb->key)) != CRYPT_OK) { goto LBL_ERR; } @@ -76,7 +72,7 @@ int ocb3_decrypt_last(ocb3_state *ocb, const unsigned char *ct, unsigned long ct for(x=0; xblock_len; x++) { ocb->tag_part[x] = (ocb->checksum[x] ^ iOffset_star[x]) ^ ocb->L_dollar[x]; } - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) { goto LBL_ERR; } } @@ -86,7 +82,7 @@ int ocb3_decrypt_last(ocb3_state *ocb, const unsigned char *ct, unsigned long ct for(x=0; xblock_len; x++) { ocb->tag_part[x] = (ocb->checksum[x] ^ ocb->Offset_current[x]) ^ ocb->L_dollar[x]; } - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) { goto LBL_ERR; } } diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_done.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_done.c index 688aa80..00a8c29 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_done.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_done.c @@ -24,9 +24,6 @@ int ocb3_done(ocb3_state *ocb, unsigned char *tag, unsigned long *taglen) LTC_ARGCHK(ocb != NULL); LTC_ARGCHK(tag != NULL); LTC_ARGCHK(taglen != NULL); - if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) { - goto LBL_ERR; - } /* check taglen */ if ((int)*taglen < ocb->tag_len) { @@ -52,7 +49,7 @@ int ocb3_done(ocb3_state *ocb, unsigned char *tag, unsigned long *taglen) } /* Sum = Sum_m xor ENCIPHER(K, CipherInput) */ - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(tmp, tmp, &ocb->key)) != CRYPT_OK) { goto LBL_ERR; } ocb3_int_xor_blocks(ocb->aSum_current, ocb->aSum_current, tmp, ocb->block_len); diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_encrypt.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_encrypt.c index 7f17715..152c1b5 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_encrypt.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_encrypt.c @@ -32,14 +32,7 @@ int ocb3_encrypt(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen, LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); - if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) { - return err; - } - if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) { - return CRYPT_INVALID_ARG; - } - - if (ptlen % ocb->block_len) { /* ptlen has to bu multiple of block_len */ + if (ptlen % ocb->block_len) { /* ptlen has to be multiple of block_len */ return CRYPT_INVALID_ARG; } @@ -55,7 +48,7 @@ int ocb3_encrypt(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen, ocb3_int_xor_blocks(tmp, pt_b, ocb->Offset_current, ocb->block_len); /* encrypt */ - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(tmp, tmp, &ocb->key)) != CRYPT_OK) { goto LBL_ERR; } diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_encrypt_last.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_encrypt_last.c index c5d7b68..81bfa5c 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_encrypt_last.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_encrypt_last.c @@ -34,10 +34,6 @@ int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long pt LTC_ARGCHK(ct != NULL); } - if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) { - goto LBL_ERR; - } - full_blocks = ptlen/ocb->block_len; full_blocks_len = full_blocks * ocb->block_len; last_block_len = ptlen - full_blocks_len; @@ -56,7 +52,7 @@ int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long pt ocb3_int_xor_blocks(iOffset_star, ocb->Offset_current, ocb->L_star, ocb->block_len); /* Pad = ENCIPHER(K, Offset_*) */ - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(iOffset_star, iPad, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(iOffset_star, iPad, &ocb->key)) != CRYPT_OK) { goto LBL_ERR; } @@ -78,7 +74,7 @@ int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long pt for(x=0; xblock_len; x++) { ocb->tag_part[x] = (ocb->checksum[x] ^ iOffset_star[x]) ^ ocb->L_dollar[x]; } - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) { goto LBL_ERR; } } else { @@ -87,7 +83,7 @@ int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long pt for(x=0; xblock_len; x++) { ocb->tag_part[x] = (ocb->checksum[x] ^ ocb->Offset_current[x]) ^ ocb->L_dollar[x]; } - if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) { goto LBL_ERR; } } diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_init.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_init.c index d85d006..51279a2 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_init.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_init.c @@ -34,7 +34,7 @@ static void s_ocb3_int_calc_offset_zero(ocb3_state *ocb, const unsigned char *no /* Ktop = ENCIPHER(K, Nonce[1..122] || zeros(6)) */ iNonce[ocb->block_len-1] = iNonce[ocb->block_len-1] & 0xC0; - if ((cipher_descriptor[ocb->cipher].ecb_encrypt(iNonce, iKtop, &ocb->key)) != CRYPT_OK) { + if ((ecb_encrypt_block(iNonce, iKtop, &ocb->key)) != CRYPT_OK) { zeromem(ocb->Offset_current, ocb->block_len); return; } @@ -100,7 +100,6 @@ int ocb3_init(ocb3_state *ocb, int cipher, if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { return err; } - ocb->cipher = cipher; /* Valid Nonce? * As of RFC7253: "string of no more than 120 bits" */ @@ -121,7 +120,7 @@ int ocb3_init(ocb3_state *ocb, int cipher, /* determine which polys to use */ ocb->block_len = cipher_descriptor[cipher].block_length; - x = (int)(sizeof(polys)/sizeof(polys[0])); + x = (int)LTC_ARRAY_SIZE(polys); for (poly = 0; poly < x; poly++) { if (polys[poly].len == ocb->block_len) { break; @@ -135,13 +134,13 @@ int ocb3_init(ocb3_state *ocb, int cipher, } /* schedule the key */ - if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, 0, &ocb->key)) != CRYPT_OK) { return err; } /* L_* = ENCIPHER(K, zeros(128)) */ zeromem(ocb->L_star, ocb->block_len); - if ((err = cipher_descriptor[cipher].ecb_encrypt(ocb->L_star, ocb->L_star, &ocb->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ocb->L_star, ocb->L_star, &ocb->key)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_int_ntz.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_int_ntz.c index 86942ce..a667ff6 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_int_ntz.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_int_ntz.c @@ -16,6 +16,11 @@ */ int ocb3_int_ntz(unsigned long x) { +#if defined(LTC_HAVE_CTZL_BUILTIN) + if (x == 0) + return sizeof(unsigned long) * CHAR_BIT; + return __builtin_ctzl(x); +#else int c; x &= 0xFFFFFFFFUL; c = 0; @@ -24,6 +29,7 @@ int ocb3_int_ntz(unsigned long x) x >>= 1; } return c; +#endif } #endif diff --git a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_test.c b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_test.c index 3a9816e..3c6cfe5 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_test.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/ocb3/ocb3_test.c @@ -209,7 +209,7 @@ int ocb3_test(void) } } - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { len = 16; /* must be the same as the required taglen */ if ((err = ocb3_encrypt_authenticate_memory(idx, key, sizeof(key), diff --git a/Sources/DataLiteC/libtomcrypt/encauth/siv/siv.c b/Sources/DataLiteC/libtomcrypt/encauth/siv/siv.c index 425dd9e..d3dd578 100644 --- a/Sources/DataLiteC/libtomcrypt/encauth/siv/siv.c +++ b/Sources/DataLiteC/libtomcrypt/encauth/siv/siv.c @@ -612,7 +612,7 @@ int siv_test(void) cipher = find_cipher("aes"); - for (n = 0; n < sizeof(siv_tests)/sizeof(siv_tests[0]); ++n) { + for (n = 0; n < LTC_ARRAY_SIZE(siv_tests); ++n) { buflen = sizeof(buf); if ((err = siv_encrypt_memory(cipher, siv_tests[n].Key, siv_tests[n].Keylen, diff --git a/Sources/DataLiteC/libtomcrypt/hashes/chc/chc.c b/Sources/DataLiteC/libtomcrypt/hashes/chc/chc.c index e82b2b3..3a394d4 100644 --- a/Sources/DataLiteC/libtomcrypt/hashes/chc/chc.c +++ b/Sources/DataLiteC/libtomcrypt/hashes/chc/chc.c @@ -277,7 +277,7 @@ int chc_test(void) oldhashidx = cipher_idx; chc_register(idx); - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { if ((err = chc_init(&md)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/hashes/rmd128.c b/Sources/DataLiteC/libtomcrypt/hashes/rmd128.c index 7d57af8..cc894e1 100644 --- a/Sources/DataLiteC/libtomcrypt/hashes/rmd128.c +++ b/Sources/DataLiteC/libtomcrypt/hashes/rmd128.c @@ -380,7 +380,7 @@ int rmd128_test(void) unsigned char tmp[16]; hash_state md; - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { rmd128_init(&md); rmd128_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg)); rmd128_done(&md, tmp); diff --git a/Sources/DataLiteC/libtomcrypt/hashes/rmd160.c b/Sources/DataLiteC/libtomcrypt/hashes/rmd160.c index 6e45a7e..d4cba27 100644 --- a/Sources/DataLiteC/libtomcrypt/hashes/rmd160.c +++ b/Sources/DataLiteC/libtomcrypt/hashes/rmd160.c @@ -439,7 +439,7 @@ int rmd160_test(void) unsigned char tmp[20]; hash_state md; - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { rmd160_init(&md); rmd160_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg)); rmd160_done(&md, tmp); diff --git a/Sources/DataLiteC/libtomcrypt/hashes/rmd256.c b/Sources/DataLiteC/libtomcrypt/hashes/rmd256.c index 704c86e..4eecd3f 100644 --- a/Sources/DataLiteC/libtomcrypt/hashes/rmd256.c +++ b/Sources/DataLiteC/libtomcrypt/hashes/rmd256.c @@ -405,7 +405,7 @@ int rmd256_test(void) unsigned char tmp[32]; hash_state md; - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { rmd256_init(&md); rmd256_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg)); rmd256_done(&md, tmp); diff --git a/Sources/DataLiteC/libtomcrypt/hashes/rmd320.c b/Sources/DataLiteC/libtomcrypt/hashes/rmd320.c index e25b6d9..1148b42 100644 --- a/Sources/DataLiteC/libtomcrypt/hashes/rmd320.c +++ b/Sources/DataLiteC/libtomcrypt/hashes/rmd320.c @@ -470,7 +470,7 @@ int rmd320_test(void) unsigned char tmp[40]; hash_state md; - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { rmd320_init(&md); rmd320_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg)); rmd320_done(&md, tmp); diff --git a/Sources/DataLiteC/libtomcrypt/hashes/tiger.c b/Sources/DataLiteC/libtomcrypt/hashes/tiger.c index e88fb3a..a1b66f7 100644 --- a/Sources/DataLiteC/libtomcrypt/hashes/tiger.c +++ b/Sources/DataLiteC/libtomcrypt/hashes/tiger.c @@ -566,7 +566,7 @@ static const ulong64 table[4*256] = { CONST64(0xC83223F1720AEF96) /* 1022 */, CONST64(0xC3A0396F7363A51F) /* 1023 */}; /* one round of the hash function */ -LTC_INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul) +static LTC_INLINE void s_tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul) { ulong64 tmp; tmp = (*c ^= x); @@ -582,14 +582,14 @@ LTC_INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x /* one complete pass */ static void s_pass(ulong64 *a, ulong64 *b, ulong64 *c, const ulong64 *x, int mul) { - tiger_round(a,b,c,x[0],mul); - tiger_round(b,c,a,x[1],mul); - tiger_round(c,a,b,x[2],mul); - tiger_round(a,b,c,x[3],mul); - tiger_round(b,c,a,x[4],mul); - tiger_round(c,a,b,x[5],mul); - tiger_round(a,b,c,x[6],mul); - tiger_round(b,c,a,x[7],mul); + s_tiger_round(a,b,c,x[0],mul); + s_tiger_round(b,c,a,x[1],mul); + s_tiger_round(c,a,b,x[2],mul); + s_tiger_round(a,b,c,x[3],mul); + s_tiger_round(b,c,a,x[4],mul); + s_tiger_round(c,a,b,x[5],mul); + s_tiger_round(a,b,c,x[6],mul); + s_tiger_round(b,c,a,x[7],mul); } /* The key mixing schedule */ diff --git a/Sources/DataLiteC/libtomcrypt/hashes/whirl/whirl.c b/Sources/DataLiteC/libtomcrypt/hashes/whirl/whirl.c index c1e4616..802cdc3 100644 --- a/Sources/DataLiteC/libtomcrypt/hashes/whirl/whirl.c +++ b/Sources/DataLiteC/libtomcrypt/hashes/whirl/whirl.c @@ -281,7 +281,7 @@ int whirlpool_test(void) unsigned char tmp[64]; hash_state md; - for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { + for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) { whirlpool_init(&md); whirlpool_process(&md, (unsigned char *)tests[i].msg, tests[i].len); whirlpool_done(&md, tmp); diff --git a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_cfg.h b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_cfg.h index 38eca37..af828e2 100644 --- a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_cfg.h +++ b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_cfg.h @@ -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 diff --git a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_cipher.h b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_cipher.h index 9ba04fe..60e6c9a 100644 --- a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_cipher.h +++ b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_cipher.h @@ -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; diff --git a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_custom.h b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_custom.h index b1731b9..31440b4 100644 --- a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_custom.h +++ b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_custom.h @@ -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 diff --git a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_mac.h b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_mac.h index 3ca09ff..2e067bc 100644 --- a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_mac.h +++ b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_mac.h @@ -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 */ diff --git a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_pk.h b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_pk.h index 8ba34d8..c4a2017 100644 --- a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_pk.h +++ b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_pk.h @@ -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); diff --git a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_private.h b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_private.h index f6b0bc2..7069b16 100644 --- a/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_private.h +++ b/Sources/DataLiteC/libtomcrypt/headers/tomcrypt_private.h @@ -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; diff --git a/Sources/DataLiteC/libtomcrypt/mac/f9/f9_done.c b/Sources/DataLiteC/libtomcrypt/mac/f9/f9_done.c index 38d1371..596a33b 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/f9/f9_done.c +++ b/Sources/DataLiteC/libtomcrypt/mac/f9/f9_done.c @@ -33,7 +33,7 @@ int f9_done(f9_state *f9, unsigned char *out, unsigned long *outlen) if (f9->buflen != 0) { /* encrypt */ - cipher_descriptor[f9->cipher].ecb_encrypt(f9->IV, f9->IV, &f9->key); + ecb_encrypt_block(f9->IV, f9->IV, &f9->key); f9->buflen = 0; for (x = 0; x < f9->blocksize; x++) { f9->ACC[x] ^= f9->IV[x]; @@ -41,13 +41,13 @@ int f9_done(f9_state *f9, unsigned char *out, unsigned long *outlen) } /* schedule modified key */ - if ((err = cipher_descriptor[f9->cipher].setup(f9->akey, f9->keylen, 0, &f9->key)) != CRYPT_OK) { + if ((err = ecb_start(f9->cipher, f9->akey, f9->keylen, 0, &f9->key)) != CRYPT_OK) { return err; } /* encrypt the ACC */ - cipher_descriptor[f9->cipher].ecb_encrypt(f9->ACC, f9->ACC, &f9->key); - cipher_descriptor[f9->cipher].done(&f9->key); + ecb_encrypt_block(f9->ACC, f9->ACC, &f9->key); + ecb_done(&f9->key); /* extract tag */ for (x = 0; x < f9->blocksize && (unsigned long)x < *outlen; x++) { diff --git a/Sources/DataLiteC/libtomcrypt/mac/f9/f9_init.c b/Sources/DataLiteC/libtomcrypt/mac/f9/f9_init.c index 60e25dc..8a6e630 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/f9/f9_init.c +++ b/Sources/DataLiteC/libtomcrypt/mac/f9/f9_init.c @@ -38,7 +38,7 @@ int f9_init(f9_state *f9, int cipher, const unsigned char *key, unsigned long ke } #endif - if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &f9->key)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, 0, &f9->key)) != CRYPT_OK) { goto done; } diff --git a/Sources/DataLiteC/libtomcrypt/mac/f9/f9_process.c b/Sources/DataLiteC/libtomcrypt/mac/f9/f9_process.c index e416b54..8860da3 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/f9/f9_process.c +++ b/Sources/DataLiteC/libtomcrypt/mac/f9/f9_process.c @@ -38,7 +38,7 @@ int f9_process(f9_state *f9, const unsigned char *in, unsigned long inlen) for (x = 0; x < f9->blocksize; x += sizeof(LTC_FAST_TYPE)) { *(LTC_FAST_TYPE_PTR_CAST(&(f9->IV[x]))) ^= *(LTC_FAST_TYPE_PTR_CAST(&(in[x]))); } - cipher_descriptor[f9->cipher].ecb_encrypt(f9->IV, f9->IV, &f9->key); + ecb_encrypt_block(f9->IV, f9->IV, &f9->key); for (x = 0; x < f9->blocksize; x += sizeof(LTC_FAST_TYPE)) { *(LTC_FAST_TYPE_PTR_CAST(&(f9->ACC[x]))) ^= *(LTC_FAST_TYPE_PTR_CAST(&(f9->IV[x]))); } @@ -50,7 +50,7 @@ int f9_process(f9_state *f9, const unsigned char *in, unsigned long inlen) while (inlen) { if (f9->buflen == f9->blocksize) { - cipher_descriptor[f9->cipher].ecb_encrypt(f9->IV, f9->IV, &f9->key); + ecb_encrypt_block(f9->IV, f9->IV, &f9->key); for (x = 0; x < f9->blocksize; x++) { f9->ACC[x] ^= f9->IV[x]; } diff --git a/Sources/DataLiteC/libtomcrypt/mac/f9/f9_test.c b/Sources/DataLiteC/libtomcrypt/mac/f9/f9_test.c index 779fdf1..2616d42 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/f9/f9_test.c +++ b/Sources/DataLiteC/libtomcrypt/mac/f9/f9_test.c @@ -48,7 +48,7 @@ int f9_test(void) return CRYPT_NOP; } - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { taglen = 4; if ((err = f9_memory(idx, tests[x].K, 16, tests[x].M, tests[x].msglen, T, &taglen)) != CRYPT_OK) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/mac/omac/omac_done.c b/Sources/DataLiteC/libtomcrypt/mac/omac/omac_done.c index c60067f..7997d7a 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/omac/omac_done.c +++ b/Sources/DataLiteC/libtomcrypt/mac/omac/omac_done.c @@ -24,9 +24,6 @@ int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen) LTC_ARGCHK(omac != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); - if ((err = cipher_is_valid(omac->cipher_idx)) != CRYPT_OK) { - return err; - } if ((omac->buflen > (int)sizeof(omac->block)) || (omac->buflen < 0) || (omac->blklen > (int)sizeof(omac->block)) || (omac->buflen > omac->blklen)) { @@ -53,10 +50,10 @@ int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen) } /* encrypt it */ - if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->block, omac->block, &omac->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(omac->block, omac->block, &omac->key)) != CRYPT_OK) { return err; } - cipher_descriptor[omac->cipher_idx].done(&omac->key); + ecb_done(&omac->key); /* output it */ for (x = 0; x < (unsigned)omac->blklen && x < *outlen; x++) { diff --git a/Sources/DataLiteC/libtomcrypt/mac/omac/omac_init.c b/Sources/DataLiteC/libtomcrypt/mac/omac/omac_init.c index 440bc01..f2e9857 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/omac/omac_init.c +++ b/Sources/DataLiteC/libtomcrypt/mac/omac/omac_init.c @@ -51,7 +51,7 @@ int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned l default: return CRYPT_INVALID_ARG; } - if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &omac->key)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, 0, &omac->key)) != CRYPT_OK) { return err; } @@ -59,7 +59,7 @@ int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned l /* first calc L which is Ek(0) */ zeromem(omac->Lu[0], cipher_descriptor[cipher].block_length); - if ((err = cipher_descriptor[cipher].ecb_encrypt(omac->Lu[0], omac->Lu[0], &omac->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(omac->Lu[0], omac->Lu[0], &omac->key)) != CRYPT_OK) { return err; } @@ -81,7 +81,6 @@ int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned l } /* setup state */ - omac->cipher_idx = cipher; omac->buflen = 0; omac->blklen = len; zeromem(omac->prev, sizeof(omac->prev)); diff --git a/Sources/DataLiteC/libtomcrypt/mac/omac/omac_process.c b/Sources/DataLiteC/libtomcrypt/mac/omac/omac_process.c index b8e3160..35ee9d2 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/omac/omac_process.c +++ b/Sources/DataLiteC/libtomcrypt/mac/omac/omac_process.c @@ -24,9 +24,6 @@ int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen) LTC_ARGCHK(omac != NULL); LTC_ARGCHK(in != NULL); - if ((err = cipher_is_valid(omac->cipher_idx)) != CRYPT_OK) { - return err; - } if ((omac->buflen > (int)sizeof(omac->block)) || (omac->buflen < 0) || (omac->blklen > (int)sizeof(omac->block)) || (omac->buflen > omac->blklen)) { @@ -34,22 +31,17 @@ int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen) } #ifdef LTC_FAST - { - unsigned long blklen = cipher_descriptor[omac->cipher_idx].block_length; - - if (omac->buflen == 0 && inlen > blklen) { - unsigned long y; - for (x = 0; x < (inlen - blklen); x += blklen) { - for (y = 0; y < blklen; y += sizeof(LTC_FAST_TYPE)) { - *(LTC_FAST_TYPE_PTR_CAST(&omac->prev[y])) ^= *(LTC_FAST_TYPE_PTR_CAST(&in[y])); - } - in += blklen; - if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->prev, omac->prev, &omac->key)) != CRYPT_OK) { - return err; - } - } - inlen -= x; - } + if (omac->buflen == 0 && inlen > (unsigned long)omac->blklen) { + for (x = 0; x < (inlen - omac->blklen); x += omac->blklen) { + for (n = 0; n < (unsigned long)omac->blklen; n += sizeof(LTC_FAST_TYPE)) { + *(LTC_FAST_TYPE_PTR_CAST(&omac->prev[n])) ^= *(LTC_FAST_TYPE_PTR_CAST(&in[n])); + } + in += omac->blklen; + if ((err = ecb_encrypt_block(omac->prev, omac->prev, &omac->key)) != CRYPT_OK) { + return err; + } + } + inlen -= x; } #endif @@ -59,7 +51,7 @@ int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen) for (x = 0; x < (unsigned long)omac->blklen; x++) { omac->block[x] ^= omac->prev[x]; } - if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->block, omac->prev, &omac->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(omac->block, omac->prev, &omac->key)) != CRYPT_OK) { return err; } omac->buflen = 0; diff --git a/Sources/DataLiteC/libtomcrypt/mac/omac/omac_test.c b/Sources/DataLiteC/libtomcrypt/mac/omac/omac_test.c index bca6d9c..c797794 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/omac/omac_test.c +++ b/Sources/DataLiteC/libtomcrypt/mac/omac/omac_test.c @@ -76,7 +76,7 @@ int omac_test(void) } } - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { len = sizeof(out); if ((err = omac_memory(idx, tests[x].key, tests[x].keylen, tests[x].msg, tests[x].msglen, out, &len)) != CRYPT_OK) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/mac/pelican/pelican_test.c b/Sources/DataLiteC/libtomcrypt/mac/pelican/pelican_test.c index 2ec696a..31a237c 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/pelican/pelican_test.c +++ b/Sources/DataLiteC/libtomcrypt/mac/pelican/pelican_test.c @@ -80,7 +80,7 @@ int pelican_test(void) unsigned char out[16]; pelican_state pel; - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { if ((err = pelican_init(&pel, tests[x].K, tests[x].keylen)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_done.c b/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_done.c index 222790d..bcad06a 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_done.c +++ b/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_done.c @@ -15,9 +15,6 @@ int pmac_done(pmac_state *pmac, unsigned char *out, unsigned long *outlen) LTC_ARGCHK(pmac != NULL); LTC_ARGCHK(out != NULL); - if ((err = cipher_is_valid(pmac->cipher_idx)) != CRYPT_OK) { - return err; - } if ((pmac->buflen > (int)sizeof(pmac->block)) || (pmac->buflen < 0) || (pmac->block_len > (int)sizeof(pmac->block)) || (pmac->buflen > pmac->block_len)) { @@ -41,10 +38,10 @@ int pmac_done(pmac_state *pmac, unsigned char *out, unsigned long *outlen) } /* encrypt it */ - if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(pmac->checksum, pmac->checksum, &pmac->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(pmac->checksum, pmac->checksum, &pmac->key)) != CRYPT_OK) { return err; } - cipher_descriptor[pmac->cipher_idx].done(&pmac->key); + ecb_done(&pmac->key); /* store it */ for (x = 0; x < pmac->block_len && x < (int)*outlen; x++) { diff --git a/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_init.c b/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_init.c index 9787e6a..fb5daa9 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_init.c +++ b/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_init.c @@ -55,12 +55,12 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l /* determine which polys to use */ pmac->block_len = cipher_descriptor[cipher].block_length; - for (poly = 0; poly < (int)(sizeof(polys)/sizeof(polys[0])); poly++) { + for (poly = 0; poly < (int)LTC_ARRAY_SIZE(polys); poly++) { if (polys[poly].len == pmac->block_len) { break; } } - if (poly >= (int)(sizeof(polys)/sizeof(polys[0]))) { + if (poly >= (int)LTC_ARRAY_SIZE(polys)) { return CRYPT_INVALID_ARG; } if (polys[poly].len != pmac->block_len) { @@ -75,7 +75,7 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l /* schedule the key */ - if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &pmac->key)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, 0, &pmac->key)) != CRYPT_OK) { return err; } @@ -87,7 +87,7 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l /* find L = E[0] */ zeromem(L, pmac->block_len); - if ((err = cipher_descriptor[cipher].ecb_encrypt(L, L, &pmac->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(L, L, &pmac->key)) != CRYPT_OK) { goto error; } @@ -124,7 +124,6 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l /* zero buffer, counters, etc... */ pmac->block_index = 1; - pmac->cipher_idx = cipher; pmac->buflen = 0; zeromem(pmac->block, sizeof(pmac->block)); zeromem(pmac->Li, sizeof(pmac->Li)); diff --git a/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_ntz.c b/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_ntz.c index ed71f33..86142ba 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_ntz.c +++ b/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_ntz.c @@ -14,6 +14,11 @@ */ int pmac_ntz(unsigned long x) { +#if defined(LTC_HAVE_CTZL_BUILTIN) + if (x == 0) + return sizeof(unsigned long) * CHAR_BIT; + return __builtin_ctzl(x); +#else int c; x &= 0xFFFFFFFFUL; c = 0; @@ -22,6 +27,7 @@ int pmac_ntz(unsigned long x) x >>= 1; } return c; +#endif } #endif diff --git a/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_process.c b/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_process.c index 7b654f4..b154aef 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_process.c +++ b/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_process.c @@ -29,9 +29,6 @@ int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen) LTC_ARGCHK(pmac != NULL); LTC_ARGCHK(in != NULL); - if ((err = cipher_is_valid(pmac->cipher_idx)) != CRYPT_OK) { - return err; - } if ((pmac->buflen > (int)sizeof(pmac->block)) || (pmac->buflen < 0) || (pmac->block_len > (int)sizeof(pmac->block)) || (pmac->buflen > pmac->block_len)) { @@ -46,7 +43,7 @@ int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen) for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) { *(LTC_FAST_TYPE_PTR_CAST(&Z[y])) = *(LTC_FAST_TYPE_PTR_CAST(&in[y])) ^ *(LTC_FAST_TYPE_PTR_CAST(&pmac->Li[y])); } - if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(Z, Z, &pmac->key)) != CRYPT_OK) { return err; } for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) { @@ -65,7 +62,7 @@ int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen) for (x = 0; x < (unsigned long)pmac->block_len; x++) { Z[x] = pmac->Li[x] ^ pmac->block[x]; } - if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(Z, Z, &pmac->key)) != CRYPT_OK) { return err; } for (x = 0; x < (unsigned long)pmac->block_len; x++) { diff --git a/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_test.c b/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_test.c index 3ccf06e..ccbd6c6 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_test.c +++ b/Sources/DataLiteC/libtomcrypt/mac/pmac/pmac_test.c @@ -124,7 +124,7 @@ int pmac_test(void) } } - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { len = sizeof(outtag); if ((err = pmac_memory(idx, tests[x].key, 16, tests[x].msg, tests[x].msglen, outtag, &len)) != CRYPT_OK) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_done.c b/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_done.c index 7da72f3..4163e22 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_done.c +++ b/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_done.c @@ -17,17 +17,11 @@ */ int xcbc_done(xcbc_state *xcbc, unsigned char *out, unsigned long *outlen) { - int err, x; + int x; LTC_ARGCHK(xcbc != NULL); LTC_ARGCHK(out != NULL); - /* check structure */ - if ((err = cipher_is_valid(xcbc->cipher)) != CRYPT_OK) { - return err; - } - - if ((xcbc->blocksize > cipher_descriptor[xcbc->cipher].block_length) || (xcbc->blocksize < 0) || - (xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) { + if ((xcbc->blocksize < 0) || (xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) { return CRYPT_INVALID_ARG; } @@ -46,8 +40,8 @@ int xcbc_done(xcbc_state *xcbc, unsigned char *out, unsigned long *outlen) } /* encrypt */ - cipher_descriptor[xcbc->cipher].ecb_encrypt(xcbc->IV, xcbc->IV, &xcbc->key); - cipher_descriptor[xcbc->cipher].done(&xcbc->key); + ecb_encrypt_block(xcbc->IV, xcbc->IV, &xcbc->key); + ecb_done(&xcbc->key); /* extract tag */ for (x = 0; x < xcbc->blocksize && (unsigned long)x < *outlen; x++) { diff --git a/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_init.c b/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_init.c index a80f9e4..10c55da 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_init.c +++ b/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_init.c @@ -23,7 +23,7 @@ int xcbc_init(xcbc_state *xcbc, int cipher, const unsigned char *key, unsigned long keylen) { int x, y, err; - symmetric_key *skey; + symmetric_ECB *skey; unsigned long k1; LTC_ARGCHK(xcbc != NULL); @@ -64,7 +64,7 @@ int xcbc_init(xcbc_state *xcbc, int cipher, const unsigned char *key, unsigned l return CRYPT_MEM; } - if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, skey)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, 0, skey)) != CRYPT_OK) { goto done; } @@ -73,20 +73,19 @@ int xcbc_init(xcbc_state *xcbc, int cipher, const unsigned char *key, unsigned l for (x = 0; x < cipher_descriptor[cipher].block_length; x++) { xcbc->K[y][x] = y + 1; } - cipher_descriptor[cipher].ecb_encrypt(xcbc->K[y], xcbc->K[y], skey); + ecb_encrypt_block(xcbc->K[y], xcbc->K[y], skey); } } /* setup K1 */ - err = cipher_descriptor[cipher].setup(xcbc->K[0], k1, 0, &xcbc->key); + err = ecb_start(cipher, xcbc->K[0], k1, 0, &xcbc->key); /* setup struct */ zeromem(xcbc->IV, cipher_descriptor[cipher].block_length); xcbc->blocksize = cipher_descriptor[cipher].block_length; - xcbc->cipher = cipher; xcbc->buflen = 0; done: - cipher_descriptor[cipher].done(skey); + ecb_done(skey); if (skey != NULL) { #ifdef LTC_CLEAN_STACK zeromem(skey, sizeof(*skey)); diff --git a/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_process.c b/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_process.c index 3cb0c46..a6e5145 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_process.c +++ b/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_process.c @@ -17,7 +17,6 @@ */ int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen) { - int err; #ifdef LTC_FAST int x; #endif @@ -25,13 +24,7 @@ int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen) LTC_ARGCHK(xcbc != NULL); LTC_ARGCHK(in != NULL); - /* check structure */ - if ((err = cipher_is_valid(xcbc->cipher)) != CRYPT_OK) { - return err; - } - - if ((xcbc->blocksize > cipher_descriptor[xcbc->cipher].block_length) || (xcbc->blocksize < 0) || - (xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) { + if ((xcbc->blocksize < 0) || (xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) { return CRYPT_INVALID_ARG; } @@ -41,7 +34,7 @@ int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen) for (x = 0; x < xcbc->blocksize; x += sizeof(LTC_FAST_TYPE)) { *(LTC_FAST_TYPE_PTR_CAST(&(xcbc->IV[x]))) ^= *(LTC_FAST_TYPE_PTR_CAST(&(in[x]))); } - cipher_descriptor[xcbc->cipher].ecb_encrypt(xcbc->IV, xcbc->IV, &xcbc->key); + ecb_encrypt_block(xcbc->IV, xcbc->IV, &xcbc->key); in += xcbc->blocksize; inlen -= xcbc->blocksize; } @@ -50,7 +43,7 @@ int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen) while (inlen) { if (xcbc->buflen == xcbc->blocksize) { - cipher_descriptor[xcbc->cipher].ecb_encrypt(xcbc->IV, xcbc->IV, &xcbc->key); + ecb_encrypt_block(xcbc->IV, xcbc->IV, &xcbc->key); xcbc->buflen = 0; } xcbc->IV[xcbc->buflen++] ^= *in++; diff --git a/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_test.c b/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_test.c index 23555de..2c61105 100644 --- a/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_test.c +++ b/Sources/DataLiteC/libtomcrypt/mac/xcbc/xcbc_test.c @@ -98,7 +98,7 @@ int xcbc_test(void) } } - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { taglen = 16; if ((err = xcbc_memory(idx, tests[x].K, 16, tests[x].M, tests[x].msglen, T, &taglen)) != CRYPT_OK) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/math/ltm_desc.c b/Sources/DataLiteC/libtomcrypt/math/ltm_desc.c index a7df222..99c9f14 100644 --- a/Sources/DataLiteC/libtomcrypt/math/ltm_desc.c +++ b/Sources/DataLiteC/libtomcrypt/math/ltm_desc.c @@ -33,7 +33,7 @@ static int mpi_to_ltc_error(mp_err err) { size_t x; - for (x = 0; x < sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0]); x++) { + for (x = 0; x < LTC_ARRAY_SIZE(mpi_to_ltc_codes); x++) { if (err == mpi_to_ltc_codes[x].mpi_code) { return mpi_to_ltc_codes[x].ltc_code; } diff --git a/Sources/DataLiteC/libtomcrypt/math/tfm_desc.c b/Sources/DataLiteC/libtomcrypt/math/tfm_desc.c index 94a1d78..7c3b400 100644 --- a/Sources/DataLiteC/libtomcrypt/math/tfm_desc.c +++ b/Sources/DataLiteC/libtomcrypt/math/tfm_desc.c @@ -39,7 +39,7 @@ static int tfm_to_ltc_error(int err) { int x; - for (x = 0; x < (int)(sizeof(tfm_to_ltc_codes)/sizeof(tfm_to_ltc_codes[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tfm_to_ltc_codes); x++) { if (err == tfm_to_ltc_codes[x].tfm_code) { return tfm_to_ltc_codes[x].ltc_code; } diff --git a/Sources/DataLiteC/libtomcrypt/misc/base16/base16_encode.c b/Sources/DataLiteC/libtomcrypt/misc/base16/base16_encode.c index 649a4d8..206827c 100644 --- a/Sources/DataLiteC/libtomcrypt/misc/base16/base16_encode.c +++ b/Sources/DataLiteC/libtomcrypt/misc/base16/base16_encode.c @@ -52,10 +52,11 @@ int base16_encode(const unsigned char *in, unsigned long inlen, alphabet = alphabets[1]; } - for (i = 0; i < x; i += 2) { - out[i] = alphabet[(in[i/2] >> 4) & 0x0f]; - out[i+1] = alphabet[in[i/2] & 0x0f]; + for (i = x; i > 0; i -= 2) { + out[i-2] = alphabet[(in[(i-1)/2] >> 4) & 0x0f]; + out[i-1] = alphabet[in[(i-1)/2] & 0x0f]; } + out[x] = '\0'; return CRYPT_OK; diff --git a/Sources/DataLiteC/libtomcrypt/misc/crypt/crypt.c b/Sources/DataLiteC/libtomcrypt/misc/crypt/crypt.c index c4675de..61b5cd0 100644 --- a/Sources/DataLiteC/libtomcrypt/misc/crypt/crypt.c +++ b/Sources/DataLiteC/libtomcrypt/misc/crypt/crypt.c @@ -548,6 +548,15 @@ const char *crypt_build_settings = " LTC_NO_ROLC " #endif #endif +#if defined(LTC_HAVE_ROTATE_BUILTIN) + " LTC_HAVE_ROTATE_BUILTIN " +#endif +#if defined(LTC_HAVE_CLZL_BUILTIN) + " LTC_HAVE_CLZL_BUILTIN " +#endif +#if defined(LTC_HAVE_CTZL_BUILTIN) + " LTC_HAVE_CTZL_BUILTIN " +#endif #if defined(LTC_NO_TEST) " LTC_NO_TEST " #endif diff --git a/Sources/DataLiteC/libtomcrypt/misc/crypt/crypt_register_all_hashes.c b/Sources/DataLiteC/libtomcrypt/misc/crypt/crypt_register_all_hashes.c index 328e84a..362205f 100644 --- a/Sources/DataLiteC/libtomcrypt/misc/crypt/crypt_register_all_hashes.c +++ b/Sources/DataLiteC/libtomcrypt/misc/crypt/crypt_register_all_hashes.c @@ -89,8 +89,13 @@ int register_all_hashes(void) REGISTER_HASH(&blake2b_512_desc); #endif #ifdef LTC_CHC_HASH - REGISTER_HASH(&chc_desc); - LTC_ARGCHK(chc_register(find_cipher_any("aes", 8, 16)) == CRYPT_OK); + { + int aes_index = find_cipher_any("aes", 8, 16); + if (aes_index != -1) { + REGISTER_HASH(&chc_desc); + LTC_ARGCHK(chc_register(aes_index) == CRYPT_OK); + } + } #endif return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/misc/error_to_string.c b/Sources/DataLiteC/libtomcrypt/misc/error_to_string.c index 631fdbc..fd306bb 100644 --- a/Sources/DataLiteC/libtomcrypt/misc/error_to_string.c +++ b/Sources/DataLiteC/libtomcrypt/misc/error_to_string.c @@ -54,7 +54,7 @@ static const char * const err_2_str[CRYPT_ERR_NUM] = "The PEM header was not recognized", }; -LTC_STATIC_ASSERT(correct_err_2_str_size, (sizeof(err_2_str)/sizeof(err_2_str[0])) == CRYPT_ERR_NUM) +LTC_STATIC_ASSERT(correct_err_2_str_size, LTC_ARRAY_SIZE(err_2_str) == CRYPT_ERR_NUM) /** Convert an LTC error code to ASCII diff --git a/Sources/DataLiteC/libtomcrypt/misc/pbes/pbes2.c b/Sources/DataLiteC/libtomcrypt/misc/pbes/pbes2.c index cfb3426..75d17d3 100644 --- a/Sources/DataLiteC/libtomcrypt/misc/pbes/pbes2.c +++ b/Sources/DataLiteC/libtomcrypt/misc/pbes/pbes2.c @@ -51,7 +51,7 @@ static const oid_to_pbes s_pbes2_list[] = { static int s_pbes2_from_oid(const ltc_asn1_list *cipher_oid, const ltc_asn1_list *hmac_oid, pbes_properties *res) { unsigned int i; - for (i = 0; i < sizeof(s_pbes2_list)/sizeof(s_pbes2_list[0]); ++i) { + for (i = 0; i < LTC_ARRAY_SIZE(s_pbes2_list); ++i) { if (pk_oid_cmp_with_asn1(s_pbes2_list[i].oid, cipher_oid) == CRYPT_OK) { *res = *s_pbes2_list[i].data; break; @@ -59,7 +59,7 @@ static int s_pbes2_from_oid(const ltc_asn1_list *cipher_oid, const ltc_asn1_list } if (res->c == NULL) return CRYPT_INVALID_CIPHER; if (hmac_oid != NULL) { - for (i = 0; i < sizeof(s_hmac_oid_names)/sizeof(s_hmac_oid_names[0]); ++i) { + for (i = 0; i < LTC_ARRAY_SIZE(s_hmac_oid_names); ++i) { if (pk_oid_cmp_with_asn1(s_hmac_oid_names[i].oid, hmac_oid) == CRYPT_OK) { res->h = s_hmac_oid_names[i].id; return CRYPT_OK; diff --git a/Sources/DataLiteC/libtomcrypt/misc/pem/pem.c b/Sources/DataLiteC/libtomcrypt/misc/pem/pem.c index 35a8fac..ca6e350 100644 --- a/Sources/DataLiteC/libtomcrypt/misc/pem/pem.c +++ b/Sources/DataLiteC/libtomcrypt/misc/pem/pem.c @@ -69,7 +69,7 @@ const struct pem_header_id pem_std_headers[] = { .pka = LTC_PKA_DSA, }, }; -const unsigned long pem_std_headers_num = sizeof(pem_std_headers)/sizeof(pem_std_headers[0]); +const unsigned long pem_std_headers_num = LTC_ARRAY_SIZE(pem_std_headers); /* Encrypted PEM files */ const struct str pem_proc_type_encrypted = { SET_CSTR(, "Proc-Type: 4,ENCRYPTED") }; @@ -151,7 +151,7 @@ const struct blockcipher_info pem_dek_infos[] = { .name = "SEED-CFB,", .algo = "seed", .keylen = 128 / 8, .mode = cm_cfb, }, { .name = "SEED-OFB,", .algo = "seed", .keylen = 128 / 8, .mode = cm_ofb, }, }; -const unsigned long pem_dek_infos_num = sizeof(pem_dek_infos)/sizeof(pem_dek_infos[0]); +const unsigned long pem_dek_infos_num = LTC_ARRAY_SIZE(pem_dek_infos); int pem_decrypt(unsigned char *data, unsigned long *datalen, unsigned char *key, unsigned long keylen, @@ -201,7 +201,7 @@ int pem_decrypt(unsigned char *data, unsigned long *datalen, goto error_out; } - if ((err = padding_depad(data, datalen, padding | s.ctx.cbc.blocklen)) != CRYPT_OK) { + if ((err = padding_depad(data, datalen, padding | s.ctx.cbc.ecb.blocklen)) != CRYPT_OK) { goto error_out; } #else diff --git a/Sources/DataLiteC/libtomcrypt/misc/pem/pem_pkcs.c b/Sources/DataLiteC/libtomcrypt/misc/pem/pem_pkcs.c index c8fc1bf..682d819 100644 --- a/Sources/DataLiteC/libtomcrypt/misc/pem/pem_pkcs.c +++ b/Sources/DataLiteC/libtomcrypt/misc/pem/pem_pkcs.c @@ -16,7 +16,7 @@ extern const struct pem_header_id pem_std_headers[]; extern const unsigned long pem_std_headers_num; -static int s_decrypt_pem(unsigned char *pem, unsigned long *l, const struct pem_headers *hdr) +static int s_decrypt_pem(unsigned char *asn1_cert, unsigned long *asn1_len, const struct pem_headers *hdr) { unsigned char iv[MAXBLOCKSIZE], key[MAXBLOCKSIZE]; unsigned long ivlen, klen; @@ -38,7 +38,7 @@ static int s_decrypt_pem(unsigned char *pem, unsigned long *l, const struct pem_ return err; } - err = pem_decrypt(pem, l, key, klen, iv, ivlen, NULL, 0, &hdr->info, LTC_PAD_PKCS7); + err = pem_decrypt(asn1_cert, asn1_len, key, klen, iv, ivlen, NULL, 0, &hdr->info, LTC_PAD_PKCS7); zeromem(key, sizeof(key)); zeromem(iv, sizeof(iv)); @@ -86,12 +86,12 @@ static const import_fn s_import_x509_fns[LTC_PKA_NUM] = { #endif }; -static int s_import_x509(unsigned char *pem, unsigned long l, ltc_pka_key *k) +static int s_import_x509(unsigned char *asn1_cert, unsigned long asn1_len, ltc_pka_key *k) { enum ltc_pka_id pka = LTC_PKA_UNDEF; ltc_asn1_list *d, *spki; int err; - if ((err = x509_decode_spki(pem, l, &d, &spki)) != CRYPT_OK) { + if ((err = x509_decode_spki(asn1_cert, asn1_len, &d, &spki)) != CRYPT_OK) { return err; } err = s_get_pka(spki, &pka); @@ -100,23 +100,23 @@ static int s_import_x509(unsigned char *pem, unsigned long l, ltc_pka_key *k) return err; } if (pka < 0 - || pka > sizeof(s_import_x509_fns)/sizeof(s_import_x509_fns[0]) + || pka > LTC_ARRAY_SIZE(s_import_x509_fns) || s_import_x509_fns[pka] == NULL) { return CRYPT_PK_INVALID_TYPE; } - if ((err = s_import_x509_fns[pka](pem, l, &k->u)) == CRYPT_OK) { + if ((err = s_import_x509_fns[pka](asn1_cert, asn1_len, &k->u)) == CRYPT_OK) { k->id = pka; } return err; } -static int s_import_pkcs8(unsigned char *pem, unsigned long l, ltc_pka_key *k, const password_ctx *pw_ctx) +static int s_import_pkcs8(unsigned char *asn1_cert, unsigned long asn1_len, ltc_pka_key *k, const password_ctx *pw_ctx) { int err; enum ltc_oid_id pka; ltc_asn1_list *alg_id, *priv_key; ltc_asn1_list *p8_asn1 = NULL; - if ((err = pkcs8_decode_flexi(pem, l, pw_ctx, &p8_asn1)) != CRYPT_OK) { + if ((err = pkcs8_decode_flexi(asn1_cert, asn1_len, pw_ctx, &p8_asn1)) != CRYPT_OK) { goto cleanup; } if ((err = pkcs8_get_children(p8_asn1, &pka, &alg_id, &priv_key)) != CRYPT_OK) { @@ -168,11 +168,11 @@ cleanup: return err; } -static int s_extract_pka(unsigned char *pem, unsigned long w, enum ltc_pka_id *pka) +static int s_extract_pka(unsigned char *asn1_cert, unsigned long asn1_len, enum ltc_pka_id *pka) { ltc_asn1_list *pub; int err = CRYPT_ERROR; - if ((err = der_decode_sequence_flexi(pem, &w, &pub)) != CRYPT_OK) { + if ((err = der_decode_sequence_flexi(asn1_cert, &asn1_len, &pub)) != CRYPT_OK) { return err; } err = s_get_pka(pub, pka); @@ -198,8 +198,8 @@ static const import_fn s_import_openssl_fns[LTC_PKA_NUM] = { static int s_decode(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_ctx) { - unsigned char *pem = NULL; - unsigned long w, l, n; + unsigned char *asn1_cert = NULL; + unsigned long w, asn1_len, n; int err = CRYPT_ERROR; struct pem_headers hdr = { 0 }; struct password pw = { 0 }; @@ -207,10 +207,10 @@ static int s_decode(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_c XMEMSET(k, 0, sizeof(*k)); w = LTC_PEM_READ_BUFSIZE * 2; retry: - pem = XREALLOC(pem, w); + asn1_cert = XREALLOC(asn1_cert, w); for (n = 0; n < pem_std_headers_num; ++n) { hdr.id = &pem_std_headers[n]; - err = pem_read(pem, &w, &hdr, g); + err = pem_read(asn1_cert, &w, &hdr, g); if (err == CRYPT_BUFFER_OVERFLOW) { goto retry; } else if (err == CRYPT_OK) { @@ -223,15 +223,15 @@ retry: /* id not found */ if (hdr.id == NULL) goto cleanup; - l = w; + asn1_len = w; if (hdr.id->flags & pf_pkcs8) { - err = s_import_pkcs8(pem, l, k, pw_ctx); + err = s_import_pkcs8(asn1_cert, asn1_len, k, pw_ctx); goto cleanup; } else if (hdr.id->flags == pf_x509) { - err = s_import_x509(pem, l, k); + err = s_import_x509(asn1_cert, asn1_len, k); goto cleanup; } else if ((hdr.id->flags & pf_public) && hdr.id->pka == LTC_PKA_UNDEF) { - if ((err = s_extract_pka(pem, w, &pka)) != CRYPT_OK) { + if ((err = s_extract_pka(asn1_cert, asn1_len, &pka)) != CRYPT_OK) { goto cleanup; } } else if (hdr.encrypted) { @@ -246,7 +246,7 @@ retry: goto cleanup; } - if ((err = s_decrypt_pem(pem, &l, &hdr)) != CRYPT_OK) { + if ((err = s_decrypt_pem(asn1_cert, &asn1_len, &hdr)) != CRYPT_OK) { goto cleanup; } pka = hdr.id->pka; @@ -255,18 +255,18 @@ retry: } if (pka < 0 - || pka > sizeof(s_import_openssl_fns)/sizeof(s_import_openssl_fns[0]) + || pka > LTC_ARRAY_SIZE(s_import_openssl_fns) || s_import_openssl_fns[pka] == NULL) { err = CRYPT_PK_INVALID_TYPE; goto cleanup; } - if ((err = s_import_openssl_fns[pka](pem, l, &k->u)) == CRYPT_OK) { + if ((err = s_import_openssl_fns[pka](asn1_cert, asn1_len, &k->u)) == CRYPT_OK) { k->id = pka; } cleanup: password_free(hdr.pw, pw_ctx); - XFREE(pem); + XFREE(asn1_cert); return err; } diff --git a/Sources/DataLiteC/libtomcrypt/misc/pem/pem_read.c b/Sources/DataLiteC/libtomcrypt/misc/pem/pem_read.c index 3625b1a..abefff9 100644 --- a/Sources/DataLiteC/libtomcrypt/misc/pem/pem_read.c +++ b/Sources/DataLiteC/libtomcrypt/misc/pem/pem_read.c @@ -62,10 +62,10 @@ static void s_tts(char *buf, unsigned long *buflen) } } -static char* s_get_line(char *buf, unsigned long *buflen, struct get_char *g) +static char* s_get_line_i(char *buf, unsigned long *buflen, struct get_char *g, int search_for_start) { - unsigned long blen = 0; - int c = -1, c_; + unsigned long blen = 0, wr = 0; + int c_; if (g->unget_buf.p) { if (*buflen < g->unget_buf.len) { return NULL; @@ -75,30 +75,44 @@ static char* s_get_line(char *buf, unsigned long *buflen, struct get_char *g) RESET_STR(g->unget_buf); return buf; } - while(blen < *buflen) { - c_ = c; - c = g->get(g); - if (c == '\n') { - buf[blen] = '\0'; + if (g->prev_get == -1) { + return NULL; + } + while(blen < *buflen || search_for_start) { + wr = blen < *buflen ? blen : *buflen - 1; + c_ = g->prev_get; + g->prev_get = g->get(g); + if (g->prev_get == '\n') { + buf[wr] = '\0'; if (c_ == '\r') { - buf[--blen] = '\0'; + buf[--wr] = '\0'; } - s_tts(buf, &blen); - *buflen = blen; + s_tts(buf, &wr); + *buflen = wr; return buf; } - if (c == -1 || c == '\0') { - buf[blen] = '\0'; - s_tts(buf, &blen); - *buflen = blen; + if (g->prev_get == -1 || g->prev_get == '\0') { + buf[wr] = '\0'; + s_tts(buf, &wr); + *buflen = wr; return buf; } - buf[blen] = c; + buf[wr] = g->prev_get; blen++; } return NULL; } +LTC_INLINE static char* s_get_first_line(char *buf, unsigned long *buflen, struct get_char *g) +{ + return s_get_line_i(buf, buflen, g, 1); +} + +LTC_INLINE static char* s_get_line(char *buf, unsigned long *buflen, struct get_char *g) +{ + return s_get_line_i(buf, buflen, g, 0); +} + static LTC_INLINE int s_fits_buf(void *dest, unsigned long to_write, void *end) { unsigned char *d = dest; @@ -176,20 +190,29 @@ static int s_pem_decode_headers(struct pem_headers *hdr, struct get_char *g) return CRYPT_OK; } -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) { char buf[LTC_PEM_DECODE_BUFSZ]; - char *wpem = pem; - char *end = wpem + *w; + char *wpem = asn1_cert; + char *end = wpem + *asn1_len; + const char pem_start[] = "----"; unsigned long slen, linelen; int err, hdr_ok = 0; int would_overflow = 0; unsigned char empty_lines = 0; - linelen = sizeof(buf); - if (s_get_line(buf, &linelen, g) == NULL) { - return CRYPT_INVALID_PACKET; - } + g->prev_get = 0; + do { + linelen = sizeof(buf); + if (s_get_first_line(buf, &linelen, g) == NULL) { + if (g->prev_get == -1) + return CRYPT_NOP; + else + return CRYPT_INVALID_PACKET; + } + if (linelen < sizeof(pem_start) - 1) + continue; + } while(XMEMCMP(buf, pem_start, sizeof(pem_start) - 1) != 0); if (hdr->id->start.len != linelen || XMEMCMP(buf, hdr->id->start.p, hdr->id->start.len)) { s_unget_line(buf, linelen, g); return CRYPT_UNKNOWN_PEM; @@ -226,16 +249,16 @@ int pem_read(void *pem, unsigned long *w, struct pem_headers *hdr, struct get_ch /* NUL termination */ wpem++; /* prevent a wrap-around */ - if (wpem < (char*)pem) + if (wpem < (char*)asn1_cert) return CRYPT_OVERFLOW; - *w = wpem - (char*)pem; + *asn1_len = wpem - (char*)asn1_cert; return CRYPT_BUFFER_OVERFLOW; } - *w = wpem - (char*)pem; + *asn1_len = wpem - (char*)asn1_cert; *wpem++ = '\0'; - if ((err = base64_strict_decode(pem, *w, pem, w)) != CRYPT_OK) { + if ((err = base64_strict_decode(asn1_cert, *asn1_len, asn1_cert, asn1_len)) != CRYPT_OK) { return err; } return CRYPT_OK; diff --git a/Sources/DataLiteC/libtomcrypt/misc/pem/pem_ssh.c b/Sources/DataLiteC/libtomcrypt/misc/pem/pem_ssh.c index fe096d5..18a18d0 100644 --- a/Sources/DataLiteC/libtomcrypt/misc/pem/pem_ssh.c +++ b/Sources/DataLiteC/libtomcrypt/misc/pem/pem_ssh.c @@ -51,7 +51,7 @@ const struct blockcipher_info ssh_ciphers[] = { .name = "twofish256-cbc", .algo = "twofish", .keylen = 256 / 8, .mode = cm_cbc }, { .name = "twofish256-ctr", .algo = "twofish", .keylen = 256 / 8, .mode = cm_ctr }, }; -const unsigned long ssh_ciphers_num = sizeof(ssh_ciphers)/sizeof(ssh_ciphers[0]); +const unsigned long ssh_ciphers_num = LTC_ARRAY_SIZE(ssh_ciphers); struct kdf_options { const char *name; @@ -402,7 +402,7 @@ static int s_decode_key(const unsigned char *in, unsigned long *inlen, ltc_pka_k remaining -= cur_len; cur_len = remaining; - for (n = 0; n < sizeof(ssh_pkas)/sizeof(ssh_pkas[0]); ++n) { + for (n = 0; n < LTC_ARRAY_SIZE(ssh_pkas); ++n) { if (ssh_pkas[n].name.p != NULL) { if (pkalen != ssh_pkas[n].name.len || XMEMCMP(pka, ssh_pkas[n].name.p, ssh_pkas[n].name.len) != 0) continue; @@ -415,7 +415,7 @@ static int s_decode_key(const unsigned char *in, unsigned long *inlen, ltc_pka_k } break; } - if (n == sizeof(ssh_pkas)/sizeof(ssh_pkas[0])) { + if (n == LTC_ARRAY_SIZE(ssh_pkas)) { return CRYPT_PK_INVALID_TYPE; } @@ -490,7 +490,7 @@ static int s_parse_line(char *line, unsigned long *len, ltc_pka_key *key, char * rlen = *len; /* Chop up string into the three authorized_keys_elements */ - for (n = 0; n < sizeof(elements)/sizeof(elements[0]) && rlen; ++n) { + for (n = 0; n < LTC_ARRAY_SIZE(elements) && rlen; ++n) { skip_spaces(&r, &rlen); elements[n].p = r; if (n != 2) @@ -502,7 +502,7 @@ static int s_parse_line(char *line, unsigned long *len, ltc_pka_key *key, char * r++; } - for (n = 0; n < sizeof(ssh_pkas)/sizeof(ssh_pkas[0]); ++n) { + for (n = 0; n < LTC_ARRAY_SIZE(ssh_pkas); ++n) { if (ssh_pkas[n].name.p != NULL) { if (elements[ake_algo_name].len != ssh_pkas[n].name.len || XMEMCMP(elements[ake_algo_name].p, ssh_pkas[n].name.p, ssh_pkas[n].name.len) != 0) continue; @@ -711,7 +711,7 @@ static const struct pem_header_id pem_openssh[] = { .flags = pf_public }, }; -static const unsigned long pem_openssh_num = sizeof(pem_openssh)/sizeof(pem_openssh[0]); +static const unsigned long pem_openssh_num = LTC_ARRAY_SIZE(pem_openssh); static int s_decode_openssh(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_ctx) { @@ -819,9 +819,11 @@ int ssh_read_authorized_keys_filehandle(FILE *f, ssh_authorized_key_cb cb, void LTC_ARGCHK(f != NULL); LTC_ARGCHK(cb != NULL); - fseek(f, 0, SEEK_END); + if (fseek(f, 0, SEEK_END) == -1) + return CRYPT_ERROR; tot_data = ftell(f); - rewind(f); + if (fseek(f, 0, SEEK_SET) == -1) + return CRYPT_ERROR; buf = XMALLOC(tot_data); if (buf == NULL) { return CRYPT_MEM; diff --git a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_decrypt.c b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_decrypt.c index 5fa3ee3..4c3add7 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_decrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_decrypt.c @@ -32,51 +32,51 @@ int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s LTC_ARGCHK(ct != NULL); LTC_ARGCHK(cbc != NULL); - if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(cbc->ecb.cipher)) != CRYPT_OK) { return err; } /* is blocklen valid? */ - if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV) || cbc->blocklen > (int)sizeof(tmp)) { + if (cbc->ecb.blocklen < 1 || cbc->ecb.blocklen > (int)sizeof(cbc->IV) || cbc->ecb.blocklen > (int)sizeof(tmp)) { return CRYPT_INVALID_ARG; } - if (len % cbc->blocklen) { + if (len % cbc->ecb.blocklen) { return CRYPT_INVALID_ARG; } #ifdef LTC_FAST - if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) { + if (cbc->ecb.blocklen % sizeof(LTC_FAST_TYPE)) { return CRYPT_INVALID_ARG; } #endif - if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) { - return cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key); + if (cipher_descriptor[cbc->ecb.cipher].accel_cbc_decrypt != NULL) { + return cipher_descriptor[cbc->ecb.cipher].accel_cbc_decrypt(ct, pt, len / cbc->ecb.blocklen, cbc->IV, &cbc->ecb.key); } while (len) { /* decrypt */ - if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) { + if ((err = ecb_decrypt_block(ct, tmp, &cbc->ecb)) != CRYPT_OK) { return err; } /* xor IV against plaintext */ #if defined(LTC_FAST) - for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) { + for (x = 0; x < cbc->ecb.blocklen; x += sizeof(LTC_FAST_TYPE)) { tmpy = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)tmp + x)); *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x)); *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) = tmpy; } #else - for (x = 0; x < cbc->blocklen; x++) { + for (x = 0; x < cbc->ecb.blocklen; x++) { tmpy = tmp[x] ^ cbc->IV[x]; cbc->IV[x] = ct[x]; pt[x] = tmpy; } #endif - ct += cbc->blocklen; - pt += cbc->blocklen; - len -= cbc->blocklen; + ct += cbc->ecb.blocklen; + pt += cbc->ecb.blocklen; + len -= cbc->ecb.blocklen; } return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_done.c b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_done.c index 985551f..d66e86f 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_done.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_done.c @@ -15,14 +15,9 @@ */ int cbc_done(symmetric_CBC *cbc) { - int err; LTC_ARGCHK(cbc != NULL); - if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) { - return err; - } - cipher_descriptor[cbc->cipher].done(&cbc->key); - return CRYPT_OK; + return ecb_done(&cbc->ecb); } diff --git a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_encrypt.c b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_encrypt.c index 50d91c4..7274d69 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_encrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_encrypt.c @@ -26,58 +26,58 @@ int cbc_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s LTC_ARGCHK(ct != NULL); LTC_ARGCHK(cbc != NULL); - if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(cbc->ecb.cipher)) != CRYPT_OK) { return err; } /* is blocklen valid? */ - if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV)) { + if (cbc->ecb.blocklen < 1 || cbc->ecb.blocklen > (int)sizeof(cbc->IV)) { return CRYPT_INVALID_ARG; } - if (len % cbc->blocklen) { + if (len % cbc->ecb.blocklen) { return CRYPT_INVALID_ARG; } #ifdef LTC_FAST - if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) { + if (cbc->ecb.blocklen % sizeof(LTC_FAST_TYPE)) { return CRYPT_INVALID_ARG; } #endif - if (cipher_descriptor[cbc->cipher].accel_cbc_encrypt != NULL) { - return cipher_descriptor[cbc->cipher].accel_cbc_encrypt(pt, ct, len / cbc->blocklen, cbc->IV, &cbc->key); + if (cipher_descriptor[cbc->ecb.cipher].accel_cbc_encrypt != NULL) { + return cipher_descriptor[cbc->ecb.cipher].accel_cbc_encrypt(pt, ct, len / cbc->ecb.blocklen, cbc->IV, &cbc->ecb.key); } while (len) { /* xor IV against plaintext */ #if defined(LTC_FAST) - for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) { + for (x = 0; x < cbc->ecb.blocklen; x += sizeof(LTC_FAST_TYPE)) { *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^= *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)); } #else - for (x = 0; x < cbc->blocklen; x++) { + for (x = 0; x < cbc->ecb.blocklen; x++) { cbc->IV[x] ^= pt[x]; } #endif /* encrypt */ - if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(cbc->IV, ct, &cbc->ecb)) != CRYPT_OK) { return err; } /* store IV [ciphertext] for a future block */ #if defined(LTC_FAST) - for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) { + for (x = 0; x < cbc->ecb.blocklen; x += sizeof(LTC_FAST_TYPE)) { *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x)); } #else - for (x = 0; x < cbc->blocklen; x++) { + for (x = 0; x < cbc->ecb.blocklen; x++) { cbc->IV[x] = ct[x]; } #endif - ct += cbc->blocklen; - pt += cbc->blocklen; - len -= cbc->blocklen; + ct += cbc->ecb.blocklen; + pt += cbc->ecb.blocklen; + len -= cbc->ecb.blocklen; } return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_getiv.c b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_getiv.c index 7af2cf1..37bf600 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_getiv.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_getiv.c @@ -21,12 +21,12 @@ int cbc_getiv(unsigned char *IV, unsigned long *len, const symmetric_CBC *cbc) LTC_ARGCHK(IV != NULL); LTC_ARGCHK(len != NULL); LTC_ARGCHK(cbc != NULL); - if ((unsigned long)cbc->blocklen > *len) { - *len = cbc->blocklen; + if ((unsigned long)cbc->ecb.blocklen > *len) { + *len = cbc->ecb.blocklen; return CRYPT_BUFFER_OVERFLOW; } - XMEMCPY(IV, cbc->IV, cbc->blocklen); - *len = cbc->blocklen; + XMEMCPY(IV, cbc->IV, cbc->ecb.blocklen); + *len = cbc->ecb.blocklen; return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_setiv.c b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_setiv.c index a9e91c3..92620b0 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_setiv.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_setiv.c @@ -21,7 +21,7 @@ int cbc_setiv(const unsigned char *IV, unsigned long len, symmetric_CBC *cbc) { LTC_ARGCHK(IV != NULL); LTC_ARGCHK(cbc != NULL); - if (len != (unsigned long)cbc->blocklen) { + if (len != (unsigned long)cbc->ecb.blocklen) { return CRYPT_INVALID_ARG; } XMEMCPY(cbc->IV, IV, len); diff --git a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_start.c b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_start.c index 4156739..58322e1 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_start.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cbc/cbc_start.c @@ -28,20 +28,13 @@ int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key, LTC_ARGCHK(key != NULL); LTC_ARGCHK(cbc != NULL); - /* bad param? */ - if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { - return err; - } - /* setup cipher */ - if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &cbc->key)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, num_rounds, &cbc->ecb)) != CRYPT_OK) { return err; } /* copy IV */ - cbc->blocklen = cipher_descriptor[cipher].block_length; - cbc->cipher = cipher; - for (x = 0; x < cbc->blocklen; x++) { + for (x = 0; x < cbc->ecb.blocklen; x++) { cbc->IV[x] = IV[x]; } return CRYPT_OK; diff --git a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_decrypt.c b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_decrypt.c index ee06988..69026e3 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_decrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_decrypt.c @@ -57,12 +57,12 @@ int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s return CRYPT_OVERFLOW; } - if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(cfb->ecb.cipher)) != CRYPT_OK) { return err; } /* is blocklen/padlen valid? */ - if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) || + if (cfb->ecb.blocklen < 0 || cfb->ecb.blocklen > (int)sizeof(cfb->IV) || cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) { return CRYPT_INVALID_ARG; } @@ -70,8 +70,8 @@ int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s bits_per_round = cfb->width == 1 ? 1 : 8; while (bitlen > 0) { - if (cfb->padlen == cfb->blocklen) { - if ((err = cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key)) != CRYPT_OK) { + if (cfb->padlen == cfb->ecb.blocklen) { + if ((err = ecb_encrypt_block(cfb->pad, cfb->IV, &cfb->ecb)) != CRYPT_OK) { return err; } cfb->padlen = 0; @@ -85,22 +85,22 @@ int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s ct_ <<= 1; pt_ <<= 1; } - if (cfb->blocklen == 16) + if (cfb->ecb.blocklen == 16) s_shift1left_128(cfb->pad, ct_ >> 7); else s_shift1left_64(cfb->pad, ct_ >> 7); pt_ |= ((ct_ ^ cfb->IV[0]) >> 7) & 0x01u; - cfb->padlen = cfb->blocklen; + cfb->padlen = cfb->ecb.blocklen; if (cur_bit % 8 == 0) { *pt++ = pt_; cur_bit = 0; } break; case 8: - XMEMMOVE(cfb->pad, cfb->pad + 1, cfb->blocklen - 1); - cfb->pad[cfb->blocklen - 1] = *ct; + XMEMMOVE(cfb->pad, cfb->pad + 1, cfb->ecb.blocklen - 1); + cfb->pad[cfb->ecb.blocklen - 1] = *ct; *pt++ = *ct++ ^ cfb->IV[0]; - cfb->padlen = cfb->blocklen; + cfb->padlen = cfb->ecb.blocklen; break; case 64: case 128: diff --git a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_done.c b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_done.c index 8f8f9cd..881a17c 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_done.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_done.c @@ -15,14 +15,9 @@ */ int cfb_done(symmetric_CFB *cfb) { - int err; LTC_ARGCHK(cfb != NULL); - if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) { - return err; - } - cipher_descriptor[cfb->cipher].done(&cfb->key); - return CRYPT_OK; + return ecb_done(&cfb->ecb); } diff --git a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_encrypt.c b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_encrypt.c index e0a3643..22e27aa 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_encrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_encrypt.c @@ -57,12 +57,12 @@ int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s return CRYPT_OVERFLOW; } - if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(cfb->ecb.cipher)) != CRYPT_OK) { return err; } /* is blocklen/padlen valid? */ - if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) || + if (cfb->ecb.blocklen < 0 || cfb->ecb.blocklen > (int)sizeof(cfb->IV) || cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) { return CRYPT_INVALID_ARG; } @@ -70,8 +70,8 @@ int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s bits_per_round = cfb->width == 1 ? 1 : 8; while (bitlen > 0) { - if (cfb->padlen == cfb->blocklen) { - if ((err = cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key)) != CRYPT_OK) { + if (cfb->padlen == cfb->ecb.blocklen) { + if ((err = ecb_encrypt_block(cfb->pad, cfb->IV, &cfb->ecb)) != CRYPT_OK) { return err; } cfb->padlen = 0; @@ -86,22 +86,22 @@ int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s ct_ <<= 1; } ct_ |= ((pt_ ^ cfb->IV[0]) >> 7) & 0x01u; - if (cfb->blocklen == 16) + if (cfb->ecb.blocklen == 16) s_shift1left_128(cfb->pad, ct_); else s_shift1left_64(cfb->pad, ct_); - cfb->padlen = cfb->blocklen; + cfb->padlen = cfb->ecb.blocklen; if (cur_bit % 8 == 0) { *ct++ = ct_; cur_bit = 0; } break; case 8: - XMEMMOVE(cfb->pad, cfb->pad + 1, cfb->blocklen - 1); - cfb->pad[cfb->blocklen - 1] = (*ct = *pt ^ cfb->IV[0]); + XMEMMOVE(cfb->pad, cfb->pad + 1, cfb->ecb.blocklen - 1); + cfb->pad[cfb->ecb.blocklen - 1] = (*ct = *pt ^ cfb->IV[0]); ++pt; ++ct; - cfb->padlen = cfb->blocklen; + cfb->padlen = cfb->ecb.blocklen; break; case 64: case 128: diff --git a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_getiv.c b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_getiv.c index ca72dd6..75ce264 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_getiv.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_getiv.c @@ -21,12 +21,12 @@ int cfb_getiv(unsigned char *IV, unsigned long *len, const symmetric_CFB *cfb) LTC_ARGCHK(IV != NULL); LTC_ARGCHK(len != NULL); LTC_ARGCHK(cfb != NULL); - if ((unsigned long)cfb->blocklen > *len) { - *len = cfb->blocklen; + if ((unsigned long)cfb->ecb.blocklen > *len) { + *len = cfb->ecb.blocklen; return CRYPT_BUFFER_OVERFLOW; } - XMEMCPY(IV, cfb->pad, cfb->blocklen); - *len = cfb->blocklen; + XMEMCPY(IV, cfb->pad, cfb->ecb.blocklen); + *len = cfb->ecb.blocklen; return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_setiv.c b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_setiv.c index 60572de..1758d64 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_setiv.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_setiv.c @@ -23,18 +23,18 @@ int cfb_setiv(const unsigned char *IV, unsigned long len, symmetric_CFB *cfb) LTC_ARGCHK(IV != NULL); LTC_ARGCHK(cfb != NULL); - if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(cfb->ecb.cipher)) != CRYPT_OK) { return err; } - if (len != (unsigned long)cfb->blocklen) { + if (len != (unsigned long)cfb->ecb.blocklen) { return CRYPT_INVALID_ARG; } /* force next block */ cfb->padlen = 0; XMEMCPY(cfb->pad, IV, len); - return cipher_descriptor[cfb->cipher].ecb_encrypt(IV, cfb->IV, &cfb->key); + return ecb_encrypt_block(IV, cfb->IV, &cfb->ecb); } #endif diff --git a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_start.c b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_start.c index c95a657..8b78b34 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_start.c +++ b/Sources/DataLiteC/libtomcrypt/modes/cfb/cfb_start.c @@ -52,23 +52,19 @@ int cfb_start_ex(int cipher, const unsigned char *IV, const unsigned char *key, return CRYPT_INVALID_ARG; } - - /* copy data */ - cfb->cipher = cipher; cfb->width = width; - cfb->blocklen = cipher_descriptor[cipher].block_length; - for (x = 0; x < cfb->blocklen; x++) { - cfb->pad[x] = IV[x]; - } - /* init the cipher */ - if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &cfb->key)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, num_rounds, &cfb->ecb)) != CRYPT_OK) { return err; } + /* copy data */ + for (x = 0; x < cfb->ecb.blocklen; x++) { + cfb->pad[x] = IV[x]; + } /* encrypt the IV */ cfb->padlen = 0; - return cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key); + return ecb_encrypt_block(cfb->pad, cfb->IV, &cfb->ecb); } /** diff --git a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_done.c b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_done.c index f93d971..670c63b 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_done.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_done.c @@ -15,14 +15,9 @@ */ int ctr_done(symmetric_CTR *ctr) { - int err; LTC_ARGCHK(ctr != NULL); - if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) { - return err; - } - cipher_descriptor[ctr->cipher].done(&ctr->key); - return CRYPT_OK; + return ecb_done(&ctr->ecb); } diff --git a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_encrypt.c b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_encrypt.c index b8c08f7..2859574 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_encrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_encrypt.c @@ -24,7 +24,7 @@ static int s_ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned lo while (len) { /* is the pad empty? */ - if (ctr->padlen == ctr->blocklen) { + if (ctr->padlen == ctr->ecb.blocklen) { /* increment counter */ if (ctr->mode == CTR_COUNTER_LITTLE_ENDIAN) { /* little-endian */ @@ -36,7 +36,7 @@ static int s_ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned lo } } else { /* big-endian */ - for (x = ctr->blocklen-1; x >= ctr->ctrlen; x--) { + for (x = ctr->ecb.blocklen-1; x >= ctr->ctrlen; x--) { ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255; if (ctr->ctr[x] != (unsigned char)0) { break; @@ -45,21 +45,21 @@ static int s_ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned lo } /* encrypt it */ - if ((err = cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ctr->ctr, ctr->pad, &ctr->ecb)) != CRYPT_OK) { return err; } ctr->padlen = 0; } #ifdef LTC_FAST - if ((ctr->padlen == 0) && (len >= (unsigned long)ctr->blocklen)) { - for (x = 0; x < ctr->blocklen; x += sizeof(LTC_FAST_TYPE)) { + if ((ctr->padlen == 0) && (len >= (unsigned long)ctr->ecb.blocklen)) { + for (x = 0; x < ctr->ecb.blocklen; x += sizeof(LTC_FAST_TYPE)) { *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) ^ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ctr->pad + x)); } - pt += ctr->blocklen; - ct += ctr->blocklen; - len -= ctr->blocklen; - ctr->padlen = ctr->blocklen; + pt += ctr->ecb.blocklen; + ct += ctr->ecb.blocklen; + len -= ctr->ecb.blocklen; + ctr->padlen = ctr->ecb.blocklen; continue; } #endif @@ -85,26 +85,26 @@ int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s LTC_ARGCHK(ct != NULL); LTC_ARGCHK(ctr != NULL); - if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(ctr->ecb.cipher)) != CRYPT_OK) { return err; } /* is blocklen/padlen valid? */ - if ((ctr->blocklen < 1) || (ctr->blocklen > (int)sizeof(ctr->ctr)) || + if ((ctr->ecb.blocklen < 1) || (ctr->ecb.blocklen > (int)sizeof(ctr->ctr)) || (ctr->padlen < 0) || (ctr->padlen > (int)sizeof(ctr->pad))) { return CRYPT_INVALID_ARG; } #ifdef LTC_FAST - if (ctr->blocklen % sizeof(LTC_FAST_TYPE)) { + if (ctr->ecb.blocklen % sizeof(LTC_FAST_TYPE)) { return CRYPT_INVALID_ARG; } #endif /* handle acceleration only if pad is empty, accelerator is present and length is >= a block size */ - if ((cipher_descriptor[ctr->cipher].accel_ctr_encrypt != NULL) && (len >= (unsigned long)ctr->blocklen)) { - if (ctr->padlen < ctr->blocklen) { - fr = ctr->blocklen - ctr->padlen; + if ((cipher_descriptor[ctr->ecb.cipher].accel_ctr_encrypt != NULL) && (len >= (unsigned long)ctr->ecb.blocklen)) { + if (ctr->padlen < ctr->ecb.blocklen) { + fr = ctr->ecb.blocklen - ctr->padlen; if ((err = s_ctr_encrypt(pt, ct, fr, ctr)) != CRYPT_OK) { return err; } @@ -113,13 +113,13 @@ int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s len -= fr; } - if (len >= (unsigned long)ctr->blocklen) { - if ((err = cipher_descriptor[ctr->cipher].accel_ctr_encrypt(pt, ct, len/ctr->blocklen, ctr->ctr, ctr->mode, &ctr->key)) != CRYPT_OK) { + if (len >= (unsigned long)ctr->ecb.blocklen) { + if ((err = cipher_descriptor[ctr->ecb.cipher].accel_ctr_encrypt(pt, ct, len/ctr->ecb.blocklen, ctr->ctr, ctr->mode, &ctr->ecb.key)) != CRYPT_OK) { return err; } - pt += (len / ctr->blocklen) * ctr->blocklen; - ct += (len / ctr->blocklen) * ctr->blocklen; - len %= ctr->blocklen; + pt += (len / ctr->ecb.blocklen) * ctr->ecb.blocklen; + ct += (len / ctr->ecb.blocklen) * ctr->ecb.blocklen; + len %= ctr->ecb.blocklen; } } diff --git a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_getiv.c b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_getiv.c index 05277fa..7704a7f 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_getiv.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_getiv.c @@ -21,12 +21,12 @@ int ctr_getiv(unsigned char *IV, unsigned long *len, const symmetric_CTR *ctr) LTC_ARGCHK(IV != NULL); LTC_ARGCHK(len != NULL); LTC_ARGCHK(ctr != NULL); - if ((unsigned long)ctr->blocklen > *len) { - *len = ctr->blocklen; + if ((unsigned long)ctr->ecb.blocklen > *len) { + *len = ctr->ecb.blocklen; return CRYPT_BUFFER_OVERFLOW; } - XMEMCPY(IV, ctr->ctr, ctr->blocklen); - *len = ctr->blocklen; + XMEMCPY(IV, ctr->ctr, ctr->ecb.blocklen); + *len = ctr->ecb.blocklen; return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_setiv.c b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_setiv.c index be80f1a..75ea1ab 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_setiv.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_setiv.c @@ -24,11 +24,11 @@ int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr) LTC_ARGCHK(ctr != NULL); /* bad param? */ - if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(ctr->ecb.cipher)) != CRYPT_OK) { return err; } - if (len != (unsigned long)ctr->blocklen) { + if (len != (unsigned long)ctr->ecb.blocklen) { return CRYPT_INVALID_ARG; } @@ -37,7 +37,7 @@ int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr) /* force next block */ ctr->padlen = 0; - return cipher_descriptor[ctr->cipher].ecb_encrypt(IV, ctr->pad, &ctr->key); + return ecb_encrypt_block(IV, ctr->pad, &ctr->ecb); } #endif diff --git a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_start.c b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_start.c index 0ccdfd2..03b18a4 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_start.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_start.c @@ -49,16 +49,14 @@ int ctr_start( int cipher, } /* setup cipher */ - if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ctr->key)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, num_rounds, &ctr->ecb)) != CRYPT_OK) { return err; } /* copy ctr */ - ctr->blocklen = cipher_descriptor[cipher].block_length; - ctr->cipher = cipher; ctr->padlen = 0; ctr->mode = ctr_mode & 0x1000; - for (x = 0; x < ctr->blocklen; x++) { + for (x = 0; x < ctr->ecb.blocklen; x++) { ctr->ctr[x] = IV[x]; } @@ -74,7 +72,7 @@ int ctr_start( int cipher, } } else { /* big-endian */ - for (x = ctr->blocklen-1; x >= ctr->ctrlen; x--) { + for (x = ctr->ecb.blocklen-1; x >= ctr->ctrlen; x--) { ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255; if (ctr->ctr[x] != (unsigned char)0) { break; @@ -83,7 +81,7 @@ int ctr_start( int cipher, } } - return cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key); + return ecb_encrypt_block(ctr->ctr, ctr->pad, &ctr->ecb); } #endif diff --git a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_test.c b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_test.c index df7e649..48c9498 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_test.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ctr/ctr_test.c @@ -51,7 +51,7 @@ int ctr_test(void) } } - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { if ((err = ctr_start(idx, tests[x].IV, tests[x].key, tests[x].keylen, 0, CTR_COUNTER_BIG_ENDIAN|LTC_CTR_RFC3686, &ctr)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/modes/ecb/ecb_decrypt.c b/Sources/DataLiteC/libtomcrypt/modes/ecb/ecb_decrypt.c index 3069779..b8a48b0 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ecb/ecb_decrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ecb/ecb_decrypt.c @@ -8,6 +8,14 @@ */ #ifdef LTC_ECB_MODE +int ecb_decrypt_block(const unsigned char *ct, unsigned char *pt, const symmetric_ECB *ecb) +{ + /* check for accel */ + if (cipher_descriptor[ecb->cipher].accel_ecb_decrypt != NULL) { + return cipher_descriptor[ecb->cipher].accel_ecb_decrypt(ct, pt, 1, &ecb->key); + } + return cipher_descriptor[ecb->cipher].ecb_decrypt(ct, pt, &ecb->key); +} /** ECB decrypt @@ -17,7 +25,7 @@ @param ecb ECB state @return CRYPT_OK if successful */ -int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb) +int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, const symmetric_ECB *ecb) { int err; LTC_ARGCHK(pt != NULL); diff --git a/Sources/DataLiteC/libtomcrypt/modes/ecb/ecb_encrypt.c b/Sources/DataLiteC/libtomcrypt/modes/ecb/ecb_encrypt.c index 661d994..07e8672 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ecb/ecb_encrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ecb/ecb_encrypt.c @@ -8,6 +8,14 @@ */ #ifdef LTC_ECB_MODE +int ecb_encrypt_block(const unsigned char *pt, unsigned char *ct, const symmetric_ECB *ecb) +{ + /* check for accel */ + if (cipher_descriptor[ecb->cipher].accel_ecb_encrypt != NULL) { + return cipher_descriptor[ecb->cipher].accel_ecb_encrypt(pt, ct, 1, &ecb->key); + } + return cipher_descriptor[ecb->cipher].ecb_encrypt(pt, ct, &ecb->key); +} /** ECB encrypt @@ -17,7 +25,7 @@ @param ecb ECB state @return CRYPT_OK if successful */ -int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_ECB *ecb) +int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, const symmetric_ECB *ecb) { int err; LTC_ARGCHK(pt != NULL); diff --git a/Sources/DataLiteC/libtomcrypt/modes/f8/f8_done.c b/Sources/DataLiteC/libtomcrypt/modes/f8/f8_done.c index 7d25b04..78ee9fe 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/f8/f8_done.c +++ b/Sources/DataLiteC/libtomcrypt/modes/f8/f8_done.c @@ -15,14 +15,9 @@ */ int f8_done(symmetric_F8 *f8) { - int err; LTC_ARGCHK(f8 != NULL); - if ((err = cipher_is_valid(f8->cipher)) != CRYPT_OK) { - return err; - } - cipher_descriptor[f8->cipher].done(&f8->key); - return CRYPT_OK; + return ecb_done(&f8->ecb); } diff --git a/Sources/DataLiteC/libtomcrypt/modes/f8/f8_encrypt.c b/Sources/DataLiteC/libtomcrypt/modes/f8/f8_encrypt.c index 671c904..ec147fd 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/f8/f8_encrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/f8/f8_encrypt.c @@ -24,12 +24,12 @@ int f8_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, sy LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); LTC_ARGCHK(f8 != NULL); - if ((err = cipher_is_valid(f8->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(f8->ecb.cipher)) != CRYPT_OK) { return err; } /* is blocklen/padlen valid? */ - if (f8->blocklen < 0 || f8->blocklen > (int)sizeof(f8->IV) || + if (f8->ecb.blocklen < 0 || f8->ecb.blocklen > (int)sizeof(f8->IV) || f8->padlen < 0 || f8->padlen > (int)sizeof(f8->IV)) { return CRYPT_INVALID_ARG; } @@ -37,14 +37,14 @@ int f8_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, sy zeromem(buf, sizeof(buf)); /* make sure the pad is empty */ - if (f8->padlen == f8->blocklen) { + if (f8->padlen == f8->ecb.blocklen) { /* xor of IV, MIV and blockcnt == what goes into cipher */ - STORE32H(f8->blockcnt, (buf+(f8->blocklen-4))); + STORE32H(f8->blockcnt, (buf+(f8->ecb.blocklen-4))); ++(f8->blockcnt); - for (x = 0; x < f8->blocklen; x++) { + for (x = 0; x < f8->ecb.blocklen; x++) { f8->IV[x] ^= f8->MIV[x] ^ buf[x]; } - if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(f8->IV, f8->IV, &f8->ecb)) != CRYPT_OK) { return err; } f8->padlen = 0; @@ -52,14 +52,14 @@ int f8_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, sy #ifdef LTC_FAST if (f8->padlen == 0) { - while (len >= (unsigned long)f8->blocklen) { - STORE32H(f8->blockcnt, (buf+(f8->blocklen-4))); + while (len >= (unsigned long)f8->ecb.blocklen) { + STORE32H(f8->blockcnt, (buf+(f8->ecb.blocklen-4))); ++(f8->blockcnt); - for (x = 0; x < f8->blocklen; x += sizeof(LTC_FAST_TYPE)) { + for (x = 0; x < f8->ecb.blocklen; x += sizeof(LTC_FAST_TYPE)) { *(LTC_FAST_TYPE_PTR_CAST(&ct[x])) = *(LTC_FAST_TYPE_PTR_CAST(&pt[x])) ^ *(LTC_FAST_TYPE_PTR_CAST(&f8->IV[x])); *(LTC_FAST_TYPE_PTR_CAST(&f8->IV[x])) ^= *(LTC_FAST_TYPE_PTR_CAST(&f8->MIV[x])) ^ *(LTC_FAST_TYPE_PTR_CAST(&buf[x])); } - if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(f8->IV, f8->IV, &f8->ecb)) != CRYPT_OK) { return err; } len -= x; @@ -70,14 +70,14 @@ int f8_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, sy #endif while (len > 0) { - if (f8->padlen == f8->blocklen) { + if (f8->padlen == f8->ecb.blocklen) { /* xor of IV, MIV and blockcnt == what goes into cipher */ - STORE32H(f8->blockcnt, (buf+(f8->blocklen-4))); + STORE32H(f8->blockcnt, (buf+(f8->ecb.blocklen-4))); ++(f8->blockcnt); - for (x = 0; x < f8->blocklen; x++) { + for (x = 0; x < f8->ecb.blocklen; x++) { f8->IV[x] ^= f8->MIV[x] ^ buf[x]; } - if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(f8->IV, f8->IV, &f8->ecb)) != CRYPT_OK) { return err; } f8->padlen = 0; diff --git a/Sources/DataLiteC/libtomcrypt/modes/f8/f8_getiv.c b/Sources/DataLiteC/libtomcrypt/modes/f8/f8_getiv.c index 1a4e53f..a38ffde 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/f8/f8_getiv.c +++ b/Sources/DataLiteC/libtomcrypt/modes/f8/f8_getiv.c @@ -21,12 +21,12 @@ int f8_getiv(unsigned char *IV, unsigned long *len, const symmetric_F8 *f8) LTC_ARGCHK(IV != NULL); LTC_ARGCHK(len != NULL); LTC_ARGCHK(f8 != NULL); - if ((unsigned long)f8->blocklen > *len) { - *len = f8->blocklen; + if ((unsigned long)f8->ecb.blocklen > *len) { + *len = f8->ecb.blocklen; return CRYPT_BUFFER_OVERFLOW; } - XMEMCPY(IV, f8->IV, f8->blocklen); - *len = f8->blocklen; + XMEMCPY(IV, f8->IV, f8->ecb.blocklen); + *len = f8->ecb.blocklen; return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/modes/f8/f8_setiv.c b/Sources/DataLiteC/libtomcrypt/modes/f8/f8_setiv.c index 51a80ab..fd5411e 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/f8/f8_setiv.c +++ b/Sources/DataLiteC/libtomcrypt/modes/f8/f8_setiv.c @@ -23,17 +23,17 @@ int f8_setiv(const unsigned char *IV, unsigned long len, symmetric_F8 *f8) LTC_ARGCHK(IV != NULL); LTC_ARGCHK(f8 != NULL); - if ((err = cipher_is_valid(f8->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(f8->ecb.cipher)) != CRYPT_OK) { return err; } - if (len != (unsigned long)f8->blocklen) { + if (len != (unsigned long)f8->ecb.blocklen) { return CRYPT_INVALID_ARG; } /* force next block */ f8->padlen = 0; - return cipher_descriptor[f8->cipher].ecb_encrypt(IV, f8->IV, &f8->key); + return ecb_encrypt_block(IV, f8->IV, &f8->ecb); } #endif diff --git a/Sources/DataLiteC/libtomcrypt/modes/f8/f8_start.c b/Sources/DataLiteC/libtomcrypt/modes/f8/f8_start.c index 58f126f..cb60803 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/f8/f8_start.c +++ b/Sources/DataLiteC/libtomcrypt/modes/f8/f8_start.c @@ -47,9 +47,7 @@ int f8_start( int cipher, const unsigned char *IV, /* copy details */ f8->blockcnt = 0; - f8->cipher = cipher; - f8->blocklen = cipher_descriptor[cipher].block_length; - f8->padlen = f8->blocklen; + f8->padlen = cipher_descriptor[cipher].block_length; /* now get key ^ salt_key [extend salt_ket with 0x55 as required to match length] */ zeromem(tkey, sizeof(tkey)); @@ -64,23 +62,23 @@ int f8_start( int cipher, const unsigned char *IV, } /* now encrypt with tkey[0..keylen-1] the IV and use that as the IV */ - if ((err = cipher_descriptor[cipher].setup(tkey, keylen, num_rounds, &f8->key)) != CRYPT_OK) { + if ((err = ecb_start(cipher, tkey, keylen, num_rounds, &f8->ecb)) != CRYPT_OK) { return err; } /* encrypt IV */ - if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(IV, f8->MIV, &f8->key)) != CRYPT_OK) { - cipher_descriptor[f8->cipher].done(&f8->key); + if ((err = ecb_encrypt_block(IV, f8->MIV, &f8->ecb)) != CRYPT_OK) { + ecb_done(&f8->ecb); return err; } zeromem(tkey, sizeof(tkey)); zeromem(f8->IV, sizeof(f8->IV)); /* terminate this cipher */ - cipher_descriptor[f8->cipher].done(&f8->key); + ecb_done(&f8->ecb); /* init the cipher */ - return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &f8->key); + return ecb_start(cipher, key, keylen, num_rounds, &f8->ecb); } #endif diff --git a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_decrypt.c b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_decrypt.c index ba3b38a..b4f4059 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_decrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_decrypt.c @@ -24,12 +24,12 @@ int lrw_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s LTC_ARGCHK(ct != NULL); LTC_ARGCHK(lrw != NULL); - if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(lrw->ecb.cipher)) != CRYPT_OK) { return err; } - if (cipher_descriptor[lrw->cipher].accel_lrw_decrypt != NULL) { - return cipher_descriptor[lrw->cipher].accel_lrw_decrypt(ct, pt, len, lrw->IV, lrw->tweak, &lrw->key); + if (cipher_descriptor[lrw->ecb.cipher].accel_lrw_decrypt != NULL) { + return cipher_descriptor[lrw->ecb.cipher].accel_lrw_decrypt(ct, pt, len, lrw->IV, lrw->tweak, &lrw->ecb.key); } return lrw_process(ct, pt, len, LRW_DECRYPT, lrw); diff --git a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_done.c b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_done.c index dc649a0..83ad5f3 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_done.c +++ b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_done.c @@ -16,16 +16,9 @@ */ int lrw_done(symmetric_LRW *lrw) { - int err; - LTC_ARGCHK(lrw != NULL); - if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) { - return err; - } - cipher_descriptor[lrw->cipher].done(&lrw->key); - - return CRYPT_OK; + return ecb_done(&lrw->ecb); } #endif diff --git a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_encrypt.c b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_encrypt.c index fde442b..7c8e1ba 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_encrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_encrypt.c @@ -24,12 +24,12 @@ int lrw_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s LTC_ARGCHK(ct != NULL); LTC_ARGCHK(lrw != NULL); - if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(lrw->ecb.cipher)) != CRYPT_OK) { return err; } - if (cipher_descriptor[lrw->cipher].accel_lrw_encrypt != NULL) { - return cipher_descriptor[lrw->cipher].accel_lrw_encrypt(pt, ct, len, lrw->IV, lrw->tweak, &lrw->key); + if (cipher_descriptor[lrw->ecb.cipher].accel_lrw_encrypt != NULL) { + return cipher_descriptor[lrw->ecb.cipher].accel_lrw_encrypt(pt, ct, len, lrw->IV, lrw->tweak, &lrw->ecb.key); } return lrw_process(pt, ct, len, LRW_ENCRYPT, lrw); diff --git a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_process.c b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_process.c index d9a3edd..a04f90d 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_process.c +++ b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_process.c @@ -77,11 +77,11 @@ int lrw_process(const unsigned char *pt, unsigned char *ct, unsigned long len, i /* send through cipher */ if (mode == LRW_ENCRYPT) { - if ((err = cipher_descriptor[lrw->cipher].ecb_encrypt(ct, ct, &lrw->key)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(ct, ct, &lrw->ecb)) != CRYPT_OK) { return err; } } else { - if ((err = cipher_descriptor[lrw->cipher].ecb_decrypt(ct, ct, &lrw->key)) != CRYPT_OK) { + if ((err = ecb_decrypt_block(ct, ct, &lrw->ecb)) != CRYPT_OK) { return err; } } diff --git a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_setiv.c b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_setiv.c index efb4412..72615e7 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_setiv.c +++ b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_setiv.c @@ -30,7 +30,7 @@ int lrw_setiv(const unsigned char *IV, unsigned long len, symmetric_LRW *lrw) return CRYPT_INVALID_ARG; } - if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(lrw->ecb.cipher)) != CRYPT_OK) { return err; } @@ -38,7 +38,7 @@ int lrw_setiv(const unsigned char *IV, unsigned long len, symmetric_LRW *lrw) XMEMCPY(lrw->IV, IV, 16); /* check if we have to actually do work */ - if (cipher_descriptor[lrw->cipher].accel_lrw_encrypt != NULL && cipher_descriptor[lrw->cipher].accel_lrw_decrypt != NULL) { + if (cipher_descriptor[lrw->ecb.cipher].accel_lrw_encrypt != NULL && cipher_descriptor[lrw->ecb.cipher].accel_lrw_decrypt != NULL) { /* we have accelerators, let's bail since they don't use lrw->pad anyways */ return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_start.c b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_start.c index 2095685..fb0b95e 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_start.c +++ b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_start.c @@ -53,10 +53,10 @@ int lrw_start( int cipher, } /* schedule key */ - if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &lrw->key)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key, keylen, num_rounds, &lrw->ecb)) != CRYPT_OK) { return err; } - lrw->cipher = cipher; + lrw->ecb.cipher = cipher; /* copy the IV and tweak */ XMEMCPY(lrw->tweak, tweak, 16); diff --git a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_test.c b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_test.c index 3d9015b..893f88d 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_test.c +++ b/Sources/DataLiteC/libtomcrypt/modes/lrw/lrw_test.c @@ -73,7 +73,7 @@ int lrw_test(void) } } - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) { /* schedule it */ if ((err = lrw_start(idx, tests[x].IV, tests[x].key, 16, tests[x].tweak, 0, &lrw)) != CRYPT_OK) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_done.c b/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_done.c index c4a0184..15cf1f6 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_done.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_done.c @@ -15,14 +15,9 @@ */ int ofb_done(symmetric_OFB *ofb) { - int err; LTC_ARGCHK(ofb != NULL); - if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) { - return err; - } - cipher_descriptor[ofb->cipher].done(&ofb->key); - return CRYPT_OK; + return ecb_done(&ofb->ecb); } diff --git a/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_encrypt.c b/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_encrypt.c index 63f1e18..211efcc 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_encrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_encrypt.c @@ -23,19 +23,19 @@ int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); LTC_ARGCHK(ofb != NULL); - if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(ofb->ecb.cipher)) != CRYPT_OK) { return err; } /* is blocklen/padlen valid? */ - if (ofb->blocklen < 0 || ofb->blocklen > (int)sizeof(ofb->IV) || + if (ofb->ecb.blocklen < 0 || ofb->ecb.blocklen > (int)sizeof(ofb->IV) || ofb->padlen < 0 || ofb->padlen > (int)sizeof(ofb->IV)) { return CRYPT_INVALID_ARG; } while (len-- > 0) { - if (ofb->padlen == ofb->blocklen) { - if ((err = cipher_descriptor[ofb->cipher].ecb_encrypt(ofb->IV, ofb->IV, &ofb->key)) != CRYPT_OK) { + if (ofb->padlen == ofb->ecb.blocklen) { + if ((err = ecb_encrypt_block(ofb->IV, ofb->IV, &ofb->ecb)) != CRYPT_OK) { return err; } ofb->padlen = 0; diff --git a/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_getiv.c b/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_getiv.c index 0a799f0..6a40c1b 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_getiv.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_getiv.c @@ -21,12 +21,12 @@ int ofb_getiv(unsigned char *IV, unsigned long *len, const symmetric_OFB *ofb) LTC_ARGCHK(IV != NULL); LTC_ARGCHK(len != NULL); LTC_ARGCHK(ofb != NULL); - if ((unsigned long)ofb->blocklen > *len) { - *len = ofb->blocklen; + if ((unsigned long)ofb->ecb.blocklen > *len) { + *len = ofb->ecb.blocklen; return CRYPT_BUFFER_OVERFLOW; } - XMEMCPY(IV, ofb->IV, ofb->blocklen); - *len = ofb->blocklen; + XMEMCPY(IV, ofb->IV, ofb->ecb.blocklen); + *len = ofb->ecb.blocklen; return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_setiv.c b/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_setiv.c index 1fdec7f..9a06cc5 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_setiv.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_setiv.c @@ -23,17 +23,17 @@ int ofb_setiv(const unsigned char *IV, unsigned long len, symmetric_OFB *ofb) LTC_ARGCHK(IV != NULL); LTC_ARGCHK(ofb != NULL); - if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) { + if ((err = cipher_is_valid(ofb->ecb.cipher)) != CRYPT_OK) { return err; } - if (len != (unsigned long)ofb->blocklen) { + if (len != (unsigned long)ofb->ecb.blocklen) { return CRYPT_INVALID_ARG; } /* force next block */ ofb->padlen = 0; - return cipher_descriptor[ofb->cipher].ecb_encrypt(IV, ofb->IV, &ofb->key); + return ecb_encrypt_block(IV, ofb->IV, &ofb->ecb); } #endif diff --git a/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_start.c b/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_start.c index 179fe57..2998f3f 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_start.c +++ b/Sources/DataLiteC/libtomcrypt/modes/ofb/ofb_start.c @@ -33,16 +33,18 @@ int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key, return err; } + /* init the cipher */ + if ((err = ecb_start(cipher, key, keylen, num_rounds, &ofb->ecb)) != CRYPT_OK) { + return err; + } + ofb->padlen = cipher_descriptor[cipher].block_length; + /* copy details */ - ofb->cipher = cipher; - ofb->blocklen = cipher_descriptor[cipher].block_length; - for (x = 0; x < ofb->blocklen; x++) { + for (x = 0; x < ofb->ecb.blocklen; x++) { ofb->IV[x] = IV[x]; } - /* init the cipher */ - ofb->padlen = ofb->blocklen; - return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ofb->key); + return CRYPT_OK; } #endif diff --git a/Sources/DataLiteC/libtomcrypt/modes/xts/xts_decrypt.c b/Sources/DataLiteC/libtomcrypt/modes/xts/xts_decrypt.c index f1747d5..50019b9 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/xts/xts_decrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/xts/xts_decrypt.c @@ -24,7 +24,7 @@ static int s_tweak_uncrypt(const unsigned char *C, unsigned char *P, unsigned ch } #endif - err = cipher_descriptor[xts->cipher].ecb_decrypt(P, P, &xts->key1); + err = ecb_decrypt_block(P, P, &xts->key1); #ifdef LTC_FAST for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) { @@ -53,7 +53,7 @@ static int s_tweak_uncrypt(const unsigned char *C, unsigned char *P, unsigned ch int xts_decrypt(const unsigned char *ct, unsigned long ptlen, unsigned char *pt, unsigned char *tweak, const symmetric_xts *xts) { - unsigned char PP[16], CC[16], T[16]; + unsigned char PP[16] = {0}, CC[16], T[16]; unsigned long i, m, mo, lim; int err; @@ -86,7 +86,7 @@ int xts_decrypt(const unsigned char *ct, unsigned long ptlen, unsigned char *pt, if (cipher_descriptor[xts->cipher].accel_xts_decrypt && lim > 0) { /* use accelerated decryption for whole blocks */ - if ((err = cipher_descriptor[xts->cipher].accel_xts_decrypt(ct, pt, lim, tweak, &xts->key1, &xts->key2)) != + if ((err = cipher_descriptor[xts->cipher].accel_xts_decrypt(ct, pt, lim, tweak, &xts->key1.key, &xts->key2.key)) != CRYPT_OK) { return err; } @@ -97,7 +97,7 @@ int xts_decrypt(const unsigned char *ct, unsigned long ptlen, unsigned char *pt, XMEMCPY(T, tweak, sizeof(T)); } else { /* encrypt the tweak */ - if ((err = cipher_descriptor[xts->cipher].ecb_encrypt(tweak, T, &xts->key2)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(tweak, T, &xts->key2)) != CRYPT_OK) { return err; } @@ -136,7 +136,7 @@ int xts_decrypt(const unsigned char *ct, unsigned long ptlen, unsigned char *pt, } /* Decrypt the tweak back */ - if ((err = cipher_descriptor[xts->cipher].ecb_decrypt(T, tweak, &xts->key2)) != CRYPT_OK) { + if ((err = ecb_decrypt_block(T, tweak, &xts->key2)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/modes/xts/xts_done.c b/Sources/DataLiteC/libtomcrypt/modes/xts/xts_done.c index 4f77521..dd7ed25 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/xts/xts_done.c +++ b/Sources/DataLiteC/libtomcrypt/modes/xts/xts_done.c @@ -14,8 +14,8 @@ void xts_done(symmetric_xts *xts) { LTC_ARGCHKVD(xts != NULL); - cipher_descriptor[xts->cipher].done(&xts->key1); - cipher_descriptor[xts->cipher].done(&xts->key2); + ecb_done(&xts->key1); + ecb_done(&xts->key2); } #endif diff --git a/Sources/DataLiteC/libtomcrypt/modes/xts/xts_encrypt.c b/Sources/DataLiteC/libtomcrypt/modes/xts/xts_encrypt.c index c269b7c..65b129c 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/xts/xts_encrypt.c +++ b/Sources/DataLiteC/libtomcrypt/modes/xts/xts_encrypt.c @@ -24,7 +24,7 @@ static int s_tweak_crypt(const unsigned char *P, unsigned char *C, unsigned char } #endif - if ((err = cipher_descriptor[xts->cipher].ecb_encrypt(C, C, &xts->key1)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(C, C, &xts->key1)) != CRYPT_OK) { return err; } @@ -55,7 +55,7 @@ static int s_tweak_crypt(const unsigned char *P, unsigned char *C, unsigned char int xts_encrypt(const unsigned char *pt, unsigned long ptlen, unsigned char *ct, unsigned char *tweak, const symmetric_xts *xts) { - unsigned char PP[16], CC[16], T[16]; + unsigned char PP[16], CC[16] = {0}, T[16]; unsigned long i, m, mo, lim; int err; @@ -88,7 +88,7 @@ int xts_encrypt(const unsigned char *pt, unsigned long ptlen, unsigned char *ct, if (cipher_descriptor[xts->cipher].accel_xts_encrypt && lim > 0) { /* use accelerated encryption for whole blocks */ - if ((err = cipher_descriptor[xts->cipher].accel_xts_encrypt(pt, ct, lim, tweak, &xts->key1, &xts->key2)) != + if ((err = cipher_descriptor[xts->cipher].accel_xts_encrypt(pt, ct, lim, tweak, &xts->key1.key, &xts->key2.key)) != CRYPT_OK) { return err; } @@ -100,7 +100,7 @@ int xts_encrypt(const unsigned char *pt, unsigned long ptlen, unsigned char *ct, } else { /* encrypt the tweak */ - if ((err = cipher_descriptor[xts->cipher].ecb_encrypt(tweak, T, &xts->key2)) != CRYPT_OK) { + if ((err = ecb_encrypt_block(tweak, T, &xts->key2)) != CRYPT_OK) { return err; } @@ -137,7 +137,7 @@ int xts_encrypt(const unsigned char *pt, unsigned long ptlen, unsigned char *ct, } /* Decrypt the tweak back */ - if ((err = cipher_descriptor[xts->cipher].ecb_decrypt(T, tweak, &xts->key2)) != CRYPT_OK) { + if ((err = ecb_decrypt_block(T, tweak, &xts->key2)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/modes/xts/xts_init.c b/Sources/DataLiteC/libtomcrypt/modes/xts/xts_init.c index 92e10a0..b523467 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/xts/xts_init.c +++ b/Sources/DataLiteC/libtomcrypt/modes/xts/xts_init.c @@ -41,10 +41,10 @@ int xts_start(int cipher, const unsigned char *key1, const unsigned char *key2, } /* schedule the two ciphers */ - if ((err = cipher_descriptor[cipher].setup(key1, keylen, num_rounds, &xts->key1)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key1, keylen, num_rounds, &xts->key1)) != CRYPT_OK) { return err; } - if ((err = cipher_descriptor[cipher].setup(key2, keylen, num_rounds, &xts->key2)) != CRYPT_OK) { + if ((err = ecb_start(cipher, key2, keylen, num_rounds, &xts->key2)) != CRYPT_OK) { return err; } xts->cipher = cipher; diff --git a/Sources/DataLiteC/libtomcrypt/modes/xts/xts_test.c b/Sources/DataLiteC/libtomcrypt/modes/xts/xts_test.c index ad92735..90e5bdf 100644 --- a/Sources/DataLiteC/libtomcrypt/modes/xts/xts_test.c +++ b/Sources/DataLiteC/libtomcrypt/modes/xts/xts_test.c @@ -23,8 +23,10 @@ static int s_xts_test_accel_xts_encrypt(const unsigned char *pt, unsigned char * orig = cipher_descriptor[xts.cipher].accel_xts_encrypt; cipher_descriptor[xts.cipher].accel_xts_encrypt = NULL; - XMEMCPY(&xts.key1, skey1, sizeof(symmetric_key)); - XMEMCPY(&xts.key2, skey2, sizeof(symmetric_key)); + XMEMCPY(&xts.key1.key, skey1, sizeof(xts.key1)); + XMEMCPY(&xts.key2.key, skey2, sizeof(xts.key2)); + xts.key1.cipher = xts.key2.cipher = xts.cipher; + xts.key1.blocklen = xts.key2.blocklen = cipher_descriptor[xts.cipher].block_length; ret = xts_encrypt(pt, blocks << 4, ct, tweak, &xts); cipher_descriptor[xts.cipher].accel_xts_encrypt = orig; @@ -50,8 +52,10 @@ static int s_xts_test_accel_xts_decrypt(const unsigned char *ct, unsigned char * orig = cipher_descriptor[xts.cipher].accel_xts_decrypt; cipher_descriptor[xts.cipher].accel_xts_decrypt = NULL; - XMEMCPY(&xts.key1, skey1, sizeof(symmetric_key)); - XMEMCPY(&xts.key2, skey2, sizeof(symmetric_key)); + XMEMCPY(&xts.key1.key, skey1, sizeof(xts.key1)); + XMEMCPY(&xts.key2.key, skey2, sizeof(xts.key2)); + xts.key1.cipher = xts.key2.cipher = xts.cipher; + xts.key1.blocklen = xts.key2.blocklen = cipher_descriptor[xts.cipher].block_length; ret = xts_decrypt(ct, blocks << 4, pt, tweak, &xts); cipher_descriptor[xts.cipher].accel_xts_decrypt = orig; diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/general/der_asn1_maps.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/general/der_asn1_maps.c index 8f54f09..e98ab39 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/general/der_asn1_maps.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/general/der_asn1_maps.c @@ -39,7 +39,7 @@ const int der_asn1_type_to_identifier_map[] = 24, /* LTC_ASN1_GENERALIZEDTIME, */ -1, /* LTC_ASN1_CUSTOM_TYPE, */ }; -const unsigned long der_asn1_type_to_identifier_map_sz = sizeof(der_asn1_type_to_identifier_map)/sizeof(der_asn1_type_to_identifier_map[0]); +const unsigned long der_asn1_type_to_identifier_map_sz = LTC_ARRAY_SIZE(der_asn1_type_to_identifier_map); /** A Map from the ASN.1 Class to its string @@ -51,7 +51,7 @@ const char* der_asn1_class_to_string_map[] = "CONTEXT-SPECIFIC", "PRIVATE", }; -const unsigned long der_asn1_class_to_string_map_sz = sizeof(der_asn1_class_to_string_map)/sizeof(der_asn1_class_to_string_map[0]); +const unsigned long der_asn1_class_to_string_map_sz = LTC_ARRAY_SIZE(der_asn1_class_to_string_map); /** A Map from the ASN.1 P/C-bit to its string @@ -61,7 +61,7 @@ const char* der_asn1_pc_to_string_map[] = "PRIMITIVE", "CONSTRUCTED", }; -const unsigned long der_asn1_pc_to_string_map_sz = sizeof(der_asn1_pc_to_string_map)/sizeof(der_asn1_pc_to_string_map[0]); +const unsigned long der_asn1_pc_to_string_map_sz = LTC_ARRAY_SIZE(der_asn1_pc_to_string_map); /** A Map from the ASN.1 tag to its string @@ -106,7 +106,7 @@ const char* der_asn1_tag_to_string_map[] = "OID internationalized resource identifier type", "Relative OID internationalized resource identifier type", }; -const unsigned long der_asn1_tag_to_string_map_sz = sizeof(der_asn1_tag_to_string_map)/sizeof(der_asn1_tag_to_string_map[0]); +const unsigned long der_asn1_tag_to_string_map_sz = LTC_ARRAY_SIZE(der_asn1_tag_to_string_map); /** A Map from ASN.1 Tags to ltc_asn1_type @@ -152,6 +152,6 @@ const ltc_asn1_type der_asn1_tag_to_type_map[] = /* 30 */ LTC_ASN1_CUSTOM_TYPE, /* BMPString types */ }; -const unsigned long der_asn1_tag_to_type_map_sz = sizeof(der_asn1_tag_to_type_map)/sizeof(der_asn1_tag_to_type_map[0]); +const unsigned long der_asn1_tag_to_type_map_sz = LTC_ARRAY_SIZE(der_asn1_tag_to_type_map); #endif diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/general/der_decode_asn1_identifier.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/general/der_decode_asn1_identifier.c index 865e097..78866c9 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/general/der_decode_asn1_identifier.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/general/der_decode_asn1_identifier.c @@ -47,7 +47,7 @@ static const unsigned char tag_constructed_map[] = LTC_ASN1_PC_PRIMITIVE, LTC_ASN1_PC_PRIMITIVE, }; - static const unsigned long tag_constructed_map_sz = sizeof(tag_constructed_map)/sizeof(tag_constructed_map[0]); + static const unsigned long tag_constructed_map_sz = LTC_ARRAY_SIZE(tag_constructed_map); /** Decode the ASN.1 Identifier diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.c index e397b1c..4bce85e 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.c @@ -119,7 +119,7 @@ static const struct { int der_ia5_char_encode(int c) { int x; - for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(ia5_table); x++) { if (ia5_table[x].code == c) { return ia5_table[x].value; } @@ -130,7 +130,7 @@ int der_ia5_char_encode(int c) int der_ia5_value_decode(int v) { int x; - for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(ia5_table); x++) { if (ia5_table[x].value == v) { return ia5_table[x].code; } diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.c index 9a9c62d..af56354 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.c @@ -27,7 +27,7 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long nwor LTC_ARGCHK(outlen != NULL); /* check length */ - if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) { + if ((err = der_length_object_identifier_full(words, nwords, &x, &z)) != CRYPT_OK) { return err; } if (x > *outlen) { @@ -35,17 +35,6 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long nwor return CRYPT_BUFFER_OVERFLOW; } - /* compute length to store OID data */ - z = 0; - wordbuf = words[0] * 40 + words[1]; - for (y = 1; y < nwords; y++) { - t = der_object_identifier_bits(wordbuf); - z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0); - if (y < nwords - 1) { - wordbuf = words[y + 1]; - } - } - /* store header + length */ x = 0; out[x++] = 0x06; @@ -59,7 +48,7 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long nwor wordbuf = words[0] * 40 + words[1]; for (i = 1; i < nwords; i++) { /* store 7 bit words in little endian */ - t = wordbuf & 0xFFFFFFFF; + t = wordbuf; if (t) { y = x; mask = 0; diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.c index d9ded02..8facf74 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.c @@ -9,27 +9,24 @@ #ifdef LTC_DER -unsigned long der_object_identifier_bits(unsigned long x) +static LTC_INLINE unsigned long s_der_object_identifier_bits(unsigned long x) { +#if defined(LTC_HAVE_CLZL_BUILTIN) + if (x == 0) + return 0; + return sizeof(unsigned long) * CHAR_BIT - __builtin_clzl(x); +#else unsigned long c; - x &= 0xFFFFFFFF; c = 0; while (x) { ++c; x >>= 1; } return c; +#endif } - -/** - Gets length of DER encoding of Object Identifier - @param nwords The number of OID words - @param words The actual OID words to get the size of - @param outlen [out] The length of the DER encoding for the given string - @return CRYPT_OK if successful -*/ -int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen) +int der_length_object_identifier_full(const unsigned long *words, unsigned long nwords, unsigned long *outlen, unsigned long *datalen) { unsigned long y, z, t, wordbuf; @@ -51,7 +48,7 @@ int der_length_object_identifier(const unsigned long *words, unsigned long nword z = 0; wordbuf = words[0] * 40 + words[1]; for (y = 1; y < nwords; y++) { - t = der_object_identifier_bits(wordbuf); + t = s_der_object_identifier_bits(wordbuf); z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0); if (y < nwords - 1) { /* grab next word */ @@ -59,6 +56,9 @@ int der_length_object_identifier(const unsigned long *words, unsigned long nword } } + if (datalen) { + *datalen = z; + } /* now depending on the length our length encoding changes */ if (z < 128) { z += 2; @@ -74,4 +74,16 @@ int der_length_object_identifier(const unsigned long *words, unsigned long nword return CRYPT_OK; } +/** + Gets length of DER encoding of Object Identifier + @param nwords The number of OID words + @param words The actual OID words to get the size of + @param outlen [out] The length of the DER encoding for the given string + @return CRYPT_OK if successful +*/ +int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen) +{ + return der_length_object_identifier_full(words, nwords, outlen, NULL); +} + #endif diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.c index c52e36d..dcfcae9 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.c @@ -91,7 +91,7 @@ static const struct { int der_printable_char_encode(int c) { int x; - for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(printable_table); x++) { if (printable_table[x].code == c) { return printable_table[x].value; } @@ -102,7 +102,7 @@ int der_printable_char_encode(int c) int der_printable_value_decode(int v) { int x; - for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(printable_table); x++) { if (printable_table[x].value == v) { return printable_table[x].code; } diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.c index 61fe34a..e434f05 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.c @@ -31,6 +31,22 @@ static int s_new_element(ltc_asn1_list **l) } return CRYPT_OK; } +#if defined(LTC_TEST_DBG) +void s_print_err(const char *errstr, ltc_asn1_list *l, int err, unsigned long identifier, unsigned long data_offset, unsigned long len) +{ +#if LTC_TEST_DBG <= 1 + if (err == CRYPT_OK) + return; +#endif + if (l->type == LTC_ASN1_CUSTOM_TYPE) { + fprintf(stderr, "%s %02lx: hl=%4lu l=%4lu - %s[%s %llu] (%s)\n", errstr, identifier, data_offset, len, der_asn1_class_to_string_map[l->klass], der_asn1_pc_to_string_map[l->pc], l->tag, error_to_string(err)); + } else { + fprintf(stderr, "%s %02lx: hl=%4lu l=%4lu - %s (%s)\n", errstr, identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag], error_to_string(err)); + } +} +#else +#define s_print_err(errstr, l, err, identifier, data_offset, len) LTC_UNUSED_PARAM(data_offset) +#endif /** ASN.1 DER Flexi(ble) decoder will decode arbitrary DER packets and create a linked list of the decoded elements. @@ -43,7 +59,8 @@ static int s_new_element(ltc_asn1_list **l) static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out, unsigned long depth) { ltc_asn1_list *l; - unsigned long err, identifier, len, totlen, data_offset, id_len, len_len; + int err; + unsigned long identifier, len, totlen, data_offset, id_len, len_len; void *realloc_tmp; LTC_ARGCHK(in != NULL); @@ -77,30 +94,19 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i if (l->type != LTC_ASN1_EOL) { /* fetch length */ len_len = *inlen - id_len; -#if defined(LTC_TEST_DBG) + /* init with dummy values for error cases */ data_offset = 666; len = 0; -#endif if ((err = der_decode_asn1_length(&in[id_len], &len_len, &len)) != CRYPT_OK) { -#if defined(LTC_TEST_DBG) - fprintf(stderr, "E1 %02lx: hl=%4lu l=%4lu - %s (%s)\n", identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag], error_to_string(err)); -#endif + s_print_err("E1", l, err, identifier, data_offset, len); goto error; } else if (len > (*inlen - id_len - len_len)) { err = CRYPT_INVALID_PACKET; -#if defined(LTC_TEST_DBG) - fprintf(stderr, "E2 %02lx: hl=%4lu l=%4lu - %s (%s)\n", identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag], error_to_string(err)); -#endif + s_print_err("E2", l, err, identifier, data_offset, len); goto error; } data_offset = id_len + len_len; -#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1 - if (l->type == LTC_ASN1_CUSTOM_TYPE && l->klass == LTC_ASN1_CL_CONTEXT_SPECIFIC) { - fprintf(stderr, "OK %02lx: hl=%4lu l=%4lu - Context Specific[%s %llu]\n", identifier, data_offset, len, der_asn1_pc_to_string_map[l->pc], l->tag); - } else { - fprintf(stderr, "OK %02lx: hl=%4lu l=%4lu - %s\n", identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag]); - } -#endif + s_print_err("OK", l, err, identifier, data_offset, len); len += data_offset; if (l->type == LTC_ASN1_CUSTOM_TYPE) { diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/teletex_string/der_length_teletex_string.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/teletex_string/der_length_teletex_string.c index 05dc4b0..01b94a3 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/der/teletex_string/der_length_teletex_string.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/der/teletex_string/der_length_teletex_string.c @@ -135,7 +135,7 @@ static const struct { int der_teletex_char_encode(int c) { int x; - for (x = 0; x < (int)(sizeof(teletex_table)/sizeof(teletex_table[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(teletex_table); x++) { if (teletex_table[x].code == c) { return teletex_table[x].value; } @@ -146,7 +146,7 @@ int der_teletex_char_encode(int c) int der_teletex_value_decode(int v) { int x; - for (x = 0; x < (int)(sizeof(teletex_table)/sizeof(teletex_table[0])); x++) { + for (x = 0; x < (int)LTC_ARRAY_SIZE(teletex_table); x++) { if (teletex_table[x].value == v) { return teletex_table[x].code; } diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_get.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_get.c index 48a8a98..1fd5872 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_get.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_get.c @@ -66,7 +66,7 @@ int pk_get_oid_id(enum ltc_pka_id pka, enum ltc_oid_id *oid) { unsigned int i; LTC_ARGCHK(oid != NULL); - for (i = 1; i < sizeof(pka_oids)/sizeof(pka_oids[0]); ++i) { + for (i = 1; i < LTC_ARRAY_SIZE(pka_oids); ++i) { if (pka_oids[i].pka == pka) { *oid = pka_oids[i].id; return CRYPT_OK; @@ -95,7 +95,7 @@ int pk_get_oid_from_asn1(const ltc_asn1_list *oid, enum ltc_oid_id *id) return err; } - for (i = 1; i < sizeof(pka_oids)/sizeof(pka_oids[0]); ++i) { + for (i = 1; i < LTC_ARRAY_SIZE(pka_oids); ++i) { if (XSTRCMP(pka_oids[i].oid, tmp) == 0) { *id = pka_oids[i].id; return CRYPT_OK; diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_oid_str.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_oid_str.c index fef567b..e12cdf4 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_oid_str.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/oid/pk_oid_str.c @@ -10,20 +10,18 @@ int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen) { unsigned long i, j, limit, oid_j; - size_t OID_len; LTC_ARGCHK(oidlen != NULL); limit = *oidlen; *oidlen = 0; /* make sure that we return zero oidlen on error */ - for (i = 0; i < limit; i++) oid[i] = 0; - + if (oid != NULL) { + XMEMSET(oid, 0, sizeof(*oid) * limit); + } if (OID == NULL) return CRYPT_OK; + if (OID[0] == '\0') return CRYPT_OK; - OID_len = XSTRLEN(OID); - if (OID_len == 0) return CRYPT_OK; - - for (i = 0, j = 0; i < OID_len; i++) { + for (i = 0, j = 0; OID[i] != '\0'; i++) { if (OID[i] == '.') { if (++j >= limit) continue; } @@ -38,50 +36,75 @@ int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen } } if (j == 0) return CRYPT_ERROR; - if (j >= limit) { - *oidlen = j; + *oidlen = j + 1; + if (j >= limit || oid == NULL) { return CRYPT_BUFFER_OVERFLOW; } - *oidlen = j + 1; return CRYPT_OK; } +typedef struct num_to_str { + int err; + char *wr; + unsigned long max_len, res_len; +} num_to_str; + +static LTC_INLINE void s_wr(char c, num_to_str *w) +{ + if (w->res_len == ULONG_MAX) { + w->err = CRYPT_OVERFLOW; + return; + } + w->res_len++; + if (w->res_len > w->max_len) w->wr = NULL; + if (w->wr) w->wr[w->max_len - w->res_len] = c; +} + int pk_oid_num_to_str(const unsigned long *oid, unsigned long oidlen, char *OID, unsigned long *outlen) { int i; - unsigned long j, k; - char tmp[LTC_OID_MAX_STRLEN] = { 0 }; + num_to_str w; + unsigned long j; LTC_ARGCHK(oid != NULL); LTC_ARGCHK(oidlen < INT_MAX); LTC_ARGCHK(outlen != NULL); - for (i = oidlen - 1, k = 0; i >= 0; i--) { + if (OID == NULL || *outlen == 0) { + w.max_len = ULONG_MAX; + w.wr = NULL; + } else { + w.max_len = *outlen; + w.wr = OID; + } + w.res_len = 0; + w.err = CRYPT_OK; + + s_wr('\0', &w); + for (i = oidlen; i --> 0;) { j = oid[i]; if (j == 0) { - tmp[k] = '0'; - if (++k >= sizeof(tmp)) return CRYPT_ERROR; - } - else { + s_wr('0', &w); + } else { while (j > 0) { - tmp[k] = '0' + (j % 10); - if (++k >= sizeof(tmp)) return CRYPT_ERROR; + s_wr('0' + (j % 10), &w); j /= 10; } } if (i > 0) { - tmp[k] = '.'; - if (++k >= sizeof(tmp)) return CRYPT_ERROR; + s_wr('.', &w); } } - if (*outlen < k + 1) { - *outlen = k + 1; + if (w.err != CRYPT_OK) { + return w.err; + } + if (*outlen < w.res_len || OID == NULL) { + *outlen = w.res_len; return CRYPT_BUFFER_OVERFLOW; } LTC_ARGCHK(OID != NULL); - for (j = 0; j < k; j++) OID[j] = tmp[k - j - 1]; - OID[k] = '\0'; - *outlen = k; /* the length without terminating NUL byte */ + XMEMMOVE(OID, OID + (w.max_len - w.res_len), w.res_len); + *outlen = w.res_len; return CRYPT_OK; } diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/x509/x509_decode_subject_public_key_info.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/x509/x509_decode_subject_public_key_info.c index f958825..0725611 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/x509/x509_decode_subject_public_key_info.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/x509/x509_decode_subject_public_key_info.c @@ -70,7 +70,7 @@ int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long i } /* this includes the internal hash ID and optional params (NULL in this case) */ - LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, sizeof(tmpoid)/sizeof(tmpoid[0])); + LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, LTC_ARRAY_SIZE(tmpoid)); if (parameters_type == LTC_ASN1_EOL) { alg_id_num = 1; } else { diff --git a/Sources/DataLiteC/libtomcrypt/pk/asn1/x509/x509_encode_subject_public_key_info.c b/Sources/DataLiteC/libtomcrypt/pk/asn1/x509/x509_encode_subject_public_key_info.c index 874574d..52a80a1 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/asn1/x509/x509_encode_subject_public_key_info.c +++ b/Sources/DataLiteC/libtomcrypt/pk/asn1/x509/x509_encode_subject_public_key_info.c @@ -47,7 +47,7 @@ int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outle return err; } - oidlen = sizeof(oid)/sizeof(oid[0]); + oidlen = LTC_ARRAY_SIZE(oid); if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) { return err; } @@ -56,7 +56,7 @@ int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outle LTC_SET_ASN1(alg_id, 1, parameters_type, parameters, parameters_len); return der_encode_sequence_multi(out, outlen, - LTC_ASN1_SEQUENCE, (unsigned long)sizeof(alg_id)/sizeof(alg_id[0]), alg_id, + LTC_ASN1_SEQUENCE, (unsigned long)LTC_ARRAY_SIZE(alg_id), alg_id, LTC_ASN1_RAW_BIT_STRING, public_key_len*8U, public_key, LTC_ASN1_EOL, 0UL, NULL); diff --git a/Sources/DataLiteC/libtomcrypt/pk/dsa/dsa_decrypt_key.c b/Sources/DataLiteC/libtomcrypt/pk/dsa/dsa_decrypt_key.c index fa87ef9..734a2e0 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/dsa/dsa_decrypt_key.c +++ b/Sources/DataLiteC/libtomcrypt/pk/dsa/dsa_decrypt_key.c @@ -40,7 +40,7 @@ int dsa_decrypt_key(const unsigned char *in, unsigned long inlen, } /* decode to find out hash */ - LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, sizeof(hashOID)/sizeof(hashOID[0])); + LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, LTC_ARRAY_SIZE(hashOID)); err = der_decode_sequence(in, inlen, decode, 1); if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/pk/dsa/dsa_generate_pqg.c b/Sources/DataLiteC/libtomcrypt/pk/dsa/dsa_generate_pqg.c index 8e47f50..a9887c0 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/dsa/dsa_generate_pqg.c +++ b/Sources/DataLiteC/libtomcrypt/pk/dsa/dsa_generate_pqg.c @@ -93,7 +93,7 @@ static int s_dsa_make_params(prng_state *prng, int wprng, int group_size, int mo #endif hash = -1; - for (i = 0; i < sizeof(accepted_hashes)/sizeof(accepted_hashes[0]); ++i) { + for (i = 0; i < LTC_ARRAY_SIZE(accepted_hashes); ++i) { hash = find_hash(accepted_hashes[i]); if (hash != -1) break; } diff --git a/Sources/DataLiteC/libtomcrypt/pk/ec25519/ec25519_export.c b/Sources/DataLiteC/libtomcrypt/pk/ec25519/ec25519_export.c index a6dba67..5cd7c10 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/ec25519/ec25519_export.c +++ b/Sources/DataLiteC/libtomcrypt/pk/ec25519/ec25519_export.c @@ -46,7 +46,7 @@ int ec25519_export( unsigned char *out, unsigned long *outlen, if ((err = pk_get_oid(oid_id, &OID)) != CRYPT_OK) { return err; } - oidlen = sizeof(oid)/sizeof(oid[0]); + oidlen = LTC_ARRAY_SIZE(oid); if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) { return err; } diff --git a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_decrypt_key.c b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_decrypt_key.c index 6697eda..7a4fac2 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_decrypt_key.c +++ b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_decrypt_key.c @@ -41,7 +41,7 @@ int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, } /* decode to find out hash */ - LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, sizeof(hashOID)/sizeof(hashOID[0])); + LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, LTC_ARRAY_SIZE(hashOID)); err = der_decode_sequence(in, inlen, decode, 1); if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) { return err; diff --git a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_export_openssl.c b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_export_openssl.c index 35d8ed0..a58f40e 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_export_openssl.c +++ b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_export_openssl.c @@ -90,7 +90,7 @@ int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, cons } */ - oidlen = sizeof(oid)/sizeof(oid[0]); + oidlen = LTC_ARRAY_SIZE(oid); if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) { goto error; } diff --git a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_make_key.c b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_make_key.c index 1b04741..c8239cf 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_make_key.c +++ b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_make_key.c @@ -59,6 +59,7 @@ int ecc_generate_key(prng_state *prng, int wprng, ecc_key *key) goto error; } key->type = PK_PRIVATE; + key->rfc6979_hash_alg = NULL; /* success */ err = CRYPT_OK; diff --git a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_rfc6979_key.c b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_rfc6979_key.c new file mode 100644 index 0000000..ebd17eb --- /dev/null +++ b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_rfc6979_key.c @@ -0,0 +1,162 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +#include "tomcrypt_private.h" + +/** + @file ecc_rfc6979_key.c + ECC Crypto, Russ Williams +*/ + +#ifdef LTC_MECC +#ifdef LTC_SHA256 + +/** + Make deterministic ECC key using the RFC6979 method + @param priv [in] Private key for HMAC + @param in Message to sign for HMAC + @param inlen Length of the message + @param key [out] Newly created deterministic key + @return CRYPT_OK if successful, upon error all allocated memory will be freed +*/ +int ecc_rfc6979_key(const ecc_key *priv, const unsigned char *in, unsigned long inlen, ecc_key *key) +{ + int err, hash = -1; + unsigned char v[MAXBLOCKSIZE], k[MAXBLOCKSIZE]; + unsigned char buffer[256], sep[1], privkey[128]; + unsigned long order_bits, len_diff, pk_len, zero_extend, outlen, klen, vlen, buflen, qlen, hashsize; + void *r, *d; + + LTC_ARGCHK(ltc_mp.name != NULL); + LTC_ARGCHK(priv != NULL); + LTC_ARGCHK(key != NULL); + LTC_ARGCHK(key->dp.size > 0); + + if (priv->rfc6979_hash_alg == NULL) { + return CRYPT_INVALID_ARG; + } + hash = find_hash(priv->rfc6979_hash_alg); + if ((err = hash_is_valid(hash)) != CRYPT_OK) { + return err; + } + + hashsize = hash_descriptor[hash].hashsize; + + if ((err = ltc_mp_init_multi(&r, &d, NULL)) != CRYPT_OK) { + return err; + } + + /* Length, in bytes, of key */ + order_bits = ltc_mp_count_bits(key->dp.order); + qlen = (order_bits+7) >> 3; + len_diff = qlen > inlen ? qlen - inlen : 0; + pk_len = (ltc_mp_count_bits(priv->k)+7) >> 3; + zero_extend = qlen - pk_len; + XMEMSET(buffer, 0x00, len_diff + zero_extend); + + /* RFC6979 3.2b, set V */ + XMEMSET(v, 0x01, hashsize); + + /* RFC6979 3.2c, set K */ + XMEMSET(k, 0x00, hashsize); + + if ((err = ltc_mp_to_unsigned_bin(priv->k, privkey) != CRYPT_OK)) { goto error; } + /* RFC6979 3.2d, set K to HMAC_K(V::0x00::priv::in) */ + sep[0] = 0; + klen = sizeof(k); + if((err = hmac_memory_multi(hash, + k, hashsize, + k, &klen, + v, hashsize, + sep, 1, + buffer, zero_extend, + privkey, qlen - zero_extend, + buffer, len_diff, + in, qlen - len_diff, + LTC_NULL)) != CRYPT_OK) { goto error; } + + /* RFC6979 3.2e, set V = HMAC_K(V) */ + vlen = sizeof(v); + if((err = hmac_memory(hash, k, klen, v, hashsize, v, &vlen)) != CRYPT_OK) { goto error; } + + /* RFC6979 3.2f, set K to HMAC_K(V::0x01::priv::in) */ + sep[0] = 0x01; + outlen = sizeof(k); + if((err = hmac_memory_multi(hash, + k, klen, + k, &klen, + v, hashsize, + sep, 1, + buffer, zero_extend, + privkey, qlen - zero_extend, + buffer, len_diff, + in, qlen - len_diff, + LTC_NULL)) != CRYPT_OK) { goto error; } + + /* RFC6979 3.2g, set V = HMAC_K(V) */ + outlen = sizeof(v); + if((err = hmac_memory(hash, k, klen, v, hashsize, v, &outlen)) != CRYPT_OK) { goto error; } + + /* RFC6979 3.2h, generate and check key */ + do { + /* concatenate hash bits into T */ + buflen = 0; + while (buflen < qlen) { + if (buflen + hashsize >= sizeof(buffer) || buflen + hashsize < buflen) { + err = CRYPT_BUFFER_OVERFLOW; + goto error; + } + outlen = sizeof(v); + if((err = hmac_memory(hash, k, klen, v, hashsize, v, &outlen)) != CRYPT_OK) { goto error; } + XMEMCPY(&buffer[buflen], v, hashsize); + buflen += hashsize; + } + + /* key->k = bits2int(T) */ + if ((err = ltc_mp_read_unsigned_bin(r, buffer, qlen)) != CRYPT_OK) { goto error; } + if ((qlen * 8) > order_bits) { + if ((err = ltc_mp_2expt(d, (qlen * 8) - order_bits)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_div(r, d, r, NULL)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_to_unsigned_bin(r, buffer)) != CRYPT_OK) { goto error; } + qlen = ltc_mp_unsigned_bin_size(r); + } + + if ((err = ecc_set_key(buffer, qlen, PK_PRIVATE, key))!= CRYPT_OK) { goto error; } + + /* check that k is in range [1,q-1] */ + if (ltc_mp_cmp_d(key->k, 0) == LTC_MP_GT && ltc_mp_cmp(key->k, key->dp.order) == LTC_MP_LT) { + /* Check that pubkey.x != 0 (mod p) */ + if ((err = ltc_mp_mod(key->pubkey.x, key->dp.order, r)) != CRYPT_OK) { goto error; } + + /* if we have a valid key, exit loop */ + if (ltc_mp_iszero(r) == LTC_MP_NO) + break; + } else { + /* K = HMAC_K(V::0x00) */ + buffer[0] = 0x0; + outlen = sizeof(k); + if((err = hmac_memory_multi(hash, k, klen, k, &klen, v, hashsize, buffer, 1, LTC_NULL)) != CRYPT_OK) { goto error; } + + /* V = HMAC_K(V) */ + outlen = sizeof(v); + if((err = hmac_memory(hash, k, klen, v, hashsize, v, &outlen)) != CRYPT_OK) { goto error; } + + /* ... and try again! */ + } + } while (1); + + key->type = PK_PRIVATE; + + /* success */ + err = CRYPT_OK; + goto cleanup; + +error: + ecc_free(key); +cleanup: + ltc_mp_cleanup_multi(&d, &r, NULL); + return err; +} + +#endif +#endif diff --git a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_set_curve.c b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_set_curve.c index b6c21e8..f340776 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_set_curve.c +++ b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_set_curve.c @@ -23,6 +23,8 @@ int ecc_set_curve(const ltc_ecc_curve *cu, ecc_key *key) return err; } + key->rfc6979_hash_alg = NULL; + /* A, B, order, prime, Gx, Gy */ if ((err = ltc_mp_read_radix(key->dp.prime, cu->prime, 16)) != CRYPT_OK) { goto error; } if ((err = ltc_mp_read_radix(key->dp.order, cu->order, 16)) != CRYPT_OK) { goto error; } diff --git a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_set_key.c b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_set_key.c index 2b47580..b7204b8 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_set_key.c +++ b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_set_key.c @@ -33,7 +33,7 @@ int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key else if (type == PK_PUBLIC) { /* load public key */ if ((err = ltc_ecc_import_point(in, inlen, prime, a, b, key->pubkey.x, key->pubkey.y)) != CRYPT_OK) { goto error; } - if ((err = ltc_mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto error; } } else { err = CRYPT_INVALID_PACKET; @@ -46,6 +46,7 @@ int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key } key->type = type; + key->rfc6979_hash_alg = NULL; return CRYPT_OK; error: diff --git a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_sign_hash_internal.c b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_sign_hash_internal.c index ef46281..5321dde 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_sign_hash_internal.c +++ b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_sign_hash_internal.c @@ -57,8 +57,12 @@ int ecc_sign_hash_internal(const unsigned char *in, unsigned long inlen, /* make up a key and export the public copy */ do { - if ((err = ecc_copy_curve(key, &pubkey)) != CRYPT_OK) { goto errnokey; } - if ((err = ecc_generate_key(prng, wprng, &pubkey)) != CRYPT_OK) { goto errnokey; } + if ((err = ecc_copy_curve(key, &pubkey)) != CRYPT_OK) { goto errnokey; } + if (key->rfc6979_hash_alg != NULL) { + if ((err = ecc_rfc6979_key(key, in, inlen, &pubkey)) != CRYPT_OK) { goto errnokey; } + } else { + if ((err = ecc_generate_key(prng, wprng, &pubkey)) != CRYPT_OK) { goto errnokey; } + } /* find r = x1 mod n */ if ((err = ltc_mp_mod(pubkey.pubkey.x, p, r)) != CRYPT_OK) { goto error; } @@ -78,7 +82,7 @@ int ecc_sign_hash_internal(const unsigned char *in, unsigned long inlen, if (ltc_mp_iszero(r) == LTC_MP_YES) { ecc_free(&pubkey); } else { - if ((err = rand_bn_upto(b, p, prng, wprng)) != CRYPT_OK) { goto error; } /* b = blinding value */ + if ((err = rand_bn_upto(b, p, prng, wprng)) != CRYPT_OK) { goto error; } /* b = blinding value */ /* find s = (e + xr)/k */ if ((err = ltc_mp_mulmod(pubkey.k, b, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = kb */ if ((err = ltc_mp_invmod(pubkey.k, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = 1/kb */ diff --git a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_ssh_ecdsa_encode_name.c b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_ssh_ecdsa_encode_name.c index 4b8d554..f10a2c6 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_ssh_ecdsa_encode_name.c +++ b/Sources/DataLiteC/libtomcrypt/pk/ecc/ecc_ssh_ecdsa_encode_name.c @@ -19,7 +19,7 @@ */ int ecc_ssh_ecdsa_encode_name(char *buffer, unsigned long *buflen, const ecc_key *key) { - char oidstr[64] = {0}; + char oidstr[LTC_OID_MAX_STRLEN] = {0}; unsigned long oidlen = sizeof(oidstr); int err, size = 0; diff --git a/Sources/DataLiteC/libtomcrypt/pk/rsa/rsa_key.c b/Sources/DataLiteC/libtomcrypt/pk/rsa/rsa_key.c index 12b88ce..7eb21b8 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/rsa/rsa_key.c +++ b/Sources/DataLiteC/libtomcrypt/pk/rsa/rsa_key.c @@ -24,7 +24,7 @@ static void s_mpi_shrink_multi(void **a, ...) cur = a; va_start(args, a); while (cur != NULL) { - if (n >= sizeof(tmp)/sizeof(tmp[0])) { + if (n >= LTC_ARRAY_SIZE(tmp)) { goto out; } if (*cur != NULL) { @@ -49,8 +49,8 @@ out: * or after this was called with too many args */ if ((err != CRYPT_OK) || - (n >= sizeof(tmp)/sizeof(tmp[0]))) { - for (n = 0; n < sizeof(tmp)/sizeof(tmp[0]); ++n) { + (n >= LTC_ARRAY_SIZE(tmp))) { + for (n = 0; n < LTC_ARRAY_SIZE(tmp); ++n) { if (tmp[n] != NULL) { ltc_mp_clear(tmp[n]); } diff --git a/Sources/DataLiteC/libtomcrypt/pk/rsa/rsa_verify_hash.c b/Sources/DataLiteC/libtomcrypt/pk/rsa/rsa_verify_hash.c index 9b2577b..9ca1641 100644 --- a/Sources/DataLiteC/libtomcrypt/pk/rsa/rsa_verify_hash.c +++ b/Sources/DataLiteC/libtomcrypt/pk/rsa/rsa_verify_hash.c @@ -131,7 +131,7 @@ int rsa_verify_hash_ex(const unsigned char *sig, unsigned long sigle hash OCTET STRING } */ - LTC_SET_ASN1(digestinfo, 0, LTC_ASN1_OBJECT_IDENTIFIER, loid, sizeof(loid)/sizeof(loid[0])); + LTC_SET_ASN1(digestinfo, 0, LTC_ASN1_OBJECT_IDENTIFIER, loid, LTC_ARRAY_SIZE(loid)); LTC_SET_ASN1(digestinfo, 1, LTC_ASN1_NULL, NULL, 0); LTC_SET_ASN1(siginfo, 0, LTC_ASN1_SEQUENCE, digestinfo, 2); LTC_SET_ASN1(siginfo, 1, LTC_ASN1_OCTET_STRING, tmpbuf, siglen);