brainfoolong / php-ascon
Ascon的实现,是一个轻量级的认证加密(AEAD)和哈希算法系列,适用于PHP 8+
Requires
- php: >=8.0.0
README
这是Ascon v1.2的PHP 8+实现,是一种认证密钥和哈希函数。它可以加密和解密任何类型的消息。它可以被看作是AES加密的继承者。该实现深受https://github.com/meichlseder/pyascon中Ascon的Python实现的影响。
关于Ascon
Ascon是一个轻量级且易于实现的认证加密(AEAD)和哈希算法系列,甚至可以采取针对侧信道攻击的额外防护措施。它是由格拉茨工业大学、英飞凌技术和拉德布德大学的密码学家团队设计的:Christoph Dobraunig、Maria Eichlseder、Florian Mendel和Martin Schläffer。
Ascon已被选为NIST轻量级密码学竞赛(2019–2023)的标准,并在CAESAR竞赛(2014–2019)的最终组合中被选为轻量级认证加密的主要选择。
有关规范和更多实现的更多信息,请在此处查找
关于我
我以前已经为AES PHP/JS加密编写了库。位操作有点酷,真的很有技术感。我喜欢Ascon的实现,当时还没有PHP实现。所以我编写了一个。如果你留个关注或者花点虚拟咖啡,那就太好了。
JavaScript/TypeScript实现
你可能还需要一个JavaScript/TypeScript(用于你的前端)实现。我已经在这里做了 -> https://github.com/brainfoolong/js-ascon
安装
# via composer
composer require brainfoolong/php-ascon
使用方法
更多示例请查看demo
文件夹。
use Nullix\Ascon\Ascon; // convenient usage (generating random nonce and hashing keys for you) $key = "mypassword"; $message = ["this can be any data type 😎 文 or encoding", 123]; $associatedData = "Some data 😋 文 This data is not contained in the encrypt output but must be passed to both encrypt and decrypt."; $encrypted = Ascon::encryptToHex($key, $message, $associatedData); $decrypted = Ascon::decryptFromHex($key, $encrypted, $associatedData); // raw usage of basic methods // key must be 16 bytes or 20 bytes, depending on variant $key = [0x90, 0x80, 0x70, 0x60, 0x50, 0x40, 0x30, 0x20, 0x10, 0xAA, 0x90, 0x90, 0x90, 0x90, 0xCC, 0xEF]; // nonce must be 16 bytes and should always be random bytes, you must use same nonce for encrypt and decrypt the same message $nonce = random_bytes(16); // this is the text you want to encrypt $plaintext = "Hi, i am a secret message!"; // associated data is not being encrypted, but is taken into account in the ciphertext // this means, you can only decrypt when you pass the exact same associated data to the decrypt function as well // so you can make sure that associated data and plaintext is not manipulated for given encrypted message // this is optional and can be an empty string $associatedData = "Some data to pass to encryption and decryption - This data is not contained in the ciphertext output."; $ciphertextByteArray = Ascon::encrypt($key, $nonce, $associatedData, $plaintext); $plaintextDecrypted = Ascon::decrypt($key, $nonce, $associatedData, $ciphertextByteArray); var_dump(Ascon::hash('Testmessage')); var_dump(Ascon::mac($key, 'Testmessage'));
性能和PHP限制(不是问题,但你应该注意)
Ascon需要64位无符号整数。PHP没有64位无符号整数,它只有64位有符号整数。因此,默认情况下,我们失去了一位完整的无符号64位数字(因为第64位用于符号而不是最后一个数字位)。Php内部将一个完整的64位数字转换为浮点数,这是一个混乱,因为它会丢失数据,因为浮点精度限制。
但是不要害怕,在这个实现中,我使用了两个32位整数。这会有一些性能影响,因为需要更多的操作来获得与uint 64位相同的结果。如果你需要大量的加密/解密,我总是推荐你不使用PHP来做这样的工作。使用原生的C实现,这要快得多。从PHP 8.2开始,你可以通过FFI嵌入它。
然而,你可能在这里是因为你需要将其集成到你的web服务应用程序中。在这种情况下,性能应该很好。
请参阅tests/performance.php
以获取有关各种消息数据大小的测试信息。
# no scientific tests, just executed on my local machine, results depend on your machine
# a "cycle" is one encryption and one decryption
### 10 cycles with 64 byte message data and 256 byte associated data ###
Total Time: 0.07 seconds
Memory Usage: 2MB
### 10 cycles with 256 byte message data and 1024 byte associated data ###
Total Time: 0.21 seconds
Memory Usage: 2MB
### 10 cycles with 2048 byte message data and 4096 byte associated data ###
Total Time: 0.92 seconds
Memory Usage: 2MB
### 10 cycles with 8192 byte message data and 0 byte associated data ###
Total Time: 1.34 seconds
Memory Usage: 4MB
实现算法
这是一个NIST LWC竞赛提交的Ascon v1.2的简单参考实现,包括以下内容:
-
以下3种变体的认证加密/解密:
Ascon-128
Ascon-128a
Ascon-80pq
-
哈希算法,包括4种哈希函数变体,具有固定256位(
Hash
)或可变(Xof
)输出长度Ascon-Hash
Ascon-Hasha
Ascon-Xof
Ascon-Xofa
-
消息认证码,包括5种MAC变体(来自https://eprint.iacr.org/2021/1574,不属于LWC提案),具有固定128位(
Mac
)或可变(Prf
)输出长度,包括一种适用于最多128位短消息的变体(PrfShort
)。Ascon-Mac
Ascon-Maca
Ascon-Prf
Ascon-Prfa
Ascon-PrfShort