diff --git a/src/Makefile b/src/Makefile index 05bed2b..45e5373 100644 --- a/src/Makefile +++ b/src/Makefile @@ -56,38 +56,38 @@ ltc/misc/crypt/crypt_register_cipher.o ltc/misc/crypt/crypt_register_hash.o ltc/misc/crypt/crypt_register_prng.o \ ltc/misc/crypt/crypt_sizes.o ltc/misc/crypt/crypt_unregister_cipher.o ltc/misc/crypt/crypt_unregister_hash.o \ ltc/misc/crypt/crypt_unregister_prng.o ltc/misc/hkdf/hkdf.o ltc/misc/padding/padding_depad.o \ -ltc/misc/padding/padding_pad.o ltc/misc/pkcs5/pkcs_5_1.o ltc/misc/pkcs5/pkcs_5_2.o \ -ltc/modes/cbc/cbc_decrypt.o ltc/modes/cbc/cbc_done.o ltc/modes/cbc/cbc_encrypt.o \ -ltc/modes/cbc/cbc_getiv.o ltc/modes/cbc/cbc_setiv.o ltc/modes/cbc/cbc_start.o ltc/modes/cfb/cfb_decrypt.o \ -ltc/modes/cfb/cfb_done.o ltc/modes/cfb/cfb_encrypt.o ltc/modes/cfb/cfb_getiv.o ltc/modes/cfb/cfb_setiv.o \ -ltc/modes/cfb/cfb_start.o ltc/modes/ctr/ctr_decrypt.o ltc/modes/ctr/ctr_done.o ltc/modes/ctr/ctr_encrypt.o \ -ltc/modes/ctr/ctr_getiv.o ltc/modes/ctr/ctr_setiv.o ltc/modes/ctr/ctr_start.o ltc/modes/ecb/ecb_decrypt.o \ -ltc/modes/ecb/ecb_done.o ltc/modes/ecb/ecb_encrypt.o ltc/modes/ecb/ecb_start.o ltc/modes/ofb/ofb_decrypt.o \ -ltc/modes/ofb/ofb_done.o ltc/modes/ofb/ofb_encrypt.o ltc/modes/ofb/ofb_getiv.o ltc/modes/ofb/ofb_setiv.o \ -ltc/modes/ofb/ofb_start.o ltc/pk/asn1/der/bit/der_decode_bit_string.o ltc/pk/asn1/der/bit/der_decode_raw_bit_string.o \ -ltc/pk/asn1/der/bit/der_encode_bit_string.o ltc/pk/asn1/der/bit/der_encode_raw_bit_string.o \ -ltc/pk/asn1/der/bit/der_length_bit_string.o ltc/pk/asn1/der/boolean/der_decode_boolean.o \ -ltc/pk/asn1/der/boolean/der_encode_boolean.o ltc/pk/asn1/der/boolean/der_length_boolean.o \ -ltc/pk/asn1/der/choice/der_decode_choice.o ltc/pk/asn1/der/custom_type/der_decode_custom_type.o \ -ltc/pk/asn1/der/custom_type/der_encode_custom_type.o ltc/pk/asn1/der/custom_type/der_length_custom_type.o \ -ltc/pk/asn1/der/general/der_asn1_maps.o ltc/pk/asn1/der/general/der_decode_asn1_identifier.o \ -ltc/pk/asn1/der/general/der_decode_asn1_length.o ltc/pk/asn1/der/general/der_encode_asn1_identifier.o \ -ltc/pk/asn1/der/general/der_encode_asn1_length.o ltc/pk/asn1/der/general/der_length_asn1_identifier.o \ -ltc/pk/asn1/der/general/der_length_asn1_length.o ltc/pk/asn1/der/generalizedtime/der_decode_generalizedtime.o \ -ltc/pk/asn1/der/generalizedtime/der_encode_generalizedtime.o ltc/pk/asn1/der/generalizedtime/der_length_generalizedtime.o \ -ltc/pk/asn1/der/ia5/der_decode_ia5_string.o ltc/pk/asn1/der/ia5/der_encode_ia5_string.o \ -ltc/pk/asn1/der/ia5/der_length_ia5_string.o ltc/pk/asn1/der/integer/der_decode_integer.o \ -ltc/pk/asn1/der/integer/der_encode_integer.o ltc/pk/asn1/der/integer/der_length_integer.o \ -ltc/pk/asn1/der/object_identifier/der_decode_object_identifier.o ltc/pk/asn1/der/object_identifier/der_encode_object_identifier.o \ -ltc/pk/asn1/der/object_identifier/der_length_object_identifier.o ltc/pk/asn1/der/octet/der_decode_octet_string.o \ -ltc/pk/asn1/der/octet/der_encode_octet_string.o ltc/pk/asn1/der/octet/der_length_octet_string.o \ -ltc/pk/asn1/der/printable_string/der_decode_printable_string.o ltc/pk/asn1/der/printable_string/der_encode_printable_string.o \ -ltc/pk/asn1/der/printable_string/der_length_printable_string.o ltc/pk/asn1/der/sequence/der_decode_sequence_ex.o \ -ltc/pk/asn1/der/sequence/der_decode_sequence_flexi.o ltc/pk/asn1/der/sequence/der_decode_sequence_multi.o \ -ltc/pk/asn1/der/sequence/der_encode_sequence_ex.o ltc/pk/asn1/der/sequence/der_encode_sequence_multi.o \ -ltc/pk/asn1/der/sequence/der_length_sequence.o ltc/pk/asn1/der/sequence/der_sequence_free.o \ -ltc/pk/asn1/der/sequence/der_sequence_shrink.o ltc/pk/asn1/der/set/der_encode_set.o \ -ltc/pk/asn1/der/set/der_encode_setof.o ltc/pk/asn1/der/short_integer/der_decode_short_integer.o \ +ltc/misc/padding/padding_pad.o ltc/misc/pkcs12/pkcs12_kdf.o ltc/misc/pkcs12/pkcs12_utf8_to_utf16.o \ +ltc/misc/pkcs5/pkcs_5_1.o ltc/misc/pkcs5/pkcs_5_2.o ltc/modes/cbc/cbc_decrypt.o ltc/modes/cbc/cbc_done.o \ +ltc/modes/cbc/cbc_encrypt.o ltc/modes/cbc/cbc_getiv.o ltc/modes/cbc/cbc_setiv.o ltc/modes/cbc/cbc_start.o \ +ltc/modes/cfb/cfb_decrypt.o ltc/modes/cfb/cfb_done.o ltc/modes/cfb/cfb_encrypt.o \ +ltc/modes/cfb/cfb_getiv.o ltc/modes/cfb/cfb_setiv.o ltc/modes/cfb/cfb_start.o ltc/modes/ctr/ctr_decrypt.o \ +ltc/modes/ctr/ctr_done.o ltc/modes/ctr/ctr_encrypt.o ltc/modes/ctr/ctr_getiv.o ltc/modes/ctr/ctr_setiv.o \ +ltc/modes/ctr/ctr_start.o ltc/modes/ecb/ecb_decrypt.o ltc/modes/ecb/ecb_done.o ltc/modes/ecb/ecb_encrypt.o \ +ltc/modes/ecb/ecb_start.o ltc/modes/ofb/ofb_decrypt.o ltc/modes/ofb/ofb_done.o ltc/modes/ofb/ofb_encrypt.o \ +ltc/modes/ofb/ofb_getiv.o ltc/modes/ofb/ofb_setiv.o ltc/modes/ofb/ofb_start.o ltc/pk/asn1/der/bit/der_decode_bit_string.o \ +ltc/pk/asn1/der/bit/der_decode_raw_bit_string.o ltc/pk/asn1/der/bit/der_encode_bit_string.o \ +ltc/pk/asn1/der/bit/der_encode_raw_bit_string.o ltc/pk/asn1/der/bit/der_length_bit_string.o \ +ltc/pk/asn1/der/boolean/der_decode_boolean.o ltc/pk/asn1/der/boolean/der_encode_boolean.o \ +ltc/pk/asn1/der/boolean/der_length_boolean.o ltc/pk/asn1/der/choice/der_decode_choice.o \ +ltc/pk/asn1/der/custom_type/der_decode_custom_type.o ltc/pk/asn1/der/custom_type/der_encode_custom_type.o \ +ltc/pk/asn1/der/custom_type/der_length_custom_type.o ltc/pk/asn1/der/general/der_asn1_maps.o \ +ltc/pk/asn1/der/general/der_decode_asn1_identifier.o ltc/pk/asn1/der/general/der_decode_asn1_length.o \ +ltc/pk/asn1/der/general/der_encode_asn1_identifier.o ltc/pk/asn1/der/general/der_encode_asn1_length.o \ +ltc/pk/asn1/der/general/der_length_asn1_identifier.o ltc/pk/asn1/der/general/der_length_asn1_length.o \ +ltc/pk/asn1/der/generalizedtime/der_decode_generalizedtime.o ltc/pk/asn1/der/generalizedtime/der_encode_generalizedtime.o \ +ltc/pk/asn1/der/generalizedtime/der_length_generalizedtime.o ltc/pk/asn1/der/ia5/der_decode_ia5_string.o \ +ltc/pk/asn1/der/ia5/der_encode_ia5_string.o ltc/pk/asn1/der/ia5/der_length_ia5_string.o \ +ltc/pk/asn1/der/integer/der_decode_integer.o ltc/pk/asn1/der/integer/der_encode_integer.o \ +ltc/pk/asn1/der/integer/der_length_integer.o ltc/pk/asn1/der/object_identifier/der_decode_object_identifier.o \ +ltc/pk/asn1/der/object_identifier/der_encode_object_identifier.o ltc/pk/asn1/der/object_identifier/der_length_object_identifier.o \ +ltc/pk/asn1/der/octet/der_decode_octet_string.o ltc/pk/asn1/der/octet/der_encode_octet_string.o \ +ltc/pk/asn1/der/octet/der_length_octet_string.o ltc/pk/asn1/der/printable_string/der_decode_printable_string.o \ +ltc/pk/asn1/der/printable_string/der_encode_printable_string.o ltc/pk/asn1/der/printable_string/der_length_printable_string.o \ +ltc/pk/asn1/der/sequence/der_decode_sequence_ex.o ltc/pk/asn1/der/sequence/der_decode_sequence_flexi.o \ +ltc/pk/asn1/der/sequence/der_decode_sequence_multi.o ltc/pk/asn1/der/sequence/der_encode_sequence_ex.o \ +ltc/pk/asn1/der/sequence/der_encode_sequence_multi.o ltc/pk/asn1/der/sequence/der_length_sequence.o \ +ltc/pk/asn1/der/sequence/der_sequence_free.o ltc/pk/asn1/der/sequence/der_sequence_shrink.o \ +ltc/pk/asn1/der/set/der_encode_set.o ltc/pk/asn1/der/set/der_encode_setof.o ltc/pk/asn1/der/short_integer/der_decode_short_integer.o \ ltc/pk/asn1/der/short_integer/der_encode_short_integer.o ltc/pk/asn1/der/short_integer/der_length_short_integer.o \ ltc/pk/asn1/der/teletex_string/der_decode_teletex_string.o ltc/pk/asn1/der/teletex_string/der_length_teletex_string.o \ ltc/pk/asn1/der/utctime/der_decode_utctime.o ltc/pk/asn1/der/utctime/der_encode_utctime.o \ @@ -102,12 +102,12 @@ ltc/pk/dsa/dsa_set_pqg_dsaparam.o ltc/pk/dsa/dsa_shared_secret.o ltc/pk/dsa/dsa_sign_hash.o \ ltc/pk/dsa/dsa_verify_hash.o ltc/pk/dsa/dsa_verify_key.o ltc/pk/ecc/ecc.o ltc/pk/ecc/ecc_ansi_x963_export.o \ ltc/pk/ecc/ecc_ansi_x963_import.o ltc/pk/ecc/ecc_decrypt_key.o ltc/pk/ecc/ecc_encrypt_key.o \ -ltc/pk/ecc/ecc_export.o ltc/pk/ecc/ecc_export_openssl.o ltc/pk/ecc/ecc_free.o ltc/pk/ecc/ecc_get_curve.o \ -ltc/pk/ecc/ecc_get_key.o ltc/pk/ecc/ecc_get_oid_str.o ltc/pk/ecc/ecc_get_size.o ltc/pk/ecc/ecc_import.o \ -ltc/pk/ecc/ecc_import_openssl.o ltc/pk/ecc/ecc_import_pkcs8.o ltc/pk/ecc/ecc_import_x509.o \ -ltc/pk/ecc/ecc_make_key.o ltc/pk/ecc/ecc_set_dp.o ltc/pk/ecc/ecc_set_dp_internal.o \ -ltc/pk/ecc/ecc_set_key.o ltc/pk/ecc/ecc_shared_secret.o ltc/pk/ecc/ecc_sign_hash.o \ -ltc/pk/ecc/ecc_sizes.o ltc/pk/ecc/ecc_verify_hash.o ltc/pk/ecc/ltc_ecc_export_point.o \ +ltc/pk/ecc/ecc_export.o ltc/pk/ecc/ecc_export_openssl.o ltc/pk/ecc/ecc_find_curve.o \ +ltc/pk/ecc/ecc_free.o ltc/pk/ecc/ecc_get_key.o ltc/pk/ecc/ecc_get_oid_str.o ltc/pk/ecc/ecc_get_size.o \ +ltc/pk/ecc/ecc_import.o ltc/pk/ecc/ecc_import_openssl.o ltc/pk/ecc/ecc_import_pkcs8.o \ +ltc/pk/ecc/ecc_import_x509.o ltc/pk/ecc/ecc_make_key.o ltc/pk/ecc/ecc_set_curve.o \ +ltc/pk/ecc/ecc_set_curve_internal.o ltc/pk/ecc/ecc_set_key.o ltc/pk/ecc/ecc_shared_secret.o \ +ltc/pk/ecc/ecc_sign_hash.o ltc/pk/ecc/ecc_sizes.o ltc/pk/ecc/ecc_verify_hash.o ltc/pk/ecc/ltc_ecc_export_point.o \ ltc/pk/ecc/ltc_ecc_import_point.o ltc/pk/ecc/ltc_ecc_is_point.o ltc/pk/ecc/ltc_ecc_is_point_at_infinity.o \ ltc/pk/ecc/ltc_ecc_map.o ltc/pk/ecc/ltc_ecc_mul2add.o ltc/pk/ecc/ltc_ecc_mulmod.o \ ltc/pk/ecc/ltc_ecc_mulmod_timing.o ltc/pk/ecc/ltc_ecc_points.o ltc/pk/ecc/ltc_ecc_projective_add_point.o \ diff --git a/src/Makefile.nmake b/src/Makefile.nmake index 47703d5..05f9f99 100644 --- a/src/Makefile.nmake +++ b/src/Makefile.nmake @@ -60,17 +60,18 @@ ltc/misc/crypt/crypt_register_hash.obj ltc/misc/crypt/crypt_register_prng.obj ltc/misc/crypt/crypt_sizes.obj \ ltc/misc/crypt/crypt_unregister_cipher.obj ltc/misc/crypt/crypt_unregister_hash.obj \ ltc/misc/crypt/crypt_unregister_prng.obj ltc/misc/hkdf/hkdf.obj ltc/misc/padding/padding_depad.obj \ -ltc/misc/padding/padding_pad.obj ltc/misc/pkcs5/pkcs_5_1.obj ltc/misc/pkcs5/pkcs_5_2.obj \ -ltc/modes/cbc/cbc_decrypt.obj ltc/modes/cbc/cbc_done.obj ltc/modes/cbc/cbc_encrypt.obj \ -ltc/modes/cbc/cbc_getiv.obj ltc/modes/cbc/cbc_setiv.obj ltc/modes/cbc/cbc_start.obj \ -ltc/modes/cfb/cfb_decrypt.obj ltc/modes/cfb/cfb_done.obj ltc/modes/cfb/cfb_encrypt.obj \ -ltc/modes/cfb/cfb_getiv.obj ltc/modes/cfb/cfb_setiv.obj ltc/modes/cfb/cfb_start.obj \ -ltc/modes/ctr/ctr_decrypt.obj ltc/modes/ctr/ctr_done.obj ltc/modes/ctr/ctr_encrypt.obj \ -ltc/modes/ctr/ctr_getiv.obj ltc/modes/ctr/ctr_setiv.obj ltc/modes/ctr/ctr_start.obj \ -ltc/modes/ecb/ecb_decrypt.obj ltc/modes/ecb/ecb_done.obj ltc/modes/ecb/ecb_encrypt.obj \ -ltc/modes/ecb/ecb_start.obj ltc/modes/ofb/ofb_decrypt.obj ltc/modes/ofb/ofb_done.obj \ -ltc/modes/ofb/ofb_encrypt.obj ltc/modes/ofb/ofb_getiv.obj ltc/modes/ofb/ofb_setiv.obj \ -ltc/modes/ofb/ofb_start.obj ltc/pk/asn1/der/bit/der_decode_bit_string.obj ltc/pk/asn1/der/bit/der_decode_raw_bit_string.obj \ +ltc/misc/padding/padding_pad.obj ltc/misc/pkcs12/pkcs12_kdf.obj ltc/misc/pkcs12/pkcs12_utf8_to_utf16.obj \ +ltc/misc/pkcs5/pkcs_5_1.obj ltc/misc/pkcs5/pkcs_5_2.obj ltc/modes/cbc/cbc_decrypt.obj \ +ltc/modes/cbc/cbc_done.obj ltc/modes/cbc/cbc_encrypt.obj ltc/modes/cbc/cbc_getiv.obj \ +ltc/modes/cbc/cbc_setiv.obj ltc/modes/cbc/cbc_start.obj ltc/modes/cfb/cfb_decrypt.obj \ +ltc/modes/cfb/cfb_done.obj ltc/modes/cfb/cfb_encrypt.obj ltc/modes/cfb/cfb_getiv.obj \ +ltc/modes/cfb/cfb_setiv.obj ltc/modes/cfb/cfb_start.obj ltc/modes/ctr/ctr_decrypt.obj \ +ltc/modes/ctr/ctr_done.obj ltc/modes/ctr/ctr_encrypt.obj ltc/modes/ctr/ctr_getiv.obj \ +ltc/modes/ctr/ctr_setiv.obj ltc/modes/ctr/ctr_start.obj ltc/modes/ecb/ecb_decrypt.obj \ +ltc/modes/ecb/ecb_done.obj ltc/modes/ecb/ecb_encrypt.obj ltc/modes/ecb/ecb_start.obj \ +ltc/modes/ofb/ofb_decrypt.obj ltc/modes/ofb/ofb_done.obj ltc/modes/ofb/ofb_encrypt.obj \ +ltc/modes/ofb/ofb_getiv.obj ltc/modes/ofb/ofb_setiv.obj ltc/modes/ofb/ofb_start.obj \ +ltc/pk/asn1/der/bit/der_decode_bit_string.obj ltc/pk/asn1/der/bit/der_decode_raw_bit_string.obj \ ltc/pk/asn1/der/bit/der_encode_bit_string.obj ltc/pk/asn1/der/bit/der_encode_raw_bit_string.obj \ ltc/pk/asn1/der/bit/der_length_bit_string.obj ltc/pk/asn1/der/boolean/der_decode_boolean.obj \ ltc/pk/asn1/der/boolean/der_encode_boolean.obj ltc/pk/asn1/der/boolean/der_length_boolean.obj \ @@ -109,11 +110,11 @@ ltc/pk/dsa/dsa_shared_secret.obj ltc/pk/dsa/dsa_sign_hash.obj ltc/pk/dsa/dsa_verify_hash.obj \ ltc/pk/dsa/dsa_verify_key.obj ltc/pk/ecc/ecc.obj ltc/pk/ecc/ecc_ansi_x963_export.obj \ ltc/pk/ecc/ecc_ansi_x963_import.obj ltc/pk/ecc/ecc_decrypt_key.obj ltc/pk/ecc/ecc_encrypt_key.obj \ -ltc/pk/ecc/ecc_export.obj ltc/pk/ecc/ecc_export_openssl.obj ltc/pk/ecc/ecc_free.obj \ -ltc/pk/ecc/ecc_get_curve.obj ltc/pk/ecc/ecc_get_key.obj ltc/pk/ecc/ecc_get_oid_str.obj \ +ltc/pk/ecc/ecc_export.obj ltc/pk/ecc/ecc_export_openssl.obj ltc/pk/ecc/ecc_find_curve.obj \ +ltc/pk/ecc/ecc_free.obj ltc/pk/ecc/ecc_get_key.obj ltc/pk/ecc/ecc_get_oid_str.obj \ ltc/pk/ecc/ecc_get_size.obj ltc/pk/ecc/ecc_import.obj ltc/pk/ecc/ecc_import_openssl.obj \ ltc/pk/ecc/ecc_import_pkcs8.obj ltc/pk/ecc/ecc_import_x509.obj ltc/pk/ecc/ecc_make_key.obj \ -ltc/pk/ecc/ecc_set_dp.obj ltc/pk/ecc/ecc_set_dp_internal.obj ltc/pk/ecc/ecc_set_key.obj \ +ltc/pk/ecc/ecc_set_curve.obj ltc/pk/ecc/ecc_set_curve_internal.obj ltc/pk/ecc/ecc_set_key.obj \ ltc/pk/ecc/ecc_shared_secret.obj ltc/pk/ecc/ecc_sign_hash.obj ltc/pk/ecc/ecc_sizes.obj \ ltc/pk/ecc/ecc_verify_hash.obj ltc/pk/ecc/ltc_ecc_export_point.obj ltc/pk/ecc/ltc_ecc_import_point.obj \ ltc/pk/ecc/ltc_ecc_is_point.obj ltc/pk/ecc/ltc_ecc_is_point_at_infinity.obj ltc/pk/ecc/ltc_ecc_map.obj \ diff --git a/src/ltc/ciphers/aes/aes.c b/src/ltc/ciphers/aes/aes.c index 537fece..1712248 100644 --- a/src/ltc/ciphers/aes/aes.c +++ b/src/ltc/ciphers/aes/aes.c @@ -723,18 +723,19 @@ { LTC_ARGCHK(keysize != NULL); - if (*keysize < 16) + if (*keysize < 16) { return CRYPT_INVALID_KEYSIZE; + } if (*keysize < 24) { *keysize = 16; return CRYPT_OK; - } else if (*keysize < 32) { + } + if (*keysize < 32) { *keysize = 24; return CRYPT_OK; - } else { - *keysize = 32; - return CRYPT_OK; } + *keysize = 32; + return CRYPT_OK; } #endif diff --git a/src/ltc/ciphers/blowfish.c b/src/ltc/ciphers/blowfish.c index caeb46e..355a235 100644 --- a/src/ltc/ciphers/blowfish.c +++ b/src/ltc/ciphers/blowfish.c @@ -580,7 +580,8 @@ if (*keysize < 8) { return CRYPT_INVALID_KEYSIZE; - } else if (*keysize > 56) { + } + if (*keysize > 56) { *keysize = 56; } return CRYPT_OK; diff --git a/src/ltc/ciphers/cast5.c b/src/ltc/ciphers/cast5.c index 1089ade..712b57d 100644 --- a/src/ltc/ciphers/cast5.c +++ b/src/ltc/ciphers/cast5.c @@ -707,7 +707,8 @@ LTC_ARGCHK(keysize != NULL); if (*keysize < 5) { return CRYPT_INVALID_KEYSIZE; - } else if (*keysize > 16) { + } + if (*keysize > 16) { *keysize = 16; } return CRYPT_OK; diff --git a/src/ltc/ciphers/des.c b/src/ltc/ciphers/des.c index a72fdf5..a521030 100644 --- a/src/ltc/ciphers/des.c +++ b/src/ltc/ciphers/des.c @@ -2068,8 +2068,9 @@ int des3_keysize(int *keysize) { LTC_ARGCHK(keysize != NULL); - if (*keysize < 16) + if (*keysize < 16) { return CRYPT_INVALID_KEYSIZE; + } if (*keysize < 24) { *keysize = 16; return CRYPT_OK; diff --git a/src/ltc/ciphers/kasumi.c b/src/ltc/ciphers/kasumi.c index fba9b65..ccd84e5 100644 --- a/src/ltc/ciphers/kasumi.c +++ b/src/ltc/ciphers/kasumi.c @@ -243,9 +243,8 @@ if (*keysize >= 16) { *keysize = 16; return CRYPT_OK; - } else { - return CRYPT_INVALID_KEYSIZE; } + return CRYPT_INVALID_KEYSIZE; } int kasumi_test(void) diff --git a/src/ltc/ciphers/khazad.c b/src/ltc/ciphers/khazad.c index c8d8d7a..aa75182 100644 --- a/src/ltc/ciphers/khazad.c +++ b/src/ltc/ciphers/khazad.c @@ -843,9 +843,8 @@ if (*keysize >= 16) { *keysize = 16; return CRYPT_OK; - } else { - return CRYPT_INVALID_KEYSIZE; } + return CRYPT_INVALID_KEYSIZE; } #endif diff --git a/src/ltc/ciphers/noekeon.c b/src/ltc/ciphers/noekeon.c index c933d45..8850273 100644 --- a/src/ltc/ciphers/noekeon.c +++ b/src/ltc/ciphers/noekeon.c @@ -314,10 +314,9 @@ LTC_ARGCHK(keysize != NULL); if (*keysize < 16) { return CRYPT_INVALID_KEYSIZE; - } else { - *keysize = 16; - return CRYPT_OK; - } + } + *keysize = 16; + return CRYPT_OK; } #endif diff --git a/src/ltc/ciphers/rc2.c b/src/ltc/ciphers/rc2.c index 61219fe..ba09573 100644 --- a/src/ltc/ciphers/rc2.c +++ b/src/ltc/ciphers/rc2.c @@ -401,7 +401,8 @@ LTC_ARGCHK(keysize != NULL); if (*keysize < 1) { return CRYPT_INVALID_KEYSIZE; - } else if (*keysize > 128) { + } + if (*keysize > 128) { *keysize = 128; } return CRYPT_OK; diff --git a/src/ltc/ciphers/rc5.c b/src/ltc/ciphers/rc5.c index 41305f0..77951eb 100644 --- a/src/ltc/ciphers/rc5.c +++ b/src/ltc/ciphers/rc5.c @@ -308,7 +308,8 @@ LTC_ARGCHK(keysize != NULL); if (*keysize < 8) { return CRYPT_INVALID_KEYSIZE; - } else if (*keysize > 128) { + } + if (*keysize > 128) { *keysize = 128; } return CRYPT_OK; diff --git a/src/ltc/ciphers/rc6.c b/src/ltc/ciphers/rc6.c index 1a71e50..6d4ee2a 100644 --- a/src/ltc/ciphers/rc6.c +++ b/src/ltc/ciphers/rc6.c @@ -318,7 +318,8 @@ LTC_ARGCHK(keysize != NULL); if (*keysize < 8) { return CRYPT_INVALID_KEYSIZE; - } else if (*keysize > 128) { + } + if (*keysize > 128) { *keysize = 128; } return CRYPT_OK; diff --git a/src/ltc/ciphers/safer/safer.c b/src/ltc/ciphers/safer/safer.c index 913e193..61b394a 100644 --- a/src/ltc/ciphers/safer/safer.c +++ b/src/ltc/ciphers/safer/safer.c @@ -113,8 +113,9 @@ unsigned char ka[LTC_SAFER_BLOCK_LEN + 1]; unsigned char kb[LTC_SAFER_BLOCK_LEN + 1]; - if (LTC_SAFER_MAX_NOF_ROUNDS < nof_rounds) + if (LTC_SAFER_MAX_NOF_ROUNDS < nof_rounds) { nof_rounds = LTC_SAFER_MAX_NOF_ROUNDS; + } *key++ = (unsigned char)nof_rounds; ka[LTC_SAFER_BLOCK_LEN] = (unsigned char)0; kb[LTC_SAFER_BLOCK_LEN] = (unsigned char)0; @@ -176,12 +177,12 @@ } #endif -int safer_k64_setup(const unsigned char *key, int keylen, int numrounds, symmetric_key *skey) +int safer_k64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) { LTC_ARGCHK(key != NULL); LTC_ARGCHK(skey != NULL); - if (numrounds != 0 && (numrounds < 6 || numrounds > LTC_SAFER_MAX_NOF_ROUNDS)) { + if (num_rounds != 0 && (num_rounds < 6 || num_rounds > LTC_SAFER_MAX_NOF_ROUNDS)) { return CRYPT_INVALID_ROUNDS; } @@ -189,16 +190,16 @@ return CRYPT_INVALID_KEYSIZE; } - Safer_Expand_Userkey(key, key, (unsigned int)(numrounds != 0 ?numrounds:LTC_SAFER_K64_DEFAULT_NOF_ROUNDS), 0, skey->safer.key); - return CRYPT_OK; -} - -int safer_sk64_setup(const unsigned char *key, int keylen, int numrounds, symmetric_key *skey) + Safer_Expand_Userkey(key, key, (unsigned int)(num_rounds != 0 ?num_rounds:LTC_SAFER_K64_DEFAULT_NOF_ROUNDS), 0, skey->safer.key); + return CRYPT_OK; +} + +int safer_sk64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) { LTC_ARGCHK(key != NULL); LTC_ARGCHK(skey != NULL); - if (numrounds != 0 && (numrounds < 6 || numrounds > LTC_SAFER_MAX_NOF_ROUNDS)) { + if (num_rounds != 0 && (num_rounds < 6 || num_rounds > LTC_SAFER_MAX_NOF_ROUNDS)) { return CRYPT_INVALID_ROUNDS; } @@ -206,16 +207,16 @@ return CRYPT_INVALID_KEYSIZE; } - Safer_Expand_Userkey(key, key, (unsigned int)(numrounds != 0 ?numrounds:LTC_SAFER_SK64_DEFAULT_NOF_ROUNDS), 1, skey->safer.key); - return CRYPT_OK; -} - -int safer_k128_setup(const unsigned char *key, int keylen, int numrounds, symmetric_key *skey) + Safer_Expand_Userkey(key, key, (unsigned int)(num_rounds != 0 ?num_rounds:LTC_SAFER_SK64_DEFAULT_NOF_ROUNDS), 1, skey->safer.key); + return CRYPT_OK; +} + +int safer_k128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) { LTC_ARGCHK(key != NULL); LTC_ARGCHK(skey != NULL); - if (numrounds != 0 && (numrounds < 6 || numrounds > LTC_SAFER_MAX_NOF_ROUNDS)) { + if (num_rounds != 0 && (num_rounds < 6 || num_rounds > LTC_SAFER_MAX_NOF_ROUNDS)) { return CRYPT_INVALID_ROUNDS; } @@ -223,16 +224,16 @@ return CRYPT_INVALID_KEYSIZE; } - Safer_Expand_Userkey(key, key+8, (unsigned int)(numrounds != 0 ?numrounds:LTC_SAFER_K128_DEFAULT_NOF_ROUNDS), 0, skey->safer.key); - return CRYPT_OK; -} - -int safer_sk128_setup(const unsigned char *key, int keylen, int numrounds, symmetric_key *skey) + Safer_Expand_Userkey(key, key+8, (unsigned int)(num_rounds != 0 ?num_rounds:LTC_SAFER_K128_DEFAULT_NOF_ROUNDS), 0, skey->safer.key); + return CRYPT_OK; +} + +int safer_sk128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) { LTC_ARGCHK(key != NULL); LTC_ARGCHK(skey != NULL); - if (numrounds != 0 && (numrounds < 6 || numrounds > LTC_SAFER_MAX_NOF_ROUNDS)) { + if (num_rounds != 0 && (num_rounds < 6 || num_rounds > LTC_SAFER_MAX_NOF_ROUNDS)) { return CRYPT_INVALID_ROUNDS; } @@ -240,30 +241,30 @@ return CRYPT_INVALID_KEYSIZE; } - Safer_Expand_Userkey(key, key+8, (unsigned int)(numrounds != 0?numrounds:LTC_SAFER_SK128_DEFAULT_NOF_ROUNDS), 1, skey->safer.key); - return CRYPT_OK; -} - -#ifdef LTC_CLEAN_STACK -static int _safer_ecb_encrypt(const unsigned char *block_in, - unsigned char *block_out, + Safer_Expand_Userkey(key, key+8, (unsigned int)(num_rounds != 0?num_rounds:LTC_SAFER_SK128_DEFAULT_NOF_ROUNDS), 1, skey->safer.key); + return CRYPT_OK; +} + +#ifdef LTC_CLEAN_STACK +static int _safer_ecb_encrypt(const unsigned char *pt, + unsigned char *ct, const symmetric_key *skey) #else -int safer_ecb_encrypt(const unsigned char *block_in, - unsigned char *block_out, +int safer_ecb_encrypt(const unsigned char *pt, + unsigned char *ct, const symmetric_key *skey) #endif { unsigned char a, b, c, d, e, f, g, h, t; unsigned int round; const unsigned char *key; - LTC_ARGCHK(block_in != NULL); - LTC_ARGCHK(block_out != NULL); + LTC_ARGCHK(pt != NULL); + LTC_ARGCHK(ct != NULL); LTC_ARGCHK(skey != NULL); key = skey->safer.key; - a = block_in[0]; b = block_in[1]; c = block_in[2]; d = block_in[3]; - e = block_in[4]; f = block_in[5]; g = block_in[6]; h = block_in[7]; + a = pt[0]; b = pt[1]; c = pt[2]; d = pt[3]; + e = pt[4]; f = pt[5]; g = pt[6]; h = pt[7]; if (LTC_SAFER_MAX_NOF_ROUNDS < (round = *key)) round = LTC_SAFER_MAX_NOF_ROUNDS; while(round-- > 0) { @@ -280,44 +281,44 @@ } a ^= *++key; b += *++key; c += *++key; d ^= *++key; e ^= *++key; f += *++key; g += *++key; h ^= *++key; - block_out[0] = a & 0xFF; block_out[1] = b & 0xFF; - block_out[2] = c & 0xFF; block_out[3] = d & 0xFF; - block_out[4] = e & 0xFF; block_out[5] = f & 0xFF; - block_out[6] = g & 0xFF; block_out[7] = h & 0xFF; + ct[0] = a & 0xFF; ct[1] = b & 0xFF; + ct[2] = c & 0xFF; ct[3] = d & 0xFF; + ct[4] = e & 0xFF; ct[5] = f & 0xFF; + ct[6] = g & 0xFF; ct[7] = h & 0xFF; return CRYPT_OK; } #ifdef LTC_CLEAN_STACK -int safer_ecb_encrypt(const unsigned char *block_in, - unsigned char *block_out, - const symmetric_key *skey) -{ - int err = _safer_ecb_encrypt(block_in, block_out, skey); +int safer_ecb_encrypt(const unsigned char *pt, + unsigned char *ct, + const symmetric_key *skey) +{ + int err = _safer_ecb_encrypt(pt, ct, skey); burn_stack(sizeof(unsigned char) * 9 + sizeof(unsigned int) + sizeof(unsigned char *)); return err; } #endif #ifdef LTC_CLEAN_STACK -static int _safer_ecb_decrypt(const unsigned char *block_in, - unsigned char *block_out, +static int _safer_ecb_decrypt(const unsigned char *ct, + unsigned char *pt, const symmetric_key *skey) #else -int safer_ecb_decrypt(const unsigned char *block_in, - unsigned char *block_out, +int safer_ecb_decrypt(const unsigned char *ct, + unsigned char *pt, const symmetric_key *skey) #endif { unsigned char a, b, c, d, e, f, g, h, t; unsigned int round; const unsigned char *key; - LTC_ARGCHK(block_in != NULL); - LTC_ARGCHK(block_out != NULL); + LTC_ARGCHK(ct != NULL); + LTC_ARGCHK(pt != NULL); LTC_ARGCHK(skey != NULL); key = skey->safer.key; - a = block_in[0]; b = block_in[1]; c = block_in[2]; d = block_in[3]; - e = block_in[4]; f = block_in[5]; g = block_in[6]; h = block_in[7]; + a = ct[0]; b = ct[1]; c = ct[2]; d = ct[3]; + e = ct[4]; f = ct[5]; g = ct[6]; h = ct[7]; if (LTC_SAFER_MAX_NOF_ROUNDS < (round = *key)) round = LTC_SAFER_MAX_NOF_ROUNDS; key += LTC_SAFER_BLOCK_LEN * (1 + 2 * round); h ^= *key; g -= *--key; f -= *--key; e ^= *--key; @@ -335,19 +336,19 @@ d = LOG(d) ^ *--key; c = EXP(c) - *--key; b = EXP(b) - *--key; a = LOG(a) ^ *--key; } - block_out[0] = a & 0xFF; block_out[1] = b & 0xFF; - block_out[2] = c & 0xFF; block_out[3] = d & 0xFF; - block_out[4] = e & 0xFF; block_out[5] = f & 0xFF; - block_out[6] = g & 0xFF; block_out[7] = h & 0xFF; + pt[0] = a & 0xFF; pt[1] = b & 0xFF; + pt[2] = c & 0xFF; pt[3] = d & 0xFF; + pt[4] = e & 0xFF; pt[5] = f & 0xFF; + pt[6] = g & 0xFF; pt[7] = h & 0xFF; return CRYPT_OK; } #ifdef LTC_CLEAN_STACK -int safer_ecb_decrypt(const unsigned char *block_in, - unsigned char *block_out, - const symmetric_key *skey) -{ - int err = _safer_ecb_decrypt(block_in, block_out, skey); +int safer_ecb_decrypt(const unsigned char *ct, + unsigned char *pt, + const symmetric_key *skey) +{ + int err = _safer_ecb_decrypt(ct, pt, skey); burn_stack(sizeof(unsigned char) * 9 + sizeof(unsigned int) + sizeof(unsigned char *)); return err; } @@ -358,10 +359,9 @@ LTC_ARGCHK(keysize != NULL); if (*keysize < 8) { return CRYPT_INVALID_KEYSIZE; - } else { - *keysize = 8; - return CRYPT_OK; - } + } + *keysize = 8; + return CRYPT_OK; } int safer_128_keysize(int *keysize) @@ -369,10 +369,9 @@ LTC_ARGCHK(keysize != NULL); if (*keysize < 16) { return CRYPT_INVALID_KEYSIZE; - } else { - *keysize = 16; - return CRYPT_OK; - } + } + *keysize = 16; + return CRYPT_OK; } int safer_k64_test(void) diff --git a/src/ltc/ciphers/safer/saferp.c b/src/ltc/ciphers/safer/saferp.c index e6e1c90..562b2e9 100644 --- a/src/ltc/ciphers/safer/saferp.c +++ b/src/ltc/ciphers/safer/saferp.c @@ -547,8 +547,9 @@ { LTC_ARGCHK(keysize != NULL); - if (*keysize < 16) + if (*keysize < 16) { return CRYPT_INVALID_KEYSIZE; + } if (*keysize < 24) { *keysize = 16; } else if (*keysize < 32) { diff --git a/src/ltc/ciphers/skipjack.c b/src/ltc/ciphers/skipjack.c index 8726af2..8df1f85 100644 --- a/src/ltc/ciphers/skipjack.c +++ b/src/ltc/ciphers/skipjack.c @@ -330,7 +330,8 @@ LTC_ARGCHK(keysize != NULL); if (*keysize < 10) { return CRYPT_INVALID_KEYSIZE; - } else if (*keysize > 10) { + } + if (*keysize > 10) { *keysize = 10; } return CRYPT_OK; diff --git a/src/ltc/ciphers/twofish/twofish.c b/src/ltc/ciphers/twofish/twofish.c index cbc85fa..0a52aef 100644 --- a/src/ltc/ciphers/twofish/twofish.c +++ b/src/ltc/ciphers/twofish/twofish.c @@ -689,23 +689,22 @@ int twofish_keysize(int *keysize) { LTC_ARGCHK(keysize); - if (*keysize < 16) + if (*keysize < 16) { return CRYPT_INVALID_KEYSIZE; + } if (*keysize < 24) { *keysize = 16; return CRYPT_OK; - } else if (*keysize < 32) { + } + if (*keysize < 32) { *keysize = 24; return CRYPT_OK; - } else { - *keysize = 32; - return CRYPT_OK; - } -} - -#endif - - + } + *keysize = 32; + return CRYPT_OK; +} + +#endif /* ref: $Format:%D$ */ diff --git a/src/ltc/encauth/ocb3/ocb3_decrypt_last.c b/src/ltc/encauth/ocb3/ocb3_decrypt_last.c index cb0b6ea..2b5158c 100644 --- a/src/ltc/encauth/ocb3/ocb3_decrypt_last.c +++ b/src/ltc/encauth/ocb3/ocb3_decrypt_last.c @@ -66,10 +66,11 @@ /* Checksum_* = Checksum_m xor (P_* || 1 || zeros(127-bitlen(P_*))) */ ocb3_int_xor_blocks(ocb->checksum, ocb->checksum, pt+full_blocks_len, last_block_len); for(x=last_block_len; xblock_len; x++) { - if (x == last_block_len) + if (x == last_block_len) { ocb->checksum[x] ^= 0x80; - else + } else { ocb->checksum[x] ^= 0x00; + } } /* Tag = ENCIPHER(K, Checksum_* xor Offset_* xor L_$) xor HASH(K,A) */ diff --git a/src/ltc/encauth/ocb3/ocb3_encrypt_last.c b/src/ltc/encauth/ocb3/ocb3_encrypt_last.c index 5834ef4..0334305 100644 --- a/src/ltc/encauth/ocb3/ocb3_encrypt_last.c +++ b/src/ltc/encauth/ocb3/ocb3_encrypt_last.c @@ -68,10 +68,11 @@ /* Checksum_* = Checksum_m xor (P_* || 1 || zeros(127-bitlen(P_*))) */ ocb3_int_xor_blocks(ocb->checksum, ocb->checksum, pt+full_blocks_len, last_block_len); for(x=last_block_len; xblock_len; x++) { - if (x == last_block_len) + if (x == last_block_len) { ocb->checksum[x] ^= 0x80; - else + } else { ocb->checksum[x] ^= 0x00; + } } /* Tag = ENCIPHER(K, Checksum_* xor Offset_* xor L_$) xor HASH(K,A) */ @@ -82,8 +83,7 @@ if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) { goto LBL_ERR; } - } - else { + } else { /* Tag = ENCIPHER(K, Checksum_m xor Offset_m xor L_$) xor HASH(K,A) */ /* at this point we calculate only: Tag_part = ENCIPHER(K, Checksum_m xor Offset_m xor L_$) */ for(x=0; xblock_len; x++) { diff --git a/src/ltc/hashes/blake2b.c b/src/ltc/hashes/blake2b.c index 6b91a85..0f6f0c0 100644 --- a/src/ltc/hashes/blake2b.c +++ b/src/ltc/hashes/blake2b.c @@ -160,9 +160,9 @@ static void blake2b_set_lastblock(hash_state *md) { - if (md->blake2b.last_node) + if (md->blake2b.last_node) { blake2b_set_lastnode(md); - + } md->blake2b.f[0] = CONST64(0xffffffffffffffff); } @@ -177,8 +177,9 @@ unsigned long i; XMEMSET(&md->blake2b, 0, sizeof(md->blake2b)); - for (i = 0; i < 8; ++i) + for (i = 0; i < 8; ++i) { md->blake2b.h[i] = blake2b_IV[i]; + } } /* init xors IV with input parameter block */ @@ -219,11 +220,12 @@ LTC_ARGCHK(md != NULL); - if ((!outlen) || (outlen > BLAKE2B_OUTBYTES)) + if ((!outlen) || (outlen > BLAKE2B_OUTBYTES)) { return CRYPT_INVALID_ARG; - - if ((key && !keylen) || (keylen && !key) || (keylen > BLAKE2B_KEYBYTES)) + } + if ((key && !keylen) || (keylen && !key) || (keylen > BLAKE2B_KEYBYTES)) { return CRYPT_INVALID_ARG; + } XMEMSET(P, 0, sizeof(P)); @@ -416,16 +418,18 @@ /* if(md->blakebs.outlen != outlen) return CRYPT_INVALID_ARG; */ - if (blake2b_is_lastblock(md)) + if (blake2b_is_lastblock(md)) { return CRYPT_ERROR; + } blake2b_increment_counter(md, md->blake2b.curlen); blake2b_set_lastblock(md); XMEMSET(md->blake2b.buf + md->blake2b.curlen, 0, BLAKE2B_BLOCKBYTES - md->blake2b.curlen); /* Padding */ blake2b_compress(md, md->blake2b.buf); - for (i = 0; i < 8; ++i) /* Output full hash to temp buffer */ + for (i = 0; i < 8; ++i) { /* Output full hash to temp buffer */ STORE64L(md->blake2b.h[i], buffer + i * 8); + } XMEMCPY(out, buffer, md->blake2b.outlen); zeromem(md, sizeof(hash_state)); diff --git a/src/ltc/hashes/blake2s.c b/src/ltc/hashes/blake2s.c index 96439eb..2714ce6 100644 --- a/src/ltc/hashes/blake2s.c +++ b/src/ltc/hashes/blake2s.c @@ -152,9 +152,9 @@ static void blake2s_set_lastblock(hash_state *md) { - if (md->blake2s.last_node) + if (md->blake2s.last_node) { blake2s_set_lastnode(md); - + } md->blake2s.f[0] = 0xffffffffUL; } @@ -169,8 +169,9 @@ int i; XMEMSET(&md->blake2s, 0, sizeof(struct blake2s_state)); - for (i = 0; i < 8; ++i) + for (i = 0; i < 8; ++i) { md->blake2s.h[i] = blake2s_IV[i]; + } return CRYPT_OK; } @@ -213,11 +214,12 @@ LTC_ARGCHK(md != NULL); - if ((!outlen) || (outlen > BLAKE2S_OUTBYTES)) + if ((!outlen) || (outlen > BLAKE2S_OUTBYTES)) { return CRYPT_INVALID_ARG; - - if ((key && !keylen) || (keylen && !key) || (keylen > BLAKE2S_KEYBYTES)) + } + if ((key && !keylen) || (keylen && !key) || (keylen > BLAKE2S_KEYBYTES)) { return CRYPT_INVALID_ARG; + } XMEMSET(P, 0, sizeof(P)); @@ -308,8 +310,9 @@ LOAD32L(m[i], buf + i * sizeof(m[i])); } - for (i = 0; i < 8; ++i) + for (i = 0; i < 8; ++i) { v[i] = md->blake2s.h[i]; + } v[8] = blake2s_IV[0]; v[9] = blake2s_IV[1]; @@ -331,9 +334,9 @@ ROUND(8); ROUND(9); - for (i = 0; i < 8; ++i) + for (i = 0; i < 8; ++i) { md->blake2s.h[i] = md->blake2s.h[i] ^ v[i] ^ v[i + 8]; - + } return CRYPT_OK; } #undef G @@ -404,16 +407,17 @@ /* if(md->blake2s.outlen != outlen) return CRYPT_INVALID_ARG; */ - if (blake2s_is_lastblock(md)) + if (blake2s_is_lastblock(md)) { return CRYPT_ERROR; - + } blake2s_increment_counter(md, md->blake2s.curlen); blake2s_set_lastblock(md); XMEMSET(md->blake2s.buf + md->blake2s.curlen, 0, BLAKE2S_BLOCKBYTES - md->blake2s.curlen); /* Padding */ blake2s_compress(md, md->blake2s.buf); - for (i = 0; i < 8; ++i) /* Output full hash to temp buffer */ + for (i = 0; i < 8; ++i) { /* Output full hash to temp buffer */ STORE32L(md->blake2s.h[i], buffer + i * 4); + } XMEMCPY(out, buffer, md->blake2s.outlen); zeromem(md, sizeof(hash_state)); diff --git a/src/ltc/hashes/chc/chc.c b/src/ltc/hashes/chc/chc.c index bcb6947..0a526d6 100644 --- a/src/ltc/hashes/chc/chc.c +++ b/src/ltc/hashes/chc/chc.c @@ -160,7 +160,7 @@ @param len The length of the data (octets) @return CRYPT_OK if successful */ -static int _chc_process(hash_state * md, const unsigned char *buf, unsigned long len); +static int _chc_process(hash_state * md, const unsigned char *in, unsigned long inlen); static HASH_PROCESS(_chc_process, chc_compress, chc, (unsigned long)cipher_blocksize) /** diff --git a/src/ltc/hashes/sha3.c b/src/ltc/hashes/sha3.c index d9bbc8a..180344e 100644 --- a/src/ltc/hashes/sha3.c +++ b/src/ltc/hashes/sha3.c @@ -167,13 +167,14 @@ for(round = 0; round < SHA3_KECCAK_ROUNDS; round++) { /* Theta */ - for(i = 0; i < 5; i++) + for(i = 0; i < 5; i++) { bc[i] = s[i] ^ s[i + 5] ^ s[i + 10] ^ s[i + 15] ^ s[i + 20]; - + } for(i = 0; i < 5; i++) { t = bc[(i + 4) % 5] ^ ROL64(bc[(i + 1) % 5], 1); - for(j = 0; j < 25; j += 5) + for(j = 0; j < 25; j += 5) { s[j + i] ^= t; + } } /* Rho Pi */ t = s[1]; @@ -185,10 +186,12 @@ } /* Chi */ for(j = 0; j < 25; j += 5) { - for(i = 0; i < 5; i++) + for(i = 0; i < 5; i++) { bc[i] = s[j + i]; - for(i = 0; i < 5; i++) + } + for(i = 0; i < 5; i++) { s[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5]; + } } /* Iota */ s[0] ^= keccakf_rndc[round]; @@ -313,16 +316,16 @@ } #ifdef LTC_SHA3 -int sha3_done(hash_state *md, unsigned char *hash) -{ - return _done(md, hash, CONST64(0x06)); +int sha3_done(hash_state *md, unsigned char *out) +{ + return _done(md, out, CONST64(0x06)); } #endif #ifdef LTC_KECCAK -int keccak_done(hash_state *md, unsigned char *hash) -{ - return _done(md, hash, CONST64(0x01)); +int keccak_done(hash_state *md, unsigned char *out) +{ + return _done(md, out, CONST64(0x01)); } #endif diff --git a/src/ltc/headers/tomcrypt.h b/src/ltc/headers/tomcrypt.h index 1aca366..38641ae 100644 --- a/src/ltc/headers/tomcrypt.h +++ b/src/ltc/headers/tomcrypt.h @@ -27,13 +27,15 @@ /* version */ #define CRYPT 0x0118 -#define SCRYPT "1.18.1-develop" +#define SCRYPT "1.18.2-develop" /* max size of either a cipher/hash block or symmetric key [largest of the two] */ #define MAXBLOCKSIZE 144 +#ifndef TAB_SIZE /* descriptor table size */ #define TAB_SIZE 34 +#endif /* error codes [will be expanded in future releases] */ enum { diff --git a/src/ltc/headers/tomcrypt_cipher.h b/src/ltc/headers/tomcrypt_cipher.h index 4b98359..5c3a5e6 100644 --- a/src/ltc/headers/tomcrypt_cipher.h +++ b/src/ltc/headers/tomcrypt_cipher.h @@ -673,8 +673,8 @@ int safer_sk64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int safer_k128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int safer_sk128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); -int safer_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *key); -int safer_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *key); +int safer_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey); +int safer_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey); int safer_k64_test(void); int safer_sk64_test(void); int safer_sk128_test(void); @@ -1057,7 +1057,7 @@ int sosemanuk_setup(sosemanuk_state *ss, const unsigned char *key, unsigned long keylen); int sosemanuk_setiv(sosemanuk_state *ss, const unsigned char *iv, unsigned long ivlen); -int sosemanuk_crypt(sosemanuk_state *ss, const unsigned char *in, unsigned long datalen, unsigned char *out); +int sosemanuk_crypt(sosemanuk_state *ss, const unsigned char *in, unsigned long inlen, unsigned char *out); int sosemanuk_keystream(sosemanuk_state *ss, unsigned char *out, unsigned long outlen); int sosemanuk_done(sosemanuk_state *ss); int sosemanuk_test(void); diff --git a/src/ltc/headers/tomcrypt_custom.h b/src/ltc/headers/tomcrypt_custom.h index 2772718..a151187 100644 --- a/src/ltc/headers/tomcrypt_custom.h +++ b/src/ltc/headers/tomcrypt_custom.h @@ -417,7 +417,7 @@ #define LTC_DH1536 #define LTC_DH2048 -#ifndef TFM_DESC +#if defined(LTM_DESC) || defined(GMP_DESC) /* tfm has a problem in fp_isprime for larger key sizes */ #define LTC_DH3072 #define LTC_DH4096 @@ -463,6 +463,7 @@ #define LTC_PKCS_1 #define LTC_PKCS_5 +#define LTC_PKCS_12 /* Include ASN.1 DER (required by DSA/RSA) */ #define LTC_DER diff --git a/src/ltc/headers/tomcrypt_hash.h b/src/ltc/headers/tomcrypt_hash.h index dbb6b9a..fb043ca 100644 --- a/src/ltc/headers/tomcrypt_hash.h +++ b/src/ltc/headers/tomcrypt_hash.h @@ -250,7 +250,7 @@ int chc_register(int cipher); int chc_init(hash_state * md); int chc_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int chc_done(hash_state * md, unsigned char *hash); +int chc_done(hash_state * md, unsigned char *out); int chc_test(void); extern const struct ltc_hash_descriptor chc_desc; #endif @@ -258,7 +258,7 @@ #ifdef LTC_WHIRLPOOL int whirlpool_init(hash_state * md); int whirlpool_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int whirlpool_done(hash_state * md, unsigned char *hash); +int whirlpool_done(hash_state * md, unsigned char *out); int whirlpool_test(void); extern const struct ltc_hash_descriptor whirlpool_desc; #endif @@ -282,7 +282,7 @@ extern const struct ltc_hash_descriptor sha3_256_desc; int sha3_224_test(void); extern const struct ltc_hash_descriptor sha3_224_desc; -int sha3_done(hash_state *md, unsigned char *hash); +int sha3_done(hash_state *md, unsigned char *out); /* SHAKE128 + SHAKE256 */ int sha3_shake_init(hash_state *md, int num); #define sha3_shake_process(a,b,c) sha3_process(a,b,c) @@ -305,13 +305,13 @@ int keccak_256_test(void); extern const struct ltc_hash_descriptor keccak_224_desc; int keccak_224_test(void); -int keccak_done(hash_state *md, unsigned char *hash); +int keccak_done(hash_state *md, unsigned char *out); #endif #ifdef LTC_SHA512 int sha512_init(hash_state * md); int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int sha512_done(hash_state * md, unsigned char *hash); +int sha512_done(hash_state * md, unsigned char *out); int sha512_test(void); extern const struct ltc_hash_descriptor sha512_desc; #endif @@ -322,7 +322,7 @@ #endif int sha384_init(hash_state * md); #define sha384_process sha512_process -int sha384_done(hash_state * md, unsigned char *hash); +int sha384_done(hash_state * md, unsigned char *out); int sha384_test(void); extern const struct ltc_hash_descriptor sha384_desc; #endif @@ -333,7 +333,7 @@ #endif int sha512_256_init(hash_state * md); #define sha512_256_process sha512_process -int sha512_256_done(hash_state * md, unsigned char *hash); +int sha512_256_done(hash_state * md, unsigned char *out); int sha512_256_test(void); extern const struct ltc_hash_descriptor sha512_256_desc; #endif @@ -344,7 +344,7 @@ #endif int sha512_224_init(hash_state * md); #define sha512_224_process sha512_process -int sha512_224_done(hash_state * md, unsigned char *hash); +int sha512_224_done(hash_state * md, unsigned char *out); int sha512_224_test(void); extern const struct ltc_hash_descriptor sha512_224_desc; #endif @@ -352,7 +352,7 @@ #ifdef LTC_SHA256 int sha256_init(hash_state * md); int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int sha256_done(hash_state * md, unsigned char *hash); +int sha256_done(hash_state * md, unsigned char *out); int sha256_test(void); extern const struct ltc_hash_descriptor sha256_desc; @@ -362,7 +362,7 @@ #endif int sha224_init(hash_state * md); #define sha224_process sha256_process -int sha224_done(hash_state * md, unsigned char *hash); +int sha224_done(hash_state * md, unsigned char *out); int sha224_test(void); extern const struct ltc_hash_descriptor sha224_desc; #endif @@ -371,7 +371,7 @@ #ifdef LTC_SHA1 int sha1_init(hash_state * md); int sha1_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int sha1_done(hash_state * md, unsigned char *hash); +int sha1_done(hash_state * md, unsigned char *out); int sha1_test(void); extern const struct ltc_hash_descriptor sha1_desc; #endif @@ -395,7 +395,7 @@ int blake2s_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen); int blake2s_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int blake2s_done(hash_state * md, unsigned char *hash); +int blake2s_done(hash_state * md, unsigned char *out); #endif #ifdef LTC_BLAKE2B @@ -417,13 +417,13 @@ int blake2b_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen); int blake2b_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int blake2b_done(hash_state * md, unsigned char *hash); +int blake2b_done(hash_state * md, unsigned char *out); #endif #ifdef LTC_MD5 int md5_init(hash_state * md); int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int md5_done(hash_state * md, unsigned char *hash); +int md5_done(hash_state * md, unsigned char *out); int md5_test(void); extern const struct ltc_hash_descriptor md5_desc; #endif @@ -431,7 +431,7 @@ #ifdef LTC_MD4 int md4_init(hash_state * md); int md4_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int md4_done(hash_state * md, unsigned char *hash); +int md4_done(hash_state * md, unsigned char *out); int md4_test(void); extern const struct ltc_hash_descriptor md4_desc; #endif @@ -439,7 +439,7 @@ #ifdef LTC_MD2 int md2_init(hash_state * md); int md2_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int md2_done(hash_state * md, unsigned char *hash); +int md2_done(hash_state * md, unsigned char *out); int md2_test(void); extern const struct ltc_hash_descriptor md2_desc; #endif @@ -447,7 +447,7 @@ #ifdef LTC_TIGER int tiger_init(hash_state * md); int tiger_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int tiger_done(hash_state * md, unsigned char *hash); +int tiger_done(hash_state * md, unsigned char *out); int tiger_test(void); extern const struct ltc_hash_descriptor tiger_desc; #endif @@ -455,7 +455,7 @@ #ifdef LTC_RIPEMD128 int rmd128_init(hash_state * md); int rmd128_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int rmd128_done(hash_state * md, unsigned char *hash); +int rmd128_done(hash_state * md, unsigned char *out); int rmd128_test(void); extern const struct ltc_hash_descriptor rmd128_desc; #endif @@ -463,7 +463,7 @@ #ifdef LTC_RIPEMD160 int rmd160_init(hash_state * md); int rmd160_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int rmd160_done(hash_state * md, unsigned char *hash); +int rmd160_done(hash_state * md, unsigned char *out); int rmd160_test(void); extern const struct ltc_hash_descriptor rmd160_desc; #endif @@ -471,7 +471,7 @@ #ifdef LTC_RIPEMD256 int rmd256_init(hash_state * md); int rmd256_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int rmd256_done(hash_state * md, unsigned char *hash); +int rmd256_done(hash_state * md, unsigned char *out); int rmd256_test(void); extern const struct ltc_hash_descriptor rmd256_desc; #endif @@ -479,7 +479,7 @@ #ifdef LTC_RIPEMD320 int rmd320_init(hash_state * md); int rmd320_process(hash_state * md, const unsigned char *in, unsigned long inlen); -int rmd320_done(hash_state * md, unsigned char *hash); +int rmd320_done(hash_state * md, unsigned char *out); int rmd320_test(void); extern const struct ltc_hash_descriptor rmd320_desc; #endif diff --git a/src/ltc/headers/tomcrypt_mac.h b/src/ltc/headers/tomcrypt_mac.h index e4fdf14..cafb9a0 100644 --- a/src/ltc/headers/tomcrypt_mac.h +++ b/src/ltc/headers/tomcrypt_mac.h @@ -29,7 +29,7 @@ const unsigned char *in, unsigned long inlen, ...); int hmac_file(int hash, const char *fname, const unsigned char *key, unsigned long keylen, - unsigned char *dst, unsigned long *dstlen); + unsigned char *out, unsigned long *outlen); #endif #ifdef LTC_OMAC @@ -84,7 +84,7 @@ int pmac_memory(int cipher, const unsigned char *key, unsigned long keylen, - const unsigned char *msg, unsigned long msglen, + const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen); int pmac_memory_multi(int cipher, @@ -230,7 +230,7 @@ const unsigned char *in, unsigned long inlen, ...); int f9_file(int cipher, const unsigned char *key, unsigned long keylen, - const char *filename, + const char *fname, unsigned char *out, unsigned long *outlen); int f9_test(void); @@ -419,7 +419,7 @@ } ccm_state; int ccm_init(ccm_state *ccm, int cipher, - const unsigned char *key, int keylen, int ptlen, int taglen, int aad_len); + const unsigned char *key, int keylen, int ptlen, int taglen, int aadlen); int ccm_reset(ccm_state *ccm); diff --git a/src/ltc/headers/tomcrypt_misc.h b/src/ltc/headers/tomcrypt_misc.h index b30e087..a5dd130 100644 --- a/src/ltc/headers/tomcrypt_misc.h +++ b/src/ltc/headers/tomcrypt_misc.h @@ -9,26 +9,26 @@ /* ---- LTC_BASE64 Routines ---- */ #ifdef LTC_BASE64 -int base64_encode(const unsigned char *in, unsigned long len, +int base64_encode(const unsigned char *in, unsigned long inlen, char *out, unsigned long *outlen); -int base64_decode(const char *in, unsigned long len, +int base64_decode(const char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen); -int base64_strict_decode(const char *in, unsigned long len, +int base64_strict_decode(const char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen); int base64_sane_decode(const char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen); #endif #ifdef LTC_BASE64_URL -int base64url_encode(const unsigned char *in, unsigned long len, +int base64url_encode(const unsigned char *in, unsigned long inlen, char *out, unsigned long *outlen); int base64url_strict_encode(const unsigned char *in, unsigned long inlen, char *out, unsigned long *outlen); -int base64url_decode(const char *in, unsigned long len, +int base64url_decode(const char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen); -int base64url_strict_decode(const char *in, unsigned long len, +int base64url_strict_decode(const char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen); int base64url_sane_decode(const char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen); @@ -84,7 +84,7 @@ /* ---- MEM routines ---- */ int mem_neq(const void *a, const void *b, size_t len); -void zeromem(volatile void *dst, size_t len); +void zeromem(volatile void *out, size_t outlen); void burn_stack(unsigned long len); const char *error_to_string(int err); diff --git a/src/ltc/headers/tomcrypt_pk.h b/src/ltc/headers/tomcrypt_pk.h index 64e83bd..b4e5fff 100644 --- a/src/ltc/headers/tomcrypt_pk.h +++ b/src/ltc/headers/tomcrypt_pk.h @@ -251,8 +251,8 @@ void ecc_sizes(int *low, int *high); int ecc_get_size(const ecc_key *key); -int ecc_get_curve(const char* name_or_oid, const ltc_ecc_curve** cu); -int ecc_set_dp(const ltc_ecc_curve *cu, ecc_key *key); +int ecc_find_curve(const char* name_or_oid, const ltc_ecc_curve** cu); +int ecc_set_curve(const ltc_ecc_curve *cu, ecc_key *key); int ecc_generate_key(prng_state *prng, int wprng, ecc_key *key); int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key *key); int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key); @@ -573,7 +573,7 @@ /* INTEGER */ int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen); int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num); -int der_length_integer(void *num, unsigned long *len); +int der_length_integer(void *num, unsigned long *outlen); /* INTEGER -- handy for 0..2^32-1 values */ int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num); diff --git a/src/ltc/headers/tomcrypt_pkcs.h b/src/ltc/headers/tomcrypt_pkcs.h index 247e538..0c99f7c 100644 --- a/src/ltc/headers/tomcrypt_pkcs.h +++ b/src/ltc/headers/tomcrypt_pkcs.h @@ -103,6 +103,7 @@ int pkcs_5_test (void); #endif /* LTC_PKCS_5 */ + /* ref: $Format:%D$ */ /* git commit: $Format:%H$ */ /* commit time: $Format:%ai$ */ diff --git a/src/ltc/headers/tomcrypt_private.h b/src/ltc/headers/tomcrypt_private.h index eeabfc2..7fe4b01 100644 --- a/src/ltc/headers/tomcrypt_private.h +++ b/src/ltc/headers/tomcrypt_private.h @@ -192,9 +192,9 @@ /* ---- ECC Routines ---- */ #ifdef LTC_MECC -int ecc_set_dp_from_mpis(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key); -int ecc_copy_dp(const ecc_key *srckey, ecc_key *key); -int ecc_set_dp_by_size(int size, ecc_key *key); +int ecc_set_curve_from_mpis(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key); +int ecc_copy_curve(const ecc_key *srckey, ecc_key *key); +int ecc_set_curve_by_size(int size, ecc_key *key); int ecc_import_subject_public_key_info(const unsigned char *in, unsigned long inlen, ecc_key *key); /* low level functions */ @@ -275,7 +275,7 @@ int der_length_asn1_identifier(const ltc_asn1_list *id, unsigned long *idlen); int der_encode_asn1_length(unsigned long len, unsigned char* out, unsigned long* outlen); -int der_decode_asn1_length(const unsigned char* len, unsigned long* lenlen, unsigned long* outlen); +int der_decode_asn1_length(const unsigned char *in, unsigned long *inlen, unsigned long *outlen); int der_length_asn1_length(unsigned long len, unsigned long *outlen); int der_length_sequence_ex(const ltc_asn1_list *list, unsigned long inlen, @@ -304,6 +304,21 @@ ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len); #endif /* LTC_DER */ + +/* tomcrypt_pkcs.h */ + +#ifdef LTC_PKCS_12 + +int pkcs12_utf8_to_utf16(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); + +int pkcs12_kdf( int hash_id, + const unsigned char *pw, unsigned long pwlen, + const unsigned char *salt, unsigned long saltlen, + unsigned int iterations, unsigned char purpose, + unsigned char *out, unsigned long outlen); + +#endif /* LTC_PKCS_12 */ /* tomcrypt_prng.h */ diff --git a/src/ltc/mac/pmac/pmac_done.c b/src/ltc/mac/pmac/pmac_done.c index 18296e0..347890f 100644 --- a/src/ltc/mac/pmac/pmac_done.c +++ b/src/ltc/mac/pmac/pmac_done.c @@ -15,51 +15,51 @@ #ifdef LTC_PMAC -int pmac_done(pmac_state *state, unsigned char *out, unsigned long *outlen) +int pmac_done(pmac_state *pmac, unsigned char *out, unsigned long *outlen) { int err, x; - LTC_ARGCHK(state != NULL); - LTC_ARGCHK(out != NULL); - if ((err = cipher_is_valid(state->cipher_idx)) != CRYPT_OK) { + LTC_ARGCHK(pmac != NULL); + LTC_ARGCHK(out != NULL); + if ((err = cipher_is_valid(pmac->cipher_idx)) != CRYPT_OK) { return err; } - if ((state->buflen > (int)sizeof(state->block)) || (state->buflen < 0) || - (state->block_len > (int)sizeof(state->block)) || (state->buflen > state->block_len)) { + if ((pmac->buflen > (int)sizeof(pmac->block)) || (pmac->buflen < 0) || + (pmac->block_len > (int)sizeof(pmac->block)) || (pmac->buflen > pmac->block_len)) { return CRYPT_INVALID_ARG; } /* handle padding. If multiple xor in L/x */ - if (state->buflen == state->block_len) { + if (pmac->buflen == pmac->block_len) { /* xor Lr against the checksum */ - for (x = 0; x < state->block_len; x++) { - state->checksum[x] ^= state->block[x] ^ state->Lr[x]; + for (x = 0; x < pmac->block_len; x++) { + pmac->checksum[x] ^= pmac->block[x] ^ pmac->Lr[x]; } } else { /* otherwise xor message bytes then the 0x80 byte */ - for (x = 0; x < state->buflen; x++) { - state->checksum[x] ^= state->block[x]; + for (x = 0; x < pmac->buflen; x++) { + pmac->checksum[x] ^= pmac->block[x]; } - state->checksum[x] ^= 0x80; + pmac->checksum[x] ^= 0x80; } /* encrypt it */ - if ((err = cipher_descriptor[state->cipher_idx].ecb_encrypt(state->checksum, state->checksum, &state->key)) != CRYPT_OK) { + if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(pmac->checksum, pmac->checksum, &pmac->key)) != CRYPT_OK) { return err; } - cipher_descriptor[state->cipher_idx].done(&state->key); + cipher_descriptor[pmac->cipher_idx].done(&pmac->key); /* store it */ - for (x = 0; x < state->block_len && x < (int)*outlen; x++) { - out[x] = state->checksum[x]; + for (x = 0; x < pmac->block_len && x < (int)*outlen; x++) { + out[x] = pmac->checksum[x]; } *outlen = x; #ifdef LTC_CLEAN_STACK - zeromem(state, sizeof(*state)); + zeromem(pmac, sizeof(*pmac)); #endif return CRYPT_OK; } diff --git a/src/ltc/misc/adler32.c b/src/ltc/misc/adler32.c index c1d7c86..6f9a869 100644 --- a/src/ltc/misc/adler32.c +++ b/src/ltc/misc/adler32.c @@ -41,8 +41,9 @@ length--; } while (length % 8 != 0); - if (s1 >= _adler32_base) + if (s1 >= _adler32_base) { s1 -= _adler32_base; + } s2 %= _adler32_base; } @@ -67,8 +68,9 @@ length -= 8; input += 8; - if (s1 >= _adler32_base) + if (s1 >= _adler32_base) { s1 -= _adler32_base; + } s2 %= _adler32_base; } diff --git a/src/ltc/misc/base16/base16_encode.c b/src/ltc/misc/base16/base16_encode.c index f052fd1..5646ee9 100644 --- a/src/ltc/misc/base16/base16_encode.c +++ b/src/ltc/misc/base16/base16_encode.c @@ -52,8 +52,11 @@ x--; *outlen = x; /* returning the length without terminating NUL */ - if (options == 0) alphabet = alphabets[0]; - else alphabet = alphabets[1]; + if (options == 0) { + alphabet = alphabets[0]; + } else { + alphabet = alphabets[1]; + } for (i = 0; i < x; i += 2) { out[i] = alphabet[(in[i/2] >> 4) & 0x0f]; diff --git a/src/ltc/misc/base64/base64_decode.c b/src/ltc/misc/base64/base64_decode.c index 7506d50..9511ba1 100644 --- a/src/ltc/misc/base64/base64_decode.c +++ b/src/ltc/misc/base64/base64_decode.c @@ -104,16 +104,16 @@ continue; } if (c == 253) { - if (mode == strict) + if (mode == strict) { return CRYPT_INVALID_PACKET; - else - continue; /* allow to ignore white-spaces (relaxed+insane) */ + } + continue; /* allow to ignore white-spaces (relaxed+insane) */ } if (c == 255) { - if (mode == insane) + if (mode == insane) { continue; /* allow to ignore invalid garbage (insane) */ - else - return CRYPT_INVALID_PACKET; + } + return CRYPT_INVALID_PACKET; } if ((g > 0) && (mode != insane)) { /* we only allow '=' to be at the end (strict+relaxed) */ diff --git a/src/ltc/misc/burn_stack.c b/src/ltc/misc/burn_stack.c index b79f540..95fc699 100644 --- a/src/ltc/misc/burn_stack.c +++ b/src/ltc/misc/burn_stack.c @@ -21,8 +21,9 @@ { unsigned char buf[32]; zeromem(buf, sizeof(buf)); - if (len > (unsigned long)sizeof(buf)) + if (len > (unsigned long)sizeof(buf)) { burn_stack(len - sizeof(buf)); + } } diff --git a/src/ltc/misc/compare_testvector.c b/src/ltc/misc/compare_testvector.c index 842678a..b0d7baa 100644 --- a/src/ltc/misc/compare_testvector.c +++ b/src/ltc/misc/compare_testvector.c @@ -63,11 +63,11 @@ int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which) { int res = 0; - if(is_len != should_len) + if(is_len != should_len) { res = is_len > should_len ? -1 : 1; - else + } else { res = XMEMCMP(is, should, is_len); - + } #if defined(LTC_TEST) && defined(LTC_TEST_DBG) if (res != 0) { fprintf(stderr, "Testvector #%i of %s failed:\n", which, what); diff --git a/src/ltc/misc/crc32.c b/src/ltc/misc/crc32.c index 5c49f42..99c6645 100644 --- a/src/ltc/misc/crc32.c +++ b/src/ltc/misc/crc32.c @@ -153,8 +153,9 @@ LTC_ARGCHKVD(input != NULL); crc = ctx->crc; - while (length--) + while (length--) { crc = crc32_m_tab[CRC32_INDEX(crc) ^ *input++] ^ CRC32_SHIFTED(crc); + } ctx->crc = crc; } diff --git a/src/ltc/misc/crypt/crypt.c b/src/ltc/misc/crypt/crypt.c index 1f76662..0b4912c 100644 --- a/src/ltc/misc/crypt/crypt.c +++ b/src/ltc/misc/crypt/crypt.c @@ -436,6 +436,9 @@ #if defined(LTC_PKCS_5) " PKCS#5 " #endif +#if defined(LTC_PKCS_12) + " PKCS#12 " +#endif #if defined(LTC_PADDING) " PADDING " #endif diff --git a/src/ltc/misc/crypt/crypt_constants.c b/src/ltc/misc/crypt/crypt_constants.c index 8d7623d..a0d09af 100644 --- a/src/ltc/misc/crypt/crypt_constants.c +++ b/src/ltc/misc/crypt/crypt_constants.c @@ -265,8 +265,9 @@ /* calculate amount of memory required for the list */ for (i=0; i= (int)(sizeof(err_2_str)/sizeof(err_2_str[0]))) { return "Invalid error code."; - } else { - return err_2_str[err]; } + return err_2_str[err]; } diff --git a/src/ltc/misc/hkdf/hkdf.c b/src/ltc/misc/hkdf/hkdf.c index 1ca3ae3..c43a162 100644 --- a/src/ltc/misc/hkdf/hkdf.c +++ b/src/ltc/misc/hkdf/hkdf.c @@ -30,9 +30,8 @@ valid results for HKDF. */ if (salt == NULL || saltlen == 0) { return hmac_memory(hash_idx, (const unsigned char *)"", 1, in, inlen, out, outlen); - } else { - return hmac_memory(hash_idx, salt, saltlen, in, inlen, out, outlen); } + return hmac_memory(hash_idx, salt, saltlen, in, inlen, out, outlen); } int hkdf_expand(int hash_idx, const unsigned char *info, unsigned long infolen, @@ -55,10 +54,12 @@ hashsize = hash_descriptor[hash_idx].hashsize; /* RFC5869 parameter restrictions */ - if (inlen < hashsize || outlen > hashsize * 255) + if (inlen < hashsize || outlen > hashsize * 255) { return CRYPT_INVALID_ARG; - if (info == NULL && infolen != 0) + } + if (info == NULL && infolen != 0) { return CRYPT_INVALID_ARG; + } LTC_ARGCHK(out != NULL); Tlen = hashsize + infolen + 1; @@ -87,8 +88,9 @@ } outoff += Noutlen; - if (outoff >= outlen) /* loop exit condition */ + if (outoff >= outlen) { /* loop exit condition */ break; + } /* All subsequent HMAC data T(N) DOES include the previous hash value */ XMEMCPY(T, out + hashsize * (N-1), hashsize); diff --git a/src/ltc/misc/padding/padding_pad.c b/src/ltc/misc/padding/padding_pad.c index ae1a8ae..b1745ae 100644 --- a/src/ltc/misc/padding/padding_pad.c +++ b/src/ltc/misc/padding/padding_pad.c @@ -99,8 +99,11 @@ type = mode & LTC_PAD_MASK; if (*padded_length < l) { - if (type != LTC_PAD_ISO_10126) *padded_length = l; - else *padded_length = length + 256; + if (type != LTC_PAD_ISO_10126) { + *padded_length = l; + } else { + *padded_length = length + 256; + } return CRYPT_BUFFER_OVERFLOW; } diff --git a/src/ltc/misc/pkcs12/pkcs12_kdf.c b/src/ltc/misc/pkcs12/pkcs12_kdf.c new file mode 100644 index 0000000..d097e8a --- /dev/null +++ b/src/ltc/misc/pkcs12/pkcs12_kdf.c @@ -0,0 +1,92 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + */ + +#include "tomcrypt_private.h" + +#ifdef LTC_PKCS_12 + +int pkcs12_kdf( int hash_id, + const unsigned char *pw, unsigned long pwlen, + const unsigned char *salt, unsigned long saltlen, + unsigned int iterations, unsigned char purpose, + unsigned char *out, unsigned long outlen) +{ + unsigned long u = hash_descriptor[hash_id].hashsize; + unsigned long v = hash_descriptor[hash_id].blocksize; + unsigned long c = (outlen + u - 1) / u; + unsigned long Slen = ((saltlen + v - 1) / v) * v; + unsigned long Plen = ((pwlen + v - 1) / v) * v; + unsigned long k = (Plen + Slen) / v; + unsigned long Alen, keylen = 0; + unsigned int tmp, i, j, n; + unsigned char ch; + unsigned char D[MAXBLOCKSIZE], A[MAXBLOCKSIZE], B[MAXBLOCKSIZE]; + unsigned char *I = NULL, *key = NULL; + int err = CRYPT_ERROR; + + LTC_ARGCHK(pw != NULL); + LTC_ARGCHK(salt != NULL); + LTC_ARGCHK(out != NULL); + + key = XMALLOC(u * c); + I = XMALLOC(Plen + Slen); + if (key == NULL || I == NULL) goto DONE; + zeromem(key, u * c); + + for (i = 0; i < v; i++) D[i] = purpose; /* D - diversifier */ + for (i = 0; i < Slen; i++) I[i] = salt[i % saltlen]; + for (i = 0; i < Plen; i++) I[Slen + i] = pw[i % pwlen]; /* I = Salt || Pass */ + + for (i = 0; i < c; i++) { + Alen = sizeof(A); + err = hash_memory_multi(hash_id, A, &Alen, D, v, I, Slen + Plen, NULL); /* A = HASH(D || I) */ + if (err != CRYPT_OK) goto DONE; + for (j = 1; j < iterations; j++) { + err = hash_memory(hash_id, A, Alen, A, &Alen); /* A = HASH(A) */ + if (err != CRYPT_OK) goto DONE; + } + /* fill buffer B with A */ + for (j = 0; j < v; j++) B[j] = A[j % Alen]; + /* B += 1 */ + for (j = v; j > 0; j--) { + if (++B[j - 1] != 0) break; + } + /* I_n += B */ + for (n = 0; n < k; n++) { + ch = 0; + for (j = v; j > 0; j--) { + tmp = I[n * v + j - 1] + B[j - 1] + ch; + ch = (unsigned char)((tmp >> 8) & 0xFF); + I[n * v + j - 1] = (unsigned char)(tmp & 0xFF); + } + } + /* store derived key block */ + XMEMCPY(&key[keylen], A, Alen); + keylen += Alen; + } + + XMEMCPY(out, key, outlen); + err = CRYPT_OK; +DONE: + if (I) { + zeromem(I, Plen + Slen); + XFREE(I); + } + if (key) { + zeromem(key, u * c); + XFREE(key); + } + return err; +} + +#endif + +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/src/ltc/misc/pkcs12/pkcs12_utf8_to_utf16.c b/src/ltc/misc/pkcs12/pkcs12_utf8_to_utf16.c new file mode 100644 index 0000000..5175a15 --- /dev/null +++ b/src/ltc/misc/pkcs12/pkcs12_utf8_to_utf16.c @@ -0,0 +1,69 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + */ + +#include "tomcrypt_private.h" + +#ifdef LTC_PKCS_12 + +int pkcs12_utf8_to_utf16(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen) { + unsigned long len = 0; + const unsigned char* in_end = in + inlen; + const ulong32 offset[6] = { + 0x00000000UL, 0x00003080UL, 0x000E2080UL, + 0x03C82080UL, 0xFA082080UL, 0x82082080UL + }; + int err = CRYPT_ERROR; + + LTC_ARGCHK(in != NULL); + LTC_ARGCHK(out != NULL); + LTC_ARGCHK(outlen != NULL); + + while (in < in_end) { + ulong32 ch = 0; + unsigned short extra = 0; /* 0 */ + if (*in >= 192) extra++; /* 1 */ + if (*in >= 224) extra++; /* 2 */ + if (*in >= 240) extra++; /* 3 */ + if (*in >= 248) extra++; /* 4 */ + if (*in >= 252) extra++; /* 5 */ + if (in + extra >= in_end) goto ERROR; + switch (extra) { + case 5: ch += *in++; ch <<= 6; + /* FALLTHROUGH */ + case 4: ch += *in++; ch <<= 6; + /* FALLTHROUGH */ + case 3: ch += *in++; ch <<= 6; + /* FALLTHROUGH */ + case 2: ch += *in++; ch <<= 6; + /* FALLTHROUGH */ + case 1: ch += *in++; ch <<= 6; + /* FALLTHROUGH */ + case 0: ch += *in++; + } + ch -= offset[extra]; + if (ch > 0xFFFF) goto ERROR; + if (*outlen >= len + 2) { + out[len] = (unsigned short)((ch >> 8) & 0xFF); + out[len + 1] = (unsigned char)(ch & 0xFF); + } + len += 2; + } + + err = len > *outlen ? CRYPT_BUFFER_OVERFLOW : CRYPT_OK; + *outlen = len; +ERROR: + return err; +} + +#endif + +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/src/ltc/misc/pkcs5/pkcs_5_1.c b/src/ltc/misc/pkcs5/pkcs_5_1.c index 0794698..a90bc57 100644 --- a/src/ltc/misc/pkcs5/pkcs_5_1.c +++ b/src/ltc/misc/pkcs5/pkcs_5_1.c @@ -109,19 +109,23 @@ the output buffer (and how many bytes we have to copy) */ outidx = block*hash_descriptor[hash_idx].hashsize; nb = hash_descriptor[hash_idx].hashsize; - if(outidx+nb > *outlen) + if(outidx+nb > *outlen) { nb = *outlen - outidx; - if(nb > 0) + } + if(nb > 0) { XMEMCPY(out+outidx, buf, nb); + } block++; - if (!openssl_compat) + if (!openssl_compat) { break; + } } /* In strict mode, we always return the hashsize, in compat we filled it as much as was requested, so we leave it alone. */ - if(!openssl_compat) + if(!openssl_compat) { *outlen = hash_descriptor[hash_idx].hashsize; + } err = CRYPT_OK; LBL_ERR: diff --git a/src/ltc/modes/cbc/cbc_decrypt.c b/src/ltc/modes/cbc/cbc_decrypt.c index 9cbf6f3..fa56c69 100644 --- a/src/ltc/modes/cbc/cbc_decrypt.c +++ b/src/ltc/modes/cbc/cbc_decrypt.c @@ -58,32 +58,31 @@ 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); - } else { - while (len) { - /* decrypt */ - if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) { - return err; - } + } + while (len) { + /* decrypt */ + if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) { + return err; + } - /* xor IV against plaintext */ - #if defined(LTC_FAST) - for (x = 0; x < cbc->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++) { - tmpy = tmp[x] ^ cbc->IV[x]; - cbc->IV[x] = ct[x]; - pt[x] = tmpy; - } - #endif + /* xor IV against plaintext */ +#if defined(LTC_FAST) + for (x = 0; x < cbc->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++) { + 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->blocklen; + pt += cbc->blocklen; + len -= cbc->blocklen; } return CRYPT_OK; } diff --git a/src/ltc/modes/cbc/cbc_encrypt.c b/src/ltc/modes/cbc/cbc_encrypt.c index 6ba9a0c..88f519d 100644 --- a/src/ltc/modes/cbc/cbc_encrypt.c +++ b/src/ltc/modes/cbc/cbc_encrypt.c @@ -52,39 +52,38 @@ 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); - } else { - while (len) { - /* xor IV against plaintext */ - #if defined(LTC_FAST) - for (x = 0; x < cbc->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++) { - cbc->IV[x] ^= pt[x]; - } - #endif + } + while (len) { + /* xor IV against plaintext */ +#if defined(LTC_FAST) + for (x = 0; x < cbc->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++) { + cbc->IV[x] ^= pt[x]; + } +#endif - /* encrypt */ - if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) { - return err; - } + /* encrypt */ + if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != 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)) { - *(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++) { - cbc->IV[x] = ct[x]; - } - #endif + /* store IV [ciphertext] for a future block */ +#if defined(LTC_FAST) + for (x = 0; x < cbc->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++) { + cbc->IV[x] = ct[x]; + } +#endif - ct += cbc->blocklen; - pt += cbc->blocklen; - len -= cbc->blocklen; - } + ct += cbc->blocklen; + pt += cbc->blocklen; + len -= cbc->blocklen; } return CRYPT_OK; } diff --git a/src/ltc/modes/cfb/cfb_start.c b/src/ltc/modes/cfb/cfb_start.c index caccb5a..22b3e3c 100644 --- a/src/ltc/modes/cfb/cfb_start.c +++ b/src/ltc/modes/cfb/cfb_start.c @@ -43,8 +43,9 @@ /* copy data */ cfb->cipher = cipher; cfb->blocklen = cipher_descriptor[cipher].block_length; - for (x = 0; x < cfb->blocklen; x++) + for (x = 0; x < cfb->blocklen; x++) { cfb->IV[x] = IV[x]; + } /* init the cipher */ if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &cfb->key)) != CRYPT_OK) { diff --git a/src/ltc/modes/ecb/ecb_decrypt.c b/src/ltc/modes/ecb/ecb_decrypt.c index b8727d2..fdeec4d 100644 --- a/src/ltc/modes/ecb/ecb_decrypt.c +++ b/src/ltc/modes/ecb/ecb_decrypt.c @@ -39,15 +39,14 @@ /* check for accel */ if (cipher_descriptor[ecb->cipher].accel_ecb_decrypt != NULL) { return cipher_descriptor[ecb->cipher].accel_ecb_decrypt(ct, pt, len / cipher_descriptor[ecb->cipher].block_length, &ecb->key); - } else { - while (len) { - if ((err = cipher_descriptor[ecb->cipher].ecb_decrypt(ct, pt, &ecb->key)) != CRYPT_OK) { - return err; - } - pt += cipher_descriptor[ecb->cipher].block_length; - ct += cipher_descriptor[ecb->cipher].block_length; - len -= cipher_descriptor[ecb->cipher].block_length; + } + while (len) { + if ((err = cipher_descriptor[ecb->cipher].ecb_decrypt(ct, pt, &ecb->key)) != CRYPT_OK) { + return err; } + pt += cipher_descriptor[ecb->cipher].block_length; + ct += cipher_descriptor[ecb->cipher].block_length; + len -= cipher_descriptor[ecb->cipher].block_length; } return CRYPT_OK; } diff --git a/src/ltc/modes/ecb/ecb_encrypt.c b/src/ltc/modes/ecb/ecb_encrypt.c index d31635b..e36c3d3 100644 --- a/src/ltc/modes/ecb/ecb_encrypt.c +++ b/src/ltc/modes/ecb/ecb_encrypt.c @@ -39,15 +39,14 @@ /* check for accel */ if (cipher_descriptor[ecb->cipher].accel_ecb_encrypt != NULL) { return cipher_descriptor[ecb->cipher].accel_ecb_encrypt(pt, ct, len / cipher_descriptor[ecb->cipher].block_length, &ecb->key); - } else { - while (len) { - if ((err = cipher_descriptor[ecb->cipher].ecb_encrypt(pt, ct, &ecb->key)) != CRYPT_OK) { - return err; - } - pt += cipher_descriptor[ecb->cipher].block_length; - ct += cipher_descriptor[ecb->cipher].block_length; - len -= cipher_descriptor[ecb->cipher].block_length; + } + while (len) { + if ((err = cipher_descriptor[ecb->cipher].ecb_encrypt(pt, ct, &ecb->key)) != CRYPT_OK) { + return err; } + pt += cipher_descriptor[ecb->cipher].block_length; + ct += cipher_descriptor[ecb->cipher].block_length; + len -= cipher_descriptor[ecb->cipher].block_length; } return CRYPT_OK; } diff --git a/src/ltc/pk/asn1/der/general/der_decode_asn1_length.c b/src/ltc/pk/asn1/der/general/der_decode_asn1_length.c index 0021eea..bf8595d 100644 --- a/src/ltc/pk/asn1/der/general/der_decode_asn1_length.c +++ b/src/ltc/pk/asn1/der/general/der_decode_asn1_length.c @@ -41,9 +41,11 @@ real_len &= 0x7F; if (real_len == 0) { return CRYPT_PK_ASN1_ERROR; - } else if (real_len > sizeof(decoded_len)) { + } + if (real_len > sizeof(decoded_len)) { return CRYPT_OVERFLOW; - } else if (real_len > (*inlen - 1)) { + } + if (real_len > (*inlen - 1)) { return CRYPT_BUFFER_OVERFLOW; } decoded_len = 0; diff --git a/src/ltc/pk/asn1/der/general/der_encode_asn1_identifier.c b/src/ltc/pk/asn1/der/general/der_encode_asn1_identifier.c index c366ad3..38fcaf6 100644 --- a/src/ltc/pk/asn1/der/general/der_encode_asn1_identifier.c +++ b/src/ltc/pk/asn1/der/general/der_encode_asn1_identifier.c @@ -41,16 +41,15 @@ } *outlen = 1; return CRYPT_OK; - } else { - if (id->klass < LTC_ASN1_CL_UNIVERSAL || id->klass > LTC_ASN1_CL_PRIVATE) { - return CRYPT_INVALID_ARG; - } - if (id->pc < LTC_ASN1_PC_PRIMITIVE || id->pc > LTC_ASN1_PC_CONSTRUCTED) { - return CRYPT_INVALID_ARG; - } - if (id->tag > (ULONG_MAX >> (8 + 7))) { - return CRYPT_INVALID_ARG; - } + } + if (id->klass < LTC_ASN1_CL_UNIVERSAL || id->klass > LTC_ASN1_CL_PRIVATE) { + return CRYPT_INVALID_ARG; + } + if (id->pc < LTC_ASN1_PC_PRIMITIVE || id->pc > LTC_ASN1_PC_CONSTRUCTED) { + return CRYPT_INVALID_ARG; + } + if (id->tag > (ULONG_MAX >> (8 + 7))) { + return CRYPT_INVALID_ARG; } if (out != NULL) { diff --git a/src/ltc/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c b/src/ltc/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c index a5f8473..34ea828 100644 --- a/src/ltc/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c +++ b/src/ltc/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c @@ -112,7 +112,8 @@ /* now is it Z or . */ if (buf[x] == 'Z') { return CRYPT_OK; - } else if (buf[x] == '.') { + } + if (buf[x] == '.') { x++; while (buf[x] >= '0' && buf[x] <= '9') { unsigned fs = out->fs; @@ -127,14 +128,14 @@ /* now is it Z, +, - */ if (buf[x] == 'Z') { return CRYPT_OK; - } else if (buf[x] == '+' || buf[x] == '-') { + } + if (buf[x] == '+' || buf[x] == '-') { out->off_dir = (buf[x++] == '+') ? 0 : 1; DECODE_V(out->off_hh, 24); DECODE_V(out->off_mm, 60); return CRYPT_OK; - } else { - return CRYPT_INVALID_PACKET; } + return CRYPT_INVALID_PACKET; } #endif diff --git a/src/ltc/pk/asn1/der/set/der_encode_set.c b/src/ltc/pk/asn1/der/set/der_encode_set.c index 636669c..2597197 100644 --- a/src/ltc/pk/asn1/der/set/der_encode_set.c +++ b/src/ltc/pk/asn1/der/set/der_encode_set.c @@ -33,9 +33,8 @@ if (r == 0) { /* their order in the original list now determines the position */ return A->used - B->used; - } else { - return r; } + return r; } /* diff --git a/src/ltc/pk/asn1/der/utctime/der_decode_utctime.c b/src/ltc/pk/asn1/der/utctime/der_decode_utctime.c index fba9184..c377396 100644 --- a/src/ltc/pk/asn1/der/utctime/der_decode_utctime.c +++ b/src/ltc/pk/asn1/der/utctime/der_decode_utctime.c @@ -95,7 +95,8 @@ /* now is it Z, +, - or 0-9 */ if (buf[x] == 'Z') { return CRYPT_OK; - } else if (buf[x] == '+' || buf[x] == '-') { + } + if (buf[x] == '+' || buf[x] == '-') { out->off_dir = (buf[x++] == '+') ? 0 : 1; DECODE_V(out->off_hh, 24); DECODE_V(out->off_mm, 60); @@ -108,14 +109,14 @@ /* now is it Z, +, - */ if (buf[x] == 'Z') { return CRYPT_OK; - } else if (buf[x] == '+' || buf[x] == '-') { + } + if (buf[x] == '+' || buf[x] == '-') { out->off_dir = (buf[x++] == '+') ? 0 : 1; DECODE_V(out->off_hh, 24); DECODE_V(out->off_mm, 60); return CRYPT_OK; - } else { - return CRYPT_INVALID_PACKET; } + return CRYPT_INVALID_PACKET; } #endif diff --git a/src/ltc/pk/asn1/der/utf8/der_length_utf8_string.c b/src/ltc/pk/asn1/der/utf8/der_length_utf8_string.c index 48eed3b..acece0c 100644 --- a/src/ltc/pk/asn1/der/utf8/der_length_utf8_string.c +++ b/src/ltc/pk/asn1/der/utf8/der_length_utf8_string.c @@ -23,18 +23,17 @@ { if (c <= 0x7F) { return 1; - } else if (c <= 0x7FF) { + } + if (c <= 0x7FF) { return 2; + } #if LTC_WCHAR_MAX == 0xFFFF - } else { + return 3; +#else + if (c <= 0xFFFF) { return 3; } -#else - } else if (c <= 0xFFFF) { - return 3; - } else { - return 4; - } + return 4; #endif } diff --git a/src/ltc/pk/dh/dh_generate_key.c b/src/ltc/pk/dh/dh_generate_key.c index ba737fc..fc9e851 100644 --- a/src/ltc/pk/dh/dh_generate_key.c +++ b/src/ltc/pk/dh/dh_generate_key.c @@ -19,27 +19,25 @@ if (groupsize <= 0) { return 0; } - else if (groupsize <= 192) { + if (groupsize <= 192) { return 30; /* 1536-bit => key size 240-bit */ } - else if (groupsize <= 256) { + if (groupsize <= 256) { return 40; /* 2048-bit => key size 320-bit */ } - else if (groupsize <= 384) { + if (groupsize <= 384) { return 52; /* 3072-bit => key size 416-bit */ } - else if (groupsize <= 512) { + if (groupsize <= 512) { return 60; /* 4096-bit => key size 480-bit */ } - else if (groupsize <= 768) { + if (groupsize <= 768) { return 67; /* 6144-bit => key size 536-bit */ } - else if (groupsize <= 1024) { + if (groupsize <= 1024) { return 77; /* 8192-bit => key size 616-bit */ } - else { - return 0; - } + return 0; } int dh_generate_key(prng_state *prng, int wprng, dh_key *key) diff --git a/src/ltc/pk/dsa/dsa_export.c b/src/ltc/pk/dsa/dsa_export.c index 9058dd3..d254ae6 100644 --- a/src/ltc/pk/dsa/dsa_export.c +++ b/src/ltc/pk/dsa/dsa_export.c @@ -26,6 +26,7 @@ int dsa_export(unsigned char *out, unsigned long *outlen, int type, const dsa_key *key) { unsigned long zero=0; + unsigned char flags[1]; int err, std; LTC_ARGCHK(out != NULL); @@ -35,13 +36,8 @@ std = type & PK_STD; type &= ~PK_STD; - /* can we store the static header? */ if (type == PK_PRIVATE && key->type != PK_PRIVATE) { return CRYPT_PK_TYPE_MISMATCH; - } - - if (type != PK_PUBLIC && type != PK_PRIVATE) { - return CRYPT_INVALID_ARG; } if (type == PK_PRIVATE) { @@ -55,10 +51,8 @@ LTC_ASN1_INTEGER, 1UL, key->x, LTC_ASN1_EOL, 0UL, NULL); } - else { - unsigned char flags[1]; - flags[0] = 1; - return der_encode_sequence_multi(out, outlen, + flags[0] = 1; + return der_encode_sequence_multi(out, outlen, LTC_ASN1_BIT_STRING, 1UL, flags, LTC_ASN1_INTEGER, 1UL, key->g, LTC_ASN1_INTEGER, 1UL, key->p, @@ -66,8 +60,9 @@ LTC_ASN1_INTEGER, 1UL, key->y, LTC_ASN1_INTEGER, 1UL, key->x, LTC_ASN1_EOL, 0UL, NULL); - } - } else { + } + + if (type == PK_PUBLIC) { if (std) { unsigned long tmplen = (unsigned long)(mp_count_bits(key->y) / 8) + 8; unsigned char* tmp = XMALLOC(tmplen); @@ -94,18 +89,17 @@ XFREE(tmp); return err; } - else { - unsigned char flags[1]; - flags[0] = 0; - return der_encode_sequence_multi(out, outlen, + flags[0] = 0; + return der_encode_sequence_multi(out, outlen, LTC_ASN1_BIT_STRING, 1UL, flags, LTC_ASN1_INTEGER, 1UL, key->g, LTC_ASN1_INTEGER, 1UL, key->p, LTC_ASN1_INTEGER, 1UL, key->q, LTC_ASN1_INTEGER, 1UL, key->y, LTC_ASN1_EOL, 0UL, NULL); - } } + + return CRYPT_INVALID_ARG; } #endif diff --git a/src/ltc/pk/ecc/ecc_ansi_x963_import.c b/src/ltc/pk/ecc/ecc_ansi_x963_import.c index 6dbdd21..1dccfbe 100644 --- a/src/ltc/pk/ecc/ecc_ansi_x963_import.c +++ b/src/ltc/pk/ecc/ecc_ansi_x963_import.c @@ -41,11 +41,11 @@ /* initialize key->dp */ if (cu == NULL) { /* this case works only for uncompressed public keys */ - if ((err = ecc_set_dp_by_size((inlen-1)>>1, key)) != CRYPT_OK) { return err; } + if ((err = ecc_set_curve_by_size((inlen-1)>>1, key)) != CRYPT_OK) { return err; } } else { /* this one works for both compressed / uncompressed pubkeys */ - if ((err = ecc_set_dp(cu, key)) != CRYPT_OK) { return err; } + if ((err = ecc_set_curve(cu, key)) != CRYPT_OK) { return err; } } /* load public key */ diff --git a/src/ltc/pk/ecc/ecc_decrypt_key.c b/src/ltc/pk/ecc/ecc_decrypt_key.c index 8c29a17..d6b825e 100644 --- a/src/ltc/pk/ecc/ecc_decrypt_key.c +++ b/src/ltc/pk/ecc/ecc_decrypt_key.c @@ -85,7 +85,7 @@ } /* import ECC key from packet */ - if ((err = ecc_copy_dp(key, &pubkey)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ecc_copy_curve(key, &pubkey)) != CRYPT_OK) { goto LBL_ERR; } if ((err = ecc_set_key(decode[1].data, decode[1].size, PK_PUBLIC, &pubkey)) != CRYPT_OK) { goto LBL_ERR; } /* make shared key */ diff --git a/src/ltc/pk/ecc/ecc_encrypt_key.c b/src/ltc/pk/ecc/ecc_encrypt_key.c index 17201df..2ea7e35 100644 --- a/src/ltc/pk/ecc/ecc_encrypt_key.c +++ b/src/ltc/pk/ecc/ecc_encrypt_key.c @@ -52,7 +52,7 @@ } /* make a random key and export the public copy */ - if ((err = ecc_copy_dp(key, &pubkey)) != CRYPT_OK) { return err; } + if ((err = ecc_copy_curve(key, &pubkey)) != CRYPT_OK) { return err; } if ((err = ecc_generate_key(prng, wprng, &pubkey)) != CRYPT_OK) { return err; } pub_expt = XMALLOC(ECC_BUF_SIZE); diff --git a/src/ltc/pk/ecc/ecc_find_curve.c b/src/ltc/pk/ecc/ecc_find_curve.c new file mode 100644 index 0000000..d9544fd --- /dev/null +++ b/src/ltc/pk/ecc/ecc_find_curve.c @@ -0,0 +1,252 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + */ + +#include "tomcrypt_private.h" + +#ifdef LTC_MECC + +static const struct { + const char *OID; + const char *names[6]; +} _curve_names[] = { +#ifdef LTC_ECC_SECP112R1 + { + "1.3.132.0.6", { "SECP112R1", "ECC-112", NULL } + }, +#endif +#ifdef LTC_ECC_SECP112R2 + { + "1.3.132.0.7", { "SECP112R2", NULL } + }, +#endif +#ifdef LTC_ECC_SECP128R1 + { + "1.3.132.0.28", { "SECP128R1", "ECC-128", NULL } + }, +#endif +#ifdef LTC_ECC_SECP128R2 + { + "1.3.132.0.29", { "SECP128R2", NULL } + }, +#endif +#ifdef LTC_ECC_SECP160R1 + { + "1.3.132.0.8", { "SECP160R1", "ECC-160", NULL } + }, +#endif +#ifdef LTC_ECC_SECP160R2 + { + "1.3.132.0.30", { "SECP160R2", NULL } + }, +#endif +#ifdef LTC_ECC_SECP160K1 + { + "1.3.132.0.9", { "SECP160K1", NULL } + }, +#endif +#ifdef LTC_ECC_SECP192R1 + { + "1.2.840.10045.3.1.1", { "SECP192R1", "NISTP192", "PRIME192V1", "ECC-192", "P-192", NULL } + }, +#endif +#ifdef LTC_ECC_PRIME192V2 + { + "1.2.840.10045.3.1.2", { "PRIME192V2", NULL } + }, +#endif +#ifdef LTC_ECC_PRIME192V3 + { + "1.2.840.10045.3.1.3", { "PRIME192V3", NULL } + }, +#endif +#ifdef LTC_ECC_SECP192K1 + { + "1.3.132.0.31", { "SECP192K1", NULL } + }, +#endif +#ifdef LTC_ECC_SECP224R1 + { + "1.3.132.0.33", { "SECP224R1", "NISTP224", "ECC-224", "P-224", NULL } + }, +#endif +#ifdef LTC_ECC_SECP224K1 + { + "1.3.132.0.32", { "SECP224K1", NULL } + }, +#endif +#ifdef LTC_ECC_SECP256R1 + { + "1.2.840.10045.3.1.7", { "SECP256R1", "NISTP256", "PRIME256V1", "ECC-256", "P-256", NULL } + }, +#endif +#ifdef LTC_ECC_SECP256K1 + { + "1.3.132.0.10", { "SECP256K1", NULL } + }, +#endif +#ifdef LTC_ECC_SECP384R1 + { + "1.3.132.0.34", { "SECP384R1", "NISTP384", "ECC-384", "P-384", NULL } + }, +#endif +#ifdef LTC_ECC_SECP521R1 + { + "1.3.132.0.35", { "SECP521R1", "NISTP521", "ECC-521", "P-521", NULL } + }, +#endif +#ifdef LTC_ECC_PRIME239V1 + { + "1.2.840.10045.3.1.4", { "PRIME239V1", NULL } + }, +#endif +#ifdef LTC_ECC_PRIME239V2 + { + "1.2.840.10045.3.1.5", { "PRIME239V2", NULL } + }, +#endif +#ifdef LTC_ECC_PRIME239V3 + { + "1.2.840.10045.3.1.6", { "PRIME239V3", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP160R1 + { + "1.3.36.3.3.2.8.1.1.1", { "BRAINPOOLP160R1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP192R1 + { + "1.3.36.3.3.2.8.1.1.3", { "BRAINPOOLP192R1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP224R1 + { + "1.3.36.3.3.2.8.1.1.5", { "BRAINPOOLP224R1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP256R1 + { + "1.3.36.3.3.2.8.1.1.7", { "BRAINPOOLP256R1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP320R1 + { + "1.3.36.3.3.2.8.1.1.9", { "BRAINPOOLP320R1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP384R1 + { + "1.3.36.3.3.2.8.1.1.11", { "BRAINPOOLP384R1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP512R1 + { + "1.3.36.3.3.2.8.1.1.13", { "BRAINPOOLP512R1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP160T1 + { + "1.3.36.3.3.2.8.1.1.2", { "BRAINPOOLP160T1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP192T1 + { + "1.3.36.3.3.2.8.1.1.4", { "BRAINPOOLP192T1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP224T1 + { + "1.3.36.3.3.2.8.1.1.6", { "BRAINPOOLP224T1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP256T1 + { + "1.3.36.3.3.2.8.1.1.8", { "BRAINPOOLP256T1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP320T1 + { + "1.3.36.3.3.2.8.1.1.10", { "BRAINPOOLP320T1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP384T1 + { + "1.3.36.3.3.2.8.1.1.12", { "BRAINPOOLP384T1", NULL } + }, +#endif +#ifdef LTC_ECC_BRAINPOOLP512T1 + { + "1.3.36.3.3.2.8.1.1.14", { "BRAINPOOLP512T1", NULL } + }, +#endif + { + NULL, { NULL } + } +}; + +/* case-insensitive match + ignore '-', '_', ' ' */ +static int _name_match(const char *left, const char *right) +{ + char lc_r, lc_l; + + while ((*left != '\0') && (*right != '\0')) { + while ((*left == ' ') || (*left == '-') || (*left == '_')) left++; + while ((*right == ' ') || (*right == '-') || (*right == '_')) right++; + if (*left == '\0' || *right == '\0') break; + lc_r = *right; + lc_l = *left; + if ((lc_r >= 'A') && (lc_r <= 'Z')) lc_r += 32; + if ((lc_l >= 'A') && (lc_l <= 'Z')) lc_l += 32; + if (lc_l != lc_r) return 0; + left++; + right++; + } + + if ((*left == '\0') && (*right == '\0')) return 1; + return 0; +} + +int ecc_find_curve(const char *name_or_oid, const ltc_ecc_curve **cu) +{ + int i, j; + const char *OID = NULL; + + LTC_ARGCHK(cu != NULL); + LTC_ARGCHK(name_or_oid != NULL); + + *cu = NULL; + + for (i = 0; _curve_names[i].OID != NULL && !OID; i++) { + if (XSTRCMP(_curve_names[i].OID, name_or_oid) == 0) { + OID = _curve_names[i].OID; + } + for (j = 0; _curve_names[i].names[j] != NULL && !OID; j++) { + if (_name_match(_curve_names[i].names[j], name_or_oid)) { + OID = _curve_names[i].OID; + } + } + } + + if (OID != NULL) { + for (i = 0; ltc_ecc_curves[i].prime != NULL; i++) { + if (XSTRCMP(ltc_ecc_curves[i].OID, OID) == 0) { + *cu = <c_ecc_curves[i]; + return CRYPT_OK; + } + } + } + + return CRYPT_INVALID_ARG; /* not found */ +} + +#endif + +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/src/ltc/pk/ecc/ecc_get_curve.c b/src/ltc/pk/ecc/ecc_get_curve.c deleted file mode 100644 index a2f52b0..0000000 --- a/src/ltc/pk/ecc/ecc_get_curve.c +++ /dev/null @@ -1,254 +0,0 @@ -/* LibTomCrypt, modular cryptographic library -- Tom St Denis - * - * LibTomCrypt is a library that provides various cryptographic - * algorithms in a highly modular and flexible manner. - * - * The library is free for all purposes without any express - * guarantee it works. - */ - -#include "tomcrypt_private.h" - -#ifdef LTC_MECC - -static const struct { - const char *OID; - const char *names[6]; -} _curve_names[] = { -#ifdef LTC_ECC_SECP112R1 - { - "1.3.132.0.6", { "SECP112R1", "ECC-112", NULL } - }, -#endif -#ifdef LTC_ECC_SECP112R2 - { - "1.3.132.0.7", { "SECP112R2", NULL } - }, -#endif -#ifdef LTC_ECC_SECP128R1 - { - "1.3.132.0.28", { "SECP128R1", "ECC-128", NULL } - }, -#endif -#ifdef LTC_ECC_SECP128R2 - { - "1.3.132.0.29", { "SECP128R2", NULL } - }, -#endif -#ifdef LTC_ECC_SECP160R1 - { - "1.3.132.0.8", { "SECP160R1", "ECC-160", NULL } - }, -#endif -#ifdef LTC_ECC_SECP160R2 - { - "1.3.132.0.30", { "SECP160R2", NULL } - }, -#endif -#ifdef LTC_ECC_SECP160K1 - { - "1.3.132.0.9", { "SECP160K1", NULL } - }, -#endif -#ifdef LTC_ECC_SECP192R1 - { - "1.2.840.10045.3.1.1", { "SECP192R1", "NISTP192", "PRIME192V1", "ECC-192", "P-192", NULL } - }, -#endif -#ifdef LTC_ECC_PRIME192V2 - { - "1.2.840.10045.3.1.2", { "PRIME192V2", NULL } - }, -#endif -#ifdef LTC_ECC_PRIME192V3 - { - "1.2.840.10045.3.1.3", { "PRIME192V3", NULL } - }, -#endif -#ifdef LTC_ECC_SECP192K1 - { - "1.3.132.0.31", { "SECP192K1", NULL } - }, -#endif -#ifdef LTC_ECC_SECP224R1 - { - "1.3.132.0.33", { "SECP224R1", "NISTP224", "ECC-224", "P-224", NULL } - }, -#endif -#ifdef LTC_ECC_SECP224K1 - { - "1.3.132.0.32", { "SECP224K1", NULL } - }, -#endif -#ifdef LTC_ECC_SECP256R1 - { - "1.2.840.10045.3.1.7", { "SECP256R1", "NISTP256", "PRIME256V1", "ECC-256", "P-256", NULL } - }, -#endif -#ifdef LTC_ECC_SECP256K1 - { - "1.3.132.0.10", { "SECP256K1", NULL } - }, -#endif -#ifdef LTC_ECC_SECP384R1 - { - "1.3.132.0.34", { "SECP384R1", "NISTP384", "ECC-384", "P-384", NULL } - }, -#endif -#ifdef LTC_ECC_SECP521R1 - { - "1.3.132.0.35", { "SECP521R1", "NISTP521", "ECC-521", "P-521", NULL } - }, -#endif -#ifdef LTC_ECC_PRIME239V1 - { - "1.2.840.10045.3.1.4", { "PRIME239V1", NULL } - }, -#endif -#ifdef LTC_ECC_PRIME239V2 - { - "1.2.840.10045.3.1.5", { "PRIME239V2", NULL } - }, -#endif -#ifdef LTC_ECC_PRIME239V3 - { - "1.2.840.10045.3.1.6", { "PRIME239V3", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP160R1 - { - "1.3.36.3.3.2.8.1.1.1", { "BRAINPOOLP160R1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP192R1 - { - "1.3.36.3.3.2.8.1.1.3", { "BRAINPOOLP192R1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP224R1 - { - "1.3.36.3.3.2.8.1.1.5", { "BRAINPOOLP224R1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP256R1 - { - "1.3.36.3.3.2.8.1.1.7", { "BRAINPOOLP256R1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP320R1 - { - "1.3.36.3.3.2.8.1.1.9", { "BRAINPOOLP320R1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP384R1 - { - "1.3.36.3.3.2.8.1.1.11", { "BRAINPOOLP384R1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP512R1 - { - "1.3.36.3.3.2.8.1.1.13", { "BRAINPOOLP512R1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP160T1 - { - "1.3.36.3.3.2.8.1.1.2", { "BRAINPOOLP160T1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP192T1 - { - "1.3.36.3.3.2.8.1.1.4", { "BRAINPOOLP192T1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP224T1 - { - "1.3.36.3.3.2.8.1.1.6", { "BRAINPOOLP224T1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP256T1 - { - "1.3.36.3.3.2.8.1.1.8", { "BRAINPOOLP256T1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP320T1 - { - "1.3.36.3.3.2.8.1.1.10", { "BRAINPOOLP320T1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP384T1 - { - "1.3.36.3.3.2.8.1.1.12", { "BRAINPOOLP384T1", NULL } - }, -#endif -#ifdef LTC_ECC_BRAINPOOLP512T1 - { - "1.3.36.3.3.2.8.1.1.14", { "BRAINPOOLP512T1", NULL } - }, -#endif - { - NULL, { NULL } - } -}; - -/* case-insensitive match + ignore '-', '_', ' ' */ -static int _name_match(const char *left, const char *right) -{ - char lc_r, lc_l; - - while ((*left != '\0') && (*right != '\0')) { - while ((*left == ' ') || (*left == '-') || (*left == '_')) left++; - while ((*right == ' ') || (*right == '-') || (*right == '_')) right++; - if (*left == '\0' || *right == '\0') break; - lc_r = *right; - lc_l = *left; - if ((lc_r >= 'A') && (lc_r <= 'Z')) lc_r += 32; - if ((lc_l >= 'A') && (lc_l <= 'Z')) lc_l += 32; - if (lc_l != lc_r) return 0; - left++; - right++; - } - - if ((*left == '\0') && (*right == '\0')) - return 1; - else - return 0; -} - -int ecc_get_curve(const char *name_or_oid, const ltc_ecc_curve **cu) -{ - int i, j; - const char *OID = NULL; - - LTC_ARGCHK(cu != NULL); - LTC_ARGCHK(name_or_oid != NULL); - - *cu = NULL; - - for (i = 0; _curve_names[i].OID != NULL && !OID; i++) { - if (XSTRCMP(_curve_names[i].OID, name_or_oid) == 0) { - OID = _curve_names[i].OID; - } - for (j = 0; _curve_names[i].names[j] != NULL && !OID; j++) { - if (_name_match(_curve_names[i].names[j], name_or_oid)) { - OID = _curve_names[i].OID; - } - } - } - - if (OID != NULL) { - for (i = 0; ltc_ecc_curves[i].prime != NULL; i++) { - if (XSTRCMP(ltc_ecc_curves[i].OID, OID) == 0) { - *cu = <c_ecc_curves[i]; - return CRYPT_OK; - } - } - } - - return CRYPT_INVALID_ARG; /* not found */ -} - -#endif - -/* ref: $Format:%D$ */ -/* git commit: $Format:%H$ */ -/* commit time: $Format:%ai$ */ diff --git a/src/ltc/pk/ecc/ecc_import.c b/src/ltc/pk/ecc/ecc_import.c index e691add..74097a7 100644 --- a/src/ltc/pk/ecc/ecc_import.c +++ b/src/ltc/pk/ecc/ecc_import.c @@ -56,9 +56,9 @@ /* allocate & initialize the key */ if (cu == NULL) { - if ((err = ecc_set_dp_by_size(key_size, key)) != CRYPT_OK) { goto done; } + if ((err = ecc_set_curve_by_size(key_size, key)) != CRYPT_OK) { goto done; } } else { - if ((err = ecc_set_dp(cu, key)) != CRYPT_OK) { goto done; } + if ((err = ecc_set_curve(cu, key)) != CRYPT_OK) { goto done; } } if (flags[0] == 1) { diff --git a/src/ltc/pk/ecc/ecc_import_openssl.c b/src/ltc/pk/ecc/ecc_import_openssl.c index a4b2cf5..e765091 100644 --- a/src/ltc/pk/ecc/ecc_import_openssl.c +++ b/src/ltc/pk/ecc/ecc_import_openssl.c @@ -36,8 +36,8 @@ /* load curve parameters for given curve OID */ len = sizeof(OID); if ((err = pk_oid_num_to_str(curveoid, custom[0].size, OID, &len)) != CRYPT_OK) { goto error; } - if ((err = ecc_get_curve(OID, &curve)) != CRYPT_OK) { goto error; } - if ((err = ecc_set_dp(curve, key)) != CRYPT_OK) { goto error; } + if ((err = ecc_find_curve(OID, &curve)) != CRYPT_OK) { goto error; } + if ((err = ecc_set_curve(curve, key)) != CRYPT_OK) { goto error; } /* load private+public key */ err = ecc_set_key(bin_k, seq_priv[1].size, PK_PRIVATE, key); } @@ -96,7 +96,7 @@ if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; } if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; } /* load curve parameters */ - if ((err = ecc_set_dp_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; } + if ((err = ecc_set_curve_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; } /* load private+public key */ err = ecc_set_key(bin_k, len_k, PK_PRIVATE, key); } diff --git a/src/ltc/pk/ecc/ecc_import_pkcs8.c b/src/ltc/pk/ecc/ecc_import_pkcs8.c index 323db2a..e606699 100644 --- a/src/ltc/pk/ecc/ecc_import_pkcs8.c +++ b/src/ltc/pk/ecc/ecc_import_pkcs8.c @@ -54,115 +54,6 @@ { { 0 }, 0 }, }; -static int _simple_utf8_to_utf16(const unsigned char *in, unsigned long inlen, - unsigned char *out, unsigned long *outlen) { - unsigned long len = 0; - const unsigned char* in_end = in + inlen; - const ulong32 offset[6] = { - 0x00000000UL, 0x00003080UL, 0x000E2080UL, - 0x03C82080UL, 0xFA082080UL, 0x82082080UL - }; - int err = CRYPT_ERROR; - - while (in < in_end) { - ulong32 ch = 0; - unsigned short extra = 0; /* 0 */ - if (*in >= 192) extra++; /* 1 */ - if (*in >= 224) extra++; /* 2 */ - if (*in >= 240) extra++; /* 3 */ - if (*in >= 248) extra++; /* 4 */ - if (*in >= 252) extra++; /* 5 */ - if (in + extra >= in_end) goto ERROR; - switch (extra) { - case 5: ch += *in++; ch <<= 6; - /* FALLTHROUGH */ - case 4: ch += *in++; ch <<= 6; - /* FALLTHROUGH */ - case 3: ch += *in++; ch <<= 6; - /* FALLTHROUGH */ - case 2: ch += *in++; ch <<= 6; - /* FALLTHROUGH */ - case 1: ch += *in++; ch <<= 6; - /* FALLTHROUGH */ - case 0: ch += *in++; - } - ch -= offset[extra]; - if (ch > 0xFFFF) goto ERROR; - if (*outlen >= len + 2) { - out[len] = (unsigned short)((ch >> 8) & 0xFF); - out[len + 1] = (unsigned char)(ch & 0xFF); - } - len += 2; - } - - err = len > *outlen ? CRYPT_BUFFER_OVERFLOW : CRYPT_OK; - *outlen = len; -ERROR: - return err; -} - -static int _kdf_pkcs12(int hash_id, const unsigned char *pw, unsigned long pwlen, - const unsigned char *salt, unsigned long saltlen, - unsigned int iterations, unsigned char purpose, - unsigned char *out, unsigned long outlen) -{ - unsigned long u = hash_descriptor[hash_id].hashsize; - unsigned long v = hash_descriptor[hash_id].blocksize; - unsigned long c = (outlen + u - 1) / u; - unsigned long Slen = ((saltlen + v - 1) / v) * v; - unsigned long Plen = ((pwlen + v - 1) / v) * v; - unsigned long k = (Plen + Slen) / v; - unsigned long Alen, keylen = 0; - unsigned int tmp, i, j, n; - unsigned char ch; - unsigned char D[MAXBLOCKSIZE], A[MAXBLOCKSIZE], B[MAXBLOCKSIZE]; - unsigned char *I = NULL, *key = NULL; - int err = CRYPT_ERROR; - - key = XMALLOC(u * c); - I = XMALLOC(Plen + Slen); - if (key == NULL || I == NULL) goto DONE; - zeromem(key, u * c); - - for (i = 0; i < v; i++) D[i] = purpose; /* D - diversifier */ - for (i = 0; i < Slen; i++) I[i] = salt[i % saltlen]; - for (i = 0; i < Plen; i++) I[Slen + i] = pw[i % pwlen]; /* I = Salt || Pass */ - - for (i = 0; i < c; i++) { - Alen = u; /* hash size */ - err = hash_memory_multi(hash_id, A, &Alen, D, v, I, Slen + Plen, NULL); /* A = HASH(D || I) */ - if (err != CRYPT_OK) goto DONE; - for (j = 1; j < iterations; j++) { - err = hash_memory(hash_id, A, Alen, A, &Alen); /* A = HASH(A) */ - if (err != CRYPT_OK) goto DONE; - } - /* fill buffer B with A */ - for (j = 0; j < v; j++) B[j] = A[j % Alen]; - /* B += 1 */ - for (j = v; j > 0; j--) { - if (++B[j - 1] != 0) break; - } - /* I_n += B */ - for (n = 0; n < k; n++) { - ch = 0; - for (j = v; j > 0; j--) { - tmp = I[n * v + j - 1] + B[j - 1] + ch; - ch = (unsigned char)((tmp >> 8) & 0xFF); - I[n * v + j - 1] = (unsigned char)(tmp & 0xFF); - } - } - /* store derived key block */ - for (j = 0; j < Alen; j++) key[keylen++] = A[j]; - } - - for (i = 0; i < outlen; i++) out[i] = key[i]; - err = CRYPT_OK; -DONE: - if (I) XFREE(I); - if (key) XFREE(key); - return err; -} - static int _oid_to_id(const unsigned long *oid, unsigned long oid_size) { int i, j; @@ -216,13 +107,13 @@ pwlen = pass_size * 2; pw = XMALLOC(pwlen + 2); if (pw == NULL) goto LBL_ERROR; - if ((err = _simple_utf8_to_utf16(pass, pass_size, pw, &pwlen) != CRYPT_OK)) goto LBL_ERROR; + if ((err = pkcs12_utf8_to_utf16(pass, pass_size, pw, &pwlen) != CRYPT_OK)) goto LBL_ERROR; pw[pwlen++] = 0; pw[pwlen++] = 0; /* derive KEY */ - if ((err = _kdf_pkcs12(hid, pw, pwlen, salt, salt_size, iterations, 1, key_iv, keylen)) != CRYPT_OK) goto LBL_ERROR; + if ((err = pkcs12_kdf(hid, pw, pwlen, salt, salt_size, iterations, 1, key_iv, keylen)) != CRYPT_OK) goto LBL_ERROR; /* derive IV */ - if ((err = _kdf_pkcs12(hid, pw, pwlen, salt, salt_size, iterations, 2, key_iv+24, blklen)) != CRYPT_OK) goto LBL_ERROR; + if ((err = pkcs12_kdf(hid, pw, pwlen, salt, salt_size, iterations, 2, key_iv+24, blklen)) != CRYPT_OK) goto LBL_ERROR; } else { if ((err = pkcs_5_alg1(pass, pass_size, salt, iterations, hid, key_iv, &len)) != CRYPT_OK) goto LBL_ERROR; @@ -515,8 +406,8 @@ ltc_asn1_list *loid = lseq->child->next; len = sizeof(OID); if ((err = pk_oid_num_to_str(loid->data, loid->size, OID, &len)) != CRYPT_OK) { goto LBL_DONE; } - if ((err = ecc_get_curve(OID, &curve)) != CRYPT_OK) { goto LBL_DONE; } - if ((err = ecc_set_dp(curve, key)) != CRYPT_OK) { goto LBL_DONE; } + if ((err = ecc_find_curve(OID, &curve)) != CRYPT_OK) { goto LBL_DONE; } + if ((err = ecc_set_curve(curve, key)) != CRYPT_OK) { goto LBL_DONE; } } else if (LTC_ASN1_IS_TYPE(lseq->child->next, LTC_ASN1_SEQUENCE)) { /* CASE 2: explicit curve parameters (AKA long variant): @@ -567,7 +458,7 @@ if ((err = ltc_ecc_import_point(lg->data, lg->size, lprime->data, a, b, gx, gy)) != CRYPT_OK) { goto LBL_DONE; } - if ((err = ecc_set_dp_from_mpis(a, b, lprime->data, lorder->data, gx, gy, cofactor, key)) != CRYPT_OK) { + if ((err = ecc_set_curve_from_mpis(a, b, lprime->data, lorder->data, gx, gy, cofactor, key)) != CRYPT_OK) { goto LBL_DONE; } } diff --git a/src/ltc/pk/ecc/ecc_import_x509.c b/src/ltc/pk/ecc/ecc_import_x509.c index 2e7eb10..99a2750 100644 --- a/src/ltc/pk/ecc/ecc_import_x509.c +++ b/src/ltc/pk/ecc/ecc_import_x509.c @@ -27,8 +27,8 @@ /* load curve parameters for given curve OID */ len = sizeof(OID); if ((err = pk_oid_num_to_str(curveoid, len_oid, OID, &len)) != CRYPT_OK) { goto error; } - if ((err = ecc_get_curve(OID, &curve)) != CRYPT_OK) { goto error; } - if ((err = ecc_set_dp(curve, key)) != CRYPT_OK) { goto error; } + if ((err = ecc_find_curve(OID, &curve)) != CRYPT_OK) { goto error; } + if ((err = ecc_set_curve(curve, key)) != CRYPT_OK) { goto error; } /* load public key */ err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key); } @@ -80,7 +80,7 @@ if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; } if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; } /* load curve parameters */ - if ((err = ecc_set_dp_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; } + if ((err = ecc_set_curve_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; } /* load public key */ err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key); } diff --git a/src/ltc/pk/ecc/ecc_make_key.c b/src/ltc/pk/ecc/ecc_make_key.c index da59585..4a8f91e 100644 --- a/src/ltc/pk/ecc/ecc_make_key.c +++ b/src/ltc/pk/ecc/ecc_make_key.c @@ -28,15 +28,15 @@ { int err; - if ((err = ecc_set_dp_by_size(keysize, key)) != CRYPT_OK) { return err; } - if ((err = ecc_generate_key(prng, wprng, key)) != CRYPT_OK) { return err; } + if ((err = ecc_set_curve_by_size(keysize, key)) != CRYPT_OK) { return err; } + if ((err = ecc_generate_key(prng, wprng, key)) != CRYPT_OK) { return err; } return CRYPT_OK; } int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_curve *cu) { int err; - if ((err = ecc_set_dp(cu, key)) != CRYPT_OK) { return err; } + if ((err = ecc_set_curve(cu, key)) != CRYPT_OK) { return err; } if ((err = ecc_generate_key(prng, wprng, key)) != CRYPT_OK) { return err; } return CRYPT_OK; } diff --git a/src/ltc/pk/ecc/ecc_set_curve.c b/src/ltc/pk/ecc/ecc_set_curve.c new file mode 100644 index 0000000..9fa44fd --- /dev/null +++ b/src/ltc/pk/ecc/ecc_set_curve.c @@ -0,0 +1,90 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + */ + +#include "tomcrypt_private.h" + +#ifdef LTC_MECC + +int ecc_set_curve(const ltc_ecc_curve *cu, ecc_key *key) +{ + int err; + + LTC_ARGCHK(key != NULL); + LTC_ARGCHK(cu != NULL); + + if ((err = mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, + &key->dp.base.x, &key->dp.base.y, &key->dp.base.z, + &key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, + NULL)) != CRYPT_OK) { + return err; + } + + /* A, B, order, prime, Gx, Gy */ + if ((err = mp_read_radix(key->dp.prime, cu->prime, 16)) != CRYPT_OK) { goto error; } + if ((err = mp_read_radix(key->dp.order, cu->order, 16)) != CRYPT_OK) { goto error; } + if ((err = mp_read_radix(key->dp.A, cu->A, 16)) != CRYPT_OK) { goto error; } + if ((err = mp_read_radix(key->dp.B, cu->B, 16)) != CRYPT_OK) { goto error; } + if ((err = mp_read_radix(key->dp.base.x, cu->Gx, 16)) != CRYPT_OK) { goto error; } + if ((err = mp_read_radix(key->dp.base.y, cu->Gy, 16)) != CRYPT_OK) { goto error; } + if ((err = mp_set(key->dp.base.z, 1)) != CRYPT_OK) { goto error; } + /* cofactor & size */ + key->dp.cofactor = cu->cofactor; + key->dp.size = mp_unsigned_bin_size(key->dp.prime); + /* OID string >> unsigned long oid[16] + oidlen */ + key->dp.oidlen = 16; + if ((err = pk_oid_str_to_num(cu->OID, key->dp.oid, &key->dp.oidlen)) != CRYPT_OK) { goto error; } + /* success */ + return CRYPT_OK; + +error: + ecc_free(key); + return err; +} + +int ecc_set_curve_by_size(int size, ecc_key *key) +{ + const ltc_ecc_curve *cu = NULL; + int err = CRYPT_ERROR; + + /* for compatibility with libtomcrypt-1.17 the sizes below must match the specific curves */ + if (size <= 14) { + err = ecc_find_curve("SECP112R1", &cu); + } + else if (size <= 16) { + err = ecc_find_curve("SECP128R1", &cu); + } + else if (size <= 20) { + err = ecc_find_curve("SECP160R1", &cu); + } + else if (size <= 24) { + err = ecc_find_curve("SECP192R1", &cu); + } + else if (size <= 28) { + err = ecc_find_curve("SECP224R1", &cu); + } + else if (size <= 32) { + err = ecc_find_curve("SECP256R1", &cu); + } + else if (size <= 48) { + err = ecc_find_curve("SECP384R1", &cu); + } + else if (size <= 66) { + err = ecc_find_curve("SECP521R1", &cu); + } + + if (err == CRYPT_OK && cu != NULL) return ecc_set_curve(cu, key); + + return CRYPT_INVALID_ARG; +} + +#endif + +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/src/ltc/pk/ecc/ecc_set_curve_internal.c b/src/ltc/pk/ecc/ecc_set_curve_internal.c new file mode 100644 index 0000000..aed515c --- /dev/null +++ b/src/ltc/pk/ecc/ecc_set_curve_internal.c @@ -0,0 +1,129 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + */ + +#include "tomcrypt_private.h" + +#ifdef LTC_MECC + +static int _ecc_cmp_hex_bn(const char *left_hex, void *right_bn, void *tmp_bn) +{ + if (mp_read_radix(tmp_bn, left_hex, 16) != CRYPT_OK) return 0; + if (mp_cmp(tmp_bn, right_bn) != LTC_MP_EQ) return 0; + return 1; +} + +static void _ecc_oid_lookup(ecc_key *key) +{ + void *bn; + const ltc_ecc_curve *curve; + + key->dp.oidlen = 0; + if (mp_init(&bn) != CRYPT_OK) return; + for (curve = ltc_ecc_curves; curve->prime != NULL; curve++) { + if (_ecc_cmp_hex_bn(curve->prime, key->dp.prime, bn) != 1) continue; + if (_ecc_cmp_hex_bn(curve->order, key->dp.order, bn) != 1) continue; + if (_ecc_cmp_hex_bn(curve->A, key->dp.A, bn) != 1) continue; + if (_ecc_cmp_hex_bn(curve->B, key->dp.B, bn) != 1) continue; + if (_ecc_cmp_hex_bn(curve->Gx, key->dp.base.x, bn) != 1) continue; + if (_ecc_cmp_hex_bn(curve->Gy, key->dp.base.y, bn) != 1) continue; + if (key->dp.cofactor != curve->cofactor) continue; + break; /* found */ + } + mp_clear(bn); + if (curve->prime && curve->OID) { + key->dp.oidlen = 16; /* size of key->dp.oid */ + pk_oid_str_to_num(curve->OID, key->dp.oid, &key->dp.oidlen); + } +} + +int ecc_copy_curve(const ecc_key *srckey, ecc_key *key) +{ + unsigned long i; + int err; + + LTC_ARGCHK(key != NULL); + LTC_ARGCHK(srckey != NULL); + + if ((err = mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, + &key->dp.base.x, &key->dp.base.y, &key->dp.base.z, + &key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, + NULL)) != CRYPT_OK) { + return err; + } + + /* A, B, order, prime, Gx, Gy */ + if ((err = mp_copy(srckey->dp.prime, key->dp.prime )) != CRYPT_OK) { goto error; } + if ((err = mp_copy(srckey->dp.order, key->dp.order )) != CRYPT_OK) { goto error; } + if ((err = mp_copy(srckey->dp.A, key->dp.A )) != CRYPT_OK) { goto error; } + if ((err = mp_copy(srckey->dp.B, key->dp.B )) != CRYPT_OK) { goto error; } + if ((err = ltc_ecc_copy_point(&srckey->dp.base, &key->dp.base)) != CRYPT_OK) { goto error; } + /* cofactor & size */ + key->dp.cofactor = srckey->dp.cofactor; + key->dp.size = srckey->dp.size; + /* OID */ + if (srckey->dp.oidlen > 0) { + key->dp.oidlen = srckey->dp.oidlen; + for (i = 0; i < key->dp.oidlen; i++) key->dp.oid[i] = srckey->dp.oid[i]; + } + else { + _ecc_oid_lookup(key); /* try to find OID in ltc_ecc_curves */ + } + /* success */ + return CRYPT_OK; + +error: + ecc_free(key); + return err; +} + +int ecc_set_curve_from_mpis(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key) +{ + int err; + + LTC_ARGCHK(key != NULL); + LTC_ARGCHK(a != NULL); + LTC_ARGCHK(b != NULL); + LTC_ARGCHK(prime != NULL); + LTC_ARGCHK(order != NULL); + LTC_ARGCHK(gx != NULL); + LTC_ARGCHK(gy != NULL); + + if ((err = mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, + &key->dp.base.x, &key->dp.base.y, &key->dp.base.z, + &key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, + NULL)) != CRYPT_OK) { + return err; + } + + /* A, B, order, prime, Gx, Gy */ + if ((err = mp_copy(prime, key->dp.prime )) != CRYPT_OK) { goto error; } + if ((err = mp_copy(order, key->dp.order )) != CRYPT_OK) { goto error; } + if ((err = mp_copy(a, key->dp.A )) != CRYPT_OK) { goto error; } + if ((err = mp_copy(b, key->dp.B )) != CRYPT_OK) { goto error; } + if ((err = mp_copy(gx, key->dp.base.x)) != CRYPT_OK) { goto error; } + if ((err = mp_copy(gy, key->dp.base.y)) != CRYPT_OK) { goto error; } + if ((err = mp_set(key->dp.base.z, 1)) != CRYPT_OK) { goto error; } + /* cofactor & size */ + key->dp.cofactor = cofactor; + key->dp.size = mp_unsigned_bin_size(prime); + /* try to find OID in ltc_ecc_curves */ + _ecc_oid_lookup(key); + /* success */ + return CRYPT_OK; + +error: + ecc_free(key); + return err; +} + +#endif + +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/src/ltc/pk/ecc/ecc_set_dp.c b/src/ltc/pk/ecc/ecc_set_dp.c deleted file mode 100644 index 4c4f487..0000000 --- a/src/ltc/pk/ecc/ecc_set_dp.c +++ /dev/null @@ -1,90 +0,0 @@ -/* LibTomCrypt, modular cryptographic library -- Tom St Denis - * - * LibTomCrypt is a library that provides various cryptographic - * algorithms in a highly modular and flexible manner. - * - * The library is free for all purposes without any express - * guarantee it works. - */ - -#include "tomcrypt_private.h" - -#ifdef LTC_MECC - -int ecc_set_dp(const ltc_ecc_curve *curve, ecc_key *key) -{ - int err; - - LTC_ARGCHK(key != NULL); - LTC_ARGCHK(curve != NULL); - - if ((err = mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, - &key->dp.base.x, &key->dp.base.y, &key->dp.base.z, - &key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, - NULL)) != CRYPT_OK) { - return err; - } - - /* A, B, order, prime, Gx, Gy */ - if ((err = mp_read_radix(key->dp.prime, curve->prime, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_read_radix(key->dp.order, curve->order, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_read_radix(key->dp.A, curve->A, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_read_radix(key->dp.B, curve->B, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_read_radix(key->dp.base.x, curve->Gx, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_read_radix(key->dp.base.y, curve->Gy, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_set(key->dp.base.z, 1)) != CRYPT_OK) { goto error; } - /* cofactor & size */ - key->dp.cofactor = curve->cofactor; - key->dp.size = mp_unsigned_bin_size(key->dp.prime); - /* OID string >> unsigned long oid[16] + oidlen */ - key->dp.oidlen = 16; - if ((err = pk_oid_str_to_num(curve->OID, key->dp.oid, &key->dp.oidlen)) != CRYPT_OK) { goto error; } - /* success */ - return CRYPT_OK; - -error: - ecc_free(key); - return err; -} - -int ecc_set_dp_by_size(int size, ecc_key *key) -{ - const ltc_ecc_curve *cu = NULL; - int err = CRYPT_ERROR; - - /* for compatibility with libtomcrypt-1.17 the sizes below must match the specific curves */ - if (size <= 14) { - err = ecc_get_curve("SECP112R1", &cu); - } - else if (size <= 16) { - err = ecc_get_curve("SECP128R1", &cu); - } - else if (size <= 20) { - err = ecc_get_curve("SECP160R1", &cu); - } - else if (size <= 24) { - err = ecc_get_curve("SECP192R1", &cu); - } - else if (size <= 28) { - err = ecc_get_curve("SECP224R1", &cu); - } - else if (size <= 32) { - err = ecc_get_curve("SECP256R1", &cu); - } - else if (size <= 48) { - err = ecc_get_curve("SECP384R1", &cu); - } - else if (size <= 66) { - err = ecc_get_curve("SECP521R1", &cu); - } - - if (err == CRYPT_OK && cu != NULL) return ecc_set_dp(cu, key); - - return CRYPT_INVALID_ARG; -} - -#endif - -/* ref: $Format:%D$ */ -/* git commit: $Format:%H$ */ -/* commit time: $Format:%ai$ */ diff --git a/src/ltc/pk/ecc/ecc_set_dp_internal.c b/src/ltc/pk/ecc/ecc_set_dp_internal.c deleted file mode 100644 index cf9020b..0000000 --- a/src/ltc/pk/ecc/ecc_set_dp_internal.c +++ /dev/null @@ -1,129 +0,0 @@ -/* LibTomCrypt, modular cryptographic library -- Tom St Denis - * - * LibTomCrypt is a library that provides various cryptographic - * algorithms in a highly modular and flexible manner. - * - * The library is free for all purposes without any express - * guarantee it works. - */ - -#include "tomcrypt_private.h" - -#ifdef LTC_MECC - -static int _ecc_cmp_hex_bn(const char *left_hex, void *right_bn, void *tmp_bn) -{ - if (mp_read_radix(tmp_bn, left_hex, 16) != CRYPT_OK) return 0; - if (mp_cmp(tmp_bn, right_bn) != LTC_MP_EQ) return 0; - return 1; -} - -static void _ecc_oid_lookup(ecc_key *key) -{ - void *bn; - const ltc_ecc_curve *curve; - - key->dp.oidlen = 0; - if (mp_init(&bn) != CRYPT_OK) return; - for (curve = ltc_ecc_curves; curve->prime != NULL; curve++) { - if (_ecc_cmp_hex_bn(curve->prime, key->dp.prime, bn) != 1) continue; - if (_ecc_cmp_hex_bn(curve->order, key->dp.order, bn) != 1) continue; - if (_ecc_cmp_hex_bn(curve->A, key->dp.A, bn) != 1) continue; - if (_ecc_cmp_hex_bn(curve->B, key->dp.B, bn) != 1) continue; - if (_ecc_cmp_hex_bn(curve->Gx, key->dp.base.x, bn) != 1) continue; - if (_ecc_cmp_hex_bn(curve->Gy, key->dp.base.y, bn) != 1) continue; - if (key->dp.cofactor != curve->cofactor) continue; - break; /* found */ - } - mp_clear(bn); - if (curve->prime && curve->OID) { - key->dp.oidlen = 16; /* size of key->dp.oid */ - pk_oid_str_to_num(curve->OID, key->dp.oid, &key->dp.oidlen); - } -} - -int ecc_copy_dp(const ecc_key *srckey, ecc_key *key) -{ - unsigned long i; - int err; - - LTC_ARGCHK(key != NULL); - LTC_ARGCHK(srckey != NULL); - - if ((err = mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, - &key->dp.base.x, &key->dp.base.y, &key->dp.base.z, - &key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, - NULL)) != CRYPT_OK) { - return err; - } - - /* A, B, order, prime, Gx, Gy */ - if ((err = mp_copy(srckey->dp.prime, key->dp.prime )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(srckey->dp.order, key->dp.order )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(srckey->dp.A, key->dp.A )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(srckey->dp.B, key->dp.B )) != CRYPT_OK) { goto error; } - if ((err = ltc_ecc_copy_point(&srckey->dp.base, &key->dp.base)) != CRYPT_OK) { goto error; } - /* cofactor & size */ - key->dp.cofactor = srckey->dp.cofactor; - key->dp.size = srckey->dp.size; - /* OID */ - if (srckey->dp.oidlen > 0) { - key->dp.oidlen = srckey->dp.oidlen; - for (i = 0; i < key->dp.oidlen; i++) key->dp.oid[i] = srckey->dp.oid[i]; - } - else { - _ecc_oid_lookup(key); /* try to find OID in ltc_ecc_curves */ - } - /* success */ - return CRYPT_OK; - -error: - ecc_free(key); - return err; -} - -int ecc_set_dp_from_mpis(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key) -{ - int err; - - LTC_ARGCHK(key != NULL); - LTC_ARGCHK(a != NULL); - LTC_ARGCHK(b != NULL); - LTC_ARGCHK(prime != NULL); - LTC_ARGCHK(order != NULL); - LTC_ARGCHK(gx != NULL); - LTC_ARGCHK(gy != NULL); - - if ((err = mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, - &key->dp.base.x, &key->dp.base.y, &key->dp.base.z, - &key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, - NULL)) != CRYPT_OK) { - return err; - } - - /* A, B, order, prime, Gx, Gy */ - if ((err = mp_copy(prime, key->dp.prime )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(order, key->dp.order )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(a, key->dp.A )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(b, key->dp.B )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(gx, key->dp.base.x)) != CRYPT_OK) { goto error; } - if ((err = mp_copy(gy, key->dp.base.y)) != CRYPT_OK) { goto error; } - if ((err = mp_set(key->dp.base.z, 1)) != CRYPT_OK) { goto error; } - /* cofactor & size */ - key->dp.cofactor = cofactor; - key->dp.size = mp_unsigned_bin_size(prime); - /* try to find OID in ltc_ecc_curves */ - _ecc_oid_lookup(key); - /* success */ - return CRYPT_OK; - -error: - ecc_free(key); - return err; -} - -#endif - -/* ref: $Format:%D$ */ -/* git commit: $Format:%H$ */ -/* commit time: $Format:%ai$ */ diff --git a/src/ltc/pk/ecc/ecc_sign_hash.c b/src/ltc/pk/ecc/ecc_sign_hash.c index bad5286..3302b07 100644 --- a/src/ltc/pk/ecc/ecc_sign_hash.c +++ b/src/ltc/pk/ecc/ecc_sign_hash.c @@ -63,7 +63,7 @@ /* make up a key and export the public copy */ do { - if ((err = ecc_copy_dp(key, &pubkey)) != CRYPT_OK) { goto errnokey; } + if ((err = ecc_copy_curve(key, &pubkey)) != CRYPT_OK) { goto errnokey; } if ((err = ecc_generate_key(prng, wprng, &pubkey)) != CRYPT_OK) { goto errnokey; } /* find r = x1 mod n */ diff --git a/src/ltc/pk/ecc/ltc_ecc_is_point_at_infinity.c b/src/ltc/pk/ecc/ltc_ecc_is_point_at_infinity.c index 85bd47a..146f9f4 100644 --- a/src/ltc/pk/ecc/ltc_ecc_is_point_at_infinity.c +++ b/src/ltc/pk/ecc/ltc_ecc_is_point_at_infinity.c @@ -44,10 +44,11 @@ /* test y^2 == x^3 */ err = CRYPT_OK; - if ((mp_cmp(x3, y2) == LTC_MP_EQ) && !mp_iszero(y2)) + if ((mp_cmp(x3, y2) == LTC_MP_EQ) && !mp_iszero(y2)) { *retval = 1; - else + } else { *retval = 0; + } cleanup: mp_clear_multi(x3, y2, NULL); diff --git a/src/ltc/pk/rsa/rsa_export.c b/src/ltc/pk/rsa/rsa_export.c index 6bd2199..43ff7c3 100644 --- a/src/ltc/pk/rsa/rsa_export.c +++ b/src/ltc/pk/rsa/rsa_export.c @@ -26,14 +26,16 @@ int rsa_export(unsigned char *out, unsigned long *outlen, int type, const rsa_key *key) { unsigned long zero=0; - int err; + int err, std; LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); - /* type valid? */ - if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) { - return CRYPT_PK_INVALID_TYPE; + std = type & PK_STD; + type &= ~PK_STD; + + if (type == PK_PRIVATE && key->type != PK_PRIVATE) { + return CRYPT_PK_TYPE_MISMATCH; } if (type == PK_PRIVATE) { @@ -52,12 +54,14 @@ LTC_ASN1_INTEGER, 1UL, key->dQ, LTC_ASN1_INTEGER, 1UL, key->qP, LTC_ASN1_EOL, 0UL, NULL); - } else { + } + + if (type == PK_PUBLIC) { /* public key */ unsigned long tmplen, *ptmplen; unsigned char* tmp = NULL; - if (type & PK_STD) { + if (std) { tmplen = (unsigned long)(mp_count_bits(key->N) / 8) * 2 + 8; tmp = XMALLOC(tmplen); ptmplen = &tmplen; @@ -75,7 +79,7 @@ LTC_ASN1_INTEGER, 1UL, key->e, LTC_ASN1_EOL, 0UL, NULL); - if ((err != CRYPT_OK) || !(type & PK_STD)) { + if ((err != CRYPT_OK) || !std) { goto finish; } @@ -83,11 +87,11 @@ PKA_RSA, tmp, tmplen, LTC_ASN1_NULL, NULL, 0); finish: - if (tmp != out) - XFREE(tmp); + if (tmp != out) XFREE(tmp); return err; + } - } + return CRYPT_INVALID_ARG; } #endif /* LTC_MRSA */ diff --git a/src/ltc/pk/rsa/rsa_import.c b/src/ltc/pk/rsa/rsa_import.c index bb8793d..69f5b23 100644 --- a/src/ltc/pk/rsa/rsa_import.c +++ b/src/ltc/pk/rsa/rsa_import.c @@ -116,9 +116,9 @@ mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, NULL); LBL_FREE: - if (tmpbuf != NULL) - XFREE(tmpbuf); - + if (tmpbuf != NULL) { + XFREE(tmpbuf); + } return err; } diff --git a/src/ltc/prngs/fortuna.c b/src/ltc/prngs/fortuna.c index ab56def..908f6a5 100644 --- a/src/ltc/prngs/fortuna.c +++ b/src/ltc/prngs/fortuna.c @@ -108,11 +108,13 @@ #ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED ulong64 now = _fortuna_current_time(); - if (now == prng->u.fortuna.wd) + if (now == prng->u.fortuna.wd) { return CRYPT_OK; + } #else - if (++prng->u.fortuna.wd < LTC_FORTUNA_WD) + if (++prng->u.fortuna.wd < LTC_FORTUNA_WD) { return CRYPT_OK; + } #endif /* new K == LTC_SHA256(K || s) where s == LTC_SHA256(P0) || LTC_SHA256(P1) ... */ diff --git a/src/ltc/prngs/rng_get_bytes.c b/src/ltc/prngs/rng_get_bytes.c index 10b3cad..1a32f33 100644 --- a/src/ltc/prngs/rng_get_bytes.c +++ b/src/ltc/prngs/rng_get_bytes.c @@ -30,9 +30,12 @@ LTC_UNUSED_PARAM(callback); #ifdef LTC_TRY_URANDOM_FIRST f = fopen("/dev/urandom", "rb"); - if (f == NULL) + if (f == NULL) { + f = fopen("/dev/random", "rb"); + } +#else + f = fopen("/dev/random", "rb"); #endif /* LTC_TRY_URANDOM_FIRST */ - f = fopen("/dev/random", "rb"); if (f == NULL) { return 0; diff --git a/src/ltc/prngs/sprng.c b/src/ltc/prngs/sprng.c index a587606..085e43a 100644 --- a/src/ltc/prngs/sprng.c +++ b/src/ltc/prngs/sprng.c @@ -102,6 +102,7 @@ @param prng The PRNG to export @return CRYPT_OK if successful */ +/* NOLINTNEXTLINE(readability-non-const-parameter) - silence clang-tidy warning */ int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng) { LTC_ARGCHK(outlen != NULL); diff --git a/src/ltc/stream/rabbit/rabbit.c b/src/ltc/stream/rabbit/rabbit.c index 4607bc9..7314d32 100644 --- a/src/ltc/stream/rabbit/rabbit.c +++ b/src/ltc/stream/rabbit/rabbit.c @@ -99,8 +99,9 @@ ulong32 g[8], c_old[8], i; /* Save old counter values */ - for (i=0; i<8; i++) + for (i=0; i<8; i++) { c_old[i] = p_instance->c[i]; + } /* Calculate new counter values */ p_instance->c[0] = (ulong32)(p_instance->c[0] + 0x4D34D34D + p_instance->carry); @@ -114,8 +115,9 @@ p_instance->carry = (p_instance->c[7] < c_old[7]); /* Calculate the g-values */ - for (i=0;i<8;i++) + for (i=0;i<8;i++) { g[i] = _rabbit_g_func((ulong32)(p_instance->x[i] + p_instance->c[i])); + } /* Calculate new state values */ p_instance->x[0] = (ulong32)(g[0] + ROLc(g[7],16) + ROLc(g[6], 16)); @@ -198,12 +200,14 @@ st->master_ctx.carry = 0; /* Iterate the master context four times */ - for (i=0; i<4; i++) + for (i=0; i<4; i++) { _rabbit_next_state(&(st->master_ctx)); + } /* Modify the counters */ - for (i=0; i<8; i++) + for (i=0; i<8; i++) { st->master_ctx.c[i] ^= st->master_ctx.x[(i+4)&0x7]; + } /* Copy master instance to work instance */ for (i=0; i<8; i++) { @@ -250,13 +254,15 @@ st->work_ctx.c[7] = st->master_ctx.c[7] ^ i3; /* Copy state variables */ - for (i=0; i<8; i++) + for (i=0; i<8; i++) { st->work_ctx.x[i] = st->master_ctx.x[i]; + } st->work_ctx.carry = st->master_ctx.carry; /* Iterate the work context four times */ - for (i=0; i<4; i++) + for (i=0; i<4; i++) { _rabbit_next_state(&(st->work_ctx)); + } /* reset keystream buffer and unused count */ XMEMSET(&(st->block), 0, sizeof(st->block)); @@ -297,13 +303,12 @@ /* copy remainder to block */ for (i = inlen; i < 16; ++i) st->block[i] = buf[i]; return CRYPT_OK; - } else { - /* XOR entire buf and send to out */ - for (i = 0; i < 16; ++i) out[i] = in[i] ^ buf[i]; - inlen -= 16; - out += 16; - in += 16; } + /* XOR entire buf and send to out */ + for (i = 0; i < 16; ++i) out[i] = in[i] ^ buf[i]; + inlen -= 16; + out += 16; + in += 16; } } diff --git a/src/ltc/stream/sober128/sober128_stream.c b/src/ltc/stream/sober128/sober128_stream.c index 44e0c32..ecbe4d8 100644 --- a/src/ltc/stream/sober128/sober128_stream.c +++ b/src/ltc/stream/sober128/sober128_stream.c @@ -68,68 +68,68 @@ /* Return a non-linear function of some parts of the register. */ -#define NLFUNC(c,z) \ +#define NLFUNC(st,z) \ { \ - t = c->R[OFF(z,0)] + c->R[OFF(z,16)]; \ + t = st->R[OFF(z,0)] + st->R[OFF(z,16)]; \ t ^= Sbox[(t >> 24) & 0xFF]; \ t = RORc(t, 8); \ - t = ((t + c->R[OFF(z,1)]) ^ c->konst) + c->R[OFF(z,6)]; \ + t = ((t + st->R[OFF(z,1)]) ^ st->konst) + st->R[OFF(z,6)]; \ t ^= Sbox[(t >> 24) & 0xFF]; \ - t = t + c->R[OFF(z,13)]; \ -} - -static ulong32 nltap(const sober128_state *c) + t = t + st->R[OFF(z,13)]; \ +} + +static ulong32 nltap(const sober128_state *st) { ulong32 t; - NLFUNC(c, 0); + NLFUNC(st, 0); return t; } /* Save the current register state */ -static void s128_savestate(sober128_state *c) +static void s128_savestate(sober128_state *st) { int i; for (i = 0; i < N; ++i) { - c->initR[i] = c->R[i]; + st->initR[i] = st->R[i]; } } /* initialise to previously saved register state */ -static void s128_reloadstate(sober128_state *c) +static void s128_reloadstate(sober128_state *st) { int i; for (i = 0; i < N; ++i) { - c->R[i] = c->initR[i]; + st->R[i] = st->initR[i]; } } /* Initialise "konst" */ -static void s128_genkonst(sober128_state *c) +static void s128_genkonst(sober128_state *st) { ulong32 newkonst; do { - cycle(c->R); - newkonst = nltap(c); + cycle(st->R); + newkonst = nltap(st); } while ((newkonst & 0xFF000000) == 0); - c->konst = newkonst; + st->konst = newkonst; } /* Load key material into the register */ #define ADDKEY(k) \ - c->R[KEYP] += (k); + st->R[KEYP] += (k); #define XORNL(nl) \ - c->R[FOLDP] ^= (nl); + st->R[FOLDP] ^= (nl); /* nonlinear diffusion of register for key */ -#define DROUND(z) STEP(c->R,z); NLFUNC(c,(z+1)); c->R[OFF((z+1),FOLDP)] ^= t; -static void s128_diffuse(sober128_state *c) +#define DROUND(z) STEP(st->R,z); NLFUNC(st,(z+1)); st->R[OFF((z+1),FOLDP)] ^= t; +static void s128_diffuse(sober128_state *st) { ulong32 t; /* relies on FOLD == N == 17! */ @@ -154,16 +154,16 @@ /** Initialize an Sober128 context (only the key) - @param c [out] The destination of the Sober128 state + @param st [out] The destination of the Sober128 state @param key The secret key @param keylen The length of the secret key (octets) @return CRYPT_OK if successful */ -int sober128_stream_setup(sober128_state *c, const unsigned char *key, unsigned long keylen) +int sober128_stream_setup(sober128_state *st, const unsigned char *key, unsigned long keylen) { ulong32 i, k; - LTC_ARGCHK(c != NULL); + LTC_ARGCHK(st != NULL); LTC_ARGCHK(key != NULL); LTC_ARGCHK(keylen > 0); @@ -173,49 +173,49 @@ } /* Register initialised to Fibonacci numbers */ - c->R[0] = 1; - c->R[1] = 1; + st->R[0] = 1; + st->R[1] = 1; for (i = 2; i < N; ++i) { - c->R[i] = c->R[i-1] + c->R[i-2]; - } - c->konst = INITKONST; + st->R[i] = st->R[i-1] + st->R[i-2]; + } + st->konst = INITKONST; for (i = 0; i < keylen; i += 4) { k = BYTE2WORD((unsigned char *)&key[i]); ADDKEY(k); - cycle(c->R); - XORNL(nltap(c)); + cycle(st->R); + XORNL(nltap(st)); } /* also fold in the length of the key */ ADDKEY(keylen); /* now diffuse */ - s128_diffuse(c); - s128_genkonst(c); - s128_savestate(c); - c->nbuf = 0; + s128_diffuse(st); + s128_genkonst(st); + s128_savestate(st); + st->nbuf = 0; return CRYPT_OK; } /** Set IV to the Sober128 state - @param c The Sober12820 state + @param st The Sober12820 state @param iv The IV data to add @param ivlen The length of the IV (must be 12) @return CRYPT_OK on success */ -int sober128_stream_setiv(sober128_state *c, const unsigned char *iv, unsigned long ivlen) +int sober128_stream_setiv(sober128_state *st, const unsigned char *iv, unsigned long ivlen) { ulong32 i, k; - LTC_ARGCHK(c != NULL); + LTC_ARGCHK(st != NULL); LTC_ARGCHK(iv != NULL); LTC_ARGCHK(ivlen > 0); /* ok we are adding an IV then... */ - s128_reloadstate(c); + s128_reloadstate(st); /* ivlen must be multiple of 4 bytes */ if ((ivlen & 3) != 0) { @@ -225,45 +225,45 @@ for (i = 0; i < ivlen; i += 4) { k = BYTE2WORD((unsigned char *)&iv[i]); ADDKEY(k); - cycle(c->R); - XORNL(nltap(c)); + cycle(st->R); + XORNL(nltap(st)); } /* also fold in the length of the key */ ADDKEY(ivlen); /* now diffuse */ - s128_diffuse(c); - c->nbuf = 0; + s128_diffuse(st); + st->nbuf = 0; return CRYPT_OK; } /* XOR pseudo-random bytes into buffer */ -#define SROUND(z) STEP(c->R,z); NLFUNC(c,(z+1)); XORWORD(t, in+(z*4), out+(z*4)); +#define SROUND(z) STEP(st->R,z); NLFUNC(st,(z+1)); XORWORD(t, in+(z*4), out+(z*4)); /** Encrypt (or decrypt) bytes of ciphertext (or plaintext) with Sober128 - @param c The Sober128 state + @param st The Sober128 state @param in The plaintext (or ciphertext) @param inlen The length of the input (octets) @param out [out] The ciphertext (or plaintext), length inlen @return CRYPT_OK if successful */ -int sober128_stream_crypt(sober128_state *c, const unsigned char *in, unsigned long inlen, unsigned char *out) +int sober128_stream_crypt(sober128_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out) { ulong32 t; if (inlen == 0) return CRYPT_OK; /* nothing to do */ LTC_ARGCHK(out != NULL); - LTC_ARGCHK(c != NULL); + LTC_ARGCHK(st != NULL); /* handle any previously buffered bytes */ - while (c->nbuf != 0 && inlen != 0) { - *out++ = *in++ ^ (unsigned char)(c->sbuf & 0xFF); - c->sbuf >>= 8; - c->nbuf -= 8; + while (st->nbuf != 0 && inlen != 0) { + *out++ = *in++ ^ (unsigned char)(st->sbuf & 0xFF); + st->sbuf >>= 8; + st->nbuf -= 8; --inlen; } @@ -295,8 +295,8 @@ /* do small or odd size buffers the slow way */ while (4 <= inlen) { - cycle(c->R); - t = nltap(c); + cycle(st->R); + t = nltap(st); XORWORD(t, in, out); out += 4; in += 4; @@ -305,13 +305,13 @@ /* handle any trailing bytes */ if (inlen != 0) { - cycle(c->R); - c->sbuf = nltap(c); - c->nbuf = 32; - while (c->nbuf != 0 && inlen != 0) { - *out++ = *in++ ^ (unsigned char)(c->sbuf & 0xFF); - c->sbuf >>= 8; - c->nbuf -= 8; + cycle(st->R); + st->sbuf = nltap(st); + st->nbuf = 32; + while (st->nbuf != 0 && inlen != 0) { + *out++ = *in++ ^ (unsigned char)(st->sbuf & 0xFF); + st->sbuf >>= 8; + st->nbuf -= 8; --inlen; } } @@ -319,23 +319,23 @@ return CRYPT_OK; } -int sober128_stream_keystream(sober128_state *c, unsigned char *out, unsigned long outlen) +int sober128_stream_keystream(sober128_state *st, unsigned char *out, unsigned long outlen) { if (outlen == 0) return CRYPT_OK; /* nothing to do */ LTC_ARGCHK(out != NULL); XMEMSET(out, 0, outlen); - return sober128_stream_crypt(c, out, outlen, out); + return sober128_stream_crypt(st, out, outlen, out); } /** Terminate and clear Sober128 state - @param c The Sober128 state + @param st The Sober128 state @return CRYPT_OK on success */ -int sober128_stream_done(sober128_state *c) -{ - LTC_ARGCHK(c != NULL); - XMEMSET(c, 0, sizeof(sober128_state)); +int sober128_stream_done(sober128_state *st) +{ + LTC_ARGCHK(st != NULL); + XMEMSET(st, 0, sizeof(sober128_state)); return CRYPT_OK; } diff --git a/src/ltc/stream/sosemanuk/sosemanuk.c b/src/ltc/stream/sosemanuk/sosemanuk.c index b0838f8..4137198 100644 --- a/src/ltc/stream/sosemanuk/sosemanuk.c +++ b/src/ltc/stream/sosemanuk/sosemanuk.c @@ -266,8 +266,9 @@ XMEMCPY(wbuf, key, keylen); if (keylen < 32) { wbuf[keylen] = 0x01; - if (keylen < 31) + if (keylen < 31) { XMEMSET(wbuf + keylen + 1, 0, 31 - keylen); + } } LOAD32L(w0, wbuf); @@ -725,8 +726,9 @@ static LTC_INLINE void _xorbuf(const unsigned char *in1, const unsigned char *in2, unsigned char *out, unsigned long datalen) { - while (datalen -- > 0) + while (datalen -- > 0) { *out ++ = *in1 ++ ^ *in2 ++; + } } @@ -751,8 +753,9 @@ if (ss->ptr < (sizeof(ss->buf))) { unsigned long rlen = (sizeof(ss->buf)) - ss->ptr; - if (rlen > inlen) + if (rlen > inlen) { rlen = inlen; + } _xorbuf(ss->buf + ss->ptr, in, out, rlen); in += rlen; out += rlen;