zoukankan      html  css  js  c++  java
  • 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相比。也相对较慢。

    眼下SHA1的应用较为广泛,主要应用于CA和数字证书中,另外在眼下互联网中流行的BT软件中,也是使用SHA1来进行文件校验的。

    MD5(Message-Digest Algorithm 5,消息摘要算法版本号5)。它由MD2、MD3、MD4发展而来,由Ron Rivest(RSA公司)在1992年提出,眼下被广泛应用于数据完整性校验、数据(消息)摘要、数据加密等。

    MD2、MD4、MD5 都产生16字节(128位)的校验值,一般用32位十六进制数表示。MD2的算法较慢但相对安全。MD4速度非常快,但安全性下降,MD5比MD4更安全、速度更快。

    眼下在互联网上进行大文件传输时。都要得用MD5算法产生一个与文件匹配的、存储MD5值的文本文件(后缀名为 .md5或.md5sum),这样接收者在接收到文件后,就能够利用与 SFV 类似的方法来检查文件完整性。眼下绝大多数大型软件公司或开源组织都是以这样的方式来校验数据完整性,并且部分操作系统也使用此算法来对用户password进行加密,另外,它也是眼下计算机犯罪中数据取证的最经常使用算法。


         压缩算法。如deflate。

         DEFLATE是同一时候使用了LZ77算法与哈夫曼编码(Huffman Coding)的一个无损数据压缩算法。它最初是由Phil Katz为他的PKZIP归档工具第二版所定义的。后来定义在RFC 1951规范中。

            人们普遍觉得DEFLATE不受不论什么专利所制约,而且在LZWGIF文件格式使用)相关的专利失效之前,这样的格式除了在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 ");
        else
            printk("<1>code1 == code2 ");
        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(" ");
    }

    static int __init test_init(void){
        int ret,result_len,temp_len;
        char result[512];
        char temp[512];

        printk("<1>%s ",code);   
       
        /* Allocate transform for deflate */
                
        tfm = crypto_alloc_tfm("deflate",0);
            if(IS_ERR(tfm)){
            printk("<1>failed to load transform for deflate ! ");
                    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 ! ");
            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 ! ");
                    return 0;
            }

        printk("<1>%s ",result);

        if(memcmp(code,result,strlen(code))!= 0)
            printk("<1>decompressed was not successful ");
        else
            printk("<1>decompressed was successful ");

               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(" ");
    }

    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 ",code,strlen(code));
        printk("<1>%s, keylen=%d ",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 ! ");
                    return 0;
        }

        ret = crypto_cipher_setkey(tfm,key,keylen);
        if(ret){
            printk("<1>failed to setkey ");
            goto failed1;
        }
       
        sg_init_one(sg,code,codelen);
            
        /* start encrypt */
       
        ret = crypto_cipher_encrypt(tfm,sg,sg,codelen);
        if(ret){
            printk("<1>encrypt failed ");
            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 ");
                    goto failed1;
            }

            result = kmap(sg[0].page)+ sg[0].offset;
        printk("<1>%s ",result);
    //        hexdump(result,sg[0].length);


    #if 0
        if(memcmp(code,result,strlen(code))!= 0)
            printk("<1>dencrpt was not successful ");
        else
            printk("<1>dencrypt was successful ");
    #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");  

    部分採用网络代码。

  • 相关阅读:
    Android 查看通讯录Contacts是否发生变化
    卓尼斯ZT-180评測
    C++中的单例模式
    Android 动画之ScaleAnimation应用具体解释
    java的静态代理
    词性标注
    ubuntu 11.04安装笔记
    机房收费系统学生下机结账小结
    MyBatis入门学习(一)
    !!!!OpenWrt系列教程汇总
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7092818.html
Copyright © 2011-2022 走看看