├── .github └── workflows │ └── ci.yml ├── .gitignore ├── Examples └── PackageTraits │ ├── MiddlewareClientWithSQLCipher │ ├── .gitignore │ ├── Package.swift │ ├── Sources │ │ └── MiddlewareClientWithSQLCipher │ │ │ └── MiddlewareClientWithSQLCipher.swift │ └── Tests │ │ └── MiddlewareClientWithSQLCipherTests │ │ └── MiddlewareClientWithSQLCipherTests.swift │ ├── MiddlewareClientWithoutSQLCipher │ ├── .gitignore │ ├── Package.swift │ ├── Sources │ │ └── MiddlewareClientWithoutSQLCipher │ │ │ └── MiddlewareClientWithoutSQLCipher.swift │ └── Tests │ │ └── MiddlewareClientWithoutSQLCipherTests │ │ └── MiddlewareClientWithoutSQLCipherTests.swift │ ├── README.md │ └── SQLMiddleware │ ├── .gitignore │ ├── Package.swift │ └── Sources │ └── SQLMiddleware │ └── SQLMiddleware.swift ├── LICENSE-SQLCIPHER.txt ├── LICENSE-SQLITE.txt ├── LICENSE.txt ├── Package.swift ├── README.md ├── Sources ├── SQLCipher │ ├── libtomcrypt │ │ ├── ciphers │ │ │ ├── aes │ │ │ │ ├── aes.c │ │ │ │ ├── aes_desc.c │ │ │ │ ├── aes_tab.c │ │ │ │ └── aesni.c │ │ │ ├── anubis.c │ │ │ ├── blowfish.c │ │ │ ├── camellia.c │ │ │ ├── cast5.c │ │ │ ├── des.c │ │ │ ├── idea.c │ │ │ ├── kasumi.c │ │ │ ├── khazad.c │ │ │ ├── kseed.c │ │ │ ├── multi2.c │ │ │ ├── noekeon.c │ │ │ ├── rc2.c │ │ │ ├── rc5.c │ │ │ ├── rc6.c │ │ │ ├── safer │ │ │ │ ├── safer.c │ │ │ │ ├── safer_tab.c │ │ │ │ └── saferp.c │ │ │ ├── serpent.c │ │ │ ├── skipjack.c │ │ │ ├── tea.c │ │ │ ├── twofish │ │ │ │ ├── twofish.c │ │ │ │ └── twofish_tab.c │ │ │ └── xtea.c │ │ ├── encauth │ │ │ ├── ccm │ │ │ │ ├── ccm_add_aad.c │ │ │ │ ├── ccm_add_nonce.c │ │ │ │ ├── ccm_done.c │ │ │ │ ├── ccm_init.c │ │ │ │ ├── ccm_memory.c │ │ │ │ ├── ccm_process.c │ │ │ │ ├── ccm_reset.c │ │ │ │ └── ccm_test.c │ │ │ ├── chachapoly │ │ │ │ ├── chacha20poly1305_add_aad.c │ │ │ │ ├── chacha20poly1305_decrypt.c │ │ │ │ ├── chacha20poly1305_done.c │ │ │ │ ├── chacha20poly1305_encrypt.c │ │ │ │ ├── chacha20poly1305_init.c │ │ │ │ ├── chacha20poly1305_memory.c │ │ │ │ ├── chacha20poly1305_setiv.c │ │ │ │ ├── chacha20poly1305_setiv_rfc7905.c │ │ │ │ └── chacha20poly1305_test.c │ │ │ ├── eax │ │ │ │ ├── eax_addheader.c │ │ │ │ ├── eax_decrypt.c │ │ │ │ ├── eax_decrypt_verify_memory.c │ │ │ │ ├── eax_done.c │ │ │ │ ├── eax_encrypt.c │ │ │ │ ├── eax_encrypt_authenticate_memory.c │ │ │ │ ├── eax_init.c │ │ │ │ └── eax_test.c │ │ │ ├── gcm │ │ │ │ ├── gcm_add_aad.c │ │ │ │ ├── gcm_add_iv.c │ │ │ │ ├── gcm_done.c │ │ │ │ ├── gcm_gf_mult.c │ │ │ │ ├── gcm_init.c │ │ │ │ ├── gcm_memory.c │ │ │ │ ├── gcm_mult_h.c │ │ │ │ ├── gcm_process.c │ │ │ │ ├── gcm_reset.c │ │ │ │ └── gcm_test.c │ │ │ ├── ocb │ │ │ │ ├── ocb_decrypt.c │ │ │ │ ├── ocb_decrypt_verify_memory.c │ │ │ │ ├── ocb_done_decrypt.c │ │ │ │ ├── ocb_done_encrypt.c │ │ │ │ ├── ocb_encrypt.c │ │ │ │ ├── ocb_encrypt_authenticate_memory.c │ │ │ │ ├── ocb_init.c │ │ │ │ ├── ocb_ntz.c │ │ │ │ ├── ocb_shift_xor.c │ │ │ │ ├── ocb_test.c │ │ │ │ └── s_ocb_done.c │ │ │ └── ocb3 │ │ │ │ ├── ocb3_add_aad.c │ │ │ │ ├── ocb3_decrypt.c │ │ │ │ ├── ocb3_decrypt_last.c │ │ │ │ ├── ocb3_decrypt_verify_memory.c │ │ │ │ ├── ocb3_done.c │ │ │ │ ├── ocb3_encrypt.c │ │ │ │ ├── ocb3_encrypt_authenticate_memory.c │ │ │ │ ├── ocb3_encrypt_last.c │ │ │ │ ├── ocb3_init.c │ │ │ │ ├── ocb3_int_ntz.c │ │ │ │ ├── ocb3_int_xor_blocks.c │ │ │ │ └── ocb3_test.c │ │ ├── hashes │ │ │ ├── blake2b.c │ │ │ ├── blake2s.c │ │ │ ├── chc │ │ │ │ └── chc.c │ │ │ ├── helper │ │ │ │ ├── hash_file.c │ │ │ │ ├── hash_filehandle.c │ │ │ │ ├── hash_memory.c │ │ │ │ └── hash_memory_multi.c │ │ │ ├── md2.c │ │ │ ├── md4.c │ │ │ ├── md5.c │ │ │ ├── rmd128.c │ │ │ ├── rmd160.c │ │ │ ├── rmd256.c │ │ │ ├── rmd320.c │ │ │ ├── sha1.c │ │ │ ├── sha2 │ │ │ │ ├── sha224.c │ │ │ │ ├── sha256.c │ │ │ │ ├── sha384.c │ │ │ │ ├── sha512.c │ │ │ │ ├── sha512_224.c │ │ │ │ └── sha512_256.c │ │ │ ├── sha3.c │ │ │ ├── sha3_test.c │ │ │ ├── tiger.c │ │ │ └── whirl │ │ │ │ ├── whirl.c │ │ │ │ └── whirltab.c │ │ ├── headers │ │ │ ├── tomcrypt.h │ │ │ ├── tomcrypt_argchk.h │ │ │ ├── tomcrypt_cfg.h │ │ │ ├── tomcrypt_cipher.h │ │ │ ├── tomcrypt_custom.h │ │ │ ├── tomcrypt_hash.h │ │ │ ├── tomcrypt_mac.h │ │ │ ├── tomcrypt_macros.h │ │ │ ├── tomcrypt_math.h │ │ │ ├── tomcrypt_misc.h │ │ │ ├── tomcrypt_pk.h │ │ │ ├── tomcrypt_pkcs.h │ │ │ ├── tomcrypt_private.h │ │ │ └── tomcrypt_prng.h │ │ ├── mac │ │ │ ├── blake2 │ │ │ │ ├── blake2bmac.c │ │ │ │ ├── blake2bmac_file.c │ │ │ │ ├── blake2bmac_memory.c │ │ │ │ ├── blake2bmac_memory_multi.c │ │ │ │ ├── blake2bmac_test.c │ │ │ │ ├── blake2smac.c │ │ │ │ ├── blake2smac_file.c │ │ │ │ ├── blake2smac_memory.c │ │ │ │ ├── blake2smac_memory_multi.c │ │ │ │ └── blake2smac_test.c │ │ │ ├── f9 │ │ │ │ ├── f9_done.c │ │ │ │ ├── f9_file.c │ │ │ │ ├── f9_init.c │ │ │ │ ├── f9_memory.c │ │ │ │ ├── f9_memory_multi.c │ │ │ │ ├── f9_process.c │ │ │ │ └── f9_test.c │ │ │ ├── hmac │ │ │ │ ├── hmac_done.c │ │ │ │ ├── hmac_file.c │ │ │ │ ├── hmac_init.c │ │ │ │ ├── hmac_memory.c │ │ │ │ ├── hmac_memory_multi.c │ │ │ │ ├── hmac_process.c │ │ │ │ └── hmac_test.c │ │ │ ├── omac │ │ │ │ ├── omac_done.c │ │ │ │ ├── omac_file.c │ │ │ │ ├── omac_init.c │ │ │ │ ├── omac_memory.c │ │ │ │ ├── omac_memory_multi.c │ │ │ │ ├── omac_process.c │ │ │ │ └── omac_test.c │ │ │ ├── pelican │ │ │ │ ├── pelican.c │ │ │ │ ├── pelican_memory.c │ │ │ │ └── pelican_test.c │ │ │ ├── pmac │ │ │ │ ├── pmac_done.c │ │ │ │ ├── pmac_file.c │ │ │ │ ├── pmac_init.c │ │ │ │ ├── pmac_memory.c │ │ │ │ ├── pmac_memory_multi.c │ │ │ │ ├── pmac_ntz.c │ │ │ │ ├── pmac_process.c │ │ │ │ ├── pmac_shift_xor.c │ │ │ │ └── pmac_test.c │ │ │ ├── poly1305 │ │ │ │ ├── poly1305.c │ │ │ │ ├── poly1305_file.c │ │ │ │ ├── poly1305_memory.c │ │ │ │ ├── poly1305_memory_multi.c │ │ │ │ └── poly1305_test.c │ │ │ └── xcbc │ │ │ │ ├── xcbc_done.c │ │ │ │ ├── xcbc_file.c │ │ │ │ ├── xcbc_init.c │ │ │ │ ├── xcbc_memory.c │ │ │ │ ├── xcbc_memory_multi.c │ │ │ │ ├── xcbc_process.c │ │ │ │ └── xcbc_test.c │ │ ├── math │ │ │ ├── fp │ │ │ │ └── ltc_ecc_fp_mulmod.c │ │ │ ├── gmp_desc.c │ │ │ ├── ltm_desc.c │ │ │ ├── multi.c │ │ │ ├── radix_to_bin.c │ │ │ ├── rand_bn.c │ │ │ ├── rand_prime.c │ │ │ └── tfm_desc.c │ │ ├── misc │ │ │ ├── adler32.c │ │ │ ├── base16 │ │ │ │ ├── base16_decode.c │ │ │ │ └── base16_encode.c │ │ │ ├── base32 │ │ │ │ ├── base32_decode.c │ │ │ │ └── base32_encode.c │ │ │ ├── base64 │ │ │ │ ├── base64_decode.c │ │ │ │ └── base64_encode.c │ │ │ ├── bcrypt │ │ │ │ └── bcrypt.c │ │ │ ├── burn_stack.c │ │ │ ├── compare_testvector.c │ │ │ ├── copy_or_zeromem.c │ │ │ ├── crc32.c │ │ │ ├── crypt │ │ │ │ ├── crypt.c │ │ │ │ ├── crypt_argchk.c │ │ │ │ ├── crypt_cipher_descriptor.c │ │ │ │ ├── crypt_cipher_is_valid.c │ │ │ │ ├── crypt_constants.c │ │ │ │ ├── crypt_find_cipher.c │ │ │ │ ├── crypt_find_cipher_any.c │ │ │ │ ├── crypt_find_cipher_id.c │ │ │ │ ├── crypt_find_hash.c │ │ │ │ ├── crypt_find_hash_any.c │ │ │ │ ├── crypt_find_hash_id.c │ │ │ │ ├── crypt_find_hash_oid.c │ │ │ │ ├── crypt_find_prng.c │ │ │ │ ├── crypt_fsa.c │ │ │ │ ├── crypt_hash_descriptor.c │ │ │ │ ├── crypt_hash_is_valid.c │ │ │ │ ├── crypt_inits.c │ │ │ │ ├── crypt_ltc_mp_descriptor.c │ │ │ │ ├── crypt_prng_descriptor.c │ │ │ │ ├── crypt_prng_is_valid.c │ │ │ │ ├── crypt_prng_rng_descriptor.c │ │ │ │ ├── crypt_register_all_ciphers.c │ │ │ │ ├── crypt_register_all_hashes.c │ │ │ │ ├── crypt_register_all_prngs.c │ │ │ │ ├── crypt_register_cipher.c │ │ │ │ ├── crypt_register_hash.c │ │ │ │ ├── crypt_register_prng.c │ │ │ │ ├── crypt_sizes.c │ │ │ │ ├── crypt_unregister_cipher.c │ │ │ │ ├── crypt_unregister_hash.c │ │ │ │ └── crypt_unregister_prng.c │ │ │ ├── error_to_string.c │ │ │ ├── hkdf │ │ │ │ ├── hkdf.c │ │ │ │ └── hkdf_test.c │ │ │ ├── mem_neq.c │ │ │ ├── padding │ │ │ │ ├── padding_depad.c │ │ │ │ └── padding_pad.c │ │ │ ├── pbes │ │ │ │ ├── pbes.c │ │ │ │ ├── pbes1.c │ │ │ │ └── pbes2.c │ │ │ ├── pkcs12 │ │ │ │ ├── pkcs12_kdf.c │ │ │ │ └── pkcs12_utf8_to_utf16.c │ │ │ ├── pkcs5 │ │ │ │ ├── pkcs_5_1.c │ │ │ │ ├── pkcs_5_2.c │ │ │ │ └── pkcs_5_test.c │ │ │ ├── ssh │ │ │ │ ├── ssh_decode_sequence_multi.c │ │ │ │ └── ssh_encode_sequence_multi.c │ │ │ └── zeromem.c │ │ ├── modes │ │ │ ├── cbc │ │ │ │ ├── cbc_decrypt.c │ │ │ │ ├── cbc_done.c │ │ │ │ ├── cbc_encrypt.c │ │ │ │ ├── cbc_getiv.c │ │ │ │ ├── cbc_setiv.c │ │ │ │ └── cbc_start.c │ │ │ ├── cfb │ │ │ │ ├── cfb_decrypt.c │ │ │ │ ├── cfb_done.c │ │ │ │ ├── cfb_encrypt.c │ │ │ │ ├── cfb_getiv.c │ │ │ │ ├── cfb_setiv.c │ │ │ │ └── cfb_start.c │ │ │ ├── ctr │ │ │ │ ├── ctr_decrypt.c │ │ │ │ ├── ctr_done.c │ │ │ │ ├── ctr_encrypt.c │ │ │ │ ├── ctr_getiv.c │ │ │ │ ├── ctr_setiv.c │ │ │ │ ├── ctr_start.c │ │ │ │ └── ctr_test.c │ │ │ ├── ecb │ │ │ │ ├── ecb_decrypt.c │ │ │ │ ├── ecb_done.c │ │ │ │ ├── ecb_encrypt.c │ │ │ │ └── ecb_start.c │ │ │ ├── f8 │ │ │ │ ├── f8_decrypt.c │ │ │ │ ├── f8_done.c │ │ │ │ ├── f8_encrypt.c │ │ │ │ ├── f8_getiv.c │ │ │ │ ├── f8_setiv.c │ │ │ │ ├── f8_start.c │ │ │ │ └── f8_test_mode.c │ │ │ ├── lrw │ │ │ │ ├── lrw_decrypt.c │ │ │ │ ├── lrw_done.c │ │ │ │ ├── lrw_encrypt.c │ │ │ │ ├── lrw_getiv.c │ │ │ │ ├── lrw_process.c │ │ │ │ ├── lrw_setiv.c │ │ │ │ ├── lrw_start.c │ │ │ │ └── lrw_test.c │ │ │ ├── ofb │ │ │ │ ├── ofb_decrypt.c │ │ │ │ ├── ofb_done.c │ │ │ │ ├── ofb_encrypt.c │ │ │ │ ├── ofb_getiv.c │ │ │ │ ├── ofb_setiv.c │ │ │ │ └── ofb_start.c │ │ │ └── xts │ │ │ │ ├── xts_decrypt.c │ │ │ │ ├── xts_done.c │ │ │ │ ├── xts_encrypt.c │ │ │ │ ├── xts_init.c │ │ │ │ ├── xts_mult_x.c │ │ │ │ └── xts_test.c │ │ ├── pk │ │ │ ├── asn1 │ │ │ │ ├── der │ │ │ │ │ ├── bit │ │ │ │ │ │ ├── der_decode_bit_string.c │ │ │ │ │ │ ├── der_decode_raw_bit_string.c │ │ │ │ │ │ ├── der_encode_bit_string.c │ │ │ │ │ │ ├── der_encode_raw_bit_string.c │ │ │ │ │ │ └── der_length_bit_string.c │ │ │ │ │ ├── boolean │ │ │ │ │ │ ├── der_decode_boolean.c │ │ │ │ │ │ ├── der_encode_boolean.c │ │ │ │ │ │ └── der_length_boolean.c │ │ │ │ │ ├── choice │ │ │ │ │ │ └── der_decode_choice.c │ │ │ │ │ ├── custom_type │ │ │ │ │ │ ├── der_decode_custom_type.c │ │ │ │ │ │ ├── der_encode_custom_type.c │ │ │ │ │ │ └── der_length_custom_type.c │ │ │ │ │ ├── general │ │ │ │ │ │ ├── der_asn1_maps.c │ │ │ │ │ │ ├── der_decode_asn1_identifier.c │ │ │ │ │ │ ├── der_decode_asn1_length.c │ │ │ │ │ │ ├── der_encode_asn1_identifier.c │ │ │ │ │ │ ├── der_encode_asn1_length.c │ │ │ │ │ │ ├── der_length_asn1_identifier.c │ │ │ │ │ │ └── der_length_asn1_length.c │ │ │ │ │ ├── generalizedtime │ │ │ │ │ │ ├── der_decode_generalizedtime.c │ │ │ │ │ │ ├── der_encode_generalizedtime.c │ │ │ │ │ │ └── der_length_generalizedtime.c │ │ │ │ │ ├── ia5 │ │ │ │ │ │ ├── der_decode_ia5_string.c │ │ │ │ │ │ ├── der_encode_ia5_string.c │ │ │ │ │ │ └── der_length_ia5_string.c │ │ │ │ │ ├── integer │ │ │ │ │ │ ├── der_decode_integer.c │ │ │ │ │ │ ├── der_encode_integer.c │ │ │ │ │ │ └── der_length_integer.c │ │ │ │ │ ├── object_identifier │ │ │ │ │ │ ├── der_decode_object_identifier.c │ │ │ │ │ │ ├── der_encode_object_identifier.c │ │ │ │ │ │ └── der_length_object_identifier.c │ │ │ │ │ ├── octet │ │ │ │ │ │ ├── der_decode_octet_string.c │ │ │ │ │ │ ├── der_encode_octet_string.c │ │ │ │ │ │ └── der_length_octet_string.c │ │ │ │ │ ├── printable_string │ │ │ │ │ │ ├── der_decode_printable_string.c │ │ │ │ │ │ ├── der_encode_printable_string.c │ │ │ │ │ │ └── der_length_printable_string.c │ │ │ │ │ ├── sequence │ │ │ │ │ │ ├── der_decode_sequence_ex.c │ │ │ │ │ │ ├── der_decode_sequence_flexi.c │ │ │ │ │ │ ├── der_decode_sequence_multi.c │ │ │ │ │ │ ├── der_encode_sequence_ex.c │ │ │ │ │ │ ├── der_encode_sequence_multi.c │ │ │ │ │ │ ├── der_length_sequence.c │ │ │ │ │ │ ├── der_sequence_free.c │ │ │ │ │ │ └── der_sequence_shrink.c │ │ │ │ │ ├── set │ │ │ │ │ │ ├── der_encode_set.c │ │ │ │ │ │ └── der_encode_setof.c │ │ │ │ │ ├── short_integer │ │ │ │ │ │ ├── der_decode_short_integer.c │ │ │ │ │ │ ├── der_encode_short_integer.c │ │ │ │ │ │ └── der_length_short_integer.c │ │ │ │ │ ├── teletex_string │ │ │ │ │ │ ├── der_decode_teletex_string.c │ │ │ │ │ │ └── der_length_teletex_string.c │ │ │ │ │ ├── utctime │ │ │ │ │ │ ├── der_decode_utctime.c │ │ │ │ │ │ ├── der_encode_utctime.c │ │ │ │ │ │ └── der_length_utctime.c │ │ │ │ │ └── utf8 │ │ │ │ │ │ ├── der_decode_utf8_string.c │ │ │ │ │ │ ├── der_encode_utf8_string.c │ │ │ │ │ │ └── der_length_utf8_string.c │ │ │ │ ├── oid │ │ │ │ │ ├── pk_get_oid.c │ │ │ │ │ ├── pk_oid_cmp.c │ │ │ │ │ └── pk_oid_str.c │ │ │ │ ├── pkcs8 │ │ │ │ │ └── pkcs8_decode_flexi.c │ │ │ │ └── x509 │ │ │ │ │ ├── x509_decode_public_key_from_certificate.c │ │ │ │ │ ├── x509_decode_subject_public_key_info.c │ │ │ │ │ └── x509_encode_subject_public_key_info.c │ │ │ ├── dh │ │ │ │ ├── dh.c │ │ │ │ ├── dh_check_pubkey.c │ │ │ │ ├── dh_export.c │ │ │ │ ├── dh_export_key.c │ │ │ │ ├── dh_free.c │ │ │ │ ├── dh_generate_key.c │ │ │ │ ├── dh_import.c │ │ │ │ ├── dh_set.c │ │ │ │ ├── dh_set_pg_dhparam.c │ │ │ │ └── dh_shared_secret.c │ │ │ ├── dsa │ │ │ │ ├── dsa_decrypt_key.c │ │ │ │ ├── dsa_encrypt_key.c │ │ │ │ ├── dsa_export.c │ │ │ │ ├── dsa_free.c │ │ │ │ ├── dsa_generate_key.c │ │ │ │ ├── dsa_generate_pqg.c │ │ │ │ ├── dsa_import.c │ │ │ │ ├── dsa_make_key.c │ │ │ │ ├── dsa_set.c │ │ │ │ ├── dsa_set_pqg_dsaparam.c │ │ │ │ ├── dsa_shared_secret.c │ │ │ │ ├── dsa_sign_hash.c │ │ │ │ ├── dsa_verify_hash.c │ │ │ │ └── dsa_verify_key.c │ │ │ ├── ec25519 │ │ │ │ ├── ec25519_crypto_ctx.c │ │ │ │ ├── ec25519_export.c │ │ │ │ ├── ec25519_import_pkcs8.c │ │ │ │ └── tweetnacl.c │ │ │ ├── ecc │ │ │ │ ├── ecc.c │ │ │ │ ├── ecc_ansi_x963_export.c │ │ │ │ ├── ecc_ansi_x963_import.c │ │ │ │ ├── ecc_decrypt_key.c │ │ │ │ ├── ecc_encrypt_key.c │ │ │ │ ├── ecc_export.c │ │ │ │ ├── ecc_export_openssl.c │ │ │ │ ├── ecc_find_curve.c │ │ │ │ ├── ecc_free.c │ │ │ │ ├── ecc_get_key.c │ │ │ │ ├── ecc_get_oid_str.c │ │ │ │ ├── ecc_get_size.c │ │ │ │ ├── ecc_import.c │ │ │ │ ├── ecc_import_openssl.c │ │ │ │ ├── ecc_import_pkcs8.c │ │ │ │ ├── ecc_import_x509.c │ │ │ │ ├── ecc_make_key.c │ │ │ │ ├── ecc_recover_key.c │ │ │ │ ├── ecc_set_curve.c │ │ │ │ ├── ecc_set_curve_internal.c │ │ │ │ ├── ecc_set_key.c │ │ │ │ ├── ecc_shared_secret.c │ │ │ │ ├── ecc_sign_hash.c │ │ │ │ ├── ecc_sizes.c │ │ │ │ ├── ecc_ssh_ecdsa_encode_name.c │ │ │ │ ├── ecc_verify_hash.c │ │ │ │ ├── ltc_ecc_export_point.c │ │ │ │ ├── ltc_ecc_import_point.c │ │ │ │ ├── ltc_ecc_is_point.c │ │ │ │ ├── ltc_ecc_is_point_at_infinity.c │ │ │ │ ├── ltc_ecc_map.c │ │ │ │ ├── ltc_ecc_mul2add.c │ │ │ │ ├── ltc_ecc_mulmod.c │ │ │ │ ├── ltc_ecc_mulmod_timing.c │ │ │ │ ├── ltc_ecc_points.c │ │ │ │ ├── ltc_ecc_projective_add_point.c │ │ │ │ ├── ltc_ecc_projective_dbl_point.c │ │ │ │ └── ltc_ecc_verify_key.c │ │ │ ├── ed25519 │ │ │ │ ├── ed25519_export.c │ │ │ │ ├── ed25519_import.c │ │ │ │ ├── ed25519_import_pkcs8.c │ │ │ │ ├── ed25519_import_raw.c │ │ │ │ ├── ed25519_import_x509.c │ │ │ │ ├── ed25519_make_key.c │ │ │ │ ├── ed25519_sign.c │ │ │ │ └── ed25519_verify.c │ │ │ ├── pkcs1 │ │ │ │ ├── pkcs_1_i2osp.c │ │ │ │ ├── pkcs_1_mgf1.c │ │ │ │ ├── pkcs_1_oaep_decode.c │ │ │ │ ├── pkcs_1_oaep_encode.c │ │ │ │ ├── pkcs_1_os2ip.c │ │ │ │ ├── pkcs_1_pss_decode.c │ │ │ │ ├── pkcs_1_pss_encode.c │ │ │ │ ├── pkcs_1_v1_5_decode.c │ │ │ │ └── pkcs_1_v1_5_encode.c │ │ │ ├── rsa │ │ │ │ ├── rsa_decrypt_key.c │ │ │ │ ├── rsa_encrypt_key.c │ │ │ │ ├── rsa_export.c │ │ │ │ ├── rsa_exptmod.c │ │ │ │ ├── rsa_get_size.c │ │ │ │ ├── rsa_import.c │ │ │ │ ├── rsa_import_pkcs8.c │ │ │ │ ├── rsa_import_x509.c │ │ │ │ ├── rsa_key.c │ │ │ │ ├── rsa_make_key.c │ │ │ │ ├── rsa_set.c │ │ │ │ ├── rsa_sign_hash.c │ │ │ │ ├── rsa_sign_saltlen_get.c │ │ │ │ └── rsa_verify_hash.c │ │ │ └── x25519 │ │ │ │ ├── x25519_export.c │ │ │ │ ├── x25519_import.c │ │ │ │ ├── x25519_import_pkcs8.c │ │ │ │ ├── x25519_import_raw.c │ │ │ │ ├── x25519_import_x509.c │ │ │ │ ├── x25519_make_key.c │ │ │ │ └── x25519_shared_secret.c │ │ ├── prngs │ │ │ ├── chacha20.c │ │ │ ├── fortuna.c │ │ │ ├── rc4.c │ │ │ ├── rng_get_bytes.c │ │ │ ├── rng_make_prng.c │ │ │ ├── sober128.c │ │ │ ├── sprng.c │ │ │ └── yarrow.c │ │ └── stream │ │ │ ├── chacha │ │ │ ├── chacha_crypt.c │ │ │ ├── chacha_done.c │ │ │ ├── chacha_ivctr32.c │ │ │ ├── chacha_ivctr64.c │ │ │ ├── chacha_keystream.c │ │ │ ├── chacha_memory.c │ │ │ ├── chacha_setup.c │ │ │ └── chacha_test.c │ │ │ ├── rabbit │ │ │ ├── rabbit.c │ │ │ └── rabbit_memory.c │ │ │ ├── rc4 │ │ │ ├── rc4_stream.c │ │ │ ├── rc4_stream_memory.c │ │ │ └── rc4_test.c │ │ │ ├── salsa20 │ │ │ ├── salsa20_crypt.c │ │ │ ├── salsa20_done.c │ │ │ ├── salsa20_ivctr64.c │ │ │ ├── salsa20_keystream.c │ │ │ ├── salsa20_memory.c │ │ │ ├── salsa20_setup.c │ │ │ ├── salsa20_test.c │ │ │ ├── xsalsa20_memory.c │ │ │ ├── xsalsa20_setup.c │ │ │ └── xsalsa20_test.c │ │ │ ├── sober128 │ │ │ ├── sober128_stream.c │ │ │ ├── sober128_stream_memory.c │ │ │ ├── sober128_test.c │ │ │ └── sober128tab.c │ │ │ └── sosemanuk │ │ │ ├── sosemanuk.c │ │ │ ├── sosemanuk_memory.c │ │ │ └── sosemanuk_test.c │ └── sqlite │ │ ├── grdb.h │ │ ├── sqlite3.c │ │ └── sqlite3.h └── SQLiteDB │ ├── Core │ ├── Backup.swift │ ├── Blob.swift │ ├── Connection+Aggregation.swift │ ├── Connection+Attach.swift │ ├── Connection+Pragmas.swift │ ├── Connection.swift │ ├── Errors.swift │ ├── Result.swift │ ├── SQLiteFeature.swift │ ├── SQLiteVersion.swift │ ├── Statement.swift │ ├── URIQueryParameter.swift │ └── Value.swift │ ├── Extensions │ ├── Cipher.swift │ ├── FTS4.swift │ ├── FTS5.swift │ └── RTree.swift │ ├── Foundation.swift │ ├── Helpers.swift │ ├── PrivacyInfo.xcprivacy │ ├── SQLite.h │ ├── Schema │ ├── Connection+Schema.swift │ ├── SchemaChanger.swift │ ├── SchemaDefinitions.swift │ └── SchemaReader.swift │ └── Typed │ ├── AggregateFunctions.swift │ ├── Coding.swift │ ├── Collation.swift │ ├── CoreFunctions.swift │ ├── CustomFunctions.swift │ ├── DateAndTimeFunctions.swift │ ├── Expression.swift │ ├── Operators.swift │ ├── Query+with.swift │ ├── Query.swift │ ├── Schema.swift │ ├── Setter.swift │ └── WindowFunctions.swift ├── Tests ├── .swiftlint.yml ├── SPM │ ├── .gitignore │ ├── Package.swift │ ├── Sources │ │ └── test │ │ │ └── main.swift │ └── db.sqlite └── SQLiteDBTests │ ├── Core │ ├── BlobTests.swift │ ├── Connection+AttachTests.swift │ ├── Connection+PragmaTests.swift │ ├── ConnectionTests.swift │ ├── CoreFunctionsTests.swift │ ├── ResultTests.swift │ ├── StatementTests.swift │ └── ValueTests.swift │ ├── Extensions │ ├── CipherTests.swift │ ├── FTS4Tests.swift │ ├── FTS5Tests.swift │ ├── FTSIntegrationTests.swift │ └── RTreeTests.swift │ ├── Fixtures.swift │ ├── FoundationTests.swift │ ├── Resources │ ├── encrypted-3.x.sqlite │ ├── encrypted-4.x.sqlite │ └── test.sqlite │ ├── Schema │ ├── Connection+SchemaTests.swift │ ├── SchemaChangerTests.swift │ ├── SchemaDefinitionsTests.swift │ ├── SchemaReaderTests.swift │ └── SchemaTests.swift │ ├── TestHelpers.swift │ └── Typed │ ├── AggregateFunctionsTests.swift │ ├── CustomAggregationTests.swift │ ├── CustomFunctionsTests.swift │ ├── DateAndTimeFunctionTests.swift │ ├── ExpressionTests.swift │ ├── OperatorsTests.swift │ ├── QueryIntegrationTests.swift │ ├── QueryTests.swift │ ├── RowTests.swift │ ├── SelectTests.swift │ ├── SetterTests.swift │ └── WindowFunctionsTests.swift └── scripts └── build_sqlcipher.sh /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: swift-sqlcipher ci 2 | on: 3 | push: 4 | branches: [ main ] 5 | workflow_dispatch: 6 | pull_request: 7 | branches: 8 | - '*' 9 | schedule: 10 | - cron: '0 6,18 * * *' 11 | jobs: 12 | linux-android: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v4 16 | - name: "Test Swift Package Android" 17 | uses: skiptools/swift-android-action@v2 18 | - name: "Test Swift Package Linux" 19 | run: swift test 20 | macos-ios: 21 | runs-on: macos-latest 22 | steps: 23 | - uses: actions/checkout@v4 24 | - name: "Test Swift Package macOS" 25 | run: swift test 26 | - name: "Test Swift Package iOS" 27 | run: xcodebuild test -sdk "iphonesimulator" -destination "platform=iOS Simulator,name=$(xcrun simctl list devices --json | jq -r '.devices | to_entries[] | .value[] | select(.availability == "(available)" or .isAvailable == true) | .name' | grep -E '^iPhone [0-9]+$' | sort -V | tail -n 1)" -scheme "$(xcodebuild -list -json | jq -r '.workspace.schemes[-1]')" 28 | windows: 29 | runs-on: windows-2022 30 | steps: 31 | - uses: compnerd/gha-setup-swift@main 32 | with: 33 | branch: swift-6.1-release 34 | tag: 6.1-RELEASE 35 | - uses: actions/checkout@v4 36 | - run: swift test 37 | 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OS X 2 | .DS_Store 3 | .*.swp 4 | 5 | # Xcode 6 | build/ 7 | *.pbxuser 8 | !default.pbxuser 9 | *.mode1v3 10 | !default.mode1v3 11 | *.mode2v3 12 | !default.mode2v3 13 | *.perspectivev3 14 | !default.perspectivev3 15 | xcuserdata 16 | *.xccheckout 17 | *.moved-aside 18 | DerivedData 19 | *.hmap 20 | *.ipa 21 | *.xcuserstate 22 | 23 | # Carthage 24 | /Carthage/ 25 | 26 | # Makefile 27 | bin/ 28 | 29 | # Swift Package Manager 30 | .build 31 | Packages/ 32 | .swiftpm/ 33 | -------------------------------------------------------------------------------- /Examples/PackageTraits/MiddlewareClientWithSQLCipher/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | xcuserdata/ 5 | DerivedData/ 6 | .swiftpm/configuration/registries.json 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .netrc 9 | -------------------------------------------------------------------------------- /Examples/PackageTraits/MiddlewareClientWithSQLCipher/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 6.1 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "MiddlewareClientWithSQLCipher", 6 | products: [ 7 | .library(name: "MiddlewareClientWithSQLCipher", 8 | targets: ["MiddlewareClientWithSQLCipher"]) 9 | ], 10 | dependencies: [ 11 | .package(path: "../SQLMiddleware", traits: ["SQLCipher"]) 12 | ], 13 | targets: [ 14 | .target(name: "MiddlewareClientWithSQLCipher", dependencies: [ 15 | .product(name: "SQLMiddleware", package: "SQLMiddleware") 16 | ]), 17 | .testTarget(name: "MiddlewareClientWithSQLCipherTests", 18 | dependencies: ["MiddlewareClientWithSQLCipher"]) 19 | ] 20 | ) 21 | -------------------------------------------------------------------------------- /Examples/PackageTraits/MiddlewareClientWithSQLCipher/Sources/MiddlewareClientWithSQLCipher/MiddlewareClientWithSQLCipher.swift: -------------------------------------------------------------------------------- 1 | import SQLMiddleware 2 | 3 | func databaseVersion() -> String? { 4 | middlewareDatabaseType() 5 | } 6 | 7 | -------------------------------------------------------------------------------- /Examples/PackageTraits/MiddlewareClientWithSQLCipher/Tests/MiddlewareClientWithSQLCipherTests/MiddlewareClientWithSQLCipherTests.swift: -------------------------------------------------------------------------------- 1 | import Testing 2 | @testable import MiddlewareClientWithSQLCipher 3 | 4 | @Test func testDatabaseIsSQLCipher() async throws { 5 | #expect(databaseVersion() == "SQLCipher 4.8.0 community") 6 | } 7 | -------------------------------------------------------------------------------- /Examples/PackageTraits/MiddlewareClientWithoutSQLCipher/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | xcuserdata/ 5 | DerivedData/ 6 | .swiftpm/configuration/registries.json 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .netrc 9 | -------------------------------------------------------------------------------- /Examples/PackageTraits/MiddlewareClientWithoutSQLCipher/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 6.1 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "MiddlewareClientWithoutSQLCipher", 6 | products: [ 7 | .library(name: "MiddlewareClientWithoutSQLCipher", 8 | targets: ["MiddlewareClientWithoutSQLCipher"]) 9 | ], 10 | dependencies: [ 11 | .package(path: "../SQLMiddleware") 12 | ], 13 | targets: [ 14 | .target(name: "MiddlewareClientWithoutSQLCipher", dependencies: [ 15 | .product(name: "SQLMiddleware", package: "SQLMiddleware") 16 | ]), 17 | .testTarget(name: "MiddlewareClientWithoutSQLCipherTests", 18 | dependencies: ["MiddlewareClientWithoutSQLCipher"]) 19 | ] 20 | ) 21 | -------------------------------------------------------------------------------- /Examples/PackageTraits/MiddlewareClientWithoutSQLCipher/Sources/MiddlewareClientWithoutSQLCipher/MiddlewareClientWithoutSQLCipher.swift: -------------------------------------------------------------------------------- 1 | import SQLMiddleware 2 | 3 | func databaseVersion() -> String? { 4 | middlewareDatabaseType() 5 | } 6 | 7 | -------------------------------------------------------------------------------- /Examples/PackageTraits/MiddlewareClientWithoutSQLCipher/Tests/MiddlewareClientWithoutSQLCipherTests/MiddlewareClientWithoutSQLCipherTests.swift: -------------------------------------------------------------------------------- 1 | import Testing 2 | @testable import MiddlewareClientWithoutSQLCipher 3 | 4 | @Test func testDatabaseIsSQLite() async throws { 5 | #expect(databaseVersion() == "SQLite3 3.43.2") 6 | } 7 | -------------------------------------------------------------------------------- /Examples/PackageTraits/SQLMiddleware/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | xcuserdata/ 5 | DerivedData/ 6 | .swiftpm/configuration/registries.json 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .netrc 9 | -------------------------------------------------------------------------------- /Examples/PackageTraits/SQLMiddleware/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 6.1 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "SQLMiddleware", 6 | products: [ 7 | .library(name: "SQLMiddleware", targets: ["SQLMiddleware"]), 8 | ], 9 | traits: [ 10 | .trait(name: "SQLCipher", description: "Use the SQLCipher library rather than the vendored SQLite") 11 | ], 12 | dependencies: [ 13 | .package(path: "../../..") 14 | ], 15 | targets: [ 16 | .target(name: "SQLMiddleware", dependencies: [ 17 | // target only depends on SQLCipher when the "SQLCipher" trait is activated by a dependent package 18 | // otherwise it will default to using the system "SQLite3" framework 19 | .product(name: "SQLCipher", package: "swift-sqlcipher", condition: .when(traits: ["SQLCipher"])) 20 | ]) 21 | ] 22 | ) 23 | -------------------------------------------------------------------------------- /Examples/PackageTraits/SQLMiddleware/Sources/SQLMiddleware/SQLMiddleware.swift: -------------------------------------------------------------------------------- 1 | #if canImport(SQLCipher) 2 | import SQLCipher 3 | #else 4 | import SQLite3 5 | #endif 6 | 7 | public func middlewareDatabaseType() -> String? { 8 | #if canImport(SQLCipher) 9 | let dbtype = "SQLCipher" 10 | let sql = "PRAGMA cipher_version;" 11 | #else 12 | let dbtype = "SQLite3" 13 | let sql = "SELECT sqlite_version();" 14 | #endif 15 | let version = queryString(sql: sql) 16 | return "\(dbtype) \(version ?? "unknown")" 17 | } 18 | 19 | public func queryString(sql: String, databasePath: String = ":memory:") -> String? { 20 | var db: OpaquePointer? 21 | var result: String? 22 | 23 | if sqlite3_open(databasePath, &db) == SQLITE_OK { 24 | defer { sqlite3_close(db) } 25 | var statement: OpaquePointer? 26 | if sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK { 27 | defer { sqlite3_finalize(statement) } 28 | if sqlite3_step(statement) == SQLITE_ROW { 29 | if let cString = sqlite3_column_text(statement, 0) { 30 | result = String(cString: cString) 31 | } 32 | } 33 | } 34 | } 35 | 36 | return result 37 | } 38 | 39 | -------------------------------------------------------------------------------- /LICENSE-SQLCIPHER.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2008-2023, ZETETIC LLC 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of the ZETETIC LLC nor the 12 | names of its contributors may be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY 16 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2023-2025 Skip.tools () 4 | Copyright (c) 2014-2015 Stephen Celis () 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/ccm/ccm_add_aad.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | #ifdef LTC_CCM_MODE 6 | 7 | /** 8 | Add AAD to the CCM state 9 | @param ccm The CCM state 10 | @param adata The additional authentication data to add to the CCM state 11 | @param adatalen The length of the AAD data. 12 | @return CRYPT_OK on success 13 | */ 14 | int ccm_add_aad(ccm_state *ccm, 15 | const unsigned char *adata, unsigned long adatalen) 16 | { 17 | unsigned long y; 18 | int err; 19 | 20 | LTC_ARGCHK(ccm != NULL); 21 | LTC_ARGCHK(adata != NULL); 22 | 23 | if (ccm->aadlen < ccm->current_aadlen + adatalen) { 24 | return CRYPT_INVALID_ARG; 25 | } 26 | ccm->current_aadlen += adatalen; 27 | 28 | /* now add the data */ 29 | for (y = 0; y < adatalen; y++) { 30 | if (ccm->x == 16) { 31 | /* full block so let's encrypt it */ 32 | if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { 33 | return err; 34 | } 35 | ccm->x = 0; 36 | } 37 | ccm->PAD[ccm->x++] ^= adata[y]; 38 | } 39 | 40 | /* remainder? */ 41 | if (ccm->aadlen == ccm->current_aadlen) { 42 | if (ccm->x != 0) { 43 | if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { 44 | return err; 45 | } 46 | } 47 | ccm->x = 0; 48 | } 49 | 50 | return CRYPT_OK; 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/ccm/ccm_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | #ifdef LTC_CCM_MODE 6 | 7 | /** 8 | Terminate a CCM stream 9 | @param ccm The CCM state 10 | @param tag [out] The destination for the MAC tag 11 | @param taglen [in/out] The length of the MAC tag 12 | @return CRYPT_OK on success 13 | */ 14 | int ccm_done(ccm_state *ccm, 15 | unsigned char *tag, unsigned long *taglen) 16 | { 17 | unsigned long x, y; 18 | int err; 19 | 20 | LTC_ARGCHK(ccm != NULL); 21 | 22 | /* Check all data have been processed */ 23 | if (ccm->ptlen != ccm->current_ptlen) { 24 | return CRYPT_ERROR; 25 | } 26 | 27 | LTC_ARGCHK(tag != NULL); 28 | LTC_ARGCHK(taglen != NULL); 29 | 30 | if (ccm->x != 0) { 31 | if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { 32 | return err; 33 | } 34 | } 35 | 36 | /* setup CTR for the TAG (zero the count) */ 37 | for (y = 15; y > 15 - ccm->L; y--) { 38 | ccm->ctr[y] = 0x00; 39 | } 40 | if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) { 41 | return err; 42 | } 43 | 44 | cipher_descriptor[ccm->cipher].done(&ccm->K); 45 | 46 | /* store the TAG */ 47 | for (x = 0; x < 16 && x < *taglen; x++) { 48 | tag[x] = ccm->PAD[x] ^ ccm->CTRPAD[x]; 49 | } 50 | *taglen = x; 51 | 52 | return CRYPT_OK; 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/ccm/ccm_reset.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | #ifdef LTC_CCM_MODE 6 | 7 | /** 8 | Reset a CCM state to as if you just called ccm_init(). This saves the initialization time. 9 | @param ccm The CCM state to reset 10 | @return CRYPT_OK on success 11 | */ 12 | int ccm_reset(ccm_state *ccm) 13 | { 14 | LTC_ARGCHK(ccm != NULL); 15 | zeromem(ccm->PAD, sizeof(ccm->PAD)); 16 | zeromem(ccm->ctr, sizeof(ccm->ctr)); 17 | zeromem(ccm->CTRPAD, sizeof(ccm->CTRPAD)); 18 | ccm->CTRlen = 0; 19 | ccm->current_ptlen = 0; 20 | ccm->current_aadlen = 0; 21 | 22 | return CRYPT_OK; 23 | } 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/chachapoly/chacha20poly1305_add_aad.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_CHACHA20POLY1305_MODE 7 | 8 | /** 9 | Add AAD to the ChaCha20Poly1305 state 10 | @param st The ChaCha20Poly1305 state 11 | @param in The additional authentication data to add to the ChaCha20Poly1305 state 12 | @param inlen The length of the ChaCha20Poly1305 data. 13 | @return CRYPT_OK on success 14 | */ 15 | int chacha20poly1305_add_aad(chacha20poly1305_state *st, const unsigned char *in, unsigned long inlen) 16 | { 17 | int err; 18 | 19 | if (inlen == 0) return CRYPT_OK; /* nothing to do */ 20 | LTC_ARGCHK(st != NULL); 21 | 22 | if (st->aadflg == 0) return CRYPT_ERROR; 23 | if ((err = poly1305_process(&st->poly, in, inlen)) != CRYPT_OK) return err; 24 | st->aadlen += (ulong64)inlen; 25 | return CRYPT_OK; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/chachapoly/chacha20poly1305_decrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_CHACHA20POLY1305_MODE 7 | 8 | /** 9 | Decrypt bytes of ciphertext with ChaCha20Poly1305 10 | @param st The ChaCha20Poly1305 state 11 | @param in The ciphertext 12 | @param inlen The length of the input (octets) 13 | @param out [out] The plaintext (length inlen) 14 | @return CRYPT_OK if successful 15 | */ 16 | int chacha20poly1305_decrypt(chacha20poly1305_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out) 17 | { 18 | unsigned char padzero[16] = { 0 }; 19 | unsigned long padlen; 20 | int err; 21 | 22 | LTC_ARGCHK(st != NULL); 23 | 24 | if (st->aadflg) { 25 | padlen = 16 - (unsigned long)(st->aadlen % 16); 26 | if (padlen < 16) { 27 | if ((err = poly1305_process(&st->poly, padzero, padlen)) != CRYPT_OK) return err; 28 | } 29 | st->aadflg = 0; /* no more AAD */ 30 | } 31 | if (st->aadflg) st->aadflg = 0; /* no more AAD */ 32 | if ((err = poly1305_process(&st->poly, in, inlen)) != CRYPT_OK) return err; 33 | if ((err = chacha_crypt(&st->chacha, in, inlen, out)) != CRYPT_OK) return err; 34 | st->ctlen += (ulong64)inlen; 35 | return CRYPT_OK; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/chachapoly/chacha20poly1305_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_CHACHA20POLY1305_MODE 7 | 8 | /** 9 | Terminate a ChaCha20Poly1305 stream 10 | @param st The ChaCha20Poly1305 state 11 | @param tag [out] The destination for the MAC tag 12 | @param taglen [in/out] The length of the MAC tag 13 | @return CRYPT_OK on success 14 | */ 15 | int chacha20poly1305_done(chacha20poly1305_state *st, unsigned char *tag, unsigned long *taglen) 16 | { 17 | unsigned char padzero[16] = { 0 }; 18 | unsigned long padlen; 19 | unsigned char buf[16]; 20 | int err; 21 | 22 | LTC_ARGCHK(st != NULL); 23 | 24 | padlen = 16 - (unsigned long)(st->ctlen % 16); 25 | if (padlen < 16) { 26 | if ((err = poly1305_process(&st->poly, padzero, padlen)) != CRYPT_OK) return err; 27 | } 28 | STORE64L(st->aadlen, buf); 29 | STORE64L(st->ctlen, buf + 8); 30 | if ((err = poly1305_process(&st->poly, buf, 16)) != CRYPT_OK) return err; 31 | if ((err = poly1305_done(&st->poly, tag, taglen)) != CRYPT_OK) return err; 32 | if ((err = chacha_done(&st->chacha)) != CRYPT_OK) return err; 33 | return CRYPT_OK; 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/chachapoly/chacha20poly1305_encrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_CHACHA20POLY1305_MODE 7 | 8 | /** 9 | Encrypt bytes of ciphertext with ChaCha20Poly1305 10 | @param st The ChaCha20Poly1305 state 11 | @param in The plaintext 12 | @param inlen The length of the input (octets) 13 | @param out [out] The ciphertext (length inlen) 14 | @return CRYPT_OK if successful 15 | */ 16 | int chacha20poly1305_encrypt(chacha20poly1305_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out) 17 | { 18 | unsigned char padzero[16] = { 0 }; 19 | unsigned long padlen; 20 | int err; 21 | 22 | LTC_ARGCHK(st != NULL); 23 | 24 | if ((err = chacha_crypt(&st->chacha, in, inlen, out)) != CRYPT_OK) return err; 25 | if (st->aadflg) { 26 | padlen = 16 - (unsigned long)(st->aadlen % 16); 27 | if (padlen < 16) { 28 | if ((err = poly1305_process(&st->poly, padzero, padlen)) != CRYPT_OK) return err; 29 | } 30 | st->aadflg = 0; /* no more AAD */ 31 | } 32 | if ((err = poly1305_process(&st->poly, out, inlen)) != CRYPT_OK) return err; 33 | st->ctlen += (ulong64)inlen; 34 | return CRYPT_OK; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/chachapoly/chacha20poly1305_init.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_CHACHA20POLY1305_MODE 7 | 8 | /** 9 | Initialize an ChaCha20Poly1305 context (only the key) 10 | @param st [out] The destination of the ChaCha20Poly1305 state 11 | @param key The secret key 12 | @param keylen The length of the secret key (octets) 13 | @return CRYPT_OK if successful 14 | */ 15 | int chacha20poly1305_init(chacha20poly1305_state *st, const unsigned char *key, unsigned long keylen) 16 | { 17 | return chacha_setup(&st->chacha, key, keylen, 20); 18 | } 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/chachapoly/chacha20poly1305_setiv_rfc7905.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_CHACHA20POLY1305_MODE 7 | 8 | /** 9 | Set IV + counter data (with RFC7905-magic) to the ChaCha20Poly1305 state and reset the context 10 | @param st The ChaCha20Poly1305 state 11 | @param iv The IV data to add 12 | @param ivlen The length of the IV (must be 12 or 8) 13 | @param sequence_number 64bit sequence number which is incorporated into IV as described in RFC7905 14 | @return CRYPT_OK on success 15 | */ 16 | int chacha20poly1305_setiv_rfc7905(chacha20poly1305_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 sequence_number) 17 | { 18 | int i; 19 | unsigned char combined_iv[12] = { 0 }; 20 | 21 | LTC_ARGCHK(st != NULL); 22 | LTC_ARGCHK(iv != NULL); 23 | LTC_ARGCHK(ivlen == 12); 24 | 25 | STORE64L(sequence_number, combined_iv + 4); 26 | for (i = 0; i < 12; i++) combined_iv[i] = iv[i] ^ combined_iv[i]; 27 | return chacha20poly1305_setiv(st, combined_iv, 12); 28 | } 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/eax/eax_addheader.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | /** 4 | @file eax_addheader.c 5 | EAX implementation, add meta-data, by Tom St Denis 6 | */ 7 | #include "tomcrypt_private.h" 8 | 9 | #ifdef LTC_EAX_MODE 10 | 11 | /** 12 | add header (metadata) to the stream 13 | @param eax The current EAX state 14 | @param header The header (meta-data) data you wish to add to the state 15 | @param length The length of the header data 16 | @return CRYPT_OK if successful 17 | */ 18 | int eax_addheader(eax_state *eax, const unsigned char *header, 19 | unsigned long length) 20 | { 21 | LTC_ARGCHK(eax != NULL); 22 | LTC_ARGCHK(header != NULL); 23 | return omac_process(&eax->headeromac, header, length); 24 | } 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/eax/eax_decrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /** 5 | @file eax_decrypt.c 6 | EAX implementation, decrypt block, by Tom St Denis 7 | */ 8 | #include "tomcrypt_private.h" 9 | 10 | #ifdef LTC_EAX_MODE 11 | 12 | /** 13 | Decrypt data with the EAX protocol 14 | @param eax The EAX state 15 | @param ct The ciphertext 16 | @param pt [out] The plaintext 17 | @param length The length (octets) of the ciphertext 18 | @return CRYPT_OK if successful 19 | */ 20 | int eax_decrypt(eax_state *eax, const unsigned char *ct, unsigned char *pt, 21 | unsigned long length) 22 | { 23 | int err; 24 | 25 | LTC_ARGCHK(eax != NULL); 26 | LTC_ARGCHK(pt != NULL); 27 | LTC_ARGCHK(ct != NULL); 28 | 29 | /* omac ciphertext */ 30 | if ((err = omac_process(&eax->ctomac, ct, length)) != CRYPT_OK) { 31 | return err; 32 | } 33 | 34 | /* decrypt */ 35 | return ctr_decrypt(ct, pt, length, &eax->ctr); 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/eax/eax_encrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /** 5 | @file eax_encrypt.c 6 | EAX implementation, encrypt block by Tom St Denis 7 | */ 8 | #include "tomcrypt_private.h" 9 | 10 | #ifdef LTC_EAX_MODE 11 | 12 | /** 13 | Encrypt with EAX a block of data. 14 | @param eax The EAX state 15 | @param pt The plaintext to encrypt 16 | @param ct [out] The ciphertext as encrypted 17 | @param length The length of the plaintext (octets) 18 | @return CRYPT_OK if successful 19 | */ 20 | int eax_encrypt(eax_state *eax, const unsigned char *pt, unsigned char *ct, 21 | unsigned long length) 22 | { 23 | int err; 24 | 25 | LTC_ARGCHK(eax != NULL); 26 | LTC_ARGCHK(pt != NULL); 27 | LTC_ARGCHK(ct != NULL); 28 | 29 | /* encrypt */ 30 | if ((err = ctr_encrypt(pt, ct, length, &eax->ctr)) != CRYPT_OK) { 31 | return err; 32 | } 33 | 34 | /* omac ciphertext */ 35 | return omac_process(&eax->ctomac, ct, length); 36 | } 37 | 38 | #endif 39 | 40 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/gcm/gcm_mult_h.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /** 5 | @file gcm_mult_h.c 6 | GCM implementation, do the GF mult, by Tom St Denis 7 | */ 8 | #include "tomcrypt_private.h" 9 | 10 | #if defined(LTC_GCM_MODE) 11 | /** 12 | GCM multiply by H 13 | @param gcm The GCM state which holds the H value 14 | @param I The value to multiply H by 15 | */ 16 | void gcm_mult_h(const gcm_state *gcm, unsigned char *I) 17 | { 18 | unsigned char T[16]; 19 | #ifdef LTC_GCM_TABLES 20 | int x; 21 | #ifdef LTC_GCM_TABLES_SSE2 22 | asm("movdqa (%0),%%xmm0"::"r"(&gcm->PC[0][I[0]][0])); 23 | for (x = 1; x < 16; x++) { 24 | asm("pxor (%0),%%xmm0"::"r"(&gcm->PC[x][I[x]][0])); 25 | } 26 | asm("movdqa %%xmm0,(%0)"::"r"(&T)); 27 | #else 28 | int y; 29 | XMEMCPY(T, &gcm->PC[0][I[0]][0], 16); 30 | for (x = 1; x < 16; x++) { 31 | #ifdef LTC_FAST 32 | for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) { 33 | *(LTC_FAST_TYPE_PTR_CAST(T + y)) ^= *(LTC_FAST_TYPE_PTR_CAST(&gcm->PC[x][I[x]][y])); 34 | } 35 | #else 36 | for (y = 0; y < 16; y++) { 37 | T[y] ^= gcm->PC[x][I[x]][y]; 38 | } 39 | #endif /* LTC_FAST */ 40 | } 41 | #endif /* LTC_GCM_TABLES_SSE2 */ 42 | #else 43 | gcm_gf_mult(gcm->H, I, T); 44 | #endif 45 | XMEMCPY(I, T, 16); 46 | } 47 | #endif 48 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/gcm/gcm_reset.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /** 5 | @file gcm_reset.c 6 | GCM implementation, reset a used state so it can accept IV data, by Tom St Denis 7 | */ 8 | #include "tomcrypt_private.h" 9 | 10 | #ifdef LTC_GCM_MODE 11 | 12 | /** 13 | Reset a GCM state to as if you just called gcm_init(). This saves the initialization time. 14 | @param gcm The GCM state to reset 15 | @return CRYPT_OK on success 16 | */ 17 | int gcm_reset(gcm_state *gcm) 18 | { 19 | LTC_ARGCHK(gcm != NULL); 20 | 21 | zeromem(gcm->buf, sizeof(gcm->buf)); 22 | zeromem(gcm->X, sizeof(gcm->X)); 23 | gcm->mode = LTC_GCM_MODE_IV; 24 | gcm->ivmode = 0; 25 | gcm->buflen = 0; 26 | gcm->totlen = 0; 27 | gcm->pttotlen = 0; 28 | 29 | return CRYPT_OK; 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/ocb/ocb_done_encrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /** 5 | @file ocb_done_encrypt.c 6 | OCB implementation, terminate encryption, by Tom St Denis 7 | */ 8 | #include "tomcrypt_private.h" 9 | 10 | #ifdef LTC_OCB_MODE 11 | 12 | /** 13 | Terminate an encryption OCB state 14 | @param ocb The OCB state 15 | @param pt Remaining plaintext (if any) 16 | @param ptlen The length of the plaintext (octets) 17 | @param ct [out] The ciphertext (if any) 18 | @param tag [out] The tag for the OCB stream 19 | @param taglen [in/out] The max size and resulting size of the tag 20 | @return CRYPT_OK if successful 21 | */ 22 | int ocb_done_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen, 23 | unsigned char *ct, unsigned char *tag, unsigned long *taglen) 24 | { 25 | LTC_ARGCHK(ocb != NULL); 26 | LTC_ARGCHK(pt != NULL); 27 | LTC_ARGCHK(ct != NULL); 28 | LTC_ARGCHK(tag != NULL); 29 | LTC_ARGCHK(taglen != NULL); 30 | return s_ocb_done(ocb, pt, ptlen, ct, tag, taglen, 0); 31 | } 32 | 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/ocb/ocb_ntz.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /** 5 | @file ocb_ntz.c 6 | OCB implementation, internal function, by Tom St Denis 7 | */ 8 | 9 | #include "tomcrypt_private.h" 10 | 11 | #ifdef LTC_OCB_MODE 12 | 13 | /** 14 | Returns the number of leading zero bits [from lsb up] 15 | @param x The 32-bit value to observe 16 | @return The number of bits [from the lsb up] that are zero 17 | */ 18 | int ocb_ntz(unsigned long x) 19 | { 20 | int c; 21 | x &= 0xFFFFFFFFUL; 22 | c = 0; 23 | while ((x & 1) == 0) { 24 | ++c; 25 | x >>= 1; 26 | } 27 | return c; 28 | } 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/ocb/ocb_shift_xor.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /** 5 | @file ocb_shift_xor.c 6 | OCB implementation, internal function, by Tom St Denis 7 | */ 8 | #include "tomcrypt_private.h" 9 | 10 | #ifdef LTC_OCB_MODE 11 | 12 | /** 13 | Compute the shift/xor for OCB (internal function) 14 | @param ocb The OCB state 15 | @param Z The destination of the shift 16 | */ 17 | void ocb_shift_xor(ocb_state *ocb, unsigned char *Z) 18 | { 19 | int x, y; 20 | y = ocb_ntz(ocb->block_index++); 21 | for (x = 0; x < ocb->block_len; x++) { 22 | ocb->Li[x] ^= ocb->Ls[y][x]; 23 | Z[x] = ocb->Li[x] ^ ocb->R[x]; 24 | } 25 | } 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/ocb3/ocb3_int_ntz.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /** 5 | @file ocb3_int_ntz.c 6 | OCB implementation, INTERNAL ONLY helper, by Tom St Denis 7 | */ 8 | #include "tomcrypt_private.h" 9 | 10 | #ifdef LTC_OCB3_MODE 11 | 12 | /** 13 | Returns the number of leading zero bits [from lsb up] (internal function) 14 | @param x The 32-bit value to observe 15 | @return The number of bits [from the lsb up] that are zero 16 | */ 17 | int ocb3_int_ntz(unsigned long x) 18 | { 19 | int c; 20 | x &= 0xFFFFFFFFUL; 21 | c = 0; 22 | while ((x & 1) == 0) { 23 | ++c; 24 | x >>= 1; 25 | } 26 | return c; 27 | } 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/encauth/ocb3/ocb3_int_xor_blocks.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /** 5 | @file ocb3_int_xor_blocks.c 6 | OCB implementation, INTERNAL ONLY helper, by Karel Miko 7 | */ 8 | #include "tomcrypt_private.h" 9 | 10 | #ifdef LTC_OCB3_MODE 11 | 12 | /** 13 | Compute xor for two blocks of bytes 'out = block_a XOR block_b' (internal function) 14 | @param out The block of bytes (output) 15 | @param block_a The block of bytes (input) 16 | @param block_b The block of bytes (input) 17 | @param block_len The size of block_a, block_b, out 18 | */ 19 | void ocb3_int_xor_blocks(unsigned char *out, const unsigned char *block_a, const unsigned char *block_b, unsigned long block_len) 20 | { 21 | int x; 22 | if (out == block_a) { 23 | for (x = 0; x < (int)block_len; x++) out[x] ^= block_b[x]; 24 | } 25 | else { 26 | for (x = 0; x < (int)block_len; x++) out[x] = block_a[x] ^ block_b[x]; 27 | } 28 | } 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/hashes/helper/hash_file.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | #ifndef LTC_NO_FILE 6 | /** 7 | @file hash_file.c 8 | Hash a file, Tom St Denis 9 | */ 10 | 11 | /** 12 | @param hash The index of the hash desired 13 | @param fname The name of the file you wish to hash 14 | @param out [out] The destination of the digest 15 | @param outlen [in/out] The max size and resulting size of the message digest 16 | @result CRYPT_OK if successful 17 | */ 18 | int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen) 19 | { 20 | FILE *in; 21 | int err; 22 | LTC_ARGCHK(fname != NULL); 23 | LTC_ARGCHK(out != NULL); 24 | LTC_ARGCHK(outlen != NULL); 25 | 26 | if ((err = hash_is_valid(hash)) != CRYPT_OK) { 27 | return err; 28 | } 29 | 30 | in = fopen(fname, "rb"); 31 | if (in == NULL) { 32 | return CRYPT_FILE_NOTFOUND; 33 | } 34 | 35 | err = hash_filehandle(hash, in, out, outlen); 36 | if (fclose(in) != 0) { 37 | return CRYPT_ERROR; 38 | } 39 | 40 | return err; 41 | } 42 | #endif /* #ifndef LTC_NO_FILE */ 43 | 44 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/headers/tomcrypt_argchk.h: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /* Defines the LTC_ARGCHK macro used within the library */ 5 | /* ARGTYPE is defined in tomcrypt_cfg.h */ 6 | 7 | /* ARGTYPE is per default defined to 0 */ 8 | #if ARGTYPE == 0 9 | 10 | #include 11 | 12 | LTC_NORETURN void crypt_argchk(const char *v, const char *s, int d); 13 | #define LTC_ARGCHK(x) do { if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); } }while(0) 14 | #define LTC_ARGCHKVD(x) do { if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); } }while(0) 15 | 16 | #elif ARGTYPE == 1 17 | 18 | /* fatal type of error */ 19 | #define LTC_ARGCHK(x) assert((x)) 20 | #define LTC_ARGCHKVD(x) LTC_ARGCHK(x) 21 | 22 | #elif ARGTYPE == 2 23 | 24 | #define LTC_ARGCHK(x) if (!(x)) { fprintf(stderr, "\nwarning: ARGCHK failed at %s:%d\n", __FILE__, __LINE__); } 25 | #define LTC_ARGCHKVD(x) LTC_ARGCHK(x) 26 | 27 | #elif ARGTYPE == 3 28 | 29 | #define LTC_ARGCHK(x) LTC_UNUSED_PARAM(x) 30 | #define LTC_ARGCHKVD(x) LTC_ARGCHK(x) 31 | 32 | #elif ARGTYPE == 4 33 | 34 | #define LTC_ARGCHK(x) if (!(x)) return CRYPT_INVALID_ARG; 35 | #define LTC_ARGCHKVD(x) if (!(x)) return; 36 | 37 | #endif 38 | 39 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/mac/blake2/blake2bmac_memory.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_BLAKE2BMAC 7 | 8 | /** 9 | BLAKE2B MAC a block of memory to produce the authentication tag 10 | @param key The secret key 11 | @param keylen The length of the secret key (octets) 12 | @param in The data to BLAKE2B MAC 13 | @param inlen The length of the data to BLAKE2B MAC (octets) 14 | @param mac [out] Destination of the authentication tag 15 | @param maclen [in/out] Max size and resulting size of authentication tag 16 | @return CRYPT_OK if successful 17 | */ 18 | int blake2bmac_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen) 19 | { 20 | blake2bmac_state st; 21 | int err; 22 | 23 | LTC_ARGCHK(key != NULL); 24 | LTC_ARGCHK(in != NULL); 25 | LTC_ARGCHK(mac != NULL); 26 | LTC_ARGCHK(maclen != NULL); 27 | 28 | if ((err = blake2bmac_init(&st, *maclen, key, keylen)) != CRYPT_OK) { goto LBL_ERR; } 29 | if ((err = blake2bmac_process(&st, in, inlen)) != CRYPT_OK) { goto LBL_ERR; } 30 | err = blake2bmac_done(&st, mac, maclen); 31 | LBL_ERR: 32 | #ifdef LTC_CLEAN_STACK 33 | zeromem(&st, sizeof(blake2bmac_state)); 34 | #endif 35 | return err; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/mac/blake2/blake2smac_memory.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_BLAKE2SMAC 7 | 8 | /** 9 | BLAKE2S MAC a block of memory to produce the authentication tag 10 | @param key The secret key 11 | @param keylen The length of the secret key (octets) 12 | @param in The data to BLAKE2S MAC 13 | @param inlen The length of the data to BLAKE2S MAC (octets) 14 | @param mac [out] Destination of the authentication tag 15 | @param maclen [in/out] Max size and resulting size of authentication tag 16 | @return CRYPT_OK if successful 17 | */ 18 | int blake2smac_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen) 19 | { 20 | blake2smac_state st; 21 | int err; 22 | 23 | LTC_ARGCHK(key != NULL); 24 | LTC_ARGCHK(in != NULL); 25 | LTC_ARGCHK(mac != NULL); 26 | LTC_ARGCHK(maclen != NULL); 27 | 28 | if ((err = blake2smac_init(&st, *maclen, key, keylen)) != CRYPT_OK) { goto LBL_ERR; } 29 | if ((err = blake2smac_process(&st, in, inlen)) != CRYPT_OK) { goto LBL_ERR; } 30 | err = blake2smac_done(&st, mac, maclen); 31 | LBL_ERR: 32 | #ifdef LTC_CLEAN_STACK 33 | zeromem(&st, sizeof(blake2smac_state)); 34 | #endif 35 | return err; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/mac/f9/f9_init.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file f9_init.c 7 | F9 Support, start an F9 state 8 | */ 9 | 10 | #ifdef LTC_F9_MODE 11 | 12 | /** Initialize F9-MAC state 13 | @param f9 [out] f9 state to initialize 14 | @param cipher Index of cipher to use 15 | @param key [in] Secret key 16 | @param keylen Length of secret key in octets 17 | Return CRYPT_OK on success 18 | */ 19 | int f9_init(f9_state *f9, int cipher, const unsigned char *key, unsigned long keylen) 20 | { 21 | int x, err; 22 | 23 | LTC_ARGCHK(f9 != NULL); 24 | LTC_ARGCHK(key != NULL); 25 | 26 | /* schedule the key */ 27 | if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { 28 | return err; 29 | } 30 | 31 | #ifdef LTC_FAST 32 | if (cipher_descriptor[cipher].block_length % sizeof(LTC_FAST_TYPE)) { 33 | return CRYPT_INVALID_ARG; 34 | } 35 | #endif 36 | 37 | if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &f9->key)) != CRYPT_OK) { 38 | goto done; 39 | } 40 | 41 | /* make the second key */ 42 | for (x = 0; (unsigned)x < keylen; x++) { 43 | f9->akey[x] = key[x] ^ 0xAA; 44 | } 45 | 46 | /* setup struct */ 47 | zeromem(f9->IV, cipher_descriptor[cipher].block_length); 48 | zeromem(f9->ACC, cipher_descriptor[cipher].block_length); 49 | f9->blocksize = cipher_descriptor[cipher].block_length; 50 | f9->cipher = cipher; 51 | f9->buflen = 0; 52 | f9->keylen = keylen; 53 | done: 54 | return err; 55 | } 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/mac/hmac/hmac_process.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file hmac_process.c 7 | HMAC support, process data, Tom St Denis/Dobes Vandermeer 8 | */ 9 | 10 | #ifdef LTC_HMAC 11 | 12 | /** 13 | Process data through HMAC 14 | @param hmac The hmac state 15 | @param in The data to send through HMAC 16 | @param inlen The length of the data to HMAC (octets) 17 | @return CRYPT_OK if successful 18 | */ 19 | int hmac_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen) 20 | { 21 | int err; 22 | LTC_ARGCHK(hmac != NULL); 23 | LTC_ARGCHK(in != NULL); 24 | if ((err = hash_is_valid(hmac->hash)) != CRYPT_OK) { 25 | return err; 26 | } 27 | return hash_descriptor[hmac->hash].process(&hmac->md, in, inlen); 28 | } 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/mac/pelican/pelican_memory.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file pelican_memory.c 7 | Pelican MAC, MAC a block of memory, by Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_PELICAN 11 | 12 | /** 13 | Pelican block of memory 14 | @param key The key for the MAC 15 | @param keylen The length of the key (octets) 16 | @param in The input to MAC 17 | @param inlen The length of the input (octets) 18 | @param out [out] The output TAG 19 | @return CRYPT_OK on success 20 | */ 21 | int pelican_memory(const unsigned char *key, unsigned long keylen, 22 | const unsigned char *in, unsigned long inlen, 23 | unsigned char *out) 24 | { 25 | pelican_state *pel; 26 | int err; 27 | 28 | pel = XMALLOC(sizeof(*pel)); 29 | if (pel == NULL) { 30 | return CRYPT_MEM; 31 | } 32 | 33 | if ((err = pelican_init(pel, key, keylen)) != CRYPT_OK) { 34 | XFREE(pel); 35 | return err; 36 | } 37 | if ((err = pelican_process(pel, in ,inlen)) != CRYPT_OK) { 38 | XFREE(pel); 39 | return err; 40 | } 41 | err = pelican_done(pel, out); 42 | XFREE(pel); 43 | return err; 44 | } 45 | 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/mac/pmac/pmac_ntz.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file pmac_ntz.c 7 | PMAC implementation, internal function, by Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_PMAC 11 | 12 | /** 13 | Internal PMAC function 14 | */ 15 | int pmac_ntz(unsigned long x) 16 | { 17 | int c; 18 | x &= 0xFFFFFFFFUL; 19 | c = 0; 20 | while ((x & 1) == 0) { 21 | ++c; 22 | x >>= 1; 23 | } 24 | return c; 25 | } 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/mac/pmac/pmac_shift_xor.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file pmac_shift_xor.c 7 | PMAC implementation, internal function, by Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_PMAC 11 | 12 | /** 13 | Internal function. Performs the state update (adding correct multiple) 14 | @param pmac The PMAC state. 15 | */ 16 | void pmac_shift_xor(pmac_state *pmac) 17 | { 18 | int x, y; 19 | y = pmac_ntz(pmac->block_index++); 20 | #ifdef LTC_FAST 21 | for (x = 0; x < pmac->block_len; x += sizeof(LTC_FAST_TYPE)) { 22 | *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pmac->Li + x)) ^= 23 | *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pmac->Ls[y] + x)); 24 | } 25 | #else 26 | for (x = 0; x < pmac->block_len; x++) { 27 | pmac->Li[x] ^= pmac->Ls[y][x]; 28 | } 29 | #endif 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/mac/poly1305/poly1305_memory.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /* The implementation is based on: 5 | * Public Domain poly1305 from Andrew Moon 6 | * https://github.com/floodyberry/poly1305-donna 7 | */ 8 | 9 | #include "tomcrypt_private.h" 10 | 11 | #ifdef LTC_POLY1305 12 | 13 | /** 14 | POLY1305 a block of memory to produce the authentication tag 15 | @param key The secret key 16 | @param keylen The length of the secret key (octets) 17 | @param in The data to POLY1305 18 | @param inlen The length of the data to POLY1305 (octets) 19 | @param mac [out] Destination of the authentication tag 20 | @param maclen [in/out] Max size and resulting size of authentication tag 21 | @return CRYPT_OK if successful 22 | */ 23 | int poly1305_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen) 24 | { 25 | poly1305_state st; 26 | int err; 27 | 28 | LTC_ARGCHK(key != NULL); 29 | LTC_ARGCHK(in != NULL); 30 | LTC_ARGCHK(mac != NULL); 31 | LTC_ARGCHK(maclen != NULL); 32 | 33 | if ((err = poly1305_init(&st, key, keylen)) != CRYPT_OK) { goto LBL_ERR; } 34 | if ((err = poly1305_process(&st, in, inlen)) != CRYPT_OK) { goto LBL_ERR; } 35 | err = poly1305_done(&st, mac, maclen); 36 | LBL_ERR: 37 | #ifdef LTC_CLEAN_STACK 38 | zeromem(&st, sizeof(poly1305_state)); 39 | #endif 40 | return err; 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/math/multi.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | #ifdef LTC_MPI 6 | #include 7 | 8 | int ltc_init_multi(void **a, ...) 9 | { 10 | void **cur = a; 11 | int np = 0; 12 | va_list args; 13 | 14 | va_start(args, a); 15 | while (cur != NULL) { 16 | if (mp_init(cur) != CRYPT_OK) { 17 | /* failed */ 18 | va_list clean_list; 19 | 20 | va_start(clean_list, a); 21 | cur = a; 22 | while (np--) { 23 | mp_clear(*cur); 24 | cur = va_arg(clean_list, void**); 25 | } 26 | va_end(clean_list); 27 | va_end(args); 28 | return CRYPT_MEM; 29 | } 30 | ++np; 31 | cur = va_arg(args, void**); 32 | } 33 | va_end(args); 34 | return CRYPT_OK; 35 | } 36 | 37 | void ltc_deinit_multi(void *a, ...) 38 | { 39 | void *cur = a; 40 | va_list args; 41 | 42 | va_start(args, a); 43 | while (cur != NULL) { 44 | mp_clear(cur); 45 | cur = va_arg(args, void *); 46 | } 47 | va_end(args); 48 | } 49 | 50 | void ltc_cleanup_multi(void **a, ...) 51 | { 52 | void **cur = a; 53 | va_list args; 54 | 55 | va_start(args, a); 56 | while (cur != NULL) { 57 | if (*cur != NULL) { 58 | mp_clear(*cur); 59 | *cur = NULL; 60 | } 61 | cur = va_arg(args, void**); 62 | } 63 | va_end(args); 64 | } 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/math/radix_to_bin.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file radix_to_bin.c 7 | Convert data from a specific radix to binary. 8 | Steffen Jaeckel 9 | */ 10 | 11 | /** 12 | Convert data from a specific radix to binary 13 | 14 | The default MPI descriptors #ltm_desc, #tfm_desc and #gmp_desc 15 | have the following restrictions on parameters: 16 | 17 | \p in - NUL-terminated char buffer 18 | 19 | \p radix - 2..64 20 | 21 | @param in The input 22 | @param radix The radix of the input 23 | @param out The output buffer 24 | @param len [in/out] The length of the output buffer 25 | 26 | @return CRYPT_OK on success. 27 | */ 28 | int radix_to_bin(const void *in, int radix, void *out, unsigned long *len) 29 | { 30 | unsigned long l; 31 | void* mpi; 32 | int err; 33 | 34 | LTC_ARGCHK(in != NULL); 35 | LTC_ARGCHK(len != NULL); 36 | 37 | if ((err = mp_init(&mpi)) != CRYPT_OK) return err; 38 | if ((err = mp_read_radix(mpi, in, radix)) != CRYPT_OK) goto LBL_ERR; 39 | 40 | if ((l = mp_unsigned_bin_size(mpi)) > *len) { 41 | *len = l; 42 | err = CRYPT_BUFFER_OVERFLOW; 43 | goto LBL_ERR; 44 | } 45 | *len = l; 46 | 47 | if ((err = mp_to_unsigned_bin(mpi, out)) != CRYPT_OK) goto LBL_ERR; 48 | 49 | LBL_ERR: 50 | mp_clear(mpi); 51 | return err; 52 | } 53 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/burn_stack.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file burn_stack.c 7 | Burn stack, Tom St Denis 8 | */ 9 | 10 | /** 11 | Burn some stack memory 12 | @param len amount of stack to burn in bytes 13 | */ 14 | void burn_stack(unsigned long len) 15 | { 16 | unsigned char buf[32]; 17 | zeromem(buf, sizeof(buf)); 18 | if (len > (unsigned long)sizeof(buf)) { 19 | burn_stack(len - sizeof(buf)); 20 | } 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/copy_or_zeromem.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file copy_or_zeromem.c 7 | Either copy or zero a block of memory in constant time, Steffen Jaeckel 8 | */ 9 | 10 | /** 11 | Either copy or zero a block of memory in constant time 12 | @param src The source where to read from 13 | @param dest The destination where to write to 14 | @param len The length of the area to process (octets) 15 | @param coz Copy (on 0) Or Zero (> 0) 16 | */ 17 | void copy_or_zeromem(const unsigned char* src, unsigned char* dest, unsigned long len, int coz) 18 | { 19 | unsigned long y; 20 | #ifdef LTC_FAST 21 | unsigned long z; 22 | LTC_FAST_TYPE fastMask = ~(LTC_FAST_TYPE)0; /* initialize fastMask at all ones */ 23 | #endif 24 | unsigned char mask = 0xff; /* initialize mask at all ones */ 25 | 26 | LTC_ARGCHKVD(src != NULL); 27 | LTC_ARGCHKVD(dest != NULL); 28 | 29 | if (coz != 0) coz = 1; 30 | y = 0; 31 | mask *= 1 - coz; /* mask = ( coz ? 0 : 0xff ) */ 32 | #ifdef LTC_FAST 33 | fastMask *= 1 - coz; 34 | if (len & ~15) { 35 | for (; y < (len & ~15); y += 16) { 36 | for (z = 0; z < 16; z += sizeof(LTC_FAST_TYPE)) { 37 | *(LTC_FAST_TYPE_PTR_CAST(&dest[y+z])) = *(LTC_FAST_TYPE_PTR_CAST(&src[y+z])) & fastMask; 38 | } 39 | } 40 | } 41 | #endif 42 | for (; y < len; y++) { 43 | dest[y] = src[y] & mask; 44 | } 45 | #ifdef LTC_CLEAN_STACK 46 | #ifdef LTC_FAST 47 | fastMask = 0; 48 | #endif 49 | mask = 0; 50 | #endif 51 | } 52 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_argchk.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_argchk.c 7 | Perform argument checking, Tom St Denis 8 | */ 9 | 10 | #if (ARGTYPE == 0) 11 | void crypt_argchk(const char *v, const char *s, int d) 12 | { 13 | fprintf(stderr, "LTC_ARGCHK '%s' failure on line %d of file %s\n", 14 | v, d, s); 15 | abort(); 16 | } 17 | #endif 18 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_cipher_descriptor.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_cipher_descriptor.c 7 | Stores the cipher descriptor table, Tom St Denis 8 | */ 9 | 10 | struct ltc_cipher_descriptor cipher_descriptor[TAB_SIZE] = { 11 | { NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } 12 | }; 13 | 14 | LTC_MUTEX_GLOBAL(ltc_cipher_mutex) 15 | 16 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_cipher_is_valid.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_cipher_is_valid.c 7 | Determine if cipher is valid, Tom St Denis 8 | */ 9 | 10 | /* 11 | Test if a cipher index is valid 12 | @param idx The index of the cipher to search for 13 | @return CRYPT_OK if valid 14 | */ 15 | int cipher_is_valid(int idx) 16 | { 17 | LTC_MUTEX_LOCK(<c_cipher_mutex); 18 | if (idx < 0 || idx >= TAB_SIZE || cipher_descriptor[idx].name == NULL) { 19 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 20 | return CRYPT_INVALID_CIPHER; 21 | } 22 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 23 | return CRYPT_OK; 24 | } 25 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_find_cipher.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_find_cipher.c 7 | Find a cipher in the descriptor tables, Tom St Denis 8 | */ 9 | 10 | /** 11 | Find a registered cipher by name 12 | @param name The name of the cipher to look for 13 | @return >= 0 if found, -1 if not present 14 | */ 15 | int find_cipher(const char *name) 16 | { 17 | int x; 18 | LTC_ARGCHK(name != NULL); 19 | LTC_MUTEX_LOCK(<c_cipher_mutex); 20 | for (x = 0; x < TAB_SIZE; x++) { 21 | if (cipher_descriptor[x].name != NULL && !XSTRCMP(cipher_descriptor[x].name, name)) { 22 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 23 | return x; 24 | } 25 | } 26 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 27 | return -1; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_find_cipher_any.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_find_cipher_any.c 7 | Find a cipher in the descriptor tables, Tom St Denis 8 | */ 9 | 10 | /** 11 | Find a cipher flexibly. First by name then if not present by block and key size 12 | @param name The name of the cipher desired 13 | @param blocklen The minimum length of the block cipher desired (octets) 14 | @param keylen The minimum length of the key size desired (octets) 15 | @return >= 0 if found, -1 if not present 16 | */ 17 | int find_cipher_any(const char *name, int blocklen, int keylen) 18 | { 19 | int x; 20 | 21 | if(name != NULL) { 22 | x = find_cipher(name); 23 | if (x != -1) return x; 24 | } 25 | 26 | LTC_MUTEX_LOCK(<c_cipher_mutex); 27 | for (x = 0; x < TAB_SIZE; x++) { 28 | if (cipher_descriptor[x].name == NULL) { 29 | continue; 30 | } 31 | if (blocklen <= (int)cipher_descriptor[x].block_length && keylen <= (int)cipher_descriptor[x].max_key_length) { 32 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 33 | return x; 34 | } 35 | } 36 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 37 | return -1; 38 | } 39 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_find_cipher_id.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_find_cipher_id.c 7 | Find cipher by ID, Tom St Denis 8 | */ 9 | 10 | /** 11 | Find a cipher by ID number 12 | @param ID The ID (not same as index) of the cipher to find 13 | @return >= 0 if found, -1 if not present 14 | */ 15 | int find_cipher_id(unsigned char ID) 16 | { 17 | int x; 18 | LTC_MUTEX_LOCK(<c_cipher_mutex); 19 | for (x = 0; x < TAB_SIZE; x++) { 20 | if (cipher_descriptor[x].ID == ID) { 21 | x = (cipher_descriptor[x].name == NULL) ? -1 : x; 22 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 23 | return x; 24 | } 25 | } 26 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 27 | return -1; 28 | } 29 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_find_hash.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_find_hash.c 7 | Find a hash, Tom St Denis 8 | */ 9 | 10 | /** 11 | Find a registered hash by name 12 | @param name The name of the hash to look for 13 | @return >= 0 if found, -1 if not present 14 | */ 15 | int find_hash(const char *name) 16 | { 17 | int x; 18 | LTC_ARGCHK(name != NULL); 19 | LTC_MUTEX_LOCK(<c_hash_mutex); 20 | for (x = 0; x < TAB_SIZE; x++) { 21 | if (hash_descriptor[x].name != NULL && XSTRCMP(hash_descriptor[x].name, name) == 0) { 22 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 23 | return x; 24 | } 25 | } 26 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 27 | return -1; 28 | } 29 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_find_hash_any.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_find_hash_any.c 7 | Find a hash, Tom St Denis 8 | */ 9 | 10 | /** 11 | Find a hash flexibly. First by name then if not present by digest size 12 | @param name The name of the hash desired 13 | @param digestlen The minimum length of the digest size (octets) 14 | @return >= 0 if found, -1 if not present 15 | */int find_hash_any(const char *name, int digestlen) 16 | { 17 | int x, y, z; 18 | LTC_ARGCHK(name != NULL); 19 | 20 | x = find_hash(name); 21 | if (x != -1) return x; 22 | 23 | LTC_MUTEX_LOCK(<c_hash_mutex); 24 | y = MAXBLOCKSIZE+1; 25 | z = -1; 26 | for (x = 0; x < TAB_SIZE; x++) { 27 | if (hash_descriptor[x].name == NULL) { 28 | continue; 29 | } 30 | if ((int)hash_descriptor[x].hashsize >= digestlen && (int)hash_descriptor[x].hashsize < y) { 31 | z = x; 32 | y = hash_descriptor[x].hashsize; 33 | } 34 | } 35 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 36 | return z; 37 | } 38 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_find_hash_id.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_find_hash_id.c 7 | Find hash by ID, Tom St Denis 8 | */ 9 | 10 | /** 11 | Find a hash by ID number 12 | @param ID The ID (not same as index) of the hash to find 13 | @return >= 0 if found, -1 if not present 14 | */ 15 | int find_hash_id(unsigned char ID) 16 | { 17 | int x; 18 | LTC_MUTEX_LOCK(<c_hash_mutex); 19 | for (x = 0; x < TAB_SIZE; x++) { 20 | if (hash_descriptor[x].ID == ID) { 21 | x = (hash_descriptor[x].name == NULL) ? -1 : x; 22 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 23 | return x; 24 | } 25 | } 26 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 27 | return -1; 28 | } 29 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_find_hash_oid.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_find_hash_oid.c 7 | Find a hash, Tom St Denis 8 | */ 9 | 10 | int find_hash_oid(const unsigned long *ID, unsigned long IDlen) 11 | { 12 | int x; 13 | LTC_ARGCHK(ID != NULL); 14 | LTC_MUTEX_LOCK(<c_hash_mutex); 15 | for (x = 0; x < TAB_SIZE; x++) { 16 | if (hash_descriptor[x].name != NULL && hash_descriptor[x].OIDlen == IDlen && !XMEMCMP(hash_descriptor[x].OID, ID, sizeof(unsigned long) * IDlen)) { 17 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 18 | return x; 19 | } 20 | } 21 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 22 | return -1; 23 | } 24 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_find_prng.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_find_prng.c 7 | Find a PRNG, Tom St Denis 8 | */ 9 | 10 | /** 11 | Find a registered PRNG by name 12 | @param name The name of the PRNG to look for 13 | @return >= 0 if found, -1 if not present 14 | */ 15 | int find_prng(const char *name) 16 | { 17 | int x; 18 | LTC_ARGCHK(name != NULL); 19 | LTC_MUTEX_LOCK(<c_prng_mutex); 20 | for (x = 0; x < TAB_SIZE; x++) { 21 | if ((prng_descriptor[x].name != NULL) && XSTRCMP(prng_descriptor[x].name, name) == 0) { 22 | LTC_MUTEX_UNLOCK(<c_prng_mutex); 23 | return x; 24 | } 25 | } 26 | LTC_MUTEX_UNLOCK(<c_prng_mutex); 27 | return -1; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_fsa.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | #include 5 | 6 | /** 7 | @file crypt_fsa.c 8 | LibTomCrypt FULL SPEED AHEAD!, Tom St Denis 9 | */ 10 | 11 | /* format is ltc_mp, cipher_desc, [cipher_desc], NULL, hash_desc, [hash_desc], NULL, prng_desc, [prng_desc], NULL */ 12 | int crypt_fsa(void *mp, ...) 13 | { 14 | va_list args; 15 | void *p; 16 | 17 | va_start(args, mp); 18 | if (mp != NULL) { 19 | XMEMCPY(<c_mp, mp, sizeof(ltc_mp)); 20 | } 21 | 22 | while ((p = va_arg(args, void*)) != NULL) { 23 | if (register_cipher(p) == -1) { 24 | va_end(args); 25 | return CRYPT_INVALID_CIPHER; 26 | } 27 | } 28 | 29 | while ((p = va_arg(args, void*)) != NULL) { 30 | if (register_hash(p) == -1) { 31 | va_end(args); 32 | return CRYPT_INVALID_HASH; 33 | } 34 | } 35 | 36 | while ((p = va_arg(args, void*)) != NULL) { 37 | if (register_prng(p) == -1) { 38 | va_end(args); 39 | return CRYPT_INVALID_PRNG; 40 | } 41 | } 42 | 43 | va_end(args); 44 | return CRYPT_OK; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_hash_descriptor.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_hash_descriptor.c 7 | Stores the hash descriptor table, Tom St Denis 8 | */ 9 | 10 | struct ltc_hash_descriptor hash_descriptor[TAB_SIZE] = { 11 | { NULL, 0, 0, 0, { 0 }, 0, NULL, NULL, NULL, NULL, NULL } 12 | }; 13 | 14 | LTC_MUTEX_GLOBAL(ltc_hash_mutex) 15 | 16 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_hash_is_valid.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_hash_is_valid.c 7 | Determine if hash is valid, Tom St Denis 8 | */ 9 | 10 | /* 11 | Test if a hash index is valid 12 | @param idx The index of the hash to search for 13 | @return CRYPT_OK if valid 14 | */ 15 | int hash_is_valid(int idx) 16 | { 17 | LTC_MUTEX_LOCK(<c_hash_mutex); 18 | if (idx < 0 || idx >= TAB_SIZE || hash_descriptor[idx].name == NULL) { 19 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 20 | return CRYPT_INVALID_HASH; 21 | } 22 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 23 | return CRYPT_OK; 24 | } 25 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_ltc_mp_descriptor.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /* Initialize ltc_mp to nulls, to force allocation on all platforms, including macOS. */ 6 | ltc_math_descriptor ltc_mp = { 0 }; 7 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_prng_descriptor.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_prng_descriptor.c 7 | Stores the PRNG descriptors, Tom St Denis 8 | */ 9 | struct ltc_prng_descriptor prng_descriptor[TAB_SIZE] = { 10 | { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } 11 | }; 12 | 13 | LTC_MUTEX_GLOBAL(ltc_prng_mutex) 14 | 15 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_prng_is_valid.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_prng_is_valid.c 7 | Determine if PRNG is valid, Tom St Denis 8 | */ 9 | 10 | /* 11 | Test if a PRNG index is valid 12 | @param idx The index of the PRNG to search for 13 | @return CRYPT_OK if valid 14 | */ 15 | int prng_is_valid(int idx) 16 | { 17 | LTC_MUTEX_LOCK(<c_prng_mutex); 18 | if (idx < 0 || idx >= TAB_SIZE || prng_descriptor[idx].name == NULL) { 19 | LTC_MUTEX_UNLOCK(<c_prng_mutex); 20 | return CRYPT_INVALID_PRNG; 21 | } 22 | LTC_MUTEX_UNLOCK(<c_prng_mutex); 23 | return CRYPT_OK; 24 | } 25 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_prng_rng_descriptor.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | #ifdef LTC_PRNG_ENABLE_LTC_RNG 6 | unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void)); 7 | #endif 8 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_register_all_prngs.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | /** 7 | @file crypt_register_all_prngs.c 8 | 9 | Steffen Jaeckel 10 | */ 11 | 12 | #define REGISTER_PRNG(h) do {\ 13 | LTC_ARGCHK(register_prng(h) != -1); \ 14 | } while(0) 15 | 16 | int register_all_prngs(void) 17 | { 18 | #ifdef LTC_YARROW 19 | REGISTER_PRNG(&yarrow_desc); 20 | #endif 21 | #ifdef LTC_FORTUNA 22 | REGISTER_PRNG(&fortuna_desc); 23 | #endif 24 | #ifdef LTC_RC4 25 | REGISTER_PRNG(&rc4_desc); 26 | #endif 27 | #ifdef LTC_CHACHA20_PRNG 28 | REGISTER_PRNG(&chacha20_prng_desc); 29 | #endif 30 | #ifdef LTC_SOBER128 31 | REGISTER_PRNG(&sober128_desc); 32 | #endif 33 | #ifdef LTC_SPRNG 34 | REGISTER_PRNG(&sprng_desc); 35 | #endif 36 | 37 | return CRYPT_OK; 38 | } 39 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_register_cipher.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_register_cipher.c 7 | Register a cipher, Tom St Denis 8 | */ 9 | 10 | /** 11 | Register a cipher with the descriptor table 12 | @param cipher The cipher you wish to register 13 | @return value >= 0 if successfully added (or already present), -1 if unsuccessful 14 | */ 15 | int register_cipher(const struct ltc_cipher_descriptor *cipher) 16 | { 17 | int x; 18 | 19 | LTC_ARGCHK(cipher != NULL); 20 | 21 | /* is it already registered? */ 22 | LTC_MUTEX_LOCK(<c_cipher_mutex); 23 | for (x = 0; x < TAB_SIZE; x++) { 24 | if (cipher_descriptor[x].name != NULL && cipher_descriptor[x].ID == cipher->ID) { 25 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 26 | return x; 27 | } 28 | } 29 | 30 | /* find a blank spot */ 31 | for (x = 0; x < TAB_SIZE; x++) { 32 | if (cipher_descriptor[x].name == NULL) { 33 | XMEMCPY(&cipher_descriptor[x], cipher, sizeof(struct ltc_cipher_descriptor)); 34 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 35 | return x; 36 | } 37 | } 38 | 39 | /* no spot */ 40 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 41 | return -1; 42 | } 43 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_register_hash.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_register_hash.c 7 | Register a HASH, Tom St Denis 8 | */ 9 | 10 | /** 11 | Register a hash with the descriptor table 12 | @param hash The hash you wish to register 13 | @return value >= 0 if successfully added (or already present), -1 if unsuccessful 14 | */ 15 | int register_hash(const struct ltc_hash_descriptor *hash) 16 | { 17 | int x; 18 | 19 | LTC_ARGCHK(hash != NULL); 20 | 21 | /* is it already registered? */ 22 | LTC_MUTEX_LOCK(<c_hash_mutex); 23 | for (x = 0; x < TAB_SIZE; x++) { 24 | if (XMEMCMP(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor)) == 0) { 25 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 26 | return x; 27 | } 28 | } 29 | 30 | /* find a blank spot */ 31 | for (x = 0; x < TAB_SIZE; x++) { 32 | if (hash_descriptor[x].name == NULL) { 33 | XMEMCPY(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor)); 34 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 35 | return x; 36 | } 37 | } 38 | 39 | /* no spot */ 40 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 41 | return -1; 42 | } 43 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_register_prng.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_register_prng.c 7 | Register a PRNG, Tom St Denis 8 | */ 9 | 10 | /** 11 | Register a PRNG with the descriptor table 12 | @param prng The PRNG you wish to register 13 | @return value >= 0 if successfully added (or already present), -1 if unsuccessful 14 | */ 15 | int register_prng(const struct ltc_prng_descriptor *prng) 16 | { 17 | int x; 18 | 19 | LTC_ARGCHK(prng != NULL); 20 | 21 | /* is it already registered? */ 22 | LTC_MUTEX_LOCK(<c_prng_mutex); 23 | for (x = 0; x < TAB_SIZE; x++) { 24 | if (XMEMCMP(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor)) == 0) { 25 | LTC_MUTEX_UNLOCK(<c_prng_mutex); 26 | return x; 27 | } 28 | } 29 | 30 | /* find a blank spot */ 31 | for (x = 0; x < TAB_SIZE; x++) { 32 | if (prng_descriptor[x].name == NULL) { 33 | XMEMCPY(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor)); 34 | LTC_MUTEX_UNLOCK(<c_prng_mutex); 35 | return x; 36 | } 37 | } 38 | 39 | /* no spot */ 40 | LTC_MUTEX_UNLOCK(<c_prng_mutex); 41 | return -1; 42 | } 43 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_unregister_cipher.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_unregister_cipher.c 7 | Unregister a cipher, Tom St Denis 8 | */ 9 | 10 | /** 11 | Unregister a cipher from the descriptor table 12 | @param cipher The cipher descriptor to remove 13 | @return CRYPT_OK on success 14 | */ 15 | int unregister_cipher(const struct ltc_cipher_descriptor *cipher) 16 | { 17 | int x; 18 | 19 | LTC_ARGCHK(cipher != NULL); 20 | 21 | /* is it already registered? */ 22 | LTC_MUTEX_LOCK(<c_cipher_mutex); 23 | for (x = 0; x < TAB_SIZE; x++) { 24 | if (XMEMCMP(&cipher_descriptor[x], cipher, sizeof(struct ltc_cipher_descriptor)) == 0) { 25 | cipher_descriptor[x].name = NULL; 26 | cipher_descriptor[x].ID = 255; 27 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 28 | return CRYPT_OK; 29 | } 30 | } 31 | LTC_MUTEX_UNLOCK(<c_cipher_mutex); 32 | return CRYPT_ERROR; 33 | } 34 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_unregister_hash.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_unregister_hash.c 7 | Unregister a hash, Tom St Denis 8 | */ 9 | 10 | /** 11 | Unregister a hash from the descriptor table 12 | @param hash The hash descriptor to remove 13 | @return CRYPT_OK on success 14 | */ 15 | int unregister_hash(const struct ltc_hash_descriptor *hash) 16 | { 17 | int x; 18 | 19 | LTC_ARGCHK(hash != NULL); 20 | 21 | /* is it already registered? */ 22 | LTC_MUTEX_LOCK(<c_hash_mutex); 23 | for (x = 0; x < TAB_SIZE; x++) { 24 | if (XMEMCMP(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor)) == 0) { 25 | hash_descriptor[x].name = NULL; 26 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 27 | return CRYPT_OK; 28 | } 29 | } 30 | LTC_MUTEX_UNLOCK(<c_hash_mutex); 31 | return CRYPT_ERROR; 32 | } 33 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/crypt/crypt_unregister_prng.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file crypt_unregister_prng.c 7 | Unregister a PRNG, Tom St Denis 8 | */ 9 | 10 | /** 11 | Unregister a PRNG from the descriptor table 12 | @param prng The PRNG descriptor to remove 13 | @return CRYPT_OK on success 14 | */ 15 | int unregister_prng(const struct ltc_prng_descriptor *prng) 16 | { 17 | int x; 18 | 19 | LTC_ARGCHK(prng != NULL); 20 | 21 | /* is it already registered? */ 22 | LTC_MUTEX_LOCK(<c_prng_mutex); 23 | for (x = 0; x < TAB_SIZE; x++) { 24 | if (XMEMCMP(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor)) == 0) { 25 | prng_descriptor[x].name = NULL; 26 | LTC_MUTEX_UNLOCK(<c_prng_mutex); 27 | return CRYPT_OK; 28 | } 29 | } 30 | LTC_MUTEX_UNLOCK(<c_prng_mutex); 31 | return CRYPT_ERROR; 32 | } 33 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/mem_neq.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file mem_neq.c 7 | Compare two blocks of memory for inequality in constant time. 8 | Steffen Jaeckel 9 | */ 10 | 11 | /** 12 | Compare two blocks of memory for inequality in constant time. 13 | 14 | The usage is similar to that of standard memcmp, but you can only test 15 | if the memory is equal or not - you can not determine by how much the 16 | first different byte differs. 17 | 18 | This function shall be used to compare results of cryptographic 19 | operations where inequality means most likely usage of a wrong key. 20 | The execution time has therefore to be constant as otherwise 21 | timing attacks could be possible. 22 | 23 | @param a The first memory region 24 | @param b The second memory region 25 | @param len The length of the area to compare (octets) 26 | 27 | @return 0 when a and b are equal for len bytes, 1 they are not equal. 28 | */ 29 | int mem_neq(const void *a, const void *b, size_t len) 30 | { 31 | unsigned char ret = 0; 32 | const unsigned char* pa; 33 | const unsigned char* pb; 34 | 35 | LTC_ARGCHK(a != NULL); 36 | LTC_ARGCHK(b != NULL); 37 | 38 | pa = a; 39 | pb = b; 40 | 41 | while (len-- > 0) { 42 | ret |= *pa ^ *pb; 43 | ++pa; 44 | ++pb; 45 | } 46 | 47 | ret |= ret >> 4; 48 | ret |= ret >> 2; 49 | ret |= ret >> 1; 50 | ret &= 1; 51 | 52 | return ret; 53 | } 54 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/misc/zeromem.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file zeromem.c 7 | Zero a block of memory, Tom St Denis 8 | */ 9 | 10 | /** 11 | Zero a block of memory 12 | @param out The destination of the area to zero 13 | @param outlen The length of the area to zero (octets) 14 | */ 15 | void zeromem(volatile void *out, size_t outlen) 16 | { 17 | volatile char *mem = out; 18 | LTC_ARGCHKVD(out != NULL); 19 | while (outlen-- > 0) { 20 | *mem++ = '\0'; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/cbc/cbc_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file cbc_done.c 7 | CBC implementation, finish chain, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CBC_MODE 11 | 12 | /** Terminate the chain 13 | @param cbc The CBC chain to terminate 14 | @return CRYPT_OK on success 15 | */ 16 | int cbc_done(symmetric_CBC *cbc) 17 | { 18 | int err; 19 | LTC_ARGCHK(cbc != NULL); 20 | 21 | if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) { 22 | return err; 23 | } 24 | cipher_descriptor[cbc->cipher].done(&cbc->key); 25 | return CRYPT_OK; 26 | } 27 | 28 | 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/cbc/cbc_getiv.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file cbc_getiv.c 7 | CBC implementation, get IV, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CBC_MODE 11 | 12 | /** 13 | Get the current initialization vector 14 | @param IV [out] The destination of the initialization vector 15 | @param len [in/out] The max size and resulting size of the initialization vector 16 | @param cbc The CBC state 17 | @return CRYPT_OK if successful 18 | */ 19 | int cbc_getiv(unsigned char *IV, unsigned long *len, const symmetric_CBC *cbc) 20 | { 21 | LTC_ARGCHK(IV != NULL); 22 | LTC_ARGCHK(len != NULL); 23 | LTC_ARGCHK(cbc != NULL); 24 | if ((unsigned long)cbc->blocklen > *len) { 25 | *len = cbc->blocklen; 26 | return CRYPT_BUFFER_OVERFLOW; 27 | } 28 | XMEMCPY(IV, cbc->IV, cbc->blocklen); 29 | *len = cbc->blocklen; 30 | 31 | return CRYPT_OK; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/cbc/cbc_setiv.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file cbc_setiv.c 7 | CBC implementation, set IV, Tom St Denis 8 | */ 9 | 10 | 11 | #ifdef LTC_CBC_MODE 12 | 13 | /** 14 | Set an initialization vector 15 | @param IV The initialization vector 16 | @param len The length of the vector (in octets) 17 | @param cbc The CBC state 18 | @return CRYPT_OK if successful 19 | */ 20 | int cbc_setiv(const unsigned char *IV, unsigned long len, symmetric_CBC *cbc) 21 | { 22 | LTC_ARGCHK(IV != NULL); 23 | LTC_ARGCHK(cbc != NULL); 24 | if (len != (unsigned long)cbc->blocklen) { 25 | return CRYPT_INVALID_ARG; 26 | } 27 | XMEMCPY(cbc->IV, IV, len); 28 | return CRYPT_OK; 29 | } 30 | 31 | #endif 32 | 33 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/cbc/cbc_start.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file cbc_start.c 7 | CBC implementation, start chain, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CBC_MODE 11 | 12 | /** 13 | Initialize a CBC context 14 | @param cipher The index of the cipher desired 15 | @param IV The initialization vector 16 | @param key The secret key 17 | @param keylen The length of the secret key (octets) 18 | @param num_rounds Number of rounds in the cipher desired (0 for default) 19 | @param cbc The CBC state to initialize 20 | @return CRYPT_OK if successful 21 | */ 22 | int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key, 23 | int keylen, int num_rounds, symmetric_CBC *cbc) 24 | { 25 | int x, err; 26 | 27 | LTC_ARGCHK(IV != NULL); 28 | LTC_ARGCHK(key != NULL); 29 | LTC_ARGCHK(cbc != NULL); 30 | 31 | /* bad param? */ 32 | if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { 33 | return err; 34 | } 35 | 36 | /* setup cipher */ 37 | if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &cbc->key)) != CRYPT_OK) { 38 | return err; 39 | } 40 | 41 | /* copy IV */ 42 | cbc->blocklen = cipher_descriptor[cipher].block_length; 43 | cbc->cipher = cipher; 44 | for (x = 0; x < cbc->blocklen; x++) { 45 | cbc->IV[x] = IV[x]; 46 | } 47 | return CRYPT_OK; 48 | } 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/cfb/cfb_decrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file cfb_decrypt.c 7 | CFB implementation, decrypt data, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CFB_MODE 11 | 12 | /** 13 | CFB decrypt 14 | @param ct Ciphertext 15 | @param pt [out] Plaintext 16 | @param len Length of ciphertext (octets) 17 | @param cfb CFB state 18 | @return CRYPT_OK if successful 19 | */ 20 | int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CFB *cfb) 21 | { 22 | int err; 23 | 24 | LTC_ARGCHK(pt != NULL); 25 | LTC_ARGCHK(ct != NULL); 26 | LTC_ARGCHK(cfb != NULL); 27 | 28 | if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) { 29 | return err; 30 | } 31 | 32 | /* is blocklen/padlen valid? */ 33 | if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) || 34 | cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) { 35 | return CRYPT_INVALID_ARG; 36 | } 37 | 38 | while (len-- > 0) { 39 | if (cfb->padlen == cfb->blocklen) { 40 | if ((err = cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key)) != CRYPT_OK) { 41 | return err; 42 | } 43 | cfb->padlen = 0; 44 | } 45 | cfb->pad[cfb->padlen] = *ct; 46 | *pt = *ct ^ cfb->IV[cfb->padlen]; 47 | ++pt; 48 | ++ct; 49 | ++(cfb->padlen); 50 | } 51 | return CRYPT_OK; 52 | } 53 | 54 | #endif 55 | 56 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/cfb/cfb_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file cfb_done.c 7 | CFB implementation, finish chain, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CFB_MODE 11 | 12 | /** Terminate the chain 13 | @param cfb The CFB chain to terminate 14 | @return CRYPT_OK on success 15 | */ 16 | int cfb_done(symmetric_CFB *cfb) 17 | { 18 | int err; 19 | LTC_ARGCHK(cfb != NULL); 20 | 21 | if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) { 22 | return err; 23 | } 24 | cipher_descriptor[cfb->cipher].done(&cfb->key); 25 | return CRYPT_OK; 26 | } 27 | 28 | 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/cfb/cfb_encrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file cfb_encrypt.c 7 | CFB implementation, encrypt data, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CFB_MODE 11 | 12 | /** 13 | CFB encrypt 14 | @param pt Plaintext 15 | @param ct [out] Ciphertext 16 | @param len Length of plaintext (octets) 17 | @param cfb CFB state 18 | @return CRYPT_OK if successful 19 | */ 20 | int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CFB *cfb) 21 | { 22 | int err; 23 | 24 | LTC_ARGCHK(pt != NULL); 25 | LTC_ARGCHK(ct != NULL); 26 | LTC_ARGCHK(cfb != NULL); 27 | 28 | if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) { 29 | return err; 30 | } 31 | 32 | /* is blocklen/padlen valid? */ 33 | if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) || 34 | cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) { 35 | return CRYPT_INVALID_ARG; 36 | } 37 | 38 | while (len-- > 0) { 39 | if (cfb->padlen == cfb->blocklen) { 40 | if ((err = cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key)) != CRYPT_OK) { 41 | return err; 42 | } 43 | cfb->padlen = 0; 44 | } 45 | cfb->pad[cfb->padlen] = (*ct = *pt ^ cfb->IV[cfb->padlen]); 46 | ++pt; 47 | ++ct; 48 | ++(cfb->padlen); 49 | } 50 | return CRYPT_OK; 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/cfb/cfb_getiv.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file cfb_getiv.c 7 | CFB implementation, get IV, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CFB_MODE 11 | 12 | /** 13 | Get the current initialization vector 14 | @param IV [out] The destination of the initialization vector 15 | @param len [in/out] The max size and resulting size of the initialization vector 16 | @param cfb The CFB state 17 | @return CRYPT_OK if successful 18 | */ 19 | int cfb_getiv(unsigned char *IV, unsigned long *len, const symmetric_CFB *cfb) 20 | { 21 | LTC_ARGCHK(IV != NULL); 22 | LTC_ARGCHK(len != NULL); 23 | LTC_ARGCHK(cfb != NULL); 24 | if ((unsigned long)cfb->blocklen > *len) { 25 | *len = cfb->blocklen; 26 | return CRYPT_BUFFER_OVERFLOW; 27 | } 28 | XMEMCPY(IV, cfb->IV, cfb->blocklen); 29 | *len = cfb->blocklen; 30 | 31 | return CRYPT_OK; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/cfb/cfb_setiv.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file cfb_setiv.c 7 | CFB implementation, set IV, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CFB_MODE 11 | 12 | /** 13 | Set an initialization vector 14 | @param IV The initialization vector 15 | @param len The length of the vector (in octets) 16 | @param cfb The CFB state 17 | @return CRYPT_OK if successful 18 | */ 19 | int cfb_setiv(const unsigned char *IV, unsigned long len, symmetric_CFB *cfb) 20 | { 21 | int err; 22 | 23 | LTC_ARGCHK(IV != NULL); 24 | LTC_ARGCHK(cfb != NULL); 25 | 26 | if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) { 27 | return err; 28 | } 29 | 30 | if (len != (unsigned long)cfb->blocklen) { 31 | return CRYPT_INVALID_ARG; 32 | } 33 | 34 | /* force next block */ 35 | cfb->padlen = 0; 36 | return cipher_descriptor[cfb->cipher].ecb_encrypt(IV, cfb->IV, &cfb->key); 37 | } 38 | 39 | #endif 40 | 41 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/cfb/cfb_start.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file cfb_start.c 7 | CFB implementation, start chain, Tom St Denis 8 | */ 9 | 10 | 11 | #ifdef LTC_CFB_MODE 12 | 13 | /** 14 | Initialize a CFB context 15 | @param cipher The index of the cipher desired 16 | @param IV The initialization vector 17 | @param key The secret key 18 | @param keylen The length of the secret key (octets) 19 | @param num_rounds Number of rounds in the cipher desired (0 for default) 20 | @param cfb The CFB state to initialize 21 | @return CRYPT_OK if successful 22 | */ 23 | int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key, 24 | int keylen, int num_rounds, symmetric_CFB *cfb) 25 | { 26 | int x, err; 27 | 28 | LTC_ARGCHK(IV != NULL); 29 | LTC_ARGCHK(key != NULL); 30 | LTC_ARGCHK(cfb != NULL); 31 | 32 | if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { 33 | return err; 34 | } 35 | 36 | 37 | /* copy data */ 38 | cfb->cipher = cipher; 39 | cfb->blocklen = cipher_descriptor[cipher].block_length; 40 | for (x = 0; x < cfb->blocklen; x++) { 41 | cfb->IV[x] = IV[x]; 42 | } 43 | 44 | /* init the cipher */ 45 | if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &cfb->key)) != CRYPT_OK) { 46 | return err; 47 | } 48 | 49 | /* encrypt the IV */ 50 | cfb->padlen = 0; 51 | return cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->IV, cfb->IV, &cfb->key); 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ctr/ctr_decrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ctr_decrypt.c 7 | CTR implementation, decrypt data, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CTR_MODE 11 | 12 | /** 13 | CTR decrypt 14 | @param ct Ciphertext 15 | @param pt [out] Plaintext 16 | @param len Length of ciphertext (octets) 17 | @param ctr CTR state 18 | @return CRYPT_OK if successful 19 | */ 20 | int ctr_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CTR *ctr) 21 | { 22 | LTC_ARGCHK(pt != NULL); 23 | LTC_ARGCHK(ct != NULL); 24 | LTC_ARGCHK(ctr != NULL); 25 | 26 | return ctr_encrypt(ct, pt, len, ctr); 27 | } 28 | 29 | #endif 30 | 31 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ctr/ctr_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ctr_done.c 7 | CTR implementation, finish chain, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CTR_MODE 11 | 12 | /** Terminate the chain 13 | @param ctr The CTR chain to terminate 14 | @return CRYPT_OK on success 15 | */ 16 | int ctr_done(symmetric_CTR *ctr) 17 | { 18 | int err; 19 | LTC_ARGCHK(ctr != NULL); 20 | 21 | if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) { 22 | return err; 23 | } 24 | cipher_descriptor[ctr->cipher].done(&ctr->key); 25 | return CRYPT_OK; 26 | } 27 | 28 | 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ctr/ctr_getiv.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ctr_getiv.c 7 | CTR implementation, get IV, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CTR_MODE 11 | 12 | /** 13 | Get the current initialization vector 14 | @param IV [out] The destination of the initialization vector 15 | @param len [in/out] The max size and resulting size of the initialization vector 16 | @param ctr The CTR state 17 | @return CRYPT_OK if successful 18 | */ 19 | int ctr_getiv(unsigned char *IV, unsigned long *len, const symmetric_CTR *ctr) 20 | { 21 | LTC_ARGCHK(IV != NULL); 22 | LTC_ARGCHK(len != NULL); 23 | LTC_ARGCHK(ctr != NULL); 24 | if ((unsigned long)ctr->blocklen > *len) { 25 | *len = ctr->blocklen; 26 | return CRYPT_BUFFER_OVERFLOW; 27 | } 28 | XMEMCPY(IV, ctr->ctr, ctr->blocklen); 29 | *len = ctr->blocklen; 30 | 31 | return CRYPT_OK; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ctr/ctr_setiv.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ctr_setiv.c 7 | CTR implementation, set IV, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_CTR_MODE 11 | 12 | /** 13 | Set an initialization vector 14 | @param IV The initialization vector 15 | @param len The length of the vector (in octets) 16 | @param ctr The CTR state 17 | @return CRYPT_OK if successful 18 | */ 19 | int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr) 20 | { 21 | int err; 22 | 23 | LTC_ARGCHK(IV != NULL); 24 | LTC_ARGCHK(ctr != NULL); 25 | 26 | /* bad param? */ 27 | if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) { 28 | return err; 29 | } 30 | 31 | if (len != (unsigned long)ctr->blocklen) { 32 | return CRYPT_INVALID_ARG; 33 | } 34 | 35 | /* set IV */ 36 | XMEMCPY(ctr->ctr, IV, len); 37 | 38 | /* force next block */ 39 | ctr->padlen = 0; 40 | return cipher_descriptor[ctr->cipher].ecb_encrypt(IV, ctr->pad, &ctr->key); 41 | } 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ecb/ecb_decrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ecb_decrypt.c 7 | ECB implementation, decrypt a block, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_ECB_MODE 11 | 12 | /** 13 | ECB decrypt 14 | @param ct Ciphertext 15 | @param pt [out] Plaintext 16 | @param len The number of octets to process (must be multiple of the cipher block size) 17 | @param ecb ECB state 18 | @return CRYPT_OK if successful 19 | */ 20 | int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb) 21 | { 22 | int err; 23 | LTC_ARGCHK(pt != NULL); 24 | LTC_ARGCHK(ct != NULL); 25 | LTC_ARGCHK(ecb != NULL); 26 | if ((err = cipher_is_valid(ecb->cipher)) != CRYPT_OK) { 27 | return err; 28 | } 29 | if (len % cipher_descriptor[ecb->cipher].block_length) { 30 | return CRYPT_INVALID_ARG; 31 | } 32 | 33 | /* check for accel */ 34 | if (cipher_descriptor[ecb->cipher].accel_ecb_decrypt != NULL) { 35 | return cipher_descriptor[ecb->cipher].accel_ecb_decrypt(ct, pt, len / cipher_descriptor[ecb->cipher].block_length, &ecb->key); 36 | } 37 | while (len) { 38 | if ((err = cipher_descriptor[ecb->cipher].ecb_decrypt(ct, pt, &ecb->key)) != CRYPT_OK) { 39 | return err; 40 | } 41 | pt += cipher_descriptor[ecb->cipher].block_length; 42 | ct += cipher_descriptor[ecb->cipher].block_length; 43 | len -= cipher_descriptor[ecb->cipher].block_length; 44 | } 45 | return CRYPT_OK; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ecb/ecb_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ecb_done.c 7 | ECB implementation, finish chain, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_ECB_MODE 11 | 12 | /** Terminate the chain 13 | @param ecb The ECB chain to terminate 14 | @return CRYPT_OK on success 15 | */ 16 | int ecb_done(symmetric_ECB *ecb) 17 | { 18 | int err; 19 | LTC_ARGCHK(ecb != NULL); 20 | 21 | if ((err = cipher_is_valid(ecb->cipher)) != CRYPT_OK) { 22 | return err; 23 | } 24 | cipher_descriptor[ecb->cipher].done(&ecb->key); 25 | return CRYPT_OK; 26 | } 27 | 28 | 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ecb/ecb_encrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ecb_encrypt.c 7 | ECB implementation, encrypt a block, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_ECB_MODE 11 | 12 | /** 13 | ECB encrypt 14 | @param pt Plaintext 15 | @param ct [out] Ciphertext 16 | @param len The number of octets to process (must be multiple of the cipher block size) 17 | @param ecb ECB state 18 | @return CRYPT_OK if successful 19 | */ 20 | int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_ECB *ecb) 21 | { 22 | int err; 23 | LTC_ARGCHK(pt != NULL); 24 | LTC_ARGCHK(ct != NULL); 25 | LTC_ARGCHK(ecb != NULL); 26 | if ((err = cipher_is_valid(ecb->cipher)) != CRYPT_OK) { 27 | return err; 28 | } 29 | if (len % cipher_descriptor[ecb->cipher].block_length) { 30 | return CRYPT_INVALID_ARG; 31 | } 32 | 33 | /* check for accel */ 34 | if (cipher_descriptor[ecb->cipher].accel_ecb_encrypt != NULL) { 35 | return cipher_descriptor[ecb->cipher].accel_ecb_encrypt(pt, ct, len / cipher_descriptor[ecb->cipher].block_length, &ecb->key); 36 | } 37 | while (len) { 38 | if ((err = cipher_descriptor[ecb->cipher].ecb_encrypt(pt, ct, &ecb->key)) != CRYPT_OK) { 39 | return err; 40 | } 41 | pt += cipher_descriptor[ecb->cipher].block_length; 42 | ct += cipher_descriptor[ecb->cipher].block_length; 43 | len -= cipher_descriptor[ecb->cipher].block_length; 44 | } 45 | return CRYPT_OK; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ecb/ecb_start.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ecb_start.c 7 | ECB implementation, start chain, Tom St Denis 8 | */ 9 | 10 | 11 | #ifdef LTC_ECB_MODE 12 | 13 | /** 14 | Initialize a ECB context 15 | @param cipher The index of the cipher desired 16 | @param key The secret key 17 | @param keylen The length of the secret key (octets) 18 | @param num_rounds Number of rounds in the cipher desired (0 for default) 19 | @param ecb The ECB state to initialize 20 | @return CRYPT_OK if successful 21 | */ 22 | int ecb_start(int cipher, const unsigned char *key, int keylen, int num_rounds, symmetric_ECB *ecb) 23 | { 24 | int err; 25 | LTC_ARGCHK(key != NULL); 26 | LTC_ARGCHK(ecb != NULL); 27 | 28 | if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { 29 | return err; 30 | } 31 | ecb->cipher = cipher; 32 | ecb->blocklen = cipher_descriptor[cipher].block_length; 33 | return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ecb->key); 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/f8/f8_decrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file f8_decrypt.c 7 | F8 implementation, decrypt data, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_F8_MODE 11 | 12 | /** 13 | F8 decrypt 14 | @param ct Ciphertext 15 | @param pt [out] Plaintext 16 | @param len Length of ciphertext (octets) 17 | @param f8 F8 state 18 | @return CRYPT_OK if successful 19 | */ 20 | int f8_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_F8 *f8) 21 | { 22 | LTC_ARGCHK(pt != NULL); 23 | LTC_ARGCHK(ct != NULL); 24 | LTC_ARGCHK(f8 != NULL); 25 | return f8_encrypt(ct, pt, len, f8); 26 | } 27 | 28 | 29 | #endif 30 | 31 | 32 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/f8/f8_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file f8_done.c 7 | F8 implementation, finish chain, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_F8_MODE 11 | 12 | /** Terminate the chain 13 | @param f8 The F8 chain to terminate 14 | @return CRYPT_OK on success 15 | */ 16 | int f8_done(symmetric_F8 *f8) 17 | { 18 | int err; 19 | LTC_ARGCHK(f8 != NULL); 20 | 21 | if ((err = cipher_is_valid(f8->cipher)) != CRYPT_OK) { 22 | return err; 23 | } 24 | cipher_descriptor[f8->cipher].done(&f8->key); 25 | return CRYPT_OK; 26 | } 27 | 28 | 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/f8/f8_getiv.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ofb_getiv.c 7 | F8 implementation, get IV, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_F8_MODE 11 | 12 | /** 13 | Get the current initialization vector 14 | @param IV [out] The destination of the initialization vector 15 | @param len [in/out] The max size and resulting size of the initialization vector 16 | @param f8 The F8 state 17 | @return CRYPT_OK if successful 18 | */ 19 | int f8_getiv(unsigned char *IV, unsigned long *len, const symmetric_F8 *f8) 20 | { 21 | LTC_ARGCHK(IV != NULL); 22 | LTC_ARGCHK(len != NULL); 23 | LTC_ARGCHK(f8 != NULL); 24 | if ((unsigned long)f8->blocklen > *len) { 25 | *len = f8->blocklen; 26 | return CRYPT_BUFFER_OVERFLOW; 27 | } 28 | XMEMCPY(IV, f8->IV, f8->blocklen); 29 | *len = f8->blocklen; 30 | 31 | return CRYPT_OK; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/f8/f8_setiv.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file f8_setiv.c 7 | F8 implementation, set IV, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_F8_MODE 11 | 12 | /** 13 | Set an initialization vector 14 | @param IV The initialization vector 15 | @param len The length of the vector (in octets) 16 | @param f8 The F8 state 17 | @return CRYPT_OK if successful 18 | */ 19 | int f8_setiv(const unsigned char *IV, unsigned long len, symmetric_F8 *f8) 20 | { 21 | int err; 22 | 23 | LTC_ARGCHK(IV != NULL); 24 | LTC_ARGCHK(f8 != NULL); 25 | 26 | if ((err = cipher_is_valid(f8->cipher)) != CRYPT_OK) { 27 | return err; 28 | } 29 | 30 | if (len != (unsigned long)f8->blocklen) { 31 | return CRYPT_INVALID_ARG; 32 | } 33 | 34 | /* force next block */ 35 | f8->padlen = 0; 36 | return cipher_descriptor[f8->cipher].ecb_encrypt(IV, f8->IV, &f8->key); 37 | } 38 | 39 | #endif 40 | 41 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/lrw/lrw_decrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file lrw_decrypt.c 7 | LRW_MODE implementation, Decrypt blocks, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_LRW_MODE 11 | 12 | /** 13 | LRW decrypt blocks 14 | @param ct The ciphertext 15 | @param pt [out] The plaintext 16 | @param len The length in octets, must be a multiple of 16 17 | @param lrw The LRW state 18 | */ 19 | int lrw_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_LRW *lrw) 20 | { 21 | int err; 22 | 23 | LTC_ARGCHK(pt != NULL); 24 | LTC_ARGCHK(ct != NULL); 25 | LTC_ARGCHK(lrw != NULL); 26 | 27 | if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) { 28 | return err; 29 | } 30 | 31 | if (cipher_descriptor[lrw->cipher].accel_lrw_decrypt != NULL) { 32 | return cipher_descriptor[lrw->cipher].accel_lrw_decrypt(ct, pt, len, lrw->IV, lrw->tweak, &lrw->key); 33 | } 34 | 35 | return lrw_process(ct, pt, len, LRW_DECRYPT, lrw); 36 | } 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/lrw/lrw_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file lrw_done.c 7 | LRW_MODE implementation, Free resources, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_LRW_MODE 11 | 12 | /** 13 | Terminate a LRW state 14 | @param lrw The state to terminate 15 | @return CRYPT_OK if successful 16 | */ 17 | int lrw_done(symmetric_LRW *lrw) 18 | { 19 | int err; 20 | 21 | LTC_ARGCHK(lrw != NULL); 22 | 23 | if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) { 24 | return err; 25 | } 26 | cipher_descriptor[lrw->cipher].done(&lrw->key); 27 | 28 | return CRYPT_OK; 29 | } 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/lrw/lrw_encrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file lrw_encrypt.c 7 | LRW_MODE implementation, Encrypt blocks, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_LRW_MODE 11 | 12 | /** 13 | LRW encrypt blocks 14 | @param pt The plaintext 15 | @param ct [out] The ciphertext 16 | @param len The length in octets, must be a multiple of 16 17 | @param lrw The LRW state 18 | */ 19 | int lrw_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_LRW *lrw) 20 | { 21 | int err; 22 | 23 | LTC_ARGCHK(pt != NULL); 24 | LTC_ARGCHK(ct != NULL); 25 | LTC_ARGCHK(lrw != NULL); 26 | 27 | if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) { 28 | return err; 29 | } 30 | 31 | if (cipher_descriptor[lrw->cipher].accel_lrw_encrypt != NULL) { 32 | return cipher_descriptor[lrw->cipher].accel_lrw_encrypt(pt, ct, len, lrw->IV, lrw->tweak, &lrw->key); 33 | } 34 | 35 | return lrw_process(pt, ct, len, LRW_ENCRYPT, lrw); 36 | } 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/lrw/lrw_getiv.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file lrw_getiv.c 7 | LRW_MODE implementation, Retrieve the current IV, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_LRW_MODE 11 | 12 | /** 13 | Get the IV for LRW 14 | @param IV [out] The IV, must be 16 octets 15 | @param len Length ... must be at least 16 :-) 16 | @param lrw The LRW state to read 17 | @return CRYPT_OK if successful 18 | */ 19 | int lrw_getiv(unsigned char *IV, unsigned long *len, const symmetric_LRW *lrw) 20 | { 21 | LTC_ARGCHK(IV != NULL); 22 | LTC_ARGCHK(len != NULL); 23 | LTC_ARGCHK(lrw != NULL); 24 | if (*len < 16) { 25 | *len = 16; 26 | return CRYPT_BUFFER_OVERFLOW; 27 | } 28 | 29 | XMEMCPY(IV, lrw->IV, 16); 30 | *len = 16; 31 | return CRYPT_OK; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ofb/ofb_decrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ofb_decrypt.c 7 | OFB implementation, decrypt data, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_OFB_MODE 11 | 12 | /** 13 | OFB decrypt 14 | @param ct Ciphertext 15 | @param pt [out] Plaintext 16 | @param len Length of ciphertext (octets) 17 | @param ofb OFB state 18 | @return CRYPT_OK if successful 19 | */ 20 | int ofb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_OFB *ofb) 21 | { 22 | LTC_ARGCHK(pt != NULL); 23 | LTC_ARGCHK(ct != NULL); 24 | LTC_ARGCHK(ofb != NULL); 25 | return ofb_encrypt(ct, pt, len, ofb); 26 | } 27 | 28 | 29 | #endif 30 | 31 | 32 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ofb/ofb_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ofb_done.c 7 | OFB implementation, finish chain, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_OFB_MODE 11 | 12 | /** Terminate the chain 13 | @param ofb The OFB chain to terminate 14 | @return CRYPT_OK on success 15 | */ 16 | int ofb_done(symmetric_OFB *ofb) 17 | { 18 | int err; 19 | LTC_ARGCHK(ofb != NULL); 20 | 21 | if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) { 22 | return err; 23 | } 24 | cipher_descriptor[ofb->cipher].done(&ofb->key); 25 | return CRYPT_OK; 26 | } 27 | 28 | 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ofb/ofb_encrypt.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ofb_encrypt.c 7 | OFB implementation, encrypt data, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_OFB_MODE 11 | 12 | /** 13 | OFB encrypt 14 | @param pt Plaintext 15 | @param ct [out] Ciphertext 16 | @param len Length of plaintext (octets) 17 | @param ofb OFB state 18 | @return CRYPT_OK if successful 19 | */ 20 | int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb) 21 | { 22 | int err; 23 | LTC_ARGCHK(pt != NULL); 24 | LTC_ARGCHK(ct != NULL); 25 | LTC_ARGCHK(ofb != NULL); 26 | if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) { 27 | return err; 28 | } 29 | 30 | /* is blocklen/padlen valid? */ 31 | if (ofb->blocklen < 0 || ofb->blocklen > (int)sizeof(ofb->IV) || 32 | ofb->padlen < 0 || ofb->padlen > (int)sizeof(ofb->IV)) { 33 | return CRYPT_INVALID_ARG; 34 | } 35 | 36 | while (len-- > 0) { 37 | if (ofb->padlen == ofb->blocklen) { 38 | if ((err = cipher_descriptor[ofb->cipher].ecb_encrypt(ofb->IV, ofb->IV, &ofb->key)) != CRYPT_OK) { 39 | return err; 40 | } 41 | ofb->padlen = 0; 42 | } 43 | *ct++ = *pt++ ^ ofb->IV[(ofb->padlen)++]; 44 | } 45 | return CRYPT_OK; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ofb/ofb_getiv.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ofb_getiv.c 7 | OFB implementation, get IV, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_OFB_MODE 11 | 12 | /** 13 | Get the current initialization vector 14 | @param IV [out] The destination of the initialization vector 15 | @param len [in/out] The max size and resulting size of the initialization vector 16 | @param ofb The OFB state 17 | @return CRYPT_OK if successful 18 | */ 19 | int ofb_getiv(unsigned char *IV, unsigned long *len, const symmetric_OFB *ofb) 20 | { 21 | LTC_ARGCHK(IV != NULL); 22 | LTC_ARGCHK(len != NULL); 23 | LTC_ARGCHK(ofb != NULL); 24 | if ((unsigned long)ofb->blocklen > *len) { 25 | *len = ofb->blocklen; 26 | return CRYPT_BUFFER_OVERFLOW; 27 | } 28 | XMEMCPY(IV, ofb->IV, ofb->blocklen); 29 | *len = ofb->blocklen; 30 | 31 | return CRYPT_OK; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ofb/ofb_setiv.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ofb_setiv.c 7 | OFB implementation, set IV, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_OFB_MODE 11 | 12 | /** 13 | Set an initialization vector 14 | @param IV The initialization vector 15 | @param len The length of the vector (in octets) 16 | @param ofb The OFB state 17 | @return CRYPT_OK if successful 18 | */ 19 | int ofb_setiv(const unsigned char *IV, unsigned long len, symmetric_OFB *ofb) 20 | { 21 | int err; 22 | 23 | LTC_ARGCHK(IV != NULL); 24 | LTC_ARGCHK(ofb != NULL); 25 | 26 | if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) { 27 | return err; 28 | } 29 | 30 | if (len != (unsigned long)ofb->blocklen) { 31 | return CRYPT_INVALID_ARG; 32 | } 33 | 34 | /* force next block */ 35 | ofb->padlen = 0; 36 | return cipher_descriptor[ofb->cipher].ecb_encrypt(IV, ofb->IV, &ofb->key); 37 | } 38 | 39 | #endif 40 | 41 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/ofb/ofb_start.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ofb_start.c 7 | OFB implementation, start chain, Tom St Denis 8 | */ 9 | 10 | 11 | #ifdef LTC_OFB_MODE 12 | 13 | /** 14 | Initialize a OFB context 15 | @param cipher The index of the cipher desired 16 | @param IV The initialization vector 17 | @param key The secret key 18 | @param keylen The length of the secret key (octets) 19 | @param num_rounds Number of rounds in the cipher desired (0 for default) 20 | @param ofb The OFB state to initialize 21 | @return CRYPT_OK if successful 22 | */ 23 | int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key, 24 | int keylen, int num_rounds, symmetric_OFB *ofb) 25 | { 26 | int x, err; 27 | 28 | LTC_ARGCHK(IV != NULL); 29 | LTC_ARGCHK(key != NULL); 30 | LTC_ARGCHK(ofb != NULL); 31 | 32 | if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { 33 | return err; 34 | } 35 | 36 | /* copy details */ 37 | ofb->cipher = cipher; 38 | ofb->blocklen = cipher_descriptor[cipher].block_length; 39 | for (x = 0; x < ofb->blocklen; x++) { 40 | ofb->IV[x] = IV[x]; 41 | } 42 | 43 | /* init the cipher */ 44 | ofb->padlen = ofb->blocklen; 45 | return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ofb->key); 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/xts/xts_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects 7 | */ 8 | 9 | #ifdef LTC_XTS_MODE 10 | 11 | /** Terminate XTS state 12 | @param xts The state to terminate 13 | */ 14 | void xts_done(symmetric_xts *xts) 15 | { 16 | LTC_ARGCHKVD(xts != NULL); 17 | cipher_descriptor[xts->cipher].done(&xts->key1); 18 | cipher_descriptor[xts->cipher].done(&xts->key2); 19 | } 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/xts/xts_init.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects 7 | */ 8 | 9 | #ifdef LTC_XTS_MODE 10 | 11 | /** Start XTS mode 12 | @param cipher The index of the cipher to use 13 | @param key1 The encrypt key 14 | @param key2 The tweak encrypt key 15 | @param keylen The length of the keys (each) in octets 16 | @param num_rounds The number of rounds for the cipher (0 == default) 17 | @param xts [out] XTS structure 18 | Returns CRYPT_OK upon success. 19 | */ 20 | int xts_start(int cipher, const unsigned char *key1, const unsigned char *key2, unsigned long keylen, int num_rounds, 21 | symmetric_xts *xts) 22 | { 23 | int err; 24 | 25 | /* check inputs */ 26 | LTC_ARGCHK(key1 != NULL); 27 | LTC_ARGCHK(key2 != NULL); 28 | LTC_ARGCHK(xts != NULL); 29 | 30 | /* check if valid */ 31 | if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { 32 | return err; 33 | } 34 | 35 | if (cipher_descriptor[cipher].block_length != 16) { 36 | return CRYPT_INVALID_ARG; 37 | } 38 | 39 | /* schedule the two ciphers */ 40 | if ((err = cipher_descriptor[cipher].setup(key1, keylen, num_rounds, &xts->key1)) != CRYPT_OK) { 41 | return err; 42 | } 43 | if ((err = cipher_descriptor[cipher].setup(key2, keylen, num_rounds, &xts->key2)) != CRYPT_OK) { 44 | return err; 45 | } 46 | xts->cipher = cipher; 47 | 48 | return err; 49 | } 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/modes/xts/xts_mult_x.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects 7 | */ 8 | 9 | #ifdef LTC_XTS_MODE 10 | 11 | /** multiply by x 12 | @param I The value to multiply by x (LFSR shift) 13 | */ 14 | void xts_mult_x(unsigned char *I) 15 | { 16 | int x; 17 | unsigned char t, tt; 18 | 19 | for (x = t = 0; x < 16; x++) { 20 | tt = I[x] >> 7; 21 | I[x] = ((I[x] << 1) | t) & 0xFF; 22 | t = tt; 23 | } 24 | if (tt) { 25 | I[0] ^= 0x87; 26 | } 27 | } 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/bit/der_length_bit_string.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_length_bit_string.c 7 | ASN.1 DER, get length of BIT STRING, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_DER 11 | /** 12 | Gets length of DER encoding of BIT STRING 13 | @param nbits The number of bits in the string to encode 14 | @param outlen [out] The length of the DER encoding for the given string 15 | @return CRYPT_OK if successful 16 | */ 17 | int der_length_bit_string(unsigned long nbits, unsigned long *outlen) 18 | { 19 | unsigned long nbytes, x; 20 | int err; 21 | 22 | LTC_ARGCHK(outlen != NULL); 23 | 24 | /* get the number of the bytes */ 25 | nbytes = (nbits >> 3) + ((nbits & 7) ? 1 : 0) + 1; 26 | 27 | if ((err = der_length_asn1_length(nbytes, &x)) != CRYPT_OK) { 28 | return err; 29 | } 30 | *outlen = 1 + x + nbytes; 31 | 32 | return CRYPT_OK; 33 | } 34 | 35 | #endif 36 | 37 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/boolean/der_decode_boolean.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_decode_boolean.c 7 | ASN.1 DER, decode a BOOLEAN, Tom St Denis 8 | */ 9 | 10 | 11 | #ifdef LTC_DER 12 | 13 | /** 14 | Read a BOOLEAN 15 | @param in The destination for the DER encoded BOOLEAN 16 | @param inlen The size of the DER BOOLEAN 17 | @param out [out] The boolean to decode 18 | @return CRYPT_OK if successful 19 | */ 20 | int der_decode_boolean(const unsigned char *in, unsigned long inlen, 21 | int *out) 22 | { 23 | LTC_ARGCHK(in != NULL); 24 | LTC_ARGCHK(out != NULL); 25 | 26 | if (inlen < 3 || in[0] != 0x01 || in[1] != 0x01 || (in[2] != 0x00 && in[2] != 0xFF)) { 27 | return CRYPT_INVALID_ARG; 28 | } 29 | 30 | *out = (in[2]==0xFF) ? 1 : 0; 31 | 32 | return CRYPT_OK; 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/boolean/der_encode_boolean.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_encode_boolean.c 7 | ASN.1 DER, encode a BOOLEAN, Tom St Denis 8 | */ 9 | 10 | 11 | #ifdef LTC_DER 12 | 13 | /** 14 | Store a BOOLEAN 15 | @param in The boolean to encode 16 | @param out [out] The destination for the DER encoded BOOLEAN 17 | @param outlen [in/out] The max size and resulting size of the DER BOOLEAN 18 | @return CRYPT_OK if successful 19 | */ 20 | int der_encode_boolean(int in, 21 | unsigned char *out, unsigned long *outlen) 22 | { 23 | LTC_ARGCHK(outlen != NULL); 24 | LTC_ARGCHK(out != NULL); 25 | 26 | if (*outlen < 3) { 27 | *outlen = 3; 28 | return CRYPT_BUFFER_OVERFLOW; 29 | } 30 | 31 | *outlen = 3; 32 | out[0] = 0x01; 33 | out[1] = 0x01; 34 | out[2] = in ? 0xFF : 0x00; 35 | 36 | return CRYPT_OK; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/boolean/der_length_boolean.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_length_boolean.c 7 | ASN.1 DER, get length of a BOOLEAN, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_DER 11 | /** 12 | Gets length of DER encoding of a BOOLEAN 13 | @param outlen [out] The length of the DER encoding 14 | @return CRYPT_OK if successful 15 | */ 16 | int der_length_boolean(unsigned long *outlen) 17 | { 18 | LTC_ARGCHK(outlen != NULL); 19 | *outlen = 3; 20 | return CRYPT_OK; 21 | } 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/general/der_decode_asn1_length.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_decode_asn1_length.c 7 | ASN.1 DER, decode the ASN.1 Length field, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_DER 11 | /** 12 | Decode the ASN.1 Length field 13 | @param in Where to read the length field from 14 | @param inlen [in/out] The size of in available/read 15 | @param outlen [out] The decoded ASN.1 length 16 | @return CRYPT_OK if successful 17 | */ 18 | int der_decode_asn1_length(const unsigned char *in, unsigned long *inlen, unsigned long *outlen) 19 | { 20 | unsigned long real_len, decoded_len, offset, i; 21 | 22 | LTC_ARGCHK(in != NULL); 23 | LTC_ARGCHK(inlen != NULL); 24 | 25 | if (*inlen < 1) { 26 | return CRYPT_BUFFER_OVERFLOW; 27 | } 28 | 29 | real_len = in[0]; 30 | 31 | if (real_len < 128) { 32 | decoded_len = real_len; 33 | offset = 1; 34 | } else { 35 | real_len &= 0x7F; 36 | if (real_len == 0) { 37 | return CRYPT_PK_ASN1_ERROR; 38 | } 39 | if (real_len > sizeof(decoded_len)) { 40 | return CRYPT_OVERFLOW; 41 | } 42 | if (real_len > (*inlen - 1)) { 43 | return CRYPT_BUFFER_OVERFLOW; 44 | } 45 | decoded_len = 0; 46 | offset = 1 + real_len; 47 | for (i = 0; i < real_len; i++) { 48 | decoded_len = (decoded_len << 8) | in[1 + i]; 49 | } 50 | } 51 | 52 | if (outlen != NULL) *outlen = decoded_len; 53 | if (decoded_len > (*inlen - offset)) return CRYPT_OVERFLOW; 54 | *inlen = offset; 55 | 56 | return CRYPT_OK; 57 | } 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/general/der_length_asn1_identifier.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_length_asn1_identifier.c 7 | ASN.1 DER, determine the length when encoding the ASN.1 Identifier, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_DER 11 | /** 12 | Determine the length required when encoding the ASN.1 Identifier 13 | @param id The ASN.1 identifier to encode 14 | @param idlen [out] The required length to encode list 15 | @return CRYPT_OK if successful 16 | */ 17 | 18 | int der_length_asn1_identifier(const ltc_asn1_list *id, unsigned long *idlen) 19 | { 20 | return der_encode_asn1_identifier(id, NULL, idlen); 21 | } 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/general/der_length_asn1_length.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_length_asn1_length.c 7 | ASN.1 DER, determine the length of the ASN.1 length field, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_DER 11 | /** 12 | Determine the length required to encode len in the ASN.1 length field 13 | @param len The length to encode 14 | @param outlen [out] The length that's required to store len 15 | @return CRYPT_OK if successful 16 | */ 17 | int der_length_asn1_length(unsigned long len, unsigned long *outlen) 18 | { 19 | return der_encode_asn1_length(len, NULL, outlen); 20 | } 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/generalizedtime/der_length_generalizedtime.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_length_utctime.c 7 | ASN.1 DER, get length of GeneralizedTime, Steffen Jaeckel 8 | Based on der_length_utctime.c 9 | */ 10 | 11 | #ifdef LTC_DER 12 | 13 | /** 14 | Gets length of DER encoding of GeneralizedTime 15 | @param gtime The GeneralizedTime structure to get the size of 16 | @param outlen [out] The length of the DER encoding 17 | @return CRYPT_OK if successful 18 | */ 19 | int der_length_generalizedtime(const ltc_generalizedtime *gtime, unsigned long *outlen) 20 | { 21 | LTC_ARGCHK(outlen != NULL); 22 | LTC_ARGCHK(gtime != NULL); 23 | 24 | if (gtime->fs == 0) { 25 | /* we encode as YYYYMMDDhhmmssZ */ 26 | *outlen = 2 + 14 + 1; 27 | } else { 28 | unsigned long len = 2 + 14 + 1; 29 | unsigned fs = gtime->fs; 30 | do { 31 | fs /= 10; 32 | len++; 33 | } while(fs != 0); 34 | if (gtime->off_hh == 0 && gtime->off_mm == 0) { 35 | /* we encode as YYYYMMDDhhmmss.fsZ */ 36 | len += 1; 37 | } 38 | else { 39 | /* we encode as YYYYMMDDhhmmss.fs{+|-}hh'mm' */ 40 | len += 5; 41 | } 42 | *outlen = len; 43 | } 44 | 45 | return CRYPT_OK; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/ia5/der_encode_ia5_string.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_encode_ia5_string.c 7 | ASN.1 DER, encode a IA5 STRING, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_DER 11 | 12 | /** 13 | Store an IA5 STRING 14 | @param in The array of IA5 to store (one per char) 15 | @param inlen The number of IA5 to store 16 | @param out [out] The destination for the DER encoded IA5 STRING 17 | @param outlen [in/out] The max size and resulting size of the DER IA5 STRING 18 | @return CRYPT_OK if successful 19 | */ 20 | int der_encode_ia5_string(const unsigned char *in, unsigned long inlen, 21 | unsigned char *out, unsigned long *outlen) 22 | { 23 | unsigned long x, y, len; 24 | int err; 25 | 26 | LTC_ARGCHK(in != NULL); 27 | LTC_ARGCHK(out != NULL); 28 | LTC_ARGCHK(outlen != NULL); 29 | 30 | /* get the size */ 31 | if ((err = der_length_ia5_string(in, inlen, &len)) != CRYPT_OK) { 32 | return err; 33 | } 34 | 35 | /* too big? */ 36 | if (len > *outlen) { 37 | *outlen = len; 38 | return CRYPT_BUFFER_OVERFLOW; 39 | } 40 | 41 | /* encode the header+len */ 42 | x = 0; 43 | out[x++] = 0x16; 44 | len = *outlen - x; 45 | if ((err = der_encode_asn1_length(inlen, out + x, &len)) != CRYPT_OK) { 46 | return err; 47 | } 48 | x += len; 49 | 50 | /* store octets */ 51 | for (y = 0; y < inlen; y++) { 52 | out[x++] = der_ia5_char_encode(in[y]); 53 | } 54 | 55 | /* retun length */ 56 | *outlen = x; 57 | 58 | return CRYPT_OK; 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/integer/der_length_integer.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_length_integer.c 7 | ASN.1 DER, get length of encoding, Tom St Denis 8 | */ 9 | 10 | 11 | #ifdef LTC_DER 12 | /** 13 | Gets length of DER encoding of num 14 | @param num The int to get the size of 15 | @param outlen [out] The length of the DER encoding for the given integer 16 | @return CRYPT_OK if successful 17 | */ 18 | int der_length_integer(void *num, unsigned long *outlen) 19 | { 20 | unsigned long z, len; 21 | int leading_zero, err; 22 | 23 | LTC_ARGCHK(num != NULL); 24 | LTC_ARGCHK(outlen != NULL); 25 | 26 | if (mp_cmp_d(num, 0) != LTC_MP_LT) { 27 | /* positive */ 28 | 29 | /* we only need a leading zero if the msb of the first byte is one */ 30 | if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == LTC_MP_YES) { 31 | leading_zero = 1; 32 | } else { 33 | leading_zero = 0; 34 | } 35 | 36 | /* size for bignum */ 37 | len = leading_zero + mp_unsigned_bin_size(num); 38 | } else { 39 | /* it's negative */ 40 | /* find power of 2 that is a multiple of eight and greater than count bits */ 41 | z = mp_count_bits(num); 42 | z = z + (8 - (z & 7)); 43 | if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) --z; 44 | len = z >> 3; 45 | } 46 | 47 | if ((err = der_length_asn1_length(len, &z)) != CRYPT_OK) { 48 | return err; 49 | } 50 | *outlen = 1 + z + len; 51 | 52 | return CRYPT_OK; 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/octet/der_encode_octet_string.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_encode_octet_string.c 7 | ASN.1 DER, encode a OCTET STRING, Tom St Denis 8 | */ 9 | 10 | 11 | #ifdef LTC_DER 12 | 13 | /** 14 | Store an OCTET STRING 15 | @param in The array of OCTETS to store (one per char) 16 | @param inlen The number of OCTETS to store 17 | @param out [out] The destination for the DER encoded OCTET STRING 18 | @param outlen [in/out] The max size and resulting size of the DER OCTET STRING 19 | @return CRYPT_OK if successful 20 | */ 21 | int der_encode_octet_string(const unsigned char *in, unsigned long inlen, 22 | unsigned char *out, unsigned long *outlen) 23 | { 24 | unsigned long x, y, len; 25 | int err; 26 | 27 | LTC_ARGCHK(in != NULL); 28 | LTC_ARGCHK(out != NULL); 29 | LTC_ARGCHK(outlen != NULL); 30 | 31 | /* get the size */ 32 | if ((err = der_length_octet_string(inlen, &len)) != CRYPT_OK) { 33 | return err; 34 | } 35 | 36 | /* too big? */ 37 | if (len > *outlen) { 38 | *outlen = len; 39 | return CRYPT_BUFFER_OVERFLOW; 40 | } 41 | 42 | /* encode the header+len */ 43 | x = 0; 44 | out[x++] = 0x04; 45 | len = *outlen - x; 46 | if ((err = der_encode_asn1_length(inlen, out + x, &len)) != CRYPT_OK) { 47 | return err; 48 | } 49 | x += len; 50 | 51 | /* store octets */ 52 | for (y = 0; y < inlen; y++) { 53 | out[x++] = in[y]; 54 | } 55 | 56 | /* retun length */ 57 | *outlen = x; 58 | 59 | return CRYPT_OK; 60 | } 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/octet/der_length_octet_string.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_length_octet_string.c 7 | ASN.1 DER, get length of OCTET STRING, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_DER 11 | /** 12 | Gets length of DER encoding of OCTET STRING 13 | @param noctets The number of octets in the string to encode 14 | @param outlen [out] The length of the DER encoding for the given string 15 | @return CRYPT_OK if successful 16 | */ 17 | int der_length_octet_string(unsigned long noctets, unsigned long *outlen) 18 | { 19 | unsigned long x; 20 | int err; 21 | 22 | LTC_ARGCHK(outlen != NULL); 23 | 24 | if ((err = der_length_asn1_length(noctets, &x)) != CRYPT_OK) { 25 | return err; 26 | } 27 | *outlen = 1 + x + noctets; 28 | 29 | return CRYPT_OK; 30 | } 31 | 32 | #endif 33 | 34 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_ex.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | 6 | /** 7 | @file der_decode_sequence_ex.c 8 | ASN.1 DER, decode a SEQUENCE, Tom St Denis 9 | */ 10 | 11 | #ifdef LTC_DER 12 | 13 | /** 14 | Decode a SEQUENCE 15 | @param in The DER encoded input 16 | @param inlen The size of the input 17 | @param list The list of items to decode 18 | @param outlen The number of items in the list 19 | @param flags c.f. enum ltc_der_seq 20 | @return CRYPT_OK on success 21 | */ 22 | int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen, 23 | ltc_asn1_list *list, unsigned long outlen, unsigned int flags) 24 | { 25 | return der_decode_custom_type_ex(in, inlen, NULL, list, outlen, flags); 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/sequence/der_sequence_free.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_sequence_free.c 7 | ASN.1 DER, free's a structure allocated by der_decode_sequence_flexi(), Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_DER 11 | 12 | /** 13 | Free memory allocated by der_decode_sequence_flexi() 14 | @param in The list to free 15 | */ 16 | void der_sequence_free(ltc_asn1_list *in) 17 | { 18 | ltc_asn1_list *l; 19 | 20 | if (!in) return; 21 | 22 | /* walk to the start of the chain */ 23 | while (in->prev != NULL || in->parent != NULL) { 24 | if (in->parent != NULL) { 25 | in = in->parent; 26 | } else { 27 | in = in->prev; 28 | } 29 | } 30 | 31 | /* now walk the list and free stuff */ 32 | while (in != NULL) { 33 | /* is there a child? */ 34 | if (in->child) { 35 | /* disconnect */ 36 | in->child->parent = NULL; 37 | der_sequence_free(in->child); 38 | } 39 | 40 | switch (in->type) { 41 | case LTC_ASN1_SETOF: break; 42 | case LTC_ASN1_INTEGER : if (in->data != NULL) { mp_clear(in->data); } break; 43 | default : if (in->data != NULL) { XFREE(in->data); } 44 | } 45 | 46 | /* move to next and free current */ 47 | l = in->next; 48 | XFREE(in); 49 | in = l; 50 | } 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/sequence/der_sequence_shrink.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_sequence_shrink.c 7 | Free memory allocated for CONSTRUCTED, SET or SEQUENCE elements by der_decode_sequence_flexi(), Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_DER 11 | 12 | /** 13 | Free memory allocated for CONSTRUCTED, 14 | SET or SEQUENCE elements by der_decode_sequence_flexi() 15 | @param in The list to shrink 16 | */ 17 | void der_sequence_shrink(ltc_asn1_list *in) 18 | { 19 | if (!in) return; 20 | 21 | /* now walk the list and free stuff */ 22 | while (in != NULL) { 23 | /* is there a child? */ 24 | if (in->child) { 25 | der_sequence_shrink(in->child); 26 | } 27 | 28 | switch (in->type) { 29 | case LTC_ASN1_CUSTOM_TYPE: 30 | case LTC_ASN1_SET: 31 | case LTC_ASN1_SEQUENCE : if (in->data != NULL) { XFREE(in->data); in->data = NULL; } break; 32 | default: break; 33 | } 34 | 35 | /* move to next and free current */ 36 | in = in->next; 37 | } 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/short_integer/der_decode_short_integer.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_decode_short_integer.c 7 | ASN.1 DER, decode an integer, Tom St Denis 8 | */ 9 | 10 | 11 | #ifdef LTC_DER 12 | 13 | /** 14 | Read a short integer 15 | @param in The DER encoded data 16 | @param inlen Size of data 17 | @param num [out] The integer to decode 18 | @return CRYPT_OK if successful 19 | */ 20 | int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num) 21 | { 22 | unsigned long len, x, y; 23 | 24 | LTC_ARGCHK(num != NULL); 25 | LTC_ARGCHK(in != NULL); 26 | 27 | /* check length */ 28 | if (inlen < 2) { 29 | return CRYPT_INVALID_PACKET; 30 | } 31 | 32 | /* check header */ 33 | x = 0; 34 | if ((in[x++] & 0x1F) != 0x02) { 35 | return CRYPT_INVALID_PACKET; 36 | } 37 | 38 | /* get the packet len */ 39 | len = in[x++]; 40 | 41 | if (x + len > inlen) { 42 | return CRYPT_INVALID_PACKET; 43 | } 44 | 45 | if (len > sizeof(unsigned long)) { 46 | return CRYPT_OVERFLOW; 47 | } 48 | 49 | /* read number */ 50 | y = 0; 51 | while (len--) { 52 | y = (y<<8) | (unsigned long)in[x++]; 53 | } 54 | *num = y; 55 | 56 | return CRYPT_OK; 57 | 58 | } 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/short_integer/der_length_short_integer.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_length_short_integer.c 7 | ASN.1 DER, get length of encoding, Tom St Denis 8 | */ 9 | 10 | 11 | #ifdef LTC_DER 12 | /** 13 | Gets length of DER encoding of num 14 | @param num The integer to get the size of 15 | @param outlen [out] The length of the DER encoding for the given integer 16 | @return CRYPT_OK if successful 17 | */ 18 | int der_length_short_integer(unsigned long num, unsigned long *outlen) 19 | { 20 | unsigned long z, y; 21 | int err; 22 | 23 | LTC_ARGCHK(outlen != NULL); 24 | 25 | /* force to 32 bits */ 26 | num &= 0xFFFFFFFFUL; 27 | 28 | /* get the number of bytes */ 29 | z = 0; 30 | y = num; 31 | while (y) { 32 | ++z; 33 | y >>= 8; 34 | } 35 | 36 | /* handle zero */ 37 | if (z == 0) { 38 | z = 1; 39 | } else if ((num&(1UL<<((z<<3) - 1))) != 0) { 40 | /* in case msb is set */ 41 | ++z; 42 | } 43 | 44 | if ((err = der_length_asn1_length(z, &y)) != CRYPT_OK) { 45 | return err; 46 | } 47 | *outlen = 1 + y + z; 48 | 49 | return CRYPT_OK; 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/der/utctime/der_length_utctime.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file der_length_utctime.c 7 | ASN.1 DER, get length of UTCTIME, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_DER 11 | 12 | /** 13 | Gets length of DER encoding of UTCTIME 14 | @param utctime The UTC time structure to get the size of 15 | @param outlen [out] The length of the DER encoding 16 | @return CRYPT_OK if successful 17 | */ 18 | int der_length_utctime(const ltc_utctime *utctime, unsigned long *outlen) 19 | { 20 | LTC_ARGCHK(outlen != NULL); 21 | LTC_ARGCHK(utctime != NULL); 22 | 23 | if (utctime->off_hh == 0 && utctime->off_mm == 0) { 24 | /* we encode as YYMMDDhhmmssZ */ 25 | *outlen = 2 + 13; 26 | } else { 27 | /* we encode as YYMMDDhhmmss{+|-}hh'mm' */ 28 | *outlen = 2 + 17; 29 | } 30 | 31 | return CRYPT_OK; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/oid/pk_get_oid.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | #ifdef LTC_DER 6 | 7 | typedef struct { 8 | enum ltc_oid_id id; 9 | const char* oid; 10 | } oid_table_entry; 11 | 12 | static const oid_table_entry pka_oids[] = { 13 | { LTC_OID_RSA, "1.2.840.113549.1.1.1" }, 14 | { LTC_OID_DSA, "1.2.840.10040.4.1" }, 15 | { LTC_OID_EC, "1.2.840.10045.2.1" }, 16 | { LTC_OID_EC_PRIMEF, "1.2.840.10045.1.1" }, 17 | { LTC_OID_X25519, "1.3.101.110" }, 18 | { LTC_OID_ED25519, "1.3.101.112" }, 19 | }; 20 | 21 | /* 22 | Returns the OID requested. 23 | @return CRYPT_OK if valid 24 | */ 25 | int pk_get_oid(enum ltc_oid_id id, const char **st) 26 | { 27 | unsigned int i; 28 | LTC_ARGCHK(st != NULL); 29 | for (i = 0; i < sizeof(pka_oids)/sizeof(pka_oids[0]); ++i) { 30 | if (pka_oids[i].id == id) { 31 | *st = pka_oids[i].oid; 32 | return CRYPT_OK; 33 | } 34 | } 35 | return CRYPT_INVALID_ARG; 36 | } 37 | #endif 38 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/asn1/oid/pk_oid_cmp.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | #ifdef LTC_DER 6 | 7 | /* 8 | Compare an OID string to an array of `unsigned long`. 9 | @return CRYPT_OK if equal 10 | */ 11 | int pk_oid_cmp_with_ulong(const char *o1, const unsigned long *o2, unsigned long o2size) 12 | { 13 | unsigned long i; 14 | char tmp[256] = { 0 }; 15 | int err; 16 | 17 | if (o1 == NULL || o2 == NULL) return CRYPT_ERROR; 18 | 19 | i = sizeof(tmp); 20 | if ((err = pk_oid_num_to_str(o2, o2size, tmp, &i)) != CRYPT_OK) { 21 | return err; 22 | } 23 | 24 | if (XSTRCMP(o1, tmp) != 0) { 25 | return CRYPT_PK_INVALID_TYPE; 26 | } 27 | 28 | return CRYPT_OK; 29 | } 30 | 31 | /* 32 | Compare an OID string to an OID element decoded from ASN.1. 33 | @return CRYPT_OK if equal 34 | */ 35 | int pk_oid_cmp_with_asn1(const char *o1, const ltc_asn1_list *o2) 36 | { 37 | if (o1 == NULL || o2 == NULL) return CRYPT_ERROR; 38 | 39 | if (o2->type != LTC_ASN1_OBJECT_IDENTIFIER) return CRYPT_INVALID_ARG; 40 | 41 | return pk_oid_cmp_with_ulong(o1, o2->data, o2->size); 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/dh/dh_check_pubkey.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_MDH 7 | 8 | /** 9 | Check DH public key (INTERNAL ONLY, not part of public API) 10 | @param key The key you wish to test 11 | @return CRYPT_OK if successful 12 | */ 13 | int dh_check_pubkey(const dh_key *key) 14 | { 15 | void *p_minus1; 16 | ltc_mp_digit digit; 17 | int i, digit_count, bits_set = 0, err; 18 | 19 | LTC_ARGCHK(key != NULL); 20 | 21 | if ((err = mp_init(&p_minus1)) != CRYPT_OK) { 22 | return err; 23 | } 24 | 25 | /* avoid: y <= 1 OR y >= p-1 */ 26 | if ((err = mp_sub_d(key->prime, 1, p_minus1)) != CRYPT_OK) { 27 | goto error; 28 | } 29 | if (mp_cmp(key->y, p_minus1) != LTC_MP_LT || mp_cmp_d(key->y, 1) != LTC_MP_GT) { 30 | err = CRYPT_INVALID_ARG; 31 | goto error; 32 | } 33 | 34 | /* public key must have more than one bit set */ 35 | digit_count = mp_get_digit_count(key->y); 36 | for (i = 0; i < digit_count && bits_set < 2; i++) { 37 | digit = mp_get_digit(key->y, i); 38 | while (digit > 0) { 39 | if (digit & 1) bits_set++; 40 | digit >>= 1; 41 | } 42 | } 43 | if (bits_set > 1) { 44 | err = CRYPT_OK; 45 | } 46 | else { 47 | err = CRYPT_INVALID_ARG; 48 | } 49 | 50 | error: 51 | mp_clear(p_minus1); 52 | return err; 53 | } 54 | 55 | #endif /* LTC_MDH */ 56 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/dh/dh_export_key.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_MDH 7 | 8 | /** 9 | Binary export a DH key to a buffer 10 | @param out [out] The destination for the key 11 | @param outlen [in/out] The max size and resulting size of the DH key 12 | @param type Which type of key (PK_PRIVATE or PK_PUBLIC) 13 | @param key The key you wish to export 14 | @return CRYPT_OK if successful 15 | */ 16 | int dh_export_key(void *out, unsigned long *outlen, int type, const dh_key *key) 17 | { 18 | unsigned long len; 19 | void *k; 20 | 21 | LTC_ARGCHK(out != NULL); 22 | LTC_ARGCHK(outlen != NULL); 23 | LTC_ARGCHK(key != NULL); 24 | 25 | k = (type == PK_PRIVATE) ? key->x : key->y; 26 | len = mp_unsigned_bin_size(k); 27 | 28 | if (*outlen < len) { 29 | *outlen = len; 30 | return CRYPT_BUFFER_OVERFLOW; 31 | } 32 | *outlen = len; 33 | 34 | return mp_to_unsigned_bin(k, out); 35 | } 36 | 37 | #endif /* LTC_MDH */ 38 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/dh/dh_free.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_MDH 7 | 8 | /** 9 | Free the allocated ram for a DH key 10 | @param key The key which you wish to free 11 | */ 12 | void dh_free(dh_key *key) 13 | { 14 | LTC_ARGCHKVD(key != NULL); 15 | mp_cleanup_multi(&key->prime, &key->base, &key->y, &key->x, LTC_NULL); 16 | } 17 | 18 | #endif /* LTC_MDH */ 19 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/dh/dh_set_pg_dhparam.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_MDH 7 | 8 | /** 9 | Import DH key parts p and g from dhparam 10 | 11 | dhparam data: openssl dhparam -outform DER -out dhparam.der 2048 12 | 13 | @param dhparam The DH param DER encoded data 14 | @param dhparamlen The length of dhparam data 15 | @param key [out] Where the newly created DH key will be stored 16 | @return CRYPT_OK if successful, note: on error all allocated memory will be freed automatically. 17 | */ 18 | int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key) 19 | { 20 | int err; 21 | 22 | LTC_ARGCHK(key != NULL); 23 | LTC_ARGCHK(ltc_mp.name != NULL); 24 | LTC_ARGCHK(dhparam != NULL); 25 | LTC_ARGCHK(dhparamlen > 0); 26 | 27 | if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, LTC_NULL)) != CRYPT_OK) { 28 | return err; 29 | } 30 | if ((err = der_decode_sequence_multi(dhparam, dhparamlen, 31 | LTC_ASN1_INTEGER, 1UL, key->prime, 32 | LTC_ASN1_INTEGER, 1UL, key->base, 33 | LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { 34 | goto LBL_ERR; 35 | } 36 | 37 | return CRYPT_OK; 38 | 39 | LBL_ERR: 40 | dh_free(key); 41 | return err; 42 | } 43 | 44 | #endif /* LTC_MDH */ 45 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/dsa/dsa_free.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file dsa_free.c 7 | DSA implementation, free a DSA key, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_MDSA 11 | 12 | /** 13 | Free a DSA key 14 | @param key The key to free from memory 15 | */ 16 | void dsa_free(dsa_key *key) 17 | { 18 | LTC_ARGCHKVD(key != NULL); 19 | mp_cleanup_multi(&key->y, &key->x, &key->q, &key->g, &key->p, LTC_NULL); 20 | key->type = key->qord = 0; 21 | } 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/dsa/dsa_generate_key.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file dsa_make_key.c 7 | DSA implementation, generate a DSA key 8 | */ 9 | 10 | #ifdef LTC_MDSA 11 | 12 | /** 13 | Create a DSA key 14 | @param prng An active PRNG state 15 | @param wprng The index of the PRNG desired 16 | @param key [in/out] Where to store the created key 17 | @return CRYPT_OK if successful. 18 | */ 19 | int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key) 20 | { 21 | int err; 22 | 23 | LTC_ARGCHK(key != NULL); 24 | LTC_ARGCHK(ltc_mp.name != NULL); 25 | 26 | /* so now we have our DH structure, generator g, order q, modulus p 27 | Now we need a random exponent [mod q] and it's power g^x mod p 28 | */ 29 | /* private key x should be from range: 1 <= x <= q-1 (see FIPS 186-4 B.1.2) */ 30 | if ((err = rand_bn_upto(key->x, key->q, prng, wprng)) != CRYPT_OK) { return err; } 31 | if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { return err; } 32 | key->type = PK_PRIVATE; 33 | 34 | return CRYPT_OK; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/dsa/dsa_make_key.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file dsa_make_key.c 7 | DSA implementation, generate a DSA key 8 | */ 9 | 10 | #ifdef LTC_MDSA 11 | 12 | /** 13 | Old-style creation of a DSA key 14 | @param prng An active PRNG state 15 | @param wprng The index of the PRNG desired 16 | @param group_size Size of the multiplicative group (octets) 17 | @param modulus_size Size of the modulus (octets) 18 | @param key [out] Where to store the created key 19 | @return CRYPT_OK if successful. 20 | */ 21 | int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key) 22 | { 23 | int err; 24 | 25 | if ((err = dsa_generate_pqg(prng, wprng, group_size, modulus_size, key)) != CRYPT_OK) { return err; } 26 | if ((err = dsa_generate_key(prng, wprng, key)) != CRYPT_OK) { return err; } 27 | 28 | return CRYPT_OK; 29 | } 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ec25519/ec25519_crypto_ctx.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ec25519_crypto_ctx.c 7 | curve25519 crypto context helper 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | int ec25519_crypto_ctx(unsigned char *out, unsigned long *outlen, unsigned char flag, const unsigned char *ctx, unsigned long ctxlen) 13 | { 14 | unsigned char *buf = out; 15 | 16 | const char *prefix = "SigEd25519 no Ed25519 collisions"; 17 | const unsigned long prefix_len = XSTRLEN(prefix); 18 | const unsigned char ctxlen8 = (unsigned char)ctxlen; 19 | 20 | if (ctxlen > 255u) return CRYPT_INPUT_TOO_LONG; 21 | if (*outlen < prefix_len + 2u + ctxlen) return CRYPT_BUFFER_OVERFLOW; 22 | 23 | XMEMCPY(buf, prefix, prefix_len); 24 | buf += prefix_len; 25 | XMEMCPY(buf, &flag, 1); 26 | buf++; 27 | XMEMCPY(buf, &ctxlen8, 1); 28 | buf++; 29 | 30 | if (ctxlen > 0u) { 31 | LTC_ARGCHK(ctx != NULL); 32 | XMEMCPY(buf, ctx, ctxlen); 33 | buf += ctxlen; 34 | } 35 | 36 | *outlen = buf-out; 37 | 38 | return CRYPT_OK; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ecc/ecc_ansi_x963_export.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | /** 7 | @file ecc_ansi_x963_export.c 8 | ECC Crypto, Tom St Denis 9 | */ 10 | 11 | #ifdef LTC_MECC 12 | 13 | /** ECC X9.63 (Sec. 4.3.6) uncompressed export 14 | @param key Key to export 15 | @param out [out] destination of export 16 | @param outlen [in/out] Length of destination and final output size 17 | Return CRYPT_OK on success 18 | */ 19 | int ecc_ansi_x963_export(const ecc_key *key, unsigned char *out, unsigned long *outlen) 20 | { 21 | return ecc_get_key(out, outlen, PK_PUBLIC, key); 22 | } 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ecc/ecc_ansi_x963_import.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | /** 7 | @file ecc_ansi_x963_import.c 8 | ECC Crypto, Tom St Denis 9 | */ 10 | 11 | #ifdef LTC_MECC 12 | 13 | /** Import an ANSI X9.63 format public key 14 | @param in The input data to read 15 | @param inlen The length of the input data 16 | @param key [out] destination to store imported key \ 17 | */ 18 | int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key) 19 | { 20 | return ecc_ansi_x963_import_ex(in, inlen, key, NULL); 21 | } 22 | 23 | int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_curve *cu) 24 | { 25 | int err; 26 | 27 | LTC_ARGCHK(in != NULL); 28 | LTC_ARGCHK(key != NULL); 29 | 30 | /* must be odd */ 31 | if ((inlen & 1) == 0) { 32 | return CRYPT_INVALID_ARG; 33 | } 34 | 35 | /* initialize key->dp */ 36 | if (cu == NULL) { 37 | /* this case works only for uncompressed public keys */ 38 | if ((err = ecc_set_curve_by_size((inlen-1)>>1, key)) != CRYPT_OK) { return err; } 39 | } 40 | else { 41 | /* this one works for both compressed / uncompressed pubkeys */ 42 | if ((err = ecc_set_curve(cu, key)) != CRYPT_OK) { return err; } 43 | } 44 | 45 | /* load public key */ 46 | if ((err = ecc_set_key((unsigned char *)in, inlen, PK_PUBLIC, key)) != CRYPT_OK) { return err; } 47 | 48 | /* we're done */ 49 | return CRYPT_OK; 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ecc/ecc_free.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | /** 7 | @file ecc_free.c 8 | ECC Crypto, Tom St Denis 9 | */ 10 | 11 | #ifdef LTC_MECC 12 | 13 | /** 14 | Free an ECC key from memory 15 | @param key The key you wish to free 16 | */ 17 | void ecc_free(ecc_key *key) 18 | { 19 | LTC_ARGCHKVD(key != NULL); 20 | 21 | mp_cleanup_multi(&key->dp.prime, &key->dp.order, 22 | &key->dp.A, &key->dp.B, 23 | &key->dp.base.x, &key->dp.base.y, &key->dp.base.z, 24 | &key->pubkey.x, &key->pubkey.y, &key->pubkey.z, 25 | &key->k, NULL); 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ecc/ecc_get_oid_str.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_MECC 7 | 8 | /** Extract OID as a string from ECC key 9 | @param out [out] destination buffer 10 | @param outlen [in/out] Length of destination buffer and final output size (without terminating NUL byte) 11 | @param key The ECC key 12 | Return CRYPT_OK on success 13 | */ 14 | 15 | int ecc_get_oid_str(char *out, unsigned long *outlen, const ecc_key *key) 16 | { 17 | LTC_ARGCHK(key != NULL); 18 | 19 | return pk_oid_num_to_str(key->dp.oid, key->dp.oidlen, out, outlen); 20 | } 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ecc/ecc_get_size.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | /** 7 | @file ecc_get_size.c 8 | ECC Crypto, Tom St Denis 9 | */ 10 | 11 | #ifdef LTC_MECC 12 | 13 | /** 14 | Get the size of an ECC key 15 | @param key The key to get the size of 16 | @return The size (octets) of the key or INT_MAX on error 17 | */ 18 | int ecc_get_size(const ecc_key *key) 19 | { 20 | if (key == NULL) { 21 | return INT_MAX; 22 | } 23 | return key->dp.size; 24 | } 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ecc/ecc_sizes.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | /** 7 | @file ecc_sizes.c 8 | ECC Crypto, Tom St Denis 9 | */ 10 | 11 | #ifdef LTC_MECC 12 | 13 | void ecc_sizes(int *low, int *high) 14 | { 15 | int i, size; 16 | void *prime; 17 | 18 | LTC_ARGCHKVD(low != NULL); 19 | LTC_ARGCHKVD(high != NULL); 20 | 21 | *low = INT_MAX; 22 | *high = 0; 23 | 24 | if (mp_init(&prime) == CRYPT_OK) { 25 | for (i = 0; ltc_ecc_curves[i].prime != NULL; i++) { 26 | if (mp_read_radix(prime, ltc_ecc_curves[i].prime, 16) == CRYPT_OK) { 27 | size = mp_unsigned_bin_size(prime); 28 | if (size < *low) *low = size; 29 | if (size > *high) *high = size; 30 | } 31 | } 32 | mp_clear(prime); 33 | } 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ecc/ltc_ecc_is_point_at_infinity.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_MECC 7 | 8 | /* http://crypto.stackexchange.com/questions/41468/point-at-infinity-for-jacobian-coordinates 9 | * a point at infinity is any point (x,y,0) such that y^2 == x^3, except (0,0,0) 10 | */ 11 | 12 | int ltc_ecc_is_point_at_infinity(const ecc_point *P, void *modulus, int *retval) 13 | { 14 | int err; 15 | void *x3, *y2; 16 | 17 | /* trivial case */ 18 | if (!mp_iszero(P->z)) { 19 | *retval = 0; 20 | return CRYPT_OK; 21 | } 22 | 23 | /* point (0,0,0) is not at infinity */ 24 | if (mp_iszero(P->x) && mp_iszero(P->y)) { 25 | *retval = 0; 26 | return CRYPT_OK; 27 | } 28 | 29 | /* initialize */ 30 | if ((err = mp_init_multi(&x3, &y2, LTC_NULL)) != CRYPT_OK) goto done; 31 | 32 | /* compute y^2 */ 33 | if ((err = mp_mulmod(P->y, P->y, modulus, y2)) != CRYPT_OK) goto cleanup; 34 | 35 | /* compute x^3 */ 36 | if ((err = mp_mulmod(P->x, P->x, modulus, x3)) != CRYPT_OK) goto cleanup; 37 | if ((err = mp_mulmod(P->x, x3, modulus, x3)) != CRYPT_OK) goto cleanup; 38 | 39 | /* test y^2 == x^3 */ 40 | err = CRYPT_OK; 41 | if ((mp_cmp(x3, y2) == LTC_MP_EQ) && !mp_iszero(y2)) { 42 | *retval = 1; 43 | } else { 44 | *retval = 0; 45 | } 46 | 47 | cleanup: 48 | mp_clear_multi(x3, y2, LTC_NULL); 49 | done: 50 | return err; 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ed25519/ed25519_export.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ed25519_export.c 7 | Export an Ed25519 key to a binary packet, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | /** 13 | Export an Ed25519 key to a binary packet 14 | @param out [out] The destination for the key 15 | @param outlen [in/out] The max size and resulting size of the Ed25519 key 16 | @param type Which type of key (PK_PRIVATE, PK_PUBLIC|PK_STD or PK_PUBLIC) 17 | @param key The key you wish to export 18 | @return CRYPT_OK if successful 19 | */ 20 | int ed25519_export( unsigned char *out, unsigned long *outlen, 21 | int which, 22 | const curve25519_key *key) 23 | { 24 | LTC_ARGCHK(key != NULL); 25 | 26 | if (key->algo != LTC_OID_ED25519) return CRYPT_PK_INVALID_TYPE; 27 | 28 | return ec25519_export(out, outlen, which, key); 29 | } 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ed25519/ed25519_import.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ed25519_import.c 7 | Import a Ed25519 key from a SubjectPublicKeyInfo, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | /** 13 | Import an Ed25519 public key 14 | @param in The packet to read 15 | @param inlen The length of the input packet 16 | @param key [out] Where to import the key to 17 | @return CRYPT_OK if successful, on error all allocated memory is freed automatically 18 | */ 19 | int ed25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key) 20 | { 21 | int err; 22 | unsigned long key_len; 23 | 24 | LTC_ARGCHK(in != NULL); 25 | LTC_ARGCHK(key != NULL); 26 | 27 | key_len = sizeof(key->pub); 28 | if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_ED25519, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) { 29 | key->type = PK_PUBLIC; 30 | key->algo = LTC_OID_ED25519; 31 | } 32 | return err; 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ed25519/ed25519_import_pkcs8.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ed25519_import_pkcs8.c 7 | Import an Ed25519 key in PKCS#8 format, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | /** 13 | Import an Ed25519 private key in PKCS#8 format 14 | @param in The DER-encoded PKCS#8-formatted private key 15 | @param inlen The length of the input data 16 | @param passwd The password to decrypt the private key 17 | @param passwdlen Password's length (octets) 18 | @param key [out] Where to import the key to 19 | @return CRYPT_OK if successful, on error all allocated memory is freed automatically 20 | */ 21 | int ed25519_import_pkcs8(const unsigned char *in, unsigned long inlen, 22 | const void *pwd, unsigned long pwdlen, 23 | curve25519_key *key) 24 | { 25 | return ec25519_import_pkcs8(in, inlen, pwd, pwdlen, LTC_OID_ED25519, tweetnacl_crypto_sk_to_pk, key); 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ed25519/ed25519_import_raw.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ed25519_import_raw.c 7 | Set the parameters of an Ed25519 key, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | /** 13 | Set the parameters of an Ed25519 key 14 | 15 | @param in The key 16 | @param inlen The length of the key 17 | @param which Which type of key (PK_PRIVATE or PK_PUBLIC) 18 | @param key [out] Destination of the key 19 | @return CRYPT_OK if successful 20 | */ 21 | int ed25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key) 22 | { 23 | LTC_ARGCHK(in != NULL); 24 | LTC_ARGCHK(inlen == 32uL); 25 | LTC_ARGCHK(key != NULL); 26 | 27 | if (which == PK_PRIVATE) { 28 | XMEMCPY(key->priv, in, sizeof(key->priv)); 29 | tweetnacl_crypto_sk_to_pk(key->pub, key->priv); 30 | } else if (which == PK_PUBLIC) { 31 | XMEMCPY(key->pub, in, sizeof(key->pub)); 32 | } else { 33 | return CRYPT_INVALID_ARG; 34 | } 35 | key->algo = LTC_OID_ED25519; 36 | key->type = which; 37 | 38 | return CRYPT_OK; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ed25519/ed25519_import_x509.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ed25519_import_x509.c 7 | Import an Ed25519 key from a X.509 certificate, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | static int s_ed25519_decode(const unsigned char *in, unsigned long inlen, curve25519_key *key) 13 | { 14 | if (inlen != sizeof(key->pub)) return CRYPT_PK_INVALID_SIZE; 15 | XMEMCPY(key->pub, in, sizeof(key->pub)); 16 | return CRYPT_OK; 17 | } 18 | 19 | /** 20 | Import an Ed25519 public key from a X.509 certificate 21 | @param in The DER encoded X.509 certificate 22 | @param inlen The length of the certificate 23 | @param key [out] Where to import the key to 24 | @return CRYPT_OK if successful, on error all allocated memory is freed automatically 25 | */ 26 | int ed25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key) 27 | { 28 | int err; 29 | 30 | LTC_ARGCHK(in != NULL); 31 | LTC_ARGCHK(key != NULL); 32 | 33 | if ((err = x509_decode_public_key_from_certificate(in, inlen, 34 | LTC_OID_ED25519, 35 | LTC_ASN1_EOL, NULL, NULL, 36 | (public_key_decode_cb)s_ed25519_decode, key)) != CRYPT_OK) { 37 | return err; 38 | } 39 | key->type = PK_PUBLIC; 40 | key->algo = LTC_OID_ED25519; 41 | 42 | return err; 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/ed25519/ed25519_make_key.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file ed25519_make_key.c 7 | Create an Ed25519 key, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | /** 13 | Create an Ed25519 key 14 | @param prng An active PRNG state 15 | @param wprng The index of the PRNG desired 16 | @param key [out] Destination of a newly created private key pair 17 | @return CRYPT_OK if successful 18 | */ 19 | int ed25519_make_key(prng_state *prng, int wprng, curve25519_key *key) 20 | { 21 | int err; 22 | 23 | LTC_ARGCHK(prng != NULL); 24 | LTC_ARGCHK(key != NULL); 25 | 26 | if ((err = tweetnacl_crypto_sign_keypair(prng, wprng, key->pub, key->priv)) != CRYPT_OK) { 27 | return err; 28 | } 29 | 30 | key->type = PK_PRIVATE; 31 | key->algo = LTC_OID_ED25519; 32 | 33 | return err; 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/pkcs1/pkcs_1_i2osp.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file pkcs_1_i2osp.c 7 | Integer to Octet I2OSP, Tom St Denis 8 | */ 9 | 10 | #ifdef LTC_PKCS_1 11 | 12 | /* always stores the same # of bytes, pads with leading zero bytes 13 | as required 14 | */ 15 | 16 | /** 17 | PKCS #1 Integer to binary 18 | @param n The integer to store 19 | @param modulus_len The length of the RSA modulus 20 | @param out [out] The destination for the integer 21 | @return CRYPT_OK if successful 22 | */ 23 | int pkcs_1_i2osp(void *n, unsigned long modulus_len, unsigned char *out) 24 | { 25 | unsigned long size; 26 | 27 | size = mp_unsigned_bin_size(n); 28 | 29 | if (size > modulus_len) { 30 | return CRYPT_BUFFER_OVERFLOW; 31 | } 32 | 33 | /* store it */ 34 | zeromem(out, modulus_len); 35 | return mp_to_unsigned_bin(n, out+(modulus_len-size)); 36 | } 37 | 38 | #endif /* LTC_PKCS_1 */ 39 | 40 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/pkcs1/pkcs_1_os2ip.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file pkcs_1_os2ip.c 7 | Octet to Integer OS2IP, Tom St Denis 8 | */ 9 | #ifdef LTC_PKCS_1 10 | 11 | /** 12 | Read a binary string into an mp_int 13 | @param n [out] The mp_int destination 14 | @param in The binary string to read 15 | @param inlen The length of the binary string 16 | @return CRYPT_OK if successful 17 | */ 18 | int pkcs_1_os2ip(void *n, unsigned char *in, unsigned long inlen) 19 | { 20 | return mp_read_unsigned_bin(n, in, inlen); 21 | } 22 | 23 | #endif /* LTC_PKCS_1 */ 24 | 25 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/rsa/rsa_get_size.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file rsa_get_size.c 7 | Retrieve the size of an RSA key, Steffen Jaeckel. 8 | */ 9 | 10 | #ifdef LTC_MRSA 11 | 12 | /** 13 | Retrieve the size in bytes of an RSA key. 14 | @param key The RSA key 15 | @return The size in bytes of the RSA key or INT_MAX on error. 16 | */ 17 | int rsa_get_size(const rsa_key *key) 18 | { 19 | int ret = INT_MAX; 20 | LTC_ARGCHK(key != NULL); 21 | 22 | if (key) 23 | { 24 | ret = mp_unsigned_bin_size(key->N); 25 | } /* if */ 26 | 27 | return ret; 28 | } 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/rsa/rsa_sign_saltlen_get.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file rsa_sign_saltlen_get.c 7 | Retrieve the maximum size of the salt, Steffen Jaeckel. 8 | */ 9 | 10 | #ifdef LTC_MRSA 11 | 12 | /** 13 | Retrieve the maximum possible size of the salt when creating a PKCS#1 PSS signature. 14 | @param padding Type of padding (LTC_PKCS_1_PSS only) 15 | @param hash_idx The index of the desired hash 16 | @param key The RSA key 17 | @return The maximum salt length in bytes or INT_MAX on error. 18 | */ 19 | int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, const rsa_key *key) 20 | { 21 | int ret = INT_MAX; 22 | LTC_ARGCHK(key != NULL); 23 | 24 | if ((hash_is_valid(hash_idx) == CRYPT_OK) && 25 | (padding == LTC_PKCS_1_PSS)) 26 | { 27 | ret = rsa_get_size(key); 28 | if (ret < INT_MAX) 29 | { 30 | ret -= (hash_descriptor[hash_idx].hashsize + 2); 31 | } /* if */ 32 | } /* if */ 33 | 34 | return ret; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/x25519/x25519_export.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file x25519_export.c 7 | Export a X25519 key to a binary packet, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | /** 13 | Export a X25519 key to a binary packet 14 | @param out [out] The destination for the key 15 | @param outlen [in/out] The max size and resulting size of the X25519 key 16 | @param type Which type of key (PK_PRIVATE, PK_PUBLIC|PK_STD or PK_PUBLIC) 17 | @param key The key you wish to export 18 | @return CRYPT_OK if successful 19 | */ 20 | int x25519_export( unsigned char *out, unsigned long *outlen, 21 | int which, 22 | const curve25519_key *key) 23 | { 24 | LTC_ARGCHK(key != NULL); 25 | 26 | if (key->algo != LTC_OID_X25519) return CRYPT_PK_INVALID_TYPE; 27 | 28 | return ec25519_export(out, outlen, which, key); 29 | } 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/x25519/x25519_import.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file x25519_import.c 7 | Import a X25519 key from a SubjectPublicKeyInfo, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | /** 13 | Import a X25519 key 14 | @param in The packet to read 15 | @param inlen The length of the input packet 16 | @param key [out] Where to import the key to 17 | @return CRYPT_OK if successful, on error all allocated memory is freed automatically 18 | */ 19 | int x25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key) 20 | { 21 | int err; 22 | unsigned long key_len; 23 | 24 | LTC_ARGCHK(in != NULL); 25 | LTC_ARGCHK(key != NULL); 26 | 27 | key_len = sizeof(key->pub); 28 | if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_X25519, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) { 29 | key->type = PK_PUBLIC; 30 | key->algo = LTC_OID_X25519; 31 | } 32 | return err; 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/x25519/x25519_import_pkcs8.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file x25519_import_pkcs8.c 7 | Import a X25519 key in PKCS#8 format, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | /** 13 | Import a X25519 private key in PKCS#8 format 14 | @param in The DER-encoded PKCS#8-formatted private key 15 | @param inlen The length of the input data 16 | @param passwd The password to decrypt the private key 17 | @param passwdlen Password's length (octets) 18 | @param key [out] Where to import the key to 19 | @return CRYPT_OK if successful, on error all allocated memory is freed automatically 20 | */ 21 | int x25519_import_pkcs8(const unsigned char *in, unsigned long inlen, 22 | const void *pwd, unsigned long pwdlen, 23 | curve25519_key *key) 24 | { 25 | return ec25519_import_pkcs8(in, inlen, pwd, pwdlen, LTC_OID_X25519, tweetnacl_crypto_scalarmult_base, key); 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/x25519/x25519_import_raw.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file x25519_import_raw.c 7 | Set the parameters of a X25519 key, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | /** 13 | Set the parameters of a X25519 key 14 | 15 | @param in The key 16 | @param inlen The length of the key 17 | @param which Which type of key (PK_PRIVATE or PK_PUBLIC) 18 | @param key [out] Destination of the key 19 | @return CRYPT_OK if successful 20 | */ 21 | int x25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key) 22 | { 23 | LTC_ARGCHK(in != NULL); 24 | LTC_ARGCHK(inlen == 32uL); 25 | LTC_ARGCHK(key != NULL); 26 | 27 | if (which == PK_PRIVATE) { 28 | XMEMCPY(key->priv, in, sizeof(key->priv)); 29 | tweetnacl_crypto_scalarmult_base(key->pub, key->priv); 30 | } else if (which == PK_PUBLIC) { 31 | XMEMCPY(key->pub, in, sizeof(key->pub)); 32 | } else { 33 | return CRYPT_INVALID_ARG; 34 | } 35 | key->algo = LTC_OID_X25519; 36 | key->type = which; 37 | 38 | return CRYPT_OK; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/x25519/x25519_import_x509.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file x25519_import_x509.c 7 | Import a X25519 key from a X.509 certificate, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | static int s_x25519_decode(const unsigned char *in, unsigned long inlen, curve25519_key *key) 13 | { 14 | if (inlen != sizeof(key->pub)) return CRYPT_PK_INVALID_SIZE; 15 | XMEMCPY(key->pub, in, sizeof(key->pub)); 16 | return CRYPT_OK; 17 | } 18 | 19 | /** 20 | Import a X25519 public key from a X.509 certificate 21 | @param in The DER encoded X.509 certificate 22 | @param inlen The length of the certificate 23 | @param key [out] Where to import the key to 24 | @return CRYPT_OK if successful, on error all allocated memory is freed automatically 25 | */ 26 | int x25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key) 27 | { 28 | int err; 29 | 30 | LTC_ARGCHK(in != NULL); 31 | LTC_ARGCHK(key != NULL); 32 | 33 | if ((err = x509_decode_public_key_from_certificate(in, inlen, 34 | LTC_OID_X25519, 35 | LTC_ASN1_EOL, NULL, NULL, 36 | (public_key_decode_cb)s_x25519_decode, key)) != CRYPT_OK) { 37 | return err; 38 | } 39 | key->type = PK_PUBLIC; 40 | key->algo = LTC_OID_X25519; 41 | 42 | return err; 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/x25519/x25519_make_key.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file x25519_make_key.c 7 | Create a X25519 key, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | /** 13 | Create a X25519 key 14 | @param prng An active PRNG state 15 | @param wprng The index of the PRNG desired 16 | @param key [out] Destination of a newly created private key pair 17 | @return CRYPT_OK if successful 18 | */ 19 | int x25519_make_key(prng_state *prng, int wprng, curve25519_key *key) 20 | { 21 | int err; 22 | 23 | LTC_ARGCHK(prng != NULL); 24 | LTC_ARGCHK(key != NULL); 25 | 26 | if ((err = prng_is_valid(wprng)) != CRYPT_OK) { 27 | return err; 28 | } 29 | 30 | if (prng_descriptor[wprng].read(key->priv, sizeof(key->priv), prng) != sizeof(key->priv)) { 31 | return CRYPT_ERROR_READPRNG; 32 | } 33 | 34 | tweetnacl_crypto_scalarmult_base(key->pub, key->priv); 35 | 36 | key->type = PK_PRIVATE; 37 | key->algo = LTC_OID_X25519; 38 | 39 | return err; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/pk/x25519/x25519_shared_secret.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | #include "tomcrypt_private.h" 4 | 5 | /** 6 | @file x25519_shared_secret.c 7 | Create a X25519 shared secret, Steffen Jaeckel 8 | */ 9 | 10 | #ifdef LTC_CURVE25519 11 | 12 | /** 13 | Create a X25519 shared secret. 14 | @param private_key The private X25519 key in the pair 15 | @param public_key The public X25519 key in the pair 16 | @param out [out] The destination of the shared data 17 | @param outlen [in/out] The max size and resulting size of the shared data. 18 | @return CRYPT_OK if successful 19 | */ 20 | int x25519_shared_secret(const curve25519_key *private_key, 21 | const curve25519_key *public_key, 22 | unsigned char *out, unsigned long *outlen) 23 | { 24 | LTC_ARGCHK(private_key != NULL); 25 | LTC_ARGCHK(public_key != NULL); 26 | LTC_ARGCHK(out != NULL); 27 | LTC_ARGCHK(outlen != NULL); 28 | 29 | if(private_key->type != PK_PRIVATE) return CRYPT_PK_INVALID_TYPE; 30 | 31 | if(*outlen < 32uL) { 32 | *outlen = 32uL; 33 | return CRYPT_BUFFER_OVERFLOW; 34 | } 35 | 36 | tweetnacl_crypto_scalarmult(out, private_key->priv, public_key->pub); 37 | *outlen = 32uL; 38 | 39 | return CRYPT_OK; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/chacha/chacha_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_CHACHA 7 | 8 | /** 9 | Terminate and clear ChaCha state 10 | @param st The ChaCha state 11 | @return CRYPT_OK on success 12 | */ 13 | int chacha_done(chacha_state *st) 14 | { 15 | LTC_ARGCHK(st != NULL); 16 | zeromem(st, sizeof(chacha_state)); 17 | return CRYPT_OK; 18 | } 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/chacha/chacha_ivctr32.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /* The implementation is based on: 5 | * chacha-ref.c version 20080118 6 | * Public domain from D. J. Bernstein 7 | */ 8 | 9 | #include "tomcrypt_private.h" 10 | 11 | #ifdef LTC_CHACHA 12 | 13 | /** 14 | Set IV + counter data to the ChaCha state 15 | @param st The ChaCha20 state 16 | @param iv The IV data to add 17 | @param ivlen The length of the IV (must be 12) 18 | @param counter 32bit (unsigned) initial counter value 19 | @return CRYPT_OK on success 20 | */ 21 | int chacha_ivctr32(chacha_state *st, const unsigned char *iv, unsigned long ivlen, ulong32 counter) 22 | { 23 | LTC_ARGCHK(st != NULL); 24 | LTC_ARGCHK(iv != NULL); 25 | /* 96bit IV + 32bit counter */ 26 | LTC_ARGCHK(ivlen == 12); 27 | 28 | st->input[12] = counter; 29 | LOAD32L(st->input[13], iv + 0); 30 | LOAD32L(st->input[14], iv + 4); 31 | LOAD32L(st->input[15], iv + 8); 32 | st->ksleft = 0; 33 | st->ivlen = ivlen; 34 | return CRYPT_OK; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/chacha/chacha_ivctr64.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /* The implementation is based on: 5 | * chacha-ref.c version 20080118 6 | * Public domain from D. J. Bernstein 7 | */ 8 | 9 | #include "tomcrypt_private.h" 10 | 11 | #ifdef LTC_CHACHA 12 | 13 | /** 14 | Set IV + counter data to the ChaCha state 15 | @param st The ChaCha20 state 16 | @param iv The IV data to add 17 | @param ivlen The length of the IV (must be 8) 18 | @param counter 64bit (unsigned) initial counter value 19 | @return CRYPT_OK on success 20 | */ 21 | int chacha_ivctr64(chacha_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 counter) 22 | { 23 | LTC_ARGCHK(st != NULL); 24 | LTC_ARGCHK(iv != NULL); 25 | /* 64bit IV + 64bit counter */ 26 | LTC_ARGCHK(ivlen == 8); 27 | 28 | st->input[12] = (ulong32)(counter & 0xFFFFFFFF); 29 | st->input[13] = (ulong32)(counter >> 32); 30 | LOAD32L(st->input[14], iv + 0); 31 | LOAD32L(st->input[15], iv + 4); 32 | st->ksleft = 0; 33 | st->ivlen = ivlen; 34 | return CRYPT_OK; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/chacha/chacha_keystream.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /* The implementation is based on: 5 | * chacha-ref.c version 20080118 6 | * Public domain from D. J. Bernstein 7 | */ 8 | 9 | #include "tomcrypt_private.h" 10 | 11 | #ifdef LTC_CHACHA 12 | 13 | /** 14 | Generate a stream of random bytes via ChaCha 15 | @param st The ChaCha20 state 16 | @param out [out] The output buffer 17 | @param outlen The output length 18 | @return CRYPT_OK on success 19 | */ 20 | int chacha_keystream(chacha_state *st, unsigned char *out, unsigned long outlen) 21 | { 22 | if (outlen == 0) return CRYPT_OK; /* nothing to do */ 23 | LTC_ARGCHK(out != NULL); 24 | XMEMSET(out, 0, outlen); 25 | return chacha_crypt(st, out, outlen, out); 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/chacha/chacha_memory.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_CHACHA 7 | 8 | /** 9 | Encrypt (or decrypt) bytes of ciphertext (or plaintext) with ChaCha 10 | @param key The key 11 | @param keylen The key length 12 | @param iv The initial vector 13 | @param ivlen The initial vector length 14 | @param datain The plaintext (or ciphertext) 15 | @param datalen The length of the input and output (octets) 16 | @param rounds The number of rounds 17 | @param dataout [out] The ciphertext (or plaintext) 18 | @return CRYPT_OK if successful 19 | */ 20 | int chacha_memory(const unsigned char *key, unsigned long keylen, unsigned long rounds, 21 | const unsigned char *iv, unsigned long ivlen, ulong64 counter, 22 | const unsigned char *datain, unsigned long datalen, unsigned char *dataout) 23 | { 24 | chacha_state st; 25 | int err; 26 | 27 | LTC_ARGCHK(ivlen <= 8 || counter < 4294967296); /* 2**32 */ 28 | 29 | if ((err = chacha_setup(&st, key, keylen, rounds)) != CRYPT_OK) goto WIPE_KEY; 30 | if (ivlen > 8) { 31 | if ((err = chacha_ivctr32(&st, iv, ivlen, (ulong32)counter)) != CRYPT_OK) goto WIPE_KEY; 32 | } else { 33 | if ((err = chacha_ivctr64(&st, iv, ivlen, counter)) != CRYPT_OK) goto WIPE_KEY; 34 | } 35 | err = chacha_crypt(&st, datain, datalen, dataout); 36 | WIPE_KEY: 37 | chacha_done(&st); 38 | return err; 39 | } 40 | 41 | #endif /* LTC_CHACHA */ 42 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/rabbit/rabbit_memory.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /* The implementation is based on: 5 | * chacha-ref.c version 20080118 6 | * Public domain from D. J. Bernstein 7 | */ 8 | 9 | #include "tomcrypt_private.h" 10 | 11 | #ifdef LTC_RABBIT 12 | 13 | /** 14 | Encrypt (or decrypt) bytes of ciphertext (or plaintext) with Rabbit 15 | @param key The key 16 | @param keylen The key length 17 | @param iv The initial vector 18 | @param ivlen The initial vector length 19 | @param datain The plaintext (or ciphertext) 20 | @param datalen The length of the input and output (octets) 21 | @param dataout [out] The ciphertext (or plaintext) 22 | @return CRYPT_OK if successful 23 | */ 24 | int rabbit_memory(const unsigned char *key, unsigned long keylen, 25 | const unsigned char *iv, unsigned long ivlen, 26 | const unsigned char *datain, unsigned long datalen, 27 | unsigned char *dataout) 28 | { 29 | rabbit_state st; 30 | int err; 31 | 32 | if ((err = rabbit_setup(&st, key, keylen)) != CRYPT_OK) goto WIPE_KEY; 33 | if ((err = rabbit_setiv(&st, iv, ivlen)) != CRYPT_OK) goto WIPE_KEY; 34 | err = rabbit_crypt(&st, datain, datalen, dataout); 35 | WIPE_KEY: 36 | rabbit_done(&st); 37 | return err; 38 | } 39 | 40 | #endif /* LTC_RABBIT */ 41 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/rc4/rc4_stream_memory.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_RC4_STREAM 7 | 8 | /** 9 | Encrypt (or decrypt) bytes of ciphertext (or plaintext) with RC4 10 | @param key The key 11 | @param keylen The key length 12 | @param datain The plaintext (or ciphertext) 13 | @param datalen The length of the input and output (octets) 14 | @param dataout [out] The ciphertext (or plaintext) 15 | @return CRYPT_OK if successful 16 | */ 17 | int rc4_stream_memory(const unsigned char *key, unsigned long keylen, 18 | const unsigned char *datain, unsigned long datalen, 19 | unsigned char *dataout) 20 | { 21 | rc4_state st; 22 | int err; 23 | 24 | if ((err = rc4_stream_setup(&st, key, keylen)) != CRYPT_OK) goto WIPE_KEY; 25 | err = rc4_stream_crypt(&st, datain, datalen, dataout); 26 | WIPE_KEY: 27 | rc4_stream_done(&st); 28 | return err; 29 | } 30 | 31 | #endif /* LTC_RC4_STREAM */ 32 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/rc4/rc4_test.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_RC4_STREAM 7 | 8 | int rc4_stream_test(void) 9 | { 10 | #ifndef LTC_TEST 11 | return CRYPT_NOP; 12 | #else 13 | rc4_state st; 14 | int err; 15 | const unsigned char key[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; 16 | const unsigned char pt[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; 17 | const unsigned char ct[] = { 0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96 }; 18 | unsigned char buf[10]; 19 | 20 | if ((err = rc4_stream_setup(&st, key, sizeof(key))) != CRYPT_OK) return err; 21 | if ((err = rc4_stream_crypt(&st, pt, sizeof(pt), buf)) != CRYPT_OK) return err; 22 | if (compare_testvector(buf, sizeof(ct), ct, sizeof(ct), "RC4-TV1", 0)) return CRYPT_FAIL_TESTVECTOR; 23 | if ((err = rc4_stream_done(&st)) != CRYPT_OK) return err; 24 | 25 | /* crypt in a single call */ 26 | if ((err = rc4_stream_memory(key, sizeof(key), pt, sizeof(pt), buf)) != CRYPT_OK) return err; 27 | if (compare_testvector(buf, sizeof(ct), ct, sizeof(ct), "RC4-TV2", 0)) return CRYPT_FAIL_TESTVECTOR; 28 | 29 | return CRYPT_OK; 30 | #endif 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/salsa20/salsa20_done.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_SALSA20 7 | 8 | /** 9 | Terminate and clear Salsa20 state 10 | @param st The Salsa20 state 11 | @return CRYPT_OK on success 12 | */ 13 | int salsa20_done(salsa20_state *st) 14 | { 15 | LTC_ARGCHK(st != NULL); 16 | zeromem(st, sizeof(salsa20_state)); 17 | return CRYPT_OK; 18 | } 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/salsa20/salsa20_ivctr64.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /* The implementation is based on: 5 | * "Salsa20 specification", http://cr.yp.to/snuffle/spec.pdf 6 | * and salsa20-ref.c version 20051118 7 | * Public domain from D. J. Bernstein 8 | */ 9 | 10 | #include "tomcrypt_private.h" 11 | 12 | #ifdef LTC_SALSA20 13 | 14 | /** 15 | Set IV + counter data to the Salsa20 state 16 | @param st The Salsa20 state 17 | @param iv The IV data to add 18 | @param ivlen The length of the IV (must be 8) 19 | @param counter 64bit (unsigned) initial counter value 20 | @return CRYPT_OK on success 21 | */ 22 | int salsa20_ivctr64(salsa20_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 counter) 23 | { 24 | LTC_ARGCHK(st != NULL); 25 | LTC_ARGCHK(iv != NULL); 26 | /* Salsa20: 64-bit IV (nonce) + 64-bit counter */ 27 | LTC_ARGCHK(ivlen == 8); 28 | 29 | LOAD32L(st->input[6], iv + 0); 30 | LOAD32L(st->input[7], iv + 4); 31 | st->input[8] = (ulong32)(counter & 0xFFFFFFFF); 32 | st->input[9] = (ulong32)(counter >> 32); 33 | st->ksleft = 0; 34 | st->ivlen = ivlen; 35 | return CRYPT_OK; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/salsa20/salsa20_keystream.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | /* The implementation is based on: 5 | * "Salsa20 specification", http://cr.yp.to/snuffle/spec.pdf 6 | * and salsa20-ref.c version 20051118 7 | * Public domain from D. J. Bernstein 8 | */ 9 | 10 | #include "tomcrypt_private.h" 11 | 12 | #ifdef LTC_SALSA20 13 | 14 | /** 15 | Generate a stream of random bytes via Salsa20 16 | @param st The Salsa20 state 17 | @param out [out] The output buffer 18 | @param outlen The output length 19 | @return CRYPT_OK on success 20 | */ 21 | int salsa20_keystream(salsa20_state *st, unsigned char *out, unsigned long outlen) 22 | { 23 | if (outlen == 0) return CRYPT_OK; /* nothing to do */ 24 | LTC_ARGCHK(out != NULL); 25 | XMEMSET(out, 0, outlen); 26 | return salsa20_crypt(st, out, outlen, out); 27 | } 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/salsa20/salsa20_memory.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_SALSA20 7 | 8 | /** 9 | Encrypt (or decrypt) bytes of ciphertext (or plaintext) with Salsa20 10 | @param key The key 11 | @param keylen The key length 12 | @param iv The initial vector 13 | @param ivlen The initial vector length 14 | @param datain The plaintext (or ciphertext) 15 | @param datalen The length of the input and output (octets) 16 | @param rounds The number of rounds 17 | @param dataout [out] The ciphertext (or plaintext) 18 | @return CRYPT_OK if successful 19 | */ 20 | int salsa20_memory(const unsigned char *key, unsigned long keylen, unsigned long rounds, 21 | const unsigned char *iv, unsigned long ivlen, ulong64 counter, 22 | const unsigned char *datain, unsigned long datalen, unsigned char *dataout) 23 | { 24 | salsa20_state st; 25 | int err; 26 | 27 | if ((err = salsa20_setup(&st, key, keylen, rounds)) != CRYPT_OK) goto WIPE_KEY; 28 | if ((err = salsa20_ivctr64(&st, iv, ivlen, counter)) != CRYPT_OK) goto WIPE_KEY; 29 | err = salsa20_crypt(&st, datain, datalen, dataout); 30 | WIPE_KEY: 31 | salsa20_done(&st); 32 | return err; 33 | } 34 | 35 | #endif /* LTC_SALSA20 */ 36 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/salsa20/xsalsa20_memory.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_XSALSA20 7 | 8 | /** 9 | Encrypt (or decrypt) bytes of ciphertext (or plaintext) with XSalsa20 10 | @param key The key 11 | @param keylen The key length 12 | @param nonce The initial vector 13 | @param noncelen The initial vector length 14 | @param datain The plaintext (or ciphertext) 15 | @param datalen The length of the input and output (octets) 16 | @param rounds The number of rounds 17 | @param dataout [out] The ciphertext (or plaintext) 18 | @return CRYPT_OK if successful 19 | */ 20 | int xsalsa20_memory(const unsigned char *key, unsigned long keylen, unsigned long rounds, 21 | const unsigned char *nonce, unsigned long noncelen, 22 | const unsigned char *datain, unsigned long datalen, unsigned char *dataout) 23 | { 24 | salsa20_state st; 25 | int err; 26 | 27 | if ((err = xsalsa20_setup(&st, key, keylen, nonce, noncelen, rounds)) != CRYPT_OK) goto WIPE_KEY; 28 | err = salsa20_crypt(&st, datain, datalen, dataout); 29 | WIPE_KEY: 30 | salsa20_done(&st); 31 | return err; 32 | } 33 | 34 | #endif /* LTC_XSALSA20 */ 35 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/sober128/sober128_stream_memory.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_SOBER128_STREAM 7 | 8 | /** 9 | Encrypt (or decrypt) bytes of ciphertext (or plaintext) with SOBER128 10 | @param key The key 11 | @param keylen The key length 12 | @param iv The initial vector 13 | @param ivlen The initial vector length 14 | @param datain The plaintext (or ciphertext) 15 | @param datalen The length of the input and output (octets) 16 | @param dataout [out] The ciphertext (or plaintext) 17 | @return CRYPT_OK if successful 18 | */ 19 | int sober128_stream_memory(const unsigned char *key, unsigned long keylen, 20 | const unsigned char *iv, unsigned long ivlen, 21 | const unsigned char *datain, unsigned long datalen, 22 | unsigned char *dataout) 23 | { 24 | sober128_state st; 25 | int err; 26 | 27 | if ((err = sober128_stream_setup(&st, key, keylen)) != CRYPT_OK) goto WIPE_KEY; 28 | if ((err = sober128_stream_setiv(&st, iv, ivlen)) != CRYPT_OK) goto WIPE_KEY; 29 | err = sober128_stream_crypt(&st, datain, datalen, dataout); 30 | WIPE_KEY: 31 | sober128_stream_done(&st); 32 | return err; 33 | } 34 | 35 | #endif /* LTC_SOBER128_STREAM */ 36 | -------------------------------------------------------------------------------- /Sources/SQLCipher/libtomcrypt/stream/sosemanuk/sosemanuk_memory.c: -------------------------------------------------------------------------------- 1 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 | /* SPDX-License-Identifier: Unlicense */ 3 | 4 | #include "tomcrypt_private.h" 5 | 6 | #ifdef LTC_SOSEMANUK 7 | 8 | /** 9 | Encrypt (or decrypt) bytes of ciphertext (or plaintext) with Sosemanuk 10 | @param key The key 11 | @param keylen The key length 12 | @param iv The initial vector 13 | @param ivlen The initial vector length 14 | @param datain The plaintext (or ciphertext) 15 | @param datalen The length of the input and output (octets) 16 | @param dataout [out] The ciphertext (or plaintext) 17 | @return CRYPT_OK if successful 18 | */ 19 | int sosemanuk_memory(const unsigned char *key, unsigned long keylen, 20 | const unsigned char *iv, unsigned long ivlen, 21 | const unsigned char *datain, unsigned long datalen, 22 | unsigned char *dataout) 23 | { 24 | sosemanuk_state st; 25 | int err; 26 | 27 | if ((err = sosemanuk_setup(&st, key, keylen)) != CRYPT_OK) goto WIPE_KEY; 28 | if ((err = sosemanuk_setiv(&st, iv, ivlen)) != CRYPT_OK) goto WIPE_KEY; 29 | err = sosemanuk_crypt(&st, datain, datalen, dataout); 30 | WIPE_KEY: 31 | sosemanuk_done(&st); 32 | return err; 33 | } 34 | 35 | #endif /* LTC_SOSEMANUK */ 36 | -------------------------------------------------------------------------------- /Sources/SQLiteDB/Core/Connection+Attach.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | #if SQLITE_SWIFT_STANDALONE 3 | import sqlite3 4 | #elseif SQLITE_SWIFT_SQLCIPHER 5 | import SQLCipher 6 | #elseif os(Linux) || os(Windows) || os(Android) 7 | import CSQLite 8 | #else 9 | import SQLite3 10 | #endif 11 | 12 | extension Connection { 13 | #if SQLITE_SWIFT_SQLCIPHER 14 | /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#attach 15 | public func attach(_ location: Location, as schemaName: String, key: String? = nil) throws { 16 | if let key { 17 | try run("ATTACH DATABASE ? AS ? KEY ?", location.description, schemaName, key) 18 | } else { 19 | try run("ATTACH DATABASE ? AS ?", location.description, schemaName) 20 | } 21 | } 22 | #else 23 | /// See https://www3.sqlite.org/lang_attach.html 24 | public func attach(_ location: Location, as schemaName: String) throws { 25 | try run("ATTACH DATABASE ? AS ?", location.description, schemaName) 26 | } 27 | #endif 28 | 29 | /// See https://www3.sqlite.org/lang_detach.html 30 | public func detach(_ schemaName: String) throws { 31 | try run("DETACH DATABASE ?", schemaName) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Sources/SQLiteDB/Core/Errors.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | public enum QueryError: Error, CustomStringConvertible { 4 | case noSuchTable(name: String) 5 | case noSuchColumn(name: String, columns: [String]) 6 | case ambiguousColumn(name: String, similar: [String]) 7 | case unexpectedNullValue(name: String) 8 | 9 | public var description: String { 10 | switch self { 11 | case .noSuchTable(let name): 12 | return "No such table: \(name)" 13 | case .noSuchColumn(let name, let columns): 14 | return "No such column `\(name)` in columns \(columns)" 15 | case .ambiguousColumn(let name, let similar): 16 | return "Ambiguous column `\(name)` (please disambiguate: \(similar))" 17 | case .unexpectedNullValue(let name): 18 | return "Unexpected null value for column `\(name)`" 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Sources/SQLiteDB/Core/SQLiteFeature.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | enum SQLiteFeature { 4 | case partialIntegrityCheck // PRAGMA integrity_check(table) 5 | case sqliteSchemaTable // sqlite_master => sqlite_schema 6 | case renameColumn // ALTER TABLE ... RENAME COLUMN 7 | case dropColumn // ALTER TABLE ... DROP COLUMN 8 | 9 | func isSupported(by version: SQLiteVersion) -> Bool { 10 | switch self { 11 | case .partialIntegrityCheck, .sqliteSchemaTable: 12 | return version >= .init(major: 3, minor: 33) 13 | case .renameColumn: 14 | return version >= .init(major: 3, minor: 25) 15 | case .dropColumn: 16 | return version >= .init(major: 3, minor: 35) 17 | } 18 | } 19 | } 20 | 21 | extension Connection { 22 | func supports(_ feature: SQLiteFeature) -> Bool { 23 | feature.isSupported(by: sqliteVersion) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sources/SQLiteDB/Core/SQLiteVersion.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | public struct SQLiteVersion: Comparable, CustomStringConvertible { 4 | public let major: Int 5 | public let minor: Int 6 | public var point: Int = 0 7 | 8 | public var description: String { 9 | "SQLite \(major).\(minor).\(point)" 10 | } 11 | 12 | public static func <(lhs: SQLiteVersion, rhs: SQLiteVersion) -> Bool { 13 | lhs.tuple < rhs.tuple 14 | } 15 | 16 | public static func ==(lhs: SQLiteVersion, rhs: SQLiteVersion) -> Bool { 17 | lhs.tuple == rhs.tuple 18 | } 19 | 20 | static var zero: SQLiteVersion = .init(major: 0, minor: 0) 21 | private var tuple: (Int, Int, Int) { (major, minor, point) } 22 | } 23 | -------------------------------------------------------------------------------- /Sources/SQLiteDB/PrivacyInfo.xcprivacy: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NSPrivacyTrackingDomains 6 | 7 | NSPrivacyCollectedDataTypes 8 | 9 | NSPrivacyAccessedAPITypes 10 | 11 | NSPrivacyTracking 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Sources/SQLiteDB/SQLite.h: -------------------------------------------------------------------------------- 1 | @import Foundation; 2 | 3 | FOUNDATION_EXPORT double SQLiteVersionNumber; 4 | FOUNDATION_EXPORT const unsigned char SQLiteVersionString[]; 5 | -------------------------------------------------------------------------------- /Tests/.swiftlint.yml: -------------------------------------------------------------------------------- 1 | parent_config: ../.swiftlint.yml 2 | 3 | disabled_rules: 4 | - force_cast 5 | - force_try 6 | - identifier_name 7 | 8 | type_body_length: 9 | warning: 1000 10 | error: 1000 11 | 12 | function_body_length: 13 | warning: 200 14 | error: 200 15 | 16 | file_length: 17 | warning: 1000 18 | error: 1000 19 | 20 | 21 | -------------------------------------------------------------------------------- /Tests/SPM/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | -------------------------------------------------------------------------------- /Tests/SPM/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.7 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "test", 8 | platforms: [ 9 | .iOS(.v11), 10 | .macOS(.v10_13), 11 | .watchOS(.v4), 12 | .tvOS(.v11) 13 | ], 14 | dependencies: [ 15 | // for testing from same repository 16 | .package(path: "../..") 17 | // normally this would be: 18 | // .package(url: "https://github.com/stephencelis/SQLite.swift.git", from: "0.15.3") 19 | ], 20 | targets: [ 21 | .executableTarget(name: "test", dependencies: [.product(name: "SQLite", package: "SQLite.swift")]) 22 | ] 23 | ) 24 | -------------------------------------------------------------------------------- /Tests/SPM/Sources/test/main.swift: -------------------------------------------------------------------------------- 1 | import SkipSQLite 2 | 3 | let table = Table("test") 4 | let name = Expression("name") 5 | 6 | let db = try Connection("db.sqlite", readonly: true) 7 | 8 | for row in try db.prepare(table) { 9 | print(row[name]) 10 | } 11 | -------------------------------------------------------------------------------- /Tests/SPM/db.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skiptools/swift-sqlcipher/abad5a4c3cb294585f34e36927b1832726f8183a/Tests/SPM/db.sqlite -------------------------------------------------------------------------------- /Tests/SQLiteDBTests/Core/BlobTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | import SQLiteDB 3 | 4 | class BlobTests: XCTestCase { 5 | 6 | func test_toHex() { 7 | let blob = Blob(bytes: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 250, 255]) 8 | XCTAssertEqual(blob.toHex(), "000a141e28323c46505a6496faff") 9 | } 10 | 11 | func test_toHex_empty() { 12 | let blob = Blob(bytes: []) 13 | XCTAssertEqual(blob.toHex(), "") 14 | } 15 | 16 | func test_description() { 17 | let blob = Blob(bytes: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 250, 255]) 18 | XCTAssertEqual(blob.description, "x'000a141e28323c46505a6496faff'") 19 | } 20 | 21 | func test_description_empty() { 22 | let blob = Blob(bytes: []) 23 | XCTAssertEqual(blob.description, "x''") 24 | } 25 | 26 | func test_init_array() { 27 | let blob = Blob(bytes: [42, 43, 44]) 28 | XCTAssertEqual(blob.bytes, [42, 43, 44]) 29 | } 30 | 31 | func test_init_unsafeRawPointer() { 32 | let pointer = UnsafeMutablePointer.allocate(capacity: 3) 33 | pointer.initialize(repeating: 42, count: 3) 34 | let blob = Blob(bytes: pointer, length: 3) 35 | XCTAssertEqual(blob.bytes, [42, 42, 42]) 36 | } 37 | 38 | func test_equality() { 39 | let blob1 = Blob(bytes: [42, 42, 42]) 40 | let blob2 = Blob(bytes: [42, 42, 42]) 41 | let blob3 = Blob(bytes: [42, 42, 43]) 42 | 43 | XCTAssertEqual(Blob(bytes: []), Blob(bytes: [])) 44 | XCTAssertEqual(blob1, blob2) 45 | XCTAssertNotEqual(blob1, blob3) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Tests/SQLiteDBTests/Core/Connection+PragmaTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | import Foundation 3 | @testable import SQLiteDB 4 | 5 | #if SQLITE_SWIFT_STANDALONE 6 | import sqlite3 7 | #elseif SQLITE_SWIFT_SQLCIPHER 8 | import SQLCipher 9 | #elseif os(Linux) || os(Windows) || os(Android) 10 | import CSQLite 11 | #else 12 | import SQLite3 13 | #endif 14 | 15 | class ConnectionPragmaTests: SQLiteTestCase { 16 | func test_userVersion() { 17 | db.userVersion = 2 18 | XCTAssertEqual(2, db.userVersion!) 19 | } 20 | 21 | func test_sqlite_version() { 22 | XCTAssertTrue(db.sqliteVersion >= .init(major: 3, minor: 0)) 23 | } 24 | 25 | func test_foreignKeys_defaults_to_false() { 26 | XCTAssertFalse(db.foreignKeys) 27 | } 28 | 29 | func test_foreignKeys_sets_value() { 30 | db.foreignKeys = true 31 | XCTAssertTrue(db.foreignKeys) 32 | } 33 | 34 | func test_defer_foreignKeys_defaults_to_false() { 35 | XCTAssertFalse(db.deferForeignKeys) 36 | } 37 | 38 | func test_defer_foreignKeys_sets_value() { 39 | db.deferForeignKeys = true 40 | XCTAssertTrue(db.deferForeignKeys) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Tests/SQLiteDBTests/Core/ValueTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | import SQLiteDB 3 | 4 | class ValueTests: XCTestCase { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /Tests/SQLiteDBTests/Extensions/RTreeTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | import SQLiteDB 3 | 4 | class RTreeTests: XCTestCase { 5 | 6 | func test_create_onVirtualTable_withRTree_createVirtualTableExpression() { 7 | XCTAssertEqual( 8 | "CREATE VIRTUAL TABLE \"virtual_table\" USING rtree(\"int64\", \"double\", \"double\")", 9 | virtualTable.create(.RTree(int64, (double, double))) 10 | ) 11 | XCTAssertEqual( 12 | "CREATE VIRTUAL TABLE \"virtual_table\" USING rtree(\"int64\", \"double\", \"double\", \"double\", \"double\")", 13 | virtualTable.create(.RTree(int64, (double, double), (double, double))) 14 | ) 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Tests/SQLiteDBTests/Fixtures.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | func fixture(_ name: String, withExtension: String?) -> String { 4 | #if SWIFT_PACKAGE 5 | let testBundle = Bundle.module 6 | #else 7 | let testBundle = Bundle(for: SQLiteTestCase.self) 8 | #endif 9 | 10 | for resource in [name, "Resources/\(name)"] { 11 | if let url = testBundle.url( 12 | forResource: resource, 13 | withExtension: withExtension) { 14 | return url.path 15 | } 16 | } 17 | fatalError("Cannot find \(name).\(withExtension ?? "")") 18 | } 19 | 20 | func temporaryFile() -> String { 21 | URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString).path 22 | } 23 | -------------------------------------------------------------------------------- /Tests/SQLiteDBTests/FoundationTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | import SQLiteDB 3 | 4 | class FoundationTests: XCTestCase { 5 | func testDataFromBlob() { 6 | let data = Data([1, 2, 3]) 7 | let blob = data.datatypeValue 8 | XCTAssertEqual([1, 2, 3], blob.bytes) 9 | } 10 | 11 | func testBlobToData() { 12 | let blob = Blob(bytes: [1, 2, 3]) 13 | let data = Data.fromDatatypeValue(blob) 14 | XCTAssertEqual(Data([1, 2, 3]), data) 15 | } 16 | 17 | func testStringFromUUID() { 18 | let uuid = UUID(uuidString: "4ABE10C9-FF12-4CD4-90C1-4B429001BAD3")! 19 | let string = uuid.datatypeValue 20 | XCTAssertEqual("4ABE10C9-FF12-4CD4-90C1-4B429001BAD3", string) 21 | } 22 | 23 | func testUUIDFromString() { 24 | let string = "4ABE10C9-FF12-4CD4-90C1-4B429001BAD3" 25 | let uuid = UUID.fromDatatypeValue(string) 26 | XCTAssertEqual(UUID(uuidString: "4ABE10C9-FF12-4CD4-90C1-4B429001BAD3"), uuid) 27 | } 28 | 29 | func testURLFromString() { 30 | let string = "http://foo.com" 31 | let url = URL.fromDatatypeValue(string) 32 | XCTAssertEqual(URL(string: string), url) 33 | } 34 | 35 | func testStringFromURL() { 36 | let url = URL(string: "http://foo.com")! 37 | let string = url.datatypeValue 38 | XCTAssertEqual("http://foo.com", string) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Tests/SQLiteDBTests/Resources/encrypted-3.x.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skiptools/swift-sqlcipher/abad5a4c3cb294585f34e36927b1832726f8183a/Tests/SQLiteDBTests/Resources/encrypted-3.x.sqlite -------------------------------------------------------------------------------- /Tests/SQLiteDBTests/Resources/encrypted-4.x.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skiptools/swift-sqlcipher/abad5a4c3cb294585f34e36927b1832726f8183a/Tests/SQLiteDBTests/Resources/encrypted-4.x.sqlite -------------------------------------------------------------------------------- /Tests/SQLiteDBTests/Resources/test.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skiptools/swift-sqlcipher/abad5a4c3cb294585f34e36927b1832726f8183a/Tests/SQLiteDBTests/Resources/test.sqlite -------------------------------------------------------------------------------- /Tests/SQLiteDBTests/Typed/ExpressionTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | @testable import SQLiteDB 3 | 4 | class ExpressionTests: XCTestCase { 5 | 6 | func test_asSQL_expression_bindings() { 7 | let expression = SQLExpression("foo ? bar", ["baz"]) 8 | XCTAssertEqual(expression.asSQL(), "foo 'baz' bar") 9 | } 10 | 11 | func test_asSQL_expression_bindings_quoting() { 12 | let expression = SQLExpression("foo ? bar", ["'baz'"]) 13 | XCTAssertEqual(expression.asSQL(), "foo '''baz''' bar") 14 | } 15 | 16 | func test_expression_custom_string_convertible() { 17 | let expression = SQLExpression("foo ? bar", ["baz"]) 18 | XCTAssertEqual(expression.asSQL(), expression.description) 19 | } 20 | 21 | func test_builtin_unambiguously_custom_string_convertible() { 22 | let integer: Int = 45 23 | XCTAssertEqual(integer.description, "45") 24 | } 25 | 26 | func test_init_literal() { 27 | let expression = SQLExpression(literal: "literal") 28 | XCTAssertEqual(expression.template, "literal") 29 | } 30 | 31 | func test_init_identifier() { 32 | let expression = SQLExpression("identifier") 33 | XCTAssertEqual(expression.template, "\"identifier\"") 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Tests/SQLiteDBTests/Typed/SelectTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | @testable import SQLiteDB 3 | 4 | class SelectTests: SQLiteTestCase { 5 | 6 | override func setUpWithError() throws { 7 | try super.setUpWithError() 8 | try createUsersTable() 9 | try createUsersDataTable() 10 | } 11 | 12 | func createUsersDataTable() throws { 13 | try db.execute(""" 14 | CREATE TABLE users_name ( 15 | id INTEGER, 16 | user_id INTEGER REFERENCES users(id), 17 | name TEXT 18 | ) 19 | """ 20 | ) 21 | } 22 | 23 | func test_select_columns_from_multiple_tables() throws { 24 | let usersData = Table("users_name") 25 | let users = Table("users") 26 | 27 | let name = SQLExpression("name") 28 | let id = SQLExpression("id") 29 | let userID = SQLExpression("user_id") 30 | let email = SQLExpression("email") 31 | 32 | try insertUser("Joey") 33 | try db.run(usersData.insert( 34 | id <- 1, 35 | userID <- 1, 36 | name <- "Joey" 37 | )) 38 | 39 | try db.prepare(users.select(name, email).join(usersData, on: userID == users[id])).forEach { 40 | XCTAssertEqual($0[name], "Joey") 41 | XCTAssertEqual($0[email], "Joey@example.com") 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /scripts/build_sqlcipher.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -ex 2 | # This script will download the latest SQLCipher and create 3 | # the amalgamated sqlite3.c and sqlite3.h files 4 | 5 | SQLCIPHER_REPO="https://github.com/sqlcipher/sqlcipher" 6 | 7 | # check https://github.com/sqlcipher/sqlcipher/releases for latest 8 | # SQLCIPHER_VERSION=4.6.1 -> SQLite 3.46.1 9 | #SQLCIPHER_VERSION=4.6.1 10 | 11 | # get the latest tag from the SQLCipher repository 12 | SQLCIPHER_VERSION=$(git -c 'versionsort.suffix=-' ls-remote --tags --sort='v:refname' ${SQLCIPHER_REPO} | tail -n 1 | cut -d '/' -f 3 | cut -f 1 -d '^' | cut -f 2 -d 'v') 13 | 14 | # might need to first run: brew install libtomcrypt 15 | 16 | cd `mktemp -d` 17 | wget ${SQLCIPHER_REPO}/archive/refs/tags/v${SQLCIPHER_VERSION}.tar.gz 18 | tar xvzf v${SQLCIPHER_VERSION}.tar.gz 19 | ./sqlcipher-${SQLCIPHER_VERSION}/configure --with-tempstore=yes CFLAGS="-DSQLCIPHER_CRYPTO_LIBTOMCRYPT -DSQLITE_HAS_CODEC -DSQLITE_EXTRA_INIT=sqlcipher_extra_init -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown -I/opt/homebrew/include/ -L/opt/homebrew/lib/" 20 | make sqlite3.c 21 | 22 | cp -v sqlite3.c sqlite3.h ${OLDPWD}/Sources/SQLCipher/sqlite/ 23 | 24 | --------------------------------------------------------------------------------