Skip to content

Commit

Permalink
Used OpenSSL correctly. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
tfpf authored Oct 1, 2023
1 parent 3114963 commit 31817eb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ add_library(hdrbg SHARED ${sources})
target_include_directories(hdrbg PRIVATE include)
configure_file(hdrbg.pc.in hdrbg.pc @ONLY)

find_package(OpenSSL 3.0.0)
if(OPENSSL_FOUND)
target_compile_definitions(hdrbg PRIVATE TFPF_HASH_DRBG_OPENSSL_FOUND=1)
target_include_directories(hdrbg PRIVATE ${OPENSSL_INCLUDE_DIR})
target_link_libraries(hdrbg PRIVATE ${OPENSSL_CRYPTO_LIBRARIES})
endif()

set_target_properties(hdrbg PROPERTIES
PUBLIC_HEADER include/hdrbg.h
SOVERSION 1
Expand Down
6 changes: 4 additions & 2 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
| Additional Input | No |
| Personalisation String | No |

* In C, a byte need not be 8 bits wide. However, this implementation uses the term 'byte' to refer to an 8-bit number.
Hence, fixed-width integer types are used liberally.
* SHA-256 has been implemented from scratch, because I wanted this package to have no dependencies.
* However, if OpenSSL development libraries are found, its SHA-256 implementation is used if the C compiler provides
8-bit bytes.
* `/dev/urandom` is read to obtain entropy for seeding and reseeding.
* It is assumed to always provide sufficient entropy.
* Nonces are generated by appending a monotonically increasing sequence number to the timestamp.
Expand All @@ -17,8 +21,6 @@
which load the library at the same time will also generate the same nonce, because the sequence number is
initialised to 0.) Which shouldn't be a problem, because their entropy inputs will be different with high
probability.
* In C, a byte need not be 8 bits wide. However, this implementation uses the term 'byte' to refer to an 8-bit number.
Hence, fixed-width integer types are used liberally.
* The `hd` argument of any function (where applicable) denotes the HDRBG object to use.
* If it is `NULL`, the internal HDRBG object is used.
* For instance, `hdrbg_rand(NULL)` and `hdrbg_rand(foo)` are both valid invocations of `hdrbg_rand`—the former
Expand Down
9 changes: 9 additions & 0 deletions lib/sha256.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include <inttypes.h>
#include <limits.h>
#include <stddef.h>
#include <string.h>

#include "extras.h"
#include "sha.h"

#ifdef TFPF_HASH_DRBG_OPENSSL_FOUND
#include <openssl/sha.h>
#endif

#define ROTR32(x, n) ((x) >> (n) | (x) << (32 - (n)))

// Hash initialiser.
Expand Down Expand Up @@ -46,6 +51,10 @@ sha256_bytes[32];
uint8_t *
sha256(uint8_t const *m_bytes_, size_t m_length_, uint8_t *h_bytes)
{
#if defined TFPF_HASH_DRBG_OPENSSL_FOUND && CHAR_BIT == 8
return SHA256(m_bytes_, m_length_, h_bytes);
#endif

// Initialise the hash.
uint32_t h_words[8];
memcpy(h_words, sha256_init, sizeof sha256_init);
Expand Down

0 comments on commit 31817eb

Please sign in to comment.