]> foleosoft.com Git - CryptoFoleo.git/commitdiff
Mon Aug 14 03:51:31 PM EDT 2023
authorserver <[email protected]>
Mon, 14 Aug 2023 19:51:31 +0000 (15:51 -0400)
committerserver <[email protected]>
Mon, 14 Aug 2023 19:51:31 +0000 (15:51 -0400)
bin/CryptoFoleo.h
bin/CryptoFoleo.hs
bin/libCryptoFoleo.so
src/all.c
src/auth.c [new file with mode: 0644]
src/headers.h
src/pam [deleted file]
src/pam.c [deleted file]
src/test [deleted file]
src/test2.c [deleted file]

index f45039d7821c7f1a8cb6bc35be2e7b6e70088871..c7a6885a8f728e2e21e4108c9c925ccec02b04a6 100644 (file)
@@ -43,4 +43,5 @@ uint8_t* foleo_hmac(uint8_t, uint8_t*, uint32_t, uint8_t*, uint32_t);
 uint8_t* foleo_hmac_hkdf(uint8_t, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t);
 uint8_t* foleo_hmac_prf(uint8_t, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t);
 
-uint8_t foleo_hash_size(uint8_t);
\ No newline at end of file
+uint8_t foleo_hash_size(uint8_t);
+uint8_t foleo_auth(const char*, const char*);
index 401baa05fddf0a83477fb2c759fd0c1fd58febe2..f5c6bc1d21997ea90ab63cb0b5b9f7e45cf3f6d5 100644 (file)
@@ -3,6 +3,7 @@ module CryptoFoleo
         dhke,
         chacha20,
         poly1305,
+        chacha20_poly1305,
 
         rsa_keygen,
         rsa_import,
@@ -22,7 +23,8 @@ module CryptoFoleo
         rsa_padding_oaep,
         rsa_padding_pss,
 
-        byteStringToHexString
+        byteStringToHexString,
+        fromNumberFixedSize
     )
 where
 import System.IO as IO
@@ -33,7 +35,6 @@ import Data.ByteString as BS
 import Data.ByteString.Internal
 import Data.Word
 import Data.ByteString.Unsafe
-import Control.DeepSeq
 import Control.Monad
 import qualified Data.ByteString.Internal as BI
 import qualified Foreign.Marshal.Utils as MU
@@ -149,7 +150,7 @@ rsa_keygen n fn = do
             c_rsa_keygen (fromIntegral n) pubKeyPtr prvKeyPtr
             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
+            fn prvKey pubKey
             rsa_free pubKey
             rsa_free prvKey
 
@@ -176,31 +177,31 @@ rsa_export keyBS = do
 rsa_free :: ByteString -> IO()
 rsa_free blob = useAsCString blob $ \ptr -> c_rsa_free (castPtr ptr)
 
-dhke :: (ByteString, ByteString) -> IO (ByteString)
+dhke :: [ByteString] -> IO (ByteString)
 dhke v = do
     c_modSize <- c_dhke_modsize
     let modSize = fromIntegral c_modSize
-    if ((BS.length(fst v) + BS.length(snd v)) == 0) then do
+    if Prelude.length v == 0 then do
             secretPtr <- c_dhke nullPtr nullPtr
             bsPtr <- BI.create modSize (\ptr -> MU.copyBytes ptr (castPtr secretPtr) modSize)
             c_free secretPtr
             return bsPtr
-    else if (BS.length(snd v) == 0) then do
-        useAsCString (fst v) $ \secretPtr -> do
+    else if Prelude.length v == 1 then do
+        useAsCString (v !! 0) $ \secretPtr -> do
             sharePtr <- c_dhke (castPtr secretPtr) nullPtr
             bsPtr <- BI.create modSize (\ptr -> MU.copyBytes ptr (castPtr sharePtr) modSize)
             c_free sharePtr
             return bsPtr
-    else if (BS.length(snd v) > 0) then do
-        useAsCString (fst v) $ \secretPtr -> do
-            useAsCString (snd v) $ \sharePtr -> do
+    else if Prelude.length v == 2 then do
+        useAsCString (v !! 0) $ \secretPtr -> do
+            useAsCString (v !! 1) $ \sharePtr -> do
                 keyPtr <- c_dhke (castPtr secretPtr) (castPtr sharePtr)
                 bsPtr <- BI.create modSize (\ptr -> MU.copyBytes ptr (castPtr keyPtr) modSize)
                 c_free keyPtr
                 return bsPtr
     else return BS.empty
 
-sha256 :: ByteString -> IO (String)
+sha256 :: ByteString -> IO (ByteString)
 sha256 ptBS = do
     let ptSize :: Word32
         ptSize = fromIntegral (BS.length ptBS)
@@ -208,7 +209,7 @@ sha256 ptBS = do
         hPtr <- c_sha256 (castPtr ptPtr) (fromIntegral ptSize)
         hBS <- BI.create 32 (\ptr -> MU.copyBytes ptr (castPtr hPtr) 32)
         c_free hPtr
-        return (byteStringToHexString hBS)
+        return hBS
 
 hmac :: Int -> ByteString -> ByteString -> IO (ByteString)
 hmac h k m = do
@@ -249,13 +250,15 @@ hmac_hkdf hf db sc lb sd = do
                     c_free rPtr
                     return r
 
-chacha20 :: ByteString -> ByteString -> Int -> Int -> IO (ByteString)
+chacha20 :: ByteString -> Integer -> Int -> Int -> IO (ByteString)
 chacha20 key nonce block count = do
-    if (BS.length key) /= 32 || (BS.length nonce) /= 12 || block < 0 || count < 0 then
+    let nonceBS :: BS.ByteString
+        nonceBS = fromNumberFixedSize nonce 12
+    if (BS.length key) /= 32 || (BS.length nonceBS) /= 12 || block < 0 || count < 0 then
         return BS.empty
     else
         useAsCString key $ \keyPtr -> do
-            useAsCString nonce $ \noncePtr -> do
+            useAsCString nonceBS $ \noncePtr -> do
                 rPtr <- c_chacha20 (castPtr keyPtr) (castPtr noncePtr) (fromIntegral block) (fromIntegral count)
                 r <- BI.create count (\ptr -> MU.copyBytes ptr (castPtr rPtr) count)
                 c_free rPtr
@@ -273,20 +276,21 @@ poly1305 k m = do
                 c_free hPtr
                 return h
 
-chacha20_poly1305 :: ByteString -> ByteString -> ByteString -> IO (ByteString)
+chacha20_poly1305 :: ByteString -> Integer -> ByteString -> IO (ByteString)
 chacha20_poly1305 key nonce ctext = do
-    if (BS.length key) /= 32 || (BS.length nonce) /= 12 then
+    let nonceBS :: BS.ByteString
+        nonceBS = fromNumberFixedSize nonce 12
+    if (BS.length key) /= 32 || (BS.length nonceBS) /= 12 then
         return BS.empty
     else
         useAsCString key $ \keyPtr -> do
-            useAsCString nonce $ \noncePtr -> do
+            useAsCString nonceBS $ \noncePtr -> do
                 useAsCString ctext $ \ctextPtr -> do
                     rPtr <- c_chacha20_poly1305 (castPtr keyPtr) (castPtr noncePtr) (castPtr ctextPtr) (fromIntegral (BS.length ctext))
                     r <- BI.create 16 (\ptr -> MU.copyBytes ptr (castPtr rPtr) 16)
                     c_free rPtr
                     return r
 
-
 byteToHexString :: Word8 -> String
 byteToHexString b = do
     case (div b 16) of
@@ -326,7 +330,6 @@ byteToHexString b = do
         14 -> "e"
         15 -> "f"
         _ -> "0"
-
     
 byteStringToByteList :: ByteString -> [Word8]
 byteStringToByteList b = BS.unpack b
@@ -336,3 +339,14 @@ byteStringToHexString b = Prelude.foldr (\i s -> (byteToHexString i) ++ s) "" (b
 
 byteStringToInteger :: ByteString -> Integer
 byteStringToInteger = foldl' (\acc byte -> acc * 256 + fromIntegral byte) 0
+
+fromNumberFixedSize :: Integer -> Int -> BS.ByteString
+fromNumberFixedSize n p = do
+    if n < 0 || p < 0 then BS.empty
+    else do
+        let fn :: Integer -> [Word8] -> [Word8]
+            fn x r = if x == 0 then r else fn (div x 256) ([fromIntegral (mod x 256)] ++ r)
+            pd :: [Word8] -> [Word8]
+            pd x = if (Prelude.length x) < p then pd ([0] ++ x) else x
+            rt = pd (fn n [])
+        BS.pack $ if (Prelude.length rt) > p then Prelude.drop ((Prelude.length rt) - p) rt else rt
\ No newline at end of file
index 8b3724f0460aadbd9dc3269d812749d20875dadb..b5387187cbd296f8ef1efb2213afe8a3af57d3ff 100755 (executable)
Binary files a/bin/libCryptoFoleo.so and b/bin/libCryptoFoleo.so differ
index 1155cc527b0f5bcf5fafd09931b340386b346098..f961fd234d9d9e9d66e9ec4ce0f9c7254313e245 100644 (file)
--- a/src/all.c
+++ b/src/all.c
@@ -5,4 +5,6 @@
 #include "poly1305.c"
 #include "prigen.c"
 #include "rsa.c"
-#include "sha256.c"
\ No newline at end of file
+#include "sha256.c"
+#include "auth.c"
+
diff --git a/src/auth.c b/src/auth.c
new file mode 100644 (file)
index 0000000..ea0086b
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef __AUTHENTICATE__
+#define __AUTHENTICATE__
+#include <crypt.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <pwd.h>
+#include <shadow.h>
+#include <string.h>
+#include <stdint.h>
+#include <unistd.h>
+
+uint8_t foleo_auth(const char *username, const char* password) {
+        struct spwd spw, *result;
+        char *buf;
+        size_t bufsize;
+
+        bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
+        if (bufsize == -1) {
+            bufsize = 16384; // use a default size if sysconf returns indeterminate size
+        }
+
+        buf = malloc(bufsize);
+        getspnam_r(username, &spw, buf, bufsize, &result);
+        if (result == NULL)
+        {
+            free(buf);
+            return 0;
+        }
+
+        int status = strcmp(crypt(password, spw.sp_pwdp), spw.sp_pwdp) == 0;
+        free(buf);
+        return status;
+}
+#endif
index f45039d7821c7f1a8cb6bc35be2e7b6e70088871..c7a6885a8f728e2e21e4108c9c925ccec02b04a6 100644 (file)
@@ -43,4 +43,5 @@ uint8_t* foleo_hmac(uint8_t, uint8_t*, uint32_t, uint8_t*, uint32_t);
 uint8_t* foleo_hmac_hkdf(uint8_t, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t);
 uint8_t* foleo_hmac_prf(uint8_t, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t);
 
-uint8_t foleo_hash_size(uint8_t);
\ No newline at end of file
+uint8_t foleo_hash_size(uint8_t);
+uint8_t foleo_auth(const char*, const char*);
diff --git a/src/pam b/src/pam
deleted file mode 100755 (executable)
index b603d34..0000000
Binary files a/src/pam and /dev/null differ
diff --git a/src/pam.c b/src/pam.c
deleted file mode 100644 (file)
index aa39d17..0000000
--- a/src/pam.c
+++ /dev/null
@@ -1,62 +0,0 @@
-#ifndef __PAM__
-#define __PAM__
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
-#include <security/pam_appl.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <pwd.h>
-#include <stdlib.h>
-#include <stdbool.h>
-#include <string.h>
-#include <security/pam_appl.h>
-
-static int pamconv(int num_msg, const struct pam_message **msg,
-        struct pam_response **resp, void *appdata_ptr)
-{
-    char *pass = malloc(strlen(appdata_ptr)+1);
-    strcpy(pass, appdata_ptr);
-
-    int i;
-
-    *resp = calloc(num_msg, sizeof(struct pam_response));
-
-    for (i = 0; i < num_msg; ++i)
-    {
-        /* Ignore all PAM messages except prompting for hidden input */
-        if (msg[i]->msg_style != PAM_PROMPT_ECHO_OFF)
-            continue;
-
-        /* Assume PAM is only prompting for the password as hidden input */
-        resp[i]->resp = pass;
-    }
-
-    return PAM_SUCCESS;
-}
-
-bool checkAuthentication(const char *user, const char *pass)
-{
-    /* use own PAM conversation function just responding with the
-       password passed here */
-    struct pam_conv conv = { &pamconv, (void *)pass };
-
-    pam_handle_t *handle;
-    int authResult;
-
-    pam_start("shutterd", user, &conv, &handle);
-    authResult = pam_authenticate(handle,
-            PAM_SILENT|PAM_DISALLOW_NULL_AUTHTOK);
-    pam_end(handle, authResult);
-
-    return (authResult == PAM_SUCCESS);
-}
-
-
-void main()
-{
-       printf("%i\n", checkAuthentication("home", "jasopoint"));
-
-}
-#endif
diff --git a/src/test b/src/test
deleted file mode 100755 (executable)
index 289a101..0000000
Binary files a/src/test and /dev/null differ
diff --git a/src/test2.c b/src/test2.c
deleted file mode 100644 (file)
index 8d04889..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-
-uint8_t authenticate(const char *username, const char* password) {
-    struct spwd spw;
-    struct spwd *result;
-    char *buf;
-    size_t bufsize;
-    int s;
-
-    bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
-    if (bufsize == -1) {
-        bufsize = 16384; // use a default size if sysconf returns indeterminate size
-    }
-
-    buf = malloc(bufsize);
-    if (buf == NULL) {
-        perror("malloc");
-        exit(EXIT_FAILURE);
-    }
-
-    s = getspnam_r(username, &spw, buf, bufsize, &result);
-    if (result == NULL) {
-        if (s == 0) {
-            fprintf(stderr, "User not found\n");
-        } else {
-            perror("getspnam_r");
-        }
-        free(buf);
-        return 0;
-    }
-
-    const char *hashedPassword = crypt(password, spw.sp_pwdp);
-    
-    int status = strcmp(hashedPassword, spw.sp_pwdp) == 0;
-    free(buf);
-    return status;
-}
-
-int main(int argc, char **argv) {
-    if (argc != 3) {
-        printf("Usage: %s <username> <password>\n", argv[0]);
-        return 1;
-    }
-
-    if (authenticate(argv[1], argv[2])) {
-        printf("Authenticated!\n");
-    } else {
-        printf("Authentication failed.\n");
-    }
-
-    return 0;
-}