Ignore:
Timestamp:
Jul 4, 2017 5:22:51 AM (7 years ago)
Author:
nanang
Message:

Close #1993: Updated bundled libSRTP version to 2.1.0.

File:
1 edited

Legend:

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

    r5261 r5614  
    99/* 
    1010 *       
    11  * Copyright (c) 2001-2006, Cisco Systems, Inc. 
     11 * Copyright (c) 2001-2017, Cisco Systems, Inc. 
    1212 * All rights reserved. 
    1313 *  
     
    4343 */ 
    4444 
     45// Leave this as the top level import. Ensures the existence of defines 
     46#include "config.h" 
    4547 
    4648#include "srtp_priv.h" 
     49#include "crypto_types.h" 
     50#include "err.h" 
    4751#include "ekt.h"             /* for SRTP Encrypted Key Transport */ 
    48 #include "alloc.h"           /* for crypto_alloc()          */ 
     52#include "alloc.h"           /* for srtp_crypto_alloc()          */ 
    4953#ifdef OPENSSL 
    5054#include "aes_gcm_ossl.h"    /* for AES GCM mode  */ 
     55# ifdef OPENSSL_KDF 
     56# include <openssl/kdf.h> 
     57# include "aes_icm_ossl.h"    /* for AES GCM mode  */ 
     58# endif 
    5159#endif 
    5260 
    53 #ifndef SRTP_KERNEL 
    54 # include <limits.h> 
    55 # ifdef HAVE_NETINET_IN_H 
    56 #  include <netinet/in.h> 
    57 # elif defined(HAVE_WINSOCK2_H) 
    58 #  include <winsock2.h> 
    59 # endif 
    60 #endif /* ! SRTP_KERNEL */ 
     61#include <limits.h> 
     62#ifdef HAVE_NETINET_IN_H 
     63# include <netinet/in.h> 
     64#elif defined(HAVE_WINSOCK2_H) 
     65# include <winsock2.h> 
     66#endif 
    6167 
    6268 
    6369/* the debug module for srtp */ 
    6470 
    65 debug_module_t mod_srtp = { 
     71srtp_debug_module_t mod_srtp = { 
    6672  0,                  /* debugging is off by default */ 
    6773  "srtp"              /* printable name for module   */ 
     
    7480#define octets_in_rtp_extn_hdr 4 
    7581 
    76 static err_status_t 
     82static srtp_err_status_t 
    7783srtp_validate_rtp_header(void *rtp_hdr, int *pkt_octet_len) { 
    7884  srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr; 
     85  int rtp_header_len = octets_in_rtp_header + 4 * hdr->cc; 
     86 
     87  if (*pkt_octet_len < octets_in_rtp_header) 
     88    return srtp_err_status_bad_param; 
    7989 
    8090  /* Check RTP header length */ 
    81   int rtp_header_len = octets_in_rtp_header + 4 * hdr->cc; 
    8291  if (hdr->x == 1) 
    8392    rtp_header_len += octets_in_rtp_extn_hdr; 
    8493 
    8594  if (*pkt_octet_len < rtp_header_len) 
    86     return err_status_bad_param; 
     95    return srtp_err_status_bad_param; 
    8796 
    8897  /* Verifing profile length. */ 
     
    94103    /* profile length counts the number of 32-bit words */ 
    95104    if (*pkt_octet_len < rtp_header_len) 
    96       return err_status_bad_param; 
    97   } 
    98   return err_status_ok; 
     105      return srtp_err_status_bad_param; 
     106  } 
     107  return srtp_err_status_ok; 
    99108} 
    100109 
     
    138147} 
    139148 
    140 err_status_t 
     149/* Release (maybe partially allocated) stream. */ 
     150static void 
     151srtp_stream_free(srtp_stream_ctx_t *str) { 
     152  unsigned int i = 0; 
     153  srtp_session_keys_t *session_keys = NULL; 
     154 
     155  for (i = 0; i < str->num_master_keys; i++) { 
     156    session_keys = &str->session_keys[i]; 
     157 
     158    if (session_keys->rtp_xtn_hdr_cipher) { 
     159      srtp_cipher_dealloc(session_keys->rtp_xtn_hdr_cipher); 
     160    } 
     161 
     162    if (session_keys->rtcp_cipher) { 
     163      srtp_cipher_dealloc(session_keys->rtcp_cipher); 
     164    } 
     165 
     166    if (session_keys->rtcp_auth) { 
     167      srtp_auth_dealloc(session_keys->rtcp_auth); 
     168    } 
     169 
     170    if (session_keys->rtp_cipher) { 
     171      srtp_cipher_dealloc(session_keys->rtp_cipher); 
     172    } 
     173 
     174    if (session_keys->rtp_auth) { 
     175      srtp_auth_dealloc(session_keys->rtp_auth); 
     176    } 
     177 
     178    if (session_keys->mki_id) { 
     179      srtp_crypto_free(session_keys->mki_id); 
     180    } 
     181 
     182    if (session_keys->limit) { 
     183      srtp_crypto_free(session_keys->limit); 
     184    } 
     185  } 
     186 
     187  srtp_crypto_free(str->session_keys); 
     188 
     189  if (str->enc_xtn_hdr) { 
     190    srtp_crypto_free(str->enc_xtn_hdr); 
     191  } 
     192 
     193  srtp_crypto_free(str); 
     194} 
     195 
     196srtp_err_status_t 
    141197srtp_stream_alloc(srtp_stream_ctx_t **str_ptr, 
    142198                  const srtp_policy_t *p) { 
    143199  srtp_stream_ctx_t *str; 
    144   err_status_t stat; 
     200  srtp_err_status_t stat; 
     201  unsigned int i = 0; 
     202  srtp_session_keys_t *session_keys = NULL; 
    145203 
    146204  /* 
     
    153211 
    154212  /* allocate srtp stream and set str_ptr */ 
    155   str = (srtp_stream_ctx_t *) crypto_alloc(sizeof(srtp_stream_ctx_t)); 
     213  str = (srtp_stream_ctx_t *) srtp_crypto_alloc(sizeof(srtp_stream_ctx_t)); 
    156214  if (str == NULL) 
    157     return err_status_alloc_fail; 
    158   *str_ptr = str;   
    159    
    160   /* allocate cipher */ 
    161   stat = crypto_kernel_alloc_cipher(p->rtp.cipher_type,  
    162                                     &str->rtp_cipher,  
    163                                     p->rtp.cipher_key_len, 
    164                                     p->rtp.auth_tag_len);  
     215    return srtp_err_status_alloc_fail; 
     216 
     217  memset(str, 0, sizeof(srtp_stream_ctx_t)); 
     218  *str_ptr = str; 
     219 
     220  /* To keep backwards API compatible if someone is using multiple master 
     221   * keys then key should be set to NULL 
     222   */ 
     223  if (p->key != NULL) { 
     224      str->num_master_keys = 1; 
     225  } else { 
     226      str->num_master_keys = p->num_master_keys; 
     227  } 
     228 
     229  str->session_keys = (srtp_session_keys_t *)srtp_crypto_alloc( 
     230      sizeof(srtp_session_keys_t) * str->num_master_keys); 
     231 
     232  if (str->session_keys == NULL) { 
     233      srtp_stream_free(str); 
     234      return srtp_err_status_alloc_fail; 
     235  } 
     236 
     237  memset(str->session_keys, 0, sizeof(srtp_session_keys_t) * str->num_master_keys); 
     238 
     239  for (i = 0; i < str->num_master_keys; i++) { 
     240    session_keys = &str->session_keys[i]; 
     241 
     242    /* allocate cipher */  
     243    stat = srtp_crypto_kernel_alloc_cipher(p->rtp.cipher_type, 
     244                                           &session_keys->rtp_cipher, 
     245                                           p->rtp.cipher_key_len, 
     246                                           p->rtp.auth_tag_len); 
     247    if (stat) { 
     248      srtp_stream_free(str); 
     249      return stat; 
     250    } 
     251 
     252    /* allocate auth function */ 
     253    stat = srtp_crypto_kernel_alloc_auth(p->rtp.auth_type, 
     254                                         &session_keys->rtp_auth, 
     255                                         p->rtp.auth_key_len, 
     256                                         p->rtp.auth_tag_len); 
     257    if (stat) { 
     258      srtp_stream_free(str); 
     259      return stat; 
     260    } 
     261 
     262    /* 
     263     * ...and now the RTCP-specific initialization - first, allocate 
     264     * the cipher  
     265     */ 
     266    stat = srtp_crypto_kernel_alloc_cipher(p->rtcp.cipher_type, 
     267                                           &session_keys->rtcp_cipher, 
     268                                           p->rtcp.cipher_key_len, 
     269                                           p->rtcp.auth_tag_len); 
     270    if (stat) { 
     271      srtp_stream_free(str); 
     272      return stat; 
     273    } 
     274 
     275    /* allocate auth function */ 
     276    stat = srtp_crypto_kernel_alloc_auth(p->rtcp.auth_type, 
     277                                         &session_keys->rtcp_auth, 
     278                                         p->rtcp.auth_key_len, 
     279                                         p->rtcp.auth_tag_len); 
     280    if (stat) { 
     281      srtp_stream_free(str); 
     282      return stat; 
     283    } 
     284    
     285    session_keys->mki_id = NULL; 
     286 
     287    /* allocate key limit structure */ 
     288    session_keys->limit = (srtp_key_limit_ctx_t*) srtp_crypto_alloc(sizeof(srtp_key_limit_ctx_t)); 
     289    if (session_keys->limit == NULL) { 
     290        srtp_stream_free(str); 
     291        return srtp_err_status_alloc_fail; 
     292    } 
     293  } 
     294 
     295  /* allocate ekt data associated with stream */ 
     296  stat = srtp_ekt_alloc(&str->ekt, p->ekt); 
    165297  if (stat) { 
    166     crypto_free(str); 
     298    srtp_stream_free(str); 
    167299    return stat; 
    168300  } 
    169301 
    170   /* allocate auth function */ 
    171   stat = crypto_kernel_alloc_auth(p->rtp.auth_type,  
    172                                   &str->rtp_auth, 
    173                                   p->rtp.auth_key_len,  
    174                                   p->rtp.auth_tag_len);  
    175   if (stat) { 
    176     cipher_dealloc(str->rtp_cipher); 
    177     crypto_free(str); 
    178     return stat; 
    179   } 
    180    
    181   /* allocate key limit structure */ 
    182   str->limit = (key_limit_ctx_t*) crypto_alloc(sizeof(key_limit_ctx_t)); 
    183   if (str->limit == NULL) { 
    184     auth_dealloc(str->rtp_auth); 
    185     cipher_dealloc(str->rtp_cipher); 
    186     crypto_free(str);  
    187     return err_status_alloc_fail; 
    188   } 
    189  
    190   /* 
    191    * ...and now the RTCP-specific initialization - first, allocate 
    192    * the cipher  
    193    */ 
    194   stat = crypto_kernel_alloc_cipher(p->rtcp.cipher_type,  
    195                                     &str->rtcp_cipher,  
    196                                     p->rtcp.cipher_key_len,  
    197                                     p->rtcp.auth_tag_len);  
    198   if (stat) { 
    199     auth_dealloc(str->rtp_auth); 
    200     cipher_dealloc(str->rtp_cipher); 
    201     crypto_free(str->limit); 
    202     crypto_free(str); 
    203     return stat; 
    204   } 
    205  
    206   /* allocate auth function */ 
    207   stat = crypto_kernel_alloc_auth(p->rtcp.auth_type,  
    208                                   &str->rtcp_auth, 
    209                                   p->rtcp.auth_key_len,  
    210                                   p->rtcp.auth_tag_len);  
    211   if (stat) { 
    212     cipher_dealloc(str->rtcp_cipher); 
    213     auth_dealloc(str->rtp_auth); 
    214     cipher_dealloc(str->rtp_cipher); 
    215     crypto_free(str->limit); 
    216     crypto_free(str); 
    217    return stat; 
    218   }   
    219  
    220   /* allocate ekt data associated with stream */ 
    221   stat = ekt_alloc(&str->ekt, p->ekt); 
    222   if (stat) { 
    223     auth_dealloc(str->rtcp_auth); 
    224     cipher_dealloc(str->rtcp_cipher); 
    225     auth_dealloc(str->rtp_auth); 
    226     cipher_dealloc(str->rtp_cipher); 
    227     crypto_free(str->limit); 
    228     crypto_free(str); 
    229    return stat;     
    230   } 
    231  
    232   return err_status_ok; 
    233 } 
    234  
    235 err_status_t 
    236 srtp_stream_dealloc(srtp_t session, srtp_stream_ctx_t *stream) {  
    237   err_status_t status; 
    238    
     302  if (p->enc_xtn_hdr && p->enc_xtn_hdr_count > 0) { 
     303    srtp_cipher_type_id_t enc_xtn_hdr_cipher_type; 
     304    int enc_xtn_hdr_cipher_key_len; 
     305 
     306    str->enc_xtn_hdr = (int*) srtp_crypto_alloc(p->enc_xtn_hdr_count * sizeof(p->enc_xtn_hdr[0])); 
     307    if (!str->enc_xtn_hdr) { 
     308      srtp_stream_free(str); 
     309      return srtp_err_status_alloc_fail; 
     310    } 
     311    memcpy(str->enc_xtn_hdr, p->enc_xtn_hdr, p->enc_xtn_hdr_count * sizeof(p->enc_xtn_hdr[0])); 
     312    str->enc_xtn_hdr_count = p->enc_xtn_hdr_count; 
     313 
     314    /* For GCM ciphers, the corresponding ICM cipher is used for header extensions encryption. */ 
     315    switch (p->rtp.cipher_type) { 
     316    case SRTP_AES_GCM_128: 
     317      enc_xtn_hdr_cipher_type = SRTP_AES_ICM_128; 
     318      enc_xtn_hdr_cipher_key_len = SRTP_AES_ICM_128_KEY_LEN_WSALT; 
     319      break; 
     320    case SRTP_AES_GCM_256: 
     321      enc_xtn_hdr_cipher_type = SRTP_AES_ICM_256; 
     322      enc_xtn_hdr_cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT; 
     323      break; 
     324    default: 
     325      enc_xtn_hdr_cipher_type = p->rtp.cipher_type; 
     326      enc_xtn_hdr_cipher_key_len = p->rtp.cipher_key_len; 
     327      break; 
     328    } 
     329 
     330    for (i = 0; i < str->num_master_keys; i++) { 
     331      session_keys = &str->session_keys[i]; 
     332 
     333      /* allocate cipher for extensions header encryption */ 
     334      stat = srtp_crypto_kernel_alloc_cipher(enc_xtn_hdr_cipher_type, 
     335                                             &session_keys->rtp_xtn_hdr_cipher, 
     336                                             enc_xtn_hdr_cipher_key_len, 
     337                                             0); 
     338      if (stat) { 
     339        srtp_stream_free(str); 
     340        return stat; 
     341      } 
     342    } 
     343  } else { 
     344    for (i = 0; i < str->num_master_keys; i++) { 
     345      session_keys = &str->session_keys[i]; 
     346      session_keys->rtp_xtn_hdr_cipher = NULL; 
     347    } 
     348 
     349    str->enc_xtn_hdr = NULL; 
     350    str->enc_xtn_hdr_count = 0; 
     351  } 
     352 
     353  return srtp_err_status_ok; 
     354} 
     355 
     356srtp_err_status_t 
     357srtp_stream_dealloc(srtp_stream_ctx_t *stream, srtp_stream_ctx_t *stream_template) { 
     358  srtp_err_status_t status; 
     359  unsigned int i = 0; 
     360  srtp_session_keys_t *session_keys = NULL; 
     361  srtp_session_keys_t *template_session_keys = NULL; 
     362 
    239363  /* 
    240364   * we use a conservative deallocation strategy - if any deallocation 
     
    242366   * anything else 
    243367   */ 
    244  
    245   /* deallocate cipher, if it is not the same as that in template */ 
    246   if (session->stream_template 
    247       && stream->rtp_cipher == session->stream_template->rtp_cipher) { 
    248     /* do nothing */ 
     368  for ( i = 0; i < stream->num_master_keys; i++) { 
     369    session_keys = &stream->session_keys[i]; 
     370 
     371    if (stream_template) { 
     372      template_session_keys = &stream_template->session_keys[i]; 
     373    } else { 
     374      template_session_keys = NULL; 
     375    } 
     376 
     377    /* deallocate cipher, if it is not the same as that in template */ 
     378    if (template_session_keys 
     379        && session_keys->rtp_cipher == template_session_keys->rtp_cipher) { 
     380       /* do nothing */ 
     381    } else { 
     382       status = srtp_cipher_dealloc(session_keys->rtp_cipher); 
     383       if (status) 
     384         return status; 
     385    } 
     386 
     387    /* deallocate auth function, if it is not the same as that in template */ 
     388    if (template_session_keys 
     389        && session_keys->rtp_auth == template_session_keys->rtp_auth) { 
     390       /* do nothing */ 
     391    } else { 
     392      status = srtp_auth_dealloc(session_keys->rtp_auth); 
     393      if (status) 
     394        return status; 
     395    } 
     396 
     397    if (template_session_keys 
     398        && session_keys->rtp_xtn_hdr_cipher == template_session_keys->rtp_xtn_hdr_cipher) { 
     399       /* do nothing */ 
     400    } else if (session_keys->rtp_xtn_hdr_cipher) { 
     401      status = srtp_cipher_dealloc(session_keys->rtp_xtn_hdr_cipher); 
     402      if (status) 
     403        return status; 
     404    } 
     405 
     406    /* 
     407     * deallocate rtcp cipher, if it is not the same as that in 
     408     * template 
     409     */ 
     410    if (template_session_keys 
     411        && session_keys->rtcp_cipher == template_session_keys->rtcp_cipher) { 
     412      /* do nothing */ 
     413    } else { 
     414      status = srtp_cipher_dealloc(session_keys->rtcp_cipher); 
     415      if (status) 
     416        return status; 
     417    } 
     418 
     419    /* 
     420     * deallocate rtcp auth function, if it is not the same as that in 
     421     * template 
     422     */ 
     423    if (template_session_keys 
     424        && session_keys->rtcp_auth == template_session_keys->rtcp_auth) { 
     425      /* do nothing */ 
     426    } else { 
     427      status = srtp_auth_dealloc(session_keys->rtcp_auth); 
     428      if (status) 
     429        return status; 
     430    } 
     431 
     432    /* 
     433     * zeroize the salt value 
     434     */ 
     435    octet_string_set_to_zero(session_keys->salt, SRTP_AEAD_SALT_LEN); 
     436    octet_string_set_to_zero(session_keys->c_salt, SRTP_AEAD_SALT_LEN); 
     437 
     438    if (session_keys->mki_id) { 
     439      octet_string_set_to_zero(session_keys->mki_id, session_keys->mki_size); 
     440      srtp_crypto_free(session_keys->mki_id); 
     441      session_keys->mki_id = NULL; 
     442    } 
     443 
     444    /* deallocate key usage limit, if it is not the same as that in template */ 
     445    if (template_session_keys 
     446        && session_keys->limit == template_session_keys->limit) { 
     447        /* do nothing */ 
     448    } else { 
     449      srtp_crypto_free(session_keys->limit); 
     450    } 
     451 
     452  } 
     453 
     454  if (stream_template 
     455      && stream->session_keys == stream_template->session_keys) { 
     456      /* do nothing */ 
    249457  } else { 
    250     status = cipher_dealloc(stream->rtp_cipher);  
    251     if (status)  
    252       return status; 
    253   } 
    254  
    255   /* deallocate auth function, if it is not the same as that in template */ 
    256   if (session->stream_template 
    257       && stream->rtp_auth == session->stream_template->rtp_auth) { 
    258     /* do nothing */ 
    259   } else { 
    260     status = auth_dealloc(stream->rtp_auth); 
    261     if (status) 
    262       return status; 
    263   } 
    264  
    265   /* deallocate key usage limit, if it is not the same as that in template */ 
    266   if (session->stream_template 
    267       && stream->limit == session->stream_template->limit) { 
    268     /* do nothing */ 
    269   } else { 
    270     crypto_free(stream->limit); 
    271   }    
    272  
    273   /*  
    274    * deallocate rtcp cipher, if it is not the same as that in 
    275    * template  
    276    */ 
    277   if (session->stream_template 
    278       && stream->rtcp_cipher == session->stream_template->rtcp_cipher) { 
    279     /* do nothing */ 
    280   } else { 
    281     status = cipher_dealloc(stream->rtcp_cipher);  
    282     if (status)  
    283       return status; 
    284   } 
    285  
    286   /* 
    287    * deallocate rtcp auth function, if it is not the same as that in 
    288    * template  
    289    */ 
    290   if (session->stream_template 
    291       && stream->rtcp_auth == session->stream_template->rtcp_auth) { 
    292     /* do nothing */ 
    293   } else { 
    294     status = auth_dealloc(stream->rtcp_auth); 
    295     if (status) 
    296       return status; 
    297   } 
    298  
    299   status = rdbx_dealloc(&stream->rtp_rdbx); 
     458    srtp_crypto_free(stream->session_keys); 
     459  } 
     460 
     461  status = srtp_rdbx_dealloc(&stream->rtp_rdbx); 
    300462  if (status) 
    301463    return status; 
     
    303465  /* DAM - need to deallocate EKT here */ 
    304466 
    305   /* 
    306    * zeroize the salt value 
    307    */ 
    308   memset(stream->salt, 0, SRTP_AEAD_SALT_LEN); 
    309   memset(stream->c_salt, 0, SRTP_AEAD_SALT_LEN); 
    310  
    311    
     467  if (stream_template 
     468      && stream->enc_xtn_hdr == stream_template->enc_xtn_hdr) { 
     469    /* do nothing */ 
     470  } else if (stream->enc_xtn_hdr) { 
     471    srtp_crypto_free(stream->enc_xtn_hdr); 
     472  } 
     473 
    312474  /* deallocate srtp stream context */ 
    313   crypto_free(stream); 
    314  
    315   return err_status_ok; 
     475  srtp_crypto_free(stream); 
     476 
     477  return srtp_err_status_ok; 
    316478} 
    317479 
     
    325487 */ 
    326488 
    327 err_status_t 
     489srtp_err_status_t 
    328490srtp_stream_clone(const srtp_stream_ctx_t *stream_template,  
    329491                  uint32_t ssrc,  
    330492                  srtp_stream_ctx_t **str_ptr) { 
    331   err_status_t status; 
     493  srtp_err_status_t status; 
    332494  srtp_stream_ctx_t *str; 
    333  
    334   debug_print(mod_srtp, "cloning stream (SSRC: 0x%08x)", ssrc); 
     495  unsigned int i = 0; 
     496  srtp_session_keys_t *session_keys = NULL; 
     497  const srtp_session_keys_t *template_session_keys = NULL; 
     498 
     499  debug_print(mod_srtp, "cloning stream (SSRC: 0x%08x)", ntohl(ssrc)); 
    335500 
    336501  /* allocate srtp stream and set str_ptr */ 
    337   str = (srtp_stream_ctx_t *) crypto_alloc(sizeof(srtp_stream_ctx_t)); 
     502  str = (srtp_stream_ctx_t *) srtp_crypto_alloc(sizeof(srtp_stream_ctx_t)); 
    338503  if (str == NULL) 
    339     return err_status_alloc_fail; 
     504    return srtp_err_status_alloc_fail; 
    340505  *str_ptr = str;   
    341506 
    342   /* set cipher and auth pointers to those of the template */ 
    343   str->rtp_cipher  = stream_template->rtp_cipher; 
    344   str->rtp_auth    = stream_template->rtp_auth; 
    345   str->rtcp_cipher = stream_template->rtcp_cipher; 
    346   str->rtcp_auth   = stream_template->rtcp_auth; 
    347  
    348   /* set key limit to point to that of the template */ 
    349   status = key_limit_clone(stream_template->limit, &str->limit); 
    350   if (status) {  
    351     crypto_free(*str_ptr); 
     507  str->num_master_keys = stream_template->num_master_keys; 
     508  str->session_keys = (srtp_session_keys_t *)srtp_crypto_alloc( 
     509      sizeof(srtp_session_keys_t) * str->num_master_keys); 
     510 
     511  if (str->session_keys == NULL) { 
     512    srtp_crypto_free(*str_ptr); 
     513    *str_ptr = NULL; 
     514    return srtp_err_status_alloc_fail; 
     515  } 
     516 
     517  for (i = 0; i < stream_template->num_master_keys; i++){ 
     518    session_keys = &str->session_keys[i]; 
     519    template_session_keys = &stream_template->session_keys[i]; 
     520 
     521    /* set cipher and auth pointers to those of the template */ 
     522    session_keys->rtp_cipher  = template_session_keys->rtp_cipher; 
     523    session_keys->rtp_auth    = template_session_keys->rtp_auth; 
     524    session_keys->rtp_xtn_hdr_cipher = template_session_keys->rtp_xtn_hdr_cipher; 
     525    session_keys->rtcp_cipher = template_session_keys->rtcp_cipher; 
     526    session_keys->rtcp_auth   = template_session_keys->rtcp_auth; 
     527    session_keys->mki_size = template_session_keys->mki_size; 
     528 
     529    if (template_session_keys->mki_size == 0) { 
     530      session_keys->mki_id = NULL; 
     531    } else { 
     532      session_keys->mki_id = srtp_crypto_alloc(template_session_keys->mki_size); 
     533 
     534      if (session_keys->mki_id == NULL) { 
     535        return srtp_err_status_init_fail; 
     536      } 
     537      memset(session_keys->mki_id, 0x0, session_keys->mki_size); 
     538      memcpy(session_keys->mki_id, template_session_keys->mki_id, session_keys->mki_size); 
     539    } 
     540    /* Copy the salt values */ 
     541    memcpy(session_keys->salt, template_session_keys->salt, SRTP_AEAD_SALT_LEN); 
     542    memcpy(session_keys->c_salt, template_session_keys->c_salt, SRTP_AEAD_SALT_LEN); 
     543 
     544    /* set key limit to point to that of the template */ 
     545    status = srtp_key_limit_clone(template_session_keys->limit, &session_keys->limit); 
     546    if (status) {  
     547      srtp_crypto_free(*str_ptr); 
     548      *str_ptr = NULL; 
     549      return status; 
     550    } 
     551  } 
     552 
     553 
     554  /* initialize replay databases */ 
     555  status = srtp_rdbx_init(&str->rtp_rdbx, 
     556                     srtp_rdbx_get_window_size(&stream_template->rtp_rdbx)); 
     557  if (status) { 
     558    srtp_crypto_free(*str_ptr); 
    352559    *str_ptr = NULL; 
    353560    return status; 
    354561  } 
    355  
    356   /* initialize replay databases */ 
    357   status = rdbx_init(&str->rtp_rdbx, 
    358                      rdbx_get_window_size(&stream_template->rtp_rdbx)); 
    359   if (status) { 
    360     crypto_free(*str_ptr); 
    361     *str_ptr = NULL; 
    362     return status; 
    363   } 
    364   rdb_init(&str->rtcp_rdb); 
     562  srtp_rdb_init(&str->rtcp_rdb); 
    365563  str->allow_repeat_tx = stream_template->allow_repeat_tx; 
    366    
     564 
    367565  /* set ssrc to that provided */ 
    368566  str->ssrc = ssrc; 
     567 
     568  /* reset pending ROC */ 
     569  str->pending_roc = 0; 
    369570 
    370571  /* set direction and security services */ 
     
    376577  str->ekt = stream_template->ekt; 
    377578 
    378   /* Copy the salt values */ 
    379   memcpy(str->salt, stream_template->salt, SRTP_AEAD_SALT_LEN); 
    380   memcpy(str->c_salt, stream_template->c_salt, SRTP_AEAD_SALT_LEN); 
     579  /* copy information about extensions header encryption */ 
     580  str->enc_xtn_hdr = stream_template->enc_xtn_hdr; 
     581  str->enc_xtn_hdr_count = stream_template->enc_xtn_hdr_count; 
    381582 
    382583  /* defensive coding */ 
    383584  str->next = NULL; 
    384  
    385   return err_status_ok; 
     585  return srtp_err_status_ok; 
    386586} 
    387587 
     
    409609  label_rtcp_encryption = 0x03, 
    410610  label_rtcp_msg_auth   = 0x04, 
    411   label_rtcp_salt       = 0x05 
     611  label_rtcp_salt       = 0x05, 
     612  label_rtp_header_encryption = 0x06, 
     613  label_rtp_header_salt = 0x07 
    412614} srtp_prf_label; 
    413615 
     616#define MAX_SRTP_KEY_LEN 256 
     617 
     618#if defined(OPENSSL) && defined(OPENSSL_KDF) 
     619#define MAX_SRTP_AESKEY_LEN 32 
     620#define MAX_SRTP_SALT_LEN 14  
    414621 
    415622/* 
     
    417624 * default KDF is the only one implemented at present. 
    418625 */ 
    419  
    420626typedef struct {  
    421   cipher_t *cipher;    /* cipher used for key derivation  */   
     627    uint8_t master_key[MAX_SRTP_AESKEY_LEN]; 
     628    uint8_t master_salt[MAX_SRTP_SALT_LEN]; 
     629    const EVP_CIPHER *evp; 
    422630} srtp_kdf_t; 
    423631 
    424 err_status_t 
    425 srtp_kdf_init(srtp_kdf_t *kdf, cipher_type_id_t cipher_id, const uint8_t *key, int length) { 
    426  
    427   err_status_t stat; 
    428   stat = crypto_kernel_alloc_cipher(cipher_id, &kdf->cipher, length, 0); 
    429   if (stat) 
    430     return stat; 
    431  
    432   stat = cipher_init(kdf->cipher, key); 
    433   if (stat) { 
    434     cipher_dealloc(kdf->cipher); 
    435     return stat; 
    436   } 
    437  
    438   return err_status_ok; 
    439 } 
    440  
    441 err_status_t 
    442 srtp_kdf_generate(srtp_kdf_t *kdf, srtp_prf_label label, 
    443                   uint8_t *key, unsigned int length) { 
    444  
    445   v128_t nonce; 
    446   err_status_t status; 
     632 
     633static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf, const uint8_t *key, int key_len, int salt_len)  
     634{ 
     635    memset(kdf, 0x0, sizeof(srtp_kdf_t)); 
     636 
     637    /* The NULL cipher has zero key length */ 
     638    if (key_len == 0) return srtp_err_status_ok; 
     639 
     640    if ((key_len > MAX_SRTP_AESKEY_LEN) || (salt_len > MAX_SRTP_SALT_LEN)) { 
     641        return srtp_err_status_bad_param; 
     642    } 
     643    switch (key_len) { 
     644    case SRTP_AES_256_KEYSIZE: 
     645        kdf->evp = EVP_aes_256_ctr(); 
     646        break; 
     647    case SRTP_AES_192_KEYSIZE: 
     648        kdf->evp = EVP_aes_192_ctr(); 
     649        break; 
     650    case SRTP_AES_128_KEYSIZE: 
     651        kdf->evp = EVP_aes_128_ctr(); 
     652        break; 
     653    default: 
     654        return srtp_err_status_bad_param; 
     655        break; 
     656    } 
     657    memcpy(kdf->master_key, key, key_len);  
     658    memcpy(kdf->master_salt, key+key_len, salt_len);  
     659    return srtp_err_status_ok; 
     660} 
     661 
     662static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf, srtp_prf_label label, uint8_t *key, unsigned int length)  
     663{ 
     664    int ret; 
     665 
     666    /* The NULL cipher will not have an EVP */ 
     667    if (!kdf->evp) return srtp_err_status_ok; 
     668    octet_string_set_to_zero(key, length); 
     669 
     670    /* 
     671     * Invoke the OpenSSL SRTP KDF function 
     672     * This is useful if OpenSSL is in FIPS mode and FIP 
     673     * compliance is required for SRTP. 
     674     */ 
     675    ret = kdf_srtp(kdf->evp, (char *)&kdf->master_key, (char *)&kdf->master_salt, NULL, NULL, label, (char *)key); 
     676    if (ret == -1) { 
     677        return (srtp_err_status_algo_fail); 
     678    } 
     679 
     680    return srtp_err_status_ok; 
     681} 
     682 
     683static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf) { 
     684    octet_string_set_to_zero(kdf->master_key, MAX_SRTP_AESKEY_LEN); 
     685    octet_string_set_to_zero(kdf->master_salt, MAX_SRTP_SALT_LEN); 
     686    kdf->evp = NULL; 
     687 
     688    return srtp_err_status_ok;   
     689} 
     690 
     691#else /* if OPENSSL_KDF */ 
     692 
     693/* 
     694 * srtp_kdf_t represents a key derivation function.  The SRTP 
     695 * default KDF is the only one implemented at present. 
     696 */ 
     697typedef struct {  
     698    srtp_cipher_t *cipher;    /* cipher used for key derivation  */   
     699} srtp_kdf_t; 
     700 
     701static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf, const uint8_t *key, int key_len) 
     702{ 
     703    srtp_cipher_type_id_t cipher_id; 
     704    srtp_err_status_t stat; 
     705    switch (key_len) { 
     706    case SRTP_AES_ICM_256_KEY_LEN_WSALT: 
     707        cipher_id = SRTP_AES_ICM_256; 
     708        break; 
     709    case SRTP_AES_ICM_192_KEY_LEN_WSALT: 
     710        cipher_id = SRTP_AES_ICM_192; 
     711        break; 
     712    case SRTP_AES_ICM_128_KEY_LEN_WSALT: 
     713        cipher_id = SRTP_AES_ICM_128; 
     714        break; 
     715    default: 
     716        return srtp_err_status_bad_param; 
     717        break; 
     718    } 
     719 
     720    stat = srtp_crypto_kernel_alloc_cipher(cipher_id, &kdf->cipher, key_len, 0); 
     721    if (stat) return stat; 
     722 
     723    stat = srtp_cipher_init(kdf->cipher, key); 
     724    if (stat) { 
     725        srtp_cipher_dealloc(kdf->cipher); 
     726        return stat; 
     727    } 
     728    return srtp_err_status_ok; 
     729} 
     730 
     731static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf, srtp_prf_label label, uint8_t *key, unsigned int length)  
     732{ 
     733    srtp_err_status_t status; 
     734    v128_t nonce; 
    447735   
    448   /* set eigth octet of nonce to <label>, set the rest of it to zero */ 
    449   v128_set_to_zero(&nonce); 
    450   nonce.v8[7] = label; 
     736    /* set eigth octet of nonce to <label>, set the rest of it to zero */ 
     737    v128_set_to_zero(&nonce); 
     738    nonce.v8[7] = label; 
    451739  
    452   status = cipher_set_iv(kdf->cipher, &nonce, direction_encrypt); 
    453   if (status) 
    454     return status; 
     740    status = srtp_cipher_set_iv(kdf->cipher, (uint8_t*)&nonce, srtp_direction_encrypt); 
     741    if (status) return status; 
    455742   
    456   /* generate keystream output */ 
    457   octet_string_set_to_zero(key, length); 
    458   status = cipher_encrypt(kdf->cipher, key, &length); 
    459   if (status) 
    460     return status; 
    461  
    462   return err_status_ok; 
    463 } 
    464  
    465 err_status_t 
    466 srtp_kdf_clear(srtp_kdf_t *kdf) { 
    467   err_status_t status; 
    468   status = cipher_dealloc(kdf->cipher); 
    469   if (status) 
    470     return status; 
    471   kdf->cipher = NULL; 
    472  
    473   return err_status_ok;   
    474 } 
     743    /* generate keystream output */ 
     744    octet_string_set_to_zero(key, length); 
     745    status = srtp_cipher_encrypt(kdf->cipher, key, &length); 
     746    if (status) return status; 
     747 
     748    return srtp_err_status_ok; 
     749} 
     750 
     751static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf) { 
     752    srtp_err_status_t status; 
     753    status = srtp_cipher_dealloc(kdf->cipher); 
     754    if (status) return status; 
     755    kdf->cipher = NULL; 
     756    return srtp_err_status_ok;   
     757} 
     758#endif /* else OPENSSL_KDF */ 
    475759 
    476760/* 
     
    478762 */ 
    479763 
    480 #define MAX_SRTP_KEY_LEN 256 
    481764 
    482765 
    483766/* Get the base key length corresponding to a given combined key+salt 
    484767 * length for the given cipher. 
    485  * Assumption is that for AES-ICM a key length < 30 is Ismacryp using 
    486  * AES-128 and short salts; everything else uses a salt length of 14. 
    487768 * TODO: key and salt lengths should be separate fields in the policy.  */ 
    488 static inline int base_key_length(const cipher_type_t *cipher, int key_length) 
     769static inline int base_key_length(const srtp_cipher_type_t *cipher, int key_length) 
    489770{ 
    490771  switch (cipher->id) { 
    491   case AES_128_ICM: 
    492   case AES_192_ICM: 
    493   case AES_256_ICM: 
     772  case SRTP_AES_ICM_128: 
     773  case SRTP_AES_ICM_192: 
     774  case SRTP_AES_ICM_256: 
    494775    /* The legacy modes are derived from 
    495776     * the configured key length on the policy */ 
    496     return key_length - 14; 
     777    return key_length - SRTP_SALT_LEN; 
    497778    break; 
    498   case AES_128_GCM: 
    499     return 16; 
     779  case SRTP_AES_GCM_128: 
     780    return key_length - SRTP_AEAD_SALT_LEN; 
    500781    break; 
    501   case AES_256_GCM: 
    502     return 32; 
     782  case SRTP_AES_GCM_256: 
     783      return key_length - SRTP_AEAD_SALT_LEN; 
    503784    break; 
    504785  default: 
     
    508789} 
    509790 
    510 err_status_t 
    511 srtp_stream_init_keys(srtp_stream_ctx_t *srtp, const void *key) { 
    512   err_status_t stat; 
     791unsigned int 
     792srtp_validate_policy_master_keys(const srtp_policy_t *policy) 
     793{ 
     794    int i = 0; 
     795 
     796    if (policy->key == NULL) { 
     797        if (policy->num_master_keys <= 0) 
     798            return 0; 
     799 
     800        if (policy->num_master_keys > SRTP_MAX_NUM_MASTER_KEYS) 
     801            return 0; 
     802         
     803        for (i = 0; i < policy->num_master_keys; i++) { 
     804            if (policy->keys[i]->key == NULL) 
     805                return 0; 
     806            if (policy->keys[i]->mki_size > SRTP_MAX_MKI_LEN) 
     807                return 0; 
     808        } 
     809    } 
     810 
     811    return 1; 
     812} 
     813 
     814srtp_session_keys_t* 
     815srtp_get_session_keys_with_mki_index(srtp_stream_ctx_t *stream, 
     816                                     unsigned int use_mki, 
     817                                     unsigned int mki_index) { 
     818    if (use_mki) { 
     819        if (mki_index < stream->num_master_keys) { 
     820            return &stream->session_keys[mki_index]; 
     821        } 
     822    } 
     823 
     824    return &stream->session_keys[0]; 
     825} 
     826 
     827unsigned int 
     828srtp_inject_mki(uint8_t *mki_tag_location, srtp_session_keys_t* session_keys, 
     829                unsigned int use_mki) 
     830{ 
     831    unsigned int mki_size = 0; 
     832 
     833    if (use_mki) { 
     834        mki_size = session_keys->mki_size; 
     835 
     836        if (mki_size != 0) { 
     837            // Write MKI into memory 
     838            memcpy(mki_tag_location, session_keys->mki_id, mki_size); 
     839        } 
     840    } 
     841 
     842    return mki_size; 
     843} 
     844 
     845srtp_err_status_t 
     846srtp_stream_init_all_master_keys(srtp_stream_ctx_t *srtp, 
     847                                 unsigned char *key, 
     848                                 srtp_master_key_t **keys, 
     849                                 const unsigned int max_master_keys) { 
     850    int i = 0; 
     851    srtp_err_status_t status = srtp_err_status_ok; 
     852    srtp_master_key_t single_master_key; 
     853 
     854    if ( key != NULL ) { 
     855        srtp->num_master_keys = 1; 
     856        single_master_key.key = key; 
     857        single_master_key.mki_id = NULL; 
     858        single_master_key.mki_size = 0; 
     859        status = srtp_stream_init_keys(srtp, &single_master_key, 0); 
     860    } else { 
     861        srtp->num_master_keys = max_master_keys; 
     862 
     863        for (i = 0; i < srtp->num_master_keys && i < SRTP_MAX_NUM_MASTER_KEYS; i++) { 
     864            status = srtp_stream_init_keys(srtp, keys[i], i); 
     865 
     866            if (status) { 
     867                return status; 
     868            } 
     869        } 
     870    } 
     871 
     872    return status; 
     873} 
     874 
     875srtp_err_status_t 
     876srtp_stream_init_keys(srtp_stream_ctx_t *srtp, srtp_master_key_t *master_key, 
     877                      const unsigned int current_mki_index) { 
     878  srtp_err_status_t stat; 
    513879  srtp_kdf_t kdf; 
    514880  uint8_t tmp_key[MAX_SRTP_KEY_LEN]; 
     
    516882  int rtp_base_key_len, rtp_salt_len; 
    517883  int rtcp_base_key_len, rtcp_salt_len; 
     884  srtp_session_keys_t *session_keys = NULL; 
     885  unsigned char *key = master_key->key; 
    518886 
    519887  /* If RTP or RTCP have a key length > AES-128, assume matching kdf. */ 
    520888  /* TODO: kdf algorithm, master key length, and master salt length should 
    521889   * be part of srtp_policy_t. */ 
    522   rtp_keylen = cipher_get_key_length(srtp->rtp_cipher); 
    523   rtcp_keylen = cipher_get_key_length(srtp->rtcp_cipher); 
    524   rtp_base_key_len = base_key_length(srtp->rtp_cipher->type, rtp_keylen); 
     890  session_keys = &srtp->session_keys[current_mki_index]; 
     891 
     892   /* initialize key limit to maximum value */ 
     893#ifdef NO_64BIT_MATH 
     894{ 
     895   uint64_t temp; 
     896   temp = make64(UINT_MAX,UINT_MAX); 
     897   srtp_key_limit_set(session_keys->limit, temp); 
     898} 
     899#else 
     900   srtp_key_limit_set(session_keys->limit, 0xffffffffffffLL); 
     901#endif 
     902 
     903 
     904  if ( master_key->mki_size != 0 ) { 
     905      session_keys->mki_id = srtp_crypto_alloc(master_key->mki_size); 
     906 
     907      if (session_keys->mki_id == NULL) { 
     908        return srtp_err_status_init_fail; 
     909      } 
     910      memset(session_keys->mki_id, 0x0, master_key->mki_size); 
     911      memcpy(session_keys->mki_id, master_key->mki_id, master_key->mki_size); 
     912  } else { 
     913      session_keys->mki_id = NULL; 
     914  } 
     915 
     916  session_keys->mki_size = master_key->mki_size; 
     917 
     918  rtp_keylen = srtp_cipher_get_key_length(session_keys->rtp_cipher); 
     919  rtcp_keylen = srtp_cipher_get_key_length(session_keys->rtcp_cipher); 
     920  rtp_base_key_len = base_key_length(session_keys->rtp_cipher->type, rtp_keylen); 
    525921  rtp_salt_len = rtp_keylen - rtp_base_key_len; 
    526922 
     
    548944 
    549945  /* initialize KDF state     */ 
    550   stat = srtp_kdf_init(&kdf, AES_ICM, (const uint8_t *)tmp_key, kdf_keylen); 
     946#if defined(OPENSSL) && defined(OPENSSL_KDF) 
     947  stat = srtp_kdf_init(&kdf, (const uint8_t *)tmp_key, rtp_base_key_len, rtp_salt_len);  
     948#else 
     949  stat = srtp_kdf_init(&kdf, (const uint8_t *)tmp_key, kdf_keylen); 
     950#endif 
    551951  if (stat) { 
    552     return err_status_init_fail; 
     952    /* zeroize temp buffer */ 
     953    octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
     954    return srtp_err_status_init_fail; 
    553955  } 
    554956   
     
    559961    /* zeroize temp buffer */ 
    560962    octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
    561     return err_status_init_fail; 
     963    return srtp_err_status_init_fail; 
    562964  } 
    563965  debug_print(mod_srtp, "cipher key: %s",  
    564               octet_string_hex_string(tmp_key, rtp_base_key_len)); 
     966              srtp_octet_string_hex_string(tmp_key, rtp_base_key_len)); 
    565967 
    566968  /*  
     
    577979      /* zeroize temp buffer */ 
    578980      octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
    579       return err_status_init_fail; 
    580     } 
    581     memcpy(srtp->salt, tmp_key + rtp_base_key_len, SRTP_AEAD_SALT_LEN); 
     981      return srtp_err_status_init_fail; 
     982    } 
     983    memcpy(session_keys->salt, tmp_key + rtp_base_key_len, SRTP_AEAD_SALT_LEN); 
    582984  } 
    583985  if (rtp_salt_len > 0) { 
    584986    debug_print(mod_srtp, "cipher salt: %s", 
    585                 octet_string_hex_string(tmp_key + rtp_base_key_len, rtp_salt_len)); 
     987                srtp_octet_string_hex_string(tmp_key + rtp_base_key_len, rtp_salt_len)); 
    586988  } 
    587989 
    588990  /* initialize cipher */ 
    589   stat = cipher_init(srtp->rtp_cipher, tmp_key); 
     991  stat = srtp_cipher_init(session_keys->rtp_cipher, tmp_key); 
    590992  if (stat) { 
    591993    /* zeroize temp buffer */ 
    592994    octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
    593     return err_status_init_fail; 
     995    return srtp_err_status_init_fail; 
     996  } 
     997 
     998  if (session_keys->rtp_xtn_hdr_cipher) { 
     999    /* generate extensions header encryption key  */ 
     1000    int rtp_xtn_hdr_keylen; 
     1001    int rtp_xtn_hdr_base_key_len; 
     1002    int rtp_xtn_hdr_salt_len; 
     1003    srtp_kdf_t tmp_kdf; 
     1004    srtp_kdf_t *xtn_hdr_kdf; 
     1005 
     1006    if (session_keys->rtp_xtn_hdr_cipher->type != session_keys->rtp_cipher->type) { 
     1007      /* With GCM ciphers, the header extensions are still encrypted using the corresponding ICM cipher. */ 
     1008      /* See https://tools.ietf.org/html/rfc7714#section-8.3 */ 
     1009      uint8_t tmp_xtn_hdr_key[MAX_SRTP_KEY_LEN]; 
     1010      rtp_xtn_hdr_keylen = srtp_cipher_get_key_length(session_keys->rtp_xtn_hdr_cipher); 
     1011      rtp_xtn_hdr_base_key_len = base_key_length(session_keys->rtp_xtn_hdr_cipher->type, 
     1012                                                 rtp_xtn_hdr_keylen); 
     1013      rtp_xtn_hdr_salt_len = rtp_xtn_hdr_keylen - rtp_xtn_hdr_base_key_len; 
     1014      if (rtp_xtn_hdr_salt_len > rtp_salt_len) { 
     1015        switch (session_keys->rtp_cipher->type->id) { 
     1016        case SRTP_AES_GCM_128: 
     1017        case SRTP_AES_GCM_256: 
     1018          /* The shorter GCM salt is padded to the required ICM salt length. */ 
     1019          rtp_xtn_hdr_salt_len = rtp_salt_len; 
     1020          break; 
     1021        default: 
     1022          /* zeroize temp buffer */ 
     1023          octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
     1024          return srtp_err_status_bad_param; 
     1025        } 
     1026      } 
     1027      memset(tmp_xtn_hdr_key, 0x0, MAX_SRTP_KEY_LEN); 
     1028      memcpy(tmp_xtn_hdr_key, key, (rtp_xtn_hdr_base_key_len + rtp_xtn_hdr_salt_len)); 
     1029      xtn_hdr_kdf = &tmp_kdf; 
     1030 
     1031      /* initialize KDF state     */ 
     1032#if defined(OPENSSL) && defined(OPENSSL_KDF) 
     1033      stat = srtp_kdf_init(xtn_hdr_kdf, (const uint8_t *)tmp_xtn_hdr_key, rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len); 
     1034#else 
     1035      stat = srtp_kdf_init(xtn_hdr_kdf, (const uint8_t *)tmp_xtn_hdr_key, kdf_keylen); 
     1036#endif 
     1037      octet_string_set_to_zero(tmp_xtn_hdr_key, MAX_SRTP_KEY_LEN); 
     1038      if (stat) { 
     1039        /* zeroize temp buffer */ 
     1040        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
     1041        return srtp_err_status_init_fail; 
     1042      } 
     1043    } else { 
     1044      /* Reuse main KDF. */ 
     1045      rtp_xtn_hdr_keylen = rtp_keylen; 
     1046      rtp_xtn_hdr_base_key_len = rtp_base_key_len; 
     1047      rtp_xtn_hdr_salt_len = rtp_salt_len; 
     1048      xtn_hdr_kdf = &kdf; 
     1049    } 
     1050 
     1051    stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_encryption, 
     1052           tmp_key, rtp_xtn_hdr_base_key_len); 
     1053    if (stat) { 
     1054      /* zeroize temp buffer */ 
     1055      octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
     1056      return srtp_err_status_init_fail; 
     1057    } 
     1058    debug_print(mod_srtp, "extensions cipher key: %s", 
     1059          srtp_octet_string_hex_string(tmp_key, rtp_xtn_hdr_base_key_len)); 
     1060 
     1061    /* 
     1062     * if the cipher in the srtp context uses a salt, then we need 
     1063     * to generate the salt value 
     1064     */ 
     1065    if (rtp_xtn_hdr_salt_len > 0) { 
     1066      debug_print(mod_srtp, "found rtp_xtn_hdr_salt_len > 0, generating salt", NULL); 
     1067 
     1068      /* generate encryption salt, put after encryption key */ 
     1069      stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_salt, 
     1070             tmp_key + rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len); 
     1071      if (stat) { 
     1072        /* zeroize temp buffer */ 
     1073        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
     1074        return srtp_err_status_init_fail; 
     1075      } 
     1076    } 
     1077    if (rtp_xtn_hdr_salt_len > 0) { 
     1078      debug_print(mod_srtp, "extensions cipher salt: %s", 
     1079      srtp_octet_string_hex_string(tmp_key + rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len)); 
     1080    } 
     1081 
     1082    /* initialize extensions header cipher */ 
     1083    stat = srtp_cipher_init(session_keys->rtp_xtn_hdr_cipher, tmp_key); 
     1084    if (stat) { 
     1085      /* zeroize temp buffer */ 
     1086      octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
     1087      return srtp_err_status_init_fail; 
     1088    } 
     1089 
     1090    if (xtn_hdr_kdf != &kdf) { 
     1091      /* release memory for custom header extension encryption kdf */ 
     1092      stat = srtp_kdf_clear(xtn_hdr_kdf); 
     1093      if (stat) { 
     1094        /* zeroize temp buffer */ 
     1095        octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
     1096        return srtp_err_status_init_fail; 
     1097      } 
     1098    } 
    5941099  } 
    5951100 
    5961101  /* generate authentication key */ 
    5971102  stat = srtp_kdf_generate(&kdf, label_rtp_msg_auth, 
    598                            tmp_key, auth_get_key_length(srtp->rtp_auth)); 
     1103                           tmp_key, srtp_auth_get_key_length(session_keys->rtp_auth)); 
    5991104  if (stat) { 
    6001105    /* zeroize temp buffer */ 
    6011106    octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
    602     return err_status_init_fail; 
     1107    return srtp_err_status_init_fail; 
    6031108  } 
    6041109  debug_print(mod_srtp, "auth key:   %s", 
    605               octet_string_hex_string(tmp_key,  
    606                                       auth_get_key_length(srtp->rtp_auth)));  
     1110              srtp_octet_string_hex_string(tmp_key,  
     1111                                      srtp_auth_get_key_length(session_keys->rtp_auth)));  
    6071112 
    6081113  /* initialize auth function */ 
    609   stat = auth_init(srtp->rtp_auth, tmp_key); 
     1114  stat = srtp_auth_init(session_keys->rtp_auth, tmp_key); 
    6101115  if (stat) { 
    6111116    /* zeroize temp buffer */ 
    6121117    octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
    613     return err_status_init_fail; 
     1118    return srtp_err_status_init_fail; 
    6141119  } 
    6151120 
     
    6181123   */ 
    6191124 
    620   rtcp_base_key_len = base_key_length(srtp->rtcp_cipher->type, rtcp_keylen); 
     1125  rtcp_base_key_len = base_key_length(session_keys->rtcp_cipher->type, rtcp_keylen); 
    6211126  rtcp_salt_len = rtcp_keylen - rtcp_base_key_len; 
    6221127  debug_print(mod_srtp, "rtcp salt len: %d", rtcp_salt_len); 
     
    6281133    /* zeroize temp buffer */ 
    6291134    octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
    630     return err_status_init_fail; 
     1135    return srtp_err_status_init_fail; 
    6311136  } 
    6321137 
     
    6451150      /* zeroize temp buffer */ 
    6461151      octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
    647       return err_status_init_fail; 
    648     } 
    649     memcpy(srtp->c_salt, tmp_key + rtcp_base_key_len, SRTP_AEAD_SALT_LEN); 
     1152      return srtp_err_status_init_fail; 
     1153    } 
     1154    memcpy(session_keys->c_salt, tmp_key + rtcp_base_key_len, SRTP_AEAD_SALT_LEN); 
    6501155  } 
    6511156  debug_print(mod_srtp, "rtcp cipher key: %s",  
    652               octet_string_hex_string(tmp_key, rtcp_base_key_len));   
     1157              srtp_octet_string_hex_string(tmp_key, rtcp_base_key_len));   
    6531158  if (rtcp_salt_len > 0) { 
    6541159    debug_print(mod_srtp, "rtcp cipher salt: %s", 
    655                 octet_string_hex_string(tmp_key + rtcp_base_key_len, rtcp_salt_len)); 
     1160                srtp_octet_string_hex_string(tmp_key + rtcp_base_key_len, rtcp_salt_len)); 
    6561161  } 
    6571162 
    6581163  /* initialize cipher */ 
    659   stat = cipher_init(srtp->rtcp_cipher, tmp_key); 
     1164  stat = srtp_cipher_init(session_keys->rtcp_cipher, tmp_key); 
    6601165  if (stat) { 
    6611166    /* zeroize temp buffer */ 
    6621167    octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
    663     return err_status_init_fail; 
     1168    return srtp_err_status_init_fail; 
    6641169  } 
    6651170 
    6661171  /* generate authentication key */ 
    6671172  stat = srtp_kdf_generate(&kdf, label_rtcp_msg_auth, 
    668                            tmp_key, auth_get_key_length(srtp->rtcp_auth)); 
     1173                           tmp_key, srtp_auth_get_key_length(session_keys->rtcp_auth)); 
    6691174  if (stat) { 
    6701175    /* zeroize temp buffer */ 
    6711176    octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
    672     return err_status_init_fail; 
     1177    return srtp_err_status_init_fail; 
    6731178  } 
    6741179 
    6751180  debug_print(mod_srtp, "rtcp auth key:   %s", 
    676               octet_string_hex_string(tmp_key,  
    677                      auth_get_key_length(srtp->rtcp_auth)));  
     1181              srtp_octet_string_hex_string(tmp_key,  
     1182                     srtp_auth_get_key_length(session_keys->rtcp_auth)));  
    6781183 
    6791184  /* initialize auth function */ 
    680   stat = auth_init(srtp->rtcp_auth, tmp_key); 
     1185  stat = srtp_auth_init(session_keys->rtcp_auth, tmp_key); 
    6811186  if (stat) { 
    6821187    /* zeroize temp buffer */ 
    6831188    octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
    684     return err_status_init_fail; 
     1189    return srtp_err_status_init_fail; 
    6851190  } 
    6861191 
    6871192  /* clear memory then return */ 
    6881193  stat = srtp_kdf_clear(&kdf); 
    689   octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);   
     1194  octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); 
    6901195  if (stat) 
    691     return err_status_init_fail; 
    692  
    693   return err_status_ok; 
    694 } 
    695  
    696 err_status_t 
     1196    return srtp_err_status_init_fail; 
     1197 
     1198  return srtp_err_status_ok; 
     1199} 
     1200 
     1201srtp_err_status_t 
    6971202srtp_stream_init(srtp_stream_ctx_t *srtp,  
    6981203                  const srtp_policy_t *p) { 
    699   err_status_t err; 
     1204  srtp_err_status_t err; 
    7001205 
    7011206   debug_print(mod_srtp, "initializing stream (SSRC: 0x%08x)",  
     
    7081213 
    7091214   if (p->window_size != 0 && (p->window_size < 64 || p->window_size >= 0x8000)) 
    710      return err_status_bad_param; 
     1215     return srtp_err_status_bad_param; 
    7111216 
    7121217   if (p->window_size != 0) 
    713      err = rdbx_init(&srtp->rtp_rdbx, p->window_size); 
     1218     err = srtp_rdbx_init(&srtp->rtp_rdbx, p->window_size); 
    7141219   else 
    715      err = rdbx_init(&srtp->rtp_rdbx, 128); 
     1220     err = srtp_rdbx_init(&srtp->rtp_rdbx, 128); 
    7161221   if (err) return err; 
    717  
    718    /* initialize key limit to maximum value */ 
    719 #ifdef NO_64BIT_MATH 
    720 { 
    721    uint64_t temp; 
    722    temp = make64(UINT_MAX,UINT_MAX); 
    723    key_limit_set(srtp->limit, temp); 
    724 } 
    725 #else 
    726    key_limit_set(srtp->limit, 0xffffffffffffLL); 
    727 #endif 
    7281222 
    7291223   /* set the SSRC value */ 
    7301224   srtp->ssrc = htonl(p->ssrc.value); 
     1225 
     1226   /* reset pending ROC */ 
     1227   srtp->pending_roc = 0; 
    7311228 
    7321229   /* set the security service flags */ 
     
    7421239 
    7431240   /* initialize SRTCP replay database */ 
    744    rdb_init(&srtp->rtcp_rdb); 
     1241   srtp_rdb_init(&srtp->rtcp_rdb); 
    7451242 
    7461243   /* initialize allow_repeat_tx */ 
    7471244   /* guard against uninitialized memory: allow only 0 or 1 here */ 
    7481245   if (p->allow_repeat_tx != 0 && p->allow_repeat_tx != 1) { 
    749      rdbx_dealloc(&srtp->rtp_rdbx); 
    750      return err_status_bad_param; 
     1246     srtp_rdbx_dealloc(&srtp->rtp_rdbx); 
     1247     return srtp_err_status_bad_param; 
    7511248   } 
    7521249   srtp->allow_repeat_tx = p->allow_repeat_tx; 
     
    7551252 
    7561253   /* initialize keys */ 
    757    err = srtp_stream_init_keys(srtp, p->key); 
     1254   err = srtp_stream_init_all_master_keys(srtp, p->key, p->keys, p->num_master_keys); 
    7581255   if (err) { 
    759      rdbx_dealloc(&srtp->rtp_rdbx); 
     1256     srtp_rdbx_dealloc(&srtp->rtp_rdbx); 
    7601257     return err; 
    7611258   } 
     
    7651262    * the stream 
    7661263    */ 
    767    err = ekt_stream_init_from_policy(srtp->ekt, p->ekt); 
     1264   err = srtp_ekt_stream_init_from_policy(srtp->ekt, p->ekt); 
    7681265   if (err) { 
    769      rdbx_dealloc(&srtp->rtp_rdbx); 
     1266     srtp_rdbx_dealloc(&srtp->rtp_rdbx); 
    7701267     return err; 
    7711268   } 
    7721269 
    773    return err_status_ok;   
     1270   return srtp_err_status_ok;   
    7741271 } 
    7751272 
     
    7831280 srtp_event_reporter(srtp_event_data_t *data) { 
    7841281 
    785    err_report(err_level_warning, "srtp: in stream 0x%x: ",  
    786               data->stream->ssrc); 
     1282   srtp_err_report(srtp_err_level_warning, "srtp: in stream 0x%x: ",  
     1283                   data->ssrc); 
    7871284 
    7881285   switch(data->event) { 
    7891286   case event_ssrc_collision: 
    790      err_report(err_level_warning, "\tSSRC collision\n"); 
     1287     srtp_err_report(srtp_err_level_warning, "\tSSRC collision\n"); 
    7911288     break; 
    7921289   case event_key_soft_limit: 
    793      err_report(err_level_warning, "\tkey usage soft limit reached\n"); 
     1290     srtp_err_report(srtp_err_level_warning, "\tkey usage soft limit reached\n"); 
    7941291     break; 
    7951292   case event_key_hard_limit: 
    796      err_report(err_level_warning, "\tkey usage hard limit reached\n"); 
     1293     srtp_err_report(srtp_err_level_warning, "\tkey usage hard limit reached\n"); 
    7971294     break; 
    7981295   case event_packet_index_limit: 
    799      err_report(err_level_warning, "\tpacket index limit reached\n"); 
     1296     srtp_err_report(srtp_err_level_warning, "\tpacket index limit reached\n"); 
    8001297     break; 
    8011298   default: 
    802      err_report(err_level_warning, "\tunknown event reported to handler\n"); 
     1299     srtp_err_report(srtp_err_level_warning, "\tunknown event reported to handler\n"); 
    8031300   } 
    8041301 } 
     
    8161313 static srtp_event_handler_func_t *srtp_event_handler = srtp_event_reporter; 
    8171314 
    818  err_status_t 
     1315 srtp_err_status_t 
    8191316 srtp_install_event_handler(srtp_event_handler_func_t func) { 
    8201317 
     
    8271324   /* set global event handling function */ 
    8281325   srtp_event_handler = func; 
    829    return err_status_ok; 
     1326   return srtp_err_status_ok; 
    8301327 } 
     1328 
     1329 
     1330/* 
     1331 * Check if the given extension header id is / should be encrypted. 
     1332 * Returns 1 if yes, otherwise 0. 
     1333 */ 
     1334static int 
     1335srtp_protect_extension_header(srtp_stream_ctx_t *stream, int id) { 
     1336  int* enc_xtn_hdr = stream->enc_xtn_hdr; 
     1337  int count = stream->enc_xtn_hdr_count; 
     1338 
     1339  if (!enc_xtn_hdr || count <= 0) { 
     1340    return 0; 
     1341  } 
     1342 
     1343  while (count > 0) { 
     1344    if (*enc_xtn_hdr == id) { 
     1345      return 1; 
     1346    } 
     1347 
     1348    enc_xtn_hdr++; 
     1349    count--; 
     1350  } 
     1351  return 0; 
     1352} 
     1353 
     1354 
     1355/* 
     1356 * extensions header encryption RFC 6904 
     1357 */ 
     1358static srtp_err_status_t 
     1359srtp_process_header_encryption(srtp_stream_ctx_t *stream, 
     1360                               srtp_hdr_xtnd_t *xtn_hdr, 
     1361                               srtp_session_keys_t *session_keys) { 
     1362  srtp_err_status_t status; 
     1363  uint8_t keystream[257];  /* Maximum 2 bytes header + 255 bytes data. */ 
     1364  int keystream_pos; 
     1365  uint8_t* xtn_hdr_data = ((uint8_t *)xtn_hdr) + octets_in_rtp_extn_hdr; 
     1366  uint8_t* xtn_hdr_end = xtn_hdr_data + (ntohs(xtn_hdr->length) * sizeof(uint32_t)); 
     1367 
     1368  if (ntohs(xtn_hdr->profile_specific) == 0xbede) { 
     1369    /* RFC 5285, section 4.2. One-Byte Header */ 
     1370    while (xtn_hdr_data < xtn_hdr_end) { 
     1371      uint8_t xid = (*xtn_hdr_data & 0xf0) >> 4; 
     1372      unsigned int xlen = (*xtn_hdr_data & 0x0f) + 1; 
     1373      uint32_t xlen_with_header = 1+xlen; 
     1374      xtn_hdr_data++; 
     1375 
     1376      if (xtn_hdr_data + xlen > xtn_hdr_end) 
     1377        return srtp_err_status_parse_err; 
     1378 
     1379      if (xid == 15) { 
     1380        /* found header 15, stop further processing. */ 
     1381        break; 
     1382      } 
     1383 
     1384      status = srtp_cipher_output(session_keys->rtp_xtn_hdr_cipher, 
     1385                                  keystream, &xlen_with_header); 
     1386      if (status) 
     1387        return srtp_err_status_cipher_fail; 
     1388 
     1389      if (srtp_protect_extension_header(stream, xid)) { 
     1390        keystream_pos = 1; 
     1391        while (xlen > 0) { 
     1392          *xtn_hdr_data ^= keystream[keystream_pos++]; 
     1393          xtn_hdr_data++; 
     1394          xlen--; 
     1395        } 
     1396      } else { 
     1397        xtn_hdr_data += xlen; 
     1398      } 
     1399 
     1400      /* skip padding bytes. */ 
     1401      while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) { 
     1402        xtn_hdr_data++; 
     1403      } 
     1404    } 
     1405  } else if ((ntohs(xtn_hdr->profile_specific) & 0x1fff) == 0x100) { 
     1406    /* RFC 5285, section 4.3. Two-Byte Header */ 
     1407    while (xtn_hdr_data + 1 < xtn_hdr_end) { 
     1408      uint8_t xid = *xtn_hdr_data; 
     1409      unsigned int xlen = *(xtn_hdr_data+1); 
     1410      uint32_t xlen_with_header = 2+xlen; 
     1411      xtn_hdr_data += 2; 
     1412 
     1413      if (xtn_hdr_data + xlen > xtn_hdr_end) 
     1414        return srtp_err_status_parse_err; 
     1415 
     1416      status = srtp_cipher_output(session_keys->rtp_xtn_hdr_cipher, 
     1417                                  keystream, &xlen_with_header); 
     1418      if (status) 
     1419        return srtp_err_status_cipher_fail; 
     1420 
     1421      if (xlen > 0 && srtp_protect_extension_header(stream, xid)) { 
     1422        keystream_pos = 2; 
     1423        while (xlen > 0) { 
     1424          *xtn_hdr_data ^= keystream[keystream_pos++]; 
     1425          xtn_hdr_data++; 
     1426          xlen--; 
     1427        } 
     1428      } else { 
     1429        xtn_hdr_data += xlen; 
     1430      } 
     1431 
     1432      /* skip padding bytes. */ 
     1433      while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) { 
     1434        xtn_hdr_data++; 
     1435      } 
     1436    } 
     1437  } else { 
     1438    /* unsupported extension header format. */ 
     1439    return srtp_err_status_parse_err; 
     1440  } 
     1441 
     1442  return srtp_err_status_ok; 
     1443} 
     1444 
    8311445 
    8321446/* 
    8331447 * AEAD uses a new IV formation method.  This function implements 
    834  * section 9.1 from draft-ietf-avtcore-srtp-aes-gcm-07.txt.  The 
    835  * calculation is defined as, where (+) is the xor operation: 
     1448 * section 8.1. (SRTP IV Formation for AES-GCM) of RFC7714. 
     1449 * The calculation is defined as, where (+) is the xor operation: 
    8361450 * 
    8371451 * 
     
    8501464 *            +--+--+--+--+--+--+--+--+--+--+--+--+* 
    8511465 * 
    852  * Input:  *stream - pointer to SRTP stream context, used to retrieve 
    853  *                   the SALT  
     1466 * Input:  *session_keys - pointer to SRTP stream context session keys, 
     1467 *                         used to retrieve the SALT  
    8541468 *         *iv     - Pointer to receive the calculated IV 
    8551469 *         *seq    - The ROC and SEQ value to use for the 
     
    8581472 * 
    8591473 */ 
    860 static void srtp_calc_aead_iv(srtp_stream_ctx_t *stream, v128_t *iv,  
    861                               xtd_seq_num_t *seq, srtp_hdr_t *hdr) 
     1474 
     1475static void srtp_calc_aead_iv(srtp_session_keys_t *session_keys, v128_t *iv, 
     1476                              srtp_xtd_seq_num_t *seq, srtp_hdr_t *hdr) 
    8621477{ 
    8631478    v128_t      in; 
     
    8891504     * Get the SALT value from the context 
    8901505     */ 
    891     memcpy(salt.v8, stream->salt, SRTP_AEAD_SALT_LEN); 
     1506    memcpy(salt.v8, session_keys->salt, SRTP_AEAD_SALT_LEN); 
    8921507    debug_print(mod_srtp, "RTP SALT = %s\n", v128_hex_string(&salt)); 
    8931508 
     
    8981513} 
    8991514 
     1515 
     1516srtp_session_keys_t*  
     1517srtp_get_session_keys(srtp_stream_ctx_t *stream, uint8_t* hdr, 
     1518                      const unsigned int* pkt_octet_len, 
     1519                      unsigned int* mki_size) { 
     1520  unsigned int base_mki_start_location = *pkt_octet_len; 
     1521  unsigned int mki_start_location = 0; 
     1522  unsigned int tag_len = 0; 
     1523  unsigned int i = 0; 
     1524 
     1525  // Determine the authentication tag size 
     1526  if (stream->session_keys[0].rtp_cipher->algorithm == SRTP_AES_GCM_128 || 
     1527      stream->session_keys[0].rtp_cipher->algorithm == SRTP_AES_GCM_256) { 
     1528      tag_len = 0; 
     1529  } else { 
     1530      tag_len = srtp_auth_get_tag_length(stream->session_keys[0].rtp_auth); 
     1531  } 
     1532 
     1533  if (tag_len > base_mki_start_location) { 
     1534     *mki_size = 0; 
     1535     return NULL; 
     1536  } 
     1537 
     1538  base_mki_start_location -= tag_len; 
     1539 
     1540  for (i = 0; i < stream->num_master_keys; i++) { 
     1541      if (stream->session_keys[i].mki_size != 0) { 
     1542          *mki_size = stream->session_keys[i].mki_size; 
     1543          mki_start_location = base_mki_start_location - *mki_size; 
     1544 
     1545          if ( mki_start_location >= *mki_size && 
     1546              memcmp(hdr + mki_start_location, stream->session_keys[i].mki_id, *mki_size) == 0 ) { 
     1547              return &stream->session_keys[i]; 
     1548          } 
     1549      } 
     1550  } 
     1551 
     1552  *mki_size = 0; 
     1553  return NULL; 
     1554} 
     1555 
     1556static srtp_err_status_t 
     1557srtp_estimate_index(srtp_rdbx_t *rdbx, 
     1558                    uint32_t roc, 
     1559                    srtp_xtd_seq_num_t *est, 
     1560                    srtp_sequence_number_t seq, 
     1561                    int *delta) 
     1562{ 
     1563#ifdef NO_64BIT_MATH 
     1564  uint32_t internal_pkt_idx_reduced; 
     1565  uint32_t external_pkt_idx_reduced; 
     1566  uint32_t internal_roc; 
     1567  uint32_t roc_difference; 
     1568#endif 
     1569 
     1570#ifdef NO_64BIT_MATH 
     1571  *est = (srtp_xtd_seq_num_t)make64(roc >> 16, (roc << 16) | seq); 
     1572  *delta = low32(est) - rdbx->index; 
     1573#else 
     1574  *est = (srtp_xtd_seq_num_t)(((uint64_t)roc) << 16) | seq; 
     1575  *delta = (int)(*est - rdbx->index); 
     1576#endif 
     1577 
     1578  if (*est > rdbx->index) { 
     1579#ifdef NO_64BIT_MATH 
     1580    internal_roc = (uint32_t)(rdbx->index >> 16); 
     1581    roc_difference = roc - internal_roc; 
     1582    if (roc_difference > 1) { 
     1583      *delta = 0; 
     1584      return srtp_err_status_pkt_idx_adv; 
     1585    } 
     1586 
     1587    internal_pkt_idx_reduced = (uint32_t)(rdbx->index & 0xFFFF); 
     1588    external_pkt_idx_reduced = (uint32_t)((roc_difference << 16) | seq); 
     1589 
     1590    if (external_pkt_idx_reduced - internal_pkt_idx_reduced > 
     1591                                                    seq_num_median) { 
     1592      *delta = 0; 
     1593      return srtp_err_status_pkt_idx_adv; 
     1594    } 
     1595#else 
     1596    if (*est - rdbx->index > seq_num_median) { 
     1597      *delta = 0; 
     1598      return srtp_err_status_pkt_idx_adv; 
     1599    } 
     1600#endif 
     1601  } else if (*est < rdbx->index) { 
     1602#ifdef NO_64BIT_MATH 
     1603 
     1604    internal_roc = (uint32_t)(rdbx->index >> 16); 
     1605    roc_difference = internal_roc - roc; 
     1606    if (roc_difference > 1) { 
     1607      *delta = 0; 
     1608      return srtp_err_status_pkt_idx_adv; 
     1609    } 
     1610 
     1611    internal_pkt_idx_reduced = 
     1612                (uint32_t)((roc_difference << 16) | rdbx->index & 0xFFFF); 
     1613    external_pkt_idx_reduced = (uint32_t)(seq); 
     1614 
     1615    if (internal_pkt_idx_reduced - external_pkt_idx_reduced > 
     1616                                                    seq_num_median) { 
     1617      *delta = 0; 
     1618      return srtp_err_status_pkt_idx_old; 
     1619    } 
     1620#else 
     1621    if (rdbx->index - *est > seq_num_median) { 
     1622      *delta = 0; 
     1623      return srtp_err_status_pkt_idx_old; 
     1624    } 
     1625#endif 
     1626  } 
     1627 
     1628  return srtp_err_status_ok; 
     1629} 
     1630 
     1631static srtp_err_status_t 
     1632srtp_get_est_pkt_index(srtp_hdr_t *hdr, 
     1633                       srtp_stream_ctx_t *stream, 
     1634                       srtp_xtd_seq_num_t *est, 
     1635                       int *delta) 
     1636{ 
     1637  srtp_err_status_t result = srtp_err_status_ok; 
     1638 
     1639  if (stream->pending_roc) { 
     1640    result = srtp_estimate_index(&stream->rtp_rdbx, 
     1641                                 stream->pending_roc, 
     1642                                 est, 
     1643                                 ntohs(hdr->seq), 
     1644                                 delta); 
     1645  } else { 
     1646    /* estimate packet index from seq. num. in header */ 
     1647    *delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, 
     1648                                      est, 
     1649                                      ntohs(hdr->seq)); 
     1650  } 
     1651 
     1652#ifdef NO_64BIT_MATH 
     1653  debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(*est), low32(*est)); 
     1654#else 
     1655  debug_print(mod_srtp, "estimated u_packet index: %016llx", *est); 
     1656#endif 
     1657  return result; 
     1658} 
    9001659 
    9011660/* 
     
    9041663 * encrypted and authenticated. 
    9051664 */ 
    906 static err_status_t 
    907 srtp_protect_aead (srtp_ctx_t *ctx, srtp_stream_ctx_t *stream,  
    908                    void *rtp_hdr, unsigned int *pkt_octet_len) 
     1665static srtp_err_status_t 
     1666srtp_protect_aead (srtp_ctx_t *ctx, srtp_stream_ctx_t *stream, 
     1667                   void *rtp_hdr, unsigned int *pkt_octet_len, 
     1668                   srtp_session_keys_t *session_keys, unsigned int use_mki) 
    9091669{ 
    9101670    srtp_hdr_t *hdr = (srtp_hdr_t*)rtp_hdr; 
    9111671    uint32_t *enc_start;        /* pointer to start of encrypted portion  */ 
    9121672    int enc_octet_len = 0; /* number of octets in encrypted portion  */ 
    913     xtd_seq_num_t est;          /* estimated xtd_seq_num_t of *hdr        */ 
     1673    srtp_xtd_seq_num_t est;          /* estimated xtd_seq_num_t of *hdr        */ 
    9141674    int delta;                  /* delta of local pkt idx and that in hdr */ 
    915     err_status_t status; 
    916     int tag_len; 
     1675    srtp_err_status_t status; 
     1676    uint32_t tag_len; 
    9171677    v128_t iv; 
    9181678    unsigned int aad_len; 
     1679    srtp_hdr_xtnd_t *xtn_hdr = NULL; 
     1680    unsigned int mki_size = 0; 
     1681    uint8_t *mki_location = NULL; 
    9191682 
    9201683    debug_print(mod_srtp, "function srtp_protect_aead", NULL); 
     
    9251688     * the event handler if we hit either. 
    9261689     */ 
    927     switch (key_limit_update(stream->limit)) { 
    928     case key_event_normal: 
     1690    switch (srtp_key_limit_update(session_keys->limit)) { 
     1691    case srtp_key_event_normal: 
    9291692        break; 
    930     case key_event_hard_limit: 
     1693    case srtp_key_event_hard_limit: 
    9311694        srtp_handle_event(ctx, stream, event_key_hard_limit); 
    932         return err_status_key_expired; 
    933     case key_event_soft_limit: 
     1695        return srtp_err_status_key_expired; 
     1696    case srtp_key_event_soft_limit: 
    9341697    default: 
    9351698        srtp_handle_event(ctx, stream, event_key_soft_limit); 
     
    9381701 
    9391702    /* get tag length from stream */ 
    940     tag_len = auth_get_tag_length(stream->rtp_auth); 
     1703    tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth); 
    9411704 
    9421705    /* 
     
    9481711     enc_start = (uint32_t*)hdr + uint32s_in_rtp_header + hdr->cc; 
    9491712     if (hdr->x == 1) { 
    950          srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t*)enc_start; 
     1713         xtn_hdr = (srtp_hdr_xtnd_t*)enc_start; 
    9511714         enc_start += (ntohs(xtn_hdr->length) + 1); 
    9521715     } 
     1716     /* note: the passed size is without the auth tag */ 
    9531717     if (!((uint8_t*)enc_start <= (uint8_t*)hdr + *pkt_octet_len)) 
    954          return err_status_parse_err; 
     1718         return srtp_err_status_parse_err; 
    9551719     enc_octet_len = (int)(*pkt_octet_len - 
    9561720                                    ((uint8_t*)enc_start - (uint8_t*)hdr)); 
    957      if (enc_octet_len < 0) return err_status_parse_err; 
     1721     if (enc_octet_len < 0) return srtp_err_status_parse_err; 
    9581722 
    9591723    /* 
     
    9611725     * and the sequence number from the header 
    9621726     */ 
    963     delta = rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq)); 
    964     status = rdbx_check(&stream->rtp_rdbx, delta); 
     1727    delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq)); 
     1728    status = srtp_rdbx_check(&stream->rtp_rdbx, delta); 
    9651729    if (status) { 
    966         if (status != err_status_replay_fail || !stream->allow_repeat_tx) { 
     1730        if (status != srtp_err_status_replay_fail || !stream->allow_repeat_tx) { 
    9671731            return status;  /* we've been asked to reuse an index */ 
    9681732        } 
    9691733    } else { 
    970         rdbx_add_index(&stream->rtp_rdbx, delta); 
     1734        srtp_rdbx_add_index(&stream->rtp_rdbx, delta); 
    9711735    } 
    9721736 
     
    9811745     * AEAD uses a new IV formation method 
    9821746     */ 
    983     srtp_calc_aead_iv(stream, &iv, &est, hdr); 
    984     status = cipher_set_iv(stream->rtp_cipher, &iv, direction_encrypt); 
    985     if (status) { 
    986         return err_status_cipher_fail; 
    987     } 
    988  
     1747    srtp_calc_aead_iv(session_keys, &iv, &est, hdr); 
    9891748    /* shift est, put into network byte order */ 
    9901749#ifdef NO_64BIT_MATH 
     
    9961755#endif 
    9971756 
     1757    status = srtp_cipher_set_iv(session_keys->rtp_cipher, 
     1758                                (uint8_t*)&iv, srtp_direction_encrypt); 
     1759    if (!status && session_keys->rtp_xtn_hdr_cipher) { 
     1760      iv.v32[0] = 0; 
     1761      iv.v32[1] = hdr->ssrc; 
     1762      iv.v64[1] = est; 
     1763      status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, 
     1764                                  (uint8_t*)&iv, srtp_direction_encrypt); 
     1765    } 
     1766    if (status) { 
     1767        return srtp_err_status_cipher_fail; 
     1768    } 
     1769 
     1770    if (xtn_hdr && session_keys->rtp_xtn_hdr_cipher) { 
     1771      /* 
     1772       * extensions header encryption RFC 6904 
     1773       */ 
     1774        status = srtp_process_header_encryption(stream, xtn_hdr, session_keys); 
     1775      if (status) { 
     1776        return status; 
     1777      } 
     1778    } 
     1779 
    9981780    /* 
    9991781     * Set the AAD over the RTP header  
    10001782     */ 
    10011783    aad_len = (uint8_t *)enc_start - (uint8_t *)hdr; 
    1002     status = cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len); 
     1784    status = srtp_cipher_set_aad(session_keys->rtp_cipher, (uint8_t*)hdr, aad_len); 
    10031785    if (status) { 
    1004         return ( err_status_cipher_fail); 
     1786        return ( srtp_err_status_cipher_fail); 
    10051787    } 
    10061788 
    10071789    /* Encrypt the payload  */ 
    1008     status = cipher_encrypt(stream->rtp_cipher, 
     1790    status = srtp_cipher_encrypt(session_keys->rtp_cipher, 
    10091791                            (uint8_t*)enc_start, (unsigned int *)&enc_octet_len); 
    10101792    if (status) { 
    1011         return err_status_cipher_fail; 
     1793        return srtp_err_status_cipher_fail; 
    10121794    } 
    10131795    /* 
     
    10151797     * and append that to the output 
    10161798     */ 
    1017     status = cipher_get_tag(stream->rtp_cipher,  
     1799    status = srtp_cipher_get_tag(session_keys->rtp_cipher,  
    10181800                            (uint8_t*)enc_start+enc_octet_len, &tag_len); 
    10191801    if (status) { 
    1020         return ( err_status_cipher_fail); 
    1021     } 
     1802        return ( srtp_err_status_cipher_fail); 
     1803    } 
     1804 
     1805    mki_location = (uint8_t *)hdr + *pkt_octet_len + tag_len; 
     1806    mki_size = srtp_inject_mki(mki_location, session_keys, use_mki); 
    10221807 
    10231808    /* increase the packet length by the length of the auth tag */ 
    10241809    *pkt_octet_len += tag_len; 
    10251810 
    1026     return err_status_ok; 
     1811    /* increase the packet length by the length of the mki_size */ 
     1812    *pkt_octet_len += mki_size; 
     1813 
     1814    return srtp_err_status_ok; 
    10271815} 
    10281816 
     
    10351823 * when decrypting the payload. 
    10361824 */ 
    1037 static err_status_t 
     1825static srtp_err_status_t 
    10381826srtp_unprotect_aead (srtp_ctx_t *ctx, srtp_stream_ctx_t *stream, int delta,  
    1039                      xtd_seq_num_t est, void *srtp_hdr, unsigned int *pkt_octet_len) 
     1827                     srtp_xtd_seq_num_t est, void *srtp_hdr, unsigned int *pkt_octet_len, 
     1828                     srtp_session_keys_t *session_keys, unsigned int mki_size) 
    10401829{ 
    10411830    srtp_hdr_t *hdr = (srtp_hdr_t*)srtp_hdr; 
     
    10431832    unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */ 
    10441833    v128_t iv; 
    1045     err_status_t status; 
     1834    srtp_err_status_t status; 
    10461835    int tag_len; 
    10471836    unsigned int aad_len; 
     1837    srtp_hdr_xtnd_t *xtn_hdr = NULL; 
    10481838 
    10491839    debug_print(mod_srtp, "function srtp_unprotect_aead", NULL); 
     
    10561846 
    10571847    /* get tag length from stream */ 
    1058     tag_len = auth_get_tag_length(stream->rtp_auth); 
     1848    tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth); 
    10591849 
    10601850    /* 
    10611851     * AEAD uses a new IV formation method  
    10621852     */ 
    1063     srtp_calc_aead_iv(stream, &iv, &est, hdr); 
    1064     status = cipher_set_iv(stream->rtp_cipher, &iv, direction_decrypt); 
     1853    srtp_calc_aead_iv(session_keys, &iv, &est, hdr); 
     1854    status = srtp_cipher_set_iv(session_keys->rtp_cipher, 
     1855                                (uint8_t*)&iv, srtp_direction_decrypt); 
     1856    if (!status && session_keys->rtp_xtn_hdr_cipher) { 
     1857      iv.v32[0] = 0; 
     1858      iv.v32[1] = hdr->ssrc; 
     1859#ifdef NO_64BIT_MATH 
     1860      iv.v64[1] = be64_to_cpu(make64((high32(est) << 16) | (low32(est) >> 16), 
     1861                  low32(est) << 16)); 
     1862#else 
     1863      iv.v64[1] = be64_to_cpu(est << 16); 
     1864#endif 
     1865      status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, (uint8_t*)&iv, srtp_direction_encrypt); 
     1866    } 
    10651867    if (status) { 
    1066         return err_status_cipher_fail; 
     1868        return srtp_err_status_cipher_fail; 
    10671869    } 
    10681870 
     
    10751877    enc_start = (uint32_t*)hdr + uint32s_in_rtp_header + hdr->cc; 
    10761878    if (hdr->x == 1) { 
    1077         srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t*)enc_start; 
     1879        xtn_hdr = (srtp_hdr_xtnd_t*)enc_start; 
    10781880        enc_start += (ntohs(xtn_hdr->length) + 1); 
    10791881    } 
    1080     if (!((uint8_t*)enc_start <= (uint8_t*)hdr + (*pkt_octet_len - tag_len))) 
    1081         return err_status_parse_err; 
     1882    if (!((uint8_t*)enc_start <= (uint8_t*)hdr + (*pkt_octet_len - tag_len - mki_size))) 
     1883        return srtp_err_status_parse_err; 
    10821884    /* 
    10831885     * We pass the tag down to the cipher when doing GCM mode  
    10841886     */ 
    1085     enc_octet_len = (unsigned int)(*pkt_octet_len -  
     1887    enc_octet_len = (unsigned int)(*pkt_octet_len - mki_size - 
    10861888                                   ((uint8_t*)enc_start - (uint8_t*)hdr)); 
    10871889 
     
    10921894     */ 
    10931895    if (enc_octet_len < (unsigned int) tag_len) { 
    1094         return err_status_cipher_fail; 
     1896        return srtp_err_status_cipher_fail; 
    10951897    } 
    10961898 
     
    11001902     * the event handler if we hit either. 
    11011903     */ 
    1102     switch (key_limit_update(stream->limit)) { 
    1103     case key_event_normal: 
     1904    switch (srtp_key_limit_update(session_keys->limit)) { 
     1905    case srtp_key_event_normal: 
    11041906        break; 
    1105     case key_event_soft_limit: 
     1907    case srtp_key_event_soft_limit: 
    11061908        srtp_handle_event(ctx, stream, event_key_soft_limit); 
    11071909        break; 
    1108     case key_event_hard_limit: 
     1910    case srtp_key_event_hard_limit: 
    11091911        srtp_handle_event(ctx, stream, event_key_hard_limit); 
    1110         return err_status_key_expired; 
     1912        return srtp_err_status_key_expired; 
    11111913    default: 
    11121914        break; 
     
    11171919     */ 
    11181920    aad_len = (uint8_t *)enc_start - (uint8_t *)hdr; 
    1119     status = cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len); 
     1921    status = srtp_cipher_set_aad(session_keys->rtp_cipher, (uint8_t*)hdr, aad_len); 
    11201922    if (status) { 
    1121         return ( err_status_cipher_fail); 
    1122     } 
    1123  
    1124     /* Decrypt the ciphertext.  This also checks the auth tag based  
     1923        return ( srtp_err_status_cipher_fail); 
     1924    } 
     1925 
     1926    /* Decrypt the ciphertext.  This also checks the auth tag based 
    11251927     * on the AAD we just specified above */ 
    1126     status = cipher_decrypt(stream->rtp_cipher, 
    1127                             (uint8_t*)enc_start, &enc_octet_len); 
     1928    status = srtp_cipher_decrypt(session_keys->rtp_cipher, 
     1929                                 (uint8_t*)enc_start, &enc_octet_len); 
    11281930    if (status) { 
    11291931        return status; 
     1932    } 
     1933 
     1934    if (xtn_hdr && session_keys->rtp_xtn_hdr_cipher) { 
     1935      /* 
     1936       * extensions header encryption RFC 6904 
     1937       */ 
     1938      status = srtp_process_header_encryption(stream, xtn_hdr, session_keys); 
     1939      if (status) { 
     1940        return status; 
     1941      } 
    11301942    } 
    11311943 
     
    11801992     * index into the replay database 
    11811993     */ 
    1182     rdbx_add_index(&stream->rtp_rdbx, delta); 
     1994    srtp_rdbx_add_index(&stream->rtp_rdbx, delta); 
    11831995 
    11841996    /* decrease the packet length by the length of the auth tag */ 
    11851997    *pkt_octet_len -= tag_len; 
    11861998 
    1187     return err_status_ok; 
    1188 } 
    1189  
    1190  
    1191  
    1192  
    1193  err_status_t 
     1999    /* decrease the packet length by the length of the mki_size */ 
     2000    *pkt_octet_len -= mki_size; 
     2001 
     2002    return srtp_err_status_ok; 
     2003} 
     2004 
     2005 
     2006 srtp_err_status_t 
    11942007 srtp_protect(srtp_ctx_t *ctx, void *rtp_hdr, int *pkt_octet_len) { 
     2008   return srtp_protect_mki(ctx, rtp_hdr, pkt_octet_len, 0, 0); 
     2009 } 
     2010 
     2011srtp_err_status_t 
     2012srtp_protect_mki(srtp_ctx_t *ctx, void *rtp_hdr, int *pkt_octet_len, 
     2013                 unsigned int use_mki, unsigned int mki_index ) { 
    11952014   srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr; 
    11962015   uint32_t *enc_start;        /* pointer to start of encrypted portion  */ 
    11972016   uint32_t *auth_start;       /* pointer to start of auth. portion      */ 
    11982017   int enc_octet_len = 0; /* number of octets in encrypted portion  */ 
    1199    xtd_seq_num_t est;          /* estimated xtd_seq_num_t of *hdr        */ 
     2018   srtp_xtd_seq_num_t est;          /* estimated xtd_seq_num_t of *hdr        */ 
    12002019   int delta;                  /* delta of local pkt idx and that in hdr */ 
    12012020   uint8_t *auth_tag = NULL;   /* location of auth_tag within packet     */ 
    1202    err_status_t status;    
     2021   srtp_err_status_t status;    
    12032022   int tag_len; 
    12042023   srtp_stream_ctx_t *stream; 
    1205    int prefix_len; 
     2024   uint32_t prefix_len; 
     2025   srtp_hdr_xtnd_t *xtn_hdr = NULL; 
     2026   unsigned int mki_size = 0; 
     2027   srtp_session_keys_t *session_keys = NULL; 
     2028   uint8_t* mki_location = NULL; 
     2029   int advance_packet_index = 0; 
    12062030 
    12072031   debug_print(mod_srtp, "function srtp_protect", NULL); 
     
    12162040   /* check the packet length - it must at least contain a full header */ 
    12172041   if (*pkt_octet_len < octets_in_rtp_header) 
    1218      return err_status_bad_param; 
     2042     return srtp_err_status_bad_param; 
    12192043 
    12202044   /* 
     
    12472071     } else { 
    12482072       /* no template stream, so we return an error */ 
    1249        return err_status_no_ctx; 
     2073       return srtp_err_status_no_ctx; 
    12502074     }  
    12512075   } 
     
    12572081    * those functions. 
    12582082    */ 
     2083 
    12592084  if (stream->direction != dir_srtp_sender) { 
    12602085     if (stream->direction == dir_unknown) { 
     
    12652090  } 
    12662091 
     2092  session_keys = srtp_get_session_keys_with_mki_index(stream, use_mki, mki_index); 
     2093 
    12672094   /* 
    12682095    * Check if this is an AEAD stream (GCM mode).  If so, then dispatch 
    12692096    * the request to our AEAD handler. 
    12702097    */ 
    1271   if (stream->rtp_cipher->algorithm == AES_128_GCM || 
    1272       stream->rtp_cipher->algorithm == AES_256_GCM) { 
    1273       return srtp_protect_aead(ctx, stream, rtp_hdr, (unsigned int*)pkt_octet_len); 
     2098  if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 || 
     2099      session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) { 
     2100      return srtp_protect_aead(ctx, stream, rtp_hdr, 
     2101                               (unsigned int*)pkt_octet_len, session_keys, 
     2102                               use_mki); 
    12742103  } 
    12752104 
     
    12792108   * the event handler if we hit either. 
    12802109   */ 
    1281   switch(key_limit_update(stream->limit)) { 
    1282   case key_event_normal: 
     2110  switch(srtp_key_limit_update(session_keys->limit)) { 
     2111  case srtp_key_event_normal: 
    12832112    break; 
    1284   case key_event_soft_limit:  
     2113  case srtp_key_event_soft_limit:  
    12852114    srtp_handle_event(ctx, stream, event_key_soft_limit); 
    12862115    break;  
    1287   case key_event_hard_limit: 
     2116  case srtp_key_event_hard_limit: 
    12882117    srtp_handle_event(ctx, stream, event_key_hard_limit); 
    1289         return err_status_key_expired; 
     2118        return srtp_err_status_key_expired; 
    12902119  default: 
    12912120    break; 
     
    12932122 
    12942123   /* get tag length from stream */ 
    1295    tag_len = auth_get_tag_length(stream->rtp_auth);  
     2124   tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth);  
    12962125 
    12972126   /* 
     
    13062135     enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;   
    13072136     if (hdr->x == 1) { 
    1308        srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t *)enc_start; 
     2137       xtn_hdr = (srtp_hdr_xtnd_t *)enc_start; 
    13092138       enc_start += (ntohs(xtn_hdr->length) + 1); 
    13102139     } 
     2140     /* note: the passed size is without the auth tag */ 
    13112141     if (!((uint8_t*)enc_start <= (uint8_t*)hdr + *pkt_octet_len)) 
    1312        return err_status_parse_err; 
     2142       return srtp_err_status_parse_err; 
    13132143     enc_octet_len = (int)(*pkt_octet_len - 
    13142144                                    ((uint8_t*)enc_start - (uint8_t*)hdr)); 
    1315      if (enc_octet_len < 0) return err_status_parse_err; 
     2145     if (enc_octet_len < 0) return srtp_err_status_parse_err; 
    13162146   } else { 
    13172147     enc_start = NULL; 
    13182148   } 
     2149 
     2150   mki_location = (uint8_t *)hdr + *pkt_octet_len; 
     2151   mki_size = srtp_inject_mki(mki_location, session_keys, use_mki); 
    13192152 
    13202153   /*  
     
    13252158   if (stream->rtp_services & sec_serv_auth) { 
    13262159     auth_start = (uint32_t *)hdr; 
    1327      auth_tag = (uint8_t *)hdr + *pkt_octet_len; 
     2160     auth_tag = (uint8_t *)hdr + *pkt_octet_len + mki_size; 
    13282161   } else { 
    13292162     auth_start = NULL; 
     
    13312164   } 
    13322165 
    1333    /* 
    1334     * estimate the packet index using the start of the replay window    
    1335     * and the sequence number from the header 
    1336     */ 
    1337    delta = rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq)); 
    1338    status = rdbx_check(&stream->rtp_rdbx, delta); 
    1339    if (status) { 
    1340      if (status != err_status_replay_fail || !stream->allow_repeat_tx) 
    1341        return status;  /* we've been asked to reuse an index */ 
    1342    } 
    1343    else 
    1344      rdbx_add_index(&stream->rtp_rdbx, delta); 
     2166  /* 
     2167   * estimate the packet index using the start of the replay window 
     2168   * and the sequence number from the header 
     2169   */ 
     2170  status = srtp_get_est_pkt_index(hdr, 
     2171                                  stream, 
     2172                                  &est, 
     2173                                  &delta); 
     2174 
     2175  if (status && (status != srtp_err_status_pkt_idx_adv)) 
     2176    return status; 
     2177 
     2178  if (status == srtp_err_status_pkt_idx_adv) 
     2179    advance_packet_index = 1; 
     2180 
     2181  if (advance_packet_index) { 
     2182    srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, 
     2183                          (uint32_t)(est >> 16), 
     2184                          (uint16_t)(est & 0xFFFF)); 
     2185    stream->pending_roc = 0; 
     2186    srtp_rdbx_add_index(&stream->rtp_rdbx, 0); 
     2187  } else { 
     2188    status = srtp_rdbx_check(&stream->rtp_rdbx, delta); 
     2189    if (status) { 
     2190      if (status != srtp_err_status_replay_fail || !stream->allow_repeat_tx) 
     2191        return status; /* we've been asked to reuse an index */ 
     2192    } 
     2193    srtp_rdbx_add_index(&stream->rtp_rdbx, delta); 
     2194  } 
    13452195 
    13462196#ifdef NO_64BIT_MATH 
     
    13542204    * if we're using rindael counter mode, set nonce and seq  
    13552205    */ 
    1356    if (stream->rtp_cipher->type->id == AES_ICM || 
    1357        stream->rtp_cipher->type->id == AES_256_ICM) { 
     2206   if (session_keys->rtp_cipher->type->id == SRTP_AES_ICM_128 || 
     2207       session_keys->rtp_cipher->type->id == SRTP_AES_ICM_192 || 
     2208       session_keys->rtp_cipher->type->id == SRTP_AES_ICM_256) { 
    13582209     v128_t iv; 
    13592210 
     
    13662217     iv.v64[1] = be64_to_cpu(est << 16); 
    13672218#endif 
    1368      status = cipher_set_iv(stream->rtp_cipher, &iv, direction_encrypt); 
    1369  
     2219     status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t*)&iv, srtp_direction_encrypt); 
     2220     if (!status && session_keys->rtp_xtn_hdr_cipher) { 
     2221       status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, (uint8_t*)&iv, srtp_direction_encrypt); 
     2222     } 
    13702223   } else {   
    13712224     v128_t iv; 
     
    13792232#endif 
    13802233     iv.v64[1] = be64_to_cpu(est); 
    1381      status = cipher_set_iv(stream->rtp_cipher, &iv, direction_encrypt); 
     2234     status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t*)&iv, srtp_direction_encrypt); 
     2235     if (!status && session_keys->rtp_xtn_hdr_cipher) { 
     2236       status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, (uint8_t*)&iv, srtp_direction_encrypt); 
     2237     } 
    13822238   } 
    13832239   if (status) 
    1384      return err_status_cipher_fail; 
     2240     return srtp_err_status_cipher_fail; 
    13852241 
    13862242   /* shift est, put into network byte order */ 
     
    13992255   if (auth_start) { 
    14002256      
    1401     prefix_len = auth_get_prefix_length(stream->rtp_auth);     
     2257    prefix_len = srtp_auth_get_prefix_length(session_keys->rtp_auth);     
    14022258    if (prefix_len) { 
    1403       status = cipher_output(stream->rtp_cipher, auth_tag, prefix_len); 
     2259      status = srtp_cipher_output(session_keys->rtp_cipher, auth_tag, &prefix_len); 
    14042260      if (status) 
    1405         return err_status_cipher_fail; 
     2261        return srtp_err_status_cipher_fail; 
    14062262      debug_print(mod_srtp, "keystream prefix: %s",  
    1407                   octet_string_hex_string(auth_tag, prefix_len)); 
     2263                  srtp_octet_string_hex_string(auth_tag, prefix_len)); 
     2264    } 
     2265  } 
     2266 
     2267  if (xtn_hdr && session_keys->rtp_xtn_hdr_cipher) { 
     2268    /* 
     2269     * extensions header encryption RFC 6904 
     2270     */ 
     2271      status = srtp_process_header_encryption(stream, xtn_hdr, session_keys); 
     2272    if (status) { 
     2273      return status; 
    14082274    } 
    14092275  } 
     
    14112277  /* if we're encrypting, exor keystream into the message */ 
    14122278  if (enc_start) { 
    1413     status = cipher_encrypt(stream->rtp_cipher,  
    1414                             (uint8_t *)enc_start, (unsigned int*)&enc_octet_len); 
     2279    status = srtp_cipher_encrypt(session_keys->rtp_cipher,  
     2280                                (uint8_t *)enc_start, (unsigned int *)&enc_octet_len); 
    14152281    if (status) 
    1416       return err_status_cipher_fail; 
     2282      return srtp_err_status_cipher_fail; 
    14172283  } 
    14182284 
     
    14242290 
    14252291    /* initialize auth func context */ 
    1426     status = auth_start(stream->rtp_auth); 
     2292    status = srtp_auth_start(session_keys->rtp_auth); 
    14272293    if (status) return status; 
    14282294 
    14292295    /* run auth func over packet */ 
    1430     status = auth_update(stream->rtp_auth,  
     2296    status = srtp_auth_update(session_keys->rtp_auth, 
    14312297                         (uint8_t *)auth_start, *pkt_octet_len); 
    14322298    if (status) return status; 
     
    14342300    /* run auth func over ROC, put result into auth_tag */ 
    14352301    debug_print(mod_srtp, "estimated packet index: %016llx", est); 
    1436     status = auth_compute(stream->rtp_auth, (uint8_t *)&est, 4, auth_tag);  
     2302    status = srtp_auth_compute(session_keys->rtp_auth, (uint8_t *)&est, 4, auth_tag); 
    14372303    debug_print(mod_srtp, "srtp auth tag:    %s",  
    1438                 octet_string_hex_string(auth_tag, tag_len)); 
     2304                srtp_octet_string_hex_string(auth_tag, tag_len)); 
    14392305    if (status) 
    1440       return err_status_auth_fail;    
     2306      return srtp_err_status_auth_fail;    
    14412307 
    14422308  } 
     
    14482314  } 
    14492315 
    1450   return err_status_ok;   
    1451 } 
    1452  
    1453  
    1454 err_status_t 
     2316  if (use_mki) { 
     2317    /* increate the packet length by the mki size */ 
     2318    *pkt_octet_len += mki_size; 
     2319  } 
     2320 
     2321  return srtp_err_status_ok;   
     2322} 
     2323 
     2324 
     2325srtp_err_status_t 
    14552326srtp_unprotect(srtp_ctx_t *ctx, void *srtp_hdr, int *pkt_octet_len) { 
     2327    return srtp_unprotect_mki(ctx, srtp_hdr, pkt_octet_len, 0); 
     2328} 
     2329 
     2330srtp_err_status_t 
     2331srtp_unprotect_mki(srtp_ctx_t *ctx, void *srtp_hdr, int *pkt_octet_len, 
     2332                   unsigned int use_mki) { 
    14562333  srtp_hdr_t *hdr = (srtp_hdr_t *)srtp_hdr; 
    14572334  uint32_t *enc_start;      /* pointer to start of encrypted portion  */ 
     
    14592336  unsigned int enc_octet_len = 0;/* number of octets in encrypted portion */ 
    14602337  uint8_t *auth_tag = NULL; /* location of auth_tag within packet     */ 
    1461   xtd_seq_num_t est;        /* estimated xtd_seq_num_t of *hdr        */ 
     2338  srtp_xtd_seq_num_t est;        /* estimated xtd_seq_num_t of *hdr        */ 
    14622339  int delta;                /* delta of local pkt idx and that in hdr */ 
    14632340  v128_t iv; 
    1464   err_status_t status; 
     2341  srtp_err_status_t status; 
    14652342  srtp_stream_ctx_t *stream; 
    14662343  uint8_t tmp_tag[SRTP_MAX_TAG_LEN]; 
    1467   int tag_len, prefix_len; 
     2344  uint32_t tag_len, prefix_len; 
     2345  srtp_hdr_xtnd_t *xtn_hdr = NULL; 
     2346  unsigned int mki_size = 0; 
     2347  srtp_session_keys_t *session_keys = NULL; 
     2348  int advance_packet_index = 0; 
     2349  uint32_t roc_to_set = 0; 
     2350  uint16_t seq_to_set = 0; 
    14682351 
    14692352  debug_print(mod_srtp, "function srtp_unprotect", NULL); 
     
    14782361  /* check the packet length - it must at least contain a full header */ 
    14792362  if (*pkt_octet_len < octets_in_rtp_header) 
    1480     return err_status_bad_param; 
     2363    return srtp_err_status_bad_param; 
    14812364 
    14822365  /* 
     
    14922375      stream = ctx->stream_template; 
    14932376      debug_print(mod_srtp, "using provisional stream (SSRC: 0x%08x)", 
    1494                   hdr->ssrc); 
     2377                  ntohl(hdr->ssrc)); 
    14952378       
    14962379      /*  
     
    14992382       */ 
    15002383#ifdef NO_64BIT_MATH 
    1501       est = (xtd_seq_num_t) make64(0,ntohs(hdr->seq)); 
     2384      est = (srtp_xtd_seq_num_t) make64(0,ntohs(hdr->seq)); 
    15022385      delta = low32(est); 
    15032386#else 
    1504       est = (xtd_seq_num_t) ntohs(hdr->seq); 
     2387      est = (srtp_xtd_seq_num_t) ntohs(hdr->seq); 
    15052388      delta = (int)est; 
    15062389#endif 
     
    15112394       * key-sharing, so return an error 
    15122395       */ 
    1513       return err_status_no_ctx; 
     2396      return srtp_err_status_no_ctx; 
    15142397    } 
    15152398  } else { 
    1516    
    1517     /* estimate packet index from seq. num. in header */ 
    1518     delta = rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq)); 
    1519      
     2399    status = srtp_get_est_pkt_index(hdr, 
     2400                                    stream, 
     2401                                    &est, 
     2402                                    &delta); 
     2403 
     2404    if (status && (status != srtp_err_status_pkt_idx_adv)) 
     2405      return status; 
     2406 
     2407    if (status == srtp_err_status_pkt_idx_adv) { 
     2408      advance_packet_index = 1; 
     2409      roc_to_set = (uint32_t)(est >> 16); 
     2410      seq_to_set = (uint16_t)(est & 0xFFFF); 
     2411    } 
     2412 
    15202413    /* check replay database */ 
    1521     status = rdbx_check(&stream->rtp_rdbx, delta); 
    1522     if (status) 
    1523       return status; 
     2414    if (!advance_packet_index) { 
     2415      status = srtp_rdbx_check(&stream->rtp_rdbx, delta); 
     2416      if (status) 
     2417        return status; 
     2418    } 
    15242419  } 
    15252420 
     
    15312426 
    15322427  /* 
     2428   * Determine if MKI is being used and what session keys should be used 
     2429   */ 
     2430  if (use_mki) { 
     2431      session_keys = srtp_get_session_keys(stream, (uint8_t *)hdr, 
     2432                                           (const unsigned int*)pkt_octet_len, 
     2433                                           &mki_size); 
     2434 
     2435      if (session_keys == NULL)  
     2436         return srtp_err_status_bad_mki; 
     2437  } else { 
     2438      session_keys = &stream->session_keys[0]; 
     2439  } 
     2440 
     2441  /* 
    15332442   * Check if this is an AEAD stream (GCM mode).  If so, then dispatch 
    15342443   * the request to our AEAD handler. 
    15352444   */ 
    1536   if (stream->rtp_cipher->algorithm == AES_128_GCM || 
    1537       stream->rtp_cipher->algorithm == AES_256_GCM) { 
    1538       return srtp_unprotect_aead(ctx, stream, delta, est, srtp_hdr, (unsigned int*)pkt_octet_len); 
     2445  if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 || 
     2446      session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) { 
     2447      return srtp_unprotect_aead(ctx, stream, delta, est, srtp_hdr, 
     2448                                 (unsigned int*)pkt_octet_len, session_keys, 
     2449                                 mki_size); 
    15392450  } 
    15402451 
    15412452  /* get tag length from stream */ 
    1542   tag_len = auth_get_tag_length(stream->rtp_auth);  
     2453  tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth);  
    15432454 
    15442455  /*  
     
    15462457   * happen to be using 
    15472458   */ 
    1548   if (stream->rtp_cipher->type->id == AES_ICM || 
    1549       stream->rtp_cipher->type->id == AES_256_ICM) { 
    1550  
     2459  if (session_keys->rtp_cipher->type->id == SRTP_AES_ICM_128 || 
     2460      session_keys->rtp_cipher->type->id == SRTP_AES_ICM_192 || 
     2461      session_keys->rtp_cipher->type->id == SRTP_AES_ICM_256) { 
    15512462    /* aes counter mode */ 
    15522463    iv.v32[0] = 0; 
     
    15582469    iv.v64[1] = be64_to_cpu(est << 16); 
    15592470#endif 
    1560     status = cipher_set_iv(stream->rtp_cipher, &iv, direction_decrypt); 
     2471    status = srtp_cipher_set_iv(session_keys->rtp_cipher, 
     2472                                (uint8_t*)&iv, srtp_direction_decrypt); 
     2473    if (!status && session_keys->rtp_xtn_hdr_cipher) { 
     2474      status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, 
     2475                                  (uint8_t*)&iv, srtp_direction_decrypt); 
     2476    } 
    15612477  } else {   
    15622478     
     
    15692485#endif 
    15702486    iv.v64[1] = be64_to_cpu(est); 
    1571     status = cipher_set_iv(stream->rtp_cipher, &iv, direction_decrypt); 
     2487    status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t*)&iv, srtp_direction_decrypt); 
     2488    if (!status && session_keys->rtp_xtn_hdr_cipher) { 
     2489      status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, (uint8_t*)&iv, srtp_direction_decrypt); 
     2490    } 
    15722491  } 
    15732492  if (status) 
    1574     return err_status_cipher_fail; 
     2493    return srtp_err_status_cipher_fail; 
    15752494 
    15762495  /* shift est, put into network byte order */ 
     
    15942513    enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;   
    15952514    if (hdr->x == 1) { 
    1596       srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t *)enc_start; 
     2515      xtn_hdr = (srtp_hdr_xtnd_t *)enc_start; 
    15972516      enc_start += (ntohs(xtn_hdr->length) + 1); 
    15982517    }   
    1599     if (!((uint8_t*)enc_start <= (uint8_t*)hdr + (*pkt_octet_len - tag_len))) 
    1600       return err_status_parse_err; 
    1601     enc_octet_len = (uint32_t)(*pkt_octet_len - tag_len - 
     2518    if (!((uint8_t*)enc_start <= (uint8_t*)hdr + (*pkt_octet_len - tag_len - mki_size))) 
     2519      return srtp_err_status_parse_err; 
     2520    enc_octet_len = (uint32_t)(*pkt_octet_len - tag_len - mki_size - 
    16022521                               ((uint8_t*)enc_start - (uint8_t*)hdr)); 
    16032522  } else { 
     
    16312550     * the authenticator isn't using a universal hash function 
    16322551     */   
    1633     if (stream->rtp_auth->prefix_len != 0) { 
     2552    if (session_keys->rtp_auth->prefix_len != 0) { 
    16342553       
    1635       prefix_len = auth_get_prefix_length(stream->rtp_auth);     
    1636       status = cipher_output(stream->rtp_cipher, tmp_tag, prefix_len); 
     2554      prefix_len = srtp_auth_get_prefix_length(session_keys->rtp_auth);     
     2555      status = srtp_cipher_output(session_keys->rtp_cipher, tmp_tag, &prefix_len); 
    16372556      debug_print(mod_srtp, "keystream prefix: %s",  
    1638                   octet_string_hex_string(tmp_tag, prefix_len)); 
     2557                  srtp_octet_string_hex_string(tmp_tag, prefix_len)); 
    16392558      if (status) 
    1640         return err_status_cipher_fail; 
     2559        return srtp_err_status_cipher_fail; 
    16412560    }  
    16422561 
    16432562    /* initialize auth func context */ 
    1644     status = auth_start(stream->rtp_auth); 
     2563    status = srtp_auth_start(session_keys->rtp_auth); 
    16452564    if (status) return status; 
    16462565  
    16472566    /* now compute auth function over packet */ 
    1648     status = auth_update(stream->rtp_auth, (uint8_t *)auth_start,   
    1649                          *pkt_octet_len - tag_len); 
     2567    status = srtp_auth_update(session_keys->rtp_auth, (uint8_t *)auth_start, 
     2568                         *pkt_octet_len - tag_len - mki_size); 
    16502569 
    16512570    /* run auth func over ROC, then write tmp tag */ 
    1652     status = auth_compute(stream->rtp_auth, (uint8_t *)&est, 4, tmp_tag);   
     2571    status = srtp_auth_compute(session_keys->rtp_auth, (uint8_t *)&est, 4, tmp_tag); 
    16532572 
    16542573    debug_print(mod_srtp, "computed auth tag:    %s",  
    1655                 octet_string_hex_string(tmp_tag, tag_len)); 
     2574                srtp_octet_string_hex_string(tmp_tag, tag_len)); 
    16562575    debug_print(mod_srtp, "packet auth tag:      %s",  
    1657                 octet_string_hex_string(auth_tag, tag_len)); 
     2576                srtp_octet_string_hex_string(auth_tag, tag_len)); 
    16582577    if (status) 
    1659       return err_status_auth_fail;    
     2578      return srtp_err_status_auth_fail;    
    16602579 
    16612580    if (octet_string_is_eq(tmp_tag, auth_tag, tag_len)) 
    1662       return err_status_auth_fail; 
     2581      return srtp_err_status_auth_fail; 
    16632582  } 
    16642583 
     
    16682587   * the event handler if we hit either. 
    16692588   */ 
    1670   switch(key_limit_update(stream->limit)) { 
    1671   case key_event_normal: 
     2589  switch(srtp_key_limit_update(session_keys->limit)) { 
     2590  case srtp_key_event_normal: 
    16722591    break; 
    1673   case key_event_soft_limit:  
     2592  case srtp_key_event_soft_limit:  
    16742593    srtp_handle_event(ctx, stream, event_key_soft_limit); 
    16752594    break;  
    1676   case key_event_hard_limit: 
     2595  case srtp_key_event_hard_limit: 
    16772596    srtp_handle_event(ctx, stream, event_key_hard_limit); 
    1678     return err_status_key_expired; 
     2597    return srtp_err_status_key_expired; 
    16792598  default: 
    16802599    break; 
    16812600  } 
    16822601 
     2602  if (xtn_hdr && session_keys->rtp_xtn_hdr_cipher) { 
     2603    /* 
     2604     * extensions header encryption RFC 6904 
     2605     */ 
     2606      status = srtp_process_header_encryption(stream, xtn_hdr, session_keys); 
     2607    if (status) { 
     2608      return status; 
     2609    } 
     2610  } 
     2611 
    16832612  /* if we're decrypting, add keystream into ciphertext */ 
    16842613  if (enc_start) { 
    1685     status = cipher_decrypt(stream->rtp_cipher,  
    1686                             (uint8_t *)enc_start, &enc_octet_len); 
     2614    status = srtp_cipher_decrypt(session_keys->rtp_cipher, 
     2615                                 (uint8_t *)enc_start, &enc_octet_len); 
    16872616    if (status) 
    1688       return err_status_cipher_fail; 
     2617      return srtp_err_status_cipher_fail; 
    16892618  } 
    16902619 
     
    17332662    stream = new_stream; 
    17342663  } 
    1735    
     2664 
    17362665  /*  
    17372666   * the message authentication function passed, so add the packet 
    17382667   * index into the replay database  
    17392668   */ 
    1740   rdbx_add_index(&stream->rtp_rdbx, delta); 
     2669  if (advance_packet_index) { 
     2670    srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, 
     2671                           roc_to_set, 
     2672                           seq_to_set); 
     2673    stream->pending_roc = 0; 
     2674    srtp_rdbx_add_index(&stream->rtp_rdbx, 0); 
     2675  } else { 
     2676    srtp_rdbx_add_index(&stream->rtp_rdbx, delta); 
     2677  } 
    17412678 
    17422679  /* decrease the packet length by the length of the auth tag */ 
    17432680  *pkt_octet_len -= tag_len; 
    17442681 
    1745   return err_status_ok;   
    1746 } 
    1747  
    1748 err_status_t 
     2682  /* decrease the packet length by the mki size */ 
     2683  *pkt_octet_len -= mki_size; 
     2684 
     2685  return srtp_err_status_ok;   
     2686} 
     2687 
     2688srtp_err_status_t 
    17492689srtp_init() { 
    1750   err_status_t status; 
     2690  srtp_err_status_t status; 
    17512691 
    17522692  /* initialize crypto kernel */ 
    1753   status = crypto_kernel_init(); 
     2693  status = srtp_crypto_kernel_init(); 
    17542694  if (status)  
    17552695    return status; 
    17562696 
    17572697  /* load srtp debug module into the kernel */ 
    1758   status = crypto_kernel_load_debug_module(&mod_srtp); 
     2698  status = srtp_crypto_kernel_load_debug_module(&mod_srtp); 
    17592699  if (status) 
    17602700    return status; 
    17612701 
    1762   return err_status_ok; 
    1763 } 
    1764  
    1765 err_status_t 
     2702  return srtp_err_status_ok; 
     2703} 
     2704 
     2705srtp_err_status_t 
    17662706srtp_shutdown() { 
    1767   err_status_t status; 
     2707  srtp_err_status_t status; 
    17682708 
    17692709  /* shut down crypto kernel */ 
    1770   status = crypto_kernel_shutdown(); 
     2710  status = srtp_crypto_kernel_shutdown(); 
    17712711  if (status)  
    17722712    return status; 
     
    17742714  /* shutting down crypto kernel frees the srtp debug module as well */ 
    17752715 
    1776   return err_status_ok; 
     2716  return srtp_err_status_ok; 
    17772717} 
    17782718 
     
    17922732int 
    17932733srtp_get_trailer_length(const srtp_stream_t s) { 
    1794   return auth_get_tag_length(s->rtp_auth); 
     2734  return srtp_auth_get_tag_length(s->rtp_auth); 
    17952735} 
    17962736 
     
    18202760} 
    18212761 
    1822 err_status_t 
     2762srtp_err_status_t 
    18232763srtp_dealloc(srtp_t session) { 
    18242764  srtp_stream_ctx_t *stream; 
    1825   err_status_t status; 
     2765  srtp_err_status_t status; 
    18262766 
    18272767  /* 
     
    18352775  while (stream != NULL) { 
    18362776    srtp_stream_t next = stream->next; 
    1837     status = srtp_stream_dealloc(session, stream); 
     2777    status = srtp_stream_dealloc(stream, session->stream_template); 
    18382778    if (status) 
    18392779      return status; 
     
    18432783  /* deallocate stream template, if there is one */ 
    18442784  if (session->stream_template != NULL) { 
    1845     status = auth_dealloc(session->stream_template->rtcp_auth);  
    1846     if (status)  
    1847       return status;  
    1848     status = cipher_dealloc(session->stream_template->rtcp_cipher);  
    1849     if (status)  
    1850       return status;  
    1851     crypto_free(session->stream_template->limit); 
    1852     status = cipher_dealloc(session->stream_template->rtp_cipher);  
    1853     if (status)  
    1854       return status;  
    1855     status = auth_dealloc(session->stream_template->rtp_auth); 
     2785    status = srtp_stream_dealloc(session->stream_template, NULL); 
    18562786    if (status) 
    18572787      return status; 
    1858     status = rdbx_dealloc(&session->stream_template->rtp_rdbx); 
    1859     if (status) 
    1860       return status; 
    1861     crypto_free(session->stream_template); 
    18622788  } 
    18632789 
    18642790  /* deallocate session context */ 
    1865   crypto_free(session); 
    1866  
    1867   return err_status_ok; 
    1868 } 
    1869  
    1870  
    1871 err_status_t 
     2791  srtp_crypto_free(session); 
     2792 
     2793  return srtp_err_status_ok; 
     2794} 
     2795 
     2796 
     2797srtp_err_status_t 
    18722798srtp_add_stream(srtp_t session,  
    18732799                const srtp_policy_t *policy)  { 
    1874   err_status_t status; 
     2800  srtp_err_status_t status; 
    18752801  srtp_stream_t tmp; 
    18762802 
    18772803  /* sanity check arguments */ 
    1878   if ((session == NULL) || (policy == NULL) || (policy->key == NULL)) 
    1879     return err_status_bad_param; 
     2804  if ((session == NULL) || (policy == NULL) || (!srtp_validate_policy_master_keys(policy))) 
     2805    return srtp_err_status_bad_param; 
    18802806 
    18812807  /* allocate stream  */ 
     
    18882814  status = srtp_stream_init(tmp, policy); 
    18892815  if (status) { 
    1890     crypto_free(tmp); 
     2816    srtp_crypto_free(tmp); 
    18912817    return status; 
    18922818  } 
     
    19032829  case (ssrc_any_outbound): 
    19042830    if (session->stream_template) { 
    1905       return err_status_bad_param; 
     2831      return srtp_err_status_bad_param; 
    19062832    } 
    19072833    session->stream_template = tmp; 
     
    19102836  case (ssrc_any_inbound): 
    19112837    if (session->stream_template) { 
    1912       return err_status_bad_param; 
     2838      return srtp_err_status_bad_param; 
    19132839    } 
    19142840    session->stream_template = tmp; 
     
    19212847  case (ssrc_undefined): 
    19222848  default: 
    1923     crypto_free(tmp); 
    1924     return err_status_bad_param; 
     2849    srtp_crypto_free(tmp); 
     2850    return srtp_err_status_bad_param; 
    19252851  } 
    19262852     
    1927   return err_status_ok; 
    1928 } 
    1929  
    1930  
    1931 err_status_t 
     2853  return srtp_err_status_ok; 
     2854} 
     2855 
     2856 
     2857srtp_err_status_t 
    19322858srtp_create(srtp_t *session,               /* handle for session     */  
    19332859            const srtp_policy_t *policy) { /* SRTP policy (list)     */ 
    1934   err_status_t stat; 
     2860  srtp_err_status_t stat; 
    19352861  srtp_ctx_t *ctx; 
    19362862 
    19372863  /* sanity check arguments */ 
    19382864  if (session == NULL) 
    1939     return err_status_bad_param; 
     2865    return srtp_err_status_bad_param; 
    19402866 
    19412867  /* allocate srtp context and set ctx_ptr */ 
    1942   ctx = (srtp_ctx_t *) crypto_alloc(sizeof(srtp_ctx_t)); 
     2868  ctx = (srtp_ctx_t *) srtp_crypto_alloc(sizeof(srtp_ctx_t)); 
    19432869  if (ctx == NULL) 
    1944     return err_status_alloc_fail; 
     2870    return srtp_err_status_alloc_fail; 
    19452871  *session = ctx; 
    19462872 
     
    19582884      /* clean up everything */ 
    19592885      srtp_dealloc(*session); 
     2886      *session = NULL; 
    19602887      return stat; 
    19612888    }     
     
    19652892  } 
    19662893 
    1967   return err_status_ok; 
    1968 } 
    1969  
    1970  
    1971 err_status_t 
     2894  return srtp_err_status_ok; 
     2895} 
     2896 
     2897 
     2898srtp_err_status_t 
    19722899srtp_remove_stream(srtp_t session, uint32_t ssrc) { 
    19732900  srtp_stream_ctx_t *stream, *last_stream; 
    1974   err_status_t status; 
     2901  srtp_err_status_t status; 
    19752902 
    19762903  /* sanity check arguments */ 
    19772904  if (session == NULL) 
    1978     return err_status_bad_param; 
     2905    return srtp_err_status_bad_param; 
    19792906   
    19802907  /* find stream in list; complain if not found */ 
     
    19852912  } 
    19862913  if (stream == NULL) 
    1987     return err_status_no_ctx; 
     2914    return srtp_err_status_no_ctx; 
    19882915 
    19892916  /* remove stream from the list */ 
     
    19952922 
    19962923  /* deallocate the stream */ 
    1997   status = srtp_stream_dealloc(session, stream); 
     2924  status = srtp_stream_dealloc(stream, session->stream_template); 
    19982925  if (status) 
    19992926    return status; 
    20002927 
    2001   return err_status_ok; 
     2928  return srtp_err_status_ok; 
     2929} 
     2930 
     2931 
     2932srtp_err_status_t 
     2933srtp_update(srtp_t session, const srtp_policy_t *policy) { 
     2934  srtp_err_status_t stat; 
     2935 
     2936  /* sanity check arguments */ 
     2937  if ((session == NULL) || (policy == NULL) || (!srtp_validate_policy_master_keys(policy))) { 
     2938    return srtp_err_status_bad_param; 
     2939  } 
     2940 
     2941  while (policy != NULL) { 
     2942    stat = srtp_update_stream(session, policy); 
     2943    if (stat) { 
     2944      return stat; 
     2945    } 
     2946 
     2947    /* set policy to next item in list  */ 
     2948    policy = policy->next; 
     2949  } 
     2950  return srtp_err_status_ok; 
     2951} 
     2952 
     2953 
     2954static srtp_err_status_t 
     2955update_template_streams(srtp_t session, const srtp_policy_t *policy) { 
     2956  srtp_err_status_t status; 
     2957  srtp_stream_t new_stream_template; 
     2958  srtp_stream_t new_stream_list = NULL; 
     2959 
     2960  if (session->stream_template == NULL) { 
     2961    return srtp_err_status_bad_param; 
     2962  } 
     2963 
     2964  /* allocate new template stream  */ 
     2965  status = srtp_stream_alloc(&new_stream_template, policy); 
     2966  if (status) { 
     2967    return status; 
     2968  } 
     2969 
     2970  /* initialize new template stream  */ 
     2971  status = srtp_stream_init(new_stream_template, policy); 
     2972  if (status) { 
     2973    srtp_crypto_free(new_stream_template); 
     2974    return status; 
     2975  } 
     2976 
     2977  /* for all old templated streams */ 
     2978  for (;;) { 
     2979    srtp_stream_t stream; 
     2980    uint32_t ssrc; 
     2981    srtp_xtd_seq_num_t old_index; 
     2982    srtp_rdb_t old_rtcp_rdb; 
     2983 
     2984    stream = session->stream_list; 
     2985    while ((stream != NULL) && 
     2986           (stream->session_keys[0].rtp_auth != 
     2987            session->stream_template->session_keys[0].rtp_auth)) { 
     2988      stream = stream->next; 
     2989    } 
     2990    if (stream == NULL) { 
     2991      /* no more templated streams */ 
     2992      break; 
     2993    } 
     2994 
     2995    /* save old extendard seq */ 
     2996    ssrc = stream->ssrc; 
     2997    old_index = stream->rtp_rdbx.index; 
     2998    old_rtcp_rdb = stream->rtcp_rdb; 
     2999 
     3000    /* remove stream */ 
     3001    status = srtp_remove_stream(session, ssrc); 
     3002    if (status) { 
     3003      /* free new allocations */ 
     3004      while (new_stream_list != NULL) { 
     3005        srtp_stream_t next = new_stream_list->next; 
     3006        srtp_stream_dealloc(new_stream_list, new_stream_template); 
     3007        new_stream_list = next; 
     3008      } 
     3009      srtp_stream_dealloc(new_stream_template, NULL); 
     3010      return status; 
     3011    } 
     3012 
     3013    /* allocate and initialize a new stream */ 
     3014    status = srtp_stream_clone(new_stream_template, ssrc, &stream); 
     3015    if (status) { 
     3016      /* free new allocations */ 
     3017      while (new_stream_list != NULL) { 
     3018        srtp_stream_t next = new_stream_list->next; 
     3019        srtp_stream_dealloc(new_stream_list, new_stream_template); 
     3020        new_stream_list = next; 
     3021      } 
     3022      srtp_stream_dealloc(new_stream_template, NULL); 
     3023      return status; 
     3024    } 
     3025 
     3026    /* add new stream to the head of the new_stream_list */ 
     3027    stream->next = new_stream_list; 
     3028    new_stream_list = stream; 
     3029 
     3030    /* restore old extended seq */ 
     3031    stream->rtp_rdbx.index = old_index; 
     3032    stream->rtcp_rdb = old_rtcp_rdb; 
     3033  } 
     3034  /* dealloc old template */ 
     3035  srtp_stream_dealloc(session->stream_template, NULL); 
     3036  /* set new template */ 
     3037  session->stream_template = new_stream_template; 
     3038  /* add new list */ 
     3039  if (new_stream_list) { 
     3040    srtp_stream_t tail = new_stream_list; 
     3041    while (tail->next) { 
     3042      tail = tail->next; 
     3043    } 
     3044    tail->next = session->stream_list; 
     3045    session->stream_list = new_stream_list; 
     3046  } 
     3047  return status; 
     3048} 
     3049 
     3050 
     3051static srtp_err_status_t 
     3052update_stream(srtp_t session, const srtp_policy_t *policy) { 
     3053  srtp_err_status_t status; 
     3054  srtp_xtd_seq_num_t old_index; 
     3055  srtp_rdb_t old_rtcp_rdb; 
     3056  srtp_stream_t stream; 
     3057 
     3058  stream = srtp_get_stream(session, htonl(policy->ssrc.value)); 
     3059  if (stream == NULL) { 
     3060    return srtp_err_status_bad_param; 
     3061  } 
     3062 
     3063  /* save old extendard seq */ 
     3064  old_index = stream->rtp_rdbx.index; 
     3065  old_rtcp_rdb = stream->rtcp_rdb; 
     3066 
     3067  status = srtp_remove_stream(session, htonl(policy->ssrc.value)); 
     3068  if (status) { 
     3069    return status; 
     3070  } 
     3071 
     3072  status = srtp_add_stream(session, policy); 
     3073  if (status) { 
     3074    return status; 
     3075  } 
     3076 
     3077  stream = srtp_get_stream(session, htonl(policy->ssrc.value)); 
     3078  if (stream == NULL) { 
     3079    return srtp_err_status_fail; 
     3080  } 
     3081 
     3082  /* restore old extended seq */ 
     3083  stream->rtp_rdbx.index = old_index; 
     3084  stream->rtcp_rdb = old_rtcp_rdb; 
     3085 
     3086  return srtp_err_status_ok; 
     3087} 
     3088 
     3089 
     3090srtp_err_status_t 
     3091srtp_update_stream(srtp_t session, const srtp_policy_t *policy) { 
     3092  srtp_err_status_t status; 
     3093 
     3094  /* sanity check arguments */ 
     3095  if ((session == NULL) || (policy == NULL) || (!srtp_validate_policy_master_keys(policy))) 
     3096    return srtp_err_status_bad_param; 
     3097 
     3098  switch (policy->ssrc.type) { 
     3099  case (ssrc_any_outbound): 
     3100  case (ssrc_any_inbound): 
     3101    status = update_template_streams(session, policy); 
     3102    break; 
     3103  case (ssrc_specific): 
     3104    status = update_stream(session, policy); 
     3105    break; 
     3106  case (ssrc_undefined): 
     3107  default: 
     3108    return srtp_err_status_bad_param; 
     3109  } 
     3110 
     3111  return status; 
    20023112} 
    20033113 
    20043114 
    20053115/* 
    2006  * the default policy - provides a convenient way for callers to use 
     3116 * The default policy - provides a convenient way for callers to use 
    20073117 * the default security policy 
    2008  *  
    2009  * this policy is that defined in the current SRTP internet draft. 
     3118 * 
     3119 * The default policy is defined in RFC 3711 
     3120 * (Section 5. Default and mandatory-to-implement Transforms) 
    20103121 * 
    20113122 */ 
     
    20183129 
    20193130void 
    2020 crypto_policy_set_rtp_default(crypto_policy_t *p) { 
    2021  
    2022   p->cipher_type     = AES_ICM;            
    2023   p->cipher_key_len  = 30;                /* default 128 bits per RFC 3711 */ 
    2024   p->auth_type       = HMAC_SHA1;              
     3131srtp_crypto_policy_set_rtp_default(srtp_crypto_policy_t *p) { 
     3132 
     3133  p->cipher_type     = SRTP_AES_ICM_128; 
     3134  p->cipher_key_len  = SRTP_AES_ICM_128_KEY_LEN_WSALT; /* default 128 bits per RFC 3711 */ 
     3135  p->auth_type       = SRTP_HMAC_SHA1;              
    20253136  p->auth_key_len    = 20;                /* default 160 bits per RFC 3711 */ 
    20263137  p->auth_tag_len    = 10;                /* default 80 bits per RFC 3711 */ 
     
    20303141 
    20313142void 
    2032 crypto_policy_set_rtcp_default(crypto_policy_t *p) { 
    2033  
    2034   p->cipher_type     = AES_ICM;            
    2035   p->cipher_key_len  = 30;                /* default 128 bits per RFC 3711 */ 
    2036   p->auth_type       = HMAC_SHA1;              
     3143srtp_crypto_policy_set_rtcp_default(srtp_crypto_policy_t *p) { 
     3144 
     3145  p->cipher_type     = SRTP_AES_ICM_128; 
     3146  p->cipher_key_len  = SRTP_AES_ICM_128_KEY_LEN_WSALT; /* default 128 bits per RFC 3711 */ 
     3147  p->auth_type       = SRTP_HMAC_SHA1;              
    20373148  p->auth_key_len    = 20;                 /* default 160 bits per RFC 3711 */ 
    20383149  p->auth_tag_len    = 10;                 /* default 80 bits per RFC 3711 */ 
     
    20423153 
    20433154void 
    2044 crypto_policy_set_aes_cm_128_hmac_sha1_32(crypto_policy_t *p) { 
     3155srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(srtp_crypto_policy_t *p) { 
    20453156 
    20463157  /* 
     
    20503161   */ 
    20513162 
    2052   p->cipher_type     = AES_ICM;            
    2053   p->cipher_key_len  = 30;                /* 128 bit key, 112 bit salt */ 
    2054   p->auth_type       = HMAC_SHA1;              
     3163  p->cipher_type     = SRTP_AES_ICM_128; 
     3164  p->cipher_key_len  = SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */ 
     3165  p->auth_type       = SRTP_HMAC_SHA1;              
    20553166  p->auth_key_len    = 20;                /* 160 bit key               */ 
    20563167  p->auth_tag_len    = 4;                 /* 32 bit tag                */ 
     
    20613172 
    20623173void 
    2063 crypto_policy_set_aes_cm_128_null_auth(crypto_policy_t *p) { 
     3174srtp_crypto_policy_set_aes_cm_128_null_auth(srtp_crypto_policy_t *p) { 
    20643175 
    20653176  /* 
     
    20693180   */ 
    20703181 
    2071   p->cipher_type     = AES_ICM;            
    2072   p->cipher_key_len  = 30;                /* 128 bit key, 112 bit salt */ 
    2073   p->auth_type       = NULL_AUTH;              
     3182  p->cipher_type     = SRTP_AES_ICM_128; 
     3183  p->cipher_key_len  = SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */ 
     3184  p->auth_type       = SRTP_NULL_AUTH;              
    20743185  p->auth_key_len    = 0;  
    20753186  p->auth_tag_len    = 0;  
     
    20803191 
    20813192void 
    2082 crypto_policy_set_null_cipher_hmac_sha1_80(crypto_policy_t *p) { 
     3193srtp_crypto_policy_set_null_cipher_hmac_sha1_80(srtp_crypto_policy_t *p) { 
    20833194 
    20843195  /* 
     
    20863197   */ 
    20873198 
    2088   p->cipher_type     = NULL_CIPHER;            
     3199  p->cipher_type     = SRTP_NULL_CIPHER;            
    20893200  p->cipher_key_len  = 0; 
    2090   p->auth_type       = HMAC_SHA1;              
     3201  p->auth_type       = SRTP_HMAC_SHA1;              
    20913202  p->auth_key_len    = 20;  
    20923203  p->auth_tag_len    = 10;  
     
    20953206} 
    20963207 
    2097  
    20983208void 
    2099 crypto_policy_set_aes_cm_256_hmac_sha1_80(crypto_policy_t *p) { 
     3209srtp_crypto_policy_set_null_cipher_hmac_null(srtp_crypto_policy_t *p) { 
    21003210 
    21013211  /* 
    2102    * corresponds to draft-ietf-avt-big-aes-03.txt 
    2103    */ 
    2104  
    2105   p->cipher_type     = AES_ICM;            
    2106   p->cipher_key_len  = 46; 
    2107   p->auth_type       = HMAC_SHA1;              
     3212   * Should only be used for testing 
     3213   */ 
     3214 
     3215  p->cipher_type     = SRTP_NULL_CIPHER;            
     3216  p->cipher_key_len  = 0; 
     3217  p->auth_type       = SRTP_NULL_AUTH;              
     3218  p->auth_key_len    = 0;  
     3219  p->auth_tag_len    = 0;  
     3220  p->sec_serv        = sec_serv_none; 
     3221   
     3222} 
     3223 
     3224 
     3225void 
     3226srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(srtp_crypto_policy_t *p) { 
     3227 
     3228  /* 
     3229   * corresponds to RFC 6188 
     3230   */ 
     3231 
     3232  p->cipher_type     = SRTP_AES_ICM_256; 
     3233  p->cipher_key_len  = SRTP_AES_ICM_256_KEY_LEN_WSALT; 
     3234  p->auth_type       = SRTP_HMAC_SHA1;              
    21083235  p->auth_key_len    = 20;                /* default 160 bits per RFC 3711 */ 
    21093236  p->auth_tag_len    = 10;                /* default 80 bits per RFC 3711 */ 
     
    21133240 
    21143241void 
    2115 crypto_policy_set_aes_cm_256_hmac_sha1_32(crypto_policy_t *p) { 
     3242srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(srtp_crypto_policy_t *p) { 
    21163243 
    21173244  /* 
    2118    * corresponds to draft-ietf-avt-big-aes-03.txt 
     3245   * corresponds to RFC 6188 
    21193246   * 
    21203247   * note that this crypto policy is intended for SRTP, but not SRTCP 
    21213248   */ 
    21223249 
    2123   p->cipher_type     = AES_ICM;            
    2124   p->cipher_key_len  = 46; 
    2125   p->auth_type       = HMAC_SHA1;              
     3250  p->cipher_type     = SRTP_AES_ICM_256; 
     3251  p->cipher_key_len  = SRTP_AES_ICM_256_KEY_LEN_WSALT; 
     3252  p->auth_type       = SRTP_HMAC_SHA1;              
    21263253  p->auth_key_len    = 20;                /* default 160 bits per RFC 3711 */ 
    21273254  p->auth_tag_len    = 4;                 /* default 80 bits per RFC 3711 */ 
     
    21333260 */ 
    21343261void 
    2135 crypto_policy_set_aes_cm_256_null_auth (crypto_policy_t *p) 
     3262srtp_crypto_policy_set_aes_cm_256_null_auth (srtp_crypto_policy_t *p) 
    21363263{ 
    2137     p->cipher_type     = AES_ICM; 
    2138     p->cipher_key_len  = 46; 
    2139     p->auth_type       = NULL_AUTH; 
     3264    p->cipher_type     = SRTP_AES_ICM_256; 
     3265    p->cipher_key_len  = SRTP_AES_ICM_256_KEY_LEN_WSALT; 
     3266    p->auth_type       = SRTP_NULL_AUTH; 
    21403267    p->auth_key_len    = 0; 
    21413268    p->auth_tag_len    = 0; 
     
    21443271 
    21453272#ifdef OPENSSL 
     3273void 
     3274srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(srtp_crypto_policy_t *p) { 
     3275 
     3276  /* 
     3277   * corresponds to RFC 6188 
     3278   */ 
     3279 
     3280  p->cipher_type     = SRTP_AES_ICM_192; 
     3281  p->cipher_key_len  = SRTP_AES_ICM_192_KEY_LEN_WSALT; 
     3282  p->auth_type       = SRTP_HMAC_SHA1; 
     3283  p->auth_key_len    = 20;                /* default 160 bits per RFC 3711 */ 
     3284  p->auth_tag_len    = 10;                /* default 80 bits per RFC 3711 */ 
     3285  p->sec_serv        = sec_serv_conf_and_auth; 
     3286} 
     3287 
     3288 
     3289void 
     3290srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(srtp_crypto_policy_t *p) { 
     3291 
     3292  /* 
     3293   * corresponds to RFC 6188 
     3294   * 
     3295   * note that this crypto policy is intended for SRTP, but not SRTCP 
     3296   */ 
     3297 
     3298  p->cipher_type     = SRTP_AES_ICM_192; 
     3299  p->cipher_key_len  = SRTP_AES_ICM_192_KEY_LEN_WSALT; 
     3300  p->auth_type       = SRTP_HMAC_SHA1; 
     3301  p->auth_key_len    = 20;                /* default 160 bits per RFC 3711 */ 
     3302  p->auth_tag_len    = 4;                 /* default 80 bits per RFC 3711 */ 
     3303  p->sec_serv        = sec_serv_conf_and_auth; 
     3304} 
     3305 
     3306/* 
     3307 * AES-192 with no authentication. 
     3308 */ 
     3309void 
     3310srtp_crypto_policy_set_aes_cm_192_null_auth (srtp_crypto_policy_t *p) 
     3311{ 
     3312    p->cipher_type     = SRTP_AES_ICM_192; 
     3313    p->cipher_key_len  = SRTP_AES_ICM_192_KEY_LEN_WSALT; 
     3314    p->auth_type       = SRTP_NULL_AUTH; 
     3315    p->auth_key_len    = 0; 
     3316    p->auth_tag_len    = 0; 
     3317    p->sec_serv        = sec_serv_conf; 
     3318} 
     3319 
    21463320/* 
    21473321 * AES-128 GCM mode with 8 octet auth tag.  
    21483322 */ 
    21493323void 
    2150 crypto_policy_set_aes_gcm_128_8_auth(crypto_policy_t *p) { 
    2151   p->cipher_type     = AES_128_GCM;            
    2152   p->cipher_key_len  = AES_128_GCM_KEYSIZE_WSALT;  
    2153   p->auth_type       = NULL_AUTH; /* GCM handles the auth for us */             
     3324srtp_crypto_policy_set_aes_gcm_128_8_auth(srtp_crypto_policy_t *p) { 
     3325  p->cipher_type     = SRTP_AES_GCM_128; 
     3326  p->cipher_key_len  = SRTP_AES_GCM_128_KEY_LEN_WSALT; 
     3327  p->auth_type       = SRTP_NULL_AUTH; /* GCM handles the auth for us */             
    21543328  p->auth_key_len    = 0;  
    21553329  p->auth_tag_len    = 8;   /* 8 octet tag length */ 
     
    21613335 */ 
    21623336void 
    2163 crypto_policy_set_aes_gcm_256_8_auth(crypto_policy_t *p) { 
    2164   p->cipher_type     = AES_256_GCM;            
    2165   p->cipher_key_len  = AES_256_GCM_KEYSIZE_WSALT;  
    2166   p->auth_type       = NULL_AUTH; /* GCM handles the auth for us */  
     3337srtp_crypto_policy_set_aes_gcm_256_8_auth(srtp_crypto_policy_t *p) { 
     3338  p->cipher_type     = SRTP_AES_GCM_256; 
     3339  p->cipher_key_len  = SRTP_AES_GCM_256_KEY_LEN_WSALT; 
     3340  p->auth_type       = SRTP_NULL_AUTH; /* GCM handles the auth for us */  
    21673341  p->auth_key_len    = 0;  
    21683342  p->auth_tag_len    = 8;   /* 8 octet tag length */ 
     
    21743348 */ 
    21753349void 
    2176 crypto_policy_set_aes_gcm_128_8_only_auth(crypto_policy_t *p) { 
    2177   p->cipher_type     = AES_128_GCM;            
    2178   p->cipher_key_len  = AES_128_GCM_KEYSIZE_WSALT;  
    2179   p->auth_type       = NULL_AUTH; /* GCM handles the auth for us */  
     3350srtp_crypto_policy_set_aes_gcm_128_8_only_auth(srtp_crypto_policy_t *p) { 
     3351  p->cipher_type     = SRTP_AES_GCM_128; 
     3352  p->cipher_key_len  = SRTP_AES_GCM_128_KEY_LEN_WSALT; 
     3353  p->auth_type       = SRTP_NULL_AUTH; /* GCM handles the auth for us */  
    21803354  p->auth_key_len    = 0;  
    21813355  p->auth_tag_len    = 8;   /* 8 octet tag length */ 
     
    21873361 */ 
    21883362void 
    2189 crypto_policy_set_aes_gcm_256_8_only_auth(crypto_policy_t *p) { 
    2190   p->cipher_type     = AES_256_GCM;            
    2191   p->cipher_key_len  = AES_256_GCM_KEYSIZE_WSALT;  
    2192   p->auth_type       = NULL_AUTH; /* GCM handles the auth for us */  
     3363srtp_crypto_policy_set_aes_gcm_256_8_only_auth(srtp_crypto_policy_t *p) { 
     3364  p->cipher_type     = SRTP_AES_GCM_256; 
     3365  p->cipher_key_len  = SRTP_AES_GCM_256_KEY_LEN_WSALT; 
     3366  p->auth_type       = SRTP_NULL_AUTH; /* GCM handles the auth for us */  
    21933367  p->auth_key_len    = 0;  
    21943368  p->auth_tag_len    = 8;   /* 8 octet tag length */ 
     
    22003374 */ 
    22013375void 
    2202 crypto_policy_set_aes_gcm_128_16_auth(crypto_policy_t *p) { 
    2203   p->cipher_type     = AES_128_GCM;            
    2204   p->cipher_key_len  = AES_128_GCM_KEYSIZE_WSALT;  
    2205   p->auth_type       = NULL_AUTH; /* GCM handles the auth for us */             
     3376srtp_crypto_policy_set_aes_gcm_128_16_auth(srtp_crypto_policy_t *p) { 
     3377  p->cipher_type     = SRTP_AES_GCM_128; 
     3378  p->cipher_key_len  = SRTP_AES_GCM_128_KEY_LEN_WSALT; 
     3379  p->auth_type       = SRTP_NULL_AUTH; /* GCM handles the auth for us */             
    22063380  p->auth_key_len    = 0;  
    22073381  p->auth_tag_len    = 16;   /* 16 octet tag length */ 
     
    22133387 */ 
    22143388void 
    2215 crypto_policy_set_aes_gcm_256_16_auth(crypto_policy_t *p) { 
    2216   p->cipher_type     = AES_256_GCM;            
    2217   p->cipher_key_len  = AES_256_GCM_KEYSIZE_WSALT;  
    2218   p->auth_type       = NULL_AUTH; /* GCM handles the auth for us */  
     3389srtp_crypto_policy_set_aes_gcm_256_16_auth(srtp_crypto_policy_t *p) { 
     3390  p->cipher_type     = SRTP_AES_GCM_256; 
     3391  p->cipher_key_len  = SRTP_AES_GCM_256_KEY_LEN_WSALT; 
     3392  p->auth_type       = SRTP_NULL_AUTH; /* GCM handles the auth for us */  
    22193393  p->auth_key_len    = 0;  
    22203394  p->auth_tag_len    = 16;   /* 16 octet tag length */ 
     
    22303404/* 
    22313405 * AEAD uses a new IV formation method.  This function implements 
    2232  * section 10.1 from draft-ietf-avtcore-srtp-aes-gcm-07.txt.  The 
    2233  * calculation is defined as, where (+) is the xor operation: 
     3406 * section 9.1 (SRTCP IV Formation for AES-GCM) from RFC7714. 
     3407 * The calculation is defined as, where (+) is the xor operation: 
    22343408 * 
    22353409 *                0  1  2  3  4  5  6  7  8  9 10 11 
     
    22463420 *               +--+--+--+--+--+--+--+--+--+--+--+--+* 
    22473421 * 
    2248  * Input:  *stream - pointer to SRTP stream context, used to retrieve 
    2249  *                   the SALT  
    2250  *         *iv     - Pointer to recieve the calculated IV 
    2251  *         seq_num - The SEQ value to use for the IV calculation. 
    2252  *         *hdr    - The RTP header, used to get the SSRC value 
     3422 * Input:  *session_keys - pointer to SRTP stream context session keys, 
     3423 *                        used to retrieve the SALT  
     3424 *         *iv           - Pointer to recieve the calculated IV 
     3425 *         seq_num       - The SEQ value to use for the IV calculation. 
     3426 *         *hdr          - The RTP header, used to get the SSRC value 
     3427 * 
     3428 * Returns: srtp_err_status_ok if no error or srtp_err_status_bad_param 
     3429 *          if seq_num is invalid 
    22533430 * 
    22543431 */ 
    2255 static void srtp_calc_aead_iv_srtcp(srtp_stream_ctx_t *stream, v128_t *iv,  
    2256                                     uint32_t seq_num, srtcp_hdr_t *hdr) 
     3432static srtp_err_status_t 
     3433srtp_calc_aead_iv_srtcp(srtp_session_keys_t *session_keys, v128_t *iv, 
     3434                        uint32_t seq_num, srtcp_hdr_t *hdr) 
    22573435{ 
    22583436    v128_t      in; 
     
    22653443    memcpy(&in.v16[1], &hdr->ssrc, 4); /* still in network order! */ 
    22663444    in.v16[3] = 0; 
    2267     in.v32[2] = 0x7FFFFFFF & htonl(seq_num); /* bit 32 is suppose to be zero */ 
     3445 
     3446    /* 
     3447     *  The SRTCP index (seq_num) spans bits 0 through 30 inclusive. 
     3448     *  The most significant bit should be zero. 
     3449     */ 
     3450    if (seq_num & 0x80000000UL) { 
     3451        return srtp_err_status_bad_param; 
     3452    } 
     3453    in.v32[2] = htonl(seq_num); 
    22683454 
    22693455    debug_print(mod_srtp, "Pre-salted RTCP IV = %s\n", v128_hex_string(&in)); 
     
    22723458     * Get the SALT value from the context 
    22733459     */ 
    2274     memcpy(salt.v8, stream->c_salt, 12); 
     3460    memcpy(salt.v8, session_keys->c_salt, 12); 
    22753461    debug_print(mod_srtp, "RTCP SALT = %s\n", v128_hex_string(&salt)); 
    22763462 
     
    22793465     */ 
    22803466    v128_xor(iv, &in, &salt); 
     3467 
     3468    return srtp_err_status_ok; 
    22813469} 
    22823470 
     
    22853473 * AES-GCM mode with 128 or 256 bit keys.  
    22863474 */ 
    2287 static err_status_t 
     3475static srtp_err_status_t 
    22883476srtp_protect_rtcp_aead (srtp_t ctx, srtp_stream_ctx_t *stream,  
    2289                         void *rtcp_hdr, unsigned int *pkt_octet_len) 
     3477                        void *rtcp_hdr, unsigned int *pkt_octet_len, 
     3478                        srtp_session_keys_t *session_keys, unsigned int use_mki) 
    22903479{ 
    22913480    srtcp_hdr_t *hdr = (srtcp_hdr_t*)rtcp_hdr; 
     
    22943483    unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */ 
    22953484    uint8_t *auth_tag = NULL;   /* location of auth_tag within packet     */ 
    2296     err_status_t status; 
    2297     int tag_len; 
     3485    srtp_err_status_t status; 
     3486    uint32_t tag_len; 
    22983487    uint32_t seq_num; 
    22993488    v128_t iv; 
    23003489    uint32_t tseq; 
     3490    unsigned int mki_size = 0; 
    23013491 
    23023492    /* get tag length from stream context */ 
    2303     tag_len = auth_get_tag_length(stream->rtcp_auth); 
     3493    tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth); 
    23043494 
    23053495    /* 
     
    23253515    } 
    23263516 
     3517    mki_size = srtp_inject_mki((uint8_t *)hdr + *pkt_octet_len + tag_len + sizeof(srtcp_trailer_t), 
     3518                               session_keys, use_mki); 
     3519 
    23273520    /* 
    23283521     * set the auth_tag pointer to the proper location, which is after 
     
    23373530     * if its value isn't too big 
    23383531     */ 
    2339     status = rdb_increment(&stream->rtcp_rdb); 
     3532    status = srtp_rdb_increment(&stream->rtcp_rdb); 
    23403533    if (status) { 
    23413534        return status; 
    23423535    } 
    2343     seq_num = rdb_get_value(&stream->rtcp_rdb); 
     3536    seq_num = srtp_rdb_get_value(&stream->rtcp_rdb); 
    23443537    *trailer |= htonl(seq_num); 
    23453538    debug_print(mod_srtp, "srtcp index: %x", seq_num); 
    23463539 
    23473540    /* 
    2348      * Calculating the IV and pass it down to the cipher  
    2349      */ 
    2350     srtp_calc_aead_iv_srtcp(stream, &iv, seq_num, hdr); 
    2351     status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_encrypt); 
     3541     * Calculate and set the IV 
     3542     */ 
     3543    status = srtp_calc_aead_iv_srtcp(session_keys, &iv, seq_num, hdr); 
    23523544    if (status) { 
    2353         return err_status_cipher_fail; 
     3545        return srtp_err_status_cipher_fail; 
     3546    } 
     3547    status = srtp_cipher_set_iv(session_keys->rtcp_cipher, 
     3548                                (uint8_t*)&iv, srtp_direction_encrypt); 
     3549    if (status) { 
     3550        return srtp_err_status_cipher_fail; 
    23543551    } 
    23553552 
     
    23583555     */ 
    23593556    if (enc_start) { 
    2360         /* 
    2361         * If payload encryption is enabled, then the AAD consist of 
    2362         * the RTCP header and the seq# at the end of the packet 
    2363         */ 
    2364         status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,  
    2365                                 octets_in_rtcp_header); 
    2366         if (status) { 
    2367             return ( err_status_cipher_fail); 
    2368         } 
     3557        /* 
     3558        * If payload encryption is enabled, then the AAD consist of 
     3559        * the RTCP header and the seq# at the end of the packet 
     3560        */ 
     3561        status = srtp_cipher_set_aad(session_keys->rtcp_cipher, 
     3562                                 (uint8_t*)hdr, octets_in_rtcp_header); 
     3563        if (status) { 
     3564            return ( srtp_err_status_cipher_fail); 
     3565        } 
    23693566    } else { 
    2370         /* 
    2371         * Since payload encryption is not enabled, we must authenticate 
    2372          * the entire packet as described in section 10.3 in revision 07 
    2373          * of the draft. 
    2374         */ 
    2375         status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,  
    2376                                 *pkt_octet_len); 
    2377         if (status) { 
    2378             return ( err_status_cipher_fail); 
    2379         } 
    2380     } 
    2381     /*  
     3567        /* 
     3568        * Since payload encryption is not enabled, we must authenticate 
     3569         * the entire packet as described in RFC 7714 (Section 9.3. Data 
     3570         * Types in Unencrypted SRTCP Compound Packets) 
     3571        */ 
     3572        status = srtp_cipher_set_aad(session_keys->rtcp_cipher, 
     3573                                 (uint8_t*)hdr, *pkt_octet_len); 
     3574        if (status) { 
     3575            return ( srtp_err_status_cipher_fail); 
     3576        } 
     3577    } 
     3578    /* 
    23823579     * Process the sequence# as AAD 
    23833580     */ 
    23843581    tseq = *trailer; 
    2385     status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq,  
    2386                             sizeof(srtcp_trailer_t)); 
     3582    status = srtp_cipher_set_aad(session_keys->rtcp_cipher, (uint8_t*)&tseq, 
     3583                                 sizeof(srtcp_trailer_t)); 
    23873584    if (status) { 
    2388         return ( err_status_cipher_fail); 
     3585        return ( srtp_err_status_cipher_fail); 
    23893586    } 
    23903587 
    23913588    /* if we're encrypting, exor keystream into the message */ 
    23923589    if (enc_start) { 
    2393         status = cipher_encrypt(stream->rtcp_cipher, 
    2394                                 (uint8_t*)enc_start, &enc_octet_len); 
     3590        status = srtp_cipher_encrypt(session_keys->rtcp_cipher, 
     3591                                     (uint8_t*)enc_start, &enc_octet_len); 
    23953592        if (status) { 
    2396             return err_status_cipher_fail; 
     3593            return srtp_err_status_cipher_fail; 
    23973594        } 
    2398         /* 
    2399         * Get the tag and append that to the output 
    2400         */ 
    2401         status = cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag,  
    2402                                 &tag_len); 
    2403         if (status) { 
    2404             return ( err_status_cipher_fail); 
    2405         } 
    2406         enc_octet_len += tag_len; 
     3595        /* 
     3596        * Get the tag and append that to the output 
     3597        */ 
     3598        status = srtp_cipher_get_tag(session_keys->rtcp_cipher, (uint8_t*)auth_tag, 
     3599                                     &tag_len); 
     3600        if (status) { 
     3601            return ( srtp_err_status_cipher_fail); 
     3602        } 
     3603        enc_octet_len += tag_len; 
    24073604    } else { 
    2408         /* 
    2409         * Even though we're not encrypting the payload, we need 
    2410         * to run the cipher to get the auth tag. 
    2411         */ 
    2412         unsigned int nolen = 0; 
    2413         status = cipher_encrypt(stream->rtcp_cipher, NULL, &nolen); 
     3605        /* 
     3606        * Even though we're not encrypting the payload, we need 
     3607        * to run the cipher to get the auth tag. 
     3608        */ 
     3609        unsigned int nolen = 0; 
     3610        status = srtp_cipher_encrypt(session_keys->rtcp_cipher, NULL, &nolen); 
    24143611        if (status) { 
    2415             return err_status_cipher_fail; 
     3612            return srtp_err_status_cipher_fail; 
    24163613        } 
    2417         /* 
    2418         * Get the tag and append that to the output 
    2419         */ 
    2420         status = cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag,  
    2421                                 &tag_len); 
    2422         if (status) { 
    2423             return ( err_status_cipher_fail); 
    2424         } 
    2425         enc_octet_len += tag_len; 
     3614        /* 
     3615        * Get the tag and append that to the output 
     3616        */ 
     3617        status = srtp_cipher_get_tag(session_keys->rtcp_cipher, (uint8_t*)auth_tag, 
     3618                                     &tag_len); 
     3619        if (status) { 
     3620            return ( srtp_err_status_cipher_fail); 
     3621        } 
     3622        enc_octet_len += tag_len; 
    24263623    } 
    24273624 
     
    24293626    *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t)); 
    24303627 
    2431     return err_status_ok; 
     3628    /* increase the packet by the mki_size */ 
     3629    *pkt_octet_len += mki_size; 
     3630 
     3631    return srtp_err_status_ok; 
    24323632} 
    24333633 
     
    24383638 * when decrypting the payload. 
    24393639 */ 
    2440 static err_status_t 
     3640static srtp_err_status_t 
    24413641srtp_unprotect_rtcp_aead (srtp_t ctx, srtp_stream_ctx_t *stream,  
    2442                           void *srtcp_hdr, unsigned int *pkt_octet_len) 
     3642                          void *srtcp_hdr, unsigned int *pkt_octet_len, 
     3643                          srtp_session_keys_t *session_keys, unsigned int use_mki) 
    24433644{ 
    24443645    srtcp_hdr_t *hdr = (srtcp_hdr_t*)srtcp_hdr; 
     
    24473648    unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */ 
    24483649    uint8_t *auth_tag = NULL;   /* location of auth_tag within packet     */ 
    2449     err_status_t status; 
     3650    srtp_err_status_t status; 
    24503651    int tag_len; 
    24513652    unsigned int tmp_len; 
     
    24533654    v128_t iv; 
    24543655    uint32_t tseq; 
     3656    unsigned int mki_size = 0; 
    24553657 
    24563658    /* get tag length from stream context */ 
    2457     tag_len = auth_get_tag_length(stream->rtcp_auth); 
     3659    tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth); 
     3660 
     3661    if (use_mki) { 
     3662      mki_size = session_keys->mki_size; 
     3663    } 
    24583664 
    24593665    /* 
     
    24693675     *   multiples of 32-bits (RFC 3550 6.1) 
    24703676     */ 
    2471     trailer = (uint32_t*)((char*)hdr + *pkt_octet_len - sizeof(srtcp_trailer_t)); 
     3677    trailer = (uint32_t*)((char*)hdr + *pkt_octet_len - sizeof(srtcp_trailer_t) - mki_size); 
    24723678    /* 
    24733679     * We pass the tag down to the cipher when doing GCM mode  
    24743680     */ 
    24753681    enc_octet_len = *pkt_octet_len - (octets_in_rtcp_header +  
    2476                                       sizeof(srtcp_trailer_t)); 
    2477     auth_tag = (uint8_t*)hdr + *pkt_octet_len - tag_len - sizeof(srtcp_trailer_t); 
     3682                                      sizeof(srtcp_trailer_t) + mki_size); 
     3683    auth_tag = (uint8_t*)hdr + *pkt_octet_len - tag_len - mki_size - sizeof(srtcp_trailer_t); 
    24783684 
    24793685    if (*((unsigned char*)trailer) & SRTCP_E_BYTE_BIT) { 
     
    24903696    seq_num = ntohl(*trailer) & SRTCP_INDEX_MASK; 
    24913697    debug_print(mod_srtp, "srtcp index: %x", seq_num); 
    2492     status = rdb_check(&stream->rtcp_rdb, seq_num); 
     3698    status = srtp_rdb_check(&stream->rtcp_rdb, seq_num); 
    24933699    if (status) { 
    24943700        return status; 
     
    24983704     * Calculate and set the IV 
    24993705     */ 
    2500     srtp_calc_aead_iv_srtcp(stream, &iv, seq_num, hdr); 
    2501     status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_decrypt); 
     3706    status = srtp_calc_aead_iv_srtcp(session_keys, &iv, seq_num, hdr); 
    25023707    if (status) { 
    2503         return err_status_cipher_fail; 
     3708        return srtp_err_status_cipher_fail; 
     3709    } 
     3710    status = srtp_cipher_set_iv(session_keys->rtcp_cipher, 
     3711                                (uint8_t*)&iv, srtp_direction_decrypt); 
     3712    if (status) { 
     3713        return srtp_err_status_cipher_fail; 
    25043714    } 
    25053715 
     
    25083718     */ 
    25093719    if (enc_start) { 
    2510         /* 
    2511         * If payload encryption is enabled, then the AAD consist of 
    2512         * the RTCP header and the seq# at the end of the packet 
    2513         */ 
    2514         status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,  
    2515                                 octets_in_rtcp_header); 
    2516         if (status) { 
    2517             return ( err_status_cipher_fail); 
    2518         } 
     3720        /* 
     3721        * If payload encryption is enabled, then the AAD consist of 
     3722        * the RTCP header and the seq# at the end of the packet 
     3723        */ 
     3724        status = srtp_cipher_set_aad(session_keys->rtcp_cipher, 
     3725                                   (uint8_t*)hdr, octets_in_rtcp_header); 
     3726        if (status) { 
     3727            return ( srtp_err_status_cipher_fail); 
     3728        } 
    25193729    } else { 
    2520         /* 
    2521          * Since payload encryption is not enabled, we must authenticate 
    2522          * the entire packet as described in section 10.3 in revision 07 
    2523          * of the draft. 
    2524          */ 
    2525         status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,  
    2526                                (*pkt_octet_len - tag_len - sizeof(srtcp_trailer_t))); 
    2527         if (status) { 
    2528             return ( err_status_cipher_fail); 
    2529         } 
    2530     } 
    2531  
    2532     /*  
    2533      * Process the sequence# as AAD  
     3730        /* 
     3731         * Since payload encryption is not enabled, we must authenticate 
     3732         * the entire packet as described in RFC 7714 (Section 9.3. Data 
     3733         * Types in Unencrypted SRTCP Compound Packets) 
     3734         */ 
     3735        status = srtp_cipher_set_aad( 
     3736          session_keys->rtcp_cipher, (uint8_t*)hdr, 
     3737          (*pkt_octet_len - tag_len - sizeof(srtcp_trailer_t) - mki_size)); 
     3738        if (status) { 
     3739            return ( srtp_err_status_cipher_fail); 
     3740        } 
     3741    } 
     3742 
     3743    /* 
     3744     * Process the sequence# as AAD 
    25343745     */ 
    25353746    tseq = *trailer; 
    2536     status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq,  
    2537                             sizeof(srtcp_trailer_t)); 
     3747    status = srtp_cipher_set_aad(session_keys->rtcp_cipher, 
     3748                                 (uint8_t*)&tseq, sizeof(srtcp_trailer_t)); 
    25383749    if (status) { 
    2539         return ( err_status_cipher_fail); 
     3750        return ( srtp_err_status_cipher_fail); 
    25403751    } 
    25413752 
    25423753    /* if we're decrypting, exor keystream into the message */ 
    25433754    if (enc_start) { 
    2544         status = cipher_decrypt(stream->rtcp_cipher, 
    2545                                 (uint8_t*)enc_start, &enc_octet_len); 
     3755        status = srtp_cipher_decrypt(session_keys->rtcp_cipher, (uint8_t*)enc_start, &enc_octet_len); 
    25463756        if (status) { 
    25473757            return status; 
    25483758        } 
    25493759    } else { 
    2550         /* 
    2551          * Still need to run the cipher to check the tag 
    2552          */ 
    2553         tmp_len = tag_len; 
    2554         status = cipher_decrypt(stream->rtcp_cipher, (uint8_t*)auth_tag,  
    2555                                 &tmp_len); 
     3760        /* 
     3761         * Still need to run the cipher to check the tag 
     3762         */ 
     3763        tmp_len = tag_len; 
     3764        status = srtp_cipher_decrypt(session_keys->rtcp_cipher, (uint8_t*)auth_tag, &tmp_len); 
    25563765        if (status) { 
    25573766            return status; 
     
    25603769 
    25613770    /* decrease the packet length by the length of the auth tag and seq_num*/ 
    2562     *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t)); 
     3771    *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t) + mki_size); 
    25633772 
    25643773    /* 
     
    26093818 
    26103819    /* we've passed the authentication check, so add seq_num to the rdb */ 
    2611     rdb_add_index(&stream->rtcp_rdb, seq_num); 
    2612  
    2613     return err_status_ok; 
    2614 } 
    2615  
    2616 err_status_t  
     3820    srtp_rdb_add_index(&stream->rtcp_rdb, seq_num); 
     3821 
     3822    return srtp_err_status_ok; 
     3823} 
     3824 
     3825srtp_err_status_t  
    26173826srtp_protect_rtcp(srtp_t ctx, void *rtcp_hdr, int *pkt_octet_len) { 
     3827  return srtp_protect_rtcp_mki(ctx, rtcp_hdr, pkt_octet_len, 0, 0); 
     3828} 
     3829 
     3830srtp_err_status_t  
     3831srtp_protect_rtcp_mki(srtp_t ctx, void *rtcp_hdr, int *pkt_octet_len, 
     3832                      unsigned int use_mki, unsigned int mki_index) { 
    26183833  srtcp_hdr_t *hdr = (srtcp_hdr_t *)rtcp_hdr; 
    26193834  uint32_t *enc_start;      /* pointer to start of encrypted portion  */ 
     
    26223837  unsigned int enc_octet_len = 0;/* number of octets in encrypted portion */ 
    26233838  uint8_t *auth_tag = NULL; /* location of auth_tag within packet     */ 
    2624   err_status_t status;    
     3839  srtp_err_status_t status;    
    26253840  int tag_len; 
    26263841  srtp_stream_ctx_t *stream; 
    2627   int prefix_len; 
     3842  uint32_t prefix_len; 
    26283843  uint32_t seq_num; 
     3844  unsigned int mki_size = 0; 
     3845  srtp_session_keys_t *session_keys = NULL; 
    26293846 
    26303847  /* we assume the hdr is 32-bit aligned to start */ 
     
    26323849  /* check the packet length - it must at least contain a full header */ 
    26333850  if (*pkt_octet_len < octets_in_rtcp_header) 
    2634     return err_status_bad_param; 
     3851    return srtp_err_status_bad_param; 
    26353852 
    26363853  /* 
     
    26603877    } else { 
    26613878      /* no template stream, so we return an error */ 
    2662       return err_status_no_ctx; 
     3879      return srtp_err_status_no_ctx; 
    26633880    }  
    26643881  } 
     
    26783895  }   
    26793896 
     3897  session_keys = srtp_get_session_keys_with_mki_index(stream, use_mki, mki_index); 
     3898 
    26803899  /* 
    26813900   * Check if this is an AEAD stream (GCM mode).  If so, then dispatch 
    26823901   * the request to our AEAD handler. 
    26833902   */ 
    2684   if (stream->rtp_cipher->algorithm == AES_128_GCM || 
    2685       stream->rtp_cipher->algorithm == AES_256_GCM) { 
    2686       return srtp_protect_rtcp_aead(ctx, stream, rtcp_hdr, (unsigned int*)pkt_octet_len); 
     3903  if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 || 
     3904      session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) { 
     3905      return srtp_protect_rtcp_aead(ctx, stream, rtcp_hdr, 
     3906                                    (unsigned int*)pkt_octet_len, session_keys, 
     3907                                    use_mki); 
    26873908  } 
    26883909 
    26893910  /* get tag length from stream context */ 
    2690   tag_len = auth_get_tag_length(stream->rtcp_auth);  
     3911  tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth);  
    26913912 
    26923913  /* 
     
    27133934  } 
    27143935 
     3936  mki_size = srtp_inject_mki((uint8_t *)hdr + *pkt_octet_len + sizeof(srtcp_trailer_t), 
     3937                             session_keys, use_mki); 
     3938 
    27153939  /*  
    27163940   * set the auth_start and auth_tag pointers to the proper locations 
     
    27193943  /* Note: This would need to change for optional mikey data */ 
    27203944  auth_start = (uint32_t *)hdr; 
    2721   auth_tag = (uint8_t *)hdr + *pkt_octet_len + sizeof(srtcp_trailer_t);  
     3945  auth_tag = (uint8_t *)hdr + *pkt_octet_len + sizeof(srtcp_trailer_t) + mki_size;  
    27223946 
    27233947  /* perform EKT processing if needed */ 
    2724   ekt_write_data(stream->ekt, auth_tag, tag_len, pkt_octet_len,  
    2725                  rdbx_get_packet_index(&stream->rtp_rdbx)); 
     3948  srtp_ekt_write_data(stream->ekt, auth_tag, tag_len, pkt_octet_len,  
     3949                      srtp_rdbx_get_packet_index(&stream->rtp_rdbx)); 
    27263950 
    27273951  /*  
     
    27293953   * if its value isn't too big 
    27303954   */ 
    2731   status = rdb_increment(&stream->rtcp_rdb); 
     3955  status = srtp_rdb_increment(&stream->rtcp_rdb); 
    27323956  if (status) 
    27333957    return status; 
    2734   seq_num = rdb_get_value(&stream->rtcp_rdb); 
     3958  seq_num = srtp_rdb_get_value(&stream->rtcp_rdb); 
    27353959  *trailer |= htonl(seq_num); 
    27363960  debug_print(mod_srtp, "srtcp index: %x", seq_num); 
     
    27393963   * if we're using rindael counter mode, set nonce and seq  
    27403964   */ 
    2741   if (stream->rtcp_cipher->type->id == AES_ICM) { 
     3965  if (session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_128 || 
     3966      session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_192 || 
     3967      session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_256) { 
    27423968    v128_t iv; 
    27433969     
     
    27463972    iv.v32[2] = htonl(seq_num >> 16); 
    27473973    iv.v32[3] = htonl(seq_num << 16); 
    2748     status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_encrypt); 
     3974    status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t*)&iv, 
     3975                                srtp_direction_encrypt); 
    27493976 
    27503977  } else {   
     
    27563983    iv.v32[2] = 0; 
    27573984    iv.v32[3] = htonl(seq_num); 
    2758     status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_encrypt); 
     3985    status = srtp_cipher_set_iv(session_keys->rtcp_cipher, 
     3986                                (uint8_t*)&iv, srtp_direction_encrypt); 
    27593987  } 
    27603988  if (status) 
    2761     return err_status_cipher_fail; 
     3989    return srtp_err_status_cipher_fail; 
    27623990 
    27633991  /*  
     
    27703998 
    27713999    /* put keystream prefix into auth_tag */ 
    2772     prefix_len = auth_get_prefix_length(stream->rtcp_auth);     
    2773     status = cipher_output(stream->rtcp_cipher, auth_tag, prefix_len); 
     4000    prefix_len = srtp_auth_get_prefix_length(session_keys->rtcp_auth);     
     4001    status = srtp_cipher_output(session_keys->rtcp_cipher, auth_tag, &prefix_len); 
    27744002 
    27754003    debug_print(mod_srtp, "keystream prefix: %s",  
    2776                 octet_string_hex_string(auth_tag, prefix_len)); 
     4004                srtp_octet_string_hex_string(auth_tag, prefix_len)); 
    27774005 
    27784006    if (status) 
    2779       return err_status_cipher_fail; 
     4007      return srtp_err_status_cipher_fail; 
    27804008  } 
    27814009 
    27824010  /* if we're encrypting, exor keystream into the message */ 
    27834011  if (enc_start) { 
    2784     status = cipher_encrypt(stream->rtcp_cipher,  
    2785                             (uint8_t *)enc_start, &enc_octet_len); 
     4012    status = srtp_cipher_encrypt(session_keys->rtcp_cipher,  
     4013                                (uint8_t *)enc_start, &enc_octet_len); 
    27864014    if (status) 
    2787       return err_status_cipher_fail; 
     4015      return srtp_err_status_cipher_fail; 
    27884016  } 
    27894017 
    27904018  /* initialize auth func context */ 
    2791   auth_start(stream->rtcp_auth); 
     4019  srtp_auth_start(session_keys->rtcp_auth); 
    27924020 
    27934021  /*  
     
    27954023   * result at auth_tag  
    27964024   */ 
    2797   status = auth_compute(stream->rtcp_auth,  
     4025  status = srtp_auth_compute(session_keys->rtcp_auth, 
    27984026                        (uint8_t *)auth_start,  
    27994027                        (*pkt_octet_len) + sizeof(srtcp_trailer_t),  
    28004028                        auth_tag); 
    28014029  debug_print(mod_srtp, "srtcp auth tag:    %s",  
    2802               octet_string_hex_string(auth_tag, tag_len)); 
     4030              srtp_octet_string_hex_string(auth_tag, tag_len)); 
    28034031  if (status) 
    2804     return err_status_auth_fail;    
     4032    return srtp_err_status_auth_fail;    
    28054033     
    28064034  /* increase the packet length by the length of the auth tag and seq_num*/ 
    28074035  *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t)); 
     4036 
     4037  /* increase the packet by the mki_size */ 
     4038  *pkt_octet_len += mki_size; 
    28084039     
    2809   return err_status_ok;   
    2810 } 
    2811  
    2812  
    2813 err_status_t  
     4040  return srtp_err_status_ok;   
     4041} 
     4042 
     4043 
     4044srtp_err_status_t  
    28144045srtp_unprotect_rtcp(srtp_t ctx, void *srtcp_hdr, int *pkt_octet_len) { 
     4046    return srtp_unprotect_rtcp_mki(ctx, srtcp_hdr, pkt_octet_len, 0); 
     4047} 
     4048 
     4049srtp_err_status_t 
     4050srtp_unprotect_rtcp_mki(srtp_t ctx, void *srtcp_hdr, int *pkt_octet_len, 
     4051                        unsigned int use_mki) { 
    28154052  srtcp_hdr_t *hdr = (srtcp_hdr_t *)srtcp_hdr; 
    28164053  uint32_t *enc_start;      /* pointer to start of encrypted portion  */ 
     
    28214058  uint8_t tmp_tag[SRTP_MAX_TAG_LEN]; 
    28224059  uint8_t tag_copy[SRTP_MAX_TAG_LEN]; 
    2823   err_status_t status;    
     4060  srtp_err_status_t status;    
    28244061  unsigned int auth_len; 
    28254062  int tag_len; 
    28264063  srtp_stream_ctx_t *stream; 
    2827   int prefix_len; 
     4064  uint32_t prefix_len; 
    28284065  uint32_t seq_num; 
    28294066  int e_bit_in_packet;     /* whether the E-bit was found in the packet */ 
    28304067  int sec_serv_confidentiality; /* whether confidentiality was requested */ 
     4068  unsigned int mki_size = 0; 
     4069  srtp_session_keys_t *session_keys = NULL; 
    28314070 
    28324071  /* we assume the hdr is 32-bit aligned to start */ 
     
    28364075     a positive value */ 
    28374076  if (*pkt_octet_len < octets_in_rtcp_header + sizeof(srtcp_trailer_t)) 
    2838     return err_status_bad_param; 
     4077    return srtp_err_status_bad_param; 
    28394078 
    28404079  /* 
     
    28674106 
    28684107      debug_print(mod_srtp, "srtcp using provisional stream (SSRC: 0x%08x)",  
    2869                   hdr->ssrc); 
     4108                  ntohl(hdr->ssrc)); 
    28704109    } else { 
    28714110      /* no template stream, so we return an error */ 
    2872       return err_status_no_ctx; 
     4111      return srtp_err_status_no_ctx; 
    28734112    }  
    28744113  } 
    2875    
     4114 
     4115  /* 
     4116   * Determine if MKI is being used and what session keys should be used 
     4117   */ 
     4118  if (use_mki) { 
     4119      session_keys = srtp_get_session_keys(stream, (uint8_t *)hdr, 
     4120                                           (const unsigned int*)pkt_octet_len, 
     4121                                           &mki_size); 
     4122 
     4123      if (session_keys == NULL)  
     4124         return srtp_err_status_bad_mki; 
     4125  } else { 
     4126      session_keys = &stream->session_keys[0]; 
     4127  }   
     4128 
     4129 
    28764130  /* get tag length from stream context */ 
    2877   tag_len = auth_get_tag_length(stream->rtcp_auth); 
     4131  tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth); 
    28784132 
    28794133  /* check the packet length - it must contain at least a full RTCP 
    28804134     header, an auth tag (if applicable), and the SRTCP encrypted flag 
    28814135     and 31-bit index value */ 
    2882   if (*pkt_octet_len < (int) (octets_in_rtcp_header + tag_len + sizeof(srtcp_trailer_t))) { 
    2883     return err_status_bad_param; 
     4136  if (*pkt_octet_len < (int) (octets_in_rtcp_header + tag_len + mki_size + sizeof(srtcp_trailer_t))) { 
     4137    return srtp_err_status_bad_param; 
    28844138  } 
    28854139 
     
    28884142   * the request to our AEAD handler. 
    28894143   */ 
    2890   if (stream->rtp_cipher->algorithm == AES_128_GCM || 
    2891       stream->rtp_cipher->algorithm == AES_256_GCM) { 
    2892       return srtp_unprotect_rtcp_aead(ctx, stream, srtcp_hdr, (unsigned int*)pkt_octet_len); 
     4144  if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 || 
     4145      session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) { 
     4146      return srtp_unprotect_rtcp_aead(ctx, stream, srtcp_hdr, 
     4147                                      (unsigned int*)pkt_octet_len, session_keys, 
     4148                                      mki_size); 
    28934149  } 
    28944150 
     
    29004156   */ 
    29014157  enc_octet_len = *pkt_octet_len -  
    2902                   (octets_in_rtcp_header + tag_len + sizeof(srtcp_trailer_t)); 
     4158                  (octets_in_rtcp_header + tag_len + mki_size + sizeof(srtcp_trailer_t)); 
    29034159  /* index & E (encryption) bit follow normal data.  hdr->len 
    29044160         is the number of words (32-bit) in the normal packet minus 1 */ 
     
    29114167   */ 
    29124168  trailer = (uint32_t *) ((char *) hdr + 
    2913       *pkt_octet_len -(tag_len + sizeof(srtcp_trailer_t))); 
     4169      *pkt_octet_len -(tag_len + mki_size + sizeof(srtcp_trailer_t))); 
    29144170  e_bit_in_packet = 
    29154171      (*((unsigned char *) trailer) & SRTCP_E_BYTE_BIT) == SRTCP_E_BYTE_BIT; 
    29164172  if (e_bit_in_packet != sec_serv_confidentiality) { 
    2917     return err_status_cant_check; 
     4173    return srtp_err_status_cant_check; 
    29184174  } 
    29194175  if (sec_serv_confidentiality) { 
     
    29294185   */ 
    29304186  auth_start = (uint32_t *)hdr; 
    2931   auth_len = *pkt_octet_len - tag_len; 
    2932   auth_tag = (uint8_t *)hdr + auth_len; 
     4187 
     4188  /* 
     4189   * The location of the auth tag in the packet needs to know MKI  
     4190   * could be present.  The data needed to calculate the Auth tag 
     4191   * must not include the MKI 
     4192   */ 
     4193  auth_len = *pkt_octet_len - tag_len - mki_size; 
     4194  auth_tag = (uint8_t *)hdr + auth_len + mki_size; 
    29334195 
    29344196  /*  
     
    29404202   */ 
    29414203  if (stream->ekt) { 
    2942     auth_tag -= ekt_octets_after_base_tag(stream->ekt); 
     4204    auth_tag -= srtp_ekt_octets_after_base_tag(stream->ekt); 
    29434205    memcpy(tag_copy, auth_tag, tag_len); 
    29444206    octet_string_set_to_zero(auth_tag, tag_len); 
     
    29534215  seq_num = ntohl(*trailer) & SRTCP_INDEX_MASK; 
    29544216  debug_print(mod_srtp, "srtcp index: %x", seq_num); 
    2955   status = rdb_check(&stream->rtcp_rdb, seq_num); 
     4217  status = srtp_rdb_check(&stream->rtcp_rdb, seq_num); 
    29564218  if (status) 
    29574219    return status; 
     
    29604222   * if we're using aes counter mode, set nonce and seq  
    29614223   */ 
    2962   if (stream->rtcp_cipher->type->id == AES_ICM) { 
     4224  if (session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_128 || 
     4225      session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_192 || 
     4226      session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_256) { 
    29634227    v128_t iv; 
    29644228 
     
    29674231    iv.v32[2] = htonl(seq_num >> 16); 
    29684232    iv.v32[3] = htonl(seq_num << 16); 
    2969     status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_decrypt); 
     4233    status = srtp_cipher_set_iv(session_keys->rtcp_cipher, 
     4234                                (uint8_t*)&iv, srtp_direction_decrypt); 
    29704235 
    29714236  } else {   
     
    29774242    iv.v32[2] = 0; 
    29784243    iv.v32[3] = htonl(seq_num); 
    2979     status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_decrypt); 
     4244    status = srtp_cipher_set_iv(session_keys->rtcp_cipher, 
     4245                                (uint8_t*)&iv, srtp_direction_decrypt); 
    29804246 
    29814247  } 
    29824248  if (status) 
    2983     return err_status_cipher_fail; 
     4249    return srtp_err_status_cipher_fail; 
    29844250 
    29854251  /* initialize auth func context */ 
    2986   auth_start(stream->rtcp_auth); 
     4252  srtp_auth_start(session_keys->rtcp_auth); 
    29874253 
    29884254  /* run auth func over packet, put result into tmp_tag */ 
    2989   status = auth_compute(stream->rtcp_auth, (uint8_t *)auth_start,   
     4255  status = srtp_auth_compute(session_keys->rtcp_auth, (uint8_t *)auth_start, 
    29904256                        auth_len, tmp_tag); 
    29914257  debug_print(mod_srtp, "srtcp computed tag:       %s",  
    2992               octet_string_hex_string(tmp_tag, tag_len)); 
     4258              srtp_octet_string_hex_string(tmp_tag, tag_len)); 
    29934259  if (status) 
    2994     return err_status_auth_fail;    
     4260    return srtp_err_status_auth_fail;    
    29954261   
    29964262  /* compare the tag just computed with the one in the packet */ 
    29974263  debug_print(mod_srtp, "srtcp tag from packet:    %s",  
    2998               octet_string_hex_string(auth_tag, tag_len));   
     4264              srtp_octet_string_hex_string(auth_tag, tag_len));   
    29994265  if (octet_string_is_eq(tmp_tag, auth_tag, tag_len)) 
    3000     return err_status_auth_fail; 
     4266    return srtp_err_status_auth_fail; 
    30014267 
    30024268  /*  
     
    30044270   * prefix into the authentication tag 
    30054271   */ 
    3006   prefix_len = auth_get_prefix_length(stream->rtcp_auth);     
     4272  prefix_len = srtp_auth_get_prefix_length(session_keys->rtcp_auth);     
    30074273  if (prefix_len) { 
    3008     status = cipher_output(stream->rtcp_cipher, auth_tag, prefix_len); 
     4274    status = srtp_cipher_output(session_keys->rtcp_cipher, auth_tag, &prefix_len); 
    30094275    debug_print(mod_srtp, "keystream prefix: %s",  
    3010                 octet_string_hex_string(auth_tag, prefix_len)); 
     4276                srtp_octet_string_hex_string(auth_tag, prefix_len)); 
    30114277    if (status) 
    3012       return err_status_cipher_fail; 
     4278      return srtp_err_status_cipher_fail; 
    30134279  } 
    30144280 
    30154281  /* if we're decrypting, exor keystream into the message */ 
    30164282  if (enc_start) { 
    3017     status = cipher_decrypt(stream->rtcp_cipher,  
    3018                             (uint8_t *)enc_start, &enc_octet_len); 
     4283    status = srtp_cipher_decrypt(session_keys->rtcp_cipher, (uint8_t *)enc_start, 
     4284                                &enc_octet_len); 
    30194285    if (status) 
    3020       return err_status_cipher_fail; 
     4286      return srtp_err_status_cipher_fail; 
    30214287  } 
    30224288 
    30234289  /* decrease the packet length by the length of the auth tag and seq_num */ 
    30244290  *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t)); 
     4291 
     4292  /* decrease the packet length by the length of the mki_size */ 
     4293  *pkt_octet_len -= mki_size; 
    30254294 
    30264295  /* 
     
    30284297   * length 
    30294298   */ 
    3030   *pkt_octet_len -= ekt_octets_after_base_tag(stream->ekt); 
     4299  *pkt_octet_len -= srtp_ekt_octets_after_base_tag(stream->ekt); 
    30314300 
    30324301  /*  
     
    30764345 
    30774346  /* we've passed the authentication check, so add seq_num to the rdb */ 
    3078   rdb_add_index(&stream->rtcp_rdb, seq_num); 
     4347  srtp_rdb_add_index(&stream->rtcp_rdb, seq_num); 
    30794348     
    30804349     
    3081   return err_status_ok;   
     4350  return srtp_err_status_ok;   
    30824351} 
    30834352 
     
    31024371 */ 
    31034372 
    3104 err_status_t 
    3105 crypto_policy_set_from_profile_for_rtp(crypto_policy_t *policy,  
    3106                                        srtp_profile_t profile) { 
     4373srtp_err_status_t 
     4374srtp_crypto_policy_set_from_profile_for_rtp(srtp_crypto_policy_t *policy,  
     4375                                            srtp_profile_t profile) { 
    31074376 
    31084377  /* set SRTP policy from the SRTP profile in the key set */ 
    31094378  switch(profile) { 
    31104379  case srtp_profile_aes128_cm_sha1_80: 
    3111     crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); 
     4380    srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); 
    31124381    break; 
    31134382  case srtp_profile_aes128_cm_sha1_32: 
    3114     crypto_policy_set_aes_cm_128_hmac_sha1_32(policy); 
     4383    srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(policy); 
    31154384    break; 
    31164385  case srtp_profile_null_sha1_80: 
    3117     crypto_policy_set_null_cipher_hmac_sha1_80(policy); 
     4386    srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy); 
    31184387    break; 
    3119   case srtp_profile_aes256_cm_sha1_80: 
    3120     crypto_policy_set_aes_cm_256_hmac_sha1_80(policy); 
     4388#if defined(OPENSSL) 
     4389  case srtp_profile_aead_aes_128_gcm: 
     4390    srtp_crypto_policy_set_aes_gcm_128_16_auth(policy); 
    31214391    break; 
    3122   case srtp_profile_aes256_cm_sha1_32: 
    3123     crypto_policy_set_aes_cm_256_hmac_sha1_32(policy); 
     4392  case srtp_profile_aead_aes_256_gcm: 
     4393    srtp_crypto_policy_set_aes_gcm_256_16_auth(policy); 
    31244394    break; 
     4395#endif 
    31254396    /* the following profiles are not (yet) supported */ 
    31264397  case srtp_profile_null_sha1_32: 
    31274398  default: 
    3128     return err_status_bad_param; 
    3129   } 
    3130  
    3131   return err_status_ok; 
    3132 } 
    3133  
    3134 err_status_t 
    3135 crypto_policy_set_from_profile_for_rtcp(crypto_policy_t *policy,  
    3136                                         srtp_profile_t profile) { 
     4399    return srtp_err_status_bad_param; 
     4400  } 
     4401 
     4402  return srtp_err_status_ok; 
     4403} 
     4404 
     4405srtp_err_status_t 
     4406srtp_crypto_policy_set_from_profile_for_rtcp(srtp_crypto_policy_t *policy,  
     4407                                             srtp_profile_t profile) { 
    31374408 
    31384409  /* set SRTP policy from the SRTP profile in the key set */ 
    31394410  switch(profile) { 
    31404411  case srtp_profile_aes128_cm_sha1_80: 
    3141     crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); 
     4412    srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); 
    31424413    break; 
    31434414  case srtp_profile_aes128_cm_sha1_32: 
    31444415    /* We do not honor the 32-bit auth tag request since 
    31454416     * this is not compliant with RFC 3711 */ 
    3146     crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); 
     4417    srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); 
    31474418    break; 
    31484419  case srtp_profile_null_sha1_80: 
    3149     crypto_policy_set_null_cipher_hmac_sha1_80(policy); 
     4420    srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy); 
    31504421    break; 
    3151   case srtp_profile_aes256_cm_sha1_80: 
    3152     crypto_policy_set_aes_cm_256_hmac_sha1_80(policy); 
     4422#if defined(OPENSSL) 
     4423  case srtp_profile_aead_aes_128_gcm: 
     4424    srtp_crypto_policy_set_aes_gcm_128_16_auth(policy); 
    31534425    break; 
    3154   case srtp_profile_aes256_cm_sha1_32: 
    3155     /* We do not honor the 32-bit auth tag request since 
    3156      * this is not compliant with RFC 3711 */ 
    3157     crypto_policy_set_aes_cm_256_hmac_sha1_80(policy); 
     4426  case srtp_profile_aead_aes_256_gcm: 
     4427    srtp_crypto_policy_set_aes_gcm_256_16_auth(policy); 
    31584428    break; 
     4429#endif 
    31594430    /* the following profiles are not (yet) supported */ 
    31604431  case srtp_profile_null_sha1_32: 
    31614432  default: 
    3162     return err_status_bad_param; 
    3163   } 
    3164  
    3165   return err_status_ok; 
    3166 } 
    3167  
    3168 void 
    3169 append_salt_to_key(uint8_t *key, unsigned int bytes_in_key, 
    3170                    uint8_t *salt, unsigned int bytes_in_salt) { 
    3171  
     4433    return srtp_err_status_bad_param; 
     4434  } 
     4435 
     4436  return srtp_err_status_ok; 
     4437} 
     4438 
     4439void srtp_append_salt_to_key(uint8_t *key, unsigned int bytes_in_key, uint8_t *salt, unsigned int bytes_in_salt) { 
    31724440  memcpy(key + bytes_in_key, salt, bytes_in_salt); 
    3173  
    31744441} 
    31754442 
     
    31794446  switch(profile) { 
    31804447  case srtp_profile_aes128_cm_sha1_80: 
    3181     return 16; 
     4448    return SRTP_AES_128_KEY_LEN; 
    31824449    break; 
    31834450  case srtp_profile_aes128_cm_sha1_32: 
    3184     return 16; 
     4451    return SRTP_AES_128_KEY_LEN; 
    31854452    break; 
    31864453  case srtp_profile_null_sha1_80: 
    3187     return 16; 
     4454    return SRTP_AES_128_KEY_LEN; 
    31884455    break; 
    3189   case srtp_profile_aes256_cm_sha1_80: 
    3190     return 32; 
     4456  case srtp_profile_aead_aes_128_gcm: 
     4457    return SRTP_AES_128_KEY_LEN; 
    31914458    break; 
    3192   case srtp_profile_aes256_cm_sha1_32: 
    3193     return 32; 
     4459  case srtp_profile_aead_aes_256_gcm: 
     4460    return SRTP_AES_256_KEY_LEN; 
    31944461    break; 
    31954462    /* the following profiles are not (yet) supported */ 
     
    32054472  switch(profile) { 
    32064473  case srtp_profile_aes128_cm_sha1_80: 
    3207     return 14; 
     4474    return SRTP_SALT_LEN; 
    32084475    break; 
    32094476  case srtp_profile_aes128_cm_sha1_32: 
    3210     return 14; 
     4477    return SRTP_SALT_LEN; 
    32114478    break; 
    32124479  case srtp_profile_null_sha1_80: 
    3213     return 14; 
     4480    return SRTP_SALT_LEN; 
    32144481    break; 
    3215   case srtp_profile_aes256_cm_sha1_80: 
    3216     return 14; 
     4482  case srtp_profile_aead_aes_128_gcm: 
     4483    return SRTP_AEAD_SALT_LEN; 
    32174484    break; 
    3218   case srtp_profile_aes256_cm_sha1_32: 
    3219     return 14; 
     4485  case srtp_profile_aead_aes_256_gcm: 
     4486    return SRTP_AEAD_SALT_LEN; 
    32204487    break; 
    32214488    /* the following profiles are not (yet) supported */ 
     
    32254492  } 
    32264493} 
     4494 
     4495srtp_err_status_t 
     4496srtp_get_protect_trailer_length(srtp_t session, 
     4497                                uint32_t use_mki, 
     4498                                uint32_t mki_index, 
     4499                                uint32_t *length)  
     4500{ 
     4501    srtp_stream_ctx_t *stream; 
     4502 
     4503    if (session == NULL) 
     4504      return srtp_err_status_bad_param; 
     4505 
     4506    *length = 0; 
     4507 
     4508    /* Try obtaining stream from stream_list */ 
     4509    stream = session->stream_list; 
     4510 
     4511    if (stream == NULL) { 
     4512        /* Try obtaining the template stream */ 
     4513        stream = session->stream_template; 
     4514    } 
     4515 
     4516    if (stream == NULL) { 
     4517        return srtp_err_status_bad_param; 
     4518    } 
     4519 
     4520    if (use_mki) { 
     4521       if (mki_index > stream->num_master_keys) 
     4522           return srtp_err_status_bad_mki; 
     4523 
     4524       *length += stream->session_keys[mki_index].mki_size; 
     4525       *length += srtp_auth_get_tag_length(stream->session_keys[mki_index].rtp_auth); 
     4526    } else { 
     4527       *length += srtp_auth_get_tag_length(stream->session_keys[0].rtp_auth); 
     4528    } 
     4529 
     4530    return srtp_err_status_ok; 
     4531} 
     4532 
     4533srtp_err_status_t 
     4534srtp_get_protect_rtcp_trailer_length(srtp_t session, 
     4535                                     uint32_t use_mki, 
     4536                                     uint32_t mki_index, 
     4537                                     uint32_t *length)  
     4538{ 
     4539    srtp_stream_ctx_t *stream; 
     4540 
     4541    if (session == NULL) 
     4542      return srtp_err_status_bad_param; 
     4543 
     4544    *length = 0; 
     4545 
     4546    /* Try obtaining stream from stream_list */ 
     4547    stream = session->stream_list; 
     4548 
     4549    if (stream == NULL) { 
     4550        /* Try obtaining the template stream */ 
     4551        stream = session->stream_template; 
     4552    } 
     4553 
     4554    if (stream == NULL) { 
     4555        return srtp_err_status_bad_param; 
     4556    } 
     4557 
     4558    if (use_mki) { 
     4559       if (mki_index > stream->num_master_keys) 
     4560           return srtp_err_status_bad_mki; 
     4561 
     4562       *length += stream->session_keys[mki_index].mki_size; 
     4563       *length += srtp_auth_get_tag_length(stream->session_keys[mki_index].rtcp_auth); 
     4564    } else { 
     4565       *length += srtp_auth_get_tag_length(stream->session_keys[0].rtcp_auth); 
     4566    } 
     4567  
     4568    *length += sizeof(srtcp_trailer_t); 
     4569 
     4570    return srtp_err_status_ok; 
     4571} 
     4572 
     4573 
     4574/* 
     4575 * SRTP debug interface 
     4576 */ 
     4577srtp_err_status_t srtp_set_debug_module(const char *mod_name, int v) 
     4578{ 
     4579    return srtp_crypto_kernel_set_debug_module(mod_name, v); 
     4580} 
     4581 
     4582srtp_err_status_t srtp_list_debug_modules(void) 
     4583{ 
     4584    return srtp_crypto_kernel_list_debug_modules(); 
     4585} 
     4586 
     4587/* 
     4588 * srtp_log_handler is a global variable holding a pointer to the 
     4589 * log handler function; this function is called for any log 
     4590 * output. 
     4591 */ 
     4592 
     4593static srtp_log_handler_func_t *srtp_log_handler = NULL; 
     4594static void * srtp_log_handler_data = NULL; 
     4595 
     4596void srtp_err_handler(srtp_err_reporting_level_t level, const char * msg) 
     4597{ 
     4598    if (srtp_log_handler) { 
     4599        srtp_log_level_t log_level = srtp_log_level_error; 
     4600        switch(level) { 
     4601            case srtp_err_level_error: log_level = srtp_log_level_error; break; 
     4602            case srtp_err_level_warning: log_level = srtp_log_level_warning; break; 
     4603            case srtp_err_level_info: log_level = srtp_log_level_info; break; 
     4604            case srtp_err_level_debug: log_level = srtp_log_level_debug; break; 
     4605        } 
     4606 
     4607        srtp_log_handler(log_level, msg, srtp_log_handler_data); 
     4608    } 
     4609} 
     4610 
     4611srtp_err_status_t srtp_install_log_handler(srtp_log_handler_func_t func, void * data) 
     4612{ 
     4613 
     4614    /* 
     4615     * note that we accept NULL arguments intentionally - calling this 
     4616     * function with a NULL arguments removes a log handler that's 
     4617     * been previously installed 
     4618     */ 
     4619 
     4620    if (srtp_log_handler) { 
     4621        srtp_install_err_report_handler(NULL); 
     4622    } 
     4623    srtp_log_handler = func; 
     4624    srtp_log_handler_data = data; 
     4625    if (srtp_log_handler) { 
     4626        srtp_install_err_report_handler(srtp_err_handler); 
     4627    } 
     4628    return srtp_err_status_ok; 
     4629} 
     4630 
     4631srtp_err_status_t 
     4632srtp_set_stream_roc(srtp_t session, uint32_t ssrc, uint32_t roc) { 
     4633    srtp_stream_t stream; 
     4634 
     4635    stream = srtp_get_stream(session, htonl(ssrc)); 
     4636    if (stream == NULL) 
     4637        return srtp_err_status_bad_param; 
     4638 
     4639    stream->pending_roc = roc; 
     4640 
     4641    return srtp_err_status_ok; 
     4642} 
     4643 
     4644srtp_err_status_t 
     4645srtp_get_stream_roc(srtp_t session, uint32_t ssrc, uint32_t *roc) { 
     4646    srtp_stream_t stream; 
     4647 
     4648    stream = srtp_get_stream(session, htonl(ssrc)); 
     4649    if (stream == NULL) 
     4650        return srtp_err_status_bad_param; 
     4651 
     4652    *roc = srtp_rdbx_get_roc(&stream->rtp_rdbx); 
     4653 
     4654    return srtp_err_status_ok; 
     4655} 
Note: See TracChangeset for help on using the changeset viewer.