cast.mx raw

   1  // Copyright 2024 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  	"bytes"
   9  	"crypto/internal/fips140"
  10  	_ "crypto/internal/fips140/check"
  11  	"errors"
  12  )
  13  
  14  func init() {
  15  	fips140.CAST("AES-CBC", func() error {
  16  		key := []byte{
  17  			0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  18  			0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
  19  		}
  20  		iv := [16]byte{
  21  			0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  22  			0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
  23  		}
  24  		plaintext := []byte{
  25  			0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
  26  			0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
  27  		}
  28  		ciphertext := []byte{
  29  			0xdf, 0x76, 0x26, 0x4b, 0xd3, 0xb2, 0xc4, 0x8d,
  30  			0x40, 0xa2, 0x6e, 0x7a, 0xc4, 0xff, 0xbd, 0x35,
  31  		}
  32  		b, err := New(key)
  33  		if err != nil {
  34  			return err
  35  		}
  36  		buf := []byte{:16}
  37  		NewCBCEncrypter(b, iv).CryptBlocks(buf, plaintext)
  38  		if !bytes.Equal(buf, ciphertext) {
  39  			return errors.New("unexpected result")
  40  		}
  41  		NewCBCDecrypter(b, iv).CryptBlocks(buf, ciphertext)
  42  		if !bytes.Equal(buf, plaintext) {
  43  			return errors.New("unexpected result")
  44  		}
  45  		return nil
  46  	})
  47  }
  48