Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

182 rindas
4.6 KiB

  1. """
  2. Implementation of the SHAKE-256 algorithm for Ed448
  3. """
  4. try:
  5. import hashlib
  6. hashlib.new("shake256").digest(64)
  7. def shake_256(msg, outlen):
  8. return hashlib.new("shake256", msg).digest(outlen)
  9. except (TypeError, ValueError):
  10. from ._compat import bytes_to_int, int_to_bytes
  11. # From little endian.
  12. def _from_le(s):
  13. return bytes_to_int(s, byteorder="little")
  14. # Rotate a word x by b places to the left.
  15. def _rol(x, b):
  16. return ((x << b) | (x >> (64 - b))) & (2**64 - 1)
  17. # Do the SHA-3 state transform on state s.
  18. def _sha3_transform(s):
  19. ROTATIONS = [
  20. 0,
  21. 1,
  22. 62,
  23. 28,
  24. 27,
  25. 36,
  26. 44,
  27. 6,
  28. 55,
  29. 20,
  30. 3,
  31. 10,
  32. 43,
  33. 25,
  34. 39,
  35. 41,
  36. 45,
  37. 15,
  38. 21,
  39. 8,
  40. 18,
  41. 2,
  42. 61,
  43. 56,
  44. 14,
  45. ]
  46. PERMUTATION = [
  47. 1,
  48. 6,
  49. 9,
  50. 22,
  51. 14,
  52. 20,
  53. 2,
  54. 12,
  55. 13,
  56. 19,
  57. 23,
  58. 15,
  59. 4,
  60. 24,
  61. 21,
  62. 8,
  63. 16,
  64. 5,
  65. 3,
  66. 18,
  67. 17,
  68. 11,
  69. 7,
  70. 10,
  71. ]
  72. RC = [
  73. 0x0000000000000001,
  74. 0x0000000000008082,
  75. 0x800000000000808A,
  76. 0x8000000080008000,
  77. 0x000000000000808B,
  78. 0x0000000080000001,
  79. 0x8000000080008081,
  80. 0x8000000000008009,
  81. 0x000000000000008A,
  82. 0x0000000000000088,
  83. 0x0000000080008009,
  84. 0x000000008000000A,
  85. 0x000000008000808B,
  86. 0x800000000000008B,
  87. 0x8000000000008089,
  88. 0x8000000000008003,
  89. 0x8000000000008002,
  90. 0x8000000000000080,
  91. 0x000000000000800A,
  92. 0x800000008000000A,
  93. 0x8000000080008081,
  94. 0x8000000000008080,
  95. 0x0000000080000001,
  96. 0x8000000080008008,
  97. ]
  98. for rnd in range(0, 24):
  99. # AddColumnParity (Theta)
  100. c = [0] * 5
  101. d = [0] * 5
  102. for i in range(0, 25):
  103. c[i % 5] ^= s[i]
  104. for i in range(0, 5):
  105. d[i] = c[(i + 4) % 5] ^ _rol(c[(i + 1) % 5], 1)
  106. for i in range(0, 25):
  107. s[i] ^= d[i % 5]
  108. # RotateWords (Rho)
  109. for i in range(0, 25):
  110. s[i] = _rol(s[i], ROTATIONS[i])
  111. # PermuteWords (Pi)
  112. t = s[PERMUTATION[0]]
  113. for i in range(0, len(PERMUTATION) - 1):
  114. s[PERMUTATION[i]] = s[PERMUTATION[i + 1]]
  115. s[PERMUTATION[-1]] = t
  116. # NonlinearMixRows (Chi)
  117. for i in range(0, 25, 5):
  118. t = [
  119. s[i],
  120. s[i + 1],
  121. s[i + 2],
  122. s[i + 3],
  123. s[i + 4],
  124. s[i],
  125. s[i + 1],
  126. ]
  127. for j in range(0, 5):
  128. s[i + j] = t[j] ^ ((~t[j + 1]) & (t[j + 2]))
  129. # AddRoundConstant (Iota)
  130. s[0] ^= RC[rnd]
  131. # Reinterpret octet array b to word array and XOR it to state s.
  132. def _reinterpret_to_words_and_xor(s, b):
  133. for j in range(0, len(b) // 8):
  134. s[j] ^= _from_le(b[8 * j : 8 * j + 8])
  135. # Reinterpret word array w to octet array and return it.
  136. def _reinterpret_to_octets(w):
  137. mp = bytearray()
  138. for j in range(0, len(w)):
  139. mp += int_to_bytes(w[j], 8, byteorder="little")
  140. return mp
  141. def _sha3_raw(msg, r_w, o_p, e_b):
  142. """Semi-generic SHA-3 implementation"""
  143. r_b = 8 * r_w
  144. s = [0] * 25
  145. # Handle whole blocks.
  146. idx = 0
  147. blocks = len(msg) // r_b
  148. for i in range(0, blocks):
  149. _reinterpret_to_words_and_xor(s, msg[idx : idx + r_b])
  150. idx += r_b
  151. _sha3_transform(s)
  152. # Handle last block padding.
  153. m = bytearray(msg[idx:])
  154. m.append(o_p)
  155. while len(m) < r_b:
  156. m.append(0)
  157. m[len(m) - 1] |= 128
  158. # Handle padded last block.
  159. _reinterpret_to_words_and_xor(s, m)
  160. _sha3_transform(s)
  161. # Output.
  162. out = bytearray()
  163. while len(out) < e_b:
  164. out += _reinterpret_to_octets(s[:r_w])
  165. _sha3_transform(s)
  166. return out[:e_b]
  167. def shake_256(msg, outlen):
  168. return _sha3_raw(msg, 17, 31, outlen)