Changeset 473
- Timestamp:
- 05/13/07 18:44:13
- Files:
-
- openpgpsdk/trunk/include/openpgpsdk/create.h (modified) (2 diffs)
- openpgpsdk/trunk/include/openpgpsdk/crypto.h (modified) (4 diffs)
- openpgpsdk/trunk/include/openpgpsdk/packet.h (modified) (1 diff)
- openpgpsdk/trunk/src/advanced/adv_create.c (modified) (5 diffs)
- openpgpsdk/trunk/src/advanced/adv_crypto.c (modified) (1 diff)
- openpgpsdk/trunk/src/advanced/adv_packet-parse.c (modified) (2 diffs)
- openpgpsdk/trunk/src/advanced/adv_symmetric.c (modified) (4 diffs)
- openpgpsdk/trunk/tests/Makefile.template (modified) (1 diff)
- openpgpsdk/trunk/tests/test_crypt_mpi.c (modified) (1 diff)
- openpgpsdk/trunk/tests/test_packet_types.c (added)
- openpgpsdk/trunk/tests/test_rsa_encrypt.c (modified) (9 diffs)
- openpgpsdk/trunk/tests/tests.c (modified) (4 diffs)
- openpgpsdk/trunk/tests/tests.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/include/openpgpsdk/create.h
r316 r473 10 10 #include "memory.h" 11 11 #include "errors.h" 12 #include "keyring.h" 12 13 13 14 typedef struct ops_writer_info ops_writer_info_t; … … 88 89 ops_create_info_t *info); 89 90 91 ops_pk_session_key_t *ops_create_pk_session_key(const ops_key_data_t *key); 92 93 void ops_create_m_buf(ops_pk_session_key_t *session_key, unsigned char *buf); 94 95 ops_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); 90 99 #endif openpgpsdk/trunk/include/openpgpsdk/crypto.h
r470 r473 38 38 typedef void ops_crypt_finish_t(ops_crypt_t *crypt); 39 39 40 /** _ops_ decrypt_t */40 /** _ops_crypt_t */ 41 41 struct _ops_crypt_t 42 42 { … … 54 54 unsigned char siv[OPS_MAX_BLOCK_SIZE]; /* Needed for weird v3 resync */ 55 55 unsigned char key[OPS_MAX_KEY_SIZE]; 56 size_t num; 56 size_t num; /* Count of characters encrypted so far */ 57 57 void *data; 58 58 }; … … 93 93 void ops_crypt_any(ops_crypt_t *decrypt,ops_symmetric_algorithm_t alg); 94 94 void ops_decrypt_init(ops_crypt_t *decrypt); 95 void ops_encrypt_init(ops_crypt_t *encrypt); 95 96 size_t ops_decrypt(ops_crypt_t *decrypt,void *out,const void *in, 96 97 size_t count); … … 116 117 struct ops_key_data; 117 118 void ops_writer_push_encrypt(ops_create_info_t *info, 119 // ops_crypt_t *encrypt, 118 120 const struct ops_key_data *key); 119 121 openpgpsdk/trunk/include/openpgpsdk/packet.h
r417 r473 737 737 OPS_LDT_LOCAL='l', 738 738 OPS_LDT_LOCAL2='1' 739 } literal_data_type_t;739 } ops_literal_data_type_t; 740 740 741 741 /** ops_literal_data_header_t */ 742 742 typedef struct 743 743 { 744 literal_data_type_t format;744 ops_literal_data_type_t format; 745 745 char filename[256]; 746 746 time_t modification_time; openpgpsdk/trunk/src/advanced/adv_create.c
r470 r473 749 749 { return ops_stacked_write(src,length,errors,winfo); } 750 750 751 void 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 751 772 ops_pk_session_key_t *ops_create_pk_session_key(const ops_key_data_t *key) 752 773 { 774 unsigned char buf[256/8+1+2]; 753 775 ops_pk_session_key_t *session_key=ops_mallocz(sizeof *session_key); 754 776 … … 762 784 ops_random(session_key->key, 256/8); 763 785 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)) 765 802 return NULL; 766 803 … … 781 818 { 782 819 assert(pksk->algorithm == OPS_PKA_RSA); 820 783 821 return ops_write_ptag(OPS_PTAG_CT_PK_SESSION_KEY, info) 784 822 && ops_write_length(1 + 8 + 1 + BN_num_bytes(pksk->parameters.rsa.encrypted_m) + 2, info) … … 787 825 && ops_write_scalar(pksk->algorithm, 1, info) 788 826 && ops_write_mpi(pksk->parameters.rsa.encrypted_m, info) 789 /* XXX: write the checksum! */790 827 && ops_write_scalar(0, 2, info); 791 828 } 792 829 793 830 static ops_boolean_t encrypted_writer(const unsigned char *src ATTRIBUTE_UNUSED, 794 unsigned length ATTRIBUTE_UNUSED,831 unsigned length, 795 832 ops_error_t **errors ATTRIBUTE_UNUSED, 796 833 ops_writer_info_t *winfo ATTRIBUTE_UNUSED) 797 834 { 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 858 static ops_boolean_t encrypted_finaliser(ops_error_t **errors ATTRIBUTE_UNUSED, 859 ops_writer_info_t *winfo ATTRIBUTE_UNUSED) 860 { 861 798 862 /* \todo */ 799 863 assert(0); … … 802 866 } 803 867 804 static ops_boolean_t encrypted_finaliser(ops_error_t **errors ATTRIBUTE_UNUSED, 805 ops_writer_info_t *winfo ATTRIBUTE_UNUSED) 868 void encrypted_destroyer (ops_writer_info_t *winfo ATTRIBUTE_UNUSED) 869 806 870 { 807 871 /* \todo */ 808 872 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);818 873 } 819 874 820 875 /* end of dummy code */ 821 876 877 ops_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 822 899 void ops_writer_push_encrypt(ops_create_info_t *info, 900 // ops_crypt_t *encrypt, 823 901 const ops_key_data_t *key) 824 902 { 903 904 // has ops_key_data_t * key 905 // needs ops_crypt_t * crypt 825 906 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]; 827 909 828 910 session_key=ops_create_pk_session_key(key); 829 911 ops_write_pk_session_key(info,session_key); 830 912 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 919 ops_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 65 65 unsigned i; 66 66 67 // implementation of EME-PKCS1-v1_5-ENCODE, as defined in OpenPGP RFC 68 67 69 assert(pkey->algorithm == OPS_PKA_RSA); 68 70 69 71 n=BN_num_bytes(pkey->key.rsa.n); 72 73 // these two bytes defined by RFC 70 74 padded[0]=0; 71 75 padded[1]=2; 72 // add non-zero random bytes 76 // add non-zero random bytes of length k - mLen -3 73 77 for(i=2 ; i < n-buflen-1 ; ++i) 74 78 do 75 79 ops_random(padded+i, 1); 76 80 while(padded[i] == 0); 81 82 assert (i >= 8+2); 83 77 84 padded[i++]=0; 85 78 86 memcpy(padded+i, buf, buflen); 79 87 openpgpsdk/trunk/src/advanced/adv_packet-parse.c
r470 r473 5 5 #include <openpgpsdk/packet.h> 6 6 #include <openpgpsdk/packet-parse.h> 7 #include <openpgpsdk/keyring.h> 7 8 #include <openpgpsdk/util.h> 8 9 #include <openpgpsdk/compress.h> … … 2458 2459 2459 2460 case OPS_PTAG_CT_PK_SESSION_KEY: 2461 printf("reading pk_session_key: length=%d\n",region.length); 2460 2462 r=parse_pk_session_key(®ion,pinfo); 2461 2463 break; openpgpsdk/trunk/src/advanced/adv_symmetric.c
r470 r473 11 11 12 12 #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* 13 17 14 18 typedef struct … … 326 330 } 327 331 332 void 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 328 341 void ops_decrypt_init(ops_crypt_t *decrypt) 329 342 { … … 360 373 } 361 374 362 size_t ops_encrypt(ops_crypt_t * decrypt,void *out_,const void *in_,375 size_t ops_encrypt(ops_crypt_t *encrypt,void *out_,const void *in_, 363 376 size_t count) 364 377 { … … 371 384 while(count-- > 0) 372 385 { 373 if( decrypt->num == decrypt->blocksize)386 if(encrypt->num == encrypt->blocksize) 374 387 { 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; 378 391 } 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; 381 394 } 382 395 openpgpsdk/trunk/tests/Makefile.template
r471 r473 12 12 LIBS=$(LIBDEPS) %CRYPTO_LIBS% %ZLIB% $(DM_LIB) $(CUNIT_LIB) 13 13 14 TESTSRC=tests.c test_crypt_mpi.c test_rsa_decrypt.c test_rsa_encrypt.c 14 TESTSRC=tests.c \ 15 test_packet_types.c \ 16 test_crypt_mpi.c test_rsa_decrypt.c test_rsa_encrypt.c 15 17 TESTOBJ=$(TESTSRC:.c=.o) 16 18 openpgpsdk/trunk/tests/test_crypt_mpi.c
r472 r473 85 85 void test_crypt_mpi(void) 86 86 { 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]; 90 91 91 92 ops_boolean_t rtn; 92 93 93 94 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 94 101 // the encrypted_mpi is now in session_key->parameters.rsa.encrypted_m 95 102 96 103 // 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 99 109 100 110 // 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); 102 112 } 103 113 openpgpsdk/trunk/tests/test_rsa_encrypt.c
r472 r473 11 11 #include "tests.h" 12 12 13 /*14 These include files are needed by callback.15 To be removed when callback gets added to main body of code16 #include "../src/advanced/parse_local.h"17 #include "../src/advanced/keyring_local.h"18 */19 20 13 #define MAXBUF 128 21 14 static char pub_keyring_name[MAXBUF+1]; … … 23 16 static ops_keyring_t pub_keyring; 24 17 static 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 }42 18 43 19 static int create_testfile(const char *name) … … 51 27 return 0; 52 28 53 snprintf(buffer,MAXBUF,create_testtext(name));29 create_testtext(name,&buffer[0],MAXBUF); 54 30 write(fd,buffer,strlen(buffer)); 55 31 close(fd); … … 367 343 } 368 344 369 // ops_parse(pinfo);370 371 345 // 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 382 350 // 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); 395 355 } 396 356 … … 404 364 } 405 365 406 /* 366 #ifdef TBD 407 367 void test_rsa_encrypt_armour(void) 408 368 { … … 424 384 test_rsa_encrypt(armour,passphrase,filename_rsa_armour_passphrase); 425 385 } 426 */386 #endif /*TBD*/ 427 387 428 388 CU_pSuite suite_rsa_encrypt() … … 434 394 return NULL; 435 395 396 #ifdef TBD 436 397 // add tests to suite 437 398 … … 439 400 return NULL; 440 401 441 /*442 402 if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_encrypt_armour_nopassphrase)) 443 403 return NULL; … … 448 408 if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_encrypt_armour_passphrase)) 449 409 return NULL; 450 */410 #endif /*TBD*/ 451 411 452 412 return suite; openpgpsdk/trunk/tests/tests.c
r471 r473 9 9 #include "tests.h" 10 10 11 extern CU_pSuite suite_packet_types(); 11 12 extern CU_pSuite suite_crypt_mpi(); 12 13 extern CU_pSuite suite_rsa_decrypt(); … … 20 21 if (CUE_SUCCESS != CU_initialize_registry()) 21 22 return CU_get_error(); 23 24 if (NULL == suite_packet_types()) 25 { 26 CU_cleanup_registry(); 27 return CU_get_error(); 28 } 22 29 23 30 if (NULL == suite_crypt_mpi()) … … 38 45 return CU_get_error(); 39 46 } 40 47 41 48 // Run tests 42 49 CU_basic_set_mode(CU_BRM_VERBOSE); … … 70 77 } 71 78 79 void 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 13 13 int mktmpdir(); 14 14 extern char dir[]; 15 void create_testtext(const char *text, char *buf, const int maxlen); 15 16 #define MAXBUF 128 16 17
