基于ASCON的AEAD

1. 引言

前序博客:

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

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

ASCON代码示例见:

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

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

可以用于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为例:

shell 复制代码
cargo new arc

相应的cargo.toml文件为:

rust 复制代码
[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"

相应的main.rs源代码为:

rust 复制代码
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);
}

运行结果为:

shell 复制代码
Message:        hello

AAD:            test

Key:            6680811197f36de07227b8f08ae31c33

Cipher:         01e0d0d020

Tag:            6e90b3a9790c28188172d5bd8041555d

Decryped:       hello

参考资料

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

相关推荐
mutourend9 个月前
OpenSSL 3.2.0新增Argon2支持——防GPU暴力攻击
基础理论
mutourend1 年前
FRI及相关SNARKs的Fiat-Shamir安全
基础理论
mutourend1 年前
Hard Problems简介
基础理论
mutourend1 年前
GPU/CPU友好的模乘算法:Multi-Precision Fast Modular Multiplication
基础理论