Skip to content

Commit

Permalink
Update Crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
chiliec committed Feb 9, 2024
1 parent cc6ae8a commit ae4700f
Show file tree
Hide file tree
Showing 24 changed files with 458 additions and 722 deletions.
401 changes: 155 additions & 246 deletions Sources/Crypto/base58.c
100755 → 100644

Large diffs are not rendered by default.

96 changes: 0 additions & 96 deletions Sources/Crypto/hasher.c

This file was deleted.

62 changes: 0 additions & 62 deletions Sources/Crypto/hasher.h

This file was deleted.

21 changes: 5 additions & 16 deletions Sources/Crypto/base58.h → Sources/Crypto/include/base58.h
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Copyright (c) 2013-2014 Tomas Dzetkulic
* Copyright (c) 2013-2014 Pavol Rusnak
* Copyright (c) 2019 Johan Nordberg
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
Expand All @@ -24,23 +25,11 @@
#ifndef __BASE58_H__
#define __BASE58_H__

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "hasher.h"
#include "options.h"

int base58_encode_check(const uint8_t *data, int len, HasherType hasher_type, char *str, int strsize);
int base58_decode_check(const char *str, HasherType hasher_type, uint8_t *data, int datalen);

// Private
bool b58tobin(void *bin, size_t *binszp, const char *b58);
int b58check(const void *bin, size_t binsz, HasherType hasher_type, const char *base58str);
bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz);

#if USE_GRAPHENE
int base58gph_encode_check(const uint8_t *data, int datalen, char *str, int strsize);
int base58gph_decode_check(const char *str, uint8_t *data, int datalen);
int b58gphcheck(const void *bin, size_t binsz, const char *base58str);
#endif
size_t base58_encode(const uint8_t *data, size_t datalen, char *str,
size_t strsize);
size_t base58_decode(const char *str, uint8_t *data, size_t datalen);

#endif
10 changes: 6 additions & 4 deletions Sources/Crypto/include/crypto.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef CRYPTO_H__
#define CRYPTO_H__
#ifndef __CRYPTO_H__
#define __CRYPTO_H__

#include "../base58.h"
#include "base58.h"
#include "ripemd160.h"
#include "sha2.h"

#endif
#endif
22 changes: 22 additions & 0 deletions Sources/Crypto/include/ripemd160.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef __RIPEMD160_H__
#define __RIPEMD160_H__

#include <stdint.h>

#define RIPEMD160_BLOCK_LENGTH 64
#define RIPEMD160_DIGEST_LENGTH 20

typedef struct _RIPEMD160_CTX {
uint32_t total[2]; /*!< number of bytes processed */
uint32_t state[5]; /*!< intermediate digest state */
uint8_t buffer[RIPEMD160_BLOCK_LENGTH]; /*!< data block being processed */
} RIPEMD160_CTX;

void ripemd160_Init(RIPEMD160_CTX *ctx);
void ripemd160_Update(RIPEMD160_CTX *ctx, const uint8_t *input, uint32_t ilen);
void ripemd160_Final(RIPEMD160_CTX *ctx,
uint8_t output[RIPEMD160_DIGEST_LENGTH]);
void ripemd160(const uint8_t *msg, uint32_t msg_len,
uint8_t hash[RIPEMD160_DIGEST_LENGTH]);

#endif
2 changes: 1 addition & 1 deletion Sources/Crypto/sha2.h → Sources/Crypto/include/sha2.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ typedef struct _SHA512_CTX {
#define REVERSE32(w,x) { \
uint32_t tmp = (w); \
tmp = (tmp >> 16) | (tmp << 16); \
(x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
(x) = ((tmp & (uint32_t)0xff00ff00UL) >> 8) | ((tmp & (uint32_t)0x00ff00ffUL) << 8); \
}
#define REVERSE64(w,x) { \
uint64_t tmp = (w); \
Expand Down
66 changes: 63 additions & 3 deletions Sources/Crypto/memzero.c
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,66 @@
#ifndef __STDC_WANT_LIB_EXT1__
#define __STDC_WANT_LIB_EXT1__ 1 // C11's bounds-checking interface.
#endif
#include <string.h>

void memzero(void *s, size_t n)
{
memset(s, 0, n);
#ifdef _WIN32
#include <Windows.h>
#endif

#ifdef __unix__
#include <strings.h>
#include <sys/param.h>
#endif

// C11's bounds-checking interface.
#if defined(__STDC_LIB_EXT1__)
#define HAVE_MEMSET_S 1
#endif

// GNU C Library version 2.25 or later.
#if defined(__GLIBC__) && \
(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))
#define HAVE_EXPLICIT_BZERO 1
#endif

// Newlib
#if defined(__NEWLIB__)
#define HAVE_EXPLICIT_BZERO 1
#endif

// FreeBSD version 11.0 or later.
#if defined(__FreeBSD__) && __FreeBSD_version >= 1100037
#define HAVE_EXPLICIT_BZERO 1
#endif

// OpenBSD version 5.5 or later.
#if defined(__OpenBSD__) && OpenBSD >= 201405
#define HAVE_EXPLICIT_BZERO 1
#endif

// NetBSD version 7.2 or later.
#if defined(__NetBSD__) && __NetBSD_Version__ >= 702000000
#define HAVE_EXPLICIT_MEMSET 1
#endif

// Adapted from
// https://github.com/jedisct1/libsodium/blob/1647f0d53ae0e370378a9195477e3df0a792408f/src/libsodium/sodium/utils.c#L102-L130

void memzero(void *const pnt, const size_t len) {
#ifdef _WIN32
SecureZeroMemory(pnt, len);
#elif defined(HAVE_MEMSET_S)
memset_s(pnt, (rsize_t)len, 0, (rsize_t)len);
#elif defined(HAVE_EXPLICIT_BZERO)
explicit_bzero(pnt, len);
#elif defined(HAVE_EXPLICIT_MEMSET)
explicit_memset(pnt, 0, len);
#else
volatile unsigned char *volatile pnt_ = (volatile unsigned char *volatile)pnt;
size_t i = (size_t)0U;

while (i < len) {
pnt_[i++] = 0U;
}
#endif
}
2 changes: 1 addition & 1 deletion Sources/Crypto/memzero.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

#include <stddef.h>

void memzero(void *s, size_t n);
void memzero(void* const pnt, const size_t len);

#endif
Loading

0 comments on commit ae4700f

Please sign in to comment.