File: sha256.nsh

package info (click to toggle)
win32-loader 0.10.6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 1,868 kB
  • sloc: ansic: 2,492; sh: 706; asm: 344; makefile: 338
file content (290 lines) | stat: -rw-r--r-- 7,544 bytes parent folder | download | duplicates (2)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
; Licensed under the zlib/libpng license (same as NSIS)

!ifndef SHA256_INCLUDED
!define SHA256_INCLUDED

!define SHA256_BLOCK_LEN 64
!define SHA256_LENGTH_SIZE 8
!define SHA256_HELPER_DLL sha256.dll
!searchreplace SHA256_NAME ${SHA256_HELPER_DLL} ".dll" ""
!ifndef SHA256_HELPER_DIR
  !error "Please define the directory containing ${SHA256_HELPER_DLL}"
!endif

!define SHA256_PREFIX "SHA256_"
!define SHA256_UNFUNC "un."

!include include\stdmacros.nsh
!include LogicLib.nsh

!define SHA256_FUNCDEF \
  `!insertmacro STDMACROS_FUNCDEF ${SHA256_PREFIX}`
!define SHA256_FUNCINC \
  `!insertmacro STDMACROS_FUNCINC ${SHA256_PREFIX}`
!define SHA256_FUNCPROLOG \
  `!insertmacro STDMACROS_FUNCPROLOG ${SHA256_PREFIX}`

; ${SHA256_Int32ArrayAsHexString} array size string
${SHA256_FUNCDEF} Int32ArrayAsHexString
!macro SHA256_Int32ArrayAsHexString_Call UN ARRAY SIZE STRING
  Push ${ARRAY}
  Push ${SIZE}
  Call `${UN}${SHA256_PREFIX}Int32ArrayAsHexString`
  Pop ${STRING}
!macroend

; ${SHA256_Alloc} handle
${SHA256_FUNCDEF} Alloc
!macro SHA256_Alloc_Call UN HANDLE
  Call `${UN}${SHA256_PREFIX}Alloc`
  Pop ${HANDLE}
!macroend

; ${SHA256_Init} handle
; Parameter:
;   module - handle of loaded module
; Initialize the digest computation
; Return value:
;   Handle to new hash object
!macro SHA256_Init UN
  !define ${UN}SHA256_Init_INCLUDED
!macroend
${SHA256_FUNCDEF} Init
!macro SHA256_Init_Call UN MODULE HANDLE
  !insertmacro STDMACROS_ToSystemReg ${HANDLE} ${__MACRO__}_HANDLE
  System::Call "${SHA256_NAME}::init() p.${${__MACRO__}_HANDLE}"
!macroend

; ${SHA256_Update} handle data len result
; Update digest computation with the provided data
; Parameters:
;   handle - hash object
;   data - byte array of data
;   len - size of data byte array
; Return value:
;   Non zero value if successful
!macro SHA256_Update UN
  !define ${UN}SHA256_Update_INCLUDED
!macroend
${SHA256_FUNCDEF} Update
!macro SHA256_Update_Call UN HANDLE DATA LEN RESULT
  !insertmacro STDMACROS_ToSystemReg ${HANDLE} ${__MACRO__}_HANDLE
  !insertmacro STDMACROS_ToSystemReg ${DATA} ${__MACRO__}_DATA
  !insertmacro STDMACROS_ToSystemReg ${LEN} ${__MACRO__}_LEN
  !insertmacro STDMACROS_ToSystemReg ${RESULT} ${__MACRO__}_RESULT
  System::Call "${SHA256_NAME}::update(p ${${__MACRO__}_HANDLE}, p ${${__MACRO__}_DATA}, i ${${__MACRO__}_LEN}) i.${${__MACRO__}_RESULT}"
!macroend

; ${SHA256_Update_GetFunctionAddress} address
; Get address of update function
; Return value:
;   address of update function
!macro SHA256_Update_GetFunctionAddress UN
  !define ${UN}SHA256_Update_GetFunctionAddress_INCLUDED
!macroend
${SHA256_FUNCDEF} Update_GetFunctionAddress
!macro SHA256_Update_GetFunctionAddress_Call UN ADDRESS
  System::Get "${SHA256_NAME}::update"
  Pop ${ADDRESS}
!macroend

; ${SHA256_Final} module handle result
${SHA256_FUNCDEF} Final
!macro SHA256_Final_Call UN HANDLE RESULT
  Push ${HANDLE}
  Call `${UN}${SHA256_PREFIX}Final`
  Pop ${RESULT}
!macroend

; ${SHA256_Destroy} module handle
; Parameter:
;   handle - hash object
; Return value:
;  0 if successful otherwise error code
!macro SHA256_Destroy UN
  !define ${UN}SHA256_Destroy_INCLUDED
!macroend
${SHA256_FUNCDEF} Destroy
!macro SHA256_Destroy_Call UN HANDLE RESULT
  !insertmacro STDMACROS_ToSystemReg ${HANDLE} ${__MACRO__}_HANDLE
  !insertmacro STDMACROS_ToSystemReg ${RESULT} ${__MACRO__}_RESULT
  System::Call "${SHA256_NAME}::destroy(p ${${__MACRO__}_HANDLE}) i.${${__MACRO__}_RESULT}?e"
  ${If} ${RESULT} != 0
    StrCpy ${RESULT} 0
    Exch ${RESULT}
  ${EndIf}
  Pop ${RESULT}
!macroend

; ${SHA256_Destroy_GetFunctionAddress} address
; Get address of destroy function
; Return value:
;   address of destroy function
!macro SHA256_Destroy_GetFunctionAddress UN
  !define ${UN}SHA256_Destroy_GetFunctionAddress_INCLUDED
!macroend
${SHA256_FUNCDEF} Destroy_GetFunctionAddress
!macro SHA256_Destroy_GetFunctionAddress_Call UN ADDRESS
  System::Get "${SHA256_NAME}::destroy"
  Pop ${ADDRESS}
!macroend

; ${SHA256_Hash} module filehandle result
${SHA256_FUNCDEF} Hash
!macro SHA256_Hash_Call UN MODULE FILEHANDLE RESULT
  Push ${MODULE}
  Push ${FILEHANDLE}
  Call `${UN}${SHA256_PREFIX}Hash`
  Pop ${RESULT}
!macroend

; ${SHA256_Free} module
; Release the loaded sha256 helper module context
; Parameter:
;   module - handle to the loaded module
!macro SHA256_Free UN
  !define ${UN}SHA256_Free_INCLUDED
!macroend
${SHA256_FUNCDEF} Free
!macro SHA256_Free_Call UN MODULE
  !insertmacro STDMACROS_ToSystemReg ${MODULE} ${__MACRO__}_MODULE
  System::Call "kernel32::FreeLibrary(p ${${__MACRO__}_MODULE})"
!macroend

!macro SHA256_Int32ArrayAsHexString UN
; Get hex string from an array of 32-bit integers
; Parameters:
;   array - array holding the 32-bit integers
;   size - size of arry in bytes
; Return value:
;   Hex string
${SHA256_FUNCPROLOG} "${UN}" Int32ArrayAsHexString
  Push $0
  Exch 2
  Exch $1
  Exch
  Exch $2
  Push $3
  Push $4

  ; array=$1, size=$2
  StrCpy $0 ""
  StrCpy $3 $1
  ${While} $2 > 3
    System::Call "*$3(&i4 .r4)"
    IntFmt $4 "%08x" $4
    StrCpy $0 "$0$4"
    IntOp $3 $3 + 4
    IntOp $2 $2 - 4
  ${EndWhile}

  Pop $4
  Pop $3
  Pop $2
  Pop $1
  Exch $0
FunctionEnd
!macroend ; SHA256_Int32ArrayAsHexString

!macro SHA256_Alloc UN
; Unpack and load sha256 helper dll
; Result:
;   Handle of loaded module
${SHA256_FUNCPROLOG} "${UN}" Alloc
  Push $0
  Push $1

  StrCpy $1 "$PLUGINSDIR\${SHA256_HELPER_DLL}"
  ${IfNot} ${FileExists} "$1"
    ; Unpack sha256 helper dll
    Push $OUTDIR
    SetOutPath $PLUGINSDIR
    ReserveFile "${SHA256_HELPER_DIR}\${SHA256_HELPER_DLL}"
    File "/oname=${SHA256_HELPER_DLL}" "${SHA256_HELPER_DIR}\${SHA256_HELPER_DLL}"
    Pop $0
    SetOutPath $0
  ${EndIf}
  System::Call "kernel32::LoadLibrary(t r1) p.r0 ?e"
  Pop $1

  Pop $1
  Exch $0
FunctionEnd
!macroend ; SHA256_Alloc

!macro SHA256_Final UN
${SHA256_FUNCINC} "${UN}" Int32ArrayAsHexString
; Complete hashing and get digest value
; Parameters:
;  handle  - hash object
; Return value:
;   Hex string of digest value or empty string in case of failure
${SHA256_FUNCPROLOG} "${UN}" Final
  Push $0
  Exch
  Exch $1
  Push $2
  Push $3

  StrCpy $0 ""
  ${If} $1 P<> 0
    ; Allocate 32-bit integer array
    System::Call "*(&l .r2, &i4,&i4,&i4,&i4,&i4,&i4,&i4,&i4) p.r3"
    ${If} $3 P<> 0
      System::Call "${SHA256_NAME}::final(p r1, p r3)"
      ; Get hash value
      !insertmacro ${SHA256_PREFIX}Int32ArrayAsHexString_Call "${UN}" $3 $2 $0
      System::Free $3
    ${EndIf}
  ${EndIf}

  Pop $3
  Pop $2
  Pop $1
  Exch $0
FunctionEnd
!macroend ; SHA256_Final

!macro SHA256_Hash UN
${SHA256_FUNCINC} "${UN}" Int32ArrayAsHexString
; Compute SHA256 hash value
; Parameters:
;   module - handle of loaded module
;   filehandle - handle of file to read from
; Return value:
;   Hex string of digest value or empty string in case of failure
${SHA256_FUNCPROLOG} "${UN}" Hash
  Push $0
  Exch 2
  Exch $1
  Exch
  Exch $2
  Push $3
  Push $4

  ; module=$1, filehandle=$2
  StrCpy $0 ""
  ${If} $1 P<> 0
  ${AndIf} $2 P<> 0
    System::Call "kernel32::GetProcAddress(p r1, m 'hash') p.r1"
    ${If} $1 P<> 0
      ; Allocate 32-bit integer array
      System::Call "*(&l .r3, &i4,&i4,&i4,&i4,&i4,&i4,&i4,&i4) p.r4"
      ${If} $4 P<> 0
        System::Call "::$1(p r2, p r4)"
        ; Get hash value
        !insertmacro ${SHA256_PREFIX}Int32ArrayAsHexString_Call "${UN}" $4 $3 $0
	System::Free $4
      ${EndIf}
    ${EndIf}
  ${EndIf}

  Pop $4
  Pop $3
  Pop $2
  Pop $1
  Exch $0
FunctionEnd
!macroend ; SHA256_Hash

!endif ; SHA256_INCLUDED