ChaCha20-Poly1305是Google所采用的一种新式加密算法,性能强大,在CPU为精简指令集的ARM平台上尤为显著(ARM v8前效果较明显),在同等配置的手机中表现是AES的4倍。
Chacha20需要用到的代码可以分别从以下链接获取到:
https://cr.yp.to/streamciphers/timings/estreambench/submissions/salsa20/chacha20/merged/chacha.c
https://cr.yp.to/streamciphers/timings/estreambench/submissions/salsa20/chacha20/ref/ecrypt-sync.h
https://cr.yp.to/snuffle/ecrypt-config.h
https://cr.yp.to/snuffle/ecrypt-machine.h
https://cr.yp.to/snuffle/ecrypt-portable.h
上述5个文件,一个字母都不用动。直接放入工程编译就可以。
测试代码如下:
#include <stdio.h>
#include “ecrypt-sync.h”
int main(int argc, char* argv[])
{
ECRYPT_ctx chacha;
int i, j, index;
u8 iv[] = {0, 0, 0, 0, 0, 0, 0, 0};
u8 key[] = {0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,