OpenSSL中对称加密算法的统一接口详解
http://tech.ddvip.com 2007年04月25日 社区交流
本文详细介绍OpenSSL中对称加密算法的统一接口详解
cipher:算法指针
engine:加解密引擎
encrypt:加密或解密
buf_len:剩余空间
oiv:原始的初始向量
iv:当前的初始向量
buf:保存的部分块数据
num:cfb/ofb方式时的数据数量
app_data:应用相关数据
key_len:密钥长度
flags:标志
cipher_data:各算法相关部分,主要是各算法的key等
final_used:
block_mask:块的掩码
final:最后的分组块
1.2.2 算法接口
每种算法就是要填写各自的EVP_CIPHER结构,以RC4为例,定义了两个RC4的EVP_CIPHER结构,只是密钥长度不同,一个是128位(16字节密钥),一个是40位(5字节密钥),而算法都一样:
#ifndef OPENSSL_NO_RC4
#include
#include "cryptlib.h"
#include
#include
#include
/* FIXME: surely this is available elsewhere? */
#define EVP_RC4_KEY_SIZE 16
//这个结构是各加密算法独有的,各算法的各自不同
//也就是EVP_CIPHER_CTX结构中cipher_data
typedef struct
{
RC4_KEY ks; /* working key */
} EVP_RC4_KEY;
#define data(ctx) ((EVP_RC4_KEY *)(ctx)->cipher_data)
static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv,int enc);
static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, unsigned int inl);
static const EVP_CIPHER r4_cipher=
{
NID_rc4,
1,EVP_RC4_KEY_SIZE,0,
EVP_CIPH_VARIABLE_LENGTH,
rc4_init_key,
rc4_cipher,
NULL,
sizeof(EVP_RC4_KEY),
NULL,
NULL,
NULL
};
static const EVP_CIPHER r4_40_cipher=
{
NID_rc4_40,
1,5 /* 40 bit */,0,
EVP_CIPH_VARIABLE_LENGTH,
rc4_init_key,
rc4_cipher,
NULL,
sizeof(EVP_RC4_KEY),
NULL,
NULL,
NULL
};
// 返回算法结构指针
const EVP_CIPHER *EVP_rc4(void)
{
return(&r4_cipher);
}
const EVP_CIPHER *EVP_rc4_40(void)
{
return(&r4_40_cipher);
}
// 密钥初始化函数
// ctx:加解密上下文;key:密钥字符串;iv:初始化向量;enc:加密还是解密
static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc)
{
// RC4算法设置密钥函数
// 一般来说,加解密时算法里用的密钥并不是用户输入的密码字符串本身,因为算法
// 使用的密钥长度要求是固定的,通常为64位或128位,而用户自己定义的密码长度
// 则不确定,所以一般都都要对用户输入的密码进行变换,映射到一个固定长度密钥
// 上,然后算法再使用该密钥加密,所以算法中用的密钥和用户的密码一般是不同的
RC4_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
key);
return 1;
}
// 加解密处理函数
// ctx:加解密上下文;out:输出数据;in:输入数据;inl:输入数据长度
static int rc4_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对于定义好EVP_CIPHER结构的加解密算法,最后通过EVP_add_cipher()函数添加到系统算法链表中:
责编:豆豆技术应用
正在加载评论...
- Linux/Unix 新闻
- Linux/Unix 入门
- Linux/Unix 命令
- Linux/Unix 安装
- Linux 嵌入式系统
- Linux/Unix 编程
- Linux/Unix 管理
- Linux/Unix 桌面
- Linux/Unix 内核
- Linux/Unix 软件
- SCO Unix
- NetBSD
- OpenBSD
- Redhat/Fedora Linux
- 手机
- Linux/Unix find 搜索命令
- Linux/Unix vi 命令
- Linux/Unix kde 桌面环境
- Linux/Unix GNOME 桌面环境
- Linux/Unix Make 命令
- Linux/Unix crontab 命令
- Linux/Unix ext3 文件系统
- Linux/Unix 文件系统详解
- Linux/Unix ADSL 拨号设置
- Linux/Unix GRUB 配置及应用
- Linux/Unix nfs配置
- Linux/Unix 硬件信息查看及管理
- Linux/Unix 优化
- Linux/Unix 交换分区Swap管理及应用
- Linux/Unix 用户管理
- Linux/Unix Ramdisk
- Linux/Unix 密码恢复管理
- Linux/Unix 文件删除恢复
- Linux/Unix fdisk分区
- Linux/Unix lvs负载均衡管理
- Linux/Unix root用户
- Linux/Unix 集群
- Linux/Unix 日志
- 更多Linux/Unix专题……