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*);
dhke,
chacha20,
poly1305,
+ chacha20_poly1305,
rsa_keygen,
rsa_import,
rsa_padding_oaep,
rsa_padding_pss,
- byteStringToHexString
+ byteStringToHexString,
+ fromNumberFixedSize
)
where
import System.IO as IO
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
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
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)
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
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
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
14 -> "e"
15 -> "f"
_ -> "0"
-
byteStringToByteList :: ByteString -> [Word8]
byteStringToByteList b = BS.unpack 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
#include "poly1305.c"
#include "prigen.c"
#include "rsa.c"
-#include "sha256.c"
\ No newline at end of file
+#include "sha256.c"
+#include "auth.c"
+
--- /dev/null
+#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
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*);
+++ /dev/null
-#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
+++ /dev/null
-
-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;
-}