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.
5 // CFB (Cipher Feedback) Mode.
16 // NewCFBEncrypter returns a Stream which encrypts with cipher feedback mode,
17 // using the given Block. The iv must be the same length as the Block's block
19 func NewCFBEncrypter(block Block, iv []byte) Stream {
20 if len(iv) != block.BlockSize() {
21 panic("cipher.NewCBFEncrypter: IV length must equal block size")
23 return newCFB(block, iv, false)
26 // NewCFBDecrypter returns a Stream which decrypts with cipher feedback mode,
27 // using the given Block. The iv must be the same length as the Block's block
29 func NewCFBDecrypter(block Block, iv []byte) Stream {
30 if len(iv) != block.BlockSize() {
31 panic("cipher.NewCBFEncrypter: IV length must equal block size")
33 return newCFB(block, iv, true)
36 func newCFB(block Block, iv []byte, decrypt bool) Stream {
37 blockSize := block.BlockSize()
38 if len(iv) != blockSize {
44 out: make([]byte, blockSize),
48 block.Encrypt(x.out, iv)
53 func (x *cfb) XORKeyStream(dst, src []byte) {
54 for i := 0; i < len(src); i++ {
55 if x.outUsed == len(x.out) {
56 x.b.Encrypt(x.out, x.out)
62 dst[i] = src[i] ^ x.out[x.outUsed]
65 x.out[x.outUsed] ^= src[i]
66 dst[i] = x.out[x.outUsed]