2

I want to use my own hash algorithm in openvpn config. I think I should define it in openssl (library), and I look at MD5 algorithm as an example to see how it is defined. I did something like what is done for MD5 but my algorithm isn't added yet! I check this using command "openvpn --show-digests". can you refer me to some document about this?I added "m_myhash.c" in crypto/evp/m_myhash.c (like m_md5.c)

#include <stdio.h>
#include "cryptlib.h"

#ifndef OPENSSL_NO_MD5

# include <openssl/evp.h>
# include <openssl/objects.h>
# include <openssl/x509.h>
# include <openssl/md5.h>
# ifndef OPENSSL_NO_RSA
#  include <openssl/rsa.h>
# endif
# include "evp_locl.h"

static int init(EVP_MD_CTX *ctx)
{
    return MD5_Init(ctx->md_data);
}

static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
{
    return MD5_Update(ctx->md_data, data, count);
}

static int final(EVP_MD_CTX *ctx, unsigned char *md)
{
    return MD5_Final(md, ctx->md_data);
}

static const EVP_MD myhash_md = {
    NID_md5,
    NID_md5WithRSAEncryption,
    MD5_DIGEST_LENGTH,
    0,
    init,
    update,
    final,
    NULL,
    NULL,
    EVP_PKEY_RSA_method,
    MD5_CBLOCK,
    sizeof(EVP_MD *) + sizeof(MD5_CTX),
};

const EVP_MD *EVP_myhash(void)
{
    return (&myhash_md);
}
#endif

I did not changed functions already, I used md5's functions.(I want to understand defining a new hash algorithm how and where should be done and be compiled, so for now I use md5's init,update, ...)

I added lines to crypto/evp/Makefile to compile m_myhash.c and its object file is produced.

then I added this EVP_add_digest(EVP_myhash());

void OpenSSL_add_all_digests(void)
{
#ifndef OPENSSL_NO_MD4
    EVP_add_digest(EVP_md4());
#endif
#ifndef OPENSSL_NO_MD5
    EVP_add_digest(EVP_md5());
    EVP_add_digest_alias(SN_md5, "ssl2-md5");
    EVP_add_digest_alias(SN_md5, "ssl3-md5");
    EVP_add_digest(EVP_myhash());
#endif
  ...
}

to this file crypto/evp/c_alld.c.

I added this

#define SN_myhash          "MYHASH"
#define LN_myhash          "myhash"
#define NID_myhash         920
#define OBJ_myhash         OBJ_rsadsi,2L,5L

to file crypto/evp/c_alld.c. (here also last line is same as md5,I'm not sure about this!)

I added 920, /* OBJ_gholi 1 2 840 113549 2 5 */

I added this line

920,    /* OBJ_gholi                        1 2 840 113549 2 5 */

to file crypto/objects/obj_dat.h

I added {"MYHASH","myhash",NID_myhash,8,&(lvalues[5973]),0}, to this structure in file crypto/objects/obj_dat.h

static const ASN1_OBJECT nid_objs[NUM_NID]={
{"UNDEF","undefined",NID_undef,0,NULL,0},
{"rsadsi","RSA Data Security, Inc.",NID_rsadsi,6,&(lvalues[0]),0},
{"pkcs","RSA Data Security, Inc. PKCS",NID_pkcs,7,&(lvalues[6]),0},
{"MD2","md2",NID_md2,8,&(lvalues[13]),0},
{"MD5","md5",NID_md5,8,&(lvalues[21]),0},
{"MYHASH","myhash",NID_myhash,8,&(lvalues[5973]),0},
{"RC4","rc4",NID_rc4,8,&(lvalues[29]),0},

... }

I added this gholi 920 to file crypto/objects/obj_mac.num

also added

const EVP_MD *EVP_gholi(void);

to file crypto/evp/evp.h

I'm completely new to openssl code, I'm pretty sure I may have done blind and stupid things, sorry for that!

Peggy
  • 639
  • 9
  • 28
  • You did *something like what is done for MD5* obviously not because what you did doesn't work and you forgot to tell us what you EXACTLY did. – rene Aug 04 '15 at 06:34
  • @rene : sorry I asked this way I thought you just refer me to some document. I didnt think you may help me through the code. – Peggy Aug 04 '15 at 08:13
  • @SalvadorDali sorry I asked this way I thought you just refer me to some document. I didnt think you may help me through the code. – Peggy Aug 04 '15 at 08:13
  • We don't refer to off-site resources, that is in the [help] but your edit is helpful and I voted to reopen your question. Good luck. – rene Aug 04 '15 at 08:54

1 Answers1

2

I find the answer, I defined a new engine like gost (engines/ccgost) and defined e new digest using MD_DIGEST EVP struct (can get help from gost digest algorithm: engines/ccgost/gost_md.c ). then I load my engine and used its digest algorithm. for more detail one can see this Introduce GOST R 34.11-2012 hash function as an example.

Peggy
  • 639
  • 9
  • 28