Changeset 661
- Timestamp:
- 06/08/09 13:02:36
- Files:
-
- openpgpsdk/trunk/include/openpgpsdk/crypto.h (modified) (1 diff)
- openpgpsdk/trunk/include/openpgpsdk/packet.h (modified) (1 diff)
- openpgpsdk/trunk/src/lib/openssl_crypto.c (modified) (1 diff)
- openpgpsdk/trunk/src/lib/packet-show.c (modified) (1 diff)
- openpgpsdk/trunk/src/lib/symmetric.c (modified) (5 diffs)
- openpgpsdk/trunk/tests/test_crypto.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/include/openpgpsdk/crypto.h
r629 r661 31 31 #include "packet-parse.h" 32 32 #include <openssl/dsa.h> 33 #include <openssl/opensslv.h> 34 #include <openssl/opensslconf.h> 35 36 #if OPENSSL_VERSION_NUMBER < 0x00908030L 37 # define OPENSSL_NO_CAMELLIA 38 #endif 33 39 34 40 #define OPS_MIN_HASH_SIZE 16 openpgpsdk/trunk/include/openpgpsdk/packet.h
r627 r661 421 421 OPS_SA_AES_256 =9, /*!< AES with 256-bit key */ 422 422 OPS_SA_TWOFISH =10, /*!< Twofish with 256-bit key (TWOFISH) */ 423 OPS_SA_CAMELLIA_128 =11, /*!< Camellia with 128-bit key */ 424 OPS_SA_CAMELLIA_192 =12, /*!< Camellia with 192-bit key */ 425 OPS_SA_CAMELLIA_256 =13, /*!< Camellia with 256-bit key */ 423 426 } ops_symmetric_algorithm_t; 424 427 openpgpsdk/trunk/src/lib/openssl_crypto.c
r660 r661 609 609 { 610 610 CRYPTO_cleanup_all_ex_data(); 611 ERR_remove_state(0); 611 // FIXME: what should we do instead (function is deprecated)? 612 // ERR_remove_state(0); 612 613 #ifdef DMALLOC 613 614 CRYPTO_mem_leaks_fp(stderr); openpgpsdk/trunk/src/lib/packet-show.c
r636 r661 213 213 { OPS_SA_AES_256, "AES (256-bit key)" }, 214 214 { OPS_SA_TWOFISH, "Twofish(256-bit key)" }, 215 { OPS_SA_CAMELLIA_128, "Camellia (128-bit key)" }, 216 { OPS_SA_CAMELLIA_192, "Camellia (192-bit key)" }, 217 { OPS_SA_CAMELLIA_256, "Camellia (256-bit key)" }, 215 218 { 0x00, NULL }, /* this is the end-of-array marker */ 216 219 }; openpgpsdk/trunk/src/lib/symmetric.c
r621 r661 25 25 #include <openssl/cast.h> 26 26 #ifndef OPENSSL_NO_IDEA 27 # include <openssl/idea.h>27 # include <openssl/idea.h> 28 28 #endif 29 29 #include <openssl/aes.h> 30 #ifndef OPENSSL_NO_CAMELLIA 31 # include <openssl/camellia.h> 32 #endif 30 33 #include <openssl/des.h> 31 34 #include "parse_local.h" … … 279 282 }; 280 283 284 #ifndef OPENSSL_NO_CAMELLIA 285 286 // CAMELLIA with 128-bit key 287 288 #define KEYBITS_CAMELLIA128 128 289 290 static void camellia128_init(ops_crypt_t *crypt) 291 { 292 if (crypt->encrypt_key) 293 free(crypt->encrypt_key); 294 crypt->encrypt_key=malloc(sizeof(CAMELLIA_KEY)); 295 if (Camellia_set_key(crypt->key,KEYBITS_CAMELLIA128,crypt->encrypt_key)) 296 fprintf(stderr,"camellia128_init: Error setting encrypt_key\n"); 297 298 if (crypt->decrypt_key) 299 free(crypt->decrypt_key); 300 crypt->decrypt_key=malloc(sizeof(CAMELLIA_KEY)); 301 if (Camellia_set_key(crypt->key,KEYBITS_CAMELLIA128,crypt->decrypt_key)) 302 fprintf(stderr,"camellia128_init: Error setting decrypt_key\n"); 303 } 304 305 static void camellia_block_encrypt(ops_crypt_t *crypt,void *out,const void *in) 306 { Camellia_encrypt(in,out,crypt->encrypt_key); } 307 308 static void camellia_block_decrypt(ops_crypt_t *crypt,void *out,const void *in) 309 { Camellia_decrypt(in,out,crypt->decrypt_key); } 310 311 static void camellia_cfb_encrypt(ops_crypt_t *crypt,void *out,const void *in, size_t count) 312 { 313 Camellia_cfb128_encrypt(in,out,count, 314 crypt->encrypt_key, crypt->iv, (int *)&crypt->num, 315 CAMELLIA_ENCRYPT); 316 } 317 318 static void camellia_cfb_decrypt(ops_crypt_t *crypt,void *out,const void *in, size_t count) 319 { 320 Camellia_cfb128_encrypt(in,out,count, 321 crypt->encrypt_key, crypt->iv, (int *)&crypt->num, 322 CAMELLIA_DECRYPT); 323 } 324 325 static const ops_crypt_t camellia128= 326 { 327 OPS_SA_CAMELLIA_128, 328 CAMELLIA_BLOCK_SIZE, 329 KEYBITS_CAMELLIA128/8, 330 std_set_iv, 331 std_set_key, 332 camellia128_init, 333 std_resync, 334 camellia_block_encrypt, 335 camellia_block_decrypt, 336 camellia_cfb_encrypt, 337 camellia_cfb_decrypt, 338 std_finish, 339 TRAILER 340 }; 341 342 // CAMELLIA with 192-bit key 343 344 #define KEYBITS_CAMELLIA192 192 345 346 static void camellia192_init(ops_crypt_t *crypt) 347 { 348 if (crypt->encrypt_key) 349 free(crypt->encrypt_key); 350 crypt->encrypt_key=malloc(sizeof(CAMELLIA_KEY)); 351 if (Camellia_set_key(crypt->key,KEYBITS_CAMELLIA192,crypt->encrypt_key)) 352 fprintf(stderr,"camellia192_init: Error setting encrypt_key\n"); 353 354 if (crypt->decrypt_key) 355 free(crypt->decrypt_key); 356 crypt->decrypt_key=malloc(sizeof(CAMELLIA_KEY)); 357 if (Camellia_set_key(crypt->key,KEYBITS_CAMELLIA192,crypt->decrypt_key)) 358 fprintf(stderr,"camellia192_init: Error setting decrypt_key\n"); 359 } 360 361 static const ops_crypt_t camellia192= 362 { 363 OPS_SA_CAMELLIA_192, 364 CAMELLIA_BLOCK_SIZE, 365 KEYBITS_CAMELLIA192/8, 366 std_set_iv, 367 std_set_key, 368 camellia192_init, 369 std_resync, 370 camellia_block_encrypt, 371 camellia_block_decrypt, 372 camellia_cfb_encrypt, 373 camellia_cfb_decrypt, 374 std_finish, 375 TRAILER 376 }; 377 378 // CAMELLIA with 256-bit key 379 380 #define KEYBITS_CAMELLIA256 256 381 382 static void camellia256_init(ops_crypt_t *crypt) 383 { 384 if (crypt->encrypt_key) 385 free(crypt->encrypt_key); 386 crypt->encrypt_key=malloc(sizeof(CAMELLIA_KEY)); 387 if (Camellia_set_key(crypt->key,KEYBITS_CAMELLIA256,crypt->encrypt_key)) 388 fprintf(stderr,"camellia256_init: Error setting encrypt_key\n"); 389 390 if (crypt->decrypt_key) 391 free(crypt->decrypt_key); 392 crypt->decrypt_key=malloc(sizeof(CAMELLIA_KEY)); 393 if (Camellia_set_key(crypt->key,KEYBITS_CAMELLIA256,crypt->decrypt_key)) 394 fprintf(stderr,"camellia256_init: Error setting decrypt_key\n"); 395 } 396 397 static const ops_crypt_t camellia256= 398 { 399 OPS_SA_CAMELLIA_256, 400 CAMELLIA_BLOCK_SIZE, 401 KEYBITS_CAMELLIA256/8, 402 std_set_iv, 403 std_set_key, 404 camellia256_init, 405 std_resync, 406 camellia_block_encrypt, 407 camellia_block_decrypt, 408 camellia_cfb_encrypt, 409 camellia_cfb_decrypt, 410 std_finish, 411 TRAILER 412 }; 413 414 #endif // ndef OPENSSL_NO_CAMELLIA 415 281 416 // Triple DES 282 417 … … 310 445 } 311 446 312 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) 447 static void tripledes_cfb_encrypt(ops_crypt_t *crypt ATTRIBUTE_UNUSED, 448 void *out ATTRIBUTE_UNUSED, 449 const void *in ATTRIBUTE_UNUSED, 450 size_t count ATTRIBUTE_UNUSED) 313 451 { 314 452 DES_key_schedule *keys=crypt->encrypt_key; 315 453 DES_ede3_cfb64_encrypt(in,out,count, 316 &keys[0],&keys[1],&keys[2], (DES_cblock *)crypt->iv, (int *)&crypt->num, 317 DES_ENCRYPT); 318 } 319 320 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) 454 &keys[0],&keys[1],&keys[2], 455 (DES_cblock *)crypt->iv, (int *)&crypt->num, 456 DES_ENCRYPT); 457 } 458 459 static void tripledes_cfb_decrypt(ops_crypt_t *crypt ATTRIBUTE_UNUSED, 460 void *out ATTRIBUTE_UNUSED, 461 const void *in ATTRIBUTE_UNUSED, 462 size_t count ATTRIBUTE_UNUSED) 321 463 { 322 464 DES_key_schedule *keys=crypt->encrypt_key; 323 465 DES_ede3_cfb64_encrypt(in,out,count, 324 &keys[0],&keys[1],&keys[2], (DES_cblock *)crypt->iv, (int *)&crypt->num, 325 DES_DECRYPT); 466 &keys[0],&keys[1],&keys[2], 467 (DES_cblock *)crypt->iv, (int *)&crypt->num, 468 DES_DECRYPT); 326 469 } 327 470 … … 361 504 return &aes256; 362 505 506 #ifndef OPENSSL_NO_CAMELLIA 507 case OPS_SA_CAMELLIA_128: 508 return &camellia128; 509 510 case OPS_SA_CAMELLIA_192: 511 return &camellia192; 512 513 case OPS_SA_CAMELLIA_256: 514 return &camellia256; 515 #endif // ndef OPENSSL_NO_CAMELLIA 516 363 517 case OPS_SA_TRIPLEDES: 364 518 return &tripledes; 365 519 366 520 default: 367 fprintf(stderr,"Unknown algorithm: %d (%s)\n",alg,ops_show_symmetric_algorithm(alg)); 521 fprintf(stderr,"Unknown algorithm: %d (%s)\n",alg, 522 ops_show_symmetric_algorithm(alg)); 368 523 // assert(0); 369 524 } … … 484 639 case OPS_SA_AES_128: 485 640 case OPS_SA_AES_256: 641 case OPS_SA_CAMELLIA_128: 642 case OPS_SA_CAMELLIA_192: 643 case OPS_SA_CAMELLIA_256: 486 644 case OPS_SA_CAST5: 487 645 case OPS_SA_TRIPLEDES: openpgpsdk/trunk/tests/test_crypto.c
r643 r661 143 143 } 144 144 145 #ifndef OPENSSL_NO_CAMELLIA 146 static void test_ecb_camellia128() 147 { 148 test_ecb(OPS_SA_CAMELLIA_128); 149 } 150 151 static void test_ecb_camellia192() 152 { 153 test_ecb(OPS_SA_CAMELLIA_192); 154 } 155 156 static void test_ecb_camellia256() 157 { 158 test_ecb(OPS_SA_CAMELLIA_256); 159 } 160 #endif // ndef OPENSSL_NO_CAMELLIA 161 145 162 static void test_cfb(ops_symmetric_algorithm_t alg) 146 163 { … … 166 183 if(!ops_crypt_any(&crypt, alg)) 167 184 { 168 fprintf(stderr,"Failed to initialise crypt struct: alg=%d (%s)\n",alg,ops_show_symmetric_algorithm(alg)); 185 fprintf(stderr,"Failed to initialise crypt struct: alg=%d (%s)\n", 186 alg,ops_show_symmetric_algorithm(alg)); 169 187 CU_FAIL("Failed to initialise crypt struct"); 170 188 return; … … 198 216 // plaintext 199 217 printf("\n"); 200 printf("plaintext: 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", 201 plaintext[0], plaintext[1], plaintext[2], plaintext[3], plaintext[4], plaintext[5], plaintext[6], plaintext[7]); 202 printf("plaintext: %c %c %c %c %c %c %c %c\n", 203 plaintext[0], plaintext[1], plaintext[2], plaintext[3], plaintext[4], plaintext[5], plaintext[6], plaintext[7]); 218 printf("plaintext: 0x%.2x 0x%.2x 0x%.2x 0x%.2x" 219 " 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", 220 plaintext[0], plaintext[1], plaintext[2], plaintext[3], 221 plaintext[4], plaintext[5], plaintext[6], plaintext[7]); 222 printf("plaintext: %c %c %c %c" 223 " %c %c %c %c\n", 224 plaintext[0], plaintext[1], plaintext[2], plaintext[3], 225 plaintext[4], plaintext[5], plaintext[6], plaintext[7]); 204 226 205 227 // encrypted 206 printf("encrypted: 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", 228 printf("encrypted: 0x%.2x 0x%.2x 0x%.2x 0x%.2x" 229 " 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", 207 230 out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 208 231 printf("encrypted: %c %c %c %c %c %c %c %c\n", … … 210 233 211 234 // decrypted 212 printf("decrypted: 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", 213 out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 235 printf("decrypted: 0x%.2x 0x%.2x 0x%.2x 0x%.2x" 236 " 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", 237 out2[0], out2[1], out2[2], out2[3], 238 out2[4], out2[5], out2[6], out2[7]); 214 239 printf("decrypted: %c %c %c %c %c %c %c %c\n", 215 out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 240 out2[0], out2[1], out2[2], out2[3], 241 out2[4], out2[5], out2[6], out2[7]); 216 242 } 217 243 } … … 246 272 } 247 273 274 static void test_cfb_camellia128() 275 { 276 test_cfb(OPS_SA_CAMELLIA_128); 277 } 278 279 static void test_cfb_camellia192() 280 { 281 test_cfb(OPS_SA_CAMELLIA_192); 282 } 283 284 static void test_cfb_camellia256() 285 { 286 test_cfb(OPS_SA_CAMELLIA_256); 287 } 288 248 289 static void test_dsa_verify() 249 290 { … … 252 293 // and signature generation when these are implemented in OPS. 253 294 254 //DSA *params=DSA_new();255 256 DSA *dsa = DSA_generate_parameters(100, NULL, 0, NULL, NULL, NULL, NULL);295 DSA *dsa = DSA_new(); 296 297 DSA_generate_parameters_ex(dsa, 100, NULL, 0, NULL, NULL, NULL); 257 298 CU_ASSERT(DSA_generate_key(dsa)==1); 258 299 … … 300 341 // test_one_cfb(OPS_SA_TWOFISH); 301 342 343 #ifndef OPENSSL_NO_CAMELLIA 344 if (NULL == CU_add_test(suite, "Test ECB (Camellia 128)", 345 test_ecb_camellia128)) 346 return NULL; 347 348 if (NULL == CU_add_test(suite, "Test ECB (Camellia 192)", 349 test_ecb_camellia192)) 350 return NULL; 351 352 if (NULL == CU_add_test(suite, "Test ECB (Camellia 256)", 353 test_ecb_camellia256)) 354 return NULL; 355 #endif // ndef OPENSSL_NO_CAMELLIA 356 357 302 358 /* CFB tests */ 303 359 … … 327 383 // test_one_cfb(OPS_SA_TWOFISH); 328 384 385 if (NULL == CU_add_test(suite, "Test CFB (Camellia 128)", 386 test_cfb_camellia128)) 387 return NULL; 388 389 if (NULL == CU_add_test(suite, "Test CFB (Camellia 192)", 390 test_cfb_camellia192)) 391 return NULL; 392 393 if (NULL == CU_add_test(suite, "Test CFB (Camellia 256)", 394 test_cfb_camellia256)) 395 return NULL; 396 329 397 if (NULL == CU_add_test(suite, "Test DSA Verify", test_dsa_verify)) 330 398 return NULL;
