]> foleosoft.com Git - CryptoFoleo.git/commitdiff
Sun Aug 20 07:39:55 PM EDT 2023
authormiha-q <>
Sun, 20 Aug 2023 23:39:55 +0000 (19:39 -0400)
committermiha-q <>
Sun, 20 Aug 2023 23:39:55 +0000 (19:39 -0400)
12 files changed:
bin/CryptoFoleo.h
bin/CryptoFoleo.hi
bin/CryptoFoleo.hs
bin/CryptoFoleo.o
bin/Main.hi
bin/Main.hs
bin/Main.o
bin/libCryptoFoleo.so
bin/main
src/headers.h
src/headers.hs
src/rand.c

index 7c7e0e3548981f792446d6977d4c7b186fa6437d..3fc192efdb396c0f56a2c8325b0408e9727db209 100644 (file)
@@ -1,3 +1,5 @@
+#ifndef __HEADERS__
+#define __HEADERS__
 #include <stdint.h>
 #include <gmp.h>
 
@@ -12,6 +14,9 @@ uint8_t* foleo_poly1305(uint8_t[32], uint8_t*, size_t);
 #define FOLEO_RSA_PADDING_SIGNATURE 2
 #define FOLEO_RSA_PADDING_OAEP 3
 #define FOLEO_RSA_PADDING_PSS 4
+#define FOLEO_RAND_MODE_DEVR 1
+#define FOLEO_RAND_MODE_DEV 2
+#define FOLEO_RAND_MODE_X86 3
 typedef struct
 {
     mpz_t n, k;
@@ -44,3 +49,6 @@ 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);
+
+void foleo_rand_mode(uint8_t, uint8_t*);
+#endif
\ No newline at end of file
index 85910568c220edfad1945db2c5ab0a1360297cbd..0a36e7eca1b4275af0fc2d3067e7937a9809e51c 100644 (file)
Binary files a/bin/CryptoFoleo.hi and b/bin/CryptoFoleo.hi differ
index 8adc21ee383681d65767b1b7b78f5bc3603b65aa..29166786b576789fba5ae23d191c28f330df5c91 100644 (file)
@@ -23,6 +23,11 @@ module CryptoFoleo
         rsa_padding_oaep,
         rsa_padding_pss,
 
+        rand_mode,
+        rand_mode_dev,
+        rand_mode_devr,
+        rand_mode_x86,
+
         byteStringToHexString,
         fromNumberFixedSize
     )
@@ -94,6 +99,9 @@ foreign import ccall unsafe "foleo_poly1305"
 foreign import ccall unsafe "foleo_chacha20_poly1305"
     c_chacha20_poly1305 :: Ptr (CUChar) -> Ptr (CUChar) -> Ptr (CUChar) -> CSize -> IO (Ptr (CUChar))
 
+foreign import ccall unsafe "foleo_rand_mode"
+    c_foleo_rand_mode :: CUChar -> Ptr (CUChar) -> IO ()
+
 foreign import ccall unsafe "free"
     c_free :: Ptr a -> IO ()
 
@@ -115,6 +123,15 @@ rsa_padding_pss = 4
 hash_sha256 :: Int
 hash_sha256 = 1
 
+rand_mode_devr :: Int
+rand_mode_devr = 1
+
+rand_mode_dev :: Int
+rand_mode_dev = 2
+
+rand_mode_x86 :: Int
+rand_mode_x86 = 3
+
 rsa_encrypt :: ByteString -> Int -> ByteString-> IO (ByteString)
 rsa_encrypt keyBS padType ptBS = do
     useAsCString keyBS $ \keyPtr -> do
@@ -142,17 +159,19 @@ rsa_decrypt keyBS padType ctBS = do
 
 rsa_keygen :: Word16 -> (ByteString -> ByteString -> IO ()) -> IO ()
 rsa_keygen n fn = do
-    sKeySize <- c_rsa_keysize
-    let keySize :: Int
-        keySize = fromIntegral sKeySize
-    allocaBytes keySize $ \pubKeyPtr ->
-        allocaBytes keySize $ \prvKeyPtr -> do
-            c_rsa_keygen (fromIntegral n) pubKeyPtr prvKeyPtr
-            pubKey <- BI.create keySize (\ptr -> MU.copyBytes ptr (castPtr pubKeyPtr) keySize)
-            prvKey <- BI.create keySize (\ptr -> MU.copyBytes ptr (castPtr prvKeyPtr) keySize)
-            fn prvKey pubKey
-            rsa_free pubKey
-            rsa_free prvKey
+    if n >= 64 then do
+        sKeySize <- c_rsa_keysize
+        let keySize :: Int
+            keySize = fromIntegral sKeySize
+        allocaBytes keySize $ \pubKeyPtr ->
+            allocaBytes keySize $ \prvKeyPtr -> do
+                c_rsa_keygen (fromIntegral n) pubKeyPtr prvKeyPtr
+                pubKey <- BI.create keySize (\ptr -> MU.copyBytes ptr (castPtr pubKeyPtr) keySize)
+                prvKey <- BI.create keySize (\ptr -> MU.copyBytes ptr (castPtr prvKeyPtr) keySize)
+                fn prvKey pubKey
+                rsa_free pubKey
+                rsa_free prvKey
+    else return ()
 
 rsa_import :: String -> (ByteString -> IO ()) -> IO ()
 rsa_import n fn = do
@@ -291,6 +310,11 @@ chacha20_poly1305 key nonce ctext = do
                     c_free rPtr
                     return r
 
+rand_mode :: Int -> String -> IO ()
+rand_mode mode info = do
+    useAsCString (C8.pack info) $ \infoPtr -> do
+        c_foleo_rand_mode (fromIntegral mode) (castPtr infoPtr)
+
 byteToHexString :: Word8 -> String
 byteToHexString b = do
     case (div b 16) of
index f5f42efb17fd05107f8890695c70a59fa7960d90..467ce57e231b396cfbd7712f53c75122a66e3381 100644 (file)
Binary files a/bin/CryptoFoleo.o and b/bin/CryptoFoleo.o differ
index 6d0bb4c6d382d39e4b03ec5cfac172823f9444d8..d19125bcf4ada91c88401c0f9cc435128600a642 100644 (file)
Binary files a/bin/Main.hi and b/bin/Main.hi differ
index efebe6a60d40cb33091e34f11753a0d59104ee9b..ac2a74a6f8e73b08969b3eade31799956f75f2be 100644 (file)
@@ -8,5 +8,7 @@ import qualified Numeric as N
 
 main :: IO()
 main = do
-    q <- auth "server" "-"
-    print q
+    rand_mode rand_mode_dev "/dev/null"
+    rsa_keygen 1024 $ \vk bk -> do
+        vks <- rsa_export vk
+        putStrLn vks
\ No newline at end of file
index f852d22da1d3e68479bd2dbf72fc051dab44e8f8..57210d71b7e3a906018de09c9b24d15b6a053911 100644 (file)
Binary files a/bin/Main.o and b/bin/Main.o differ
index 658bc466b9c3facc1252eeb87dad68519608c77b..6ced20bbe12dcd8349440733ee232f60f586a360 100755 (executable)
Binary files a/bin/libCryptoFoleo.so and b/bin/libCryptoFoleo.so differ
index 39ec996c12c9a95bf2b604f6e1a881057f9933ca..34ab573af5ddedccbf1651e5fb9655420f7f5be7 100755 (executable)
Binary files a/bin/main and b/bin/main differ
index 7c7e0e3548981f792446d6977d4c7b186fa6437d..3fc192efdb396c0f56a2c8325b0408e9727db209 100644 (file)
@@ -1,3 +1,5 @@
+#ifndef __HEADERS__
+#define __HEADERS__
 #include <stdint.h>
 #include <gmp.h>
 
@@ -12,6 +14,9 @@ uint8_t* foleo_poly1305(uint8_t[32], uint8_t*, size_t);
 #define FOLEO_RSA_PADDING_SIGNATURE 2
 #define FOLEO_RSA_PADDING_OAEP 3
 #define FOLEO_RSA_PADDING_PSS 4
+#define FOLEO_RAND_MODE_DEVR 1
+#define FOLEO_RAND_MODE_DEV 2
+#define FOLEO_RAND_MODE_X86 3
 typedef struct
 {
     mpz_t n, k;
@@ -44,3 +49,6 @@ 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);
+
+void foleo_rand_mode(uint8_t, uint8_t*);
+#endif
\ No newline at end of file
index 8adc21ee383681d65767b1b7b78f5bc3603b65aa..29166786b576789fba5ae23d191c28f330df5c91 100644 (file)
@@ -23,6 +23,11 @@ module CryptoFoleo
         rsa_padding_oaep,
         rsa_padding_pss,
 
+        rand_mode,
+        rand_mode_dev,
+        rand_mode_devr,
+        rand_mode_x86,
+
         byteStringToHexString,
         fromNumberFixedSize
     )
@@ -94,6 +99,9 @@ foreign import ccall unsafe "foleo_poly1305"
 foreign import ccall unsafe "foleo_chacha20_poly1305"
     c_chacha20_poly1305 :: Ptr (CUChar) -> Ptr (CUChar) -> Ptr (CUChar) -> CSize -> IO (Ptr (CUChar))
 
+foreign import ccall unsafe "foleo_rand_mode"
+    c_foleo_rand_mode :: CUChar -> Ptr (CUChar) -> IO ()
+
 foreign import ccall unsafe "free"
     c_free :: Ptr a -> IO ()
 
@@ -115,6 +123,15 @@ rsa_padding_pss = 4
 hash_sha256 :: Int
 hash_sha256 = 1
 
+rand_mode_devr :: Int
+rand_mode_devr = 1
+
+rand_mode_dev :: Int
+rand_mode_dev = 2
+
+rand_mode_x86 :: Int
+rand_mode_x86 = 3
+
 rsa_encrypt :: ByteString -> Int -> ByteString-> IO (ByteString)
 rsa_encrypt keyBS padType ptBS = do
     useAsCString keyBS $ \keyPtr -> do
@@ -142,17 +159,19 @@ rsa_decrypt keyBS padType ctBS = do
 
 rsa_keygen :: Word16 -> (ByteString -> ByteString -> IO ()) -> IO ()
 rsa_keygen n fn = do
-    sKeySize <- c_rsa_keysize
-    let keySize :: Int
-        keySize = fromIntegral sKeySize
-    allocaBytes keySize $ \pubKeyPtr ->
-        allocaBytes keySize $ \prvKeyPtr -> do
-            c_rsa_keygen (fromIntegral n) pubKeyPtr prvKeyPtr
-            pubKey <- BI.create keySize (\ptr -> MU.copyBytes ptr (castPtr pubKeyPtr) keySize)
-            prvKey <- BI.create keySize (\ptr -> MU.copyBytes ptr (castPtr prvKeyPtr) keySize)
-            fn prvKey pubKey
-            rsa_free pubKey
-            rsa_free prvKey
+    if n >= 64 then do
+        sKeySize <- c_rsa_keysize
+        let keySize :: Int
+            keySize = fromIntegral sKeySize
+        allocaBytes keySize $ \pubKeyPtr ->
+            allocaBytes keySize $ \prvKeyPtr -> do
+                c_rsa_keygen (fromIntegral n) pubKeyPtr prvKeyPtr
+                pubKey <- BI.create keySize (\ptr -> MU.copyBytes ptr (castPtr pubKeyPtr) keySize)
+                prvKey <- BI.create keySize (\ptr -> MU.copyBytes ptr (castPtr prvKeyPtr) keySize)
+                fn prvKey pubKey
+                rsa_free pubKey
+                rsa_free prvKey
+    else return ()
 
 rsa_import :: String -> (ByteString -> IO ()) -> IO ()
 rsa_import n fn = do
@@ -291,6 +310,11 @@ chacha20_poly1305 key nonce ctext = do
                     c_free rPtr
                     return r
 
+rand_mode :: Int -> String -> IO ()
+rand_mode mode info = do
+    useAsCString (C8.pack info) $ \infoPtr -> do
+        c_foleo_rand_mode (fromIntegral mode) (castPtr infoPtr)
+
 byteToHexString :: Word8 -> String
 byteToHexString b = do
     case (div b 16) of
index f380a6624deae32435244a7288b599aeeee6e4cb..628da77ba774809e4d9f00dda370e98308b67d3e 100644 (file)
@@ -5,16 +5,14 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include "headers.h"
 
-#define RAND_MODE_DEVR 1
-#define RAND_MODE_DEV 2
-#define RAND_MODE_X86 3
-uint8_t RAND_MODE = RAND_MODE_DEVR;
-uint8_t* RAND_INFO = NULL;
+uint8_t RAND_MODE = FOLEO_RAND_MODE_DEVR;
+uint8_t RAND_INFO[1024];
 
 static void* rand_begin()
 {
-    if (RAND_MODE == RAND_MODE_DEVR)
+    if (RAND_MODE == FOLEO_RAND_MODE_DEVR)
     {
         FILE* f = fopen("/dev/random", "r");
         if (!f)
@@ -24,7 +22,7 @@ static void* rand_begin()
         }
         return f;
     }
-    else if (RAND_MODE == RAND_MODE_DEV)
+    else if (RAND_MODE == FOLEO_RAND_MODE_DEV)
     {
         FILE* f = fopen(RAND_INFO, "r");
         if (!f)
@@ -42,7 +40,7 @@ static void* rand_begin()
 
 static void rand_get(void* context, uint8_t* buf, size_t bytes)
 {
-    if (RAND_MODE == RAND_MODE_DEVR || RAND_MODE == RAND_MODE_DEV)
+    if (RAND_MODE == FOLEO_RAND_MODE_DEVR || RAND_MODE == FOLEO_RAND_MODE_DEV)
     {
         if (context == NULL)
         {
@@ -54,7 +52,7 @@ static void rand_get(void* context, uint8_t* buf, size_t bytes)
             fread(buf, 1, bytes, f);
         }
     }
-    else if (RAND_MODE == RAND_MODE_X86)
+    else if (RAND_MODE == FOLEO_RAND_MODE_X86)
     {
         uint64_t r;
         uint8_t avail = 0;
@@ -74,7 +72,7 @@ static void rand_get(void* context, uint8_t* buf, size_t bytes)
 
 static uint8_t rand_getc(void* context)
 {
-    if (RAND_MODE == RAND_MODE_DEVR || RAND_MODE == RAND_MODE_DEV)
+    if (RAND_MODE == FOLEO_RAND_MODE_DEVR || RAND_MODE == FOLEO_RAND_MODE_DEV)
     {
         if (context == NULL)
         {
@@ -85,7 +83,7 @@ static uint8_t rand_getc(void* context)
             return fgetc((FILE*)context);
         }
     }
-    else if (RAND_MODE == RAND_MODE_X86)
+    else if (RAND_MODE == FOLEO_RAND_MODE_X86)
     {
         uint64_t r;
         __asm__ volatile ("1:;rdseed %0;;jnc 1b;" : "=r" (r));
@@ -95,22 +93,22 @@ static uint8_t rand_getc(void* context)
 
 static void rand_end(void* context)
 {
-    if (RAND_MODE == RAND_MODE_DEVR || RAND_MODE == RAND_MODE_DEV)
+    if (RAND_MODE == FOLEO_RAND_MODE_DEVR || RAND_MODE == FOLEO_RAND_MODE_DEV)
     {
         fclose((FILE*)context);
     }
 }
 
-void rand_mode(uint8_t mode, uint8_t* info)
+void foleo_rand_mode(uint8_t mode, uint8_t* info)
 {
-    if (mode == RAND_MODE_DEVR || mode == RAND_MODE_DEV || mode == RAND_MODE_X86)
+    if (mode == FOLEO_RAND_MODE_DEVR || mode == FOLEO_RAND_MODE_DEV || mode == FOLEO_RAND_MODE_X86)
     {
-        if (mode == RAND_MODE_DEV)
+        if (mode == FOLEO_RAND_MODE_DEV)
         {
-            if (info != NULL)
+            if (info != NULL && strlen(info) < 1024 - 1)
             {
                 RAND_MODE = mode;
-                RAND_INFO = info;
+                strcpy(RAND_INFO, info);
             }
         }
         else