#include #include #include #include #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 "); for(j=0; j '%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 "); for(j=0; j '%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 "); for(j=0; j '%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 "); for(j=0; j '%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 "); for(j=0; j '%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 "); for(j=0; j '%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 "); for(j=0; j '%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 "); for(j=0; j '%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 "); for(j=0; j '%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 "); for(j=0; j '%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