From: server Date: Mon, 14 Aug 2023 20:33:46 +0000 (-0400) Subject: Mon Aug 14 04:33:46 PM EDT 2023 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=8bc5049e0979677b6a2b6cc1ff1d0fd67e678404;p=CryptoFoleo.git Mon Aug 14 04:33:46 PM EDT 2023 --- diff --git a/bin/CryptoFoleo.h b/bin/CryptoFoleo.h index c7a6885..5829a63 100644 --- a/bin/CryptoFoleo.h +++ b/bin/CryptoFoleo.h @@ -44,4 +44,4 @@ 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(const char*, const char*); +uint8_t foleo_auth(uint8_t*, uint8_t*); diff --git a/bin/CryptoFoleo.hi b/bin/CryptoFoleo.hi index fa24614..8591056 100644 Binary files a/bin/CryptoFoleo.hi and b/bin/CryptoFoleo.hi differ diff --git a/bin/CryptoFoleo.hs b/bin/CryptoFoleo.hs index f5c6bc1..41753f3 100644 --- a/bin/CryptoFoleo.hs +++ b/bin/CryptoFoleo.hs @@ -1,5 +1,7 @@ module CryptoFoleo ( + auth, + dhke, chacha20, poly1305, @@ -40,6 +42,9 @@ 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) @@ -177,6 +182,13 @@ 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 @@ -311,7 +323,7 @@ byteToHexString b = do 14 -> "e" 15 -> "f" _ -> "0" - ++ + ++ case (mod b 16) of 0 -> "0" 1 -> "1" @@ -330,7 +342,7 @@ byteToHexString b = do 14 -> "e" 15 -> "f" _ -> "0" - + byteStringToByteList :: ByteString -> [Word8] byteStringToByteList b = BS.unpack b @@ -349,4 +361,4 @@ fromNumberFixedSize n p = do pd :: [Word8] -> [Word8] pd x = if (Prelude.length x) < p then pd ([0] ++ x) else x rt = pd (fn n []) - BS.pack $ if (Prelude.length rt) > p then Prelude.drop ((Prelude.length rt) - p) rt else rt \ No newline at end of file + BS.pack $ if (Prelude.length rt) > p then Prelude.drop ((Prelude.length rt) - p) rt else rt diff --git a/bin/CryptoFoleo.o b/bin/CryptoFoleo.o index d62821a..f5f42ef 100644 Binary files a/bin/CryptoFoleo.o and b/bin/CryptoFoleo.o differ diff --git a/bin/Main.hi b/bin/Main.hi index fe2978d..6d0bb4c 100644 Binary files a/bin/Main.hi and b/bin/Main.hi differ diff --git a/bin/Main.hs b/bin/Main.hs index 9d4a3e5..efebe6a 100644 --- a/bin/Main.hs +++ b/bin/Main.hs @@ -8,81 +8,5 @@ import qualified Numeric as N main :: IO() main = do - let s :: BS.ByteString - s = BS.pack - [ - 0x01, 0x03, 0x80, 0x8a, 0xfb, 0x0d, 0xb2, 0xfd, - 0x4a, 0xbf, 0xf6, 0xaf, 0x41, 0x49, 0xf5, 0x1b - ] - r :: BS.ByteString - r = BS.pack - [ - 0x85, 0xd6, 0xbe, 0x78, 0x57, 0x55, 0x6d, 0x33, - 0x7f, 0x44, 0x52, 0xfe, 0x42, 0xd5, 0x06, 0xa8 - ] - m :: BS.ByteString - m = BS.pack - [ - 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72, - 0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x46, 0x6f, - 0x72, 0x75, 0x6d, 0x20, 0x52, 0x65, 0x73, 0x65, - 0x61, 0x72, 0x63, 0x68, 0x20, 0x47, 0x72, 0x6f, - 0x75, 0x70 - ] - - p <- poly1305 r m - print $ byteStringToHexString p - - --- --generate key files if the don't exist --- existsPub <- doesFileExist "pub.key" --- existsPrv <- doesFileExist "prv.key" --- unless (existsPub && existsPrv) $ do --- rsa_keygen 2048 $ \pub prv -> do --- spub <- rsa_export pub --- sprv <- rsa_export prv --- writeFile "pub.key" spub --- writeFile "prv.key" sprv --- --- --load the key files if they do exist --- spub <- readFile "pub.key" --- sprv <- readFile "prv.key" --- rsa_import spub $ \pubKey -> do --- rsa_import sprv $ \prvKey -> do --- let pt = C8.pack "one two three it's photosynthesis you see" --- ct <- rsa_encrypt pubKey rsa_padding_oaep pt --- dt <- rsa_decrypt prvKey rsa_padding_oaep ct --- putStrLn $ "Plaintext:\n\t" ++ (show pt) --- putStrLn $ "Ciphertext:\n\t" ++ (show ct) --- putStrLn $ "Decrypted message:\n\t" ++ (show dt) --- --- --- g <- sha256 (C8.pack "abc") --- print g --- --- prv1 <- dhke (BS.empty, BS.empty) --- putStrLn "prv1=" --- print prv1 --- --- prv2 <- dhke (BS.empty, BS.empty) --- putStrLn "prv2=" --- print prv2 --- --- pub1 <- dhke (prv1, BS.empty) --- putStrLn "pub1=" --- print pub1 --- --- pub2 <- dhke (prv2, BS.empty) --- putStrLn "pub2=" --- print pub2 --- --- key1 <- dhke(prv1, pub2) --- putStrLn "key1=" --- print key1 --- --- key2 <- dhke(prv2, pub1) --- putStrLn "key2=" --- print key2 --- --- - + q <- auth "server" "-" + print q diff --git a/bin/Main.o b/bin/Main.o index 7a0ecde..f852d22 100644 Binary files a/bin/Main.o and b/bin/Main.o differ diff --git a/bin/main b/bin/main index a5af5bd..39ec996 100755 Binary files a/bin/main and b/bin/main differ diff --git a/src/auth.c b/src/auth.c index ea0086b..f2a28ef 100644 --- a/src/auth.c +++ b/src/auth.c @@ -9,7 +9,8 @@ #include #include -uint8_t foleo_auth(const char *username, const char* password) { +uint8_t foleo_auth(uint8_t* username, uint8_t* password) +{ struct spwd spw, *result; char *buf; size_t bufsize; diff --git a/src/headers.h b/src/headers.h index c7a6885..5829a63 100644 --- a/src/headers.h +++ b/src/headers.h @@ -44,4 +44,4 @@ 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(const char*, const char*); +uint8_t foleo_auth(uint8_t*, uint8_t*); diff --git a/src/headers.hs b/src/headers.hs index 723422e..41753f3 100644 --- a/src/headers.hs +++ b/src/headers.hs @@ -43,7 +43,7 @@ import qualified Foreign.Marshal.Utils as MU import qualified Data.ByteString.Char8 as C8 foreign import ccall unsafe "foleo_auth" - c_auth :: Ptr(CChar) -> Ptr(CChar) -> IO(CUChar) + c_auth :: Ptr(CUChar) -> Ptr(CUChar) -> IO(CUChar) foreign import ccall unsafe "foleo_rsa_keysize" c_rsa_keysize :: IO (CUShort) @@ -184,10 +184,10 @@ rsa_free blob = useAsCString blob $ \ptr -> c_rsa_free (castPtr ptr) auth :: String -> String -> IO (Bool) auth u p = do - useAsCString u $ \uPtr -> do - useAsCString p $ \pPtr -> do - r <- c_auth uPtr pPtr - if (fromIntegral c) == 1 then return True else return False + 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 @@ -323,7 +323,7 @@ byteToHexString b = do 14 -> "e" 15 -> "f" _ -> "0" - ++ + ++ case (mod b 16) of 0 -> "0" 1 -> "1" @@ -342,7 +342,7 @@ byteToHexString b = do 14 -> "e" 15 -> "f" _ -> "0" - + byteStringToByteList :: ByteString -> [Word8] byteStringToByteList b = BS.unpack b