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_SIGN_MASTER_KEY sign_master;
20 SM9_SIGN_MASTER_KEY sign_master_public;
21 SM9_SIGN_KEY sign_key;
22 SM9_SIGN_CTX sign_ctx;
23 const char *id = "Alice";
24 uint8_t sig[SM9_SIGNATURE_SIZE];
25 size_t siglen;
26 uint8_t buf[512];
27 uint8_t *p = buf;
28 const uint8_t *cp = buf;
29 size_t len;
30 int ret;
31
32 sm9_sign_master_key_generate(&sign_master);
33
34 sm9_sign_master_key_extract_key(&sign_master, id, strlen(id), &sign_key);
35
36 sm9_sign_init(&sign_ctx);
37 sm9_sign_update(&sign_ctx, (uint8_t *)"hello world", strlen("hello world"));
38 sm9_sign_finish(&sign_ctx, &sign_key, sig, &siglen);
39
40 format_bytes(stdout, 0, 0, "signature", sig, siglen);
41
42
43 sm9_sign_master_public_key_to_der(&sign_master, &p, &len);
44 sm9_sign_master_public_key_from_der(&sign_master_public, &cp, &len);
45
46 sm9_verify_init(&sign_ctx);
47 sm9_verify_update(&sign_ctx, (uint8_t *)"hello world", strlen("hello world"));
48 ret = sm9_verify_finish(&sign_ctx, sig, siglen, &sign_master_public, id, strlen(id));
49 printf("verify %s\n", ret == 1 ? "success" : "failure");
50
51
52 return 0;
53 }
54