1// Copyright 2021 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//go:build (ppc64 || ppc64le) && !purego
6
7package aes
8
9import (
10	"crypto/cipher"
11	"crypto/internal/alias"
12)
13
14// Assert that aesCipherAsm implements the cbcEncAble and cbcDecAble interfaces.
15var _ cbcEncAble = (*aesCipherAsm)(nil)
16var _ cbcDecAble = (*aesCipherAsm)(nil)
17
18const cbcEncrypt = 1
19const cbcDecrypt = 0
20
21type cbc struct {
22	b   *aesCipherAsm
23	enc int
24	iv  [BlockSize]byte
25}
26
27func (b *aesCipherAsm) NewCBCEncrypter(iv []byte) cipher.BlockMode {
28	var c cbc
29	c.b = b
30	c.enc = cbcEncrypt
31	copy(c.iv[:], iv)
32	return &c
33}
34
35func (b *aesCipherAsm) NewCBCDecrypter(iv []byte) cipher.BlockMode {
36	var c cbc
37	c.b = b
38	c.enc = cbcDecrypt
39	copy(c.iv[:], iv)
40	return &c
41}
42
43func (x *cbc) BlockSize() int { return BlockSize }
44
45// cryptBlocksChain invokes the cipher message identifying encrypt or decrypt.
46//
47//go:noescape
48func cryptBlocksChain(src, dst *byte, length int, key *uint32, iv *byte, enc int, nr int)
49
50func (x *cbc) CryptBlocks(dst, src []byte) {
51	if len(src)%BlockSize != 0 {
52		panic("crypto/cipher: input not full blocks")
53	}
54	if len(dst) < len(src) {
55		panic("crypto/cipher: output smaller than input")
56	}
57	if alias.InexactOverlap(dst[:len(src)], src) {
58		panic("crypto/cipher: invalid buffer overlap")
59	}
60	if len(src) > 0 {
61		if x.enc == cbcEncrypt {
62			cryptBlocksChain(&src[0], &dst[0], len(src), &x.b.enc[0], &x.iv[0], x.enc, int(x.b.l)/4-1)
63		} else {
64			cryptBlocksChain(&src[0], &dst[0], len(src), &x.b.dec[0], &x.iv[0], x.enc, int(x.b.l)/4-1)
65		}
66	}
67}
68
69func (x *cbc) SetIV(iv []byte) {
70	if len(iv) != BlockSize {
71		panic("cipher: incorrect length IV")
72	}
73	copy(x.iv[:], iv)
74}
75