Source file src/crypto/aes/cbc_s390x.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/alias"
    10  )
    11  
    12  // Assert that aesCipherAsm implements the cbcEncAble and cbcDecAble interfaces.
    13  var _ cbcEncAble = (*aesCipherAsm)(nil)
    14  var _ cbcDecAble = (*aesCipherAsm)(nil)
    15  
    16  type cbc struct {
    17  	b  *aesCipherAsm
    18  	c  code
    19  	iv [BlockSize]byte
    20  }
    21  
    22  func (b *aesCipherAsm) NewCBCEncrypter(iv []byte) cipher.BlockMode {
    23  	var c cbc
    24  	c.b = b
    25  	c.c = b.function
    26  	copy(c.iv[:], iv)
    27  	return &c
    28  }
    29  
    30  func (b *aesCipherAsm) NewCBCDecrypter(iv []byte) cipher.BlockMode {
    31  	var c cbc
    32  	c.b = b
    33  	c.c = b.function + 128 // decrypt function code is encrypt + 128
    34  	copy(c.iv[:], iv)
    35  	return &c
    36  }
    37  
    38  func (x *cbc) BlockSize() int { return BlockSize }
    39  
    40  // cryptBlocksChain invokes the cipher message with chaining (KMC) instruction
    41  // with the given function code. The length must be a multiple of BlockSize (16).
    42  //
    43  //go:noescape
    44  func cryptBlocksChain(c code, iv, key, dst, src *byte, length int)
    45  
    46  func (x *cbc) CryptBlocks(dst, src []byte) {
    47  	if len(src)%BlockSize != 0 {
    48  		panic("crypto/cipher: input not full blocks")
    49  	}
    50  	if len(dst) < len(src) {
    51  		panic("crypto/cipher: output smaller than input")
    52  	}
    53  	if alias.InexactOverlap(dst[:len(src)], src) {
    54  		panic("crypto/cipher: invalid buffer overlap")
    55  	}
    56  	if len(src) > 0 {
    57  		cryptBlocksChain(x.c, &x.iv[0], &x.b.key[0], &dst[0], &src[0], len(src))
    58  	}
    59  }
    60  
    61  func (x *cbc) SetIV(iv []byte) {
    62  	if len(iv) != BlockSize {
    63  		panic("cipher: incorrect length IV")
    64  	}
    65  	copy(x.iv[:], iv)
    66  }
    67  

View as plain text