LibXenon
Bare-metal Xbox 360 homebrew library
Loading...
Searching...
No Matches
hmac_sha1.c
Go to the documentation of this file.
1/*
2 * hmac_sha1.c
3 *
4 * Version 1.0.0
5 *
6 * Written by Aaron D. Gifford <me@aarongifford.com>
7 *
8 * Copyright 1998, 2000 Aaron D. Gifford. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the copyright holder nor the names of contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35/*
36 * The HMAC-SHA1 has is defined as:
37 *
38 * HMAC = SHA1(K XOR opad, SHA1(K XOR ipad, message))
39 *
40 * "opad" is 64 bytes filled with 0x5c
41 * "ipad" is 64 bytes filled with 0x36
42 * "K" is the key material
43 *
44 * If the key material "K" is longer than 64 bytes, then the key material
45 * will first be digested (K = SHA1(K)) resulting in a 20-byte hash.
46 * If the key material is shorter than 64 bytes, it is padded with zero
47 * bytes.
48 *
49 * This code precomputes "K XOR ipad" and "K XOR opad" since that just makes
50 * sense.
51 *
52 * This code was heavily influenced by Eric A. Young's in how the interface
53 * was designed and how this file is formatted.
54 */
55
56#ifndef __HMAC_SHA1_H__
57#define __HMAC_SHA1_H__
58
59#include "hmac_sha1.h"
60#include <string.h>
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66/* Filler bytes: */
67#define IPAD_BYTE 0x36
68#define OPAD_BYTE 0x5c
69#define ZERO_BYTE 0x00
70
72 memset(&(ctx->key[0]), ZERO_BYTE, HMAC_SHA1_BLOCK_LENGTH);
73 memset(&(ctx->ipad[0]), IPAD_BYTE, HMAC_SHA1_BLOCK_LENGTH);
74 memset(&(ctx->opad[0]), OPAD_BYTE, HMAC_SHA1_BLOCK_LENGTH);
75 ctx->keylen = 0;
76 ctx->hashkey = 0;
77}
78
79void HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, unsigned char *key, unsigned int keylen) {
80
81 /* Do we have anything to work with? If not, return right away. */
82 if (keylen < 1)
83 return;
84
85 /*
86 * Is the total key length (current data and any previous data)
87 * longer than the hash block length?
88 */
89 if (ctx->hashkey !=0 || (keylen + ctx->keylen) > HMAC_SHA1_BLOCK_LENGTH) {
90 /*
91 * Looks like the key data exceeds the hash block length,
92 * so that means we use a hash of the key as the key data
93 * instead.
94 */
95 if (ctx->hashkey == 0) {
96 /*
97 * Ah, we haven't started hashing the key
98 * data yet, so we must init. the hash
99 * monster to begin feeding it.
100 */
101
102 /* Set the hash key flag to true (non-zero) */
103 ctx->hashkey = 1;
104
105 /* Init. the hash beastie... */
106 SHA1_Init(&ctx->shactx);
107
108 /* If there's any previous key data, use it */
109 if (ctx->keylen > 0) {
110 SHA1_Update(&ctx->shactx, &(ctx->key[0]), ctx->keylen);
111 }
112
113 /*
114 * Reset the key length to the future true
115 * key length, HMAC_SHA1_DIGEST_LENGTH
116 */
118 }
119 /* Now feed the latest key data to the has monster */
120 SHA1_Update(&ctx->shactx, key, keylen);
121 } else {
122 /*
123 * Key data length hasn't yet exceeded the hash
124 * block length (HMAC_SHA1_BLOCK_LENGTH), so theres
125 * no need to hash the key data (yet). Copy it
126 * into the key buffer.
127 */
128 memcpy(&(ctx->key[ctx->keylen]), key, keylen);
129 ctx->keylen += keylen;
130 }
131}
132
134 unsigned char *ipad, *opad, *key;
135 int i;
136
137 /* Did we end up hashing the key? */
138 if (ctx->hashkey) {
139 memset(&(ctx->key[0]), ZERO_BYTE, HMAC_SHA1_BLOCK_LENGTH);
140 /* Yes, so finish up and copy the key data */
141 SHA1_Final(&(ctx->key[0]), &ctx->shactx);
142 /* ctx->keylen was already set correctly */
143 }
144 /* Pad the key if necessary with zero bytes */
145 if ((i = HMAC_SHA1_BLOCK_LENGTH - ctx->keylen) > 0) {
146 memset(&(ctx->key[ctx->keylen]), ZERO_BYTE, i);
147 }
148
149 ipad = &(ctx->ipad[0]);
150 opad = &(ctx->opad[0]);
151
152 /* Precompute the respective pads XORed with the key */
153 key = &(ctx->key[0]);
154 for (i = 0; i < ctx->keylen; i++, key++) {
155 /* XOR the key byte with the appropriate pad filler byte */
156 *ipad++ ^= *key;
157 *opad++ ^= *key;
158 }
159}
160
162 SHA1_Init(&ctx->shactx);
163 SHA1_Update(&ctx->shactx, &(ctx->ipad[0]), HMAC_SHA1_BLOCK_LENGTH);
164}
165
166void HMAC_SHA1_UpdateMessage(HMAC_SHA1_CTX *ctx, unsigned char *data, unsigned int datalen) {
167 SHA1_Update(&ctx->shactx, data, datalen);
168}
169
170void HMAC_SHA1_EndMessage(unsigned char *out, HMAC_SHA1_CTX *ctx) {
171 unsigned char buf[HMAC_SHA1_DIGEST_LENGTH];
172 SHA_CTX *c = &ctx->shactx;
173
174 SHA1_Final(&(buf[0]), c);
175 SHA1_Init(c);
178 SHA1_Final(out, c);
179}
180
182 /* Just to be safe, toast all context data */
183 memset(&(ctx->ipad[0]), ZERO_BYTE, HMAC_SHA1_BLOCK_LENGTH);
184 memset(&(ctx->ipad[0]), ZERO_BYTE, HMAC_SHA1_BLOCK_LENGTH);
185 memset(&(ctx->key[0]), ZERO_BYTE, HMAC_SHA1_BLOCK_LENGTH);
186 ctx->keylen = 0;
187 ctx->hashkey = 0;
188}
189
190void HMAC_SHA1(void *secret, void *data, void *res, int len)
191{
192 unsigned char out[20];
193 HMAC_SHA1_CTX ctx;
194 HMAC_SHA1_Init(&ctx);
195 HMAC_SHA1_UpdateKey(&ctx, (unsigned char *) secret, 0x10);
196 HMAC_SHA1_EndKey(&ctx);
198 HMAC_SHA1_UpdateMessage(&ctx, (unsigned char *) data, len);
199 HMAC_SHA1_EndMessage(out, &ctx);
200 HMAC_SHA1_Done(&ctx);
201 memcpy(res, out, 0x10);
202}
203
204#ifdef __cplusplus
205}
206#endif
207
208#endif
#define OPAD_BYTE
Definition: hmac_sha1.c:68
#define ZERO_BYTE
Definition: hmac_sha1.c:69
#define IPAD_BYTE
Definition: hmac_sha1.c:67
void HMAC_SHA1_EndMessage(unsigned char *out, HMAC_SHA1_CTX *ctx)
Definition: hmac_sha1.c:170
void HMAC_SHA1(void *secret, void *data, void *res, int len)
Definition: hmac_sha1.c:190
void HMAC_SHA1_UpdateMessage(HMAC_SHA1_CTX *ctx, unsigned char *data, unsigned int datalen)
Definition: hmac_sha1.c:166
void HMAC_SHA1_Done(HMAC_SHA1_CTX *ctx)
Definition: hmac_sha1.c:181
void HMAC_SHA1_Init(HMAC_SHA1_CTX *ctx)
Definition: hmac_sha1.c:71
void HMAC_SHA1_StartMessage(HMAC_SHA1_CTX *ctx)
Definition: hmac_sha1.c:161
void HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, unsigned char *key, unsigned int keylen)
Definition: hmac_sha1.c:79
void HMAC_SHA1_EndKey(HMAC_SHA1_CTX *ctx)
Definition: hmac_sha1.c:133
#define HMAC_SHA1_BLOCK_LENGTH
Definition: hmac_sha1.h:58
#define HMAC_SHA1_DIGEST_LENGTH
Definition: hmac_sha1.h:57
void SHA1_Final(sha1_byte digest[SHA1_DIGEST_LENGTH], SHA_CTX *context)
Definition: sha1.c:131
void SHA1_Update(SHA_CTX *context, sha1_byte *data, unsigned int len)
Definition: sha1.c:111
void SHA1_Init(SHA_CTX *context)
Definition: sha1.c:100
unsigned int keylen
Definition: hmac_sha1.h:66
unsigned char key[HMAC_SHA1_BLOCK_LENGTH]
Definition: hmac_sha1.h:65
unsigned char ipad[HMAC_SHA1_BLOCK_LENGTH]
Definition: hmac_sha1.h:62
unsigned char opad[HMAC_SHA1_BLOCK_LENGTH]
Definition: hmac_sha1.h:63
unsigned int hashkey
Definition: hmac_sha1.h:67
SHA_CTX shactx
Definition: hmac_sha1.h:64
Definition: sha.h:53
u8 c
Definition: xenos_edid.h:7
union @15 data