Created
August 21, 2018 18:49
-
-
Save ahmed-bhs/e08f722626ebe68cb08fcba63790771f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc -Wall ecdsapubkey.c -o ecdsapubkey -lcrypto | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <openssl/ec.h> | |
#include <openssl/obj_mac.h> | |
#include <openssl/bn.h> | |
int main() | |
{ | |
EC_KEY *eckey = NULL; | |
EC_POINT *pub_key = NULL; | |
const EC_GROUP *group = NULL; | |
BIGNUM *start; | |
BIGNUM *res; | |
BN_CTX *ctx; | |
start = BN_new(); | |
ctx = BN_CTX_new(); // ctx is an optional buffer to save time from allocating and deallocating memory whenever required | |
res = start; | |
BN_hex2bn(&res,"8A9A35145C4EA5260DF9972C804FE2D3F9F3D7A2AC01A6BEB21C82BB30957B3952273AC9166B90C1207347A925780F84A1D2359E7AA05201C674D2B9746FCA07"); | |
eckey = EC_KEY_new_by_curve_name(NID_secp256k1); | |
group = EC_KEY_get0_group(eckey); | |
pub_key = EC_POINT_new(group); | |
printf("private key : "); BN_print_fp(stdout, res); printf("\n"); | |
EC_KEY_set_private_key(eckey, res); | |
/* pub_key is a new uninitialized `EC_POINT*`. priv_key res is a `BIGNUM*`. */ | |
if (!EC_POINT_mul(group, pub_key, res, NULL, NULL, ctx)) | |
printf("Error at EC_POINT_mul.\n"); | |
// assert(EC_POINT_bn2point(group, &res, pub_key, ctx)); // Null here | |
EC_KEY_set_public_key(eckey, pub_key); | |
char *cc = EC_POINT_point2hex(group, pub_key, 4, ctx); | |
char *c=cc; | |
int i; | |
printf("public key : "); | |
for (i=0; i<130; i++) // 1 byte 0x42, 32 bytes for X coordinate, 32 bytes for Y coordinate | |
{ | |
printf("%c", *c++); | |
} | |
printf("\n"); | |
BN_CTX_free(ctx); | |
free(cc); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment