refactor: create header file
This commit is contained in:
parent
668120ae48
commit
96577f67ca
@ -1,3 +1,4 @@
|
|||||||
|
#include "ChaCha20_DRBG.h"
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@ -9,42 +10,7 @@
|
|||||||
|
|
||||||
// #include <openssl/evp.h>
|
// #include <openssl/evp.h>
|
||||||
|
|
||||||
#define SUCCESS 0
|
|
||||||
#define ERROR -1
|
|
||||||
|
|
||||||
// 为ChaCha20设置适当的长度。
|
|
||||||
#define KEYLEN 32 // ChaCha20的密钥长度(字节)
|
|
||||||
#define COUNTERLEN 4 // ChaCha20的计数器长度(字节)
|
|
||||||
#define NONCELEN 12 // ChaCha20的nonce长度(字节)
|
|
||||||
#define OUTLEN 64 // 输出长度是64字节(512位)
|
|
||||||
#define SEEDLEN 96 // KEYLEN + OUTLEN(字节)
|
|
||||||
|
|
||||||
// for Instantiate
|
|
||||||
#define MAX_PERSONALIZATION_STRING_LEN 800 // (字节)
|
|
||||||
#define min_entropy_input_length 32 // (字节)
|
|
||||||
#define max_entropy_input_length pow(2, 35) / 8 // (字节)
|
|
||||||
|
|
||||||
// for handles
|
|
||||||
#define MAX_STATES 100
|
|
||||||
#define INVALID_HANDLE -1
|
|
||||||
|
|
||||||
// for get_entropy_input
|
|
||||||
#define reseed_interval_in_counter pow(2, 10) // (次)
|
|
||||||
#define reseed_interval_in_time 60 //(秒)
|
|
||||||
|
|
||||||
#define MAX_NUMBER_OF_BITS 1024 // (比特)
|
|
||||||
|
|
||||||
// for entropy
|
|
||||||
#define BLOCK_SIZE 64
|
|
||||||
#define HASH_SIZE 32
|
|
||||||
|
|
||||||
/*----------------熵源的相关定义与函数----------------*/
|
/*----------------熵源的相关定义与函数----------------*/
|
||||||
typedef struct {
|
|
||||||
uint8_t data[BLOCK_SIZE];
|
|
||||||
uint32_t datalen;
|
|
||||||
uint64_t bitlen;
|
|
||||||
uint32_t state[8];
|
|
||||||
} SHA256_CTX;
|
|
||||||
|
|
||||||
void sha256_transform(SHA256_CTX *ctx, const uint8_t data[]) {
|
void sha256_transform(SHA256_CTX *ctx, const uint8_t data[]) {
|
||||||
uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
|
uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
|
||||||
@ -183,12 +149,9 @@ void get_entropy_input(const uint8_t *data, size_t len, uint8_t *entropy) {
|
|||||||
|
|
||||||
memcpy(entropy, hash, HASH_SIZE);
|
memcpy(entropy, hash, HASH_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------*/
|
/*----------------------------------------------*/
|
||||||
|
|
||||||
/*--------------------卡方检验相关定义与函数---------------------------*/
|
/*--------------------卡方检验相关定义与函数---------------------------*/
|
||||||
#define RANDOM_BITS_NUMBER 100
|
|
||||||
#define RANDOM_BITS_SIZE 128
|
|
||||||
|
|
||||||
int test_randomness(const unsigned char *bits) {
|
int test_randomness(const unsigned char *bits) {
|
||||||
// Test the randomness of the given bits using the chi-squared test
|
// Test the randomness of the given bits using the chi-squared test
|
||||||
@ -214,30 +177,6 @@ int test_randomness(const unsigned char *bits) {
|
|||||||
/*-------------------------------------------------------*/
|
/*-------------------------------------------------------*/
|
||||||
|
|
||||||
/*----------------ChaCha20的相关定义与函数------------------*/
|
/*----------------ChaCha20的相关定义与函数------------------*/
|
||||||
#define ROTL32(v, n) (((v) << (n)) | ((v) >> (32 - (n))))
|
|
||||||
|
|
||||||
#define U32TO8_LITTLE(p, v) \
|
|
||||||
{ \
|
|
||||||
(p)[0] = (uint8_t)((v)); \
|
|
||||||
(p)[1] = (uint8_t)((v) >> 8); \
|
|
||||||
(p)[2] = (uint8_t)((v) >> 16); \
|
|
||||||
(p)[3] = (uint8_t)((v) >> 24); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define U8TO32_LITTLE(p) \
|
|
||||||
(((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \
|
|
||||||
((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24))
|
|
||||||
|
|
||||||
// ChaCha的轮函数
|
|
||||||
#define QUARTERROUND(x, a, b, c, d) \
|
|
||||||
x[a] += x[b]; \
|
|
||||||
x[d] = ROTL32(x[d] ^ x[a], 16); \
|
|
||||||
x[c] += x[d]; \
|
|
||||||
x[b] = ROTL32(x[b] ^ x[c], 12); \
|
|
||||||
x[a] += x[b]; \
|
|
||||||
x[d] = ROTL32(x[d] ^ x[a], 8); \
|
|
||||||
x[c] += x[d]; \
|
|
||||||
x[b] = ROTL32(x[b] ^ x[c], 7);
|
|
||||||
|
|
||||||
static const char sigma[] = "expand 32-byte k";
|
static const char sigma[] = "expand 32-byte k";
|
||||||
static const char tau[] = "expand 16-byte k";
|
static const char tau[] = "expand 16-byte k";
|
||||||
|
111
ChaCha20_DRBG.h
Normal file
111
ChaCha20_DRBG.h
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#ifndef CHACHA20_DRBG_H
|
||||||
|
#define CHACHA20_DRBG_H
|
||||||
|
|
||||||
|
#define SUCCESS 0
|
||||||
|
#define ERROR -1
|
||||||
|
|
||||||
|
// 为ChaCha20设置适当的长度。
|
||||||
|
#define KEYLEN 32 // ChaCha20的密钥长度(字节)
|
||||||
|
#define COUNTERLEN 4 // ChaCha20的计数器长度(字节)
|
||||||
|
#define NONCELEN 12 // ChaCha20的nonce长度(字节)
|
||||||
|
#define OUTLEN 64 // 输出长度是64字节(512位)
|
||||||
|
#define SEEDLEN 96 // KEYLEN + OUTLEN(字节)
|
||||||
|
|
||||||
|
// for Instantiate
|
||||||
|
#define MAX_PERSONALIZATION_STRING_LEN 800 // (字节)
|
||||||
|
#define min_entropy_input_length 32 // (字节)
|
||||||
|
#define max_entropy_input_length pow(2, 35) / 8 // (字节)
|
||||||
|
|
||||||
|
// for handles
|
||||||
|
#define MAX_STATES 100
|
||||||
|
#define INVALID_HANDLE -1
|
||||||
|
|
||||||
|
// for get_entropy_input
|
||||||
|
#define reseed_interval_in_counter pow(2, 10) // (次)
|
||||||
|
#define reseed_interval_in_time 60 //(秒)
|
||||||
|
|
||||||
|
#define MAX_NUMBER_OF_BITS 1024 // (比特)
|
||||||
|
|
||||||
|
// for entropy
|
||||||
|
#define BLOCK_SIZE 64
|
||||||
|
#define HASH_SIZE 32
|
||||||
|
|
||||||
|
/*----------------熵源的相关定义与函数----------------*/
|
||||||
|
typedef struct {
|
||||||
|
uint8_t data[BLOCK_SIZE];
|
||||||
|
uint32_t datalen;
|
||||||
|
uint64_t bitlen;
|
||||||
|
uint32_t state[8];
|
||||||
|
} SHA256_CTX;
|
||||||
|
|
||||||
|
void sha256_transform(SHA256_CTX *ctx, const uint8_t data[]);
|
||||||
|
|
||||||
|
void sha256_init(SHA256_CTX *ctx);
|
||||||
|
|
||||||
|
void sha256_update(SHA256_CTX *ctx, const uint8_t data[], size_t len);
|
||||||
|
|
||||||
|
void sha256_final(SHA256_CTX *ctx, uint8_t hash[]);
|
||||||
|
|
||||||
|
void get_entropy_source(uint8_t *entropy_source);
|
||||||
|
|
||||||
|
void get_entropy_input(const uint8_t *data, size_t len, uint8_t *entropy);
|
||||||
|
|
||||||
|
|
||||||
|
/*----------------------------------------------*/
|
||||||
|
|
||||||
|
/*--------------------卡方检验相关定义与函数---------------------------*/
|
||||||
|
#define RANDOM_BITS_NUMBER 100
|
||||||
|
#define RANDOM_BITS_SIZE 128
|
||||||
|
|
||||||
|
int test_randomness(const unsigned char *bits);
|
||||||
|
|
||||||
|
/*-------------------------------------------------------*/
|
||||||
|
|
||||||
|
/*----------------ChaCha20的相关定义与函数------------------*/
|
||||||
|
|
||||||
|
#define ROTL32(v, n) (((v) << (n)) | ((v) >> (32 - (n))))
|
||||||
|
|
||||||
|
#define U32TO8_LITTLE(p, v) \
|
||||||
|
{ \
|
||||||
|
(p)[0] = (uint8_t)((v)); \
|
||||||
|
(p)[1] = (uint8_t)((v) >> 8); \
|
||||||
|
(p)[2] = (uint8_t)((v) >> 16); \
|
||||||
|
(p)[3] = (uint8_t)((v) >> 24); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define U8TO32_LITTLE(p) \
|
||||||
|
(((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \
|
||||||
|
((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24))
|
||||||
|
|
||||||
|
// ChaCha的轮函数
|
||||||
|
#define QUARTERROUND(x, a, b, c, d) \
|
||||||
|
x[a] += x[b]; \
|
||||||
|
x[d] = ROTL32(x[d] ^ x[a], 16); \
|
||||||
|
x[c] += x[d]; \
|
||||||
|
x[b] = ROTL32(x[b] ^ x[c], 12); \
|
||||||
|
x[a] += x[b]; \
|
||||||
|
x[d] = ROTL32(x[d] ^ x[a], 8); \
|
||||||
|
x[c] += x[d]; \
|
||||||
|
x[b] = ROTL32(x[b] ^ x[c], 7);
|
||||||
|
|
||||||
|
void ChaCha20_block(uint32_t state[16], uint32_t output[16]);
|
||||||
|
|
||||||
|
void ChaCha20_setup(uint32_t state[16], const uint8_t key[32],
|
||||||
|
const uint8_t nonce[12], uint8_t counter[4]);
|
||||||
|
|
||||||
|
void ChaCha20_encrypt(uint32_t state[16], const uint8_t *in, uint8_t *out,
|
||||||
|
size_t length, bool increment_flag);
|
||||||
|
|
||||||
|
|
||||||
|
/*----------------------------------------------------*/
|
||||||
|
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user