mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-06-22 13:13:42 +08:00
Rewrite TLS 1.2 as a state machine
This commit is contained in:
49
src/ecdh.c
49
src/ecdh.c
@@ -8,5 +8,54 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/error.h>
|
||||
#include <gmssl/secp256r1_key.h>
|
||||
|
||||
|
||||
int secp256r1_do_ecdh(const SECP256R1_KEY *key, const SECP256R1_KEY *peer_key, uint8_t out[32])
|
||||
{
|
||||
SECP256R1_POINT point;
|
||||
secp256r1_t x;
|
||||
secp256r1_t y;
|
||||
|
||||
if (!key || !peer_key || !out) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
secp256r1_point_mul(&point, key->private_key, &peer_key->public_key);
|
||||
secp256r1_point_get_xy(&point, x, y);
|
||||
secp256r1_to_32bytes(x, out);
|
||||
|
||||
gmssl_secure_clear(&point, sizeof(SECP256R1_POINT));
|
||||
gmssl_secure_clear(x, sizeof(secp256r1_t));
|
||||
gmssl_secure_clear(y, sizeof(secp256r1_t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int secp256r1_ecdh(const SECP256R1_KEY *key, const uint8_t uncompressed_point[65], uint8_t out[32])
|
||||
{
|
||||
SECP256R1_POINT point;
|
||||
secp256r1_t x;
|
||||
secp256r1_t y;
|
||||
|
||||
if (!key || !uncompressed_point || !out) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (secp256r1_point_from_uncompressed_octets(&point, uncompressed_point) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
secp256r1_point_mul(&point, key->private_key, &point);
|
||||
secp256r1_point_get_xy(&point, x,y);
|
||||
secp256r1_to_32bytes(x, out);
|
||||
|
||||
gmssl_secure_clear(&point, sizeof(SECP256R1_POINT));
|
||||
gmssl_secure_clear(x, sizeof(secp256r1_t));
|
||||
gmssl_secure_clear(y, sizeof(secp256r1_t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user