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
7package aes
8
9import (
10	"crypto/cipher"
11	"crypto/internal/subtle"
12)
13
14// defined in asm_ppc64le.s
15
16//go:noescape
17func setEncryptKeyAsm(key *byte, keylen int, enc *uint32) int
18
19//go:noescape
20func setDecryptKeyAsm(key *byte, keylen int, dec *uint32) int
21
22//go:noescape
23func doEncryptKeyAsm(key *byte, keylen int, dec *uint32) int
24
25//go:noescape
26func encryptBlockAsm(dst, src *byte, enc *uint32)
27
28//go:noescape
29func decryptBlockAsm(dst, src *byte, dec *uint32)
30
31type aesCipherAsm struct {
32	aesCipher
33}
34
35func newCipher(key []byte) (cipher.Block, error) {
36	n := 64 // size is fixed for all and round value is stored inside it too
37	c := aesCipherAsm{aesCipher{make([]uint32, n), make([]uint32, n)}}
38	k := len(key)
39
40	ret := 0
41	ret += setEncryptKeyAsm(&key[0], k*8, &c.enc[0])
42	ret += setDecryptKeyAsm(&key[0], k*8, &c.dec[0])
43
44	if ret > 0 {
45		return nil, KeySizeError(k)
46	}
47
48	return &c, nil
49}
50
51func (c *aesCipherAsm) BlockSize() int { return BlockSize }
52
53func (c *aesCipherAsm) Encrypt(dst, src []byte) {
54	if len(src) < BlockSize {
55		panic("crypto/aes: input not full block")
56	}
57	if len(dst) < BlockSize {
58		panic("crypto/aes: output not full block")
59	}
60	if subtle.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
61		panic("crypto/aes: invalid buffer overlap")
62	}
63	encryptBlockAsm(&dst[0], &src[0], &c.enc[0])
64}
65
66func (c *aesCipherAsm) Decrypt(dst, src []byte) {
67	if len(src) < BlockSize {
68		panic("crypto/aes: input not full block")
69	}
70	if len(dst) < BlockSize {
71		panic("crypto/aes: output not full block")
72	}
73	if subtle.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
74		panic("crypto/aes: invalid buffer overlap")
75	}
76	decryptBlockAsm(&dst[0], &src[0], &c.dec[0])
77}
78
79// expandKey is used by BenchmarkExpand to ensure that the asm implementation
80// of key expansion is used for the benchmark when it is available.
81func expandKey(key []byte, enc, dec []uint32) {
82	setEncryptKeyAsm(&key[0], len(key)*8, &enc[0])
83	setDecryptKeyAsm(&key[0], len(key)*8, &dec[0])
84}
85