-
Notifications
You must be signed in to change notification settings - Fork 4
/
fs_hmac.c
executable file
·111 lines (78 loc) · 2.31 KB
/
fs_hmac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <string.h>
#include "tools.h"
#include "fs_hmac.h"
// reversing done by gray
static unsigned char hmac_key[20];
void hmac_init(hmac_ctx *ctx, const char *key, int key_size) {
int i;
key_size = key_size<0x40 ? key_size: 0x40;
memset(ctx->key,0,0x40);
memcpy(ctx->key,key,key_size);
for(i=0;i<0x40;++i)
ctx->key[i] ^= 0x36; // ipad
SHA1Reset(&ctx->hash_ctx);
SHA1Input(&ctx->hash_ctx,ctx->key,0x40);
}
void hmac_update(hmac_ctx *ctx, const u8 *data, int size)
{
SHA1Input(&ctx->hash_ctx,data,size);
}
void hmac_final(hmac_ctx *ctx, unsigned char *hmac)
{
int i;
unsigned char hash[0x14];
SHA1Result(&ctx->hash_ctx);
// this sha1 implementation is buggy, needs to switch endian
for(i=0;i<5;++i) {
wbe32(hash + 4*i, ctx->hash_ctx.Message_Digest[i]);
}
for(i=0;i<0x40;++i)
ctx->key[i] ^= 0x36^0x5c; // opad
SHA1Reset(&ctx->hash_ctx);
SHA1Input(&ctx->hash_ctx,ctx->key,0x40);
SHA1Input(&ctx->hash_ctx,hash,0x14);
SHA1Result(&ctx->hash_ctx);
for(i=0;i<5;++i){
wbe32(hash + 4*i, ctx->hash_ctx.Message_Digest[i]);
}
for(i=0;i<20;++i)
hmac[i] = hash[i];
}
void hmac_print(FILE *f,const unsigned char *hmac){
int i;
for(i=0;i<20;++i){
fprintf(f,"%02X ",hmac[i]);
}
fprintf(f,"\n");
}
void fs_hmac_set_key(const char *key, int key_size)
{
memset(hmac_key,0,0x14);
memcpy(hmac_key,key,key_size<0x14?key_size:0x14);
}
void fs_hmac_generic(const unsigned char *data, int size, const unsigned char *extra, int extra_size, unsigned char *hmac)
{
hmac_ctx ctx;
hmac_init(&ctx,hmac_key,0x14);
hmac_update(&ctx,extra,extra_size);
hmac_update(&ctx,data,size);
hmac_final(&ctx,hmac);
}
void fs_hmac_meta(const unsigned char *super_data, short super_blk, unsigned char *hmac)
{
unsigned char extra[0x40];
memset(extra,0,0x40);
wbe16(extra + 0x12, super_blk);
fs_hmac_generic(super_data,0x40000,extra,0x40,hmac);
}
void fs_hmac_data(const unsigned char *data, int uid, const unsigned char *name, int entry_n, int x3, short blk, unsigned char *hmac)
{
unsigned char extra[0x40];
memset(extra,0,0x40);
wbe32(extra, uid);
memcpy(extra+4,name,12);
wbe16(extra + 0x12, blk);
wbe32(extra + 0x14, entry_n);
wbe32(extra + 0x18, x3);
fs_hmac_generic(data,0x4000,extra,0x40,hmac);
}