1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     crypto_HashContextBase.h
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 12449 $
14  *---------------------------------------------------------------------------*/
15 
16 
17 #ifndef NN_CRYPTO_CRYPTO_HASHCONTEXTBASE_H_
18 #define NN_CRYPTO_CRYPTO_HASHCONTEXTBASE_H_
19 
20 #include <nn/types.h>
21 #include <nn/config.h>
22 
23 #ifdef __cplusplus
24 
25 namespace nn{ namespace crypto{
26 
27 /*!
28     @brief  Hash 計算用コンテキストオブジェクトの基底クラスです。
29 
30             このクラスを直接インスタンス化することはできません。
31             継承先のクラスを使用してください。
32 */
33 class HashContextBase
34 {
35 public:
36 
37     /*
38         @brief    オブジェクトの初期化処理
39 
40         @return   無し。
41     */
Initialize()42     virtual void Initialize() {}
43 
44     /*
45         @brief    オブジェクトの完了処理
46 
47         @return   無し。
48     */
Finalize()49     virtual void Finalize() {}
50 
51     /*
52         @brief    ハッシュ/ダイジェスト値を与えられたデータで更新します。
53 
54         @return   無し。
55     */
Update(const void * pData,size_t length)56     virtual void Update(const void* pData, size_t length)
57     {
58         NN_UNUSED_VAR(pData);
59         NN_UNUSED_VAR(length);
60     }
61 
62     /*
63         @brief    ハッシュ/ダイジェストの長さを取得します。
64 
65         @return   ハッシュ/ダイジェストの長さ。
66     */
GetHashLength()67     virtual size_t GetHashLength() { return 0; }
68 
69     /*
70         @brief    最終的なハッシュ/ダイジェスト計算結果を取得します。
71 
72         @return   無し。
73     */
GetHash(void * pOut)74     virtual void GetHash(void* pOut)
75     {
76         NN_UNUSED_VAR(pOut);
77     }
78 
79 protected:
80 
81     // 単独インスタンス化の禁止
HashContextBase()82     HashContextBase() {}
~HashContextBase()83     ~HashContextBase() {}
84 
85     // 内部ブロック処理
ProcessBlock()86     virtual void ProcessBlock() {}
87 
88     // 必要内部関数
RotateLeft32(int shift,u32 value)89     inline static u32 RotateLeft32(int shift, u32 value)
90     {
91         return (u32)((value << shift) | (value >> (u32)(32 - shift)));
92     }
93 
Convert32HToBE(u32 val)94     inline static u32 Convert32HToBE( u32 val )
95     {
96 #if NN_ENDIAN == NN_ENDIAN_VALUE_LITTLE
97         return (u32)( (((val) >> 24UL) & 0x000000FFUL) |
98                       (((val) >>  8UL) & 0x0000FF00UL) |
99                       (((val) <<  8UL) & 0x00FF0000UL) |
100                       (((val) << 24UL) & 0xFF000000UL) );
101 #else
102         return val;
103 #endif
104     }
105 
FillBytes(u8 * pDst,u8 data,size_t length)106     inline static void FillBytes(u8* pDst, u8 data, size_t length)
107     {
108         for ( s32 i = 0 ; i < length ; ++i )
109         {
110             *pDst++ = data;
111         }
112     }
113 
CopyBytes(const u8 * pSrc,u8 * pDst,size_t length)114     inline static void CopyBytes(const u8* pSrc, u8* pDst, size_t length)
115     {
116         for ( s32 i = 0 ; i < length ; ++i )
117         {
118             *pDst++ = *pSrc++;
119         }
120     }
121 
122 private:
123 
124 };
125 
126 
127 }} // namespace nn::crypto
128 
129 #endif // __cplusplus
130 
131 
132 #endif /* NN_CRYPTO_CRYPTO_HASHCONTEXTBASE_H_ */
133