1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     crypto_CcmMode.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_CCMMODE_H_
17 #define NN_CRYPTO_DETAIL_CRYPTO_CCMMODE_H_
18 
19 #include <nn/crypto/crypto_BlockCipher.h>
20 
21 
22 namespace nn {
23 namespace crypto {
24 namespace detail {
25 
26 
27 
28 class CcmMode
29 {
30 public:
31     static const size_t  BLOCK_SIZE     = 16;
32     static const size_t  MAX_IV_SIZE    = 13;
33     static const size_t  UNIT_SIZE      = 1;
34     static const size_t  MAX_MAC_SIZE   = 16;
35 
36 public:
CcmMode()37     CcmMode() : m_pCipher(NULL) {}
38 
39     void    Initialize(const BlockCipher& c, const void* pIv, size_t ivSize,
40                         size_t adataSize, size_t pdataSize, size_t macSize);
41     void    Finalize();
42 
GetIvSize()43     static size_t  GetIvSize()   { return MAX_IV_SIZE; }
GetUnitSize()44     static size_t  GetUnitSize() { return 1; }
GetMacSize()45     static size_t  GetMacSize()  { return MAX_MAC_SIZE; }
46 
47     void    UpdateAdata(const void* pSrc, size_t size);
48     void    UpdateAdataFinal();
49     size_t  UpdatePdata(void* pDst, size_t dstSize, const void* pSrc, size_t srcSize);
50     size_t  UpdatePdataFinal(void* pDst, size_t size);
51     size_t  UpdateCdata(void* pDst, size_t dstSize, const void* pSrc, size_t srcSize);
52     size_t  UpdateCdataFinal(void* pDst, size_t size);
53     void    GenerateMac(void* pDst, size_t size);
54 
55 private:
56     const BlockCipher*  m_pCipher;
57     s8      m_DataSize;
58     s8      m_NonceSize;
59     s8      m_MacSize;
60     NN_PADDING1;
61     bit8    m_Data[BLOCK_SIZE];
62     bit32   m_CbcBuffer[ BLOCK_SIZE / sizeof(bit32) ];
63     bit32   m_CtrBuffer[ BLOCK_SIZE / sizeof(bit32) ];
64 };
65 
66 
67 
68 }   // namespace detail
69 }   // namespace crypto
70 }   // namespace nn
71 
72 
73 
74 #endif /* NN_CRYPTO_DETAIL_CRYPTO_CCMMODE_H_ */
75