]> foleosoft.com Git - CryptoFoleo.git/commitdiff
Wed Aug 16 10:55:14 PM EDT 2023
authormiha-q <>
Thu, 17 Aug 2023 02:55:14 +0000 (22:55 -0400)
committermiha-q <>
Thu, 17 Aug 2023 02:55:14 +0000 (22:55 -0400)
bin/CryptoFoleo.h
bin/CryptoFoleo.hs
bin/libCryptoFoleo.so
src/all.c
src/auth.c [deleted file]
src/dhke.c
src/headers.h
src/headers.hs
src/prigen.c
src/rand.c [new file with mode: 0644]
src/rsa.c

index 5829a6320c70dc6de8b71e581a8857cdcd17f3cf..7c7e0e3548981f792446d6977d4c7b186fa6437d 100644 (file)
@@ -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*);
index 41753f36dbff4d4314f36ed1f8767471e82d3986..9a3ba7a8dbca57d32334b26801c7ab2b402fcae6 100644 (file)
@@ -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
index b5387187cbd296f8ef1efb2213afe8a3af57d3ff..b885eda57b779e8852bbdff89734bff3f844473e 100755 (executable)
Binary files a/bin/libCryptoFoleo.so and b/bin/libCryptoFoleo.so differ
index f961fd234d9d9e9d66e9ec4ce0f9c7254313e245..7085facd828c9eadc8944d93fb0c5bd73e00498a 100644 (file)
--- 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 (file)
index f2a28ef..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-#ifndef __AUTHENTICATE__
-#define __AUTHENTICATE__
-#include <crypt.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <pwd.h>
-#include <shadow.h>
-#include <string.h>
-#include <stdint.h>
-#include <unistd.h>
-
-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
index 488bbba41ea7822510eb0cfeb23f6eeed098e2cc..4e939e77d539b5f82e33a52df729002f43130d87 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <gmp.h>
+#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)
index 5829a6320c70dc6de8b71e581a8857cdcd17f3cf..7c7e0e3548981f792446d6977d4c7b186fa6437d 100644 (file)
@@ -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*);
index 41753f36dbff4d4314f36ed1f8767471e82d3986..9a3ba7a8dbca57d32334b26801c7ab2b402fcae6 100644 (file)
@@ -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
index a78187ee46f2dd92519a89099be4d145eb24493c..7b2d84db1bfc51aec449b6b5bc2bd4ca0c461744 100644 (file)
@@ -4,14 +4,15 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <gmp.h>
+#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 (file)
index 0000000..fe463bb
--- /dev/null
@@ -0,0 +1,123 @@
+#ifndef __RAND__
+#define __RAND__
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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
index 7774c56c3f6105c26eab84711f2c4221caabcd47..b0354d3ecda36b995291128f3b24a5ea6f6bd3c3 100644 (file)
--- a/src/rsa.c
+++ b/src/rsa.c
@@ -6,6 +6,7 @@
 #include <gmp.h>
 #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