-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKISA_HMAC.asp
155 lines (123 loc) · 4.37 KB
/
KISA_HMAC.asp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!--#include file="KISA_SHA256.asp"-->
<%
Private Const SHA256_DIGEST_VALUELEN = 32
Private Const SHA256_DIGEST_BLOCKLEN = 64
class KISA_HMAC_C
public Function HMAC_SHA256(message, mlen, key, klen, byref hmac)
dim ipad
dim opad
dim i
dim tk(31)
dim tb(63)
dim macLen
dim tmp
dim tmplen
macLen = SHA256_DIGEST_VALUELEN
ipad = &H36
opad = &H5C
if klen > SHA256_DIGEST_BLOCKLEN then
call SHA256_Encrypt(key, klen, tk)
klen = SHA256_DIGEST_VALUELEN
key = tk
end if
for i = 0 To klen - 1
tb(i) = ipad xor key(i)
next
for i = klen to SHA256_DIGEST_BLOCKLEN - 1
tb(i) = ipad xor 0
next
'message = tb(64) | message(mlen)'
tmplen = SHA256_DIGEST_BLOCKLEN + mlen
redim tmp(tmplen)
for i = 0 to SHA256_DIGEST_BLOCKLEN - 1
tmp(i) = tb(i)
next
for i = 0 to mlen - 1
tmp(SHA256_DIGEST_BLOCKLEN + i) = message(i)
next
'hmac = SHA256_Encrypt(message, mlen)
call SHA256_Encrypt(tmp, tmplen, hmac)
for i = 0 to klen - 1
tb(i) = opad xor key(i)
next
for i = klen to SHA256_DIGEST_BLOCKLEN - 1
tb(i) = opad xor 0
next
'message = tb(64) | hmac(32)'
'hmac = SHA256_Encrypt(message)
tmplen = SHA256_DIGEST_BLOCKLEN + SHA256_DIGEST_VALUELEN
redim tmp(tmplen)
for i = 0 to SHA256_DIGEST_BLOCKLEN - 1
tmp(i) = tb(i)
next
for i = 0 to SHA256_DIGEST_VALUELEN - 1
tmp(SHA256_DIGEST_BLOCKLEN + i) = hmac(i)
next
'hmac = SHA256_Encrypt(message, mlen)
call SHA256_Encrypt(tmp, tmplen, hmac)
HMAC_SHA256 = 0
end function
public Function Verify_HMAC_SHA256(message, mlen, key, klen, hmac)
dim ipad
dim opad
dim i
dim tk(31)
dim tb(63)
dim macLen
dim tmp
dim tmplen
dim cmp_hmac(31)
macLen = SHA256_DIGEST_VALUELEN
ipad = &H36
opad = &H5C
if klen > SHA256_DIGEST_BLOCKLEN then
call SHA256_Encrypt(key, klen, tk)
klen = SHA256_DIGEST_VALUELEN
key = tk
end if
for i = 0 To klen - 1
tb(i) = ipad xor key(i)
next
for i = klen to SHA256_DIGEST_BLOCKLEN - 1
tb(i) = ipad xor 0
next
'message = tb(64) | message(mlen)'
tmplen = SHA256_DIGEST_BLOCKLEN + mlen
redim tmp(tmplen)
for i = 0 to SHA256_DIGEST_BLOCKLEN - 1
tmp(i) = tb(i)
next
for i = 0 to mlen - 1
tmp(SHA256_DIGEST_BLOCKLEN + i) = message(i)
next
'hmac = SHA256_Encrypt(message, mlen)
call SHA256_Encrypt(tmp, tmplen, cmp_hmac)
for i = 0 to klen - 1
tb(i) = opad xor key(i)
next
for i = klen to SHA256_DIGEST_BLOCKLEN - 1
tb(i) = opad xor 0
next
'message = tb(64) | hmac(32)'
'hmac = SHA256_Encrypt(message)
tmplen = SHA256_DIGEST_BLOCKLEN + SHA256_DIGEST_VALUELEN
redim tmp(tmplen)
for i = 0 to SHA256_DIGEST_BLOCKLEN - 1
tmp(i) = tb(i)
next
for i = 0 to SHA256_DIGEST_VALUELEN - 1
tmp(SHA256_DIGEST_BLOCKLEN + i) = cmp_hmac(i)
next
'hmac = SHA256_Encrypt(message, mlen)
call SHA256_Encrypt(tmp, tmplen, cmp_hmac)
for i = 0 to 31
if hmac(i) <> cmp_hmac(i) then
Verify_HMAC_SHA256 = 1
exit function
end if
next
Verify_HMAC_SHA256 = 0
end function
end class
set KISA_HMAC = new KISA_HMAC_C
%>