mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Merge pull request #925 from splinterofchaos/sha256
sha256.c: clint, -Wconversion, stdbool, and other improvements.
This commit is contained in:
commit
d611115305
@ -106,8 +106,6 @@ src/nvim/screen.c
|
||||
src/nvim/screen.h
|
||||
src/nvim/search.c
|
||||
src/nvim/search.h
|
||||
src/nvim/sha256.c
|
||||
src/nvim/sha256.h
|
||||
src/nvim/sign_defs.h
|
||||
src/nvim/spell.c
|
||||
src/nvim/spell.h
|
||||
|
@ -81,7 +81,6 @@ set(CONV_SOURCES
|
||||
regexp.c
|
||||
screen.c
|
||||
search.c
|
||||
sha256.c
|
||||
spell.c
|
||||
syntax.c
|
||||
tag.c
|
||||
|
@ -12,12 +12,13 @@
|
||||
/// 2. sha2_seed() generates a random header.
|
||||
/// sha256_self_test() is implicitly called once.
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h> // for size_t
|
||||
#include <stdio.h> // for snprintf().
|
||||
#include <stdlib.h> // for rand_r().
|
||||
|
||||
#include "nvim/os/time.h"
|
||||
#include "nvim/vim.h"
|
||||
#include "nvim/sha256.h"
|
||||
#include "nvim/os/time.h" // for os_hrtime().
|
||||
#include "nvim/sha256.h" // for context_sha256_T
|
||||
#include "nvim/vim.h" // for STRCPY()/STRLEN().
|
||||
|
||||
#ifdef INCLUDE_GENERATED_DECLARATIONS
|
||||
# include "sha256.c.generated.h"
|
||||
@ -51,9 +52,10 @@ void sha256_start(context_sha256_T *ctx)
|
||||
ctx->state[7] = 0x5BE0CD19;
|
||||
}
|
||||
|
||||
static void sha256_process(context_sha256_T *ctx, char_u data[64])
|
||||
static void sha256_process(context_sha256_T *ctx,
|
||||
const char_u data[SHA256_BUFFER_SIZE])
|
||||
{
|
||||
uint32_t temp1, temp2, W[64];
|
||||
uint32_t temp1, temp2, W[SHA256_BUFFER_SIZE];
|
||||
uint32_t A, B, C, D, E, F, G, H;
|
||||
|
||||
GET_UINT32(W[0], data, 0);
|
||||
@ -179,24 +181,23 @@ static void sha256_process(context_sha256_T *ctx, char_u data[64])
|
||||
ctx->state[7] += H;
|
||||
}
|
||||
|
||||
void sha256_update(context_sha256_T *ctx, char_u *input, uint32_t length)
|
||||
void sha256_update(context_sha256_T *ctx, const char_u *input, size_t length)
|
||||
{
|
||||
uint32_t left, fill;
|
||||
|
||||
if (length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
left = ctx->total[0] & 0x3F;
|
||||
fill = 64 - left;
|
||||
uint32_t left = ctx->total[0] & (SHA256_BUFFER_SIZE-1); // left < buf size
|
||||
|
||||
ctx->total[0] += length;
|
||||
ctx->total[0] += (uint32_t) length;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if (ctx->total[0] < length) {
|
||||
ctx->total[1]++;
|
||||
}
|
||||
|
||||
size_t fill = SHA256_BUFFER_SIZE - left;
|
||||
|
||||
if (left && (length >= fill)) {
|
||||
memcpy((void *)(ctx->buffer + left), (void *)input, fill);
|
||||
sha256_process(ctx, ctx->buffer);
|
||||
@ -205,10 +206,10 @@ void sha256_update(context_sha256_T *ctx, char_u *input, uint32_t length)
|
||||
left = 0;
|
||||
}
|
||||
|
||||
while (length >= 64) {
|
||||
while (length >= SHA256_BUFFER_SIZE) {
|
||||
sha256_process(ctx, input);
|
||||
length -= 64;
|
||||
input += 64;
|
||||
length -= SHA256_BUFFER_SIZE;
|
||||
input += SHA256_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
if (length) {
|
||||
@ -216,14 +217,14 @@ void sha256_update(context_sha256_T *ctx, char_u *input, uint32_t length)
|
||||
}
|
||||
}
|
||||
|
||||
static char_u sha256_padding[64] = {
|
||||
static char_u sha256_padding[SHA256_BUFFER_SIZE] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
void sha256_finish(context_sha256_T *ctx, char_u digest[32])
|
||||
void sha256_finish(context_sha256_T *ctx, char_u digest[SHA256_SUM_SIZE])
|
||||
{
|
||||
uint32_t last, padn;
|
||||
uint32_t high, low;
|
||||
@ -251,6 +252,7 @@ void sha256_finish(context_sha256_T *ctx, char_u digest[32])
|
||||
PUT_UINT32(ctx->state[7], digest, 28);
|
||||
}
|
||||
|
||||
#define SHA_STEP 2
|
||||
|
||||
/// Gets the hex digest of the buffer.
|
||||
///
|
||||
@ -261,25 +263,25 @@ void sha256_finish(context_sha256_T *ctx, char_u digest[32])
|
||||
///
|
||||
/// @returns hex digest of "buf[buf_len]" in a static array.
|
||||
/// if "salt" is not NULL also do "salt[salt_len]".
|
||||
char_u *sha256_bytes(char_u *buf, int buf_len, char_u *salt, int salt_len)
|
||||
char_u *sha256_bytes(const char_u *restrict buf, size_t buf_len,
|
||||
const char_u *restrict salt, size_t salt_len)
|
||||
{
|
||||
char_u sha256sum[32];
|
||||
static char_u hexit[65];
|
||||
int j;
|
||||
char_u sha256sum[SHA256_SUM_SIZE];
|
||||
static char_u hexit[SHA256_BUFFER_SIZE + 1]; // buf size + NULL
|
||||
context_sha256_T ctx;
|
||||
|
||||
sha256_self_test();
|
||||
|
||||
sha256_start(&ctx);
|
||||
sha256_update(&ctx, buf, buf_len);
|
||||
sha256_update(&ctx, buf, buf_len);
|
||||
|
||||
if (salt != NULL) {
|
||||
sha256_update(&ctx, salt, salt_len);
|
||||
}
|
||||
sha256_finish(&ctx, sha256sum);
|
||||
|
||||
for (j = 0; j < 32; j++) {
|
||||
sprintf((char *) hexit + j * 2, "%02x", sha256sum[j]);
|
||||
for (size_t j = 0; j < SHA256_SUM_SIZE; j++) {
|
||||
snprintf((char *) hexit + j * SHA_STEP, SHA_STEP+1, "%02x", sha256sum[j]);
|
||||
}
|
||||
hexit[sizeof(hexit) - 1] = '\0';
|
||||
return hexit;
|
||||
@ -303,51 +305,51 @@ static char *sha_self_test_vector[] = {
|
||||
|
||||
/// Perform a test on the SHA256 algorithm.
|
||||
///
|
||||
/// @return FAIL or OK.
|
||||
int sha256_self_test(void)
|
||||
/// @returns true if not failures generated.
|
||||
bool sha256_self_test(void)
|
||||
{
|
||||
int i, j;
|
||||
char output[65];
|
||||
char output[SHA256_BUFFER_SIZE + 1]; // buf size + NULL
|
||||
context_sha256_T ctx;
|
||||
char_u buf[1000];
|
||||
char_u sha256sum[32];
|
||||
static int failures = 0;
|
||||
char_u sha256sum[SHA256_SUM_SIZE];
|
||||
char_u *hexit;
|
||||
static int sha256_self_tested = 0;
|
||||
|
||||
if (sha256_self_tested > 0) {
|
||||
return failures > 0 ? FAIL : OK;
|
||||
static bool sha256_self_tested = false;
|
||||
static bool failures = false;
|
||||
|
||||
if (sha256_self_tested) {
|
||||
return failures == false;
|
||||
}
|
||||
sha256_self_tested = 1;
|
||||
sha256_self_tested = true;
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
for (size_t i = 0; i < 3; i++) {
|
||||
if (i < 2) {
|
||||
hexit = sha256_bytes((char_u *) sha_self_test_msg[i],
|
||||
(int) STRLEN(sha_self_test_msg[i]),
|
||||
STRLEN(sha_self_test_msg[i]),
|
||||
NULL, 0);
|
||||
STRCPY(output, hexit);
|
||||
} else {
|
||||
sha256_start(&ctx);
|
||||
memset(buf, 'a', 1000);
|
||||
|
||||
for (j = 0; j < 1000; j++) {
|
||||
for (size_t j = 0; j < 1000; j++) {
|
||||
sha256_update(&ctx, (char_u *) buf, 1000);
|
||||
}
|
||||
sha256_finish(&ctx, sha256sum);
|
||||
|
||||
for (j = 0; j < 32; j++) {
|
||||
sprintf(output + j * 2, "%02x", sha256sum[j]);
|
||||
for (size_t j = 0; j < SHA256_SUM_SIZE; j++) {
|
||||
snprintf(output + j * SHA_STEP, SHA_STEP+1, "%02x", sha256sum[j]);
|
||||
}
|
||||
}
|
||||
|
||||
if (memcmp(output, sha_self_test_vector[i], 64)) {
|
||||
failures++;
|
||||
if (memcmp(output, sha_self_test_vector[i], SHA256_BUFFER_SIZE)) {
|
||||
failures = true;
|
||||
output[sizeof(output) - 1] = '\0';
|
||||
|
||||
// printf("sha256_self_test %d failed %s\n", i, output);
|
||||
}
|
||||
}
|
||||
return failures > 0 ? FAIL : OK;
|
||||
return failures == false;
|
||||
}
|
||||
|
||||
/// Fill "header[header_len]" with random_data.
|
||||
@ -357,20 +359,21 @@ int sha256_self_test(void)
|
||||
/// @param header_len
|
||||
/// @param salt
|
||||
/// @param salt_len
|
||||
void sha2_seed(char_u *header, int header_len, char_u *salt, int salt_len)
|
||||
void sha2_seed(char_u *restrict header, size_t header_len,
|
||||
char_u *restrict salt, size_t salt_len)
|
||||
{
|
||||
static char_u random_data[1000];
|
||||
char_u sha256sum[32];
|
||||
char_u sha256sum[SHA256_SUM_SIZE];
|
||||
context_sha256_T ctx;
|
||||
|
||||
srand((unsigned int) os_hrtime());
|
||||
unsigned int seed = (unsigned int) os_hrtime();
|
||||
|
||||
int i;
|
||||
for (i = 0; i < (int) sizeof(random_data) - 1; i++) {
|
||||
random_data[i] = (char_u) ((os_hrtime() ^ rand()) & 0xff);
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(random_data) - 1; i++) {
|
||||
random_data[i] = (char_u) ((os_hrtime() ^ (uint64_t)rand_r(&seed)) & 0xff);
|
||||
}
|
||||
sha256_start(&ctx);
|
||||
sha256_update(&ctx, (char_u *) random_data, sizeof(random_data));
|
||||
sha256_update(&ctx, random_data, sizeof(random_data));
|
||||
sha256_finish(&ctx, sha256sum);
|
||||
|
||||
// put first block into header.
|
||||
|
@ -1,10 +1,17 @@
|
||||
#ifndef NVIM_SHA256_H
|
||||
#define NVIM_SHA256_H
|
||||
|
||||
#include <stdint.h> // for uint32_t
|
||||
|
||||
#include "nvim/types.h" // for char_u
|
||||
|
||||
#define SHA256_BUFFER_SIZE 64
|
||||
#define SHA256_SUM_SIZE 32
|
||||
|
||||
typedef struct {
|
||||
uint32_t total[2];
|
||||
uint32_t state[8];
|
||||
char_u buffer[64];
|
||||
char_u buffer[SHA256_BUFFER_SIZE];
|
||||
} context_sha256_T;
|
||||
|
||||
#ifdef INCLUDE_GENERATED_DECLARATIONS
|
||||
|
Loading…
Reference in New Issue
Block a user