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