Index: openpgpsdk/trunk/include/openpgpsdk/crypto.h =================================================================== --- openpgpsdk/trunk/include/openpgpsdk/crypto.h (revision 482) +++ openpgpsdk/trunk/include/openpgpsdk/crypto.h (revision 485) @@ -114,5 +114,5 @@ void ops_reader_pop_hash(ops_parse_info_t *pinfo); -int ops_decrypt_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi, +int ops_decrypt_and_unencode_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi, const ops_secret_key_t *skey); ops_boolean_t ops_encrypt_mpi(const unsigned char *buf, size_t buflen, Index: openpgpsdk/trunk/include/openpgpsdk/std_print.h =================================================================== --- openpgpsdk/trunk/include/openpgpsdk/std_print.h (revision 484) +++ openpgpsdk/trunk/include/openpgpsdk/std_print.h (revision 485) @@ -9,4 +9,6 @@ #include "openpgpsdk/keyring.h" +void print_bn( const char *name, + const BIGNUM *bn); void ops_print_pk_session_key(ops_content_tag_t tag, const ops_pk_session_key_t *key); Index: openpgpsdk/trunk/src/advanced/adv_create.c =================================================================== --- openpgpsdk/trunk/src/advanced/adv_create.c (revision 484) +++ openpgpsdk/trunk/src/advanced/adv_create.c (revision 485) @@ -1,4 +1,6 @@ /** \file */ + +#include #include @@ -751,28 +753,97 @@ { return ops_stacked_write(src,length,errors,winfo); } -void ops_create_m_buf(ops_pk_session_key_t *session_key, unsigned char *buf) +static void create_unencoded_m_buf(ops_pk_session_key_t *session_key, unsigned char *m_buf) { int i=0; - unsigned int checksum=0; - + unsigned long checksum=0; + + // m_buf is the buffer which will be encoded in PKCS#1 block + // encoding to form the "m" value used in the + // Public Key Encrypted Session Key Packet // as defined in RFC Section 5.1 "Public-Key Encrypted Session Key Packet" - buf[0]=session_key->symmetric_algorithm; - - // \todo parameterise key length - for (i=0; i<256/8; i++) + m_buf[0]=session_key->symmetric_algorithm; + assert(session_key->symmetric_algorithm==OPS_SA_CAST5); + + for (i=0; ikey[i]; - buf[1+i]=session_key->key[i]; + m_buf[1+i]=session_key->key[i]; } checksum = checksum % 65536; - buf[i++]=checksum >> 8; - buf[i++]=checksum & 0xFF; + m_buf[1+i++]=checksum >> 8; + m_buf[1+i++]=checksum & 0xFF; + } + +ops_boolean_t encode_m_buf(const unsigned char *M, size_t mLen, + const ops_public_key_t *pkey, + unsigned char* EM +) + { + //unsigned char encmpibuf[8192]; + // unsigned char EM[8192]; + unsigned int k; + unsigned i; + + // implementation of EME-PKCS1-v1_5-ENCODE, as defined in OpenPGP RFC + + assert(pkey->algorithm == OPS_PKA_RSA); + + k=BN_num_bytes(pkey->key.rsa.n); + assert(mLen <= k-11); + if (mLen > k-11) + { + fprintf(stderr,"message too long\n"); + return ops_false; + } + + // these two bytes defined by RFC + EM[0]=0x00; + EM[1]=0x02; + + // add non-zero random bytes of length k - mLen -3 + for(i=2 ; i < k-mLen-1 ; ++i) + do + ops_random(EM+i, 1); + while(EM[i] == 0); + + assert (i >= 8+2); + + EM[i++]=0; + + memcpy(EM+i, M, mLen); + + + /* + // int i=0; + fprintf(stderr,"Encoded Message: \n"); + for (i=0; ikey.rsa.n); + unsigned char encoded_m_buf[sz_encoded_m_buf]; + ops_pk_session_key_t *session_key=ops_mallocz(sizeof *session_key); @@ -780,15 +851,40 @@ session_key->version=OPS_PKSK_V3; memcpy(session_key->key_id, key->key_id, sizeof session_key->key_id); + /* + fprintf(stderr,"Encrypting for RSA key id : "); + unsigned int i=0; + for (i=0; ikey_id; i++) + fprintf(stderr,"%2x ", key->key_id[i]); + fprintf(stderr,"\n"); + */ assert(key->key.pkey.algorithm == OPS_PKA_RSA); session_key->algorithm=key->key.pkey.algorithm; + /* session_key->symmetric_algorithm=OPS_SA_AES_256; ops_random(session_key->key, 256/8); - - ops_create_m_buf(session_key, buf); - - // and encode it - if(!ops_encrypt_mpi(buf, (256/8+1+2), &key->key.pkey, &session_key->parameters)) - return NULL; + */ + session_key->symmetric_algorithm=OPS_SA_CAST5; + + ops_random(session_key->key, CAST_KEY_LENGTH); + /* + fprintf(stderr,"CAST5 session key created (len=%d):\n ", CAST_KEY_LENGTH); + for (i=0; ikey[i]); + fprintf(stderr,"\n"); + */ + + create_unencoded_m_buf(session_key, &unencoded_m_buf[0]); + /* + printf("unencoded m buf:\n"); + for (i=0; iparameters)) + return NULL; return session_key; @@ -842,4 +938,17 @@ assert(done==len); + + /* + fprintf(stderr,"WRITING:\nunencrypted: "); + int i=0; + for (i=0; i<16; i++) + fprintf(stderr,"%2x ", buf[i]); + fprintf(stderr,"\n"); + fprintf(stderr,"encrypted: "); + for (i=0; i<16; i++) + fprintf(stderr,"%2x ", encbuf[i]); + fprintf(stderr,"\n"); + */ + if (!ops_stacked_write(encbuf,len,errors,winfo)) return ops_false; @@ -930,4 +1039,8 @@ ops_writer_push(info,encrypted_writer,encrypted_finaliser, encrypted_destroyer,arg); + + /* + fprintf(stderr,"writing %ld + %d + %ld\n", sz_preamble, len, ops_memory_get_length(mem_mdc)); + */ if (!ops_write(preamble, sz_preamble,info) @@ -997,5 +1110,6 @@ int done=ops_encrypt_se(&crypt_info, encrypted, data, len); - printf("len=%d, done: %d\n", len, done); + assert(done==len); + // printf("len=%d, done: %d\n", len, done); return ops_write_ptag(OPS_PTAG_CT_SE_DATA, info) Index: openpgpsdk/trunk/src/advanced/adv_crypto.c =================================================================== --- openpgpsdk/trunk/src/advanced/adv_crypto.c (revision 473) +++ openpgpsdk/trunk/src/advanced/adv_crypto.c (revision 485) @@ -7,5 +7,5 @@ #include -int ops_decrypt_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi, +int ops_decrypt_and_unencode_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi, const ops_secret_key_t *skey) { @@ -23,6 +23,22 @@ assert(skey->public_key.algorithm == OPS_PKA_RSA); + /* + fprintf(stderr,"\nDECRYPTING\n"); + fprintf(stderr,"encrypted data : "); + for (i=0; i<16; i++) + fprintf(stderr,"%2x ", encmpibuf[i]); + fprintf(stderr,"\n"); + */ + n=ops_rsa_private_decrypt(mpibuf,encmpibuf,(BN_num_bits(encmpi)+7)/8, &skey->key.rsa,&skey->public_key.key.rsa); + assert(n!=-1); + + /* + fprintf(stderr,"decrypted encoded m buf : "); + for (i=0; i<16; i++) + fprintf(stderr,"%2x ", mpibuf[i]); + fprintf(stderr,"\n"); + */ if(n <= 0) @@ -38,29 +54,43 @@ if(mpibuf[0] != 0 || mpibuf[1] != 2) - return ops_false; + return ops_false; // Skip the random bytes. for(i=2 ; i < n && mpibuf[i] ; ++i) - ; + ; if(i == n || i < 10) - return ops_false; + return ops_false; // Skip the zero ++i; + // this is the unencoded m buf if((unsigned)(n-i) <= buflen) - memcpy(buf,mpibuf+i,n-i); + memcpy(buf,mpibuf+i,n-i); + + /* + printf("unencoded m buf:\n"); + int j; + for (j=0; jkey.rsa.n)); + unsigned char encmpibuf[8192]; - unsigned char padded[8192]; - int n; + int n=0; +#ifdef XXX + unsigned char EM[8192]; + int k; unsigned i; @@ -69,22 +99,45 @@ assert(pkey->algorithm == OPS_PKA_RSA); - n=BN_num_bytes(pkey->key.rsa.n); + k=BN_num_bytes(pkey->key.rsa.n); + /* + printf("k=%d (length in octets of key modulus)\n",k); + printf("mLen=%d\n",mLen); + */ + assert(mLen <= k-11); + if (mLen > k-11) + { + fprintf(stderr,"message too long\n"); + return false; + } + + // output will be written to ?? // these two bytes defined by RFC - padded[0]=0; - padded[1]=2; + EM[0]=0x00; + EM[1]=0x02; + // add non-zero random bytes of length k - mLen -3 - for(i=2 ; i < n-buflen-1 ; ++i) - do - ops_random(padded+i, 1); - while(padded[i] == 0); + for(i=2 ; i < k-mLen-1 ; ++i) + do + ops_random(EM+i, 1); + while(EM[i] == 0); assert (i >= 8+2); - padded[i++]=0; + EM[i++]=0; - memcpy(padded+i, buf, buflen); + memcpy(EM+i, M, mLen); - n=ops_rsa_public_encrypt(encmpibuf, padded, n, &pkey->key.rsa); + /* + int i=0; + fprintf(stderr,"Encoded Message: \n"); + for (i=0; ikey.rsa); + assert(n!=-1); if(n <= 0) @@ -93,4 +146,12 @@ skp->rsa.encrypted_m=BN_bin2bn(encmpibuf, n, NULL); + /* + fprintf(stderr,"encrypted mpi buf : "); + int i; + for (i=0; i<16; i++) + fprintf(stderr,"%2x ", encmpibuf[i]); + fprintf(stderr,"\n"); + */ + return ops_true; } Index: openpgpsdk/trunk/src/advanced/adv_openssl_crypto.c =================================================================== --- openpgpsdk/trunk/src/advanced/adv_openssl_crypto.c (revision 470) +++ openpgpsdk/trunk/src/advanced/adv_openssl_crypto.c (revision 485) @@ -4,4 +4,5 @@ #include #include +#include #include #include @@ -167,4 +168,12 @@ n=RSA_private_decrypt(length,in,out,orsa,RSA_NO_PADDING); + char errbuf[1024]; + errbuf[0]='\0'; + if (n==-1) + { + unsigned long err=ERR_get_error(); + ERR_error_string(err,&errbuf[0]); + fprintf(stderr,"openssl error : %s\n",errbuf); + } orsa->n=orsa->d=orsa->p=orsa->q=NULL; RSA_free(orsa); @@ -179,9 +188,21 @@ int n; + // printf("ops_rsa_public_encrypt: length=%ld\n", length); + orsa=RSA_new(); orsa->n=rsa->n; orsa->e=rsa->e; + // printf("len: %ld\n", length); + // ops_print_bn("n: ", orsa->n); + // ops_print_bn("e: ", orsa->e); n=RSA_public_encrypt(length,in,out,orsa,RSA_NO_PADDING); + + if (n==-1) + { + BIO *out; + out=BIO_new_fd(fileno(stderr), BIO_NOCLOSE); + ERR_print_errors(out); + } orsa->n=orsa->e=NULL; Index: openpgpsdk/trunk/src/advanced/adv_packet-parse.c =================================================================== --- openpgpsdk/trunk/src/advanced/adv_packet-parse.c (revision 480) +++ openpgpsdk/trunk/src/advanced/adv_packet-parse.c (revision 485) @@ -2,4 +2,6 @@ * \brief Parser for OpenPGP packets */ + +#include #include @@ -2166,14 +2168,21 @@ static int parse_pk_session_key(ops_region_t *region, - ops_parse_info_t *pinfo) + ops_parse_info_t *pinfo) { unsigned char c[1]; ops_parser_content_t content; ops_parser_content_t pc; - unsigned char buf[8192]; + // unsigned char buf[8192]; int n; BIGNUM *enc_m; unsigned k; const ops_secret_key_t *secret; + + const size_t sz_unencoded_m_buf=CAST_KEY_LENGTH+1+2; + unsigned char unencoded_m_buf[sz_unencoded_m_buf]; + + // const size_t sz_encoded_m_buf=BN_num_bytes(pub_key->key.rsa.n); + // const size_t sz_encoded_m_buf=128; //\todo FIXME RW + //unsigned char encoded_m_buf[sz_encoded_m_buf]; if(!limited_read(c,1,region,pinfo)) @@ -2189,4 +2198,13 @@ return 0; + /* + int i; + int x=sizeof C.pk_session_key.key_id; + printf("session key id: x=%d\n",x); + for (i=0; idecrypt,C.pk_session_key.symmetric_algorithm); + unsigned char *iv=ops_mallocz(pinfo->decrypt.blocksize); + pinfo->decrypt.set_iv(&pinfo->decrypt, iv); pinfo->decrypt.set_key(&pinfo->decrypt,C.pk_session_key.key); - + ops_encrypt_init(&pinfo->decrypt); return 1; } @@ -2298,6 +2332,8 @@ if(buf[b-2] != buf[b] || buf[b-1] != buf[b+1]) { + fprintf(stderr,"Bad symmetric decrypt (%02x%02x vs %02x%02x)\n", + buf[b-2],buf[b-1],buf[b],buf[b+1]); // ERR4P(pinfo,"Bad symmetric decrypt (%02x%02x vs %02x%02x)", - // buf[b-2],buf[b-1],buf[b],buf[b+1]); + // buf[b-2],buf[b-1],buf[b],buf[b+1]); return 0; } @@ -2327,5 +2363,5 @@ if (memcmp(mdc_hash,hashed,OPS_SHA1_HASH_SIZE)) { - fprintf(stderr,"Hash is bad"); + fprintf(stderr,"Hash is bad\n"); // ERRP(pinfo,"Bad hash in MDC"); return 0; Index: openpgpsdk/trunk/src/advanced/adv_symmetric.c =================================================================== --- openpgpsdk/trunk/src/advanced/adv_symmetric.c (revision 480) +++ openpgpsdk/trunk/src/advanced/adv_symmetric.c (revision 485) @@ -102,4 +102,16 @@ arg->decrypted, buffer,n); + + /* + fprintf(stderr,"READING:\nencrypted: "); + int i=0; + for (i=0; i<16; i++) + fprintf(stderr,"%2x ", buffer[i]); + fprintf(stderr,"\n"); + fprintf(stderr,"decrypted: "); + for (i=0; i<16; i++) + fprintf(stderr,"%2x ", arg->decrypted[i]); + fprintf(stderr,"\n"); + */ } else Index: openpgpsdk/trunk/src/standard/std_print.c =================================================================== --- openpgpsdk/trunk/src/standard/std_print.c (revision 484) +++ openpgpsdk/trunk/src/standard/std_print.c (revision 485) @@ -18,5 +18,5 @@ static int indent=0; -static void print_bn( const char *name, +void print_bn( const char *name, const BIGNUM *bn); #ifdef NOTYETUSED @@ -225,5 +225,5 @@ } -static void print_bn( const char *name, const BIGNUM *bn) +void print_bn( const char *name, const BIGNUM *bn) { print_indent(); Index: openpgpsdk/trunk/tests/Makefile.template =================================================================== --- openpgpsdk/trunk/tests/Makefile.template (revision 480) +++ openpgpsdk/trunk/tests/Makefile.template (revision 485) @@ -14,5 +14,7 @@ TESTSRC=tests.c \ test_packet_types.c \ - test_crypt_mpi.c test_rsa_decrypt.c test_rsa_encrypt.c + test_crypt_mpi.c test_rsa_decrypt.c test_rsa_encrypt.c \ + test_crypto.c + TESTOBJ=$(TESTSRC:.c=.o) Index: openpgpsdk/trunk/tests/test_crypt_mpi.c =================================================================== --- openpgpsdk/trunk/tests/test_crypt_mpi.c (revision 474) +++ openpgpsdk/trunk/tests/test_crypt_mpi.c (revision 485) @@ -1,3 +1,5 @@ #include "CUnit/Basic.h" + +#include #include "tests.h" @@ -8,8 +10,6 @@ #include "openpgpsdk/create.h" -static char secring[MAXBUF+1]; -static char pubring[MAXBUF+1]; -static ops_keyring_t pub_keyring; -static ops_keyring_t sec_keyring; +//static char secring[MAXBUF+1]; +//static char pubring[MAXBUF+1]; static const ops_key_data_t *pubkey; static const ops_key_data_t *seckey; @@ -17,4 +17,5 @@ int init_suite_crypt_mpi(void) { +#ifdef XXX static char keydetails[MAXBUF+1]; int fd=0; @@ -55,6 +56,8 @@ char keyid[]="Alpha (RSA, no passphrase) "; - pubkey=ops_keyring_find_key_by_userid(&pub_keyring,keyid); - seckey=ops_keyring_find_key_by_userid(&sec_keyring,keyid); +#endif + pubkey=ops_keyring_find_key_by_userid(&pub_keyring,alpha_user_id); + // seckey=ops_keyring_find_key_by_userid(&sec_keyring,keyid); + seckey=ops_keyring_find_key_by_userid(&sec_keyring,alpha_user_id); // Return success @@ -64,12 +67,16 @@ int clean_suite_crypt_mpi(void) { + +#ifdef XXX char cmd[MAXBUF+1]; - /* Close OPS */ ops_keyring_free(&pub_keyring); ops_keyring_free(&sec_keyring); +#endif + ops_finish(); +#ifdef XXX /* Remove test dir and files */ snprintf(cmd,MAXBUF,"rm -rf %s", dir); @@ -79,5 +86,8 @@ return 1; } +#endif + reset_vars(); + return 0; } @@ -85,5 +95,6 @@ void test_crypt_mpi(void) { -#define BSZ (256/8+1+2) + // hardcoded using CAST +#define BSZ (CAST_KEY_LENGTH+1+2) unsigned char in[BSZ]; @@ -92,8 +103,10 @@ ops_boolean_t rtn; - ops_pk_session_key_t *session_key=ops_create_pk_session_key(pubkey); + ops_pk_session_key_t *encrypted_pk_session_key=NULL; + + encrypted_pk_session_key=ops_create_pk_session_key(pubkey); // recreate what was encrypted - ops_create_m_buf(session_key, in); + // ops_create_m_buf(session_key, in); // CU_ASSERT(session_key); @@ -102,5 +115,5 @@ // decrypt it - rtn=ops_decrypt_mpi(out,BSZ, session_key->parameters.rsa.encrypted_m, &seckey->key.skey); + rtn=ops_decrypt_and_unencode_mpi(out,BSZ, encrypted_pk_session_key->parameters.rsa.encrypted_m, &seckey->key.skey); // [0] is the symmetric algorithm Index: openpgpsdk/trunk/tests/test_crypto.c =================================================================== --- (revision ) +++ openpgpsdk/trunk/tests/test_crypto.c (revision 485) @@ -1,0 +1,293 @@ +#include "CUnit/Basic.h" + +#include +#include "openpgpsdk/std_print.h" +/* +#include +#include "openpgpsdk/packet.h" +#include "openpgpsdk/packet-parse.h" +#include "openpgpsdk/keyring.h" +#include "openpgpsdk/util.h" +#include "openpgpsdk/crypto.h" +#include "openpgpsdk/readerwriter.h" +#include "../src/advanced/parse_local.h" +#include +#include +#include +*/ + +#include "tests.h" + +/* +static unsigned char* literal_data=NULL; +static size_t sz_literal_data=0; +static unsigned char* mdc_data=NULL; +static size_t sz_mdc_data=0; +static unsigned char* encrypted_pk_sk=NULL; +static size_t sz_encrypted_pk_sk=0; + +#define MAXBUF 128 + +static void cleanup(); +*/ + +/* + * initialisation + */ + +int init_suite_crypto(void) + { +#ifdef XXX + char keydetails[MAXBUF+1]; + char keyring_name[MAXBUF+1]; + int fd=0; + char cmd[MAXBUF+1]; + + // Initialise OPS + ops_init(); + + 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"; + // Create temp directory + if (!mktmpdir()) + return 1; + + /* + * Create a RSA keypair with no passphrase + */ + + snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.alpha"); + + if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) + { + fprintf(stderr,"Can't create key details\n"); + return 1; + } + + write(fd,rsa_nopass,strlen(rsa_nopass)); + close(fd); + + snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); + system(cmd); + + // read keyrings + snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir); + ops_keyring_read(&pub_keyring,keyring_name); + + // read keyring + snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir); + ops_keyring_read(&sec_keyring,keyring_name); +#endif + + // Return success + return 0; + } + +int clean_suite_crypto(void) + { +#ifdef XXX + /* Close OPS */ + + ops_finish(); +#endif + reset_vars(); + + return 0; + } + +static void test_cfb_aes() + { + // Used for trying low-level OpenSSL tests + + ops_crypt_t crypt; + ops_crypt_any(&crypt, OPS_SA_AES_256); + + /* + AES init + using empty IV and key for the moment + */ + unsigned char *iv=ops_mallocz(crypt.blocksize); + unsigned char *key=ops_mallocz(crypt.keysize); + snprintf((char *)key, crypt.keysize, "AES_KEY"); + crypt.set_iv(&crypt, iv); + crypt.set_key(&crypt, key); + ops_encrypt_init(&crypt); + + // Why does aes encrypt/decrypt work?? + // crypt=&crypt_aes; + + unsigned char *in=ops_mallocz(crypt.blocksize); + unsigned char *out=ops_mallocz(crypt.blocksize); + unsigned char *out2=ops_mallocz(crypt.blocksize); + + snprintf((char *)in,crypt.blocksize,"hello"); + /* + printf("\n"); + printf("in:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", + in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); + printf("in:\t%c %c %c %c %c %c %c %c\n", + in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); + */ + + crypt.block_encrypt(&crypt, out, in); + // AES_ecb_encrypt(in,out,crypt.data,AES_ENCRYPT); + /* + printf("out:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", + out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); + printf("out:\t%c %c %c %c %c %c %c %c\n", + out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); + */ + + crypt.block_decrypt(&crypt, out2, out); + // AES_ecb_encrypt(out,out2,crypt.data,AES_DECRYPT); + /* + printf("out2:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", + out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); + printf("out2:\t%c %c %c %c %c %c %c %c\n", + out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); + */ + CU_ASSERT(memcmp((char *)in, (char *)out2, strlen((char *)in))==0); + + } + +static void test_cfb_cast() + { + // Used for trying low-level OpenSSL tests + + ops_crypt_t crypt; + ops_crypt_any(&crypt, OPS_SA_CAST5); + + /* + * CAST + */ + unsigned char *iv=NULL; + unsigned char *key=NULL; + iv=ops_mallocz(crypt.blocksize); + key=ops_mallocz(crypt.keysize); + // snprintf((char *)key, crypt_cast.keysize, "CAST_KEY"); + crypt.set_iv(&crypt, iv); + crypt.set_key(&crypt, key); + ops_encrypt_init(&crypt); + + unsigned char *in=ops_mallocz(crypt.blocksize); + unsigned char *out=ops_mallocz(crypt.blocksize); + unsigned char *out2=ops_mallocz(crypt.blocksize); + + snprintf((char *)in,crypt.blocksize,"hello"); + /* + printf("\n"); + printf("in:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", + in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); + printf("in:\t%c %c %c %c %c %c %c %c\n", + in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); + */ + + crypt.block_encrypt(&crypt, out, in); + // AES_ecb_encrypt(in,out,crypt.data,AES_ENCRYPT); + /* + printf("out:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", + out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); + printf("out:\t%c %c %c %c %c %c %c %c\n", + out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); + */ + + crypt.block_decrypt(&crypt, out2, out); + // AES_ecb_encrypt(out,out2,crypt.data,AES_DECRYPT); + /* + printf("out2:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", + out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); + printf("out2:\t%c %c %c %c %c %c %c %c\n", + out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); + */ + CU_ASSERT(memcmp((char *)in, (char *)out2, strlen((char *)in))==0); + + } + +static void test_rsa() + { + unsigned char* in=NULL; + unsigned char* encrypted=NULL; + unsigned char* decrypted=NULL; + const ops_key_data_t *pub_key=NULL; + const ops_public_key_t *pkey=NULL; + const ops_key_data_t *sec_key=NULL; + const ops_secret_key_t *skey=NULL; + + in=ops_mallocz(128); + encrypted=ops_mallocz(128); + decrypted=ops_mallocz(128); + + ops_random(in,128); + + int n=0; + pub_key=ops_keyring_find_key_by_userid(&pub_keyring, alpha_user_id); + // ops_print_public_key(pub_key); + pkey=ops_get_public_key_from_data(pub_key); + + sec_key=ops_keyring_find_key_by_userid(&sec_keyring, alpha_user_id); + // ops_print_secret_key(sec_key); + skey=ops_get_secret_key_from_data(sec_key); + + /* + unsigned int i; + fprintf(stderr,"in: "); + for (i=0; i<128; i++) + fprintf(stderr,"%2x ", in[i]); + fprintf(stderr,"\n"); + */ + + n=ops_rsa_public_encrypt(&encrypted[0], (unsigned char *)in, 128, &pkey->key.rsa); + CU_ASSERT(n!=-1); + if (n==-1) + return; + + /* + fprintf(stderr,"%d encrypted\n",n); + fprintf(stderr,"encrypted: "); + for (i=0; i<128; i++) + fprintf(stderr,"%2x ", encrypted[i]); + fprintf(stderr,"\n"); + */ + n=ops_rsa_private_decrypt(&decrypted[0], encrypted, 128, + &skey->key.rsa, &pkey->key.rsa); + CU_ASSERT(n!=-1); + if (n==-1) + return; + + /* + fprintf(stderr,"%d decrypted\n",n); + fprintf(stderr,"decrypted: "); + for (i=0; i<128; i++) + fprintf(stderr,"%2x ", decrypted[i]); + fprintf(stderr,"\n"); + */ + CU_ASSERT(memcmp(in,&decrypted[0],128)==0); + + fprintf(stderr,"memcmp returns %d\n",memcmp(in,&decrypted[0],128)); + + free(encrypted); + free(decrypted); + } + +CU_pSuite suite_crypto() +{ + CU_pSuite suite = NULL; + + suite = CU_add_suite("Crypto Suite", init_suite_crypto, clean_suite_crypto); + if (!suite) + return NULL; + + // add tests to suite + + if (NULL == CU_add_test(suite, "Test CFB AES", test_cfb_aes)) + return NULL; + + if (NULL == CU_add_test(suite, "Test CFB CAST", test_cfb_cast)) + return NULL; + + if (NULL == CU_add_test(suite, "Test RSA", test_rsa)) + return NULL; + + return suite; +} + +// EOF Index: openpgpsdk/trunk/tests/test_packet_types.c =================================================================== --- openpgpsdk/trunk/tests/test_packet_types.c (revision 484) +++ openpgpsdk/trunk/tests/test_packet_types.c (revision 485) @@ -16,6 +16,4 @@ #include "tests.h" -static unsigned char* literal_data=NULL; -static size_t sz_literal_data=0; static unsigned char* mdc_data=NULL; static size_t sz_mdc_data=0; @@ -33,16 +31,17 @@ int init_suite_packet_types(void) { - char keydetails[MAXBUF+1]; - char keyring_name[MAXBUF+1]; - int fd=0; - char cmd[MAXBUF+1]; + // char keydetails[MAXBUF+1]; + // char keyring_name[MAXBUF+1]; + // int fd=0; + // char cmd[MAXBUF+1]; // Initialise OPS ops_init(); +#ifdef XXX 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"; // Create temp directory if (!mktmpdir()) - return 1; + return 1; /* @@ -65,10 +64,11 @@ // read keyrings + snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir); ops_keyring_read(&pub_keyring,keyring_name); - // read keyring snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir); ops_keyring_read(&sec_keyring,keyring_name); +#endif // Return success @@ -82,36 +82,9 @@ ops_finish(); + reset_vars(); + return 0; } -static ops_parse_cb_return_t -callback_literal_data(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) - { - ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; - - OPS_USED(cbinfo); - - // ops_print_packet(content_); - - // Read data from packet into static buffer - switch(content_->tag) - { - case OPS_PTAG_CT_LITERAL_DATA_BODY: - sz_literal_data=content->literal_data_body.length; - literal_data=ops_mallocz(sz_literal_data+1); - memcpy(literal_data,content->literal_data_body.data,sz_literal_data); - break; - - case OPS_PTAG_CT_LITERAL_DATA_HEADER: - // ignore - break; - - default: - return callback_general(content_,cbinfo); - } - - return OPS_RELEASE_MEMORY; - } - static ops_parse_cb_return_t callback_mdc(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) @@ -228,4 +201,5 @@ ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); rtn=ops_parse(pinfo); + CU_ASSERT(rtn==1); /* @@ -274,4 +248,5 @@ ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); rtn=ops_parse(pinfo); + CU_ASSERT(rtn==1); /* @@ -285,77 +260,4 @@ ops_teardown_memory_read(pinfo,mem); free (in); - } - -static void test_cfb() - { - // Used for trying low-level OpenSSL tests - - ops_crypt_t crypt_aes; - ops_crypt_any(&crypt_aes, OPS_SA_AES_256); - - ops_crypt_t crypt_cast; - ops_crypt_any(&crypt_cast, OPS_SA_CAST5); - - ops_crypt_t* crypt; - - /* - AES init - using empty IV and key for the moment - */ - unsigned char *iv=ops_mallocz(crypt_aes.blocksize); - unsigned char *key=ops_mallocz(crypt_aes.keysize); - snprintf((char *)key, crypt_aes.keysize, "AES_KEY"); - crypt_aes.set_iv(&crypt_aes, iv); - crypt_aes.set_key(&crypt_aes, key); - ops_encrypt_init(&crypt_aes); - - /* - * CAST - */ - iv=ops_mallocz(crypt_cast.blocksize); - key=ops_mallocz(crypt_cast.keysize); - // snprintf((char *)key, crypt_cast.keysize, "CAST_KEY"); - crypt_cast.set_iv(&crypt_cast, iv); - crypt_cast.set_key(&crypt_cast, key); - ops_encrypt_init(&crypt_cast); - - crypt=&crypt_cast; - - // Why does aes encrypt/decrypt work?? - // crypt=&crypt_aes; - - unsigned char *in=ops_mallocz(crypt->blocksize); - unsigned char *out=ops_mallocz(crypt->blocksize); - unsigned char *out2=ops_mallocz(crypt->blocksize); - - snprintf((char *)in,crypt->blocksize,"hello"); - /* - printf("\n"); - printf("in:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", - in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); - printf("in:\t%c %c %c %c %c %c %c %c\n", - in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); - */ - - crypt->block_encrypt(crypt, out, in); - // AES_ecb_encrypt(in,out,crypt.data,AES_ENCRYPT); - /* - printf("out:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", - out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); - printf("out:\t%c %c %c %c %c %c %c %c\n", - out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); - */ - - crypt->block_decrypt(crypt, out2, out); - // AES_ecb_encrypt(out,out2,crypt.data,AES_DECRYPT); - /* - printf("out2:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", - out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); - printf("out2:\t%c %c %c %c %c %c %c %c\n", - out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); - */ - CU_ASSERT(memcmp((char *)in, (char *)out2, strlen((char *)in))==0); - - cleanup(); } @@ -380,4 +282,5 @@ ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); rtn=ops_parse(pinfo); + CU_ASSERT(rtn==1); // This duplicates the hash done in ops_write_mdc so that we @@ -459,4 +362,5 @@ rtn=ops_parse(pinfo); + CU_ASSERT(rtn==1); /* @@ -474,5 +378,4 @@ static void test_ops_encrypted_pk_sk() { - char *user_id="Alpha (RSA, no passphrase) "; ops_pk_session_key_t *encrypted_pk_session_key; ops_create_info_t *cinfo; @@ -485,5 +388,7 @@ // write - const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id); + const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, alpha_user_id); + assert(pub_key); + encrypted_pk_session_key=ops_create_pk_session_key(pub_key); ops_write_pk_session_key(cinfo,encrypted_pk_session_key); @@ -494,4 +399,5 @@ // read rtn=ops_parse(pinfo); + CU_ASSERT(rtn==1); // test @@ -513,7 +419,4 @@ // add tests to suite - if (NULL == CU_add_test(suite, "Test CFB", test_cfb)) - return NULL; - if (NULL == CU_add_test(suite, "Tag 11: Literal Data packet in Text mode", test_literal_data_packet_text)) return NULL; Index: openpgpsdk/trunk/tests/test_rsa_decrypt.c =================================================================== --- openpgpsdk/trunk/tests/test_rsa_decrypt.c (revision 476) +++ openpgpsdk/trunk/tests/test_rsa_decrypt.c (revision 485) @@ -19,7 +19,7 @@ #define MAXBUF 128 -static char secring[MAXBUF+1]; +//static char secring[MAXBUF+1]; //static char dir[MAXBUF+1]; -static char keydetails[MAXBUF+1]; +//static char keydetails[MAXBUF+1]; static ops_keyring_t keyring; static char *filename_rsa_noarmour_nopassphrase="rsa_noarmour_nopassphrase.txt"; @@ -33,4 +33,5 @@ static char* text; +/* static int create_testfile(const char *name) { @@ -48,4 +49,5 @@ return 1; } +*/ static ops_parse_cb_return_t @@ -170,4 +172,5 @@ int init_suite_rsa_decrypt(void) { +#ifdef XXX int fd=0; char cmd[MAXBUF+1]; @@ -255,4 +258,5 @@ snprintf(secring,MAXBUF,"%s/secring.gpg", dir); ops_keyring_read(&keyring,secring); +#endif // Return success @@ -262,6 +266,7 @@ int clean_suite_rsa_decrypt(void) { + +#ifdef XXX char cmd[MAXBUF+1]; - /* Close OPS */ @@ -276,5 +281,8 @@ return 1; } - +#endif + + reset_vars(); + return 0; } Index: openpgpsdk/trunk/tests/test_rsa_encrypt.c =================================================================== --- openpgpsdk/trunk/tests/test_rsa_encrypt.c (revision 481) +++ openpgpsdk/trunk/tests/test_rsa_encrypt.c (revision 485) @@ -8,13 +8,12 @@ #include "openpgpsdk/util.h" #include "openpgpsdk/std_print.h" +#include "openpgpsdk/readerwriter.h" #include "tests.h" #define MAXBUF 128 -static char pub_keyring_name[MAXBUF+1]; -static char keydetails[MAXBUF+1]; -static ops_keyring_t pub_keyring; static char *filename_rsa_noarmour_singlekey="rsa_noarmour_singlekey.txt"; +/* static int create_testfile(const char *name) { @@ -32,14 +31,14 @@ return 1; } - -#ifdef XXX +*/ + static ops_parse_cb_return_t -callback(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) +callback_ops_decrypt(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) { ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; static ops_boolean_t skipping; - static const ops_key_data_t *encrypter; - const ops_key_data_t *keydata=NULL; - const ops_secret_key_t *secret; + static const ops_key_data_t *decrypter; + // const ops_key_data_t *keydata=NULL; + // const ops_secret_key_t *secret; OPS_USED(cbinfo); @@ -68,61 +67,24 @@ case OPS_PTAG_CT_PK_SESSION_KEY: // printf ("OPS_PTAG_CT_PK_SESSION_KEY\n"); - if(encrypter) + if(decrypter) break; - encrypter=ops_keyring_find_key_by_id(&keyring, + decrypter=ops_keyring_find_key_by_id(&sec_keyring, content->pk_session_key.key_id); - if(!encrypter) + if(!decrypter) break; break; case OPS_PARSER_CMD_GET_SECRET_KEY: - keydata=ops_keyring_find_key_by_id(&keyring,content->get_secret_key.pk_session_key->key_id); - if (!keydata || !ops_key_is_secret(keydata)) - return 0; - - // ops_set_secret_key(content,keydata); - - // Do we need the passphrase and not have it? If so, get it - ops_parser_content_t pc; - char *passphrase; - memset(&pc,'\0',sizeof pc); - passphrase=NULL; - pc.content.secret_key_passphrase.passphrase=&passphrase; - pc.content.secret_key_passphrase.secret_key=&(keydata->key.skey); - - /* Ugh. Need to duplicate this macro here to get the passphrase - Duplication to be removed when the callback gets moved to main code. - Can we make this inline code rather than a macro? - */ -#define CB(cbinfo,t,pc) do { (pc)->tag=(t); if((cbinfo)->cb(pc,(cbinfo)) == OPS_RELEASE_MEMORY) ops_parser_content_free(pc); } while(0) - CB(cbinfo,OPS_PARSER_CMD_GET_SK_PASSPHRASE,&pc); - - /* now get the key from the data */ - secret=ops_get_secret_key_from_data(keydata); - while(!secret) - { - /* then it must be encrypted */ - secret=ops_decrypt_secret_key_from_data(keydata,passphrase); - free(passphrase); - } - - *content->get_secret_key.secret_key=secret; - - break; + return callback_cmd_get_secret_key(content_,cbinfo); case OPS_PARSER_CMD_GET_SK_PASSPHRASE: - /* - Doing this so the test can be automated. - Will move this into separate stacked callback later - */ - *(content->secret_key_passphrase.passphrase)=ops_malloc_passphrase(current_passphrase); - return OPS_KEEP_MEMORY; - break; + return callback_cmd_get_secret_key_passphrase(content_,cbinfo); case OPS_PTAG_CT_LITERAL_DATA_BODY: - text=ops_mallocz(content->literal_data_body.length+1); - memcpy(text,content->literal_data_body.data,content->literal_data_body.length); - break; + return callback_literal_data(content_,cbinfo); + // text=ops_mallocz(content->literal_data_body.length+1); + // memcpy(text,content->literal_data_body.data,content->literal_data_body.length); + // break; case OPS_PARSER_PTAG: @@ -147,6 +109,4 @@ return OPS_RELEASE_MEMORY; } -#endif - /* Decryption suite initialization. @@ -157,13 +117,4 @@ int init_suite_rsa_encrypt(void) { - int fd=0; - char cmd[MAXBUF+1]; - 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"; - 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"; - - // Create temp directory - if (!mktmpdir()) - return 1; - // Create RSA test files @@ -175,4 +126,16 @@ */ +#ifdef XXX + int fd=0; + char cmd[MAXBUF+1]; + char keydetails[MAXBUF+1]; + char keyring_name[MAXBUF+1]; + 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"; + 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"; + + // Create temp directory + if (!mktmpdir()) + return 1; + /* * Create a RSA keypair with no passphrase @@ -193,21 +156,4 @@ system(cmd); -#ifdef XXX - // Now encrypt the test file with GPG - snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --homedir=%s --recipient Alpha %s/%s", dir, dir, filename_rsa_noarmour_nopassphrase); - if (system(cmd)) - { - return 1; - } - - // Now encrypt and ascii-armour the test file with GPG - snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --armor --homedir=%s --recipient Alpha %s/%s", dir, dir, filename_rsa_armour_nopassphrase); - if (system(cmd)) - { - return 1; - } - -#endif - /* * Create a RSA keypair with passphrase @@ -227,26 +173,14 @@ system(cmd); -#ifdef XXX - // Now encrypt the test file with GPG - snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --homedir=%s --recipient Bravo %s/%s", dir, dir, filename_rsa_noarmour_passphrase); - if (system(cmd)) - { - return 1; - } - - // Now encrypt and ascii-armour the test file with GPG - snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --armor --homedir=%s --recipient Bravo %s/%s", dir, dir, filename_rsa_armour_passphrase); - if (system(cmd)) - { - return 1; - } -#endif - // Initialise OPS ops_init(); - // read keyring - snprintf(pub_keyring_name,MAXBUF,"%s/pubring.gpg", dir); - ops_keyring_read(&pub_keyring,pub_keyring_name); + // read keyrings + snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir); + ops_keyring_read(&pub_keyring,keyring_name); + + snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir); + ops_keyring_read(&sec_keyring,keyring_name); +#endif // Return success @@ -258,7 +192,10 @@ // char cmd[MAXBUF+1]; +#ifdef XXX /* Close OPS */ ops_keyring_free(&pub_keyring); +#endif + ops_finish(); @@ -272,10 +209,51 @@ } */ + + reset_vars(); + return 0; } +static void test_rsa_decrypt(const char *encfile, const char*testtext) + { + int fd=0; + ops_parse_info_t *pinfo; + int rtn=0; + + // open encrypted file + fd=open(encfile,O_RDONLY); + if(fd < 0) + { + perror(encfile); + exit(2); + } + + // Set decryption reader and handling options + + pinfo=ops_parse_info_new(); + ops_reader_set_fd(pinfo,fd); + ops_parse_cb_set(pinfo,callback_ops_decrypt,NULL); + + // current_passphrase=nopassphrase; + + // Do the decryption + + rtn=ops_parse(pinfo); + CU_ASSERT(rtn==1); + + // Tidy up + + close(fd); + + // File contents should match + CU_ASSERT(memcmp(literal_data,testtext,sz_literal_data)==0); + } + static void test_rsa_encrypt(const int has_armour __attribute__((__unused__)), const ops_key_data_t *key __attribute__((__unused__)), const char *filename __attribute__((__unused__))) { -#ifdef NOTYETUSED + ops_memory_t *mem_ldt; + ops_create_info_t *cinfo_ldt; + + //#ifdef NOTYETUSED char myfile[MAXBUF+1]; char encfile[MAXBUF+1]; @@ -283,5 +261,4 @@ int fd_in=0; int fd_out=0; - ops_create_info_t *cinfo; // ops_crypt_t encrypt; @@ -303,8 +280,4 @@ } - // Set encryption writer and handling options - - cinfo=ops_create_info_new(); - ops_writer_set_fd(cinfo,fd_out); // ops_parse_cb_set(pinfo,callback,NULL); @@ -320,5 +293,5 @@ // ops_encrypt_init(&encrypt); - ops_writer_push_encrypt(cinfo,key); + // ops_writer_push_encrypt(cinfo,key); // Set up armour/passphrase options @@ -341,6 +314,55 @@ break; assert(n>=0); +#ifdef USING_PUSH ops_write(buf,n,cinfo); - } +#else + // create a simple literal data packet as the encrypted payload + ops_setup_memory_write(&cinfo_ldt,&mem_ldt,n); + ops_write_literal_data((unsigned char *)buf, n, + OPS_LDT_BINARY, cinfo_ldt); +#endif + } + + + // write to file + + // Set encryption writer and handling options + + ops_create_info_t *cinfo; + cinfo=ops_create_info_new(); + ops_writer_set_fd(cinfo,fd_out); + + /* + * write out the encrypted packet + */ + char *user_id="Alpha (RSA, no passphrase) "; + const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id); + ops_print_public_key_verbose(pub_key); + + ops_pk_session_key_t* encrypted_pk_session_key; + encrypted_pk_session_key=ops_create_pk_session_key(pub_key); + ops_write_pk_session_key(cinfo,encrypted_pk_session_key); + + //int rtn=0; + // ops_parse_info_t *pinfo; + // ops_memory_t *mem; + // ops_setup_memory_write(&cinfo,&mem,MAXBUF); + + // ops_crypt_any(&encrypt, OPS_SA_CAST5); + ops_crypt_t encrypt; + ops_crypt_any(&encrypt, encrypted_pk_session_key->symmetric_algorithm); + unsigned char *iv=NULL; + iv=ops_mallocz(encrypt.blocksize); + encrypt.set_iv(&encrypt, iv); + key=ops_mallocz(encrypt.keysize); // using blank key for now + // snprintf((char *)key, encrypt.keysize, "CAST_KEY"); + // encrypt.set_key(&encrypt, key); + encrypt.set_key(&encrypt, &encrypted_pk_session_key->key[0]); + ops_encrypt_init(&encrypt); + + ops_write_se_ip_data( ops_memory_get_data(mem_ldt), + ops_memory_get_length(mem_ldt), + &encrypt, cinfo); + // Tidy up @@ -350,9 +372,10 @@ // File contents should match - char *text; char buffer[MAXBUF+1]; create_testtext(filename,&buffer[0],MAXBUF); - CU_ASSERT(strcmp(text,buffer)==0); -#endif + test_rsa_decrypt(encfile,buffer); + // char *text; + // CU_ASSERT(strcmp(text,buffer)==0); + //#endif } @@ -396,5 +419,4 @@ return NULL; -#ifdef TBD // add tests to suite @@ -402,4 +424,5 @@ return NULL; +#ifdef TBD if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_encrypt_armour_nopassphrase)) return NULL; Index: openpgpsdk/trunk/tests/tests.c =================================================================== --- openpgpsdk/trunk/tests/tests.c (revision 484) +++ openpgpsdk/trunk/tests/tests.c (revision 485) @@ -13,6 +13,7 @@ #include "tests.h" +extern CU_pSuite suite_crypto(); extern CU_pSuite suite_packet_types(); -extern CU_pSuite suite_crypt_mpi(); +//extern CU_pSuite suite_crypt_mpi(); extern CU_pSuite suite_rsa_decrypt(); extern CU_pSuite suite_rsa_encrypt(); @@ -22,10 +23,84 @@ ops_keyring_t sec_keyring; static char* no_passphrase=""; +unsigned char* literal_data=NULL; +size_t sz_literal_data=0; +char *alpha_user_id="Alpha (RSA, no passphrase) "; + +void setup_test_keys() + { + char keydetails[MAXBUF+1]; + char keyring_name[MAXBUF+1]; + int fd=0; + char cmd[MAXBUF+1]; + + 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"; + // Create temp directory + if (!mktmpdir()) + return; + + /* + * Create a RSA keypair with no passphrase + */ + + snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.alpha"); + + if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) + { + fprintf(stderr,"Can't create key details\n"); + return; + } + + write(fd,rsa_nopass,strlen(rsa_nopass)); + close(fd); + + snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); + system(cmd); + + // read keyrings + + snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir); + ops_keyring_read(&pub_keyring,keyring_name); + + snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir); + ops_keyring_read(&sec_keyring,keyring_name); + + } + +static void cleanup() + { + char cmd[MAXBUF]; + + return; + + /* Remove test dir and files */ + snprintf(cmd,MAXBUF,"rm -rf %s", dir); + if (system(cmd)) + { + perror("Can't delete test directory "); + return; + } + } int main() { + setup_test_keys(); + if (CUE_SUCCESS != CU_initialize_registry()) - return CU_get_error(); + return CU_get_error(); + + if (NULL == suite_crypto()) + { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* + if (NULL == suite_crypt_mpi()) + { + CU_cleanup_registry(); + return CU_get_error(); + } + */ if (NULL == suite_packet_types()) @@ -41,4 +116,5 @@ return CU_get_error(); } + */ if (NULL == suite_rsa_encrypt()) @@ -47,5 +123,4 @@ return CU_get_error(); } - */ // Run tests @@ -53,4 +128,7 @@ CU_basic_run_tests(); CU_cleanup_registry(); + + cleanup(); + return CU_get_error(); } @@ -99,4 +177,19 @@ } +void create_testfile(const char *name) + { + char filename[MAXBUF+1]; + char buffer[MAXBUF+1]; + + int fd=0; + snprintf(filename,MAXBUF,"%s/%s",dir,name); + if ((fd=open(filename,O_WRONLY| O_CREAT | O_EXCL, 0600))<0) + return; + + create_testtext(name,&buffer[0],MAXBUF); + write(fd,buffer,strlen(buffer)); + close(fd); + } + ops_parse_cb_return_t callback_general(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) @@ -220,2 +313,40 @@ } +ops_parse_cb_return_t +callback_literal_data(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) + { + ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; + + OPS_USED(cbinfo); + + // ops_print_packet(content_); + + // Read data from packet into static buffer + switch(content_->tag) + { + case OPS_PTAG_CT_LITERAL_DATA_BODY: + sz_literal_data=content->literal_data_body.length; + literal_data=ops_mallocz(sz_literal_data+1); + memcpy(literal_data,content->literal_data_body.data,sz_literal_data); + break; + + case OPS_PTAG_CT_LITERAL_DATA_HEADER: + // ignore + break; + + default: + return callback_general(content_,cbinfo); + } + + return OPS_RELEASE_MEMORY; + } + +void reset_vars() + { + if (literal_data) + { + free (literal_data); + literal_data=NULL; + sz_literal_data=0; + } + } Index: openpgpsdk/trunk/tests/tests.h =================================================================== --- openpgpsdk/trunk/tests/tests.h (revision 484) +++ openpgpsdk/trunk/tests/tests.h (revision 485) @@ -18,4 +18,5 @@ void create_testtext(const char *text, char *buf, const int maxlen); void create_testdata(const char *text, unsigned char *buf, const int maxlen); +void create_testfile(const char *name); #define MAXBUF 128 @@ -26,7 +27,14 @@ ops_parse_cb_return_t callback_cmd_get_secret_key_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); +ops_parse_cb_return_t +callback_literal_data(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); + +void reset_vars(); ops_keyring_t pub_keyring; ops_keyring_t sec_keyring; +unsigned char* literal_data; +size_t sz_literal_data; +char* alpha_user_id; #endif