Copyright 2010 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.
OpenPGP CFB Mode. http://tools.ietf.org/html/rfc4880#section-13.9

package packet

import (
	
)

type ocfbEncrypter struct {
	b       cipher.Block
	fre     []byte
	outUsed int
}
An OCFBResyncOption determines if the "resynchronization step" of OCFB is performed.
NewOCFBEncrypter returns a cipher.Stream which encrypts data with OpenPGP's cipher feedback mode using the given cipher.Block, and an initial amount of ciphertext. randData must be random bytes and be the same length as the cipher.Block's block size. Resync determines if the "resynchronization step" from RFC 4880, 13.9 step 7 is performed. Different parts of OpenPGP vary on this point.
func ( cipher.Block,  []byte,  OCFBResyncOption) (cipher.Stream, []byte) {
	 := .BlockSize()
	if len() !=  {
		return nil, nil
	}

	 := &ocfbEncrypter{
		b:       ,
		fre:     make([]byte, ),
		outUsed: 0,
	}
	 := make([]byte, +2)

	.Encrypt(.fre, .fre)
	for  := 0;  < ; ++ {
		[] = [] ^ .fre[]
	}

	.Encrypt(.fre, [:])
	[] = .fre[0] ^ [-2]
	[+1] = .fre[1] ^ [-1]

	if  {
		.Encrypt(.fre, [2:])
	} else {
		.fre[0] = []
		.fre[1] = [+1]
		.outUsed = 2
	}
	return , 
}

func ( *ocfbEncrypter) (,  []byte) {
	for  := 0;  < len(); ++ {
		if .outUsed == len(.fre) {
			.b.Encrypt(.fre, .fre)
			.outUsed = 0
		}

		.fre[.outUsed] ^= []
		[] = .fre[.outUsed]
		.outUsed++
	}
}

type ocfbDecrypter struct {
	b       cipher.Block
	fre     []byte
	outUsed int
}
NewOCFBDecrypter returns a cipher.Stream which decrypts data with OpenPGP's cipher feedback mode using the given cipher.Block. Prefix must be the first blockSize + 2 bytes of the ciphertext, where blockSize is the cipher.Block's block size. If an incorrect key is detected then nil is returned. On successful exit, blockSize+2 bytes of decrypted data are written into prefix. Resync determines if the "resynchronization step" from RFC 4880, 13.9 step 7 is performed. Different parts of OpenPGP vary on this point.
func ( cipher.Block,  []byte,  OCFBResyncOption) cipher.Stream {
	 := .BlockSize()
	if len() != +2 {
		return nil
	}

	 := &ocfbDecrypter{
		b:       ,
		fre:     make([]byte, ),
		outUsed: 0,
	}
	 := make([]byte, len())
	copy(, )

	.Encrypt(.fre, .fre)
	for  := 0;  < ; ++ {
		[] ^= .fre[]
	}

	.Encrypt(.fre, [:])
	[] ^= .fre[0]
	[+1] ^= .fre[1]

	if [-2] != [] ||
		[-1] != [+1] {
		return nil
	}

	if  {
		.Encrypt(.fre, [2:])
	} else {
		.fre[0] = []
		.fre[1] = [+1]
		.outUsed = 2
	}
	copy(, )
	return 
}

func ( *ocfbDecrypter) (,  []byte) {
	for  := 0;  < len(); ++ {
		if .outUsed == len(.fre) {
			.b.Encrypt(.fre, .fre)
			.outUsed = 0
		}

		 := []
		[] = .fre[.outUsed] ^ []
		.fre[.outUsed] = 
		.outUsed++
	}