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*);
module CryptoFoleo
(
- auth,
-
dhke,
chacha20,
poly1305,
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)
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
#include "prigen.c"
#include "rsa.c"
#include "sha256.c"
-#include "auth.c"
-
+#include "rand.c"
+++ /dev/null
-#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
#include <stdlib.h>
#include <stdint.h>
#include <gmp.h>
+#include "rand.c"
/*
dhke(private, 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)
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*);
module CryptoFoleo
(
- auth,
-
dhke,
chacha20,
poly1305,
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)
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
#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);
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
} 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
--- /dev/null
+#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
#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)
{
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++)
{
//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
//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