Changeset 343
- Timestamp:
- 01/30/06 16:10:43
- Files:
-
- openpgpsdk/trunk/examples/packet-dump.c (modified) (3 diffs)
- openpgpsdk/trunk/include/openpgpsdk/crypto.h (modified) (2 diffs)
- openpgpsdk/trunk/include/openpgpsdk/packet.h (modified) (2 diffs)
- openpgpsdk/trunk/src/packet-parse.c (modified) (7 diffs)
- openpgpsdk/trunk/src/signature.c (modified) (1 diff)
- openpgpsdk/trunk/src/symmetric.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/examples/packet-dump.c
r342 r343 831 831 print_hexdump("Salt",content->secret_key.salt, 832 832 sizeof content->secret_key.salt); 833 printf(" Iterations: %d\n",content->secret_key.iterations);833 printf("Octet count: %d\n",content->secret_key.octet_count); 834 834 print_hexdump("IV",content->secret_key.iv, 835 835 ops_block_size(content->secret_key.algorithm)); … … 838 838 if(content_->tag == OPS_PTAG_CT_ENCRYPTED_SECRET_KEY) 839 839 break; 840 841 printf("Checksum: %04x\n",content->secret_key.checksum);842 840 843 841 switch(content->secret_key.public_key.algorithm) … … 857 855 assert(0); 858 856 } 857 858 if(content->secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED) 859 print_hexdump("Checkhash",content->secret_key.checkhash, 860 OPS_CHECKHASH_SIZE); 861 else 862 printf("Checksum: %04x\n",content->secret_key.checksum); 863 859 864 break; 860 865 openpgpsdk/trunk/include/openpgpsdk/crypto.h
r341 r343 9 9 #include "packet-parse.h" 10 10 11 #define OPS_MAX_HASH 64 11 #define OPS_MAX_HASH_SIZE 64 12 #define OPS_MIN_HASH_SIZE 16 12 13 13 14 typedef void ops_hash_init_t(ops_hash_t *hash); … … 76 77 77 78 unsigned ops_block_size(ops_symmetric_algorithm_t alg); 79 unsigned ops_key_size(ops_symmetric_algorithm_t alg); 78 80 79 81 int ops_decrypt_data(ops_region_t *region,ops_parse_info_t *parse_info); openpgpsdk/trunk/include/openpgpsdk/packet.h
r341 r343 422 422 #define OPS_SALT_SIZE 8 423 423 424 // Hash size for secret key check 425 #define OPS_CHECKHASH_SIZE 20 426 424 427 /** ops_secret_key_t 425 428 */ … … 432 435 ops_hash_algorithm_t hash_algorithm; 433 436 unsigned char salt[OPS_SALT_SIZE]; 434 unsigned iterations;437 unsigned octet_count; 435 438 unsigned char iv[OPS_MAX_BLOCK_SIZE]; 439 ops_secret_key_union_t key; 436 440 unsigned checksum; 437 ops_secret_key_union_t key;441 unsigned char checkhash[OPS_CHECKHASH_SIZE]; 438 442 } ops_secret_key_t; 439 443 openpgpsdk/trunk/src/packet-parse.c
r342 r343 1793 1793 ops_region_t encregion; 1794 1794 ops_region_t *saved_region=NULL; 1795 size_t checksum_length=2; 1795 1796 1796 1797 memset(&content,'\0',sizeof content); … … 1800 1801 return 0; 1801 1802 C.secret_key.s2k_usage=c[0]; 1803 1804 if(C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED) 1805 checksum_length=20; 1802 1806 1803 1807 if(C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED … … 1828 1832 if(!limited_read(c,1,region,parse_info)) 1829 1833 return 0; 1830 C.secret_key. iterations=(16+(c[0]&15)) << ((c[0] >> 4)+6);1834 C.secret_key.octet_count=(16+(c[0]&15)) << ((c[0] >> 4)+6); 1831 1835 } 1832 1836 } … … 1846 1850 ops_parser_content_t pc; 1847 1851 char *passphrase; 1848 unsigned char hash[OPS_MAX_HASH]; 1852 unsigned char key[OPS_MAX_KEY_SIZE+OPS_MAX_HASH_SIZE]; 1853 ops_hash_t hashes[(OPS_MAX_KEY_SIZE+OPS_MIN_HASH_SIZE-1)/OPS_MIN_HASH_SIZE]; 1854 int keysize; 1855 int hashsize; 1856 size_t l; 1849 1857 1850 1858 n=ops_block_size(C.secret_key.algorithm); … … 1867 1875 } 1868 1876 1869 ops_hash(hash,C.secret_key.hash_algorithm,passphrase, 1870 strlen(passphrase)); 1877 keysize=ops_key_size(C.secret_key.algorithm); 1878 assert(keysize > 0 && keysize <= OPS_MAX_KEY_SIZE); 1879 1880 hashsize=ops_hash_size(C.secret_key.hash_algorithm); 1881 assert(hashsize > 0 && hashsize <= OPS_MAX_HASH_SIZE); 1882 1883 for(n=0 ; n*hashsize < keysize ; ++n) 1884 { 1885 int i; 1886 1887 ops_hash_any(&hashes[n],C.secret_key.hash_algorithm); 1888 hashes[n].init(&hashes[n]); 1889 // preload hashes with zeroes... 1890 for(i=0 ; i < n ; ++i) 1891 hashes[n].add(&hashes[n],"",1); 1892 } 1893 1894 l=strlen(passphrase); 1895 1896 for(n=0 ; n*hashsize < keysize ; ++n) 1897 { 1898 unsigned i; 1899 1900 switch(C.secret_key.s2k_specifier) 1901 { 1902 case OPS_S2KS_SALTED: 1903 hashes[n].add(&hashes[n],C.secret_key.salt,OPS_SALT_SIZE); 1904 // flow through... 1905 case OPS_S2KS_SIMPLE: 1906 hashes[n].add(&hashes[n],passphrase,l); 1907 break; 1908 1909 case OPS_S2KS_ITERATED_AND_SALTED: 1910 for(i=0 ; i < C.secret_key.octet_count ; i+=l+OPS_SALT_SIZE) 1911 { 1912 int j=l+OPS_SALT_SIZE; 1913 1914 if(i+j > C.secret_key.octet_count && i != 0) 1915 j=C.secret_key.octet_count-i; 1916 1917 hashes[n].add(&hashes[n],C.secret_key.salt, 1918 j > OPS_SALT_SIZE ? OPS_SALT_SIZE : j); 1919 if(j > OPS_SALT_SIZE) 1920 hashes[n].add(&hashes[n],passphrase,j-OPS_SALT_SIZE); 1921 } 1922 1923 } 1924 } 1925 1926 for(n=0 ; n*hashsize < keysize ; ++n) 1927 { 1928 int r=hashes[n].finish(&hashes[n],key+n*hashsize); 1929 assert(r == hashsize); 1930 } 1871 1931 1872 1932 ops_decrypt_any(&decrypt,C.secret_key.algorithm); 1873 1933 decrypt.set_iv(&decrypt,C.secret_key.iv); 1874 decrypt.set_key(&decrypt,hash); 1875 1934 decrypt.set_key(&decrypt,key); 1935 1936 /* We need to prevent the decrypter from reading the trailing 1937 checksum */ 1938 region->length-=checksum_length; 1876 1939 ops_reader_push_decrypt(parse_info,&decrypt,region); 1877 1940 1878 /* Since all known encryption for PGP doesn't compress, we can limit 1879 to the same length as the current region (for now) */ 1941 /* Since all known encryption for PGP doesn't compress, we can 1942 limit to the same length as the current region (for now), 1943 allowing for the trailing checksum. 1944 */ 1945 1880 1946 ops_init_subregion(&encregion,NULL); 1881 1947 encregion.length=region->length-region->length_read; … … 1912 1978 { 1913 1979 ops_reader_pop_decrypt(parse_info); 1980 assert(region->length_read == region->length); 1914 1981 region=saved_region; 1982 /* put back checksum data */ 1983 region->length+=checksum_length; 1915 1984 } 1916 1985 … … 1918 1987 return 0; 1919 1988 1920 if(!limited_read_scalar(&C.secret_key.checksum,2,region,parse_info))1921 return 0;1922 1989 // XXX: check the checksum 1990 1991 if(C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED) 1992 { 1993 if(!limited_read(C.secret_key.checkhash,20,region,parse_info)) 1994 return 0; 1995 } 1996 else 1997 { 1998 if(!limited_read_scalar(&C.secret_key.checksum,2,region,parse_info)) 1999 return 0; 2000 } 1923 2001 1924 2002 CBP(parse_info,OPS_PTAG_CT_SECRET_KEY,&content); openpgpsdk/trunk/src/signature.c
r319 r343 218 218 { 219 219 int n; 220 unsigned char hashout[OPS_MAX_HASH ];220 unsigned char hashout[OPS_MAX_HASH_SIZE]; 221 221 222 222 n=hash->finish(hash,hashout); openpgpsdk/trunk/src/symmetric.c
r341 r343 4 4 #include <openssl/cast.h> 5 5 6 unsigned ops_block_size(ops_symmetric_algorithm_t alg)7 {8 // perhaps do this via the underlying algorithm later9 switch(alg)10 {11 case OPS_SA_PLAINTEXT:12 return 1;13 14 case OPS_SA_IDEA:15 case OPS_SA_TRIPLEDES:16 case OPS_SA_CAST5:17 case OPS_SA_BLOWFISH:18 case OPS_SA_TWOFISH:19 return 8;20 21 case OPS_SA_AES_128:22 case OPS_SA_AES_192:23 case OPS_SA_AES_256:24 return 16;25 }26 27 return 0;28 }29 30 6 typedef struct 31 7 { 32 8 unsigned char decrypted[1024]; 33 9 size_t decrypted_count; 10 size_t decrypted_offset; 34 11 ops_decrypt_t *decrypt; 35 12 ops_region_t *region; … … 59 36 n=length; 60 37 61 memcpy(dest,arg->decrypted ,n);38 memcpy(dest,arg->decrypted+arg->decrypted_offset,n); 62 39 arg->decrypted_count-=n; 40 arg->decrypted_offset+=n; 63 41 length-=n; 64 42 dest+=n; … … 89 67 buffer,n); 90 68 assert(arg->decrypted_count > 0); 69 70 arg->decrypted_offset=0; 91 71 } 92 72 } … … 174 154 }; 175 155 176 void ops_decrypt_any(ops_decrypt_t *decrypt,ops_symmetric_algorithm_t alg)156 static ops_decrypt_t *get_proto(ops_symmetric_algorithm_t alg) 177 157 { 178 158 switch(alg) 179 159 { 180 160 case OPS_SA_CAST5: 181 *decrypt=cast5; 182 break; 161 return &cast5; 183 162 184 163 default: 185 164 assert(0); 186 165 } 166 167 return NULL; 187 168 } 188 169 170 void ops_decrypt_any(ops_decrypt_t *decrypt,ops_symmetric_algorithm_t alg) 171 { *decrypt=*get_proto(alg); } 172 173 unsigned ops_block_size(ops_symmetric_algorithm_t alg) 174 { 175 ops_decrypt_t *p=get_proto(alg); 176 177 if(!p) 178 return 0; 179 180 return p->blocksize; 181 } 182 183 unsigned ops_key_size(ops_symmetric_algorithm_t alg) 184 { 185 ops_decrypt_t *p=get_proto(alg); 186 187 if(!p) 188 return 0; 189 190 return p->keysize; 191 }
