sm4_aesni_avxtest.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/hex.h>
  13. #include <gmssl/sm4.h>
  14. #include <gmssl/error.h>
  15. #include <gmssl/rand.h>
  16. extern void sm4_aesni_avx_encrypt(const uint32_t rk[32], const uint8_t in[16 * 4], uint8_t out[16 * 4]);
  17. static int test_sm4_aesni_avx(void)
  18. {
  19. const uint32_t rk[32] = {
  20. 0xf12186f9, 0x41662b61, 0x5a6ab19a, 0x7ba92077,
  21. 0x367360f4, 0x776a0c61, 0xb6bb89b3, 0x24763151,
  22. 0xa520307c, 0xb7584dbd, 0xc30753ed, 0x7ee55b57,
  23. 0x6988608c, 0x30d895b7, 0x44ba14af, 0x104495a1,
  24. 0xd120b428, 0x73b55fa3, 0xcc874966, 0x92244439,
  25. 0xe89e641f, 0x98ca015a, 0xc7159060, 0x99e1fd2e,
  26. 0xb79bd80c, 0x1d2115b0, 0x0e228aeb, 0xf1780c81,
  27. 0x428d3654, 0x62293496, 0x01cf72e5, 0x9124a012,
  28. };
  29. const uint8_t plaintext[16 * 4] = {
  30. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  31. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  32. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  33. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  34. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  35. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  36. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  37. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  38. };
  39. const uint8_t ciphertext[16 * 4] = {
  40. 0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
  41. 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
  42. 0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
  43. 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
  44. 0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
  45. 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
  46. 0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
  47. 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
  48. };
  49. const uint8_t ciphertext1m[16 * 4] = {
  50. 0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
  51. 0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66,
  52. 0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
  53. 0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66,
  54. 0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
  55. 0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66,
  56. 0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
  57. 0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66,
  58. };
  59. uint8_t buf[16 * 4];
  60. int i;
  61. // test encrypt once
  62. sm4_aesni_avx_encrypt(rk, plaintext, buf);
  63. if (memcmp(buf, ciphertext, sizeof(ciphertext)) != 0) {
  64. fprintf(stderr, "%s %d: %s error\n", __FILE__, __LINE__, __FUNCTION__);
  65. return -1;
  66. }
  67. // test encrypt 1000000 times
  68. memcpy(buf, plaintext, sizeof(plaintext));
  69. for (i = 0; i < 1000000; i++) {
  70. sm4_aesni_avx_encrypt(rk, buf, buf);
  71. }
  72. if (memcmp(buf, ciphertext1m, sizeof(ciphertext1m)) != 0) {
  73. fprintf(stderr, "%s %d: %s 1 million times error\n", __FILE__, __LINE__, __FUNCTION__);
  74. return -1;
  75. }
  76. printf("%s() ok\n", __FUNCTION__);
  77. return 1;
  78. }
  79. int main(void)
  80. {
  81. if (test_sm4_aesni_avx() != 1) goto err;
  82. printf("%s all tests passed\n", __FILE__);
  83. return 0;
  84. err:
  85. error_print();
  86. return 1;
  87. }