void write_client_msg(Client* client, int write_fd) { rb_red_blk_node* transfer = RBExactQuery(client->transfers, &write_fd); File_msg* file_msg = (File_msg*)(transfer->info); /*it is Encryption of a request or a transfer + or (data + Encryption)*/ int valwrite = write(write_fd, file_msg->msg.buffer+file_msg->msg.offset, BUFFER_SIZE-file_msg->msg.offset); file_msg->msg.offset += valwrite; if(file_msg->msg.offset == BUFFER_SIZE) { logger("<Client><write_client_msg> got full message to write\n"); if((file_msg->msg.total_len != 0) && (file_msg->msg.offset < file_msg->msg.total_len)) { /*fetch next part of the file*/ if(ENCRYPTION_ENABLED) { char file_read_buffer[BUFFER_SIZE]; for(int i=0; i<BUFFER_SIZE; ++i) file_read_buffer[i] = file_msg->msg.buffer[i]; file_msg->msg.offset += read(file_msg->file_fd, file_read_buffer, BUFFER_SIZE); AES128_ECB_encrypt(file_read_buffer, file_msg->aes_key, file_msg->msg.buffer); logger("<Client><write_client_msg>read full chunck from file and encrypted it\n"); } else { read(file_msg->file_fd, file_msg->msg.buffer, BUFFER_SIZE); logger("<Client><write_client_msg>read full chunck from file and encrypted it\n"); } } else if((file_msg->msg.total_len != 0)) { //write finished logger("<Client><write_client_msg>writing to the other end finished\n"); FD_CLR(write_fd, &(client->fd_write_set)); close(file_msg->file_fd); } else { logger("<Client><write_client_msg>transfer message\n"); char file_read_buffer[BUFFER_SIZE]; for(int i=0; i<BUFFER_SIZE; ++i) file_read_buffer[i] = file_msg->msg.buffer[i]; AES128_ECB_encrypt(file_read_buffer, file_msg->aes_key, file_msg->msg.buffer); FD_CLR(write_fd, &(client->fd_read_set)); } } }
static void pdcmode_aes_ecb_encrypt(const ccecb_ctx *ctx, unsigned long nblocks, const void *in, void *out) { printf("%s\n", __func__); AES128_ECB_encrypt((struct _pdcmode_aes128_ctx *)ctx, nblocks, in, out); }
static void test_encrypt_ecb(void) { uint8_t key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; uint8_t in[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a}; uint8_t expected[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97}; uint8_t buffer[16]; AES128_ECB_encrypt(in, key, buffer); NP_ASSERT_BYTES_EQUAL(expected,buffer,sizeof(buffer)); }
static void test_encrypt_ecb(void) { uint8_t key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; uint8_t in[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a}; uint8_t out[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97}; uint8_t buffer[16]; AES128_ECB_encrypt(in, key, buffer); printf("ECB decrypt: "); if(0 == strncmp((char*) out, (char*) buffer, 16)) { printf("SUCCESS!\n"); } else { printf("FAILURE!\n"); } }
static void test_encrypt_ecb_verbose(void) { // Example of more verbose verification uint8_t i, buf[64], buf2[64]; // 128bit key uint8_t key[16] = { (uint8_t) 0x2b, (uint8_t) 0x7e, (uint8_t) 0x15, (uint8_t) 0x16, (uint8_t) 0x28, (uint8_t) 0xae, (uint8_t) 0xd2, (uint8_t) 0xa6, (uint8_t) 0xab, (uint8_t) 0xf7, (uint8_t) 0x15, (uint8_t) 0x88, (uint8_t) 0x09, (uint8_t) 0xcf, (uint8_t) 0x4f, (uint8_t) 0x3c }; // 512bit text uint8_t plain_text[64] = { (uint8_t) 0x6b, (uint8_t) 0xc1, (uint8_t) 0xbe, (uint8_t) 0xe2, (uint8_t) 0x2e, (uint8_t) 0x40, (uint8_t) 0x9f, (uint8_t) 0x96, (uint8_t) 0xe9, (uint8_t) 0x3d, (uint8_t) 0x7e, (uint8_t) 0x11, (uint8_t) 0x73, (uint8_t) 0x93, (uint8_t) 0x17, (uint8_t) 0x2a, (uint8_t) 0xae, (uint8_t) 0x2d, (uint8_t) 0x8a, (uint8_t) 0x57, (uint8_t) 0x1e, (uint8_t) 0x03, (uint8_t) 0xac, (uint8_t) 0x9c, (uint8_t) 0x9e, (uint8_t) 0xb7, (uint8_t) 0x6f, (uint8_t) 0xac, (uint8_t) 0x45, (uint8_t) 0xaf, (uint8_t) 0x8e, (uint8_t) 0x51, (uint8_t) 0x30, (uint8_t) 0xc8, (uint8_t) 0x1c, (uint8_t) 0x46, (uint8_t) 0xa3, (uint8_t) 0x5c, (uint8_t) 0xe4, (uint8_t) 0x11, (uint8_t) 0xe5, (uint8_t) 0xfb, (uint8_t) 0xc1, (uint8_t) 0x19, (uint8_t) 0x1a, (uint8_t) 0x0a, (uint8_t) 0x52, (uint8_t) 0xef, (uint8_t) 0xf6, (uint8_t) 0x9f, (uint8_t) 0x24, (uint8_t) 0x45, (uint8_t) 0xdf, (uint8_t) 0x4f, (uint8_t) 0x9b, (uint8_t) 0x17, (uint8_t) 0xad, (uint8_t) 0x2b, (uint8_t) 0x41, (uint8_t) 0x7b, (uint8_t) 0xe6, (uint8_t) 0x6c, (uint8_t) 0x37, (uint8_t) 0x10 }; memset(buf, 0, 64); memset(buf2, 0, 64); // print text to encrypt, key and IV printf("ECB encrypt verbose:\n\n"); printf("plain text:\n"); for(i = (uint8_t) 0; i < (uint8_t) 4; ++i) { phex(plain_text + i * (uint8_t) 16); } printf("\n"); printf("key:\n"); phex(key); printf("\n"); // print the resulting cipher as 4 x 16 byte strings printf("ciphertext:\n"); for(i = 0; i < 4; ++i) { AES128_ECB_encrypt(plain_text + (i*16), key, buf+(i*16)); phex(buf + (i*16)); } printf("\n"); }
double encrypt_file(char* infile, char* outfile) { FILE *fp_in; FILE *fp_out; fp_in = fopen(infile, "rb"); if (fp_in == NULL && !silent) { fprintf(stderr, "Can't open input file %s!\n", infile); exit(1); } fp_out = fopen(outfile, "wb+"); if (fp_out == NULL && !silent) { fprintf(stderr, "Can't open output file %s!\n", outfile); exit(1); } KeyExpansion(roundKey, key); #if defined(DEBUG) && DEBUG printf("Round Keys:\n"); uint8_t i; for (i = 0; i < ROUNDS + 1; i++) { phex(roundKey + (i * ROUNDS)); } #endif // determine size of file, read file into plaintext and determine number of plaintext blocks fseek(fp_in, 0, SEEK_END); uintmax_t plaintext_size = ftell(fp_in); rewind(fp_in); uint8_t* plaintext = (uint8_t*)malloc(plaintext_size); uintmax_t bytes_read = fread(plaintext, sizeof(uint8_t), plaintext_size, fp_in); assert(bytes_read == plaintext_size); uintmax_t plaintext_blocks = (bytes_read + BLOCKSIZE - 1) / BLOCKSIZE; uint8_t* ciphertext = (uint8_t*)malloc(plaintext_blocks*BLOCKSIZE); if (!silent) { printf("File size: %llu bytes\n", plaintext_size); printf("Number of plaintext blocks: %llu (blocksize: %d bytes)\n", plaintext_blocks, BLOCKSIZE); } #if defined(DEBUG) && DEBUG printf("Plaintext:\n"); for (i = 0; i < plaintext_blocks; i++) { phex(plaintext + (i * BLOCKSIZE)); } #endif // measure time double cpu_time_used; LARGE_INTEGER frequency; LARGE_INTEGER start, end; QueryPerformanceFrequency(&frequency); // start timer QueryPerformanceCounter(&start); uintmax_t j; for (j = 0; j < plaintext_blocks; j++) { // encrypt plaintext block AES128_ECB_encrypt(plaintext + j*BLOCKSIZE, roundKey, ciphertext_block); // write ciphertext block to output file memcpy(ciphertext + j*BLOCKSIZE, ciphertext_block, sizeof(uint8_t)*BLOCKSIZE); } // stop timer QueryPerformanceCounter(&end); cpu_time_used = ((double)(end.QuadPart - start.QuadPart)) / ((double)frequency.QuadPart); // write ciphertext to output file fwrite(ciphertext, sizeof(uint8_t), BLOCKSIZE * plaintext_blocks, fp_out); #if defined(DEBUG) && DEBUG printf("Ciphertext:\n"); for (i = 0; i < plaintext_blocks; i++) { phex(ciphertext + (i * BLOCKSIZE)); } #endif fclose(fp_in); fclose(fp_out); if (!silent) printf("Encryption of %llu plaintext blocks successful!\n", plaintext_blocks); return cpu_time_used; }