blob: d34668433097916ce5f1570e54e49ca864b8738a [file] [log] [blame]
Russ Cox470549d2012-01-25 15:31:12 -05001// 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.
6package blowfish
7
8// The code is a port of Bruce Schneier's C implementation.
9// See http://www.schneier.com/blowfish.html.
10
11import "strconv"
12
13// The Blowfish block size in bytes.
14const BlockSize = 8
15
16// A Cipher is an instance of Blowfish encryption using a particular key.
17type Cipher struct {
18 p [18]uint32
19 s0, s1, s2, s3 [256]uint32
20}
21
22type KeySizeError int
23
24func (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 Langley8f45c682014-03-31 13:41:02 -040029// The key argument should be the Blowfish key, from 1 to 56 bytes.
Russ Cox470549d2012-01-25 15:31:12 -050030func NewCipher(key []byte) (*Cipher, error) {
31 var result Cipher
Adam Langley8f45c682014-03-31 13:41:02 -040032 if k := len(key); k < 1 || k > 56 {
Russ Cox470549d2012-01-25 15:31:12 -050033 return nil, KeySizeError(k)
34 }
35 initCipher(key, &result)
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
42// sufficient and desirable. For bcrypt compatiblity, the key can be over 56
Dmitry Chestnykhdc673542012-10-06 10:03:43 -040043// bytes. Only the first 16 bytes of salt are used.
Russ Cox470549d2012-01-25 15:31:12 -050044func NewSaltedCipher(key, salt []byte) (*Cipher, error) {
45 var result Cipher
Adam Langley8f45c682014-03-31 13:41:02 -040046 if k := len(key); k < 1 {
Russ Cox470549d2012-01-25 15:31:12 -050047 return nil, KeySizeError(k)
48 }
49 initCipher(key, &result)
50 expandKeyWithSalt(key, salt, &result)
51 return &result, nil
52}
53
54// BlockSize returns the Blowfish block size, 8 bytes.
55// It is necessary to satisfy the Block interface in the
56// package "crypto/cipher".
57func (c *Cipher) BlockSize() int { return BlockSize }
58
59// Encrypt encrypts the 8-byte buffer src using the key k
60// and stores the result in dst.
61// Note that for amounts of data larger than a block,
62// it is not safe to just call Encrypt on successive blocks;
63// instead, use an encryption mode like CBC (see crypto/cipher/cbc.go).
64func (c *Cipher) Encrypt(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 = encryptBlock(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
72// Decrypt decrypts the 8-byte buffer src using the key k
73// and stores the result in dst.
74func (c *Cipher) Decrypt(dst, src []byte) {
75 l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
76 r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
77 l, r = decryptBlock(l, r, c)
78 dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
79 dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r)
80}
81
Russ Cox470549d2012-01-25 15:31:12 -050082func initCipher(key []byte, c *Cipher) {
83 copy(c.p[0:], p[0:])
84 copy(c.s0[0:], s0[0:])
85 copy(c.s1[0:], s1[0:])
86 copy(c.s2[0:], s2[0:])
87 copy(c.s3[0:], s3[0:])
88}