From: miha-q <> Date: Thu, 17 Aug 2023 02:55:14 +0000 (-0400) Subject: Wed Aug 16 10:55:14 PM EDT 2023 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=4a7145f57b080d8f59d24732ea8e0fb6f52fe205;p=CryptoFoleo.git Wed Aug 16 10:55:14 PM EDT 2023 --- diff --git a/bin/CryptoFoleo.h b/bin/CryptoFoleo.h index 5829a63..7c7e0e3 100644 --- a/bin/CryptoFoleo.h +++ b/bin/CryptoFoleo.h @@ -44,4 +44,3 @@ uint8_t* foleo_hmac_hkdf(uint8_t, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32 uint8_t* foleo_hmac_prf(uint8_t, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t); uint8_t foleo_hash_size(uint8_t); -uint8_t foleo_auth(uint8_t*, uint8_t*); diff --git a/bin/CryptoFoleo.hs b/bin/CryptoFoleo.hs index 41753f3..9a3ba7a 100644 --- a/bin/CryptoFoleo.hs +++ b/bin/CryptoFoleo.hs @@ -1,7 +1,5 @@ module CryptoFoleo ( - auth, - dhke, chacha20, poly1305, @@ -42,9 +40,6 @@ import qualified Data.ByteString.Internal as BI import qualified Foreign.Marshal.Utils as MU import qualified Data.ByteString.Char8 as C8 -foreign import ccall unsafe "foleo_auth" - c_auth :: Ptr(CUChar) -> Ptr(CUChar) -> IO(CUChar) - foreign import ccall unsafe "foleo_rsa_keysize" c_rsa_keysize :: IO (CUShort) @@ -182,13 +177,6 @@ rsa_export keyBS = do rsa_free :: ByteString -> IO() rsa_free blob = useAsCString blob $ \ptr -> c_rsa_free (castPtr ptr) -auth :: String -> String -> IO (Bool) -auth u p = do - useAsCString (C8.pack u) $ \uPtr -> do - useAsCString (C8.pack p) $ \pPtr -> do - r <- c_auth (castPtr uPtr) (castPtr pPtr) - if (fromIntegral r) == 1 then return True else return False - dhke :: [ByteString] -> IO (ByteString) dhke v = do c_modSize <- c_dhke_modsize diff --git a/bin/libCryptoFoleo.so b/bin/libCryptoFoleo.so index b538718..b885eda 100755 Binary files a/bin/libCryptoFoleo.so and b/bin/libCryptoFoleo.so differ diff --git a/src/all.c b/src/all.c index f961fd2..7085fac 100644 --- a/src/all.c +++ b/src/all.c @@ -6,5 +6,4 @@ #include "prigen.c" #include "rsa.c" #include "sha256.c" -#include "auth.c" - +#include "rand.c" diff --git a/src/auth.c b/src/auth.c deleted file mode 100644 index f2a28ef..0000000 --- a/src/auth.c +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef __AUTHENTICATE__ -#define __AUTHENTICATE__ -#include -#include -#include -#include -#include -#include -#include -#include - -uint8_t foleo_auth(uint8_t* username, uint8_t* password) -{ - struct spwd spw, *result; - char *buf; - size_t bufsize; - - bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); - if (bufsize == -1) { - bufsize = 16384; // use a default size if sysconf returns indeterminate size - } - - buf = malloc(bufsize); - getspnam_r(username, &spw, buf, bufsize, &result); - if (result == NULL) - { - free(buf); - return 0; - } - - int status = strcmp(crypt(password, spw.sp_pwdp), spw.sp_pwdp) == 0; - free(buf); - return status; -} -#endif diff --git a/src/dhke.c b/src/dhke.c index 488bbba..4e939e7 100644 --- a/src/dhke.c +++ b/src/dhke.c @@ -4,6 +4,7 @@ #include #include #include +#include "rand.c" /* dhke(private, public) @@ -167,9 +168,9 @@ uint8_t* foleo_dhke(uint8_t* private, uint8_t* public) if (private == NULL && public == NULL) { - FILE* f = fopen(DEVICE, "r"); - fread(out, 1, 512, f); - fclose(f); + void* f = rand_begin(); + rand_get(f, out, 512); + rand_end(f); out[0] = out[0] & 0b01111111; } else if (private != NULL && public == NULL) diff --git a/src/headers.h b/src/headers.h index 5829a63..7c7e0e3 100644 --- a/src/headers.h +++ b/src/headers.h @@ -44,4 +44,3 @@ uint8_t* foleo_hmac_hkdf(uint8_t, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32 uint8_t* foleo_hmac_prf(uint8_t, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t); uint8_t foleo_hash_size(uint8_t); -uint8_t foleo_auth(uint8_t*, uint8_t*); diff --git a/src/headers.hs b/src/headers.hs index 41753f3..9a3ba7a 100644 --- a/src/headers.hs +++ b/src/headers.hs @@ -1,7 +1,5 @@ module CryptoFoleo ( - auth, - dhke, chacha20, poly1305, @@ -42,9 +40,6 @@ import qualified Data.ByteString.Internal as BI import qualified Foreign.Marshal.Utils as MU import qualified Data.ByteString.Char8 as C8 -foreign import ccall unsafe "foleo_auth" - c_auth :: Ptr(CUChar) -> Ptr(CUChar) -> IO(CUChar) - foreign import ccall unsafe "foleo_rsa_keysize" c_rsa_keysize :: IO (CUShort) @@ -182,13 +177,6 @@ rsa_export keyBS = do rsa_free :: ByteString -> IO() rsa_free blob = useAsCString blob $ \ptr -> c_rsa_free (castPtr ptr) -auth :: String -> String -> IO (Bool) -auth u p = do - useAsCString (C8.pack u) $ \uPtr -> do - useAsCString (C8.pack p) $ \pPtr -> do - r <- c_auth (castPtr uPtr) (castPtr pPtr) - if (fromIntegral r) == 1 then return True else return False - dhke :: [ByteString] -> IO (ByteString) dhke v = do c_modSize <- c_dhke_modsize diff --git a/src/prigen.c b/src/prigen.c index a78187e..7b2d84d 100644 --- a/src/prigen.c +++ b/src/prigen.c @@ -4,14 +4,15 @@ #include #include #include +#include "rand.c" -static void FOLEO_PRIGEN_GetRandom(mpz_t n, uint16_t bytes, FILE *f) +static void FOLEO_PRIGEN_GetRandom(mpz_t n, uint16_t bytes, void* f) { mpz_set_ui(n, 0); for (uint16_t i = 0; i < bytes; i++) { mpz_mul_2exp(n, n, 8); - uint8_t c = fgetc(f); + uint8_t c = rand_getc(f); if (i == 0) c |= 0b10000000; if (i == bytes - 1) c |= 1; mpz_add_ui(n, n, c); @@ -103,19 +104,19 @@ static uint8_t FOLEO_PRIGEN_PrimeTest(mpz_t n) static void FOLEO_PRIGEN_GeneratePrime(mpz_t n, int bytes) { - FILE *f = fopen(DEVICE, "r"); + void* f = rand_begin(); do { FOLEO_PRIGEN_GetRandom(n, bytes, f); } while (!FOLEO_PRIGEN_PrimeTest(n)); - fclose(f); + rand_end(f); } /* Generate prime of X bytes */ static uint8_t* foleo_prigen(uint16_t bytes) { uint8_t* buffer = malloc(bytes); - FILE *f = fopen(DEVICE, "r"); + void* f = rand_begin(); mpz_t n, t; mpz_init(n); do @@ -124,7 +125,7 @@ static uint8_t* foleo_prigen(uint16_t bytes) } while (!FOLEO_PRIGEN_PrimeTest(n)); mpz_export(buffer, NULL, 1, 1, 0, 0, n); mpz_clear(n); - fclose(f); + rand_end(f); return buffer; } #endif diff --git a/src/rand.c b/src/rand.c new file mode 100644 index 0000000..fe463bb --- /dev/null +++ b/src/rand.c @@ -0,0 +1,123 @@ +#ifndef __RAND__ +#define __RAND__ + +#include +#include +#include +#include + +#define RAND_MODE_DEVR 1 +#define RAND_MODE_DEV 2 +#define RAND_MODE_X86 3 +uint8_t RAND_MODE = RAND_MODE_X86; +uint8_t* RAND_INFO = NULL; + +static void* rand_begin() +{ + if (RAND_MODE == RAND_MODE_DEVR) + { + FILE* f = fopen("/dev/random", "r"); + if (!f) + { + fprintf(stderr, "rand_begin(): RAND_MODE invalid.\n"); + return NULL; + } + return f; + } + else if (RAND_MODE == RAND_MODE_DEV) + { + FILE* f = fopen(RAND_INFO, "r"); + if (!f) + { + fprintf(stderr, "rand_begin(): RAND_MODE invalid.\n"); + return NULL; + } + return f; + } + else + { + return NULL; + } +} + +static void rand_get(void* context, uint8_t* buf, size_t bytes) +{ + if (RAND_MODE == RAND_MODE_DEVR || RAND_MODE == RAND_MODE_DEV) + { + if (context == NULL) + { + memset(buf, 0, bytes); + } + else + { + FILE* f = (FILE*)context; + fread(buf, 1, bytes, f); + } + } + else if (RAND_MODE == RAND_MODE_X86) + { + uint64_t r; + uint8_t avail = 0; + for (size_t i = 0; i < bytes; i++) + { + if (avail == 0) + { + __asm__ volatile ("1:;rdseed %0;;jnc 1b;" : "=r" (r)); + avail = 4; + } + buf[i] = r & 0xFF; + r >>= 8; + avail -= 1; + } + } +} + +static uint8_t rand_getc(void* context) +{ + if (RAND_MODE == RAND_MODE_DEVR || RAND_MODE == RAND_MODE_DEV) + { + if (context == NULL) + { + return 0; + } + else + { + return fgetc((FILE*)context); + } + } + else if (RAND_MODE == RAND_MODE_X86) + { + uint64_t r; + __asm__ volatile ("1:;rdseed %0;;jnc 1b;" : "=r" (r)); + return r & 0xFF; + } +} + +static void rand_end(void* context) +{ + if (RAND_MODE == RAND_MODE_DEVR || RAND_MODE == RAND_MODE_DEV) + { + fclose((FILE*)context); + } +} + +void rand_mode(uint8_t mode, uint8_t* info) +{ + if (mode == RAND_MODE_DEVR || mode == RAND_MODE_DEV || mode == RAND_MODE_X86) + { + if (mode == RAND_MODE_DEV) + { + if (info != NULL) + { + RAND_MODE = mode; + RAND_INFO = info; + } + } + else + { + RAND_MODE = mode; + } + } +} + +#endif \ No newline at end of file diff --git a/src/rsa.c b/src/rsa.c index 7774c56..b0354d3 100644 --- a/src/rsa.c +++ b/src/rsa.c @@ -6,6 +6,7 @@ #include #include "sha256.c" #include "prigen.c" +#include "rand.c" static void foleo_rsa_store(mpz_t n, uint8_t* b, uint32_t s) { @@ -422,8 +423,9 @@ static uint8_t* FOLEO_RSA_Pad(uint16_t size, uint8_t* buffer, uint16_t bufferSiz uint16_t psLen = size - (3 + bufferSizeInBytes); uint8_t* ps = malloc(psLen); - FILE* f = fopen(DEVICE, "r"); - fread(ps, 1, psLen, f); + void* f = rand_begin(); + rand_get(f, ps, psLen); + rand_end(f); for (uint16_t j = 0; j < psLen; j++) { @@ -564,8 +566,8 @@ static uint8_t* FOLEO_RSA_PadOAEP(uint16_t size, uint8_t* buffer, uint16_t mLen) //Build Seed uint8_t Seed[hLen]; - FILE* f = fopen(DEVICE, "r"); - fread(Seed, 1, hLen, f); + void* f = rand_begin(); + rand_get(f, Seed, hLen); fclose(f); //Build DB @@ -716,4 +718,6 @@ uint16_t foleo_rsa_keysize() { return sizeof(rsakey_t); } //get the size of the RSA modulus in bytes uint16_t foleo_rsa_modsize(rsakey_t* key) { return (key->bitWidth / 8) + (key->bitWidth % 8 != 0 ? 1 : 0); } +void rand_mode(uint8_t, uint8_t*); + #endif