]> foleosoft.com Git - CryptoFoleo.git/commitdiff
Thu Aug 10 09:09:06 AM EDT 2023
authormiha-q <>
Thu, 10 Aug 2023 13:09:06 +0000 (09:09 -0400)
committermiha-q <>
Thu, 10 Aug 2023 13:09:06 +0000 (09:09 -0400)
12 files changed:
bin/CryptoFoleo.h
bin/Main.hi
bin/Main.hs
bin/Main.o
bin/libCryptoFoleo.so
bin/main
bin/test [deleted file]
build [new file with mode: 0755]
build.sh
src/headers.h
src/rsa.c
tests/test.c

index a32b714ec08f0afb3113cbc5b561614f4bfb069a..da94f6d5faf7a03c0acf662a05831f929be7052e 100644 (file)
@@ -15,13 +15,15 @@ static uint8_t* foleo_prigen(uint16_t);
 typedef struct
 {
     mpz_t n, k;
+    uint8_t* label;
     uint16_t bitWidth;
 } rsakey_t;
-rsakey_t foleo_rsa_public(uint8_t*, uint16_t, uint8_t*, uint16_t, uint8_t*, uint16_t);
-rsakey_t foleo_rsa_private(uint8_t*, uint16_t, uint8_t*, uint16_t, uint8_t*, uint16_t);
-rsakey_t foleo_rsa_import(uint8_t*, uint16_t, uint8_t*, uint16_t);
-uint8_t* foleo_rsa_export(rsakey_t, uint8_t, uint16_t*);
-void foleo_rsa_free(rsakey_t);
+void foleo_rsa_import(rsakey_t*, uint8_t*);
+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*);
 uint8_t* foleo_sha256(uint8_t*, uint32_t);
index 0d85271937b7c1a68cbabca8333f1d8d59179e45..aee81498a6b08f5117656b8c5ccb92f47dceb369 100644 (file)
Binary files a/bin/Main.hi and b/bin/Main.hi differ
index bae2276b9f00b322c70589d6971b185f7a5e0f03..f4236ba70262e78277244130f8b84866c417cf99 100644 (file)
@@ -1,37 +1,60 @@
-import System.IO
+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 "prigen"
-    c_prigen :: CUShort -> IO (Ptr CChar)
+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()
 
-prigen :: Word16 -> IO ByteString
-prigen lengthInBytes = do
-    ptr <- c_prigen (fromIntegral lengthInBytes)
-    q <- unsafePackCStringLen (ptr, fromIntegral lengthInBytes)
-    let out = copy q
-    out `deepseq` free ptr
-    return out
+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)
 
 main :: IO()
 main = do
-    g <- prigen 256
-    print $ byteStringToInteger g
+    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
-
---```haskell
---import Data.ByteString (ByteString)
---import qualified Data.ByteString as BS
---
---byteStringToInteger :: ByteString -> Integer
---byteStringToInteger = BS.foldl' (\acc byte -> acc * 256 + fromIntegral byte) 0
---
\ No newline at end of file
index 528aa67de25a8f27dbfa7f7b596383ed54aecd3e..931635fa1abeed3b11da8f0551eae0768d074bdd 100644 (file)
Binary files a/bin/Main.o and b/bin/Main.o differ
index 6b353e5008bbfd89c16c59c3e5cb38d58802dde2..a40af30c168b0817911d8afc70515a2e11a04fa8 100755 (executable)
Binary files a/bin/libCryptoFoleo.so and b/bin/libCryptoFoleo.so differ
index 8afaf663f9ead9c84e3056ff19fda466c3256e18..895a2efb348d230f615fd68ee8d08cf6611f6cde 100755 (executable)
Binary files a/bin/main and b/bin/main differ
diff --git a/bin/test b/bin/test
deleted file mode 100755 (executable)
index 88de3ff..0000000
Binary files a/bin/test and /dev/null differ
diff --git a/build b/build
new file mode 100755 (executable)
index 0000000..0b2e461
--- /dev/null
+++ b/build
@@ -0,0 +1,9 @@
+if [ "$1" == "test" ]
+then
+       gcc tests/test.c -o bin/test -lgmp -DDEVICE='"/dev/random"'
+       bin/test
+       rm bin/test
+else
+       gcc -shared -fPIC src/all.c -o bin/libCryptoFoleo.so -lgmp -DDEVICE='"/dev/random"'
+       cp src/headers.h bin/CryptoFoleo.h
+fi
index 0b2e46184890b6ecb65946b130143c3a9abd7adf..54195d16b79aed478f1c13ecf884ea74774b7a2f 100644 (file)
--- a/build.sh
+++ b/build.sh
@@ -1,7 +1,12 @@
 if [ "$1" == "test" ]
 then
        gcc tests/test.c -o bin/test -lgmp -DDEVICE='"/dev/random"'
-       bin/test
+       if [ "$2" == "hard" ]
+       then
+               valgrind bin/test
+       else
+               bin/test
+       fi
        rm bin/test
 else
        gcc -shared -fPIC src/all.c -o bin/libCryptoFoleo.so -lgmp -DDEVICE='"/dev/random"'
index dc6dd2fdc6a3ab91a2726ff059ffef8b7abb01ee..da94f6d5faf7a03c0acf662a05831f929be7052e 100644 (file)
@@ -18,11 +18,12 @@ typedef struct
     uint8_t* label;
     uint16_t bitWidth;
 } rsakey_t;
-rsakey_t foleo_rsa_public(uint8_t*, uint16_t, uint8_t*, uint16_t, uint8_t*, uint16_t);
-rsakey_t foleo_rsa_private(uint8_t*, uint16_t, uint8_t*, uint16_t, uint8_t*, uint16_t);
-//rsakey_t foleo_rsa_import(uint8_t*, uint16_t, uint8_t*, uint16_t);
-//uint8_t* foleo_rsa_export(rsakey_t, uint8_t, uint16_t*);
-void foleo_rsa_free(rsakey_t);
+void foleo_rsa_import(rsakey_t*, uint8_t*);
+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*);
 uint8_t* foleo_sha256(uint8_t*, uint32_t);
index 6c5b8ca83f3b8568850474139965b139627d1c5d..11c2e1c02b37830908ad25ce7f684dd5e11c754d 100644 (file)
--- a/src/rsa.c
+++ b/src/rsa.c
@@ -32,66 +32,6 @@ static void foleo_rsa_load(mpz_t n, uint8_t* b, uint32_t s)
     }
 }
 
-//Keys and their sizes in terms of BYTES
-rsakey_t foleo_rsa_public(uint8_t* p, uint16_t pS, uint8_t* q, uint16_t qS, uint8_t* e, uint16_t eS)
-{
-    mpz_t np, nq, ne;
-    mpz_init(np);
-    mpz_init(nq);
-    mpz_init(ne);
-    mpz_set_ui(np, 0);
-    mpz_set_ui(nq, 0);
-    mpz_set_ui(ne, 0);
-
-    foleo_rsa_load(np, p, pS);
-    foleo_rsa_load(nq, q, qS);
-    foleo_rsa_load(ne, q, qS);
-
-    rsakey_t ret;
-    ret.bitWidth = (pS + qS) * 8;
-    mpz_init(ret.n);
-    mpz_init(ret.k);
-    mpz_mul(ret.n, np, nq);
-    mpz_set(ret.k, ne);
-
-    mpz_clear(np);
-    mpz_clear(nq);
-    mpz_clear(ne);
-    return ret;
-}
-
-rsakey_t foleo_rsa_private(uint8_t* p, uint16_t pS, uint8_t* q, uint16_t qS, uint8_t* e, uint16_t eS)
-{
-    mpz_t np, nq, ne;
-    mpz_init(np);
-    mpz_init(nq);
-    mpz_init(ne);
-    mpz_set_ui(np, 0);
-    mpz_set_ui(nq, 0);
-    mpz_set_ui(ne, 0);
-
-    foleo_rsa_load(np, p, pS);
-    foleo_rsa_load(nq, q, qS);
-    foleo_rsa_load(ne, q, qS);
-
-    rsakey_t ret;
-    ret.bitWidth = (pS + qS) * 8;
-    mpz_init(ret.n);
-    mpz_init(ret.k);
-    mpz_mul(ret.n, np, nq);
-    mpz_set(ret.k, ne);
-
-    mpz_sub_ui(np, np, 1);
-    mpz_sub_ui(nq, nq, 1);
-    mpz_mul(np, np, nq);
-    mpz_invert(ret.k, ret.k, np);
-
-    mpz_clear(np);
-    mpz_clear(nq);
-    mpz_clear(ne);
-    return ret;
-}
-
 static uint8_t foleo_rsa_prettymap(uint8_t in, uint8_t dir)
 {
     if (dir)
@@ -173,14 +113,13 @@ static uint8_t foleo_rsa_prettymap(uint8_t in, uint8_t dir)
     }
 }
 
-static rsakey_t foleo_rsa_import(uint8_t* buf)
+void foleo_rsa_import(rsakey_t* k, uint8_t* buf)
 {
-    rsakey_t k;
-    mpz_init(k.k);
-    mpz_init(k.n);
-    mpz_set_ui(k.k, 0);
-    mpz_set_ui(k.n, 0);
-    k.label = malloc(0);
+    mpz_init(k->k);
+    mpz_init(k->n);
+    mpz_set_ui(k->k, 0);
+    mpz_set_ui(k->n, 0);
+    k->label = malloc(0);
     uint32_t lblS = 0;
     uint32_t i;
     uint8_t state = 0;
@@ -197,13 +136,13 @@ static rsakey_t foleo_rsa_import(uint8_t* buf)
             if (buf[i] == ']')
             {
                 state = 2;
-                k.label = realloc(k.label, lblS + 1);
-                k.label[lblS] = 0;
+                k->label = realloc(k->label, lblS + 1);
+                k->label[lblS] = 0;
             }
             else
             {
-                k.label = realloc(k.label, lblS + 1);
-                k.label[lblS++] = buf[i];
+                k->label = realloc(k->label, lblS + 1);
+                k->label[lblS++] = buf[i];
             }
         }
         else if (state == 2)
@@ -211,8 +150,8 @@ static rsakey_t foleo_rsa_import(uint8_t* buf)
             if (buf[i] == ' ') state = 3;
             else if (foleo_rsa_prettymap(buf[i], 1) != 0xFF)
             {
-                mpz_mul_ui(k.k, k.k, 32);
-                mpz_add_ui(k.k, k.k, foleo_rsa_prettymap(buf[i], 1));
+                mpz_mul_ui(k->k, k->k, 32);
+                mpz_add_ui(k->k, k->k, foleo_rsa_prettymap(buf[i], 1));
                 bitWidth += 1;
             }
         }
@@ -221,27 +160,26 @@ static rsakey_t foleo_rsa_import(uint8_t* buf)
             if (buf[i] == ' ') break;
             else if (foleo_rsa_prettymap(buf[i], 1) != 0xFF)
             {
-                mpz_mul_ui(k.n, k.n, 32);
-                mpz_add_ui(k.n, k.n, foleo_rsa_prettymap(buf[i], 1));
+                mpz_mul_ui(k->n, k->n, 32);
+                mpz_add_ui(k->n, k->n, foleo_rsa_prettymap(buf[i], 1));
             }
         }
     }
-    k.bitWidth = mpz_sizeinbase(k.n, 2);
-    if (k.bitWidth % 8 != 0)
+    k->bitWidth = mpz_sizeinbase(k->n, 2);
+    if (k->bitWidth % 8 != 0)
     {
-        k.bitWidth = (k.bitWidth + 1) % 8 == 0 ? k.bitWidth + 1 : k.bitWidth - 1;
-        while (k.bitWidth % 8 != 0) k.bitWidth++;
+        k->bitWidth = (k->bitWidth + 1) % 8 == 0 ? k->bitWidth + 1 : k->bitWidth - 1;
+        while (k->bitWidth % 8 != 0) k->bitWidth++;
     }
-    return k;
 }
 
-static uint8_t* foleo_rsa_export(rsakey_t k)
+uint8_t* foleo_rsa_export(rsakey_t* k)
 {
     const uint8_t width = 32;
     mpz_t n, t;
     mpz_init(n);
     mpz_init(t);
-    mpz_set(n, k.n);
+    mpz_set(n, k->n);
     uint8_t* kn = malloc(0);
     uint16_t knS = 0;
     while (mpz_sgn(n) != 0)
@@ -251,7 +189,7 @@ static uint8_t* foleo_rsa_export(rsakey_t k)
         kn[knS++] = foleo_rsa_prettymap(mpz_get_ui(t), 0);
         mpz_div_ui(n, n, 32);
     }
-    mpz_set(n, k.k);
+    mpz_set(n, k->k);
     uint8_t* kk = malloc(0);
     uint16_t kkS = 0;
     while (mpz_sgn(n) != 0)
@@ -267,11 +205,11 @@ static uint8_t* foleo_rsa_export(rsakey_t k)
     uint32_t cnt = 0;
 
     uint8_t nolabel = 0;
-    if (k.label == NULL)
+    if (k->label == NULL)
     {
         nolabel = 1;
     }
-    else if (k.label[0] == 0)
+    else if (k->label[0] == 0)
     {
         nolabel = 1;
     }
@@ -295,8 +233,8 @@ static uint8_t* foleo_rsa_export(rsakey_t k)
         for (;;)
         {
             tmsg = realloc(tmsg, mS + 1);
-            tmsg[mS++] = k.label[li++];
-            if (k.label[li] == 0 || mS > (width / 3) * 2)
+            tmsg[mS++] = k->label[li++];
+            if (k->label[li] == 0 || mS > (width / 3) * 2)
             {
                 tmsg = realloc(tmsg, mS + 2);
                 tmsg[mS++] = ']';
@@ -330,7 +268,7 @@ static uint8_t* foleo_rsa_export(rsakey_t k)
         {
             keyascii = realloc(keyascii, keyascii_i + 1);
             keyascii[keyascii_i++] = ' ';
-            mpz_set(n, k.n);
+            mpz_set(n, k->n);
             cnt++;
             if (cnt % width == 0)
             {
@@ -461,14 +399,15 @@ void foleo_rsa_keygen(uint16_t bitWidth, rsakey_t* public, rsakey_t* private)
     private->label[13] = 'e';
     private->label[14] = 'y';
     private->label[15] = 0;
+
 }
 
-void foleo_rsa_free(rsakey_t key)
+void foleo_rsa_free(rsakey_t* key)
 {
-    key.bitWidth = -1;
-    mpz_clear(key.n);
-    mpz_clear(key.k);
-    if (key.label != NULL) free(key.label);
+    key->bitWidth = -1;
+    mpz_clear(key->n);
+    mpz_clear(key->k);
+    if (key->label != NULL) free(key->label);
 }
 
 //convert to PKCS#1 v1.5 encryption block
@@ -778,4 +717,10 @@ uint16_t foleo_rsa_size(rsakey_t key, uint8_t padding)
     return key.bitWidth / 8;
 }
 
+//returns the size of an rsa key
+uint16_t foleo_rsa_keysize()
+{
+    return sizeof(rsakey_t);
+}
+
 #endif
index 25442f0857453255c41f9ee6c672f5034d0b8d45..cca7239688e494327870459b4a7e88442feacdd6 100644 (file)
@@ -64,33 +64,30 @@ void main()
     //test_hkdf(2, RFC5869_HKDF_ikm2, sizeof(RFC5869_HKDF_ikm2), RFC5869_HKDF_salt2, sizeof(RFC5869_HKDF_salt2), RFC5869_HKDF_info2, sizeof(RFC5869_HKDF_info2), RFC5869_HKDF_result2, sizeof(RFC5869_HKDF_result2));
     //test_hkdf(3, RFC5869_HKDF_ikm3, sizeof(RFC5869_HKDF_ikm3), RFC5869_HKDF_salt3, sizeof(RFC5869_HKDF_salt3), RFC5869_HKDF_info3, sizeof(RFC5869_HKDF_info3), RFC5869_HKDF_result3, sizeof(RFC5869_HKDF_result3));
 
+    //Randomly generate a private and public key
     rsakey_t pub, prv;
-    foleo_rsa_keygen(2048, &pub, &prv);
+    foleo_rsa_keygen(1024, &pub, &prv);
 
-    uint8_t* b1 = foleo_rsa_export(pub);
-    uint8_t* b2 = foleo_rsa_export(prv);
-
-    foleo_rsa_free(pub);
-    foleo_rsa_free(prv);
-    pub = foleo_rsa_import(b1);
-    prv = foleo_rsa_import(b2);
-
-    printf(">%i<\n", foleo_rsa_size(pub, FOLEO_RSA_ENCRYPTION));
-
-    uint8_t plaintext[245];
-    for (uint16_t i = 0; i < sizeof(plaintext); i++) plaintext[i] = i;
-    uint8_t* ciphertext = foleo_rsa_encrypt(pub, FOLEO_RSA_ENCRYPTION, plaintext, sizeof(plaintext));
-    uint16_t newSize;
-    uint8_t* newplaintext = foleo_rsa_decrypt(prv, FOLEO_RSA_ENCRYPTION, ciphertext, &newSize);
-    
-    test_pbuf("...", newplaintext, newSize);
+    uint8_t* tmp = foleo_rsa_export(&prv);
+    printf("%s\n", tmp);
+    ////Encrypt some plaintext into ciphertext
+    //uint8_t plaintext[] = "here is some plaintext to encrypt";
+    //uint8_t* ciphertext = foleo_rsa_encrypt(pub, FOLEO_RSA_OAEP, plaintext, sizeof(plaintext));
+    //
+    ////Display them
+    //test_pbuf("plaintext", plaintext, sizeof(plaintext));
+    //test_pbuf("ciphertext", ciphertext, 2048 / 8);
+ //
+    ////Decrypt the ciphertext
+    //uint16_t newSize;
+    //uint8_t* new_plaintext = foleo_rsa_decrypt(prv, FOLEO_RSA_OAEP, ciphertext, &newSize);
+//
+    ////Display it
+    //test_pbuf("new_plaintext", plaintext, newSize);
 
-    free(b1);
-    free(b2);
-    free(ciphertext);
-    free(newplaintext);
-    foleo_rsa_free(pub);
-    foleo_rsa_free(prv);
+    //free(ciphertext);
+    foleo_rsa_free(&pub);
+    foleo_rsa_free(&prv);