github.com/mangodowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/crypto/cipher/cfb_test.go (about)

     1  // Copyright 2010 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 cipher_test
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/aes"
    10  	"crypto/cipher"
    11  	"crypto/rand"
    12  	"encoding/hex"
    13  	"testing"
    14  )
    15  
    16  // cfbTests contains the test vectors from
    17  // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, section
    18  // F.3.13.
    19  var cfbTests = []struct {
    20  	key, iv, plaintext, ciphertext string
    21  }{
    22  	{
    23  		"2b7e151628aed2a6abf7158809cf4f3c",
    24  		"000102030405060708090a0b0c0d0e0f",
    25  		"6bc1bee22e409f96e93d7e117393172a",
    26  		"3b3fd92eb72dad20333449f8e83cfb4a",
    27  	},
    28  	{
    29  		"2b7e151628aed2a6abf7158809cf4f3c",
    30  		"3B3FD92EB72DAD20333449F8E83CFB4A",
    31  		"ae2d8a571e03ac9c9eb76fac45af8e51",
    32  		"c8a64537a0b3a93fcde3cdad9f1ce58b",
    33  	},
    34  	{
    35  		"2b7e151628aed2a6abf7158809cf4f3c",
    36  		"C8A64537A0B3A93FCDE3CDAD9F1CE58B",
    37  		"30c81c46a35ce411e5fbc1191a0a52ef",
    38  		"26751f67a3cbb140b1808cf187a4f4df",
    39  	},
    40  	{
    41  		"2b7e151628aed2a6abf7158809cf4f3c",
    42  		"26751F67A3CBB140B1808CF187A4F4DF",
    43  		"f69f2445df4f9b17ad2b417be66c3710",
    44  		"c04b05357c5d1c0eeac4c66f9ff7f2e6",
    45  	},
    46  }
    47  
    48  func TestCFBVectors(t *testing.T) {
    49  	for i, test := range cfbTests {
    50  		key, err := hex.DecodeString(test.key)
    51  		if err != nil {
    52  			t.Fatal(err)
    53  		}
    54  		iv, err := hex.DecodeString(test.iv)
    55  		if err != nil {
    56  			t.Fatal(err)
    57  		}
    58  		plaintext, err := hex.DecodeString(test.plaintext)
    59  		if err != nil {
    60  			t.Fatal(err)
    61  		}
    62  		expected, err := hex.DecodeString(test.ciphertext)
    63  		if err != nil {
    64  			t.Fatal(err)
    65  		}
    66  
    67  		block, err := aes.NewCipher(key)
    68  		if err != nil {
    69  			t.Fatal(err)
    70  		}
    71  
    72  		ciphertext := make([]byte, len(plaintext))
    73  		cfb := cipher.NewCFBEncrypter(block, iv)
    74  		cfb.XORKeyStream(ciphertext, plaintext)
    75  
    76  		if !bytes.Equal(ciphertext, expected) {
    77  			t.Errorf("#%d: wrong output: got %x, expected %x", i, ciphertext, expected)
    78  		}
    79  
    80  		cfbdec := cipher.NewCFBDecrypter(block, iv)
    81  		plaintextCopy := make([]byte, len(ciphertext))
    82  		cfbdec.XORKeyStream(plaintextCopy, ciphertext)
    83  
    84  		if !bytes.Equal(plaintextCopy, plaintextCopy) {
    85  			t.Errorf("#%d: wrong plaintext: got %x, expected %x", i, plaintextCopy, plaintext)
    86  		}
    87  	}
    88  }
    89  
    90  func TestCFBInverse(t *testing.T) {
    91  	block, err := aes.NewCipher(commonKey128)
    92  	if err != nil {
    93  		t.Error(err)
    94  		return
    95  	}
    96  
    97  	plaintext := []byte("this is the plaintext. this is the plaintext.")
    98  	iv := make([]byte, block.BlockSize())
    99  	rand.Reader.Read(iv)
   100  	cfb := cipher.NewCFBEncrypter(block, iv)
   101  	ciphertext := make([]byte, len(plaintext))
   102  	copy(ciphertext, plaintext)
   103  	cfb.XORKeyStream(ciphertext, ciphertext)
   104  
   105  	cfbdec := cipher.NewCFBDecrypter(block, iv)
   106  	plaintextCopy := make([]byte, len(plaintext))
   107  	copy(plaintextCopy, ciphertext)
   108  	cfbdec.XORKeyStream(plaintextCopy, plaintextCopy)
   109  
   110  	if !bytes.Equal(plaintextCopy, plaintext) {
   111  		t.Errorf("got: %x, want: %x", plaintextCopy, plaintext)
   112  	}
   113  }