Changeset 489

Show
Ignore:
Timestamp:
08/20/07 17:19:16
Author:
rachel
Message:

MDC hash now working.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/include/openpgpsdk/create.h

    r480 r489  
    101101                                                     ops_create_info_t *info); 
    102102 
    103 ops_boolean_t ops_write_mdc(const unsigned char *date, 
    104                                                         const unsigned int len, 
     103ops_boolean_t ops_write_mdc(const unsigned char *hashed, 
    105104                                                        ops_create_info_t *info); 
    106105 
     
    111110ops_boolean_t ops_write_pk_session_key(ops_create_info_t *info, 
    112111                                       ops_pk_session_key_t *pksk); 
     112void ops_calc_session_key_checksum(ops_pk_session_key_t *session_key, unsigned char *cs); 
     113void ops_calc_mdc_hash(const unsigned char* preamble, const size_t sz_preamble, const unsigned char* data, const unsigned int len, unsigned char *hashed); 
    113114 
    114115#endif 
  • openpgpsdk/trunk/src/advanced/adv_create.c

    r485 r489  
    753753    { return ops_stacked_write(src,length,errors,winfo); } 
    754754 
    755 static void create_unencoded_m_buf(ops_pk_session_key_t *session_key, unsigned char *m_buf) 
     755 
     756void ops_calc_session_key_checksum(ops_pk_session_key_t *session_key, unsigned char *cs) 
    756757    { 
    757758    int i=0; 
    758759    unsigned long checksum=0; 
     760 
     761    assert(session_key->symmetric_algorithm==OPS_SA_CAST5); 
     762    for (i=0; i<CAST_KEY_LENGTH; i++) 
     763        { 
     764        checksum+=session_key->key[i]; 
     765        } 
     766    checksum = checksum % 65536; 
     767 
     768    fprintf(stderr,"\nm buf checksum: "); 
     769    cs[0]=checksum >> 8; 
     770    fprintf(stderr," %2x",cs[0]); 
     771    cs[1]=checksum & 0xFF; 
     772    fprintf(stderr," %2x\n",cs[1]); 
     773    }     
     774 
     775static void create_unencoded_m_buf(ops_pk_session_key_t *session_key, unsigned char *m_buf) 
     776    { 
     777    int i=0; 
     778    //    unsigned long checksum=0; 
    759779 
    760780    // m_buf is the buffer which will be encoded in PKCS#1 block 
     
    764784 
    765785    m_buf[0]=session_key->symmetric_algorithm; 
     786 
    766787    assert(session_key->symmetric_algorithm==OPS_SA_CAST5); 
    767  
    768788    for (i=0; i<CAST_KEY_LENGTH; i++) 
    769789        { 
    770         checksum+=session_key->key[i]; 
    771790        m_buf[1+i]=session_key->key[i]; 
    772791        } 
    773     checksum = checksum % 65536; 
    774  
    775     m_buf[1+i++]=checksum >> 8; 
    776     m_buf[1+i++]=checksum & 0xFF; 
     792 
     793    ops_calc_session_key_checksum(session_key, m_buf+1+CAST_KEY_LENGTH); 
    777794    } 
    778795 
     
    851868    session_key->version=OPS_PKSK_V3; 
    852869    memcpy(session_key->key_id, key->key_id, sizeof session_key->key_id); 
     870 
    853871    /* 
    854872    fprintf(stderr,"Encrypting for RSA key id : "); 
     
    978996/* end of dummy code */ 
    979997 
    980 ops_boolean_t ops_write_mdc(const unsigned char* data, 
    981                             const unsigned int len, 
     998ops_boolean_t ops_write_mdc(const unsigned char *hashed, 
    982999                            ops_create_info_t* info) 
    9831000    { 
    984     // calculate the hash 
    985     ops_hash_t hash; 
    986     unsigned char hashed[SHA_DIGEST_LENGTH]; 
    987     unsigned char c[0]; 
    988  
    989     ops_hash_any(&hash, OPS_HASH_SHA1); 
    990     hash.init(&hash); 
    991     hash.add(&hash,data,len); // preamble + plaintext 
    992     c[0]=0xD3; 
    993     hash.add(&hash,&c[0],1);   // MDC packet tag 
    994     c[0]=0x14; 
    995     hash.add(&hash,&c[0],1);   // MDC packet len 
    996     hash.finish(&hash,&hashed[0]); 
    997  
    998     // and write it out 
     1001    // write it out 
    9991002    return ops_write_ptag(OPS_PTAG_CT_MDC, info) 
    10001003        && ops_write_length(OPS_SHA1_HASH_SIZE,info) 
    10011004        && ops_write(hashed, OPS_SHA1_HASH_SIZE, info); 
     1005    } 
     1006 
     1007void ops_calc_mdc_hash(const unsigned char* preamble, const size_t sz_preamble, const unsigned char* data, const unsigned int len, unsigned char *hashed) 
     1008    { 
     1009    ops_hash_t hash; 
     1010    //    unsigned char hashed[SHA_DIGEST_LENGTH]; 
     1011    unsigned char c[0]; 
     1012 
     1013    // init 
     1014    ops_hash_any(&hash, OPS_HASH_SHA1); 
     1015    hash.init(&hash); 
     1016 
     1017    // preamble 
     1018    hash.add(&hash,preamble,sz_preamble); 
     1019    // plaintext 
     1020    hash.add(&hash,data,len);  
     1021    // MDC packet tag 
     1022    c[0]=0xD3; 
     1023    hash.add(&hash,&c[0],1);    
     1024    // MDC packet len 
     1025    c[0]=0x14; 
     1026    hash.add(&hash,&c[0],1);    
     1027 
     1028    //finish 
     1029    hash.finish(&hash,hashed); 
    10021030    } 
    10031031 
     
    10071035                                   ops_create_info_t *info) 
    10081036    { 
     1037    unsigned char hashed[SHA_DIGEST_LENGTH]; 
    10091038    const size_t sz_mdc=1+1+SHA_DIGEST_LENGTH; 
    10101039    encrypted_arg_t *arg=ops_mallocz(sizeof *arg); 
     
    10321061 
    10331062    ops_setup_memory_write(&cinfo_mdc, &mem_mdc,sz_mdc); 
    1034     ops_write_mdc(data, len, cinfo_mdc); 
     1063 
     1064    ops_calc_mdc_hash(preamble,sz_preamble,data,len,&hashed[0]); 
     1065 
     1066    ops_write_mdc(hashed, cinfo_mdc); 
    10351067 
    10361068    // and write it out 
  • openpgpsdk/trunk/src/advanced/adv_crypto.c

    r485 r489  
    2323    assert(skey->public_key.algorithm == OPS_PKA_RSA); 
    2424 
    25     /* 
    2625    fprintf(stderr,"\nDECRYPTING\n"); 
    2726    fprintf(stderr,"encrypted data     : "); 
     
    2928        fprintf(stderr,"%2x ", encmpibuf[i]); 
    3029    fprintf(stderr,"\n"); 
    31     */ 
    3230 
    3331    n=ops_rsa_private_decrypt(mpibuf,encmpibuf,(BN_num_bits(encmpi)+7)/8, 
     
    3533    assert(n!=-1); 
    3634 
    37     /* 
    3835    fprintf(stderr,"decrypted encoded m buf     : "); 
    3936    for (i=0; i<16; i++) 
    4037        fprintf(stderr,"%2x ", mpibuf[i]); 
    4138    fprintf(stderr,"\n"); 
    42     */ 
    4339 
    4440    if(n <= 0) 
    4541        return -1; 
    4642 
    47     /* 
    48     printf(" decrypt=%d ",n); 
     43    printf(" decrypted=%d ",n); 
    4944    hexdump(mpibuf,n); 
    5045    printf("\n"); 
    51     */ 
    5246 
    5347    // Decode EME-PKCS1_V1_5 (RFC 2437). 
     
    7064        memcpy(buf,mpibuf+i,n-i); 
    7165 
    72     /* 
    73     printf("unencoded m buf:\n"); 
     66    printf("decoded m buf:\n"); 
    7467    int j; 
    7568    for (j=0; j<n-i; j++) 
    7669        printf("%2x ",buf[j]); 
    7770    printf("\n"); 
    78     */ 
    7971 
    8072    return n-i; 
  • openpgpsdk/trunk/src/advanced/adv_openssl_crypto.c

    r485 r489  
    168168    n=RSA_private_decrypt(length,in,out,orsa,RSA_NO_PADDING); 
    169169 
     170    printf("ops_rsa_private_decrypt: n=%d\n",n); 
     171 
    170172    char errbuf[1024]; 
    171173    errbuf[0]='\0'; 
  • openpgpsdk/trunk/src/advanced/adv_packet-parse.c

    r487 r489  
    1212#include <openpgpsdk/errors.h> 
    1313#include <openpgpsdk/readerwriter.h> 
     14#include "openpgpsdk/packet-show.h" 
     15 
    1416#include "parse_local.h" 
    1517 
     
    21792181    const ops_secret_key_t *secret; 
    21802182     
    2181     const size_t sz_unencoded_m_buf=CAST_KEY_LENGTH+1+2; 
     2183    // Can't rely on it being CAST5 
     2184    //    const size_t sz_unencoded_m_buf=CAST_KEY_LENGTH+1+2; 
     2185    const size_t sz_unencoded_m_buf=1024; 
    21822186    unsigned char unencoded_m_buf[sz_unencoded_m_buf]; 
    21832187     
     
    21982202        return 0; 
    21992203 
    2200     /* 
    22012204    int i; 
    22022205    int x=sizeof C.pk_session_key.key_id; 
    2203     printf("session key id: x=%d\n",x); 
     2206    printf("session key: public key id: x=%d\n",x); 
    22042207    for (i=0; i<x; i++) 
    22052208        printf("%2x ", C.pk_session_key.key_id[i]); 
    22062209    printf("\n"); 
    2207     */ 
    22082210 
    22092211    if(!limited_read(c,1,region,pinfo)) 
     
    22532255 
    22542256    if(n < 1) 
    2255         ERRP(pinfo,"decrypted message too short"); 
     2257        { 
     2258        ERRP(pinfo,"decrypted message too short"); 
     2259        return 0; 
     2260        } 
    22562261 
    22572262    // PKA 
    22582263    C.pk_session_key.symmetric_algorithm=unencoded_m_buf[0]; 
     2264 
     2265    if (C.pk_session_key.symmetric_algorithm!=OPS_SA_CAST5) 
     2266        //        && C.pk_session_key.symmetric_algorithm!=OPS_SA_AES_256) 
     2267        { 
     2268        fprintf(stderr,"*** Warning: should implement support for %s\n", 
     2269                ops_show_symmetric_algorithm(C.pk_session_key.symmetric_algorithm)); 
     2270        } 
     2271    //    assert(unencoded_m_buf[0]==OPS_SA_CAST5 || OPS_SA_AES_256); 
    22592272    assert(unencoded_m_buf[0]==OPS_SA_CAST5); 
    22602273    k=ops_key_size(C.pk_session_key.symmetric_algorithm); 
    22612274 
    22622275    if((unsigned)n != k+3) 
     2276        { 
    22632277        ERR2P(pinfo,"decrypted message wrong length (got %d expected %d)", 
    22642278              n,k+3); 
     2279        return 0; 
     2280        } 
    22652281     
    22662282    assert(k <= sizeof C.pk_session_key.key); 
     
    22682284    memcpy(C.pk_session_key.key,unencoded_m_buf+1,k); 
    22692285 
    2270     /* 
    22712286    printf("session key recovered (len=%d):\n",k); 
    22722287    unsigned int j; 
     
    22742289        printf("%2x ", C.pk_session_key.key[j]); 
    22752290    printf("\n"); 
    2276     */ 
    22772291 
    22782292    C.pk_session_key.checksum=unencoded_m_buf[k+1]+(unencoded_m_buf[k+2] << 8); 
    2279     /* 
    2280     printf("checksum: %2x %2x\n", unencoded_m_buf[k+1], unencoded_m_buf[k+2]); 
    2281     */ 
    2282  
    2283     // XXX: Check checksum! 
    2284  
     2293    printf("session key checksum: %2x %2x\n", unencoded_m_buf[k+1], unencoded_m_buf[k+2]); 
     2294 
     2295    // Check checksum 
     2296 
     2297    unsigned char cs[2]; 
     2298    ops_calc_session_key_checksum(&C.pk_session_key, &cs[0]); 
     2299    if (unencoded_m_buf[k+1]!=cs[0] || unencoded_m_buf[k+2]!=cs[1]) 
     2300        { 
     2301        ERR4P(pinfo, "Session key checksum wrong: expected %2x %2x, got %2x %2x", 
     2302              cs[0], cs[1], unencoded_m_buf[k+1], unencoded_m_buf[k+2]); 
     2303        return 0; 
     2304        } 
     2305 
     2306    // all is well 
    22852307    CBP(pinfo,OPS_PTAG_CT_PK_SESSION_KEY,&content); 
    22862308 
     
    23462368        size_t sz_plaintext=decrypted_region.length-sz_preamble-sz_mdc; 
    23472369 
    2348         //        unsigned char* preamble=buf; 
     2370        unsigned char* preamble=buf; 
    23492371        unsigned char* plaintext=buf+sz_preamble; 
    23502372        unsigned char* mdc=plaintext+sz_plaintext; 
    23512373        unsigned char* mdc_hash=mdc+2; 
    23522374     
     2375        ops_calc_mdc_hash(preamble,sz_preamble,plaintext,sz_plaintext,&hashed[0]); 
     2376        /* 
    23532377        unsigned char c[0]; 
    23542378 
     
    23602384         
    23612385        hash.finish(&hash,&hashed[0]); 
     2386        */ 
    23622387 
    23632388        if (memcmp(mdc_hash,hashed,OPS_SHA1_HASH_SIZE)) 
     
    24202445 
    24212446// XXX: make this static? 
    2422 int ops_decrypt_data(ops_content_tag_t tag,ops_region_t *region, 
     2447int ops_decrypt_se_data(ops_content_tag_t tag,ops_region_t *region, 
    24232448                     ops_parse_info_t *pinfo) 
    24242449    { 
     
    25322557    /* The content of an encrypted data packet is more OpenPGP packets 
    25332558       once decrypted, so recursively handle them */ 
    2534     return ops_decrypt_data(OPS_PTAG_CT_SE_DATA_BODY,region,pinfo); 
     2559    return ops_decrypt_se_data(OPS_PTAG_CT_SE_DATA_BODY,region,pinfo); 
    25352560    } 
    25362561 
  • openpgpsdk/trunk/tests/test_packet_types.c

    r488 r489  
    99#include "openpgpsdk/crypto.h" 
    1010#include "openpgpsdk/readerwriter.h" 
     11#include "openpgpsdk/random.h" 
    1112#include "../src/advanced/parse_local.h" 
     13 
    1214#include <openssl/aes.h> 
    1315#include <openssl/cast.h> 
     
    268270        ops_create_info_t *cinfo; 
    269271        ops_parse_info_t *pinfo; 
    270       ops_hash_t hash; 
     272    //        ops_hash_t hash; 
    271273        char* plaintext="Text to be hashed in test_ops_mdc"; 
    272274        int rtn=0; 
    273275 
     276    ops_crypt_t crypt; 
     277    unsigned char hashed[SHA_DIGEST_LENGTH]; 
     278    unsigned char* preamble; 
     279    ops_crypt_any(&crypt, OPS_SA_CAST5); 
     280    ops_encrypt_init(&crypt); 
     281 
     282    size_t sz_preamble=crypt.blocksize+2; 
     283    preamble=ops_mallocz(sz_preamble); 
     284    ops_random(preamble, crypt.blocksize); 
     285    preamble[crypt.blocksize]=preamble[crypt.blocksize-2]; 
     286    preamble[crypt.blocksize+1]=preamble[crypt.blocksize-1]; 
     287 
    274288        // Write packet to memory 
    275289        ops_setup_memory_write(&cinfo,&mem,strlen(plaintext)); 
    276         ops_write_mdc((unsigned char *)plaintext,strlen(plaintext),cinfo); 
     290    ops_calc_mdc_hash(preamble,sz_preamble,(unsigned char *)plaintext,strlen(plaintext),&hashed[0]); 
     291        ops_write_mdc(hashed,cinfo); 
    277292 
    278293        // Read back and verify contents 
     
    285300        // can verify it's been written correctly. 
    286301 
     302#ifdef TODO 
    287303    int x; 
    288304    unsigned char hashed[SHA_DIGEST_LENGTH]; 
     
    302318    if (mdc_data) 
    303319        CU_ASSERT(memcmp(mdc_data, hashed, OPS_SHA1_HASH_SIZE)==0); 
     320#endif 
    304321 
    305322        // clean up 
  • openpgpsdk/trunk/tests/test_rsa_decrypt.c

    r488 r489  
    2727static char *current_passphrase=NULL; 
    2828 
    29 static char* text; 
     29//static char* text; 
    3030 
    3131/* 
     
    294294    // File contents should match 
    295295    create_testtext(filename,&testtext[0],MAXBUF); 
    296     CU_ASSERT(strcmp(text,testtext)==0); 
     296    CU_ASSERT(memcmp(literal_data,testtext,sz_literal_data)==0); 
    297297    } 
    298298 
     
    348348            return NULL; 
    349349     
     350#ifdef TODO 
    350351    if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_decrypt_armour_nopassphrase)) 
    351352            return NULL; 
     
    356357    if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_decrypt_armour_passphrase)) 
    357358            return NULL; 
    358      
     359#endif     
    359360    return suite; 
    360361}