cryanc icon indicating copy to clipboard operation
cryanc copied to clipboard

Split cryanc.c

Open 89z opened this issue 3 years ago • 8 comments

It's cool and impressive that cryanc.c is a single file. However it's so large, that GitHub won't render it, outside of a plain text dump. Would you consider splitting it up? Probably two or three files would do it

89z avatar Jul 16 '22 22:07 89z

I've thought about splitting it into pieces for algorithms, TLS and applications and #includeing them together in the past. Plus, there are some functions we don't even use. However, I probably wouldn't embark upon this until it's more or less feature complete.

classilla avatar Jul 16 '22 22:07 classilla

Modern GCC is actually excellent for this kind of investigation.

I didn't investigate the structure of this program at all - in fact, I didn't look at the source code at all yet, but I might.

Here is what isn't used (at least on my modern Linux system):

$ gcc -fno-inline -O0 -fdata-sections -ffunction-sections     \
  -Wl,--gc-sections -Wl,--print-gc-sections -fdump-rtl-expand \
    -pipe -o carl carl.c 2>&1 | 
  grep 'removing unused section.*tmp\/.*\.o.$' |
    cut -d "'" -f2 | sort -u

.data.crypt_build_settings
.rodata.cd64
.rodata.default_curve
.rodata.der_decode_sequence_flexi
.rodata.err_2_str
.rodata._private_tls_hash_len
.rodata.rem_105
.rodata.rem_128
.rodata.rijndael_desc
.rodata.sizes
.rodata.Te4
.rodata.tls_certificate_verify_signature
.rodata.tls_cipher_name
.text.aarc4_getword
.text.aarc4random_addrandom
.text.aarc4random_maybe
.text.cbc_getiv
.text.cbc_setiv
.text.crypt_fsa
.text.ctr_decrypt
.text.ctr_done
.text.ctr_encrypt
.text.ctr_getiv
.text.ctr_setiv
.text.ctr_start
.text.der_decode_sequence_flexi
.text.der_sequence_free
.text.ecc_ansi_x963_import
.text.ecc_decrypt_key
.text.ecc_encrypt_key
.text.ecc_export
.text.ecc_get_size
.text.ecc_import
.text.ecc_import_ex
.text.ecc_make_key
.text.ecc_sizes
.text.ecc_test
.text.error_to_string
.text.fetch_length
.text.find_cipher_any
.text.find_cipher_id
.text.find_hash_any
.text.find_hash_id
.text.find_hash_oid
.text.hash_file
.text.hash_filehandle
.text.hash_memory_multi
.text.is_point
.text.mp_addmod
.text.mp_and
.text.mp_export
.text.mp_expt_d
.text.mp_expt_d_ex
.text.mp_exteuclid
.text.mp_fread
.text.mp_fwrite
.text.mp_get_long
.text.mp_get_long_long
.text.mp_import
.text.mp_init_set
.text.mp_init_set_int
.text.mp_is_square
.text.mp_jacobi
.text.mp_n_root
.text.mp_n_root_ex
.text.mp_or
.text.mp_prime_fermat
.text.mp_prime_next_prime
.text.mp_prime_rabin_miller_trials
.text.mp_prime_random_ex
.text.mp_radix_size
.text.mp_rand
.text.mp_read_signed_bin
.text.mp_set_long
.text.mp_set_long_long
.text.mp_shrink
.text.mp_signed_bin_size
.text.mp_sqrt
.text.mp_sqrtmod_prime
.text.mp_submod
.text.mp_to_signed_bin
.text.mp_to_signed_bin_n
.text.mp_xor
.text.pkcs_1_i2osp
.text.pkcs_1_os2ip
.text.poly1305_verify
.text.print_index
.text._private_b64_decode
.text._private_b64_decodeblock
.text._private_tls_crypto_done
.text._private_tls_hash_len
.text._private_tls_read_from_file
.text._private_tls_set_session_id
.text.tls_accept
.text.tls_add_alpn
.text.tls_alert
.text.tls_alpn
.text.tls_certificate_chain_is_valid
.text.tls_certificate_chain_is_valid_root
.text.tls_certificate_is_valid
.text.tls_certificate_valid_subject
.text.tls_certificate_valid_subject_name
.text.tls_certificate_verify_signature
.text.tls_cipher_name
.text.tls_clear_certificates
.text.tls_client_verified
.text.tls_close_notify
.text.tls_default_verify
.text.tls_destroy_certificate
.text.tls_destroy_context
.text.tls_export_context
.text.tls_get_message
.text.tls_import_context
.text.tls_is_broken
.text.tls_load_certificates
.text.tls_load_private_key
.text.tls_load_root_certificates
.text.tls_make_exportable
.text.tls_make_ktls
.text.tls_packet_uint32
.text.tls_pem_decode
.text.tls_pending
.text.tls_read_clear
.text.tls_remote_error
.text.tls_request_client_certificate
.text.tls_set_curve
.text.tls_set_default_dhe_pg
.text.tls_sni
.text.tls_unmake_ktls
.text.unregister_cipher
.text.unregister_hash
.text.unregister_prng

johnsonjh avatar Jul 17 '22 02:07 johnsonjh

Also, since we saved the RTL expansion output, we can look at it with Cally which might help decide what should be split up or refactored, and also spot things that are unused, over on the left (as long as they aren't indirectly called somehow, I guess) ...

$ cally carl.c.*.expand --no-externs | 
  dot -Grankdir=LR -Tsvg -o out.svg

cally.svg

johnsonjh avatar Jul 17 '22 02:07 johnsonjh

Some of those will be in use when certificate validation gets up-to-date, and some of them are part of the defined interface which carl doesn't use but other applications might. That's why I don't want to do this prematurely.

classilla avatar Jul 17 '22 02:07 classilla

Probably the best way to test would be to write some kind of unit tests for the documented external interface, because then what is left over would be totally safe to remove, if the tests are covering everything that is. :)

Somewhat related - Many many eons ago, there was a program on SunOS (I think it was possibly a Perl script?) that would split up a large C program into individual functions, one per C file, where each file would #include any global variables and definitions. It didn't do a perfect job, by any means, but it got you most of the way there. It worked well to reduce the size of compiled binaries in the old days when static linkers weren't as smart as they are today, and also when you couldn't compile a single large file for whatever reason (compiler crashing, memory constraints, etc.) ... @classilla would you happen to remember this tool, or what it was called?

johnsonjh avatar Jul 17 '22 02:07 johnsonjh

I don't think I ever ran into that one, though it certainly does sound handy.

classilla avatar Jul 17 '22 02:07 classilla

If you come across it, or any similar thing in your retro-computing adventures, ping me.. although it shouldn't be too hard to write one. (I've got g.c which I'd like to split up one day.)

johnsonjh avatar Jul 17 '22 02:07 johnsonjh

Sure thing. Interesting project there.

classilla avatar Jul 17 '22 02:07 classilla