前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >AEAD 认证加密

AEAD 认证加密

原创
作者头像
vanguard
修改2020-03-17 18:41:27
2.9K0
修改2020-03-17 18:41:27
举报
文章被收录于专栏:vanguardvanguard

AEAD stands for Authenticated Encryption with Associated Data. AEAD ciphers simultaneously provide confidentiality, integrity, and authenticity. They have excellent performance and power efficiency on modern hardware. Users should use AEAD ciphers whenever possible.

认证加密/Authenticated encryption/AE和带有关联数据的认证加密/authenticated encryption with associated data/AEAD/AE的变种是一种能够同时保证数据的保密性、 完整性和真实性的一种加密模式。这些属性都是在一个易于使用的编程接口下提供的。人们观察发现安全地将保密模式与认证模式组合可能是容易出错和困难的,于是认证加密应运而生。这一点已由许多实际攻击证实,这些攻击通过对身份验证(包括SSL与TLS)的不正确实现或缺失,引入到了生产协议和应用程序中。

AEAD 产生的原因很简单,单纯的对称加密算法,其解密步骤是无法确认密钥是否正确的。也就是说,加密后的数据可以用任何密钥执行解密运算,得到一组疑似原始数据,而不知道密钥是否是正确的,也不知道解密出来的原始数据是否正确。

因此,需要在单纯的加密算法之上,加上一层验证手段,来确认解密步骤是否正确。

简单地把加密算法和认证算法组合,可以实现上述目的,并由此产生了几个方案:

  • EtM (Encryption then MAC)

先加密,然后对密文进行 MAC 运算(一般用各种 HMAC),把二者拼接起来,发给接收方。

接收方先验证 MAC,如果验证通过,则证明密钥是正确的,然后执行解密运算。

  • E&M (Encryption and MAC)

同时对原始数据执行加密和 MAC 运算,把二者拼接起来,发给接收方。

接收方先进行解密,然后对解密结果执行 MAC 运算,比对发来的 MAC,验证正确性。

  • MtE (MAC then Encryption)

与 EtM 相反,先对原始数据执行 MAC 运算,与原始数据拼接后,执行加密算法,将密文发送给接收方。

接受方先进行解密,然后执行 MAC 运算,验证解密结果是否正确。

从 2008 年起,业内开始提出,需要在一个算法在内部同时实现加密和认证

基于这个思想,一些新的算法被提出,这些算法被称为真正的 AEAD 算法。

Name

Alias

Key Size

Salt Size

Nonce Size

Tag Size

AEAD_CHACHA20_POLY1305

chacha20-ietf-poly1305

32

32

12

16

AEAD_AES_256_GCM

aes-256-gcm

32

32

12

16

AEAD_AES_192_GCM

aes-192-gcm

24

24

12

16

AEAD_AES_128_GCM

aes-128-gcm

16

16

12

16

还有一种效率最高的一种: xchacha20-ietf-poly1305

XChaCha20-Poly1305 applies the construction described in Daniel Bernstein's Extending the Salsa20 nonce paper to the ChaCha20 cipher in order to extend the nonce size to 192-bit. This extended nonce size allows random nonces to be safely used, and also facilitates the construction of misuse-resistant schemes. The XChaCha20-Poly1305 implementation in libsodium is portable across all supported architectures.

The main limitation of XChaCha20-Poly1305 is that it is not widely implemented in other libraries yet. However, it will soon become an IETF standard.

在具备 AES 加速的 CPU(桌面,服务器)上,建议使用 AES-XXX-GCM 系列,

移动设备建议使用 ChaCha20-IETF-Poly1305 系列。

#!/usr/bin/env python
# ============================== AES-256 GCM ============================== #
# The GCM/Galois Counter Mode is a mode of operation for block ciphers.
# An AEAD mode is a type of block cipher mode that simultaneously encrupts 
# the message as well as authenticating it.
#
# pip install cryptography
#
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.backends import default_backend
import os

backend = default_backend()
nonce = os.urandom(12)
message = b"Hello world ~"
aaed = b"Not Secret"
key = AESGCM.generate_key(bit_length=256)
aesgcm = AESGCM(key)
ct = aesgcm.encrypt(nonce,message,aaed)
assert message,aesgcm.decrypt(nonce,ct,aaed)

print(ct)
print(message)
print(aesgcm.decrypt(nonce,ct,aaed))

b'\x18N\xcc@Y\x00\x1f\xf2WG\x97V8\x93\xe2\x08\xffy#\xa5\xdd\xfe\xa5@\xdf\xa563\x0c'

b'Hello world ~'

b'Hello world ~'

[Finished in 0.2s]

https://zhuanlan.zhihu.com/p/28566058

https://download.libsodium.org/doc/secret-key_cryptography/aead#tldr-which-one-should-i-use

https://libsodium.gitbook.io/doc/secret-key_cryptography/aead/chacha20-poly1305/xchacha20-poly1305_construction

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
多因子身份认证
多因子身份认证(Multi-factor Authentication Service,MFAS)的目的是建立一个多层次的防御体系,通过结合两种或三种认证因子(基于记忆的/基于持有物的/基于生物特征的认证因子)验证访问者的身份,使系统或资源更加安全。攻击者即使破解单一因子(如口令、人脸),应用的安全依然可以得到保障。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档