#include #include #include #include #include #include "crypto_buffer.h" int main(void) { static int fd, ret; static crypto_buffer out_buffer; static crypto_buffer in_buffer; static u8 phrase[64]="blah_et_vlan_et_rantanplan_=_rien_de_cohérent."; static u8 decrypted[64]=""; /* blowfish */ printf("** crypto buffer tests with BLOWFISH **\n\n"); /* write */ fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644); if(fd == -1) return -1; ret = write(fd,"AAA",3); assert(ret==3); if(crypto_buffer_init(&out_buffer, fd, u8_unix_write, 4, BLOWFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; crypto_buffer_putflush(&out_buffer,phrase,5); close(fd); printf("file written\n"); memset(decrypted,0,64); /* read */ fd = open("./crypto_file",O_RDONLY); if(fd == -1) return -1; if(crypto_buffer_init(&in_buffer, fd, u8_unix_read, 5, BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64)); printf("decrypted => '%s'\n\n",decrypted); close(fd); assert(memcmp(phrase,decrypted,5)==0); memset(decrypted,0,64); /* write */ fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644); if(fd == -1) return -1; ret = write(fd,"AAA",3); assert(ret==3); if(crypto_buffer_init(&out_buffer, fd, &u8_unix_write, 4, BLOWFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; crypto_buffer_putflush(&out_buffer,phrase,8); close(fd); printf("file written\n"); memset(decrypted,0,64); /* read */ fd = open("./crypto_file",O_RDONLY); if(fd == -1) return -1; if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5, BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64)); printf("decrypted => '%s'\n\n",decrypted); close(fd); assert(memcmp(phrase,decrypted,8)==0); memset(decrypted,0,64); /* write */ fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644); if(fd == -1) return -1; ret = write(fd,"AAA",0); assert(ret==0); if(crypto_buffer_init(&out_buffer, fd, &u8_unix_write, 4, BLOWFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; crypto_buffer_putflush(&out_buffer,phrase,64); close(fd); printf("file written\n"); memset(decrypted,0,64); /* read */ fd = open("./crypto_file",O_RDONLY); if(fd == -1) return -1; if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5, BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64)); printf("decrypted => '%s'\n\n",decrypted); close(fd); assert(memcmp(phrase,decrypted,64)==0); memset(decrypted,0,64); /* read */ fd = open("./crypto_file",O_RDONLY); if(fd == -1) return -1; if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5, BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,8)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+8,15)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+23,24)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+47,5)); printf("decrypted => '%s'\n\n",decrypted); close(fd); assert(memcmp(phrase,decrypted,64)==0); memset(decrypted,0,64); /* write */ fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644); if(fd == -1) return -1; ret = write(fd,"AAAAA",5); assert(ret==5); if(crypto_buffer_init(&out_buffer, fd, &u8_unix_write, 4, BLOWFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; crypto_buffer_put(&out_buffer,phrase,12); crypto_buffer_putflush(&out_buffer,phrase+12,14); crypto_buffer_putflush(&out_buffer,phrase+26,23); close(fd); printf("file written\n"); memset(decrypted,0,64); /* read */ fd = open("./crypto_file",O_RDONLY); if(fd == -1) return -1; if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5, BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64)); printf("decrypted => '%s'\n\n",decrypted); close(fd); assert(memcmp(phrase,decrypted,64)==0); memset(decrypted,0,64); /* read */ fd = open("./crypto_file",O_RDONLY); if(fd == -1) return -1; if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5, BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,9)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+9,9)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+18,20)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+38,22)); printf("decrypted => '%s'\n\n",decrypted); close(fd); assert(memcmp(phrase,decrypted,64)==0); /* twofish */ printf("** crypto buffer tests with TWOFISH **\n\n"); /* write */ fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644); if(fd == -1) return -1; ret = write(fd,"AAA",3); assert(ret==3); if(crypto_buffer_init(&out_buffer, fd, &u8_unix_write, 4, TWOFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; crypto_buffer_putflush(&out_buffer,phrase,64); close(fd); printf("file written\n"); memset(decrypted,0,64); /* read */ fd = open("./crypto_file",O_RDONLY); if(fd == -1) return -1; if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5, TWOFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64)); printf("decrypted => '%s'\n\n",decrypted); close(fd); assert(memcmp(phrase,decrypted,64)==0); memset(decrypted,0,64); /* read */ fd = open("./crypto_file",O_RDONLY); if(fd == -1) return -1; if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5, TWOFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,9)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+9,9)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+18,20)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+38,22)); printf("decrypted => '%s'\n\n",decrypted); close(fd); assert(memcmp(phrase,decrypted,64)==0); memset(decrypted,0,64); /* write */ fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644); if(fd == -1) return -1; ret = write(fd,"AAAA",4); assert(ret==4); if(crypto_buffer_init(&out_buffer, fd, &u8_unix_write, 4, TWOFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; crypto_buffer_put(&out_buffer,phrase,12); crypto_buffer_putflush(&out_buffer,phrase+12,14); crypto_buffer_putflush(&out_buffer,phrase+26,21); close(fd); printf("file written\n"); memset(decrypted,0,64); /* read */ fd = open("./crypto_file",O_RDONLY); if(fd == -1) return -1; if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5, TWOFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64)); printf("decrypted => '%s'\n\n",decrypted); close(fd); assert(memcmp(phrase,decrypted,64)==0); memset(decrypted,0,64); /* read */ fd = open("./crypto_file",O_RDONLY); if(fd == -1) return -1; if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5, TWOFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1; printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,16)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+16,7)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+23,28)); printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+51,13)); printf("decrypted => '%s'\n\n",decrypted); close(fd); assert(memcmp(phrase,decrypted,64)==0); memset(decrypted,0,64); printf("tests ok\n"); return 0; }