From: server Date: Thu, 10 Aug 2023 17:06:33 +0000 (-0400) Subject: Thu Aug 10 01:06:33 PM EDT 2023 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=a3bb761ecc4b34ea91a4e6a43279e2e98a27ebc4;p=CryptoFoleo.git Thu Aug 10 01:06:33 PM EDT 2023 --- diff --git a/bin/CryptoFoleo.hi b/bin/CryptoFoleo.hi new file mode 100644 index 0000000..bfdcf98 Binary files /dev/null and b/bin/CryptoFoleo.hi differ diff --git a/bin/CryptoFoleo.hs b/bin/CryptoFoleo.hs new file mode 100644 index 0000000..7459bae --- /dev/null +++ b/bin/CryptoFoleo.hs @@ -0,0 +1,83 @@ +module CryptoFoleo + ( + rsa_keygen, + rsa_import, + rsa_export + ) +where +import System.IO as IO +import Foreign +import Foreign.C.Types +import Foreign.C.String +import Data.ByteString +import Data.ByteString.Internal +import Data.Word +import Data.ByteString.Unsafe +import Control.DeepSeq +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_rsa_keysize" + c_rsa_keysize :: IO (CUShort) + +foreign import ccall unsafe "foleo_rsa_keygen" + c_rsa_keygen :: CUShort -> Ptr () -> Ptr () -> IO () + +foreign import ccall unsafe "foleo_rsa_export" + c_rsa_export :: Ptr () -> IO (Ptr (CUChar)) + +foreign import ccall unsafe "foleo_rsa_import" + c_rsa_import :: Ptr (CUChar) -> Ptr () -> IO () + +foreign import ccall unsafe "foleo_rsa_free" + c_rsa_free :: Ptr () -> IO () + +foreign import ccall unsafe "free" + c_free :: Ptr a -> IO () + +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 + 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) + 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 + 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) + fn key + rsa_free key + +rsa_export :: ByteString -> IO(String) +rsa_export blob = do + useAsCString blob $ \blobPtr -> do + cStrPtr <- c_rsa_export (castPtr blobPtr) + cStr <- peekCString (castPtr cStrPtr) + c_free cStrPtr + return cStr + +rsa_free :: ByteString -> IO() +rsa_free blob = useAsCString blob $ \ptr -> c_rsa_free (castPtr ptr) + +--main :: IO() +--main = do +-- rsa_keygen 2048 $ \pub prv -> do +-- spub <- rsa_export pub +-- IO.putStr spub + +byteStringToInteger :: ByteString -> Integer +byteStringToInteger = foldl' (\acc byte -> acc * 256 + fromIntegral byte) 0 diff --git a/bin/CryptoFoleo.o b/bin/CryptoFoleo.o new file mode 100644 index 0000000..53f1f2b Binary files /dev/null and b/bin/CryptoFoleo.o differ diff --git a/bin/Main.hi b/bin/Main.hi index aee8149..160f954 100644 Binary files a/bin/Main.hi and b/bin/Main.hi differ diff --git a/bin/Main.hs b/bin/Main.hs index f4236ba..67d63d5 100644 --- a/bin/Main.hs +++ b/bin/Main.hs @@ -1,60 +1,22 @@ import System.IO as IO -import Foreign -import Foreign.C.Types -import Foreign.C.String -import Data.ByteString -import Data.ByteString.Internal -import Data.Word -import Data.ByteString.Unsafe -import Control.DeepSeq -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_rsa_keysize" - c_rsa_keysize :: IO(CUShort) - -foreign import ccall unsafe "foleo_rsa_keygen" - c_rsa_keygen :: CUShort -> Ptr() -> Ptr() -> IO() - -foreign import ccall unsafe "foleo_rsa_export" - c_rsa_export :: Ptr () -> IO (Ptr CUChar) - -foreign import ccall unsafe "foleo_rsa_free" - c_rsa_free :: Ptr () -> IO () -foreign import ccall unsafe "free" - c_free :: Ptr a -> IO () - -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 - 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) - fn pubKey prvKey - rsa_free pubKey - rsa_free prvKey - -rsa_export :: ByteString -> IO(String) -rsa_export blob = do - useAsCString blob $ \blobPtr -> do - cStrPtr <- c_rsa_export (castPtr blobPtr) - cStr <- peekCString (castPtr cStrPtr) - c_free cStrPtr - return cStr - -rsa_free :: ByteString -> IO() -rsa_free blob = useAsCString blob $ \ptr -> c_rsa_free (castPtr ptr) +import System.Directory +import Control.Monad +import CryptoFoleo main :: IO() main = do - rsa_keygen 2048 $ \pub prv -> do - spub <- rsa_export pub - IO.putStr spub + 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 + + spub <- readFile "pub.key" + rsa_import spub $ \pubKey -> do + sspub <- rsa_export pubKey + putStrLn sspub + putStrLn spub -byteStringToInteger :: ByteString -> Integer -byteStringToInteger = foldl' (\acc byte -> acc * 256 + fromIntegral byte) 0 diff --git a/bin/Main.o b/bin/Main.o index 931635f..f2d221f 100644 Binary files a/bin/Main.o and b/bin/Main.o differ diff --git a/bin/main b/bin/main index 895a2ef..b57ff40 100755 Binary files a/bin/main and b/bin/main differ diff --git a/bin/prv.key b/bin/prv.key new file mode 100644 index 0000000..bffcf44 --- /dev/null +++ b/bin/prv.key @@ -0,0 +1,28 @@ ++-------[RSA Private Key]--------+ +|#.[/[@$%#,\~%=@<[<(^,(&$:~'}<}``| +|%$>(;&&%'($;')`#e~"},<<)@),^}%"<| +|.%(?`]._*?@!e/@[*:)]/ex*},>~[s/(| +|#;}#`*{=";[\,(=>.${x,:>!x(\"=]=&| +|"%){"&x#>~}e]{^~\s'`'}>"| +|$^]~%!`^~".\@<<{,@#<&;x")}:]{'^~| +|`!@).{%.{}>e!/s:>`_es^?=(/}<]_*=| +|]{@(s>.&\!`"$x@!{]>==s`.`$@{@`/<| +|^??^/}^*['_<:@(^?]/:`~[[={*[_@#]| +|::]}*@!)?}/`]=`/e#\';#(}[!,&:"`>!{'x!*./!,x;_%{^<]:<)$""^| +|;x[[^&!?e?=__']s~.,`,$&!?`:)/{\}!&!\{/| +|`#:`)x%)"e`?,[s.)e=>>^`(;,$}'(<:| +|%."{(=_,%^!ex,$,_x>*`%^,ex;]x_$;| +|!.!.#.x]e/}*~;<,s/{~?e`@}!~$)e_.| +|;,s),s@.{$ex{@=;];:{/(}$^.%(&;%\x!{}<,x!{| +|!<={'/}x?^'.s&;$(*~*")ses`},!:;*| +|e;s&~.:`;xx&_,}[};x@e=){]s&%'[_:| +|%!!\_%{(!`(;^/_:<^?@,=@%)>,/$)@,_| +|[.~!<\!<&.`%}^=s/?@_x=":'{~[_={)| +|\*$~~?.#};"`\[)[\[~;s | ++--------------------------------+ diff --git a/bin/pub.key b/bin/pub.key new file mode 100644 index 0000000..6b608b5 --- /dev/null +++ b/bin/pub.key @@ -0,0 +1,15 @@ ++--------[RSA Public Key]--------+ +|(^:/# $`,^_=^`s\e#^e,>.,`,$&!?`:| +|)/{\}!&!\{/`#:`)x%)"e`?,[s.)e=>>| +|^`(;,$}'(<:%."{(=_,%^!ex,$,_x>*`| +|%^,ex;]x_$;!.!.#.x]e/}*~;<,s/{~?| +|e`@}!~$)e_.;,s),s@.{$ex{@=;];:{/(}$^.%(&;| +|%\x!{}<,x!{!<={'/}x?^'.s&;$(*~*"| +|)ses`},!:;*e;s&~.:`;xx&_,}[};x@e| +|=){]s&%'[_:%!!\_%{(!`(;^/_:<^?@,| +|=@%)>,/$)@,_[.~!<\!<&.`%}^=s/?@_x| +|=":'{~[_={)\*$~~?.#};"`\[)[\[~;s| ++--------------------------------+