forked from guanzhi/GmSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsm2_exch.c
49 lines (41 loc) · 1.12 KB
/
sm2_exch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/mem.h>
#include <gmssl/sm2.h>
#include <gmssl/sm3.h>
#include <gmssl/asn1.h>
#include <gmssl/error.h>
#include <gmssl/endian.h>
int sm2_do_ecdh(const SM2_KEY *key, const SM2_Z256_POINT *peer_public, SM2_Z256_POINT *out)
{
sm2_z256_point_mul(out, key->private_key, peer_public);
return 1;
}
// FIXME: 输入(octets)和输出(bytes)格式不一致
int sm2_ecdh(const SM2_KEY *key, const uint8_t *peer_public, size_t peer_public_len, uint8_t out[64])
{
SM2_Z256_POINT point;
if (!key || !peer_public || !peer_public_len || !out) {
error_print();
return -1;
}
if (sm2_z256_point_from_octets(&point, peer_public, peer_public_len) != 1) {
error_print();
return -1;
}
if (sm2_do_ecdh(key, &point, &point) != 1) {
error_print();
return -1;
}
sm2_z256_point_to_bytes(&point, out);
return 1;
}