Changeset 473

Show
Ignore:
Timestamp:
05/13/07 18:44:13
Author:
rachel
Message:

Implemented write of Symmetric-Key Encrypted Session Key Packet
Implemented write of Literal Data packet
These packet types are needed for encryption.

Files:

Legend:

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

    r316 r473  
    1010#include "memory.h" 
    1111#include "errors.h" 
     12#include "keyring.h" 
    1213 
    1314typedef struct ops_writer_info ops_writer_info_t; 
     
    8889                                          ops_create_info_t *info); 
    8990 
     91ops_pk_session_key_t *ops_create_pk_session_key(const ops_key_data_t *key); 
     92 
     93void ops_create_m_buf(ops_pk_session_key_t *session_key, unsigned char *buf); 
     94 
     95ops_boolean_t ops_write_literal_data(const unsigned char *data,  
     96                                     const int maxlen,  
     97                                     const ops_literal_data_type_t type, 
     98                                     ops_create_info_t *info); 
    9099#endif 
  • openpgpsdk/trunk/include/openpgpsdk/crypto.h

    r470 r473  
    3838typedef void ops_crypt_finish_t(ops_crypt_t *crypt); 
    3939 
    40 /** _ops_decrypt_t */ 
     40/** _ops_crypt_t */ 
    4141struct _ops_crypt_t 
    4242    { 
     
    5454    unsigned char siv[OPS_MAX_BLOCK_SIZE]; /* Needed for weird v3 resync */ 
    5555    unsigned char key[OPS_MAX_KEY_SIZE]; 
    56     size_t num; 
     56    size_t num; /* Count of characters encrypted so far */ 
    5757    void *data; 
    5858    }; 
     
    9393void ops_crypt_any(ops_crypt_t *decrypt,ops_symmetric_algorithm_t alg); 
    9494void ops_decrypt_init(ops_crypt_t *decrypt); 
     95void ops_encrypt_init(ops_crypt_t *encrypt); 
    9596size_t ops_decrypt(ops_crypt_t *decrypt,void *out,const void *in, 
    9697                   size_t count); 
     
    116117struct ops_key_data; 
    117118void ops_writer_push_encrypt(ops_create_info_t *info, 
     119                             //            ops_crypt_t *encrypt, 
    118120                             const struct ops_key_data *key); 
    119121 
  • openpgpsdk/trunk/include/openpgpsdk/packet.h

    r417 r473  
    737737    OPS_LDT_LOCAL='l', 
    738738    OPS_LDT_LOCAL2='1' 
    739     } literal_data_type_t; 
     739    } ops_literal_data_type_t; 
    740740 
    741741/** ops_literal_data_header_t */ 
    742742typedef struct 
    743743    { 
    744     literal_data_type_t               format; 
     744    ops_literal_data_type_t           format; 
    745745    char                        filename[256]; 
    746746    time_t                      modification_time; 
  • openpgpsdk/trunk/src/advanced/adv_create.c

    r470 r473  
    749749    { return ops_stacked_write(src,length,errors,winfo); } 
    750750 
     751void ops_create_m_buf(ops_pk_session_key_t *session_key, unsigned char *buf) 
     752    { 
     753    int i=0; 
     754    unsigned int checksum=0; 
     755 
     756    // as defined in RFC Section 5.1 "Public-Key Encrypted Session Key Packet" 
     757 
     758    buf[0]=session_key->symmetric_algorithm; 
     759 
     760    // \todo parameterise key length 
     761    for (i=0; i<256/8; i++) 
     762        { 
     763        checksum+=session_key->key[i]; 
     764        buf[1+i]=session_key->key[i]; 
     765        } 
     766    checksum = checksum % 65536; 
     767 
     768    buf[i++]=checksum >> 8; 
     769    buf[i++]=checksum & 0xFF; 
     770    } 
     771 
    751772ops_pk_session_key_t *ops_create_pk_session_key(const ops_key_data_t *key) 
    752773    { 
     774    unsigned char buf[256/8+1+2]; 
    753775    ops_pk_session_key_t *session_key=ops_mallocz(sizeof *session_key); 
    754776 
     
    762784    ops_random(session_key->key, 256/8); 
    763785 
    764     if(!ops_encrypt_mpi(session_key->key, 256/8, &key->key.pkey, &session_key->parameters)) 
     786    ops_create_m_buf(session_key, buf); 
     787    /* 
     788    // create buf to be encoded 
     789    buf[0]=session_key->symmetric_algorithm; 
     790    for (i=0; i<256/8; i++) 
     791        { 
     792        checksum+=session_key->key[i]; 
     793        buf[1+i]=session_key->key[i]; 
     794        } 
     795    checksum = checksum % 65536; 
     796    buf[i++]=checksum >> 8; 
     797    buf[i++]=checksum & 0xFF; 
     798    */ 
     799 
     800    // and encode it 
     801    if(!ops_encrypt_mpi(buf, (256/8+1+2), &key->key.pkey, &session_key->parameters)) 
    765802        return NULL; 
    766803 
     
    781818    { 
    782819    assert(pksk->algorithm == OPS_PKA_RSA); 
     820 
    783821    return ops_write_ptag(OPS_PTAG_CT_PK_SESSION_KEY, info) 
    784822        && ops_write_length(1 + 8 + 1 + BN_num_bytes(pksk->parameters.rsa.encrypted_m) + 2, info) 
     
    787825        && ops_write_scalar(pksk->algorithm, 1, info) 
    788826        && ops_write_mpi(pksk->parameters.rsa.encrypted_m, info) 
    789         /* XXX: write the checksum! */ 
    790827        && ops_write_scalar(0, 2, info); 
    791828    } 
    792829 
    793830static ops_boolean_t encrypted_writer(const unsigned char *src ATTRIBUTE_UNUSED, 
    794                                       unsigned length ATTRIBUTE_UNUSED
     831                                      unsigned length
    795832                                      ops_error_t **errors ATTRIBUTE_UNUSED, 
    796833                                      ops_writer_info_t *winfo ATTRIBUTE_UNUSED) 
    797834    { 
     835    encrypted_arg_t *arg=ops_writer_get_arg(winfo); 
     836 
     837#define BUFSZ 1024 // arbitrary number 
     838    unsigned char buf[BUFSZ]; 
     839    unsigned char encbuf[BUFSZ]; 
     840    unsigned remaining=length; 
     841    while (remaining) 
     842        { 
     843        unsigned len = remaining < BUFSZ ? remaining : BUFSZ; 
     844        memcpy(buf,src,len); 
     845        unsigned done = ops_encrypt(arg->encrypter, 
     846                                    encbuf, 
     847                                    buf, 
     848                                    len); 
     849        assert(done==len); 
     850        if (!ops_stacked_write(encbuf,len,errors,winfo)) 
     851            return ops_false; 
     852        remaining-=done; 
     853        } 
     854 
     855    return ops_true; 
     856    } 
     857 
     858static ops_boolean_t encrypted_finaliser(ops_error_t **errors ATTRIBUTE_UNUSED, 
     859                                         ops_writer_info_t *winfo ATTRIBUTE_UNUSED) 
     860    { 
     861     
    798862    /* \todo */ 
    799863    assert(0); 
     
    802866    } 
    803867 
    804 static ops_boolean_t encrypted_finaliser(ops_error_t **errors ATTRIBUTE_UNUSED, 
    805                                          ops_writer_info_t *winfo ATTRIBUTE_UNUSED) 
     868void encrypted_destroyer (ops_writer_info_t *winfo ATTRIBUTE_UNUSED) 
     869      
    806870    { 
    807871    /* \todo */ 
    808872    assert(0); 
    809  
    810     return ops_false; 
    811     } 
    812  
    813 void encrypted_destroyer (ops_writer_info_t *winfo ATTRIBUTE_UNUSED) 
    814       
    815     { 
    816     /* \todo */ 
    817     assert(0); 
    818873    } 
    819874 
    820875/* end of dummy code */ 
    821876 
     877ops_boolean_t ops_write_se_ip_data(ops_create_info_t *info,ops_crypt_t *crypt) 
     878    { 
     879    encrypted_arg_t *arg=ops_mallocz(sizeof *arg); 
     880 
     881#define SE_IP_DATA_VERSION 1 //\todo move this 
     882    if (!ops_write_ptag(OPS_PTAG_CT_SE_IP_DATA,info)) 
     883        return 0; 
     884    if (!ops_write_scalar(SE_IP_DATA_VERSION,1,info)) 
     885        return 0; 
     886 
     887    // need any equivalent of base_init? 
     888    //    encrypt.set_iv(&encrypt,session_key->symmetric_algorithm.iv); 
     889    //    encrypt.set_key(&encrypt,mykey); // should be sym_alg.key? 
     890     
     891    arg->encrypter=crypt; 
     892 
     893    ops_writer_push(info,encrypted_writer,encrypted_finaliser, 
     894                    encrypted_destroyer,arg); 
     895 
     896    return 1; 
     897    } 
     898 
    822899void ops_writer_push_encrypt(ops_create_info_t *info, 
     900                             //                             ops_crypt_t *encrypt, 
    823901                             const ops_key_data_t *key) 
    824902    { 
     903 
     904    // has ops_key_data_t * key 
     905    // needs ops_crypt_t * crypt 
    825906    ops_pk_session_key_t *session_key; 
    826     encrypted_arg_t *arg=ops_mallocz(sizeof *arg); 
     907    ops_crypt_t crypt; 
     908    //  unsigned char mykey[OPS_MAX_KEY_SIZE+OPS_MAX_HASH_SIZE]; 
    827909 
    828910    session_key=ops_create_pk_session_key(key); 
    829911    ops_write_pk_session_key(info,session_key); 
    830912 
    831     ops_write_ptag(OPS_PTAG_CT_SE_DATA,info); 
    832     ops_writer_push(info,encrypted_writer,encrypted_finaliser, 
    833                     encrypted_destroyer,arg); 
    834     } 
     913    ops_crypt_any(&crypt, session_key->symmetric_algorithm); 
     914    ops_encrypt_init(&crypt); 
     915    ops_write_se_ip_data(info,&crypt); 
     916 
     917    } 
     918 
     919ops_boolean_t ops_write_literal_data(const unsigned char *data,  
     920                                     const int maxlen,  
     921                                     const ops_literal_data_type_t type, 
     922                                     ops_create_info_t *info) 
     923    { 
     924    // \todo add filename  
     925    // \todo add date 
     926    // \todo do we need to check text data for <cr><lf> line endings ? 
     927    return ops_write_ptag(OPS_PTAG_CT_LITERAL_DATA, info) 
     928        && ops_write_length(1+1+4+maxlen,info) 
     929        && ops_write_scalar(type, 1, info) 
     930        && ops_write_scalar(0, 1, info) 
     931        && ops_write_scalar(0, 4, info) 
     932        && ops_write(data, maxlen, info); 
     933    } 
  • openpgpsdk/trunk/src/advanced/adv_crypto.c

    r470 r473  
    6565    unsigned i; 
    6666 
     67    // implementation of EME-PKCS1-v1_5-ENCODE, as defined in OpenPGP RFC 
     68     
    6769    assert(pkey->algorithm == OPS_PKA_RSA); 
    6870 
    6971    n=BN_num_bytes(pkey->key.rsa.n); 
     72 
     73    // these two bytes defined by RFC 
    7074    padded[0]=0; 
    7175    padded[1]=2; 
    72     // add non-zero random bytes 
     76    // add non-zero random bytes of length k - mLen -3 
    7377    for(i=2 ; i < n-buflen-1 ; ++i) 
    7478        do 
    7579            ops_random(padded+i, 1); 
    7680        while(padded[i] == 0); 
     81 
     82    assert (i >= 8+2); 
     83 
    7784    padded[i++]=0; 
     85 
    7886    memcpy(padded+i, buf, buflen); 
    7987     
  • openpgpsdk/trunk/src/advanced/adv_packet-parse.c

    r470 r473  
    55#include <openpgpsdk/packet.h> 
    66#include <openpgpsdk/packet-parse.h> 
     7#include <openpgpsdk/keyring.h> 
    78#include <openpgpsdk/util.h> 
    89#include <openpgpsdk/compress.h> 
     
    24582459 
    24592460    case OPS_PTAG_CT_PK_SESSION_KEY: 
     2461        printf("reading pk_session_key: length=%d\n",region.length); 
    24602462        r=parse_pk_session_key(&region,pinfo); 
    24612463        break; 
  • openpgpsdk/trunk/src/advanced/adv_symmetric.c

    r470 r473  
    1111 
    1212#include <openpgpsdk/final.h> 
     13 
     14// \todo there's also a encrypted_arg_t in adv_create.c  
     15// which is used for *encrypting* whereas this is used 
     16// for *decrypting* 
    1317 
    1418typedef struct 
     
    326330    } 
    327331 
     332void ops_encrypt_init(ops_crypt_t * encrypt) 
     333    { 
     334    // \todo does there need to be both a ops_encrypt_init and a ops_decrypt_init? 
     335    encrypt->base_init(encrypt); 
     336    // needed?    decrypt->block_encrypt(decrypt,decrypt->siv,decrypt->iv); 
     337    // needed?    memcpy(decrypt->civ,decrypt->siv,decrypt->blocksize); 
     338    encrypt->num=0; 
     339    } 
     340 
    328341void ops_decrypt_init(ops_crypt_t *decrypt) 
    329342    { 
     
    360373    } 
    361374 
    362 size_t ops_encrypt(ops_crypt_t *decrypt,void *out_,const void *in_, 
     375size_t ops_encrypt(ops_crypt_t *encrypt,void *out_,const void *in_, 
    363376                   size_t count) 
    364377    { 
     
    371384    while(count-- > 0) 
    372385        { 
    373         if(decrypt->num == decrypt->blocksize) 
     386        if(encrypt->num == encrypt->blocksize) 
    374387            { 
    375             memcpy(decrypt->siv,decrypt->civ,decrypt->blocksize); 
    376             decrypt->block_encrypt(decrypt,decrypt->civ,decrypt->civ); 
    377             decrypt->num=0; 
     388            memcpy(encrypt->siv,encrypt->civ,encrypt->blocksize); 
     389            encrypt->block_encrypt(encrypt,encrypt->civ,encrypt->civ); 
     390            encrypt->num=0; 
    378391            } 
    379         decrypt->civ[decrypt->num]=*out++=decrypt->civ[decrypt->num]^*in++; 
    380         ++decrypt->num; 
     392        encrypt->civ[encrypt->num]=*out++=encrypt->civ[encrypt->num]^*in++; 
     393        ++encrypt->num; 
    381394        } 
    382395 
  • openpgpsdk/trunk/tests/Makefile.template

    r471 r473  
    1212LIBS=$(LIBDEPS) %CRYPTO_LIBS% %ZLIB% $(DM_LIB) $(CUNIT_LIB) 
    1313 
    14 TESTSRC=tests.c test_crypt_mpi.c test_rsa_decrypt.c test_rsa_encrypt.c 
     14TESTSRC=tests.c \ 
     15                test_packet_types.c \ 
     16                test_crypt_mpi.c test_rsa_decrypt.c test_rsa_encrypt.c 
    1517TESTOBJ=$(TESTSRC:.c=.o) 
    1618 
  • openpgpsdk/trunk/tests/test_crypt_mpi.c

    r472 r473  
    8585void test_crypt_mpi(void) 
    8686    { 
    87 #define BUFSZ 1024 
    88     unsigned char buf[]="hello world"; 
    89     unsigned char out[BUFSZ]; 
     87#define BSZ (256/8+1+2) 
     88 
     89    unsigned char in[BSZ]; 
     90    unsigned char out[BSZ]; 
    9091 
    9192    ops_boolean_t rtn; 
    9293     
    9394    ops_pk_session_key_t *session_key=ops_create_pk_session_key(pubkey); 
     95 
     96    // recreate what was encrypted 
     97    ops_create_m_buf(session_key, in); 
     98 
     99    //    CU_ASSERT(session_key); 
     100 
    94101    // the encrypted_mpi is now in session_key->parameters.rsa.encrypted_m 
    95102 
    96103    // decrypt it 
    97     //    BIGNUM *enc_m; 
    98     rtn=ops_decrypt_mpi(out,BUFSZ, session_key->parameters.rsa.encrypted_m, &seckey->key.skey); 
     104    rtn=ops_decrypt_mpi(out,BSZ, session_key->parameters.rsa.encrypted_m, &seckey->key.skey); 
     105 
     106    // [0] is the symmetric algorithm 
     107    // [body] is the session key 
     108    // [last two] is the checksum 
    99109 
    100110    // is it the same? 
    101     CU_ASSERT(strncmp((char *)buf,(char *)out,sizeof(buf))==0); 
     111    CU_ASSERT(strncmp((char *)in,(char *)out,sizeof(in))==0); 
    102112    } 
    103113 
  • openpgpsdk/trunk/tests/test_rsa_encrypt.c

    r472 r473  
    1111#include "tests.h" 
    1212 
    13 /*  
    14 These include files are needed by callback. 
    15 To be removed when callback gets added to main body of code 
    16 #include "../src/advanced/parse_local.h" 
    17 #include "../src/advanced/keyring_local.h" 
    18 */ 
    19  
    2013#define MAXBUF 128 
    2114static char pub_keyring_name[MAXBUF+1]; 
     
    2316static ops_keyring_t pub_keyring; 
    2417static char *filename_rsa_noarmour_singlekey="rsa_noarmour_singlekey.txt"; 
    25 /* 
    26 static char *filename_rsa_armour_nopassphrase="rsa_armour_nopassphrase.txt"; 
    27 static char *filename_rsa_noarmour_passphrase="rsa_noarmour_passphrase.txt"; 
    28 static char *filename_rsa_armour_passphrase="rsa_armour_passphrase.txt"; 
    29 static char *nopassphrase=""; 
    30 static char *passphrase="hello"; 
    31 static char *current_passphrase=NULL; 
    32 */ 
    33  
    34 //static char* text; 
    35  
    36 static char *create_testtext(const char *filename) 
    37     { 
    38     static char buffer[MAXBUF+1]; 
    39     snprintf(buffer,MAXBUF,"Hello world : %s/%s\n", dir, filename); 
    40     return &buffer[0]; 
    41     } 
    4218 
    4319static int create_testfile(const char *name) 
     
    5127        return 0; 
    5228 
    53     snprintf(buffer,MAXBUF,create_testtext(name)); 
     29    create_testtext(name,&buffer[0],MAXBUF); 
    5430    write(fd,buffer,strlen(buffer)); 
    5531    close(fd); 
     
    367343    } 
    368344     
    369 //    ops_parse(pinfo); 
    370  
    371345    // Tidy up 
    372      /* 
    373     if (has_armour) 
    374         ops_writer_pop_armour(cinfo); 
    375         */ 
    376  
    377 close(fd_in); 
    378 close(fd_out); 
    379  
    380 //    close(fd); 
    381      
     346 
     347    close(fd_in); 
     348    close(fd_out); 
     349 
    382350     // File contents should match 
    383 /* 
    384  int n=0; 
    385  char  
    386  fd_out=open(encfile,O_RDONLY); 
    387  if (fd_out < 0) 
    388      { 
    389      perror(encfile); 
    390      exit(2); 
    391      } 
    392 */ 
    393  char *text; 
    394    CU_ASSERT(strcmp(text,create_testtext(filename))==0); 
     351    char *text; 
     352    char buffer[MAXBUF+1]; 
     353    create_testtext(filename,&buffer[0],MAXBUF); 
     354    CU_ASSERT(strcmp(text,buffer)==0); 
    395355    } 
    396356 
     
    404364    } 
    405365 
    406 /* 
     366#ifdef TBD 
    407367void test_rsa_encrypt_armour(void) 
    408368    { 
     
    424384    test_rsa_encrypt(armour,passphrase,filename_rsa_armour_passphrase); 
    425385    } 
    426 */ 
     386#endif /*TBD*/ 
    427387 
    428388CU_pSuite suite_rsa_encrypt() 
     
    434394            return NULL; 
    435395 
     396#ifdef TBD 
    436397    // add tests to suite 
    437398     
     
    439400            return NULL; 
    440401     
    441     /* 
    442402    if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_encrypt_armour_nopassphrase)) 
    443403            return NULL; 
     
    448408    if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_encrypt_armour_passphrase)) 
    449409            return NULL; 
    450            */ 
     410#endif /*TBD*/ 
    451411     
    452412    return suite; 
  • openpgpsdk/trunk/tests/tests.c

    r471 r473  
    99#include "tests.h" 
    1010 
     11extern CU_pSuite suite_packet_types(); 
    1112extern CU_pSuite suite_crypt_mpi(); 
    1213extern CU_pSuite suite_rsa_decrypt(); 
     
    2021    if (CUE_SUCCESS != CU_initialize_registry()) 
    2122        return CU_get_error(); 
     23 
     24    if (NULL == suite_packet_types()) 
     25        { 
     26        CU_cleanup_registry(); 
     27        return CU_get_error(); 
     28        } 
    2229 
    2330    if (NULL == suite_crypt_mpi()) 
     
    3845        return CU_get_error(); 
    3946        } 
    40      
     47 
    4148    // Run tests 
    4249    CU_basic_set_mode(CU_BRM_VERBOSE); 
     
    7077    } 
    7178 
     79void create_testtext(const char *text, char *buf, const int maxlen) 
     80    { 
     81    buf[maxlen]='\0'; 
     82    snprintf(buf,maxlen,"%s : Test Text\n", text); 
     83    } 
     84 
     85 
  • openpgpsdk/trunk/tests/tests.h

    r471 r473  
    1313int mktmpdir(); 
    1414extern char dir[]; 
     15void create_testtext(const char *text, char *buf, const int maxlen); 
    1516#define MAXBUF 128 
    1617