Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ; libcrash -- cryptographic hash (and other) functions
  2. ;
  3. ; Copyright (C) <2021> Ivan Baravy
  4. ;
  5. ; SPDX-License-Identifier: GPL-2.0-or-later
  6. ;
  7. ; This program is free software: you can redistribute it and/or modify it under
  8. ; the terms of the GNU General Public License as published by the Free Software
  9. ; Foundation, either version 2 of the License, or (at your option) any later
  10. ; version.
  11. ;
  12. ; This program is distributed in the hope that it will be useful, but WITHOUT
  13. ; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. ; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. ;
  16. ; You should have received a copy of the GNU General Public License along with
  17. ; this program. If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. macro max target, [source] {
  20.   common
  21.     target = 0
  22.   forward
  23.     if target < source
  24.       target = source
  25.     end if
  26. }
  27.  
  28. max MAX_HMAC_HASH_CTX_SIZE, sizeof.ctx_sha2_224256, sizeof.ctx_sha2_384512
  29. max MAX_HMAC_HASH_BLOCK_SIZE, SHA2_224256_BLOCK_SIZE, SHA2_384512_BLOCK_SIZE
  30. max MAX_HMAC_HASH_LEN, SHA2_256_LEN, SHA2_512_LEN
  31.  
  32. struct ctx_hmac
  33.         ctx_hash        rb MAX_HMAC_HASH_CTX_SIZE
  34.         key_pad         rb MAX_HMAC_HASH_BLOCK_SIZE
  35.         mac             rd MAX_HMAC_HASH_LEN/4
  36.         hash_init       dd ?
  37.         hash_update     dd ?
  38.         hash_finish     dd ?
  39.         hash_oneshot    dd ?
  40.         block_size      dd ?
  41.         hash_size       dd ?
  42. ends
  43.  
  44. assert sizeof.ctx_hmac <= LIBCRASH_CTX_LEN
  45.  
  46. ; ebx = _ctx
  47. proc hmac._.init uses ebx esi edi, _key, _key_len
  48.         mov     ecx, [_key_len]
  49.         cmp     ecx, [ebx+ctx_hmac.block_size]
  50.         mov     esi, [_key]
  51.         jbe     .pad
  52.         ; hash
  53.         lea     esi, [ebx+ctx_hmac.ctx_hash]
  54.         stdcall [ebx+ctx_hmac.hash_oneshot], esi, [_key], [_key_len]
  55.         mov     ecx, [ebx+ctx_hmac.hash_size]
  56. .pad:
  57.         lea     edi, [ebx+ctx_hmac.key_pad]
  58.         mov     edx, [ebx+ctx_hmac.block_size]
  59.         sub     edx, ecx
  60.         xor     eax, eax
  61.         rep movsb
  62.         mov     ecx, edx
  63.         rep stosb
  64.  
  65.         ; xor with 0x36
  66.         mov     eax, 0x36363636
  67.         lea     edx, [ebx+ctx_hmac.key_pad]
  68.         mov     ecx, [ebx+ctx_hmac.block_size]
  69.         shr     ecx, 2
  70. @@:
  71.         xor     [edx], eax
  72.         add     edx, 4
  73.         dec     ecx
  74.         jnz     @b
  75.  
  76.         lea     esi, [ebx+ctx_hmac.ctx_hash]
  77.         stdcall [ebx+ctx_hmac.hash_init], esi
  78.         lea     eax, [ebx+ctx_hmac.key_pad]
  79.         stdcall [ebx+ctx_hmac.hash_update], esi, eax, [ebx+ctx_hmac.block_size]
  80.  
  81.         ; xor with 0x36 xor 0x5c
  82.         mov     eax, 0x36363636 XOR 0x5c5c5c5c
  83.         lea     edx, [ebx+ctx_hmac.key_pad]
  84.         mov     ecx, [ebx+ctx_hmac.block_size]
  85.         shr     ecx, 2
  86. @@:
  87.         xor     [edx], eax
  88.         add     edx, 4
  89.         dec     ecx
  90.         jnz     @b
  91.  
  92.         ret
  93. endp
  94.  
  95. proc hmac_sha2_256.init uses ebx, _ctx, _key, _key_len
  96.         mov     ebx, [_ctx]
  97.         mov     [ebx+ctx_hmac.hash_init], sha2_256.init
  98.         mov     [ebx+ctx_hmac.hash_update], sha2_256.update
  99.         mov     [ebx+ctx_hmac.hash_finish], sha2_256.finish
  100.         mov     [ebx+ctx_hmac.hash_oneshot], sha2_256.oneshot
  101.         mov     [ebx+ctx_hmac.block_size], SHA2_256_BLOCK_SIZE
  102.         mov     [ebx+ctx_hmac.hash_size], SHA2_256_LEN
  103.         stdcall hmac._.init, [_key], [_key_len]
  104.         ret
  105. endp
  106.  
  107. proc hmac_sha2_512.init uses ebx, _ctx, _key, _key_len
  108.         mov     ebx, [_ctx]
  109.         mov     [ebx+ctx_hmac.hash_init], sha2_512.init
  110.         mov     [ebx+ctx_hmac.hash_update], sha2_512.update
  111.         mov     [ebx+ctx_hmac.hash_finish], sha2_512.finish
  112.         mov     [ebx+ctx_hmac.hash_oneshot], sha2_512.oneshot
  113.         mov     [ebx+ctx_hmac.block_size], SHA2_512_BLOCK_SIZE
  114.         mov     [ebx+ctx_hmac.hash_size], SHA2_512_LEN
  115.         stdcall hmac._.init, [_key], [_key_len]
  116.         ret
  117. endp
  118.  
  119.  
  120. hmac_sha2_256.update = hmac._.update
  121. hmac_sha2_512.update = hmac._.update
  122. proc hmac._.update uses ebx esi edi, _ctx, _in, _len
  123.         mov     ebx, [_ctx]
  124.         lea     eax, [ebx+ctx_hmac.ctx_hash]
  125.         stdcall [ebx+ctx_hmac.hash_update], eax, [_in], [_len]
  126. .quit:
  127.         ret
  128. endp
  129.  
  130. hmac_sha2_256.finish = hmac._.finish
  131. hmac_sha2_512.finish = hmac._.finish
  132. proc hmac._.finish uses ebx esi edi, _ctx
  133.         mov     ebx, [_ctx]
  134.         lea     esi, [ebx+ctx_hmac.ctx_hash]
  135.         stdcall [ebx+ctx_hmac.hash_finish], esi
  136.         lea     edi, [ebx+ctx_hmac.mac]
  137.         mov     ecx, [ebx+ctx_hmac.hash_size]
  138.         rep movsb
  139.         lea     esi, [ebx+ctx_hmac.ctx_hash]
  140.         stdcall [ebx+ctx_hmac.hash_init], esi
  141.         lea     eax, [ebx+ctx_hmac.key_pad]
  142.         stdcall [ebx+ctx_hmac.hash_update], esi, eax, [ebx+ctx_hmac.block_size]
  143.         lea     eax, [ebx+ctx_hmac.mac]
  144.         stdcall [ebx+ctx_hmac.hash_update], esi, eax, [ebx+ctx_hmac.hash_size]
  145.         stdcall [ebx+ctx_hmac.hash_finish], esi
  146.         ret
  147. endp
  148.  
  149. proc hmac_sha2_256.oneshot _ctx, _in, _len, _key, _key_len
  150.         stdcall hmac_sha2_256.init, [_ctx], [_key], [_key_len]
  151.         stdcall hmac_sha2_256.update, [_ctx], [_in], [_len]
  152.         stdcall hmac_sha2_256.finish, [_ctx]
  153.         ret
  154. endp
  155.  
  156. proc hmac_sha2_512.oneshot _ctx, _in, _len, _key, _key_len
  157.         stdcall hmac_sha2_512.init, [_ctx], [_key], [_key_len]
  158.         stdcall hmac_sha2_512.update, [_ctx], [_in], [_len]
  159.         stdcall hmac_sha2_512.finish, [_ctx]
  160.         ret
  161. endp
  162.