LEFT | RIGHT |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // CFB (Cipher Feedback) Mode. | 5 // CFB (Cipher Feedback) Mode. |
6 | 6 |
7 package cipher | 7 package cipher |
8 | 8 |
9 type cfb struct { | 9 type cfb struct { |
10 b Block | 10 b Block |
11 next []byte | 11 next []byte |
12 out []byte | 12 out []byte |
13 outUsed int | 13 outUsed int |
14 | 14 |
15 // We can precompute a larger segment of the keystream on | |
16 // decryption. This will allow larger batches for xor, and we | |
17 // should be able to match CTR/OFB performance. | |
18 decrypt bool | 15 decrypt bool |
19 } | 16 } |
20 | 17 |
21 func (x *cfb) XORKeyStream(dst, src []byte) { | 18 func (x *cfb) XORKeyStream(dst, src []byte) { |
22 for i := 0; i < len(src); i++ { | 19 for i := 0; i < len(src); i++ { |
23 if x.outUsed == len(x.out) { | 20 if x.outUsed == len(x.out) { |
24 x.b.Encrypt(x.out, x.next) | 21 x.b.Encrypt(x.out, x.next) |
25 x.outUsed = 0 | 22 x.outUsed = 0 |
26 } | 23 } |
27 | 24 |
28 n := xorBytes(dst, src, x.out[x.outUsed:]) | 25 n := xorBytes(dst, src, x.out[x.outUsed:]) |
29 if x.decrypt { | 26 if x.decrypt { |
| 27 // We can precompute a larger segment of the |
| 28 // keystream on decryption. This will allow |
| 29 // larger batches for xor, and we should be |
| 30 // able to match CTR/OFB performance. |
30 copy(x.next[x.outUsed:], src[:n]) | 31 copy(x.next[x.outUsed:], src[:n]) |
31 } else { | 32 } else { |
32 copy(x.next[x.outUsed:], dst[:n]) | 33 copy(x.next[x.outUsed:], dst[:n]) |
33 } | 34 } |
34 dst = dst[n:] | 35 dst = dst[n:] |
35 src = src[n:] | 36 src = src[n:] |
36 x.outUsed += n | 37 x.outUsed += n |
37 } | 38 } |
38 } | 39 } |
39 | 40 |
(...skipping 21 matching lines...) Expand all Loading... |
61 b: block, | 62 b: block, |
62 out: make([]byte, blockSize), | 63 out: make([]byte, blockSize), |
63 next: make([]byte, blockSize), | 64 next: make([]byte, blockSize), |
64 outUsed: blockSize, | 65 outUsed: blockSize, |
65 decrypt: decrypt, | 66 decrypt: decrypt, |
66 } | 67 } |
67 copy(x.next, iv) | 68 copy(x.next, iv) |
68 | 69 |
69 return x | 70 return x |
70 } | 71 } |
LEFT | RIGHT |