Source file src/pkg/crypto/cipher/ofb.go
1 // Copyright 2011 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 // OFB (Output Feedback) Mode.
6
7 package cipher
8
9 type ofb struct {
10 b Block
11 out []byte
12 outUsed int
13 }
14
15 // NewOFB returns a Stream that encrypts or decrypts using the block cipher b
16 // in output feedback mode. The initialization vector iv's length must be equal
17 // to b's block size.
18 func NewOFB(b Block, iv []byte) Stream {
19 blockSize := b.BlockSize()
20 if len(iv) != blockSize {
21 return nil
22 }
23
24 x := &ofb{
25 b: b,
26 out: make([]byte, blockSize),
27 outUsed: 0,
28 }
29 b.Encrypt(x.out, iv)
30
31 return x
32 }
33
34 func (x *ofb) XORKeyStream(dst, src []byte) {
35 for i, s := range src {
36 if x.outUsed == len(x.out) {
37 x.b.Encrypt(x.out, x.out)
38 x.outUsed = 0
39 }
40
41 dst[i] = s ^ x.out[x.outUsed]
42 x.outUsed++
43 }
44 }