diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2010-07-09 12:32:17 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2010-07-09 12:32:17 +0200 |
commit | 6c027b4de25a529908be895e7ff19236f4002a57 (patch) | |
tree | f70d3a400bbb9ba8a83e9d4ffcaa4b6e367fc8bb /test_crypto_api.c | |
download | crypto-6c027b4de25a529908be895e7ff19236f4002a57.zip crypto-6c027b4de25a529908be895e7ff19236f4002a57.tar.gz |
initial commit, resurrect one of my realy old projects
Diffstat (limited to 'test_crypto_api.c')
-rw-r--r-- | test_crypto_api.c | 325 |
1 files changed, 325 insertions, 0 deletions
diff --git a/test_crypto_api.c b/test_crypto_api.c new file mode 100644 index 0000000..5a20c77 --- /dev/null +++ b/test_crypto_api.c @@ -0,0 +1,325 @@ +#include <string.h> +#include <stdio.h> +#include <unistd.h> +#include <assert.h> + +#include "aes.h" +#include "des.h" +#include "blowfish.h" +#include "twofish.h" +#include "md4.h" +#include "md5.h" +#include "sha1.h" +#include "sha256.h" +#include "sha512.h" + + +int main(void) +{ + int i,j; + + struct blowfish_ctx bf_ctx; + struct twofish_ctx tf_ctx; + struct aes_ctx a_ctx; + struct des_ctx d_ctx; + struct des3_ede_ctx d3_ctx; + struct md4_ctx md4; + struct md5_ctx md5; + struct sha1_ctx sha1; + struct sha256_ctx sha256; + struct sha512_ctx sha512; + u32 flags; + + u8 key[]="9_&pass/#word%_0"; + + const u8 phrase[64] = "this#is/only|a?test,@nothing%more¬hing~less 0123456789 voila"; + u8 encrypt[64] = ""; + u8 decrypt[64] = ""; + u8 md4_hash[MD4_DIGEST_SIZE]; + u8 md5_hash[MD5_DIGEST_SIZE]; + u8 sha1_hash[SHA1_DIGEST_SIZE]; + u8 sha256_hash[SHA256_DIGEST_SIZE]; + u8 sha512_hash[SHA512_DIGEST_SIZE]; + + const u8 T1[64] = "123456"; + const u8 T2[64] = "789101"; + + printf("crypto api tests\n"); + printf("\n\tused phrase : '%s'\n\tlength : %d\n\n",phrase,strlen((char*)phrase)); + + memset(encrypt,0,sizeof(encrypt)); + memset(encrypt,0,sizeof(decrypt)); + + /* test blowfish */ + printf("_Blwofish: A 64-Bit Block Cipher_ by Bruce Schneier...\n"); + if(blowfish_setkey(&bf_ctx, key, strlen((char*)key), &flags)!=0){ + fprintf(stderr,"bf_setkey error.\n"); + _exit(1); + } + for(i=0; i<sizeof(phrase)/BF_BLOCK_SIZE; i++){ + blowfish_encrypt(&bf_ctx, encrypt, phrase+i*BF_BLOCK_SIZE); + blowfish_decrypt(&bf_ctx, decrypt+i*BF_BLOCK_SIZE, encrypt); + printf("\tencrypted block => "); + for(j=0; j<BF_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j]); + printf("\n"); + } + printf("\n\tdecrypted => '%s'\n\n",decrypt); + assert(memcmp(phrase,decrypt,64)==0); + + memset(encrypt,0,sizeof(encrypt)); + memset(encrypt,0,sizeof(decrypt)); + + printf("\twith non-full blocks\n"); + printf("\tfirst string : '%s'\n\tsecond string : '%s'\n",T1,T2); + blowfish_encrypt(&bf_ctx, encrypt, T1); + blowfish_encrypt(&bf_ctx, encrypt+BF_BLOCK_SIZE, T2); + printf("\tencrypted block => "); + for(j=0; j<BF_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j]); + printf("\n"); + printf("\tencrypted block => "); + for(j=0; j<BF_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j+BF_BLOCK_SIZE]); + printf("\n"); + blowfish_decrypt(&bf_ctx, decrypt, encrypt); + blowfish_decrypt(&bf_ctx, decrypt+strlen((char*)T1), encrypt+BF_BLOCK_SIZE); + printf("\n\tdecrypted => '%s'\n\n",decrypt); + assert(memcmp(decrypt,"123456789101",12)==0); + + memset(encrypt,0,sizeof(encrypt)); + memset(encrypt,0,sizeof(decrypt)); + + + + /* test twofish */ + printf("_Twofish: A 128-Bit Block Cipher_ by Bruce Schneier...\n"); + if(twofish_setkey(&tf_ctx, key, strlen((char*)key),&flags)!=0){ + fprintf(stderr,"twofish_setkey error.\n"); + _exit(1); + } + + for(i=0; i<sizeof(phrase)/TF_BLOCK_SIZE; i++){ + twofish_encrypt(&tf_ctx, encrypt, phrase+i*TF_BLOCK_SIZE); + twofish_decrypt(&tf_ctx, decrypt+i*TF_BLOCK_SIZE, encrypt); + printf("\tencrypted block => "); + for(j=0; j<TF_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j]); + printf("\n"); + } + printf("\n"); + printf("\n\tdecrypted => '%s'\n\n",decrypt); + assert(memcmp(phrase,decrypt,64)==0); + + memset(encrypt,0,sizeof(encrypt)); + memset(encrypt,0,sizeof(decrypt)); + + printf("\twith non-full blocks\n"); + printf("\tfirst string : '%s'\n\tsecond string : '%s'\n",T1,T2); + twofish_encrypt(&bf_ctx, encrypt, T1); + twofish_encrypt(&bf_ctx, encrypt+TF_BLOCK_SIZE, T2); + printf("\tencrypted block => "); + for(j=0; j<TF_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j]); + printf("\n"); + printf("\tencrypted block => "); + for(j=0; j<TF_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j+TF_BLOCK_SIZE]); + printf("\n"); + twofish_decrypt(&bf_ctx, decrypt, encrypt); + twofish_decrypt(&bf_ctx, decrypt+strlen((char*)T1), encrypt+TF_BLOCK_SIZE); + printf("\n\tdecrypted => '%s'\n\n",decrypt); + assert(memcmp(decrypt,"123456789101",12)==0); + + memset(encrypt,0,sizeof(encrypt)); + memset(encrypt,0,sizeof(decrypt)); + + + /* test aes */ + printf("_aes \n"); + if(aes_setkey(&a_ctx, key, strlen((char*)key),&flags)!=0){ + fprintf(stderr,"aes_setkey error.\n"); + _exit(1); + } + + for(i=0; i<sizeof(phrase)/AES_BLOCK_SIZE; i++){ + aes_encrypt(&a_ctx, encrypt, phrase+i*AES_BLOCK_SIZE); + aes_decrypt(&a_ctx, decrypt+i*AES_BLOCK_SIZE, encrypt); + printf("\tencrypted block => "); + for(j=0; j<AES_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j]); + printf("\n"); + } + printf("\n"); + printf("\n\tdecrypted => '%s'\n\n",decrypt); + assert(memcmp(phrase,decrypt,64)==0); + + memset(encrypt,0,sizeof(encrypt)); + memset(encrypt,0,sizeof(decrypt)); + + printf("\twith non-full blocks\n"); + printf("\tfirst string : '%s'\n\tsecond string : '%s'\n",T1,T2); + aes_encrypt(&a_ctx, encrypt, T1); + aes_encrypt(&a_ctx, encrypt+AES_BLOCK_SIZE, T2); + printf("\tencrypted block => "); + for(j=0; j<AES_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j]); + printf("\n"); + printf("\tencrypted block => "); + for(j=0; j<AES_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j+AES_BLOCK_SIZE]); + printf("\n"); + aes_decrypt(&a_ctx, decrypt, encrypt); + aes_decrypt(&a_ctx, decrypt+strlen((char*)T1), encrypt+AES_BLOCK_SIZE); + printf("\n\tdecrypted => '%s'\n\n",decrypt); + assert(memcmp(decrypt,"123456789101",12)==0); + + memset(encrypt,0,sizeof(encrypt)); + memset(encrypt,0,sizeof(decrypt)); + + /* test des */ + printf("_des \n"); + if(des_setkey(&d_ctx, key, strlen((char*)key),&flags)!=0){ + fprintf(stderr,"des_setkey error.\n"); + _exit(1); + } + + for(i=0; i<sizeof(phrase)/DES_BLOCK_SIZE; i++){ + des_encrypt(&d_ctx, encrypt, phrase+i*DES_BLOCK_SIZE); + des_decrypt(&d_ctx, decrypt+i*DES_BLOCK_SIZE, encrypt); + printf("\tencrypted block => "); + for(j=0; j<DES_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j]); + printf("\n"); + } + printf("\n"); + printf("\n\tdecrypted => '%s'\n\n",decrypt); + assert(memcmp(phrase,decrypt,64)==0); + + memset(encrypt,0,sizeof(encrypt)); + memset(encrypt,0,sizeof(decrypt)); + + printf("\twith non-full blocks\n"); + printf("\tfirst string : '%s'\n\tsecond string : '%s'\n",T1,T2); + des_encrypt(&d_ctx, encrypt, T1); + des_encrypt(&d_ctx, encrypt+DES_BLOCK_SIZE, T2); + printf("\tencrypted block => "); + for(j=0; j<DES_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j]); + printf("\n"); + printf("\tencrypted block => "); + for(j=0; j<DES_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j+DES_BLOCK_SIZE]); + printf("\n"); + des_decrypt(&d_ctx, decrypt, encrypt); + des_decrypt(&d_ctx, decrypt+strlen((char*)T1), encrypt+DES_BLOCK_SIZE); + printf("\n\tdecrypted => '%s'\n\n",decrypt); + assert(memcmp(decrypt,"123456789101",12)==0); + + memset(encrypt,0,sizeof(encrypt)); + memset(encrypt,0,sizeof(decrypt)); + + /* test des3_ede */ + printf("_des3_ede \n"); + if(des3_ede_setkey(&d3_ctx, key, strlen((char*)key),&flags)!=0){ + fprintf(stderr,"des_setkey error.\n"); + _exit(1); + } + + for(i=0; i<sizeof(phrase)/DES3_EDE_BLOCK_SIZE; i++){ + des3_ede_encrypt(&d3_ctx, encrypt, phrase+i*DES3_EDE_BLOCK_SIZE); + des3_ede_decrypt(&d3_ctx, decrypt+i*DES3_EDE_BLOCK_SIZE, encrypt); + printf("\tencrypted block => "); + for(j=0; j<DES3_EDE_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j]); + printf("\n"); + } + printf("\n"); + printf("\n\tdecrypted => '%s'\n\n",decrypt); + assert(memcmp(phrase,decrypt,64)==0); + + memset(encrypt,0,sizeof(encrypt)); + memset(encrypt,0,sizeof(decrypt)); + + printf("\twith non-full blocks\n"); + printf("\tfirst string : '%s'\n\tsecond string : '%s'\n",T1,T2); + des3_ede_encrypt(&d3_ctx, encrypt, T1); + des3_ede_encrypt(&d3_ctx, encrypt+DES3_EDE_BLOCK_SIZE, T2); + printf("\tencrypted block => "); + for(j=0; j<DES3_EDE_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j]); + printf("\n"); + printf("\tencrypted block => "); + for(j=0; j<DES3_EDE_BLOCK_SIZE; j++) + printf("%02x ",encrypt[j+DES3_EDE_BLOCK_SIZE]); + printf("\n"); + des3_ede_decrypt(&d3_ctx, decrypt, encrypt); + des3_ede_decrypt(&d3_ctx, decrypt+strlen((char*)T1), encrypt+DES3_EDE_BLOCK_SIZE); + printf("\n\tdecrypted => '%s'\n\n",decrypt); + assert(memcmp(decrypt,"123456789101",12)==0); + + memset(encrypt,0,sizeof(encrypt)); + memset(encrypt,0,sizeof(decrypt)); + + /* md4 */ + printf("MD4: one way hash with a %d bits output digest.\n",MD4_DIGEST_SIZE); + md4_init(&md4); + md4_update(&md4,(u8*)phrase,32); + md4_update(&md4,(u8*)phrase+32,strlen((char*)phrase)-32); + md4_final(&md4,md4_hash); + printf("\n\tdigest : "); + for(i=0; i<MD4_DIGEST_SIZE; i++) + printf("%02x",md4_hash[i]); + printf("\n\n"); + + /* md5 */ + printf("MD5: one way hash with a %d bits output digest.\n",MD5_DIGEST_SIZE); + md5_init(&md5); + md5_update(&md5,(u8*)phrase,32); + md5_update(&md5,(u8*)phrase+32,strlen((char*)phrase)-32); + md5_final(&md5,md5_hash); + printf("\n\tdigest : "); + for(i=0; i<MD5_DIGEST_SIZE; i++) + printf("%02x",md5_hash[i]); + printf("\n\n"); + + /* sha1 */ + printf("SHA1: one way hash with a %d bits output digest.\n",SHA1_DIGEST_SIZE); + sha1_init(&sha1); + sha1_update(&sha1,(u8*)phrase,32); + sha1_update(&sha1,(u8*)phrase+32,strlen((char*)phrase)-32); + sha1_final(&sha1,sha1_hash); + printf("\n\tdigest : "); + for(i=0; i<SHA1_DIGEST_SIZE; i++) + printf("%02x",sha1_hash[i]); + printf("\n\n"); + + /* sha256 */ + printf("SHA256: one way hash with a %d bits output digest.\n",SHA256_DIGEST_SIZE); + sha256_init(&sha256); + sha256_update(&sha256,(u8*)phrase,32); + sha256_update(&sha256,(u8*)phrase+32,strlen((char*)phrase)-32); + sha256_final(&sha256,sha256_hash); + printf("\n\tdigest : "); + for(i=0; i<SHA256_DIGEST_SIZE; i++) + printf("%02x",sha256_hash[i]); + printf("\n\n"); + + /* sha512 */ + printf("SHA512: one way hash with a %d bits output digest.\n",SHA512_DIGEST_SIZE); + sha512_init(&sha512); + sha512_update(&sha512,(u8*)phrase,32); + sha512_update(&sha512,(u8*)phrase+32,strlen((char*)phrase)-32); + sha512_final(&sha512,sha512_hash); + printf("\n\tdigest : "); + for(i=0; i<SHA512_DIGEST_SIZE/2; i++) + printf("%02x",sha512_hash[i]); + printf("\n\t "); + for(; i<SHA512_DIGEST_SIZE; i++) + printf("%02x",sha512_hash[i]); + printf("\n\n"); + + printf("tests ok\n"); + + return 0; +} |