From: miha-q <> Date: Fri, 11 Aug 2023 23:09:09 +0000 (-0400) Subject: Fri Aug 11 07:09:09 PM EDT 2023 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=f45b7ce8c7d6bc3f544fb29f6073c7d9401e0c3b;p=CryptoFoleo.git Fri Aug 11 07:09:09 PM EDT 2023 --- diff --git a/bin/CryptoFoleo.h b/bin/CryptoFoleo.h index da94f6d..f45039d 100644 --- a/bin/CryptoFoleo.h +++ b/bin/CryptoFoleo.h @@ -1,17 +1,17 @@ #include #include -uint8_t* foleo_chacha20(uint8_t[32], uint8_t[12], uint32_t, uint64_t); -uint8_t* foleo_chacha20_poly1305(uint8_t[32], uint8_t[12], uint8_t*, uint64_t); +uint8_t* foleo_chacha20(uint8_t[32], uint8_t[12], uint32_t, size_t); +uint8_t* foleo_chacha20_poly1305(uint8_t[32], uint8_t[12], uint8_t*, size_t); uint8_t* foleo_dhke(uint8_t*, uint8_t*); -uint8_t* foleo_dhke_prf(uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t); -uint8_t* foleo_poly1305(uint8_t*, uint8_t*, uint8_t*, uint64_t); -static uint8_t* foleo_prigen(uint16_t); -#define FOLEO_RSA_NONE 99 -#define FOLEO_RSA_ENCRYPTION 1 -#define FOLEO_RSA_SIGNATURE 2 -#define FOLEO_RSA_OAEP 3 -#define FOLEO_RSA_PSS 4 +uint16_t foleo_dhke_modsize(); + +uint8_t* foleo_poly1305(uint8_t[32], uint8_t*, size_t); +#define FOLEO_RSA_PADDING_NONE 99 +#define FOLEO_RSA_PADDING_ENCRYPTION 1 +#define FOLEO_RSA_PADDING_SIGNATURE 2 +#define FOLEO_RSA_PADDING_OAEP 3 +#define FOLEO_RSA_PADDING_PSS 4 typedef struct { mpz_t n, k; @@ -22,9 +22,25 @@ 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*); + +//The maximum message block size that can be used +// for a particular padding scheme. +uint16_t foleo_rsa_msgsize(rsakey_t*, uint8_t); + +//Size of the rsakey struct 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*); + +//Size in bytes of RSA modulus, same thing as the number +// of bytes the encrypt() function will return +uint16_t foleo_rsa_modsize(rsakey_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 + +#define FOLEO_SHA256 1 +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 diff --git a/bin/CryptoFoleo.hi b/bin/CryptoFoleo.hi index 48ee238..fa24614 100644 Binary files a/bin/CryptoFoleo.hi and b/bin/CryptoFoleo.hi differ diff --git a/bin/CryptoFoleo.hs b/bin/CryptoFoleo.hs index 7a2746b..401baa0 100644 --- a/bin/CryptoFoleo.hs +++ b/bin/CryptoFoleo.hs @@ -88,7 +88,7 @@ foreign import ccall unsafe "foleo_chacha20" c_chacha20 :: Ptr (CUChar) -> Ptr (CUChar) -> Word32 -> CSize -> IO (Ptr (CUChar)) foreign import ccall unsafe "foleo_poly1305" - c_poly1305 :: Ptr (CUChar) -> Ptr (CUChar) -> Ptr (CUChar) -> CSize -> IO (Ptr (CUChar)) + c_poly1305 :: Ptr (CUChar) -> Ptr (CUChar) -> CSize -> IO (Ptr (CUChar)) foreign import ccall unsafe "foleo_chacha20_poly1305" c_chacha20_poly1305 :: Ptr (CUChar) -> Ptr (CUChar) -> Ptr (CUChar) -> CSize -> IO (Ptr (CUChar)) @@ -261,18 +261,17 @@ chacha20 key nonce block count = do c_free rPtr return r -poly1305 :: ByteString -> ByteString -> ByteString -> IO (ByteString) -poly1305 r s m = do - if (BS.length r /= 16) || (BS.length s /= 16) then +poly1305 :: ByteString -> ByteString -> IO (ByteString) +poly1305 k m = do + if BS.length k /= 32 then return BS.empty else - useAsCString r $ \rPtr -> do - useAsCString s $ \sPtr -> do - useAsCString m $ \mPtr -> do - hPtr <- c_poly1305 (castPtr rPtr) (castPtr sPtr) (castPtr mPtr) (fromIntegral (BS.length m)) - h <- BI.create 16 (\ptr -> MU.copyBytes ptr (castPtr hPtr) 16) - c_free hPtr - return h + useAsCString k $ \kPtr -> do + useAsCString m $ \mPtr -> do + hPtr <- c_poly1305 (castPtr kPtr) (castPtr mPtr) (fromIntegral (BS.length m)) + h <- BI.create 16 (\ptr -> MU.copyBytes ptr (castPtr hPtr) 16) + c_free hPtr + return h chacha20_poly1305 :: ByteString -> ByteString -> ByteString -> IO (ByteString) chacha20_poly1305 key nonce ctext = do diff --git a/bin/CryptoFoleo.o b/bin/CryptoFoleo.o index 40f9b4d..d62821a 100644 Binary files a/bin/CryptoFoleo.o and b/bin/CryptoFoleo.o differ diff --git a/bin/Main.hi b/bin/Main.hi index d96de91..fe2978d 100644 Binary files a/bin/Main.hi and b/bin/Main.hi differ diff --git a/bin/Main.hs b/bin/Main.hs index 62a1c64..9d4a3e5 100644 --- a/bin/Main.hs +++ b/bin/Main.hs @@ -30,7 +30,7 @@ main = do 0x75, 0x70 ] - p <- poly1305 r s m + p <- poly1305 r m print $ byteStringToHexString p diff --git a/bin/Main.o b/bin/Main.o index 39907d3..7a0ecde 100644 Binary files a/bin/Main.o and b/bin/Main.o differ diff --git a/bin/libCryptoFoleo.so b/bin/libCryptoFoleo.so index a40af30..8b3724f 100755 Binary files a/bin/libCryptoFoleo.so and b/bin/libCryptoFoleo.so differ diff --git a/bin/main b/bin/main index e819b9b..a5af5bd 100755 Binary files a/bin/main and b/bin/main differ diff --git a/src/headers.hs b/src/headers.hs index 7a2746b..401baa0 100644 --- a/src/headers.hs +++ b/src/headers.hs @@ -88,7 +88,7 @@ foreign import ccall unsafe "foleo_chacha20" c_chacha20 :: Ptr (CUChar) -> Ptr (CUChar) -> Word32 -> CSize -> IO (Ptr (CUChar)) foreign import ccall unsafe "foleo_poly1305" - c_poly1305 :: Ptr (CUChar) -> Ptr (CUChar) -> Ptr (CUChar) -> CSize -> IO (Ptr (CUChar)) + c_poly1305 :: Ptr (CUChar) -> Ptr (CUChar) -> CSize -> IO (Ptr (CUChar)) foreign import ccall unsafe "foleo_chacha20_poly1305" c_chacha20_poly1305 :: Ptr (CUChar) -> Ptr (CUChar) -> Ptr (CUChar) -> CSize -> IO (Ptr (CUChar)) @@ -261,18 +261,17 @@ chacha20 key nonce block count = do c_free rPtr return r -poly1305 :: ByteString -> ByteString -> ByteString -> IO (ByteString) -poly1305 r s m = do - if (BS.length r /= 16) || (BS.length s /= 16) then +poly1305 :: ByteString -> ByteString -> IO (ByteString) +poly1305 k m = do + if BS.length k /= 32 then return BS.empty else - useAsCString r $ \rPtr -> do - useAsCString s $ \sPtr -> do - useAsCString m $ \mPtr -> do - hPtr <- c_poly1305 (castPtr rPtr) (castPtr sPtr) (castPtr mPtr) (fromIntegral (BS.length m)) - h <- BI.create 16 (\ptr -> MU.copyBytes ptr (castPtr hPtr) 16) - c_free hPtr - return h + useAsCString k $ \kPtr -> do + useAsCString m $ \mPtr -> do + hPtr <- c_poly1305 (castPtr kPtr) (castPtr mPtr) (fromIntegral (BS.length m)) + h <- BI.create 16 (\ptr -> MU.copyBytes ptr (castPtr hPtr) 16) + c_free hPtr + return h chacha20_poly1305 :: ByteString -> ByteString -> ByteString -> IO (ByteString) chacha20_poly1305 key nonce ctext = do