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