From: server Date: Thu, 10 Aug 2023 20:30:19 +0000 (-0400) Subject: Thu Aug 10 04:30:19 PM EDT 2023 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=eda083caa13d7b4d52fed3c99fb5a8c53e2e8e29;p=CryptoFoleo.git Thu Aug 10 04:30:19 PM EDT 2023 --- diff --git a/bin/CryptoFoleo.hi b/bin/CryptoFoleo.hi index 79305a3..19308d7 100644 Binary files a/bin/CryptoFoleo.hi and b/bin/CryptoFoleo.hi differ diff --git a/bin/CryptoFoleo.hs b/bin/CryptoFoleo.hs index 85264ed..f6014c0 100644 --- a/bin/CryptoFoleo.hs +++ b/bin/CryptoFoleo.hs @@ -4,6 +4,7 @@ module CryptoFoleo rsa_import, rsa_export, rsa_encrypt, + rsa_decrypt, rsa_padding_none, rsa_padding_encryption, @@ -46,30 +47,51 @@ foreign import ccall unsafe "foleo_rsa_free" foreign import ccall unsafe "foleo_rsa_encrypt" c_rsa_encrypt :: Ptr () -> CUChar -> Ptr (CUChar) -> CUShort -> IO (Ptr (CUChar)) +foreign import ccall unsafe "foleo_rsa_decrypt" + c_rsa_decrypt :: Ptr () -> CUChar -> Ptr (CUChar) -> Ptr (CUShort) -> IO (Ptr (CUChar)) + --uint8_t* foleo_rsa_encrypt(rsakey_t* key, uint8_t padding, uint8_t* buffer, uint16_t bufferSize) foreign import ccall unsafe "free" c_free :: Ptr a -> IO () - + +rsa_padding_none :: Int rsa_padding_none = 99 + +rsa_padding_encryption :: Int rsa_padding_encryption = 1 + +rsa_padding_signature :: Int rsa_padding_signature = 2 rsa_padding_oaep :: Int rsa_padding_oaep = 3 +rsa_padding_pss :: Int rsa_padding_pss = 4 -rsa_encrypt :: ByteString -> Int -> ByteString-> IO (ByteString) -rsa_encrypt bsKey padType bsMsg = do - useAsCString bsKey $ \keyPtr -> do - useAsCString bsMsg $ \msgPtr -> do +rsa_encrypt :: ByteString -> Int -> ByteString-> IO (ByteString) +rsa_encrypt keyBS padType ptBS = do + useAsCString keyBS $ \keyPtr -> do + useAsCString ptBS $ \ptPtr -> do + sModSize <- c_rsa_modsize (castPtr keyPtr) + ctPtr <- c_rsa_encrypt (castPtr keyPtr) (fromIntegral padType) (castPtr ptPtr) (fromIntegral (BS.length ptBS)) + ctBS <- BI.create (fromIntegral sModSize) (\ptr -> MU.copyBytes ptr (castPtr ctPtr) (fromIntegral sModSize)) + c_free ctPtr + return ctBS + +rsa_decrypt :: ByteString -> Int -> ByteString -> IO (ByteString) +rsa_decrypt keyBS padType ctBS = do + useAsCString keyBS $ \keyPtr -> do + useAsCString ctBS $ \ctPtr -> do sModSize <- c_rsa_modsize (castPtr keyPtr) - ptrCT <- c_rsa_encrypt (castPtr keyPtr) (fromIntegral padType) (castPtr msgPtr) (fromIntegral (BS.length bsMsg)) - bsCT <- BI.create (fromIntegral sModSize) (\ptr -> MU.copyBytes ptr (castPtr ptrCT) (fromIntegral sModSize)) - c_free ptrCT - return bsCT + allocaBytes 2 $ \sizePtr -> do + ptPtr <- c_rsa_decrypt (castPtr keyPtr) (fromIntegral padType) (castPtr ctPtr) (castPtr sizePtr) + ptSize <- peek sizePtr + ptBS <- BI.create ptSize (\ptr -> MU.copyBytes ptr (castPtr ptPtr) ptSize) + c_free ptPtr + return ptBS rsa_keygen :: Word16 -> (ByteString -> ByteString -> IO ()) -> IO () rsa_keygen n fn = do @@ -98,8 +120,8 @@ rsa_import n fn = do rsa_free key rsa_export :: ByteString -> IO(String) -rsa_export bsKey = do - useAsCString bsKey $ \keyPtr -> do +rsa_export keyBS = do + useAsCString keyBS $ \keyPtr -> do cStrPtr <- c_rsa_export (castPtr keyPtr) cStr <- peekCString (castPtr cStrPtr) c_free cStrPtr diff --git a/bin/CryptoFoleo.o b/bin/CryptoFoleo.o index cdeac85..ce7906b 100644 Binary files a/bin/CryptoFoleo.o and b/bin/CryptoFoleo.o differ diff --git a/bin/Main.hi b/bin/Main.hi index ae5c363..764fd94 100644 Binary files a/bin/Main.hi and b/bin/Main.hi differ diff --git a/bin/Main.hs b/bin/Main.hs index 95ef9df..893bd81 100644 --- a/bin/Main.hs +++ b/bin/Main.hs @@ -7,7 +7,7 @@ import qualified Data.ByteString.Char8 as C8 main :: IO() main = do - --generate key files if the ydon't exist + --generate key files if the don't exist existsPub <- doesFileExist "pub.key" existsPrv <- doesFileExist "prv.key" unless (existsPub && existsPrv) $ do @@ -21,11 +21,12 @@ main = do spub <- readFile "pub.key" sprv <- readFile "prv.key" rsa_import spub $ \pubKey -> do - let msg = C8.pack "my message" - ct <- rsa_encrypt pubKey rsa_padding_oaep msg - print msg - print ct - --rsa_import sprv $ \prvKey -> do - --putStrLn "hi" + 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) diff --git a/bin/Main.o b/bin/Main.o index 5aed240..669f3de 100644 Binary files a/bin/Main.o and b/bin/Main.o differ diff --git a/bin/main b/bin/main index ea361b3..dc273fa 100755 Binary files a/bin/main and b/bin/main differ