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 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <gmssl/sm9.h>
14 #include <gmssl/error.h>
15
16
main(void)17 int main(void)
18 {
19 SM9_ENC_MASTER_KEY master;
20 SM9_ENC_MASTER_KEY master_public;
21 SM9_ENC_KEY key;
22 const char *id = "Alice";
23 uint8_t buf[512];
24 uint8_t *p = buf;
25 const uint8_t *cp = buf;
26 size_t len;
27 char mbuf[256];
28 size_t mlen;
29 int ret;
30
31 sm9_enc_master_key_generate(&master);
32 sm9_enc_master_key_extract_key(&master, id, strlen(id), &key);
33
34 sm9_enc_master_public_key_to_der(&master, &p, &len);
35 sm9_enc_master_public_key_from_der(&master_public, &cp, &len);
36
37 sm9_encrypt(&master_public, id, strlen(id), (uint8_t *)"hello", strlen("hello"), buf, &len);
38 ret = sm9_decrypt(&key, id, strlen(id), buf, len, (uint8_t *)mbuf, &mlen);
39 if (ret != 1) {
40 fprintf(stderr, "decrypt failed\n");
41 return 1;
42 }
43 mbuf[mlen] = 0;
44 printf("decrypt result: %s\n", mbuf);
45
46 return 0;
47 }
48