From: miha-q <> Date: Thu, 10 Aug 2023 17:55:17 +0000 (-0400) Subject: Thu Aug 10 01:55:17 PM EDT 2023 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=c50bdcb3a4d6dda66c7dd9d6600ea8edffc8f837;p=CryptoFoleo.git Thu Aug 10 01:55:17 PM EDT 2023 --- diff --git a/bin/CryptoFoleo.h b/bin/CryptoFoleo.h index da94f6d..0b955b6 100644 --- a/bin/CryptoFoleo.h +++ b/bin/CryptoFoleo.h @@ -23,8 +23,8 @@ uint8_t* foleo_rsa_export(rsakey_t*); void foleo_rsa_free(rsakey_t*); void foleo_rsa_keygen(uint16_t, rsakey_t*, rsakey_t*); uint16_t foleo_rsa_keysize(); -uint16_t foleo_rsa_size(rsakey_t, uint8_t); -uint8_t* foleo_rsa_encrypt(rsakey_t, uint8_t, uint8_t*, uint16_t); -uint8_t* foleo_rsa_decrypt(rsakey_t, uint8_t, uint8_t*, uint16_t*); +uint16_t foleo_rsa_size(rsakey_t*, uint8_t); +uint8_t* foleo_rsa_encrypt(rsakey_t*, uint8_t, uint8_t*, uint16_t); +uint8_t* foleo_rsa_decrypt(rsakey_t*, uint8_t, uint8_t*, uint16_t*); uint8_t* foleo_sha256(uint8_t*, uint32_t); #define FOLEO_SHA256 foleo_sha256, 32, 64 diff --git a/bin/CryptoFoleo.hi b/bin/CryptoFoleo.hi index bfdcf98..79305a3 100644 Binary files a/bin/CryptoFoleo.hi and b/bin/CryptoFoleo.hi differ diff --git a/bin/CryptoFoleo.hs b/bin/CryptoFoleo.hs index 7459bae..85264ed 100644 --- a/bin/CryptoFoleo.hs +++ b/bin/CryptoFoleo.hs @@ -2,14 +2,21 @@ module CryptoFoleo ( rsa_keygen, rsa_import, - rsa_export + rsa_export, + rsa_encrypt, + + rsa_padding_none, + rsa_padding_encryption, + rsa_padding_signature, + rsa_padding_oaep, + rsa_padding_pss ) where import System.IO as IO import Foreign import Foreign.C.Types import Foreign.C.String -import Data.ByteString +import Data.ByteString as BS import Data.ByteString.Internal import Data.Word import Data.ByteString.Unsafe @@ -21,6 +28,9 @@ import qualified Data.ByteString.Char8 as C8 foreign import ccall unsafe "foleo_rsa_keysize" c_rsa_keysize :: IO (CUShort) +foreign import ccall unsafe "foleo_rsa_modsize" + c_rsa_modsize :: Ptr () -> IO (CUShort) + foreign import ccall unsafe "foleo_rsa_keygen" c_rsa_keygen :: CUShort -> Ptr () -> Ptr () -> IO () @@ -33,39 +43,64 @@ foreign import ccall unsafe "foleo_rsa_import" foreign import ccall unsafe "foleo_rsa_free" c_rsa_free :: Ptr () -> IO () +foreign import ccall unsafe "foleo_rsa_encrypt" + c_rsa_encrypt :: Ptr () -> CUChar -> Ptr (CUChar) -> 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 = 99 +rsa_padding_encryption = 1 +rsa_padding_signature = 2 + +rsa_padding_oaep :: Int +rsa_padding_oaep = 3 + +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 + 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 + rsa_keygen :: Word16 -> (ByteString -> ByteString -> IO ()) -> IO () rsa_keygen n fn = do - sRsaSize <- c_rsa_keysize - let rsaSize :: Int - rsaSize = fromIntegral sRsaSize - allocaBytes rsaSize $ \pubKeyPtr -> - allocaBytes rsaSize $ \prvKeyPtr -> 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 rsaSize (\ptr -> MU.copyBytes ptr (castPtr pubKeyPtr) rsaSize) - prvKey <- BI.create rsaSize (\ptr -> MU.copyBytes ptr (castPtr prvKeyPtr) rsaSize) + pubKey <- BI.create keySize (\ptr -> MU.copyBytes ptr (castPtr pubKeyPtr) keySize) + prvKey <- BI.create keySize (\ptr -> MU.copyBytes ptr (castPtr prvKeyPtr) keySize) fn pubKey prvKey rsa_free pubKey rsa_free prvKey rsa_import :: String -> (ByteString -> IO ()) -> IO () rsa_import n fn = do - sRsaSize <- c_rsa_keysize - let rsaSize :: Int - rsaSize = fromIntegral sRsaSize + sKeySize <- c_rsa_keysize + let keySize :: Int + keySize = fromIntegral sKeySize unsafeUseAsCStringLen (C8.pack n) $ \(bsPtr, len) -> do - allocaBytes rsaSize $ \keyPtr -> do - c_rsa_import (castPtr bsPtr) keyPtr - key <- BI.create rsaSize(\ptr -> MU.copyBytes ptr (castPtr keyPtr) rsaSize) + allocaBytes keySize $ \keyPtr -> do + c_rsa_import keyPtr (castPtr bsPtr) + key <- BI.create keySize (\ptr -> MU.copyBytes ptr (castPtr keyPtr) keySize) fn key rsa_free key rsa_export :: ByteString -> IO(String) -rsa_export blob = do - useAsCString blob $ \blobPtr -> do - cStrPtr <- c_rsa_export (castPtr blobPtr) +rsa_export bsKey = do + useAsCString bsKey $ \keyPtr -> do + cStrPtr <- c_rsa_export (castPtr keyPtr) cStr <- peekCString (castPtr cStrPtr) c_free cStrPtr return cStr diff --git a/bin/CryptoFoleo.o b/bin/CryptoFoleo.o index 53f1f2b..cdeac85 100644 Binary files a/bin/CryptoFoleo.o and b/bin/CryptoFoleo.o differ diff --git a/bin/Main.hi b/bin/Main.hi index 160f954..ae5c363 100644 Binary files a/bin/Main.hi and b/bin/Main.hi differ diff --git a/bin/Main.hs b/bin/Main.hs index 67d63d5..95ef9df 100644 --- a/bin/Main.hs +++ b/bin/Main.hs @@ -2,9 +2,12 @@ import System.IO as IO import System.Directory import Control.Monad import CryptoFoleo +import qualified Data.ByteString as BS +import qualified Data.ByteString.Char8 as C8 main :: IO() main = do + --generate key files if the ydon't exist existsPub <- doesFileExist "pub.key" existsPrv <- doesFileExist "prv.key" unless (existsPub && existsPrv) $ do @@ -14,9 +17,15 @@ main = do 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 - sspub <- rsa_export pubKey - putStrLn sspub - putStrLn spub + 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" + diff --git a/bin/Main.o b/bin/Main.o index f2d221f..5aed240 100644 Binary files a/bin/Main.o and b/bin/Main.o differ diff --git a/bin/libCryptoFoleo.so b/bin/libCryptoFoleo.so index a40af30..ae87cb7 100755 Binary files a/bin/libCryptoFoleo.so and b/bin/libCryptoFoleo.so differ diff --git a/bin/main b/bin/main index b57ff40..ea361b3 100755 Binary files a/bin/main and b/bin/main differ