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