🤔Cppcryptfs是基于gocryptfs设计的,但是由于调用的库迟迟不添加Xchacha20-poly1305,所以没有加入Xchacha20-poly1305;我给手动添加上了。
截图
部分代码
#include "stdafx.h"
#include <cstring>
#include <cstdint>
#include "chacha20.h"
static void chacha20_quarter_round(uint32_t s[16], int a, int b, int c, int d)
{
s[a] += s[b]; s[d] ^= s[a]; s[d] = (s[d] << 16) | (s[d] >> 16);
s[c] += s[d]; s[b] ^= s[c]; s[b] = (s[b] << 12) | (s[b] >> 20);
s[a] += s[b]; s[d] ^= s[a]; s[d] = (s[d] << 8) | (s[d] >> 24);
s[c] += s[d]; s[b] ^= s[c]; s[b] = (s[b] << 7) | (s[b] >> 25);
}
static void wipe_words(uint32_t *p, int n)
{
volatile uint32_t *v = p;
while (n--)
*v++ = 0;
}
void hchacha20(const unsigned char key[32], const unsigned char nonce[16], unsigned char out[32])
{
static const uint32_t constant[4] = {
0x61707865, 0x3320646e, 0x79622d32, 0x6b206574
};
uint32_t state[16];
state[0] = constant[0];
state[1] = constant[1];
state[2] = constant[2];
state[3] = constant[3];
for (int i = 0; i < 8; i++)
state[4 + i] = (uint32_t)key[4*i] | ((uint32_t)key[4*i+1] << 8) |
((uint32_t)key[4*i+2] << 16) | ((uint32_t)key[4*i+3] << 24);
for (int i = 0; i < 4; i++)
state[12 + i] = (uint32_t)nonce[4*i] | ((uint32_t)nonce[4*i+1] << 8) |
((uint32_t)nonce[4*i+2] << 16) | ((uint32_t)nonce[4*i+3] << 24);
uint32_t working[16];
memcpy(working, state, sizeof(working));
for (int round = 0; round < 10; round++) {
chacha20_quarter_round(working, 0, 4, 8, 12);
chacha20_quarter_round(working, 1, 5, 9, 13);
chacha20_quarter_round(working, 2, 6, 10, 14);
chacha20_quarter_round(working, 3, 7, 11, 15);
chacha20_quarter_round(working, 0, 5, 10, 15);
chacha20_quarter_round(working, 1, 6, 11, 12);
chacha20_quarter_round(working, 2, 7, 8, 13);
chacha20_quarter_round(working, 3, 4, 9, 14);
}
uint32_t out_words[8];
for (int i = 0; i < 4; i++)
out_words[i] = working[i];
for (int i = 0; i < 4; i++)
out_words[4 + i] = working[12 + i];
memcpy(out, out_words, 32);
wipe_words(state, 16);
wipe_words(working, 16);
wipe_words(out_words, 8);
}
开源地址
https://github.com/fzxx/cppcryptfs