Created
March 28, 2014 09:37
-
-
Save mpg/9828934 to your computer and use it in GitHub Desktop.
Parsing an EC key using secp256k1 and SpecifiedECDomain
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
#include <stdint.h> | |
#include <stdbool.h> | |
#include <string.h> | |
#include <polarssl/pk.h> | |
/* Initialise DER encoded key string*/ | |
const char privkey_str[] = "3081d302010104208ea106a78201d7bfc11af5c9230803db1eb104e0a4f47fa3af4a23a785584206a08185308182020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a12403220002a6ce40504bd48f6e71eaa27784705ba457463fb3b746e881350a09af2b850afc"; | |
bool decode_hex(unsigned char *p, size_t max_len, const char *hexstr, size_t *out_len_) | |
{ | |
static const unsigned char hexdigit_val[256] = { | |
['0'] = 0, | |
['1'] = 1, | |
['2'] = 2, | |
['3'] = 3, | |
['4'] = 4, | |
['5'] = 5, | |
['6'] = 6, | |
['7'] = 7, | |
['8'] = 8, | |
['9'] = 9, | |
['a'] = 0xa, | |
['b'] = 0xb, | |
['c'] = 0xc, | |
['d'] = 0xd, | |
['e'] = 0xe, | |
['f'] = 0xf, | |
['A'] = 0xa, | |
['B'] = 0xb, | |
['C'] = 0xc, | |
['D'] = 0xd, | |
['E'] = 0xe, | |
['F'] = 0xf,}; | |
if (!p || !hexstr) | |
return false; | |
if (!strncmp(hexstr, "0x", 2)) | |
hexstr += 2; | |
if (strlen(hexstr) > (max_len * 2)) | |
return false; | |
unsigned char *buf = p; | |
size_t out_len = 0; | |
while (*hexstr) { | |
unsigned char c1 = (unsigned char) hexstr[0]; | |
unsigned char c2 = (unsigned char) hexstr[1]; | |
unsigned char v1 = hexdigit_val[c1]; | |
unsigned char v2 = hexdigit_val[c2]; | |
if (!v1 && (c1 != '0')) | |
return false; | |
if (!v2 && (c2 != '0')) | |
return false; | |
*buf = (v1 << 4) | v2; | |
out_len++; | |
buf++; | |
hexstr += 2; | |
} | |
if (out_len_) | |
*out_len_ = out_len; | |
return true; | |
} | |
int main () | |
{ | |
unsigned char privkey[sizeof privkey_str]; | |
size_t privkey_len = 0; | |
int ret = 1; | |
const ecp_curve_info *curve; | |
fprintf(stdout,"Starting: Key is %s\n", privkey_str); | |
/* Decode DER string to HEX */ | |
if (decode_hex(privkey, sizeof privkey, privkey_str, &privkey_len) != true) { | |
fprintf(stderr,"Error: decode_hex failed\n"); | |
return ret; | |
} | |
fprintf(stdout,"decode_hex: OK\n"); | |
/* Parse DER encoded private key using PolarSSL */ | |
pk_context polar_ctx; | |
pk_init(&polar_ctx); | |
if ( (ret = pk_parse_key(&polar_ctx, privkey, privkey_len, NULL, 0)) != 0 ) { | |
fprintf(stderr,"Error: PolarSSL pk_parse_key failed : %i\n", ret); | |
return ret; | |
} | |
if (pk_can_do(&polar_ctx, POLARSSL_PK_ECDSA) != 1 ) { | |
fprintf(stderr,"Error: PolarSSL pk_can_do failed\n"); | |
return ret; | |
} | |
fprintf(stdout,"PolarSSL pk_can_do: OK\n"); | |
curve = ecp_curve_info_from_grp_id( pk_ec( polar_ctx )->grp.id ); | |
if( curve == NULL ) { | |
fprintf(stderr,"Error: PolarSSL curve_info failed\n"); | |
return ret; | |
} | |
fprintf(stdout, "PolarSSL EC key using curve: %s\n", curve->name); | |
if (&polar_ctx) { | |
pk_free(&polar_ctx); | |
} | |
fprintf(stdout,"PolarSSL pk_free: OK\n"); | |
fprintf(stdout,"Success: All keys parsed OK\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment