secretbox_reader.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * Copyright (c) 2017, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. // Copyright 2012 The Go Authors. All rights reserved.
  20. // Use of this source code is governed by a BSD-style
  21. // license that can be found in the LICENSE file.
  22. package secretbox // import "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/nacl/secretbox"
  23. import (
  24. "crypto/subtle"
  25. "encoding/binary"
  26. "fmt"
  27. "io"
  28. "golang.org/x/crypto/poly1305"
  29. "golang.org/x/crypto/salsa20/salsa"
  30. )
  31. // NewOpenReadSeeker is a streaming variant of Open.
  32. //
  33. // NewOpenReadSeeker is intended only for use in Psiphon with a payload that is
  34. // independently authenticated; and consideration has been given only for client-side
  35. // operation. Non-optimized reference implementation poly1305 and salsa20 code is used.
  36. //
  37. // The box is accessed through an io.ReadSeeker, which allows for an initial
  38. // poly1305 verification pass followed by a payload decryption pass, both
  39. // without loading the entire box into memory. As such, this implementation
  40. // should not be subject to the use-before-authentication or truncation attacks
  41. // discussed here:
  42. // https://github.com/golang/crypto/commit/9ba3862cf6a5452ae579de98f9364dd2e544844c#diff-9a969aca62172940631ad143523794ee
  43. // https://github.com/golang/go/issues/17673#issuecomment-275732868
  44. func NewOpenReadSeeker(box io.ReadSeeker, nonce *[24]byte, key *[32]byte) (io.ReadSeeker, error) {
  45. r := &salsa20ReadSeeker{
  46. box: box,
  47. nonce: *nonce,
  48. key: *key,
  49. }
  50. err := r.reset()
  51. if err != nil {
  52. return nil, err
  53. }
  54. return r, nil
  55. }
  56. type salsa20ReadSeeker struct {
  57. box io.ReadSeeker
  58. nonce [24]byte
  59. key [32]byte
  60. subKey [32]byte
  61. counter [16]byte
  62. block [64]byte
  63. blockOffset int
  64. }
  65. // Open x/crypto/nacl/secretbox/secretbox.go, adapted to streaming and rewinding.
  66. func (r *salsa20ReadSeeker) reset() error {
  67. // See comments in Open
  68. _, err := r.box.Seek(0, io.SeekStart)
  69. if err != nil {
  70. return fmt.Errorf("initial seek failed: %s", err)
  71. }
  72. var tag [poly1305.TagSize]byte
  73. _, err = io.ReadFull(r.box, tag[:])
  74. if err != nil {
  75. return fmt.Errorf("read tag failed: %s", err)
  76. }
  77. var subKey [32]byte
  78. var counter [16]byte
  79. setup(&subKey, &counter, &r.nonce, &r.key)
  80. // The Poly1305 key is generated by encrypting 32 bytes of zeros. Since
  81. // Salsa20 works with 64-byte blocks, we also generate 32 bytes of
  82. // keystream as a side effect.
  83. var firstBlock [64]byte
  84. salsa.XORKeyStream(firstBlock[:], firstBlock[:], &counter, &subKey)
  85. var poly1305Key [32]byte
  86. copy(poly1305Key[:], firstBlock[:])
  87. err = poly1305VerifyReader(&tag, r.box, &poly1305Key)
  88. if err != nil {
  89. return err
  90. }
  91. _, err = r.box.Seek(int64(len(tag)), io.SeekStart)
  92. if err != nil {
  93. return fmt.Errorf("rewind seek failed: %s", err)
  94. }
  95. counter[8] = 1
  96. r.subKey = subKey
  97. r.counter = counter
  98. // We XOR up to 32 bytes of box with the keystream generated from
  99. // the first block.
  100. r.block = firstBlock
  101. r.blockOffset = 32
  102. return nil
  103. }
  104. func (r *salsa20ReadSeeker) Read(p []byte) (int, error) {
  105. n, err := r.box.Read(p)
  106. for i := 0; i < n; i++ {
  107. if r.blockOffset == 64 {
  108. salsa20Core(&r.block, &r.counter, &r.subKey, &salsa.Sigma)
  109. u := uint32(1)
  110. for i := 8; i < 16; i++ {
  111. u += uint32(r.counter[i])
  112. r.counter[i] = byte(u)
  113. u >>= 8
  114. }
  115. r.blockOffset = 0
  116. }
  117. p[i] = p[i] ^ r.block[r.blockOffset]
  118. r.blockOffset++
  119. }
  120. return n, err
  121. }
  122. func (r *salsa20ReadSeeker) Seek(offset int64, whence int) (int64, error) {
  123. // Currently only supports Seek(0, io.SeekStart) as required for Psiphon.
  124. if offset != 0 || whence != io.SeekStart {
  125. return -1, fmt.Errorf("unsupported")
  126. }
  127. // TODO: could skip poly1305 verify after 1st reset.
  128. err := r.reset()
  129. if err != nil {
  130. return -1, err
  131. }
  132. return 0, nil
  133. }
  134. // Verify from crypto/poly1305/poly1305.go, modifed to use an io.Reader.
  135. func poly1305VerifyReader(mac *[16]byte, m io.Reader, key *[32]byte) error {
  136. var tmp [16]byte
  137. err := poly1305SumReader(&tmp, m, key)
  138. if err != nil {
  139. return err
  140. }
  141. if subtle.ConstantTimeCompare(tmp[:], mac[:]) != 1 {
  142. return fmt.Errorf("verify failed")
  143. }
  144. return nil
  145. }
  146. // Sum from crypto/poly1305/sum_ref.go, modifed to use an io.Reader.
  147. func poly1305SumReader(out *[poly1305.TagSize]byte, msg io.Reader, key *[32]byte) error {
  148. var (
  149. h0, h1, h2, h3, h4 uint32 // the hash accumulators
  150. r0, r1, r2, r3, r4 uint64 // the r part of the key
  151. )
  152. r0 = uint64(binary.LittleEndian.Uint32(key[0:]) & 0x3ffffff)
  153. r1 = uint64((binary.LittleEndian.Uint32(key[3:]) >> 2) & 0x3ffff03)
  154. r2 = uint64((binary.LittleEndian.Uint32(key[6:]) >> 4) & 0x3ffc0ff)
  155. r3 = uint64((binary.LittleEndian.Uint32(key[9:]) >> 6) & 0x3f03fff)
  156. r4 = uint64((binary.LittleEndian.Uint32(key[12:]) >> 8) & 0x00fffff)
  157. R1, R2, R3, R4 := r1*5, r2*5, r3*5, r4*5
  158. var in [poly1305.TagSize]byte
  159. for {
  160. n, err := msg.Read(in[:])
  161. if n == poly1305.TagSize {
  162. // h += msg
  163. h0 += binary.LittleEndian.Uint32(in[0:]) & 0x3ffffff
  164. h1 += (binary.LittleEndian.Uint32(in[3:]) >> 2) & 0x3ffffff
  165. h2 += (binary.LittleEndian.Uint32(in[6:]) >> 4) & 0x3ffffff
  166. h3 += (binary.LittleEndian.Uint32(in[9:]) >> 6) & 0x3ffffff
  167. h4 += (binary.LittleEndian.Uint32(in[12:]) >> 8) | (1 << 24)
  168. } else if n > 0 {
  169. in[n] = 0x01
  170. for i := n + 1; i < poly1305.TagSize; i++ {
  171. in[i] = 0
  172. }
  173. // h += msg
  174. h0 += binary.LittleEndian.Uint32(in[0:]) & 0x3ffffff
  175. h1 += (binary.LittleEndian.Uint32(in[3:]) >> 2) & 0x3ffffff
  176. h2 += (binary.LittleEndian.Uint32(in[6:]) >> 4) & 0x3ffffff
  177. h3 += (binary.LittleEndian.Uint32(in[9:]) >> 6) & 0x3ffffff
  178. h4 += (binary.LittleEndian.Uint32(in[12:]) >> 8)
  179. }
  180. if n > 0 {
  181. // h *= r
  182. d0 := (uint64(h0) * r0) + (uint64(h1) * R4) + (uint64(h2) * R3) + (uint64(h3) * R2) + (uint64(h4) * R1)
  183. d1 := (d0 >> 26) + (uint64(h0) * r1) + (uint64(h1) * r0) + (uint64(h2) * R4) + (uint64(h3) * R3) + (uint64(h4) * R2)
  184. d2 := (d1 >> 26) + (uint64(h0) * r2) + (uint64(h1) * r1) + (uint64(h2) * r0) + (uint64(h3) * R4) + (uint64(h4) * R3)
  185. d3 := (d2 >> 26) + (uint64(h0) * r3) + (uint64(h1) * r2) + (uint64(h2) * r1) + (uint64(h3) * r0) + (uint64(h4) * R4)
  186. d4 := (d3 >> 26) + (uint64(h0) * r4) + (uint64(h1) * r3) + (uint64(h2) * r2) + (uint64(h3) * r1) + (uint64(h4) * r0)
  187. // h %= p
  188. h0 = uint32(d0) & 0x3ffffff
  189. h1 = uint32(d1) & 0x3ffffff
  190. h2 = uint32(d2) & 0x3ffffff
  191. h3 = uint32(d3) & 0x3ffffff
  192. h4 = uint32(d4) & 0x3ffffff
  193. h0 += uint32(d4>>26) * 5
  194. h1 += h0 >> 26
  195. h0 = h0 & 0x3ffffff
  196. }
  197. if err == io.EOF {
  198. break
  199. }
  200. if err != nil {
  201. return err
  202. }
  203. }
  204. // h %= p reduction
  205. h2 += h1 >> 26
  206. h1 &= 0x3ffffff
  207. h3 += h2 >> 26
  208. h2 &= 0x3ffffff
  209. h4 += h3 >> 26
  210. h3 &= 0x3ffffff
  211. h0 += 5 * (h4 >> 26)
  212. h4 &= 0x3ffffff
  213. h1 += h0 >> 26
  214. h0 &= 0x3ffffff
  215. // h - p
  216. t0 := h0 + 5
  217. t1 := h1 + (t0 >> 26)
  218. t2 := h2 + (t1 >> 26)
  219. t3 := h3 + (t2 >> 26)
  220. t4 := h4 + (t3 >> 26) - (1 << 26)
  221. t0 &= 0x3ffffff
  222. t1 &= 0x3ffffff
  223. t2 &= 0x3ffffff
  224. t3 &= 0x3ffffff
  225. // select h if h < p else h - p
  226. t_mask := (t4 >> 31) - 1
  227. h_mask := ^t_mask
  228. h0 = (h0 & h_mask) | (t0 & t_mask)
  229. h1 = (h1 & h_mask) | (t1 & t_mask)
  230. h2 = (h2 & h_mask) | (t2 & t_mask)
  231. h3 = (h3 & h_mask) | (t3 & t_mask)
  232. h4 = (h4 & h_mask) | (t4 & t_mask)
  233. // h %= 2^128
  234. h0 |= h1 << 26
  235. h1 = ((h1 >> 6) | (h2 << 20))
  236. h2 = ((h2 >> 12) | (h3 << 14))
  237. h3 = ((h3 >> 18) | (h4 << 8))
  238. // s: the s part of the key
  239. // tag = (h + s) % (2^128)
  240. t := uint64(h0) + uint64(binary.LittleEndian.Uint32(key[16:]))
  241. h0 = uint32(t)
  242. t = uint64(h1) + uint64(binary.LittleEndian.Uint32(key[20:])) + (t >> 32)
  243. h1 = uint32(t)
  244. t = uint64(h2) + uint64(binary.LittleEndian.Uint32(key[24:])) + (t >> 32)
  245. h2 = uint32(t)
  246. t = uint64(h3) + uint64(binary.LittleEndian.Uint32(key[28:])) + (t >> 32)
  247. h3 = uint32(t)
  248. binary.LittleEndian.PutUint32(out[0:], h0)
  249. binary.LittleEndian.PutUint32(out[4:], h1)
  250. binary.LittleEndian.PutUint32(out[8:], h2)
  251. binary.LittleEndian.PutUint32(out[12:], h3)
  252. return nil
  253. }
  254. // core from x/crypto/salsa20/salsa/salsa20_ref.go.
  255. func salsa20Core(out *[64]byte, in *[16]byte, k *[32]byte, c *[16]byte) {
  256. j0 := uint32(c[0]) | uint32(c[1])<<8 | uint32(c[2])<<16 | uint32(c[3])<<24
  257. j1 := uint32(k[0]) | uint32(k[1])<<8 | uint32(k[2])<<16 | uint32(k[3])<<24
  258. j2 := uint32(k[4]) | uint32(k[5])<<8 | uint32(k[6])<<16 | uint32(k[7])<<24
  259. j3 := uint32(k[8]) | uint32(k[9])<<8 | uint32(k[10])<<16 | uint32(k[11])<<24
  260. j4 := uint32(k[12]) | uint32(k[13])<<8 | uint32(k[14])<<16 | uint32(k[15])<<24
  261. j5 := uint32(c[4]) | uint32(c[5])<<8 | uint32(c[6])<<16 | uint32(c[7])<<24
  262. j6 := uint32(in[0]) | uint32(in[1])<<8 | uint32(in[2])<<16 | uint32(in[3])<<24
  263. j7 := uint32(in[4]) | uint32(in[5])<<8 | uint32(in[6])<<16 | uint32(in[7])<<24
  264. j8 := uint32(in[8]) | uint32(in[9])<<8 | uint32(in[10])<<16 | uint32(in[11])<<24
  265. j9 := uint32(in[12]) | uint32(in[13])<<8 | uint32(in[14])<<16 | uint32(in[15])<<24
  266. j10 := uint32(c[8]) | uint32(c[9])<<8 | uint32(c[10])<<16 | uint32(c[11])<<24
  267. j11 := uint32(k[16]) | uint32(k[17])<<8 | uint32(k[18])<<16 | uint32(k[19])<<24
  268. j12 := uint32(k[20]) | uint32(k[21])<<8 | uint32(k[22])<<16 | uint32(k[23])<<24
  269. j13 := uint32(k[24]) | uint32(k[25])<<8 | uint32(k[26])<<16 | uint32(k[27])<<24
  270. j14 := uint32(k[28]) | uint32(k[29])<<8 | uint32(k[30])<<16 | uint32(k[31])<<24
  271. j15 := uint32(c[12]) | uint32(c[13])<<8 | uint32(c[14])<<16 | uint32(c[15])<<24
  272. x0, x1, x2, x3, x4, x5, x6, x7, x8 := j0, j1, j2, j3, j4, j5, j6, j7, j8
  273. x9, x10, x11, x12, x13, x14, x15 := j9, j10, j11, j12, j13, j14, j15
  274. const rounds = 20
  275. for i := 0; i < rounds; i += 2 {
  276. u := x0 + x12
  277. x4 ^= u<<7 | u>>(32-7)
  278. u = x4 + x0
  279. x8 ^= u<<9 | u>>(32-9)
  280. u = x8 + x4
  281. x12 ^= u<<13 | u>>(32-13)
  282. u = x12 + x8
  283. x0 ^= u<<18 | u>>(32-18)
  284. u = x5 + x1
  285. x9 ^= u<<7 | u>>(32-7)
  286. u = x9 + x5
  287. x13 ^= u<<9 | u>>(32-9)
  288. u = x13 + x9
  289. x1 ^= u<<13 | u>>(32-13)
  290. u = x1 + x13
  291. x5 ^= u<<18 | u>>(32-18)
  292. u = x10 + x6
  293. x14 ^= u<<7 | u>>(32-7)
  294. u = x14 + x10
  295. x2 ^= u<<9 | u>>(32-9)
  296. u = x2 + x14
  297. x6 ^= u<<13 | u>>(32-13)
  298. u = x6 + x2
  299. x10 ^= u<<18 | u>>(32-18)
  300. u = x15 + x11
  301. x3 ^= u<<7 | u>>(32-7)
  302. u = x3 + x15
  303. x7 ^= u<<9 | u>>(32-9)
  304. u = x7 + x3
  305. x11 ^= u<<13 | u>>(32-13)
  306. u = x11 + x7
  307. x15 ^= u<<18 | u>>(32-18)
  308. u = x0 + x3
  309. x1 ^= u<<7 | u>>(32-7)
  310. u = x1 + x0
  311. x2 ^= u<<9 | u>>(32-9)
  312. u = x2 + x1
  313. x3 ^= u<<13 | u>>(32-13)
  314. u = x3 + x2
  315. x0 ^= u<<18 | u>>(32-18)
  316. u = x5 + x4
  317. x6 ^= u<<7 | u>>(32-7)
  318. u = x6 + x5
  319. x7 ^= u<<9 | u>>(32-9)
  320. u = x7 + x6
  321. x4 ^= u<<13 | u>>(32-13)
  322. u = x4 + x7
  323. x5 ^= u<<18 | u>>(32-18)
  324. u = x10 + x9
  325. x11 ^= u<<7 | u>>(32-7)
  326. u = x11 + x10
  327. x8 ^= u<<9 | u>>(32-9)
  328. u = x8 + x11
  329. x9 ^= u<<13 | u>>(32-13)
  330. u = x9 + x8
  331. x10 ^= u<<18 | u>>(32-18)
  332. u = x15 + x14
  333. x12 ^= u<<7 | u>>(32-7)
  334. u = x12 + x15
  335. x13 ^= u<<9 | u>>(32-9)
  336. u = x13 + x12
  337. x14 ^= u<<13 | u>>(32-13)
  338. u = x14 + x13
  339. x15 ^= u<<18 | u>>(32-18)
  340. }
  341. x0 += j0
  342. x1 += j1
  343. x2 += j2
  344. x3 += j3
  345. x4 += j4
  346. x5 += j5
  347. x6 += j6
  348. x7 += j7
  349. x8 += j8
  350. x9 += j9
  351. x10 += j10
  352. x11 += j11
  353. x12 += j12
  354. x13 += j13
  355. x14 += j14
  356. x15 += j15
  357. out[0] = byte(x0)
  358. out[1] = byte(x0 >> 8)
  359. out[2] = byte(x0 >> 16)
  360. out[3] = byte(x0 >> 24)
  361. out[4] = byte(x1)
  362. out[5] = byte(x1 >> 8)
  363. out[6] = byte(x1 >> 16)
  364. out[7] = byte(x1 >> 24)
  365. out[8] = byte(x2)
  366. out[9] = byte(x2 >> 8)
  367. out[10] = byte(x2 >> 16)
  368. out[11] = byte(x2 >> 24)
  369. out[12] = byte(x3)
  370. out[13] = byte(x3 >> 8)
  371. out[14] = byte(x3 >> 16)
  372. out[15] = byte(x3 >> 24)
  373. out[16] = byte(x4)
  374. out[17] = byte(x4 >> 8)
  375. out[18] = byte(x4 >> 16)
  376. out[19] = byte(x4 >> 24)
  377. out[20] = byte(x5)
  378. out[21] = byte(x5 >> 8)
  379. out[22] = byte(x5 >> 16)
  380. out[23] = byte(x5 >> 24)
  381. out[24] = byte(x6)
  382. out[25] = byte(x6 >> 8)
  383. out[26] = byte(x6 >> 16)
  384. out[27] = byte(x6 >> 24)
  385. out[28] = byte(x7)
  386. out[29] = byte(x7 >> 8)
  387. out[30] = byte(x7 >> 16)
  388. out[31] = byte(x7 >> 24)
  389. out[32] = byte(x8)
  390. out[33] = byte(x8 >> 8)
  391. out[34] = byte(x8 >> 16)
  392. out[35] = byte(x8 >> 24)
  393. out[36] = byte(x9)
  394. out[37] = byte(x9 >> 8)
  395. out[38] = byte(x9 >> 16)
  396. out[39] = byte(x9 >> 24)
  397. out[40] = byte(x10)
  398. out[41] = byte(x10 >> 8)
  399. out[42] = byte(x10 >> 16)
  400. out[43] = byte(x10 >> 24)
  401. out[44] = byte(x11)
  402. out[45] = byte(x11 >> 8)
  403. out[46] = byte(x11 >> 16)
  404. out[47] = byte(x11 >> 24)
  405. out[48] = byte(x12)
  406. out[49] = byte(x12 >> 8)
  407. out[50] = byte(x12 >> 16)
  408. out[51] = byte(x12 >> 24)
  409. out[52] = byte(x13)
  410. out[53] = byte(x13 >> 8)
  411. out[54] = byte(x13 >> 16)
  412. out[55] = byte(x13 >> 24)
  413. out[56] = byte(x14)
  414. out[57] = byte(x14 >> 8)
  415. out[58] = byte(x14 >> 16)
  416. out[59] = byte(x14 >> 24)
  417. out[60] = byte(x15)
  418. out[61] = byte(x15 >> 8)
  419. out[62] = byte(x15 >> 16)
  420. out[63] = byte(x15 >> 24)
  421. }