Skip to content

Instantly share code, notes, and snippets.

@rtyler
Created June 19, 2009 03:40
Show Gist options
  • Save rtyler/132391 to your computer and use it in GitHub Desktop.
Save rtyler/132391 to your computer and use it in GitHub Desktop.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <gcrypt.h>
#include <glib.h>
#define DEFAULT_DATA "This message will be signed\n"
#define DEFAULT_SIG "$HPI?t(I*1vAYsl$|%21WXND=6Br*[>k(OR9B!GOwHqL0s+3Uq"
void __init()
{
gcry_error_t err;
err = gcry_control(GCRYCTL_INIT_SECMEM, 1);
if (gcry_err_code(err))
fprintf(stderr, "Cannot enable libgcrypt's secure memory management\n");
gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0);
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
}
void __test_keygen()
{
__init();
gcry_error_t err;
gcry_sexp_t spec, key_pair, pub, priv;
err = gcry_sexp_build(&spec, NULL, "(genkey (ECDSA (nbits %d)))", 256);
if (err) {
fprintf(stderr, "gcry_sexp_new() failed with: %s\n", gcry_strerror(err));
abort();
}
err = gcry_pk_genkey(&key_pair, spec);
if (err) {
fprintf(stderr, "gcry_pk_genkey() failed with: %s\n", gcry_strerror(err));
abort();
}
pub = gcry_sexp_find_token(key_pair, "public-key", 0);
priv = gcry_sexp_find_token(key_pair, "private-key", 0);
gcry_sexp_release (key_pair);
gcry_sexp_release (spec);
char *pubkey, *privkey;
int publen, privlen;
gcry_mpi_aprint(GCRYMPI_FMT_HEX, &pubkey, &publen, pub);
fprintf(stderr, "PUB: (%d) %s %p\n", publen, pubkey, pubkey);
}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/test/ecc_keygen", __test_keygen);
return g_test_run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment