1
0

cipher.go 3.0 KB

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