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.
16 // NewOFB returns a Stream that encrypts or decrypts using the block cipher b
17 // in output feedback mode. The initialization vector iv's length must be equal
19 func NewOFB(b Block
, iv
[]byte) Stream
{
20 blockSize
:= b
.BlockSize()
21 if len(iv
) != blockSize
{
24 bufSize
:= streamBufferSize
25 if bufSize
< blockSize
{
30 cipher
: make([]byte, blockSize
),
31 out
: make([]byte, 0, bufSize
),
39 func (x
*ofb
) refill() {
41 remain
:= len(x
.out
) - x
.outUsed
42 if remain
> x
.outUsed
{
45 copy(x
.out
, x
.out
[x
.outUsed
:])
46 x
.out
= x
.out
[:cap(x
.out
)]
47 for remain
< len(x
.out
)-bs
{
48 x
.b
.Encrypt(x
.cipher
, x
.cipher
)
49 copy(x
.out
[remain
:], x
.cipher
)
52 x
.out
= x
.out
[:remain
]
56 func (x
*ofb
) XORKeyStream(dst
, src
[]byte) {
58 if x
.outUsed
>= len(x
.out
)-x
.b
.BlockSize() {
61 n
:= xorBytes(dst
, src
, x
.out
[x
.outUsed
:])