Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2004-2006 the Minisip Team |
| 3 | Copyright (C) 2011 Werner Dittmann for the SRTCP support |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | |
| 22 | #ifndef CRYPTOCONTEXTCTRL_H |
| 23 | #define CRYPTOCONTEXTCTRL_H |
| 24 | |
| 25 | #include <commoncpp/config.h> |
| 26 | |
| 27 | #define REPLAY_WINDOW_SIZE 64 |
| 28 | |
| 29 | #ifdef SRTP_SUPPORT |
| 30 | #include <ccrtp/crypto/SrtpSymCrypto.h> |
| 31 | #endif |
| 32 | |
| 33 | NAMESPACE_COMMONCPP |
| 34 | |
| 35 | /** |
| 36 | * The implementation for a SRTCP cryptographic context. |
| 37 | * |
| 38 | * This class holds data and provides functions that implement a |
| 39 | * cryptographic context for SRTP, Refer to RFC 3711, chapter 3.2 for some |
| 40 | * more detailed information about the SRTP cryptographic context. |
| 41 | * |
| 42 | * Each SRTP cryptographic context maintains a RTP source identified by |
| 43 | * its SSRC. Thus you can independently protect each source inside a RTP |
| 44 | * session. |
| 45 | * |
| 46 | * Key management mechanisms negotiate the parameters for the SRTP |
| 47 | * cryptographic context, such as master key, key length, authentication |
| 48 | * length and so on. The key management mechanisms are not part of |
| 49 | * SRTP. Refer to MIKEY (RFC 3880) or to Phil Zimmermann's ZRTP protocol |
| 50 | * (draft-zimmermann-avt-zrtp-01). After key management negotiated the |
| 51 | * data the application can setup the SRTCP cryptographic context and |
| 52 | * enable SRTCP processing. |
| 53 | * |
| 54 | * |
| 55 | * @author Israel Abad <i_abad@terra.es> |
| 56 | * @author Erik Eliasson <eliasson@it.kth.se> |
| 57 | * @author Johan Bilien <jobi@via.ecp.fr> |
| 58 | * @author Joachim Orrblad <joachim@orrblad.com> |
| 59 | * @author Werner Dittmann <Werner.Dittmann@t-online.de> |
| 60 | */ |
| 61 | |
| 62 | class __EXPORT CryptoContextCtrl { |
| 63 | public: |
| 64 | /** |
| 65 | * Constructor for empty SRTP cryptographic context. |
| 66 | * |
| 67 | * This constructor creates an empty SRTP cryptographic context were |
| 68 | * all algorithms are set to the null algorithm, that is no SRTP processing |
| 69 | * is performed. |
| 70 | * |
| 71 | * @param ssrc The RTP SSRC that this SRTP cryptographic context protects. |
| 72 | */ |
| 73 | CryptoContextCtrl( uint32 ssrc ); |
| 74 | |
| 75 | /** |
| 76 | * Constructor for an active SRTP cryptographic context. |
| 77 | * |
| 78 | * This constructor creates an active SRTP cryptographic context were |
| 79 | * algorithms are enabled, keys are computed and so on. This SRTP |
| 80 | * cryptographic context can protect a RTP SSRC stream. |
| 81 | * |
| 82 | * @param ssrc |
| 83 | * The RTP SSRC that this SRTP cryptographic context protects. |
| 84 | * |
| 85 | * @param ealg |
| 86 | * The encryption algorithm to use. Possible values are <code> |
| 87 | * SrtpEncryptionNull, SrtpEncryptionAESCM, SrtpEncryptionAESF8 |
| 88 | * </code>. See chapter 4.1.1 for AESCM (Counter mode) and 4.1.2 |
| 89 | * for AES F8 mode. |
| 90 | * |
| 91 | * @param aalg |
| 92 | * The authentication algorithm to use. Possible values are <code> |
| 93 | * SrtpEncryptionNull, SrtpAuthenticationSha1Hmac</code>. The only |
| 94 | * active algorithm here is SHA1 HMAC, a SHA1 based hashed message |
| 95 | * authentication code as defined in RFC 2104. |
| 96 | * |
| 97 | * @param masterKey |
| 98 | * Pointer to the master key for this SRTP cryptographic context. |
| 99 | * Must point to <code>masterKeyLength</code> bytes. Refer to chapter |
| 100 | * 3.2.1 of the RFC about the role of the master key. |
| 101 | * |
| 102 | * @param masterKeyLength |
| 103 | * The length in bytes of the master key in bytes. The length must |
| 104 | * match the selected encryption algorithm. Because SRTP uses AES |
| 105 | * based encryption only, then master key length may be 16 or 32 |
| 106 | * bytes (128 or 256 bit master key) |
| 107 | * |
| 108 | * @param masterSalt |
| 109 | * SRTP uses the master salt to computer the initialization vector |
| 110 | * that in turn is input to compute the session key, session |
| 111 | * authentication key and the session salt. |
| 112 | * |
| 113 | * @param masterSaltLength |
| 114 | * The length in bytes of the master salt data in bytes. SRTP uses |
| 115 | * AES as encryption algorithm. AES encrypts 16 byte blocks |
| 116 | * (independent of the key length). According to RFC3711 the standard |
| 117 | * value for the master salt length should be 112 bit (14 bytes). |
| 118 | * |
| 119 | * @param ekeyl |
| 120 | * The length in bytes of the session encryption key that SRTP shall |
| 121 | * compute and use. Usually the same length as for the master key |
| 122 | * length. But you may use a different length as well. Be carefull |
| 123 | * that the key management mechanisms supports different key lengths. |
| 124 | * |
| 125 | * @param akeyl |
| 126 | * The length in bytes of the session authentication key. SRTP |
| 127 | * computes this key and uses it as input to the authentication |
| 128 | * algorithm. |
| 129 | * The standard value is 160 bits (20 bytes). |
| 130 | * |
| 131 | * @param skeyl |
| 132 | * The length in bytes of the session salt. SRTP computes this salt |
| 133 | * key and uses it as input during encryption. The length usually |
| 134 | * is the same as the master salt length. |
| 135 | * |
| 136 | * @param tagLength |
| 137 | * The length is bytes of the authentication tag that SRTP appends |
| 138 | * to the RTP packet. Refer to chapter 4.2. in the RFC 3711. |
| 139 | */ |
| 140 | CryptoContextCtrl( uint32 ssrc, |
| 141 | const int32 ealg, |
| 142 | const int32 aalg, |
| 143 | uint8* masterKey, |
| 144 | int32 masterKeyLength, |
| 145 | uint8* masterSalt, |
| 146 | int32 masterSaltLength, |
| 147 | int32 ekeyl, |
| 148 | int32 akeyl, |
| 149 | int32 skeyl, |
| 150 | int32 tagLength ); |
| 151 | /** |
| 152 | * Destructor. |
| 153 | * |
| 154 | * Cleans the SRTP cryptographic context. |
| 155 | */ |
| 156 | ~CryptoContextCtrl(); |
| 157 | |
| 158 | /** |
| 159 | * Perform SRTP encryption. |
| 160 | * |
| 161 | * This method encrypts <em>and</em> decrypts SRTP payload data. Plain |
| 162 | * data gets encrypted, encrypted data get decrypted. |
| 163 | * |
| 164 | * @param rtp |
| 165 | * The RTP packet that contains the data to encrypt. |
| 166 | * |
| 167 | * @param index |
| 168 | * The 48 bit SRTP packet index. See the <code>guessIndex</code> |
| 169 | * method. |
| 170 | * |
| 171 | * @param ssrc |
| 172 | * The RTP SSRC data in <em>host</em> order. |
| 173 | */ |
| 174 | void srtcpEncrypt( uint8* rtp, size_t len, uint64 index, uint32 ssrc ); |
| 175 | |
| 176 | /** |
| 177 | * Compute the authentication tag. |
| 178 | * |
| 179 | * Compute the authentication tag according the the paramters in the |
| 180 | * SRTP Cryptograhic context. |
| 181 | * |
| 182 | * @param rtp |
| 183 | * The RTP packet that contains the data to authenticate. |
| 184 | * |
| 185 | * @param roc |
| 186 | * The 32 bit SRTP roll-over-counter. |
| 187 | * |
| 188 | * @param tag |
| 189 | * Points to a buffer that hold the computed tag. This buffer must |
| 190 | * be able to hold <code>tagLength</code> bytes. |
| 191 | */ |
| 192 | void srtcpAuthenticate(uint8* rtp, size_t len, uint32 roc, uint8* tag ); |
| 193 | |
| 194 | /** |
| 195 | * Perform key derivation according to SRTP specification |
| 196 | * |
| 197 | * This method computes the session key, session authentication key and the |
| 198 | * session salt key. This method must be called at least once after the |
| 199 | * SRTP Cryptograhic context was set up. |
| 200 | * |
| 201 | * @param index |
| 202 | * The 48 bit SRTP packet index. See the <code>guessIndex</code> |
| 203 | * method. |
| 204 | */ |
| 205 | void deriveSrtcpKeys(); |
| 206 | |
| 207 | /** |
| 208 | * Check for packet replay. |
| 209 | * |
| 210 | * The method check if a received packet is either to old or was already |
| 211 | * received. |
| 212 | * |
| 213 | * The method supports a 64 packet history relative the the given |
| 214 | * sequence number. |
| 215 | * |
| 216 | * @param newSeqNumber |
| 217 | * The sequence number of the received RTP packet in host order. |
| 218 | * |
| 219 | * @return <code>true</code> if no replay, <code>false</code> if packet |
| 220 | * is too old ar was already received. |
| 221 | */ |
| 222 | bool checkReplay(uint32 newSeqNumber); |
| 223 | |
| 224 | /** |
| 225 | * Update the SRTP packet index. |
| 226 | * |
| 227 | * Call this method after all checks were successful. See chapter |
| 228 | * 3.3.1 in the RFC when to update the ROC and ROC processing. |
| 229 | * |
| 230 | * @param newSeqNumber |
| 231 | * The sequence number of the received RTCP packet in host order. |
| 232 | */ |
| 233 | void update( uint32 newSeqNumber ); |
| 234 | |
| 235 | /** |
| 236 | * Get the length of the SRTP authentication tag in bytes. |
| 237 | * |
| 238 | * @return the length of the authentication tag. |
| 239 | */ |
| 240 | inline int32 |
| 241 | getTagLength() const |
| 242 | {return tagLength;} |
| 243 | |
| 244 | |
| 245 | /** |
| 246 | * Get the length of the MKI in bytes. |
| 247 | * |
| 248 | * @return the length of the MKI. |
| 249 | */ |
| 250 | inline int32 |
| 251 | getMkiLength() const |
| 252 | {return mkiLength;} |
| 253 | |
| 254 | /** |
| 255 | * Get the SSRC of this SRTP Cryptograhic context. |
| 256 | * |
| 257 | * @return the SSRC. |
| 258 | */ |
| 259 | inline uint32 |
| 260 | getSsrc() const |
| 261 | {return ssrcCtx;} |
| 262 | |
| 263 | /** |
| 264 | * Derive a new Crypto Context for use with a new SSRC |
| 265 | * |
| 266 | * This method returns a new Crypto Context initialized with the data |
| 267 | * of this crypto context. Replacing the SSRC, Roll-over-Counter, and |
| 268 | * the key derivation rate the application cab use this Crypto Context |
| 269 | * to encrypt / decrypt a new stream (Synchronization source) inside |
| 270 | * one RTP session. |
| 271 | * |
| 272 | * Before the application can use this crypto context it must call |
| 273 | * the <code>deriveSrtpKeys</code> method. |
| 274 | * |
| 275 | * @param ssrc |
| 276 | * The SSRC for this context |
| 277 | * @param roc |
| 278 | * The Roll-Over-Counter for this context |
| 279 | * @param keyDerivRate |
| 280 | * The key derivation rate for this context |
| 281 | * @return |
| 282 | * a new CryptoContext with all relevant data set. |
| 283 | */ |
| 284 | CryptoContextCtrl* newCryptoContextForSSRC(uint32 ssrc); |
| 285 | |
| 286 | private: |
| 287 | |
| 288 | uint32 ssrcCtx; |
| 289 | bool using_mki; |
| 290 | uint32 mkiLength; |
| 291 | uint8* mki; |
| 292 | |
| 293 | uint32 s_l; |
| 294 | |
| 295 | /* bitmask for replay check */ |
| 296 | uint64 replay_window; |
| 297 | |
| 298 | uint8* master_key; |
| 299 | uint32 master_key_length; |
| 300 | uint8* master_salt; |
| 301 | uint32 master_salt_length; |
| 302 | |
| 303 | /* Session Encryption, Authentication keys, Salt */ |
| 304 | int32 n_e; |
| 305 | uint8* k_e; |
| 306 | int32 n_a; |
| 307 | uint8* k_a; |
| 308 | int32 n_s; |
| 309 | uint8* k_s; |
| 310 | |
| 311 | int32 ealg; |
| 312 | int32 aalg; |
| 313 | int32 ekeyl; |
| 314 | int32 akeyl; |
| 315 | int32 skeyl; |
| 316 | int32 tagLength; |
| 317 | |
| 318 | void* macCtx; |
| 319 | |
| 320 | #ifdef SRTP_SUPPORT |
| 321 | SrtpSymCrypto* cipher; |
| 322 | SrtpSymCrypto* f8Cipher; |
| 323 | #else |
| 324 | void* cipher; |
| 325 | void* f8Cipher; |
| 326 | #endif |
| 327 | |
| 328 | }; |
| 329 | |
| 330 | END_NAMESPACE |
| 331 | |
| 332 | #endif |
| 333 | |
| 334 | /** EMACS ** |
| 335 | * Local variables: |
| 336 | * mode: c++ |
| 337 | * c-default-style: ellemtel |
| 338 | * c-basic-offset: 4 |
| 339 | * End: |
| 340 | */ |
| 341 | |