Source file src/crypto/aes/cbc_ppc64x.go

     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  
     7  package aes
     8  
     9  import (
    10  	"crypto/cipher"
    11  	"crypto/internal/alias"
    12  )
    13  
    14  // Assert that aesCipherAsm implements the cbcEncAble and cbcDecAble interfaces.
    15  var _ cbcEncAble = (*aesCipherAsm)(nil)
    16  var _ cbcDecAble = (*aesCipherAsm)(nil)
    17  
    18  const cbcEncrypt = 1
    19  const cbcDecrypt = 0
    20  
    21  type cbc struct {
    22  	b   *aesCipherAsm
    23  	enc int
    24  	iv  [BlockSize]byte
    25  }
    26  
    27  func (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  
    35  func (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  
    43  func (x *cbc) BlockSize() int { return BlockSize }
    44  
    45  // cryptBlocksChain invokes the cipher message identifying encrypt or decrypt.
    46  //
    47  //go:noescape
    48  func cryptBlocksChain(src, dst *byte, length int, key *uint32, iv *byte, enc int, nr int)
    49  
    50  func (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  
    69  func (x *cbc) SetIV(iv []byte) {
    70  	if len(iv) != BlockSize {
    71  		panic("cipher: incorrect length IV")
    72  	}
    73  	copy(x.iv[:], iv)
    74  }
    75  

View as plain text