github.com/emmansun/gmsm@v0.29.1/cipher/ccm_test.go (about)

     1  package cipher_test
     2  
     3  import (
     4  	"crypto/aes"
     5  	"encoding/hex"
     6  	"testing"
     7  
     8  	"github.com/emmansun/gmsm/cipher"
     9  )
    10  
    11  // https://tools.ietf.org/html/rfc3610, 8. Test Vectors
    12  var aesCCMTests = []struct {
    13  	key, nonce, plaintext, ad, result string
    14  	tagSize                           int
    15  }{
    16  	{
    17  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
    18  		"00000003020100a0a1a2a3a4a5",
    19  		"08090a0b0c0d0e0f101112131415161718191a1b1c1d1e",
    20  		"0001020304050607",
    21  		"588c979a61c663d2f066d0c2c0f989806d5f6b61dac38417e8d12cfdf926e0",
    22  		8,
    23  	},
    24  	{
    25  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
    26  		"00000004030201a0a1a2a3a4a5",
    27  		"08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
    28  		"0001020304050607",
    29  		"72c91a36e135f8cf291ca894085c87e3cc15c439c9e43a3ba091d56e10400916",
    30  		8,
    31  	},
    32  	{
    33  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
    34  		"00000005040302a0a1a2a3a4a5",
    35  		"08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20",
    36  		"0001020304050607",
    37  		"51b1e5f44a197d1da46b0f8e2d282ae871e838bb64da8596574adaa76fbd9fb0c5",
    38  		8,
    39  	},
    40  	{
    41  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
    42  		"00000006050403a0a1a2a3a4a5",
    43  		"0c0d0e0f101112131415161718191a1b1c1d1e",
    44  		"000102030405060708090a0b",
    45  		"a28c6865939a9a79faaa5c4c2a9d4a91cdac8c96c861b9c9e61ef1",
    46  		8,
    47  	},
    48  	{
    49  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
    50  		"00000007060504a0a1a2a3a4a5",
    51  		"0c0d0e0f101112131415161718191a1b1c1d1e1f",
    52  		"000102030405060708090a0b",
    53  		"dcf1fb7b5d9e23fb9d4e131253658ad86ebdca3e51e83f077d9c2d93",
    54  		8,
    55  	},
    56  	{
    57  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
    58  		"00000008070605a0a1a2a3a4a5",
    59  		"0c0d0e0f101112131415161718191a1b1c1d1e1f20",
    60  		"000102030405060708090a0b",
    61  		"6fc1b011f006568b5171a42d953d469b2570a4bd87405a0443ac91cb94",
    62  		8,
    63  	},
    64  	{
    65  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
    66  		"00000009080706a0a1a2a3a4a5",
    67  		"08090a0b0c0d0e0f101112131415161718191a1b1c1d1e",
    68  		"0001020304050607",
    69  		"0135d1b2c95f41d5d1d4fec185d166b8094e999dfed96c048c56602c97acbb7490",
    70  		10,
    71  	},
    72  	{
    73  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
    74  		"0000000a090807a0a1a2a3a4a5",
    75  		"08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
    76  		"0001020304050607",
    77  		"7b75399ac0831dd2f0bbd75879a2fd8f6cae6b6cd9b7db24c17b4433f434963f34b4",
    78  		10,
    79  	},
    80  	{
    81  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
    82  		"0000000b0a0908a0a1a2a3a4a5",
    83  		"08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20",
    84  		"0001020304050607",
    85  		"82531a60cc24945a4b8279181ab5c84df21ce7f9b73f42e197ea9c07e56b5eb17e5f4e",
    86  		10,
    87  	},
    88  	{
    89  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
    90  		"0000000c0b0a09a0a1a2a3a4a5",
    91  		"0c0d0e0f101112131415161718191a1b1c1d1e",
    92  		"000102030405060708090a0b",
    93  		"07342594157785152b074098330abb141b947b566aa9406b4d999988dd",
    94  		10,
    95  	},
    96  	{
    97  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
    98  		"0000000d0c0b0aa0a1a2a3a4a5",
    99  		"0c0d0e0f101112131415161718191a1b1c1d1e1f",
   100  		"000102030405060708090a0b",
   101  		"676bb20380b0e301e8ab79590a396da78b834934f53aa2e9107a8b6c022c",
   102  		10,
   103  	},
   104  	{
   105  		"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf",
   106  		"0000000e0d0c0ba0a1a2a3a4a5",
   107  		"0c0d0e0f101112131415161718191a1b1c1d1e1f20",
   108  		"000102030405060708090a0b",
   109  		"c0ffa0d6f05bdb67f24d43a4338d2aa4bed7b20e43cd1aa31662e7ad65d6db",
   110  		10,
   111  	},
   112  	{
   113  		"d7828d13b2b0bdc325a76236df93cc6b",
   114  		"00412b4ea9cdbe3c9696766cfa",
   115  		"08e8cf97d820ea258460e96ad9cf5289054d895ceac47c",
   116  		"0be1a88bace018b1",
   117  		"4cb97f86a2a4689a877947ab8091ef5386a6ffbdd080f8e78cf7cb0cddd7b3",
   118  		8,
   119  	},
   120  	{
   121  		"d7828d13b2b0bdc325a76236df93cc6b",
   122  		"0033568ef7b2633c9696766cfa",
   123  		"9020ea6f91bdd85afa0039ba4baff9bfb79c7028949cd0ec",
   124  		"63018f76dc8a1bcb",
   125  		"4ccb1e7ca981befaa0726c55d378061298c85c92814abc33c52ee81d7d77c08a",
   126  		8,
   127  	},
   128  	{
   129  		"d7828d13b2b0bdc325a76236df93cc6b",
   130  		"00f8b678094e3b3c9696766cfa",
   131  		"e88b6a46c78d63e52eb8c546efb5de6f75e9cc0d",
   132  		"77b60f011c03e1525899bcae",
   133  		"5545ff1a085ee2efbf52b2e04bee1e2336c73e3f762c0c7744fe7e3c",
   134  		8,
   135  	},
   136  	{
   137  		"d7828d13b2b0bdc325a76236df93cc6b",
   138  		"00d560912d3f703c9696766cfa",
   139  		"6435acbafb11a82e2f071d7ca4a5ebd93a803ba87f",
   140  		"cd9044d2b71fdb8120ea60c0",
   141  		"009769ecabdf48625594c59251e6035722675e04c847099e5ae0704551",
   142  		8,
   143  	},
   144  	{
   145  		"d7828d13b2b0bdc325a76236df93cc6b",
   146  		"0042fff8f1951c3c9696766cfa",
   147  		"8a19b950bcf71a018e5e6701c91787659809d67dbedd18",
   148  		"d85bc7e69f944fb8",
   149  		"bc218daa947427b6db386a99ac1aef23ade0b52939cb6a637cf9bec2408897c6ba",
   150  		10,
   151  	},
   152  	{
   153  		"d7828d13b2b0bdc325a76236df93cc6b",
   154  		"00920f40e56cdc3c9696766cfa",
   155  		"1761433c37c5a35fc1f39f406302eb907c6163be38c98437",
   156  		"74a0ebc9069f5b37",
   157  		"5810e6fd25874022e80361a478e3e9cf484ab04f447efff6f0a477cc2fc9bf548944",
   158  		10,
   159  	},
   160  	{
   161  		"d7828d13b2b0bdc325a76236df93cc6b",
   162  		"0027ca0c7120bc3c9696766cfa",
   163  		"a434a8e58500c6e41530538862d686ea9e81301b5ae4226bfa",
   164  		"44a3aa3aae6475ca",
   165  		"f2beed7bc5098e83feb5b31608f8e29c38819a89c8e776f1544d4151a4ed3a8b87b9ce",
   166  		10,
   167  	},
   168  	{
   169  		"d7828d13b2b0bdc325a76236df93cc6b",
   170  		"005b8ccbcd9af83c9696766cfa",
   171  		"b96b49e21d621741632875db7f6c9243d2d7c2",
   172  		"ec46bb63b02520c33c49fd70",
   173  		"31d750a09da3ed7fddd49a2032aabf17ec8ebf7d22c8088c666be5c197",
   174  		10,
   175  	},
   176  	{
   177  		"d7828d13b2b0bdc325a76236df93cc6b",
   178  		"003ebe94044b9a3c9696766cfa",
   179  		"e2fcfbb880442c731bf95167c8ffd7895e337076",
   180  		"47a65ac78b3d594227e85e71",
   181  		"e882f1dbd38ce3eda7c23f04dd65071eb41342acdf7e00dccec7ae52987d",
   182  		10,
   183  	},
   184  	{
   185  		"d7828d13b2b0bdc325a76236df93cc6b",
   186  		"008d493b30ae8b3c9696766cfa",
   187  		"abf21c0b02feb88f856df4a37381bce3cc128517d4",
   188  		"6e37a6ef546d955d34ab6059",
   189  		"f32905b88a641b04b9c9ffb58cc390900f3da12ab16dce9e82efa16da62059",
   190  		10,
   191  	},
   192  }
   193  
   194  func TestCCMWithAES(t *testing.T) {
   195  	for i, tt := range aesCCMTests {
   196  		nonce, _ := hex.DecodeString(tt.nonce)
   197  		plaintext, _ := hex.DecodeString(tt.plaintext)
   198  		ad, _ := hex.DecodeString(tt.ad)
   199  		key, _ := hex.DecodeString(tt.key)
   200  		c, err := aes.NewCipher(key)
   201  		if err != nil {
   202  			t.Fatal(err)
   203  		}
   204  		aesccm, err := cipher.NewCCMWithNonceAndTagSize(c, len(nonce), tt.tagSize)
   205  		if err != nil {
   206  			t.Fatal(err)
   207  		}
   208  		ct := aesccm.Seal(nil, nonce, plaintext, ad)
   209  		if ctHex := hex.EncodeToString(ct); ctHex != tt.result {
   210  			t.Errorf("#%d: got %s, want %s", i, ctHex, tt.result)
   211  			continue
   212  		}
   213  
   214  		//func (c *ccm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error)
   215  		pt, err := aesccm.Open(nil, nonce, ct, ad)
   216  		if err != nil {
   217  			t.Fatal(err)
   218  		}
   219  		if ptHex := hex.EncodeToString(pt); ptHex != tt.plaintext {
   220  			t.Errorf("#%d: got %s, want %s", i, ptHex, tt.plaintext)
   221  			continue
   222  		}
   223  	}
   224  }
   225  
   226  func TestCCMInvalidTagSize(t *testing.T) {
   227  	key, _ := hex.DecodeString("ab72c77b97cb5fe9a382d9fe81ffdbed")
   228  
   229  	c, _ := aes.NewCipher(key)
   230  
   231  	for _, tagSize := range []int{0, 1, c.BlockSize() + 1} {
   232  		aesccm, err := cipher.NewCCMWithTagSize(c, tagSize)
   233  		if aesccm != nil || err == nil {
   234  			t.Fatalf("NewCCMWithNonceAndTagSize was successful with an invalid %d-byte tag size", tagSize)
   235  		}
   236  	}
   237  }
   238  
   239  func TestCCMInvalidNonceSize(t *testing.T) {
   240  	key, _ := hex.DecodeString("ab72c77b97cb5fe9a382d9fe81ffdbed")
   241  
   242  	c, _ := aes.NewCipher(key)
   243  
   244  	for _, nonceSize := range []int{0, 1, c.BlockSize() + 1} {
   245  		aesccm, err := cipher.NewCCMWithNonceSize(c, nonceSize)
   246  		if aesccm != nil || err == nil {
   247  			t.Fatalf("NewCCMWithNonceSize was successful with an invalid %d-byte tag size", nonceSize)
   248  		}
   249  	}
   250  }
   251  
   252  func TestCCMTagFailureOverwrite(t *testing.T) {
   253  	key, _ := hex.DecodeString("ab72c77b97cb5fe9a382d9fe81ffdbed")
   254  	nonce, _ := hex.DecodeString("54cc7dc2c37ec006bcc6d1db")
   255  	ciphertext, _ := hex.DecodeString("0e1bde206a07a9c2c1b65300f8c649972b4401346697138c7a4891ee59867d0c")
   256  
   257  	c, _ := aes.NewCipher(key)
   258  	aesccm, _ := cipher.NewCCM(c)
   259  
   260  	dst := make([]byte, len(ciphertext)-16)
   261  	for i := range dst {
   262  		dst[i] = 42
   263  	}
   264  
   265  	result, err := aesccm.Open(dst[:0], nonce, ciphertext, nil)
   266  	if err == nil {
   267  		t.Fatal("Bad Open still resulted in nil error.")
   268  	}
   269  
   270  	if result != nil {
   271  		t.Fatal("Failed Open returned non-nil result.")
   272  	}
   273  
   274  	for i := range dst {
   275  		if dst[i] != 0 {
   276  			t.Fatal("Failed Open didn't zero dst buffer")
   277  		}
   278  	}
   279  }