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.
9 "crypto/internal/boring"
13 // The AES block size in bytes.
16 // A cipher is an instance of AES encryption using a particular key.
17 type aesCipher struct {
24 func (k KeySizeError) Error() string {
25 return "crypto/aes: invalid key size " + strconv.Itoa(int(k))
28 // NewCipher creates and returns a new cipher.Block.
29 // The key argument should be the AES key,
30 // either 16, 24, or 32 bytes to select
31 // AES-128, AES-192, or AES-256.
32 func NewCipher(key []byte) (cipher.Block, error) {
36 return nil, KeySizeError(k)
41 return boring.NewAESCipher(key)
46 // newCipherGeneric creates and returns a new cipher.Block
47 // implemented in pure Go.
48 func newCipherGeneric(key []byte) (cipher.Block, error) {
50 c := aesCipher{make([]uint32, n), make([]uint32, n)}
51 expandKeyGo(key, c.enc, c.dec)
55 func (c *aesCipher) BlockSize() int { return BlockSize }
57 func (c *aesCipher) Encrypt(dst, src []byte) {
58 if len(src) < BlockSize {
59 panic("crypto/aes: input not full block")
61 if len(dst) < BlockSize {
62 panic("crypto/aes: output not full block")
64 encryptBlockGo(c.enc, dst, src)
67 func (c *aesCipher) Decrypt(dst, src []byte) {
68 if len(src) < BlockSize {
69 panic("crypto/aes: input not full block")
71 if len(dst) < BlockSize {
72 panic("crypto/aes: output not full block")
74 decryptBlockGo(c.dec, dst, src)