Source file src/crypto/internal/fips140/aes/ctr_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  //go:build !purego
     6  
     7  package aes
     8  
     9  import (
    10  	"crypto/internal/fips140/subtle"
    11  	"crypto/internal/fips140deps/byteorder"
    12  )
    13  
    14  func ctrBlocks1(b *Block, dst, src *[BlockSize]byte, ivlo, ivhi uint64) {
    15  	ctrBlocksS390x(b, dst[:], src[:], ivlo, ivhi)
    16  }
    17  
    18  func ctrBlocks2(b *Block, dst, src *[2 * BlockSize]byte, ivlo, ivhi uint64) {
    19  	ctrBlocksS390x(b, dst[:], src[:], ivlo, ivhi)
    20  }
    21  
    22  func ctrBlocks4(b *Block, dst, src *[4 * BlockSize]byte, ivlo, ivhi uint64) {
    23  	ctrBlocksS390x(b, dst[:], src[:], ivlo, ivhi)
    24  }
    25  
    26  func ctrBlocks8(b *Block, dst, src *[8 * BlockSize]byte, ivlo, ivhi uint64) {
    27  	ctrBlocksS390x(b, dst[:], src[:], ivlo, ivhi)
    28  }
    29  
    30  func ctrBlocksS390x(b *Block, dst, src []byte, ivlo, ivhi uint64) {
    31  	if b.fallback != nil {
    32  		ctrBlocks(b, dst, src, ivlo, ivhi)
    33  		return
    34  	}
    35  
    36  	buf := make([]byte, len(src), 8*BlockSize)
    37  	for i := 0; i < len(buf); i += BlockSize {
    38  		byteorder.BEPutUint64(buf[i:], ivhi)
    39  		byteorder.BEPutUint64(buf[i+8:], ivlo)
    40  		ivlo, ivhi = add128(ivlo, ivhi, 1)
    41  	}
    42  
    43  	// Encrypt the buffer using AES in ECB mode.
    44  	cryptBlocks(b.function, &b.key[0], &buf[0], &buf[0], len(buf))
    45  
    46  	// XOR into buf first, in case src and dst overlap (see ctrBlocks).
    47  	subtle.XORBytes(buf, src, buf)
    48  	copy(dst, buf)
    49  }
    50  

View as plain text