You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
7.6 KiB

  1. // Copyright (c) 2016 Andreas Auernhammer. All rights reserved.
  2. // Use of this source code is governed by a license that can be
  3. // found in the LICENSE file.
  4. package threefish
  5. func (t *threefish512) Encrypt(dst, src []byte) {
  6. var block [8]uint64
  7. bytesToBlock512(&block, src)
  8. Encrypt512(&block, &(t.keys), &(t.tweak))
  9. block512ToBytes(dst, &block)
  10. }
  11. func (t *threefish512) Decrypt(dst, src []byte) {
  12. var block [8]uint64
  13. bytesToBlock512(&block, src)
  14. Decrypt512(&block, &(t.keys), &(t.tweak))
  15. block512ToBytes(dst, &block)
  16. }
  17. func newCipher512(tweak *[TweakSize]byte, key []byte) *threefish512 {
  18. c := new(threefish512)
  19. c.tweak[0] = uint64(tweak[0]) | uint64(tweak[1])<<8 | uint64(tweak[2])<<16 | uint64(tweak[3])<<24 |
  20. uint64(tweak[4])<<32 | uint64(tweak[5])<<40 | uint64(tweak[6])<<48 | uint64(tweak[7])<<56
  21. c.tweak[1] = uint64(tweak[8]) | uint64(tweak[9])<<8 | uint64(tweak[10])<<16 | uint64(tweak[11])<<24 |
  22. uint64(tweak[12])<<32 | uint64(tweak[13])<<40 | uint64(tweak[14])<<48 | uint64(tweak[15])<<56
  23. c.tweak[2] = c.tweak[0] ^ c.tweak[1]
  24. for i := range c.keys[:8] {
  25. j := i * 8
  26. c.keys[i] = uint64(key[j]) | uint64(key[j+1])<<8 | uint64(key[j+2])<<16 | uint64(key[j+3])<<24 |
  27. uint64(key[j+4])<<32 | uint64(key[j+5])<<40 | uint64(key[j+6])<<48 | uint64(key[j+7])<<56
  28. }
  29. c.keys[8] = C240 ^ c.keys[0] ^ c.keys[1] ^ c.keys[2] ^ c.keys[3] ^ c.keys[4] ^ c.keys[5] ^ c.keys[6] ^ c.keys[7]
  30. return c
  31. }
  32. // Encrypt512 encrypts the 8 words of block using the expanded 512 bit key and
  33. // the 128 bit tweak. The keys[8] must be keys[0] xor keys[1] xor ... keys[8] xor C240.
  34. // The tweak[2] must be tweak[0] xor tweak[1].
  35. func Encrypt512(block *[8]uint64, keys *[9]uint64, tweak *[3]uint64) {
  36. b0, b1, b2, b3 := block[0], block[1], block[2], block[3]
  37. b4, b5, b6, b7 := block[4], block[5], block[6], block[7]
  38. for r := 0; r < 17; r++ {
  39. b0 += keys[r%9]
  40. b1 += keys[(r+1)%9]
  41. b2 += keys[(r+2)%9]
  42. b3 += keys[(r+3)%9]
  43. b4 += keys[(r+4)%9]
  44. b5 += keys[(r+5)%9] + tweak[r%3]
  45. b6 += keys[(r+6)%9] + tweak[(r+1)%3]
  46. b7 += keys[(r+7)%9] + uint64(r)
  47. b0 += b1
  48. b1 = (b1<<46 | b1>>(64-46)) ^ b0
  49. b2 += b3
  50. b3 = (b3<<36 | b3>>(64-36)) ^ b2
  51. b4 += b5
  52. b5 = (b5<<19 | b5>>(64-19)) ^ b4
  53. b6 += b7
  54. b7 = (b7<<37 | b7>>(64-37)) ^ b6
  55. b2 += b1
  56. b1 = (b1<<33 | b1>>(64-33)) ^ b2
  57. b4 += b7
  58. b7 = (b7<<27 | b7>>(64-27)) ^ b4
  59. b6 += b5
  60. b5 = (b5<<14 | b5>>(64-14)) ^ b6
  61. b0 += b3
  62. b3 = (b3<<42 | b3>>(64-42)) ^ b0
  63. b4 += b1
  64. b1 = (b1<<17 | b1>>(64-17)) ^ b4
  65. b6 += b3
  66. b3 = (b3<<49 | b3>>(64-49)) ^ b6
  67. b0 += b5
  68. b5 = (b5<<36 | b5>>(64-36)) ^ b0
  69. b2 += b7
  70. b7 = (b7<<39 | b7>>(64-39)) ^ b2
  71. b6 += b1
  72. b1 = (b1<<44 | b1>>(64-44)) ^ b6
  73. b0 += b7
  74. b7 = (b7<<9 | b7>>(64-9)) ^ b0
  75. b2 += b5
  76. b5 = (b5<<54 | b5>>(64-54)) ^ b2
  77. b4 += b3
  78. b3 = (b3<<56 | b3>>(64-56)) ^ b4
  79. r++
  80. b0 += keys[r%9]
  81. b1 += keys[(r+1)%9]
  82. b2 += keys[(r+2)%9]
  83. b3 += keys[(r+3)%9]
  84. b4 += keys[(r+4)%9]
  85. b5 += keys[(r+5)%9] + tweak[r%3]
  86. b6 += keys[(r+6)%9] + tweak[(r+1)%3]
  87. b7 += keys[(r+7)%9] + uint64(r)
  88. b0 += b1
  89. b1 = (b1<<39 | b1>>(64-39)) ^ b0
  90. b2 += b3
  91. b3 = (b3<<30 | b3>>(64-30)) ^ b2
  92. b4 += b5
  93. b5 = (b5<<34 | b5>>(64-34)) ^ b4
  94. b6 += b7
  95. b7 = (b7<<24 | b7>>(64-24)) ^ b6
  96. b2 += b1
  97. b1 = (b1<<13 | b1>>(64-13)) ^ b2
  98. b4 += b7
  99. b7 = (b7<<50 | b7>>(64-50)) ^ b4
  100. b6 += b5
  101. b5 = (b5<<10 | b5>>(64-10)) ^ b6
  102. b0 += b3
  103. b3 = (b3<<17 | b3>>(64-17)) ^ b0
  104. b4 += b1
  105. b1 = (b1<<25 | b1>>(64-25)) ^ b4
  106. b6 += b3
  107. b3 = (b3<<29 | b3>>(64-29)) ^ b6
  108. b0 += b5
  109. b5 = (b5<<39 | b5>>(64-39)) ^ b0
  110. b2 += b7
  111. b7 = (b7<<43 | b7>>(64-43)) ^ b2
  112. b6 += b1
  113. b1 = (b1<<8 | b1>>(64-8)) ^ b6
  114. b0 += b7
  115. b7 = (b7<<35 | b7>>(64-35)) ^ b0
  116. b2 += b5
  117. b5 = (b5<<56 | b5>>(64-56)) ^ b2
  118. b4 += b3
  119. b3 = (b3<<22 | b3>>(64-22)) ^ b4
  120. }
  121. b0 += keys[0]
  122. b1 += keys[1]
  123. b2 += keys[2]
  124. b3 += keys[3]
  125. b4 += keys[4]
  126. b5 += keys[5] + tweak[0]
  127. b6 += keys[6] + tweak[1]
  128. b7 += keys[7] + 18
  129. block[0], block[1], block[2], block[3] = b0, b1, b2, b3
  130. block[4], block[5], block[6], block[7] = b4, b5, b6, b7
  131. }
  132. // Decrypt512 decrypts the 8 words of block using the expanded 512 bit key and
  133. // the 128 bit tweak. The keys[8] must be keys[0] xor keys[1] xor ... keys[8] xor C240.
  134. // The tweak[2] must be tweak[0] xor tweak[1].
  135. func Decrypt512(block *[8]uint64, keys *[9]uint64, tweak *[3]uint64) {
  136. b0, b1, b2, b3 := block[0], block[1], block[2], block[3]
  137. b4, b5, b6, b7 := block[4], block[5], block[6], block[7]
  138. var tmp uint64
  139. for r := 18; r > 1; r-- {
  140. b0 -= keys[r%9]
  141. b1 -= keys[(r+1)%9]
  142. b2 -= keys[(r+2)%9]
  143. b3 -= keys[(r+3)%9]
  144. b4 -= keys[(r+4)%9]
  145. b5 -= keys[(r+5)%9] + tweak[r%3]
  146. b6 -= keys[(r+6)%9] + tweak[(r+1)%3]
  147. b7 -= keys[(r+7)%9] + uint64(r)
  148. tmp = b3 ^ b4
  149. b3 = tmp>>22 | tmp<<(64-22)
  150. b4 -= b3
  151. tmp = b5 ^ b2
  152. b5 = tmp>>56 | tmp<<(64-56)
  153. b2 -= b5
  154. tmp = b7 ^ b0
  155. b7 = tmp>>35 | tmp<<(64-35)
  156. b0 -= b7
  157. tmp = b1 ^ b6
  158. b1 = tmp>>8 | tmp<<(64-8)
  159. b6 -= b1
  160. tmp = b7 ^ b2
  161. b7 = tmp>>43 | tmp<<(64-43)
  162. b2 -= b7
  163. tmp = b5 ^ b0
  164. b5 = tmp>>39 | tmp<<(64-39)
  165. b0 -= b5
  166. tmp = b3 ^ b6
  167. b3 = tmp>>29 | tmp<<(64-29)
  168. b6 -= b3
  169. tmp = b1 ^ b4
  170. b1 = tmp>>25 | tmp<<(64-25)
  171. b4 -= b1
  172. tmp = b3 ^ b0
  173. b3 = tmp>>17 | tmp<<(64-17)
  174. b0 -= b3
  175. tmp = b5 ^ b6
  176. b5 = tmp>>10 | tmp<<(64-10)
  177. b6 -= b5
  178. tmp = b7 ^ b4
  179. b7 = tmp>>50 | tmp<<(64-50)
  180. b4 -= b7
  181. tmp = b1 ^ b2
  182. b1 = tmp>>13 | tmp<<(64-13)
  183. b2 -= b1
  184. tmp = b7 ^ b6
  185. b7 = tmp>>24 | tmp<<(64-24)
  186. b6 -= b7
  187. tmp = b5 ^ b4
  188. b5 = tmp>>34 | tmp<<(64-34)
  189. b4 -= b5
  190. tmp = b3 ^ b2
  191. b3 = tmp>>30 | tmp<<(64-30)
  192. b2 -= b3
  193. tmp = b1 ^ b0
  194. b1 = tmp>>39 | tmp<<(64-39)
  195. b0 -= b1
  196. r--
  197. b0 -= keys[r%9]
  198. b1 -= keys[(r+1)%9]
  199. b2 -= keys[(r+2)%9]
  200. b3 -= keys[(r+3)%9]
  201. b4 -= keys[(r+4)%9]
  202. b5 -= keys[(r+5)%9] + tweak[r%3]
  203. b6 -= keys[(r+6)%9] + tweak[(r+1)%3]
  204. b7 -= keys[(r+7)%9] + uint64(r)
  205. tmp = b3 ^ b4
  206. b3 = tmp>>56 | tmp<<(64-56)
  207. b4 -= b3
  208. tmp = b5 ^ b2
  209. b5 = tmp>>54 | tmp<<(64-54)
  210. b2 -= b5
  211. tmp = b7 ^ b0
  212. b7 = tmp>>9 | tmp<<(64-9)
  213. b0 -= b7
  214. tmp = b1 ^ b6
  215. b1 = tmp>>44 | tmp<<(64-44)
  216. b6 -= b1
  217. tmp = b7 ^ b2
  218. b7 = tmp>>39 | tmp<<(64-39)
  219. b2 -= b7
  220. tmp = b5 ^ b0
  221. b5 = tmp>>36 | tmp<<(64-36)
  222. b0 -= b5
  223. tmp = b3 ^ b6
  224. b3 = tmp>>49 | tmp<<(64-49)
  225. b6 -= b3
  226. tmp = b1 ^ b4
  227. b1 = tmp>>17 | tmp<<(64-17)
  228. b4 -= b1
  229. tmp = b3 ^ b0
  230. b3 = tmp>>42 | tmp<<(64-42)
  231. b0 -= b3
  232. tmp = b5 ^ b6
  233. b5 = tmp>>14 | tmp<<(64-14)
  234. b6 -= b5
  235. tmp = b7 ^ b4
  236. b7 = tmp>>27 | tmp<<(64-27)
  237. b4 -= b7
  238. tmp = b1 ^ b2
  239. b1 = tmp>>33 | tmp<<(64-33)
  240. b2 -= b1
  241. tmp = b7 ^ b6
  242. b7 = tmp>>37 | tmp<<(64-37)
  243. b6 -= b7
  244. tmp = b5 ^ b4
  245. b5 = tmp>>19 | tmp<<(64-19)
  246. b4 -= b5
  247. tmp = b3 ^ b2
  248. b3 = tmp>>36 | tmp<<(64-36)
  249. b2 -= b3
  250. tmp = b1 ^ b0
  251. b1 = tmp>>46 | tmp<<(64-46)
  252. b0 -= b1
  253. }
  254. b0 -= keys[0]
  255. b1 -= keys[1]
  256. b2 -= keys[2]
  257. b3 -= keys[3]
  258. b4 -= keys[4]
  259. b5 -= keys[5] + tweak[0]
  260. b6 -= keys[6] + tweak[1]
  261. b7 -= keys[7]
  262. block[0], block[1], block[2], block[3] = b0, b1, b2, b3
  263. block[4], block[5], block[6], block[7] = b4, b5, b6, b7
  264. }
  265. // UBI512 does a Threefish512 encryption of the given block using
  266. // the chain values hVal and the tweak.
  267. // The chain values are updated through hVal[i] = block[i] ^ Enc(block)[i]
  268. func UBI512(block *[8]uint64, hVal *[9]uint64, tweak *[3]uint64) {
  269. b0, b1, b2, b3 := block[0], block[1], block[2], block[3]
  270. b4, b5, b6, b7 := block[4], block[5], block[6], block[7]
  271. hVal[8] = C240 ^ hVal[0] ^ hVal[1] ^ hVal[2] ^ hVal[3] ^ hVal[4] ^ hVal[5] ^ hVal[6] ^ hVal[7]
  272. tweak[2] = tweak[0] ^ tweak[1]
  273. Encrypt512(block, hVal, tweak)
  274. hVal[0] = block[0] ^ b0
  275. hVal[1] = block[1] ^ b1
  276. hVal[2] = block[2] ^ b2
  277. hVal[3] = block[3] ^ b3
  278. hVal[4] = block[4] ^ b4
  279. hVal[5] = block[5] ^ b5
  280. hVal[6] = block[6] ^ b6
  281. hVal[7] = block[7] ^ b7
  282. }