Linux Kernel(Android) 加密算法总结(一)(cipher、compress、digest)
1. Linux内核支持哪些加密算法 ?
内核支持的加密算法非常多,包含:
对称加密算法。如AES,3DES。
对称password体制的发展趋势将以分组password为重点。
分组password算法通常由密钥扩展算法和加密(解密)算法两部分组成。密钥扩展算法将b字节用户主密钥扩展成r个子密钥。
加密算法由一个password学上的弱函数f与r个子密钥迭代r次组成。混乱和密钥扩散是分组password算法设计的基本原则。
抵御已知明文的差分和线性攻击,可变长密钥和分组是该体制的设计要点。 AES是美国国家标准技术研究所NIST旨在代替DES的21世纪的加密标准。
AES的基本要求是。採用对称分组password体制,密钥长度的最少支持为128、192、256,分组长度128位。算法应易于各种硬件和软件实现。1998年NIST開始AES第一轮分析、測试和征集,共产生了15个候选算法。
1999年3月完毕了第二轮AES2的分析、測试。2000年10月2日美国政府正式宣布选中比利时password学家Joan
Daemen 和 Vincent Rijmen 提出的一种password算法RIJNDAEL 作为 AES. 在应用方面,虽然DES在安全上是脆弱的,但因为高速DES芯片的大量生产,使得DES仍能临时继续使用。为提高安全强度,通常使用独立密钥的三级DES。可是DES迟早要被AES取代。流password体制较之分组password在理论上成熟且安全,但未被列入下一代加密标准。
摘要算法,如sha1,md5。
数据摘要算法是password学算法中很重要的一个分支,它通过对全部数据提取指纹信息以实现数据签名、数据完整性校验等功能,因为其不可逆性。有时候会被用做敏感信息的加密。
数据摘要算法也被称为哈希(Hash)算法或散列算法。
SHA(Secure
Hash Algorithm)是由美国专门制定password算法的标准机构—— 美国国家标准技术研究院(NIST)制定的。SHA系列算法的摘要长度分别为:SHA为20字节(160位)、SHA256为32字节(256位)、 SHA384为48字节(384位)、SHA512为64字节(512位)。因为它产生的数据摘要的长度更长。因此更难以发生碰撞,因此也更为安全,它是未来数据摘要算法的发展方向。因为SHA系列算法的数据摘要长度较长,因此其运算速度与MD5相比。也相对较慢。
MD2、MD4、MD5 都产生16字节(128位)的校验值,一般用32位十六进制数表示。MD2的算法较慢但相对安全。MD4速度非常快,但安全性下降,MD5比MD4更安全、速度更快。
压缩算法。如deflate。
DEFLATE是同一时候使用了LZ77算法与哈夫曼编码(Huffman
Coding)的一个无损数据压缩算法。它最初是由Phil
Katz为他的PKZIP归档工具第二版所定义的。后来定义在RFC
1951规范中。
人们普遍觉得DEFLATE不受不论什么专利所制约,而且在LZW(GIF文件格式使用)相关的专利失效之前,这样的格式除了在ZIP文件格式中得到应用之外也在gzip压缩文件以及PNG图像文件里得到了应用。
DEFLATE压缩与解压的源码能够在自由、通用的压缩库zlib上找到。
只是内核好像不支持非对称加密算法。
2. 加密算法源文件位置
这些算法作为加密函数框架的最底层。提供加密和解密的实际操作。这些函数能够在内核crypto目录下,对应的文件里找到。
3. 配置编译选项将加密算法作为模块编入内核
Cryptographic options
加密选项
Cryptographic API
提供核心的加密API支持.这里的加密算法被广泛的应用于驱动程序通信协议等机制中.子选项能够全不选,内核中若有其它部分依赖它,会自己主动选上
Cryptographic algorithm manager
创建加密模版实例,必需要选
HMAC support
为IPSec所必须,可为PPPoE提供压缩支持
Null algorithms
NULL加密算法(什么也不做),用于IPsec协议的封装安全载荷模块(ESP)
MD4 digest algorithm
老旧的摘要算法,已经过时
MD5 digest algorithm
主流摘要算法,128位(已被中国山东大学王小云攻破,能够高速找到碰撞)
SHA1 digest algorithm
主流摘要算法,160位(已被中国山东大学王小云攻破,能够高速找到碰撞),速度与MD5相当
SHA256 digest algorithm
更好的摘要算法,256位,速度较SHA1稍慢
SHA384 and SHA512 digest algorithms
更好的摘要算法,384/512位,速度大约仅仅有SHA1的40-50%
Whirlpool digest algorithms
最安全的摘要算法,512位,已被列入ISO标准,眼下最新版本号为3.0(2003年公布)
Tiger digest algorithms
号称最快的摘要算法,192位,专门为64位CPU进行了优化
ECB support
电子password本,最简单的加密方法
CBC support
password块链,IPSec须要使用它
DES and Triple DES EDE cipher algorithms
老迈的(DES)和尚佳的(Triple DES)对称加密算法
Blowfish cipher algorithm
又老又慢的对称加密算法
Twofish cipher algorithm
非常强的对称加密算法,使用较广
Twofish cipher algorithms (i586)
非常强的对称加密算法,使用较广(针对i586的版本号)
Serpent cipher algorithm
非常强的对称加密算法
AES cipher algorithms
最佳的对称加密算法(Rijndael),128/192/256位,强度最高,高速且节省内存
AES cipher algorithms (i586)
最佳的对称加密算法(Rijndael),128/192/256位,强度最高,高速且节省内存(针对i586的版本号)
CAST5 (CAST-128) cipher algorithm
对称加密算法
CAST6 (CAST-256) cipher algorithm
对称加密算法
TEA, XTEA and XETA cipher algorithms
较弱的对称加密算法
ARC4 cipher algorithm
脆弱的流对称加密算法
Khazad cipher algorithm
对称加密算法
Anubis cipher algorithm
对称加密算法
Deflate compression algorithm
压缩算法,当在IPSec中使用IPCOMP协议时才须要
Michael MIC keyed digest algorithm
摘要算法,只用于校验iSCSI设备传输的数据,由于算法本身比較脆弱
CRC32c CRC algorithm
摘要算法,可用于校验iSCSI设备传输的数据
Testing module
高速且丑陋的測试模块
Hardware crypto devices
仅有VIA C7系列处理器支持硬件加密(VIA PadLock高级加密引擎)
參考:
CryptoAPI support
提供核心的加密API支持.这里的加密算法被广泛的应用于驱动程序通信协议等机制上,子选项能够全不选,内核中若有其它部分依赖它。会自己主动选
(M)Cipher Algorithms
创建加密模版实例。必需要选
-- 128 bit blocksize
(M) AES (aka Rijndael) cipher
(M) Identity Function cipher
(M) Crypto Devices
(M) Loop Crypto support
4. 加密API 的调用方法:
只是内核模块不能直接调用这些函数。由于它们并没有export。内核提供一个统一的框架,来管理这些算法。
加密算法通过crypto_register_alg()和crypto_unregister_alg()注冊。
内核将加密算法分为三类,1)cipher,2)compress,3)digest。
加密函数框架中有对应的API封装,提供给模块调用。
对于使用这些加密函数,首先通过crypto_alloc_tfm()来分配一个加密函数对象的实例。初始化这些实例,然后就能够通过框架提供的API对数据进行加密和解密。
完毕以后。必须通过crypto_free_tfm()撤销实例。
5. 代码演示样例:
1)digest算法(sha1)
| #include<linux/kernel.h> #include <linux/module.h> #include <linux/crypto.h> #include <linux/scatterlist.h> #include <linux/gfp.h> #include <linux/err.h> #include <linux/syscalls.h> #include <linux/slab.h> struct crypto_tfm *tfm; struct scatterlist sg[1]; char * code1="234123132513451345"; char * code2="234123132513451345"; char *do_digest(char* code){ char *result; int code_len =strlen(code); tfm = crypto_alloc_tfm("sha1",0); if(IS_ERR(tfm)) return 0; sg_init_one(sg,code,code_len); crypto_digest_init(tfm); crypto_digest_update(tfm,sg,1); result = (char*)kmalloc(sizeof(char)*50,GFP_KERNEL); if(result == NULL) { crypto_free_tfm(tfm); return 0; } memset(result,0,sizeof(char)*50); crypto_digest_final(tfm,result); crypto_free_tfm(tfm); return result; } static int __init test_init(void) { char *result1,*result2; result1 = do_digest(code1); if(!result1) goto failed2; result2 = do_digest(code2); if(!result2) goto failed1; if(memcmp(result1,result2,50)!= 0) printk("<1>code1 != code2\n"); else printk("<1>code1 == code2\n"); kfree(result2); failed1: kfree(result1); failed2: return 0; } static void __exit test_exit(void) { } module_init(test_init); module_exit(test_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("robert@cm"); |
2)compress算法(deflate)
| #include<linux/module.h> #include <linux/kernel.h> #include <linux/crypto.h> #include <linux/scatterlist.h> #include <linux/gfp.h> #include <linux/err.h> #include <linux/syscalls.h> #include <linux/slab.h> struct crypto_tfm *tfm; char * code="Hello everyone, I'm robert from cm !"; static inline void hexdump(unsignedchar*buf,unsignedint len){ while(len--) printk("0x%02x,",*buf++); printk("\n"); } static int __init test_init(void){ int ret,result_len,temp_len; char result[512]; char temp[512]; printk("<1>%s\n",code); /* Allocate transform for deflate */ tfm = crypto_alloc_tfm("deflate",0); if(IS_ERR(tfm)){ printk("<1>failed to load transform for deflate !\n"); return 0; } memset(result,0,sizeof(result)); temp_len = 512; ret = crypto_comp_compress(tfm,code,strlen(code),temp,&temp_len); if(ret){ printk("<1>failed to compress !\n"); return 0; } hexdump(temp,strlen(temp)); memset(result,0,sizeof(result)); result_len = 512; ret = crypto_comp_decompress(tfm,temp,strlen(temp),result,&result_len); if(ret){ printk("<1>failed to decompress !\n"); return 0; } printk("<1>%s\n",result); if(memcmp(code,result,strlen(code))!= 0) printk("<1>decompressed was not successful\n"); else printk("<1>decompressed was successful\n"); crypto_free_tfm(tfm); return 0; } static void __exit test_exit(void) { } module_init(test_init); module_exit(test_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("robert@cm"); |
3)cipher算法(aes)
#include<linux/module.h>
#include
<linux/kernel.h>
#include
<linux/crypto.h>
#include
<linux/scatterlist.h>
#include
<linux/gfp.h>
#include
<linux/err.h>
#include
<linux/syscalls.h>
#include
<linux/slab.h>
#include
<linux/highmem.h>
struct crypto_tfm
*tfm;
#if 1
char *code
= "Hello everyone,I'm robert"
"Hello everyone,I'm robert"
"Hello everyone,I'm robert";
char *key
= "00112233445566778899aabbccddeeff";
#endif
#if 0
char code[]={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,
0xbb,0xcc,0xdd,0xee,0xff};
char key[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,
0x0b,0x0c,0x0d,0x0e,0x0f};
#endif
static inline void hexdump(unsignedchar*buf,unsignedint
len){
while(len--)
printk("%02x",*buf++);
printk("\n");
}
static int __init test_init(void){
int ret,templen,keylen,codelen;
struct scatterlist sg[1];
char *result;
char *temp;
keylen = 16;
codelen = strlen(code)/2;
#if 0
printk("<1>%s, codelen=%d\n",code,strlen(code));
printk("<1>%s, keylen=%d\n",key,strlen(key));
#endif
/* Allocate transform for AES ECB mode */
tfm = crypto_alloc_tfm("aes",CRYPTO_TFM_MODE_ECB);
if(IS_ERR(tfm)){
printk("<1>failed to load transform for aes ECB mode !\n");
return 0;
}
ret = crypto_cipher_setkey(tfm,key,keylen);
if(ret){
printk("<1>failed to setkey \n");
goto failed1;
}
sg_init_one(sg,code,codelen);
/* start encrypt */
ret = crypto_cipher_encrypt(tfm,sg,sg,codelen);
if(ret){
printk("<1>encrypt failed \n");
goto failed1;
}
temp = kmap(sg[0].page)+
sg[0].offset;
hexdump(temp,sg[0].length);
/* start dencrypt */
templen = strlen(temp)/2;
sg_init_one(sg,temp,templen);
ret = crypto_cipher_decrypt(tfm,sg,sg,templen);
if(ret){
printk("<1>dencrypt failed \n");
goto failed1;
}
result = kmap(sg[0].page)+
sg[0].offset;
printk("<1>%s\n",result);
// hexdump(result,sg[0].length);
#if 0
if(memcmp(code,result,strlen(code))!=
0)
printk("<1>dencrpt was not successful\n");
else
printk("<1>dencrypt was successful\n");
#endif
failed1:
crypto_free_tfm(tfm);
return 0;
}
static void __exit test_exit(void)
{
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("robert@cm");
部分採用网络代码。
Linux Kernel(Android) 加密算法总结(一)(cipher、compress、digest)的更多相关文章
- Linux Kernel(Android) 加密算法汇总(三)-应用程序调用内核加密算法接口
于Linux Kernel(Android) 加密算法总结(cipher.compress.digest)文章中.介绍了怎样在内核中增加三种不同类型的内核加密算法, 并给出了在内核模块中怎样调用他们的 ...
- Linux Kernel(Android) 加密算法汇总(四)-应用程序调用OpenSSL加密演算法
Linux Kernel(Android) 加密算法总结(三)-应用程序调用内核加密算法接口 讲到了怎样调用内核中的接口的方法. 本节主要是介绍怎样Android C/C++应用程序调用Openssl ...
- linux kernel API and google android compile guide
(1)linux kernel API website: http://docs.knobbits.org/local/linux-doc/html/regulator/index.html http ...
- 编译android的linux kernel goldfish
https://source.android.com/source/building-kernels.html $ export PATH=/home/hzh/oldhome/learn/androi ...
- 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6564592 在前一篇文章提到,从源代码树下载下 ...
- Android linux kernel privilege escalation vulnerability and exploit (CVE-2014-4322)
In this blog post we'll go over a Linux kernel privilege escalation vulnerability I discovered which ...
- Android HAL层与Linux Kernel层驱动开发简介
近日稍微对Android中的驱动开发做了一些简要的了解. HAL:Hardware Abstract Layer 硬件抽象层,由于Linux Kernel需要遵循GPL开源协议,硬件厂商为了保护自己硬 ...
- Android 怎样在linux kernel 中读写文件
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- 深入linux kernel内核配置选项
============================================================================== 深入linux kernel内核配置选项 ...
随机推荐
- LINUX/UNIX找回删除的文件
当Linux计算机受到入侵时,常见的情况是日志文件被删除,以掩盖攻击者的踪迹.管理错误也可能导致意外删除重要的文件,比如在清理旧日志时,意外地删除了数据库的活动事务日志.有时可以通过lsof来恢复这些 ...
- [JavaEE] 20141228_Java类文章搜集
http://www.blogjava.net/jiangshachina 博客园java频道 Maven入门--概念与实例(原) Maven入门--较复杂的实例(原) Maven插件使用收集(原) ...
- BZOJ-4706 B君的多边形 OEIS
题面 题意:有一个正n多边形,我们要连接一些对角线,把这个多边形分成若干个区域,要求连接的对角线不能相交,每个点可以连出也可以不连出对角线,即最终不要求所有区域均为三角形,问总方案数mod (10^9 ...
- Docker 内部之间的网络连接
一.简介 内部网络连接的2中方式: Docker NetWorking (1.9版本之后推荐使用这个)和 Docker link(1.9 版本之前都使用这个) 推荐使用docker networkin ...
- 文档控件NTKO OFFICE 详细使用说明之预览Excel文件(查看、编辑、保存回服务器)
1.在线预览Excel文件 (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将www.ntko.com加入到浏 ...
- lua的Metatables和Metamethods
Metatable: lua中的每一个表都有其Metatable,默认情况下Metatable为nil.可通过setmetatable函数设置或者改变一个表的Metatable, 也可以通过getme ...
- JUC集合-BlockingQueue
BlockingQueue 阻塞队列,支持两个附加操作. 1,在队列为空时,获取元素的线程会等待对列变为非空. 2,在队列为满时,存储元素的线程会等待对列可用. 使用场景: 生产者往对列里添加元素 消 ...
- 如何拿到阿里算法校招offer
好多同学有问过怎么能拿到阿里算法类校招的offer,刚好看到这篇文章分享给大家,详情可以看原文链接,原文链接中有视频讲解. 师兄师姐的建议: 之前初学算法的时候上过的公开课和看过的书 1. Cours ...
- 数据结构——单链表java简易实现
巩固数据结构 单链表java实现 单链表除了表尾 每个几点都有一个后继 结点有数据和后继指针组成 通过构建表头和表尾(尾部追加需要)两个特殊几点 实现单链表的一些操作,代码如下 package co ...
- Algorithms算法题<1.1>
1.1.27 二项分布.估计用一下代码计算binomial(100,50,0.25)将会产生的递归调用次数: public static double binomial(int N,int k,dou ...