#include #include #include #include #include #include #include #include #include #include "rng.h" void encrypt_test_binary(){ init_rng(); unsigned char enc_key[256]; memset( (void*)&enc_key, 0, sizeof(enc_key) ); size_t k_size = 0; for ( ; k_size < 256; k_size++ ) enc_key[k_size] = (unsigned char)get_random_int( 1, 255 ); struct stat test_bin_info; memset( (void*)&test_bin_info, 0, sizeof(struct stat) ); if ( stat("./test_binary", &test_bin_info) == -1 ){ fprintf( stderr, "Failed to stat: %s\n", strerror(errno) ); return; } if ( !((test_bin_info.st_mode & S_IFMT) == S_IFREG) ){ fputs( "Error: test binary is not regular file\n", stderr ); return; } unsigned char *test_bin_bytes = (unsigned char*)calloc( (size_t)test_bin_info.st_size, sizeof(unsigned char) ); if ( test_bin_bytes == NULL ){ fprintf( stderr, "Error: failed to allocate memory for reading test binary: %s\n", strerror(errno) ); return; } int test_bin_fd = open( "./test_binary", O_RDONLY, 0755 ); if ( test_bin_fd == -1 ){ fprintf( stderr, "Error: failed to open test binary for encryption: %s\n", strerror(errno) ); free( test_bin_bytes ); return; } if ( read(test_bin_fd, (void*)test_bin_bytes, (size_t)test_bin_info.st_size) == -1 ){ fprintf( stderr, "Error: failed to read test binary bytes: %s\n", strerror(errno) ); close( test_bin_fd ); free( test_bin_bytes ); return; } close( test_bin_fd ); size_t encrypted_bytes = 0; for ( ; encrypted_bytes < (size_t)test_bin_info.st_size; encrypted_bytes++ ) test_bin_bytes[encrypted_bytes] = test_bin_bytes[encrypted_bytes] ^ enc_key[(size_t)test_bin_info.st_size % 255]; test_bin_fd = open( "./test_binary", O_RDWR | O_CREAT | O_TRUNC ); if ( test_bin_fd == -1 ){ fprintf( stderr, "Error: failed to open test binary for writing: %s\n", strerror(errno) ); free( test_bin_bytes ); return; } if ( write(test_bin_fd, (const void*)test_bin_bytes, (size_t)test_bin_info.st_size) == -1 ){ fprintf( stderr, "Error: failed to write encrypted test binary: %s\n", strerror(errno) ); close( test_bin_fd ); free( test_bin_bytes ); return; } close( test_bin_fd ); free( test_bin_bytes ); return; } int main( int argc, char *const *args ){ encrypt_test_binary(); return 0; }