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.

557 lines
14 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 *threefish1024) Encrypt(dst, src []byte) {
  6. var block [16]uint64
  7. bytesToBlock1024(&block, src)
  8. Encrypt1024(&block, &(t.keys), &(t.tweak))
  9. block1024ToBytes(dst, &block)
  10. }
  11. func (t *threefish1024) Decrypt(dst, src []byte) {
  12. var block [16]uint64
  13. bytesToBlock1024(&block, src)
  14. Decrypt1024(&block, &(t.keys), &(t.tweak))
  15. block1024ToBytes(dst, &block)
  16. }
  17. func newCipher1024(tweak *[TweakSize]byte, key []byte) *threefish1024 {
  18. c := new(threefish1024)
  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[:16] {
  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[16] = C240 ^ c.keys[0] ^ c.keys[1] ^ c.keys[2] ^ c.keys[3] ^ c.keys[4] ^ c.keys[5] ^ c.keys[6] ^
  30. c.keys[7] ^ c.keys[8] ^ c.keys[9] ^ c.keys[10] ^ c.keys[11] ^ c.keys[12] ^ c.keys[13] ^ c.keys[14] ^ c.keys[15]
  31. return c
  32. }
  33. // Encrypt1024 encrypts the 16 words of block using the expanded 1024 bit key and
  34. // the 128 bit tweak. The keys[16] must be keys[0] xor keys[1] xor ... keys[15] xor C240.
  35. // The tweak[2] must be tweak[0] xor tweak[1].
  36. func Encrypt1024(block *[16]uint64, keys *[17]uint64, tweak *[3]uint64) {
  37. b0, b1, b2, b3 := block[0], block[1], block[2], block[3]
  38. b4, b5, b6, b7 := block[4], block[5], block[6], block[7]
  39. b8, b9, b10, b11 := block[8], block[9], block[10], block[11]
  40. b12, b13, b14, b15 := block[12], block[13], block[14], block[15]
  41. for r := 0; r < 19; r++ {
  42. b0 += keys[r%17]
  43. b1 += keys[(r+1)%17]
  44. b2 += keys[(r+2)%17]
  45. b3 += keys[(r+3)%17]
  46. b4 += keys[(r+4)%17]
  47. b5 += keys[(r+5)%17]
  48. b6 += keys[(r+6)%17]
  49. b7 += keys[(r+7)%17]
  50. b8 += keys[(r+8)%17]
  51. b9 += keys[(r+9)%17]
  52. b10 += keys[(r+10)%17]
  53. b11 += keys[(r+11)%17]
  54. b12 += keys[(r+12)%17]
  55. b13 += keys[(r+13)%17] + tweak[r%3]
  56. b14 += keys[(r+14)%17] + tweak[(r+1)%3]
  57. b15 += keys[(r+15)%17] + uint64(r)
  58. b0 += b1
  59. b1 = ((b1 << 24) | (b1 >> (64 - 24))) ^ b0
  60. b2 += b3
  61. b3 = ((b3 << 13) | (b3 >> (64 - 13))) ^ b2
  62. b4 += b5
  63. b5 = ((b5 << 8) | (b5 >> (64 - 8))) ^ b4
  64. b6 += b7
  65. b7 = ((b7 << 47) | (b7 >> (64 - 47))) ^ b6
  66. b8 += b9
  67. b9 = ((b9 << 8) | (b9 >> (64 - 8))) ^ b8
  68. b10 += b11
  69. b11 = ((b11 << 17) | (b11 >> (64 - 17))) ^ b10
  70. b12 += b13
  71. b13 = ((b13 << 22) | (b13 >> (64 - 22))) ^ b12
  72. b14 += b15
  73. b15 = ((b15 << 37) | (b15 >> (64 - 37))) ^ b14
  74. b0 += b9
  75. b9 = ((b9 << 38) | (b9 >> (64 - 38))) ^ b0
  76. b2 += b13
  77. b13 = ((b13 << 19) | (b13 >> (64 - 19))) ^ b2
  78. b6 += b11
  79. b11 = ((b11 << 10) | (b11 >> (64 - 10))) ^ b6
  80. b4 += b15
  81. b15 = ((b15 << 55) | (b15 >> (64 - 55))) ^ b4
  82. b10 += b7
  83. b7 = ((b7 << 49) | (b7 >> (64 - 49))) ^ b10
  84. b12 += b3
  85. b3 = ((b3 << 18) | (b3 >> (64 - 18))) ^ b12
  86. b14 += b5
  87. b5 = ((b5 << 23) | (b5 >> (64 - 23))) ^ b14
  88. b8 += b1
  89. b1 = ((b1 << 52) | (b1 >> (64 - 52))) ^ b8
  90. b0 += b7
  91. b7 = ((b7 << 33) | (b7 >> (64 - 33))) ^ b0
  92. b2 += b5
  93. b5 = ((b5 << 4) | (b5 >> (64 - 4))) ^ b2
  94. b4 += b3
  95. b3 = ((b3 << 51) | (b3 >> (64 - 51))) ^ b4
  96. b6 += b1
  97. b1 = ((b1 << 13) | (b1 >> (64 - 13))) ^ b6
  98. b12 += b15
  99. b15 = ((b15 << 34) | (b15 >> (64 - 34))) ^ b12
  100. b14 += b13
  101. b13 = ((b13 << 41) | (b13 >> (64 - 41))) ^ b14
  102. b8 += b11
  103. b11 = ((b11 << 59) | (b11 >> (64 - 59))) ^ b8
  104. b10 += b9
  105. b9 = ((b9 << 17) | (b9 >> (64 - 17))) ^ b10
  106. b0 += b15
  107. b15 = ((b15 << 5) | (b15 >> (64 - 5))) ^ b0
  108. b2 += b11
  109. b11 = ((b11 << 20) | (b11 >> (64 - 20))) ^ b2
  110. b6 += b13
  111. b13 = ((b13 << 48) | (b13 >> (64 - 48))) ^ b6
  112. b4 += b9
  113. b9 = ((b9 << 41) | (b9 >> (64 - 41))) ^ b4
  114. b14 += b1
  115. b1 = ((b1 << 47) | (b1 >> (64 - 47))) ^ b14
  116. b8 += b5
  117. b5 = ((b5 << 28) | (b5 >> (64 - 28))) ^ b8
  118. b10 += b3
  119. b3 = ((b3 << 16) | (b3 >> (64 - 16))) ^ b10
  120. b12 += b7
  121. b7 = ((b7 << 25) | (b7 >> (64 - 25))) ^ b12
  122. r++
  123. b0 += keys[r%17]
  124. b1 += keys[(r+1)%17]
  125. b2 += keys[(r+2)%17]
  126. b3 += keys[(r+3)%17]
  127. b4 += keys[(r+4)%17]
  128. b5 += keys[(r+5)%17]
  129. b6 += keys[(r+6)%17]
  130. b7 += keys[(r+7)%17]
  131. b8 += keys[(r+8)%17]
  132. b9 += keys[(r+9)%17]
  133. b10 += keys[(r+10)%17]
  134. b11 += keys[(r+11)%17]
  135. b12 += keys[(r+12)%17]
  136. b13 += keys[(r+13)%17] + tweak[r%3]
  137. b14 += keys[(r+14)%17] + tweak[(r+1)%3]
  138. b15 += keys[(r+15)%17] + uint64(r)
  139. b0 += b1
  140. b1 = ((b1 << 41) | (b1 >> (64 - 41))) ^ b0
  141. b2 += b3
  142. b3 = ((b3 << 9) | (b3 >> (64 - 9))) ^ b2
  143. b4 += b5
  144. b5 = ((b5 << 37) | (b5 >> (64 - 37))) ^ b4
  145. b6 += b7
  146. b7 = ((b7 << 31) | (b7 >> (64 - 31))) ^ b6
  147. b8 += b9
  148. b9 = ((b9 << 12) | (b9 >> (64 - 12))) ^ b8
  149. b10 += b11
  150. b11 = ((b11 << 47) | (b11 >> (64 - 47))) ^ b10
  151. b12 += b13
  152. b13 = ((b13 << 44) | (b13 >> (64 - 44))) ^ b12
  153. b14 += b15
  154. b15 = ((b15 << 30) | (b15 >> (64 - 30))) ^ b14
  155. b0 += b9
  156. b9 = ((b9 << 16) | (b9 >> (64 - 16))) ^ b0
  157. b2 += b13
  158. b13 = ((b13 << 34) | (b13 >> (64 - 34))) ^ b2
  159. b6 += b11
  160. b11 = ((b11 << 56) | (b11 >> (64 - 56))) ^ b6
  161. b4 += b15
  162. b15 = ((b15 << 51) | (b15 >> (64 - 51))) ^ b4
  163. b10 += b7
  164. b7 = ((b7 << 4) | (b7 >> (64 - 4))) ^ b10
  165. b12 += b3
  166. b3 = ((b3 << 53) | (b3 >> (64 - 53))) ^ b12
  167. b14 += b5
  168. b5 = ((b5 << 42) | (b5 >> (64 - 42))) ^ b14
  169. b8 += b1
  170. b1 = ((b1 << 41) | (b1 >> (64 - 41))) ^ b8
  171. b0 += b7
  172. b7 = ((b7 << 31) | (b7 >> (64 - 31))) ^ b0
  173. b2 += b5
  174. b5 = ((b5 << 44) | (b5 >> (64 - 44))) ^ b2
  175. b4 += b3
  176. b3 = ((b3 << 47) | (b3 >> (64 - 47))) ^ b4
  177. b6 += b1
  178. b1 = ((b1 << 46) | (b1 >> (64 - 46))) ^ b6
  179. b12 += b15
  180. b15 = ((b15 << 19) | (b15 >> (64 - 19))) ^ b12
  181. b14 += b13
  182. b13 = ((b13 << 42) | (b13 >> (64 - 42))) ^ b14
  183. b8 += b11
  184. b11 = ((b11 << 44) | (b11 >> (64 - 44))) ^ b8
  185. b10 += b9
  186. b9 = ((b9 << 25) | (b9 >> (64 - 25))) ^ b10
  187. b0 += b15
  188. b15 = ((b15 << 9) | (b15 >> (64 - 9))) ^ b0
  189. b2 += b11
  190. b11 = ((b11 << 48) | (b11 >> (64 - 48))) ^ b2
  191. b6 += b13
  192. b13 = ((b13 << 35) | (b13 >> (64 - 35))) ^ b6
  193. b4 += b9
  194. b9 = ((b9 << 52) | (b9 >> (64 - 52))) ^ b4
  195. b14 += b1
  196. b1 = ((b1 << 23) | (b1 >> (64 - 23))) ^ b14
  197. b8 += b5
  198. b5 = ((b5 << 31) | (b5 >> (64 - 31))) ^ b8
  199. b10 += b3
  200. b3 = ((b3 << 37) | (b3 >> (64 - 37))) ^ b10
  201. b12 += b7
  202. b7 = ((b7 << 20) | (b7 >> (64 - 20))) ^ b12
  203. }
  204. b0 += keys[3]
  205. b1 += keys[4]
  206. b2 += keys[5]
  207. b3 += keys[6]
  208. b4 += keys[7]
  209. b5 += keys[8]
  210. b6 += keys[9]
  211. b7 += keys[10]
  212. b8 += keys[11]
  213. b9 += keys[12]
  214. b10 += keys[13]
  215. b11 += keys[14]
  216. b12 += keys[15]
  217. b13 += keys[16] + tweak[2]
  218. b14 += keys[0] + tweak[0]
  219. b15 += keys[1] + 20
  220. block[0], block[1], block[2], block[3] = b0, b1, b2, b3
  221. block[4], block[5], block[6], block[7] = b4, b5, b6, b7
  222. block[8], block[9], block[10], block[11] = b8, b9, b10, b11
  223. block[12], block[13], block[14], block[15] = b12, b13, b14, b15
  224. }
  225. // Decrypt1024 decrypts the 16 words of block using the expanded 1024 bit key and
  226. // the 128 bit tweak. The keys[16] must be keys[0] xor keys[1] xor ... keys[15] xor C240.
  227. // The tweak[2] must be tweak[0] xor tweak[1].
  228. func Decrypt1024(block *[16]uint64, keys *[17]uint64, tweak *[3]uint64) {
  229. b0, b1, b2, b3 := block[0], block[1], block[2], block[3]
  230. b4, b5, b6, b7 := block[4], block[5], block[6], block[7]
  231. b8, b9, b10, b11 := block[8], block[9], block[10], block[11]
  232. b12, b13, b14, b15 := block[12], block[13], block[14], block[15]
  233. var tmp uint64
  234. for r := 20; r > 1; r-- {
  235. b0 -= keys[r%17]
  236. b1 -= keys[(r+1)%17]
  237. b2 -= keys[(r+2)%17]
  238. b3 -= keys[(r+3)%17]
  239. b4 -= keys[(r+4)%17]
  240. b5 -= keys[(r+5)%17]
  241. b6 -= keys[(r+6)%17]
  242. b7 -= keys[(r+7)%17]
  243. b8 -= keys[(r+8)%17]
  244. b9 -= keys[(r+9)%17]
  245. b10 -= keys[(r+10)%17]
  246. b11 -= keys[(r+11)%17]
  247. b12 -= keys[(r+12)%17]
  248. b13 -= keys[(r+13)%17] + tweak[r%3]
  249. b14 -= keys[(r+14)%17] + tweak[(r+1)%3]
  250. b15 -= keys[(r+15)%17] + uint64(r)
  251. tmp = b7 ^ b12
  252. b7 = (tmp >> 20) | (tmp << (64 - 20))
  253. b12 -= b7
  254. tmp = b3 ^ b10
  255. b3 = (tmp >> 37) | (tmp << (64 - 37))
  256. b10 -= b3
  257. tmp = b5 ^ b8
  258. b5 = (tmp >> 31) | (tmp << (64 - 31))
  259. b8 -= b5
  260. tmp = b1 ^ b14
  261. b1 = (tmp >> 23) | (tmp << (64 - 23))
  262. b14 -= b1
  263. tmp = b9 ^ b4
  264. b9 = (tmp >> 52) | (tmp << (64 - 52))
  265. b4 -= b9
  266. tmp = b13 ^ b6
  267. b13 = (tmp >> 35) | (tmp << (64 - 35))
  268. b6 -= b13
  269. tmp = b11 ^ b2
  270. b11 = (tmp >> 48) | (tmp << (64 - 48))
  271. b2 -= b11
  272. tmp = b15 ^ b0
  273. b15 = (tmp >> 9) | (tmp << (64 - 9))
  274. b0 -= b15
  275. tmp = b9 ^ b10
  276. b9 = (tmp >> 25) | (tmp << (64 - 25))
  277. b10 -= b9
  278. tmp = b11 ^ b8
  279. b11 = (tmp >> 44) | (tmp << (64 - 44))
  280. b8 -= b11
  281. tmp = b13 ^ b14
  282. b13 = (tmp >> 42) | (tmp << (64 - 42))
  283. b14 -= b13
  284. tmp = b15 ^ b12
  285. b15 = (tmp >> 19) | (tmp << (64 - 19))
  286. b12 -= b15
  287. tmp = b1 ^ b6
  288. b1 = (tmp >> 46) | (tmp << (64 - 46))
  289. b6 -= b1
  290. tmp = b3 ^ b4
  291. b3 = (tmp >> 47) | (tmp << (64 - 47))
  292. b4 -= b3
  293. tmp = b5 ^ b2
  294. b5 = (tmp >> 44) | (tmp << (64 - 44))
  295. b2 -= b5
  296. tmp = b7 ^ b0
  297. b7 = (tmp >> 31) | (tmp << (64 - 31))
  298. b0 -= b7
  299. tmp = b1 ^ b8
  300. b1 = (tmp >> 41) | (tmp << (64 - 41))
  301. b8 -= b1
  302. tmp = b5 ^ b14
  303. b5 = (tmp >> 42) | (tmp << (64 - 42))
  304. b14 -= b5
  305. tmp = b3 ^ b12
  306. b3 = (tmp >> 53) | (tmp << (64 - 53))
  307. b12 -= b3
  308. tmp = b7 ^ b10
  309. b7 = (tmp >> 4) | (tmp << (64 - 4))
  310. b10 -= b7
  311. tmp = b15 ^ b4
  312. b15 = (tmp >> 51) | (tmp << (64 - 51))
  313. b4 -= b15
  314. tmp = b11 ^ b6
  315. b11 = (tmp >> 56) | (tmp << (64 - 56))
  316. b6 -= b11
  317. tmp = b13 ^ b2
  318. b13 = (tmp >> 34) | (tmp << (64 - 34))
  319. b2 -= b13
  320. tmp = b9 ^ b0
  321. b9 = (tmp >> 16) | (tmp << (64 - 16))
  322. b0 -= b9
  323. tmp = b15 ^ b14
  324. b15 = (tmp >> 30) | (tmp << (64 - 30))
  325. b14 -= b15
  326. tmp = b13 ^ b12
  327. b13 = (tmp >> 44) | (tmp << (64 - 44))
  328. b12 -= b13
  329. tmp = b11 ^ b10
  330. b11 = (tmp >> 47) | (tmp << (64 - 47))
  331. b10 -= b11
  332. tmp = b9 ^ b8
  333. b9 = (tmp >> 12) | (tmp << (64 - 12))
  334. b8 -= b9
  335. tmp = b7 ^ b6
  336. b7 = (tmp >> 31) | (tmp << (64 - 31))
  337. b6 -= b7
  338. tmp = b5 ^ b4
  339. b5 = (tmp >> 37) | (tmp << (64 - 37))
  340. b4 -= b5
  341. tmp = b3 ^ b2
  342. b3 = (tmp >> 9) | (tmp << (64 - 9))
  343. b2 -= b3
  344. tmp = b1 ^ b0
  345. b1 = (tmp >> 41) | (tmp << (64 - 41))
  346. b0 -= b1
  347. r--
  348. b0 -= keys[r%17]
  349. b1 -= keys[(r+1)%17]
  350. b2 -= keys[(r+2)%17]
  351. b3 -= keys[(r+3)%17]
  352. b4 -= keys[(r+4)%17]
  353. b5 -= keys[(r+5)%17]
  354. b6 -= keys[(r+6)%17]
  355. b7 -= keys[(r+7)%17]
  356. b8 -= keys[(r+8)%17]
  357. b9 -= keys[(r+9)%17]
  358. b10 -= keys[(r+10)%17]
  359. b11 -= keys[(r+11)%17]
  360. b12 -= keys[(r+12)%17]
  361. b13 -= keys[(r+13)%17] + tweak[r%3]
  362. b14 -= keys[(r+14)%17] + tweak[(r+1)%3]
  363. b15 -= keys[(r+15)%17] + uint64(r)
  364. tmp = b7 ^ b12
  365. b7 = (tmp >> 25) | (tmp << (64 - 25))
  366. b12 -= b7
  367. tmp = b3 ^ b10
  368. b3 = (tmp >> 16) | (tmp << (64 - 16))
  369. b10 -= b3
  370. tmp = b5 ^ b8
  371. b5 = (tmp >> 28) | (tmp << (64 - 28))
  372. b8 -= b5
  373. tmp = b1 ^ b14
  374. b1 = (tmp >> 47) | (tmp << (64 - 47))
  375. b14 -= b1
  376. tmp = b9 ^ b4
  377. b9 = (tmp >> 41) | (tmp << (64 - 41))
  378. b4 -= b9
  379. tmp = b13 ^ b6
  380. b13 = (tmp >> 48) | (tmp << (64 - 48))
  381. b6 -= b13
  382. tmp = b11 ^ b2
  383. b11 = (tmp >> 20) | (tmp << (64 - 20))
  384. b2 -= b11
  385. tmp = b15 ^ b0
  386. b15 = (tmp >> 5) | (tmp << (64 - 5))
  387. b0 -= b15
  388. tmp = b9 ^ b10
  389. b9 = (tmp >> 17) | (tmp << (64 - 17))
  390. b10 -= b9
  391. tmp = b11 ^ b8
  392. b11 = (tmp >> 59) | (tmp << (64 - 59))
  393. b8 -= b11
  394. tmp = b13 ^ b14
  395. b13 = (tmp >> 41) | (tmp << (64 - 41))
  396. b14 -= b13
  397. tmp = b15 ^ b12
  398. b15 = (tmp >> 34) | (tmp << (64 - 34))
  399. b12 -= b15
  400. tmp = b1 ^ b6
  401. b1 = (tmp >> 13) | (tmp << (64 - 13))
  402. b6 -= b1
  403. tmp = b3 ^ b4
  404. b3 = (tmp >> 51) | (tmp << (64 - 51))
  405. b4 -= b3
  406. tmp = b5 ^ b2
  407. b5 = (tmp >> 4) | (tmp << (64 - 4))
  408. b2 -= b5
  409. tmp = b7 ^ b0
  410. b7 = (tmp >> 33) | (tmp << (64 - 33))
  411. b0 -= b7
  412. tmp = b1 ^ b8
  413. b1 = (tmp >> 52) | (tmp << (64 - 52))
  414. b8 -= b1
  415. tmp = b5 ^ b14
  416. b5 = (tmp >> 23) | (tmp << (64 - 23))
  417. b14 -= b5
  418. tmp = b3 ^ b12
  419. b3 = (tmp >> 18) | (tmp << (64 - 18))
  420. b12 -= b3
  421. tmp = b7 ^ b10
  422. b7 = (tmp >> 49) | (tmp << (64 - 49))
  423. b10 -= b7
  424. tmp = b15 ^ b4
  425. b15 = (tmp >> 55) | (tmp << (64 - 55))
  426. b4 -= b15
  427. tmp = b11 ^ b6
  428. b11 = (tmp >> 10) | (tmp << (64 - 10))
  429. b6 -= b11
  430. tmp = b13 ^ b2
  431. b13 = (tmp >> 19) | (tmp << (64 - 19))
  432. b2 -= b13
  433. tmp = b9 ^ b0
  434. b9 = (tmp >> 38) | (tmp << (64 - 38))
  435. b0 -= b9
  436. tmp = b15 ^ b14
  437. b15 = (tmp >> 37) | (tmp << (64 - 37))
  438. b14 -= b15
  439. tmp = b13 ^ b12
  440. b13 = (tmp >> 22) | (tmp << (64 - 22))
  441. b12 -= b13
  442. tmp = b11 ^ b10
  443. b11 = (tmp >> 17) | (tmp << (64 - 17))
  444. b10 -= b11
  445. tmp = b9 ^ b8
  446. b9 = (tmp >> 8) | (tmp << (64 - 8))
  447. b8 -= b9
  448. tmp = b7 ^ b6
  449. b7 = (tmp >> 47) | (tmp << (64 - 47))
  450. b6 -= b7
  451. tmp = b5 ^ b4
  452. b5 = (tmp >> 8) | (tmp << (64 - 8))
  453. b4 -= b5
  454. tmp = b3 ^ b2
  455. b3 = (tmp >> 13) | (tmp << (64 - 13))
  456. b2 -= b3
  457. tmp = b1 ^ b0
  458. b1 = (tmp >> 24) | (tmp << (64 - 24))
  459. b0 -= b1
  460. }
  461. b0 -= keys[0]
  462. b1 -= keys[1]
  463. b2 -= keys[2]
  464. b3 -= keys[3]
  465. b4 -= keys[4]
  466. b5 -= keys[5]
  467. b6 -= keys[6]
  468. b7 -= keys[7]
  469. b8 -= keys[8]
  470. b9 -= keys[9]
  471. b10 -= keys[10]
  472. b11 -= keys[11]
  473. b12 -= keys[12]
  474. b13 -= keys[13] + tweak[0]
  475. b14 -= keys[14] + tweak[1]
  476. b15 -= keys[15]
  477. block[0], block[1], block[2], block[3] = b0, b1, b2, b3
  478. block[4], block[5], block[6], block[7] = b4, b5, b6, b7
  479. block[8], block[9], block[10], block[11] = b8, b9, b10, b11
  480. block[12], block[13], block[14], block[15] = b12, b13, b14, b15
  481. }
  482. // UBI1024 does a Threefish1024 encryption of the given block using
  483. // the chain values hVal and the tweak.
  484. // The chain values are updated through hVal[i] = block[i] ^ Enc(block)[i]
  485. func UBI1024(block *[16]uint64, hVal *[17]uint64, tweak *[3]uint64) {
  486. b0, b1, b2, b3 := block[0], block[1], block[2], block[3]
  487. b4, b5, b6, b7 := block[4], block[5], block[6], block[7]
  488. b8, b9, b10, b11 := block[8], block[9], block[10], block[11]
  489. b12, b13, b14, b15 := block[12], block[13], block[14], block[15]
  490. hVal[16] = C240 ^ hVal[0] ^ hVal[1] ^ hVal[2] ^ hVal[3] ^ hVal[4] ^ hVal[5] ^ hVal[6] ^ hVal[7] ^
  491. hVal[8] ^ hVal[9] ^ hVal[10] ^ hVal[11] ^ hVal[12] ^ hVal[13] ^ hVal[14] ^ hVal[15]
  492. tweak[2] = tweak[0] ^ tweak[1]
  493. Encrypt1024(block, hVal, tweak)
  494. hVal[0] = block[0] ^ b0
  495. hVal[1] = block[1] ^ b1
  496. hVal[2] = block[2] ^ b2
  497. hVal[3] = block[3] ^ b3
  498. hVal[4] = block[4] ^ b4
  499. hVal[5] = block[5] ^ b5
  500. hVal[6] = block[6] ^ b6
  501. hVal[7] = block[7] ^ b7
  502. hVal[8] = block[8] ^ b8
  503. hVal[9] = block[9] ^ b9
  504. hVal[10] = block[10] ^ b10
  505. hVal[11] = block[11] ^ b11
  506. hVal[12] = block[12] ^ b12
  507. hVal[13] = block[13] ^ b13
  508. hVal[14] = block[14] ^ b14
  509. hVal[15] = block[15] ^ b15
  510. }