github.com/pion/dtls/v2@v2.2.12/pkg/crypto/ccm/ccm_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package ccm 5 6 // Refer to RFC 3610 section 8 for the vectors. 7 8 import ( 9 "bytes" 10 "crypto/aes" 11 "encoding/hex" 12 "errors" 13 "fmt" 14 "testing" 15 ) 16 17 func mustHexDecode(s string) []byte { 18 r, err := hex.DecodeString(s) 19 if err != nil { 20 panic(err) 21 } 22 return r 23 } 24 25 var ( 26 aesKey1to12 = mustHexDecode("c0c1c2c3c4c5c6c7c8c9cacbcccdcecf") //nolint:gochecknoglobals 27 aesKey13to24 = mustHexDecode("d7828d13b2b0bdc325a76236df93cc6b") //nolint:gochecknoglobals 28 ) 29 30 // AESKey: AES Key 31 // CipherText: Authenticated and encrypted output 32 // ClearHeaderOctets: Input with X cleartext header octets 33 // Data: Input with X cleartext header octets 34 // M: length(CBC-MAC) 35 // Nonce: Nonce 36 type vector struct { 37 AESKey []byte 38 CipherText []byte 39 ClearHeaderOctets int 40 Data []byte 41 M int 42 Nonce []byte 43 } 44 45 func TestRFC3610Vectors(t *testing.T) { 46 cases := []vector{ 47 // Vectors 1-12 48 { 49 AESKey: aesKey1to12, 50 CipherText: mustHexDecode("0001020304050607588c979a61c663d2f066d0c2c0f989806d5f6b61dac38417e8d12cfdf926e0"), 51 ClearHeaderOctets: 8, 52 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e"), 53 M: 8, 54 Nonce: mustHexDecode("00000003020100a0a1a2a3a4a5"), 55 }, 56 { 57 AESKey: aesKey1to12, 58 CipherText: mustHexDecode("000102030405060772c91a36e135f8cf291ca894085c87e3cc15c439c9e43a3ba091d56e10400916"), 59 ClearHeaderOctets: 8, 60 Data: mustHexDecode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"), 61 M: 8, 62 Nonce: mustHexDecode("00000004030201a0a1a2a3a4a5"), 63 }, 64 { 65 AESKey: aesKey1to12, 66 CipherText: mustHexDecode("000102030405060751b1e5f44a197d1da46b0f8e2d282ae871e838bb64da8596574adaa76fbd9fb0c5"), 67 ClearHeaderOctets: 8, 68 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"), 69 M: 8, 70 Nonce: mustHexDecode("00000005040302a0a1a2a3a4a5"), 71 }, 72 { 73 AESKey: aesKey1to12, 74 CipherText: mustHexDecode("000102030405060708090a0ba28c6865939a9a79faaa5c4c2a9d4a91cdac8c96c861b9c9e61ef1"), 75 ClearHeaderOctets: 12, 76 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e"), 77 M: 8, 78 Nonce: mustHexDecode("00000006050403a0a1a2a3a4a5"), 79 }, 80 { 81 AESKey: aesKey1to12, 82 CipherText: mustHexDecode("000102030405060708090a0bdcf1fb7b5d9e23fb9d4e131253658ad86ebdca3e51e83f077d9c2d93"), 83 ClearHeaderOctets: 12, 84 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"), 85 M: 8, 86 Nonce: mustHexDecode("00000007060504a0a1a2a3a4a5"), 87 }, 88 { 89 AESKey: aesKey1to12, 90 CipherText: mustHexDecode("000102030405060708090a0b6fc1b011f006568b5171a42d953d469b2570a4bd87405a0443ac91cb94"), 91 ClearHeaderOctets: 12, 92 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"), 93 M: 8, 94 Nonce: mustHexDecode("00000008070605a0a1a2a3a4a5"), 95 }, 96 { 97 AESKey: aesKey1to12, 98 CipherText: mustHexDecode("00010203040506070135d1b2c95f41d5d1d4fec185d166b8094e999dfed96c048c56602c97acbb7490"), 99 ClearHeaderOctets: 8, 100 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e"), 101 M: 10, 102 Nonce: mustHexDecode("00000009080706a0a1a2a3a4a5"), 103 }, 104 { 105 AESKey: aesKey1to12, 106 CipherText: mustHexDecode("00010203040506077b75399ac0831dd2f0bbd75879a2fd8f6cae6b6cd9b7db24c17b4433f434963f34b4"), 107 ClearHeaderOctets: 8, 108 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"), 109 M: 10, 110 Nonce: mustHexDecode("0000000a090807a0a1a2a3a4a5"), 111 }, 112 { 113 AESKey: aesKey1to12, 114 CipherText: mustHexDecode("000102030405060782531a60cc24945a4b8279181ab5c84df21ce7f9b73f42e197ea9c07e56b5eb17e5f4e"), 115 ClearHeaderOctets: 8, 116 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"), 117 M: 10, 118 Nonce: mustHexDecode("0000000b0a0908a0a1a2a3a4a5"), 119 }, 120 { 121 AESKey: aesKey1to12, 122 CipherText: mustHexDecode("000102030405060708090a0b07342594157785152b074098330abb141b947b566aa9406b4d999988dd"), 123 ClearHeaderOctets: 12, 124 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e"), 125 M: 10, 126 Nonce: mustHexDecode("0000000c0b0a09a0a1a2a3a4a5"), 127 }, 128 { 129 AESKey: aesKey1to12, 130 CipherText: mustHexDecode("000102030405060708090a0b676bb20380b0e301e8ab79590a396da78b834934f53aa2e9107a8b6c022c"), 131 ClearHeaderOctets: 12, 132 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"), 133 M: 10, 134 Nonce: mustHexDecode("0000000d0c0b0aa0a1a2a3a4a5"), 135 }, 136 { 137 AESKey: aesKey1to12, 138 CipherText: mustHexDecode("000102030405060708090a0bc0ffa0d6f05bdb67f24d43a4338d2aa4bed7b20e43cd1aa31662e7ad65d6db"), 139 ClearHeaderOctets: 12, 140 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"), 141 M: 10, 142 Nonce: mustHexDecode("0000000e0d0c0ba0a1a2a3a4a5"), 143 }, 144 // Vectors 13-24 145 { 146 AESKey: aesKey13to24, 147 CipherText: mustHexDecode("0be1a88bace018b14cb97f86a2a4689a877947ab8091ef5386a6ffbdd080f8e78cf7cb0cddd7b3"), 148 ClearHeaderOctets: 8, 149 Data: mustHexDecode("0be1a88bace018b108e8cf97d820ea258460e96ad9cf5289054d895ceac47c"), 150 M: 8, 151 Nonce: mustHexDecode("00412b4ea9cdbe3c9696766cfa"), 152 }, 153 { 154 AESKey: aesKey13to24, 155 CipherText: mustHexDecode("63018f76dc8a1bcb4ccb1e7ca981befaa0726c55d378061298c85c92814abc33c52ee81d7d77c08a"), 156 ClearHeaderOctets: 8, 157 Data: mustHexDecode("63018f76dc8a1bcb9020ea6f91bdd85afa0039ba4baff9bfb79c7028949cd0ec"), 158 M: 8, 159 Nonce: mustHexDecode("0033568ef7b2633c9696766cfa"), 160 }, 161 { 162 AESKey: aesKey13to24, 163 CipherText: mustHexDecode("aa6cfa36cae86b40b1d23a2220ddc0ac900d9aa03c61fcf4a559a4417767089708a776796edb723506"), 164 ClearHeaderOctets: 8, 165 Data: mustHexDecode("aa6cfa36cae86b40b916e0eacc1c00d7dcec68ec0b3bbb1a02de8a2d1aa346132e"), 166 M: 8, 167 Nonce: mustHexDecode("00103fe41336713c9696766cfa"), 168 }, 169 { 170 AESKey: aesKey13to24, 171 CipherText: mustHexDecode("d0d0735c531e1becf049c24414d253c3967b70609b7cbb7c499160283245269a6f49975bcadeaf"), 172 ClearHeaderOctets: 12, 173 Data: mustHexDecode("d0d0735c531e1becf049c24412daac5630efa5396f770ce1a66b21f7b2101c"), 174 M: 8, 175 Nonce: mustHexDecode("00764c63b8058e3c9696766cfa"), 176 }, 177 { 178 AESKey: aesKey13to24, 179 CipherText: mustHexDecode("77b60f011c03e1525899bcae5545ff1a085ee2efbf52b2e04bee1e2336c73e3f762c0c7744fe7e3c"), 180 ClearHeaderOctets: 12, 181 Data: mustHexDecode("77b60f011c03e1525899bcaee88b6a46c78d63e52eb8c546efb5de6f75e9cc0d"), 182 M: 8, 183 Nonce: mustHexDecode("00f8b678094e3b3c9696766cfa"), 184 }, 185 { 186 AESKey: aesKey13to24, 187 CipherText: mustHexDecode("cd9044d2b71fdb8120ea60c0009769ecabdf48625594c59251e6035722675e04c847099e5ae0704551"), 188 ClearHeaderOctets: 12, 189 Data: mustHexDecode("cd9044d2b71fdb8120ea60c06435acbafb11a82e2f071d7ca4a5ebd93a803ba87f"), 190 M: 8, 191 Nonce: mustHexDecode("00d560912d3f703c9696766cfa"), 192 }, 193 { 194 AESKey: aesKey13to24, 195 CipherText: mustHexDecode("d85bc7e69f944fb8bc218daa947427b6db386a99ac1aef23ade0b52939cb6a637cf9bec2408897c6ba"), 196 ClearHeaderOctets: 8, 197 Data: mustHexDecode("d85bc7e69f944fb88a19b950bcf71a018e5e6701c91787659809d67dbedd18"), 198 M: 10, 199 Nonce: mustHexDecode("0042fff8f1951c3c9696766cfa"), 200 }, 201 { 202 AESKey: aesKey13to24, 203 CipherText: mustHexDecode("74a0ebc9069f5b375810e6fd25874022e80361a478e3e9cf484ab04f447efff6f0a477cc2fc9bf548944"), 204 ClearHeaderOctets: 8, 205 Data: mustHexDecode("74a0ebc9069f5b371761433c37c5a35fc1f39f406302eb907c6163be38c98437"), 206 M: 10, 207 Nonce: mustHexDecode("00920f40e56cdc3c9696766cfa"), 208 }, 209 { 210 AESKey: aesKey13to24, 211 CipherText: mustHexDecode("44a3aa3aae6475caf2beed7bc5098e83feb5b31608f8e29c38819a89c8e776f1544d4151a4ed3a8b87b9ce"), 212 ClearHeaderOctets: 8, 213 Data: mustHexDecode("44a3aa3aae6475caa434a8e58500c6e41530538862d686ea9e81301b5ae4226bfa"), 214 M: 10, 215 Nonce: mustHexDecode("0027ca0c7120bc3c9696766cfa"), 216 }, 217 { 218 AESKey: aesKey13to24, 219 CipherText: mustHexDecode("ec46bb63b02520c33c49fd7031d750a09da3ed7fddd49a2032aabf17ec8ebf7d22c8088c666be5c197"), 220 ClearHeaderOctets: 12, 221 Data: mustHexDecode("ec46bb63b02520c33c49fd70b96b49e21d621741632875db7f6c9243d2d7c2"), 222 M: 10, 223 Nonce: mustHexDecode("005b8ccbcd9af83c9696766cfa"), 224 }, 225 { 226 AESKey: aesKey13to24, 227 CipherText: mustHexDecode("47a65ac78b3d594227e85e71e882f1dbd38ce3eda7c23f04dd65071eb41342acdf7e00dccec7ae52987d"), 228 ClearHeaderOctets: 12, 229 Data: mustHexDecode("47a65ac78b3d594227e85e71e2fcfbb880442c731bf95167c8ffd7895e337076"), 230 M: 10, 231 Nonce: mustHexDecode("003ebe94044b9a3c9696766cfa"), 232 }, 233 { 234 AESKey: aesKey13to24, 235 CipherText: mustHexDecode("6e37a6ef546d955d34ab6059f32905b88a641b04b9c9ffb58cc390900f3da12ab16dce9e82efa16da62059"), 236 ClearHeaderOctets: 12, 237 Data: mustHexDecode("6e37a6ef546d955d34ab6059abf21c0b02feb88f856df4a37381bce3cc128517d4"), 238 M: 10, 239 Nonce: mustHexDecode("008d493b30ae8b3c9696766cfa"), 240 }, 241 } 242 243 if len(cases) != 24 { 244 t.Fatalf("Expected %d test cases, got: %d", 24, len(cases)) //nolint:revive 245 t.FailNow() //nolint:revive 246 } 247 248 for idx, c := range cases { 249 c := c 250 t.Run(fmt.Sprintf("packet vector #%d", idx+1), func(t *testing.T) { 251 blk, err := aes.NewCipher(c.AESKey) 252 if err != nil { 253 t.Fatalf("could not initialize AES block cipher from key: %v", err) 254 } 255 256 lccm, err := NewCCM(blk, c.M, len(c.Nonce)) 257 if err != nil { 258 t.Fatalf("could not create CCM: %v", err) 259 } 260 261 t.Run("seal", func(t *testing.T) { 262 var dst []byte 263 dst = lccm.Seal(dst, c.Nonce, c.Data[c.ClearHeaderOctets:], c.Data[:c.ClearHeaderOctets]) 264 if !bytes.Equal(c.CipherText[c.ClearHeaderOctets:], dst) { 265 t.Fatalf("ciphertext does not match, wanted %v, got %v", 266 c.CipherText[c.ClearHeaderOctets:], dst) 267 } 268 }) 269 270 t.Run("open", func(t *testing.T) { 271 var dst []byte 272 dst, err = lccm.Open(dst, c.Nonce, c.CipherText[c.ClearHeaderOctets:], c.CipherText[:c.ClearHeaderOctets]) 273 if err != nil { 274 t.Fatalf("failed to unseal: %v", err) 275 } 276 if !bytes.Equal(c.Data[c.ClearHeaderOctets:], dst) { 277 t.Fatalf("plaintext does not match, wanted %v, got %v", 278 c.Data[c.ClearHeaderOctets:], dst) 279 } 280 }) 281 }) 282 } 283 } 284 285 func TestNewCCMError(t *testing.T) { 286 cases := map[string]struct { 287 vector 288 err error 289 }{ 290 "ShortNonceLength": { 291 vector{ 292 AESKey: aesKey1to12, 293 M: 8, 294 Nonce: mustHexDecode("a0a1a2a3a4a5"), 295 }, errInvalidNonceSize, 296 }, 297 "LongNonceLength": { 298 vector{ 299 AESKey: aesKey1to12, 300 M: 8, 301 Nonce: mustHexDecode("0001020304050607080910111213"), 302 }, errInvalidNonceSize, 303 }, 304 "ShortTag": { 305 vector{ 306 AESKey: aesKey1to12, 307 M: 3, 308 Nonce: mustHexDecode("00010203040506070809101112"), 309 }, errInvalidTagSize, 310 }, 311 "LongTag": { 312 vector{ 313 AESKey: aesKey1to12, 314 M: 17, 315 Nonce: mustHexDecode("00010203040506070809101112"), 316 }, errInvalidTagSize, 317 }, 318 } 319 320 for name, c := range cases { 321 c := c 322 t.Run(name, func(t *testing.T) { 323 blk, err := aes.NewCipher(c.AESKey) 324 if err != nil { 325 t.Fatalf("could not initialize AES block cipher from key: %v", err) 326 } 327 328 if _, err := NewCCM(blk, c.M, len(c.Nonce)); !errors.Is(err, c.err) { 329 t.Fatalf("expected error '%v', got '%v'", c.err, err) 330 } 331 }) 332 } 333 } 334 335 func TestSealError(t *testing.T) { 336 cases := map[string]struct { 337 vector 338 err error 339 }{ 340 "InvalidNonceLength": { 341 vector{ 342 Data: mustHexDecode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e"), 343 M: 8, 344 Nonce: mustHexDecode("00000003020100a0a1a2a3a4"), // short 345 }, errInvalidNonceSize, 346 }, 347 "PlaintextTooLong": { 348 vector{ 349 Data: make([]byte, 100000), 350 M: 8, 351 Nonce: mustHexDecode("00000003020100a0a1a2a3a4a5"), 352 }, errPlaintextTooLong, 353 }, 354 } 355 356 blk, err := aes.NewCipher(aesKey1to12) 357 if err != nil { 358 t.Fatalf("could not initialize AES block cipher from key: %v", err) 359 } 360 361 lccm, err := NewCCM(blk, 8, 13) 362 if err != nil { 363 t.Fatalf("could not create CCM: %v", err) 364 } 365 366 for name, c := range cases { 367 c := c 368 t.Run(name, func(t *testing.T) { 369 defer func() { 370 err, ok := recover().(error) 371 if !ok { 372 t.Errorf("expected panic '%v', got '%v'", c.err, err) 373 } 374 375 if !errors.Is(err, c.err) { 376 t.Errorf("expected panic '%v', got '%v'", c.err, err) 377 } 378 }() 379 var dst []byte 380 _ = lccm.Seal(dst, c.Nonce, c.Data[c.ClearHeaderOctets:], c.Data[:c.ClearHeaderOctets]) 381 }) 382 } 383 } 384 385 func TestOpenError(t *testing.T) { 386 cases := map[string]struct { 387 vector 388 err error 389 }{ 390 "CiphertextTooShort": { 391 vector{ 392 CipherText: make([]byte, 10), 393 ClearHeaderOctets: 8, 394 Nonce: mustHexDecode("00000003020100a0a1a2a3a4a5"), 395 }, errCiphertextTooShort, 396 }, 397 "CiphertextTooLong": { 398 vector{ 399 CipherText: make([]byte, 100000), 400 ClearHeaderOctets: 8, 401 Nonce: mustHexDecode("00000003020100a0a1a2a3a4a5"), 402 }, errCiphertextTooLong, 403 }, 404 } 405 406 blk, err := aes.NewCipher(aesKey1to12) 407 if err != nil { 408 t.Fatalf("could not initialize AES block cipher from key: %v", err) 409 } 410 411 lccm, err := NewCCM(blk, 8, 13) 412 if err != nil { 413 t.Fatalf("could not create CCM: %v", err) 414 } 415 416 for name, c := range cases { 417 c := c 418 t.Run(name, func(t *testing.T) { 419 var dst []byte 420 _, err = lccm.Open(dst, c.Nonce, c.CipherText[c.ClearHeaderOctets:], c.CipherText[:c.ClearHeaderOctets]) 421 if !errors.Is(err, c.err) { 422 t.Errorf("expected error '%v', got '%v'", c.err, err) 423 } 424 }) 425 } 426 }