1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - MATH - libraries
3 File: dgt_hmac.c
4
5 Copyright 2003-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 #include <nitro/math/dgt.h>
19 #include "hmac.h"
20
21 /* The ARM7 is located on LTDWRAM for the TWL SDK's WPA */
22 #ifdef SDK_TWLHYB
23 #ifdef SDK_ARM7
24 #include <twl/ltdwram_begin.h>
25 #endif
26 #endif
27
28 /*---------------------------------------------------------------------------*
29 Name: MATHi_CalcHMAC
30
31 Description: Calculates the MAC value using HMAC
32
33 Arguments: mac: Stores the target MAC value
34 message: Message data
35 message_length: Message data length
36 key: Key
37 key_length: Key length
38 funcs: Structure of hash function used by HMAC
39
40 Returns: None.
41 *---------------------------------------------------------------------------*/
MATHi_CalcHMAC(void * mac,const void * message,u32 message_length,const void * key,u32 key_length,MATHiHMACFuncs * funcs)42 void MATHi_CalcHMAC(void *mac, const void *message, u32 message_length,
43 const void *key, u32 key_length,
44 MATHiHMACFuncs *funcs)
45 {
46 int i;
47 u8 new_key[MATH_HASH_BLOCK_SIZE];
48 u8 *use_key;
49 u32 use_key_length;
50 u8 ipad_key[MATH_HASH_BLOCK_SIZE];
51 u8 opad_key[MATH_HASH_BLOCK_SIZE];
52
53 /* If elements are insufficient, do not process */
54 if((mac == NULL)||(message == NULL)||(message_length == 0)||(key == NULL)||
55 (key_length == 0)||(funcs == NULL))
56 {
57 return;
58 }
59
60 /* If the key is longer than the block length, the hash value is the key */
61 if(key_length > funcs->blength)
62 {
63 funcs->HashReset(funcs->context);
64 funcs->HashSetSource(funcs->context, key, key_length);
65 funcs->HashGetDigest(funcs->context, new_key);
66 use_key = new_key;
67 use_key_length = funcs->dlength;
68 }
69 else
70 {
71 use_key = (u8*)key;
72 use_key_length = key_length;
73 }
74
75 /* Key and ipad XOR */
76 for(i = 0; i < use_key_length; i++)
77 {
78 ipad_key[i] = (u8)(use_key[i] ^ 0x36);
79 }
80 /* Key padding portion and ipad XOR */
81 for(; i < funcs->blength; i++)
82 {
83 ipad_key[i] = 0x00 ^ 0x36;
84 }
85
86 /* Link to message and calculation of hash value */
87 funcs->HashReset(funcs->context);
88 funcs->HashSetSource(funcs->context, ipad_key, funcs->blength);
89 funcs->HashSetSource(funcs->context, message, message_length);
90 funcs->HashGetDigest(funcs->context, funcs->hash_buf);
91
92 /* Key and opad XOR */
93 for(i = 0; i < use_key_length; i++)
94 {
95 opad_key[i] = (u8)(use_key[i] ^ 0x5c);
96 }
97 /* Key padding portion and ipad XOR */
98 for(; i < funcs->blength; i++)
99 {
100 opad_key[i] = 0x00 ^ 0x5c;
101 }
102
103 /* Link with hash value and calculation of hash value */
104 funcs->HashReset(funcs->context);
105 funcs->HashSetSource(funcs->context, opad_key, funcs->blength);
106 funcs->HashSetSource(funcs->context, funcs->hash_buf, funcs->dlength);
107 funcs->HashGetDigest(funcs->context, mac);
108 }
109
110 #ifdef SDK_TWLHYB
111 #ifdef SDK_ARM7
112 #include <twl/ltdwram_end.h>
113 #endif
114 #endif
115