|
1 #ifndef __CRYPTO_SHACOMMON_H_ |
|
2 #define __CRYPTO_SHACOMMON_H_ |
|
3 |
|
4 #include <e32base.h>/* |
|
5 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
6 * All rights reserved. |
|
7 * This component and the accompanying materials are made available |
|
8 * under the terms of the License "Eclipse Public License v1.0" |
|
9 * which accompanies this distribution, and is available |
|
10 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
11 * |
|
12 * Initial Contributors: |
|
13 * Nokia Corporation - initial contribution. |
|
14 * |
|
15 * Contributors: |
|
16 * |
|
17 * Description: |
|
18 * Same as used in SHA1 |
|
19 * SHA_CH > CSHA1_F |
|
20 * SHA_Maj > CSHA1_H |
|
21 * SHA_Parity > CSHA1_G |
|
22 * The following definitions are equivalent and potentially faster. |
|
23 * #define SHA_Ch(x, y, z) (((x) & ((y) ^ (z))) ^ (z)) |
|
24 * #define SHA_Maj(x, y, z) (((x) & ((y) | (z))) | ((y) & (z))) |
|
25 * These functions are defined in FIPS 180-2 Section 4.1 |
|
26 * Equation 4.1, 4.2, 4.3, 4.8, 4.9 |
|
27 * |
|
28 */ |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 /** |
|
38 @file |
|
39 @internalComponent |
|
40 @released |
|
41 */ |
|
42 template<typename T> |
|
43 inline T SHA_Ch(T aX, T aY, T aZ) |
|
44 { |
|
45 return ((aX & aY) ^ ((~aX) & aZ)); |
|
46 } |
|
47 |
|
48 template<typename T> |
|
49 inline T SHA_Maj(T aX, T aY, T aZ) |
|
50 { |
|
51 return ((aX & aY) ^ (aX & aZ) ^ (aY & aZ)); |
|
52 } |
|
53 |
|
54 template<typename T> |
|
55 inline T SHA_Parity(T aX, T aY, T aZ) |
|
56 { |
|
57 return (aX ^ aY ^ aZ); |
|
58 } |
|
59 |
|
60 /** |
|
61 * Define the SHA shift, and rotate right macro |
|
62 * Defined in FIPS 180-2 Section 3.2 |
|
63 */ |
|
64 /** |
|
65 * SHA Right Shift operation: The right shift operation SHR^n(x), |
|
66 * where x is a w-bit word and n is an integer with 0 <= n < w, |
|
67 * is defined by SHR^n(x) = x >> n. |
|
68 */ |
|
69 template<typename T> |
|
70 inline T SHA_SHR(T aBits, T aWord) |
|
71 { |
|
72 return (aWord >> aBits); |
|
73 } |
|
74 |
|
75 /** |
|
76 * SHA Rotate Right Operation: The rotate right (circular right shift) operation |
|
77 * ROTR^n(x), where x is a w-bit word and n is an integer with 0 <= n < w, |
|
78 * is defined by ROTR n(x)=(x >> n) || (x << w - n). |
|
79 */ |
|
80 template<typename T> |
|
81 inline T SHA_ROTR(T aBits, T aWord) |
|
82 { |
|
83 TInt totalBits = sizeof(T) << 3; |
|
84 return ((aWord >> aBits) | (aWord << (totalBits-aBits))); |
|
85 } |
|
86 |
|
87 NONSHARABLE_CLASS(MSHA2Impl) |
|
88 { |
|
89 public: |
|
90 /** |
|
91 * This function will reset the state of hash. |
|
92 */ |
|
93 virtual void Reset(const TAny*) = 0; |
|
94 /** |
|
95 * This function will finalize the hash and return |
|
96 * the calculated hash. |
|
97 * @return Final hash |
|
98 */ |
|
99 virtual const TDesC8& Final() = 0; |
|
100 /** |
|
101 * This function will add the message to the internal |
|
102 * buffer and if the block size is reached then calcualte |
|
103 * the hash till that point. |
|
104 * @param aMessage Message to be updated. |
|
105 * @param aLength Length of the message to be updated. |
|
106 */ |
|
107 virtual void Update(const TUint8* aMessage, TUint aLength) = 0; |
|
108 /** |
|
109 * This function will save the internal state of the hash. |
|
110 */ |
|
111 virtual void StoreState() = 0; |
|
112 /** |
|
113 * This function will retrieve the saved the internal state |
|
114 * of the hash. |
|
115 */ |
|
116 virtual void RestoreState() = 0; |
|
117 /** |
|
118 * virtual distructor. |
|
119 */ |
|
120 virtual ~MSHA2Impl(){} |
|
121 }; |
|
122 |
|
123 #endif //__CRYPTO_SHACOMMON_H_ |