tls13test.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/oid.h>
  13. #include <gmssl/x509.h>
  14. #include <gmssl/rand.h>
  15. #include <gmssl/error.h>
  16. #include <gmssl/tls.h>
  17. #include <gmssl/sm3.h>
  18. #include <gmssl/sm4.h>
  19. static int test_tls13_gcm(void)
  20. {
  21. BLOCK_CIPHER_KEY block_key;
  22. uint8_t key[16];
  23. uint8_t iv[12];
  24. uint8_t seq_num[8] = {0,0,0,0,0,0,0,1};
  25. int record_type = TLS_record_handshake;
  26. uint8_t in[40];
  27. size_t padding_len = 8;
  28. uint8_t out[256];
  29. size_t outlen;
  30. uint8_t buf[256];
  31. size_t buflen;
  32. rand_bytes(key, sizeof(key));
  33. rand_bytes(iv, sizeof(iv));
  34. rand_bytes(in, sizeof(in));
  35. memset(out, 1, sizeof(out));
  36. outlen = 0;
  37. memset(buf, 1, sizeof(buf));
  38. buflen = 0;
  39. if (block_cipher_set_encrypt_key(&block_key, BLOCK_CIPHER_sm4(), key) != 1) {
  40. error_print();
  41. return -1;
  42. }
  43. if (tls13_gcm_encrypt(&block_key, iv, seq_num, record_type, in, sizeof(in), padding_len, out, &outlen) != 1) {
  44. error_print();
  45. return -1;
  46. }
  47. if (tls13_gcm_decrypt(&block_key, iv, seq_num, out, outlen, &record_type, buf, &buflen) != 1) {
  48. error_print();
  49. return -1;
  50. }
  51. if (buflen != sizeof(in)) {
  52. error_print();
  53. return -1;
  54. }
  55. if (memcmp(in, buf, buflen) != 0) {
  56. error_print();
  57. return -1;
  58. }
  59. printf("%s() ok\n", __FUNCTION__);
  60. return 1;
  61. }
  62. int main(void)
  63. {
  64. if (test_tls13_gcm() != 1) goto err;
  65. printf("%s all tests passed\n", __FILE__);
  66. return 0;
  67. err:
  68. error_print();
  69. return -1;
  70. }