summaryrefslogtreecommitdiffstats
path: root/test_crypto_buffer.c
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2010-07-09 12:32:17 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2010-07-09 12:32:17 +0200
commit6c027b4de25a529908be895e7ff19236f4002a57 (patch)
treef70d3a400bbb9ba8a83e9d4ffcaa4b6e367fc8bb /test_crypto_buffer.c
downloadcrypto-6c027b4de25a529908be895e7ff19236f4002a57.zip
crypto-6c027b4de25a529908be895e7ff19236f4002a57.tar.gz
initial commit, resurrect one of my realy old projects
Diffstat (limited to 'test_crypto_buffer.c')
-rw-r--r--test_crypto_buffer.c222
1 files changed, 222 insertions, 0 deletions
diff --git a/test_crypto_buffer.c b/test_crypto_buffer.c
new file mode 100644
index 0000000..25e6c93
--- /dev/null
+++ b/test_crypto_buffer.c
@@ -0,0 +1,222 @@
+#include <assert.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#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;
+}