From: miha-q <> Date: Sun, 20 Aug 2023 23:39:55 +0000 (-0400) Subject: Sun Aug 20 07:39:55 PM EDT 2023 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=71cf3937a2545e02affa9a3e8960068847fdac80;p=CryptoFoleo.git Sun Aug 20 07:39:55 PM EDT 2023 --- diff --git a/bin/CryptoFoleo.h b/bin/CryptoFoleo.h index 7c7e0e3..3fc192e 100644 --- a/bin/CryptoFoleo.h +++ b/bin/CryptoFoleo.h @@ -1,3 +1,5 @@ +#ifndef __HEADERS__ +#define __HEADERS__ #include #include @@ -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 diff --git a/bin/CryptoFoleo.hi b/bin/CryptoFoleo.hi index 8591056..0a36e7e 100644 Binary files a/bin/CryptoFoleo.hi and b/bin/CryptoFoleo.hi differ diff --git a/bin/CryptoFoleo.hs b/bin/CryptoFoleo.hs index 8adc21e..2916678 100644 --- a/bin/CryptoFoleo.hs +++ b/bin/CryptoFoleo.hs @@ -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 diff --git a/bin/CryptoFoleo.o b/bin/CryptoFoleo.o index f5f42ef..467ce57 100644 Binary files a/bin/CryptoFoleo.o and b/bin/CryptoFoleo.o differ diff --git a/bin/Main.hi b/bin/Main.hi index 6d0bb4c..d19125b 100644 Binary files a/bin/Main.hi and b/bin/Main.hi differ diff --git a/bin/Main.hs b/bin/Main.hs index efebe6a..ac2a74a 100644 --- a/bin/Main.hs +++ b/bin/Main.hs @@ -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 diff --git a/bin/Main.o b/bin/Main.o index f852d22..57210d7 100644 Binary files a/bin/Main.o and b/bin/Main.o differ diff --git a/bin/libCryptoFoleo.so b/bin/libCryptoFoleo.so index 658bc46..6ced20b 100755 Binary files a/bin/libCryptoFoleo.so and b/bin/libCryptoFoleo.so differ diff --git a/bin/main b/bin/main index 39ec996..34ab573 100755 Binary files a/bin/main and b/bin/main differ diff --git a/src/headers.h b/src/headers.h index 7c7e0e3..3fc192e 100644 --- a/src/headers.h +++ b/src/headers.h @@ -1,3 +1,5 @@ +#ifndef __HEADERS__ +#define __HEADERS__ #include #include @@ -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 diff --git a/src/headers.hs b/src/headers.hs index 8adc21e..2916678 100644 --- a/src/headers.hs +++ b/src/headers.hs @@ -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 diff --git a/src/rand.c b/src/rand.c index f380a66..628da77 100644 --- a/src/rand.c +++ b/src/rand.c @@ -5,16 +5,14 @@ #include #include #include +#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