Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • car/libdap-crypto
1 result
Show changes
Commits on Source (10)
Showing
with 1585 additions and 308 deletions
cmake_minimum_required(VERSION 3.0)
project (dap_crypto)
# fix implicit declaration warnings
add_definitions ("-D_GNU_SOURCE")
#set(CMAKE_C_FLAGS "-std=c11 -O2 -Wall -Wextra -fPIC -fno-pie -no-pie -fno-ident ")
#set(CMAKE_ASM_FLAGS "-std=c11 -O2 -Wall -Wextra -fPIC -fno-pie -no-pie -fno-ident ")
#set(CMAKE_C_FLAGS "-std=c11 -O3 -Wall -Wextra -fPIC -fno-pie -no-pie -fno-ident -ffast-math -ftree-vectorize -mfpmath=sse -mmmx -msse2 -fno-asynchronous-unwind-tables -ffunction-sections -Wl,--gc-sections -Wl,--strip-all")
#set(CMAKE_ASM_FLAGS "-std=c11 -O3 -Wall -Wextra -fPIC -fno-pie -no-pie -fno-ident -ffast-math -ftree-vectorize -mfpmath=sse -mmmx -msse2 -fno-asynchronous-unwind-tables -ffunction-sections -Wl,--gc-sections -Wl,--strip-all")
#set(CMAKE_C_FLAGS "-static -Wall -O3 -fno-ident -ffast-math -ftree-vectorize -mfpmath=sse -mmmx -msse2 -fno-asynchronous-unwind-tables -ffunction-sections -Wl,--gc-sections -Wl,--strip-all")
#set(CMAKE_ASM_FLAGS "-static -Wall -O3 -fno-ident -ffast-math -ftree-vectorize -mfpmath=sse -mmmx -msse2 -fno-asynchronous-unwind-tables -ffunction-sections -Wl,--gc-sections -Wl,--strip-all")
if(NOT (${SUBMODULES_NO_BUILD} MATCHES ON))
# Check whether we're on a 32-bit or 64-bit system
if(CMAKE_SIZEOF_VOID_P EQUAL "8")
set(DEFAULT_BUILD_64 ON)
else()
set(DEFAULT_BUILD_64 OFF)
endif()
option(BUILD_64 "Build for 64-bit? 'OFF' builds for 32-bit." ${DEFAULT_BUILD_64})
add_definitions ("-DDAP_SERVER")
add_definitions ("-DNODE_NETNAME=\"kelvin\"")
set(_CCOPT "-Wall -O2 -pg -fPIC -fno-pie -no-pie")
set(_LOPT "-pg")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_CCOPT}")
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${_LOPT}")
if (NOT (TARGET dap_core))
add_subdirectory(libdap)
endif()
endif()
file( GLOB CRYPTO_SRCS
src/*.c
src/rand/*.c
src/iaes/*.c
src/oaes/*.c
src/sha3/*.c
src/msrln/*.c
src/defeo_scheme/*.c
src/sig_bliss/*.c
src/sig_tesla/*.c
src/sig_picnic/*.c
src/sig_dilithium/*.c
)
file( GLOB CRYPTO_HEADERS
src/*.h
src/rand/*.h
src/iaes/*.h
src/oaes/*.h
src/sha3/*.h
src/msrln/*.h
src/defeo_scheme/*.h
src/sig_bliss/*.h
src/sig_tesla/*.h
src/sig_picnic/*.h
src/sig_dilithium/*.h
include/*.h
)
enable_language(ASM)
file( GLOB XKCP_SRCS
src/XKCP/lib/high/Keccak/FIPS202/KeccakHash.c
src/XKCP/lib/high/Keccak/KeccakSpongeWidth1600.c
src/XKCP/lib/high/Keccak/SP800-185/SP800-185.c
src/XKCP/lib/high/Keccak/FIPS202/SimpleFIPS202.c
)
if(WIN32)
if(BUILD_64)
#file( GLOB XKCP_SRCS2 src/XKCP/lib/low/KeccakP-1600/OptimizedAsmX86-64/KeccakP-1600-x86-64-gas.s ) - SIGSEGV on MinGW
file( GLOB XKCP_SRCS2 src/XKCP/lib/low/KeccakP-1600/Compact64/KeccakP-1600-compact64.c )
else()
file( GLOB XKCP_SRCS2 src/XKCP/lib/low/KeccakP-1600/Inplace32BI/KeccakP-1600-inplace32BI.c )
endif()
endif()
if(UNIX)
if(BUILD_64)
file( GLOB XKCP_SRCS2 src/XKCP/lib/low/KeccakP-1600/OptimizedAsmX86-64/KeccakP-1600-x86-64-gas.s )
else()
file( GLOB XKCP_SRCS2 src/XKCP/lib/low/KeccakP-1600/Inplace32BI/KeccakP-1600-inplace32BI.c )
endif()
endif()
add_library(${PROJECT_NAME} STATIC ${CRYPTO_SRCS} ${XKCP_SRCS} ${XKCP_SRCS2} ${CRYPTO_HEADERS} )
target_include_directories(dap_crypto PRIVATE src/rand src/iaes src/oaes sha3 src/msrln src/defeo_scheme src/sig_bliss src/sig_tesla src/sig_picnic src/sig_dilithium src include)
target_include_directories(dap_crypto INTERFACE src/ src/sha3 include/)
target_include_directories(dap_crypto PUBLIC
src/XKCP/lib/common
src/XKCP/lib/low/common
src/XKCP/lib/high/Keccak
src/XKCP/lib/high/common
src/XKCP/lib/high/Keccak/FIPS202
src/XKCP/lib/high/Keccak/SP800-185
src/XKCP/lib/low/KeccakP-1600/Optimized
)
if(WIN32)
if(BUILD_64)
#target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600/OptimizedAsmX86-64)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600/Compact64)
if (__AVX__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
elseif(__AVX2__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
else()
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
endif()
else()
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600/Inplace32BI)
if (__AVX__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
elseif(__AVX2__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
else()
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
endif()
endif()
endif()
if(UNIX)
if(BUILD_64)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600/OptimizedAsmX86-64)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600/OptimizedAsmX86-64)
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
endif()
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
elseif(__AVX__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
endif()
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
elseif(__AVX__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
endif()
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600/Inplace32BI)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600/Inplace32BI)
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
endif()
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
elseif(__AVX__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
endif()
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
elseif(__AVX__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
endif()
endif()
endif()
if(WIN32)
target_link_libraries(dap_crypto dap_core)
endif()
if(UNIX)
target_link_libraries(dap_crypto dap_core m)
endif()
if(BUILD_CRYPTO_TESTS)
enable_testing()
add_subdirectory(test)
endif()
cmake_minimum_required(VERSION 3.0)
project (dap_crypto)
# fix implicit declaration warnings
add_definitions ("-D_GNU_SOURCE")
#set(CMAKE_C_FLAGS "-std=c11 -O2 -Wall -Wextra -fPIC -fno-pie -no-pie -fno-ident ")
#set(CMAKE_ASM_FLAGS "-std=c11 -O2 -Wall -Wextra -fPIC -fno-pie -no-pie -fno-ident ")
#set(CMAKE_C_FLAGS "-std=c11 -O3 -Wall -Wextra -fPIC -fno-pie -no-pie -fno-ident -ffast-math -ftree-vectorize -mfpmath=sse -mmmx -msse2 -fno-asynchronous-unwind-tables -ffunction-sections -Wl,--gc-sections -Wl,--strip-all")
#set(CMAKE_ASM_FLAGS "-std=c11 -O3 -Wall -Wextra -fPIC -fno-pie -no-pie -fno-ident -ffast-math -ftree-vectorize -mfpmath=sse -mmmx -msse2 -fno-asynchronous-unwind-tables -ffunction-sections -Wl,--gc-sections -Wl,--strip-all")
#set(CMAKE_C_FLAGS "-static -Wall -O3 -fno-ident -ffast-math -ftree-vectorize -mfpmath=sse -mmmx -msse2 -fno-asynchronous-unwind-tables -ffunction-sections -Wl,--gc-sections -Wl,--strip-all")
#set(CMAKE_ASM_FLAGS "-static -Wall -O3 -fno-ident -ffast-math -ftree-vectorize -mfpmath=sse -mmmx -msse2 -fno-asynchronous-unwind-tables -ffunction-sections -Wl,--gc-sections -Wl,--strip-all")
if(NOT (${SUBMODULES_NO_BUILD} MATCHES ON))
# Check whether we're on a 32-bit or 64-bit system
if(CMAKE_SIZEOF_VOID_P EQUAL "8")
set(DEFAULT_BUILD_64 ON)
else()
set(DEFAULT_BUILD_64 OFF)
endif()
option(BUILD_64 "Build for 64-bit? 'OFF' builds for 32-bit." ${DEFAULT_BUILD_64})
add_definitions ("-DDAP_SERVER")
add_definitions ("-DNODE_NETNAME=\"cellframe\"")
set(_CCOPT "-Wall -O2 -pg -fPIC -fno-pie -no-pie")
set(_LOPT "-pg")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_CCOPT}")
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${_LOPT}")
if (NOT (TARGET dap_core))
add_subdirectory(libdap)
endif()
endif()
file( GLOB CRYPTO_SRCS
src/*.c
src/rand/*.c
src/iaes/*.c
src/oaes/*.c
src/sha3/*.c
src/msrln/*.c
src/defeo_scheme/*.c
src/sig_bliss/*.c
src/sig_tesla/*.c
src/sig_picnic/*.c
src/sig_dilithium/*.c
)
file( GLOB CRYPTO_HEADERS
src/*.h
src/rand/*.h
src/iaes/*.h
src/oaes/*.h
src/sha3/*.h
src/msrln/*.h
src/defeo_scheme/*.h
src/sig_bliss/*.h
src/sig_tesla/*.h
src/sig_picnic/*.h
src/sig_dilithium/*.h
include/*.h
)
enable_language(ASM)
file( GLOB XKCP_SRCS
src/XKCP/lib/high/Keccak/FIPS202/KeccakHash.c
src/XKCP/lib/high/Keccak/KeccakSpongeWidth1600.c
src/XKCP/lib/high/Keccak/SP800-185/SP800-185.c
src/XKCP/lib/high/Keccak/FIPS202/SimpleFIPS202.c
)
if(WIN32)
if(BUILD_64)
#file( GLOB XKCP_SRCS2 src/XKCP/lib/low/KeccakP-1600/OptimizedAsmX86-64/KeccakP-1600-x86-64-gas.s ) - SIGSEGV on MinGW
file( GLOB XKCP_SRCS2 src/XKCP/lib/low/KeccakP-1600/Compact64/KeccakP-1600-compact64.c )
else()
file( GLOB XKCP_SRCS2 src/XKCP/lib/low/KeccakP-1600/Inplace32BI/KeccakP-1600-inplace32BI.c )
endif()
endif()
if(UNIX)
if(BUILD_64)
file( GLOB XKCP_SRCS2 src/XKCP/lib/low/KeccakP-1600/OptimizedAsmX86-64/KeccakP-1600-x86-64-gas.s )
else()
file( GLOB XKCP_SRCS2 src/XKCP/lib/low/KeccakP-1600/Inplace32BI/KeccakP-1600-inplace32BI.c )
endif()
endif()
add_library(${PROJECT_NAME} STATIC ${CRYPTO_SRCS} ${XKCP_SRCS} ${XKCP_SRCS2} ${CRYPTO_HEADERS} )
target_include_directories(dap_crypto PRIVATE src/rand src/iaes src/oaes src/sha3 src/msrln src/defeo_scheme src/sig_bliss src/sig_tesla src/sig_picnic src/sig_dilithium src include)
target_include_directories(dap_crypto INTERFACE src/ src/sha3 include/)
target_include_directories(dap_crypto PUBLIC
src/XKCP/lib/common
src/XKCP/lib/low/common
src/XKCP/lib/high/Keccak
src/XKCP/lib/high/common
src/XKCP/lib/high/Keccak/FIPS202
src/XKCP/lib/high/Keccak/SP800-185
src/XKCP/lib/low/KeccakP-1600/Optimized
)
if(WIN32)
if(BUILD_64)
#target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600/OptimizedAsmX86-64)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600/Compact64)
if (__AVX__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
elseif(__AVX2__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
else()
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
endif()
else()
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600/Inplace32BI)
if (__AVX__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
elseif(__AVX2__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
else()
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
target_include_directories(dap_crypto PUBLIC src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
endif()
endif()
endif()
if(UNIX)
if(BUILD_64)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600/OptimizedAsmX86-64)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600/OptimizedAsmX86-64)
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
endif()
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
elseif(__AVX__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
endif()
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
elseif(__AVX__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
endif()
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600/Inplace32BI)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600/Inplace32BI)
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD512/AVX512ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/SIMD128/SSE2ufull)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times2/FallbackOn1)
endif()
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX512ufull)
elseif(__AVX__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD256)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/SIMD512/AVX2ufull)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn2)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times4/FallbackOn1)
endif()
if(__AVX2__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/SIMD512/AVX512ufull)
elseif(__AVX__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn4)
elseif(__SSE4_1__)
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn2)
else()
target_include_directories(dap_crypto INTERFACE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
target_include_directories(dap_crypto PRIVATE src/XKCP/lib/low/KeccakP-1600-times8/FallbackOn1)
endif()
endif()
endif()
if(WIN32)
target_link_libraries(dap_crypto dap_core)
endif()
if(UNIX)
target_link_libraries(dap_crypto dap_core m)
endif()
if(BUILD_CRYPTO_TESTS)
enable_testing()
add_subdirectory(test)
endif()
/*
* Authors:
* Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* DeM Labs Inc. https://demlabs.net
* CellFrame https://cellframe.net
* Sources https://gitlab.demlabs.net/cellframe
* Copyright (c) 2017-2019
* All rights reserved.
This file is part of CellFrame SDK the open source project
CellFrame SDK is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CellFrame SDK is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any CellFrame SDK based project. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "dap_sign.h"
#include "dap_pkey.h"
#include "dap_enc.h"
#include "dap_enc_key.h"
#define DAP_CERT_ITEM_NAME_MAX 40
typedef struct dap_cert {
dap_enc_key_t * enc_key;
char name[DAP_CERT_ITEM_NAME_MAX];
void * _pvt;
char * metadata;
} dap_cert_t;
int dap_cert_init();
dap_cert_t * dap_cert_new(const char * a_name);
size_t dap_cert_parse_str_list(const char * a_certs_str, dap_cert_t *** a_certs, size_t * a_certs_size);
dap_cert_t * dap_cert_generate(const char * a_cert_name,const char * a_file_path,dap_enc_key_type_t a_key_type );
dap_cert_t * dap_cert_generate_mem(const char * a_cert_name,
dap_enc_key_type_t a_key_type );
dap_cert_t * dap_cert_add_file(const char * a_cert_name,const char *a_folder_path);
int dap_cert_save_to_folder(dap_cert_t * a_cert, const char *a_file_dir_path);
void dap_cert_add_folder(const char *a_folder_path);
void dap_cert_dump(dap_cert_t * a_cert);
dap_pkey_t * dap_cert_to_pkey(dap_cert_t * a_cert);
dap_cert_t * dap_cert_find_by_name(const char * a_cert_name);
dap_sign_t * dap_cert_sign(dap_cert_t * a_cert, const void * a_data, size_t a_data_size, size_t a_output_size_wished );
int dap_cert_compare_with_sign (dap_cert_t * a_cert,dap_sign_t * a_sign);
size_t dap_cert_sign_output_size(dap_cert_t * a_cert, size_t a_size_wished);
//int dap_cert_sign_output(dap_cert_t * a_cert, const void * a_data, size_t a_data_size
// , void * a_output , size_t a_output_size);
int dap_cert_add_cert_sign(dap_cert_t * a_cert, dap_cert_t * a_cert_signer);
size_t dap_cert_count_cert_sign(dap_cert_t * a_cert);
void dap_cert_deinit();
void dap_cert_delete(dap_cert_t * a_cert);
void dap_cert_delete_by_name(const char * a_cert_name);
/*
* Authors:
* Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* DeM Labs Inc. https://demlabs.net https:/gitlab.com/demlabs
* Kelvin Project https://github.com/kelvinblockchain
* Copyright (c) 2017-2018
* All rights reserved.
This file is part of DAP (Deus Applications Prototypes) the open source project
DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include "dap_common.h"
#include "dap_cert.h"
// Magic .dapcert signature
#define dap_cert_FILE_HDR_SIGN 0x0F300C4711E29380
#define dap_cert_FILE_VERSION 1
// Default certificate with private key and optionaly some signs
#define dap_cert_FILE_TYPE_PRIVATE 0x00
// Default certificate with public key and optionaly some signs
#define dap_cert_FILE_TYPE_PUBLIC 0xf0
typedef struct dap_cert_file_hdr
{
uint64_t sign;
int version;
uint8_t type;
dap_sign_type_t sign_type;
uint64_t data_size;
uint64_t data_pvt_size;
uint64_t metadata_size;
time_t ts_last_used;
} DAP_ALIGN_PACKED dap_cert_file_hdr_t;
typedef struct dap_cert_file{
dap_cert_file_hdr_t hdr;
uint8_t data[];
}DAP_ALIGN_PACKED dap_cert_file_t;
int dap_cert_file_save(dap_cert_t * a_cert, const char * a_cert_file_path);
uint8_t* dap_cert_mem_save(dap_cert_t * a_cert, uint32_t *a_cert_size_out);
dap_cert_t* dap_cert_file_load(const char * a_cert_file_path);
dap_cert_t* dap_cert_mem_load(void * a_data, size_t a_data_size);
......@@ -15,9 +15,9 @@ extern "C" {
#include "sha3/fips202.h"
#include "KeccakHash.h"
#include "SimpleFIPS202.h"
#include "SP800-185.h"
//#include "KeccakHash.h"
//#include "SimpleFIPS202.h"
//#include "SP800-185.h"
#include "rand/dap_rand.h"
......
......@@ -25,7 +25,12 @@ size_t dap_enc_sig_dilithium_verify_sign(struct dap_enc_key * key,const void * m
void dap_enc_sig_dilithium_key_delete(struct dap_enc_key * key);
size_t dap_enc_dilithium_calc_signature_size(void);
size_t dap_enc_dilithium_calc_signature_unserialized_size(void);
static inline size_t dap_enc_dilithium_calc_signagture_size(dilithium_signature_t* a_sign)
{
return sizeof(size_t) + sizeof(dilithium_kind_t) + a_sign->sig_len + sizeof(unsigned long long);
}
uint8_t* dap_enc_dilithium_write_signature(dilithium_signature_t* a_sign, size_t *a_sign_out);
dilithium_signature_t* dap_enc_dilithium_read_signature(uint8_t *a_buf, size_t a_buflen);
......
......@@ -228,7 +228,7 @@ uint8_t* dap_enc_key_deserealize_sign(dap_enc_key_type_t a_key_type, uint8_t *a_
uint8_t* dap_enc_key_serealize_priv_key(dap_enc_key_t *a_key, size_t *a_buflen_out);
uint8_t* dap_enc_key_serealize_pub_key(dap_enc_key_t *a_key, size_t *a_buflen_out);
int dap_enc_key_deserealize_priv_key(dap_enc_key_t *a_key, uint8_t *a_buf, size_t a_buflen);
int dap_enc_key_deserealize_pub_key(dap_enc_key_t *a_key, const uint8_t *a_buf, size_t a_buflen);
int dap_enc_key_deserealize_pub_key(dap_enc_key_t *a_key, uint8_t *a_buf, size_t a_buflen);
dap_enc_key_serealize_t* dap_enc_key_serealize(dap_enc_key_t * key);
dap_enc_key_t* dap_enc_key_deserealize(void *buf, size_t buf_size);
......
#ifndef _DAP_ENC_TESLA_H_
#define _DAP_ENC_TESLA_H_
#pragma once
#include "sig_tesla/tesla_params.h"
#include "dap_enc_key.h"
......@@ -26,6 +25,7 @@ size_t dap_enc_sig_tesla_verify_sign(struct dap_enc_key * key,const void * msg,
void dap_enc_sig_tesla_key_delete(struct dap_enc_key * key);
size_t dap_enc_tesla_calc_signature_size(void);
size_t dap_enc_tesla_calc_signature_serialized_size(tesla_signature_t* a_sign);
uint8_t* dap_enc_tesla_write_signature(tesla_signature_t* a_sign, size_t *a_sign_out);
tesla_signature_t* dap_enc_tesla_read_signature(uint8_t *a_buf, size_t a_buflen);
......@@ -34,4 +34,3 @@ uint8_t* dap_enc_tesla_write_public_key(const tesla_public_key_t* a_public_key,
tesla_private_key_t* dap_enc_tesla_read_private_key(const uint8_t *a_buf, size_t a_buflen);
tesla_public_key_t* dap_enc_tesla_read_public_key(const uint8_t *a_buf, size_t a_buflen);
#endif
/*
* Authors:
* Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* DeM Labs Inc. https://demlabs.net
* CellFrame https://cellframe.net
* Sources https://gitlab.demlabs.net/cellframe
* Copyright (c) 2017-2019
* All rights reserved.
This file is part of CellFrame SDK the open source project
CellFrame SDK is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CellFrame SDK is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any CellFrame SDK based project. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include "dap_common.h"
#include "dap_hash_keccak.h"
#include "KeccakHash.h"
#include "SimpleFIPS202.h"
#define DAP_HASH_FAST_SIZE 32
#define DAP_CHAIN_HASH_FAST_SIZE 32
typedef enum dap_hash_type {
DAP_HASH_TYPE_KECCAK = 0,
DAP_HASH_TYPE_SLOW_0 = 1,
} dap_hash_type_t;
typedef union dap_chain_hash_fast{
uint8_t raw[DAP_CHAIN_HASH_FAST_SIZE];
} dap_chain_hash_fast_t;
//size_t dap_chain_hash_fast_to_str(dap_chain_hash_fast_t * a_hash, char * a_str, size_t a_str_max);
int dap_chain_str_to_hash_fast( const char * a_hash_str, dap_chain_hash_fast_t * a_hash);
static inline bool dap_hash_fast( const void *a_data_in, size_t a_data_in_size, dap_chain_hash_fast_t *a_hash_out )
{
if ( (a_data_in == NULL) || (a_data_in_size == 0) || (a_hash_out == NULL) )
return false;
// dap_hash_keccak( a_data_in, a_data_in_size, a_data_out, a_data_out_size );
SHA3_256( (unsigned char *)a_hash_out, (const unsigned char *)a_data_in, a_data_in_size );
//SHA3_256( (unsigned char *)a_hash_out, (const unsigned char *)a_data_in, a_data_in_size );
return true;
}
/**
* @brief dap_hash_fast_compare
* @param a_hash1
* @param a_hash2
* @return
*/
static inline bool dap_hash_fast_compare(dap_chain_hash_fast_t *a_hash1, dap_chain_hash_fast_t *a_hash2)
{
if(!a_hash1 || !a_hash2)
return false;
if(!memcmp(a_hash1, a_hash2, sizeof(dap_chain_hash_fast_t)))
return true;
return false;
}
static inline bool dap_hash_fast_is_blank( dap_chain_hash_fast_t *a_hash )
{
static dap_chain_hash_fast_t l_blank_hash = { 0};
// uint8_t *l_hast_bytes = (uint8_t*) a_hash;
// for(size_t i = 0; i < sizeof(dap_chain_hash_fast_t); i++) {
// if(l_hast_bytes[i])
// return false;
// }
return dap_hash_fast_compare( a_hash, &l_blank_hash);
}
DAP_STATIC_INLINE int dap_chain_hash_fast_to_str( dap_chain_hash_fast_t *a_hash, char *a_str, size_t a_str_max )
{
a_str[0] = '0';
a_str[1] = 'x';
a_str[ DAP_CHAIN_HASH_FAST_SIZE * 2 + 2 ] = 0;
dap_htoa64( (a_str + 2), a_hash->raw, DAP_CHAIN_HASH_FAST_SIZE );
return DAP_CHAIN_HASH_FAST_SIZE * 2 + 2;
}
static inline char *dap_chain_hash_fast_to_str_new(dap_chain_hash_fast_t * a_hash)
{
const size_t c_hash_str_size = sizeof(*a_hash)*2 +1 /*trailing zero*/ +2 /* heading 0x */+4/*just to be sure*/ ;
char * ret = DAP_NEW_Z_SIZE(char, c_hash_str_size);
dap_chain_hash_fast_to_str( a_hash, ret, c_hash_str_size );
return ret;
}
/*
* Authors:
* Dmitriy A. Gearasimov <kahovski@gmail.com>
* DeM Labs Inc. https://demlabs.net
* DeM Labs Open source community https://github.com/demlabsinc
* Copyright (c) 2017-2018
* All rights reserved.
This file is part of DAP (Deus Applications Prototypes) the open source project
DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
/*
* Authors:
* Dmitriy A. Gearasimov <kahovski@gmail.com>
* DeM Labs Inc. https://demlabs.net
* DeM Labs Open source community https://github.com/demlabsinc
* Copyright (c) 2017-2018
* All rights reserved.
This file is part of DAP (Deus Applications Prototypes) the open source project
DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
/*
#include "keccak.h"
static inline void dap_hash_keccak(const void * a_in, size_t a_in_size, void * a_out, size_t a_out_size)
{
keccak((const uint8_t*) a_in, a_in_size, (uint8_t *) a_out,(int) a_out_size );
}*/
/*
* Authors:
* Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* DeM Labs Inc. https://demlabs.net https:/gitlab.com/demlabs
* Kelvin Project https://github.com/kelvinblockchain
* Copyright (c) 2017-2018
* All rights reserved.
This file is part of DAP (Deus Applications Prototypes) the open source project
DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include "dap_common.h"
#include "dap_enc_key.h"
typedef union dap_pkey_type{
enum {
PKEY_TYPE_NULL = 0x0000,
PKEY_TYPE_SIGN_BLISS = 0x0901,
PKEY_TYPE_SIGN_TESLA = 0x0902,
PKEY_TYPE_SIGN_DILITHIUM = 0x0903,
PKEY_TYPE_SIGN_PICNIC = 0x0102,
PKEY_TYPE_MULTI = 0xffff /// @brief Has inside subset of different keys
} type: 16;
uint16_t raw;
} dap_pkey_type_t;
/**
* @struct dap_pkey
* @brief Public keys
*/
typedef struct dap_pkey{
struct {
dap_pkey_type_t type; /// Pkey type
uint32_t size; /// Pkey size
} header; /// Only header's hash is used for verification
uint8_t pkey[]; /// @param pkey @brief raw pkey dat
} DAP_ALIGN_PACKED dap_pkey_t;
static dap_pkey_t m_dap_pkey_null; // For sizeof nothing more
dap_pkey_t *dap_pkey_from_enc_key(dap_enc_key_t *a_key);
static inline size_t dap_pkey_from_enc_key_output_calc(dap_enc_key_t *a_key)
{
return sizeof(m_dap_pkey_null.header)+ a_key->pub_key_data_size;
}
int dap_pkey_from_enc_key_output(dap_enc_key_t *a_key, void * a_output);
/*
* Authors:
* Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* DeM Labs Inc. https://demlabs.net https:/gitlab.com/demlabs
* Kelvin Project https://github.com/kelvinblockchain
* Copyright (c) 2017-2018
* All rights reserved.
This file is part of DAP (Deus Applications Prototypes) the open source project
DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include "dap_common.h"
#include "dap_enc_ca.h"
#include "dap_enc_key.h"
#include "dap_pkey.h"
#include "dap_hash.h"
typedef union dap_sign_type{
enum {
SIG_TYPE_NULL = 0x0000,
SIG_TYPE_BLISS = 0x0001,
SIG_TYPE_DEFO = 0x0002, /// @brief key image for anonymous transaction
SIG_TYPE_TESLA = 0x0003, /// @brief
SIG_TYPE_PICNIC = 0x0101, /// @brief
SIG_TYPE_DILITHIUM = 0x0102, /// @brief
SIG_TYPE_MULTI = 0xffff /// @brief Has inside subset of different signatures and sign composed with all of them
} type: 16;
uint16_t raw;
} dap_sign_type_t;
typedef struct dap_sign_hdr{
dap_sign_type_t type; /// Signature type
uint8_t padding[2]; /// Padding for better aligmnent
uint16_t sign_size; /// Signature size
uint32_t sign_pkey_size; /// Signature serialized public key size
} DAP_ALIGN_PACKED dap_sign_hdr_t;
/**
* @struct dap_sign
* @brief Chain storage format for digital signature
*/
typedef struct dap_sign
{
dap_sign_hdr_t header; /// Only header's hash is used for verification
uint8_t pkey_n_sign[]; /// @param sig @brief raw signature data
} DAP_ALIGN_PACKED dap_sign_t;
size_t dap_sign_get_size(dap_sign_t * a_chain_sign);
int dap_sign_verify (dap_sign_t * a_chain_sign, const void * a_data, const size_t a_data_size);
dap_sign_t * dap_sign_create(dap_enc_key_t *a_key, const void * a_data, const size_t a_data_size
, size_t a_output_wish_size );
size_t dap_sign_create_output_unserialized_calc_size(dap_enc_key_t * a_key,size_t a_output_wish_size );
//int dap_sign_create_output(dap_enc_key_t *a_key, const void * a_data, const size_t a_data_size
// , void * a_output, size_t a_output_size );
dap_sign_type_t dap_sign_type_from_key_type( dap_enc_key_type_t a_key_type);
dap_enc_key_type_t dap_sign_type_to_key_type(dap_sign_type_t a_chain_sign_type);
dap_sign_type_t dap_pkey_type_from_sign( dap_pkey_type_t a_pkey_type);
uint8_t* dap_sign_get_sign(dap_sign_t *a_sign, size_t *a_sign_out);
uint8_t* dap_sign_get_pkey(dap_sign_t *a_sign, size_t *a_pub_key_out);
bool dap_sign_get_pkey_hash(dap_sign_t *a_sign, dap_chain_hash_fast_t * a_sign_hash);
dap_enc_key_t *dap_sign_to_enc_key(dap_sign_t * a_chain_sign);
const char * dap_sign_type_to_str(dap_sign_type_t a_chain_sign_type);
dap_sign_type_t dap_sign_type_from_str(const char * a_type_str);
/*
* Authors:
* Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* DeM Labs Inc. https://demlabs.net https:/gitlab.com/demlabs
* Kelvin Project https://github.com/kelvinblockchain
* Copyright (c) 2017-2018
* All rights reserved.
This file is part of DAP (Deus Applications Prototypes) the open source project
DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include "uthash.h"
#include "utlist.h"
#include "dap_common.h"
#include "dap_config.h"
#include "dap_string.h"
#include "dap_strfuncs.h"
#include "dap_cert.h"
#include "dap_cert_file.h"
//#include "dap_hash.h"
#define LOG_TAG "dap_cert"
typedef struct dap_sign_item
{
dap_sign_t * sign;
struct dap_sign_item * next;
struct dap_sign_item * prev;
} dap_sign_item_t;
typedef struct dap_cert_item
{
char name[DAP_CERT_ITEM_NAME_MAX];
dap_cert_t * cert;
UT_hash_handle hh;
} dap_cert_item_t;
typedef struct dap_cert_pvt
{
dap_sign_item_t *signs;
} dap_cert_pvt_t;
#define PVT(a) ( ( dap_cert_pvt_t *)((a)->_pvt) )
static dap_cert_item_t * s_certs = NULL;
/**
* @brief dap_cert_init
* @return
*/
int dap_cert_init()
{
return 0;
}
/**
* @brief dap_cert_parse_str_list
* @param a_certs_str
* @param a_certs
* @param a_certs_size_t
* @return summary size for signatures of all certs in list
*/
size_t dap_cert_parse_str_list(const char * a_certs_str, dap_cert_t *** a_certs, size_t * a_certs_size)
{
char * l_certs_tmp_ptrs = NULL;
char * l_certs_str_dup = strdup(a_certs_str);
char *l_cert_str = strtok_r(l_certs_str_dup, ",", &l_certs_tmp_ptrs);
// First we just calc items
while(l_cert_str) {
l_cert_str = strtok_r(NULL, ",", &l_certs_tmp_ptrs);
(*a_certs_size)++;
}
// init certs array
dap_cert_t **l_certs;
*a_certs = l_certs = DAP_NEW_Z_SIZE(dap_cert_t*, (*a_certs_size) * sizeof(dap_cert_t*) );
// Second pass we parse them all
strcpy(l_certs_str_dup, a_certs_str);
l_cert_str = strtok_r(l_certs_str_dup, ",", &l_certs_tmp_ptrs);
size_t l_certs_pos = 0;
size_t l_sign_total_size =0;
while(l_cert_str) {
// trim whitespace in certificate's name
l_cert_str = dap_strstrip(l_cert_str);// removes leading and trailing spaces
// get certificate by name
l_certs[l_certs_pos] = dap_cert_find_by_name(l_cert_str);
// if certificate is found
if(l_certs[l_certs_pos]) {
l_sign_total_size += dap_cert_sign_output_size(l_certs[l_certs_pos],0);
l_certs_pos++;
} else {
log_it(L_WARNING,"Can't load cert %s",l_cert_str);
DAP_DELETE(*a_certs);
*a_certs = NULL;
*a_certs_size = 0;
break;
}
l_cert_str = strtok_r(NULL, ",", &l_certs_tmp_ptrs);
}
free(l_certs_str_dup);
return l_sign_total_size;
}
/**
* @brief dap_cert_sign_output_size
* @param a_cert
* @param a_size_wished
* @return
*/
size_t dap_cert_sign_output_size(dap_cert_t * a_cert, size_t a_size_wished)
{
return dap_sign_create_output_unserialized_calc_size( a_cert->enc_key,a_size_wished);
}
/**
* @brief dap_cert_sign_output
* @param a_cert
* @param a_data
* @param a_data_size
* @param a_output
* @param a_output_siz
* @return
*/
/*int dap_cert_sign_output(dap_cert_t * a_cert, const void * a_data, size_t a_data_size,
void * a_output, size_t a_output_size)
{
return dap_sign_create_output( a_cert->enc_key,a_data,a_data_size,a_output,a_output_size);
}*/
/**
* @brief dap_cert_sign
* @param a_cert
* @param a_data
* @param a_data_size
* @param a_output_size_wished
* @return
*/
dap_sign_t * dap_cert_sign(dap_cert_t * a_cert, const void * a_data
, size_t a_data_size, size_t a_output_size_wished )
{
dap_enc_key_t * l_key = a_cert->enc_key;
dap_sign_t *l_ret = dap_sign_create(l_key, a_data, a_data_size, a_output_size_wished);
return l_ret;
}
/**
* @brief dap_cert_add_cert_sign
* @param a_cert
* @param a_cert_signer
* @return
*/
int dap_cert_add_cert_sign(dap_cert_t * a_cert, dap_cert_t * a_cert_signer)
{
if (a_cert->enc_key->pub_key_data_size && a_cert->enc_key->pub_key_data) {
dap_sign_item_t * l_sign_item = DAP_NEW_Z(dap_sign_item_t);
l_sign_item->sign = dap_cert_sign (a_cert_signer,a_cert->enc_key->pub_key_data,a_cert->enc_key->pub_key_data_size,0);
DL_APPEND ( PVT(a_cert)->signs, l_sign_item );
return 0;
} else {
log_it (L_ERROR, "No public key in cert \"%s\" that we are trying to sign with \"%s\"", a_cert->name,a_cert_signer->name);
return -1;
}
}
/**
* @brief dap_cert_generate_mem
* @param a_cert_name
* @param a_key_type
* @return
*/
dap_cert_t * dap_cert_generate_mem(const char * a_cert_name,
dap_enc_key_type_t a_key_type )
{
dap_enc_key_t *l_enc_key = dap_enc_key_new_generate(a_key_type, NULL, 0, NULL, 0, 0);
if ( l_enc_key ){
dap_cert_t * l_cert = dap_cert_new(a_cert_name);
l_cert->enc_key = l_enc_key;
log_it(L_DEBUG,"Certificate generated");
//dap_cert_item_t * l_cert_item = DAP_NEW_Z(dap_cert_item_t);
//snprintf(l_cert_item->name,sizeof(l_cert_item->name),"%s",a_cert_name);
//HASH_ADD_STR(s_certs,name,l_cert_item);
log_it(L_DEBUG,"Certificate name %s recorded", a_cert_name);
return l_cert;
} else {
log_it(L_ERROR,"Can't generate key in memory!");
//dap_cert_delete(l_cert);
return NULL;
}
}
/**
* @brief dap_cert_generate
* @param a_cert_name
* @param a_file_path
* @param a_key_type
* @return
*/
dap_cert_t * dap_cert_generate(const char * a_cert_name
, const char * a_file_path,dap_enc_key_type_t a_key_type )
{
dap_cert_t * l_cert = dap_cert_generate_mem(a_cert_name,a_key_type);
if ( l_cert){
if ( dap_cert_file_save(l_cert, a_file_path) == 0 ){
return l_cert;
} else{
dap_cert_delete(l_cert);
log_it(L_ERROR, "Can't save certificate to the file!");
return NULL;
}
} else {
log_it(L_ERROR,"Can't generate certificat in memory!");
}
return NULL;
}
/**
* @brief dap_cert_delete_by_name
* @param a_cert_name
*/
void dap_cert_delete_by_name(const char * a_cert_name)
{
dap_cert_t * l_cert = dap_cert_find_by_name(a_cert_name);
if ( l_cert )
dap_cert_delete( l_cert );
else
log_it(L_WARNING,"Can't find \"%s\" certificate to delete it",a_cert_name);
}
/**
* @brief dap_cert_find_by_name
* @param a_cert_name
* @return
*/
dap_cert_t * dap_cert_find_by_name(const char * a_cert_name)
{
dap_cert_item_t * l_cert_item = NULL;
HASH_FIND_STR(s_certs,a_cert_name,l_cert_item);
if ( l_cert_item ){
return l_cert_item->cert ;
} else {
dap_cert_t *l_cert = NULL;
uint16_t l_ca_folders_size = 0;
char **l_ca_folders;
char *l_cert_path = NULL;
l_ca_folders = dap_config_get_array_str(g_config, "resources", "ca_folders", &l_ca_folders_size);
for (uint16_t i = 0; i < l_ca_folders_size; ++i) {
#ifdef _WIN32
l_cert_path = dap_strjoin("", s_sys_dir_path, l_ca_folders[i], "/", a_cert_name, ".dcert", (char*)NULL);
#else
l_cert_path = dap_strjoin("", l_ca_folders[i], "/", a_cert_name, ".dcert", (char*)NULL);
#endif
l_cert = dap_cert_file_load(l_cert_path);
if (l_cert) {
goto ret;
}
}
ret:
if (l_cert_path)
DAP_DELETE(l_cert_path);
return l_cert;
}
}
/**
* @brief dap_cert_new
* @param a_name
* @return
*/
dap_cert_t * dap_cert_new(const char * a_name)
{
dap_cert_t * l_ret = DAP_NEW_Z(dap_cert_t);
l_ret->_pvt = DAP_NEW_Z(dap_cert_pvt_t);
dap_snprintf(l_ret->name,sizeof(l_ret->name),"%s",a_name);
dap_cert_item_t * l_cert_item = DAP_NEW_Z(dap_cert_item_t);
dap_snprintf(l_cert_item->name,sizeof(l_cert_item->name),"%s",a_name);
l_cert_item->cert = l_ret;
HASH_ADD_STR(s_certs,name,l_cert_item);
return l_ret;
}
/**
* @brief s_cert_delete
* @param a_cert
*/
void dap_cert_delete(dap_cert_t * a_cert)
{
dap_cert_item_t * l_cert_item = NULL;
HASH_FIND_STR(s_certs, a_cert->name, l_cert_item);
if ( l_cert_item ){
HASH_DEL(s_certs,l_cert_item);
DAP_DELETE (l_cert_item);
}
if( a_cert->enc_key )
dap_enc_key_delete (a_cert->enc_key );
if( a_cert->metadata )
DAP_DELETE (a_cert->metadata );
if (a_cert->_pvt)
DAP_DELETE( a_cert->_pvt );
DAP_DELETE (a_cert );
}
/**
* @brief dap_cert_add_file
* @param a_cert_name
* @param a_folder_path
* @return
*/
dap_cert_t * dap_cert_add_file(const char * a_cert_name,const char *a_folder_path)
{
size_t l_cert_path_length = strlen(a_cert_name)+8+strlen(a_folder_path);
char * l_cert_path = DAP_NEW_Z_SIZE(char,l_cert_path_length);
dap_snprintf(l_cert_path,l_cert_path_length,"%s/%s.dcert",a_folder_path,a_cert_name);
if( access( l_cert_path, F_OK ) == -1 ) {
log_it (L_ERROR, "File %s is not exists! ", l_cert_path);
DAP_DELETE(l_cert_path);
exit(-701);
}
dap_cert_t * l_cert;
l_cert = dap_cert_file_load(l_cert_path);
if (l_cert == NULL){
log_it (L_ERROR, "File %s is corrupted or wrong format ", l_cert_path);
}
DAP_DELETE(l_cert_path);
return l_cert;
}
/**
* @brief dap_cert_save_to_folder
* @param a_cert
* @param a_file_dir_path
*/
int dap_cert_save_to_folder(dap_cert_t * a_cert, const char *a_file_dir_path)
{
int ret = 0;
const char * l_cert_name = a_cert->name;
size_t l_cert_path_length = strlen(l_cert_name)+8+strlen(a_file_dir_path);
char * l_cert_path = DAP_NEW_Z_SIZE(char,l_cert_path_length);
dap_snprintf(l_cert_path,l_cert_path_length,"%s/%s.dcert",a_file_dir_path,l_cert_name);
ret = dap_cert_file_save(a_cert,l_cert_path);
DAP_DELETE( l_cert_path);
return ret;
}
/**
* @brief dap_cert_to_pkey
* @param a_cert
* @return
*/
dap_pkey_t * dap_cert_to_pkey(dap_cert_t * a_cert)
{
if ( a_cert )
return dap_pkey_from_enc_key( a_cert->enc_key );
else
return NULL;
}
/**
* @brief dap_cert_compare_with_sign
* @param a_cert
* @param a_sign
* @return
*/
int dap_cert_compare_with_sign (dap_cert_t * a_cert,dap_sign_t * a_sign)
{
dap_return_val_if_fail(a_cert && a_cert->enc_key && a_sign, -1);
if ( dap_sign_type_from_key_type( a_cert->enc_key->type ).type == a_sign->header.type.type ){
int l_ret;
size_t l_pub_key_size = 0;
// serialize public key
uint8_t *l_pub_key = dap_enc_key_serealize_pub_key(a_cert->enc_key, &l_pub_key_size);
if ( l_pub_key_size == a_sign->header.sign_pkey_size){
l_ret = memcmp ( l_pub_key, a_sign->pkey_n_sign, a_sign->header.sign_pkey_size );
}else
l_ret = -2; // Wrong pkey size
DAP_DELETE(l_pub_key);
return l_ret;
}else
return -1; // Wrong sign type
}
/**
* @brief dap_cert_count_cert_sign
* @param a_cert
* @return
*/
size_t dap_cert_count_cert_sign(dap_cert_t * a_cert)
{
size_t ret;
dap_sign_item_t * l_cert_item = NULL;
DL_COUNT( PVT(a_cert)->signs,l_cert_item,ret);
return ret > 0 ? ret : 0 ;
}
/**
* @brief dap_cert_dump
* @param a_cert
*/
void dap_cert_dump(dap_cert_t * a_cert)
{
printf ("Certificate name: %s\n",a_cert->name);
printf ("Signature type: %s\n", dap_sign_type_to_str( dap_sign_type_from_key_type(a_cert->enc_key->type) ) );
printf ("Private key size: %lu\n",a_cert->enc_key->priv_key_data_size);
printf ("Public key size: %lu\n", a_cert->enc_key->pub_key_data_size);
printf ("Metadata section size: %lu\n",a_cert->metadata?strlen(a_cert->metadata):0);
printf ("Certificates signatures chain size: %lu\n",dap_cert_count_cert_sign (a_cert));
}
/**
* @brief dap_cert_add_folder
* @param a_folder_path
*/
void dap_cert_add_folder(const char *a_folder_path)
{
DIR * l_dir = opendir(a_folder_path);
if( l_dir ) {
struct dirent * l_dir_entry;
while((l_dir_entry=readdir(l_dir))!=NULL){
const char * l_filename = l_dir_entry->d_name;
size_t l_filename_len = strlen (l_filename);
// Check if its not special dir entries . or ..
if( strcmp(l_filename,".") && strcmp(l_filename,"..") ){
// If not check the file's suffix
const char l_suffix[]=".dcert";
size_t l_suffix_len = strlen(l_suffix);
if (strncmp(l_filename+ l_filename_len-l_suffix_len,l_suffix,l_suffix_len) == 0 ){
char * l_cert_name = dap_strdup(l_filename);
l_cert_name[l_filename_len-l_suffix_len] = '\0'; // Remove suffix
// Load the cert file
//log_it(L_DEBUG,"Trying to load %s",l_filename);
dap_cert_add_file(l_cert_name,a_folder_path);
DAP_DELETE(l_cert_name);
}
}
}
closedir(l_dir);
log_it(L_NOTICE, "Added folder %s",a_folder_path);
}else
log_it(L_WARNING, "Can't add folder %s to cert manager",a_folder_path);
}
/**
* @brief dap_cert_deinit
*/
void dap_cert_deinit()
{
}
/*
* Authors:
* Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* DeM Labs Inc. https://demlabs.net https:/gitlab.com/demlabs
* Kelvin Project https://github.com/kelvinblockchain
* Copyright (c) 2017-2018
* All rights reserved.
This file is part of DAP (Deus Applications Prototypes) the open source project
DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "dap_common.h"
#include "dap_enc.h"
#include "dap_enc_key.h"
#include "dap_cert_file.h"
#define LOG_TAG "dap_cert_file"
/**
* @brief dap_cert_file_save
* @param a_cert
* @param a_cert_file_path
* @return
*/
int dap_cert_file_save(dap_cert_t * a_cert, const char * a_cert_file_path)
{
FILE * l_file = fopen(a_cert_file_path,"wb");
if( l_file ){
uint32_t l_data_size = 0;
void * l_data = dap_cert_mem_save(a_cert, &l_data_size);
if ( l_data ){
size_t l_retbytes;
if ( (l_retbytes = fwrite(l_data,1,l_data_size,l_file)) != l_data_size ){
log_it(L_ERROR, "Can't write %u bytes on disk (processed only %u)!", l_data_size,l_retbytes);
return -3;
}
fclose(l_file);
DAP_DELETE(l_data);
return 0;
}else{
log_it(L_ERROR,"Can't serialize certificate in memory");
fclose(l_file);
return -4;
}
}else{
log_it(L_ERROR, "Can't open file for write: %s", strerror(errno));
return -2;
}
}
/**
* @brief dap_cert_file_save_to_mem
* @param a_cert
* @param a_cert_size_out
* @return
*/
uint8_t* dap_cert_mem_save(dap_cert_t * a_cert, uint32_t *a_cert_size_out)
{
dap_cert_file_hdr_t l_hdr={0};
uint32_t l_data_offset = 0;
dap_enc_key_t * l_key = a_cert->enc_key;
uint8_t *l_data = NULL;
size_t l_priv_key_data_size = a_cert->enc_key->priv_key_data_size;
size_t l_pub_key_data_size = a_cert->enc_key->pub_key_data_size;
uint8_t *l_pub_key_data = a_cert->enc_key->pub_key_data_size ?
dap_enc_key_serealize_pub_key(l_key, &l_pub_key_data_size) :
NULL;
uint8_t *l_priv_key_data = a_cert->enc_key->priv_key_data ?
dap_enc_key_serealize_priv_key(l_key, &l_priv_key_data_size) :
NULL;
l_hdr.sign = dap_cert_FILE_HDR_SIGN;
l_hdr.type = dap_cert_FILE_TYPE_PUBLIC;
if ( l_priv_key_data ){
l_hdr.type = dap_cert_FILE_TYPE_PRIVATE;
log_it(L_DEBUG,"Private key size %u",l_priv_key_data_size);
}
if (l_pub_key_data){
log_it(L_DEBUG,"Public key size %u",l_pub_key_data_size);
}else{
log_it(L_ERROR,"No public or private key in certificate, nothing to save");
goto lb_exit;
}
log_it(L_DEBUG,"Metadata size %u",l_key->_inheritor_size);
l_hdr.version = dap_cert_FILE_VERSION;
l_hdr.data_size = l_pub_key_data_size;
l_hdr.data_pvt_size = l_priv_key_data_size;
l_hdr.metadata_size = l_key->_inheritor_size;
l_hdr.ts_last_used = l_key->last_used_timestamp;
l_hdr.sign_type = dap_sign_type_from_key_type ( l_key->type );
l_data = DAP_NEW_SIZE(void, sizeof(l_hdr) + DAP_CERT_ITEM_NAME_MAX + l_priv_key_data_size + l_pub_key_data_size + l_hdr.metadata_size);
memcpy(l_data +l_data_offset, &l_hdr ,sizeof(l_hdr) );
l_data_offset += sizeof(l_hdr);
memcpy(l_data +l_data_offset, a_cert->name, DAP_CERT_ITEM_NAME_MAX );//save cert name
l_data_offset += DAP_CERT_ITEM_NAME_MAX;
memcpy(l_data +l_data_offset, l_pub_key_data ,l_pub_key_data_size );
l_data_offset += l_pub_key_data_size;
if ( l_priv_key_data_size ) {
memcpy(l_data +l_data_offset, l_priv_key_data ,l_priv_key_data_size );
l_data_offset += l_priv_key_data_size;
}
if ( l_key->_inheritor_size ) {
memcpy(l_data +l_data_offset, l_key->_inheritor ,l_key->_inheritor_size );
l_data_offset += l_key->_inheritor_size;
}
lb_exit:
DAP_DELETE(l_pub_key_data);
DAP_DELETE(l_priv_key_data);
if (l_data)
log_it(L_NOTICE,"Certificate \"%s\" successfully serialized",a_cert->name);
else
log_it(L_ERROR,"Certificate \"%s\" was not serialized",a_cert->name);
if(a_cert_size_out)
*a_cert_size_out = l_data_offset;
return l_data;
}
/**
* @brief dap_cert_file_load
* @param a_cert_file_path
* @return
*/
dap_cert_t* dap_cert_file_load(const char * a_cert_file_path)
{
dap_cert_t * l_ret = NULL;
FILE * l_file = fopen(a_cert_file_path,"rb");
if( l_file ){
fseek(l_file, 0L, SEEK_END);
uint64_t l_file_size = ftell(l_file);
rewind(l_file);
uint8_t * l_data = DAP_NEW_SIZE(uint8_t,l_file_size);
if ( fread(l_data,1,l_file_size,l_file ) != l_file_size ){
log_it(L_ERROR, "Can't read %u bytes from the disk!", l_file_size);
DAP_DELETE (l_data);
goto lb_exit;
}else{
l_ret = dap_cert_mem_load(l_data,l_file_size);
}
DAP_DELETE(l_data);
}
lb_exit:
if( l_file )
fclose(l_file);
return l_ret;
}
/**
* @brief dap_cert_mem_load
* @param a_data
* @param a_data_size
* @return
*/
dap_cert_t* dap_cert_mem_load(void * a_data, size_t a_data_size)
{
dap_cert_t * l_ret = NULL;
dap_cert_file_hdr_t l_hdr={0};
uint8_t * l_data = (uint8_t *) a_data;
uint32_t l_data_offset = 0;
memcpy(&l_hdr,l_data, sizeof(l_hdr));
l_data_offset += sizeof(l_hdr);
if (l_hdr.sign != dap_cert_FILE_HDR_SIGN ){
log_it(L_ERROR, "Wrong cert signature, corrupted header!");
goto l_exit;
}
if (l_hdr.version >= 1 ){
if ( (sizeof(l_hdr) + l_hdr.data_size+l_hdr.data_pvt_size +l_hdr.metadata_size) > a_data_size ){
log_it(L_ERROR,"Corrupted cert data, data sections size is smaller than exists on the disk! (%llu expected, %llu on disk)",
sizeof(l_hdr)+l_hdr.data_pvt_size+l_hdr.data_size+l_hdr.metadata_size, a_data_size);
goto l_exit;
}
char l_name[DAP_CERT_ITEM_NAME_MAX];
memcpy(l_name, l_data +l_data_offset, DAP_CERT_ITEM_NAME_MAX );//save cert name
l_data_offset += DAP_CERT_ITEM_NAME_MAX;
//l_ret = DAP_NEW_Z(dap_cert_t);
l_ret = dap_cert_new(l_name);
l_ret->enc_key = dap_enc_key_new( dap_sign_type_to_key_type( l_hdr.sign_type ));
l_ret->enc_key->last_used_timestamp = l_hdr.ts_last_used;
if ( l_hdr.data_size > 0 ){
dap_enc_key_deserealize_pub_key(l_ret->enc_key, l_data + l_data_offset, l_hdr.data_size);
l_data_offset += l_hdr.data_size;
}
if ( l_hdr.data_pvt_size > 0 ){
dap_enc_key_deserealize_priv_key(l_ret->enc_key, l_data + l_data_offset, l_hdr.data_pvt_size);
l_data_offset += l_hdr.data_pvt_size;
}
if(l_hdr.metadata_size > 0 && l_ret->enc_key->_inheritor && l_ret->enc_key->_inheritor_size == l_hdr.metadata_size) {
memcpy(l_ret->enc_key->_inheritor, l_data + l_data_offset, l_ret->enc_key->_inheritor_size);
}
dap_enc_key_update(l_ret->enc_key);
log_it(L_NOTICE,"Successfully loaded certificate %s", l_ret->name);
}else
log_it(L_ERROR,"Unrecognizable certificate version, corrupted file or you have too old software");
l_exit:
return l_ret;
}
......@@ -88,23 +88,32 @@ size_t dap_enc_sig_dilithium_verify_sign(struct dap_enc_key * key, const void *
void dap_enc_sig_dilithium_key_delete(struct dap_enc_key * key)
{
dilithium_private_and_public_keys_delete((dilithium_private_key_t *) key->priv_key_data,
if( key->priv_key_data && key->pub_key_data)
dilithium_private_and_public_keys_delete((dilithium_private_key_t *) key->priv_key_data,
(dilithium_public_key_t *) key->pub_key_data);
else if ( key->pub_key_data )
dilithium_public_key_delete((dilithium_public_key_t *) key->pub_key_data);
else if ( key->priv_key_data )
dilithium_public_key_delete((dilithium_public_key_t *) key->priv_key_data);
}
size_t dap_enc_dilithium_calc_signature_size(void)
size_t dap_enc_dilithium_calc_signature_unserialized_size(void)
{
return sizeof(dilithium_signature_t);
}
/* Serialize a signature */
uint8_t* dap_enc_dilithium_write_signature(dilithium_signature_t* a_sign, size_t *a_sign_out)
{
if(!a_sign || *a_sign_out!=sizeof(dilithium_signature_t)) {
if(!a_sign ) {
return NULL ;
}
size_t l_shift_mem = 0;
size_t l_buflen = sizeof(size_t) + sizeof(dilithium_kind_t) + a_sign->sig_len + sizeof(unsigned long long);
size_t l_buflen = dap_enc_dilithium_calc_signagture_size(a_sign);
uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen);
memcpy(l_buf, &l_buflen, sizeof(size_t));
......@@ -217,10 +226,10 @@ dilithium_public_key_t* dap_enc_dilithium_read_public_key(const uint8_t *a_buf,
dilithium_param_t p;
if(!dilithium_params_init(&p, kind))
return NULL;
dilithium_public_key_t* l_public_key = DAP_NEW(dilithium_public_key_t);
dilithium_public_key_t* l_public_key = DAP_NEW_Z(dilithium_public_key_t);
l_public_key->kind = kind;
l_public_key->data = DAP_NEW_SIZE(unsigned char, p.CRYPTO_PUBLICKEYBYTES);
l_public_key->data = DAP_NEW_Z_SIZE(unsigned char, p.CRYPTO_PUBLICKEYBYTES);
memcpy(l_public_key->data, a_buf + sizeof(size_t) + sizeof(dilithium_kind_t), p.CRYPTO_PUBLICKEYBYTES);
return l_public_key;
}
......@@ -3,10 +3,10 @@
#include <string.h>
#include "dap_enc_key.h"
#include "dap_enc_iaes.h"
//#include "sha3/fips202.h"
#include "sha3/fips202.h"
#include "KeccakHash.h"
#include "SimpleFIPS202.h"
//#include "KeccakHash.h"
//#include "SimpleFIPS202.h"
#include "dap_common.h"
......@@ -56,8 +56,10 @@ void dap_enc_aes_key_generate(struct dap_enc_key * a_key, const void *kex_buf,
memcpy(id_concat_kex,seed, seed_size);
memcpy(id_concat_kex + seed_size, kex_buf, kex_size);
SHAKE256(a_key->priv_key_data, IAES_KEYSIZE, id_concat_kex, (kex_size + seed_size));
SHAKE128(DAP_ENC_AES_KEY(a_key)->ivec, IAES_BLOCK_SIZE, seed, seed_size);
//SHAKE256(a_key->priv_key_data, IAES_KEYSIZE, id_concat_kex, (kex_size + seed_size));
//SHAKE128(DAP_ENC_AES_KEY(a_key)->ivec, IAES_BLOCK_SIZE, seed, seed_size);
shake256(a_key->priv_key_data, IAES_KEYSIZE, id_concat_kex, (kex_size + seed_size));
shake128(DAP_ENC_AES_KEY(a_key)->ivec, IAES_BLOCK_SIZE, seed, seed_size);
free(id_concat_kex);
}
......
......@@ -410,7 +410,7 @@ int dap_enc_key_deserealize_priv_key(dap_enc_key_t *a_key, uint8_t *a_buf, size_
* @param a_buflen_out
* @return 0 Ok, -1 error
*/
int dap_enc_key_deserealize_pub_key(dap_enc_key_t *a_key,const uint8_t *a_buf, size_t a_buflen)
int dap_enc_key_deserealize_pub_key(dap_enc_key_t *a_key, uint8_t *a_buf, size_t a_buflen)
{
if(!a_key || !a_buf)
return -1;
......@@ -446,7 +446,8 @@ int dap_enc_key_deserealize_pub_key(dap_enc_key_t *a_key,const uint8_t *a_buf, s
dap_enc_sig_picnic_update(a_key);
break;
case DAP_ENC_KEY_TYPE_SIG_DILITHIUM:
dilithium_public_key_delete((dilithium_public_key_t *) a_key->pub_key_data);
if ( a_key->pub_key_data )
dilithium_public_key_delete((dilithium_public_key_t *) a_key->pub_key_data);
a_key->pub_key_data = (uint8_t*) dap_enc_dilithium_read_public_key(a_buf, a_buflen);
if(!a_key->pub_key_data)
{
......@@ -631,8 +632,10 @@ void dap_enc_key_delete(dap_enc_key_t * a_key)
log_it(L_ERROR, "delete callback is null. Can be leak memory!");
}
/* a_key->_inheritor must be cleaned in delete_callback func */
DAP_DELETE(a_key->pub_key_data);
DAP_DELETE(a_key->priv_key_data);
if ( a_key->pub_key_data)
DAP_DELETE(a_key->pub_key_data);
if (a_key->priv_key_data )
DAP_DELETE(a_key->priv_key_data);
DAP_DELETE(a_key);
}
......
......@@ -103,6 +103,11 @@ size_t dap_enc_tesla_calc_signature_size(void)
return sizeof(tesla_signature_t);
}
size_t dap_enc_tesla_calc_signature_serialized_size(tesla_signature_t* a_sign)
{
return sizeof(size_t) + sizeof(tesla_kind_t) + a_sign->sig_len + sizeof(unsigned long long);
}
/* Serialize a signature */
uint8_t* dap_enc_tesla_write_signature(tesla_signature_t* a_sign, size_t *a_sign_out)
{
......@@ -110,7 +115,7 @@ uint8_t* dap_enc_tesla_write_signature(tesla_signature_t* a_sign, size_t *a_sign
return NULL ;
}
size_t l_shift_mem = 0;
size_t l_buflen = sizeof(size_t) + sizeof(tesla_kind_t) + a_sign->sig_len + sizeof(unsigned long long);
size_t l_buflen = dap_enc_tesla_calc_signature_serialized_size(a_sign);
uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen);
memcpy(l_buf, &l_buflen, sizeof(size_t));
......
/*
* Authors:
* Dmitriy A. Gearasimov <kahovski@gmail.com>
* DeM Labs Inc. https://demlabs.net
* DeM Labs Open source community https://github.com/demlabsinc
* Copyright (c) 2017-2018
* All rights reserved.
This file is part of DAP (Deus Applications Prototypes) the open source project
DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include "dap_common.h"
#include "dap_hash.h"
#include "KeccakHash.h"
#include "SimpleFIPS202.h"
#define LOG_TAG "dap_hash"
/**
* @brief dap_chain_str_to_hash_fast_to_str
* @param a_hash_str
* @param a_hash
* @return
*/
int dap_chain_str_to_hash_fast( const char * a_hash_str, dap_chain_hash_fast_t * a_hash)
{
const size_t c_hash_str_size = sizeof(*a_hash) * 2 + 1 /*trailing zero*/+ 2 /* heading 0x */;
size_t l_hash_str_len = strlen( a_hash_str);
if ( l_hash_str_len + 1 == c_hash_str_size ){
for (size_t l_offset = 2; l_offset < l_hash_str_len; l_offset+=2 ){
if ( ( sscanf(a_hash_str+l_offset,"%02hhx",a_hash->raw+l_offset/2-1) != 1) ||
( sscanf(a_hash_str+l_offset,"%02hhX",a_hash->raw+l_offset/2-1) != 1)
)
log_it(L_ERROR,"dap_chain_str_to_hash_fast parse error: offset=%u, hash_str_len=%u, str=\"%2s\"",l_offset, l_hash_str_len, a_hash_str+l_offset);
return -10* ((int) l_offset); // Wrong char
}
return 0;
}else // Wromg string len
return -1;
}
/*
* Authors:
* Dmitriy A. Gearasimov <kahovski@gmail.com>
* DeM Labs Inc. https://demlabs.net
* DeM Labs Open source community https://github.com/demlabsinc
* Copyright (c) 2017-2018
* All rights reserved.
This file is part of DAP (Deus Applications Prototypes) the open source project
DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dap_common.h"
#include "dap_hash_fusion.h"
#define LOG_TAG "dap_hash_fusion"