Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / crypto / block / ofb.go
blob11aaaa4d71947274496c85830ec43c38d8135dad
1 // Copyright 2009 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 // Output feedback (OFB) mode.
7 // OFB converts a block cipher into a stream cipher by
8 // repeatedly encrypting an initialization vector and
9 // xoring the resulting stream of data with the input.
11 // See NIST SP 800-38A, pp 13-15
13 package block
15 import (
16 "fmt"
17 "io"
20 type ofbStream struct {
21 c Cipher
22 iv []byte
25 func newOFBStream(c Cipher, iv []byte) *ofbStream {
26 x := new(ofbStream)
27 x.c = c
28 n := len(iv)
29 if n != c.BlockSize() {
30 panic(fmt.Sprintln("crypto/block: newOFBStream: invalid iv size", n, "!=", c.BlockSize()))
32 x.iv = dup(iv)
33 return x
36 func (x *ofbStream) Next() []byte {
37 x.c.Encrypt(x.iv, x.iv)
38 return x.iv
41 // NewOFBReader returns a reader that reads data from r, decrypts (or encrypts)
42 // it using c in output feedback (OFB) mode with the initialization vector iv.
43 // The returned Reader does not buffer and has no block size.
44 // In OFB mode, encryption and decryption are the same operation:
45 // an OFB reader applied to an encrypted stream produces a decrypted
46 // stream and vice versa.
47 func NewOFBReader(c Cipher, iv []byte, r io.Reader) io.Reader {
48 return newXorReader(newOFBStream(c, iv), r)
51 // NewOFBWriter returns a writer that encrypts (or decrypts) data using c
52 // in cipher feedback (OFB) mode with the initialization vector iv
53 // and writes the encrypted data to w.
54 // The returned Writer does not buffer and has no block size.
55 // In OFB mode, encryption and decryption are the same operation:
56 // an OFB writer applied to an decrypted stream produces an encrypted
57 // stream and vice versa.
58 func NewOFBWriter(c Cipher, iv []byte, w io.Writer) io.Writer {
59 return newXorWriter(newOFBStream(c, iv), w)