Changeset 511

Show
Ignore:
Timestamp:
09/21/07 18:35:41
Author:
rachel
Message:

Fixed problem with large file encryption
(Note: there is an interoperability problem with GPG)
Split GPG encryption interop tests into separate suite

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/examples/packet-dump.c

    r447 r511  
    462462               content->ptag.length,content->ptag.length, 
    463463               content->ptag.position,content->ptag.position); 
    464         /* 
    465         print_tagname(ops_str_from_single_packet_tag(content->ptag.content_tag)); 
    466         */ 
     464        print_tagname(ops_show_packet_tag(content->ptag.content_tag)); 
    467465        break; 
    468466 
  • openpgpsdk/trunk/src/advanced/adv_create.c

    r509 r511  
    784784) 
    785785    { 
     786    int debug=0; 
     787 
    786788    //unsigned char encmpibuf[8192]; 
    787789    //    unsigned char EM[8192]; 
     
    818820     
    819821 
    820     /* 
    821     //    int i=0; 
    822     fprintf(stderr,"Encoded Message: \n"); 
    823     for (i=0; i<mLen; i++) 
    824         fprintf(stderr,"%2x ", EM[i]); 
    825     fprintf(stderr,"\n"); 
    826     */ 
     822    if (debug) 
     823        { 
     824        unsigned int i=0; 
     825        fprintf(stderr,"Encoded Message: \n"); 
     826        for (i=0; i<mLen; i++) 
     827            fprintf(stderr,"%2x ", EM[i]); 
     828        fprintf(stderr,"\n"); 
     829        } 
    827830 
    828831    return ops_true; 
     
    841844     */ 
    842845 
     846    int debug=0; 
     847 
    843848    const ops_public_key_t* pub_key=ops_get_public_key_from_data(key); 
    844849    const size_t sz_unencoded_m_buf=CAST_KEY_LENGTH+1+2; 
     
    854859    memcpy(session_key->key_id, key->key_id, sizeof session_key->key_id); 
    855860 
    856     /* 
    857     fprintf(stderr,"Encrypting for RSA key id : "); 
    858     unsigned int i=0; 
    859     for (i=0; i<sizeof session_key->key_id; i++) 
    860         fprintf(stderr,"%2x ", key->key_id[i]); 
    861     fprintf(stderr,"\n"); 
    862     */ 
     861    if (debug) 
     862        { 
     863        fprintf(stderr,"Encrypting for RSA key id : "); 
     864        unsigned int i=0; 
     865        for (i=0; i<sizeof session_key->key_id; i++) 
     866            fprintf(stderr,"%2x ", key->key_id[i]); 
     867        fprintf(stderr,"\n"); 
     868        } 
    863869 
    864870    assert(key->key.pkey.algorithm == OPS_PKA_RSA); 
    865871    session_key->algorithm=key->key.pkey.algorithm; 
    866     /* 
    867     session_key->symmetric_algorithm=OPS_SA_AES_256; 
    868     ops_random(session_key->key, 256/8); 
    869     */ 
     872 
    870873    // \todo allow user to specify other algorithm 
    871874    session_key->symmetric_algorithm=OPS_SA_CAST5; 
    872  
    873875    ops_random(session_key->key, CAST_KEY_LENGTH); 
    874     /* 
    875     fprintf(stderr,"CAST5 session key created (len=%d):\n ", CAST_KEY_LENGTH); 
    876     for (i=0; i<CAST_KEY_LENGTH; i++) 
    877         fprintf(stderr,"%2x ", session_key->key[i]); 
    878     fprintf(stderr,"\n"); 
    879     */ 
     876 
     877    if (debug) 
     878        { 
     879        unsigned int i=0; 
     880        fprintf(stderr,"CAST5 session key created (len=%d):\n ", CAST_KEY_LENGTH); 
     881        for (i=0; i<CAST_KEY_LENGTH; i++) 
     882            fprintf(stderr,"%2x ", session_key->key[i]); 
     883        fprintf(stderr,"\n"); 
     884        } 
    880885 
    881886    if (create_unencoded_m_buf(session_key, &unencoded_m_buf[0])==ops_false) 
    882887        return NULL; 
    883888 
    884     /* 
    885     printf("unencoded m buf:\n"); 
    886     for (i=0; i<sz_unencoded_m_buf; i++) 
    887         printf("%2x ", unencoded_m_buf[i]); 
    888     printf("\n"); 
    889     */ 
     889    if (debug) 
     890        { 
     891        unsigned int i=0; 
     892        printf("unencoded m buf:\n"); 
     893        for (i=0; i<sz_unencoded_m_buf; i++) 
     894            printf("%2x ", unencoded_m_buf[i]); 
     895        printf("\n"); 
     896        } 
    890897    encode_m_buf(&unencoded_m_buf[0], sz_unencoded_m_buf, pub_key, &encoded_m_buf[0]); 
    891  
     898     
    892899    // and encrypt it 
    893900    if(!ops_encrypt_mpi(encoded_m_buf, sz_encoded_m_buf, pub_key, &session_key->parameters)) 
  • openpgpsdk/trunk/src/advanced/adv_packet-parse.c

    r501 r511  
    22102210                                ops_parse_info_t *pinfo) 
    22112211    { 
     2212    int debug=0; 
    22122213    unsigned char c[1]; 
    22132214    ops_parser_content_t content; 
     
    22202221 
    22212222    // Can't rely on it being CAST5 
     2223    // \todo FIXME RW 
    22222224    //    const size_t sz_unencoded_m_buf=CAST_KEY_LENGTH+1+2; 
    22232225    const size_t sz_unencoded_m_buf=1024; 
     
    22392241        return 0; 
    22402242 
    2241     /* 
    2242     int i; 
    2243     int x=sizeof C.pk_session_key.key_id; 
    2244     printf("session key: public key id: x=%d\n",x); 
    2245     for (i=0; i<x; i++) 
    2246         printf("%2x ", C.pk_session_key.key_id[i]); 
    2247     printf("\n"); 
    2248     */ 
     2243    if (debug) 
     2244        { 
     2245        int i; 
     2246        int x=sizeof C.pk_session_key.key_id; 
     2247        printf("session key: public key id: x=%d\n",x); 
     2248        for (i=0; i<x; i++) 
     2249            printf("%2x ", C.pk_session_key.key_id[i]); 
     2250        printf("\n"); 
     2251        } 
    22492252 
    22502253    if(!limited_read(c,1,region,pinfo)) 
     
    23252328    memcpy(C.pk_session_key.key,unencoded_m_buf+1,k); 
    23262329 
    2327     /* 
    2328     printf("session key recovered (len=%d):\n",k); 
    2329     unsigned int j; 
    2330     for(j=0; j<k; j++) 
    2331         printf("%2x ", C.pk_session_key.key[j]); 
    2332     printf("\n"); 
    2333     */ 
     2330    if (debug) 
     2331        { 
     2332        printf("session key recovered (len=%d):\n",k); 
     2333        unsigned int j; 
     2334        for(j=0; j<k; j++) 
     2335            printf("%2x ", C.pk_session_key.key[j]); 
     2336        printf("\n"); 
     2337        } 
    23342338 
    23352339    C.pk_session_key.checksum=unencoded_m_buf[k+1]+(unencoded_m_buf[k+2] << 8); 
    2336     //    printf("session key checksum: %2x %2x\n", unencoded_m_buf[k+1], unencoded_m_buf[k+2]); 
     2340    if (debug) 
     2341        { 
     2342        printf("session key checksum: %2x %2x\n", unencoded_m_buf[k+1], unencoded_m_buf[k+2]); 
     2343        } 
    23372344 
    23382345    // Check checksum 
     
    23632370                             ops_parse_cb_info_t *cbinfo) 
    23642371    { 
     2372    int debug=0; 
     2373 
    23652374    /* 
    23662375      Gets entire SE_IP data packet. 
     
    23932402            return -1; 
    23942403 
     2404        if (debug) 
     2405            { 
     2406            fprintf(stderr,"\n\nentire SE IP packet (len=%d):\n",decrypted_region.length); 
     2407            unsigned int i=0; 
     2408            for (i=0; i<decrypted_region.length; i++) 
     2409                { 
     2410                fprintf(stderr,"0x%02x ", buf[i]); 
     2411                if (!((i+1)%8)) 
     2412                    fprintf(stderr,"\n"); 
     2413                } 
     2414            fprintf(stderr,"\n"); 
     2415            fprintf(stderr,"\n"); 
     2416            } 
     2417 
    23952418        // verify leading preamble 
    23962419 
    2397         /* debug 
    2398            fprintf(stderr,"\npreamble: "); 
    2399            unsigned int i=0; 
    2400            for (i=0; i<arg->decrypt->blocksize+2;i++) 
    2401            fprintf(stderr," 0x%02x", buf[i]); 
    2402            fprintf(stderr,"\n"); 
    2403         */ 
     2420        if (debug) 
     2421            { 
     2422            fprintf(stderr,"\npreamble: "); 
     2423            unsigned int i=0; 
     2424            for (i=0; i<arg->decrypt->blocksize+2;i++) 
     2425                fprintf(stderr," 0x%02x", buf[i]); 
     2426            fprintf(stderr,"\n"); 
     2427            } 
    24042428 
    24052429        size_t b=arg->decrypt->blocksize; 
     
    24242448        unsigned char* mdc_hash=mdc+2; 
    24252449     
     2450        if (debug) 
     2451            { 
     2452            unsigned int i=0; 
     2453 
     2454            fprintf(stderr,"\nplaintext (len=%ld): ",sz_plaintext); 
     2455            for (i=0; i<sz_plaintext;i++) 
     2456                fprintf(stderr," 0x%02x", plaintext[i]); 
     2457            fprintf(stderr,"\n"); 
     2458 
     2459            fprintf(stderr,"\nmdc (len=%ld): ",sz_mdc); 
     2460            for (i=0; i<sz_mdc;i++) 
     2461                fprintf(stderr," 0x%02x", mdc[i]); 
     2462            fprintf(stderr,"\n"); 
     2463            } 
     2464 
    24262465        ops_calc_mdc_hash(preamble,sz_preamble,plaintext,sz_plaintext,&hashed[0]); 
    24272466        /* 
  • openpgpsdk/trunk/src/advanced/adv_writer_encrypt.c

    r509 r511  
    2222                                      ops_writer_info_t *winfo) 
    2323    { 
     24    int debug=0; 
    2425    crypt_arg_t *arg=(crypt_arg_t *)ops_writer_get_arg(winfo); 
    2526 
     
    2829 
    2930#define BUFSZ 1024 // arbitrary number 
    30     unsigned char buf[BUFSZ]; 
     31    //    unsigned char buf[BUFSZ]; 
    3132    unsigned char encbuf[BUFSZ]; 
    3233    unsigned remaining=length; 
     34    unsigned done=0; 
    3335    while (remaining) 
    3436        { 
    3537        unsigned len = remaining < BUFSZ ? remaining : BUFSZ; 
    36         memcpy(buf,src,len); // \todo copy needed here? 
     38        //        memcpy(buf,src,len); // \todo copy needed here? 
    3739         
    38         arg->crypt->cfb_encrypt(arg->crypt, encbuf, buf, len); 
     40        arg->crypt->cfb_encrypt(arg->crypt, encbuf, src+done, len); 
    3941 
    40         /* 
    41         fprintf(stderr,"WRITING:\nunencrypted: "); 
    42         int i=0; 
    43         for (i=0; i<16; i++) 
    44             fprintf(stderr,"%2x ", buf[i]); 
    45         fprintf(stderr,"\n"); 
    46         fprintf(stderr,"encrypted:   "); 
    47         for (i=0; i<16; i++) 
    48             fprintf(stderr,"%2x ", encbuf[i]); 
    49         fprintf(stderr,"\n"); 
    50         */ 
     42        if (debug) 
     43            { 
     44            fprintf(stderr,"WRITING:\nunencrypted: "); 
     45            int i=0; 
     46            for (i=0; i<16; i++) 
     47                fprintf(stderr,"%2x ", src[done+i]); 
     48            fprintf(stderr,"\n"); 
     49            fprintf(stderr,"encrypted:   "); 
     50            for (i=0; i<16; i++) 
     51                fprintf(stderr,"%2x ", encbuf[i]); 
     52            fprintf(stderr,"\n"); 
     53            } 
    5154 
    5255        if (!ops_stacked_write(encbuf,len,errors,winfo)) 
    5356            return ops_false; 
    5457        remaining-=len; 
     58        done+=len; 
    5559        } 
    5660 
  • openpgpsdk/trunk/src/advanced/adv_writer_encrypt_se_ip.c

    r509 r511  
    7272    ops_create_info_t *my_cinfo; 
    7373 
    74     const unsigned int bufsz=128; // \todo good value? 
     74    const unsigned int bufsz=128; // initial value; gets expanded as necessary 
    7575    ops_setup_memory_write(&cinfo_literal,&mem_literal,bufsz); 
    7676    ops_setup_memory_write(&my_cinfo,&my_mem,bufsz); 
    7777 
    78     // \todo handle larger packets e.g. do loop 
    79  
    80     unsigned remaining=length; 
    81     unsigned done=0; 
    82     while (remaining && rtn==ops_true) 
    83         { 
    84         unsigned len = remaining < bufsz ? remaining : bufsz; 
    85  
    86         // create literal data packet from source data 
    87         ops_write_literal_data(src+done, len, OPS_LDT_BINARY, cinfo_literal); 
     78    // create literal data packet from source data 
     79    ops_write_literal_data(src, length, OPS_LDT_BINARY, cinfo_literal); 
     80    assert(ops_memory_get_length(mem_literal)>length); 
     81 
     82    // create SE IP packet set from this literal data 
     83    ops_write_se_ip_pktset(ops_memory_get_data(mem_literal),  
     84                           ops_memory_get_length(mem_literal),  
     85                           arg->crypt, my_cinfo); 
     86    assert(ops_memory_get_length(my_mem)>ops_memory_get_length(mem_literal)); 
     87 
     88    // now write memory to next writer 
     89    rtn=ops_stacked_write(ops_memory_get_data(my_mem), 
     90                          ops_memory_get_length(my_mem), 
     91                          errors, winfo); 
    8892     
    89         // create SE IP packet set from this literal data 
    90         ops_write_se_ip_pktset(ops_memory_get_data(mem_literal),  
    91                                ops_memory_get_length(mem_literal),  
    92                                arg->crypt, my_cinfo); 
    93  
    94         // now write memory to next writer 
    95         rtn=ops_stacked_write(ops_memory_get_data(my_mem), 
    96                               ops_memory_get_length(my_mem), 
    97                               errors, winfo); 
    98  
    99         remaining-=len; 
    100         done+=len; 
    101         } 
    10293    ops_memory_free(my_mem); 
    10394    ops_memory_free(mem_literal); 
     
    115106    } 
    116107 
    117 void ops_calc_mdc_hash(const unsigned char* preamble, const size_t sz_preamble, const unsigned char* data, const unsigned int len, unsigned char *hashed) 
    118     { 
     108void ops_calc_mdc_hash(const unsigned char* preamble, const size_t sz_preamble, const unsigned char* plaintext, const unsigned int sz_plaintext, unsigned char *hashed) 
     109    { 
     110    int debug=0; 
    119111    ops_hash_t hash; 
    120     //    unsigned char hashed[SHA_DIGEST_LENGTH]; 
    121112    unsigned char c[0]; 
     113 
     114    if (debug) 
     115        { 
     116        fprintf(stderr,"ops_calc_mdc_hash():\n"); 
     117 
     118        fprintf(stderr,"\npreamble: "); 
     119        unsigned int i=0; 
     120        for (i=0; i<sz_preamble;i++) 
     121            fprintf(stderr," 0x%02x", preamble[i]); 
     122        fprintf(stderr,"\n"); 
     123 
     124        fprintf(stderr,"\nplaintext (len=%d): ",sz_plaintext); 
     125        for (i=0; i<sz_plaintext;i++) 
     126            fprintf(stderr," 0x%02x", plaintext[i]); 
     127        fprintf(stderr,"\n"); 
     128        } 
    122129 
    123130    // init 
     
    128135    hash.add(&hash,preamble,sz_preamble); 
    129136    // plaintext 
    130     hash.add(&hash,data,len);  
     137    hash.add(&hash,plaintext,sz_plaintext);  
    131138    // MDC packet tag 
    132139    c[0]=0xD3; 
     
    138145    //finish 
    139146    hash.finish(&hash,hashed); 
     147 
     148    if (debug) 
     149        { 
     150        unsigned int i=0; 
     151        fprintf(stderr,"\nhashed (len=%d): ",SHA_DIGEST_LENGTH); 
     152        for (i=0; i<SHA_DIGEST_LENGTH;i++) 
     153            fprintf(stderr," 0x%02x", hashed[i]); 
     154        fprintf(stderr,"\n"); 
     155        } 
    140156    } 
    141157 
     
    145161                                   ops_create_info_t *cinfo) 
    146162    { 
     163    int debug=0; 
    147164    unsigned char hashed[SHA_DIGEST_LENGTH]; 
    148165    const size_t sz_mdc=1+1+SHA_DIGEST_LENGTH; 
     
    165182    preamble[crypt->blocksize+1]=preamble[crypt->blocksize-1]; 
    166183 
    167     /* debug 
    168     fprintf(stderr,"\npreamble: "); 
    169     unsigned int i=0; 
    170     for (i=0; i<sz_preamble;i++) 
    171         fprintf(stderr," 0x%02x", preamble[i]); 
    172     fprintf(stderr,"\n"); 
    173     */ 
     184    if (debug) 
     185        { 
     186        fprintf(stderr,"\npreamble: "); 
     187        unsigned int i=0; 
     188        for (i=0; i<sz_preamble;i++) 
     189            fprintf(stderr," 0x%02x", preamble[i]); 
     190        fprintf(stderr,"\n"); 
     191        } 
    174192 
    175193    // now construct MDC packet and add to the end of the buffer 
     
    184202    ops_write_mdc(hashed, cinfo_mdc); 
    185203 
     204    if (debug) 
     205        { 
     206        unsigned int i=0; 
     207 
     208        fprintf(stderr,"\nplaintext: "); 
     209        size_t sz_plaintext=len; 
     210        for (i=0; i<sz_plaintext;i++) 
     211            fprintf(stderr," 0x%02x", data[i]); 
     212        fprintf(stderr,"\n"); 
     213         
     214        fprintf(stderr,"\nmdc: "); 
     215        size_t sz_mdc=1+1+OPS_SHA1_HASH_SIZE; 
     216        unsigned char* mdc=ops_memory_get_data(mem_mdc); 
     217        for (i=0; i<sz_mdc;i++) 
     218            fprintf(stderr," 0x%02x", mdc[i]); 
     219        fprintf(stderr,"\n"); 
     220        } 
     221     
    186222    // and write it out 
    187223 
    188224    ops_writer_push_encrypt_crypt(cinfo, crypt); 
    189225 
    190     /* 
    191     fprintf(stderr,"writing %ld + %d + %ld\n", sz_preamble, len, ops_memory_get_length(mem_mdc)); 
    192     */ 
     226    if (debug) 
     227        { 
     228        fprintf(stderr,"writing %ld + %d + %ld\n", sz_preamble, len, ops_memory_get_length(mem_mdc)); 
     229        } 
    193230 
    194231    if (!ops_write(preamble, sz_preamble,cinfo) 
  • openpgpsdk/trunk/tests/test_packet_types.c

    r510 r511  
    162162    // and parse it 
    163163 
     164    ops_memory_init(mem_literal_data,128); 
    164165    ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
    165166    rtn=ops_parse(pinfo); 
     
    170171     */ 
    171172 
    172     CU_ASSERT(strncmp((char *)literal_data,testtext,MAXBUF)==0); 
     173    CU_ASSERT(strncmp((char *)ops_memory_get_data(mem_literal_data),testtext,MAXBUF)==0); 
    173174 
    174175    // cleanup 
     
    209210    // and parse it 
    210211 
     212    ops_memory_init(mem_literal_data,128); 
    211213    ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
    212214    rtn=ops_parse(pinfo); 
     
    217219     */ 
    218220 
    219     CU_ASSERT(memcmp(literal_data,in,MAXBUF)==0); 
     221    CU_ASSERT(memcmp(ops_memory_get_data(mem_literal_data),in,MAXBUF)==0); 
    220222 
    221223    // cleanup 
     
    339341    ops_encrypt_init(&pinfo->decrypt); 
    340342 
     343    ops_memory_init(mem_literal_data,0); 
    341344    rtn=ops_parse(pinfo); 
    342345    CU_ASSERT(rtn==1); 
     
    346349     */ 
    347350 
    348     CU_ASSERT(memcmp(literal_data,ldt_text, strlen(ldt_text))==0); 
     351    CU_ASSERT(memcmp(ops_memory_get_data(mem_literal_data),ldt_text, strlen(ldt_text))==0); 
    349352 
    350353    // cleanup 
     
    453456static void local_cleanup() 
    454457    { 
    455     if (literal_data) 
    456         { 
    457         free(literal_data); 
    458         literal_data=NULL; 
    459         } 
     458    //    ops_memory_init(mem_literal_data); 
    460459 
    461460    if (mdc_data) 
  • openpgpsdk/trunk/tests/test_rsa_decrypt.c

    r510 r511  
    7171 
    7272    case OPS_PTAG_CT_LITERAL_DATA_BODY: 
    73         /* 
    74         text=ops_mallocz(content->literal_data_body.length+1); 
    75         memcpy(text,content->literal_data_body.data,content->literal_data_body.length); 
    76         */ 
    7773        return callback_literal_data(content_,cbinfo); 
    7874                break; 
     
    239235    // Do the decryption 
    240236 
     237    ops_memory_init(mem_literal_data,0); 
    241238    rtn=ops_parse(pinfo); 
    242239    CU_ASSERT(rtn==1); 
     
    250247    // File contents should match 
    251248    testtext=create_testtext(filename); 
    252     CU_ASSERT(strlen(testtext)==sz_literal_data); 
    253     CU_ASSERT(memcmp(literal_data,testtext,sz_literal_data)==0); 
     249    CU_ASSERT(strlen(testtext)==ops_memory_get_length(mem_literal_data)); 
     250    CU_ASSERT(memcmp(ops_memory_get_data(mem_literal_data), 
     251                     testtext, 
     252                     ops_memory_get_length(mem_literal_data))==0); 
    254253    } 
    255254 
  • openpgpsdk/trunk/tests/test_rsa_encrypt.c

    r510 r511  
    11#include "CUnit/Basic.h" 
    2  
     2  
    33#include <openpgpsdk/types.h> 
    44#include "openpgpsdk/keyring.h" 
     
    1111 
    1212#include "tests.h" 
     13 
     14static int do_gpgtest=0; 
    1315 
    1416static char *filename_rsa_noarmour_nopassphrase_singlekey_old="enc_rsa_noarmour_np_singlekey_old.txt"; 
     
    8789int init_suite_rsa_encrypt(void) 
    8890    { 
     91    do_gpgtest=0; 
     92 
    8993    // Create RSA test files 
    9094 
     
    103107    } 
    104108 
     109int init_suite_rsa_encrypt_gpgtest(void) 
     110    { 
     111    init_suite_rsa_encrypt(); 
     112 
     113    do_gpgtest=1; 
     114 
     115    return 0; 
     116    } 
     117 
    105118int clean_suite_rsa_encrypt(void) 
    106119    { 
     
    113126    } 
    114127 
    115 static void test_rsa_decrypt(const char *encfile, const char*testtext) 
     128static int test_rsa_decrypt(const char *encfile, const char*testtext) 
    116129    { 
    117130    int fd=0; 
     
    135148    // Do the decryption 
    136149 
     150    ops_memory_init(mem_literal_data,0); 
    137151    rtn=ops_parse(pinfo); 
    138152    CU_ASSERT(rtn==1); 
     
    143157     
    144158    // File contents should match 
    145     CU_ASSERT(memcmp(literal_data,testtext,sz_literal_data)==0); 
     159 
     160    CU_ASSERT(memcmp(ops_memory_get_data(mem_literal_data),testtext,ops_memory_get_length(mem_literal_data))==0); 
     161 
     162    return rtn; 
    146163    } 
    147164 
     
    240257     */ 
    241258 
    242     // File contents should match - checking with OPS 
    243  
    244     testtext=create_testtext(filename); 
    245     test_rsa_decrypt(encrypted_file,testtext); 
    246  
    247     // File contents should match - check with GPG 
    248  
    249     char pp[MAXBUF]; 
    250     if (pub_key==alpha_pub_keydata) 
    251         pp[0]='\0'; 
    252     else if (pub_key==bravo_pub_keydata) 
    253         snprintf(pp,MAXBUF," --passphrase %s ", bravo_passphrase); 
    254     snprintf(decrypted_file,MAXBUF,"%s/decrypted_%s",dir,filename); 
    255     snprintf(cmd,MAXBUF,"gpg --decrypt --output=%s --quiet --homedir %s %s %s",decrypted_file, dir, pp, encrypted_file); 
    256     //    printf("cmd: %s\n", cmd); 
    257     rtn=system(cmd); 
    258     CU_ASSERT(rtn==0); 
    259     CU_ASSERT(file_compare(myfile,decrypted_file)==0); 
     259    if (!do_gpgtest) 
     260        { 
     261        // File contents should match - checking with OPS 
     262 
     263        testtext=create_testtext(filename); 
     264        CU_ASSERT(test_rsa_decrypt(encrypted_file,testtext)==1); 
     265        } 
     266 
     267    else 
     268        { 
     269        // File contents should match - check with GPG 
     270 
     271        char pp[MAXBUF]; 
     272        if (pub_key==alpha_pub_keydata) 
     273            pp[0]='\0'; 
     274        else if (pub_key==bravo_pub_keydata) 
     275            snprintf(pp,MAXBUF," --passphrase %s ", bravo_passphrase); 
     276        snprintf(decrypted_file,MAXBUF,"%s/decrypted_%s",dir,filename); 
     277        snprintf(cmd,MAXBUF,"gpg --decrypt --output=%s --quiet --homedir %s %s %s",decrypted_file, dir, pp, encrypted_file); 
     278        //    printf("cmd: %s\n", cmd); 
     279        rtn=system(cmd); 
     280        CU_ASSERT(rtn==0); 
     281        CU_ASSERT(file_compare(myfile,decrypted_file)==0); 
     282        } 
    260283    } 
    261284 
     
    267290    char decrypted_file[MAXBUF+1]; 
    268291    char *suffix= has_armour ? "asc" : "gpg"; 
     292    char *gpgtest = do_gpgtest ? "gpgtest_" : ""; 
    269293    int fd_in=0; 
    270294    int fd_out=0; 
     
    274298     * Read from test file and write plaintext to memory 
    275299     * in set of Literal Data packets 
    276      * \todo optimise using stacked writers 
    277300     */ 
    278301 
     
    286309        } 
    287310     
    288     snprintf(encrypted_file,MAXBUF,"%s/%s.%s",dir,filename,suffix); 
     311    snprintf(encrypted_file,MAXBUF,"%s/%s%s.%s",dir,gpgtest,filename,suffix); 
    289312    fd_out=open(encrypted_file,O_WRONLY | O_CREAT | O_EXCL, 0600); 
    290313    if(fd_out < 0) 
     
    295318     
    296319    /* 
    297      * This is how to do encryption 
     320     * This shows how to use encryption 
    298321     */ 
    299322 
     
    310333    // Do the writing 
    311334 
     335    const unsigned bufsz=16; 
    312336    for (;;) 
    313337        { 
    314             unsigned char buf[MAXBUF]; 
     338            unsigned char buf[bufsz]; 
    315339            int n=0; 
    316340 
     
    339363     */ 
    340364 
    341     // File contents should match - checking with OPS 
    342  
    343     char* testtext=NULL; 
    344     testtext=create_testtext(filename); 
    345     test_rsa_decrypt(encrypted_file,testtext); 
    346  
    347     // File contents should match - check with GPG 
    348  
    349     char pp[MAXBUF]; 
    350     if (pub_key==alpha_pub_keydata) 
    351         pp[0]='\0'; 
    352     else if (pub_key==bravo_pub_keydata) 
    353         snprintf(pp,MAXBUF," --passphrase %s ", bravo_passphrase); 
    354     snprintf(decrypted_file,MAXBUF,"%s/decrypted_%s",dir,filename); 
    355     snprintf(cmd,MAXBUF,"gpg --decrypt --output=%s --quiet --homedir %s %s %s",decrypted_file, dir, pp, encrypted_file); 
    356     //    printf("cmd: %s\n", cmd); 
    357     rtn=system(cmd); 
    358     CU_ASSERT(rtn==0); 
    359     CU_ASSERT(file_compare(myfile,decrypted_file)==0); 
     365    if (do_gpgtest) 
     366        { 
     367        // File contents should match - check with GPG 
     368         
     369        char pp[MAXBUF]; 
     370        if (pub_key==alpha_pub_keydata) 
     371            pp[0]='\0'; 
     372        else if (pub_key==bravo_pub_keydata) 
     373            snprintf(pp,MAXBUF," --passphrase %s ", bravo_passphrase); 
     374        snprintf(decrypted_file,MAXBUF,"%s/decrypted_%s",dir,filename); 
     375        snprintf(cmd,MAXBUF,"gpg --decrypt --output=%s --quiet --homedir %s %s %s",decrypted_file, dir, pp, encrypted_file); 
     376        //    printf("cmd: %s\n", cmd); 
     377        rtn=system(cmd); 
     378        CU_ASSERT(rtn==0); 
     379        CU_ASSERT(file_compare(myfile,decrypted_file)==0); 
     380        } 
     381    else 
     382        { 
     383        // File contents should match - checking with OPS 
     384         
     385        char* testtext=NULL; 
     386        testtext=create_testtext(filename); 
     387        test_rsa_decrypt(encrypted_file,testtext); 
     388        } 
    360389    } 
    361390 
     
    402431#endif /*TBD*/ 
    403432 
     433int add_tests(CU_pSuite suite) 
     434    { 
     435    // add tests to suite 
     436     
     437    if (NULL == CU_add_test(suite, "Unarmoured, single key, no passphrase", test_rsa_encrypt_noarmour_nopassphrase_singlekey)) 
     438            return 0; 
     439     
     440    if (NULL == CU_add_test(suite, "Unarmoured, single key, passphrase", test_rsa_encrypt_noarmour_passphrase_singlekey)) 
     441            return 0; 
     442     
     443#ifdef OLD 
     444    if (NULL == CU_add_test(suite, "Unarmoured, single key, no passphrase (OLD)", old_test_rsa_encrypt_noarmour_nopassphrase_singlekey)) 
     445            return 0; 
     446     
     447    if (NULL == CU_add_test(suite, "Unarmoured, single key, passphrase (OLD)", old_test_rsa_encrypt_noarmour_passphrase_singlekey)) 
     448            return 0; 
     449#endif 
     450     
     451#ifdef TBD 
     452    if (NULL == CU_add_test(suite, "Armoured, single key", test_rsa_encrypt_armour_singlekey)) 
     453            return 0; 
     454     
     455    if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_encrypt_armour_passphrase)) 
     456            return 0; 
     457#endif /*TBD*/ 
     458 
     459    return 1; 
     460    } 
     461     
    404462CU_pSuite suite_rsa_encrypt() 
    405 
     463   
    406464    CU_pSuite suite = NULL; 
    407465 
     
    410468            return NULL; 
    411469 
    412     // add tests to suite 
    413      
    414     if (NULL == CU_add_test(suite, "Unarmoured, single key, no passphrase", test_rsa_encrypt_noarmour_nopassphrase_singlekey)) 
     470    if (!add_tests(suite)) 
     471        return NULL; 
     472 
     473    return suite; 
     474    } 
     475 
     476CU_pSuite suite_rsa_encrypt_GPGtest() 
     477    { 
     478    CU_pSuite suite = NULL; 
     479 
     480    suite = CU_add_suite("RSA Encryption Suite (GPG interoperability)", init_suite_rsa_encrypt_gpgtest, clean_suite_rsa_encrypt); 
     481 
     482    if (!suite) 
    415483            return NULL; 
    416      
    417     if (NULL == CU_add_test(suite, "Unarmoured, single key, passphrase", test_rsa_encrypt_noarmour_passphrase_singlekey)) 
    418             return NULL; 
    419      
    420     if (NULL == CU_add_test(suite, "Unarmoured, single key, no passphrase (OLD)", old_test_rsa_encrypt_noarmour_nopassphrase_singlekey)) 
    421             return NULL; 
    422      
    423     if (NULL == CU_add_test(suite, "Unarmoured, single key, passphrase (OLD)", old_test_rsa_encrypt_noarmour_passphrase_singlekey)) 
    424             return NULL; 
    425      
    426 #ifdef TBD 
    427     if (NULL == CU_add_test(suite, "Armoured, single key", test_rsa_encrypt_armour_singlekey)) 
    428             return NULL; 
    429      
    430     if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_encrypt_armour_passphrase)) 
    431             return NULL; 
    432 #endif /*TBD*/ 
    433      
     484 
     485    if (!add_tests(suite)) 
     486        return NULL; 
     487 
    434488    return suite; 
    435 
    436  
     489    } 
     490 
     491// EOF 
  • openpgpsdk/trunk/tests/tests.c

    r510 r511  
    1717ops_keyring_t sec_keyring; 
    1818static char* no_passphrase=""; 
    19 unsigned char* literal_data=NULL; 
    20 size_t sz_literal_data=0; 
     19ops_memory_t* mem_literal_data=NULL; 
    2120 
    2221char *alpha_user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; 
     
    143142    { 
    144143 
     144    mem_literal_data=ops_memory_new(); 
    145145    setup_test_keys(); 
    146146 
     
    185185        } 
    186186#endif 
     187 
     188    if (NULL == suite_rsa_encrypt_GPGtest())  
     189        { 
     190        CU_cleanup_registry(); 
     191        return CU_get_error(); 
     192        } 
    187193 
    188194    // Run tests 
     
    201207    long int rnd=0; 
    202208    while (limit--)  
    203         { 
    204         rnd=random(); 
    205         snprintf(dir,MAXBUF,"./testdir.%ld",rnd); 
    206  
    207         // Try to create directory 
    208         if (!mkdir(dir,0700)) 
    209             { 
    210             // success 
    211             return 1; 
    212             } 
    213         else 
    214             { 
    215             fprintf (stderr,"Couldn't open dir: errno=%d\n", errno); 
    216             perror(NULL); 
    217             } 
    218         } 
    219     return 0; 
     209        { 
     210        rnd=random(); 
     211        snprintf(dir,MAXBUF,"./testdir.%ld",rnd); 
     212         
     213        // Try to create directory 
     214        if (!mkdir(dir,0700)) 
     215            { 
     216            // success 
     217            return 1; 
     218            } 
     219        else 
     220            { 
     221            fprintf (stderr,"Couldn't open dir: errno=%d\n", errno); 
     222            perror(NULL); 
     223            } 
     224        } 
     225    fprintf(stderr,"Too many temp dirs: please delete them\n"); 
     226    exit(1); 
    220227    } 
    221228 
    222229char* create_testtext(const char *text) 
    223230    { 
    224     const unsigned int repeats=2
     231    const unsigned int repeats=100
    225232    unsigned int i=0; 
    226233 
     
    394401        { 
    395402    case OPS_PTAG_CT_LITERAL_DATA_BODY: 
    396         sz_literal_data=content->literal_data_body.length; 
    397         literal_data=ops_mallocz(sz_literal_data+1); 
    398         memcpy(literal_data,content->literal_data_body.data,sz_literal_data); 
     403        ops_memory_add(mem_literal_data, 
     404                       content->literal_data_body.data, 
     405                       content->literal_data_body.length); 
    399406        break; 
    400407 
     
    470477void reset_vars() 
    471478    { 
    472     if (literal_data) 
    473         { 
    474         free (literal_data); 
    475         literal_data=NULL; 
    476         sz_literal_data=0; 
    477         } 
     479    ops_memory_init(mem_literal_data,0); 
     480 
    478481    if (decrypter) 
    479482        { 
  • openpgpsdk/trunk/tests/tests.h

    r510 r511  
    2222extern CU_pSuite suite_rsa_decrypt(); 
    2323extern CU_pSuite suite_rsa_encrypt(); 
     24extern CU_pSuite suite_rsa_encrypt_GPGtest(); 
    2425extern CU_pSuite suite_rsa_signature(); 
    2526extern CU_pSuite suite_rsa_verify(); 
     
    5253ops_keyring_t pub_keyring; 
    5354ops_keyring_t sec_keyring; 
    54 unsigned char* literal_data; 
    55 size_t sz_literal_data; 
     55ops_memory_t* mem_literal_data; 
    5656 
    5757// "Alpha" is the user who has NO passphrase on his key