1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 type CFBEncrypter struct {
23 func (c *Cipher) NewCFBEncrypter(iv []byte) *CFBEncrypter {
24 if len(iv) != BlockSize {
25 panic("iv length is not equal to blocksize")
27 encrypter := CFBEncrypter{c: c, iv: make([]byte, BlockSize)}
28 copy(encrypter.iv, iv)
32 func (c *CFBEncrypter) XORKeyStream(dst, src []byte) {
37 c.c.Encrypt(c.iv, c.iv)
38 for n = 0; n < BlockSize; n++ {
39 if i*BlockSize+n == len(src) {
42 c.iv[n] ^= src[i*BlockSize+n]
43 dst[i*BlockSize+n] = c.iv[n]
49 type CFBDecrypter struct {
54 func (c *Cipher) NewCFBDecrypter(iv []byte) *CFBDecrypter {
55 if len(iv) != BlockSize {
56 panic("iv length is not equal to blocksize")
58 decrypter := CFBDecrypter{c: c, iv: make([]byte, BlockSize)}
59 copy(decrypter.iv, iv)
63 func (c *CFBDecrypter) XORKeyStream(dst, src []byte) {
68 c.c.Encrypt(c.iv, c.iv)
69 for n = 0; n < BlockSize; n++ {
70 if i*BlockSize+n == len(src) {
73 dst[i*BlockSize+n] = c.iv[n] ^ src[i*BlockSize+n]
74 c.iv[n] = src[i*BlockSize+n]