]> foleosoft.com Git - CryptoFoleo.git/commitdiff
Fri Aug 11 01:16:12 PM EDT 2023
authorserver <[email protected]>
Fri, 11 Aug 2023 17:16:12 +0000 (13:16 -0400)
committerserver <[email protected]>
Fri, 11 Aug 2023 17:16:12 +0000 (13:16 -0400)
12 files changed:
bin/CryptoFoleo.h
bin/libCryptoFoleo.so
build [deleted file]
build.sh
src/headers.hs [new file with mode: 0644]
tests/PRF_JBP.pdf [new file with mode: 0644]
tests/cases.c
tests/rfc4231.pdf [new file with mode: 0644]
tests/rfc4231.txt.pdf [deleted file]
tests/rfc5869.pdf [new file with mode: 0644]
tests/rfc5869.txt.pdf [deleted file]
tests/test.c

index 0aba4e24c298d7500ae833a57be4c80e4c0c7de2..da94f6d5faf7a03c0acf662a05831f929be7052e 100644 (file)
@@ -1,17 +1,17 @@
 #include <stdint.h>
 #include <gmp.h>
 
-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_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_dhke(uint8_t*, uint8_t*);
-uint16_t foleo_dhke_modsize();
-
-uint8_t* foleo_poly1305(uint8_t*, uint8_t*, 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
+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
 typedef struct
 {
     mpz_t n, k;
@@ -22,25 +22,9 @@ 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();
-
-//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*);
+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 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
+#define FOLEO_SHA256 foleo_sha256, 32, 64
index 18ffc9391c7b267692d897a6deda60e2005a067d..a40af30c168b0817911d8afc70515a2e11a04fa8 100755 (executable)
Binary files a/bin/libCryptoFoleo.so and b/bin/libCryptoFoleo.so differ
diff --git a/build b/build
deleted file mode 100755 (executable)
index 0b2e461..0000000
--- a/build
+++ /dev/null
@@ -1,9 +0,0 @@
-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 54195d16b79aed478f1c13ecf884ea74774b7a2f..950dc7a5e9295d70a8361b9f953c4e07018ba534 100644 (file)
--- a/build.sh
+++ b/build.sh
@@ -11,4 +11,5 @@ then
 else
        gcc -shared -fPIC src/all.c -o bin/libCryptoFoleo.so -lgmp -DDEVICE='"/dev/random"'
        cp src/headers.h bin/CryptoFoleo.h
+       cp src/headers.hs bin/CryptoFoleo.hs
 fi
diff --git a/src/headers.hs b/src/headers.hs
new file mode 100644 (file)
index 0000000..7a2746b
--- /dev/null
@@ -0,0 +1,339 @@
+module CryptoFoleo
+    (
+        dhke,
+        chacha20,
+        poly1305,
+
+        rsa_keygen,
+        rsa_import,
+        rsa_export,
+        rsa_encrypt,
+        rsa_decrypt,
+
+        sha256,
+        hash_sha256,
+        hmac,
+        hmac_prf,
+        hmac_hkdf,
+
+        rsa_padding_none,
+        rsa_padding_encryption,
+        rsa_padding_signature,
+        rsa_padding_oaep,
+        rsa_padding_pss,
+
+        byteStringToHexString
+    )
+where
+import System.IO as IO
+import Foreign
+import Foreign.C.Types
+import Foreign.C.String
+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
+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 ()
+
+foreign import ccall unsafe "foleo_rsa_export"
+    c_rsa_export :: Ptr () -> IO (Ptr (CUChar))
+
+foreign import ccall unsafe "foleo_rsa_import"
+    c_rsa_import :: Ptr (CUChar) -> Ptr () -> IO ()
+
+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))
+
+foreign import ccall unsafe "foleo_rsa_decrypt"
+    c_rsa_decrypt :: Ptr () -> CUChar -> Ptr (CUChar) -> Ptr (CUShort) -> IO (Ptr (CUChar))
+
+foreign import ccall unsafe "foleo_dhke_modsize"
+    c_dhke_modsize :: IO (CUShort)
+
+foreign import ccall unsafe "foleo_dhke"
+    c_dhke :: Ptr (CUChar) -> Ptr (CUChar) -> IO (Ptr (CUChar))
+
+foreign import ccall unsafe "foleo_sha256"
+    c_sha256 :: Ptr (CUChar) -> Word32 -> IO (Ptr (CUChar))
+
+foreign import ccall unsafe "foleo_hash_size"
+    c_hash_size :: CUChar -> IO (CUChar)
+
+foreign import ccall unsafe "foleo_hmac"
+    c_hmac :: CUChar -> Ptr (CUChar) -> Word32 -> Ptr (CUChar) -> Word32 -> IO (Ptr (CUChar))
+
+foreign import ccall unsafe "foleo_hmac_prf"
+    c_hmac_prf :: CUChar -> Word32 -> Ptr (CUChar) -> Word32 -> Ptr (CUChar) -> Word32 -> Ptr (CUChar) -> Word32 -> IO (Ptr (CUChar))
+
+foreign import ccall unsafe "foleo_hmac_hkdf"
+    c_hmac_hkdf :: CUChar -> Word32 -> Ptr (CUChar) -> Word32 -> Ptr (CUChar) -> Word32 -> Ptr (CUChar) -> Word32 -> IO (Ptr (CUChar))
+
+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))
+
+foreign import ccall unsafe "foleo_chacha20_poly1305"
+    c_chacha20_poly1305 :: Ptr (CUChar) -> Ptr (CUChar) -> Ptr (CUChar) -> CSize -> IO (Ptr (CUChar))
+
+foreign import ccall unsafe "free"
+    c_free :: Ptr a -> IO ()
+
+rsa_padding_none :: Int
+rsa_padding_none = 99
+
+rsa_padding_encryption :: Int
+rsa_padding_encryption = 1
+
+rsa_padding_signature :: Int
+rsa_padding_signature = 2
+
+rsa_padding_oaep :: Int
+rsa_padding_oaep = 3
+
+rsa_padding_pss :: Int
+rsa_padding_pss = 4
+
+hash_sha256 :: Int
+hash_sha256 = 1
+
+rsa_encrypt :: ByteString -> Int -> ByteString-> IO (ByteString)
+rsa_encrypt keyBS padType ptBS = do
+    useAsCString keyBS $ \keyPtr -> do
+        useAsCString ptBS $ \ptPtr -> do
+            sModSize <- c_rsa_modsize (castPtr keyPtr)
+            ctPtr <- c_rsa_encrypt (castPtr keyPtr) (fromIntegral padType) (castPtr ptPtr) (fromIntegral (BS.length ptBS))
+            ctBS <- BI.create (fromIntegral sModSize) (\ptr -> MU.copyBytes ptr (castPtr ctPtr) (fromIntegral sModSize))
+            c_free ctPtr
+            return ctBS
+
+rsa_decrypt :: ByteString -> Int -> ByteString -> IO (ByteString)
+rsa_decrypt keyBS padType ctBS = do
+    useAsCString keyBS $ \keyPtr -> do
+        useAsCString ctBS $ \ctPtr -> do
+            sModSize <- c_rsa_modsize (castPtr keyPtr)
+            allocaBytes 2 $ \sizePtr -> do
+                ptPtr <- c_rsa_decrypt (castPtr keyPtr) (fromIntegral padType) (castPtr ctPtr) (castPtr sizePtr)
+                if ptPtr == nullPtr then do
+                    return BS.empty
+                else do
+                    ptSize <- peek sizePtr
+                    ptBS <- BI.create ptSize (\ptr -> MU.copyBytes ptr (castPtr ptPtr) ptSize)
+                    c_free ptPtr
+                    return ptBS
+
+rsa_keygen :: Word16 -> (ByteString -> ByteString -> IO ()) -> IO ()
+rsa_keygen n fn = 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 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
+    sKeySize <- c_rsa_keysize
+    let keySize :: Int
+        keySize = fromIntegral sKeySize
+    unsafeUseAsCStringLen (C8.pack n) $ \(bsPtr, len) -> do
+        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 keyBS = do
+    useAsCString keyBS $ \keyPtr -> do
+        cStrPtr <- c_rsa_export (castPtr keyPtr)
+        cStr <- peekCString (castPtr cStrPtr)
+        c_free cStrPtr
+        return cStr
+
+rsa_free :: ByteString -> IO()
+rsa_free blob = useAsCString blob $ \ptr -> c_rsa_free (castPtr ptr)
+
+dhke :: (ByteString, 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
+            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
+            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
+                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 ptBS = do
+    let ptSize :: Word32
+        ptSize = fromIntegral (BS.length ptBS)
+    useAsCString ptBS $ \ptPtr -> 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)
+
+hmac :: Int -> ByteString -> ByteString -> IO (ByteString)
+hmac h k m = do
+    useAsCString k $ \kPtr -> do
+        useAsCString m $ \mPtr -> do
+            rPtr <- c_hmac (fromIntegral h) (castPtr kPtr) (fromIntegral (BS.length k)) (castPtr mPtr) (fromIntegral (BS.length m))
+            if rPtr == nullPtr then do
+                return BS.empty
+            else do
+                size <- c_hash_size (fromIntegral h)
+                r <- BI.create (fromIntegral size) (\ptr -> MU.copyBytes ptr (castPtr rPtr) (fromIntegral size))
+                c_free rPtr
+                return r
+
+hmac_prf :: Int -> Int -> ByteString -> ByteString -> ByteString -> IO (ByteString)
+hmac_prf hf db sc lb sd = do
+    useAsCString sc $ \scPtr -> do
+        useAsCString lb $ \lbPtr -> do
+            useAsCString sd $ \sdPtr -> do
+                rPtr <- c_hmac_prf (fromIntegral hf) (fromIntegral db) (castPtr scPtr) (fromIntegral (BS.length sc)) (castPtr lbPtr) (fromIntegral (BS.length lb)) (castPtr sdPtr) (fromIntegral (BS.length sd))
+                if rPtr == nullPtr then do
+                    return BS.empty
+                else do
+                    r <- BI.create db (\ptr -> MU.copyBytes ptr (castPtr rPtr) db)
+                    c_free rPtr
+                    return r
+
+hmac_hkdf :: Int -> Int -> ByteString -> ByteString -> ByteString -> IO (ByteString)
+hmac_hkdf hf db sc lb sd = do
+    useAsCString sc $ \scPtr -> do
+        useAsCString lb $ \lbPtr -> do
+            useAsCString sd $ \sdPtr -> do
+                rPtr <- c_hmac_hkdf (fromIntegral hf) (fromIntegral db) (castPtr scPtr) (fromIntegral (BS.length sc)) (castPtr lbPtr) (fromIntegral (BS.length lb)) (castPtr sdPtr) (fromIntegral (BS.length sd))
+                if rPtr == nullPtr then do
+                    return BS.empty
+                else do
+                    r <- BI.create db (\ptr -> MU.copyBytes ptr (castPtr rPtr) db)
+                    c_free rPtr
+                    return r
+
+chacha20 :: ByteString -> ByteString -> Int -> Int -> IO (ByteString)
+chacha20 key nonce block count = do
+    if (BS.length key) /= 32 || (BS.length nonce) /= 12 || block < 0 || count < 0 then
+        return BS.empty
+    else
+        useAsCString key $ \keyPtr -> do
+            useAsCString nonce $ \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
+                return r
+
+poly1305 :: ByteString -> ByteString -> ByteString -> IO (ByteString)
+poly1305 r s m = do
+    if (BS.length r /= 16) || (BS.length s /= 16) 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
+
+chacha20_poly1305 :: ByteString -> ByteString -> ByteString -> IO (ByteString)
+chacha20_poly1305 key nonce ctext = do
+    if (BS.length key) /= 32 || (BS.length nonce) /= 12 then
+        return BS.empty
+    else
+        useAsCString key $ \keyPtr -> do
+            useAsCString nonce $ \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
+        0 -> "0"
+        1 -> "1"
+        2 -> "2"
+        3 -> "3"
+        4 -> "4"
+        5 -> "5"
+        6 -> "6"
+        7 -> "7"
+        8 -> "8"
+        9 -> "9"
+        10 -> "a"
+        11 -> "b"
+        12 -> "c"
+        13 -> "d"
+        14 -> "e"
+        15 -> "f"
+        _ -> "0"
+    ++ 
+    case (mod b 16) of
+        0 -> "0"
+        1 -> "1"
+        2 -> "2"
+        3 -> "3"
+        4 -> "4"
+        5 -> "5"
+        6 -> "6"
+        7 -> "7"
+        8 -> "8"
+        9 -> "9"
+        10 -> "a"
+        11 -> "b"
+        12 -> "c"
+        13 -> "d"
+        14 -> "e"
+        15 -> "f"
+        _ -> "0"
+
+    
+byteStringToByteList :: ByteString -> [Word8]
+byteStringToByteList b = BS.unpack b
+
+byteStringToHexString :: ByteString -> String
+byteStringToHexString b = Prelude.foldr (\i s -> (byteToHexString i) ++ s) "" (byteStringToByteList b)
+
+byteStringToInteger :: ByteString -> Integer
+byteStringToInteger = foldl' (\acc byte -> acc * 256 + fromIntegral byte) 0
diff --git a/tests/PRF_JBP.pdf b/tests/PRF_JBP.pdf
new file mode 100644 (file)
index 0000000..ea1933f
Binary files /dev/null and b/tests/PRF_JBP.pdf differ
index 00dc57025224fa3ca1d773879c2c968fe7d72065..15dd6fcc1330439ea79357060c7b21c8185980fe 100644 (file)
@@ -125,7 +125,7 @@ uint8_t RFC4231_HMAC_key2[] =
 };
 uint8_t RFC4231_HMAC_data2[] =
 {
-    0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20, 
+    0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20,
     0x79, 0x61, 0x20, 0x77, 0x61, 0x6e, 0x74, 0x20,
     0x66, 0x6f, 0x72, 0x20, 0x6e, 0x6f, 0x74, 0x68,
     0x69, 0x6e, 0x67, 0x3f
@@ -175,7 +175,7 @@ uint8_t RFC4231_HMAC_data4[] =
     0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
     0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
     0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
-    0xcd, 0xcd 
+    0xcd, 0xcd
 };
 uint8_t RFC4231_HMAC_result4[] =
 {
@@ -284,12 +284,42 @@ uint8_t RFC4231_HMAC_data7[] =
 };
 uint8_t RFC4231_HMAC_result7[] =
 {
-    0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 
-    0x27, 0x63, 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44, 
-    0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93, 
+    0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb,
+    0x27, 0x63, 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44,
+    0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93,
     0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2
 };
 
+uint8_t JBP_PRF_secret1[] =
+{
+    0xe1, 0x88, 0x28, 0x74, 0x03, 0x52, 0xb5, 0x30,
+    0xd6, 0x9b, 0x34, 0xc6, 0x59, 0x7d, 0xea, 0x2e
+};
+uint8_t JBP_PRF_seed1[] =
+{
+    0xf5, 0xa3, 0xfe, 0x6d, 0x34, 0xe2, 0xe2, 0x85,
+    0x60, 0xfd, 0xca, 0xf6, 0x82, 0x3f, 0x90, 0x91
+};
+uint8_t JBP_PRF_label1[] =
+{
+    0x74, 0x65, 0x73, 0x74, 0x20, 0x6c, 0x61, 0x62,
+    0x65, 0x6c
+};
+uint8_t JBP_PRF_result1[] =
+{
+    0x22, 0x4d, 0x8a, 0xf3, 0xc0, 0x45, 0x33, 0x93,
+    0xa9, 0x77, 0x97, 0x89, 0xd2, 0x1c, 0xf7, 0xda,
+    0x5e, 0xe6, 0x2a, 0xe6, 0xb6, 0x17, 0x87, 0x3d,
+    0x48, 0x94, 0x28, 0xef, 0xc8, 0xdd, 0x58, 0xd1,
+    0x56, 0x6e, 0x70, 0x29, 0xe2, 0xca, 0x3a, 0x5e,
+    0xcd, 0x35, 0x5d, 0xc6, 0x4d, 0x4d, 0x92, 0x7e,
+    0x2f, 0xbd, 0x78, 0xc4, 0x23, 0x3e, 0x86, 0x04,
+    0xb1, 0x47, 0x49, 0xa7, 0x7a, 0x92, 0xa7, 0x0f,
+    0xdd, 0xf6, 0x14, 0xbc, 0x0d, 0xf6, 0x23, 0xd7,
+    0x98, 0x60, 0x4e, 0x4c, 0xa5, 0x51, 0x27, 0x94,
+    0xd8, 0x02, 0xa2, 0x58, 0xe8, 0x2f, 0x86, 0xcf
+};
+
 uint8_t RFC5869_HKDF_ikm1[] =
 {
     0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
@@ -382,4 +412,4 @@ uint8_t RFC5869_HKDF_result3[] =
     0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
     0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
     0x96, 0xc8
-};
\ No newline at end of file
+};
diff --git a/tests/rfc4231.pdf b/tests/rfc4231.pdf
new file mode 100644 (file)
index 0000000..3873859
Binary files /dev/null and b/tests/rfc4231.pdf differ
diff --git a/tests/rfc4231.txt.pdf b/tests/rfc4231.txt.pdf
deleted file mode 100644 (file)
index 3873859..0000000
Binary files a/tests/rfc4231.txt.pdf and /dev/null differ
diff --git a/tests/rfc5869.pdf b/tests/rfc5869.pdf
new file mode 100644 (file)
index 0000000..e1d082d
Binary files /dev/null and b/tests/rfc5869.pdf differ
diff --git a/tests/rfc5869.txt.pdf b/tests/rfc5869.txt.pdf
deleted file mode 100644 (file)
index e1d082d..0000000
Binary files a/tests/rfc5869.txt.pdf and /dev/null differ
index caf8a2fcfc32fef606fb5909e81710b95a4cf968..45765947a2107d0e5c04675486a09139eb0c863f 100644 (file)
@@ -30,6 +30,23 @@ void test_hmac(uint8_t grp, uint16_t id, uint8_t key[], uint16_t keyS, uint8_t d
     printf("%s HMAC-SHA256 #%i: PASS\n", grp == 1 ? lbl1 : lbl2, id);
 }
 
+void test_prf(uint16_t id, uint8_t* secret, uint32_t secretS, uint8_t* label, uint32_t labelS, uint8_t* seed, uint32_t seedS, uint8_t* result, uint32_t resultS)
+{
+    uint8_t* out = foleo_hmac_prf(FOLEO_SHA256, resultS, secret, secretS, label, labelS, seed, seedS);
+    for (uint16_t i = 0; i < resultS; i++)
+    {
+        if (out[i] != result[i])
+        {
+            printf("     JBP PRF-SHA256  #%i: FAILED\n", id);
+            test_pbuf("result", out, resultS);
+            free(out);
+            return;
+        }
+    }
+    free(out);
+    printf("     JBP PRF-SHA256  #%i: PASS\n", id);
+}
+
 void test_hkdf(uint16_t id, uint8_t* ikm, uint32_t ikmS, uint8_t* salt, uint32_t saltS, uint8_t* info, uint32_t infoS, uint8_t* result, uint32_t resultS)
 {
     uint8_t* out = foleo_hmac_hkdf(FOLEO_SHA256, resultS, ikm, ikmS, salt, saltS, info, infoS);
@@ -59,7 +76,9 @@ void main()
     test_hmac(2, 5, RFC4231_HMAC_key5, sizeof(RFC4231_HMAC_key5), RFC4231_HMAC_data5, sizeof(RFC4231_HMAC_data5), RFC4231_HMAC_result5, sizeof(RFC4231_HMAC_result5));
     test_hmac(2, 6, RFC4231_HMAC_key6, sizeof(RFC4231_HMAC_key6), RFC4231_HMAC_data6, sizeof(RFC4231_HMAC_data6), RFC4231_HMAC_result6, sizeof(RFC4231_HMAC_result6));
     test_hmac(2, 7, RFC4231_HMAC_key7, sizeof(RFC4231_HMAC_key7), RFC4231_HMAC_data7, sizeof(RFC4231_HMAC_data7), RFC4231_HMAC_result7, sizeof(RFC4231_HMAC_result7));
-//
+
+    test_prf(1, JBP_PRF_secret1, sizeof(JBP_PRF_secret1), JBP_PRF_label1, sizeof(JBP_PRF_label1), JBP_PRF_seed1, sizeof(JBP_PRF_label1), JBP_PRF_result1, sizeof(JBP_PRF_result1));
+
     test_hkdf(1, RFC5869_HKDF_ikm1, sizeof(RFC5869_HKDF_ikm1), RFC5869_HKDF_salt1, sizeof(RFC5869_HKDF_salt1), RFC5869_HKDF_info1, sizeof(RFC5869_HKDF_info1), RFC5869_HKDF_result1, sizeof(RFC5869_HKDF_result1));
     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));