48
48
if len(dst) < len(src) {
49
49
panic("crypto/cipher: output smaller than input")
52
for i := 0; i < x.blockSize; i++ {
55
x.b.Encrypt(x.iv, x.iv)
56
for i := 0; i < x.blockSize; i++ {
55
// Write the xor to dst, then encrypt in place.
56
xorBytes(dst[:x.blockSize], src[:x.blockSize], iv)
57
x.b.Encrypt(dst[:x.blockSize], dst[:x.blockSize])
59
// Move to the next block with this block as the next iv.
60
iv = dst[:x.blockSize]
59
61
src = src[x.blockSize:]
60
62
dst = dst[x.blockSize:]
65
// Save the iv for the next CryptBlocks call.
64
69
func (x *cbcEncrypter) SetIV(iv []byte) {
89
94
if len(dst) < len(src) {
90
95
panic("crypto/cipher: output smaller than input")
93
x.b.Decrypt(x.tmp, src[:x.blockSize])
94
for i := 0; i < x.blockSize; i++ {
100
src = src[x.blockSize:]
101
dst = dst[x.blockSize:]
101
// For each block, we need to xor the decrypted data with the previous block's ciphertext (the iv).
102
// To avoid making a copy each time, we loop over the blocks BACKWARDS.
104
start := end - x.blockSize
105
prev := start - x.blockSize
107
// Copy the last block of ciphertext in preparation as the new iv.
108
copy(x.tmp, src[start:end])
110
// Loop over all but the first block.
112
x.b.Decrypt(dst[start:end], src[start:end])
113
xorBytes(dst[start:end], dst[start:end], src[prev:start])
120
// The first block is special because it uses the saved iv.
121
x.b.Decrypt(dst[start:end], src[start:end])
122
xorBytes(dst[start:end], dst[start:end], x.iv)
124
// Set the new iv to the first block we copied earlier.
125
x.iv, x.tmp = x.tmp, x.iv
105
128
func (x *cbcDecrypter) SetIV(iv []byte) {