|
1 /* |
|
2 * Copyright (c) 2005 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. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 // INCLUDES |
|
22 #include "srtputils.h" |
|
23 #include <e32math.h> |
|
24 #define DES_AS_8_BIT(str) (TPtrC8((TText8*)((str).Ptr()), (str).Size())) |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 // --------------------------------------------------------------------------- |
|
30 // TSRTPUtils::CountIV() |
|
31 // --------------------------------------------------------------------------- |
|
32 // |
|
33 TInt TSRTPUtils::CountIV(TDes8& aRes, |
|
34 const TDesC8& aSalt, |
|
35 TUint aSSRC, |
|
36 TUint64 aIndex) |
|
37 { |
|
38 SRTP_DEBUG_DETAIL( "TSRTPUtils::CountIV ENNTRY"); |
|
39 |
|
40 TBuf8<32> firstIVTerm; |
|
41 TBuf8<32> secondIVTerm; |
|
42 TBuf8<32> thirdIVTerm; |
|
43 |
|
44 GetIVFirstTerm(firstIVTerm, aSalt); |
|
45 |
|
46 GetIVSecondTerm(secondIVTerm, aSSRC); |
|
47 |
|
48 GetIVThirdTerm(thirdIVTerm, aIndex); |
|
49 |
|
50 |
|
51 |
|
52 if (aRes.Length() != 16) |
|
53 { |
|
54 SRTP_DEBUG_TINT_VALUE( "the length is", aRes.Length() ); |
|
55 return KErrArgument; |
|
56 } |
|
57 |
|
58 //first XOR 1st (=padded session salt) and 2nd (= padded SSRC) terms |
|
59 for (TInt i=0; i<16; i++) |
|
60 { |
|
61 aRes[i] = firstIVTerm[i] ^ secondIVTerm[i]; |
|
62 } |
|
63 |
|
64 //next XOR result with 1st term |
|
65 for (TInt i=0; i<16; i++) |
|
66 { |
|
67 aRes[i] = thirdIVTerm[i] ^ aRes[i]; |
|
68 } |
|
69 SRTP_DEBUG_DETAIL( "TSRTPUtils::CountIV EXIT"); |
|
70 return KErrNone; |
|
71 } |
|
72 |
|
73 // --------------------------------------------------------------------------- |
|
74 // TSRTPUtils::GetIVFirstTerm() |
|
75 // --------------------------------------------------------------------------- |
|
76 // |
|
77 void TSRTPUtils::GetIVFirstTerm(TDes8& aRes, const TDesC8& aSalt) |
|
78 { |
|
79 TInt count = 16 - aSalt.Length(); |
|
80 aRes.Copy(aSalt); |
|
81 for (TInt i = 0; i<count; i++ ) |
|
82 { |
|
83 aRes.Append(0x00); |
|
84 } |
|
85 } |
|
86 |
|
87 |
|
88 // --------------------------------------------------------------------------- |
|
89 // TSRTPUtils::GetIVSecondTerm() |
|
90 // --------------------------------------------------------------------------- |
|
91 // |
|
92 void TSRTPUtils::GetIVSecondTerm(TDes8& aRes, TUint aSSRC) |
|
93 { |
|
94 TBuf8<32> ssrc; |
|
95 TBuf8<32> temp; |
|
96 |
|
97 ssrc.AppendNumUC(aSSRC, EHex); |
|
98 TSRTPUtils::DeHex(ssrc, aRes); |
|
99 |
|
100 for (TInt i=0; i<8; i++) |
|
101 { |
|
102 aRes.Append(0x00); |
|
103 } |
|
104 |
|
105 TInt count = 16 - aRes.Length(); |
|
106 for (TInt i = 0; i<count; i++ ) |
|
107 { |
|
108 temp.Append(0x00); |
|
109 } |
|
110 |
|
111 aRes.Insert(0, temp); |
|
112 } |
|
113 |
|
114 |
|
115 // --------------------------------------------------------------------------- |
|
116 // TSRTPUtils::GetIVThirdTerm() |
|
117 // --------------------------------------------------------------------------- |
|
118 // |
|
119 void TSRTPUtils::GetIVThirdTerm(TDes8& aRes, TUint64 aIndex) |
|
120 { |
|
121 TBuf8<32> temp; |
|
122 TBuf8<32> index; |
|
123 |
|
124 index.AppendNumUC(aIndex, EHex); |
|
125 TSRTPUtils::DeHex(index, aRes); |
|
126 |
|
127 aRes.Append(0x00); |
|
128 aRes.Append(0x00); |
|
129 |
|
130 TInt count = 16 - aRes.Length(); |
|
131 for (TInt i = 0; i<count; i++ ) |
|
132 { |
|
133 temp.Append(0x00); |
|
134 } |
|
135 |
|
136 aRes.Insert(0, temp); |
|
137 } |
|
138 |
|
139 |
|
140 |
|
141 // --------------------------------------------------------------------------- |
|
142 // TSRTPUtils::Cnt_r() |
|
143 // --------------------------------------------------------------------------- |
|
144 // |
|
145 TUint64 TSRTPUtils::Cnt_r(const TUint64 aIndex, |
|
146 const TUint32 aDerivRate) |
|
147 { |
|
148 return SrtpDIV(aIndex, aDerivRate); |
|
149 } |
|
150 |
|
151 // --------------------------------------------------------------------------- |
|
152 // TSRTPUtils::Cnt_key_id() |
|
153 // --------------------------------------------------------------------------- |
|
154 // |
|
155 TUint64 TSRTPUtils::Cnt_key_id(const TUint8 aLabel, |
|
156 const TUint64 a_R, |
|
157 const TUint64 aIndexLength) |
|
158 { |
|
159 return (aLabel * aIndexLength + a_R); |
|
160 } |
|
161 |
|
162 // --------------------------------------------------------------------------- |
|
163 // TUint16 TSRTPUtils::Read16() |
|
164 // --------------------------------------------------------------------------- |
|
165 // |
|
166 TUint16 TSRTPUtils::Read16( const TUint8* const aPointer ) |
|
167 { |
|
168 return static_cast<TUint16>( aPointer[1] + ( aPointer[0] << 8 ) ); |
|
169 } |
|
170 |
|
171 // --------------------------------------------------------------------------- |
|
172 // TSRTPUtils::Write16() |
|
173 // --------------------------------------------------------------------------- |
|
174 // |
|
175 void TSRTPUtils::Write16( TUint8* const aPointer, TUint16 aValue ) |
|
176 { |
|
177 // check value range (16 bits) |
|
178 aPointer[0] = static_cast<TUint8>( ( aValue & 0xFF00 ) >> 8 ); |
|
179 aPointer[1] = static_cast<TUint8>( aValue & 0x00FF ); |
|
180 } |
|
181 |
|
182 // --------------------------------------------------------------------------- |
|
183 // TSRTPUtils::Write32() |
|
184 // Write a 32-bit aValue as 4 consecutive bytes in MSB order |
|
185 // Memory (at least 4 bytes) must have been allocated to aPointer |
|
186 // before the function is called. |
|
187 // --------------------------------------------------------------------------- |
|
188 void TSRTPUtils::Write32( TUint8* const aPointer, TUint32 aValue ) |
|
189 { |
|
190 aPointer[0] = static_cast<TUint8>( ( aValue & 0xFF000000 ) >> 24 ); |
|
191 aPointer[1] = static_cast<TUint8>( ( aValue & 0x00FF0000 ) >> 16 ); |
|
192 aPointer[2] = static_cast<TUint8>( ( aValue & 0x0000FF00 ) >> 8 ); |
|
193 aPointer[3] = static_cast<TUint8>( aValue & 0x000000FF ); |
|
194 } |
|
195 |
|
196 // --------------------------------------------------------------------------- |
|
197 // TUint32 TSRTPUtils::Read32() |
|
198 // Read a 32-bit aValue given as 4 consecutive bytes in MSB order |
|
199 // Memory (at least 4 bytes) must have been allocated to aPointer |
|
200 // before the function is called. |
|
201 // --------------------------------------------------------------------------- |
|
202 TUint32 TSRTPUtils::Read32( const TUint8* const aPointer ) |
|
203 { |
|
204 return ( aPointer[3] + |
|
205 ( static_cast<TUint32>( aPointer[2] ) << 8 ) + |
|
206 ( static_cast<TUint32>( aPointer[1] ) << 16 ) + |
|
207 ( static_cast<TUint32>( aPointer[0] ) << 24 ) ); |
|
208 } |
|
209 |
|
210 // --------------------------------------------------------------------------- |
|
211 // TSRTPUtils::Cnt_x() |
|
212 // --------------------------------------------------------------------------- |
|
213 // |
|
214 void TSRTPUtils::Cnt_x(const TUint64 a_key_id, TDes8& aRes, const TDesC8& aSalt) |
|
215 { |
|
216 |
|
217 TBuf8<32> key_id; |
|
218 TBuf8<16> key_id_dehexed; |
|
219 |
|
220 key_id.AppendNumUC(a_key_id, EHex); |
|
221 TSRTPUtils::DeHex(key_id, key_id_dehexed); |
|
222 |
|
223 aRes.Copy(aSalt); |
|
224 |
|
225 TUint key_length = key_id_dehexed.Length(); |
|
226 TUint salt_length = aRes.Length(); |
|
227 TInt i,j; |
|
228 |
|
229 for (i=key_length-1, j=salt_length-1; i>=0; i--, j--) |
|
230 { |
|
231 aRes[j] = key_id_dehexed[i] ^ aRes[j]; |
|
232 } |
|
233 } |
|
234 |
|
235 |
|
236 |
|
237 // --------------------------------------------------------------------------- |
|
238 // TSRTPUtils::SrtpDIV() |
|
239 // --------------------------------------------------------------------------- |
|
240 // |
|
241 TUint64 TSRTPUtils::SrtpDIV(const TUint64 aA ,const TUint64 aB) |
|
242 { |
|
243 if (aB == 0) |
|
244 { |
|
245 return 0; |
|
246 } |
|
247 else |
|
248 { |
|
249 return (aA / aB); |
|
250 } |
|
251 } |
|
252 |
|
253 |
|
254 // --------------------------------------------------------------------------- |
|
255 // TSRTPUtils::HexVal() |
|
256 // --------------------------------------------------------------------------- |
|
257 // |
|
258 TUint8 TSRTPUtils::HexVal(TUint8 c) |
|
259 { |
|
260 if (c >= 'a' && c <= 'f') |
|
261 return (TUint8)(c - 'a' + 10); |
|
262 else if (c >= 'A' && c <= 'F') |
|
263 return (TUint8)(c - 'A' + 10); |
|
264 else if (c >= '0' && c <= '9') |
|
265 return (TUint8)(c - '0'); |
|
266 else |
|
267 return 0; |
|
268 } |
|
269 |
|
270 // --------------------------------------------------------------------------- |
|
271 // TSRTPUtils::HexString() |
|
272 // --------------------------------------------------------------------------- |
|
273 // |
|
274 void TSRTPUtils::HexString(HBufC8& aString) |
|
275 { |
|
276 TPtr8 ptr=aString.Des(); |
|
277 if (aString.Length()%2) |
|
278 { |
|
279 ptr.SetLength(0); |
|
280 return; |
|
281 } |
|
282 TInt i; |
|
283 for (i=0;i<aString.Length();i+=2) |
|
284 { |
|
285 TUint8 tmp; |
|
286 tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0')); |
|
287 tmp*=16; |
|
288 tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0')); |
|
289 ptr[i/2]=tmp; |
|
290 } |
|
291 ptr.SetLength(aString.Length()/2); |
|
292 } |
|
293 |
|
294 |
|
295 // --------------------------------------------------------------------------- |
|
296 // TSRTPUtils::DeHex() |
|
297 // --------------------------------------------------------------------------- |
|
298 // |
|
299 void TSRTPUtils::DeHex(const TDesC8 &aSrc, TDes8 &aDest) |
|
300 { |
|
301 const TUint8 *s = aSrc.Ptr(); |
|
302 int i = aSrc.Length(); |
|
303 TUint8 d1, d2; |
|
304 |
|
305 TReal remainder; |
|
306 Math::Mod(remainder,i,2); |
|
307 if (remainder>0) |
|
308 { |
|
309 d1 = TSRTPUtils::HexVal(*s++); |
|
310 aDest.Append((TUint8)(d1)); |
|
311 i--; |
|
312 } |
|
313 while (i > 0) |
|
314 { |
|
315 d1 = TSRTPUtils::HexVal(*s++); |
|
316 d2 = TSRTPUtils::HexVal(*s++); |
|
317 aDest.Append((TUint8)(d1 * 16 + d2)); |
|
318 i -= 2; |
|
319 } |
|
320 } |
|
321 |
|
322 #ifdef _SRTP_FILE |
|
323 |
|
324 // --------------------------------------------------------------------------- |
|
325 // TInt TSRTPUtils::Print() |
|
326 // |
|
327 // --------------------------------------------------------------------------- |
|
328 // |
|
329 |
|
330 void TSRTPUtils::Print( const TDesC& aName, const TDesC& aP ) |
|
331 { |
|
332 HBufC* msg = NULL; |
|
333 TRAPD( msgError, msg = HBufC::NewL( aP.Length() + 20 ) ); |
|
334 if ( msgError != KErrNone ) |
|
335 { |
|
336 delete msg; msg = NULL; |
|
337 return; |
|
338 } |
|
339 |
|
340 TPtr16 b( msg->Des() ); |
|
341 |
|
342 b.Append( KSRTPString ); |
|
343 |
|
344 b.AppendFormat( _L( "%u - " ), ( TUint * ) ( TSRTPUtils::GtGetTime() ) ); |
|
345 |
|
346 b.Append( aP ); |
|
347 b.Append( _L( "\n" ) ); |
|
348 |
|
349 TFileUtil::LogMessage( aName, b ); |
|
350 |
|
351 delete msg; |
|
352 |
|
353 } |
|
354 |
|
355 // --------------------------------------------------------------------------- |
|
356 // TInt TSRTPUtils::Print() |
|
357 // |
|
358 // --------------------------------------------------------------------------- |
|
359 // |
|
360 void TSRTPUtils::Print( const TDesC& aName, const TDesC& aP, TInt aValue ) |
|
361 { |
|
362 HBufC* msg = NULL; |
|
363 TRAPD( msgError, msg = HBufC::NewL( aP.Length() + 106 ) ); |
|
364 if ( msgError != KErrNone ) |
|
365 { |
|
366 delete msg; msg = NULL; |
|
367 return; |
|
368 } |
|
369 |
|
370 TPtr16 b( msg->Des() ); |
|
371 b.Append( KSRTPString ); |
|
372 |
|
373 //apend current time |
|
374 b.AppendFormat( _L( "%u - " ), ( TUint * ) ( TSRTPUtils::GtGetTime() ) ); |
|
375 b.Append( aP ); |
|
376 b.AppendFormat( _L( "<%d> " ), aValue ); |
|
377 b.Append( _L( "\n" ) ); |
|
378 |
|
379 TFileUtil::LogMessage( aName, b ); |
|
380 |
|
381 delete msg; |
|
382 } |
|
383 |
|
384 // --------------------------------------------------------------------------- |
|
385 // TInt TFileUtil::LogMessage() |
|
386 // |
|
387 // --------------------------------------------------------------------------- |
|
388 // |
|
389 TInt TFileUtil::LogMessage( const TFileName& name, const TDesC& mess ) |
|
390 { |
|
391 RFs fs; |
|
392 RFile file; |
|
393 TFileName nameWithPath; |
|
394 |
|
395 nameWithPath = KDefaultPath; |
|
396 nameWithPath.Append( name ); |
|
397 nameWithPath.ZeroTerminate(); |
|
398 TInt err = fs.Connect(); |
|
399 |
|
400 if ( err != KErrNone ) |
|
401 return err; |
|
402 |
|
403 err = file.Open( fs, nameWithPath, EFileStream | EFileWrite | EFileShareExclusive ); |
|
404 if ( err == KErrNotFound ) |
|
405 err = file.Create( fs, nameWithPath, EFileStream | EFileWrite | EFileShareExclusive ); |
|
406 if ( err != KErrNone ) |
|
407 return err; |
|
408 |
|
409 TInt off( 0 ); |
|
410 err = file.Seek( ESeekEnd, off ); |
|
411 if ( err != KErrNone ) |
|
412 return err; |
|
413 |
|
414 file.Write( DES_AS_8_BIT (mess) ); |
|
415 file.Flush(); |
|
416 file.Close(); |
|
417 fs.Close(); |
|
418 |
|
419 return KErrNone; |
|
420 } |
|
421 |
|
422 // --------------------------------------------------------------------------- |
|
423 // TInt TFileUtil::InitLogFile() |
|
424 // |
|
425 // --------------------------------------------------------------------------- |
|
426 // |
|
427 TInt TFileUtil::InitLogFile( const TFileName& name ) |
|
428 { |
|
429 RFs fs; |
|
430 RFile file; |
|
431 TFileName nameWithPath; |
|
432 |
|
433 nameWithPath = KDefaultPath; |
|
434 nameWithPath.Append( name ); |
|
435 nameWithPath.ZeroTerminate(); |
|
436 TInt err = fs.Connect(); |
|
437 |
|
438 if ( err != KErrNone ) |
|
439 return err; |
|
440 |
|
441 err = file.Replace( fs, nameWithPath, EFileStream | EFileWrite | EFileShareExclusive ); |
|
442 if ( err != KErrNone ) |
|
443 return err; |
|
444 |
|
445 file.Close(); |
|
446 fs.Close(); |
|
447 |
|
448 return KErrNone; |
|
449 } |
|
450 |
|
451 // --------------------------------------------------------------------------- |
|
452 // void SrtpPrint() |
|
453 // Utility function for logging a string |
|
454 // --------------------------------------------------------------------------- |
|
455 // |
|
456 void TSRTPUtils::SrtpPrintToFile( TPtrC8 aString ) |
|
457 { |
|
458 TSRTPUtils::OctetToHexString( aString, aString.Length(), KLogFile ); |
|
459 } |
|
460 |
|
461 // --------------------------------------------------------------------------- |
|
462 // void TSRTPUtils::OctetToHexString() |
|
463 // Write the Hex Character to the files |
|
464 // --------------------------------------------------------------------------- |
|
465 // |
|
466 void |
|
467 TSRTPUtils::OctetToHexString(TPtrC8 aString, int length, const TDesC& aName) |
|
468 { |
|
469 |
|
470 TBuf<256> test; |
|
471 int i; |
|
472 |
|
473 /* double length, since one octet takes two hex characters */ |
|
474 TInt len= length *2; |
|
475 if (len<256) |
|
476 { |
|
477 |
|
478 for (i=0;i<length;i++) |
|
479 { |
|
480 TUint8 di; |
|
481 di= (TUint8)((aString[i]>>4)&0xF); |
|
482 test.AppendNumUC(di, EHex); |
|
483 di=(TUint8)(aString[i]&0xF); |
|
484 test.AppendNumUC(di, EHex); |
|
485 } |
|
486 test.Append( _L( "\n" )); |
|
487 |
|
488 TFileUtil::LogMessage( aName, test ); |
|
489 } |
|
490 } |
|
491 |
|
492 #endif |
|
493 |
|
494 #ifdef _DEBUG |
|
495 |
|
496 // --------------------------------------------------------------------------- |
|
497 // TInt TSRTPUtils::Print() |
|
498 // |
|
499 // --------------------------------------------------------------------------- |
|
500 // |
|
501 |
|
502 void TSRTPUtils::Print( const TDesC& aP ) |
|
503 { |
|
504 HBufC* msg = NULL; |
|
505 TRAPD( msgError, msg = HBufC::NewL( aP.Length() + 20 ) ); |
|
506 if ( msgError != KErrNone ) |
|
507 { |
|
508 delete msg; msg = NULL; |
|
509 return; |
|
510 } |
|
511 |
|
512 TPtr16 b( msg->Des() ); |
|
513 |
|
514 b.Append( KSRTPString ); |
|
515 |
|
516 //apend current time |
|
517 b.AppendFormat( _L( "%u - " ), ( TUint * ) ( TSRTPUtils::GtGetTime() ) ); |
|
518 |
|
519 b.Append( aP ); |
|
520 b.Append( _L( "\n" ) ); |
|
521 |
|
522 TBuf<128> buf; |
|
523 buf.Copy( b ); |
|
524 RDebug::Print( buf ); |
|
525 |
|
526 delete msg; |
|
527 } |
|
528 |
|
529 |
|
530 |
|
531 // --------------------------------------------------------------------------- |
|
532 // TInt TSRTPUtils::Print() |
|
533 // |
|
534 // --------------------------------------------------------------------------- |
|
535 // |
|
536 void TSRTPUtils::Print( const TDesC& aP, TInt aValue ) |
|
537 { |
|
538 HBufC* msg = NULL; |
|
539 TRAPD( msgError, msg = HBufC::NewL( aP.Length() + 106 ) ); |
|
540 if ( msgError != KErrNone ) |
|
541 { |
|
542 delete msg; msg = NULL; |
|
543 return; |
|
544 } |
|
545 |
|
546 TPtr16 b( msg->Des() ); |
|
547 b.Append( KSRTPString ); |
|
548 |
|
549 //apend current time |
|
550 b.AppendFormat( _L( "%u - " ), ( TUint * ) ( TSRTPUtils::GtGetTime() ) ); |
|
551 b.Append( aP ); |
|
552 b.AppendFormat( _L( "<%d> " ), aValue ); |
|
553 b.Append( _L( "\n" ) ); |
|
554 |
|
555 TBuf<128> buf; |
|
556 buf.Copy( b ); |
|
557 RDebug::Print( buf ); |
|
558 |
|
559 delete msg; |
|
560 } |
|
561 |
|
562 |
|
563 // --------------------------------------------------------------------------- |
|
564 // TUint TSRTPUtils::GtGetTime() |
|
565 // Return time elapse from 01/01/1970 in tenth of millsecond |
|
566 // --------------------------------------------------------------------------- |
|
567 // |
|
568 TUint TSRTPUtils::GtGetTime() |
|
569 { |
|
570 TTimeIntervalMicroSeconds32 uspertick; // scale factor: number of microseconds per system tick |
|
571 UserHal::TickPeriod( uspertick ); |
|
572 return static_cast<TUint>( uspertick.Int() // number of '1us periods' per system tick |
|
573 * User::TickCount() // number of system ticks |
|
574 / 100 ); // in tenths of ms, note still wraps after 5 days. |
|
575 } |
|
576 |
|
577 |
|
578 #endif |
|
579 |
|
580 |