...

Source file src/pkg/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/subtle"
    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	//go:noescape
    43	func cryptBlocksChain(c code, iv, key, dst, src *byte, length int)
    44	
    45	func (x *cbc) CryptBlocks(dst, src []byte) {
    46		if len(src)%BlockSize != 0 {
    47			panic("crypto/cipher: input not full blocks")
    48		}
    49		if len(dst) < len(src) {
    50			panic("crypto/cipher: output smaller than input")
    51		}
    52		if subtle.InexactOverlap(dst[:len(src)], src) {
    53			panic("crypto/cipher: invalid buffer overlap")
    54		}
    55		if len(src) > 0 {
    56			cryptBlocksChain(x.c, &x.iv[0], &x.b.key[0], &dst[0], &src[0], len(src))
    57		}
    58	}
    59	
    60	func (x *cbc) SetIV(iv []byte) {
    61		if len(iv) != BlockSize {
    62			panic("cipher: incorrect length IV")
    63		}
    64		copy(x.iv[:], iv)
    65	}
    66	

View as plain text