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