Changeset 500

Show
Ignore:
Timestamp:
09/04/07 17:20:22
Author:
rachel
Message:

Implemented generalised cfb_encrypt/decrypt functions in crypt structure.
Added CFB tests.

Files:

Legend:

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

    r495 r500  
    3838typedef void ops_crypt_block_decrypt_t(ops_crypt_t *crypt,void *out, 
    3939                                       const void *in); 
     40typedef void ops_crypt_cfb_encrypt_t(ops_crypt_t *crypt,void *out, 
     41                                     const void *in, size_t count); 
     42typedef void ops_crypt_cfb_decrypt_t(ops_crypt_t *crypt,void *out, 
     43                                     const void *in, size_t count); 
    4044typedef void ops_crypt_finish_t(ops_crypt_t *crypt); 
    4145 
     
    5054    ops_crypt_init_t *base_init; 
    5155    ops_crypt_resync_t *decrypt_resync; 
     56    // encrypt/decrypt one block  
    5257    ops_crypt_block_encrypt_t *block_encrypt; 
    5358    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 
    5464    ops_crypt_finish_t *decrypt_finish; 
    5565    unsigned char iv[OPS_MAX_BLOCK_SIZE]; 
  • openpgpsdk/trunk/src/advanced/adv_symmetric.c

    r495 r500  
    198198    crypt->encrypt_key=malloc(sizeof(CAST_KEY)); 
    199199    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); 
    200202    } 
    201203 
     
    205207static void cast5_block_decrypt(ops_crypt_t *crypt,void *out,const void *in) 
    206208    { CAST_ecb_encrypt(in,out,crypt->encrypt_key,CAST_DECRYPT); } 
     209 
     210static 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 
     217static 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    } 
    207223 
    208224#define TRAILER         "","","","",0,NULL,NULL 
     
    219235    cast5_block_encrypt, 
    220236    cast5_block_decrypt, 
     237    cast5_cfb_encrypt, 
     238    cast5_cfb_decrypt, 
    221239    std_finish, 
    222240    TRAILER 
     
    247265static void idea_block_decrypt(ops_crypt_t *crypt,void *out,const void *in) 
    248266    { idea_ecb_encrypt(in,out,crypt->decrypt_key); } 
     267 
     268static 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 
     275static 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    } 
    249281 
    250282static const ops_crypt_t idea= 
     
    259291    idea_block_encrypt, 
    260292    idea_block_decrypt, 
     293    idea_cfb_encrypt, 
     294    idea_cfb_decrypt, 
    261295    std_finish, 
    262296    TRAILER 
     
    288322static void aes_block_decrypt(ops_crypt_t *crypt,void *out,const void *in) 
    289323    { AES_decrypt(in,out,crypt->decrypt_key); } 
     324 
     325static 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 
     332static 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    } 
    290338 
    291339static const ops_crypt_t aes128= 
     
    300348    aes_block_encrypt, 
    301349    aes_block_decrypt, 
     350    aes_cfb_encrypt, 
     351    aes_cfb_decrypt, 
    302352    std_finish, 
    303353    TRAILER 
     
    334384    aes_block_encrypt, 
    335385    aes_block_decrypt, 
     386    aes_cfb_encrypt, 
     387    aes_cfb_decrypt, 
    336388    std_finish, 
    337389    TRAILER 
     
    367419 
    368420    DES_ecb3_encrypt((void *)in,out,&keys[0],&keys[1],&keys[2],DES_DECRYPT); 
     421    } 
     422 
     423static 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 
     433static 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    */ 
    369441    } 
    370442 
     
    380452    tripledes_block_encrypt, 
    381453    tripledes_block_decrypt, 
     454    tripledes_cfb_encrypt, 
     455    tripledes_cfb_decrypt, 
    382456    std_finish, 
    383457    TRAILER 
     
    529603        return -1; 
    530604 
    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 
    551608    return count; 
    552609    } 
     
    558615        return -1; 
    559616 
    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 
    581620    return count; 
    582621    } 
  • openpgpsdk/trunk/tests/test_crypto.c

    r494 r500  
    2323    } 
    2424 
    25 static void test_cfb(ops_symmetric_algorithm_t alg) 
     25static void test_ecb(ops_symmetric_algorithm_t alg) 
    2626    { 
    2727    // Used for trying low-level OpenSSL tests 
     
    8787 
    8888#ifndef OPENSSL_NO_IDEA 
     89static void test_ecb_idea() 
     90    { 
     91    test_ecb(OPS_SA_IDEA); 
     92    } 
     93#endif 
     94 
     95static void test_ecb_3des() 
     96    { 
     97    test_ecb(OPS_SA_TRIPLEDES); 
     98    } 
     99 
     100static void test_ecb_cast() 
     101    { 
     102    test_ecb(OPS_SA_CAST5); 
     103    } 
     104 
     105static void test_ecb_aes128() 
     106    { 
     107    test_ecb(OPS_SA_AES_128); 
     108    } 
     109 
     110static void test_ecb_aes256() 
     111    { 
     112    test_ecb(OPS_SA_AES_256); 
     113    } 
     114 
     115static 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 
    89186static void test_cfb_idea() 
    90187    { 
     
    97194    test_cfb(OPS_SA_TRIPLEDES); 
    98195    } 
     196#endif 
    99197 
    100198static void test_cfb_cast() 
     
    113211    } 
    114212 
     213#ifdef LATER 
    115214static void test_rsa() 
    116215    { 
     
    178277    free(decrypted); 
    179278    } 
     279#endif 
    180280 
    181281CU_pSuite suite_crypto() 
     
    189289    // add tests to suite 
    190290     
     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 
    191319#ifndef OPENSSL_NO_IDEA 
    192320    if (NULL == CU_add_test(suite, "Test CFB (IDEA)", test_cfb_idea)) 
     
    196324    if (NULL == CU_add_test(suite, "Test CFB (TripleDES)", test_cfb_3des)) 
    197325            return NULL; 
     326#endif 
    198327 
    199328    if (NULL == CU_add_test(suite, "Test CFB (CAST)", test_cfb_cast)) 
     
    202331    //    test_one_cfb(OPS_SA_BLOWFISH); 
    203332 
    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)) 
    205334            return NULL; 
    206335 
    207336    //    test_one_cfb(OPS_SA_AES_192); 
    208337 
    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; 
    211340 
    212341    //    test_one_cfb(OPS_SA_TWOFISH); 
     
    214343    /* 
    215344     */ 
     345#ifdef LATER 
    216346    if (NULL == CU_add_test(suite, "Test RSA", test_rsa)) 
    217347            return NULL; 
     348#endif 
    218349 
    219350    return suite;