码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 基于ASCON的AEAD


    1. 引言

    前序博客:

    • ASCON:以“慢而稳”赢得NIST轻量级加密算法标准
    • 密码学中的AEAD(authenticated encryption with associated data)

    对称密钥加密过去数年来已发生改变,具体为:

    • 当今主要使用stream ciphers,因其比block ciphers要快得多。
    • 经常会使用AEAD(Authenticated Encryption with Additional Data)。
    • 在加密过程中常使用sponge函数,进而支持使用sponge函数来创建对称密钥和哈希函数。
    • Rust现在正在成为一种事实上的软件语言,它在编码方面提高了健壮性。

    ASCON代码示例见:

    • ASCON AEAD - Light-weight cipher
    • https://github.com/usnistgov/Lightweight-Cryptography-Benchmarking(C)
    • https://github.com/meichlseder/pyascon/blob/master/ascon.py(Python)
    • https://github.com/itzmeanjan/ascon(C++)
    • https://github.com/NomanNasirMinhas/ASCON_Benchmark(Rust)
    • https://github.com/sebastinas/isap-aead(Rust)
    • https://github.com/neoilir/Simple-ascon-hash-implementation-rust(Rust)
    • https://github.com/sebastinas/ascon-aead(Rust)
    • https://github.com/IAIK/ascon_hardware(VHDL,硬件实现)
    • https://github.com/ascon/ascon-c(C和汇编)
    • https://github.com/ascon/ascon-hardware(Python和VHDL,硬件实现)
    • https://github.com/RustCrypto/sponges/tree/master/ascon(Rust)
    • https://github.com/RustCrypto/AEADs/tree/master/ascon-aead(Rust)

    2. AEAD(Authenticated Encryption with Additional Data)

    所谓对称密钥加密,是指使用相同的密钥来加解密:
    在这里插入图片描述
    但是这样的对称密钥加密存在重放攻击问题,如:

    • Bob用对称密钥加密了一条消息给Alice,Alice收到密文用相同的对称密钥解密后,获得“你明天可休假一天”,于是Alice第二天休假了。
    • 但是,Eve窃听了上述消息,在第二天将相同的密文再次发送给Alice,Alice解密后,第三天又休假了一天。
    • Bob会很奇怪,为啥Alice连休了2天假。

    原因就在于Eve对密文进行了重放攻击。因此,需要将加密过程与某网络连接或session绑定,使得Eve无法重构相同的场景。

    通过增强的加密方法,使得既可认证加密,也可证明其完整性。这被称为关联数据的身份验证加密(AEAD)。为此,提供额外的数据来认证加密过程,并可识别密文已被修改因其无法被加密:
    在这里插入图片描述
    大多数传统的AEA方法为创建一个nonce值,并添加认证但不加密的额外数据(Additional Data, AD)。额外数据AD可为:

    addresses, ports, sequence numbers, protocol version numbers, and other fields that indicate how the plaintext or ciphertext should be handled, forwarded, or processed
    
    • 1

    这样就可将网络包与加密数据绑定,提供了完整性,使得入侵者无法复制粘贴其它通道的密文来形成攻击。如,若绑定包序号和端口号,使用另一序号或端口号将认证失败。

    可以用于AEAD的主要方法是AES GCM、AES SIV、AES CCM、ChaCha20/Poly1305和AES OCB3。每一个都是流密码,避免了CBC和ECB的块方法模式。

    3. 基于ASCON的AEAD

    如以https://github.com/RustCrypto/sponges/tree/master/ascon(Rust)实现的AEAD为例:

    cargo new arc
    
    • 1

    相应的cargo.toml文件为:

    [package]
    name = "arc"
    version = "0.1.0"
    edition = "2021"
    
    # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
    
    [dependencies]
    ascon = "0.1.4"
    rand="0.8.3"
    hex="0.4.3"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    相应的main.rs源代码为:

    use ascon;
    use rand::thread_rng;
    use rand::Rng;
    use hex::{self};
    use std::env;
    
    fn get_random_key16() ->  [u8; 16]{
        let mut arr = [0u8; 16];
        thread_rng().try_fill(&mut arr[..]).expect("Ooops!");
        return arr;
    }
    
    fn main() {
    
        let mut msg="hello";
        let mut aad1="test";
    
        let args: Vec<String> = env::args().collect();
      
        if args.len() >1 { msg = args[1].as_str();}
        if args.len() >2 { aad1 = args[2].as_str();}
    
        let randkey128=get_random_key16(); //为128字节
    
        let iv=get_random_key16(); //用作salt
    
    
        let plaintext=msg.as_bytes();
        let aad=aad1.as_bytes();
    
        let (ciphertext,tag) = ascon::aead_encrypt(&randkey128, &iv, 
            plaintext, aad); //tag也为128字节
    
    
      let pt=ascon::aead_decrypt(&randkey128, &iv,&ciphertext[..], &aad, &tag);
      
      let s = String::from_utf8(pt.unwrap()).expect("Found invalid UTF-8");
    
      println!("Message:\t{}\n",msg);
      println!("AAD:\t\t{}\n",aad1);
    
      println!("Key:\t\t{}\n",hex::encode(randkey128));
      println!("Cipher:\t\t{}\n",hex::encode(ciphertext));
      println!("Tag:\t\t{}\n",hex::encode(tag));
      println!("Decryped:\t{}", s);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    运行结果为:

    Message:        hello
    
    AAD:            test
    
    Key:            6680811197f36de07227b8f08ae31c33
    
    Cipher:         01e0d0d020
    
    Tag:            6e90b3a9790c28188172d5bd8041555d
    
    Decryped:       hello
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    参考资料

    [1] Prof Bill Buchanan OBE 2023年9月博客 ASCON, Rust and AEAD: Is ASCON better than AES?

  • 相关阅读:
    ASAN入门参考
    【基础IO⑨】:重定向实现原理 &&“Linux下一切皆文件“
    钉钉事件订阅AES_KEY解密失败踩坑
    “AccelerationMotionCount“ app Tech Support(URL)
    【博学谷学习记录】超强总结,用心分享|架构师-Kafka优化手段
    API设计笔记:抽象基类、工厂方法、扩展工厂
    SpringBoot SpringBoot 运维实用篇 4 日志 4.4 文件记录日志
    zabbix监控H3C设备
    Hive-Hive函数
    Arnold在C4D中使用的ACES使用盲区!
  • 原文地址:https://blog.csdn.net/mutourend/article/details/132871205
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号