Ignore:
Timestamp:
Mar 15, 2016 3:57:39 AM (8 years ago)
Author:
nanang
Message:

Close #1847: Upgraded libsrtp version to 1.5.4 and added support for AES-CM-256 crypto.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/third_party/srtp/crypto/cipher/cipher.c

    r2660 r5261  
    1111/* 
    1212 *       
    13  * Copyright (c) 2001-2006, Cisco Systems, Inc. 
     13 * Copyright (c) 2001-2006,2013 Cisco Systems, Inc. 
    1414 * All rights reserved. 
    1515 *  
     
    4545 */ 
    4646 
     47#ifdef HAVE_CONFIG_H 
     48    #include <config.h> 
     49#endif 
     50 
    4751#include "cipher.h" 
     52#include "crypto_types.h" 
    4853#include "rand_source.h"        /* used in invertibiltiy tests        */ 
    4954#include "alloc.h"              /* for crypto_alloc(), crypto_free()  */ 
     
    7277 
    7378/*  
    74  * cipher_type_self_test(ct) tests a cipher of type ct against test cases 
    75  * provided in an array of values of key, salt, xtd_seq_num_t, 
     79 * cipher_type_test(ct, test_data) tests a cipher of type ct against 
     80 * test cases provided in a list test_data of values of key, salt, iv, 
    7681 * plaintext, and ciphertext that is known to be good 
    7782 */ 
     
    8287 
    8388err_status_t 
    84 cipher_type_self_test(const cipher_type_t *ct) { 
    85   const cipher_test_case_t *test_case = ct->test_data; 
     89cipher_type_test(const cipher_type_t *ct, const cipher_test_case_t *test_data) { 
     90  const cipher_test_case_t *test_case = test_data; 
    8691  cipher_t *c; 
    8792  err_status_t status; 
    8893  uint8_t buffer[SELF_TEST_BUF_OCTETS]; 
    8994  uint8_t buffer2[SELF_TEST_BUF_OCTETS]; 
     95  int tag_len; 
    9096  unsigned int len; 
    9197  int i, j, case_num = 0; 
     
    106112   */   
    107113  while (test_case != NULL) { 
    108  
    109114    /* allocate cipher */ 
    110     status = cipher_type_alloc(ct, &c, test_case->key_length_octets); 
     115    status = cipher_type_alloc(ct, &c, test_case->key_length_octets, test_case->tag_length_octets); 
    111116    if (status) 
    112117      return status; 
     
    118123     
    119124    /* initialize cipher */ 
    120     status = cipher_init(c, test_case->key, direction_encrypt); 
     125    status = cipher_init(c, test_case->key); 
    121126    if (status) { 
    122127      cipher_dealloc(c); 
     
    137142 
    138143    /* set the initialization vector */ 
    139     status = cipher_set_iv(c, test_case->idx); 
     144    status = cipher_set_iv(c, test_case->idx, direction_encrypt); 
    140145    if (status) { 
    141146      cipher_dealloc(c); 
     
    143148    }  
    144149     
     150    if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 
     151        debug_print(mod_cipher, "IV:    %s", 
     152                    octet_string_hex_string(test_case->idx, 12)); 
     153 
     154        /* 
     155         * Set the AAD  
     156         */ 
     157        status = cipher_set_aad(c, test_case->aad,  
     158                                test_case->aad_length_octets); 
     159        if (status) { 
     160            cipher_dealloc(c); 
     161            return status; 
     162        }  
     163        debug_print(mod_cipher, "AAD:    %s", 
     164        octet_string_hex_string(test_case->aad,  
     165                                test_case->aad_length_octets)); 
     166    } 
     167 
    145168    /* encrypt */ 
    146169    len = test_case->plaintext_length_octets; 
     
    151174    } 
    152175     
     176    if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 
     177        /* 
     178         * Get the GCM tag 
     179         */ 
     180        status = cipher_get_tag(c, buffer + len, &tag_len); 
     181        if (status) { 
     182            cipher_dealloc(c); 
     183            return status; 
     184        } 
     185        len += tag_len; 
     186    } 
     187 
    153188    debug_print(mod_cipher, "ciphertext:   %s", 
    154189             octet_string_hex_string(buffer, 
     
    185220 
    186221    /* re-initialize cipher for decryption */ 
    187     status = cipher_init(c, test_case->key, direction_decrypt); 
     222    status = cipher_init(c, test_case->key); 
    188223    if (status) { 
    189224      cipher_dealloc(c); 
     
    204239 
    205240    /* set the initialization vector */ 
    206     status = cipher_set_iv(c, test_case->idx); 
     241    status = cipher_set_iv(c, test_case->idx, direction_decrypt); 
    207242    if (status) { 
    208243      cipher_dealloc(c); 
     
    210245    }  
    211246     
     247    if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 
     248        /* 
     249         * Set the AAD  
     250         */ 
     251        status = cipher_set_aad(c, test_case->aad,  
     252                                test_case->aad_length_octets); 
     253        if (status) { 
     254            cipher_dealloc(c); 
     255            return status; 
     256        }  
     257        debug_print(mod_cipher, "AAD:    %s", 
     258                    octet_string_hex_string(test_case->aad,  
     259                                            test_case->aad_length_octets)); 
     260    } 
     261 
    212262    /* decrypt */ 
    213263    len = test_case->ciphertext_length_octets; 
     
    261311 
    262312  /* allocate cipher, using paramaters from the first test case */ 
    263   test_case = ct->test_data; 
    264   status = cipher_type_alloc(ct, &c, test_case->key_length_octets); 
     313  test_case = test_data; 
     314  status = cipher_type_alloc(ct, &c, test_case->key_length_octets, test_case->tag_length_octets); 
    265315  if (status) 
    266316      return status; 
     
    270320  for (j=0; j < NUM_RAND_TESTS; j++) { 
    271321    unsigned length; 
    272     unsigned plaintext_len; 
     322    int plaintext_len; 
    273323    uint8_t key[MAX_KEY_LEN]; 
    274324    uint8_t  iv[MAX_KEY_LEN]; 
     
    298348         
    299349    /* initialize cipher */ 
    300     status = cipher_init(c, key, direction_encrypt); 
     350    status = cipher_init(c, key); 
    301351    if (status) { 
    302352      cipher_dealloc(c); 
     
    305355 
    306356    /* set initialization vector */ 
    307     status = cipher_set_iv(c, test_case->idx); 
     357    status = cipher_set_iv(c, test_case->idx, direction_encrypt); 
    308358    if (status) { 
    309359      cipher_dealloc(c); 
    310360      return status; 
    311361    }  
     362 
     363    if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 
     364        /* 
     365         * Set the AAD  
     366         */ 
     367        status = cipher_set_aad(c, test_case->aad,  
     368        test_case->aad_length_octets); 
     369        if (status) { 
     370            cipher_dealloc(c); 
     371            return status; 
     372        }  
     373        debug_print(mod_cipher, "AAD:    %s", 
     374                    octet_string_hex_string(test_case->aad,  
     375                                            test_case->aad_length_octets)); 
     376    } 
    312377 
    313378    /* encrypt buffer with cipher */ 
     
    317382      cipher_dealloc(c); 
    318383      return status; 
     384    } 
     385    if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 
     386        /* 
     387         * Get the GCM tag 
     388         */ 
     389        status = cipher_get_tag(c, buffer + length, &tag_len); 
     390        if (status) { 
     391            cipher_dealloc(c); 
     392            return status; 
     393        } 
     394        length += tag_len; 
    319395    } 
    320396    debug_print(mod_cipher, "ciphertext:   %s", 
     
    325401     * decrypt the ciphertext 
    326402     */ 
    327     status = cipher_init(c, key, direction_decrypt); 
    328     if (status) { 
    329       cipher_dealloc(c); 
    330       return status; 
    331     } 
    332     status = cipher_set_iv(c, test_case->idx); 
     403    status = cipher_init(c, key); 
     404    if (status) { 
     405      cipher_dealloc(c); 
     406      return status; 
     407    } 
     408    status = cipher_set_iv(c, test_case->idx, direction_decrypt); 
    333409    if (status) { 
    334410      cipher_dealloc(c); 
    335411      return status; 
    336412    }  
     413    if (c->algorithm == AES_128_GCM || c->algorithm == AES_256_GCM) { 
     414        /* 
     415         * Set the AAD  
     416         */ 
     417        status = cipher_set_aad(c, test_case->aad,  
     418                                test_case->aad_length_octets); 
     419        if (status) { 
     420            cipher_dealloc(c); 
     421            return status; 
     422        }  
     423        debug_print(mod_cipher, "AAD:    %s", 
     424                    octet_string_hex_string(test_case->aad,  
     425                                            test_case->aad_length_octets)); 
     426    } 
    337427    status = cipher_decrypt(c, buffer, &length); 
    338428    if (status) { 
     
    345435 
    346436    /* compare the resulting plaintext with the original one */ 
    347     if (length != plaintext_len) 
     437    if (length != plaintext_len) { 
    348438      return err_status_algo_fail; 
     439    } 
    349440    status = err_status_ok; 
    350441    for (i=0; i < plaintext_len; i++) 
     
    361452  } 
    362453 
    363   cipher_dealloc(c); 
     454  status = cipher_dealloc(c); 
     455  if (status) 
     456    return status; 
    364457 
    365458  return err_status_ok; 
    366459} 
    367460 
     461 
     462/*  
     463 * cipher_type_self_test(ct) performs cipher_type_test on ct's internal 
     464 * list of test data. 
     465 */ 
     466 
     467err_status_t 
     468cipher_type_self_test(const cipher_type_t *ct) { 
     469  return cipher_type_test(ct, ct->test_data); 
     470} 
    368471 
    369472/* 
     
    394497  timer = clock(); 
    395498  for(i=0; i < num_trials; i++, nonce.v32[3] = i) { 
    396     cipher_set_iv(c, &nonce); 
     499    cipher_set_iv(c, &nonce, direction_encrypt); 
    397500    cipher_encrypt(c, enc_buf, &len); 
    398501  } 
Note: See TracChangeset for help on using the changeset viewer.