歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 在OpenSSL中添加自定義加密算法

在OpenSSL中添加自定義加密算法

日期:2017/2/28 13:58:56   编辑:Linux教程

一、簡介

本文以添加自定義算法EVP_ssf33為例,介紹在OpenSSL中添加自定義加密算法的方法

二、步驟

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

rsadsi 3 255 : SSF33 : ssf33

2、進入目錄:crypto/object/,執行如下命令,生成算法的聲明

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

3、在crypto/evp/下添加e_ssf33.c,內容如下

#include <stdio.h>
#include "cryptlib.h"
#ifndef OPENSSL_NO_RC4
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/rc4.h>

/* FIXME: surely this is available elsewhere? */
#define EVP_SSF33_KEY_SIZE 16

typedef struct
{
RC4_KEY ks; /* working key */
} EVP_SSF33_KEY;

#define data(ctx) ((EVP_SSF33_KEY *)(ctx)->cipher_data)

static int ssf33_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv,int enc);

static int ssf33_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl);

static const EVP_CIPHER ssf33_evp_cipher=
{
NID_ssf33,
1,
EVP_SSF33_KEY_SIZE,
0,
EVP_CIPH_VARIABLE_LENGTH,
ssf33_init_key,
ssf33_cipher,
NULL,
sizeof(EVP_SSF33_KEY),
NULL,
NULL,
NULL,
NULL
};

const EVP_CIPHER *EVP_ssf33(void)
{
return(&ssf33_evp_cipher);
}

static int ssf33_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc)
{
RC4_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx), key);

return 1;
}

static int ssf33_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl)
{
RC4(&data(ctx)->ks,inl,in,out);

return 1;
}

#endif

4、修改crypto/evp/evp.h,添加對算法的聲明,如下
const EVP_CIPHER *EVP_ssf33(void);
5、修改crypto/evp/c_allc.c,在OpenSSL_add_all_ciphers函數中使用EVP_add_cipher注冊加密函數,如下
EVP_add_cipher(EVP_ssf33());

6、修改crypto/evp/Makefile,如下

7、完成

通過OpenSSL提供FTP+SSL/TLS認證功能,並實現安全數據傳輸 http://www.linuxidc.com/Linux/2013-05/84986.htm

Linux下使用OpenSSL生成證書 http://www.linuxidc.com/Linux/2015-05/117034.htm

利用OpenSSL簽署多域名證書 http://www.linuxidc.com/Linux/2014-10/108222.htm

OpenSSL 的詳細介紹:請點這裡
OpenSSL 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved