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