|
1 /* |
|
2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Contains SRTP utility functions (mostly mathematical |
|
15 * and calculation oriented). |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 #ifndef __SRTPUTILS_H__ |
|
23 #define __SRTPUTILS_H__ |
|
24 |
|
25 // INCLUDES |
|
26 #include <e32def.h> |
|
27 #include <e32err.h> |
|
28 #include <e32math.h> |
|
29 |
|
30 #include <e32std.h> |
|
31 #include <f32file.h> |
|
32 |
|
33 #ifdef SRTP_UNIT_TEST_COVERAGE |
|
34 |
|
35 #undef _DEBUG |
|
36 #undef SRTP_LOG |
|
37 #endif |
|
38 |
|
39 |
|
40 #if ( defined( _DEBUG ) && ( defined ( SRTP_LOG ) ) ) |
|
41 #define _SRTP_FILE |
|
42 |
|
43 #endif |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 #if ( defined( _DEBUG ) && !( defined ( _SRTP_FILE ) ) ) |
|
49 |
|
50 #define SRTP_DEBUG_DETAIL( a ) { TSRTPUtils::Print( _L( a ) ); } |
|
51 #define SRTP_DEBUG_PACKET( a ) |
|
52 #define SRTP_DEBUG_TINT_VALUE( a, b ) { TSRTPUtils::Print( _L( a ), static_cast<TInt>( b ) ); } |
|
53 #define SRTP_DEBUG_TUINT_VALUE( a, b ) { TSRTPUtils::Print( _L( a ), static_cast<TInt>( b ) );} |
|
54 #define SRTP_DEBUG_TUINT32_VALUE( a, b ) { TSRTPUtils::Print( _L( a ), static_cast<TInt>( b ) );} |
|
55 |
|
56 #endif |
|
57 |
|
58 #ifdef _SRTP_FILE |
|
59 |
|
60 #define SRTP_DEBUG_DETAIL( a ) { TSRTPUtils::Print( KLogFile, _L( a ) ); } |
|
61 #define SRTP_DEBUG_PACKET( a ) { TSRTPUtils::SrtpPrintToFile( a ); } |
|
62 #define SRTP_DEBUG_TINT_VALUE( a, b ) { TSRTPUtils::Print( KLogFile, _L( a ), b ); } |
|
63 #define SRTP_DEBUG_TUINT_VALUE( a, b ) { TSRTPUtils::Print( KLogFile, _L( a ), static_cast<TInt>( b ) ); } |
|
64 #define SRTP_DEBUG_TUINT32_VALUE( a, b ) { TSRTPUtils::Print( KLogFile, _L( a ), static_cast<TInt>( b ) ); } |
|
65 |
|
66 #endif |
|
67 |
|
68 #if ( !defined( _DEBUG ) ) |
|
69 |
|
70 #define SRTP_DEBUG_DETAIL( a ) |
|
71 #define SRTP_DEBUG_PACKET( a ) |
|
72 #define SRTP_DEBUG_TINT_VALUE( a, b ) |
|
73 #define SRTP_DEBUG_TUINT_VALUE( a, b ) |
|
74 #define SRTP_DEBUG_TUINT32_VALUE( a, b ) |
|
75 |
|
76 #endif |
|
77 |
|
78 |
|
79 // DATA TYPES |
|
80 _LIT( KDefaultPath, "c:\\system\\data\\" ); |
|
81 _LIT( KLogFile, "LogFileSRTP.dat" ); |
|
82 _LIT( KSRTPString, "SRTP : "); |
|
83 |
|
84 |
|
85 // |
|
86 const TUint16 K2EXP16 = 65536; |
|
87 const TUint32 K2EXP32 = 4294967296; |
|
88 const TUint64 KSRTPIndexLength = 0x1000000000000; |
|
89 const TUint64 KSRTCPIndexLength = 0x100000000; |
|
90 |
|
91 class TSRTPUtils |
|
92 { |
|
93 public: |
|
94 /** |
|
95 * Count IV to be used as Integer Value in |
|
96 * AES-CM RTP/RTCP encryption/decryption. |
|
97 * @param aRes resulting 128 bit IV |
|
98 * @param aSalt 112 bit session salt (not padded) |
|
99 * @param aSSRC 32 bit SSRC value from packet (not padded) |
|
100 * @param aIndex 48 bit packet index (RTP, not padded) or |
|
101 * 31 bit SRTCP index (RTCP, not padded) |
|
102 * @return KErrNone/KErrArgument |
|
103 */ |
|
104 static TInt CountIV(TDes8& aRes, |
|
105 const TDesC8& aSalt, |
|
106 TUint aSSRC, |
|
107 TUint64 aIndex); |
|
108 |
|
109 |
|
110 /** |
|
111 * Add 16 bit padding for 112 bit session salt |
|
112 * @param aRes resulting 128 bit padded session salt |
|
113 * @param aSalt 112 bit session salt |
|
114 * @return void |
|
115 */ |
|
116 static void GetIVFirstTerm(TDes8& aRes, const TDesC8& aSalt); |
|
117 /** |
|
118 * Add 64 bit padding for 32 bit SSRC value |
|
119 * @param aRes resulting 96 bit padded SSRC value |
|
120 * @param aSSRC 32 bit SSRC value from packet |
|
121 * @return void |
|
122 */ |
|
123 static void GetIVSecondTerm(TDes8& aRes, TUint aSSRC); |
|
124 /** |
|
125 * Add 16 bit padding for RTP/RTCP packet index |
|
126 * @param aRes resulting 64/47 bit index |
|
127 * @param aIndex 48 bit packet index (RTP, not padded) or |
|
128 * 31 bit SRTCP index (RTCP, not padded) |
|
129 * @return void |
|
130 */ |
|
131 static void GetIVThirdTerm(TDes8& aRes, TUint64 aIndex); |
|
132 |
|
133 /* |
|
134 * r is calculated in following way: |
|
135 * 'index DIV key_derivation_rate' |
|
136 * This function is common to RTP and RTCP. |
|
137 * @param aIndex packet index |
|
138 * @param derivation rate |
|
139 * @return 'index DIV key_derivation_rate' |
|
140 */ |
|
141 static TUint64 Cnt_r(const TUint64 aIndex, const TUint32 aDerivRate); |
|
142 |
|
143 /* |
|
144 * key_id is calculated in following way: |
|
145 * 'label || r' |
|
146 * This function is common to RTP and RTCP. |
|
147 * @param aLabel 8 bit RTP/RTCP value |
|
148 * @param a_R, 'index DIV key_derivation_rate' |
|
149 * @param aIndexLength, packet index length (RTP or RTCP) |
|
150 * @return key_id ('label || r') |
|
151 */ |
|
152 static TUint64 Cnt_key_id(const TUint8 aLabel, |
|
153 const TUint64 a_R, |
|
154 const TUint64 aIndexLength); |
|
155 |
|
156 /* |
|
157 * TUint16 SRTPUtils::Read16() |
|
158 * Read a 16-bit value given as 2 consecutive bytes in MSB order |
|
159 * Memory (at least 2 bytes) must have been allocated to pointer |
|
160 * before the function is called. |
|
161 */ |
|
162 static TUint16 Read16( const TUint8* const aPointer ); |
|
163 |
|
164 /* |
|
165 * Write a 16-bit value as 2 consecutive bytes in MSB order |
|
166 * Memory (at least 2 bytes) must have been allocated to pointer |
|
167 * before the function is called. |
|
168 */ |
|
169 static void Write16( TUint8* const aPointer, TUint16 aValue ); |
|
170 |
|
171 /* |
|
172 * TUint32 SRTPUtils::Read32() |
|
173 * Read a 32-bit value given as 2 consecutive bytes in MSB order |
|
174 * Memory (at least 2 bytes) must have been allocated to pointer |
|
175 * before the function is called. |
|
176 */ |
|
177 static TUint32 Read32( const TUint8* const aPointer ); |
|
178 |
|
179 /* |
|
180 * Write a 32-bit value as 2 consecutive bytes in MSB order |
|
181 * Memory (at least 2 bytes) must have been allocated to pointer |
|
182 * before the function is called. |
|
183 */ |
|
184 static void Write32( TUint8* const aPointer, TUint32 aValue ); |
|
185 |
|
186 /* |
|
187 * Calculates 'x', which is needed in key derivation. |
|
188 * Is calculated in following way: |
|
189 * 'key_id XOR master_salt' |
|
190 * This function is common to RTP and RTCP. |
|
191 * @param a_key_id, |
|
192 * @param aRes, result |
|
193 * @param aSalt master salt |
|
194 * @return 'x', ('key_id XOR master_salt') |
|
195 */ |
|
196 static void Cnt_x(const TUint64 a_key_id, TDes8& aRes, const TDesC8& aSalt); |
|
197 |
|
198 |
|
199 /* |
|
200 * Integer division of "a div b", |
|
201 * with exception that 'a div 0' == 0 for all a |
|
202 * This function is common to RTP and RTCP. |
|
203 * @param aA, value A |
|
204 * @param aB, value B |
|
205 * @return SRTP style "a div b" |
|
206 */ |
|
207 static TUint64 SrtpDIV(const TUint64 aA ,const TUint64 aB); |
|
208 |
|
209 /* |
|
210 * Converts 8 bit descriptor ASCII values to hex values |
|
211 * @param aSrc, source descriptor |
|
212 * @param aDest, source descriptor |
|
213 * @return void |
|
214 */ |
|
215 static void DeHex(const TDesC8 &aSrc, TDes8 &aDest); |
|
216 |
|
217 /* |
|
218 * Converts an 8 bit ASCII value to hex value |
|
219 * @param c, character |
|
220 * @return hex value |
|
221 */ |
|
222 static TUint8 HexVal(TUint8 c); |
|
223 |
|
224 /* |
|
225 * Converts a string into hex |
|
226 * @param aString, string to convert |
|
227 * @return none. |
|
228 */ |
|
229 static void HexString(HBufC8& aString); |
|
230 |
|
231 #ifdef _DEBUG |
|
232 static TUint GtGetTime(); |
|
233 |
|
234 static void Print( const TDesC& aP ); |
|
235 static void Print( const TDesC& aP , TInt aValue ); |
|
236 |
|
237 |
|
238 #endif |
|
239 |
|
240 #ifdef _SRTP_FILE |
|
241 static void Print( const TDesC& name, const TDesC& aP ); |
|
242 static void Print( const TDesC& name, const TDesC& aP, TInt aValue ); |
|
243 |
|
244 static void OctetToHexString(TPtrC8 aString, int length, const TDesC& aName); |
|
245 static void SrtpPrint( TPtrC8 aString ); |
|
246 static void SrtpPrintToFile( TPtrC8 aString ); |
|
247 #endif |
|
248 |
|
249 }; |
|
250 |
|
251 #ifdef _SRTP_FILE |
|
252 |
|
253 class TFileUtil |
|
254 { |
|
255 public: |
|
256 static TInt LogMessage( const TFileName&, const TDesC& ); |
|
257 static TInt InitLogFile( const TFileName& name ); |
|
258 }; |
|
259 |
|
260 #endif |
|
261 |
|
262 #endif // __SRTPUTILS_H__ |