一、概述
Linux内核从2.5版本开始引入了强力的加密机制,主要原因有:删除冗余代码、支持IPSec协议以及通用的加密功能等。将来的应用会包括:硬件加密设备驱动、内核代码签名、硬件随机数生成器、文件系统加密等。
	
	二、如何使用加密API
1、安装
从2.6.6版本之后,内核源码就提供了丰富的密码学算法支持,需要配置编译选项将加密算法作为模块编入内核。menuconfig的时候配置这部分选项
重新安装内核之后可以看到相应的目录下这些模块
2、使用API编程
这里介绍的是使用加密API的方法,而且是运行在内核态的程序
	
	例1
	char *Kern_Digest(const void *data, size_t count, 
	unsigned char *md, unsigned int *size, const char *name) 
	{ 
	struct crypto_tfm *tfm; 
	struct scatterlist sg[1]; 
	tfm = crypto_alloc_tfm(name, 0); 
	sg_init_one(sg, data, count); //这里复制需要做哈希的数据  
	crypto_digest_init(tfm); 
	crypto_digest_update(tfm, sg, 1); 
	crypto_digest_final(tfm, md); 
	if (size != NULL) 
	*size = tfm->cra_digest.dia_digestsize; 
	crypto_free_tfm(tfm); 
	}
	
	例2
	#include <linux/crypto.h>  
	 
	int len; 
	char key[8]; 
	char result[64]; 
	struct crypto_tfm *tfm; 
	struct scatterlist sg[2]; 
	 
	tfm = crypto_alloc_tfm("des", 0); 
	if(tfm == NULL) 
	fail(); 
	 
	crypto_cipher_setkey(tfm, key, 8); 
	 
	//把需要加密的数据复制到scatterlist  
	 
	crypto_cipher_encrypt(tfm, sg[0], sg[0], len); //这里可以模拟输入输出  
	 
	crypto_free_tfm(tfm);
	
	以上两个例子仅示范了如何使用API,可以看到最重要的2个数据结构是: crypto_tfm 和 scatterlist
crypto_tfm类型指针tfm可以理解为指代了一个算法对象
scatterlist类型数据可以认为是这些密码算法操纵的数据对象。
同时也可以看到,API的命名很容易阅读(这也是Linux内核命名的一个特色)
这部分加密库提供的接口都在linux/crypto.h头文件中有定义,进行实验的时候需要与内核相关,这里可能要一些模块编程的补充。

