]> foleosoft.com Git - CryptoFoleo.git/commitdiff
Thu Aug 10 01:55:17 PM EDT 2023
authormiha-q <>
Thu, 10 Aug 2023 17:55:17 +0000 (13:55 -0400)
committermiha-q <>
Thu, 10 Aug 2023 17:55:17 +0000 (13:55 -0400)
bin/CryptoFoleo.h
bin/CryptoFoleo.hi
bin/CryptoFoleo.hs
bin/CryptoFoleo.o
bin/Main.hi
bin/Main.hs
bin/Main.o
bin/libCryptoFoleo.so
bin/main

index da94f6d5faf7a03c0acf662a05831f929be7052e..0b955b6736a366c7b3f2f2b85d6355703affb059 100644 (file)
@@ -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
index bfdcf98c210233d158a109bd2806eabf8a526f11..79305a37f5334811f5f6d59f6d96df94f7e2765a 100644 (file)
Binary files a/bin/CryptoFoleo.hi and b/bin/CryptoFoleo.hi differ
index 7459baebfcc2e0a740df277f68531d44fd940bc1..85264edcf0c9a76aef5af88c1b0fa0042cb0e182 100644 (file)
@@ -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
index 53f1f2b431a1e4668ee62b7e299e53bcb2b4d332..cdeac85b18f6898239c024b52299bd1eb760d913 100644 (file)
Binary files a/bin/CryptoFoleo.o and b/bin/CryptoFoleo.o differ
index 160f954aa4c4569bbb236dbb623e98b359638470..ae5c363c8169b66dc5b199b687df053fca22d44c 100644 (file)
Binary files a/bin/Main.hi and b/bin/Main.hi differ
index 67d63d5319c009b7513a60b6dcd7d21eb433198d..95ef9df2db55213f5bbe6d628a6fb68a6ecb09d4 100644 (file)
@@ -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"
+
 
index f2d221f4b4a09c16b02cc795805bb4b4d284f2ab..5aed240672c9ddf4261c667575996dad7ad2cb90 100644 (file)
Binary files a/bin/Main.o and b/bin/Main.o differ
index a40af30c168b0817911d8afc70515a2e11a04fa8..ae87cb7797df6e41f23ff416b79819ac10055b8e 100755 (executable)
Binary files a/bin/libCryptoFoleo.so and b/bin/libCryptoFoleo.so differ
index b57ff40b5fc655ab57e4b33f373e6de7ec2cf424..ea361b382db51036c194ac719e2ff140b683347d 100755 (executable)
Binary files a/bin/main and b/bin/main differ