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