cipher.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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, 4 to 56 bytes.
  22. func NewCipher(key []byte) (*Cipher, error) {
  23. var result Cipher
  24. k := len(key)
  25. if k < 4 || k > 56 {
  26. return nil, KeySizeError(k)
  27. }
  28. initCipher(key, &result)
  29. ExpandKey(key, &result)
  30. return &result, nil
  31. }
  32. // NewSaltedCipher creates a returns a Cipher that folds a salt into its key
  33. // schedule. For most purposes, NewCipher, instead of NewSaltedCipher, is
  34. // sufficient and desirable. For bcrypt compatiblity, the key can be over 56
  35. // bytes. Only the first 16 bytes of salt are used.
  36. func NewSaltedCipher(key, salt []byte) (*Cipher, error) {
  37. var result Cipher
  38. k := len(key)
  39. if k < 4 {
  40. return nil, KeySizeError(k)
  41. }
  42. initCipher(key, &result)
  43. expandKeyWithSalt(key, salt, &result)
  44. return &result, nil
  45. }
  46. // BlockSize returns the Blowfish block size, 8 bytes.
  47. // It is necessary to satisfy the Block interface in the
  48. // package "crypto/cipher".
  49. func (c *Cipher) BlockSize() int { return BlockSize }
  50. // Encrypt encrypts the 8-byte buffer src using the key k
  51. // and stores the result in dst.
  52. // Note that for amounts of data larger than a block,
  53. // it is not safe to just call Encrypt on successive blocks;
  54. // instead, use an encryption mode like CBC (see crypto/cipher/cbc.go).
  55. func (c *Cipher) Encrypt(dst, src []byte) {
  56. l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
  57. r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
  58. l, r = encryptBlock(l, r, c)
  59. dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
  60. dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r)
  61. }
  62. // Decrypt decrypts the 8-byte buffer src using the key k
  63. // and stores the result in dst.
  64. func (c *Cipher) Decrypt(dst, src []byte) {
  65. l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
  66. r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
  67. l, r = decryptBlock(l, r, c)
  68. dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
  69. dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r)
  70. }
  71. func initCipher(key []byte, c *Cipher) {
  72. copy(c.p[0:], p[0:])
  73. copy(c.s0[0:], s0[0:])
  74. copy(c.s1[0:], s1[0:])
  75. copy(c.s2[0:], s2[0:])
  76. copy(c.s3[0:], s3[0:])
  77. }