2024年11月Linux为OpenSSL添加自定义加密算法的技巧

发布时间:

  ⑴Linux系统OpenSSL是一套密码库系统,所以Linux系统使用OpenSSL的过程中,就一定会涉及给OpenSSL添加加密算法,本文就来介绍一下Linux为OpenSSL添加自定义加密算法的技巧。

  ⑵本文以添加自定义算法EVP_ssf为例,介绍在OpenSSL中添加自定义加密算法的方法

  ⑶、修改crypto/object/objects.txt,注册算法OID,如下:

  ⑷rsadsi : SSF: ssf

  ⑸、进入目录:crypto/object/,执行如下命令,生成算法的声明

  ⑹perl objects.pl objects.txt obj_mac.num obj_mac.h

  ⑺、在crypto/evp/下添加e_ssf.c,内容如下

  ⑻#include 《stdio.h》

  ⑼#include “cryptlib.h”

  ⑽#ifndef OPENSSL_NO_RC

  ⑾#include 《openssl/evp.h》

  ⑿#include 《openssl/objects.h》

  ⒀#include 《openssl/rc.h》

  ⒁/* FIXME: surely this is available elsewhere? */

  ⒂#define EVP_SSF_KEY_SIZE

  ⒃typedef struct

  ⒄RC_KEY ks; /* working key */

  ⒅} EVP_SSF_KEY;

  ⒆#define data(ctx ((EVP_SSF_KEY *(ctx-》cipher_data

  ⒇static int ssf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv,int enc;

  ⒈static int ssf_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl;

  ⒉static const EVP_CIPHER ssf_evp_cipher=

  ⒊NID_ssf,

  ⒋EVP_SSF_KEY_SIZE,

  ⒌EVP_CIPH_VARIABLE_LENGTH,

  ⒍ssf_init_key,

  ⒎ssf_cipher,

  ⒏sizeof(EVP_SSF_KEY,

  ⒐const EVP_CIPHER *EVP_ssf(void

  ⒑return(&ssf_evp_cipher;

  ⒒static int ssf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc

  ⒓RC_set_key(&data(ctx-》ks,EVP_CIPHER_CTX_key_length(ctx, key;

  ⒔return ;

  ⒕static int ssf_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl

  ⒖RC(&data(ctx-》ks,inl,in,out;

  ⒗return ;