Changeset 491

Show
Ignore:
Timestamp:
08/24/07 11:43:35
Author:
rachel
Message:

Added basic CFB test for more symmetric algorithms

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/tests/test_crypto.c

    r488 r491  
    1919#include "tests.h" 
    2020 
    21 /* 
    22 static unsigned char* literal_data=NULL; 
    23 static size_t sz_literal_data=0; 
    24 static unsigned char* mdc_data=NULL; 
    25 static size_t sz_mdc_data=0; 
    26 static unsigned char* encrypted_pk_sk=NULL; 
    27 static size_t sz_encrypted_pk_sk=0; 
    28  
    29 #define MAXBUF 128 
    30  
    31 static void cleanup(); 
    32 */ 
    33  
    3421/*  
    3522 * initialisation 
     
    3825int init_suite_crypto(void) 
    3926    { 
    40 #ifdef XXX 
    41     char keydetails[MAXBUF+1]; 
    42     char keyring_name[MAXBUF+1]; 
    43     int fd=0; 
    44     char cmd[MAXBUF+1]; 
    45  
    46     // Initialise OPS  
    47     ops_init(); 
    48  
    49     char *rsa_nopass="Key-Type: RSA\nKey-Usage: encrypt, sign\nName-Real: Alpha\nName-Comment: RSA, no passphrase\nName-Email: alpha@test.com\nKey-Length: 1024\n"; 
    50     // Create temp directory 
    51     if (!mktmpdir()) 
    52         return 1; 
    53  
    54     /* 
    55      * Create a RSA keypair with no passphrase 
    56      */ 
    57  
    58     snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.alpha"); 
    59  
    60     if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) 
    61         { 
    62         fprintf(stderr,"Can't create key details\n"); 
    63         return 1; 
    64         } 
    65  
    66     write(fd,rsa_nopass,strlen(rsa_nopass)); 
    67     close(fd); 
    68  
    69     snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); 
    70     system(cmd); 
    71  
    72     // read keyrings 
    73     snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir); 
    74     ops_keyring_read(&pub_keyring,keyring_name); 
    75  
    76     // read keyring 
    77     snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir); 
    78     ops_keyring_read(&sec_keyring,keyring_name); 
    79 #endif 
    80  
    8127    // Return success 
    8228    return 0; 
     
    8531int clean_suite_crypto(void) 
    8632    { 
    87 #ifdef XXX 
    88     /* Close OPS */ 
    89      
    90     ops_finish(); 
    91 #endif 
    9233    reset_vars(); 
    9334 
     
    9536    } 
    9637 
    97 static void test_cfb_aes256(
     38static void test_cfb(ops_symmetric_algorithm_t alg
    9839    { 
    9940    // Used for trying low-level OpenSSL tests 
    10041 
     42    int verbose=0; 
     43 
    10144    ops_crypt_t crypt; 
    102     ops_crypt_any(&crypt, OPS_SA_AES_256); 
    103  
    104     /*  
    105        AES init 
    106        using empty IV and key for the moment  
    107     */ 
    108     unsigned char *iv=ops_mallocz(crypt.blocksize); 
    109     unsigned char *key=ops_mallocz(crypt.keysize); 
    110     snprintf((char *)key, crypt.keysize, "AES_KEY"); 
     45    unsigned char *iv=NULL; 
     46    unsigned char *key=NULL; 
     47    unsigned char *in=NULL; 
     48    unsigned char *out=NULL; 
     49    unsigned char *out2=NULL; 
     50 
     51    /* 
     52     * Initialise Crypt structure 
     53     * Empty IV, made-up key 
     54     */ 
     55 
     56    ops_crypt_any(&crypt, alg); 
     57    iv=ops_mallocz(crypt.blocksize); 
     58    key=ops_mallocz(crypt.keysize); 
     59    snprintf((char *)key, crypt.keysize, "MY KEY"); 
    11160    crypt.set_iv(&crypt, iv); 
    11261    crypt.set_key(&crypt, key); 
    11362    ops_encrypt_init(&crypt); 
    11463 
    115     unsigned char *in=ops_mallocz(crypt.blocksize); 
    116     unsigned char *out=ops_mallocz(crypt.blocksize); 
    117     unsigned char *out2=ops_mallocz(crypt.blocksize); 
     64    /* 
     65     * Create test buffers 
     66     */ 
     67    in=ops_mallocz(crypt.blocksize); 
     68    out=ops_mallocz(crypt.blocksize); 
     69    out2=ops_mallocz(crypt.blocksize); 
    11870 
    11971    snprintf((char *)in,crypt.blocksize,"hello"); 
    12072 
    121     printf("\n"); 
    122     printf("in:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
    123            in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); 
    124     printf("in:\t%c    %c    %c    %c      %c    %c    %c    %c\n",  
    125            in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); 
    126  
    12773    crypt.block_encrypt(&crypt, out, in); 
    128     //        AES_ecb_encrypt(in,out,crypt.data,AES_ENCRYPT); 
    129  
    130     printf("out:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
    131            out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 
    132     printf("out:\t%c    %c    %c    %c      %c    %c    %c    %c\n",  
    133            out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 
    134  
    13574 
    13675    crypt.block_decrypt(&crypt, out2, out); 
    137     //        AES_ecb_encrypt(out,out2,crypt.data,AES_DECRYPT); 
    138     printf("out2:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
    139            out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 
    140     printf("out2:\t%c    %c    %c    %c      %c    %c    %c    %c\n",  
    141            out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 
    142  
    14376    CU_ASSERT(memcmp((char *)in, (char *)out2, strlen((char *)in))==0); 
    14477 
     78    if (verbose) 
     79        { 
     80        // plaintext 
     81        printf("\n"); 
     82        printf("plaintext: 0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
     83               in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); 
     84        printf("plaintext: %c    %c    %c    %c      %c    %c    %c    %c\n",  
     85               in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); 
     86 
     87        // encrypted 
     88        printf("encrypted: 0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
     89               out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 
     90        printf("encrypted: %c    %c    %c    %c      %c    %c    %c    %c\n",  
     91               out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 
     92 
     93        // decrypted 
     94        printf("decrypted: 0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
     95               out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 
     96        printf("decrypted: %c    %c    %c    %c      %c    %c    %c    %c\n",  
     97               out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 
     98        } 
     99    } 
     100 
     101#ifndef OPENSSL_NO_IDEA 
     102static void test_cfb_idea() 
     103    { 
     104    test_cfb(OPS_SA_IDEA); 
     105    } 
     106#endif 
     107 
     108static void test_cfb_3des() 
     109    { 
     110    test_cfb(OPS_SA_TRIPLEDES); 
    145111    } 
    146112 
    147113static void test_cfb_cast() 
    148114    { 
    149     // Used for trying low-level OpenSSL tests 
    150  
    151     ops_crypt_t crypt; 
    152     ops_crypt_any(&crypt, OPS_SA_CAST5); 
    153  
    154     /* 
    155      * CAST 
    156      */ 
    157     unsigned char *iv=NULL; 
    158     unsigned char *key=NULL; 
    159     iv=ops_mallocz(crypt.blocksize); 
    160     key=ops_mallocz(crypt.keysize); 
    161     //    snprintf((char *)key, crypt_cast.keysize, "CAST_KEY"); 
    162     crypt.set_iv(&crypt, iv); 
    163     crypt.set_key(&crypt, key); 
    164     ops_encrypt_init(&crypt); 
    165  
    166     unsigned char *in=ops_mallocz(crypt.blocksize); 
    167     unsigned char *out=ops_mallocz(crypt.blocksize); 
    168     unsigned char *out2=ops_mallocz(crypt.blocksize); 
    169  
    170     snprintf((char *)in,crypt.blocksize,"hello"); 
    171         /* 
    172     printf("\n"); 
    173     printf("in:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
    174            in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); 
    175     printf("in:\t%c    %c    %c    %c      %c    %c    %c    %c\n",  
    176            in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); 
    177         */ 
    178  
    179     crypt.block_encrypt(&crypt, out, in); 
    180     //    AES_ecb_encrypt(in,out,crypt.data,AES_ENCRYPT); 
    181         /* 
    182     printf("out:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
    183            out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 
    184     printf("out:\t%c    %c    %c    %c      %c    %c    %c    %c\n",  
    185            out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 
    186         */ 
    187  
    188     crypt.block_decrypt(&crypt, out2, out); 
    189     //    AES_ecb_encrypt(out,out2,crypt.data,AES_DECRYPT); 
    190         /* 
    191     printf("out2:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
    192            out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 
    193     printf("out2:\t%c    %c    %c    %c      %c    %c    %c    %c\n",  
    194            out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 
    195         */ 
    196     CU_ASSERT(memcmp((char *)in, (char *)out2, strlen((char *)in))==0); 
    197  
     115    test_cfb(OPS_SA_CAST5); 
     116    } 
     117 
     118static void test_cfb_aes256() 
     119    { 
     120    test_cfb(OPS_SA_AES_256); 
    198121    } 
    199122 
     
    274197    // add tests to suite 
    275198     
     199#ifndef OPENSSL_NO_IDEA 
     200    if (NULL == CU_add_test(suite, "Test CFB (IDEA)", test_cfb_idea)) 
     201            return NULL; 
     202#endif 
     203 
     204    if (NULL == CU_add_test(suite, "Test CFB (TripleDES)", test_cfb_3des)) 
     205            return NULL; 
     206 
     207    if (NULL == CU_add_test(suite, "Test CFB (CAST)", test_cfb_cast)) 
     208            return NULL; 
     209 
     210    //    test_one_cfb(OPS_SA_BLOWFISH); 
     211    //    test_one_cfb(OPS_SA_AES_128); 
     212    //    test_one_cfb(OPS_SA_AES_192); 
     213 
    276214    if (NULL == CU_add_test(suite, "Test CFB AES 256", test_cfb_aes256)) 
    277215            return NULL; 
    278216 
    279     if (NULL == CU_add_test(suite, "Test CFB CAST", test_cfb_cast)) 
    280             return NULL; 
    281  
     217    //    test_one_cfb(OPS_SA_TWOFISH); 
     218 
     219    /* 
     220     */ 
    282221    if (NULL == CU_add_test(suite, "Test RSA", test_rsa)) 
    283222            return NULL;