summaryrefslogtreecommitdiff
path: root/libgo/go/crypto/cipher/ofb.go
blob: 85e5f02b0a64e4c7eca2db7351c9f48ae4dfaa71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// OFB (Output Feedback) Mode.

package cipher

type ofb struct {
	b       Block
	out     []byte
	outUsed int
}

// NewOFB returns a Stream that encrypts or decrypts using the block cipher b
// in output feedback mode. The initialization vector iv's length must be equal
// to b's block size.
func NewOFB(b Block, iv []byte) Stream {
	blockSize := b.BlockSize()
	if len(iv) != blockSize {
		return nil
	}

	x := &ofb{
		b:       b,
		out:     make([]byte, blockSize),
		outUsed: 0,
	}
	b.Encrypt(x.out, iv)

	return x
}

func (x *ofb) XORKeyStream(dst, src []byte) {
	for i, s := range src {
		if x.outUsed == len(x.out) {
			x.b.Encrypt(x.out, x.out)
			x.outUsed = 0
		}

		dst[i] = s ^ x.out[x.outUsed]
		x.outUsed++
	}
}