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