Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

59 linhas
2.0 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 salsa20 implements the Salsa20 stream cipher as specified in https://cr.yp.to/snuffle/spec.pdf.
  6. Salsa20 differs from many other stream ciphers in that it is message orientated
  7. rather than byte orientated. Keystream blocks are not preserved between calls,
  8. therefore each side must encrypt/decrypt data with the same segmentation.
  9. Another aspect of this difference is that part of the counter is exposed as
  10. a nonce in each call. Encrypting two different messages with the same (key,
  11. nonce) pair leads to trivial plaintext recovery. This is analogous to
  12. encrypting two different messages with the same key with a traditional stream
  13. cipher.
  14. This package also implements XSalsa20: a version of Salsa20 with a 24-byte
  15. nonce as specified in https://cr.yp.to/snuffle/xsalsa-20081128.pdf. Simply
  16. passing a 24-byte slice as the nonce triggers XSalsa20.
  17. */
  18. package salsa20 // import "golang.org/x/crypto/salsa20"
  19. // TODO(agl): implement XORKeyStream12 and XORKeyStream8 - the reduced round variants of Salsa20.
  20. import (
  21. "golang.org/x/crypto/internal/subtle"
  22. "golang.org/x/crypto/salsa20/salsa"
  23. )
  24. // XORKeyStream crypts bytes from in to out using the given key and nonce.
  25. // In and out must overlap entirely or not at all. Nonce must
  26. // be either 8 or 24 bytes long.
  27. func XORKeyStream(out, in []byte, nonce []byte, key *[32]byte) {
  28. if len(out) < len(in) {
  29. panic("salsa20: output smaller than input")
  30. }
  31. if subtle.InexactOverlap(out[:len(in)], in) {
  32. panic("salsa20: invalid buffer overlap")
  33. }
  34. var subNonce [16]byte
  35. if len(nonce) == 24 {
  36. var subKey [32]byte
  37. var hNonce [16]byte
  38. copy(hNonce[:], nonce[:16])
  39. salsa.HSalsa20(&subKey, &hNonce, key, &salsa.Sigma)
  40. copy(subNonce[:], nonce[16:])
  41. key = &subKey
  42. } else if len(nonce) == 8 {
  43. copy(subNonce[:], nonce[:])
  44. } else {
  45. panic("salsa20: nonce must be 8 or 24 bytes")
  46. }
  47. salsa.XORKeyStream(out, in, &subNonce, key)
  48. }