libgo: update to Go 1.11
[official-gcc.git] / libgo / go / crypto / cipher / ofb.go
blobfc477248658c8d83c9b360faeae2ba3830d55c32
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.
7 package cipher
9 import "crypto/internal/subtle"
11 type ofb struct {
12 b Block
13 cipher []byte
14 out []byte
15 outUsed int
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
20 // to b's block size.
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 {
28 bufSize = blockSize
30 x := &ofb{
31 b: b,
32 cipher: make([]byte, blockSize),
33 out: make([]byte, 0, bufSize),
34 outUsed: 0,
37 copy(x.cipher, iv)
38 return x
41 func (x *ofb) refill() {
42 bs := x.b.BlockSize()
43 remain := len(x.out) - x.outUsed
44 if remain > x.outUsed {
45 return
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)
52 remain += bs
54 x.out = x.out[:remain]
55 x.outUsed = 0
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")
65 for len(src) > 0 {
66 if x.outUsed >= len(x.out)-x.b.BlockSize() {
67 x.refill()
69 n := xorBytes(dst, src, x.out[x.outUsed:])
70 dst = dst[n:]
71 src = src[n:]
72 x.outUsed += n