jcomeau_ictx

bitcoin SHA256Transform with commented disassembly

Apr 10th, 2013
139
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void SHA256Transform(void* pstate, void* pinput, const void* pinit)
  2. {
  3. SHA256_CTX ctx;
  4. unsigned char data[64];
  5.  
  6. SHA256_Init(&ctx);
  7.  
  8. for (int i = 0; i < 16; i++)
  9. ((uint32_t*)data)[i] = ByteReverse(((uint32_t*)pinput)[i]);
  10.  
  11. for (int i = 0; i < 8; i++)
  12. ctx.h[i] = ((uint32_t*)pinit)[i];
  13.  
  14. SHA256_Update(&ctx, data, sizeof(data));
  15. for (int i = 0; i < 8; i++)
  16. ((uint32_t*)pstate)[i] = ctx.h[i];
  17. }
  18. 080b3770 <_Z15SHA256TransformPvS_PKv>:
  19. 80b3770: 55 push %ebp
  20. 80b3771: 57 push %edi
  21. 80b3772: 56 push %esi
  22. 80b3773: 53 push %ebx
  23. 80b3774: 81 ec ec 00 00 00 sub $0xec,%esp
  24. 80b377a: 8b 84 24 00 01 00 00 mov 0x100(%esp),%eax ;; pstate
  25. 80b3781: 8d 5c 24 2c lea 0x2c(%esp),%ebx ;; ctx
  26. 80b3785: 8b b4 24 04 01 00 00 mov 0x104(%esp),%esi ;; pinput
  27. 80b378c: 8d ac 24 9c 00 00 00 lea 0x9c(%esp),%ebp ;; data
  28. 80b3793: 8b bc 24 08 01 00 00 mov 0x108(%esp),%edi ;; pinit
  29. 80b379a: 89 1c 24 mov %ebx,(%esp) ;; ctx for SHA256_Init call
  30. 80b379d: 89 44 24 1c mov %eax,0x1c(%esp) ;; pstate
  31. ;; initialize "stack canary"
  32. 80b37a1: 65 a1 14 00 00 00 mov %gs:0x14,%eax
  33. 80b37a7: 89 84 24 dc 00 00 00 mov %eax,0xdc(%esp)
  34. 80b37ae: 31 c0 xor %eax,%eax
  35. SHA256_Init(&ctx);
  36. 80b37b0: e8 8b 3a fa ff call 8057240 <SHA256_Init@plt>
  37. for (int i = 0; i < 16; i++)
  38. 80b37b5: 31 d2 xor %edx,%edx ;; i = 0
  39. 80b37b7: 90 nop
  40. 0: ;; start of loop
  41. ((uint32_t*)data)[i] = ByteReverse(((uint32_t*)pinput)[i]);
  42. 80b37b8: 8b 0c 96 mov (%esi,%edx,4),%ecx ;; value from pinput array
  43. ;; begin ByteReverse
  44. ;; let's say the word is 0x11223344
  45. 80b37bb: 89 c8 mov %ecx,%eax
  46. 80b37bd: 81 e1 00 ff 00 ff and $0xff00ff00,%ecx ;; 0x11003300
  47. 80b37c3: 25 ff 00 ff 00 and $0xff00ff,%eax ;; 0x220044
  48. 80b37c8: c1 e0 08 shl $0x8,%eax ;; 0x22004400
  49. 80b37cb: c1 e9 08 shr $0x8,%ecx ;; 0x00110033
  50. 80b37ce: 09 c8 or %ecx,%eax ;; 0x22114433
  51. 80b37d0: c1 c8 10 ror $0x10,%eax ;; 0x44332211
  52. ;; end ByteReverse
  53. ;; FIXED: changed to bswap %edx
  54. 80b37d3: 89 44 95 00 mov %eax,0x0(%ebp,%edx,4) ;; store in data array
  55. 80b37d7: 83 c2 01 add $0x1,%edx
  56. 80b37da: 83 fa 10 cmp $0x10,%edx ;; i < 16 ?
  57. 80b37dd: 75 d9 jne 80b37b8 <_Z15SHA256TransformPvS_PKv+0x48> ;; jne 0b
  58. for (int i = 0; i < 8; i++)
  59. 80b37df: 31 c0 xor %eax,%eax ;; i = 0
  60. 80b37e1: 8d b4 26 00 00 00 00 lea 0x0(%esi,%eiz,1),%esi
  61. ctx.h[i] = ((uint32_t*)pinit)[i];
  62. 1: ;; start of loop
  63. 80b37e8: 8b 14 87 mov (%edi,%eax,4),%edx ;; from pinit array
  64. 80b37eb: 89 14 83 mov %edx,(%ebx,%eax,4) ;; to ctx array
  65. 80b37ee: 83 c0 01 add $0x1,%eax ;; i++
  66. 80b37f1: 83 f8 08 cmp $0x8,%eax ;; i < 8
  67. 80b37f4: 75 f2 jne 80b37e8 <_Z15SHA256TransformPvS_PKv+0x78> ;; jne 1b
  68. SHA256_Update(&ctx, data, sizeof(data));
  69. 80b37f6: c7 44 24 08 40 00 00 movl $0x40,0x8(%esp) ;; sizeof(data)
  70. 80b37fd: 00
  71. 80b37fe: 89 6c 24 04 mov %ebp,0x4(%esp) ;; data
  72. 80b3802: 89 1c 24 mov %ebx,(%esp) ;; ctx
  73. 80b3805: e8 f6 33 fa ff call 8056c00 <SHA256_Update@plt>
  74. for (int i = 0; i < 8; i++)
  75. ((uint32_t*)pstate)[i] = ctx.h[i];
  76. 80b380a: 8b 4c 24 1c mov 0x1c(%esp),%ecx ;; pstate
  77. 80b380e: 31 c0 xor %eax,%eax ;; i = 0
  78. 2: ;; start of loop
  79. 80b3810: 8b 14 83 mov (%ebx,%eax,4),%edx ;; from ctx
  80. 80b3813: 89 14 81 mov %edx,(%ecx,%eax,4) ;; to pstate
  81. 80b3816: 83 c0 01 add $0x1,%eax ;; i++
  82. 80b3819: 83 f8 08 cmp $0x8,%eax ;; i < 8
  83. 80b381c: 75 f2 jne 80b3810 <_Z15SHA256TransformPvS_PKv+0xa0> ;; jne 2b
  84. 80b381e: 8b 84 24 dc 00 00 00 mov 0xdc(%esp),%eax ;; stack canary
  85. 80b3825: 65 33 05 14 00 00 00 xor %gs:0x14,%eax ;; original canary value
  86. 80b382c: 75 0b jne 80b3839 <_Z15SHA256TransformPvS_PKv+0xc9> ;; fail if no match
  87. 80b382e: 81 c4 ec 00 00 00 add $0xec,%esp ;; clean up stack
  88. 80b3834: 5b pop %ebx ;; pop registers and return
  89. 80b3835: 5e pop %esi
  90. 80b3836: 5f pop %edi
  91. 80b3837: 5d pop %ebp
  92. 80b3838: c3 ret
  93. 80b3839: e8 c2 3c fa ff call 8057500 <__stack_chk_fail@plt>
  94. 80b383e: 66 90 xchg %ax,%ax
RAW Paste Data