Changeset 492

Show
Ignore:
Timestamp:
08/24/07 13:07:08
Author:
rachel
Message:

Added tests for other symmetric algorithms in RSA decrypt test

Files:

Legend:

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

    r489 r492  
    1818#include "../src/advanced/keyring_local.h" 
    1919 
    20 //static ops_keyring_t keyring; 
    2120static char *filename_rsa_noarmour_nopassphrase="dec_rsa_noarmour_nopassphrase.txt"; 
     21 
    2222static char *filename_rsa_armour_nopassphrase="dec_rsa_armour_nopassphrase.txt"; 
    2323static char *filename_rsa_noarmour_passphrase="dec_rsa_noarmour_passphrase.txt"; 
     
    2626static char *passphrase="hello"; 
    2727static char *current_passphrase=NULL; 
    28  
    29 //static char* text; 
    30  
    31 /* 
    32 static int create_testfile(const char *name) 
    33     { 
    34     char filename[MAXBUF+1]; 
    35     char buffer[MAXBUF+1]; 
    36  
    37     int fd=0; 
    38     snprintf(filename,MAXBUF,"%s/%s",dir,name); 
    39     if ((fd=open(filename,O_WRONLY| O_CREAT | O_EXCL, 0600))<0) 
    40         return 0; 
    41  
    42     create_testtext(name,&buffer[0],MAXBUF); 
    43     write(fd,buffer,strlen(buffer)); 
    44     close(fd); 
    45     return 1; 
    46     } 
    47 */ 
    4828 
    4929static ops_parse_cb_return_t 
     
    140120    create_testfile(filename_rsa_armour_passphrase); 
    141121 
    142     // Restrict list of algorithms used 
    143     
    144     //    snprintf(cmd,MAXBUF,"gpg --homedir=%s --default-preference-list \"CAST5\"", dir); 
    145     //    if (system(cmd)) 
    146     //        return -1; 
    147  
    148     // Now encrypt the test files with GPG 
    149     // Note:: To make it do SE_IP packets, do NOT use --openpgp and DO use --force-mdc 
    150     snprintf(cmd,MAXBUF,"gpg --homedir=%s --cipher-algo \"CAST5\" --force-mdc --compress-level 0 --quiet --encrypt --recipient Alpha %s/%s", dir, dir, filename_rsa_noarmour_nopassphrase); 
    151     if (system(cmd)) 
    152         { 
    153         return 1; 
    154         } 
    155  
     122    /* 
     123     * Now encrypt the test files with GPG 
     124     * Note:: To make it do SE_IP packets, do NOT use --openpgp and DO use --force-mdc 
     125     */ 
     126 
     127    // default symmetric algorithm 
     128    snprintf(cmd,MAXBUF,"gpg --homedir=%s --force-mdc --compress-level 0 --quiet --encrypt --recipient Alpha %s/%s", dir, dir, filename_rsa_noarmour_nopassphrase); 
     129    if (system(cmd)) 
     130        { 
     131        return 1; 
     132        } 
     133 
     134#ifndef OPENSSL_NO_IDEA 
     135    /* 
     136    // IDEA 
     137    snprintf(cmd,MAXBUF,"gpg --homedir=%s --cipher-algo \"IDEA\" --output=%s/IDEA_%s.gpg  --force-mdc --compress-level 0 --quiet --encrypt --recipient Alpha %s/%s", dir, dir, filename_rsa_noarmour_nopassphrase, dir, filename_rsa_noarmour_nopassphrase); 
     138    if (system(cmd)) 
     139        { 
     140        return 1; 
     141        } 
     142    */ 
     143#endif 
     144 
     145    // TripleDES  
     146    snprintf(cmd,MAXBUF,"gpg --homedir=%s --cipher-algo \"3DES\" --output=%s/3DES_%s.gpg  --force-mdc --compress-level 0 --quiet --encrypt --recipient Alpha %s/%s", dir, dir, filename_rsa_noarmour_nopassphrase, dir, filename_rsa_noarmour_nopassphrase); 
     147    if (system(cmd)) 
     148        { 
     149        return 1; 
     150        } 
     151 
     152    // Cast5 
     153    snprintf(cmd,MAXBUF,"gpg --homedir=%s --cipher-algo \"CAST5\" --output=%s/CAST5_%s.gpg  --force-mdc --compress-level 0 --quiet --encrypt --recipient Alpha %s/%s", dir, dir, filename_rsa_noarmour_nopassphrase, dir, filename_rsa_noarmour_nopassphrase); 
     154    if (system(cmd)) 
     155        { 
     156        return 1; 
     157        } 
     158 
     159    // AES256 
     160    snprintf(cmd,MAXBUF,"gpg --homedir=%s --cipher-algo \"AES256\" --output=%s/AES256_%s.gpg  --force-mdc --compress-level 0 --quiet --encrypt --recipient Alpha %s/%s", dir, dir, filename_rsa_noarmour_nopassphrase, dir, filename_rsa_noarmour_nopassphrase); 
     161    if (system(cmd)) 
     162        { 
     163        return 1; 
     164        } 
     165 
     166 
     167#ifdef TODO 
    156168    snprintf(cmd,MAXBUF,"gpg --openpgp --quiet --encrypt --personal-cipher-preferences='CAST5' --armor --homedir=%s --recipient Alpha %s/%s", dir, dir, filename_rsa_armour_nopassphrase); 
    157169    if (system(cmd)) 
     
    171183        return 1; 
    172184        } 
    173  
    174 #ifdef XXX 
    175     int fd=0; 
    176     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"; 
    177     char *rsa_pass="Key-Type: RSA\nKey-Usage: encrypt, sign\nName-Real: Bravo\nName-Comment: RSA, passphrase\nName-Email: bravo@test.com\nPassphrase: hello\nKey-Length: 1024\n"; 
    178      
    179     // Create temp directory 
    180     if (!mktmpdir()) 
    181         return 1; 
    182  
    183     /* 
    184      * Create a RSA keypair with no passphrase 
    185      */ 
    186  
    187     snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.alpha"); 
    188  
    189     if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) 
    190         { 
    191         fprintf(stderr,"Can't create key details\n"); 
    192         return 1; 
    193         } 
    194  
    195     write(fd,rsa_nopass,strlen(rsa_nopass)); 
    196     close(fd); 
    197  
    198     snprintf(cmd,MAXBUF,"gpg --openpgp --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); 
    199     system(cmd); 
    200  
    201     /* 
    202      * Create a RSA keypair with passphrase 
    203      */ 
    204  
    205     snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.bravo"); 
    206     if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) 
    207         { 
    208         fprintf(stderr,"Can't create key details\n"); 
    209         return 1; 
    210         } 
    211  
    212     write(fd,rsa_pass,strlen(rsa_pass)); 
    213     close(fd); 
    214  
    215     snprintf(cmd,MAXBUF,"gpg --openpgp --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); 
    216     system(cmd); 
    217  
    218  
    219     // Initialise OPS  
    220     ops_init(); 
    221  
    222     // read keyring 
    223     snprintf(secring,MAXBUF,"%s/secring.gpg", dir); 
    224     ops_keyring_read(&keyring,secring); 
    225185#endif 
    226186 
     
    232192    { 
    233193         
    234 #ifdef XXX 
    235     char cmd[MAXBUF+1]; 
    236     /* Close OPS */ 
    237      
    238     ops_keyring_free(&keyring); 
    239     ops_finish(); 
    240  
    241     /* Remove test dir and files */ 
    242     snprintf(cmd,MAXBUF,"rm -rf %s", dir); 
    243     if (system(cmd)) 
    244         { 
    245         perror("Can't delete test directory "); 
    246         return 1; 
    247         } 
    248 #endif 
    249      
    250194    reset_vars(); 
    251195 
     
    253197    } 
    254198 
    255 static void test_rsa_decrypt(const int has_armour, const int has_passphrase, const char *filename
     199static void test_rsa_decrypt(const int has_armour, const int has_passphrase, const char *filename, const char* protocol
    256200    { 
    257201    char encfile[MAXBUF+1]; 
     
    262206     
    263207    // open encrypted file 
    264     snprintf(encfile,MAXBUF,"%s/%s.%s",dir,filename,suffix); 
     208    snprintf(encfile,MAXBUF,"%s/%s%s%s.%s",dir, 
     209             protocol==NULL ? "" : protocol, 
     210             protocol==NULL ? "" : "_", 
     211             filename,suffix); 
    265212    fd=open(encfile,O_RDONLY); 
    266213    if(fd < 0) 
     
    301248    int armour=0; 
    302249    int passphrase=0; 
    303     test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_nopassphrase); 
    304     } 
     250    test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_nopassphrase,NULL); 
     251    } 
     252 
     253#ifndef OPENSSL_NO_IDEA 
     254void test_rsa_decrypt_noarmour_nopassphrase_idea(void) 
     255    { 
     256    int armour=0; 
     257    int passphrase=0; 
     258    test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_nopassphrase,"IDEA"); 
     259    } 
     260#endif 
     261 
     262void test_rsa_decrypt_noarmour_nopassphrase_3des(void) 
     263    { 
     264    int armour=0; 
     265    int passphrase=0; 
     266    test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_nopassphrase,"3DES"); 
     267    } 
     268 
     269void test_rsa_decrypt_noarmour_nopassphrase_cast5(void) 
     270    { 
     271    int armour=0; 
     272    int passphrase=0; 
     273    test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_nopassphrase,"CAST5"); 
     274    } 
     275 
     276void test_rsa_decrypt_noarmour_nopassphrase_aes256(void) 
     277    { 
     278    int armour=0; 
     279    int passphrase=0; 
     280    test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_nopassphrase,"AES256"); 
     281    } 
     282 
     283// 
    305284 
    306285void test_rsa_decrypt_armour_nopassphrase(void) 
     
    308287    int armour=1; 
    309288    int passphrase=0; 
    310     test_rsa_decrypt(armour,passphrase,filename_rsa_armour_nopassphrase); 
     289    test_rsa_decrypt(armour,passphrase,filename_rsa_armour_nopassphrase,NULL); 
    311290    } 
    312291 
     
    315294    int armour=0; 
    316295    int passphrase=1; 
    317     test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_passphrase); 
     296    test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_passphrase,NULL); 
    318297    } 
    319298 
     
    322301    int armour=1; 
    323302    int passphrase=1; 
    324     test_rsa_decrypt(armour,passphrase,filename_rsa_armour_passphrase); 
     303    test_rsa_decrypt(armour,passphrase,filename_rsa_armour_passphrase,NULL); 
    325304    } 
    326305 
     
    345324    // add tests to suite 
    346325     
    347     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_decrypt_noarmour_nopassphrase)) 
     326    if (NULL == CU_add_test(suite, "Unarmoured, no passphrase (CAST5)", test_rsa_decrypt_noarmour_nopassphrase_cast5)) 
     327            return NULL; 
     328     
     329    if (NULL == CU_add_test(suite, "Unarmoured, no passphrase (Default)", test_rsa_decrypt_noarmour_nopassphrase)) 
     330            return NULL; 
     331     
     332#ifndef OPENSSL_NO_IDEA 
     333    /* 
     334    if (NULL == CU_add_test(suite, "Unarmoured, no passphrase (IDEA)", test_rsa_decrypt_noarmour_nopassphrase_idea)) 
     335            return NULL; 
     336    */ 
     337#endif 
     338     
     339    if (NULL == CU_add_test(suite, "Unarmoured, no passphrase (3DES)", test_rsa_decrypt_noarmour_nopassphrase_3des)) 
     340            return NULL; 
     341     
     342    if (NULL == CU_add_test(suite, "Unarmoured, no passphrase (AES256)", test_rsa_decrypt_noarmour_nopassphrase_aes256)) 
    348343            return NULL; 
    349344