github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/crypto/aes/cipher_ppc64le.go (about)

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