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