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/sm2.h>
14 #include <gmssl/error.h>
15
16
main(void)17 int main(void)
18 {
19 SM2_KEY sm2_key;
20 SM2_KEY pub_key;
21 unsigned char dgst[32];
22 unsigned char sig[SM2_MAX_SIGNATURE_SIZE];
23 size_t siglen;
24 int ret;
25
26 sm3_digest((unsigned char *)"hello world", strlen("hello world"), dgst);
27 format_bytes(stdout, 0, 0, "to be signed digest", dgst, sizeof(dgst));
28
29 sm2_key_generate(&sm2_key);
30
31 sm2_sign(&sm2_key, dgst, sig, &siglen);
32 format_bytes(stdout, 0, 0, "signature", sig, siglen);
33
34 memcpy(&pub_key, &sm2_key, sizeof(SM2_POINT));
35
36 if ((ret = sm2_verify(&pub_key, dgst, sig, siglen)) != 1) {
37 fprintf(stderr, "verify failed\n");
38 } else {
39 printf("verify success\n");
40 }
41
42 return 0;
43 }
44