Source file src/crypto/aes/cipher_ppc64le.go

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

View as plain text