diff --git a/tools/itssecurity/.vs/itssecurity/v16/.suo b/tools/itssecurity/.vs/itssecurity/v16/.suo new file mode 100644 index 0000000000000000000000000000000000000000..7f409fb25b385a6e1b04eafe48f26019710b25dd Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/.suo differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/Browse.VC.db b/tools/itssecurity/.vs/itssecurity/v16/Browse.VC.db new file mode 100644 index 0000000000000000000000000000000000000000..6bf3c4b719f1e3b4bacf0844bf30e6921a585c65 Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/Browse.VC.db differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/Solution.VC.db b/tools/itssecurity/.vs/itssecurity/v16/Solution.VC.db new file mode 100644 index 0000000000000000000000000000000000000000..9901f264032c2f16723ac3e1534a879dc00e0e10 Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/Solution.VC.db differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/3ae977cc1cf12c22.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/3ae977cc1cf12c22.ipch new file mode 100644 index 0000000000000000000000000000000000000000..d4784fabe27b91a43d6921d581da30bf9f45383a Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/3ae977cc1cf12c22.ipch differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/3f79d4a1bfc9156e.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/3f79d4a1bfc9156e.ipch new file mode 100644 index 0000000000000000000000000000000000000000..b3cfe5129e7c0218ab5fc3e221c2e4da7be340b4 Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/3f79d4a1bfc9156e.ipch differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/1ee3db2fc2bf098/COM_SPIRENT_ITS_SECURITY.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/1ee3db2fc2bf098/COM_SPIRENT_ITS_SECURITY.ipch new file mode 100644 index 0000000000000000000000000000000000000000..86ab7044261ead52564c1f44b325a4efcd21bb5b Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/1ee3db2fc2bf098/COM_SPIRENT_ITS_SECURITY.ipch differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/3946801aa34e60d3/COM_SPIRENT_ITS_SECURITY_NATIVESECURITY.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/3946801aa34e60d3/COM_SPIRENT_ITS_SECURITY_NATIVESECURITY.ipch new file mode 100644 index 0000000000000000000000000000000000000000..2d5aafaa3872d9465bb22153d4b2174d5150e629 Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/3946801aa34e60d3/COM_SPIRENT_ITS_SECURITY_NATIVESECURITY.ipch differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/3e166027e1674eab/LIB_ITS_SECURITY.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/3e166027e1674eab/LIB_ITS_SECURITY.ipch new file mode 100644 index 0000000000000000000000000000000000000000..40547f89cbada5e0647c38bb9c87afa336c55e68 Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/3e166027e1674eab/LIB_ITS_SECURITY.ipch differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/403d4330dabcb9cb/COM_SPIRENT_ITS_SECURITY.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/403d4330dabcb9cb/COM_SPIRENT_ITS_SECURITY.ipch new file mode 100644 index 0000000000000000000000000000000000000000..40645b8c3c294b3f9be72377ad2b8c6b48ab2cb7 Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/403d4330dabcb9cb/COM_SPIRENT_ITS_SECURITY.ipch differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/5f9167d1ee025d78/COM_SPIRENT_ITS_SECURITY.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/5f9167d1ee025d78/COM_SPIRENT_ITS_SECURITY.ipch new file mode 100644 index 0000000000000000000000000000000000000000..f29fb7376c8d53bf49078f246b141b62c3bc5c3f Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/5f9167d1ee025d78/COM_SPIRENT_ITS_SECURITY.ipch differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/afd2b246a4d1066a/TEST_LIB_ITS_SECURITY.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/afd2b246a4d1066a/TEST_LIB_ITS_SECURITY.ipch new file mode 100644 index 0000000000000000000000000000000000000000..f99e8106daf5fdd8ab2e0c440bfb4a674529a60b Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/afd2b246a4d1066a/TEST_LIB_ITS_SECURITY.ipch differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/bbf5104d398540e0/COM_SPIRENT_ITS_SECURITY_NATIVESECURITY.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/bbf5104d398540e0/COM_SPIRENT_ITS_SECURITY_NATIVESECURITY.ipch new file mode 100644 index 0000000000000000000000000000000000000000..3e930ef9c79ae9542f51e89da781ab5749b274aa Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/bbf5104d398540e0/COM_SPIRENT_ITS_SECURITY_NATIVESECURITY.ipch differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/fa071e5da418d69b/COM_SPIRENT_ITS_SECURITY.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/fa071e5da418d69b/COM_SPIRENT_ITS_SECURITY.ipch new file mode 100644 index 0000000000000000000000000000000000000000..504e35c4d72bee58e5ac46dd26cebceecea9dc49 Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/AutoPCH/fa071e5da418d69b/COM_SPIRENT_ITS_SECURITY.ipch differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/af6681a56a4499b8.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/af6681a56a4499b8.ipch new file mode 100644 index 0000000000000000000000000000000000000000..3b70e817cfc78d4f8bb62f2ca7d1f0c6950ab8ed Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/af6681a56a4499b8.ipch differ diff --git a/tools/itssecurity/.vs/itssecurity/v16/ipch/d686ced6b6a19201.ipch b/tools/itssecurity/.vs/itssecurity/v16/ipch/d686ced6b6a19201.ipch new file mode 100644 index 0000000000000000000000000000000000000000..a58e945cebeed1c1e558a45a4d398f3c3bdfb5c5 Binary files /dev/null and b/tools/itssecurity/.vs/itssecurity/v16/ipch/d686ced6b6a19201.ipch differ diff --git a/tools/itssecurity/com_spirent_its_security/com_spirent_its_security.vcxproj b/tools/itssecurity/com_spirent_its_security/com_spirent_its_security.vcxproj new file mode 100644 index 0000000000000000000000000000000000000000..3c493b33d06a2834067053ae62830d4e6fad7dff --- /dev/null +++ b/tools/itssecurity/com_spirent_its_security/com_spirent_its_security.vcxproj @@ -0,0 +1,161 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {A5E5C89A-7B89-443A-9972-E0145DBA3B7E} + Win32Proj + comspirentitssecurity + 10.0 + + + + StaticLibrary + true + v142 + Unicode + + + StaticLibrary + false + v142 + true + Unicode + + + StaticLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + false + + + true + + + true + + + false + + + + NotUsing + Level3 + true + true + true + _Win64;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + C:\Program Files\OpenSSL-Win64\include;C:\Program Files\Java\jdk1.8.0_241\include;C:\Program Files\Java\jdk1.8.0_241\include\win32;$(ProjectDir)../lib_its_security;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + lib_its_security.lib;libcrypto64MDd.lib;libssl64MDd.lib;Ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + $(ProjectDir)..\x64\Release;C:\Program Files\OpenSSL-Win64\lib\VC;C:\Program Files\Java\jre1.8.0_241\bin + + + + + Use + Level3 + true + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + + + + + Use + Level3 + true + _DEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + + + + + Use + Level3 + true + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/tools/itssecurity/com_spirent_its_security/com_spirent_its_security.vcxproj.user b/tools/itssecurity/com_spirent_its_security/com_spirent_its_security.vcxproj.user new file mode 100644 index 0000000000000000000000000000000000000000..88a550947edbc3c5003a41726f0749201fdb6822 --- /dev/null +++ b/tools/itssecurity/com_spirent_its_security/com_spirent_its_security.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/tools/itssecurity/com_spirent_its_security/com_spirent_its_security_NativeSecurity.c b/tools/itssecurity/com_spirent_its_security/com_spirent_its_security_NativeSecurity.c new file mode 100644 index 0000000000000000000000000000000000000000..dc4bcd53b0892d5bccb289295a3342c371154e80 --- /dev/null +++ b/tools/itssecurity/com_spirent_its_security/com_spirent_its_security_NativeSecurity.c @@ -0,0 +1,67 @@ +/* system headers */ +#include +#include +#include +#include +//add whatever is needed +#include +#include "com_spirent_its_security_NativeSecurity.h" + +#include "lib_its_security.h" + +//if you use other memory allocation function, please inform me! +#ifdef WIN32 +DLLAPI void* _tt_calloc(size_t, size_t); +#define calloc(NUM, SIZE) _tt_calloc(NUM, SIZE) +DLLAPI void _tt_free(void*); +#define free(PTR) _tt_free(PTR) +#else +#endif //WIN32 + +#ifdef WIN32 +void* _tt_calloc(size_t num, size_t elemSize) { +#if (_MSC_VER >= 1700) // newer than Visual Studio 2012 + size_t size = num * elemSize; + void* res = CoTaskMemAlloc(size); + memset(res, 0, size); + return res; +#else + return HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY, (num) * (elemSize)); +#endif +} + +void _tt_free(void* ptr) { +#if (_MSC_VER >= 1700) // newer than Visual Studio 2012 + CoTaskMemFree(ptr); +#else + HeapFree(GetProcessHeap(), 0, ptr); +#endif +} +#endif //WIN32 + +/* + * Class: com_spirent_its_security_NativeSecurity + * Method: hashWithSha256 + * Signature: ([B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_com_spirent_its_security_NativeSecurity_hashWithSha256(JNIEnv* env, jobject jobj, jbyteArray p_toBeHashedData) { + jbyteArray java_hashed_data; + int32_t result; + + // Extract buffers + size_t to_be_hashed_data_length = (*env)->GetArrayLength(env, p_toBeHashedData); + uint8_t* to_be_hashed_data = (uint8_t*)calloc(sizeof(uint8_t), to_be_hashed_data_length); + (*env)->GetByteArrayRegion(env, p_toBeHashedData, 0, (jsize)to_be_hashed_data_length, (jbyte*)to_be_hashed_data); + // Call lib_its_security implementation + uint8_t* hashed_data = NULL; + result = hash_with_sha256(to_be_hashed_data, to_be_hashed_data_length, &hashed_data); + // Prepare return value + java_hashed_data = (*env)->NewByteArray(env, (jsize)to_be_hashed_data_length); + if (result == 0) { + (*env)->SetByteArrayRegion(env, java_hashed_data, 0, (jsize)to_be_hashed_data_length, (jbyte*)hashed_data); + } + // Free allocated resources + free(hashed_data); + + return java_hashed_data; +} diff --git a/tools/itssecurity/com_spirent_its_security/com_spirent_its_security_NativeSecurity.h b/tools/itssecurity/com_spirent_its_security/com_spirent_its_security_NativeSecurity.h new file mode 100644 index 0000000000000000000000000000000000000000..ec5155d3e1453c2d19f68648ac314e54b9d740f4 --- /dev/null +++ b/tools/itssecurity/com_spirent_its_security/com_spirent_its_security_NativeSecurity.h @@ -0,0 +1,29 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_spirent_its_security_NativeSecurity */ + +#ifndef _Included_com_spirent_its_security +#define _Included_com_spirent_its_security +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_spirent_its_security_NativeSecurity + * Method: hashWithSha256 + * Signature: ([B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_com_spirent_its_security_NativeSecurity_hashWithSha256 + (JNIEnv *, jobject, jbyteArray); + +/* + * Class: com_spirent_its_security_NativeSecurity + * Method: hmac_sha256 + * Signature: ([B[B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_com_spirent_its_security_NativeSecurity_hmac_1sha256 + (JNIEnv *, jobject, jbyteArray, jbyteArray); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/tools/itssecurity/itssecurity.sln b/tools/itssecurity/itssecurity.sln new file mode 100644 index 0000000000000000000000000000000000000000..973e97725c3fa1e4ea0af5d96988e061b9a69a16 --- /dev/null +++ b/tools/itssecurity/itssecurity.sln @@ -0,0 +1,49 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29728.190 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lib_its_security", "lib_its_security\lib_its_security.vcxproj", "{76A8854A-3F0C-4124-9AE0-2578B1D59342}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lib_its_security_tests_unit", "test\lib_its_security_tests_unit.vcxproj", "{5151573E-B7B3-44B4-BA0B-5410831C3D9D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {76A8854A-3F0C-4124-9AE0-2578B1D59342}.Debug|x64.ActiveCfg = Debug|x64 + {76A8854A-3F0C-4124-9AE0-2578B1D59342}.Debug|x64.Build.0 = Debug|x64 + {76A8854A-3F0C-4124-9AE0-2578B1D59342}.Debug|x86.ActiveCfg = Debug|Win32 + {76A8854A-3F0C-4124-9AE0-2578B1D59342}.Debug|x86.Build.0 = Debug|Win32 + {76A8854A-3F0C-4124-9AE0-2578B1D59342}.Release|x64.ActiveCfg = Release|x64 + {76A8854A-3F0C-4124-9AE0-2578B1D59342}.Release|x64.Build.0 = Release|x64 + {76A8854A-3F0C-4124-9AE0-2578B1D59342}.Release|x86.ActiveCfg = Release|Win32 + {76A8854A-3F0C-4124-9AE0-2578B1D59342}.Release|x86.Build.0 = Release|Win32 + {5151573E-B7B3-44B4-BA0B-5410831C3D9D}.Debug|x64.ActiveCfg = Debug|x64 + {5151573E-B7B3-44B4-BA0B-5410831C3D9D}.Debug|x64.Build.0 = Debug|x64 + {5151573E-B7B3-44B4-BA0B-5410831C3D9D}.Debug|x86.ActiveCfg = Debug|Win32 + {5151573E-B7B3-44B4-BA0B-5410831C3D9D}.Debug|x86.Build.0 = Debug|Win32 + {5151573E-B7B3-44B4-BA0B-5410831C3D9D}.Release|x64.ActiveCfg = Release|x64 + {5151573E-B7B3-44B4-BA0B-5410831C3D9D}.Release|x64.Build.0 = Release|x64 + {5151573E-B7B3-44B4-BA0B-5410831C3D9D}.Release|x86.ActiveCfg = Release|Win32 + {5151573E-B7B3-44B4-BA0B-5410831C3D9D}.Release|x86.Build.0 = Release|Win32 + {A5E5C89A-7B89-443A-9972-E0145DBA3B7E}.Debug|x64.ActiveCfg = Debug|x64 + {A5E5C89A-7B89-443A-9972-E0145DBA3B7E}.Debug|x64.Build.0 = Debug|x64 + {A5E5C89A-7B89-443A-9972-E0145DBA3B7E}.Debug|x86.ActiveCfg = Debug|Win32 + {A5E5C89A-7B89-443A-9972-E0145DBA3B7E}.Debug|x86.Build.0 = Debug|Win32 + {A5E5C89A-7B89-443A-9972-E0145DBA3B7E}.Release|x64.ActiveCfg = Release|x64 + {A5E5C89A-7B89-443A-9972-E0145DBA3B7E}.Release|x64.Build.0 = Release|x64 + {A5E5C89A-7B89-443A-9972-E0145DBA3B7E}.Release|x86.ActiveCfg = Release|Win32 + {A5E5C89A-7B89-443A-9972-E0145DBA3B7E}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {CE997533-FD4F-42FF-A3AE-6D3E2AFBCDA8} + EndGlobalSection +EndGlobal diff --git a/tools/itssecurity/javasrc/com/spirent/its/security/JniTest.class b/tools/itssecurity/javasrc/com/spirent/its/security/JniTest.class new file mode 100644 index 0000000000000000000000000000000000000000..a573bf32610129a8fff20b3d6d038e05c2d9df49 Binary files /dev/null and b/tools/itssecurity/javasrc/com/spirent/its/security/JniTest.class differ diff --git a/tools/itssecurity/javasrc/com/spirent/its/security/JniTest.java b/tools/itssecurity/javasrc/com/spirent/its/security/JniTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9e7f35ad55c12dc414c60920778a76a3dcdecf1b --- /dev/null +++ b/tools/itssecurity/javasrc/com/spirent/its/security/JniTest.java @@ -0,0 +1,20 @@ +package com.spirent.its.security; + +class JniTest +{ + static { + System.loadLibrary("com_spirent_its_security.dll"); + } + + public static void main(String args[]) + { + System.out.println("Hello, World"); + byte[] toBeHashed = new byte[] { (byte)0xca, (byte)0xfe, (byte)0xde, (byte)0xca }; + byte[] hashedData = NativeSecurity.getInstance().hashWithSha256(toBeHashed); + if (hashedData == null) { + System.err.println("Failed"); + } else { + System.out.println("Succeed"); + } + } +} diff --git a/tools/itssecurity/javasrc/com/spirent/its/security/NativeSecurity.class b/tools/itssecurity/javasrc/com/spirent/its/security/NativeSecurity.class new file mode 100644 index 0000000000000000000000000000000000000000..c7a33cdbf63de0c8ef68814e132ab82ddb592010 Binary files /dev/null and b/tools/itssecurity/javasrc/com/spirent/its/security/NativeSecurity.class differ diff --git a/tools/itssecurity/javasrc/com/spirent/its/security/NativeSecurity.java b/tools/itssecurity/javasrc/com/spirent/its/security/NativeSecurity.java new file mode 100644 index 0000000000000000000000000000000000000000..54da390efe63c3592f214d162695221f8aa4f47e --- /dev/null +++ b/tools/itssecurity/javasrc/com/spirent/its/security/NativeSecurity.java @@ -0,0 +1,14 @@ +package com.spirent.its.security;; + +class NativeSecurity +{ + private static final NativeSecurity _instance = new NativeSecurity(); + + private NativeSecurity() { }; + + public native byte[] hashWithSha256(byte[] p_toBeHashedData); + + public static NativeSecurity getInstance() { return _instance; }; + +} + diff --git a/tools/itssecurity/javasrc/hello.jar b/tools/itssecurity/javasrc/hello.jar new file mode 100644 index 0000000000000000000000000000000000000000..88eff170ae882758a338dd753f20ce81edae48a1 Binary files /dev/null and b/tools/itssecurity/javasrc/hello.jar differ diff --git a/tools/itssecurity/lib_its_security/lib_its_security.c b/tools/itssecurity/lib_its_security/lib_its_security.c index 2f2ad578f204ec3da4ea3b8ba5fc8f3057c3224f..97cece158f0fb49a1afa7e19c73cbddf158be295 100644 --- a/tools/itssecurity/lib_its_security/lib_its_security.c +++ b/tools/itssecurity/lib_its_security/lib_its_security.c @@ -13,7 +13,14 @@ #include #include +#ifndef _Win64 #include +#else +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers + // Windows Header Files +#include +#include +#endif #include @@ -41,7 +48,9 @@ void show_ec_point(const int8_t* p_prefix, lib_its_security_context_t* p_lib_its char* result = EC_POINT_point2hex(p_lib_its_security_context->ec_group, p_ec_point, POINT_CONVERSION_UNCOMPRESSED, p_lib_its_security_context->bn_ctx); if (result != NULL) { fprintf(stderr, "%s\n", result); +#ifdef _Win64 free(result); +#endif } else { fprintf(stderr, "(null)\n"); } @@ -239,7 +248,7 @@ int kdf2_sha256( uint8_t* h; hash_with_sha256(hash_input, hash_input_length, &h); //show_hex((const int8_t*)"h", (const void*)h, 32); - memcpy((void*)digest + digest_idx, (const void*)h, sha256_blk_len); + memcpy((void*)(digest + digest_idx), (const void*)h, sha256_blk_len); //show_hex((const int8_t*)"digest", (const void*)digest, digest_idx + sha256_blk_len); digest_idx += sha256_blk_len; free(h); @@ -545,7 +554,7 @@ int32_t generate_and_derive_ephemeral_key_for_decryption( memcpy((void*)k1, (const void*)digest, k_enc); show_hex((const int8_t*)"k1", k1, k_enc); p_lib_its_security_context->sym_key = (uint8_t*)malloc(k_enc); - for (int i = 0; i < k_enc; *(p_lib_its_security_context->sym_key + i) = *(k1 + i) ^ *(p_lib_its_security_context->enc_sym_key + i), i++); + for (unsigned int i = 0; i < k_enc; *(p_lib_its_security_context->sym_key + i) = *(k1 + i) ^ *(p_lib_its_security_context->enc_sym_key + i), i++); show_hex((const int8_t*)"sym_key", p_lib_its_security_context->sym_key, p_lib_its_security_context->sym_key_length); free(k1); free(k2); @@ -1038,14 +1047,18 @@ int32_t generic_verify( return -1; } - uint8_t sig_r[p_lib_its_security_context->key_length]; + uint8_t* sig_r = (uint8_t*)malloc(p_lib_its_security_context->key_length); memcpy((void*)sig_r, (const void*)p_signature, p_lib_its_security_context->key_length); - uint8_t sig_s[p_lib_its_security_context->key_length]; + uint8_t* sig_s = (uint8_t*)malloc(p_lib_its_security_context->key_length); memcpy((void*)sig_s, (const void*)(p_signature + p_lib_its_security_context->key_length), p_lib_its_security_context->key_length); if (sign_verify(p_lib_its_security_context, hashed_data, p_lib_its_security_context->key_length, sig_r, sig_s, p_lib_its_security_context->key_length) == -1) { + free(sig_r); + free(sig_s); free(hashed_data); return -1; } + free(sig_r); + free(sig_s); free(hashed_data); return 0; diff --git a/tools/itssecurity/lib_its_security/lib_its_security.h b/tools/itssecurity/lib_its_security/lib_its_security.h index 5eb2bd792a0bf795aba885b9e8358cf98dfaa7c0..2b4f4dde94315d374d45647ab58efcae38f4eba8 100644 --- a/tools/itssecurity/lib_its_security/lib_its_security.h +++ b/tools/itssecurity/lib_its_security/lib_its_security.h @@ -10,7 +10,23 @@ */ #pragma once +#ifdef _Win64 +#ifdef LIBITSSECURITY_EXPORTS +#define LIBITSSECURITY_API __declspec(dllexport) +#else +#define LIBITSSECURITY_API __declspec(dllimport) +#endif +#else // _Win64 +#define LIBITSSECURITY_API +#endif // _Win64 + +#ifdef __cplusplus +extern "C" { +#endif // !__cplusplus + +#ifndef _Win64 #include +#endif // !_Win32 #include #include #include @@ -88,7 +104,7 @@ typedef struct lib_its_security_context_ { * \param[out] p_lib_its_security_context The internl context (To be released using uninitialize function) * \return 0 on success,-1 otherwise */ -int32_t initialize(const ecc_elliptic_curves_t p_elliptic_curve, lib_its_security_context_t** p_lib_its_security_context); +LIBITSSECURITY_API int32_t initialize(const ecc_elliptic_curves_t p_elliptic_curve, lib_its_security_context_t** p_lib_its_security_context); /** * \fn int32_t uninitialize(lib_its_security_context_t** p_lib_its_security_context); @@ -96,7 +112,7 @@ int32_t initialize(const ecc_elliptic_curves_t p_elliptic_curve, lib_its_securit * \param[in/out] p_lib_its_security_context The internl context * \return 0 on success,-1 otherwise */ -int32_t uninitialize(lib_its_security_context_t** p_lib_its_security_context); +LIBITSSECURITY_API int32_t uninitialize(lib_its_security_context_t** p_lib_its_security_context); /** * \fn int32_t hash_with_sha256(const uint8_t* p_to_be_hashed_data,const size_t p_to_be_hashed_data_length,uint8_t** p_hashed_data); @@ -106,7 +122,7 @@ int32_t uninitialize(lib_its_security_context_t** p_lib_its_security_context); * \param[in/out] p_hashed_data The data to be used to calculate the hash value * \return 0 on success,-1 otherwise */ -int32_t hash_with_sha256(const uint8_t* p_to_be_hashed_data, const size_t p_to_be_hashed_data_length, uint8_t** p_hashed_data); +LIBITSSECURITY_API int32_t hash_with_sha256(const uint8_t* p_to_be_hashed_data, const size_t p_to_be_hashed_data_length, uint8_t** p_hashed_data); /** * \fn int32_t hash_with_sha384(const uint8_t* p_to_be_hashed_data,const size_t p_to_be_hashed_data_length,uint8_t** p_hashed_data); * \brief Produces a 384-bit (48-bytes) hash value @@ -115,7 +131,7 @@ int32_t hash_with_sha256(const uint8_t* p_to_be_hashed_data, const size_t p_to_b * \param[in] p_hashed_data The data to be used to calculate the hash value * \return 0 on success,-1 otherwise */ -int32_t hash_with_sha384(const uint8_t* p_to_be_hashed_data, const size_t p_to_be_hashed_data_length, uint8_t** p_hashed_data); +LIBITSSECURITY_API int32_t hash_with_sha384(const uint8_t* p_to_be_hashed_data, const size_t p_to_be_hashed_data_length, uint8_t** p_hashed_data); /** * \fn int32_t hmac_sha256(const uint8_t* p_secret_key,const size p_secret_key_length,const OCTETSTRING& p_message,const size p_message_length, uint8_t** p_hmac); @@ -125,7 +141,7 @@ int32_t hash_with_sha384(const uint8_t* p_to_be_hashed_data, const size_t p_to_b * \param[out] p_hmac The HMAC with SHA256 of the message resized to 16-bytes (To be released after use) * \return 0 on success,-1 otherwise */ -int32_t hmac_sha256(const uint8_t* p_secret_key, const size_t p_secret_key_length, const uint8_t* p_message, const size_t p_message_length, uint8_t** p_hmac); +LIBITSSECURITY_API int32_t hmac_sha256(const uint8_t* p_secret_key, const size_t p_secret_key_length, const uint8_t* p_message, const size_t p_message_length, uint8_t** p_hmac); /** * \fn int32_t sign_with_ecdsa_nistp256_with_sha256(lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_signed_secured_message,const size_t p_to_be_signed_secured_message_length,const uint8_t* p_certificate_issuer,const uint8_t* p_private_key,uint8_t** p_signature); @@ -137,7 +153,7 @@ int32_t hmac_sha256(const uint8_t* p_secret_key, const size_t p_secret_key_lengt * \param[out] p_signature The signature of the data to be signed (To be released after use) * \return The signature value */ -int32_t sign_with_ecdsa_nistp256_with_sha256( +LIBITSSECURITY_API int32_t sign_with_ecdsa_nistp256_with_sha256( lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_signed_secured_message, const size_t p_to_be_signed_secured_message_length, @@ -156,7 +172,7 @@ int32_t sign_with_ecdsa_nistp256_with_sha256( * \param[out] p_signature The signature of the data to be signed (To be released after use) * \return The signature value */ -int32_t sign_with_ecdsa_brainpoolp256r1_with_sha256( +LIBITSSECURITY_API int32_t sign_with_ecdsa_brainpoolp256r1_with_sha256( lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_signed_secured_message, const size_t p_to_be_signed_secured_message_length, @@ -175,7 +191,7 @@ int32_t sign_with_ecdsa_brainpoolp256r1_with_sha256( * \param[out] p_signature The signature of the data to be signed (To be released after use) * \return 0 on success,-1 otherwise */ -int32_t sign_with_ecdsa_brainpoolp384r1_with_sha384( +LIBITSSECURITY_API int32_t sign_with_ecdsa_brainpoolp384r1_with_sha384( lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_signed_secured_message, const size_t p_to_be_signed_secured_message_length, @@ -194,7 +210,7 @@ int32_t sign_with_ecdsa_brainpoolp384r1_with_sha384( * \param[in] p_ecdsa_nistp256_publicKey_compressed The compressed public key (x coordinate only) * \return 0 on success,-1 otherwise */ -int32_t verify_with_ecdsa_nistp256_with_sha256( +LIBITSSECURITY_API int32_t verify_with_ecdsa_nistp256_with_sha256( lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_verified_data, const size_t p_to_be_verified_data_length, @@ -213,7 +229,7 @@ int32_t verify_with_ecdsa_nistp256_with_sha256( * \param[in] p_ecdsa_nistp256_publicKey_compressed The compressed public key (x coordinate only) * \return 0 on success,-1 otherwise */ -int32_t verify_with_ecdsa_nistp256_with_sha256_raw( +LIBITSSECURITY_API int32_t verify_with_ecdsa_nistp256_with_sha256_raw( lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_verified_data, const size_t p_to_be_verified_data_length, @@ -232,7 +248,7 @@ int32_t verify_with_ecdsa_nistp256_with_sha256_raw( * \param[in] p_ecdsaBrainpoolp256PublicKeyCompressed The compressed public key (x coordinate only) * \return 0 on success,-1 otherwise */ -int32_t verify_with_ecdsa_brainpoolp256r1_with_sha256( +LIBITSSECURITY_API int32_t verify_with_ecdsa_brainpoolp256r1_with_sha256( lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_verified_data, const size_t p_to_be_verified_data_length, @@ -252,7 +268,7 @@ int32_t verify_with_ecdsa_brainpoolp256r1_with_sha256( * \param[in] p_ecdsaBrainpoolp384PublicKeyCompressed The compressed public key (x coordinate only) * \return 0 on success,-1 otherwise */ -int32_t verify_with_ecdsa_brainpoolp384r1_with_sha384( +LIBITSSECURITY_API int32_t verify_with_ecdsa_brainpoolp384r1_with_sha384( lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_verified_data, const size_t p_to_be_verified_data_length, @@ -280,7 +296,7 @@ int32_t verify_with_ecdsa_brainpoolp384r1_with_sha384( * \see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/ * \see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf */ -int32_t encrypt_with_ecies_nistp256_with_sha256( +LIBITSSECURITY_API int32_t encrypt_with_ecies_nistp256_with_sha256( lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_encrypted_secured_message, const size_t p_to_be_encrypted_secured_message_length, @@ -313,7 +329,7 @@ int32_t encrypt_with_ecies_nistp256_with_sha256( * \see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/ * \see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf */ -int32_t decrypt_with_ecies_nistp256_with_sha256( +LIBITSSECURITY_API int32_t decrypt_with_ecies_nistp256_with_sha256( lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_encrypted_secured_message, const size_t p_encrypted_secured_message_length, @@ -330,7 +346,7 @@ int32_t decrypt_with_ecies_nistp256_with_sha256( size_t* p_plain_text_message_length ); -int32_t encrypt_with_ecies_brainpoolp256r1_with_sha256( +LIBITSSECURITY_API int32_t encrypt_with_ecies_brainpoolp256r1_with_sha256( lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_to_be_encrypted_secured_message, const size_t p_to_be_encrypted_secured_message_length, @@ -348,7 +364,7 @@ int32_t encrypt_with_ecies_brainpoolp256r1_with_sha256( size_t* p_encrypted_secured_message_length ); -int32_t decrypt_with_ecies_brainpoolp256r1_with_sha256( +LIBITSSECURITY_API int32_t decrypt_with_ecies_brainpoolp256r1_with_sha256( lib_its_security_context_t* p_lib_its_security_context, const uint8_t* p_encrypted_secured_message, const size_t p_encrypted_secured_message_length, @@ -374,7 +390,7 @@ int32_t decrypt_with_ecies_brainpoolp256r1_with_sha256( * \param[out] p_public_key_x The new public key value (y coordinate) (To be released after use) * \return 0 on success,-1 otherwise */ -int32_t generate_key_pair( +LIBITSSECURITY_API int32_t generate_key_pair( lib_its_security_context_t* p_lib_its_security_context, uint8_t** p_private_key, uint8_t** p_public_key_x, @@ -382,3 +398,7 @@ int32_t generate_key_pair( uint8_t** p_public_key_compressed, ecc_compressed_mode_t* p_compressed_mode ); + +#ifdef __cplusplus +} +#endif // !__cplusplus diff --git a/tools/itssecurity/lib_its_security/lib_its_security.vcxproj b/tools/itssecurity/lib_its_security/lib_its_security.vcxproj new file mode 100644 index 0000000000000000000000000000000000000000..5ea7b624b9e0c532f4d8d82e91db8424a9ddb3f5 --- /dev/null +++ b/tools/itssecurity/lib_its_security/lib_its_security.vcxproj @@ -0,0 +1,172 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {76A8854A-3F0C-4124-9AE0-2578B1D59342} + Win32Proj + libitssecurity + 10.0 + + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + lib_its_security + true + + + LIBITSSECURITY + true + + + LIBITSSECURITY + false + + + lib_its_security + false + + + + Use + Level3 + true + _DEBUG;LIBITSSECURITY_EXPORTS;_WINDOWS;_USRDLL;_Win64;%(PreprocessorDefinitions) + true + pch.h + C:\Program Files\OpenSSL-Win64\include + + + Windows + true + false + C:\Program Files\OpenSSL-Win64\lib\VC + libcrypto64MDd.lib;libssl64MDd.lib;Ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + Use + Level3 + true + WIN32;_DEBUG;LIBITSSECURITY_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + false + + + + + Use + Level3 + true + true + true + WIN32;NDEBUG;LIBITSSECURITY_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + true + true + false + + + + + NotUsing + Level3 + true + true + true + _Win64;NDEBUG;LIBITSSECURITY_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + pch.h + C:\Program Files\OpenSSL-Win64\include + + + Windows + true + true + true + false + C:\Program Files\OpenSSL-Win64\lib\VC + libcrypto64MDd.lib;libssl64MDd.lib;Ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + + + + + + + + \ No newline at end of file diff --git a/tools/itssecurity/lib_its_security/lib_its_security.vcxproj.user b/tools/itssecurity/lib_its_security/lib_its_security.vcxproj.user new file mode 100644 index 0000000000000000000000000000000000000000..88a550947edbc3c5003a41726f0749201fdb6822 --- /dev/null +++ b/tools/itssecurity/lib_its_security/lib_its_security.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/tools/itssecurity/test/lib_its_security_tests_unit.vcxproj b/tools/itssecurity/test/lib_its_security_tests_unit.vcxproj new file mode 100644 index 0000000000000000000000000000000000000000..cd0af22cc58f0c7d1d793452fb85f2cf48178524 --- /dev/null +++ b/tools/itssecurity/test/lib_its_security_tests_unit.vcxproj @@ -0,0 +1,126 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {5151573e-b7b3-44b4-ba0b-5410831c3d9d} + Win32Proj + 10.0.18362.0 + Application + v142 + Unicode + + + + + + + + + + + + + {76a8854a-3f0c-4124-9ae0-2578b1d59342} + + + + + + + + + + + + + Use + pch.h + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + EnableFastChecks + MultiThreadedDebugDLL + Level3 + + + true + Console + + + + + Use + pch.h + Disabled + _Win64;X64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + EnableFastChecks + MultiThreadedDebugDLL + Level3 + C:\Users\yanng\source\repos\TestOpenSSL\lib_its_security;C:\Program Files\OpenSSL-Win64\include;$(MSBuildThisFileDirectory)include;%(AdditionalIncludeDirectories) + + + true + Console + C:\Users\yanng\source\repos\TestOpenSSL\x64\Debug;C:\Program Files\OpenSSL-Win64\lib\VC + lib_its_security.lib;libcrypto64MDd.lib;libssl64MDd.lib;Ws2_32.lib;%(AdditionalDependencies) + $(OutDir)$(TargetName).pdb + + + + + Use + pch.h + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + ProgramDatabase + + + true + Console + true + true + + + + + NotUsing + pch.h + _Win64;X64;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + ProgramDatabase + $(ProjectDir)..\lib_its_security;C:\Program Files\OpenSSL-Win64\include;$(MSBuildThisFileDirectory)include;%(AdditionalIncludeDirectories) + + + true + Console + true + true + $(ProjectDir)..\x64\Release;C:\Program Files\OpenSSL-Win64\lib\VC + lib_its_security.lib;libcrypto64MDd.lib;libssl64MDd.lib;Ws2_32.lib;%(AdditionalDependencies) + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + \ No newline at end of file diff --git a/tools/itssecurity/test/lib_its_security_tests_unit.vcxproj.user b/tools/itssecurity/test/lib_its_security_tests_unit.vcxproj.user new file mode 100644 index 0000000000000000000000000000000000000000..88a550947edbc3c5003a41726f0749201fdb6822 --- /dev/null +++ b/tools/itssecurity/test/lib_its_security_tests_unit.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/tools/itssecurity/test/packages.config b/tools/itssecurity/test/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..287edf7fe6ca5c9fc97c3840470c73f51b6db900 --- /dev/null +++ b/tools/itssecurity/test/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/tools/itssecurity/test/test_lib_its_security.cc b/tools/itssecurity/test/test_lib_its_security.cc index d13d5c2c5de3b750ad39e1c73f4844b5b26301df..ce079dc2bf45bb460395d7d7202eb999117cd4c4 100644 --- a/tools/itssecurity/test/test_lib_its_security.cc +++ b/tools/itssecurity/test/test_lib_its_security.cc @@ -1,24 +1,65 @@ -#ifdef _Win64 -#include "pch.h" -#else -#include -#endif +#include "gtest/gtest.h" extern "C" { #include "lib_its_security.h" -int8_t* bin_to_hex(const uint8_t* p_buffer, const size_t p_buffer_length); +#ifndef _Win64 uint8_t* hex_to_bin(const int8_t* p_buffer, size_t* p_buffer_length); +#endif +} + +#ifdef _Win64 +uint8_t* hex_to_bin(const int8_t* p_buffer, size_t* p_buffer_length) { + int8_t a; + size_t i, len; + uint8_t* retval = NULL; + + // Sanity check + if (p_buffer == NULL) { + return NULL; + } + if ((len = strlen((const char*)p_buffer)) & 1) { + return NULL; } + retval = (uint8_t*)malloc(len >> 1); + for (i = 0; i < len; i++) { + a = toupper(*(p_buffer + i)); + if (!isxdigit(a)) { + break; + } + if (isdigit(a)) { + a -= '0'; + } + else { + a = a - 'A' + 0x0A; + } + if (i & 1) { + retval[i >> 1] |= a; + } + else { + retval[i >> 1] = a << 4; + } + } // End of 'for' statement + if (i < len) { + free(retval); + retval = NULL; + } + *p_buffer_length = len >> 1; + + return retval; +} +#endif +#ifndef _Win64 /** * @class lib_its_security unit tests suite implementation */ class lib_its_security_test_suite : public ::testing::Test { protected: - virtual void SetUp() { }; - virtual void TearDown() { }; + virtual void SetUp() { }; + virtual void TearDown() { }; }; +#endif TEST(lib_its_security_test_suite, Init1) { EXPECT_TRUE(initialize(nist_p_256, NULL) == -1); @@ -1222,5 +1263,3 @@ int main(int argc, char **argv) { return RUN_ALL_TESTS(); } - -