gcmtest.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/gcm.h>
  13. #include <gmssl/hex.h>
  14. #include <gmssl/rand.h>
  15. #include <gmssl/block_cipher.h>
  16. #include <gmssl/error.h>
  17. struct {
  18. char *H;
  19. char *A;
  20. char *C;
  21. char *T;
  22. } ghash_tests[] = {
  23. // test 1
  24. {
  25. "66e94bd4ef8a2c3b884cfa59ca342b2e",
  26. "",
  27. "",
  28. "00000000000000000000000000000000",
  29. },
  30. // test 2
  31. {
  32. "66e94bd4ef8a2c3b884cfa59ca342b2e",
  33. "",
  34. "0388dace60b6a392f328c2b971b2fe78",
  35. "f38cbb1ad69223dcc3457ae5b6b0f885",
  36. },
  37. // test 3
  38. {
  39. "b83b533708bf535d0aa6e52980d53b78",
  40. "",
  41. "42831ec2217774244b7221b784d0d49c"
  42. "e3aa212f2c02a4e035c17e2329aca12e"
  43. "21d514b25466931c7d8f6a5aac84aa05"
  44. "1ba30b396a0aac973d58e091473f5985",
  45. "7f1b32b81b820d02614f8895ac1d4eac",
  46. },
  47. // test 4
  48. {
  49. "b83b533708bf535d0aa6e52980d53b78",
  50. "feedfacedeadbeeffeedfacedeadbeef"
  51. "abaddad2",
  52. "42831ec2217774244b7221b784d0d49c"
  53. "e3aa212f2c02a4e035c17e2329aca12e"
  54. "21d514b25466931c7d8f6a5aac84aa05"
  55. "1ba30b396a0aac973d58e091",
  56. "698e57f70e6ecc7fd9463b7260a9ae5f",
  57. },
  58. // test 5
  59. {
  60. "b83b533708bf535d0aa6e52980d53b78",
  61. "feedfacedeadbeeffeedfacedeadbeef"
  62. "abaddad2",
  63. "61353b4c2806934a777ff51fa22a4755"
  64. "699b2a714fcdc6f83766e5f97b6c7423"
  65. "73806900e49f24b22b097544d4896b42"
  66. "4989b5e1ebac0f07c23f4598",
  67. "df586bb4c249b92cb6922877e444d37b",
  68. },
  69. // test 6
  70. {
  71. "b83b533708bf535d0aa6e52980d53b78",
  72. "feedfacedeadbeeffeedfacedeadbeef"
  73. "abaddad2",
  74. "8ce24998625615b603a033aca13fb894"
  75. "be9112a5c3a211a8ba262a3cca7e2ca7"
  76. "01e4a9a4fba43c90ccdcb281d48c7c6f"
  77. "d62875d2aca417034c34aee5",
  78. "1c5afe9760d3932f3c9a878aac3dc3de",
  79. },
  80. };
  81. int test_ghash(void)
  82. {
  83. uint8_t H[16];
  84. uint8_t A[32];
  85. uint8_t C[64];
  86. uint8_t T[16];
  87. uint8_t out[16];
  88. size_t Hlen, Alen, Clen, Tlen;
  89. int i;
  90. for (i = 0; i < sizeof(ghash_tests)/sizeof(ghash_tests[0]); i++) {
  91. hex_to_bytes(ghash_tests[i].H, strlen(ghash_tests[i].H), H, &Hlen);
  92. hex_to_bytes(ghash_tests[i].A, strlen(ghash_tests[i].A), A, &Alen);
  93. hex_to_bytes(ghash_tests[i].C, strlen(ghash_tests[i].C), C, &Clen);
  94. hex_to_bytes(ghash_tests[i].T, strlen(ghash_tests[i].T), T, &Tlen);
  95. ghash(H, A, Alen, C, Clen, out);
  96. if (memcmp(out, T, Tlen) != 0) {
  97. format_print(stderr, 0, 0, "test %d failed\n", i + 1);
  98. format_print(stderr, 0, 2, "H = %s\n", ghash_tests[i].H);
  99. format_print(stderr, 0, 2, "A = %s\n", ghash_tests[i].A);
  100. format_print(stderr, 0, 2, "C = %s\n", ghash_tests[i].C);
  101. format_bytes(stderr, 0, 2, "GHASH(H,A,C) = ", out, 16);
  102. format_print(stderr, 0, 2, " = %s\n\n", ghash_tests[i].T);
  103. }
  104. }
  105. printf("%s() ok\n", __FUNCTION__);
  106. return 1;
  107. }
  108. int test_gcm(void)
  109. {
  110. BLOCK_CIPHER_KEY block_key;
  111. uint8_t key[16];
  112. uint8_t iv[12];
  113. uint8_t aad[64];
  114. uint8_t in[100];
  115. uint8_t out[sizeof(in)];
  116. uint8_t buf[sizeof(in)];
  117. uint8_t tag[16];
  118. rand_bytes(key, sizeof(key));
  119. rand_bytes(iv, sizeof(iv));
  120. rand_bytes(aad, sizeof(aad));
  121. rand_bytes(in, sizeof(in));
  122. memset(out, 0, sizeof(out));
  123. memset(buf, 0, sizeof(buf));
  124. memset(tag, 0, sizeof(tag));
  125. if (block_cipher_set_encrypt_key(&block_key, BLOCK_CIPHER_aes128(), key) != 1) {
  126. error_print();
  127. return -1;
  128. }
  129. if (gcm_encrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), in, sizeof(in), out, sizeof(tag), tag) != 1) {
  130. error_print();
  131. return -1;
  132. }
  133. if (gcm_decrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), out, sizeof(out), tag, sizeof(tag), buf) != 1) {
  134. error_print();
  135. return -1;
  136. }
  137. if (memcmp(buf, in, sizeof(in)) != 0) {
  138. error_print();
  139. return -1;
  140. }
  141. memset(out, 0, sizeof(out));
  142. memset(buf, 0, sizeof(buf));
  143. memset(tag, 0, sizeof(tag));
  144. if (block_cipher_set_encrypt_key(&block_key, BLOCK_CIPHER_sm4(), key) != 1) {
  145. error_print();
  146. return -1;
  147. }
  148. if (gcm_encrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), in, sizeof(in), out, sizeof(tag), tag) != 1) {
  149. error_print();
  150. return -1;
  151. }
  152. if (gcm_decrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), out, sizeof(out), tag, sizeof(tag), buf) != 1) {
  153. error_print();
  154. return -1;
  155. }
  156. if (memcmp(buf, in, sizeof(in)) != 0) {
  157. error_print();
  158. return -1;
  159. }
  160. printf("%s() ok\n", __FUNCTION__);
  161. return 1;
  162. }
  163. int main(int argc, char **argv)
  164. {
  165. if (test_ghash() != 1) goto err;
  166. if (test_gcm() != 1) goto err;
  167. printf("%s all tests passed\n", __FILE__);
  168. return 0;
  169. err:
  170. error_print();
  171. return -1;
  172. }