~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/crypto/cipher/cbc.go

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-11-18 15:12:26 UTC
  • mfrom: (14.2.12 vivid-proposed)
  • Revision ID: package-import@ubuntu.com-20141118151226-zug7vn93mn3dtiz3
Tags: 2:1.3.2-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - Support co-installability with gccgo-go tool:
    - d/rules,golang-go.install: Rename bin/go -> bin/golang-go
    - d/golang-go.{postinst,prerm}: Install/remove /usr/bin/go using
      alternatives.
  - d/copyright: Amendments for full compiliance with copyright format.
  - d/control: Demote golang-go.tools to Suggests to support Ubuntu MIR.
  - dropped patches (now upstream):
    - d/p/issue27650045_40001_50001.diff
    - d/p/issue28050043_60001_70001.diff
    - d/p/issue54790044_100001_110001.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
        if len(dst) < len(src) {
49
49
                panic("crypto/cipher: output smaller than input")
50
50
        }
 
51
 
 
52
        iv := x.iv
 
53
 
51
54
        for len(src) > 0 {
52
 
                for i := 0; i < x.blockSize; i++ {
53
 
                        x.iv[i] ^= src[i]
54
 
                }
55
 
                x.b.Encrypt(x.iv, x.iv)
56
 
                for i := 0; i < x.blockSize; i++ {
57
 
                        dst[i] = x.iv[i]
58
 
                }
 
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])
 
58
 
 
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:]
61
63
        }
 
64
 
 
65
        // Save the iv for the next CryptBlocks call.
 
66
        copy(x.iv, iv)
62
67
}
63
68
 
64
69
func (x *cbcEncrypter) SetIV(iv []byte) {
89
94
        if len(dst) < len(src) {
90
95
                panic("crypto/cipher: output smaller than input")
91
96
        }
92
 
        for len(src) > 0 {
93
 
                x.b.Decrypt(x.tmp, src[:x.blockSize])
94
 
                for i := 0; i < x.blockSize; i++ {
95
 
                        x.tmp[i] ^= x.iv[i]
96
 
                        x.iv[i] = src[i]
97
 
                        dst[i] = x.tmp[i]
98
 
                }
99
 
 
100
 
                src = src[x.blockSize:]
101
 
                dst = dst[x.blockSize:]
102
 
        }
 
97
        if len(src) == 0 {
 
98
                return
 
99
        }
 
100
 
 
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.
 
103
        end := len(src)
 
104
        start := end - x.blockSize
 
105
        prev := start - x.blockSize
 
106
 
 
107
        // Copy the last block of ciphertext in preparation as the new iv.
 
108
        copy(x.tmp, src[start:end])
 
109
 
 
110
        // Loop over all but the first block.
 
111
        for start > 0 {
 
112
                x.b.Decrypt(dst[start:end], src[start:end])
 
113
                xorBytes(dst[start:end], dst[start:end], src[prev:start])
 
114
 
 
115
                end = start
 
116
                start = prev
 
117
                prev -= x.blockSize
 
118
        }
 
119
 
 
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)
 
123
 
 
124
        // Set the new iv to the first block we copied earlier.
 
125
        x.iv, x.tmp = x.tmp, x.iv
103
126
}
104
127
 
105
128
func (x *cbcDecrypter) SetIV(iv []byte) {