1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     crypto_CtrMode.h
4 
5   Copyright (C)2009-2012 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: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_CRYPTO_DETAIL_CRYPTO_CTRMODE_H_
17 #define NN_CRYPTO_DETAIL_CRYPTO_CTRMODE_H_
18 
19 #include <nn/crypto/crypto_BlockCipher.h>
20 
21 
22 namespace nn {
23 namespace crypto {
24 namespace detail {
25 
26 
27 
28 template <size_t BlockSize>
29 class CtrMode
30 {
31 public:
32     static const size_t  BLOCK_SIZE     = BlockSize;
33     static const size_t  IV_SIZE        = BlockSize;
34     static const size_t  UNIT_SIZE      = 1;
35 
36 public:
CtrMode()37     CtrMode() : m_pCipher(NULL) {}
38 
GetIvSize()39     static size_t  GetIvSize() { return IV_SIZE; }
GetUnitSize()40     static size_t  GetUnitSize() { return 1; }
41 
42     void    Initialize(const BlockCipher& c, const void* pIv, size_t ivSize);
43     void    Finalize();
44     size_t  Update(void* pDst, size_t dstSize, const void* pSrc, size_t srcSize);
45     size_t  UpdateFinal(void* pDst, size_t dstSize);
46 
47 public:
48     static void ProcessBlocks(void* pDst, void* pCounter, const void* pSrc,
49                 int numBlocks, const BlockCipher& c);
50     static void IncrementCounter(void* p);
51 
52 private:
53     const BlockCipher*  m_pCipher;
54     bit32               m_Counter[IV_SIZE/sizeof(bit32)];
55     bit8                m_Data[BLOCK_SIZE];
56     s32                 m_DataSize;
57 };
58 
59 
60 
61 }   // namespace detail
62 }   // namespace crypto
63 }   // namespace nn
64 
65 
66 
67 #endif /* NN_CRYPTO_DETAIL_CRYPTO_CTRMODE_H_ */
68