Changeset 500
- Timestamp:
- 09/04/07 17:20:22
- Files:
-
- openpgpsdk/trunk/include/openpgpsdk/crypto.h (modified) (2 diffs)
- openpgpsdk/trunk/src/advanced/adv_symmetric.c (modified) (12 diffs)
- openpgpsdk/trunk/tests/test_crypto.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/include/openpgpsdk/crypto.h
r495 r500 38 38 typedef void ops_crypt_block_decrypt_t(ops_crypt_t *crypt,void *out, 39 39 const void *in); 40 typedef void ops_crypt_cfb_encrypt_t(ops_crypt_t *crypt,void *out, 41 const void *in, size_t count); 42 typedef void ops_crypt_cfb_decrypt_t(ops_crypt_t *crypt,void *out, 43 const void *in, size_t count); 40 44 typedef void ops_crypt_finish_t(ops_crypt_t *crypt); 41 45 … … 50 54 ops_crypt_init_t *base_init; 51 55 ops_crypt_resync_t *decrypt_resync; 56 // encrypt/decrypt one block 52 57 ops_crypt_block_encrypt_t *block_encrypt; 53 58 ops_crypt_block_decrypt_t *block_decrypt; 59 60 // Standard CFB encrypt/decrypt (as used by Sym Enc Int Prot packets) 61 ops_crypt_cfb_encrypt_t *cfb_encrypt; 62 ops_crypt_cfb_decrypt_t *cfb_decrypt; 63 54 64 ops_crypt_finish_t *decrypt_finish; 55 65 unsigned char iv[OPS_MAX_BLOCK_SIZE]; openpgpsdk/trunk/src/advanced/adv_symmetric.c
r495 r500 198 198 crypt->encrypt_key=malloc(sizeof(CAST_KEY)); 199 199 CAST_set_key(crypt->encrypt_key,crypt->keysize,crypt->key); 200 crypt->decrypt_key=malloc(sizeof(CAST_KEY)); 201 CAST_set_key(crypt->decrypt_key,crypt->keysize,crypt->key); 200 202 } 201 203 … … 205 207 static void cast5_block_decrypt(ops_crypt_t *crypt,void *out,const void *in) 206 208 { CAST_ecb_encrypt(in,out,crypt->encrypt_key,CAST_DECRYPT); } 209 210 static void cast5_cfb_encrypt(ops_crypt_t *crypt,void *out,const void *in, size_t count) 211 { 212 CAST_cfb64_encrypt(in,out,count, 213 crypt->encrypt_key, crypt->iv, (int *)&crypt->num, 214 CAST_ENCRYPT); 215 } 216 217 static void cast5_cfb_decrypt(ops_crypt_t *crypt,void *out,const void *in, size_t count) 218 { 219 CAST_cfb64_encrypt(in,out,count, 220 crypt->encrypt_key, crypt->iv, (int *)&crypt->num, 221 CAST_DECRYPT); 222 } 207 223 208 224 #define TRAILER "","","","",0,NULL,NULL … … 219 235 cast5_block_encrypt, 220 236 cast5_block_decrypt, 237 cast5_cfb_encrypt, 238 cast5_cfb_decrypt, 221 239 std_finish, 222 240 TRAILER … … 247 265 static void idea_block_decrypt(ops_crypt_t *crypt,void *out,const void *in) 248 266 { idea_ecb_encrypt(in,out,crypt->decrypt_key); } 267 268 static void idea_cfb_encrypt(ops_crypt_t *crypt,void *out,const void *in, size_t count) 269 { 270 idea_cfb64_encrypt(in,out,count, 271 crypt->encrypt_key, crypt->iv, (int *)&crypt->num, 272 CAST_ENCRYPT); 273 } 274 275 static void idea_cfb_decrypt(ops_crypt_t *crypt,void *out,const void *in, size_t count) 276 { 277 idea_cfb64_encrypt(in,out,count, 278 crypt->decrypt_key, crypt->iv, (int *)&crypt->num, 279 CAST_DECRYPT); 280 } 249 281 250 282 static const ops_crypt_t idea= … … 259 291 idea_block_encrypt, 260 292 idea_block_decrypt, 293 idea_cfb_encrypt, 294 idea_cfb_decrypt, 261 295 std_finish, 262 296 TRAILER … … 288 322 static void aes_block_decrypt(ops_crypt_t *crypt,void *out,const void *in) 289 323 { AES_decrypt(in,out,crypt->decrypt_key); } 324 325 static void aes_cfb_encrypt(ops_crypt_t *crypt,void *out,const void *in, size_t count) 326 { 327 AES_cfb128_encrypt(in,out,count, 328 crypt->encrypt_key, crypt->iv, (int *)&crypt->num, 329 AES_ENCRYPT); 330 } 331 332 static void aes_cfb_decrypt(ops_crypt_t *crypt,void *out,const void *in, size_t count) 333 { 334 AES_cfb128_encrypt(in,out,count, 335 crypt->encrypt_key, crypt->iv, (int *)&crypt->num, 336 AES_DECRYPT); 337 } 290 338 291 339 static const ops_crypt_t aes128= … … 300 348 aes_block_encrypt, 301 349 aes_block_decrypt, 350 aes_cfb_encrypt, 351 aes_cfb_decrypt, 302 352 std_finish, 303 353 TRAILER … … 334 384 aes_block_encrypt, 335 385 aes_block_decrypt, 386 aes_cfb_encrypt, 387 aes_cfb_decrypt, 336 388 std_finish, 337 389 TRAILER … … 367 419 368 420 DES_ecb3_encrypt((void *)in,out,&keys[0],&keys[1],&keys[2],DES_DECRYPT); 421 } 422 423 static void tripledes_cfb_encrypt(ops_crypt_t *crypt __attribute__((__unused__)),void *out __attribute__((__unused__)),const void *in __attribute__((__unused__)), size_t count __attribute__((__unused__))) 424 { 425 assert(0); 426 /* 427 CAST_cfb64_encrypt(in,out,count, 428 crypt->encrypt_key, crypt->iv, (int *)&crypt->num, 429 CAST_ENCRYPT); 430 */ 431 } 432 433 static void tripledes_cfb_decrypt(ops_crypt_t *crypt __attribute__((__unused__)),void *out __attribute__((__unused__)),const void *in __attribute__((__unused__)), size_t count __attribute__((__unused__))) 434 { 435 assert(0); 436 /* 437 CAST_cfb64_encrypt(in,out,count, 438 crypt->encrypt_key, crypt->iv, (int *)&crypt->num, 439 CAST_DECRYPT); 440 */ 369 441 } 370 442 … … 380 452 tripledes_block_encrypt, 381 453 tripledes_block_decrypt, 454 tripledes_cfb_encrypt, 455 tripledes_cfb_decrypt, 382 456 std_finish, 383 457 TRAILER … … 529 603 return -1; 530 604 531 switch(crypt->algorithm) 532 { 533 case OPS_SA_CAST5: 534 CAST_cfb64_encrypt(in_, out_, count, 535 crypt->encrypt_key, crypt->iv, 536 (int *)&crypt->num, CAST_ENCRYPT); 537 break; 538 539 case OPS_SA_AES_128: 540 case OPS_SA_AES_256: 541 AES_cfb128_encrypt(in_,out_,count, 542 crypt->encrypt_key, crypt->iv, (int *)&crypt->num, AES_ENCRYPT); 543 break; 544 545 default: 546 fprintf(stderr,"ops_encrypt_se_ip: Implement support for %s\n", 547 ops_show_symmetric_algorithm(crypt->algorithm)); 548 assert(0); 549 } 550 605 crypt->cfb_encrypt(crypt, out_, in_, count); 606 607 // \todo test this number was encrypted 551 608 return count; 552 609 } … … 558 615 return -1; 559 616 560 switch(crypt->algorithm) 561 { 562 case OPS_SA_CAST5: 563 CAST_cfb64_encrypt(in_, out_, count, 564 crypt->encrypt_key, crypt->iv, 565 (int *)&crypt->num, CAST_DECRYPT); 566 break; 567 568 case OPS_SA_AES_128: 569 case OPS_SA_AES_256: 570 AES_cfb128_encrypt(in_,out_,count, 571 crypt->encrypt_key, crypt->iv, 572 (int *)&crypt->num, AES_DECRYPT); 573 break; 574 575 default: 576 fprintf(stderr,"ops_decrypt_se_ip: Implement support for %s\n", 577 ops_show_symmetric_algorithm(crypt->algorithm)); 578 assert(0); 579 } 580 617 crypt->cfb_decrypt(crypt, out_, in_, count); 618 619 // \todo check this number was in fact decrypted 581 620 return count; 582 621 } openpgpsdk/trunk/tests/test_crypto.c
r494 r500 23 23 } 24 24 25 static void test_ cfb(ops_symmetric_algorithm_t alg)25 static void test_ecb(ops_symmetric_algorithm_t alg) 26 26 { 27 27 // Used for trying low-level OpenSSL tests … … 87 87 88 88 #ifndef OPENSSL_NO_IDEA 89 static void test_ecb_idea() 90 { 91 test_ecb(OPS_SA_IDEA); 92 } 93 #endif 94 95 static void test_ecb_3des() 96 { 97 test_ecb(OPS_SA_TRIPLEDES); 98 } 99 100 static void test_ecb_cast() 101 { 102 test_ecb(OPS_SA_CAST5); 103 } 104 105 static void test_ecb_aes128() 106 { 107 test_ecb(OPS_SA_AES_128); 108 } 109 110 static void test_ecb_aes256() 111 { 112 test_ecb(OPS_SA_AES_256); 113 } 114 115 static void test_cfb(ops_symmetric_algorithm_t alg) 116 { 117 // Used for trying low-level OpenSSL tests 118 119 int verbose=0; 120 121 char *plaintext="This is a very long piece of text so that we can test that CFB mode works. It must be larger than the largest blocksize of the supported set of ciphers"; 122 const unsigned int sz_plaintext=strlen(plaintext+1); 123 124 ops_crypt_t crypt; 125 unsigned char *iv=NULL; 126 unsigned char *key=NULL; 127 128 unsigned char *out=NULL; 129 unsigned char *out2=NULL; 130 131 /* 132 * Initialise Crypt structure 133 * Empty IV, made-up key 134 */ 135 136 ops_crypt_any(&crypt, alg); 137 iv=ops_mallocz(crypt.blocksize); 138 key=ops_mallocz(crypt.keysize); 139 snprintf((char *)key, crypt.keysize, "MY CFB KEY"); 140 crypt.set_iv(&crypt, iv); 141 crypt.set_key(&crypt, key); 142 ops_encrypt_init(&crypt); 143 144 /* 145 * Create test buffers 146 */ 147 out=ops_mallocz(sz_plaintext); 148 out2=ops_mallocz(sz_plaintext); 149 150 crypt.cfb_encrypt(&crypt, out, plaintext, sz_plaintext); 151 152 /* 153 * Reset IV and decrypt 154 */ 155 156 crypt.set_iv(&crypt, iv); 157 ops_decrypt_init(&crypt); 158 crypt.cfb_decrypt(&crypt, out2, out, sz_plaintext); 159 CU_ASSERT(memcmp(plaintext, (char *)out2, sz_plaintext)==0); 160 161 if (verbose) 162 { 163 // plaintext 164 printf("\n"); 165 printf("plaintext: 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", 166 plaintext[0], plaintext[1], plaintext[2], plaintext[3], plaintext[4], plaintext[5], plaintext[6], plaintext[7]); 167 printf("plaintext: %c %c %c %c %c %c %c %c\n", 168 plaintext[0], plaintext[1], plaintext[2], plaintext[3], plaintext[4], plaintext[5], plaintext[6], plaintext[7]); 169 170 // encrypted 171 printf("encrypted: 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", 172 out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 173 printf("encrypted: %c %c %c %c %c %c %c %c\n", 174 out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 175 176 // decrypted 177 printf("decrypted: 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", 178 out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 179 printf("decrypted: %c %c %c %c %c %c %c %c\n", 180 out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 181 } 182 } 183 184 #ifdef LATER 185 #ifndef OPENSSL_NO_IDEA 89 186 static void test_cfb_idea() 90 187 { … … 97 194 test_cfb(OPS_SA_TRIPLEDES); 98 195 } 196 #endif 99 197 100 198 static void test_cfb_cast() … … 113 211 } 114 212 213 #ifdef LATER 115 214 static void test_rsa() 116 215 { … … 178 277 free(decrypted); 179 278 } 279 #endif 180 280 181 281 CU_pSuite suite_crypto() … … 189 289 // add tests to suite 190 290 291 /* ECB test */ 292 293 #ifndef OPENSSL_NO_IDEA 294 if (NULL == CU_add_test(suite, "Test ECB (IDEA)", test_ecb_idea)) 295 return NULL; 296 #endif 297 298 if (NULL == CU_add_test(suite, "Test ECB (TripleDES)", test_ecb_3des)) 299 return NULL; 300 301 if (NULL == CU_add_test(suite, "Test ECB (CAST)", test_ecb_cast)) 302 return NULL; 303 304 // test_one_ecb(OPS_SA_BLOWFISH); 305 306 if (NULL == CU_add_test(suite, "Test ECB (AES 128)", test_ecb_aes128)) 307 return NULL; 308 309 // test_one_ecb(OPS_SA_AES_192); 310 311 if (NULL == CU_add_test(suite, "Test ECB (AES 256)", test_ecb_aes256)) 312 return NULL; 313 314 // test_one_cfb(OPS_SA_TWOFISH); 315 316 /* CFB tests */ 317 318 #ifdef LATER 191 319 #ifndef OPENSSL_NO_IDEA 192 320 if (NULL == CU_add_test(suite, "Test CFB (IDEA)", test_cfb_idea)) … … 196 324 if (NULL == CU_add_test(suite, "Test CFB (TripleDES)", test_cfb_3des)) 197 325 return NULL; 326 #endif 198 327 199 328 if (NULL == CU_add_test(suite, "Test CFB (CAST)", test_cfb_cast)) … … 202 331 // test_one_cfb(OPS_SA_BLOWFISH); 203 332 204 if (NULL == CU_add_test(suite, "Test CFB AES 128", test_cfb_aes128))333 if (NULL == CU_add_test(suite, "Test CFB (AES 128)", test_cfb_aes128)) 205 334 return NULL; 206 335 207 336 // test_one_cfb(OPS_SA_AES_192); 208 337 209 if (NULL == CU_add_test(suite, "Test CFB AES 256", test_cfb_aes256))210 return NULL;338 if (NULL == CU_add_test(suite, "Test CFB (AES 256)", test_cfb_aes256)) 339 return NULL; 211 340 212 341 // test_one_cfb(OPS_SA_TWOFISH); … … 214 343 /* 215 344 */ 345 #ifdef LATER 216 346 if (NULL == CU_add_test(suite, "Test RSA", test_rsa)) 217 347 return NULL; 348 #endif 218 349 219 350 return suite;
