Subversion Repositories Kolibri OS

Rev

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

  1. ; DES routines (from http://board.flatassembler.net/topic.php?t=5575)
  2. ; Copyright (C) 2006, Tomasz Grysztar
  3.  
  4. macro perm_it ebx, edx, shift, mask
  5. {
  6.         mov     eax, ebx
  7.         shr     eax, shift
  8.         xor     eax, edx
  9.         and     eax, mask
  10.         xor     edx, eax
  11.         shl     eax, shift
  12.         xor     ebx, eax
  13. }
  14.  
  15. encrypt_DES:
  16. ; esi - DES key (8 bytes)
  17. ; edx:ebx = data
  18. ; return: edx:ebx = encrypted data
  19.         bswap   ebx
  20.         bswap   edx
  21.  
  22.         perm_it ebx, edx, 4, 0F0F0F0Fh
  23.         perm_it ebx, edx, 16, 0000FFFFh
  24.         perm_it edx, ebx, 2, 33333333h
  25.         perm_it edx, ebx, 8, 00FF00FFh
  26.         perm_it ebx, edx, 1, 55555555h
  27.  
  28.         rol     ebx, 1
  29.         rol     edx, 1
  30.         mov     esi, keys
  31.         call    DES_inner_encrypt
  32.         ror     ebx, 1
  33.         ror     edx, 1
  34.  
  35.         perm_it ebx, edx, 1, 55555555h
  36.         perm_it edx, ebx, 8, 00FF00FFh
  37.         perm_it edx, ebx, 2, 33333333h
  38.         perm_it ebx, edx, 16, 0000FFFFh
  39.         perm_it ebx, edx, 4, 0F0F0F0Fh
  40.  
  41.         bswap   ebx
  42.         bswap   edx
  43.         ret
  44.  
  45. DES_create_keys:
  46. ; edi - ptr to keys (32*2 dwords)
  47. ; edx:ebx = password
  48.  
  49.         perm_it ebx, edx, 4, 0F0F0F0Fh
  50.         perm_it edx, ebx, 16, 0000FFFFh
  51.         perm_it ebx, edx, 2, 33333333h
  52.         perm_it edx, ebx, 16, 0000FFFFh
  53.         perm_it ebx, edx, 1, 55555555h
  54.         perm_it edx, ebx, 8, 00FF00FFh
  55.         perm_it ebx, edx, 1, 55555555h
  56.  
  57.         mov     eax, ebx
  58.         shl     eax, 8
  59.         mov     ebx, edx
  60.         bswap   ebx
  61.         and     ebx, 0FFFFFFF0h
  62.         xchg    edx, eax
  63.         ror     eax, 20
  64.         and     eax, 0xF0
  65.         or      edx, eax
  66.  
  67.         xor     ecx, ecx
  68.     create_keys:
  69.         test    byte [shifts+ecx], 0ffh
  70.         jz      no_shift
  71.         mov     eax, ebx
  72.         shl     eax, 2
  73.         shr     ebx, 26
  74.         or      ebx, eax
  75.         mov     eax, edx
  76.         shl     eax, 2
  77.         shr     edx, 26
  78.         or      edx, eax
  79.         jmp     shift_ok
  80.     no_shift:
  81.         mov     eax, ebx
  82.         shl     eax, 1
  83.         shr     ebx, 27
  84.         or      ebx, eax
  85.         mov     eax, edx
  86.         shl     eax, 1
  87.         shr     edx, 27
  88.         or      edx, eax
  89.     shift_ok:
  90.         and     ebx, 0FFFFFFF0h
  91.         and     edx, 0FFFFFFF0h
  92.         mov     eax, ebx
  93.         shr     eax, 28
  94.         mov     ebp, [pc2_0+eax*4]
  95.         mov     eax, ebx
  96.         shr     eax, 24
  97.         and     eax, 0Fh
  98.         or      ebp, [pc2_1+eax*4]
  99.         mov     eax, ebx
  100.         shr     eax, 20
  101.         and     eax, 0Fh
  102.         or      ebp, [pc2_2+eax*4]
  103.         mov     eax, ebx
  104.         shr     eax, 16
  105.         and     eax, 0Fh
  106.         or      ebp, [pc2_3+eax*4]
  107.         mov     eax, ebx
  108.         shr     eax, 12
  109.         and     eax, 0Fh
  110.         or      ebp, [pc2_4+eax*4]
  111.         mov     eax, ebx
  112.         shr     eax, 8
  113.         and     eax, 0Fh
  114.         or      ebp, [pc2_5+eax*4]
  115.         mov     eax, ebx
  116.         shr     eax, 4
  117.         and     eax, 0Fh
  118.         or      ebp, [pc2_6+eax*4]
  119.         mov     [edi], ebp
  120.         mov     eax, edx
  121.         shr     eax, 28
  122.         and     eax, 0Fh
  123.         mov     ebp, [pc2_7+eax*4]
  124.         mov     eax, edx
  125.         shr     eax, 24
  126.         and     eax, 0Fh
  127.         or      ebp, [pc2_8+eax*4]
  128.         mov     eax, edx
  129.         shr     eax, 20
  130.         and     eax, 0Fh
  131.         or      ebp, [pc2_9+eax*4]
  132.         mov     eax, edx
  133.         shr     eax, 16
  134.         and     eax, 0Fh
  135.         or      ebp, [pc2_10+eax*4]
  136.         mov     eax, edx
  137.         shr     eax, 12
  138.         and     eax, 0Fh
  139.         or      ebp, [pc2_11+eax*4]
  140.         mov     eax, edx
  141.         shr     eax, 8
  142.         and     eax, 0Fh
  143.         or      ebp, [pc2_12+eax*4]
  144.         mov     eax, edx
  145.         shr     eax, 4
  146.         and     eax, 0Fh
  147.         or      ebp, [pc2_13+eax*4]
  148.         mov     eax, ebp
  149.         shr     eax, 16
  150.         xor     eax, [edi]
  151.         and     eax, 0FFFFh
  152.         xor     [edi], eax
  153.         shl     eax, 16
  154.         xor     ebp, eax
  155.         mov     [edi+4], ebp
  156.         add     edi, 8
  157.         inc     ecx
  158.         cmp     ecx, 16
  159.         jb      create_keys
  160.         ret
  161.  
  162. DES_inner_encrypt:
  163.         xor     ecx, ecx
  164.     .encrypt:
  165.         mov     edi, edx
  166.         ror     edi, 4
  167.         xor     edi, [esi+(ecx+1)*4]
  168.         push    esi
  169.         mov     esi, [esi+ecx*4]
  170.         xor     esi, edx
  171.         mov     ebp, ebx
  172.         mov     ebx, edx
  173.         mov     eax, esi
  174.         shr     eax, 24
  175.         and     eax, 3Fh
  176.         mov     edx, [sbox_2+eax*4]
  177.         mov     eax, esi
  178.         shr     eax, 16
  179.         and     eax, 3Fh
  180.         or      edx, [sbox_4+eax*4]
  181.         mov     eax, esi
  182.         shr     eax, 8
  183.         and     eax, 3Fh
  184.         or      edx, [sbox_6+eax*4]
  185.         mov     eax, esi
  186.         and     eax, 3Fh
  187.         or      edx, [sbox_8+eax*4]
  188.         pop     esi
  189.         mov     eax, edi
  190.         shr     eax, 24
  191.         and     eax, 3Fh
  192.         or      edx, [sbox_1+eax*4]
  193.         mov     eax, edi
  194.         shr     eax, 16
  195.         and     eax, 3Fh
  196.         or      edx, [sbox_3+eax*4]
  197.         mov     eax, edi
  198.         shr     eax, 8
  199.         and     eax, 3Fh
  200.         or      edx, [sbox_5+eax*4]
  201.         mov     eax, edi
  202.         and     eax, 3Fh
  203.         or      edx, [sbox_7+eax*4]
  204.         xor     edx, ebp
  205.         add     ecx, 2
  206.         cmp     ecx, 32
  207.         jb      .encrypt
  208.         xchg    ebx, edx
  209.         ret
  210.  
  211. shifts  db 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0
  212.  
  213. pc2_0   dd 0, 0x4, 0x20000000, 0x20000004, 0x10000, 0x10004, 0x20010000, 0x20010004, 0x200, 0x204, 0x20000200, 0x20000204, 0x10200, 0x10204, 0x20010200, 0x20010204
  214. pc2_1   dd 0, 0x1, 0x100000, 0x100001, 0x4000000, 0x4000001, 0x4100000, 0x4100001, 0x100, 0x101, 0x100100, 0x100101, 0x4000100, 0x4000101, 0x4100100, 0x4100101
  215. pc2_2   dd 0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808, 0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808
  216. pc2_3   dd 0, 0x200000, 0x8000000, 0x8200000, 0x2000, 0x202000, 0x8002000, 0x8202000, 0x20000, 0x220000, 0x8020000, 0x8220000, 0x22000, 0x222000, 0x8022000, 0x8222000
  217. pc2_4   dd 0, 0x40000, 0x10, 0x40010, 0, 0x40000, 0x10, 0x40010, 0x1000, 0x41000, 0x1010, 0x41010, 0x1000, 0x41000, 0x1010, 0x41010
  218. pc2_5   dd 0, 0x400, 0x20, 0x420, 0, 0x400, 0x20, 0x420, 0x2000000, 0x2000400, 0x2000020, 0x2000420, 0x2000000, 0x2000400, 0x2000020, 0x2000420
  219. pc2_6   dd 0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002, 0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002
  220. pc2_7   dd 0, 0x10000, 0x800, 0x10800, 0x20000000, 0x20010000, 0x20000800, 0x20010800, 0x20000, 0x30000, 0x20800, 0x30800, 0x20020000, 0x20030000, 0x20020800, 0x20030800
  221. pc2_8   dd 0, 0x40000, 0, 0x40000, 0x2, 0x40002, 0x2, 0x40002, 0x2000000, 0x2040000, 0x2000000, 0x2040000, 0x2000002, 0x2040002, 0x2000002, 0x2040002
  222. pc2_9   dd 0, 0x10000000, 0x8, 0x10000008, 0, 0x10000000, 0x8, 0x10000008, 0x400, 0x10000400, 0x408, 0x10000408, 0x400, 0x10000400, 0x408, 0x10000408
  223. pc2_10  dd 0, 0x20, 0, 0x20, 0x100000, 0x100020, 0x100000, 0x100020, 0x2000, 0x2020, 0x2000, 0x2020, 0x102000, 0x102020, 0x102000, 0x102020
  224. pc2_11  dd 0, 0x1000000, 0x200, 0x1000200, 0x200000, 0x1200000, 0x200200, 0x1200200, 0x4000000, 0x5000000, 0x4000200, 0x5000200, 0x4200000, 0x5200000, 0x4200200, 0x5200200
  225. pc2_12  dd 0, 0x1000, 0x8000000, 0x8001000, 0x80000, 0x81000, 0x8080000, 0x8081000, 0x10, 0x1010, 0x8000010, 0x8001010, 0x80010, 0x81010, 0x8080010, 0x8081010
  226. pc2_13  dd 0, 0x4, 0x100, 0x104, 0, 0x4, 0x100, 0x104, 0x1, 0x5, 0x101, 0x105, 0x1, 0x5, 0x101, 0x105
  227.  
  228. sbox_1  dd 0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404, 0x4, 0x10000, 0x400, 0x1010400, 0x1010404, 0x400, 0x1000404, 0x1010004, 0x1000000, 0x4, 0x404, 0x1000400, 0x1000400, 0x10400, 0x10400, 0x1010000, 0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 0x404, 0x10404, 0x1000000, 0x10000, 0x1010404, 0x4, 0x1010000, 0x1010400, 0x1000000, 0x1000000, 0x400, 0x1010004, 0x10000, 0x10400, 0x1000004, 0x400, 0x4, 0x1000404, 0x10404, 0x1010404, 0x10004, 0x1010000, 0x1000404, 0x1000004, 0x404, 0x10404, 0x1010400, 0x404, 0x1000400, 0x1000400, 0, 0x10004, 0x10400, 0, 0x1010004
  229. sbox_2  dd 0x80108020, 0x80008000, 0x8000, 0x108020, 0x100000, 0x20, 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x100000, 0x20, 0x80100020, 0x108000, 0x100020, 0x80008020, 0, 0x80000000, 0x8000, 0x108020, 0x80100000, 0x100020, 0x80000020, 0, 0x108000, 0x8020, 0x80108000, 0x80100000, 0x8020, 0, 0x108020, 0x80100020, 0x100000, 0x80008020, 0x80100000, 0x80108000, 0x8000, 0x80100000, 0x80008000, 0x20, 0x80108020, 0x108020, 0x20, 0x8000, 0x80000000, 0x8020, 0x80108000, 0x100000, 0x80000020, 0x100020, 0x80008020, 0x80000020, 0x100020, 0x108000, 0, 0x80008000, 0x8020, 0x80000000, 0x80100020, 0x80108020, 0x108000
  230. sbox_3  dd 0x208, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200, 0x20008, 0x8000008, 0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 0x208, 0x8000000, 0x8, 0x8020200, 0x200, 0x20200, 0x8020000, 0x8020008, 0x20208, 0x8000208, 0x20200, 0x20000, 0x8000208, 0x8, 0x8020208, 0x200, 0x8000000, 0x8020200, 0x8000000, 0x20008, 0x208, 0x20000, 0x8020200, 0x8000200, 0, 0x200, 0x20008, 0x8020208, 0x8000200, 0x8000008, 0x200, 0, 0x8020008, 0x8000208, 0x20000, 0x8000000, 0x8020208, 0x8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208, 0x208, 0x8020000, 0x20208, 0x8, 0x8020008, 0x20200
  231. sbox_4  dd 0x802001, 0x2081, 0x2081, 0x80, 0x802080, 0x800081, 0x800001, 0x2001, 0, 0x802000, 0x802000, 0x802081, 0x81, 0, 0x800080, 0x800001, 0x1, 0x2000, 0x800000, 0x802001, 0x80, 0x800000, 0x2001, 0x2080, 0x800081, 0x1, 0x2080, 0x800080, 0x2000, 0x802080, 0x802081, 0x81, 0x800080, 0x800001, 0x802000, 0x802081, 0x81, 0, 0, 0x802000, 0x2080, 0x800080, 0x800081, 0x1, 0x802001, 0x2081, 0x2081, 0x80, 0x802081, 0x81, 0x1, 0x2000, 0x800001, 0x2001, 0x802080, 0x800081, 0x2001, 0x2080, 0x800000, 0x802001, 0x80, 0x800000, 0x2000, 0x802080
  232. sbox_5  dd 0x100, 0x2080100, 0x2080000, 0x42000100, 0x80000, 0x100, 0x40000000, 0x2080000, 0x40080100, 0x80000, 0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100, 0x40000000, 0x2000000, 0x40080000, 0x40080000, 0, 0x40000100, 0x42080100, 0x42080100, 0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100, 0x2000000, 0x42000000, 0x80100, 0x80000, 0x42000100, 0x100, 0x2000000, 0x40000000, 0x2080000, 0x42000100, 0x40080100, 0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 0x100, 0x2000000, 0x42080000, 0x42080100, 0x80100, 0x42000000, 0x42080100, 0x2080000, 0, 0x40080000, 0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000, 0, 0x40080000, 0x2080100, 0x40000100
  233. sbox_6  dd 0x20000010, 0x20400000, 0x4000, 0x20404010, 0x20400000, 0x10, 0x20404010, 0x400000, 0x20004000, 0x404010, 0x400000, 0x20000010, 0x400010, 0x20004000, 0x20000000, 0x4010, 0, 0x400010, 0x20004010, 0x4000, 0x404000, 0x20004010, 0x10, 0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 0x4010, 0x404000, 0x20404000, 0x20000000, 0x20004000, 0x10, 0x20400010, 0x404000, 0x20404010, 0x400000, 0x4010, 0x20000010, 0x400000, 0x20004000, 0x20000000, 0x4010, 0x20000010, 0x20404010, 0x404000, 0x20400000, 0x404010, 0x20404000, 0, 0x20400010, 0x10, 0x4000, 0x20400000, 0x404010, 0x4000, 0x400010, 0x20004010, 0, 0x20404000, 0x20000000, 0x400010, 0x20004010
  234. sbox_7  dd 0x200000, 0x4200002, 0x4000802, 0, 0x800, 0x4000802, 0x200802, 0x4200800, 0x4200802, 0x200000, 0, 0x4000002, 0x2, 0x4000000, 0x4200002, 0x802, 0x4000800, 0x200802, 0x200002, 0x4000800, 0x4000002, 0x4200000, 0x4200800, 0x200002, 0x4200000, 0x800, 0x802, 0x4200802, 0x200800, 0x2, 0x4000000, 0x200800, 0x4000000, 0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 0x2, 0x200002, 0x4000000, 0x4000800, 0x200000, 0x4200800, 0x802, 0x200802, 0x4200800, 0x802, 0x4000002, 0x4200802, 0x4200000, 0x200800, 0, 0x2, 0x4200802, 0, 0x200802, 0x4200000, 0x800, 0x4000002, 0x4000800, 0x800, 0x200002
  235. sbox_8  dd 0x10001040, 0x1000, 0x40000, 0x10041040, 0x10000000, 0x10001040, 0x40, 0x10000000, 0x40040, 0x10040000, 0x10041040, 0x41000, 0x10041000, 0x41040, 0x1000, 0x40, 0x10040000, 0x10000040, 0x10001000, 0x1040, 0x41000, 0x40040, 0x10040040, 0x10041000, 0x1040, 0, 0, 0x10040040, 0x10000040, 0x10001000, 0x41040, 0x40000, 0x41040, 0x40000, 0x10041000, 0x1000, 0x40, 0x10040040, 0x1000, 0x41040, 0x10001000, 0x40, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040, 0x40040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0, 0x10041040, 0x41000, 0x41000, 0x1040, 0x1040, 0x40040, 0x10000000, 0x10041000