cipher.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 blowfish implements Bruce Schneier's Blowfish encryption algorithm.
  5. package blowfish
  6. // The code is a port of Bruce Schneier's C implementation.
  7. // See http://www.schneier.com/blowfish.html.
  8. import "strconv"
  9. // The Blowfish block size in bytes.
  10. const BlockSize = 8
  11. // A Cipher is an instance of Blowfish encryption using a particular key.
  12. type Cipher struct {
  13. p [18]uint32
  14. s0, s1, s2, s3 [256]uint32
  15. }
  16. type KeySizeError int
  17. func (k KeySizeError) Error() string {
  18. return "crypto/blowfish: invalid key size " + strconv.Itoa(int(k))
  19. }
  20. // NewCipher creates and returns a Cipher.
  21. // The key argument should be the Blowfish key, from 1 to 56 bytes.
  22. func NewCipher(key []byte) (*Cipher, error) {
  23. var result Cipher
  24. if k := len(key); k < 1 || k > 56 {
  25. return nil, KeySizeError(k)
  26. }
  27. initCipher(key, &result)
  28. ExpandKey(key, &result)
  29. return &result, nil
  30. }
  31. // NewSaltedCipher creates a returns a Cipher that folds a salt into its key
  32. // schedule. For most purposes, NewCipher, instead of NewSaltedCipher, is
  33. // sufficient and desirable. For bcrypt compatiblity, the key can be over 56
  34. // bytes. Only the first 16 bytes of salt are used.
  35. func NewSaltedCipher(key, salt []byte) (*Cipher, error) {
  36. var result Cipher
  37. if k := len(key); k < 1 {
  38. return nil, KeySizeError(k)
  39. }
  40. initCipher(key, &result)
  41. expandKeyWithSalt(key, salt, &result)
  42. return &result, nil
  43. }
  44. // BlockSize returns the Blowfish block size, 8 bytes.
  45. // It is necessary to satisfy the Block interface in the
  46. // package "crypto/cipher".
  47. func (c *Cipher) BlockSize() int { return BlockSize }
  48. // Encrypt encrypts the 8-byte buffer src using the key k
  49. // and stores the result in dst.
  50. // Note that for amounts of data larger than a block,
  51. // it is not safe to just call Encrypt on successive blocks;
  52. // instead, use an encryption mode like CBC (see crypto/cipher/cbc.go).
  53. func (c *Cipher) Encrypt(dst, src []byte) {
  54. l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
  55. r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
  56. l, r = encryptBlock(l, r, c)
  57. dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
  58. dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r)
  59. }
  60. // Decrypt decrypts the 8-byte buffer src using the key k
  61. // and stores the result in dst.
  62. func (c *Cipher) Decrypt(dst, src []byte) {
  63. l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
  64. r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
  65. l, r = decryptBlock(l, r, c)
  66. dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
  67. dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r)
  68. }
  69. func initCipher(key []byte, c *Cipher) {
  70. copy(c.p[0:], p[0:])
  71. copy(c.s0[0:], s0[0:])
  72. copy(c.s1[0:], s1[0:])
  73. copy(c.s2[0:], s2[0:])
  74. copy(c.s3[0:], s3[0:])
  75. }