cipher_2.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2010 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. // package aes12 implements standard block cipher modes that can be wrapped
  5. // around low-level block cipher implementations.
  6. // See http://csrc.nist.gov/groups/ST/toolkit/BCM/current_modes.html
  7. // and NIST Special Publication 800-38A.
  8. package aes12
  9. // A Block represents an implementation of block cipher
  10. // using a given key. It provides the capability to encrypt
  11. // or decrypt individual blocks. The mode implementations
  12. // extend that capability to streams of blocks.
  13. type Block interface {
  14. // BlockSize returns the cipher's block size.
  15. BlockSize() int
  16. // Encrypt encrypts the first block in src into dst.
  17. // Dst and src may point at the same memory.
  18. Encrypt(dst, src []byte)
  19. // Decrypt decrypts the first block in src into dst.
  20. // Dst and src may point at the same memory.
  21. Decrypt(dst, src []byte)
  22. }
  23. // A Stream represents a stream cipher.
  24. type Stream interface {
  25. // XORKeyStream XORs each byte in the given slice with a byte from the
  26. // cipher's key stream. Dst and src may point to the same memory.
  27. // If len(dst) < len(src), XORKeyStream should panic. It is acceptable
  28. // to pass a dst bigger than src, and in that case, XORKeyStream will
  29. // only update dst[:len(src)] and will not touch the rest of dst.
  30. XORKeyStream(dst, src []byte)
  31. }
  32. // A BlockMode represents a block cipher running in a block-based mode (CBC,
  33. // ECB etc).
  34. type BlockMode interface {
  35. // BlockSize returns the mode's block size.
  36. BlockSize() int
  37. // CryptBlocks encrypts or decrypts a number of blocks. The length of
  38. // src must be a multiple of the block size. Dst and src may point to
  39. // the same memory.
  40. CryptBlocks(dst, src []byte)
  41. }
  42. // Utility routines
  43. func dup(p []byte) []byte {
  44. q := make([]byte, len(p))
  45. copy(q, p)
  46. return q
  47. }