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.

103 lines
3.9 KiB

  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package box authenticates and encrypts small messages using public-key cryptography.
  6. Box uses Curve25519, XSalsa20 and Poly1305 to encrypt and authenticate
  7. messages. The length of messages is not hidden.
  8. It is the caller's responsibility to ensure the uniqueness of noncesfor
  9. example, by using nonce 1 for the first message, nonce 2 for the second
  10. message, etc. Nonces are long enough that randomly generated nonces have
  11. negligible risk of collision.
  12. Messages should be small because:
  13. 1. The whole message needs to be held in memory to be processed.
  14. 2. Using large messages pressures implementations on small machines to decrypt
  15. and process plaintext before authenticating it. This is very dangerous, and
  16. this API does not allow it, but a protocol that uses excessive message sizes
  17. might present some implementations with no other choice.
  18. 3. Fixed overheads will be sufficiently amortised by messages as small as 8KB.
  19. 4. Performance may be improved by working with messages that fit into data caches.
  20. Thus large amounts of data should be chunked so that each message is small.
  21. (Each message still needs a unique nonce.) If in doubt, 16KB is a reasonable
  22. chunk size.
  23. This package is interoperable with NaCl: https://nacl.cr.yp.to/box.html.
  24. */
  25. package box // import "golang.org/x/crypto/nacl/box"
  26. import (
  27. "io"
  28. "golang.org/x/crypto/curve25519"
  29. "golang.org/x/crypto/nacl/secretbox"
  30. "golang.org/x/crypto/salsa20/salsa"
  31. )
  32. // Overhead is the number of bytes of overhead when boxing a message.
  33. const Overhead = secretbox.Overhead
  34. // GenerateKey generates a new public/private key pair suitable for use with
  35. // Seal and Open.
  36. func GenerateKey(rand io.Reader) (publicKey, privateKey *[32]byte, err error) {
  37. publicKey = new([32]byte)
  38. privateKey = new([32]byte)
  39. _, err = io.ReadFull(rand, privateKey[:])
  40. if err != nil {
  41. publicKey = nil
  42. privateKey = nil
  43. return
  44. }
  45. curve25519.ScalarBaseMult(publicKey, privateKey)
  46. return
  47. }
  48. var zeros [16]byte
  49. // Precompute calculates the shared key between peersPublicKey and privateKey
  50. // and writes it to sharedKey. The shared key can be used with
  51. // OpenAfterPrecomputation and SealAfterPrecomputation to speed up processing
  52. // when using the same pair of keys repeatedly.
  53. func Precompute(sharedKey, peersPublicKey, privateKey *[32]byte) {
  54. curve25519.ScalarMult(sharedKey, privateKey, peersPublicKey)
  55. salsa.HSalsa20(sharedKey, &zeros, sharedKey, &salsa.Sigma)
  56. }
  57. // Seal appends an encrypted and authenticated copy of message to out, which
  58. // will be Overhead bytes longer than the original and must not overlap it. The
  59. // nonce must be unique for each distinct message for a given pair of keys.
  60. func Seal(out, message []byte, nonce *[24]byte, peersPublicKey, privateKey *[32]byte) []byte {
  61. var sharedKey [32]byte
  62. Precompute(&sharedKey, peersPublicKey, privateKey)
  63. return secretbox.Seal(out, message, nonce, &sharedKey)
  64. }
  65. // SealAfterPrecomputation performs the same actions as Seal, but takes a
  66. // shared key as generated by Precompute.
  67. func SealAfterPrecomputation(out, message []byte, nonce *[24]byte, sharedKey *[32]byte) []byte {
  68. return secretbox.Seal(out, message, nonce, sharedKey)
  69. }
  70. // Open authenticates and decrypts a box produced by Seal and appends the
  71. // message to out, which must not overlap box. The output will be Overhead
  72. // bytes smaller than box.
  73. func Open(out, box []byte, nonce *[24]byte, peersPublicKey, privateKey *[32]byte) ([]byte, bool) {
  74. var sharedKey [32]byte
  75. Precompute(&sharedKey, peersPublicKey, privateKey)
  76. return secretbox.Open(out, box, nonce, &sharedKey)
  77. }
  78. // OpenAfterPrecomputation performs the same actions as Open, but takes a
  79. // shared key as generated by Precompute.
  80. func OpenAfterPrecomputation(out, box []byte, nonce *[24]byte, sharedKey *[32]byte) ([]byte, bool) {
  81. return secretbox.Open(out, box, nonce, sharedKey)
  82. }