Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 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 | |
| 5 | // Package blowfish implements Bruce Schneier's Blowfish encryption algorithm. |
David Symonds | 1fbbd62 | 2014-12-09 13:38:15 +1100 | [diff] [blame] | 6 | package blowfish // import "golang.org/x/crypto/blowfish" |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 7 | |
| 8 | // The code is a port of Bruce Schneier's C implementation. |
| 9 | // See http://www.schneier.com/blowfish.html. |
| 10 | |
| 11 | import "strconv" |
| 12 | |
| 13 | // The Blowfish block size in bytes. |
| 14 | const BlockSize = 8 |
| 15 | |
| 16 | // A Cipher is an instance of Blowfish encryption using a particular key. |
| 17 | type Cipher struct { |
| 18 | p [18]uint32 |
| 19 | s0, s1, s2, s3 [256]uint32 |
| 20 | } |
| 21 | |
| 22 | type KeySizeError int |
| 23 | |
| 24 | func (k KeySizeError) Error() string { |
| 25 | return "crypto/blowfish: invalid key size " + strconv.Itoa(int(k)) |
| 26 | } |
| 27 | |
| 28 | // NewCipher creates and returns a Cipher. |
Adam Langley | 8f45c68 | 2014-03-31 13:41:02 -0400 | [diff] [blame] | 29 | // The key argument should be the Blowfish key, from 1 to 56 bytes. |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 30 | func NewCipher(key []byte) (*Cipher, error) { |
| 31 | var result Cipher |
Adam Langley | 8f45c68 | 2014-03-31 13:41:02 -0400 | [diff] [blame] | 32 | if k := len(key); k < 1 || k > 56 { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 33 | return nil, KeySizeError(k) |
| 34 | } |
Dmitry Chestnykh | c261757 | 2014-05-05 11:42:21 -0700 | [diff] [blame] | 35 | initCipher(&result) |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 36 | ExpandKey(key, &result) |
| 37 | return &result, nil |
| 38 | } |
| 39 | |
| 40 | // NewSaltedCipher creates a returns a Cipher that folds a salt into its key |
| 41 | // schedule. For most purposes, NewCipher, instead of NewSaltedCipher, is |
Joe Farrell | 77f4136 | 2016-06-07 11:36:12 +0100 | [diff] [blame] | 42 | // sufficient and desirable. For bcrypt compatibility, the key can be over 56 |
Dmitry Chestnykh | b7f382b | 2014-08-01 14:09:20 -0700 | [diff] [blame] | 43 | // bytes. |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 44 | func NewSaltedCipher(key, salt []byte) (*Cipher, error) { |
Dmitry Chestnykh | b7f382b | 2014-08-01 14:09:20 -0700 | [diff] [blame] | 45 | if len(salt) == 0 { |
| 46 | return NewCipher(key) |
| 47 | } |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 48 | var result Cipher |
Adam Langley | 8f45c68 | 2014-03-31 13:41:02 -0400 | [diff] [blame] | 49 | if k := len(key); k < 1 { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 50 | return nil, KeySizeError(k) |
| 51 | } |
Dmitry Chestnykh | c261757 | 2014-05-05 11:42:21 -0700 | [diff] [blame] | 52 | initCipher(&result) |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 53 | expandKeyWithSalt(key, salt, &result) |
| 54 | return &result, nil |
| 55 | } |
| 56 | |
| 57 | // BlockSize returns the Blowfish block size, 8 bytes. |
| 58 | // It is necessary to satisfy the Block interface in the |
| 59 | // package "crypto/cipher". |
| 60 | func (c *Cipher) BlockSize() int { return BlockSize } |
| 61 | |
| 62 | // Encrypt encrypts the 8-byte buffer src using the key k |
| 63 | // and stores the result in dst. |
| 64 | // Note that for amounts of data larger than a block, |
| 65 | // it is not safe to just call Encrypt on successive blocks; |
| 66 | // instead, use an encryption mode like CBC (see crypto/cipher/cbc.go). |
| 67 | func (c *Cipher) Encrypt(dst, src []byte) { |
| 68 | l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3]) |
| 69 | r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7]) |
| 70 | l, r = encryptBlock(l, r, c) |
| 71 | dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l) |
| 72 | dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r) |
| 73 | } |
| 74 | |
| 75 | // Decrypt decrypts the 8-byte buffer src using the key k |
| 76 | // and stores the result in dst. |
| 77 | func (c *Cipher) Decrypt(dst, src []byte) { |
| 78 | l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3]) |
| 79 | r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7]) |
| 80 | l, r = decryptBlock(l, r, c) |
| 81 | dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l) |
| 82 | dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r) |
| 83 | } |
| 84 | |
Dmitry Chestnykh | c261757 | 2014-05-05 11:42:21 -0700 | [diff] [blame] | 85 | func initCipher(c *Cipher) { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 86 | copy(c.p[0:], p[0:]) |
| 87 | copy(c.s0[0:], s0[0:]) |
| 88 | copy(c.s1[0:], s1[0:]) |
| 89 | copy(c.s2[0:], s2[0:]) |
| 90 | copy(c.s3[0:], s3[0:]) |
| 91 | } |