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.
5 // OFB (Output Feedback) Mode.
9 import "crypto/internal/subtle"
18 // NewOFB returns a Stream that encrypts or decrypts using the block cipher b
19 // in output feedback mode. The initialization vector iv's length must be equal
21 func NewOFB(b Block
, iv
[]byte) Stream
{
22 blockSize
:= b
.BlockSize()
23 if len(iv
) != blockSize
{
24 panic("cipher.NewOFB: IV length must equal block size")
26 bufSize
:= streamBufferSize
27 if bufSize
< blockSize
{
32 cipher
: make([]byte, blockSize
),
33 out
: make([]byte, 0, bufSize
),
41 func (x
*ofb
) refill() {
43 remain
:= len(x
.out
) - x
.outUsed
44 if remain
> x
.outUsed
{
47 copy(x
.out
, x
.out
[x
.outUsed
:])
48 x
.out
= x
.out
[:cap(x
.out
)]
49 for remain
< len(x
.out
)-bs
{
50 x
.b
.Encrypt(x
.cipher
, x
.cipher
)
51 copy(x
.out
[remain
:], x
.cipher
)
54 x
.out
= x
.out
[:remain
]
58 func (x
*ofb
) XORKeyStream(dst
, src
[]byte) {
59 if len(dst
) < len(src
) {
60 panic("crypto/cipher: output smaller than input")
62 if subtle
.InexactOverlap(dst
[:len(src
)], src
) {
63 panic("crypto/cipher: invalid buffer overlap")
66 if x
.outUsed
>= len(x
.out
)-x
.b
.BlockSize() {
69 n
:= xorBytes(dst
, src
, x
.out
[x
.outUsed
:])