| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// +build ignore |
| 6 |
|
| 7 |
package aes |
| 8 |
|
| 9 |
import ( |
| 10 |
"crypto/cipher" |
| 11 |
"crypto/internal/subtle" |
| 12 |
) |
| 13 |
|
| 14 |
// Assert that aesCipherAsm implements the cbcEncAble and cbcDecAble interfaces. |
| 15 |
var _ cbcEncAble = (*aesCipherAsm)(nil) |
| 16 |
var _ cbcDecAble = (*aesCipherAsm)(nil) |
| 17 |
|
| 18 |
type cbc struct { |
| 19 |
b *aesCipherAsm |
| 20 |
c code |
| 21 |
iv [BlockSize]byte |
| 22 |
} |
| 23 |
|
| 24 |
func (b *aesCipherAsm) NewCBCEncrypter(iv []byte) cipher.BlockMode { |
| 25 |
var c cbc |
| 26 |
c.b = b |
| 27 |
c.c = b.function |
| 28 |
copy(c.iv[:], iv) |
| 29 |
return &c |
| 30 |
} |
| 31 |
|
| 32 |
func (b *aesCipherAsm) NewCBCDecrypter(iv []byte) cipher.BlockMode { |
| 33 |
var c cbc |
| 34 |
c.b = b |
| 35 |
c.c = b.function + 128 // decrypt function code is encrypt + 128 |
| 36 |
copy(c.iv[:], iv) |
| 37 |
return &c |
| 38 |
} |
| 39 |
|
| 40 |
func (x *cbc) BlockSize() int { return BlockSize } |
| 41 |
|
| 42 |
// cryptBlocksChain invokes the cipher message with chaining (KMC) instruction |
| 43 |
// with the given function code. The length must be a multiple of BlockSize (16). |
| 44 |
//go:noescape |
| 45 |
func cryptBlocksChain(c code, iv, key, dst, src *byte, length int) |
| 46 |
|
| 47 |
func (x *cbc) CryptBlocks(dst, src []byte) { |
| 48 |
if len(src)%BlockSize != 0 { |
| 49 |
panic("crypto/cipher: input not full blocks") |
| 50 |
} |
| 51 |
if len(dst) < len(src) { |
| 52 |
panic("crypto/cipher: output smaller than input") |
| 53 |
} |
| 54 |
if subtle.InexactOverlap(dst[:len(src)], src) { |
| 55 |
panic("crypto/cipher: invalid buffer overlap") |
| 56 |
} |
| 57 |
if len(src) > 0 { |
| 58 |
cryptBlocksChain(x.c, &x.iv[0], &x.b.key[0], &dst[0], &src[0], len(src)) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func (x *cbc) SetIV(iv []byte) { |
| 63 |
if len(iv) != BlockSize { |
| 64 |
panic("cipher: incorrect length IV") |
| 65 |
} |
| 66 |
copy(x.iv[:], iv) |
| 67 |
} |