github.com/emmansun/gmsm@v0.29.1/cipher/benchmark_test.go (about) 1 package cipher_test 2 3 import ( 4 "crypto/aes" 5 "crypto/cipher" 6 "crypto/rand" 7 "io" 8 "testing" 9 10 smcipher "github.com/emmansun/gmsm/cipher" 11 "github.com/emmansun/gmsm/sm4" 12 ) 13 14 func BenchmarkSM4BCEncrypt1K(b *testing.B) { 15 var key [16]byte 16 c, _ := sm4.NewCipher(key[:]) 17 benchmarkBCEncrypt(b, c, make([]byte, 1024)) 18 } 19 20 func benchmarkBCEncrypt(b *testing.B, block cipher.Block, buf []byte) { 21 b.SetBytes(int64(len(buf))) 22 23 var iv [16]byte 24 bc := smcipher.NewBCEncrypter(block, iv[:]) 25 for i := 0; i < b.N; i++ { 26 bc.CryptBlocks(buf, buf) 27 } 28 } 29 30 func BenchmarkSM4BCDecrypt1K(b *testing.B) { 31 var key [16]byte 32 c, _ := sm4.NewCipher(key[:]) 33 benchmarkBCDecrypt(b, c, make([]byte, 1024)) 34 } 35 36 func benchmarkBCDecrypt(b *testing.B, block cipher.Block, buf []byte) { 37 b.SetBytes(int64(len(buf))) 38 39 var iv [16]byte 40 bc := smcipher.NewBCDecrypter(block, iv[:]) 41 for i := 0; i < b.N; i++ { 42 bc.CryptBlocks(buf, buf) 43 } 44 } 45 46 func BenchmarkSM4HCTREncrypt1K(b *testing.B) { 47 var key [16]byte 48 var tweak [32]byte 49 c, _ := sm4.NewCipher(key[:]) 50 io.ReadFull(rand.Reader, tweak[:]) 51 hctr, _ := smcipher.NewHCTR(c, tweak[:16], tweak[16:]) 52 buf := make([]byte, 1024) 53 b.SetBytes(int64(len(buf))) 54 b.ResetTimer() 55 for i := 0; i < b.N; i++ { 56 hctr.EncryptBytes(buf, buf) 57 } 58 } 59 60 func benchmarkECBEncrypt(b *testing.B, block cipher.Block, buf []byte) { 61 b.SetBytes(int64(len(buf))) 62 63 ecb := smcipher.NewECBEncrypter(block) 64 for i := 0; i < b.N; i++ { 65 ecb.CryptBlocks(buf, buf) 66 } 67 } 68 69 func BenchmarkSM4ECBEncrypt1K(b *testing.B) { 70 var key [16]byte 71 c, _ := sm4.NewCipher(key[:]) 72 benchmarkECBEncrypt(b, c, make([]byte, 1024)) 73 } 74 75 func BenchmarkAES128ECBEncrypt1K(b *testing.B) { 76 var key [16]byte 77 c, _ := aes.NewCipher(key[:]) 78 benchmarkECBEncrypt(b, c, make([]byte, 1024)) 79 } 80 81 func benchmarkCBCEncrypt(b *testing.B, block cipher.Block, buf []byte) { 82 b.SetBytes(int64(len(buf))) 83 84 var iv [16]byte 85 cbc := cipher.NewCBCEncrypter(block, iv[:]) 86 for i := 0; i < b.N; i++ { 87 cbc.CryptBlocks(buf, buf) 88 } 89 } 90 91 func BenchmarkAESCBCEncrypt1K(b *testing.B) { 92 var key [16]byte 93 c, _ := aes.NewCipher(key[:]) 94 benchmarkCBCEncrypt(b, c, make([]byte, 1024)) 95 } 96 97 func BenchmarkSM4CBCEncrypt1K(b *testing.B) { 98 var key [16]byte 99 c, _ := sm4.NewCipher(key[:]) 100 benchmarkCBCEncrypt(b, c, make([]byte, 1024)) 101 } 102 103 func BenchmarkSM4CBCEncrypt8K(b *testing.B) { 104 var key [16]byte 105 c, _ := sm4.NewCipher(key[:]) 106 benchmarkCBCEncrypt(b, c, make([]byte, 8*1024)) 107 } 108 109 func benchmarkCBCDecrypt(b *testing.B, block cipher.Block, buf []byte) { 110 b.SetBytes(int64(len(buf))) 111 112 var iv [16]byte 113 cbc := cipher.NewCBCDecrypter(block, iv[:]) 114 for i := 0; i < b.N; i++ { 115 cbc.CryptBlocks(buf, buf) 116 } 117 } 118 119 func BenchmarkAESCBCDecrypt1K(b *testing.B) { 120 var key [16]byte 121 c, _ := aes.NewCipher(key[:]) 122 benchmarkCBCDecrypt(b, c, make([]byte, 1024)) 123 } 124 125 func BenchmarkSM4CBCDecrypt1K(b *testing.B) { 126 var key [16]byte 127 c, _ := sm4.NewCipher(key[:]) 128 benchmarkCBCDecrypt(b, c, make([]byte, 1024)) 129 } 130 131 func benchmarkStream(b *testing.B, block cipher.Block, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) { 132 b.SetBytes(int64(len(buf))) 133 134 //var key [16]byte 135 var iv [16]byte 136 //c, _ := sm4.NewCipher(key[:]) 137 stream := mode(block, iv[:]) 138 139 b.ResetTimer() 140 for i := 0; i < b.N; i++ { 141 stream.XORKeyStream(buf, buf) 142 } 143 } 144 145 func benchmarkSM4Stream(b *testing.B, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) { 146 b.SetBytes(int64(len(buf))) 147 148 var key [16]byte 149 c, _ := sm4.NewCipher(key[:]) 150 benchmarkStream(b, c, mode, buf) 151 } 152 153 func benchmarkAESStream(b *testing.B, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) { 154 b.SetBytes(int64(len(buf))) 155 156 var key [16]byte 157 c, _ := aes.NewCipher(key[:]) 158 benchmarkStream(b, c, mode, buf) 159 } 160 161 // If we test exactly 1K blocks, we would generate exact multiples of 162 // the cipher's block size, and the cipher stream fragments would 163 // always be wordsize aligned, whereas non-aligned is a more typical 164 // use-case. 165 const almost1K = 1024 - 5 166 const almost8K = 8*1024 - 5 167 168 func BenchmarkAESCFBEncrypt1K(b *testing.B) { 169 benchmarkAESStream(b, cipher.NewCFBEncrypter, make([]byte, almost1K)) 170 } 171 172 func BenchmarkSM4CFBEncrypt1K(b *testing.B) { 173 benchmarkSM4Stream(b, cipher.NewCFBEncrypter, make([]byte, almost1K)) 174 } 175 176 func BenchmarkAESCFBDecrypt1K(b *testing.B) { 177 benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost1K)) 178 } 179 180 func BenchmarkSM4CFBDecrypt1K(b *testing.B) { 181 benchmarkSM4Stream(b, cipher.NewCFBDecrypter, make([]byte, almost1K)) 182 } 183 184 func BenchmarkAESCFBDecrypt8K(b *testing.B) { 185 benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost8K)) 186 } 187 188 func BenchmarkSM4CFBDecrypt8K(b *testing.B) { 189 benchmarkSM4Stream(b, cipher.NewCFBDecrypter, make([]byte, almost8K)) 190 } 191 192 func BenchmarkAESOFB1K(b *testing.B) { 193 benchmarkAESStream(b, cipher.NewOFB, make([]byte, almost1K)) 194 } 195 196 func BenchmarkSM4OFB1K(b *testing.B) { 197 benchmarkSM4Stream(b, cipher.NewOFB, make([]byte, almost1K)) 198 } 199 200 func BenchmarkAESCTR1K(b *testing.B) { 201 benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost1K)) 202 } 203 204 func BenchmarkSM4CTR1K(b *testing.B) { 205 benchmarkSM4Stream(b, cipher.NewCTR, make([]byte, almost1K)) 206 } 207 208 func BenchmarkAESCTR8K(b *testing.B) { 209 benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost8K)) 210 } 211 212 func BenchmarkSM4CTR8K(b *testing.B) { 213 benchmarkSM4Stream(b, cipher.NewCTR, make([]byte, almost8K)) 214 } 215 216 func benchmarkGCMSign(b *testing.B, aead cipher.AEAD, buf []byte) { 217 b.SetBytes(int64(len(buf))) 218 219 var nonce [12]byte 220 var out []byte 221 222 b.ResetTimer() 223 for i := 0; i < b.N; i++ { 224 out = aead.Seal(out[:0], nonce[:], nil, buf) 225 } 226 } 227 228 func benchmarkAESGCMSign(b *testing.B, buf []byte) { 229 var key [16]byte 230 c, _ := aes.NewCipher(key[:]) 231 aesgcm, _ := cipher.NewGCM(c) 232 benchmarkGCMSign(b, aesgcm, buf) 233 } 234 235 func benchmarkSM4GCMSign(b *testing.B, buf []byte) { 236 var key [16]byte 237 c, _ := sm4.NewCipher(key[:]) 238 sm4gcm, _ := cipher.NewGCM(c) 239 benchmarkGCMSign(b, sm4gcm, buf) 240 } 241 242 func benchmarkGCMSeal(b *testing.B, aead cipher.AEAD, buf []byte) { 243 b.SetBytes(int64(len(buf))) 244 245 var nonce [12]byte 246 var ad [13]byte 247 var out []byte 248 249 b.ResetTimer() 250 for i := 0; i < b.N; i++ { 251 out = aead.Seal(out[:0], nonce[:], buf, ad[:]) 252 } 253 } 254 255 func benchmarkAESGCMSeal(b *testing.B, buf []byte) { 256 var key [16]byte 257 c, _ := aes.NewCipher(key[:]) 258 sm4gcm, _ := cipher.NewGCM(c) 259 benchmarkGCMSeal(b, sm4gcm, buf) 260 } 261 262 func benchmarkSM4GCMSeal(b *testing.B, buf []byte) { 263 var key [16]byte 264 c, _ := sm4.NewCipher(key[:]) 265 sm4gcm, _ := cipher.NewGCM(c) 266 benchmarkGCMSeal(b, sm4gcm, buf) 267 } 268 269 func benchmarkGCMOpen(b *testing.B, aead cipher.AEAD, buf []byte) { 270 b.SetBytes(int64(len(buf))) 271 272 var nonce [12]byte 273 var ad [13]byte 274 var out []byte 275 out = aead.Seal(out[:0], nonce[:], buf, ad[:]) 276 277 b.ResetTimer() 278 for i := 0; i < b.N; i++ { 279 _, err := aead.Open(buf[:0], nonce[:], out, ad[:]) 280 if err != nil { 281 b.Errorf("Open: %v", err) 282 } 283 } 284 } 285 286 func benchmarkAESGCMOpen(b *testing.B, buf []byte) { 287 var key [16]byte 288 c, _ := aes.NewCipher(key[:]) 289 sm4gcm, _ := cipher.NewGCM(c) 290 benchmarkGCMOpen(b, sm4gcm, buf) 291 } 292 293 func benchmarkSM4GCMOpen(b *testing.B, buf []byte) { 294 var key [16]byte 295 c, _ := sm4.NewCipher(key[:]) 296 sm4gcm, _ := cipher.NewGCM(c) 297 benchmarkGCMOpen(b, sm4gcm, buf) 298 } 299 300 func BenchmarkAESGCMSeal1K(b *testing.B) { 301 benchmarkAESGCMSeal(b, make([]byte, 1024)) 302 } 303 304 func BenchmarkSM4GCMSeal1K(b *testing.B) { 305 benchmarkSM4GCMSeal(b, make([]byte, 1024)) 306 } 307 308 func BenchmarkAESGCMOpen1K(b *testing.B) { 309 benchmarkAESGCMOpen(b, make([]byte, 1024)) 310 } 311 312 func BenchmarkSM4GCMOpen1K(b *testing.B) { 313 benchmarkSM4GCMOpen(b, make([]byte, 1024)) 314 } 315 316 func BenchmarkAESGCMSign1K(b *testing.B) { 317 benchmarkAESGCMSign(b, make([]byte, 1024)) 318 } 319 320 func BenchmarkSM4GCMSign1K(b *testing.B) { 321 benchmarkSM4GCMSign(b, make([]byte, 1024)) 322 } 323 324 func BenchmarkAESGCMSign8K(b *testing.B) { 325 benchmarkAESGCMSign(b, make([]byte, 8*1024)) 326 } 327 328 func BenchmarkSM4GCMSign8K(b *testing.B) { 329 benchmarkSM4GCMSign(b, make([]byte, 8*1024)) 330 } 331 332 func BenchmarkAESGCMSeal8K(b *testing.B) { 333 benchmarkAESGCMSeal(b, make([]byte, 8*1024)) 334 } 335 336 func BenchmarkSM4GCMSeal8K(b *testing.B) { 337 benchmarkSM4GCMSeal(b, make([]byte, 8*1024)) 338 } 339 340 func BenchmarkAESGCMOpen8K(b *testing.B) { 341 benchmarkAESGCMOpen(b, make([]byte, 8*1024)) 342 } 343 344 func BenchmarkSM4GCMOpen8K(b *testing.B) { 345 benchmarkSM4GCMOpen(b, make([]byte, 8*1024)) 346 } 347 348 func benchmarkAESCCMSign(b *testing.B, buf []byte) { 349 var key [16]byte 350 c, _ := aes.NewCipher(key[:]) 351 aesccm, _ := smcipher.NewCCM(c) 352 benchmarkGCMSign(b, aesccm, buf) 353 } 354 355 func benchmarkSM4CCMSign(b *testing.B, buf []byte) { 356 var key [16]byte 357 c, _ := sm4.NewCipher(key[:]) 358 sm4ccm, _ := smcipher.NewCCM(c) 359 benchmarkGCMSign(b, sm4ccm, buf) 360 } 361 362 func BenchmarkAESCCMSign1K(b *testing.B) { 363 benchmarkAESCCMSign(b, make([]byte, 1024)) 364 } 365 366 func BenchmarkSM4CCMSign1K(b *testing.B) { 367 benchmarkSM4CCMSign(b, make([]byte, 1024)) 368 } 369 370 func BenchmarkAESCCMSeal1K(b *testing.B) { 371 benchmarkAESCCMSeal(b, make([]byte, 1024)) 372 } 373 374 func BenchmarkSM4CCMSeal1K(b *testing.B) { 375 benchmarkSM4CCMSeal(b, make([]byte, 1024)) 376 } 377 378 func BenchmarkAESCCMOpen1K(b *testing.B) { 379 benchmarkAESCCMOpen(b, make([]byte, 1024)) 380 } 381 382 func BenchmarkSM4CCMOpen1K(b *testing.B) { 383 benchmarkSM4CCMOpen(b, make([]byte, 1024)) 384 } 385 386 func BenchmarkAESCCMSign8K(b *testing.B) { 387 benchmarkAESCCMSign(b, make([]byte, 8*1024)) 388 } 389 390 func BenchmarkSM4CCMSign8K(b *testing.B) { 391 benchmarkSM4CCMSign(b, make([]byte, 8*1024)) 392 } 393 394 func BenchmarkAESCCMSeal8K(b *testing.B) { 395 benchmarkAESCCMSeal(b, make([]byte, 8*1024)) 396 } 397 398 func BenchmarkSM4CCMSeal8K(b *testing.B) { 399 benchmarkSM4CCMSeal(b, make([]byte, 8*1024)) 400 } 401 402 func BenchmarkAESCCMOpen8K(b *testing.B) { 403 benchmarkAESCCMOpen(b, make([]byte, 8*1024)) 404 } 405 406 func BenchmarkSM4CCMOpen8K(b *testing.B) { 407 benchmarkSM4CCMOpen(b, make([]byte, 8*1024)) 408 } 409 410 func benchmarkAESCCMSeal(b *testing.B, buf []byte) { 411 var key [16]byte 412 c, _ := aes.NewCipher(key[:]) 413 sm4gcm, _ := smcipher.NewCCM(c) 414 benchmarkGCMSeal(b, sm4gcm, buf) 415 } 416 417 func benchmarkSM4CCMSeal(b *testing.B, buf []byte) { 418 var key [16]byte 419 c, _ := sm4.NewCipher(key[:]) 420 sm4gcm, _ := smcipher.NewCCM(c) 421 benchmarkGCMSeal(b, sm4gcm, buf) 422 } 423 424 func benchmarkAESCCMOpen(b *testing.B, buf []byte) { 425 var key [16]byte 426 c, _ := aes.NewCipher(key[:]) 427 sm4gcm, _ := smcipher.NewCCM(c) 428 benchmarkGCMOpen(b, sm4gcm, buf) 429 } 430 431 func benchmarkSM4CCMOpen(b *testing.B, buf []byte) { 432 var key [16]byte 433 c, _ := sm4.NewCipher(key[:]) 434 sm4gcm, _ := smcipher.NewCCM(c) 435 benchmarkGCMOpen(b, sm4gcm, buf) 436 } 437 438 func benchmarkXTS(b *testing.B, isGB bool, cipherFunc func([]byte) (cipher.Block, error), length, keylen int64) { 439 plaintext := make([]byte, length) 440 encrypted := make([]byte, length) 441 var c cipher.BlockMode 442 var err error 443 if !isGB { 444 c, err = smcipher.NewXTSEncrypterWithSector(cipherFunc, make([]byte, keylen), make([]byte, keylen), 0) 445 if err != nil { 446 b.Fatalf("NewCipher failed: %s", err) 447 } 448 } else { 449 c, err = smcipher.NewGBXTSEncrypterWithSector(cipherFunc, make([]byte, keylen), make([]byte, keylen), 0) 450 if err != nil { 451 b.Fatalf("NewCipher failed: %s", err) 452 } 453 } 454 455 //decrypted := make([]byte, length) 456 b.SetBytes(int64(len(plaintext))) 457 b.ResetTimer() 458 for i := 0; i < b.N; i++ { 459 c.CryptBlocks(encrypted, plaintext) 460 //c.Decrypt(decrypted, encrypted[:len(plaintext)], 0) 461 } 462 } 463 464 func BenchmarkAES128XTSEncrypt512(b *testing.B) { 465 benchmarkXTS(b, false, aes.NewCipher, 512, 16) 466 } 467 468 func BenchmarkAES128XTSEncrypt1K(b *testing.B) { 469 benchmarkXTS(b, false, aes.NewCipher, 1024, 16) 470 } 471 472 func BenchmarkAES128XTSEncrypt4K(b *testing.B) { 473 benchmarkXTS(b, false, aes.NewCipher, 4096, 16) 474 } 475 476 func BenchmarkAES256XTSEncrypt512(b *testing.B) { 477 benchmarkXTS(b, false, aes.NewCipher, 512, 32) 478 } 479 480 func BenchmarkAES256XTSEncrypt1K(b *testing.B) { 481 benchmarkXTS(b, false, aes.NewCipher, 1024, 32) 482 } 483 484 func BenchmarkAES256XTSEncrypt4K(b *testing.B) { 485 benchmarkXTS(b, false, aes.NewCipher, 4096, 32) 486 } 487 488 func BenchmarkSM4XTSEncrypt512(b *testing.B) { 489 benchmarkXTS(b, false, sm4.NewCipher, 512, 16) 490 } 491 492 func BenchmarkSM4XTSEncrypt1K(b *testing.B) { 493 benchmarkXTS(b, false, sm4.NewCipher, 1024, 16) 494 } 495 496 func BenchmarkSM4XTSEncrypt4K(b *testing.B) { 497 benchmarkXTS(b, false, sm4.NewCipher, 4096, 16) 498 } 499 500 func BenchmarkSM4XTSEncrypt512_GB(b *testing.B) { 501 benchmarkXTS(b, true, sm4.NewCipher, 512, 16) 502 } 503 504 func BenchmarkSM4XTSEncrypt1K_GB(b *testing.B) { 505 benchmarkXTS(b, true, sm4.NewCipher, 1024, 16) 506 } 507 508 func BenchmarkSM4XTSEncrypt4K_GB(b *testing.B) { 509 benchmarkXTS(b, true, sm4.NewCipher, 4096, 16) 510 } 511 512 func benchmarkXTS_Decrypt(b *testing.B, isGB bool, cipherFunc func([]byte) (cipher.Block, error), length, keylen int64) { 513 plaintext := make([]byte, length) 514 encrypted := make([]byte, length) 515 var c cipher.BlockMode 516 var err error 517 if !isGB { 518 c, err = smcipher.NewXTSDecrypterWithSector(cipherFunc, make([]byte, keylen), make([]byte, keylen), 0) 519 if err != nil { 520 b.Fatalf("NewCipher failed: %s", err) 521 } 522 } else { 523 c, err = smcipher.NewGBXTSDecrypterWithSector(cipherFunc, make([]byte, keylen), make([]byte, keylen), 0) 524 if err != nil { 525 b.Fatalf("NewCipher failed: %s", err) 526 } 527 } 528 529 b.SetBytes(int64(len(plaintext))) 530 b.ResetTimer() 531 for i := 0; i < b.N; i++ { 532 c.CryptBlocks(plaintext, encrypted) 533 } 534 } 535 536 func BenchmarkAES128XTSDecrypt512(b *testing.B) { 537 benchmarkXTS_Decrypt(b, false, aes.NewCipher, 512, 16) 538 } 539 540 func BenchmarkAES128XTSDecrypt1K(b *testing.B) { 541 benchmarkXTS_Decrypt(b, false, aes.NewCipher, 1024, 16) 542 } 543 544 func BenchmarkAES128XTSDecrypt4K(b *testing.B) { 545 benchmarkXTS_Decrypt(b, false, aes.NewCipher, 4096, 16) 546 } 547 548 func BenchmarkAES256XTSDecrypt512(b *testing.B) { 549 benchmarkXTS_Decrypt(b, false, aes.NewCipher, 512, 32) 550 } 551 552 func BenchmarkAES256XTSDecrypt1K(b *testing.B) { 553 benchmarkXTS_Decrypt(b, false, aes.NewCipher, 1024, 32) 554 } 555 556 func BenchmarkAES256XTSDecrypt4K(b *testing.B) { 557 benchmarkXTS_Decrypt(b, false, aes.NewCipher, 4096, 32) 558 } 559 560 func BenchmarkSM4XTSDecrypt512(b *testing.B) { 561 benchmarkXTS_Decrypt(b, false, sm4.NewCipher, 512, 16) 562 } 563 564 func BenchmarkSM4XTSDecrypt1K(b *testing.B) { 565 benchmarkXTS_Decrypt(b, false, sm4.NewCipher, 1024, 16) 566 } 567 568 func BenchmarkSM4XTSDecrypt4K(b *testing.B) { 569 benchmarkXTS_Decrypt(b, false, sm4.NewCipher, 4096, 16) 570 } 571 572 func BenchmarkSM4XTSDecrypt512_GB(b *testing.B) { 573 benchmarkXTS_Decrypt(b, true, sm4.NewCipher, 512, 16) 574 } 575 576 func BenchmarkSM4XTSDecrypt1K_GB(b *testing.B) { 577 benchmarkXTS_Decrypt(b, true, sm4.NewCipher, 1024, 16) 578 } 579 580 func BenchmarkSM4XTSDecrypt4K_GB(b *testing.B) { 581 benchmarkXTS_Decrypt(b, true, sm4.NewCipher, 4096, 16) 582 }