Source file src/crypto/aes/cipher.go

     1  // Copyright 2009 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  	"crypto/internal/boring"
    11  	"strconv"
    12  )
    13  
    14  // The AES block size in bytes.
    15  const BlockSize = 16
    16  
    17  // A cipher is an instance of AES encryption using a particular key.
    18  type aesCipher struct {
    19  	enc []uint32
    20  	dec []uint32
    21  }
    22  
    23  type KeySizeError int
    24  
    25  func (k KeySizeError) Error() string {
    26  	return "crypto/aes: invalid key size " + strconv.Itoa(int(k))
    27  }
    28  
    29  // NewCipher creates and returns a new [cipher.Block].
    30  // The key argument should be the AES key,
    31  // either 16, 24, or 32 bytes to select
    32  // AES-128, AES-192, or AES-256.
    33  func NewCipher(key []byte) (cipher.Block, error) {
    34  	k := len(key)
    35  	switch k {
    36  	default:
    37  		return nil, KeySizeError(k)
    38  	case 16, 24, 32:
    39  		break
    40  	}
    41  	if boring.Enabled {
    42  		return boring.NewAESCipher(key)
    43  	}
    44  	return newCipher(key)
    45  }
    46  
    47  // newCipherGeneric creates and returns a new cipher.Block
    48  // implemented in pure Go.
    49  func newCipherGeneric(key []byte) (cipher.Block, error) {
    50  	n := len(key) + 28
    51  	c := aesCipher{make([]uint32, n), make([]uint32, n)}
    52  	expandKeyGo(key, c.enc, c.dec)
    53  	return &c, nil
    54  }
    55  
    56  func (c *aesCipher) BlockSize() int { return BlockSize }
    57  
    58  func (c *aesCipher) Encrypt(dst, src []byte) {
    59  	if len(src) < BlockSize {
    60  		panic("crypto/aes: input not full block")
    61  	}
    62  	if len(dst) < BlockSize {
    63  		panic("crypto/aes: output not full block")
    64  	}
    65  	if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
    66  		panic("crypto/aes: invalid buffer overlap")
    67  	}
    68  	encryptBlockGo(c.enc, dst, src)
    69  }
    70  
    71  func (c *aesCipher) Decrypt(dst, src []byte) {
    72  	if len(src) < BlockSize {
    73  		panic("crypto/aes: input not full block")
    74  	}
    75  	if len(dst) < BlockSize {
    76  		panic("crypto/aes: output not full block")
    77  	}
    78  	if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
    79  		panic("crypto/aes: invalid buffer overlap")
    80  	}
    81  	decryptBlockGo(c.dec, dst, src)
    82  }
    83  

View as plain text