1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - AES - include
3   File:     types.h
4 
5   Copyright 2007-2008 Nintendo. 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   $Date:: 2008-09-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef	TWL_AES_COMMON_TYPE_H_
19 #define	TWL_AES_COMMON_TYPE_H_
20 
21 #ifdef	__cplusplus
22 extern "C" {
23 #endif
24 
25 /*===========================================================================*/
26 
27 #include	<twl/misc.h>
28 #include	<twl/types.h>
29 
30 /*---------------------------------------------------------------------------*
31     Constant Definitions
32  *---------------------------------------------------------------------------*/
33 
34 #define AES_BLOCK_SIZE          16  // 128-bit
35 #define AES_KEY_SIZE            16  // 128-bit
36 #define AES_NONCE_SIZE          12  //  96-bit
37 #define AES_MAC_MAX_SIZE        16  // 128-bit
38 #define AES_COUNTER_SIZE        16  // 128-bit
39 
40 #define AES_ADATA_BLOCK_NUM_MAX 0xFFFF
41 #define AES_PDATA_BLOCK_NUM_MAX 0xFFFF
42 
43 #define AES_ADATA_SIZE_MAX      (AES_BLOCK_SIZE * AES_ADATA_BLOCK_NUM_MAX)
44 #define AES_PDATA_SIZE_MAX      (AES_BLOCK_SIZE * AES_PDATA_BLOCK_NUM_MAX)
45 
46 
47 #define AESi_ASSERT_MAC_LENGTH(x)                               \
48     SDK_ASSERTMSG( ((x) == AES_MAC_LENGTH_4)                    \
49                 || ((x) == AES_MAC_LENGTH_6)                    \
50                 || ((x) == AES_MAC_LENGTH_8)                    \
51                 || ((x) == AES_MAC_LENGTH_10)                   \
52                 || ((x) == AES_MAC_LENGTH_12)                   \
53                 || ((x) == AES_MAC_LENGTH_14)                   \
54                 || ((x) == AES_MAC_LENGTH_16),                  \
55                 "%s(=%d) is not valid AESMacLength.", #x, (x) )
56 
57 
58 // AES processing results
59 typedef enum AESResult
60 {
61     AES_RESULT_NONE,                    // The processing result has not been obtained
62     AES_RESULT_SUCCESS,                 // Encryption, decryption, or verification was successful
63     AES_RESULT_VERIFICATION_FAILED,     // Authentication failed
64     AES_RESULT_INVALID,                 // Invalid argument
65     AES_RESULT_BUSY,                    // AES processing is in progress
66     AES_RESULT_ON_DS,                   // Unusable because the program is running on a DS
67     AES_RESULT_UNKNOWN,                 // Internal library error
68     AES_RESULT_MAX
69 }
70 AESResult;
71 
72 // MAC length in bytes
73 typedef enum AESMacLength
74 {
75     AES_MAC_LENGTH_4  = 1,      // 4 bytes
76     AES_MAC_LENGTH_6  = 2,
77     AES_MAC_LENGTH_8  = 3,
78     AES_MAC_LENGTH_10 = 4,
79     AES_MAC_LENGTH_12 = 5,
80     AES_MAC_LENGTH_14 = 6,
81     AES_MAC_LENGTH_16 = 7,      // 16 bytes
82     AES_MAC_LENGTH_MAX
83 }
84 AESMacLength;
85 
86 
87 
88 /*---------------------------------------------------------------------------*
89     Structure Definitions
90  *---------------------------------------------------------------------------*/
91 
92 // 128-bit AES key
93 typedef union AESKey
94 {
95     u8  bytes[AES_KEY_SIZE];
96     u32 words[AES_KEY_SIZE/sizeof(u32)];
97 }
98 AESKey;
99 
100 // 96-bit AES nonce
101 typedef union AESNonce
102 {
103     u8  bytes[AES_NONCE_SIZE];
104     u32 words[AES_NONCE_SIZE/sizeof(u32)];
105 }
106 AESNonce;
107 
108 // AES MAC between 32 and 128 bits
109 typedef union AESMac
110 {
111     u8  bytes[AES_MAC_MAX_SIZE];
112     u32 words[AES_MAC_MAX_SIZE/sizeof(u32)];
113 }
114 AESMac;
115 
116 // 128-bit AES counter initial value
117 typedef union AESCounter
118 {
119     u8  bytes[AES_COUNTER_SIZE];
120     u32 words[AES_COUNTER_SIZE/sizeof(u32)];
121 }
122 AESCounter;
123 
124 // AES process completion callback
125 typedef void (*AESCallback)(AESResult result, void* arg);
126 
127 
128 
129 /*---------------------------------------------------------------------------*
130 	Function Definitions
131  *---------------------------------------------------------------------------*/
132 
133 /*---------------------------------------------------------------------------*
134   Name:         AES_GetMacLengthValue
135 
136   Description:  Gets the MAC length in bytes as an AESMacLength value.
137 
138   Arguments:    macLength:  Enumerated type indicating the MAC length
139 
140   Returns:      Returns the MAC length in bytes.
141  *---------------------------------------------------------------------------*/
AES_GetMacLengthValue(AESMacLength macLength)142 static inline u32 AES_GetMacLengthValue(AESMacLength macLength)
143 {
144     AESi_ASSERT_MAC_LENGTH(macLength);
145 
146     return (u32)macLength * 2 + 2;
147 }
148 
149 
150 
151 /*===========================================================================*/
152 
153 #ifdef	__cplusplus
154 }          /* extern "C" */
155 #endif
156 
157 #endif /* TWL_AES_COMMON_TYPE_H_ */
158 
159 /*---------------------------------------------------------------------------*
160   End of file
161  *---------------------------------------------------------------------------*/
162