Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write openssl engine

Tags:

linux

openssl

I want to write an openssl dynamic engine, but I can't find any document for that.

My algorithm that I want to write, is a chipper algorithm(like rsa) and a hash algorithm (like md5).

Is any simple engine source code that I can change that and use from that?

like image 682
hamSh Avatar asked Aug 16 '11 06:08

hamSh


1 Answers

Is any simple engine source code that I can change that and use from that?

The Intel RDRAND engine is pretty easy to understand. You can find its sources with:

openssl-1.0.1f$ grep -R -i ENGINE_rdrand *
crypto/engine/eng_rdrand.c:static ENGINE *ENGINE_rdrand(void)
crypto/engine/eng_rdrand.c:    ENGINE *toadd = ENGINE_rdrand();

Here's how you can load and set it as the default random number generator engine (from Random Numbers|Hardware):

unsigned long err = 0;
int rc = 0;

OPENSSL_cpuid_setup();
ENGINE_load_rdrand();

ENGINE* eng = ENGINE_by_id("rdrand");
err = ERR_get_error();

if(NULL == eng) {
    fprintf(stderr, "ENGINE_load_rdrand failed, err = 0x%lx\n", err);
    abort(); /* failed */
}

rc = ENGINE_init(eng);
err = ERR_get_error();

if(0 == rc) {
    fprintf(stderr, "ENGINE_init failed, err = 0x%lx\n", err);
    abort(); /* failed */
}

rc = ENGINE_set_default(eng, ENGINE_METHOD_RAND);
err = ERR_get_error();

if(0 == rc) {
    fprintf(stderr, "ENGINE_set_default failed, err = 0x%lx\n", err);
    abort(); /* failed */
}

/* OK to proceed */

...
ENGINE_finish(eng);
ENGINE_free(eng);
like image 119
jww Avatar answered Sep 22 '22 10:09

jww