Changeset 661

Show
Ignore:
Timestamp:
06/08/09 13:02:36
Author:
ben
Message:

Add Camellia support (might not work correctly if OPENSSL_NO_CAMELLIA
is defined), avoid deprecated functions.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/include/openpgpsdk/crypto.h

    r629 r661  
    3131#include "packet-parse.h" 
    3232#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 
    3339 
    3440#define OPS_MIN_HASH_SIZE       16 
  • openpgpsdk/trunk/include/openpgpsdk/packet.h

    r627 r661  
    421421    OPS_SA_AES_256      =9, /*!< AES with 256-bit key */ 
    422422    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 */ 
    423426    } ops_symmetric_algorithm_t; 
    424427 
  • openpgpsdk/trunk/src/lib/openssl_crypto.c

    r660 r661  
    609609    { 
    610610    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); 
    612613#ifdef DMALLOC 
    613614    CRYPTO_mem_leaks_fp(stderr); 
  • openpgpsdk/trunk/src/lib/packet-show.c

    r636 r661  
    213213    { OPS_SA_AES_256,           "AES (256-bit key)" }, 
    214214    { 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)" }, 
    215218    { 0x00,             NULL }, /* this is the end-of-array marker */ 
    216219    }; 
  • openpgpsdk/trunk/src/lib/symmetric.c

    r621 r661  
    2525#include <openssl/cast.h> 
    2626#ifndef OPENSSL_NO_IDEA 
    27 #include <openssl/idea.h> 
     27# include <openssl/idea.h> 
    2828#endif 
    2929#include <openssl/aes.h> 
     30#ifndef OPENSSL_NO_CAMELLIA 
     31# include <openssl/camellia.h> 
     32#endif 
    3033#include <openssl/des.h> 
    3134#include "parse_local.h" 
     
    279282    }; 
    280283 
     284#ifndef OPENSSL_NO_CAMELLIA 
     285 
     286// CAMELLIA with 128-bit key 
     287 
     288#define KEYBITS_CAMELLIA128 128 
     289 
     290static 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 
     305static void camellia_block_encrypt(ops_crypt_t *crypt,void *out,const void *in) 
     306    { Camellia_encrypt(in,out,crypt->encrypt_key); } 
     307 
     308static void camellia_block_decrypt(ops_crypt_t *crypt,void *out,const void *in) 
     309    { Camellia_decrypt(in,out,crypt->decrypt_key); } 
     310 
     311static 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 
     318static 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 
     325static 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 
     346static 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 
     361static 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 
     382static 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 
     397static 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 
    281416// Triple DES 
    282417 
     
    310445    } 
    311446 
    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) 
     447static 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) 
    313451    {  
    314452    DES_key_schedule *keys=crypt->encrypt_key; 
    315453    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 
     459static 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) 
    321463    {  
    322464    DES_key_schedule *keys=crypt->encrypt_key; 
    323465    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);  
    326469    } 
    327470 
     
    361504        return &aes256; 
    362505 
     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 
    363517    case OPS_SA_TRIPLEDES: 
    364518        return &tripledes; 
    365519 
    366520    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)); 
    368523        //      assert(0); 
    369524        } 
     
    484639    case OPS_SA_AES_128: 
    485640    case OPS_SA_AES_256: 
     641    case OPS_SA_CAMELLIA_128: 
     642    case OPS_SA_CAMELLIA_192: 
     643    case OPS_SA_CAMELLIA_256: 
    486644    case OPS_SA_CAST5: 
    487645    case OPS_SA_TRIPLEDES: 
  • openpgpsdk/trunk/tests/test_crypto.c

    r643 r661  
    143143    } 
    144144 
     145#ifndef OPENSSL_NO_CAMELLIA 
     146static void test_ecb_camellia128() 
     147    { 
     148    test_ecb(OPS_SA_CAMELLIA_128); 
     149    } 
     150 
     151static void test_ecb_camellia192() 
     152    { 
     153    test_ecb(OPS_SA_CAMELLIA_192); 
     154    } 
     155 
     156static void test_ecb_camellia256() 
     157    { 
     158    test_ecb(OPS_SA_CAMELLIA_256); 
     159    } 
     160#endif  // ndef OPENSSL_NO_CAMELLIA 
     161 
    145162static void test_cfb(ops_symmetric_algorithm_t alg) 
    146163    { 
     
    166183    if(!ops_crypt_any(&crypt, alg)) 
    167184        { 
    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)); 
    169187        CU_FAIL("Failed to initialise crypt struct"); 
    170188        return; 
     
    198216        // plaintext 
    199217        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]); 
    204226 
    205227        // 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",  
    207230               out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 
    208231        printf("encrypted: %c    %c    %c    %c      %c    %c    %c    %c\n",  
     
    210233 
    211234        // 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]); 
    214239        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]); 
    216242        } 
    217243    } 
     
    246272    } 
    247273 
     274static void test_cfb_camellia128() 
     275    { 
     276    test_cfb(OPS_SA_CAMELLIA_128); 
     277    } 
     278 
     279static void test_cfb_camellia192() 
     280    { 
     281    test_cfb(OPS_SA_CAMELLIA_192); 
     282    } 
     283 
     284static void test_cfb_camellia256() 
     285    { 
     286    test_cfb(OPS_SA_CAMELLIA_256); 
     287    } 
     288 
    248289static void test_dsa_verify() 
    249290    { 
     
    252293    // and signature generation when these are implemented in OPS. 
    253294 
    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); 
    257298    CU_ASSERT(DSA_generate_key(dsa)==1); 
    258299 
     
    300341    //    test_one_cfb(OPS_SA_TWOFISH); 
    301342 
     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 
    302358    /* CFB tests */ 
    303359 
     
    327383    //    test_one_cfb(OPS_SA_TWOFISH); 
    328384 
     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 
    329397    if (NULL == CU_add_test(suite, "Test DSA Verify", test_dsa_verify)) 
    330398        return NULL;