Subversion Repositories Kolibri OS

Rev

Rev 7698 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6469 hidnplayr 1
;    hmac.inc - HMAC: Keyed-Hashing for Message Authentication
2
;
3
;    Copyright (C) 2016 Denis Karpenko
4
;    Copyright (C) 2016 Jeffrey Amelynck
5
;
6
;    This program is free software: you can redistribute it and/or modify
7
;    it under the terms of the GNU General Public License as published by
8
;    the Free Software Foundation, either version 3 of the License, or
9
;    (at your option) any later version.
10
;
11
;    This program is distributed in the hope that it will be useful,
12
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
;    GNU General Public License for more details.
15
;
16
;    You should have received a copy of the GNU General Public License
17
;    along with this program.  If not, see .
18
 
19
; Main concept:
20
; To compute HMAC over the data `text' we perform
21
; H(K XOR opad, H(K XOR ipad, text))
22
 
9216 dunkaist 23
SHA2_256_BLOCK_SIZE = 64
24
 
6469 hidnplayr 25
struct hmac_sha256_context
9216 dunkaist 26
        hash            rb SHA2_256_LEN
27
        ipad_ctx        rb LIBCRASH_CTX_LEN
28
        opad_ctx        rb LIBCRASH_CTX_LEN
6469 hidnplayr 29
ends
30
 
31
; We will precompute partial hashes of K XOR ipad and K XOR opad,
32
; and store them in the context structure.
33
 
34
proc hmac_sha256_setkey ctx, key, key_length
35
 
36
locals
9216 dunkaist 37
        k_temp  rb SHA2_256_BLOCK_SIZE
6469 hidnplayr 38
endl
39
 
40
        pusha
41
 
42
; input esi = key, ecx=key_length
43
        mov     ecx, [key_length]
9216 dunkaist 44
        cmp     ecx, SHA2_256_BLOCK_SIZE
6469 hidnplayr 45
        ja      .hash_it
46
; Key is smaller then or equal to blocksize,
47
; copy key to ipad
48
        mov     esi, [key]
49
        lea     edi, [k_temp]
50
        rep movsb
9216 dunkaist 51
        mov     ecx, SHA2_256_BLOCK_SIZE
6469 hidnplayr 52
        sub     ecx, [key_length]
53
        jz      .finish
54
; append zeros to the key
55
        xor     al, al
56
        rep stosb
57
        jmp     .finish
58
 
59
; Given key is larger then key size, hash it
60
  .hash_it:
9216 dunkaist 61
        invoke  sha2_256_init, [ctx]
62
        invoke  sha2_256_update, [ctx], [key], [key_length]
63
        invoke  sha2_256_finish, [ctx]
6469 hidnplayr 64
        mov     esi, [ctx]
65
        lea     edi, [k_temp]
9216 dunkaist 66
        mov     ecx, SHA2_256_LEN/4
6469 hidnplayr 67
        rep movsd
68
        xor     eax, eax
9216 dunkaist 69
        mov     ecx, (SHA2_256_BLOCK_SIZE-SHA2_256_LEN)/4
6469 hidnplayr 70
        rep stosd
71
 
72
  .finish:
73
; xor ipad buffer with 0x36363...
74
        lea     esi, [k_temp]
9216 dunkaist 75
        mov     ecx, SHA2_256_BLOCK_SIZE/4
6469 hidnplayr 76
  @@:
77
        xor     dword[esi], 0x36363636          ; ipad constant
78
        add     esi, 4
79
        dec     ecx
80
        jnz     @r
81
 
82
; Init our hash with k_xor_ipad
83
        mov     ebx, [ctx]
84
        lea     edi, [ebx+hmac_sha256_context.ipad_ctx]
9216 dunkaist 85
        invoke  sha2_256_init, edi
6469 hidnplayr 86
 
87
        lea     esi, [k_temp]
88
        DEBUGF  1, "HASH: "
9216 dunkaist 89
        stdcall dump_hex, esi, SHA2_256_BLOCK_SIZE/4
6469 hidnplayr 90
 
91
        mov     ebx, [ctx]
92
        lea     edi, [ebx+hmac_sha256_context.ipad_ctx]
9216 dunkaist 93
        invoke  sha2_256_update, edi, esi, SHA2_256_BLOCK_SIZE
6469 hidnplayr 94
 
95
; xor opad buffer with 0x5c5c5...
96
        lea     esi, [k_temp]
9216 dunkaist 97
        mov     ecx, SHA2_256_BLOCK_SIZE/4
6469 hidnplayr 98
  @@:
99
        xor     dword[esi], 0x36363636 xor 0x5c5c5c5c   ; opad constant
100
        add     esi, 4
101
        dec     ecx
102
        jnz     @r
103
 
104
; Init our hash with k_xor_opad
105
        mov     ebx, [ctx]
106
        lea     edi, [ebx+hmac_sha256_context.opad_ctx]
9216 dunkaist 107
        invoke  sha2_256_init, edi
6469 hidnplayr 108
 
109
        lea     esi, [k_temp]
110
        DEBUGF  1, "HASH: "
9216 dunkaist 111
        stdcall dump_hex, esi, SHA2_256_BLOCK_SIZE/4
6469 hidnplayr 112
 
113
        mov     ebx, [ctx]
114
        lea     edi, [ebx+hmac_sha256_context.opad_ctx]
9216 dunkaist 115
        invoke  sha2_256_update, edi, esi, SHA2_256_BLOCK_SIZE
6469 hidnplayr 116
 
117
        popa
118
        ret
119
 
120
endp
121
 
122
; Copy our pre-computed partial hashes to the stack, complete and finalize them.
123
; TODO: prevent unnescessary copying of output hash
124
; TODO: remove unnescessary pushing/popping
125
 
126
proc hmac_sha256 ctx, _data, _length
127
 
128
locals
9216 dunkaist 129
        inner_ctx        rb LIBCRASH_CTX_LEN
130
        outer_ctx        rb LIBCRASH_CTX_LEN
6469 hidnplayr 131
endl
132
 
133
        pusha
134
        DEBUGF  1, "HMAC: "
135
        mov     ebx, [_length]
136
        shr     ebx, 2
137
        stdcall dump_hex, [_data], ebx
138
 
139
; Copy partial hashes of ipad and opad to our temporary buffers
140
        mov     esi, [ctx]
141
        lea     esi, [esi+hmac_sha256_context.ipad_ctx]
142
        lea     edi, [inner_ctx]
9216 dunkaist 143
repeat (LIBCRASH_CTX_LEN)/4*2
6469 hidnplayr 144
        movsd
145
end repeat
146
 
147
; Append provided data to inner hash and finalize
148
        lea     ebx, [inner_ctx]
9216 dunkaist 149
        invoke  sha2_256_update, ebx, [_data], [_length]
6469 hidnplayr 150
        lea     ebx, [inner_ctx]
9216 dunkaist 151
        invoke  sha2_256_finish, ebx
6469 hidnplayr 152
 
153
        DEBUGF  1, "Inner Hash: "
9216 dunkaist 154
        lea     esi, [inner_ctx]
155
        stdcall dump_hex, esi, SHA2_256_LEN/4
6469 hidnplayr 156
 
157
; Calculate outer hash
158
        lea     ebx, [outer_ctx]
9216 dunkaist 159
        lea     esi, [inner_ctx]
160
        invoke  sha2_256_update, ebx, esi, SHA2_256_LEN
6469 hidnplayr 161
        lea     ebx, [outer_ctx]
9216 dunkaist 162
        invoke  sha2_256_finish, ebx
6469 hidnplayr 163
; Copy output hash to ctx structure     ; FIXME
9216 dunkaist 164
        lea     esi, [outer_ctx]
6469 hidnplayr 165
        mov     edi, [ctx]
9216 dunkaist 166
repeat SHA2_256_LEN/4
6469 hidnplayr 167
        movsd
168
end repeat
169
 
170
        popa
171
        ret
172
 
173
endp