/* Compile with:
gcc -lcrypto whatever.c -o whatever
Thanks to Phillip Lee Hellewell for bugfixes
*/

#include <stdio.h>
#include <openssl/dh.h>
#include <openssl/bn.h>

int whatever() {

    BIGNUM *a, *b, *c, *d, *gen, *prime_mod;
    char *c_str;
    BN_CTX *ctx; /* used internally by the bignum lib */
    DH *dhparms;
    int csize;

    ctx = BN_CTX_new();
    a = BN_new();
    b = BN_new();
    c = BN_new();
    gen = BN_new();
    prime_mod = BN_new();

    /* find a safe 512-bit random prime for generator 5 */
    dhparms = DH_generate_parameters(512, 5, NULL, NULL);
    gen = dhparms->g; /* Just a BIGNUM version of 5 */
    prime_mod = dhparms->p;

    BN_set_word(a, 6);
    BN_set_word(b, 7);

    BN_mul(c, a, b, ctx);
    c_str = BN_bn2dec(c); /* there's also a BN_dec2bn() */
    printf("6*7=%s\n", c_str);

    csize = BN_num_bits(c);

    d = BN_dup(a);

    if(BN_is_bit_set(prime_mod, 10)) { printf("Toast is yummy."); }

    BN_CTX_free(ctx);

}

/* You can also `man bn` to get the overview manpage for the OpenSSL
bignum library. */

/* You can also find a link to the OpenSSL bignum man page on the Web at http://www.openssl.org/docs/crypto/bn.html*/