fix 修复编译问题
This commit is contained in:
parent
e59d1b24d3
commit
9751e823c9
73
.clang-format
Normal file
73
.clang-format
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
# Generated from CLion C/C++ Code Style settings
|
||||||
|
BinPackParameters: false
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
AccessModifierOffset: -4
|
||||||
|
AlignAfterOpenBracket: Align
|
||||||
|
AlignConsecutiveAssignments: None
|
||||||
|
AlignOperands: DontAlign
|
||||||
|
AllowAllArgumentsOnNextLine: false
|
||||||
|
AllowAllConstructorInitializersOnNextLine: false
|
||||||
|
AllowAllParametersOfDeclarationOnNextLine: false
|
||||||
|
AllowShortBlocksOnASingleLine: Always
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
AllowShortFunctionsOnASingleLine: All
|
||||||
|
AllowShortIfStatementsOnASingleLine: true
|
||||||
|
AllowShortLambdasOnASingleLine: All
|
||||||
|
AllowShortLoopsOnASingleLine: true
|
||||||
|
AlwaysBreakTemplateDeclarations: Yes
|
||||||
|
# 函数和返回类型分两行,方便阅读
|
||||||
|
AlwaysBreakAfterReturnType: TopLevelDefinitions
|
||||||
|
BreakBeforeBraces: Custom
|
||||||
|
BraceWrapping:
|
||||||
|
AfterCaseLabel: false
|
||||||
|
AfterClass: false
|
||||||
|
AfterControlStatement: Never
|
||||||
|
AfterEnum: false
|
||||||
|
AfterFunction: true
|
||||||
|
AfterNamespace: false
|
||||||
|
AfterUnion: false
|
||||||
|
BeforeCatch: false
|
||||||
|
BeforeElse: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: false
|
||||||
|
SplitEmptyRecord: true
|
||||||
|
BreakBeforeBinaryOperators: NonAssignment
|
||||||
|
BreakBeforeTernaryOperators: true
|
||||||
|
BreakConstructorInitializers: BeforeColon
|
||||||
|
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||||
|
BreakInheritanceList: BeforeColon
|
||||||
|
ColumnLimit: 120
|
||||||
|
CompactNamespaces: false
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||||
|
SeparateDefinitionBlocks: Always
|
||||||
|
IndentCaseLabels: false
|
||||||
|
IndentPPDirectives: None
|
||||||
|
IndentWidth: 4
|
||||||
|
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||||
|
MaxEmptyLinesToKeep: 1
|
||||||
|
NamespaceIndentation: None
|
||||||
|
ObjCSpaceAfterProperty: false
|
||||||
|
ObjCSpaceBeforeProtocolList: false
|
||||||
|
PointerAlignment: Right
|
||||||
|
ReflowComments: false
|
||||||
|
SortIncludes: CaseSensitive
|
||||||
|
SpaceAfterCStyleCast: true
|
||||||
|
SpaceAfterLogicalNot: false
|
||||||
|
SpaceAfterTemplateKeyword: false
|
||||||
|
SpaceBeforeAssignmentOperators: true
|
||||||
|
SpaceBeforeCpp11BracedList: false
|
||||||
|
SpaceBeforeCtorInitializerColon: true
|
||||||
|
SpaceBeforeInheritanceColon: true
|
||||||
|
SpaceBeforeParens: ControlStatements
|
||||||
|
SpaceBeforeRangeBasedForLoopColon: true
|
||||||
|
SpaceInEmptyParentheses: false
|
||||||
|
SpacesBeforeTrailingComments: 0
|
||||||
|
SpacesInAngles: false
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInContainerLiterals: false
|
||||||
|
SpacesInParentheses: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
TabWidth: 4
|
||||||
|
UseTab: Never
|
||||||
|
PenaltyIndentedWhitespace: 1
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -10,3 +10,6 @@ release/
|
|||||||
*.len
|
*.len
|
||||||
|
|
||||||
*.pcap
|
*.pcap
|
||||||
|
.cache
|
||||||
|
out/
|
||||||
|
compile_commands.json
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
cmake_minimum_required(VERSION 3.9)
|
cmake_minimum_required(VERSION 3.9)
|
||||||
project(SecMedia VERSION 0.0.1 DESCRIPTION "Security Media Package")
|
project(
|
||||||
|
SecMedia
|
||||||
|
VERSION 0.0.1
|
||||||
|
DESCRIPTION "Security Media Package")
|
||||||
set(CMAKE_CXX_STANDARD 11)
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
|
||||||
if(${CMAKE_BUILD_TYPE} MATCHES "Release")
|
if(${CMAKE_BUILD_TYPE} MATCHES "Release")
|
||||||
@ -16,12 +19,12 @@ else()
|
|||||||
add_definitions(-DNDEBUG)
|
add_definitions(-DNDEBUG)
|
||||||
add_definitions(-DDUMP_FILE)
|
add_definitions(-DDUMP_FILE)
|
||||||
endif()
|
endif()
|
||||||
#加载自定义模块
|
# 加载自定义模块
|
||||||
add_definitions(-DSIGN_ENABLE)
|
add_definitions(-DSIGN_ENABLE)
|
||||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||||
|
|
||||||
SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/release/lib/${BuildType})
|
set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/release/lib/${BuildType})
|
||||||
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/release/out)
|
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/release/out)
|
||||||
|
|
||||||
set(CMAKE_C_VISIBILITY_PRESET hidden)
|
set(CMAKE_C_VISIBILITY_PRESET hidden)
|
||||||
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
|
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
|
||||||
@ -29,13 +32,13 @@ set(CMAKE_CXX_VISIBILITY_PRESET hidden)
|
|||||||
# set(CMAKE_CXX_FLAGS$ "${CMAKE_CXX_FLAGS} -fvisibility = hidden")
|
# set(CMAKE_CXX_FLAGS$ "${CMAKE_CXX_FLAGS} -fvisibility = hidden")
|
||||||
set(SecMedia_Root ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
set(SecMedia_Root ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
||||||
add_compile_options(-fPIC)
|
add_compile_options(-fPIC)
|
||||||
# add_compile_options(-fvisibility=hidden)
|
# add_compile_options(-fvisibility=hidden) add_compile_options(-std=c++11)
|
||||||
# add_compile_options(-std=c++11)
|
# add_compile_options(-mcpu=cortex-a7 -mfloat-abi=softfp -mfpu=neon-vfpv4
|
||||||
#add_compile_options(-mcpu=cortex-a7 -mfloat-abi=softfp -mfpu=neon-vfpv4 -mno-unaligned-access -fno-aggressive-loop-optimizations -Wcast-align)
|
# -mno-unaligned-access -fno-aggressive-loop-optimizations -Wcast-align)
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
include(GenerateExportHeader)
|
include(GenerateExportHeader)
|
||||||
|
|
||||||
#media-server ps dec enc
|
# media-server ps dec enc
|
||||||
set(MediaServer_Root ${SecMedia_Root}/3rdpart/media-server)
|
set(MediaServer_Root ${SecMedia_Root}/3rdpart/media-server)
|
||||||
include_directories(${MediaServer_Root}/librtp/include)
|
include_directories(${MediaServer_Root}/librtp/include)
|
||||||
include_directories(${MediaServer_Root}/libmpeg/include)
|
include_directories(${MediaServer_Root}/libmpeg/include)
|
||||||
@ -48,16 +51,14 @@ aux_source_directory(${MediaServer_Root}/librtp/source src_rtp)
|
|||||||
aux_source_directory(${MediaServer_Root}/librtp/payload src_rtp)
|
aux_source_directory(${MediaServer_Root}/librtp/payload src_rtp)
|
||||||
add_library(rtp STATIC ${src_rtp})
|
add_library(rtp STATIC ${src_rtp})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
include_directories(${SecMedia_Root}/SVAC/src/svac_src)
|
include_directories(${SecMedia_Root}/SVAC/src/svac_src)
|
||||||
#添加svac解密
|
# 添加svac解密
|
||||||
aux_source_directory(${SecMedia_Root}/SVAC/src/sm2sm3 src_DEC)
|
aux_source_directory(${SecMedia_Root}/SVAC/src/sm2sm3 src_DEC)
|
||||||
aux_source_directory(${SecMedia_Root}/SVAC/src/svac_src src_DEC)
|
aux_source_directory(${SecMedia_Root}/SVAC/src/svac_src src_DEC)
|
||||||
add_library(SVAC_DEC STATIC ${src_DEC})
|
add_library(SVAC_DEC STATIC ${src_DEC})
|
||||||
#添加svac加密
|
# 添加svac加密 include_directories(${DEC_ENC_Algorithm}/SVAC/svac_enc) file(GLOB
|
||||||
#include_directories(${DEC_ENC_Algorithm}/SVAC/svac_enc)
|
# src_DEC_ENC ${DEC_ENC_Algorithm}/SVAC/svac_enc/*/*.c
|
||||||
#file(GLOB src_DEC_ENC ${DEC_ENC_Algorithm}/SVAC/svac_enc/*/*.c ${DEC_ENC_Algorithm}/SVAC/svac_enc/*/*.h)
|
# ${DEC_ENC_Algorithm}/SVAC/svac_enc/*/*.h)
|
||||||
aux_source_directory(${SecMedia_Root}/SVAC/src/sm2sm3_enc src_ENC)
|
aux_source_directory(${SecMedia_Root}/SVAC/src/sm2sm3_enc src_ENC)
|
||||||
aux_source_directory(${SecMedia_Root}/SVAC/src/svac_src src_ENC)
|
aux_source_directory(${SecMedia_Root}/SVAC/src/svac_src src_ENC)
|
||||||
add_library(SVAC_ENC STATIC ${src_ENC})
|
add_library(SVAC_ENC STATIC ${src_ENC})
|
||||||
@ -65,17 +66,25 @@ add_library(SVAC_ENC STATIC ${src_ENC})
|
|||||||
list(APPEND LINK_LIB_SVAC_LIST SVAC_DEC)
|
list(APPEND LINK_LIB_SVAC_LIST SVAC_DEC)
|
||||||
list(APPEND LINK_LIB_SVAC_LIST SVAC_ENC)
|
list(APPEND LINK_LIB_SVAC_LIST SVAC_ENC)
|
||||||
|
|
||||||
#添加密码卡库
|
# 添加密码卡库 add_definitions(-DENABLE_HARDWARE_SIGN) list(APPEND LINK_LIB_SVAC_LIST
|
||||||
# add_definitions(-DENABLE_HARDWARE_SIGN)
|
# sm.so)
|
||||||
# list(APPEND LINK_LIB_SVAC_LIST sm.so)
|
|
||||||
|
|
||||||
include_directories(include src)
|
include_directories(include src)
|
||||||
|
|
||||||
# add_definitions(-DGENERATE_EXPORT)
|
# add_definitions(-DGENERATE_EXPORT)
|
||||||
|
|
||||||
#file(GLOB SecMedia_src_list ${SecMedia_Root}/SVAC/./*.c ${SecMedia_Root}/SVAC/./*.h)
|
# file(GLOB SecMedia_src_list ${SecMedia_Root}/SVAC/./*.c
|
||||||
file(GLOB SecMedia_src_list ${SecMedia_Root}/*/*.cpp ${SecMedia_Root}/*/*.h ${SecMedia_Root}/*/*.c ${SecMedia_Root}/*/*/*.cpp ${SecMedia_Root}/*/*/*.h ${SecMedia_Root}/*/*/*.c)
|
# ${SecMedia_Root}/SVAC/./*.h)
|
||||||
file(GLOB SecMedia_api_list ${CMAKE_CURRENT_SOURCE_DIR}/include/common.h )
|
file(
|
||||||
|
GLOB
|
||||||
|
SecMedia_src_list
|
||||||
|
${SecMedia_Root}/*/*.cpp
|
||||||
|
${SecMedia_Root}/*/*.h
|
||||||
|
${SecMedia_Root}/*/*.c
|
||||||
|
${SecMedia_Root}/*/*/*.cpp
|
||||||
|
${SecMedia_Root}/*/*/*.h
|
||||||
|
${SecMedia_Root}/*/*/*.c)
|
||||||
|
file(GLOB SecMedia_api_list ${CMAKE_CURRENT_SOURCE_DIR}/include/common.h)
|
||||||
|
|
||||||
# # target_compile_options(${PROJECT_NAME} PRIVATE -fvisibility=hidden)
|
# # target_compile_options(${PROJECT_NAME} PRIVATE -fvisibility=hidden)
|
||||||
# list(APPEND LINK_LIB_LIST ${LINK_LIB_SVAC_LIST})
|
# list(APPEND LINK_LIB_LIST ${LINK_LIB_SVAC_LIST})
|
||||||
@ -85,18 +94,14 @@ add_library(${PROJECT_NAME} SHARED ${SecMedia_src_list})
|
|||||||
target_link_libraries(${PROJECT_NAME} ${LINK_LIB_SVAC_LIST} rtp)
|
target_link_libraries(${PROJECT_NAME} ${LINK_LIB_SVAC_LIST} rtp)
|
||||||
target_include_directories(${PROJECT_NAME} PRIVATE ${SecMedia_Root}/.)
|
target_include_directories(${PROJECT_NAME} PRIVATE ${SecMedia_Root}/.)
|
||||||
|
|
||||||
# set_target_properties(${PROJECT_NAME} PROPERTIES
|
# set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION}
|
||||||
# VERSION ${PROJECT_VERSION}
|
# SOVERSION 1 PUBLIC_HEADER ${SecMedia_api_list} )
|
||||||
# SOVERSION 1
|
# CXX_VISIBILITY_PRESET hidden CMAKE_C_FLAGS hidden)
|
||||||
# PUBLIC_HEADER ${SecMedia_api_list}
|
|
||||||
# )
|
|
||||||
# CXX_VISIBILITY_PRESET hidden
|
|
||||||
# CMAKE_C_FLAGS hidden)
|
|
||||||
|
|
||||||
list(APPEND LINK_LIB_LIST ${PROJECT_NAME})
|
list(APPEND LINK_LIB_LIST ${PROJECT_NAME})
|
||||||
list(APPEND LINK_LIB_LIST pthread)
|
list(APPEND LINK_LIB_LIST pthread)
|
||||||
|
|
||||||
#查找jemalloc是否安装
|
# 查找jemalloc是否安装
|
||||||
find_package(JEMALLOC QUIET)
|
find_package(JEMALLOC QUIET)
|
||||||
if(JEMALLOC_FOUND)
|
if(JEMALLOC_FOUND)
|
||||||
message(STATUS "found library:\"${JEMALLOC_LIBRARIES}\"")
|
message(STATUS "found library:\"${JEMALLOC_LIBRARIES}\"")
|
||||||
@ -106,22 +111,16 @@ else()
|
|||||||
message(WARNING "JEMALLOC not found")
|
message(WARNING "JEMALLOC not found")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
# set(LINK_LIB_LIST ) MESSAGE(STATUS ${SecMedia_api_list})
|
||||||
|
# configure_file(SecMedia.pc.in SecMedia.pc @ONLY)
|
||||||
|
|
||||||
#set(LINK_LIB_LIST )
|
# install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
#MESSAGE(STATUS ${SecMedia_api_list})
|
# PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(FILES
|
||||||
#configure_file(SecMedia.pc.in SecMedia.pc @ONLY)
|
# ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc DESTINATION
|
||||||
|
# ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
|
||||||
# install(TARGETS ${PROJECT_NAME}
|
|
||||||
# LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
|
||||||
# PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
|
||||||
# install(FILES ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc
|
|
||||||
# DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
add_subdirectory(PcapSender)
|
add_subdirectory(PcapSender)
|
||||||
add_subdirectory(PcapRawSender)
|
add_subdirectory(PcapRawSender)
|
||||||
add_subdirectory(SecSetGen)
|
add_subdirectory(SecSetGen)
|
||||||
|
|
||||||
|
@ -1,16 +1,19 @@
|
|||||||
include_directories(../include)
|
include_directories(../include)
|
||||||
|
|
||||||
file(GLOB sender_src_list ./*.cpp ./*.h ../include/*.h)
|
file(GLOB sender_src_list ./*.cpp ./*.h ../include/*.h)
|
||||||
MESSAGE(STATUS ${sender_src_list})
|
message(STATUS ${sender_src_list})
|
||||||
|
|
||||||
find_package(PcapPlusPlus CONFIG REQUIRED)
|
find_package(PcapPlusPlus CONFIG REQUIRED)
|
||||||
find_package(DPDK REQUIRED)
|
# find_package(DPDK REQUIRED)
|
||||||
message(STATUS "Using Pcap++ ${PcapPlusPlus_VERSION}")
|
message(STATUS "Using Pcap++ ${PcapPlusPlus_VERSION}")
|
||||||
message(STATUS "Include dir: ${PcapPlusPlus_INCLUDE_DIR}")
|
message(STATUS "Include dir: ${PcapPlusPlus_INCLUDE_DIR}")
|
||||||
|
|
||||||
|
|
||||||
add_executable(PcapRawSender ${sender_src_list})
|
add_executable(PcapRawSender ${sender_src_list})
|
||||||
|
|
||||||
target_link_libraries(PcapRawSender PUBLIC ${LINK_LIB_LIST} PUBLIC PcapPlusPlus::Pcap++ PUBLIC DPDK::DPDK)
|
target_link_libraries(
|
||||||
|
PcapRawSender
|
||||||
|
PUBLIC ${LINK_LIB_LIST}
|
||||||
|
PUBLIC PcapPlusPlus::Pcap++
|
||||||
|
# PUBLIC DPDK::DPDK
|
||||||
|
)
|
||||||
# target_link_libraries(PcapSender PUBLIC PcapPlusPlus::Pcap++)
|
# target_link_libraries(PcapSender PUBLIC PcapPlusPlus::Pcap++)
|
||||||
|
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
include_directories(../include)
|
include_directories(../include)
|
||||||
|
|
||||||
file(GLOB sender_src_list ./*.cpp ./*.h ../include/*.h)
|
file(GLOB sender_src_list ./*.cpp ./*.h ../include/*.h)
|
||||||
MESSAGE(STATUS ${sender_src_list})
|
message(STATUS ${sender_src_list})
|
||||||
|
|
||||||
find_package(PcapPlusPlus CONFIG REQUIRED)
|
find_package(PcapPlusPlus CONFIG REQUIRED)
|
||||||
find_package(DPDK REQUIRED)
|
# find_package(DPDK REQUIRED)
|
||||||
message(STATUS "Using Pcap++ ${PcapPlusPlus_VERSION}")
|
message(STATUS "Using Pcap++ ${PcapPlusPlus_VERSION}")
|
||||||
message(STATUS "Include dir: ${PcapPlusPlus_INCLUDE_DIR}")
|
message(STATUS "Include dir: ${PcapPlusPlus_INCLUDE_DIR}")
|
||||||
|
|
||||||
|
|
||||||
add_executable(PcapSender ${sender_src_list})
|
add_executable(PcapSender ${sender_src_list})
|
||||||
|
|
||||||
target_link_libraries(PcapSender PUBLIC ${LINK_LIB_LIST} PUBLIC PcapPlusPlus::Pcap++ PUBLIC DPDK::DPDK)
|
target_link_libraries(
|
||||||
|
PcapSender
|
||||||
|
PUBLIC ${LINK_LIB_LIST}
|
||||||
|
PUBLIC PcapPlusPlus::Pcap++ # PUBLIC DPDK::DPDK
|
||||||
|
)
|
||||||
# target_link_libraries(PcapSender PUBLIC PcapPlusPlus::Pcap++)
|
# target_link_libraries(PcapSender PUBLIC PcapPlusPlus::Pcap++)
|
||||||
|
|
||||||
|
@ -1,48 +1,53 @@
|
|||||||
#ifndef L2_SECURITY_STREAM_H
|
#ifndef L2_SECURITY_STREAM_H
|
||||||
#define L2_SECURITY_STREAM_H
|
#define L2_SECURITY_STREAM_H
|
||||||
|
|
||||||
#include <thread>
|
#include "HuaWei/HWcommon.h"
|
||||||
|
#include "HuaWei/HWsec.h"
|
||||||
|
#include "HuaWei/HWsign.h"
|
||||||
|
#include "HuaWei/RTP.h"
|
||||||
|
#include "RtpDecoder.h"
|
||||||
|
#include <array>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <array>
|
|
||||||
#include "HuaWei/HWcommon.h"
|
|
||||||
#include "HuaWei/RTP.h"
|
|
||||||
#include "HuaWei/HWsign.h"
|
|
||||||
#include "HuaWei/HWsec.h"
|
|
||||||
#include "RtpDecoder.h"
|
|
||||||
|
|
||||||
class L2SecurityStream
|
class L2SecurityStream {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
explicit L2SecurityStream(sec_set_info* set_info);
|
explicit L2SecurityStream(sec_set_info *set_info);
|
||||||
~L2SecurityStream(){
|
|
||||||
_thread_exit=1;
|
virtual ~L2SecurityStream()
|
||||||
_keyframe_seq=-2;
|
{
|
||||||
|
_thread_exit = 1;
|
||||||
|
_keyframe_seq = -2;
|
||||||
_sign_start.notify_all();
|
_sign_start.notify_all();
|
||||||
usleep(10000);
|
usleep(10000);
|
||||||
};
|
};
|
||||||
|
|
||||||
void ThreadSign();
|
void ThreadSign();
|
||||||
|
|
||||||
virtual int RtpInput(const uint8_t* data,uint16_t len, void *param=nullptr)=0;
|
virtual int RtpInput(const uint8_t *data, uint16_t len, void *param = nullptr) = 0;
|
||||||
virtual int RtpOutput(char * buf, uint32_t * len, uint16_t* sei_tail_pos, uint16_t* extra_len, void ** param);
|
virtual int RtpOutput(char *buf, uint32_t *len, uint16_t *sei_tail_pos, uint16_t *extra_len, void **param);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint8_t * gen_sec_sei(uint8_t *ptr);
|
uint8_t *gen_sec_sei(uint8_t *ptr);
|
||||||
void addSignData(const char * data, uint32_t len,uint32_t dts,int codectype ){
|
|
||||||
ver_set.total_hash_data_len=len;
|
void addSignData(const char *data, uint32_t len, uint32_t dts, int codectype)
|
||||||
ver_set.head_frame_dts=dts;
|
{
|
||||||
ver_set.head_frame_type=codectype;
|
ver_set.total_hash_data_len = len;
|
||||||
ver_set.end_flag=0x80;
|
ver_set.head_frame_dts = dts;
|
||||||
|
ver_set.head_frame_type = codectype;
|
||||||
|
ver_set.end_flag = 0x80;
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_sign_process_mtx);
|
std::lock_guard<std::mutex> lock(_sign_process_mtx);
|
||||||
}
|
}
|
||||||
_start_sign=true;
|
_start_sign = true;
|
||||||
_keyframe_seq=-2;
|
_keyframe_seq = -2;
|
||||||
_keyframe_complete=true;
|
_keyframe_complete = true;
|
||||||
_keyframe_DATA.assign(data,len);
|
_keyframe_DATA.assign(data, len);
|
||||||
_sign_start.notify_one();
|
_sign_start.notify_one();
|
||||||
INFOL("_sign_start.notify_one() dts:%u\n",dts);
|
INFOL("_sign_start.notify_one() dts:%u\n", dts);
|
||||||
// _on_sign_flag=false;
|
// _on_sign_flag=false;
|
||||||
// {
|
// {
|
||||||
|
|
||||||
@ -51,17 +56,19 @@ protected:
|
|||||||
usleep(1000);
|
usleep(1000);
|
||||||
// sleep(1);
|
// sleep(1);
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char _class_name[17]="L2SecurityStream";
|
const char _class_name[17] = "L2SecurityStream";
|
||||||
std::array<std::thread *,1> _sign_thread_list;
|
std::array<std::thread *, 1> _sign_thread_list;
|
||||||
// std::thread * _sign_thread;
|
// std::thread * _sign_thread;
|
||||||
protected:
|
protected:
|
||||||
CodecId _code_id;
|
CodecId _code_id;
|
||||||
uint32_t _all_in=0;
|
uint32_t _all_in = 0;
|
||||||
uint32_t _all_out=0;
|
uint32_t _all_out = 0;
|
||||||
bool _keyframe_complete=false;
|
bool _keyframe_complete = false;
|
||||||
uint16_t _sei_race_len;
|
uint16_t _sei_race_len;
|
||||||
HWsec_queue _package_queue;
|
HWsec_queue _package_queue;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
uint8_t _sei_race_buf[4096];
|
uint8_t _sei_race_buf[4096];
|
||||||
uint8_t *_sei_buf_ptr;
|
uint8_t *_sei_buf_ptr;
|
||||||
@ -78,70 +85,74 @@ public:
|
|||||||
std::mutex _sign_process_mtx;
|
std::mutex _sign_process_mtx;
|
||||||
// std::condition_variable _on_sign;
|
// std::condition_variable _on_sign;
|
||||||
// bool _on_sign_flag=true;
|
// bool _on_sign_flag=true;
|
||||||
bool _start_sign=false;
|
bool _start_sign = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class L2UdpStream : public L2SecurityStream {
|
||||||
class L2UdpStream: public L2SecurityStream
|
|
||||||
{
|
|
||||||
protected:
|
protected:
|
||||||
uint16_t _seq_increment=0;
|
uint16_t _seq_increment = 0;
|
||||||
RtpDecoder _rtp_decoder;
|
RtpDecoder _rtp_decoder;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t _prv_seq=0;
|
uint16_t _prv_seq = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
L2UdpStream(sec_set_info* set_info):L2SecurityStream(set_info){
|
L2UdpStream(sec_set_info *set_info) : L2SecurityStream(set_info)
|
||||||
_rtp_decoder.setOnDecode([&](const uint8_t *packet, int bytes, uint32_t timestamp, int flags){
|
{
|
||||||
on_rtp_out(packet,bytes,timestamp,flags);
|
_rtp_decoder.setOnDecode([&](const uint8_t *packet, int bytes, uint32_t timestamp, int flags) {
|
||||||
|
on_rtp_out(packet, bytes, timestamp, flags);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
int RtpInput(const uint8_t* data,uint16_t len, void *param=nullptr)override ;
|
|
||||||
virtual void on_rtp_out(const uint8_t *packet, int bytes, uint32_t timestamp, int flags)=0;
|
int RtpInput(const uint8_t *data, uint16_t len, void *param = nullptr) override;
|
||||||
|
virtual void on_rtp_out(const uint8_t *packet, int bytes, uint32_t timestamp, int flags) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
virtual void rtp_post_process(const RTPcell::Ptr &rtp_cell) = 0;
|
||||||
|
|
||||||
virtual void rtp_post_process(const RTPcell::Ptr &rtp_cell)=0;
|
uint8_t *addRTPheader(const RTPcell::Ptr &rtp_cell, uint8_t *buf_ptr, uint16_t buf_remain_len)
|
||||||
uint8_t * addRTPheader(const RTPcell::Ptr &rtp_cell,uint8_t * buf_ptr,uint16_t buf_remain_len){
|
{
|
||||||
if (rtp_cell->_prefix>buf_remain_len) return nullptr;
|
if (rtp_cell->_prefix > buf_remain_len) return nullptr;
|
||||||
memcpy(buf_ptr,rtp_cell->_buffer,rtp_cell->_prefix); //copy previous rtp header
|
memcpy(buf_ptr, rtp_cell->_buffer,
|
||||||
_seq_increment%=UINT16_MAX;
|
rtp_cell->_prefix);// copy previous rtp header
|
||||||
|
_seq_increment %= UINT16_MAX;
|
||||||
_seq_increment++;
|
_seq_increment++;
|
||||||
uint16_t seq=((uint32_t)_seq_increment+rtp_cell->seq)%((uint32_t)UINT16_MAX+1);
|
uint16_t seq = ((uint32_t) _seq_increment + rtp_cell->seq) % ((uint32_t) UINT16_MAX + 1);
|
||||||
|
|
||||||
// if( (uint32_t)(_prv_seq+1)%(uint32_t)(UINT16_MAX+1) == seq || _prv_seq==0){
|
// if( (uint32_t)(_prv_seq+1)%(uint32_t)(UINT16_MAX+1) == seq ||
|
||||||
|
// _prv_seq==0){
|
||||||
// _prv_seq=seq;
|
// _prv_seq=seq;
|
||||||
// }else{
|
// }else{
|
||||||
// _prv_seq=seq;
|
// _prv_seq=seq;
|
||||||
// ERROL("OUTPUT Disorder rtp Seq: %u+1 != %u \n",_prv_seq,seq);
|
// ERROL("OUTPUT Disorder rtp Seq: %u+1 != %u \n",_prv_seq,seq);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if(seq<rtp_cell->seq){
|
if (seq < rtp_cell->seq) { _seq_increment = 0; }
|
||||||
_seq_increment=0;
|
|
||||||
}
|
|
||||||
// DEBUGL("increase seq:%u inc:%u\n",seq,_seq_increment);
|
// DEBUGL("increase seq:%u inc:%u\n",seq,_seq_increment);
|
||||||
seq=htons(seq);
|
seq = htons(seq);
|
||||||
memcpy(buf_ptr+rtp_cell->_tcp_prefix+2,(uint8_t*)&seq,2); // change sequence
|
memcpy(buf_ptr + rtp_cell->_tcp_prefix + 2, (uint8_t *) &seq,
|
||||||
return buf_ptr+rtp_cell->_prefix;
|
2);// change sequence
|
||||||
|
return buf_ptr + rtp_cell->_prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void incrementRTPseq(const RTPcell::Ptr &rtp_cell) {
|
void incrementRTPseq(const RTPcell::Ptr &rtp_cell)
|
||||||
uint16_t seq=((uint32_t)_seq_increment+(uint32_t)rtp_cell->seq)%((uint32_t)UINT16_MAX+1);
|
{
|
||||||
|
uint16_t seq = ((uint32_t) _seq_increment + (uint32_t) rtp_cell->seq) % ((uint32_t) UINT16_MAX + 1);
|
||||||
// DEBUGL("modify seq:%u old:%u inc:%u\n",seq,rtp_cell->seq,_seq_increment);
|
// DEBUGL("modify seq:%u old:%u inc:%u\n",seq,rtp_cell->seq,_seq_increment);
|
||||||
// if( (uint32_t)(_prv_seq+1)%(uint32_t)(UINT16_MAX+1) == seq || _prv_seq==0){
|
// if( (uint32_t)(_prv_seq+1)%(uint32_t)(UINT16_MAX+1) == seq ||
|
||||||
|
// _prv_seq==0){
|
||||||
// _prv_seq=seq;
|
// _prv_seq=seq;
|
||||||
// }else{
|
// }else{
|
||||||
// _prv_seq=seq;
|
// _prv_seq=seq;
|
||||||
// ERROL("OUTPUT Disorder rtp Seq: %u+1 != %u \n",_prv_seq,seq);
|
// ERROL("OUTPUT Disorder rtp Seq: %u+1 != %u \n",_prv_seq,seq);
|
||||||
// }
|
// }
|
||||||
if(rtp_cell->_RTPpkg_ptr){
|
if (rtp_cell->_RTPpkg_ptr) {
|
||||||
seq=htons(seq);
|
seq = htons(seq);
|
||||||
rtp_cell->_RTPpkg_ptr->modifyData(rtp_cell->_RTPpkg_head_pos+rtp_cell->_tcp_prefix+2,(uint8_t*)&seq,2);
|
rtp_cell->_RTPpkg_ptr->modifyData(
|
||||||
|
rtp_cell->_RTPpkg_head_pos + rtp_cell->_tcp_prefix + 2, (uint8_t *) &seq, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,34 +1,35 @@
|
|||||||
#ifndef RTP_DECODER_H
|
#ifndef RTP_DECODER_H
|
||||||
#define RTP_DECODER_H
|
#define RTP_DECODER_H
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include "HuaWei/HWcommon.h"
|
#include "HuaWei/HWcommon.h"
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class RtpDecoder {
|
class RtpDecoder {
|
||||||
public:
|
public:
|
||||||
typedef function<void(const uint8_t *packet, int bytes, uint32_t timestamp, int flags)> on_decode_t;
|
typedef function<void(const uint8_t *packet, int bytes, uint32_t timestamp, int flags)> on_decode_t;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RtpDecoder(){
|
RtpDecoder() {
|
||||||
|
|
||||||
};
|
};
|
||||||
~RtpDecoder();
|
~RtpDecoder();
|
||||||
void decodeRtp(const void *data, int bytes);
|
void decodeRtp(const void *data, int bytes);
|
||||||
void setOnDecode(on_decode_t cb){
|
|
||||||
_on_decoded=cb;
|
void setOnDecode(on_decode_t cb) { _on_decoded = cb; };
|
||||||
};
|
|
||||||
private:
|
private:
|
||||||
void onDecode(const uint8_t *packet, int bytes, uint32_t timestamp, int flags){
|
void onDecode(const uint8_t *packet, int bytes, uint32_t timestamp, int flags)
|
||||||
if(_on_decoded){
|
{
|
||||||
_on_decoded(packet,bytes,timestamp,flags);
|
if (_on_decoded) { _on_decoded(packet, bytes, timestamp, flags); }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void *_rtp_decoder = nullptr;
|
void *_rtp_decoder = nullptr;
|
||||||
std::vector<uint8_t> _buffer;
|
std::vector<uint8_t> _buffer;
|
||||||
string _codec="MP2P";
|
std::string _codec = "MP2P";
|
||||||
uint8_t _rtp_type;
|
uint8_t _rtp_type;
|
||||||
on_decode_t _on_decoded;
|
on_decode_t _on_decoded;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
#include "StreamCodec.h"
|
#include "StreamCodec.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
void stream_split(const char *ptr, int len, int prefix, const std::function<void(const char *, int, int)> &cb) {
|
|
||||||
|
void
|
||||||
|
stream_split(const char *ptr, int len, int prefix, const std::function<void(const char *, int, int)> &cb)
|
||||||
|
{
|
||||||
auto start = ptr + prefix;
|
auto start = ptr + prefix;
|
||||||
auto end = ptr + len;
|
auto end = ptr + len;
|
||||||
int next_prefix;
|
int next_prefix;
|
||||||
while (true) {
|
while (true) {
|
||||||
//auto next_start = memfind(start, end - start, "\x00\x00\x01", 3);
|
//auto next_start = memfind(start, end - start, "\x00\x00\x01", 3);
|
||||||
auto next_start =(char*)memmem(start, end - start-3,"\x00\x00\x01",3 );
|
auto next_start = (char *) memmem(start, end - start - 3, "\x00\x00\x01", 3);
|
||||||
if (next_start) {
|
if (next_start) {
|
||||||
//找到下一帧
|
//找到下一帧
|
||||||
if (*(next_start - 1) == 0x00) {
|
if (*(next_start - 1) == 0x00) {
|
||||||
@ -31,13 +34,20 @@ void stream_split(const char *ptr, int len, int prefix, const std::function<void
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool stream_split_nd(const char *ptr, int len, int prefix, const uint8_t* needle,const uint8_t size, const std::function<bool(const char *, int, int)> &cb) {
|
bool
|
||||||
|
stream_split_nd(const char *ptr,
|
||||||
|
int len,
|
||||||
|
int prefix,
|
||||||
|
const uint8_t *needle,
|
||||||
|
const uint8_t size,
|
||||||
|
const std::function<bool(const char *, int, int)> &cb)
|
||||||
|
{
|
||||||
auto start = ptr + prefix;
|
auto start = ptr + prefix;
|
||||||
auto end = ptr + len;
|
auto end = ptr + len;
|
||||||
int next_prefix;
|
int next_prefix;
|
||||||
while (true) {
|
while (true) {
|
||||||
//auto next_start = memfind(start, end - start, "\x00\x00\x01", 3);
|
//auto next_start = memfind(start, end - start, "\x00\x00\x01", 3);
|
||||||
auto next_start =(char*)memmem(start, end - start-size,needle,size );
|
auto next_start = (char *) memmem(start, end - start - size, needle, size);
|
||||||
if (next_start) {
|
if (next_start) {
|
||||||
//找到下一帧
|
//找到下一帧
|
||||||
if (*(next_start - 1) == 0x00) {
|
if (*(next_start - 1) == 0x00) {
|
||||||
@ -49,7 +59,7 @@ bool stream_split_nd(const char *ptr, int len, int prefix, const uint8_t* needle
|
|||||||
next_prefix = 3;
|
next_prefix = 3;
|
||||||
}
|
}
|
||||||
//记得加上本帧prefix长度
|
//记得加上本帧prefix长度
|
||||||
if(cb(start - prefix, next_start - start + prefix, prefix)) return true;
|
if (cb(start - prefix, next_start - start + prefix, prefix)) return true;
|
||||||
//搜索下一帧末尾的起始位置
|
//搜索下一帧末尾的起始位置
|
||||||
start = next_start + next_prefix;
|
start = next_start + next_prefix;
|
||||||
//记录下一帧的prefix长度
|
//记录下一帧的prefix长度
|
||||||
@ -60,12 +70,13 @@ bool stream_split_nd(const char *ptr, int len, int prefix, const uint8_t* needle
|
|||||||
cb(start - prefix, end - start + prefix, prefix);
|
cb(start - prefix, end - start + prefix, prefix);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int prefixSize(const char *ptr, int len){
|
int
|
||||||
if (len < 4) {
|
prefixSize(const char *ptr, int len)
|
||||||
return 0;
|
{
|
||||||
}
|
if (len < 4) { return 0; }
|
||||||
|
|
||||||
if (ptr[0] != 0x00 || ptr[1] != 0x00) {
|
if (ptr[0] != 0x00 || ptr[1] != 0x00) {
|
||||||
//不是0x00 00开头
|
//不是0x00 00开头
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
#ifndef STREAM_CODEC_H
|
#ifndef STREAM_CODEC_H
|
||||||
#define STREAM_CODEC_H
|
#define STREAM_CODEC_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
void stream_split(const char *ptr, int len, int prefix, const std::function<void(const char *, int, int)> &cb);
|
void stream_split(const char *ptr, int len, int prefix, const std::function<void(const char *, int, int)> &cb);
|
||||||
int prefixSize(const char *ptr, int len);
|
int prefixSize(const char *ptr, int len);
|
||||||
|
|
||||||
bool stream_split_nd(const char *ptr, int len, int prefix, const uint8_t* needle,const uint8_t size, const std::function<bool(const char *, int, int)> &cb) ;
|
bool stream_split_nd(const char *ptr,
|
||||||
|
int len,
|
||||||
|
int prefix,
|
||||||
|
const uint8_t *needle,
|
||||||
|
const uint8_t size,
|
||||||
|
const std::function<bool(const char *, int, int)> &cb);
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,296 +1,293 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "common.h"
|
|
||||||
#include "HWcommon.h"
|
#include "HWcommon.h"
|
||||||
#include "HuaWei/HWsign.h"
|
#include "HuaWei/HWsign.h"
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
#include "DecEnc/NALUdecode.h"
|
#include "DecEnc/NALUdecode.h"
|
||||||
#include "DecEnc/base64.h"
|
#include "DecEnc/base64.h"
|
||||||
|
|
||||||
|
void
|
||||||
void clear_all(HWsign* HWSign_hd){
|
clear_all(HWsign *HWSign_hd)
|
||||||
|
{
|
||||||
HWSign_hd->buff->clear();
|
HWSign_hd->buff->clear();
|
||||||
HWSign_hd->rtp_buff->clear();
|
HWSign_hd->rtp_buff->clear();
|
||||||
DEBUGL("clear verify set!!!!!!!!!! \n");
|
DEBUGL("clear verify set!!!!!!!!!! \n");
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void find_sign_sei(HWsign* HWSign_hd,uint16_t seq,const char * buf, const uint32_t len){
|
inline void
|
||||||
char * ptr;
|
find_sign_sei(HWsign *HWSign_hd, uint16_t seq, const char *buf, const uint32_t len)
|
||||||
const uint8_t uuid[] = { 0x86,0xb1 ,0x16 ,0x6e ,0xad ,0xef ,0x67 ,0x3f ,0x70 ,0xbc ,0xe7 ,0xc7 ,0xe6 ,0x95 ,0xc6 ,0x09 };
|
{
|
||||||
|
char *ptr;
|
||||||
|
const uint8_t uuid[] = {
|
||||||
|
0x86, 0xb1, 0x16, 0x6e, 0xad, 0xef, 0x67, 0x3f, 0x70, 0xbc, 0xe7, 0xc7, 0xe6, 0x95, 0xc6, 0x09};
|
||||||
char sign_drace[1024];
|
char sign_drace[1024];
|
||||||
auto next_start =(char*)memmem(buf, len,uuid,sizeof(uuid));
|
auto next_start = (char *) memmem(buf, len, uuid, sizeof(uuid));
|
||||||
if(next_start){
|
if (next_start) {
|
||||||
if(!(next_start-2 > buf && *(next_start-2)==0x05)) return;
|
if (!(next_start - 2 > buf && *(next_start - 2) == 0x05)) return;
|
||||||
auto remain_len=(len-(next_start-buf));
|
auto remain_len = (len - (next_start - buf));
|
||||||
remain_len=del_racing_code((uint8_t *)sign_drace,(uint8_t *)next_start,remain_len);
|
remain_len = del_racing_code((uint8_t *) sign_drace, (uint8_t *) next_start, remain_len);
|
||||||
uint8_t sign_set_len=*(next_start-1);
|
uint8_t sign_set_len = *(next_start - 1);
|
||||||
if(remain_len<sign_set_len) return;
|
if (remain_len < sign_set_len) return;
|
||||||
ptr=sign_drace +16 +1 +4 +16;
|
ptr = sign_drace + 16 + 1 + 4 + 16;
|
||||||
// ptr=sign_drace +16 +1 +4 +16; //+ uuid + security_byte + vkek +vek
|
// ptr=sign_drace +16 +1 +4 +16; //+ uuid + security_byte + vkek +vek
|
||||||
HWSign_hd->I_len= *(ptr++)<<24 | *(ptr++)<<16 | *(ptr++)<<8 | *(ptr++);
|
HWSign_hd->I_len = *(ptr++) << 24 | *(ptr++) << 16 | *(ptr++) << 8 | *(ptr++);
|
||||||
HWSign_hd->sei_len=*(ptr++);
|
HWSign_hd->sei_len = *(ptr++);
|
||||||
if(ptr+HWSign_hd->sei_len>buf+len) {
|
if (ptr + HWSign_hd->sei_len > buf + len) {
|
||||||
HWSign_hd->sei_len=0;
|
HWSign_hd->sei_len = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
memcpy(HWSign_hd->sei_rtp_head,ptr,HWSign_hd->sei_len);
|
memcpy(HWSign_hd->sei_rtp_head, ptr, HWSign_hd->sei_len);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int verify_data(HWsign* HWSign_hd){
|
int
|
||||||
if(HWSign_hd->buff->empty()) return -2;
|
verify_data(HWsign *HWSign_hd)
|
||||||
if(HWSign_hd->sei_len==0) return -2;
|
{
|
||||||
if(HWSign_hd->I_len!=HWSign_hd->buff->size()) return -2;
|
if (HWSign_hd->buff->empty()) return -2;
|
||||||
|
if (HWSign_hd->sei_len == 0) return -2;
|
||||||
|
if (HWSign_hd->I_len != HWSign_hd->buff->size()) return -2;
|
||||||
uint8_t sha[128];
|
uint8_t sha[128];
|
||||||
|
|
||||||
sm3_update(HWSign_hd->sm3_hd,(uint8_t*)HWSign_hd->buff->data(),HWSign_hd->I_len);
|
sm3_update(HWSign_hd->sm3_hd, (uint8_t *) HWSign_hd->buff->data(), HWSign_hd->I_len);
|
||||||
sm3_final(HWSign_hd->sm3_hd,sha);
|
sm3_final(HWSign_hd->sm3_hd, sha);
|
||||||
DEBUGL("\n&&&&&&&& sign: hash &&&&&&&&&&&&&&&&&&&&&&&&");
|
DEBUGL("\n&&&&&&&& sign: hash &&&&&&&&&&&&&&&&&&&&&&&&");
|
||||||
print_data2((char*)sha,32,32);
|
print_data2((char *) sha, 32, 32);
|
||||||
DEBUGL("\n&&&&&&&& sign: sign &&&&&&&&&&&&&&&&&&&&&&&&");
|
DEBUGL("\n&&&&&&&& sign: sign &&&&&&&&&&&&&&&&&&&&&&&&");
|
||||||
print_data2((char*)HWSign_hd->sei_rtp_head,HWSign_hd->sei_len,HWSign_hd->sei_len);
|
print_data2((char *) HWSign_hd->sei_rtp_head, HWSign_hd->sei_len, HWSign_hd->sei_len);
|
||||||
|
|
||||||
|
if (do_sm2_verify((char *) HWSign_hd->sm2_hd->pubkey, (char *) sha, 32, (char *) HWSign_hd->sei_rtp_head) == 0)
|
||||||
if(do_sm2_verify((char*)HWSign_hd->sm2_hd->pubkey,(char*)sha,32,(char*)HWSign_hd->sei_rtp_head)==0) return 1;
|
return 1;
|
||||||
else return -1;
|
else
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void * HWVerify_init()
|
void *
|
||||||
|
HWVerify_init()
|
||||||
{
|
{
|
||||||
HWsign* HWSign_hd= new HWsign();
|
HWsign *HWSign_hd = new HWsign();
|
||||||
HWSign_hd->I_seq=-1;
|
HWSign_hd->I_seq = -1;
|
||||||
HWSign_hd->in=0;
|
HWSign_hd->in = 0;
|
||||||
HWSign_hd->out=0;
|
HWSign_hd->out = 0;
|
||||||
HWSign_hd->seq_accu=0;
|
HWSign_hd->seq_accu = 0;
|
||||||
HWSign_hd->stamp=0;
|
HWSign_hd->stamp = 0;
|
||||||
HWSign_hd->buff_offest=0;
|
HWSign_hd->buff_offest = 0;
|
||||||
HWSign_hd->track_type=CodecInvalid;
|
HWSign_hd->track_type = CodecInvalid;
|
||||||
HWSign_hd->sm3_hd=new sm3_ctx();
|
HWSign_hd->sm3_hd = new sm3_ctx();
|
||||||
HWSign_hd->sm2_hd=new SM2Config();
|
HWSign_hd->sm2_hd = new SM2Config();
|
||||||
HWSign_hd->buff=new string();
|
HWSign_hd->buff = new string();
|
||||||
HWSign_hd->rtp_buff = new list<pair<shared_ptr<string>,void *>>();
|
HWSign_hd->rtp_buff = new list<pair<shared_ptr<string>, void *>>();
|
||||||
HWSign_hd->buff_que =new ThreadsafeQueue<pair<shared_ptr<string>,void *>>();
|
HWSign_hd->buff_que = new ThreadsafeQueue<pair<shared_ptr<string>, void *>>();
|
||||||
// HWSign_hd->rtp_cb=rtp_callback;
|
// HWSign_hd->rtp_cb=rtp_callback;
|
||||||
HWSign_hd->rtp_cb=nullptr;
|
HWSign_hd->rtp_cb = nullptr;
|
||||||
sm3_init(HWSign_hd->sm3_hd);
|
sm3_init(HWSign_hd->sm3_hd);
|
||||||
SM2Config sm2fig={
|
SM2Config sm2fig = {
|
||||||
32,
|
32,
|
||||||
64,
|
64,
|
||||||
|
{0x24, 0x88, 0xc8, 0xdc, 0x7f, 0xd7, 0xe0, 0x91, 0x30, 0x1b, 0x5c, 0x58, 0x2f, 0xe7, 0x44, 0x7d,
|
||||||
|
0x2f, 0x43, 0xe4, 0xee, 0xc8, 0x7d, 0xc0, 0xfb, 0xa4, 0xb8, 0x7d, 0x4b, 0x8a, 0x69, 0x7c, 0x4e},
|
||||||
{
|
{
|
||||||
0x24,0x88,0xc8,0xdc,0x7f,0xd7,0xe0,0x91,0x30,0x1b,0x5c,0x58,0x2f,0xe7,0x44,0x7d,
|
0xaa, 0xb1, 0x3f, 0xd7, 0x66, 0xe2, 0x75, 0x97, 0xc0, 0x03, 0xe6, 0xe4, 0x1d, 0x77, 0x54, 0x78,
|
||||||
0x2f,0x43,0xe4,0xee,0xc8,0x7d,0xc0,0xfb,0xa4,0xb8,0x7d,0x4b,0x8a,0x69,0x7c,0x4e
|
0xc8, 0x29, 0xb2, 0x0b, 0x9e, 0xd1, 0xff, 0xa3, 0x6a, 0x6f, 0xd2, 0x7f, 0xd6, 0x2d, 0xaa, 0x3f,
|
||||||
},
|
0xc9, 0x24, 0xec, 0x6c, 0x96, 0x0a, 0x7b, 0x73, 0xf6, 0xe6, 0xfc, 0xda, 0x3a, 0x08, 0xfd, 0x92,
|
||||||
{
|
0xfc, 0x00, 0x08, 0x97, 0x78, 0x2c, 0x71, 0x6b, 0xe1, 0x26, 0xf5, 0x1e, 0xba, 0x31, 0xf5, 0xb2,
|
||||||
0xaa,0xb1,0x3f,0xd7,0x66,0xe2,0x75,0x97,0xc0,0x03,0xe6,0xe4,0x1d,0x77,0x54,0x78,
|
}};
|
||||||
0xc8,0x29,0xb2,0x0b,0x9e,0xd1,0xff,0xa3,0x6a,0x6f,0xd2,0x7f,0xd6,0x2d,0xaa,0x3f,
|
HWSign_hd->sm2_hd->prikey_size = 32;
|
||||||
0xc9,0x24,0xec,0x6c,0x96,0x0a,0x7b,0x73,0xf6,0xe6,0xfc,0xda,0x3a,0x08,0xfd,0x92,
|
HWSign_hd->sm2_hd->pubkey_size = 64;
|
||||||
0xfc,0x00,0x08,0x97,0x78,0x2c,0x71,0x6b,0xe1,0x26,0xf5,0x1e,0xba,0x31,0xf5,0xb2,
|
memcpy(HWSign_hd->sm2_hd->prikey, sm2fig.prikey, sizeof(sm2fig.prikey));
|
||||||
}
|
memcpy(HWSign_hd->sm2_hd->pubkey, sm2fig.pubkey, sizeof(sm2fig.pubkey));
|
||||||
};
|
return (void *) HWSign_hd;
|
||||||
HWSign_hd->sm2_hd->prikey_size=32;
|
|
||||||
HWSign_hd->sm2_hd->pubkey_size=64;
|
|
||||||
memcpy(HWSign_hd->sm2_hd->prikey,sm2fig.prikey,sizeof(sm2fig.prikey));
|
|
||||||
memcpy(HWSign_hd->sm2_hd->pubkey,sm2fig.pubkey,sizeof(sm2fig.pubkey));
|
|
||||||
return (void*)HWSign_hd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HWVerify_release(void* Handle)
|
void
|
||||||
|
HWVerify_release(void *Handle)
|
||||||
{
|
{
|
||||||
HWsign* HWSign_hd=(HWsign*) Handle;
|
HWsign *HWSign_hd = (HWsign *) Handle;
|
||||||
delete HWSign_hd->sm3_hd;
|
delete HWSign_hd->sm3_hd;
|
||||||
delete HWSign_hd->sm2_hd;
|
delete HWSign_hd->sm2_hd;
|
||||||
delete HWSign_hd->buff;
|
delete HWSign_hd->buff;
|
||||||
delete HWSign_hd->rtp_buff;
|
delete HWSign_hd->rtp_buff;
|
||||||
HWSign_hd->rtp_cb=nullptr;
|
HWSign_hd->rtp_cb = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int HWVerify_rtp_264(HWsign* HWSign_hd, const char * buf, const uint32_t len,int tcp, void * param){
|
int
|
||||||
|
HWVerify_rtp_264(HWsign *HWSign_hd, const char *buf, const uint32_t len, int tcp, void *param)
|
||||||
|
{
|
||||||
uint8_t offset;
|
uint8_t offset;
|
||||||
if (tcp) offset=16;
|
if (tcp)
|
||||||
else offset=12;
|
offset = 16;
|
||||||
|
else
|
||||||
|
offset = 12;
|
||||||
|
|
||||||
const char * rtp=buf+offset;
|
const char *rtp = buf + offset;
|
||||||
int length = len- offset;
|
int length = len - offset;
|
||||||
int nal_type = *rtp & 0x1F;
|
int nal_type = *rtp & 0x1F;
|
||||||
int nal_suffix = *rtp & (~0x1F);
|
int nal_suffix = *rtp & (~0x1F);
|
||||||
|
|
||||||
uint16_t now_seq= get_sequence(buf,tcp);
|
uint16_t now_seq = get_sequence(buf, tcp);
|
||||||
DEBUGL("\n###### input ##########################");
|
DEBUGL("\n###### input ##########################");
|
||||||
print_rtp2(buf,len,offset);
|
print_rtp2(buf, len, offset);
|
||||||
|
|
||||||
if (nal_type == 5 || nal_type == 7 || nal_type == 8 ) {
|
if (nal_type == 5 || nal_type == 7 || nal_type == 8) { return 0; }
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (nal_type){
|
switch (nal_type) {
|
||||||
case 6:{
|
case 6: {
|
||||||
DEBUGL("SEI!!!!!!!!!! package\n");
|
DEBUGL("SEI!!!!!!!!!! package\n");
|
||||||
find_sign_sei(HWSign_hd,now_seq,rtp,length);
|
find_sign_sei(HWSign_hd, now_seq, rtp, length);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
case 28:{
|
case 28: {
|
||||||
FU fu;
|
FU fu;
|
||||||
MakeFU((uint8_t)rtp[1], fu);
|
MakeFU((uint8_t) rtp[1], fu);
|
||||||
|
|
||||||
if (fu.type!=5)
|
if (fu.type != 5) { return 0; }
|
||||||
{
|
if (fu.S) {//第一个rtp包
|
||||||
return 0;
|
if (!HWSign_hd->buff->empty()) {
|
||||||
}
|
|
||||||
if (fu.S) { //第一个rtp包
|
|
||||||
if(!HWSign_hd->buff->empty()){
|
|
||||||
WRNGL("Warning!!!!!!!!!! missing package\n");
|
WRNGL("Warning!!!!!!!!!! missing package\n");
|
||||||
clear_all(HWSign_hd);
|
clear_all(HWSign_hd);
|
||||||
}
|
}
|
||||||
DEBUGL("!!!!!!!!!! I head\n");
|
DEBUGL("!!!!!!!!!! I head\n");
|
||||||
HWSign_hd->I_seq=now_seq;
|
HWSign_hd->I_seq = now_seq;
|
||||||
HWSign_hd->buff->assign(rtp+2,length-2);
|
HWSign_hd->buff->assign(rtp + 2, length - 2);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(HWSign_hd->I_seq>=0){
|
if (HWSign_hd->I_seq >= 0) {
|
||||||
HWSign_hd->buff->append(rtp+2,length-2);
|
HWSign_hd->buff->append(rtp + 2, length - 2);
|
||||||
|
|
||||||
if (!fu.E) { //中间rtp包
|
if (!fu.E) {//中间rtp包
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
///verify here!!!!!!!!!!!!!!!!
|
///verify here!!!!!!!!!!!!!!!!
|
||||||
int vef_ret=-2;
|
int vef_ret = -2;
|
||||||
if(HWSign_hd->I_len==HWSign_hd->buff->size()){
|
if (HWSign_hd->I_len == HWSign_hd->buff->size()) { vef_ret = verify_data(HWSign_hd); }
|
||||||
vef_ret=verify_data(HWSign_hd);
|
|
||||||
}
|
|
||||||
clear_all(HWSign_hd);
|
clear_all(HWSign_hd);
|
||||||
HWSign_hd->sei_len=0;
|
HWSign_hd->sei_len = 0;
|
||||||
HWSign_hd->I_seq=-1;
|
HWSign_hd->I_seq = -1;
|
||||||
return vef_ret;
|
return vef_ret;
|
||||||
}else{
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
default:{
|
default: {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
int HWVerify_rtp_265(HWsign* HWSign_hd, const char * buf, const uint32_t len,int tcp, void * param){
|
HWVerify_rtp_265(HWsign *HWSign_hd, const char *buf, const uint32_t len, int tcp, void *param)
|
||||||
|
{
|
||||||
uint8_t offset;
|
uint8_t offset;
|
||||||
if (tcp) offset=16;
|
if (tcp)
|
||||||
else offset=12;
|
offset = 16;
|
||||||
|
else
|
||||||
|
offset = 12;
|
||||||
|
|
||||||
const char * rtp=buf+offset;
|
const char *rtp = buf + offset;
|
||||||
int length = len- offset;
|
int length = len - offset;
|
||||||
int nal_type = H265_TYPE(*rtp);
|
int nal_type = H265_TYPE(*rtp);
|
||||||
// int nal_suffix = *rtp & (~0x1F);
|
// int nal_suffix = *rtp & (~0x1F);
|
||||||
|
|
||||||
uint16_t now_seq= get_sequence(buf,tcp);
|
uint16_t now_seq = get_sequence(buf, tcp);
|
||||||
DEBUGL("\n###### input ########################## nal: %d ", nal_type);
|
DEBUGL("\n###### input ########################## nal: %d ", nal_type);
|
||||||
print_rtp2(buf,len,offset);
|
print_rtp2(buf, len, offset);
|
||||||
|
|
||||||
switch (nal_type){
|
switch (nal_type) {
|
||||||
case H265Nal::NAL_SEI_PREFIX:{
|
case H265Nal::NAL_SEI_PREFIX: {
|
||||||
DEBUGL("SEI!!!!!!!!!! package\n");
|
DEBUGL("SEI!!!!!!!!!! package\n");
|
||||||
if(len<1300){
|
if (len < 1300) {
|
||||||
memcpy(HWSign_hd->sei_rtp_head,buf,len);
|
memcpy(HWSign_hd->sei_rtp_head, buf, len);
|
||||||
HWSign_hd->sei_len=len;
|
HWSign_hd->sei_len = len;
|
||||||
HWSign_hd->sei_param=param;
|
HWSign_hd->sei_param = param;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
case 49:{
|
case 49: {
|
||||||
FU fu;
|
FU fu;
|
||||||
Make265FU((uint8_t)rtp[2], fu);
|
Make265FU((uint8_t) rtp[2], fu);
|
||||||
|
|
||||||
if (!(fu.type>=H265Nal::NAL_IDR_W_RADL && fu.type<=H265Nal::NAL_IDR_N_LP))
|
if (!(fu.type >= H265Nal::NAL_IDR_W_RADL && fu.type <= H265Nal::NAL_IDR_N_LP)) { return 0; }
|
||||||
{
|
if (fu.S) {//第一个rtp包
|
||||||
return 0;
|
if (!HWSign_hd->buff->empty()) {
|
||||||
}
|
|
||||||
if (fu.S) { //第一个rtp包
|
|
||||||
if(!HWSign_hd->buff->empty()){
|
|
||||||
WRNGL("Warning!!!!!!!!!! missing package\n");
|
WRNGL("Warning!!!!!!!!!! missing package\n");
|
||||||
|
|
||||||
clear_all(HWSign_hd);
|
clear_all(HWSign_hd);
|
||||||
}
|
}
|
||||||
DEBUGL("!!!!!!!!!! I head\n");
|
DEBUGL("!!!!!!!!!! I head\n");
|
||||||
HWSign_hd->I_seq=now_seq;
|
HWSign_hd->I_seq = now_seq;
|
||||||
HWSign_hd->buff->assign(rtp+3,length-3);
|
HWSign_hd->buff->assign(rtp + 3, length - 3);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(HWSign_hd->I_seq>=0){
|
if (HWSign_hd->I_seq >= 0) {
|
||||||
|
|
||||||
HWSign_hd->buff->append(rtp+3,length-3);
|
HWSign_hd->buff->append(rtp + 3, length - 3);
|
||||||
if (!fu.E) { //中间rtp包
|
if (!fu.E) {//中间rtp包
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int vef_ret=-2;
|
int vef_ret = -2;
|
||||||
|
|
||||||
clear_all(HWSign_hd);
|
clear_all(HWSign_hd);
|
||||||
HWSign_hd->I_seq=-1;
|
HWSign_hd->I_seq = -1;
|
||||||
return vef_ret;
|
return vef_ret;
|
||||||
}else{
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
default:{
|
default: {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int HWVerify_rtp_input(void* Handle, const char * buf, const uint32_t len,int tcp, void * param){
|
int
|
||||||
int ret=-2;
|
HWVerify_rtp_input(void *Handle, const char *buf, const uint32_t len, int tcp, void *param)
|
||||||
HWsign* HWSign_hd=(HWsign*) Handle;
|
{
|
||||||
if(!HWSign_hd || !HWSign_hd->sm3_hd || !HWSign_hd->sm2_hd || !HWSign_hd->buff) {
|
int ret = -2;
|
||||||
throw runtime_error("HWSign_rtp initial error");
|
HWsign *HWSign_hd = (HWsign *) Handle;
|
||||||
|
if (!HWSign_hd || !HWSign_hd->sm3_hd || !HWSign_hd->sm2_hd || !HWSign_hd->buff) {
|
||||||
|
throw std::runtime_error("HWSign_rtp initial error");
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
HWSign_hd->in++;
|
HWSign_hd->in++;
|
||||||
DEBUGL("verify input: in %d que %ld out %d \n",HWSign_hd->in,HWSign_hd->buff_que->size(),HWSign_hd->out);
|
DEBUGL("verify input: in %d que %ld out %d \n", HWSign_hd->in, HWSign_hd->buff_que->size(), HWSign_hd->out);
|
||||||
tcp=0;
|
tcp = 0;
|
||||||
HWSign_hd->tcp=tcp;
|
HWSign_hd->tcp = tcp;
|
||||||
uint8_t offset;
|
uint8_t offset;
|
||||||
if (tcp) offset=16;
|
if (tcp)
|
||||||
else offset=12;
|
offset = 16;
|
||||||
if (len<=offset) {
|
else
|
||||||
return -3;
|
offset = 12;
|
||||||
}
|
if (len <= offset) { return -3; }
|
||||||
|
|
||||||
uint8_t payload_type=buf[1]& 0x7f;
|
uint8_t payload_type = buf[1] & 0x7f;
|
||||||
|
|
||||||
switch (payload_type)
|
switch (payload_type) {
|
||||||
{
|
case 99://h264
|
||||||
case 99: //h264
|
if (HWSign_hd->track_type != CodecId::CodecH264) {
|
||||||
if (HWSign_hd->track_type!=CodecId::CodecH264)
|
HWSign_hd->track_type = CodecId::CodecH264;
|
||||||
{
|
|
||||||
HWSign_hd->track_type=CodecId::CodecH264;
|
|
||||||
DEBUGL("Track type is changed to H.264");
|
DEBUGL("Track type is changed to H.264");
|
||||||
}
|
}
|
||||||
ret=HWVerify_rtp_264(HWSign_hd,buf,len,tcp,param);
|
ret = HWVerify_rtp_264(HWSign_hd, buf, len, tcp, param);
|
||||||
break;
|
break;
|
||||||
case 103 ... 108: //h265
|
case 103 ... 108://h265
|
||||||
if (HWSign_hd->track_type!=CodecId::CodecH265)
|
if (HWSign_hd->track_type != CodecId::CodecH265) {
|
||||||
{
|
HWSign_hd->track_type = CodecId::CodecH265;
|
||||||
HWSign_hd->track_type=CodecId::CodecH265;
|
|
||||||
DEBUGL("Track type is changed to H.265");
|
DEBUGL("Track type is changed to H.265");
|
||||||
}
|
}
|
||||||
ret=HWVerify_rtp_265(HWSign_hd,buf,len,tcp,param);
|
ret = HWVerify_rtp_265(HWSign_hd, buf, len, tcp, param);
|
||||||
break;
|
break;
|
||||||
case 0 ... 95: // standard type
|
case 0 ... 95:// standard type
|
||||||
return 0;
|
return 0;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
WRNGL("Error !!!!!!!!!! Unsupport track type: %u \n",payload_type);
|
WRNGL("Error !!!!!!!!!! Unsupport track type: %u \n", payload_type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
@ -1,46 +1,31 @@
|
|||||||
#include <stdio.h>
|
#include "sm2.h"
|
||||||
#include <string.h>
|
|
||||||
#include "typedef.h"
|
|
||||||
#include "big.h"
|
#include "big.h"
|
||||||
#include "ecc.h"
|
#include "ecc.h"
|
||||||
#include "sm2.h"
|
|
||||||
#include "sm3.h"
|
#include "sm3.h"
|
||||||
|
#include "typedef.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
static struct ecc_curve sm2_curve = {
|
static struct ecc_curve sm2_curve = {
|
||||||
ECC_MAX_DIGITS,
|
ECC_MAX_DIGITS,
|
||||||
{
|
{
|
||||||
|
{0x715A4589334C74C7ull, 0x8FE30BBFF2660BE1ull, 0x5F9904466A39C994ull, 0x32C4AE2C1F198119ull},
|
||||||
|
{0x02DF32E52139F0A0ull, 0xD0A9877CC62A4740ull, 0x59BDCEE36B692153ull, 0xBC3736A2F4F6779Cull},
|
||||||
|
},
|
||||||
|
{0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull, 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull},
|
||||||
|
{0x53BBF40939D54123ull, 0x7203DF6B21C6052Bull, 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull},
|
||||||
{
|
{
|
||||||
0x715A4589334C74C7ull, 0x8FE30BBFF2660BE1ull,
|
0x0000000000000001ull,
|
||||||
0x5F9904466A39C994ull, 0x32C4AE2C1F198119ull
|
0x0000000000000000ull,
|
||||||
},
|
0x0000000000000000ull,
|
||||||
{
|
0x0000000000000000ull,
|
||||||
0x02DF32E52139F0A0ull, 0xD0A9877CC62A4740ull,
|
|
||||||
0x59BDCEE36B692153ull, 0xBC3736A2F4F6779Cull
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull,
|
|
||||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0x53BBF40939D54123ull, 0x7203DF6B21C6052Bull,
|
|
||||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0x0000000000000001ull, 0x0000000000000000ull,
|
|
||||||
0x0000000000000000ull, 0x0000000000000000ull,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0xFFFFFFFFFFFFFFFCull, 0xFFFFFFFF00000000ull,
|
|
||||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0xDDBCBD414D940E93ull, 0xF39789F515AB8F92ull,
|
|
||||||
0x4D5A9E4BCF6509A7ull, 0x28E9FA9E9D9F5E34ull
|
|
||||||
},
|
},
|
||||||
|
{0xFFFFFFFFFFFFFFFCull, 0xFFFFFFFF00000000ull, 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull},
|
||||||
|
{0xDDBCBD414D940E93ull, 0xF39789F515AB8F92ull, 0x4D5A9E4BCF6509A7ull, 0x28E9FA9E9D9F5E34ull},
|
||||||
};
|
};
|
||||||
|
|
||||||
void sm2_w(u64 *result, u64 *x)
|
void
|
||||||
|
sm2_w(u64 *result, u64 *x)
|
||||||
{
|
{
|
||||||
result[0] = x[0];
|
result[0] = x[0];
|
||||||
result[1] = x[1];
|
result[1] = x[1];
|
||||||
@ -49,15 +34,16 @@ void sm2_w(u64 *result, u64 *x)
|
|||||||
result[3] = 0;
|
result[3] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sm3_kdf(u8 *Z ,u32 zlen, u8 *K, u32 klen)
|
void
|
||||||
|
sm3_kdf(u8 *Z, u32 zlen, u8 *K, u32 klen)
|
||||||
{
|
{
|
||||||
u32 ct = 0x00000001;
|
u32 ct = 0x00000001;
|
||||||
u8 ct_char[32];
|
u8 ct_char[32];
|
||||||
u8 *hash = K ;
|
u8 *hash = K;
|
||||||
u32 i, t;
|
u32 i, t;
|
||||||
struct sm3_ctx md[1];
|
struct sm3_ctx md[1];
|
||||||
|
|
||||||
t = klen/ECC_NUMWORD;
|
t = klen / ECC_NUMWORD;
|
||||||
//s4: K=Ha1||Ha2||...
|
//s4: K=Ha1||Ha2||...
|
||||||
for (i = 0; i < t; i++) {
|
for (i = 0; i < t; i++) {
|
||||||
//s2: Hai=Hv(Z||ct)
|
//s2: Hai=Hv(Z||ct)
|
||||||
@ -70,7 +56,7 @@ void sm3_kdf(u8 *Z ,u32 zlen, u8 *K, u32 klen)
|
|||||||
ct++;
|
ct++;
|
||||||
}
|
}
|
||||||
|
|
||||||
t = klen%ECC_NUMBITS;
|
t = klen % ECC_NUMBITS;
|
||||||
if (t) {
|
if (t) {
|
||||||
sm3_init(md);
|
sm3_init(md);
|
||||||
sm3_update(md, Z, zlen);
|
sm3_update(md, Z, zlen);
|
||||||
@ -81,7 +67,8 @@ void sm3_kdf(u8 *Z ,u32 zlen, u8 *K, u32 klen)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sm3_z(u8 *id, u32 idlen, ecc_point *pub, u8 *hash)
|
void
|
||||||
|
sm3_z(u8 *id, u32 idlen, ecc_point *pub, u8 *hash)
|
||||||
{
|
{
|
||||||
u8 a[ECC_NUMWORD];
|
u8 a[ECC_NUMWORD];
|
||||||
u8 b[ECC_NUMWORD];
|
u8 b[ECC_NUMWORD];
|
||||||
@ -90,12 +77,12 @@ void sm3_z(u8 *id, u32 idlen, ecc_point *pub, u8 *hash)
|
|||||||
u8 idlen_char[2];
|
u8 idlen_char[2];
|
||||||
struct sm3_ctx md[1];
|
struct sm3_ctx md[1];
|
||||||
|
|
||||||
put_unaligned_be16(idlen<<3, idlen_char);
|
put_unaligned_be16(idlen << 3, idlen_char);
|
||||||
|
|
||||||
ecc_bytes2native((u64*)a, sm2_curve.a, sm2_curve.ndigits);
|
ecc_bytes2native((u64 *) a, sm2_curve.a, sm2_curve.ndigits);
|
||||||
ecc_bytes2native((u64*)b, sm2_curve.b, sm2_curve.ndigits);
|
ecc_bytes2native((u64 *) b, sm2_curve.b, sm2_curve.ndigits);
|
||||||
ecc_bytes2native((u64*)x, sm2_curve.g.x, sm2_curve.ndigits);
|
ecc_bytes2native((u64 *) x, sm2_curve.g.x, sm2_curve.ndigits);
|
||||||
ecc_bytes2native((u64*)y, sm2_curve.g.y, sm2_curve.ndigits);
|
ecc_bytes2native((u64 *) y, sm2_curve.g.y, sm2_curve.ndigits);
|
||||||
|
|
||||||
sm3_init(md);
|
sm3_init(md);
|
||||||
sm3_update(md, idlen_char, 2);
|
sm3_update(md, idlen_char, 2);
|
||||||
@ -104,21 +91,21 @@ void sm3_z(u8 *id, u32 idlen, ecc_point *pub, u8 *hash)
|
|||||||
sm3_update(md, b, ECC_NUMWORD);
|
sm3_update(md, b, ECC_NUMWORD);
|
||||||
sm3_update(md, x, ECC_NUMWORD);
|
sm3_update(md, x, ECC_NUMWORD);
|
||||||
sm3_update(md, y, ECC_NUMWORD);
|
sm3_update(md, y, ECC_NUMWORD);
|
||||||
sm3_update(md, (u8*)pub->x, ECC_NUMWORD);
|
sm3_update(md, (u8 *) pub->x, ECC_NUMWORD);
|
||||||
sm3_update(md, (u8*)pub->y, ECC_NUMWORD);
|
sm3_update(md, (u8 *) pub->y, ECC_NUMWORD);
|
||||||
sm3_final(md, hash);
|
sm3_final(md, hash);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm2_valid_public_key(ecc_point *publicKey)
|
int
|
||||||
|
sm2_valid_public_key(ecc_point *publicKey)
|
||||||
{
|
{
|
||||||
u64 na[ECC_MAX_DIGITS] = {3}; /* a mod p = (-3) mod p */
|
u64 na[ECC_MAX_DIGITS] = {3}; /* a mod p = (-3) mod p */
|
||||||
u64 tmp1[ECC_MAX_DIGITS];
|
u64 tmp1[ECC_MAX_DIGITS];
|
||||||
u64 tmp2[ECC_MAX_DIGITS];
|
u64 tmp2[ECC_MAX_DIGITS];
|
||||||
|
|
||||||
if (ecc_point_is_zero(&sm2_curve, publicKey))
|
if (ecc_point_is_zero(&sm2_curve, publicKey)) return 1;
|
||||||
return 1;
|
|
||||||
|
|
||||||
if (vli_cmp(sm2_curve.p, publicKey->x, sm2_curve.ndigits) != 1
|
if (vli_cmp(sm2_curve.p, publicKey->x, sm2_curve.ndigits) != 1
|
||||||
|| vli_cmp(sm2_curve.p, publicKey->y, sm2_curve.ndigits) != 1)
|
|| vli_cmp(sm2_curve.p, publicKey->y, sm2_curve.ndigits) != 1)
|
||||||
@ -136,13 +123,13 @@ int sm2_valid_public_key(ecc_point *publicKey)
|
|||||||
vli_mod_add(tmp2, tmp2, sm2_curve.b, sm2_curve.p, sm2_curve.ndigits);
|
vli_mod_add(tmp2, tmp2, sm2_curve.b, sm2_curve.p, sm2_curve.ndigits);
|
||||||
|
|
||||||
/* Make sure that y^2 == x^3 + ax + b */
|
/* Make sure that y^2 == x^3 + ax + b */
|
||||||
if (vli_cmp(tmp1, tmp2, sm2_curve.ndigits) != 0)
|
if (vli_cmp(tmp1, tmp2, sm2_curve.ndigits) != 0) return 1;
|
||||||
return 1;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm2_verify(ecc_point *pubkey, u8 *hash_, u8 *r_, u8 *s_)
|
int
|
||||||
|
sm2_verify(ecc_point *pubkey, u8 *hash_, u8 *r_, u8 *s_)
|
||||||
{
|
{
|
||||||
ecc_point result;
|
ecc_point result;
|
||||||
ecc_point pub[1];
|
ecc_point pub[1];
|
||||||
@ -162,22 +149,20 @@ int sm2_verify(ecc_point *pubkey, u8 *hash_, u8 *r_, u8 *s_)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vli_cmp(sm2_curve.n, r, sm2_curve.ndigits) != 1
|
if (vli_cmp(sm2_curve.n, r, sm2_curve.ndigits) != 1 || vli_cmp(sm2_curve.n, s, sm2_curve.ndigits) != 1) {
|
||||||
|| vli_cmp(sm2_curve.n, s, sm2_curve.ndigits) != 1) {
|
|
||||||
/* r, s must be < n. */
|
/* r, s must be < n. */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
vli_mod_add(t, r, s, sm2_curve.n, sm2_curve.ndigits); // r + s
|
vli_mod_add(t, r, s, sm2_curve.n, sm2_curve.ndigits);// r + s
|
||||||
if (t == 0)
|
if (t == 0) return -1;
|
||||||
return -1;
|
|
||||||
|
|
||||||
ecc_point_mult2(&sm2_curve, &result, &sm2_curve.g, pub, s, t);
|
ecc_point_mult2(&sm2_curve, &result, &sm2_curve.g, pub, s, t);
|
||||||
|
|
||||||
/* v = x1 + e (mod n) */
|
/* v = x1 + e (mod n) */
|
||||||
vli_mod_add(result.x, result.x, hash, sm2_curve.n, sm2_curve.ndigits);
|
vli_mod_add(result.x, result.x, hash, sm2_curve.n, sm2_curve.ndigits);
|
||||||
|
|
||||||
if(vli_cmp(sm2_curve.n, result.x, sm2_curve.ndigits) != 1) {
|
if (vli_cmp(sm2_curve.n, result.x, sm2_curve.ndigits) != 1) {
|
||||||
vli_sub(result.x, result.x, sm2_curve.n, sm2_curve.ndigits);
|
vli_sub(result.x, result.x, sm2_curve.n, sm2_curve.ndigits);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,7 +170,8 @@ int sm2_verify(ecc_point *pubkey, u8 *hash_, u8 *r_, u8 *s_)
|
|||||||
return vli_cmp(result.x, r, sm2_curve.ndigits);
|
return vli_cmp(result.x, r, sm2_curve.ndigits);
|
||||||
}
|
}
|
||||||
|
|
||||||
int do_sm2_verify(char *pubkey, char *data_addr, int data_len, char *sign_addr)
|
int
|
||||||
|
do_sm2_verify(char *pubkey, char *data_addr, int data_len, char *sign_addr)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
struct sm3_ctx sm3_ctx;
|
struct sm3_ctx sm3_ctx;
|
||||||
@ -197,19 +183,17 @@ int do_sm2_verify(char *pubkey, char *data_addr, int data_len, char *sign_addr)
|
|||||||
memcpy(&ios_pub, pubkey, 64);
|
memcpy(&ios_pub, pubkey, 64);
|
||||||
|
|
||||||
ret = sm3_init(&sm3_ctx);
|
ret = sm3_init(&sm3_ctx);
|
||||||
sm3_z((u8 *)"1234567812345678", 16, &ios_pub, ios_Z);
|
sm3_z((u8 *) "1234567812345678", 16, &ios_pub, ios_Z);
|
||||||
sm3_update(&sm3_ctx, ios_Z, ECC_NUMWORD);
|
sm3_update(&sm3_ctx, ios_Z, ECC_NUMWORD);
|
||||||
sm3_update(&sm3_ctx, data_addr, data_len);
|
sm3_update(&sm3_ctx, (const u8 *) data_addr, data_len);
|
||||||
sm3_final(&sm3_ctx, ios_hash);
|
sm3_final(&sm3_ctx, ios_hash);
|
||||||
memcpy(sign_data, sign_addr, 64);
|
memcpy(sign_data, sign_addr, 64);
|
||||||
|
|
||||||
ret = sm2_verify(pubkey, ios_hash, sign_data, sign_data + 32);
|
ret = sm2_verify((ecc_point *) pubkey, ios_hash, sign_data, sign_data + 32);
|
||||||
if (ret)
|
if (ret) {
|
||||||
{
|
|
||||||
//printf("verify err ret = %d\n", ret);
|
//printf("verify err ret = %d\n", ret);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define SM4_KEY_SCHEDULE 32
|
#define SM4_KEY_SCHEDULE 32
|
||||||
|
|
||||||
@ -6,111 +7,89 @@ typedef struct SM4_KEY_st {
|
|||||||
unsigned int rk[SM4_KEY_SCHEDULE];
|
unsigned int rk[SM4_KEY_SCHEDULE];
|
||||||
} SM4_KEY;
|
} SM4_KEY;
|
||||||
|
|
||||||
typedef void (*block128_f) (const unsigned char in[16],
|
typedef void (*block128_f)(const unsigned char in[16], unsigned char out[16], const void *key);
|
||||||
unsigned char out[16], const void *key);
|
|
||||||
|
|
||||||
static const unsigned char SM4_S[256] = {
|
static const unsigned char SM4_S[256] = {
|
||||||
0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2,
|
0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2, 0x28, 0xFB, 0x2C, 0x05, 0x2B, 0x67, 0x9A,
|
||||||
0x28, 0xFB, 0x2C, 0x05, 0x2B, 0x67, 0x9A, 0x76, 0x2A, 0xBE, 0x04, 0xC3,
|
0x76, 0x2A, 0xBE, 0x04, 0xC3, 0xAA, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, 0x9C, 0x42, 0x50, 0xF4, 0x91, 0xEF,
|
||||||
0xAA, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, 0x9C, 0x42, 0x50, 0xF4,
|
0x98, 0x7A, 0x33, 0x54, 0x0B, 0x43, 0xED, 0xCF, 0xAC, 0x62, 0xE4, 0xB3, 0x1C, 0xA9, 0xC9, 0x08, 0xE8, 0x95, 0x80,
|
||||||
0x91, 0xEF, 0x98, 0x7A, 0x33, 0x54, 0x0B, 0x43, 0xED, 0xCF, 0xAC, 0x62,
|
0xDF, 0x94, 0xFA, 0x75, 0x8F, 0x3F, 0xA6, 0x47, 0x07, 0xA7, 0xFC, 0xF3, 0x73, 0x17, 0xBA, 0x83, 0x59, 0x3C, 0x19,
|
||||||
0xE4, 0xB3, 0x1C, 0xA9, 0xC9, 0x08, 0xE8, 0x95, 0x80, 0xDF, 0x94, 0xFA,
|
0xE6, 0x85, 0x4F, 0xA8, 0x68, 0x6B, 0x81, 0xB2, 0x71, 0x64, 0xDA, 0x8B, 0xF8, 0xEB, 0x0F, 0x4B, 0x70, 0x56, 0x9D,
|
||||||
0x75, 0x8F, 0x3F, 0xA6, 0x47, 0x07, 0xA7, 0xFC, 0xF3, 0x73, 0x17, 0xBA,
|
0x35, 0x1E, 0x24, 0x0E, 0x5E, 0x63, 0x58, 0xD1, 0xA2, 0x25, 0x22, 0x7C, 0x3B, 0x01, 0x21, 0x78, 0x87, 0xD4, 0x00,
|
||||||
0x83, 0x59, 0x3C, 0x19, 0xE6, 0x85, 0x4F, 0xA8, 0x68, 0x6B, 0x81, 0xB2,
|
0x46, 0x57, 0x9F, 0xD3, 0x27, 0x52, 0x4C, 0x36, 0x02, 0xE7, 0xA0, 0xC4, 0xC8, 0x9E, 0xEA, 0xBF, 0x8A, 0xD2, 0x40,
|
||||||
0x71, 0x64, 0xDA, 0x8B, 0xF8, 0xEB, 0x0F, 0x4B, 0x70, 0x56, 0x9D, 0x35,
|
0xC7, 0x38, 0xB5, 0xA3, 0xF7, 0xF2, 0xCE, 0xF9, 0x61, 0x15, 0xA1, 0xE0, 0xAE, 0x5D, 0xA4, 0x9B, 0x34, 0x1A, 0x55,
|
||||||
0x1E, 0x24, 0x0E, 0x5E, 0x63, 0x58, 0xD1, 0xA2, 0x25, 0x22, 0x7C, 0x3B,
|
0xAD, 0x93, 0x32, 0x30, 0xF5, 0x8C, 0xB1, 0xE3, 0x1D, 0xF6, 0xE2, 0x2E, 0x82, 0x66, 0xCA, 0x60, 0xC0, 0x29, 0x23,
|
||||||
0x01, 0x21, 0x78, 0x87, 0xD4, 0x00, 0x46, 0x57, 0x9F, 0xD3, 0x27, 0x52,
|
0xAB, 0x0D, 0x53, 0x4E, 0x6F, 0xD5, 0xDB, 0x37, 0x45, 0xDE, 0xFD, 0x8E, 0x2F, 0x03, 0xFF, 0x6A, 0x72, 0x6D, 0x6C,
|
||||||
0x4C, 0x36, 0x02, 0xE7, 0xA0, 0xC4, 0xC8, 0x9E, 0xEA, 0xBF, 0x8A, 0xD2,
|
0x5B, 0x51, 0x8D, 0x1B, 0xAF, 0x92, 0xBB, 0xDD, 0xBC, 0x7F, 0x11, 0xD9, 0x5C, 0x41, 0x1F, 0x10, 0x5A, 0xD8, 0x0A,
|
||||||
0x40, 0xC7, 0x38, 0xB5, 0xA3, 0xF7, 0xF2, 0xCE, 0xF9, 0x61, 0x15, 0xA1,
|
0xC1, 0x31, 0x88, 0xA5, 0xCD, 0x7B, 0xBD, 0x2D, 0x74, 0xD0, 0x12, 0xB8, 0xE5, 0xB4, 0xB0, 0x89, 0x69, 0x97, 0x4A,
|
||||||
0xE0, 0xAE, 0x5D, 0xA4, 0x9B, 0x34, 0x1A, 0x55, 0xAD, 0x93, 0x32, 0x30,
|
0x0C, 0x96, 0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84, 0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D,
|
||||||
0xF5, 0x8C, 0xB1, 0xE3, 0x1D, 0xF6, 0xE2, 0x2E, 0x82, 0x66, 0xCA, 0x60,
|
0x20, 0x79, 0xEE, 0x5F, 0x3E, 0xD7, 0xCB, 0x39, 0x48};
|
||||||
0xC0, 0x29, 0x23, 0xAB, 0x0D, 0x53, 0x4E, 0x6F, 0xD5, 0xDB, 0x37, 0x45,
|
|
||||||
0xDE, 0xFD, 0x8E, 0x2F, 0x03, 0xFF, 0x6A, 0x72, 0x6D, 0x6C, 0x5B, 0x51,
|
|
||||||
0x8D, 0x1B, 0xAF, 0x92, 0xBB, 0xDD, 0xBC, 0x7F, 0x11, 0xD9, 0x5C, 0x41,
|
|
||||||
0x1F, 0x10, 0x5A, 0xD8, 0x0A, 0xC1, 0x31, 0x88, 0xA5, 0xCD, 0x7B, 0xBD,
|
|
||||||
0x2D, 0x74, 0xD0, 0x12, 0xB8, 0xE5, 0xB4, 0xB0, 0x89, 0x69, 0x97, 0x4A,
|
|
||||||
0x0C, 0x96, 0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84,
|
|
||||||
0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D, 0x20, 0x79, 0xEE, 0x5F, 0x3E,
|
|
||||||
0xD7, 0xCB, 0x39, 0x48
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* SM4_SBOX_T[j] == L(SM4_SBOX[j]).
|
* SM4_SBOX_T[j] == L(SM4_SBOX[j]).
|
||||||
*/
|
*/
|
||||||
static const unsigned int SM4_SBOX_T[256] = {
|
static const unsigned int SM4_SBOX_T[256] = {
|
||||||
0x8ED55B5B, 0xD0924242, 0x4DEAA7A7, 0x06FDFBFB, 0xFCCF3333, 0x65E28787,
|
0x8ED55B5B, 0xD0924242, 0x4DEAA7A7, 0x06FDFBFB, 0xFCCF3333, 0x65E28787, 0xC93DF4F4, 0x6BB5DEDE, 0x4E165858,
|
||||||
0xC93DF4F4, 0x6BB5DEDE, 0x4E165858, 0x6EB4DADA, 0x44145050, 0xCAC10B0B,
|
0x6EB4DADA, 0x44145050, 0xCAC10B0B, 0x8828A0A0, 0x17F8EFEF, 0x9C2CB0B0, 0x11051414, 0x872BACAC, 0xFB669D9D,
|
||||||
0x8828A0A0, 0x17F8EFEF, 0x9C2CB0B0, 0x11051414, 0x872BACAC, 0xFB669D9D,
|
0xF2986A6A, 0xAE77D9D9, 0x822AA8A8, 0x46BCFAFA, 0x14041010, 0xCFC00F0F, 0x02A8AAAA, 0x54451111, 0x5F134C4C,
|
||||||
0xF2986A6A, 0xAE77D9D9, 0x822AA8A8, 0x46BCFAFA, 0x14041010, 0xCFC00F0F,
|
0xBE269898, 0x6D482525, 0x9E841A1A, 0x1E061818, 0xFD9B6666, 0xEC9E7272, 0x4A430909, 0x10514141, 0x24F7D3D3,
|
||||||
0x02A8AAAA, 0x54451111, 0x5F134C4C, 0xBE269898, 0x6D482525, 0x9E841A1A,
|
0xD5934646, 0x53ECBFBF, 0xF89A6262, 0x927BE9E9, 0xFF33CCCC, 0x04555151, 0x270B2C2C, 0x4F420D0D, 0x59EEB7B7,
|
||||||
0x1E061818, 0xFD9B6666, 0xEC9E7272, 0x4A430909, 0x10514141, 0x24F7D3D3,
|
0xF3CC3F3F, 0x1CAEB2B2, 0xEA638989, 0x74E79393, 0x7FB1CECE, 0x6C1C7070, 0x0DABA6A6, 0xEDCA2727, 0x28082020,
|
||||||
0xD5934646, 0x53ECBFBF, 0xF89A6262, 0x927BE9E9, 0xFF33CCCC, 0x04555151,
|
0x48EBA3A3, 0xC1975656, 0x80820202, 0xA3DC7F7F, 0xC4965252, 0x12F9EBEB, 0xA174D5D5, 0xB38D3E3E, 0xC33FFCFC,
|
||||||
0x270B2C2C, 0x4F420D0D, 0x59EEB7B7, 0xF3CC3F3F, 0x1CAEB2B2, 0xEA638989,
|
0x3EA49A9A, 0x5B461D1D, 0x1B071C1C, 0x3BA59E9E, 0x0CFFF3F3, 0x3FF0CFCF, 0xBF72CDCD, 0x4B175C5C, 0x52B8EAEA,
|
||||||
0x74E79393, 0x7FB1CECE, 0x6C1C7070, 0x0DABA6A6, 0xEDCA2727, 0x28082020,
|
0x8F810E0E, 0x3D586565, 0xCC3CF0F0, 0x7D196464, 0x7EE59B9B, 0x91871616, 0x734E3D3D, 0x08AAA2A2, 0xC869A1A1,
|
||||||
0x48EBA3A3, 0xC1975656, 0x80820202, 0xA3DC7F7F, 0xC4965252, 0x12F9EBEB,
|
0xC76AADAD, 0x85830606, 0x7AB0CACA, 0xB570C5C5, 0xF4659191, 0xB2D96B6B, 0xA7892E2E, 0x18FBE3E3, 0x47E8AFAF,
|
||||||
0xA174D5D5, 0xB38D3E3E, 0xC33FFCFC, 0x3EA49A9A, 0x5B461D1D, 0x1B071C1C,
|
0x330F3C3C, 0x674A2D2D, 0xB071C1C1, 0x0E575959, 0xE99F7676, 0xE135D4D4, 0x661E7878, 0xB4249090, 0x360E3838,
|
||||||
0x3BA59E9E, 0x0CFFF3F3, 0x3FF0CFCF, 0xBF72CDCD, 0x4B175C5C, 0x52B8EAEA,
|
0x265F7979, 0xEF628D8D, 0x38596161, 0x95D24747, 0x2AA08A8A, 0xB1259494, 0xAA228888, 0x8C7DF1F1, 0xD73BECEC,
|
||||||
0x8F810E0E, 0x3D586565, 0xCC3CF0F0, 0x7D196464, 0x7EE59B9B, 0x91871616,
|
0x05010404, 0xA5218484, 0x9879E1E1, 0x9B851E1E, 0x84D75353, 0x00000000, 0x5E471919, 0x0B565D5D, 0xE39D7E7E,
|
||||||
0x734E3D3D, 0x08AAA2A2, 0xC869A1A1, 0xC76AADAD, 0x85830606, 0x7AB0CACA,
|
0x9FD04F4F, 0xBB279C9C, 0x1A534949, 0x7C4D3131, 0xEE36D8D8, 0x0A020808, 0x7BE49F9F, 0x20A28282, 0xD4C71313,
|
||||||
0xB570C5C5, 0xF4659191, 0xB2D96B6B, 0xA7892E2E, 0x18FBE3E3, 0x47E8AFAF,
|
0xE8CB2323, 0xE69C7A7A, 0x42E9ABAB, 0x43BDFEFE, 0xA2882A2A, 0x9AD14B4B, 0x40410101, 0xDBC41F1F, 0xD838E0E0,
|
||||||
0x330F3C3C, 0x674A2D2D, 0xB071C1C1, 0x0E575959, 0xE99F7676, 0xE135D4D4,
|
0x61B7D6D6, 0x2FA18E8E, 0x2BF4DFDF, 0x3AF1CBCB, 0xF6CD3B3B, 0x1DFAE7E7, 0xE5608585, 0x41155454, 0x25A38686,
|
||||||
0x661E7878, 0xB4249090, 0x360E3838, 0x265F7979, 0xEF628D8D, 0x38596161,
|
0x60E38383, 0x16ACBABA, 0x295C7575, 0x34A69292, 0xF7996E6E, 0xE434D0D0, 0x721A6868, 0x01545555, 0x19AFB6B6,
|
||||||
0x95D24747, 0x2AA08A8A, 0xB1259494, 0xAA228888, 0x8C7DF1F1, 0xD73BECEC,
|
0xDF914E4E, 0xFA32C8C8, 0xF030C0C0, 0x21F6D7D7, 0xBC8E3232, 0x75B3C6C6, 0x6FE08F8F, 0x691D7474, 0x2EF5DBDB,
|
||||||
0x05010404, 0xA5218484, 0x9879E1E1, 0x9B851E1E, 0x84D75353, 0x00000000,
|
0x6AE18B8B, 0x962EB8B8, 0x8A800A0A, 0xFE679999, 0xE2C92B2B, 0xE0618181, 0xC0C30303, 0x8D29A4A4, 0xAF238C8C,
|
||||||
0x5E471919, 0x0B565D5D, 0xE39D7E7E, 0x9FD04F4F, 0xBB279C9C, 0x1A534949,
|
0x07A9AEAE, 0x390D3434, 0x1F524D4D, 0x764F3939, 0xD36EBDBD, 0x81D65757, 0xB7D86F6F, 0xEB37DCDC, 0x51441515,
|
||||||
0x7C4D3131, 0xEE36D8D8, 0x0A020808, 0x7BE49F9F, 0x20A28282, 0xD4C71313,
|
0xA6DD7B7B, 0x09FEF7F7, 0xB68C3A3A, 0x932FBCBC, 0x0F030C0C, 0x03FCFFFF, 0xC26BA9A9, 0xBA73C9C9, 0xD96CB5B5,
|
||||||
0xE8CB2323, 0xE69C7A7A, 0x42E9ABAB, 0x43BDFEFE, 0xA2882A2A, 0x9AD14B4B,
|
0xDC6DB1B1, 0x375A6D6D, 0x15504545, 0xB98F3636, 0x771B6C6C, 0x13ADBEBE, 0xDA904A4A, 0x57B9EEEE, 0xA9DE7777,
|
||||||
0x40410101, 0xDBC41F1F, 0xD838E0E0, 0x61B7D6D6, 0x2FA18E8E, 0x2BF4DFDF,
|
0x4CBEF2F2, 0x837EFDFD, 0x55114444, 0xBDDA6767, 0x2C5D7171, 0x45400505, 0x631F7C7C, 0x50104040, 0x325B6969,
|
||||||
0x3AF1CBCB, 0xF6CD3B3B, 0x1DFAE7E7, 0xE5608585, 0x41155454, 0x25A38686,
|
0xB8DB6363, 0x220A2828, 0xC5C20707, 0xF531C4C4, 0xA88A2222, 0x31A79696, 0xF9CE3737, 0x977AEDED, 0x49BFF6F6,
|
||||||
0x60E38383, 0x16ACBABA, 0x295C7575, 0x34A69292, 0xF7996E6E, 0xE434D0D0,
|
0x992DB4B4, 0xA475D1D1, 0x90D34343, 0x5A124848, 0x58BAE2E2, 0x71E69797, 0x64B6D2D2, 0x70B2C2C2, 0xAD8B2626,
|
||||||
0x721A6868, 0x01545555, 0x19AFB6B6, 0xDF914E4E, 0xFA32C8C8, 0xF030C0C0,
|
0xCD68A5A5, 0xCB955E5E, 0x624B2929, 0x3C0C3030, 0xCE945A5A, 0xAB76DDDD, 0x867FF9F9, 0xF1649595, 0x5DBBE6E6,
|
||||||
0x21F6D7D7, 0xBC8E3232, 0x75B3C6C6, 0x6FE08F8F, 0x691D7474, 0x2EF5DBDB,
|
0x35F2C7C7, 0x2D092424, 0xD1C61717, 0xD66FB9B9, 0xDEC51B1B, 0x94861212, 0x78186060, 0x30F3C3C3, 0x897CF5F5,
|
||||||
0x6AE18B8B, 0x962EB8B8, 0x8A800A0A, 0xFE679999, 0xE2C92B2B, 0xE0618181,
|
0x5CEFB3B3, 0xD23AE8E8, 0xACDF7373, 0x794C3535, 0xA0208080, 0x9D78E5E5, 0x56EDBBBB, 0x235E7D7D, 0xC63EF8F8,
|
||||||
0xC0C30303, 0x8D29A4A4, 0xAF238C8C, 0x07A9AEAE, 0x390D3434, 0x1F524D4D,
|
0x8BD45F5F, 0xE7C82F2F, 0xDD39E4E4, 0x68492121};
|
||||||
0x764F3939, 0xD36EBDBD, 0x81D65757, 0xB7D86F6F, 0xEB37DCDC, 0x51441515,
|
|
||||||
0xA6DD7B7B, 0x09FEF7F7, 0xB68C3A3A, 0x932FBCBC, 0x0F030C0C, 0x03FCFFFF,
|
|
||||||
0xC26BA9A9, 0xBA73C9C9, 0xD96CB5B5, 0xDC6DB1B1, 0x375A6D6D, 0x15504545,
|
|
||||||
0xB98F3636, 0x771B6C6C, 0x13ADBEBE, 0xDA904A4A, 0x57B9EEEE, 0xA9DE7777,
|
|
||||||
0x4CBEF2F2, 0x837EFDFD, 0x55114444, 0xBDDA6767, 0x2C5D7171, 0x45400505,
|
|
||||||
0x631F7C7C, 0x50104040, 0x325B6969, 0xB8DB6363, 0x220A2828, 0xC5C20707,
|
|
||||||
0xF531C4C4, 0xA88A2222, 0x31A79696, 0xF9CE3737, 0x977AEDED, 0x49BFF6F6,
|
|
||||||
0x992DB4B4, 0xA475D1D1, 0x90D34343, 0x5A124848, 0x58BAE2E2, 0x71E69797,
|
|
||||||
0x64B6D2D2, 0x70B2C2C2, 0xAD8B2626, 0xCD68A5A5, 0xCB955E5E, 0x624B2929,
|
|
||||||
0x3C0C3030, 0xCE945A5A, 0xAB76DDDD, 0x867FF9F9, 0xF1649595, 0x5DBBE6E6,
|
|
||||||
0x35F2C7C7, 0x2D092424, 0xD1C61717, 0xD66FB9B9, 0xDEC51B1B, 0x94861212,
|
|
||||||
0x78186060, 0x30F3C3C3, 0x897CF5F5, 0x5CEFB3B3, 0xD23AE8E8, 0xACDF7373,
|
|
||||||
0x794C3535, 0xA0208080, 0x9D78E5E5, 0x56EDBBBB, 0x235E7D7D, 0xC63EF8F8,
|
|
||||||
0x8BD45F5F, 0xE7C82F2F, 0xDD39E4E4, 0x68492121 };
|
|
||||||
|
|
||||||
static unsigned int rotl(unsigned int a, unsigned char n)
|
static unsigned int
|
||||||
|
rotl(unsigned int a, unsigned char n)
|
||||||
{
|
{
|
||||||
return (a << n) | (a >> (32 - n));
|
return (a << n) | (a >> (32 - n));
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int load_u32_be(const unsigned char *b, unsigned int n)
|
static unsigned int
|
||||||
|
load_u32_be(const unsigned char *b, unsigned int n)
|
||||||
{
|
{
|
||||||
return ((unsigned int)b[4 * n] << 24) |
|
return ((unsigned int) b[4 * n] << 24) | ((unsigned int) b[4 * n + 1] << 16) | ((unsigned int) b[4 * n + 2] << 8)
|
||||||
((unsigned int)b[4 * n + 1] << 16) |
|
| ((unsigned int) b[4 * n + 3]);
|
||||||
((unsigned int)b[4 * n + 2] << 8) |
|
|
||||||
((unsigned int)b[4 * n + 3]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void store_u32_be(unsigned int v, unsigned char *b)
|
static void
|
||||||
|
store_u32_be(unsigned int v, unsigned char *b)
|
||||||
{
|
{
|
||||||
b[0] = (unsigned char)(v >> 24);
|
b[0] = (unsigned char) (v >> 24);
|
||||||
b[1] = (unsigned char)(v >> 16);
|
b[1] = (unsigned char) (v >> 16);
|
||||||
b[2] = (unsigned char)(v >> 8);
|
b[2] = (unsigned char) (v >> 8);
|
||||||
b[3] = (unsigned char)(v);
|
b[3] = (unsigned char) (v);
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int SM4_T_slow(unsigned int X)
|
static unsigned int
|
||||||
|
SM4_T_slow(unsigned int X)
|
||||||
{
|
{
|
||||||
unsigned int t = 0;
|
unsigned int t = 0;
|
||||||
|
|
||||||
t |= ((unsigned int)SM4_S[(unsigned char)(X >> 24)]) << 24;
|
t |= ((unsigned int) SM4_S[(unsigned char) (X >> 24)]) << 24;
|
||||||
t |= ((unsigned int)SM4_S[(unsigned char)(X >> 16)]) << 16;
|
t |= ((unsigned int) SM4_S[(unsigned char) (X >> 16)]) << 16;
|
||||||
t |= ((unsigned int)SM4_S[(unsigned char)(X >> 8)]) << 8;
|
t |= ((unsigned int) SM4_S[(unsigned char) (X >> 8)]) << 8;
|
||||||
t |= SM4_S[(unsigned char)X];
|
t |= SM4_S[(unsigned char) X];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* L linear transform
|
* L linear transform
|
||||||
@ -118,35 +97,29 @@ static unsigned int SM4_T_slow(unsigned int X)
|
|||||||
return t ^ rotl(t, 2) ^ rotl(t, 10) ^ rotl(t, 18) ^ rotl(t, 24);
|
return t ^ rotl(t, 2) ^ rotl(t, 10) ^ rotl(t, 18) ^ rotl(t, 24);
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int SM4_T(unsigned int X)
|
static unsigned int
|
||||||
|
SM4_T(unsigned int X)
|
||||||
{
|
{
|
||||||
return SM4_SBOX_T[(unsigned char)(X >> 24)] ^
|
return SM4_SBOX_T[(unsigned char) (X >> 24)] ^ rotl(SM4_SBOX_T[(unsigned char) (X >> 16)], 24)
|
||||||
rotl(SM4_SBOX_T[(unsigned char)(X >> 16)], 24) ^
|
^ rotl(SM4_SBOX_T[(unsigned char) (X >> 8)], 16) ^ rotl(SM4_SBOX_T[(unsigned char) X], 8);
|
||||||
rotl(SM4_SBOX_T[(unsigned char)(X >> 8)], 16) ^
|
|
||||||
rotl(SM4_SBOX_T[(unsigned char)X], 8);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int SM4_set_key(const unsigned char *key, SM4_KEY *ks)
|
static int
|
||||||
|
SM4_set_key(const unsigned char *key, SM4_KEY *ks)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Family Key
|
* Family Key
|
||||||
*/
|
*/
|
||||||
static const unsigned int FK[4] =
|
static const unsigned int FK[4] = {0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc};
|
||||||
{ 0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc };
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Constant Key
|
* Constant Key
|
||||||
*/
|
*/
|
||||||
static const unsigned int CK[32] = {
|
static const unsigned int CK[32] = {
|
||||||
0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269,
|
0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269, 0x70777E85, 0x8C939AA1, 0xA8AFB6BD, 0xC4CBD2D9,
|
||||||
0x70777E85, 0x8C939AA1, 0xA8AFB6BD, 0xC4CBD2D9,
|
0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249, 0x50575E65, 0x6C737A81, 0x888F969D, 0xA4ABB2B9,
|
||||||
0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249,
|
0xC0C7CED5, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229, 0x30373E45, 0x4C535A61, 0x686F767D, 0x848B9299,
|
||||||
0x50575E65, 0x6C737A81, 0x888F969D, 0xA4ABB2B9,
|
0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209, 0x10171E25, 0x2C333A41, 0x484F565D, 0x646B7279};
|
||||||
0xC0C7CED5, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229,
|
|
||||||
0x30373E45, 0x4C535A61, 0x686F767D, 0x848B9299,
|
|
||||||
0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209,
|
|
||||||
0x10171E25, 0x2C333A41, 0x484F565D, 0x646B7279
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned int K[4];
|
unsigned int K[4];
|
||||||
int i;
|
int i;
|
||||||
@ -160,10 +133,10 @@ static int SM4_set_key(const unsigned char *key, SM4_KEY *ks)
|
|||||||
unsigned int X = K[(i + 1) % 4] ^ K[(i + 2) % 4] ^ K[(i + 3) % 4] ^ CK[i];
|
unsigned int X = K[(i + 1) % 4] ^ K[(i + 2) % 4] ^ K[(i + 3) % 4] ^ CK[i];
|
||||||
unsigned int t = 0;
|
unsigned int t = 0;
|
||||||
|
|
||||||
t |= ((unsigned int)SM4_S[(unsigned char)(X >> 24)]) << 24;
|
t |= ((unsigned int) SM4_S[(unsigned char) (X >> 24)]) << 24;
|
||||||
t |= ((unsigned int)SM4_S[(unsigned char)(X >> 16)]) << 16;
|
t |= ((unsigned int) SM4_S[(unsigned char) (X >> 16)]) << 16;
|
||||||
t |= ((unsigned int)SM4_S[(unsigned char)(X >> 8)]) << 8;
|
t |= ((unsigned int) SM4_S[(unsigned char) (X >> 8)]) << 8;
|
||||||
t |= SM4_S[(unsigned char)X];
|
t |= SM4_S[(unsigned char) X];
|
||||||
|
|
||||||
t = t ^ rotl(t, 13) ^ rotl(t, 23);
|
t = t ^ rotl(t, 13) ^ rotl(t, 23);
|
||||||
K[i % 4] ^= t;
|
K[i % 4] ^= t;
|
||||||
@ -179,9 +152,10 @@ static int SM4_set_key(const unsigned char *key, SM4_KEY *ks)
|
|||||||
B1 ^= F(B0 ^ B2 ^ B3 ^ ks->rk[k1]); \
|
B1 ^= F(B0 ^ B2 ^ B3 ^ ks->rk[k1]); \
|
||||||
B2 ^= F(B0 ^ B1 ^ B3 ^ ks->rk[k2]); \
|
B2 ^= F(B0 ^ B1 ^ B3 ^ ks->rk[k2]); \
|
||||||
B3 ^= F(B0 ^ B1 ^ B2 ^ ks->rk[k3]); \
|
B3 ^= F(B0 ^ B1 ^ B2 ^ ks->rk[k3]); \
|
||||||
} while(0)
|
} while (0)
|
||||||
|
|
||||||
static void SM4_encrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *ks)
|
static void
|
||||||
|
SM4_encrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *ks)
|
||||||
{
|
{
|
||||||
unsigned int B0 = load_u32_be(in, 0);
|
unsigned int B0 = load_u32_be(in, 0);
|
||||||
unsigned int B1 = load_u32_be(in, 1);
|
unsigned int B1 = load_u32_be(in, 1);
|
||||||
@ -192,9 +166,9 @@ static void SM4_encrypt(const unsigned char *in, unsigned char *out, const SM4_K
|
|||||||
* Uses byte-wise sbox in the first and last rounds to provide some
|
* Uses byte-wise sbox in the first and last rounds to provide some
|
||||||
* protection from cache based side channels.
|
* protection from cache based side channels.
|
||||||
*/
|
*/
|
||||||
SM4_RNDS( 0, 1, 2, 3, SM4_T_slow);
|
SM4_RNDS(0, 1, 2, 3, SM4_T_slow);
|
||||||
SM4_RNDS( 4, 5, 6, 7, SM4_T);
|
SM4_RNDS(4, 5, 6, 7, SM4_T);
|
||||||
SM4_RNDS( 8, 9, 10, 11, SM4_T);
|
SM4_RNDS(8, 9, 10, 11, SM4_T);
|
||||||
SM4_RNDS(12, 13, 14, 15, SM4_T);
|
SM4_RNDS(12, 13, 14, 15, SM4_T);
|
||||||
SM4_RNDS(16, 17, 18, 19, SM4_T);
|
SM4_RNDS(16, 17, 18, 19, SM4_T);
|
||||||
SM4_RNDS(20, 21, 22, 23, SM4_T);
|
SM4_RNDS(20, 21, 22, 23, SM4_T);
|
||||||
@ -207,7 +181,8 @@ static void SM4_encrypt(const unsigned char *in, unsigned char *out, const SM4_K
|
|||||||
store_u32_be(B0, out + 12);
|
store_u32_be(B0, out + 12);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SM4_decrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *ks)
|
static void
|
||||||
|
SM4_decrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *ks)
|
||||||
{
|
{
|
||||||
unsigned int B0 = load_u32_be(in, 0);
|
unsigned int B0 = load_u32_be(in, 0);
|
||||||
unsigned int B1 = load_u32_be(in, 1);
|
unsigned int B1 = load_u32_be(in, 1);
|
||||||
@ -220,8 +195,8 @@ static void SM4_decrypt(const unsigned char *in, unsigned char *out, const SM4_K
|
|||||||
SM4_RNDS(19, 18, 17, 16, SM4_T);
|
SM4_RNDS(19, 18, 17, 16, SM4_T);
|
||||||
SM4_RNDS(15, 14, 13, 12, SM4_T);
|
SM4_RNDS(15, 14, 13, 12, SM4_T);
|
||||||
SM4_RNDS(11, 10, 9, 8, SM4_T);
|
SM4_RNDS(11, 10, 9, 8, SM4_T);
|
||||||
SM4_RNDS( 7, 6, 5, 4, SM4_T);
|
SM4_RNDS(7, 6, 5, 4, SM4_T);
|
||||||
SM4_RNDS( 3, 2, 1, 0, SM4_T_slow);
|
SM4_RNDS(3, 2, 1, 0, SM4_T_slow);
|
||||||
|
|
||||||
store_u32_be(B3, out);
|
store_u32_be(B3, out);
|
||||||
store_u32_be(B2, out + 4);
|
store_u32_be(B2, out + 4);
|
||||||
@ -229,9 +204,14 @@ static void SM4_decrypt(const unsigned char *in, unsigned char *out, const SM4_K
|
|||||||
store_u32_be(B0, out + 12);
|
store_u32_be(B0, out + 12);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out,
|
static void
|
||||||
size_t len, const void *key,
|
CRYPTO_ofb128_encrypt(const unsigned char *in,
|
||||||
unsigned char ivec[16], int *num, block128_f block)
|
unsigned char *out,
|
||||||
|
size_t len,
|
||||||
|
const void *key,
|
||||||
|
unsigned char ivec[16],
|
||||||
|
int *num,
|
||||||
|
block128_f block)
|
||||||
{
|
{
|
||||||
unsigned int n;
|
unsigned int n;
|
||||||
size_t l = 0;
|
size_t l = 0;
|
||||||
@ -239,9 +219,7 @@ static void CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out,
|
|||||||
n = *num;
|
n = *num;
|
||||||
|
|
||||||
while (l < len) {
|
while (l < len) {
|
||||||
if (n == 0) {
|
if (n == 0) { (*block)(ivec, ivec, key); }
|
||||||
(*block) (ivec, ivec, key);
|
|
||||||
}
|
|
||||||
out[l] = in[l] ^ ivec[n];
|
out[l] = in[l] ^ ivec[n];
|
||||||
++l;
|
++l;
|
||||||
n = (n + 1) % 16;
|
n = (n + 1) % 16;
|
||||||
@ -250,23 +228,24 @@ static void CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out,
|
|||||||
*num = n;
|
*num = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sm4_init_key(const unsigned char *key, SM4_KEY *ks)
|
static int
|
||||||
|
sm4_init_key(const unsigned char *key, SM4_KEY *ks)
|
||||||
{
|
{
|
||||||
SM4_set_key(key, ks);
|
SM4_set_key(key, ks);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sm4_ofb128_encrypt(const unsigned char *in, unsigned char *out,
|
static void
|
||||||
size_t length, const SM4_KEY *key,
|
sm4_ofb128_encrypt(const unsigned char *in, unsigned char *out, size_t length, const SM4_KEY *key, unsigned char *ivec)
|
||||||
unsigned char *ivec)
|
|
||||||
{
|
{
|
||||||
int num = 0;
|
int num = 0;
|
||||||
|
|
||||||
CRYPTO_ofb128_encrypt(in, out, length, key, ivec, &num, (block128_f)SM4_encrypt);
|
CRYPTO_ofb128_encrypt(in, out, length, key, ivec, &num, (block128_f) SM4_encrypt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_sm4_ofb_encrypt(const char *in_buff, int in_len, char *out_buff, int *out_len, const char *key, const char *iv)
|
void
|
||||||
|
do_sm4_ofb_encrypt(const char *in_buff, int in_len, char *out_buff, int *out_len, const char *key, const char *iv)
|
||||||
{
|
{
|
||||||
SM4_KEY ks;
|
SM4_KEY ks;
|
||||||
char ivec[16];
|
char ivec[16];
|
||||||
@ -278,7 +257,8 @@ void do_sm4_ofb_encrypt(const char *in_buff, int in_len, char *out_buff, int *ou
|
|||||||
*out_len = in_len;
|
*out_len = in_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_sm4_ecb_decrypt(const char *in_buff, char *out_buff, const char *key)
|
void
|
||||||
|
do_sm4_ecb_decrypt(const char *in_buff, char *out_buff, const char *key)
|
||||||
{
|
{
|
||||||
SM4_KEY ks;
|
SM4_KEY ks;
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
|
#include "svac_dec.h"
|
||||||
|
#include "sm2.h"
|
||||||
|
#include "sm3.h"
|
||||||
|
#include <malloc.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <malloc.h>
|
|
||||||
#include "svac_dec.h"
|
|
||||||
#include "sm3.h"
|
|
||||||
#include "sm2.h"
|
|
||||||
|
|
||||||
#define PRINT_ERR printf
|
#define PRINT_ERR printf
|
||||||
#define PRINT_INFO printf
|
#define PRINT_INFO printf
|
||||||
@ -33,8 +33,7 @@
|
|||||||
#define SVAC_SEC_SLICE 9
|
#define SVAC_SEC_SLICE 9
|
||||||
#define SVAC_AUTH_SLICE 10
|
#define SVAC_AUTH_SLICE 10
|
||||||
|
|
||||||
struct nalu_data
|
struct nalu_data {
|
||||||
{
|
|
||||||
char *addr;
|
char *addr;
|
||||||
int len;
|
int len;
|
||||||
int encryption_idc;
|
int encryption_idc;
|
||||||
@ -43,14 +42,12 @@ struct nalu_data
|
|||||||
int need_add_racing_code;
|
int need_add_racing_code;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct vkek_info
|
struct vkek_info {
|
||||||
{
|
|
||||||
char vkek[16];
|
char vkek[16];
|
||||||
char version[32];
|
char version[32];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sec_param
|
struct sec_param {
|
||||||
{
|
|
||||||
/* <20><>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/* <20><>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||||
int encrypt_flag; //<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>
|
int encrypt_flag; //<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>
|
||||||
int encrypt_type; //<2F><><EFBFBD><EFBFBD><EFBFBD>㷨
|
int encrypt_type; //<2F><><EFBFBD><EFBFBD><EFBFBD>㷨
|
||||||
@ -61,7 +58,7 @@ struct sec_param
|
|||||||
int vek_encrypt_type; //vek<65><6B><EFBFBD><EFBFBD><EFBFBD>㷨
|
int vek_encrypt_type; //vek<65><6B><EFBFBD><EFBFBD><EFBFBD>㷨
|
||||||
char evek[32]; //evek
|
char evek[32]; //evek
|
||||||
int vek_len; //vek<65><6B><EFBFBD><EFBFBD>
|
int vek_len; //vek<65><6B><EFBFBD><EFBFBD>
|
||||||
char vkek_version[128]; //vkek version
|
char vkek_version[128];//vkek version
|
||||||
int version_len; //vkek version<6F><6E><EFBFBD><EFBFBD>
|
int version_len; //vkek version<6F><6E><EFBFBD><EFBFBD>
|
||||||
int iv_flag; //<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>iv
|
int iv_flag; //<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>iv
|
||||||
char iv[32]; //iv
|
char iv[32]; //iv
|
||||||
@ -72,15 +69,13 @@ struct sec_param
|
|||||||
char camera_idc[19]; //֤<><D6A4>id
|
char camera_idc[19]; //֤<><D6A4>id
|
||||||
};
|
};
|
||||||
|
|
||||||
struct hash_cache
|
struct hash_cache {
|
||||||
{
|
|
||||||
/* һ<><D2BB>gop<6F><70><EFBFBD>256֡ */
|
/* һ<><D2BB>gop<6F><70><EFBFBD>256֡ */
|
||||||
char hash[32];
|
char hash[32];
|
||||||
int frame_num;
|
int frame_num;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct svac_dec_handle
|
struct svac_dec_handle {
|
||||||
{
|
|
||||||
int magic;
|
int magic;
|
||||||
|
|
||||||
/* sm2 <20><>Կ */
|
/* sm2 <20><>Կ */
|
||||||
@ -120,64 +115,59 @@ struct svac_dec_handle
|
|||||||
|
|
||||||
char idr_hash[32];
|
char idr_hash[32];
|
||||||
struct hash_cache cache[HASH_CACHE_NUM];
|
struct hash_cache cache[HASH_CACHE_NUM];
|
||||||
int cache_idx; // 0 ~ HASH_CACHE_NUM - 1
|
int cache_idx;// 0 ~ HASH_CACHE_NUM - 1
|
||||||
|
|
||||||
PFSECURITYPARAMCB callback;
|
PFSECURITYPARAMCB callback;
|
||||||
void *context;
|
void *context;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define CHECK_HANDLE(h) \
|
#define CHECK_HANDLE(h) \
|
||||||
do \
|
do { \
|
||||||
{ \
|
if (!h || h->magic != MAGIC_VALUE) { \
|
||||||
if (!h || h->magic != MAGIC_VALUE) \
|
|
||||||
{ \
|
|
||||||
PRINT_ERR("handle is error!\n"); \
|
PRINT_ERR("handle is error!\n"); \
|
||||||
return -1; \
|
return -1; \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
int do_base64_decode(unsigned char *dst, int *dlen, unsigned char *src, int slen);
|
||||||
|
void do_sm4_ecb_decrypt(const char *in_buff, char *out_buff, const char *key);
|
||||||
|
|
||||||
/* <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ַƫ<D6B7><C6AB>Ϊoffset<65><74><EFBFBD><EFBFBD>read ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD> */
|
/* <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ַƫ<D6B7><C6AB>Ϊoffset<65><74><EFBFBD><EFBFBD>read ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD> */
|
||||||
static int read_bits(char *data, int read_len, char *addr, int offset)
|
static int
|
||||||
|
read_bits(char *data, int read_len, char *addr, int offset)
|
||||||
{
|
{
|
||||||
int offset_in_bytes = offset & 7;
|
int offset_in_bytes = offset & 7;
|
||||||
int offset_of_bytes = offset / 8;
|
int offset_of_bytes = offset / 8;
|
||||||
|
|
||||||
/* if need read twice */
|
/* if need read twice */
|
||||||
if (offset_in_bytes && offset_in_bytes + read_len > 8)
|
if (offset_in_bytes && offset_in_bytes + read_len > 8) {
|
||||||
{
|
|
||||||
int second_read_len = offset_in_bytes + read_len - 8;
|
int second_read_len = offset_in_bytes + read_len - 8;
|
||||||
|
|
||||||
*data = 0;
|
*data = 0;
|
||||||
*data = (addr[offset_of_bytes] & ((1 << (8 - offset_in_bytes)) - 1)) << second_read_len;
|
*data = (addr[offset_of_bytes] & ((1 << (8 - offset_in_bytes)) - 1)) << second_read_len;
|
||||||
*data |= (addr[offset_of_bytes + 1] >> (8 - second_read_len)) & ((1 << second_read_len) - 1);
|
*data |= (addr[offset_of_bytes + 1] >> (8 - second_read_len)) & ((1 << second_read_len) - 1);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
*data = ((1 << read_len) - 1) & (addr[offset_of_bytes] >> (8 - read_len - offset_in_bytes));
|
*data = ((1 << read_len) - 1) & (addr[offset_of_bytes] >> (8 - read_len - offset_in_bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
return offset + read_len;
|
return offset + read_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_long_data_bits(int *data, int read_len, char *addr, int offset)
|
static int
|
||||||
|
read_long_data_bits(int *data, int read_len, char *addr, int offset)
|
||||||
{
|
{
|
||||||
char tmp;
|
char tmp;
|
||||||
int ret_offset = 0;
|
int ret_offset = 0;
|
||||||
|
|
||||||
if (read_len <= 8)
|
if (read_len <= 8) {
|
||||||
{
|
|
||||||
read_len = read_bits(&tmp, read_len, addr, offset);
|
read_len = read_bits(&tmp, read_len, addr, offset);
|
||||||
*data = tmp;
|
*data = tmp;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
int i;
|
int i;
|
||||||
int loop;
|
int loop;
|
||||||
unsigned int first_read_len = read_len & 7;
|
unsigned int first_read_len = read_len & 7;
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD>ֽڵĵ<DAB5>first_read_len<65><6E><EFBFBD><EFBFBD>*/
|
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD>ֽڵĵ<DAB5>first_read_len<65><6E><EFBFBD><EFBFBD>*/
|
||||||
if (first_read_len)
|
if (first_read_len) {
|
||||||
{
|
|
||||||
offset = read_bits(&tmp, first_read_len, addr, offset);
|
offset = read_bits(&tmp, first_read_len, addr, offset);
|
||||||
*data = tmp << (read_len - first_read_len);
|
*data = tmp << (read_len - first_read_len);
|
||||||
}
|
}
|
||||||
@ -185,8 +175,7 @@ static int read_long_data_bits(int *data, int read_len, char *addr, int offset)
|
|||||||
loop = (read_len - first_read_len) / 8;
|
loop = (read_len - first_read_len) / 8;
|
||||||
ret_offset = offset;
|
ret_offset = offset;
|
||||||
|
|
||||||
for (i = 0; i < loop; i++)
|
for (i = 0; i < loop; i++) {
|
||||||
{
|
|
||||||
ret_offset = read_bits(&tmp, 8, addr, ret_offset);
|
ret_offset = read_bits(&tmp, 8, addr, ret_offset);
|
||||||
*data |= tmp << (read_len - first_read_len - 8 - 8 * i);
|
*data |= tmp << (read_len - first_read_len - 8 - 8 * i);
|
||||||
}
|
}
|
||||||
@ -195,20 +184,17 @@ static int read_long_data_bits(int *data, int read_len, char *addr, int offset)
|
|||||||
return ret_offset;
|
return ret_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_string_bits(char *p, int read_len, char *addr, int offset)
|
static int
|
||||||
|
read_string_bits(char *p, int read_len, char *addr, int offset)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
|
|
||||||
while (len < read_len)
|
while (len < read_len) {
|
||||||
{
|
if (read_len - len > 8) {
|
||||||
if (read_len - len > 8)
|
|
||||||
{
|
|
||||||
offset = read_bits(p + i, 8, addr, offset);
|
offset = read_bits(p + i, 8, addr, offset);
|
||||||
len += 8;
|
len += 8;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
offset = read_bits(p + i, read_len - len, addr, offset);
|
offset = read_bits(p + i, read_len - len, addr, offset);
|
||||||
len += read_len - len;
|
len += read_len - len;
|
||||||
}
|
}
|
||||||
@ -220,7 +206,8 @@ static int read_string_bits(char *p, int read_len, char *addr, int offset)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if find a nal, return the next address; if it's the end, return NULL */
|
/* if find a nal, return the next address; if it's the end, return NULL */
|
||||||
static char *findNal(char *buf, int *frame_len, char **nal_start, int *nal_len)
|
static char *
|
||||||
|
findNal(char *buf, int *frame_len, char **nal_start, int *nal_len)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char *start = NULL;
|
char *start = NULL;
|
||||||
@ -230,13 +217,10 @@ static char *findNal(char *buf, int *frame_len, char **nal_start, int *nal_len)
|
|||||||
*nal_len = 0;
|
*nal_len = 0;
|
||||||
|
|
||||||
/* parsing each NALU */
|
/* parsing each NALU */
|
||||||
for (i = 0; i < (int)(*frame_len - 3);i++)
|
for (i = 0; i < (int) (*frame_len - 3); i++) {
|
||||||
{
|
if (buf[i] == 0x00 && buf[i + 1] == 0x00 && buf[i + 2] == 0x00 && buf[i + 3] == 0x01) {
|
||||||
if (buf[i] == 0x00 && buf[i + 1] == 0x00 && buf[i + 2] == 0x00 && buf[i + 3] == 0x01)
|
|
||||||
{
|
|
||||||
/* not the first loop */
|
/* not the first loop */
|
||||||
if (start)
|
if (start) {
|
||||||
{
|
|
||||||
len = buf + i - start;
|
len = buf + i - start;
|
||||||
*nal_start = start;
|
*nal_start = start;
|
||||||
*nal_len = len;
|
*nal_len = len;
|
||||||
@ -247,8 +231,7 @@ static char *findNal(char *buf, int *frame_len, char **nal_start, int *nal_len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (start)
|
if (start) len = buf + *frame_len - start;
|
||||||
len = buf + *frame_len - start;
|
|
||||||
|
|
||||||
*nal_start = start;
|
*nal_start = start;
|
||||||
*nal_len = len;
|
*nal_len = len;
|
||||||
@ -257,25 +240,24 @@ static char *findNal(char *buf, int *frame_len, char **nal_start, int *nal_len)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int svac_parse_nalu(struct svac_dec_handle *h, char *frame_data, int frame_len)
|
static int
|
||||||
|
svac_parse_nalu(struct svac_dec_handle *h, char *frame_data, int frame_len)
|
||||||
{
|
{
|
||||||
char *ptr, *start = NULL;
|
char *ptr, *start = NULL;
|
||||||
int nal_len, frame_len_left = frame_len;
|
int nal_len, frame_len_left = frame_len;
|
||||||
|
|
||||||
ptr = frame_data;
|
ptr = frame_data;
|
||||||
|
|
||||||
do
|
do {
|
||||||
{
|
|
||||||
start = NULL;
|
start = NULL;
|
||||||
nal_len = 0;
|
nal_len = 0;
|
||||||
ptr = findNal(ptr, &frame_len_left, &start, &nal_len);
|
ptr = findNal(ptr, &frame_len_left, &start, &nal_len);
|
||||||
if (start)
|
if (start) {
|
||||||
{
|
|
||||||
memset(&h->nalu[h->nalu_num], 0, sizeof(struct nalu_data));
|
memset(&h->nalu[h->nalu_num], 0, sizeof(struct nalu_data));
|
||||||
|
|
||||||
h->nalu[h->nalu_num].addr = start;
|
h->nalu[h->nalu_num].addr = start;
|
||||||
h->nalu[h->nalu_num].len = nal_len;
|
h->nalu[h->nalu_num].len = nal_len;
|
||||||
h->nalu[h->nalu_num].encryption_idc = start[4] & 0x2 ? 1 : 0; //svac2.0 p21
|
h->nalu[h->nalu_num].encryption_idc = start[4] & 0x2 ? 1 : 0;//svac2.0 p21
|
||||||
h->nalu[h->nalu_num].authentication_idc = start[4] & 0x1; //svac2.0 p21
|
h->nalu[h->nalu_num].authentication_idc = start[4] & 0x1; //svac2.0 p21
|
||||||
h->nalu[h->nalu_num].type = (start[4] >> 2) & 0xf; //svac2.0 p21
|
h->nalu[h->nalu_num].type = (start[4] >> 2) & 0xf; //svac2.0 p21
|
||||||
|
|
||||||
@ -285,10 +267,9 @@ static int svac_parse_nalu(struct svac_dec_handle *h, char *frame_data, int fram
|
|||||||
h->sec_nalu_flag = 1;
|
h->sec_nalu_flag = 1;
|
||||||
else if (SVAC_AUTH_SLICE == h->nalu[h->nalu_num].type)
|
else if (SVAC_AUTH_SLICE == h->nalu[h->nalu_num].type)
|
||||||
h->auth_nalu_flag = 1;
|
h->auth_nalu_flag = 1;
|
||||||
else if (SVAC_PPS_SLICE == h->nalu[h->nalu_num].type)
|
else if (SVAC_PPS_SLICE == h->nalu[h->nalu_num].type) {
|
||||||
{
|
|
||||||
/* pps<70>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>֡<EFBFBD><D6A1> svac2.0 p23 */
|
/* pps<70>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>֡<EFBFBD><D6A1> svac2.0 p23 */
|
||||||
h->frame_num = (unsigned char)start[5];
|
h->frame_num = (unsigned char) start[5];
|
||||||
//PRINT_INFO("frame %d\n", h->frame_num);
|
//PRINT_INFO("frame %d\n", h->frame_num);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,33 +281,29 @@ static int svac_parse_nalu(struct svac_dec_handle *h, char *frame_data, int fram
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD> svac <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD> */
|
/* <20><><EFBFBD><EFBFBD> svac <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD> */
|
||||||
static int do_add_racing_Code(char *ptr, int *len, int offset)
|
static int
|
||||||
|
do_add_racing_Code(char *ptr, int *len, int offset)
|
||||||
{
|
{
|
||||||
int i, find = 0, start = offset;
|
int i, find = 0, start = offset;
|
||||||
unsigned int inlen = *len;
|
unsigned int inlen = *len;
|
||||||
|
|
||||||
for (i = start; i < inlen - 2; i++)
|
for (i = start; i < inlen - 2; i++) {
|
||||||
{
|
if (ptr[i] == 0 && ptr[i + 1] == 0 && !(ptr[i + 2] & 0xfc)) {
|
||||||
if (ptr[i] == 0 && ptr[i + 1] == 0 && !(ptr[i + 2] & 0xfc))
|
|
||||||
{
|
|
||||||
find = 1;
|
find = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (find)
|
if (find) {
|
||||||
{
|
|
||||||
unsigned int code_num = 0, zero_count = 0;
|
unsigned int code_num = 0, zero_count = 0;
|
||||||
unsigned int allocnum = (int)((float)inlen*1.6);
|
unsigned int allocnum = (int) ((float) inlen * 1.6);
|
||||||
char* tmp=NULL;
|
char *tmp = NULL;
|
||||||
if (allocnum < 2048) allocnum=2048;
|
if (allocnum < 2048) allocnum = 2048;
|
||||||
tmp = (char *)malloc(allocnum);
|
tmp = (char *) malloc(allocnum);
|
||||||
|
|
||||||
memcpy(tmp, ptr, start);
|
memcpy(tmp, ptr, start);
|
||||||
for (i = start; i < inlen; i++)
|
for (i = start; i < inlen; i++) {
|
||||||
{
|
if (zero_count == 2 && !(ptr[i] & 0xfc)) {
|
||||||
if (zero_count == 2 && !(ptr[i] & 0xfc))
|
|
||||||
{
|
|
||||||
tmp[i + code_num] = 3;
|
tmp[i + code_num] = 3;
|
||||||
code_num++;
|
code_num++;
|
||||||
i--;
|
i--;
|
||||||
@ -338,9 +315,8 @@ static int do_add_racing_Code(char *ptr, int *len, int offset)
|
|||||||
zero_count++;
|
zero_count++;
|
||||||
else
|
else
|
||||||
zero_count = 0;
|
zero_count = 0;
|
||||||
if (i + code_num>=allocnum)
|
if (i + code_num >= allocnum) {
|
||||||
{
|
code_num = allocnum - i - 1;
|
||||||
code_num = allocnum - i-1;
|
|
||||||
printf("svac adding racing code exceed alloced memory len!");
|
printf("svac adding racing code exceed alloced memory len!");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -355,9 +331,9 @@ static int do_add_racing_Code(char *ptr, int *len, int offset)
|
|||||||
return find;
|
return find;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* ȥ<><C8A5> svac <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD> */
|
/* ȥ<><C8A5> svac <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD> */
|
||||||
static int do_remove_racing_code(char *ptr, int *len, int offset)
|
static int
|
||||||
|
do_remove_racing_code(char *ptr, int *len, int offset)
|
||||||
{
|
{
|
||||||
int i, find = 0, start;
|
int i, find = 0, start;
|
||||||
unsigned int inlen;
|
unsigned int inlen;
|
||||||
@ -365,23 +341,18 @@ static int do_remove_racing_code(char *ptr, int *len, int offset)
|
|||||||
inlen = *len;
|
inlen = *len;
|
||||||
start = offset;
|
start = offset;
|
||||||
|
|
||||||
for (i = start; i < inlen - 2; i++)
|
for (i = start; i < inlen - 2; i++) {
|
||||||
{
|
if (ptr[i] == 0 && ptr[i + 1] == 0 && ptr[i + 2] == 0x3) {
|
||||||
if (ptr[i] == 0 && ptr[i + 1] == 0 && ptr[i + 2] == 0x3)
|
|
||||||
{
|
|
||||||
find = 1;
|
find = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (find)
|
if (find) {
|
||||||
{
|
|
||||||
unsigned int code_num = 0, zero_count = 0;
|
unsigned int code_num = 0, zero_count = 0;
|
||||||
|
|
||||||
for (i = start; i < inlen; i++)
|
for (i = start; i < inlen; i++) {
|
||||||
{
|
if (zero_count == 2 && ptr[i] == 3) {
|
||||||
if (zero_count == 2 && ptr[i] == 3)
|
|
||||||
{
|
|
||||||
code_num++;
|
code_num++;
|
||||||
zero_count = 0;
|
zero_count = 0;
|
||||||
continue;
|
continue;
|
||||||
@ -400,7 +371,8 @@ static int do_remove_racing_code(char *ptr, int *len, int offset)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nalu */
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nalu */
|
||||||
static int do_parse_security_nalu(struct svac_dec_handle *h, char *start, int len, int offset_bytes)
|
static int
|
||||||
|
do_parse_security_nalu(struct svac_dec_handle *h, char *start, int len, int offset_bytes)
|
||||||
{
|
{
|
||||||
char tmp;
|
char tmp;
|
||||||
struct sec_param *param = &h->new_param;
|
struct sec_param *param = &h->new_param;
|
||||||
@ -416,8 +388,7 @@ static int do_parse_security_nalu(struct svac_dec_handle *h, char *start, int le
|
|||||||
offset = read_bits(&tmp, 1, start, offset);
|
offset = read_bits(&tmp, 1, start, offset);
|
||||||
param->auth_flag = tmp;
|
param->auth_flag = tmp;
|
||||||
|
|
||||||
if (param->encrypt_flag)
|
if (param->encrypt_flag) {
|
||||||
{
|
|
||||||
/* read encrypt_type */
|
/* read encrypt_type */
|
||||||
offset = read_bits(&tmp, 4, start, offset);
|
offset = read_bits(&tmp, 4, start, offset);
|
||||||
if (tmp == 0)
|
if (tmp == 0)
|
||||||
@ -432,8 +403,7 @@ static int do_parse_security_nalu(struct svac_dec_handle *h, char *start, int le
|
|||||||
offset = read_bits(&tmp, 1, start, offset);
|
offset = read_bits(&tmp, 1, start, offset);
|
||||||
param->iv_flag = tmp;
|
param->iv_flag = tmp;
|
||||||
|
|
||||||
if (param->vek_flag)
|
if (param->vek_flag) {
|
||||||
{
|
|
||||||
/* read vek_encryption_type */
|
/* read vek_encryption_type */
|
||||||
offset = read_bits(&tmp, 4, start, offset);
|
offset = read_bits(&tmp, 4, start, offset);
|
||||||
param->vek_encrypt_type = SM4_ECB;
|
param->vek_encrypt_type = SM4_ECB;
|
||||||
@ -452,15 +422,13 @@ static int do_parse_security_nalu(struct svac_dec_handle *h, char *start, int le
|
|||||||
offset = read_string_bits(param->vkek_version, param->version_len * 8, start, offset);
|
offset = read_string_bits(param->vkek_version, param->version_len * 8, start, offset);
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD>vkek bug */
|
/* <20><><EFBFBD><EFBFBD>vkek bug */
|
||||||
if (param->version_len > 32)
|
if (param->version_len > 32) {
|
||||||
{
|
|
||||||
param->version_len = 19;
|
param->version_len = 19;
|
||||||
memset(param->vkek_version + 19, 0, sizeof(param->vkek_version) - 19);
|
memset(param->vkek_version + 19, 0, sizeof(param->vkek_version) - 19);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (param->iv_flag)
|
if (param->iv_flag) {
|
||||||
{
|
|
||||||
/* read iv_length_minus1 */
|
/* read iv_length_minus1 */
|
||||||
offset = read_bits(&tmp, 8, start, offset);
|
offset = read_bits(&tmp, 8, start, offset);
|
||||||
param->iv_len = tmp + 1;
|
param->iv_len = tmp + 1;
|
||||||
@ -469,8 +437,7 @@ static int do_parse_security_nalu(struct svac_dec_handle *h, char *start, int le
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (param->auth_flag)
|
if (param->auth_flag) {
|
||||||
{
|
|
||||||
/* read hash_type */
|
/* read hash_type */
|
||||||
offset = read_bits(&tmp, 2, start, offset);
|
offset = read_bits(&tmp, 2, start, offset);
|
||||||
param->hash_type = SM3_HASH;
|
param->hash_type = SM3_HASH;
|
||||||
@ -488,21 +455,19 @@ static int do_parse_security_nalu(struct svac_dec_handle *h, char *start, int le
|
|||||||
//PRINT_INFO("IDR %d period %d\n", param->only_IDR, param->hash_period);
|
//PRINT_INFO("IDR %d period %d\n", param->only_IDR, param->hash_period);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (param->vek_flag || param->auth_flag)
|
if (param->vek_flag || param->auth_flag) offset = read_string_bits(param->camera_id, 160, start, offset);
|
||||||
offset = read_string_bits(param->camera_id, 160, start, offset);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nalu */
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nalu */
|
||||||
static int parse_security_nalu(struct svac_dec_handle *h)
|
static int
|
||||||
|
parse_security_nalu(struct svac_dec_handle *h)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < h->nalu_num; i++)
|
for (i = 0; i < h->nalu_num; i++) {
|
||||||
{
|
if (h->nalu[i].type == SVAC_SEC_SLICE) {
|
||||||
if (h->nalu[i].type == SVAC_SEC_SLICE)
|
|
||||||
{
|
|
||||||
do_remove_racing_code(h->nalu[i].addr, &h->nalu[i].len, 5);
|
do_remove_racing_code(h->nalu[i].addr, &h->nalu[i].len, 5);
|
||||||
h->nalu[i].need_add_racing_code = 1;
|
h->nalu[i].need_add_racing_code = 1;
|
||||||
|
|
||||||
@ -521,7 +486,8 @@ static int parse_security_nalu(struct svac_dec_handle *h)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4>Ԫnalu */
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4>Ԫnalu */
|
||||||
static int do_parse_auth_nalu(struct svac_dec_handle *h, char *start, int len, int offset_bytes, struct auth_info *info)
|
static int
|
||||||
|
do_parse_auth_nalu(struct svac_dec_handle *h, char *start, int len, int offset_bytes, struct auth_info *info)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
char tmp;
|
char tmp;
|
||||||
@ -539,26 +505,21 @@ static int do_parse_auth_nalu(struct svac_dec_handle *h, char *start, int len, i
|
|||||||
sign_b64_len = tmp + 1;
|
sign_b64_len = tmp + 1;
|
||||||
|
|
||||||
/* read authentication_data */
|
/* read authentication_data */
|
||||||
for (i = 0; i < sign_b64_len; i++)
|
for (i = 0; i < sign_b64_len; i++) offset = read_bits(&sign_b64[i], 8, start, offset);
|
||||||
offset = read_bits(&sign_b64[i], 8, start, offset);
|
|
||||||
|
|
||||||
h->sign_len = sizeof(h->sign);
|
h->sign_len = sizeof(h->sign);
|
||||||
ret = do_base64_decode(h->sign, &h->sign_len, sign_b64, sign_b64_len);
|
ret = do_base64_decode(h->sign, &h->sign_len, sign_b64, sign_b64_len);
|
||||||
if (ret)
|
if (ret) {
|
||||||
{
|
|
||||||
PRINT_ERR("do_base64_decode failed! ret %d\n", ret);
|
PRINT_ERR("do_base64_decode failed! ret %d\n", ret);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
info->do_auth = 1;
|
info->do_auth = 1;
|
||||||
|
|
||||||
if (h->curr_param.only_IDR)
|
if (h->curr_param.only_IDR) {
|
||||||
{
|
|
||||||
ret = do_sm2_verify(h->sm2_pubkey, h->idr_hash, 32, h->sign);
|
ret = do_sm2_verify(h->sm2_pubkey, h->idr_hash, 32, h->sign);
|
||||||
info->auth_result = ret == -1 ? 0 : 1;
|
info->auth_result = ret == -1 ? 0 : 1;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
int j;
|
int j;
|
||||||
char fin_hash[32];
|
char fin_hash[32];
|
||||||
struct sm3_ctx ctx;
|
struct sm3_ctx ctx;
|
||||||
@ -566,36 +527,29 @@ static int do_parse_auth_nalu(struct svac_dec_handle *h, char *start, int len, i
|
|||||||
|
|
||||||
i = h->cache_idx;
|
i = h->cache_idx;
|
||||||
|
|
||||||
for (j = 0; j < HASH_CACHE_NUM; j++)
|
for (j = 0; j < HASH_CACHE_NUM; j++) {
|
||||||
{
|
|
||||||
cache = &h->cache[i];
|
cache = &h->cache[i];
|
||||||
if (cache->frame_num == h->sign_frame_num)
|
if (cache->frame_num == h->sign_frame_num) {
|
||||||
{
|
|
||||||
//PRINT_INFO("find! %d\n", h->sign_frame_num);
|
//PRINT_INFO("find! %d\n", h->sign_frame_num);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
i--;
|
i--;
|
||||||
if (i < 0)
|
if (i < 0) i += HASH_CACHE_NUM;
|
||||||
i += HASH_CACHE_NUM;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
i = i - h->curr_param.hash_period + 1;
|
i = i - h->curr_param.hash_period + 1;
|
||||||
if (i < 0)
|
if (i < 0) i += HASH_CACHE_NUM;
|
||||||
i += HASH_CACHE_NUM;
|
|
||||||
|
|
||||||
cache = &h->cache[i];
|
cache = &h->cache[i];
|
||||||
memcpy(fin_hash, cache->hash, 32);
|
memcpy(fin_hash, cache->hash, 32);
|
||||||
if (h->sign_frame_num == 1)
|
if (h->sign_frame_num == 1) goto verify;
|
||||||
goto verify;
|
|
||||||
|
|
||||||
for (j = 1; j < h->curr_param.hash_period; j++)
|
for (j = 1; j < h->curr_param.hash_period; j++) {
|
||||||
{
|
|
||||||
sm3_init(&ctx);
|
sm3_init(&ctx);
|
||||||
sm3_update(&ctx, fin_hash, 32);
|
sm3_update(&ctx, fin_hash, 32);
|
||||||
//hexdump("hash0", fin_hash, 32);
|
//hexdump("hash0", fin_hash, 32);
|
||||||
i++;
|
i++;
|
||||||
if (i >= HASH_CACHE_NUM)
|
if (i >= HASH_CACHE_NUM) i -= HASH_CACHE_NUM;
|
||||||
i -= HASH_CACHE_NUM;
|
|
||||||
|
|
||||||
cache = &h->cache[i];
|
cache = &h->cache[i];
|
||||||
//hexdump("hash1", cache->hash, 32);
|
//hexdump("hash1", cache->hash, 32);
|
||||||
@ -603,7 +557,7 @@ static int do_parse_auth_nalu(struct svac_dec_handle *h, char *start, int len, i
|
|||||||
sm3_final(&ctx, fin_hash);
|
sm3_final(&ctx, fin_hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
verify:
|
verify:
|
||||||
//hexdump("sign", fin_hash, 32);
|
//hexdump("sign", fin_hash, 32);
|
||||||
ret = do_sm2_verify(h->sm2_pubkey, fin_hash, 32, h->sign);
|
ret = do_sm2_verify(h->sm2_pubkey, fin_hash, 32, h->sign);
|
||||||
info->auth_result = ret == -1 ? 0 : 1;
|
info->auth_result = ret == -1 ? 0 : 1;
|
||||||
@ -612,16 +566,14 @@ verify:
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4>Ԫnalu */
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4>Ԫnalu */
|
||||||
static int parse_svac_auth_nal(struct svac_dec_handle *h, struct auth_info *info)
|
static int
|
||||||
|
parse_svac_auth_nal(struct svac_dec_handle *h, struct auth_info *info)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < h->nalu_num; i++)
|
for (i = 0; i < h->nalu_num; i++) {
|
||||||
{
|
if (h->nalu[i].type == SVAC_AUTH_SLICE) {
|
||||||
if (h->nalu[i].type == SVAC_AUTH_SLICE)
|
|
||||||
{
|
|
||||||
do_remove_racing_code(h->nalu[i].addr, &h->nalu[i].len, 5);
|
do_remove_racing_code(h->nalu[i].addr, &h->nalu[i].len, 5);
|
||||||
h->nalu[i].need_add_racing_code = 1;
|
h->nalu[i].need_add_racing_code = 1;
|
||||||
|
|
||||||
@ -636,17 +588,16 @@ static int parse_svac_auth_nal(struct svac_dec_handle *h, struct auth_info *info
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int dec_vek(struct svac_dec_handle *h)
|
static int
|
||||||
|
dec_vek(struct svac_dec_handle *h)
|
||||||
{
|
{
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
h->find_vkek_flag = 0;
|
h->find_vkek_flag = 0;
|
||||||
|
|
||||||
for (j = 0; j < VKEK_CACHE_NUM; j++)
|
for (j = 0; j < VKEK_CACHE_NUM; j++) {
|
||||||
{
|
|
||||||
//PRINT_INFO("list %d version %s\n", j, h->vkek_list[j].version);
|
//PRINT_INFO("list %d version %s\n", j, h->vkek_list[j].version);
|
||||||
if (!memcmp(h->vkek_list[j].version, h->curr_param.vkek_version, h->curr_param.version_len))
|
if (!memcmp(h->vkek_list[j].version, h->curr_param.vkek_version, h->curr_param.version_len)) {
|
||||||
{
|
|
||||||
h->find_vkek_flag = 1;
|
h->find_vkek_flag = 1;
|
||||||
do_sm4_ecb_decrypt(h->curr_param.evek, h->vek, h->vkek_list[j].vkek);
|
do_sm4_ecb_decrypt(h->curr_param.evek, h->vek, h->vkek_list[j].vkek);
|
||||||
return 0;
|
return 0;
|
||||||
@ -654,64 +605,54 @@ static int dec_vek(struct svac_dec_handle *h)
|
|||||||
}
|
}
|
||||||
|
|
||||||
PRINT_ERR("Not find vkek version %s len %d\n", h->curr_param.vkek_version, h->curr_param.version_len);
|
PRINT_ERR("Not find vkek version %s len %d\n", h->curr_param.vkek_version, h->curr_param.version_len);
|
||||||
for (j = 0; j < VKEK_CACHE_NUM; j++)
|
for (j = 0; j < VKEK_CACHE_NUM; j++) PRINT_INFO("list %d version %s\n", j, h->vkek_list[j].version);
|
||||||
PRINT_INFO("list %d version %s\n", j, h->vkek_list[j].version);
|
|
||||||
|
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int update_new_sec_param(struct svac_dec_handle *h)
|
static int
|
||||||
|
update_new_sec_param(struct svac_dec_handle *h)
|
||||||
{
|
{
|
||||||
memcpy(&h->curr_param, &h->new_param, sizeof(struct sec_param));
|
memcpy(&h->curr_param, &h->new_param, sizeof(struct sec_param));
|
||||||
h->sec_param_update_flag = 0;
|
h->sec_param_update_flag = 0;
|
||||||
|
|
||||||
if (h->curr_param.encrypt_flag)
|
if (h->curr_param.encrypt_flag) {
|
||||||
{
|
if (h->curr_param.vek_flag) dec_vek(h);
|
||||||
if (h->curr_param.vek_flag)
|
|
||||||
dec_vek(h);
|
|
||||||
|
|
||||||
if (h->curr_param.iv_flag)
|
if (h->curr_param.iv_flag) memcpy(h->iv, h->curr_param.iv, 16);
|
||||||
memcpy(h->iv, h->curr_param.iv, 16);
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int calc_nalu_hash(struct svac_dec_handle *h)
|
static int
|
||||||
|
calc_nalu_hash(struct svac_dec_handle *h)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
struct sm3_ctx ctx;
|
struct sm3_ctx ctx;
|
||||||
char hash[32];
|
char hash[32];
|
||||||
struct hash_cache *cache;
|
struct hash_cache *cache;
|
||||||
|
|
||||||
if (!h->curr_param.auth_flag)
|
if (!h->curr_param.auth_flag) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
for (i = 0; i < h->nalu_num; i++)
|
for (i = 0; i < h->nalu_num; i++) {
|
||||||
{
|
if (h->nalu[i].authentication_idc) break;
|
||||||
if (h->nalu[i].authentication_idc)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* û<><C3BB>ǩ<EFBFBD><C7A9><EFBFBD><EFBFBD>nalu */
|
/* û<><C3BB>ǩ<EFBFBD><C7A9><EFBFBD><EFBFBD>nalu */
|
||||||
if (i >= h->nalu_num)
|
if (i >= h->nalu_num) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
memset(hash, 0, sizeof(hash));
|
memset(hash, 0, sizeof(hash));
|
||||||
sm3_init(&ctx);
|
sm3_init(&ctx);
|
||||||
|
|
||||||
for (i = 0; i < h->nalu_num; i++)
|
for (i = 0; i < h->nalu_num; i++) {
|
||||||
{
|
if (!h->nalu[i].authentication_idc) continue;
|
||||||
if (!h->nalu[i].authentication_idc)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
sm3_update(&ctx, h->nalu[i].addr + 4, h->nalu[i].len - 4);
|
sm3_update(&ctx, h->nalu[i].addr + 4, h->nalu[i].len - 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
sm3_final(&ctx, hash);
|
sm3_final(&ctx, hash);
|
||||||
|
|
||||||
if (!h->frame_num)
|
if (!h->frame_num) memcpy(h->idr_hash, hash, 32);
|
||||||
memcpy(h->idr_hash, hash, 32);
|
|
||||||
|
|
||||||
cache = &h->cache[h->cache_idx];
|
cache = &h->cache[h->cache_idx];
|
||||||
memcpy(cache->hash, hash, 32);
|
memcpy(cache->hash, hash, 32);
|
||||||
@ -723,14 +664,18 @@ static int calc_nalu_hash(struct svac_dec_handle *h)
|
|||||||
|
|
||||||
void do_sm4_ofb_encrypt(const char *in_buff, int in_len, char *out_buff, int *out_len, const char *key, const char *iv);
|
void do_sm4_ofb_encrypt(const char *in_buff, int in_len, char *out_buff, int *out_len, const char *key, const char *iv);
|
||||||
|
|
||||||
static int do_dec_and_copy(int encrypt_type, const char *in_buff, int in_len, char *out_buff, int *out_len, const char *key, const char *iv)
|
static int
|
||||||
|
do_dec_and_copy(int encrypt_type,
|
||||||
|
const char *in_buff,
|
||||||
|
int in_len,
|
||||||
|
char *out_buff,
|
||||||
|
int *out_len,
|
||||||
|
const char *key,
|
||||||
|
const char *iv)
|
||||||
{
|
{
|
||||||
if (encrypt_type == SM4_OFB)
|
if (encrypt_type == SM4_OFB) {
|
||||||
{
|
|
||||||
do_sm4_ofb_encrypt(in_buff, in_len, out_buff, out_len, key, iv);
|
do_sm4_ofb_encrypt(in_buff, in_len, out_buff, out_len, key, iv);
|
||||||
}
|
} else if (encrypt_type == SM1_OFB) {
|
||||||
else if (encrypt_type == SM1_OFB)
|
|
||||||
{
|
|
||||||
PRINT_ERR("unsupport SM1 now!\n");
|
PRINT_ERR("unsupport SM1 now!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -738,44 +683,37 @@ static int do_dec_and_copy(int encrypt_type, const char *in_buff, int in_len, ch
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int make_dec_frame(struct svac_dec_handle *h, char *out_buff, int *out_len)
|
static int
|
||||||
|
make_dec_frame(struct svac_dec_handle *h, char *out_buff, int *out_len)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int out_offset = 0;
|
int out_offset = 0;
|
||||||
int out_nalu_len;
|
int out_nalu_len;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
for (i = 0; i < h->nalu_num; i++)
|
for (i = 0; i < h->nalu_num; i++) {
|
||||||
{
|
if (h->nalu[i].type == SVAC_SEC_SLICE || h->nalu[i].type == SVAC_AUTH_SLICE) continue;
|
||||||
if (h->nalu[i].type == SVAC_SEC_SLICE || h->nalu[i].type == SVAC_AUTH_SLICE)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
/* <20><><EFBFBD>naluû<75>м<EFBFBD><D0BC><EFBFBD> <20><>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD> */
|
/* <20><><EFBFBD>naluû<75>м<EFBFBD><D0BC><EFBFBD> <20><>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD> */
|
||||||
if (!h->nalu[i].encryption_idc)
|
if (!h->nalu[i].encryption_idc) {
|
||||||
{
|
|
||||||
memcpy(out_buff + out_offset, h->nalu[i].addr, h->nalu[i].len);
|
memcpy(out_buff + out_offset, h->nalu[i].addr, h->nalu[i].len);
|
||||||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǩ<EFBFBD><C7A9><EFBFBD><EFBFBD>ʶ */
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǩ<EFBFBD><C7A9><EFBFBD><EFBFBD>ʶ */
|
||||||
out_buff[out_offset + 4] &= 0xfc;
|
out_buff[out_offset + 4] &= 0xfc;
|
||||||
out_nalu_len = h->nalu[i].len;
|
out_nalu_len = h->nalu[i].len;
|
||||||
if (h->nalu[i].need_add_racing_code)
|
if (h->nalu[i].need_add_racing_code) do_add_racing_Code(out_buff + out_offset, &out_nalu_len, 5);
|
||||||
do_add_racing_Code(out_buff + out_offset, &out_nalu_len, 5);
|
|
||||||
out_offset += out_nalu_len;
|
out_offset += out_nalu_len;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
int dec_len;
|
int dec_len;
|
||||||
int out_len;
|
int out_len;
|
||||||
|
|
||||||
if (!h->find_vkek_flag)
|
if (!h->find_vkek_flag) {
|
||||||
{
|
|
||||||
PRINT_ERR("Not find vkek [%s]! skip dec frame!\n", h->curr_param.vkek_version);
|
PRINT_ERR("Not find vkek [%s]! skip dec frame!\n", h->curr_param.vkek_version);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//PRINT_INFO("before nalu[i].len %d\n", h->nalu[i].len);
|
//PRINT_INFO("before nalu[i].len %d\n", h->nalu[i].len);
|
||||||
|
|
||||||
if (!h->nalu[i].need_add_racing_code)
|
if (!h->nalu[i].need_add_racing_code) {
|
||||||
{
|
|
||||||
do_remove_racing_code(h->nalu[i].addr, &h->nalu[i].len, 5);
|
do_remove_racing_code(h->nalu[i].addr, &h->nalu[i].len, 5);
|
||||||
h->nalu[i].need_add_racing_code = 1;
|
h->nalu[i].need_add_racing_code = 1;
|
||||||
}
|
}
|
||||||
@ -784,9 +722,9 @@ static int make_dec_frame(struct svac_dec_handle *h, char *out_buff, int *out_le
|
|||||||
dec_len = h->nalu[i].len - 1 - 5;
|
dec_len = h->nalu[i].len - 1 - 5;
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||||
ret = do_dec_and_copy(h->curr_param.encrypt_type, h->nalu[i].addr + 5, dec_len, out_buff + out_offset + 5, &out_len, h->vek, h->iv);
|
ret = do_dec_and_copy(h->curr_param.encrypt_type, h->nalu[i].addr + 5, dec_len, out_buff + out_offset + 5,
|
||||||
if (ret)
|
&out_len, h->vek, h->iv);
|
||||||
return -1;
|
if (ret) return -1;
|
||||||
|
|
||||||
memcpy(out_buff + out_offset + dec_len + 5, h->nalu[i].addr + 5 + dec_len, 1);
|
memcpy(out_buff + out_offset + dec_len + 5, h->nalu[i].addr + 5 + dec_len, 1);
|
||||||
|
|
||||||
@ -806,16 +744,15 @@ static int make_dec_frame(struct svac_dec_handle *h, char *out_buff, int *out_le
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int make_bypass_frame(struct svac_dec_handle *h, char *out_buff, int *out_len)
|
static int
|
||||||
|
make_bypass_frame(struct svac_dec_handle *h, char *out_buff, int *out_len)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
*out_len = 0;
|
*out_len = 0;
|
||||||
|
|
||||||
for (i = 0; i < h->nalu_num; i++)
|
for (i = 0; i < h->nalu_num; i++) {
|
||||||
{
|
if (h->nalu[i].type == SVAC_SEC_SLICE) continue;
|
||||||
if (h->nalu[i].type == SVAC_SEC_SLICE)
|
|
||||||
continue;
|
|
||||||
memcpy(out_buff + *out_len, h->nalu[i].addr, h->nalu[i].len);
|
memcpy(out_buff + *out_len, h->nalu[i].addr, h->nalu[i].len);
|
||||||
*out_len += h->nalu[i].len;
|
*out_len += h->nalu[i].len;
|
||||||
}
|
}
|
||||||
@ -823,37 +760,35 @@ static int make_bypass_frame(struct svac_dec_handle *h, char *out_buff, int *out
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *SvacDecCreate(void)
|
void *
|
||||||
|
SvacDecCreate(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
struct svac_dec_handle *h;
|
struct svac_dec_handle *h;
|
||||||
|
|
||||||
h = (struct svac_dec_handle *)malloc(sizeof(struct svac_dec_handle));
|
h = (struct svac_dec_handle *) malloc(sizeof(struct svac_dec_handle));
|
||||||
if (!h)
|
if (!h) return NULL;
|
||||||
return NULL;
|
|
||||||
|
|
||||||
memset(h, 0, sizeof(struct svac_dec_handle));
|
memset(h, 0, sizeof(struct svac_dec_handle));
|
||||||
h->magic = MAGIC_VALUE;
|
h->magic = MAGIC_VALUE;
|
||||||
|
|
||||||
for (i = 0; i < HASH_CACHE_NUM; i++)
|
for (i = 0; i < HASH_CACHE_NUM; i++) h->cache[i].frame_num = -1;
|
||||||
h->cache[i].frame_num = -1;
|
|
||||||
|
|
||||||
return (void *)h;
|
return (void *) h;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacDecSetVkek(void *handle, char *vkek, char *version, int verion_len)
|
int
|
||||||
|
SvacDecSetVkek(void *handle, char *vkek, char *version, int verion_len)
|
||||||
{
|
{
|
||||||
struct svac_dec_handle *h = (struct svac_dec_handle *)handle;
|
struct svac_dec_handle *h = (struct svac_dec_handle *) handle;
|
||||||
int i = h->vkek_start, j;
|
int i = h->vkek_start, j;
|
||||||
|
|
||||||
CHECK_HANDLE(h);
|
CHECK_HANDLE(h);
|
||||||
|
|
||||||
if (!vkek || !version)
|
if (!vkek || !version) return -1;
|
||||||
return -1;
|
|
||||||
|
|
||||||
for (j = 0; j < VKEK_CACHE_NUM; j++)
|
for (j = 0; j < VKEK_CACHE_NUM; j++)
|
||||||
if (!memcmp(h->vkek_list[j].version, version, verion_len))
|
if (!memcmp(h->vkek_list[j].version, version, verion_len)) {
|
||||||
{
|
|
||||||
memcpy(h->vkek_list[j].vkek, vkek, 16);
|
memcpy(h->vkek_list[j].vkek, vkek, 16);
|
||||||
memset(h->vkek_list[j].version, 0, sizeof(h->vkek_list[j].version));
|
memset(h->vkek_list[j].version, 0, sizeof(h->vkek_list[j].version));
|
||||||
memcpy(h->vkek_list[j].version, version, verion_len);
|
memcpy(h->vkek_list[j].version, version, verion_len);
|
||||||
@ -871,14 +806,14 @@ int SvacDecSetVkek(void *handle, char *vkek, char *version, int verion_len)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacDecSetPubkey(void *handle, char *pubkey)
|
int
|
||||||
|
SvacDecSetPubkey(void *handle, char *pubkey)
|
||||||
{
|
{
|
||||||
struct svac_dec_handle *h = (struct svac_dec_handle *)handle;
|
struct svac_dec_handle *h = (struct svac_dec_handle *) handle;
|
||||||
|
|
||||||
CHECK_HANDLE(h);
|
CHECK_HANDLE(h);
|
||||||
|
|
||||||
if (!pubkey)
|
if (!pubkey) return -1;
|
||||||
return -1;
|
|
||||||
|
|
||||||
/* save sm2 pubkey*/
|
/* save sm2 pubkey*/
|
||||||
memcpy(h->sm2_pubkey, pubkey, 64);
|
memcpy(h->sm2_pubkey, pubkey, 64);
|
||||||
@ -888,9 +823,10 @@ int SvacDecSetPubkey(void *handle, char *pubkey)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacDecParamCallBack(void *handle, PFSECURITYPARAMCB callback, void *context)
|
int
|
||||||
|
SvacDecParamCallBack(void *handle, PFSECURITYPARAMCB callback, void *context)
|
||||||
{
|
{
|
||||||
struct svac_dec_handle *h = (struct svac_dec_handle *)handle;
|
struct svac_dec_handle *h = (struct svac_dec_handle *) handle;
|
||||||
|
|
||||||
CHECK_HANDLE(h);
|
CHECK_HANDLE(h);
|
||||||
|
|
||||||
@ -900,14 +836,14 @@ int SvacDecParamCallBack(void *handle, PFSECURITYPARAMCB callback, void *context
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int svac_dec_process(void *handle, char *in_buff, int in_len, char *out_buff, int *out_len, struct auth_info *info)
|
int
|
||||||
|
svac_dec_process(void *handle, char *in_buff, int in_len, char *out_buff, int *out_len, struct auth_info *info)
|
||||||
{
|
{
|
||||||
struct svac_dec_handle *h = (struct svac_dec_handle *)handle;
|
struct svac_dec_handle *h = (struct svac_dec_handle *) handle;
|
||||||
|
|
||||||
CHECK_HANDLE(h);
|
CHECK_HANDLE(h);
|
||||||
|
|
||||||
if (!in_buff || !out_buff || !out_len)
|
if (!in_buff || !out_buff || !out_len) return -1;
|
||||||
return -1;
|
|
||||||
|
|
||||||
h->nalu_num = 0;
|
h->nalu_num = 0;
|
||||||
h->idr_flag = 0;
|
h->idr_flag = 0;
|
||||||
@ -917,28 +853,24 @@ int svac_dec_process(void *handle, char *in_buff, int in_len, char *out_buff, in
|
|||||||
svac_parse_nalu(h, in_buff, in_len);
|
svac_parse_nalu(h, in_buff, in_len);
|
||||||
//PRINT_INFO("find nalu_num %d\n", h->nalu_num);
|
//PRINT_INFO("find nalu_num %d\n", h->nalu_num);
|
||||||
|
|
||||||
if (!h->nalu_num)
|
if (!h->nalu_num) {
|
||||||
{
|
|
||||||
PRINT_ERR("Not find nalu start code!\n");
|
PRINT_ERR("Not find nalu start code!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>NALU */
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>NALU */
|
||||||
if (h->sec_nalu_flag && parse_security_nalu(h))
|
if (h->sec_nalu_flag && parse_security_nalu(h)) {
|
||||||
{
|
|
||||||
PRINT_ERR("parse security nalu failed!\n");
|
PRINT_ERR("parse security nalu failed!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (h->idr_flag)
|
if (h->idr_flag) {
|
||||||
{
|
|
||||||
struct sec_info param;
|
struct sec_info param;
|
||||||
|
|
||||||
memset(¶m, 0, sizeof(param));
|
memset(¶m, 0, sizeof(param));
|
||||||
|
|
||||||
/* <20>µ<EFBFBD>GOP<4F><50> <20><><EFBFBD>°<EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD> & vkek */
|
/* <20>µ<EFBFBD>GOP<4F><50> <20><><EFBFBD>°<EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD> & vkek */
|
||||||
if (h->sec_param_update_flag)
|
if (h->sec_param_update_flag) {
|
||||||
{
|
|
||||||
update_new_sec_param(h);
|
update_new_sec_param(h);
|
||||||
param.encrypt_flag = h->curr_param.encrypt_flag;
|
param.encrypt_flag = h->curr_param.encrypt_flag;
|
||||||
param.auth_flag = h->curr_param.auth_flag;
|
param.auth_flag = h->curr_param.auth_flag;
|
||||||
@ -946,22 +878,18 @@ int svac_dec_process(void *handle, char *in_buff, int in_len, char *out_buff, in
|
|||||||
param.hash_period = h->curr_param.hash_period;
|
param.hash_period = h->curr_param.hash_period;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (h->callback)
|
if (h->callback) h->callback(¶m, h->context);
|
||||||
h->callback(¶m, h->context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD><EFBFBD>a<EFBFBD><61><EFBFBD><EFBFBD><EFBFBD><EFBFBD> bypass */
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>a<EFBFBD><61><EFBFBD><EFBFBD><EFBFBD><EFBFBD> bypass */
|
||||||
if (!h->curr_param.auth_flag && !h->curr_param.encrypt_flag)
|
if (!h->curr_param.auth_flag && !h->curr_param.encrypt_flag) return make_bypass_frame(h, out_buff, out_len);
|
||||||
return make_bypass_frame(h, out_buff, out_len);
|
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD>hash */
|
/* <20><><EFBFBD><EFBFBD>hash */
|
||||||
calc_nalu_hash(h);
|
calc_nalu_hash(h);
|
||||||
|
|
||||||
if (info)
|
if (info) info->do_auth = 0;
|
||||||
info->do_auth = 0;
|
|
||||||
|
|
||||||
if (h->auth_nalu_flag && info)
|
if (h->auth_nalu_flag && info) parse_svac_auth_nal(h, info);
|
||||||
parse_svac_auth_nal(h, info);
|
|
||||||
|
|
||||||
/* ƴ<><C6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/* ƴ<><C6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||||
make_dec_frame(h, out_buff, out_len);
|
make_dec_frame(h, out_buff, out_len);
|
||||||
@ -969,14 +897,16 @@ int svac_dec_process(void *handle, char *in_buff, int in_len, char *out_buff, in
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacDecProcess(void *handle, char *in_buff, int in_len, char *out_buff, int *out_len, struct auth_info *info)
|
int
|
||||||
|
SvacDecProcess(void *handle, char *in_buff, int in_len, char *out_buff, int *out_len, struct auth_info *info)
|
||||||
{
|
{
|
||||||
return svac_dec_process(handle, in_buff, in_len, out_buff, out_len, info);
|
return svac_dec_process(handle, in_buff, in_len, out_buff, out_len, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacDecRelease(void *handle)
|
int
|
||||||
|
SvacDecRelease(void *handle)
|
||||||
{
|
{
|
||||||
struct svac_dec_handle *h = (struct svac_dec_handle *)handle;
|
struct svac_dec_handle *h = (struct svac_dec_handle *) handle;
|
||||||
|
|
||||||
CHECK_HANDLE(h);
|
CHECK_HANDLE(h);
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "big.h"
|
#include "big.h"
|
||||||
#include "ecc.h"
|
#include "ecc.h"
|
||||||
@ -11,38 +11,23 @@
|
|||||||
struct ecc_curve sm2_curve = {
|
struct ecc_curve sm2_curve = {
|
||||||
ECC_MAX_DIGITS,
|
ECC_MAX_DIGITS,
|
||||||
{
|
{
|
||||||
|
{0x715A4589334C74C7ull, 0x8FE30BBFF2660BE1ull, 0x5F9904466A39C994ull, 0x32C4AE2C1F198119ull},
|
||||||
|
{0x02DF32E52139F0A0ull, 0xD0A9877CC62A4740ull, 0x59BDCEE36B692153ull, 0xBC3736A2F4F6779Cull},
|
||||||
|
},
|
||||||
|
{0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull, 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull},
|
||||||
|
{0x53BBF40939D54123ull, 0x7203DF6B21C6052Bull, 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull},
|
||||||
{
|
{
|
||||||
0x715A4589334C74C7ull, 0x8FE30BBFF2660BE1ull,
|
0x0000000000000001ull,
|
||||||
0x5F9904466A39C994ull, 0x32C4AE2C1F198119ull
|
0x0000000000000000ull,
|
||||||
},
|
0x0000000000000000ull,
|
||||||
{
|
0x0000000000000000ull,
|
||||||
0x02DF32E52139F0A0ull, 0xD0A9877CC62A4740ull,
|
|
||||||
0x59BDCEE36B692153ull, 0xBC3736A2F4F6779Cull
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull,
|
|
||||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0x53BBF40939D54123ull, 0x7203DF6B21C6052Bull,
|
|
||||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0x0000000000000001ull, 0x0000000000000000ull,
|
|
||||||
0x0000000000000000ull, 0x0000000000000000ull,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0xFFFFFFFFFFFFFFFCull, 0xFFFFFFFF00000000ull,
|
|
||||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0xDDBCBD414D940E93ull, 0xF39789F515AB8F92ull,
|
|
||||||
0x4D5A9E4BCF6509A7ull, 0x28E9FA9E9D9F5E34ull
|
|
||||||
},
|
},
|
||||||
|
{0xFFFFFFFFFFFFFFFCull, 0xFFFFFFFF00000000ull, 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull},
|
||||||
|
{0xDDBCBD414D940E93ull, 0xF39789F515AB8F92ull, 0x4D5A9E4BCF6509A7ull, 0x28E9FA9E9D9F5E34ull},
|
||||||
};
|
};
|
||||||
|
|
||||||
static void sm2_w(u64 *result, u64 *x)
|
static void
|
||||||
|
sm2_w(u64 *result, u64 *x)
|
||||||
{
|
{
|
||||||
result[0] = x[0];
|
result[0] = x[0];
|
||||||
result[1] = x[1];
|
result[1] = x[1];
|
||||||
@ -51,15 +36,16 @@ static void sm2_w(u64 *result, u64 *x)
|
|||||||
result[3] = 0;
|
result[3] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sm3_kdf(u8 *Z ,u32 zlen, u8 *K, u32 klen)
|
static void
|
||||||
|
sm3_kdf(u8 *Z, u32 zlen, u8 *K, u32 klen)
|
||||||
{
|
{
|
||||||
u32 ct = 0x00000001;
|
u32 ct = 0x00000001;
|
||||||
u8 ct_char[32];
|
u8 ct_char[32];
|
||||||
u8 *hash = K ;
|
u8 *hash = K;
|
||||||
u32 i, t;
|
u32 i, t;
|
||||||
struct sm3_ctx md[1];
|
struct sm3_ctx md[1];
|
||||||
|
|
||||||
t = klen/ECC_NUMWORD;
|
t = klen / ECC_NUMWORD;
|
||||||
//s4: K=Ha1||Ha2||...
|
//s4: K=Ha1||Ha2||...
|
||||||
for (i = 0; i < t; i++) {
|
for (i = 0; i < t; i++) {
|
||||||
//s2: Hai=Hv(Z||ct)
|
//s2: Hai=Hv(Z||ct)
|
||||||
@ -72,7 +58,7 @@ static void sm3_kdf(u8 *Z ,u32 zlen, u8 *K, u32 klen)
|
|||||||
ct++;
|
ct++;
|
||||||
}
|
}
|
||||||
|
|
||||||
t = klen%ECC_NUMBITS;
|
t = klen % ECC_NUMBITS;
|
||||||
if (t) {
|
if (t) {
|
||||||
sm3_init(md);
|
sm3_init(md);
|
||||||
sm3_update(md, Z, zlen);
|
sm3_update(md, Z, zlen);
|
||||||
@ -83,7 +69,8 @@ static void sm3_kdf(u8 *Z ,u32 zlen, u8 *K, u32 klen)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sm3_z(u8 *id, u32 idlen, ecc_point *pub, u8 *hash)
|
static void
|
||||||
|
sm3_z(u8 *id, u32 idlen, ecc_point *pub, u8 *hash)
|
||||||
{
|
{
|
||||||
u8 a[ECC_NUMWORD];
|
u8 a[ECC_NUMWORD];
|
||||||
u8 b[ECC_NUMWORD];
|
u8 b[ECC_NUMWORD];
|
||||||
@ -92,12 +79,12 @@ static void sm3_z(u8 *id, u32 idlen, ecc_point *pub, u8 *hash)
|
|||||||
u8 idlen_char[2];
|
u8 idlen_char[2];
|
||||||
struct sm3_ctx md[1];
|
struct sm3_ctx md[1];
|
||||||
|
|
||||||
put_unaligned_be16(idlen<<3, idlen_char);
|
put_unaligned_be16(idlen << 3, idlen_char);
|
||||||
|
|
||||||
ecc_bytes2native((u64*)a, sm2_curve.a, sm2_curve.ndigits);
|
ecc_bytes2native((u64 *) a, sm2_curve.a, sm2_curve.ndigits);
|
||||||
ecc_bytes2native((u64*)b, sm2_curve.b, sm2_curve.ndigits);
|
ecc_bytes2native((u64 *) b, sm2_curve.b, sm2_curve.ndigits);
|
||||||
ecc_bytes2native((u64*)x, sm2_curve.g.x, sm2_curve.ndigits);
|
ecc_bytes2native((u64 *) x, sm2_curve.g.x, sm2_curve.ndigits);
|
||||||
ecc_bytes2native((u64*)y, sm2_curve.g.y, sm2_curve.ndigits);
|
ecc_bytes2native((u64 *) y, sm2_curve.g.y, sm2_curve.ndigits);
|
||||||
|
|
||||||
sm3_init(md);
|
sm3_init(md);
|
||||||
sm3_update(md, idlen_char, 2);
|
sm3_update(md, idlen_char, 2);
|
||||||
@ -106,21 +93,21 @@ static void sm3_z(u8 *id, u32 idlen, ecc_point *pub, u8 *hash)
|
|||||||
sm3_update(md, b, ECC_NUMWORD);
|
sm3_update(md, b, ECC_NUMWORD);
|
||||||
sm3_update(md, x, ECC_NUMWORD);
|
sm3_update(md, x, ECC_NUMWORD);
|
||||||
sm3_update(md, y, ECC_NUMWORD);
|
sm3_update(md, y, ECC_NUMWORD);
|
||||||
sm3_update(md, (u8*)pub->x, ECC_NUMWORD);
|
sm3_update(md, (u8 *) pub->x, ECC_NUMWORD);
|
||||||
sm3_update(md, (u8*)pub->y, ECC_NUMWORD);
|
sm3_update(md, (u8 *) pub->y, ECC_NUMWORD);
|
||||||
sm3_final(md, hash);
|
sm3_final(md, hash);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sm2_valid_public_key(ecc_point *publicKey)
|
static int
|
||||||
|
sm2_valid_public_key(ecc_point *publicKey)
|
||||||
{
|
{
|
||||||
u64 na[ECC_MAX_DIGITS] = {3}; /* a mod p = (-3) mod p */
|
u64 na[ECC_MAX_DIGITS] = {3}; /* a mod p = (-3) mod p */
|
||||||
u64 tmp1[ECC_MAX_DIGITS];
|
u64 tmp1[ECC_MAX_DIGITS];
|
||||||
u64 tmp2[ECC_MAX_DIGITS];
|
u64 tmp2[ECC_MAX_DIGITS];
|
||||||
|
|
||||||
if (ecc_point_is_zero(&sm2_curve, publicKey))
|
if (ecc_point_is_zero(&sm2_curve, publicKey)) return 1;
|
||||||
return 1;
|
|
||||||
|
|
||||||
if (vli_cmp(sm2_curve.p, publicKey->x, sm2_curve.ndigits) != 1
|
if (vli_cmp(sm2_curve.p, publicKey->x, sm2_curve.ndigits) != 1
|
||||||
|| vli_cmp(sm2_curve.p, publicKey->y, sm2_curve.ndigits) != 1)
|
|| vli_cmp(sm2_curve.p, publicKey->y, sm2_curve.ndigits) != 1)
|
||||||
@ -138,35 +125,34 @@ static int sm2_valid_public_key(ecc_point *publicKey)
|
|||||||
vli_mod_add(tmp2, tmp2, sm2_curve.b, sm2_curve.p, sm2_curve.ndigits);
|
vli_mod_add(tmp2, tmp2, sm2_curve.b, sm2_curve.p, sm2_curve.ndigits);
|
||||||
|
|
||||||
/* Make sure that y^2 == x^3 + ax + b */
|
/* Make sure that y^2 == x^3 + ax + b */
|
||||||
if (vli_cmp(tmp1, tmp2, sm2_curve.ndigits) != 0)
|
if (vli_cmp(tmp1, tmp2, sm2_curve.ndigits) != 0) return 1;
|
||||||
return 1;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm2_make_prikey(u8 *prikey)
|
int
|
||||||
|
sm2_make_prikey(u8 *prikey)
|
||||||
{
|
{
|
||||||
ecc_point pub[1];
|
ecc_point pub[1];
|
||||||
u64 pri[ECC_MAX_DIGITS];
|
u64 pri[ECC_MAX_DIGITS];
|
||||||
int i = 10;
|
int i = 10;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
vli_get_random((u8*)pri, ECC_NUMWORD);
|
vli_get_random((u8 *) pri, ECC_NUMWORD);
|
||||||
if(vli_cmp(sm2_curve.n, pri, sm2_curve.ndigits) != 1) {
|
if (vli_cmp(sm2_curve.n, pri, sm2_curve.ndigits) != 1) { vli_sub(pri, pri, sm2_curve.n, sm2_curve.ndigits); }
|
||||||
vli_sub(pri, pri, sm2_curve.n, sm2_curve.ndigits);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The private key cannot be 0 (mod p). */
|
/* The private key cannot be 0 (mod p). */
|
||||||
if(!vli_is_zero(pri, sm2_curve.ndigits)) {
|
if (!vli_is_zero(pri, sm2_curve.ndigits)) {
|
||||||
ecc_native2bytes(prikey, pri, sm2_curve.ndigits);
|
ecc_native2bytes(prikey, pri, sm2_curve.ndigits);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} while(i--);
|
} while (i--);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm2_make_pubkey(u8 *prikey, ecc_point *pubkey)
|
int
|
||||||
|
sm2_make_pubkey(u8 *prikey, ecc_point *pubkey)
|
||||||
{
|
{
|
||||||
ecc_point pub[1];
|
ecc_point pub[1];
|
||||||
u64 pri[ECC_MAX_DIGITS];
|
u64 pri[ECC_MAX_DIGITS];
|
||||||
@ -179,14 +165,16 @@ int sm2_make_pubkey(u8 *prikey, ecc_point *pubkey)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm2_make_keypair(u8 *prikey, ecc_point *pubkey)
|
int
|
||||||
|
sm2_make_keypair(u8 *prikey, ecc_point *pubkey)
|
||||||
{
|
{
|
||||||
sm2_make_prikey(prikey);
|
sm2_make_prikey(prikey);
|
||||||
sm2_make_pubkey(prikey, pubkey);
|
sm2_make_pubkey(prikey, pubkey);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm2_point_mult(ecc_point *G, u8 *k, ecc_point *P)
|
int
|
||||||
|
sm2_point_mult(ecc_point *G, u8 *k, ecc_point *P)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
@ -206,7 +194,8 @@ int sm2_point_mult(ecc_point *G, u8 *k, ecc_point *P)
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm2_sign(u8 *r_, u8 *s_, u8 *prikey, u8 *hash_)
|
int
|
||||||
|
sm2_sign(u8 *r_, u8 *s_, u8 *prikey, u8 *hash_)
|
||||||
{
|
{
|
||||||
u64 k[ECC_MAX_DIGITS];
|
u64 k[ECC_MAX_DIGITS];
|
||||||
u64 one[ECC_MAX_DIGITS] = {1};
|
u64 one[ECC_MAX_DIGITS] = {1};
|
||||||
@ -221,25 +210,21 @@ int sm2_sign(u8 *r_, u8 *s_, u8 *prikey, u8 *hash_)
|
|||||||
ecc_bytes2native(pri, prikey, sm2_curve.ndigits);
|
ecc_bytes2native(pri, prikey, sm2_curve.ndigits);
|
||||||
ecc_bytes2native(hash, hash_, sm2_curve.ndigits);
|
ecc_bytes2native(hash, hash_, sm2_curve.ndigits);
|
||||||
|
|
||||||
vli_get_random((u8*)random, ECC_NUMWORD);
|
vli_get_random((u8 *) random, ECC_NUMWORD);
|
||||||
if (vli_is_zero(random, sm2_curve.ndigits)) {
|
if (vli_is_zero(random, sm2_curve.ndigits)) {
|
||||||
/* The random number must not be 0. */
|
/* The random number must not be 0. */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
vli_set(k, random, sm2_curve.ndigits);
|
vli_set(k, random, sm2_curve.ndigits);
|
||||||
if (vli_cmp(sm2_curve.n, k, sm2_curve.ndigits) != 1) {
|
if (vli_cmp(sm2_curve.n, k, sm2_curve.ndigits) != 1) { vli_sub(k, k, sm2_curve.n, sm2_curve.ndigits); }
|
||||||
vli_sub(k, k, sm2_curve.n, sm2_curve.ndigits);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* tmp = k * G */
|
/* tmp = k * G */
|
||||||
ecc_point_mult(&sm2_curve, &p, &sm2_curve.g, k, NULL);
|
ecc_point_mult(&sm2_curve, &p, &sm2_curve.g, k, NULL);
|
||||||
|
|
||||||
/* r = x1 + e (mod n) */
|
/* r = x1 + e (mod n) */
|
||||||
vli_mod_add(r, p.x, hash, sm2_curve.n, sm2_curve.ndigits);
|
vli_mod_add(r, p.x, hash, sm2_curve.n, sm2_curve.ndigits);
|
||||||
if (vli_cmp(sm2_curve.n, r, sm2_curve.ndigits) != 1) {
|
if (vli_cmp(sm2_curve.n, r, sm2_curve.ndigits) != 1) { vli_sub(r, r, sm2_curve.n, sm2_curve.ndigits); }
|
||||||
vli_sub(r, r, sm2_curve.n, sm2_curve.ndigits);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (vli_is_zero(r, sm2_curve.ndigits)) {
|
if (vli_is_zero(r, sm2_curve.ndigits)) {
|
||||||
/* If r == 0, fail (need a different random number). */
|
/* If r == 0, fail (need a different random number). */
|
||||||
@ -263,7 +248,8 @@ int sm2_sign(u8 *r_, u8 *s_, u8 *prikey, u8 *hash_)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sm2_verify(ecc_point *pubkey, u8 *hash_, u8 *r_, u8 *s_)
|
static int
|
||||||
|
sm2_verify(ecc_point *pubkey, u8 *hash_, u8 *r_, u8 *s_)
|
||||||
{
|
{
|
||||||
ecc_point result;
|
ecc_point result;
|
||||||
ecc_point pub[1];
|
ecc_point pub[1];
|
||||||
@ -283,22 +269,20 @@ static int sm2_verify(ecc_point *pubkey, u8 *hash_, u8 *r_, u8 *s_)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vli_cmp(sm2_curve.n, r, sm2_curve.ndigits) != 1
|
if (vli_cmp(sm2_curve.n, r, sm2_curve.ndigits) != 1 || vli_cmp(sm2_curve.n, s, sm2_curve.ndigits) != 1) {
|
||||||
|| vli_cmp(sm2_curve.n, s, sm2_curve.ndigits) != 1) {
|
|
||||||
/* r, s must be < n. */
|
/* r, s must be < n. */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
vli_mod_add(t, r, s, sm2_curve.n, sm2_curve.ndigits); // r + s
|
vli_mod_add(t, r, s, sm2_curve.n, sm2_curve.ndigits);// r + s
|
||||||
if (t == 0)
|
if (t == 0) return -1;
|
||||||
return -1;
|
|
||||||
|
|
||||||
ecc_point_mult2(&sm2_curve, &result, &sm2_curve.g, pub, s, t);
|
ecc_point_mult2(&sm2_curve, &result, &sm2_curve.g, pub, s, t);
|
||||||
|
|
||||||
/* v = x1 + e (mod n) */
|
/* v = x1 + e (mod n) */
|
||||||
vli_mod_add(result.x, result.x, hash, sm2_curve.n, sm2_curve.ndigits);
|
vli_mod_add(result.x, result.x, hash, sm2_curve.n, sm2_curve.ndigits);
|
||||||
|
|
||||||
if(vli_cmp(sm2_curve.n, result.x, sm2_curve.ndigits) != 1) {
|
if (vli_cmp(sm2_curve.n, result.x, sm2_curve.ndigits) != 1) {
|
||||||
vli_sub(result.x, result.x, sm2_curve.n, sm2_curve.ndigits);
|
vli_sub(result.x, result.x, sm2_curve.n, sm2_curve.ndigits);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -306,8 +290,13 @@ static int sm2_verify(ecc_point *pubkey, u8 *hash_, u8 *r_, u8 *s_)
|
|||||||
return vli_cmp(result.x, r, sm2_curve.ndigits);
|
return vli_cmp(result.x, r, sm2_curve.ndigits);
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm2_shared_point(u8* selfPriKey, u8* selfTempPriKey, ecc_point* selfTempPubKey,
|
int
|
||||||
ecc_point *otherPubKey, ecc_point* otherTempPubKey, ecc_point *key)
|
sm2_shared_point(u8 *selfPriKey,
|
||||||
|
u8 *selfTempPriKey,
|
||||||
|
ecc_point *selfTempPubKey,
|
||||||
|
ecc_point *otherPubKey,
|
||||||
|
ecc_point *otherTempPubKey,
|
||||||
|
ecc_point *key)
|
||||||
{
|
{
|
||||||
ecc_point selfTempPub;
|
ecc_point selfTempPub;
|
||||||
ecc_point otherTempPub;
|
ecc_point otherTempPub;
|
||||||
@ -335,8 +324,7 @@ int sm2_shared_point(u8* selfPriKey, u8* selfTempPriKey, ecc_point* selfTempPub
|
|||||||
vli_mod_mult(temp1, selfTempPri, temp1, sm2_curve.n, sm2_curve.ndigits);
|
vli_mod_mult(temp1, selfTempPri, temp1, sm2_curve.n, sm2_curve.ndigits);
|
||||||
vli_mod_add(tA, selfPri, temp1, sm2_curve.n, sm2_curve.ndigits);
|
vli_mod_add(tA, selfPri, temp1, sm2_curve.n, sm2_curve.ndigits);
|
||||||
/***********x2_=2^w+x2 & (2^w-1)*************/
|
/***********x2_=2^w+x2 & (2^w-1)*************/
|
||||||
if(sm2_valid_public_key(&otherTempPub) != 0)
|
if (sm2_valid_public_key(&otherTempPub) != 0) return -1;
|
||||||
return -1;
|
|
||||||
sm2_w(temp2, otherTempPub.x);
|
sm2_w(temp2, otherTempPub.x);
|
||||||
/**************U=[h*tA](PB+[x2_]RB)**********/
|
/**************U=[h*tA](PB+[x2_]RB)**********/
|
||||||
/* U=[x2_]RB */
|
/* U=[x2_]RB */
|
||||||
@ -345,26 +333,28 @@ int sm2_shared_point(u8* selfPriKey, u8* selfTempPriKey, ecc_point* selfTempPub
|
|||||||
ecc_point_add(&sm2_curve, U, &otherPub, U);
|
ecc_point_add(&sm2_curve, U, &otherPub, U);
|
||||||
/*tA=tA*h */
|
/*tA=tA*h */
|
||||||
vli_mod_mult(tA, tA, sm2_curve.h, sm2_curve.n, sm2_curve.ndigits);
|
vli_mod_mult(tA, tA, sm2_curve.h, sm2_curve.n, sm2_curve.ndigits);
|
||||||
ecc_point_mult(&sm2_curve, U, U,tA, NULL);
|
ecc_point_mult(&sm2_curve, U, U, tA, NULL);
|
||||||
|
|
||||||
ecc_native2bytes(key->x, U->x, sm2_curve.ndigits);
|
ecc_native2bytes(key->x, U->x, sm2_curve.ndigits);
|
||||||
ecc_native2bytes(key->y, U->y, sm2_curve.ndigits);
|
ecc_native2bytes(key->y, U->y, sm2_curve.ndigits);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm2_shared_key(ecc_point *point, u8 *ZA, u8 *ZB, u32 keyLen, u8 *key)
|
int
|
||||||
|
sm2_shared_key(ecc_point *point, u8 *ZA, u8 *ZB, u32 keyLen, u8 *key)
|
||||||
{
|
{
|
||||||
u8 Z[ECC_NUMWORD*4];
|
u8 Z[ECC_NUMWORD * 4];
|
||||||
memcpy(Z, point->x, ECC_NUMWORD);
|
memcpy(Z, point->x, ECC_NUMWORD);
|
||||||
memcpy(Z + ECC_NUMWORD, point->y, ECC_NUMWORD);
|
memcpy(Z + ECC_NUMWORD, point->y, ECC_NUMWORD);
|
||||||
memcpy(Z + ECC_NUMWORD*2, ZA, ECC_NUMWORD);
|
memcpy(Z + ECC_NUMWORD * 2, ZA, ECC_NUMWORD);
|
||||||
memcpy(Z + ECC_NUMWORD*3, ZB, ECC_NUMWORD);
|
memcpy(Z + ECC_NUMWORD * 3, ZB, ECC_NUMWORD);
|
||||||
sm3_kdf(Z, ECC_NUMWORD*4, key, keyLen);
|
sm3_kdf(Z, ECC_NUMWORD * 4, key, keyLen);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****hash = Hash(Ux||ZA||ZB||x1||y1||x2||y2)****/
|
/****hash = Hash(Ux||ZA||ZB||x1||y1||x2||y2)****/
|
||||||
int ECC_Key_ex_hash1(u8* x, ecc_point *RA, ecc_point* RB, u8 ZA[],u8 ZB[],u8 *hash)
|
int
|
||||||
|
ECC_Key_ex_hash1(u8 *x, ecc_point *RA, ecc_point *RB, u8 ZA[], u8 ZB[], u8 *hash)
|
||||||
{
|
{
|
||||||
struct sm3_ctx md[1];
|
struct sm3_ctx md[1];
|
||||||
|
|
||||||
@ -372,39 +362,52 @@ int ECC_Key_ex_hash1(u8* x, ecc_point *RA, ecc_point* RB, u8 ZA[],u8 ZB[],u8 *ha
|
|||||||
sm3_update(md, x, ECC_NUMWORD);
|
sm3_update(md, x, ECC_NUMWORD);
|
||||||
sm3_update(md, ZA, ECC_NUMWORD);
|
sm3_update(md, ZA, ECC_NUMWORD);
|
||||||
sm3_update(md, ZB, ECC_NUMWORD);
|
sm3_update(md, ZB, ECC_NUMWORD);
|
||||||
sm3_update(md, (u8*)RA->x, ECC_NUMWORD);
|
sm3_update(md, (u8 *) RA->x, ECC_NUMWORD);
|
||||||
sm3_update(md, (u8*)RA->y, ECC_NUMWORD);
|
sm3_update(md, (u8 *) RA->y, ECC_NUMWORD);
|
||||||
sm3_update(md, (u8*)RB->x, ECC_NUMWORD);
|
sm3_update(md, (u8 *) RB->x, ECC_NUMWORD);
|
||||||
sm3_update(md, (u8*)RB->y, ECC_NUMWORD);
|
sm3_update(md, (u8 *) RB->y, ECC_NUMWORD);
|
||||||
sm3_final(md, (u8*)hash);
|
sm3_final(md, (u8 *) hash);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****SA = Hash(temp||Uy||Hash)****/
|
/****SA = Hash(temp||Uy||Hash)****/
|
||||||
int ECC_Key_ex_hash2(u8 temp, u8* y,u8 *hash, u8* SA)
|
int
|
||||||
|
ECC_Key_ex_hash2(u8 temp, u8 *y, u8 *hash, u8 *SA)
|
||||||
{
|
{
|
||||||
struct sm3_ctx md[1];
|
struct sm3_ctx md[1];
|
||||||
|
|
||||||
sm3_init(md);
|
sm3_init(md);
|
||||||
sm3_update(md, &temp,1);
|
sm3_update(md, &temp, 1);
|
||||||
sm3_update(md, y,ECC_NUMWORD);
|
sm3_update(md, y, ECC_NUMWORD);
|
||||||
sm3_update(md, hash,ECC_NUMWORD);
|
sm3_update(md, hash, ECC_NUMWORD);
|
||||||
sm3_final(md, SA);
|
sm3_final(md, SA);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ECC_KeyEx_Init_I(u8 *pri, ecc_point *pub)
|
int
|
||||||
|
ECC_KeyEx_Init_I(u8 *pri, ecc_point *pub)
|
||||||
{
|
{
|
||||||
return sm2_make_pubkey(pri, pub);
|
return sm2_make_pubkey(pri, pub);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ECC_KeyEx_Re_I(u8 *rb, u8 *dB, ecc_point *RA, ecc_point *PA, u8* ZA, u8 *ZB, u8 *K, u32 klen, ecc_point *RB, ecc_point *V, u8* SB)
|
int
|
||||||
|
ECC_KeyEx_Re_I(u8 *rb,
|
||||||
|
u8 *dB,
|
||||||
|
ecc_point *RA,
|
||||||
|
ecc_point *PA,
|
||||||
|
u8 *ZA,
|
||||||
|
u8 *ZB,
|
||||||
|
u8 *K,
|
||||||
|
u32 klen,
|
||||||
|
ecc_point *RB,
|
||||||
|
ecc_point *V,
|
||||||
|
u8 *SB)
|
||||||
{
|
{
|
||||||
u8 Z[ECC_NUMWORD*2 + ECC_NUMBITS/4]={0};
|
u8 Z[ECC_NUMWORD * 2 + ECC_NUMBITS / 4] = {0};
|
||||||
u8 hash[ECC_NUMWORD],S1[ECC_NUMWORD];
|
u8 hash[ECC_NUMWORD], S1[ECC_NUMWORD];
|
||||||
u8 temp=0x02;
|
u8 temp = 0x02;
|
||||||
|
|
||||||
//--------B2: RB=[rb]G=(x2,y2)--------
|
//--------B2: RB=[rb]G=(x2,y2)--------
|
||||||
sm2_make_pubkey(rb, RB);
|
sm2_make_pubkey(rb, RB);
|
||||||
@ -412,62 +415,72 @@ int ECC_KeyEx_Re_I(u8 *rb, u8 *dB, ecc_point *RA, ecc_point *PA, u8* ZA, u8 *ZB,
|
|||||||
sm2_shared_point(dB, rb, RB, PA, RA, V);
|
sm2_shared_point(dB, rb, RB, PA, RA, V);
|
||||||
//------------B7:KB=KDF(VX,VY,ZA,ZB,KLEN)----------
|
//------------B7:KB=KDF(VX,VY,ZA,ZB,KLEN)----------
|
||||||
memcpy(Z, V->x, ECC_NUMWORD);
|
memcpy(Z, V->x, ECC_NUMWORD);
|
||||||
memcpy(Z+ECC_NUMWORD, (u8*)V->y, ECC_NUMWORD);
|
memcpy(Z + ECC_NUMWORD, (u8 *) V->y, ECC_NUMWORD);
|
||||||
memcpy(Z+ECC_NUMWORD*2, ZA,ECC_NUMWORD);
|
memcpy(Z + ECC_NUMWORD * 2, ZA, ECC_NUMWORD);
|
||||||
memcpy(Z+ECC_NUMWORD*3, ZB,ECC_NUMWORD);
|
memcpy(Z + ECC_NUMWORD * 3, ZB, ECC_NUMWORD);
|
||||||
sm3_kdf(Z,ECC_NUMWORD*4, K, klen);
|
sm3_kdf(Z, ECC_NUMWORD * 4, K, klen);
|
||||||
//---------------B8:(optional) SB=hash(0x02||Vy||HASH(Vx||ZA||ZB||x1||y1||x2||y2)-------------
|
//---------------B8:(optional) SB=hash(0x02||Vy||HASH(Vx||ZA||ZB||x1||y1||x2||y2)-------------
|
||||||
ECC_Key_ex_hash1((u8*)V->x, RA, RB, ZA, ZB, hash);
|
ECC_Key_ex_hash1((u8 *) V->x, RA, RB, ZA, ZB, hash);
|
||||||
ECC_Key_ex_hash2(temp, (u8*)V->y, hash, SB);
|
ECC_Key_ex_hash2(temp, (u8 *) V->y, hash, SB);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ECC_KeyEx_Init_II(u8* ra, u8* dA, ecc_point* RA, ecc_point* RB, ecc_point* PB, u8
|
int
|
||||||
ZA[],u8 ZB[],u8 SB[],u8 K[], u32 klen,u8 SA[])
|
ECC_KeyEx_Init_II(u8 *ra,
|
||||||
|
u8 *dA,
|
||||||
|
ecc_point *RA,
|
||||||
|
ecc_point *RB,
|
||||||
|
ecc_point *PB,
|
||||||
|
u8 ZA[],
|
||||||
|
u8 ZB[],
|
||||||
|
u8 SB[],
|
||||||
|
u8 K[],
|
||||||
|
u32 klen,
|
||||||
|
u8 SA[])
|
||||||
{
|
{
|
||||||
u8 Z[ECC_NUMWORD*2 + ECC_NUMWORD*2]={0};
|
u8 Z[ECC_NUMWORD * 2 + ECC_NUMWORD * 2] = {0};
|
||||||
u8 hash[ECC_NUMWORD],S1[ECC_NUMWORD];
|
u8 hash[ECC_NUMWORD], S1[ECC_NUMWORD];
|
||||||
u8 temp[2]={0x02,0x03};
|
u8 temp[2] = {0x02, 0x03};
|
||||||
ecc_point U[1];
|
ecc_point U[1];
|
||||||
|
|
||||||
/********************************************/
|
/********************************************/
|
||||||
sm2_shared_point(dA, ra, RA, PB, RB, U);
|
sm2_shared_point(dA, ra, RA, PB, RB, U);
|
||||||
/************KA=KDF(UX,UY,ZA,ZB,KLEN)**********/
|
/************KA=KDF(UX,UY,ZA,ZB,KLEN)**********/
|
||||||
memcpy(Z, U->x,ECC_NUMWORD);
|
memcpy(Z, U->x, ECC_NUMWORD);
|
||||||
memcpy(Z+ECC_NUMWORD, U->y,ECC_NUMWORD);
|
memcpy(Z + ECC_NUMWORD, U->y, ECC_NUMWORD);
|
||||||
memcpy(Z+ECC_NUMWORD*2,ZA,ECC_NUMWORD);
|
memcpy(Z + ECC_NUMWORD * 2, ZA, ECC_NUMWORD);
|
||||||
memcpy(Z+ECC_NUMWORD*2 +ECC_NUMWORD ,ZB,ECC_NUMWORD);
|
memcpy(Z + ECC_NUMWORD * 2 + ECC_NUMWORD, ZB, ECC_NUMWORD);
|
||||||
sm3_kdf(Z,ECC_NUMWORD*2+ECC_NUMWORD*2, K, klen);
|
sm3_kdf(Z, ECC_NUMWORD * 2 + ECC_NUMWORD * 2, K, klen);
|
||||||
/****S1 = Hash(0x02||Uy||Hash(Ux||ZA||ZB||x1||y1||x2||y2))****/
|
/****S1 = Hash(0x02||Uy||Hash(Ux||ZA||ZB||x1||y1||x2||y2))****/
|
||||||
ECC_Key_ex_hash1((u8*)U->x, RA, RB, ZA, ZB, hash);
|
ECC_Key_ex_hash1((u8 *) U->x, RA, RB, ZA, ZB, hash);
|
||||||
ECC_Key_ex_hash2(temp[0], (u8*)U->y, hash, S1);
|
ECC_Key_ex_hash2(temp[0], (u8 *) U->y, hash, S1);
|
||||||
/*test S1=SB?*/
|
/*test S1=SB?*/
|
||||||
if( memcmp(S1,SB,ECC_NUMWORD)!=0)
|
if (memcmp(S1, SB, ECC_NUMWORD) != 0) return -1;
|
||||||
return -1;
|
|
||||||
/*SA = Hash(0x03||yU||Hash(xU||ZA||ZB||x1||y1||x2||y2)) */
|
/*SA = Hash(0x03||yU||Hash(xU||ZA||ZB||x1||y1||x2||y2)) */
|
||||||
ECC_Key_ex_hash2(temp[1], (u8*)U->y, hash, SA);
|
ECC_Key_ex_hash2(temp[1], (u8 *) U->y, hash, SA);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ECC_KeyEx_Re_II(ecc_point *V, ecc_point *RA, ecc_point *RB, u8 ZA[], u8 ZB[], u8 SA[])
|
int
|
||||||
|
ECC_KeyEx_Re_II(ecc_point *V, ecc_point *RA, ecc_point *RB, u8 ZA[], u8 ZB[], u8 SA[])
|
||||||
{
|
{
|
||||||
u8 hash[ECC_NUMWORD];
|
u8 hash[ECC_NUMWORD];
|
||||||
u8 S2[ECC_NUMWORD];
|
u8 S2[ECC_NUMWORD];
|
||||||
u8 temp=0x03;
|
u8 temp = 0x03;
|
||||||
|
|
||||||
/*S2 = Hash(0x03||Vy||Hash(Vx||ZA||ZB||x1||y1||x2||y2))*/
|
/*S2 = Hash(0x03||Vy||Hash(Vx||ZA||ZB||x1||y1||x2||y2))*/
|
||||||
ECC_Key_ex_hash1((u8*)V->x, RA, RB, ZA, ZB, hash);
|
ECC_Key_ex_hash1((u8 *) V->x, RA, RB, ZA, ZB, hash);
|
||||||
ECC_Key_ex_hash2(temp, (u8*)V->y, hash, S2);
|
ECC_Key_ex_hash2(temp, (u8 *) V->y, hash, S2);
|
||||||
|
|
||||||
if( memcmp(S2,SA,ECC_NUMWORD)!=0)
|
if (memcmp(S2, SA, ECC_NUMWORD) != 0) return -1;
|
||||||
return -1;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int do_sm2_verify(char *pubkey, char *data_addr, int data_len, char *sign_addr)
|
static int
|
||||||
|
do_sm2_verify(char *pubkey, char *data_addr, int data_len, char *sign_addr)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
struct sm3_ctx sm3_ctx;
|
struct sm3_ctx sm3_ctx;
|
||||||
@ -479,15 +492,14 @@ static int do_sm2_verify(char *pubkey, char *data_addr, int data_len, char *sign
|
|||||||
memcpy(&ios_pub, pubkey, 64);
|
memcpy(&ios_pub, pubkey, 64);
|
||||||
|
|
||||||
ret = sm3_init(&sm3_ctx);
|
ret = sm3_init(&sm3_ctx);
|
||||||
sm3_z((u8 *)"1234567812345678", 16, &ios_pub, ios_Z);
|
sm3_z((u8 *) "1234567812345678", 16, &ios_pub, ios_Z);
|
||||||
sm3_update(&sm3_ctx, ios_Z, ECC_NUMWORD);
|
sm3_update(&sm3_ctx, ios_Z, ECC_NUMWORD);
|
||||||
sm3_update(&sm3_ctx, data_addr, data_len);
|
sm3_update(&sm3_ctx, (const u8 *) data_addr, data_len);
|
||||||
sm3_final(&sm3_ctx, ios_hash);
|
sm3_final(&sm3_ctx, ios_hash);
|
||||||
memcpy(sign_data, sign_addr, 64);
|
memcpy(sign_data, sign_addr, 64);
|
||||||
|
|
||||||
ret = sm2_verify(pubkey, ios_hash, sign_data, sign_data + 32);
|
ret = sm2_verify((ecc_point *) pubkey, ios_hash, sign_data, sign_data + 32);
|
||||||
if (ret)
|
if (ret) {
|
||||||
{
|
|
||||||
printf("verify err ret = %d\n", ret);
|
printf("verify err ret = %d\n", ret);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -495,7 +507,8 @@ static int do_sm2_verify(char *pubkey, char *data_addr, int data_len, char *sign
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int do_sm2_sign(char *prikey, char *pubkey, char *data_addr, int data_len, char *sign_addr)
|
int
|
||||||
|
do_sm2_sign(char *prikey, char *pubkey, char *data_addr, int data_len, char *sign_addr)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
struct sm3_ctx sm3_ctx;
|
struct sm3_ctx sm3_ctx;
|
||||||
@ -507,15 +520,14 @@ int do_sm2_sign(char *prikey, char *pubkey, char *data_addr, int data_len, char
|
|||||||
memcpy(&ios_pub, pubkey, 64);
|
memcpy(&ios_pub, pubkey, 64);
|
||||||
|
|
||||||
ret = sm3_init(&sm3_ctx);
|
ret = sm3_init(&sm3_ctx);
|
||||||
sm3_z((u8 *)"1234567812345678", 16, &ios_pub, ios_Z);
|
sm3_z((u8 *) "1234567812345678", 16, &ios_pub, ios_Z);
|
||||||
sm3_update(&sm3_ctx, ios_Z, ECC_NUMWORD);
|
sm3_update(&sm3_ctx, ios_Z, ECC_NUMWORD);
|
||||||
sm3_update(&sm3_ctx, data_addr, data_len);
|
sm3_update(&sm3_ctx, data_addr, data_len);
|
||||||
sm3_final(&sm3_ctx, ios_hash);
|
sm3_final(&sm3_ctx, ios_hash);
|
||||||
//hexdump("hash", ios_hash, 32);
|
//hexdump("hash", ios_hash, 32);
|
||||||
|
|
||||||
ret = sm2_sign(sign_data, sign_data + 32, prikey, ios_hash);
|
ret = sm2_sign(sign_data, sign_data + 32, prikey, ios_hash);
|
||||||
if (ret)
|
if (ret) {
|
||||||
{
|
|
||||||
printf("sign err ret = %d\n", ret);
|
printf("sign err ret = %d\n", ret);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <malloc.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <malloc.h>
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
// #include "svac_enc.h"
|
// #include "svac_enc.h"
|
||||||
#include "sm3.h"
|
|
||||||
#include "sm2.h"
|
#include "sm2.h"
|
||||||
|
#include "sm3.h"
|
||||||
|
|
||||||
#define PRINT_ERR printf
|
#define PRINT_ERR printf
|
||||||
#define PRINT_INFO printf
|
#define PRINT_INFO printf
|
||||||
@ -35,8 +35,7 @@
|
|||||||
#define SVAC_SEC_SLICE 9
|
#define SVAC_SEC_SLICE 9
|
||||||
#define SVAC_AUTH_SLICE 10
|
#define SVAC_AUTH_SLICE 10
|
||||||
|
|
||||||
struct nalu_data
|
struct nalu_data {
|
||||||
{
|
|
||||||
char *addr;
|
char *addr;
|
||||||
char *out_addr;
|
char *out_addr;
|
||||||
int len;
|
int len;
|
||||||
@ -47,14 +46,12 @@ struct nalu_data
|
|||||||
int need_add_racing_code;
|
int need_add_racing_code;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct vkek_info
|
struct vkek_info {
|
||||||
{
|
|
||||||
char vkek[16];
|
char vkek[16];
|
||||||
char version[32];
|
char version[32];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sec_param
|
struct sec_param {
|
||||||
{
|
|
||||||
/* <20><>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/* <20><>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||||
int encrypt_flag; //<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>
|
int encrypt_flag; //<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>
|
||||||
int encrypt_type; //<2F><><EFBFBD><EFBFBD><EFBFBD>㷨
|
int encrypt_type; //<2F><><EFBFBD><EFBFBD><EFBFBD>㷨
|
||||||
@ -65,7 +62,7 @@ struct sec_param
|
|||||||
int vek_encrypt_type; //vek<65><6B><EFBFBD><EFBFBD><EFBFBD>㷨
|
int vek_encrypt_type; //vek<65><6B><EFBFBD><EFBFBD><EFBFBD>㷨
|
||||||
char evek[32]; //evek
|
char evek[32]; //evek
|
||||||
int vek_len; //vek<65><6B><EFBFBD><EFBFBD>
|
int vek_len; //vek<65><6B><EFBFBD><EFBFBD>
|
||||||
char vkek_version[128]; //vkek version
|
char vkek_version[128];//vkek version
|
||||||
int version_len; //vkek version<6F><6E><EFBFBD><EFBFBD>
|
int version_len; //vkek version<6F><6E><EFBFBD><EFBFBD>
|
||||||
int iv_flag; //<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>iv
|
int iv_flag; //<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>iv
|
||||||
char iv[32]; //iv
|
char iv[32]; //iv
|
||||||
@ -74,15 +71,13 @@ struct sec_param
|
|||||||
int hash_period; // sign period frame
|
int hash_period; // sign period frame
|
||||||
};
|
};
|
||||||
|
|
||||||
struct hash_cache
|
struct hash_cache {
|
||||||
{
|
|
||||||
/* һ<><D2BB>gop<6F><70><EFBFBD>256֡ */
|
/* һ<><D2BB>gop<6F><70><EFBFBD>256֡ */
|
||||||
char hash[32];
|
char hash[32];
|
||||||
int frame_num;
|
int frame_num;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct svac_handle
|
struct svac_handle {
|
||||||
{
|
|
||||||
int magic;
|
int magic;
|
||||||
|
|
||||||
/* sm2 <20><>Կ */
|
/* sm2 <20><>Կ */
|
||||||
@ -93,7 +88,6 @@ struct svac_handle
|
|||||||
char sm2_prikey[32];
|
char sm2_prikey[32];
|
||||||
int sm_prikey_flag;
|
int sm_prikey_flag;
|
||||||
|
|
||||||
|
|
||||||
char vek[16];
|
char vek[16];
|
||||||
char iv[16];
|
char iv[16];
|
||||||
|
|
||||||
@ -126,30 +120,32 @@ struct svac_handle
|
|||||||
|
|
||||||
char idr_hash[32];
|
char idr_hash[32];
|
||||||
struct hash_cache cache[HASH_CACHE_NUM];
|
struct hash_cache cache[HASH_CACHE_NUM];
|
||||||
int cache_idx; // 0 ~ HASH_CACHE_NUM - 1
|
int cache_idx;// 0 ~ HASH_CACHE_NUM - 1
|
||||||
|
|
||||||
char camera_id[64]; //<2F><><EFBFBD><EFBFBD>id
|
char camera_id[64]; //<2F><><EFBFBD><EFBFBD>id
|
||||||
char camera_idc[19]; //֤<><D6A4>id
|
char camera_idc[19];//֤<><D6A4>id
|
||||||
};
|
};
|
||||||
|
|
||||||
#define CHECK_HANDLE(h) \
|
#define CHECK_HANDLE(h) \
|
||||||
do \
|
do { \
|
||||||
{ \
|
if (!h || h->magic != MAGIC_VALUE) { \
|
||||||
if (!h || h->magic != MAGIC_VALUE) \
|
|
||||||
{ \
|
|
||||||
PRINT_ERR("handle is error!\n"); \
|
PRINT_ERR("handle is error!\n"); \
|
||||||
return -1; \
|
return -1; \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
static unsigned int write_bits(unsigned char data, unsigned int write_len, unsigned char *addr, unsigned int offset)
|
void hexdump(char *title, const unsigned char *s, int l);
|
||||||
|
int do_sm2_sign(char *prikey, char *pubkey, char *data_addr, int data_len, char *sign_addr);
|
||||||
|
int do_base64_encode(unsigned char *dst, int *dlen, unsigned char *src, int slen);
|
||||||
|
|
||||||
|
static unsigned int
|
||||||
|
write_bits(unsigned char data, unsigned int write_len, unsigned char *addr, unsigned int offset)
|
||||||
{
|
{
|
||||||
unsigned int offset_in_bytes = offset & 7;
|
unsigned int offset_in_bytes = offset & 7;
|
||||||
unsigned int offset_of_bytes = offset / 8;
|
unsigned int offset_of_bytes = offset / 8;
|
||||||
|
|
||||||
/* if need write twice */
|
/* if need write twice */
|
||||||
if (offset_in_bytes && offset_in_bytes + write_len > 8)
|
if (offset_in_bytes && offset_in_bytes + write_len > 8) {
|
||||||
{
|
|
||||||
unsigned int second_write_len = offset_in_bytes + write_len - 8;
|
unsigned int second_write_len = offset_in_bytes + write_len - 8;
|
||||||
|
|
||||||
/* clean the bits which will been writen */
|
/* clean the bits which will been writen */
|
||||||
@ -159,9 +155,7 @@ static unsigned int write_bits(unsigned char data, unsigned int write_len, unsig
|
|||||||
/* write the second byte */
|
/* write the second byte */
|
||||||
addr[offset_of_bytes + 1] &= ((1 << (8 - second_write_len)) - 1);
|
addr[offset_of_bytes + 1] &= ((1 << (8 - second_write_len)) - 1);
|
||||||
addr[offset_of_bytes + 1] |= (data & ((1 << second_write_len) - 1)) << (8 - second_write_len);
|
addr[offset_of_bytes + 1] |= (data & ((1 << second_write_len) - 1)) << (8 - second_write_len);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
data &= ((1 << write_len) - 1);
|
data &= ((1 << write_len) - 1);
|
||||||
addr[offset_of_bytes] &= ~(((1 << write_len) - 1) << (8 - write_len - offset_in_bytes));
|
addr[offset_of_bytes] &= ~(((1 << write_len) - 1) << (8 - write_len - offset_in_bytes));
|
||||||
addr[offset_of_bytes] |= data << (8 - write_len - offset_in_bytes);
|
addr[offset_of_bytes] |= data << (8 - write_len - offset_in_bytes);
|
||||||
@ -170,46 +164,45 @@ static unsigned int write_bits(unsigned char data, unsigned int write_len, unsig
|
|||||||
return offset + write_len;
|
return offset + write_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int write_long_data_bits(unsigned int data, unsigned int write_len, unsigned char *addr, unsigned int offset)
|
static unsigned int
|
||||||
|
write_long_data_bits(unsigned int data, unsigned int write_len, unsigned char *addr, unsigned int offset)
|
||||||
{
|
{
|
||||||
unsigned int ret_offset = 0;
|
unsigned int ret_offset = 0;
|
||||||
|
|
||||||
if (write_len <= 8)
|
if (write_len <= 8)
|
||||||
ret_offset = write_bits((unsigned char)data, write_len, addr, offset);
|
ret_offset = write_bits((unsigned char) data, write_len, addr, offset);
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
int i;
|
int i;
|
||||||
int loop;
|
int loop;
|
||||||
unsigned int first_write_len = write_len & 7;
|
unsigned int first_write_len = write_len & 7;
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD>ֽڵĵ<DAB5>first_write_len<65><6E><EFBFBD><EFBFBD>*/
|
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD>ֽڵĵ<DAB5>first_write_len<65><6E><EFBFBD><EFBFBD>*/
|
||||||
if (first_write_len)
|
if (first_write_len)
|
||||||
offset = write_bits((unsigned char)((data >> (write_len - first_write_len)) & ((1 <<first_write_len) - 1)), first_write_len, addr, offset);
|
offset =
|
||||||
|
write_bits((unsigned char) ((data >> (write_len - first_write_len)) & ((1 << first_write_len) - 1)),
|
||||||
|
first_write_len, addr, offset);
|
||||||
|
|
||||||
loop = (write_len - first_write_len) / 8;
|
loop = (write_len - first_write_len) / 8;
|
||||||
ret_offset = offset;
|
ret_offset = offset;
|
||||||
|
|
||||||
for (i = 0; i < loop; i++)
|
for (i = 0; i < loop; i++)
|
||||||
ret_offset = write_bits((unsigned char)((data >> ((loop - i - 1) * 8)) & 0xff), 8, addr, ret_offset);
|
ret_offset = write_bits((unsigned char) ((data >> ((loop - i - 1) * 8)) & 0xff), 8, addr, ret_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret_offset;
|
return ret_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int write_string_bits(unsigned char *p, unsigned int write_len, unsigned char *addr, unsigned int offset)
|
static unsigned int
|
||||||
|
write_string_bits(unsigned char *p, unsigned int write_len, unsigned char *addr, unsigned int offset)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
unsigned int len = 0;
|
unsigned int len = 0;
|
||||||
|
|
||||||
while (len < write_len)
|
while (len < write_len) {
|
||||||
{
|
if (write_len - len > 8) {
|
||||||
if (write_len - len > 8)
|
|
||||||
{
|
|
||||||
offset = write_bits(p[i], 8, addr, offset);
|
offset = write_bits(p[i], 8, addr, offset);
|
||||||
len += 8;
|
len += 8;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
offset = write_bits(p[i], write_len - len, addr, offset);
|
offset = write_bits(p[i], write_len - len, addr, offset);
|
||||||
len += write_len - len;
|
len += write_len - len;
|
||||||
}
|
}
|
||||||
@ -220,9 +213,9 @@ static unsigned int write_string_bits(unsigned char *p, unsigned int write_len,
|
|||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* if find a nal, return the next address; if it's the end, return NULL */
|
/* if find a nal, return the next address; if it's the end, return NULL */
|
||||||
static char *findNal(char *buf, int *frame_len, char **nal_start, int *nal_len)
|
static char *
|
||||||
|
findNal(char *buf, int *frame_len, char **nal_start, int *nal_len)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char *start = NULL;
|
char *start = NULL;
|
||||||
@ -232,13 +225,10 @@ static char *findNal(char *buf, int *frame_len, char **nal_start, int *nal_len)
|
|||||||
*nal_len = 0;
|
*nal_len = 0;
|
||||||
|
|
||||||
/* parsing each NALU */
|
/* parsing each NALU */
|
||||||
for (i = 0; i < (int)(*frame_len - 3);i++)
|
for (i = 0; i < (int) (*frame_len - 3); i++) {
|
||||||
{
|
if (buf[i] == 0x00 && buf[i + 1] == 0x00 && buf[i + 2] == 0x00 && buf[i + 3] == 0x01) {
|
||||||
if (buf[i] == 0x00 && buf[i + 1] == 0x00 && buf[i + 2] == 0x00 && buf[i + 3] == 0x01)
|
|
||||||
{
|
|
||||||
/* not the first loop */
|
/* not the first loop */
|
||||||
if (start)
|
if (start) {
|
||||||
{
|
|
||||||
len = buf + i - start;
|
len = buf + i - start;
|
||||||
*nal_start = start;
|
*nal_start = start;
|
||||||
*nal_len = len;
|
*nal_len = len;
|
||||||
@ -249,8 +239,7 @@ static char *findNal(char *buf, int *frame_len, char **nal_start, int *nal_len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (start)
|
if (start) len = buf + *frame_len - start;
|
||||||
len = buf + *frame_len - start;
|
|
||||||
|
|
||||||
*nal_start = start;
|
*nal_start = start;
|
||||||
*nal_len = len;
|
*nal_len = len;
|
||||||
@ -259,25 +248,24 @@ static char *findNal(char *buf, int *frame_len, char **nal_start, int *nal_len)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int svac_parse_nalu(struct svac_handle *h, char *frame_data, int frame_len)
|
static int
|
||||||
|
svac_parse_nalu(struct svac_handle *h, char *frame_data, int frame_len)
|
||||||
{
|
{
|
||||||
char *ptr, *start = NULL;
|
char *ptr, *start = NULL;
|
||||||
int nal_len, frame_len_left = frame_len;
|
int nal_len, frame_len_left = frame_len;
|
||||||
|
|
||||||
ptr = frame_data;
|
ptr = frame_data;
|
||||||
|
|
||||||
do
|
do {
|
||||||
{
|
|
||||||
start = NULL;
|
start = NULL;
|
||||||
nal_len = 0;
|
nal_len = 0;
|
||||||
ptr = findNal(ptr, &frame_len_left, &start, &nal_len);
|
ptr = findNal(ptr, &frame_len_left, &start, &nal_len);
|
||||||
if (start)
|
if (start) {
|
||||||
{
|
|
||||||
memset(&h->nalu[h->nalu_num], 0, sizeof(struct nalu_data));
|
memset(&h->nalu[h->nalu_num], 0, sizeof(struct nalu_data));
|
||||||
|
|
||||||
h->nalu[h->nalu_num].addr = start;
|
h->nalu[h->nalu_num].addr = start;
|
||||||
h->nalu[h->nalu_num].len = nal_len;
|
h->nalu[h->nalu_num].len = nal_len;
|
||||||
h->nalu[h->nalu_num].encryption_idc = start[4] & 0x2 ? 1 : 0; //svac2.0 p21
|
h->nalu[h->nalu_num].encryption_idc = start[4] & 0x2 ? 1 : 0;//svac2.0 p21
|
||||||
h->nalu[h->nalu_num].authentication_idc = start[4] & 0x1; //svac2.0 p21
|
h->nalu[h->nalu_num].authentication_idc = start[4] & 0x1; //svac2.0 p21
|
||||||
h->nalu[h->nalu_num].type = (start[4] >> 2) & 0xf; //svac2.0 p21
|
h->nalu[h->nalu_num].type = (start[4] >> 2) & 0xf; //svac2.0 p21
|
||||||
|
|
||||||
@ -287,10 +275,9 @@ static int svac_parse_nalu(struct svac_handle *h, char *frame_data, int frame_le
|
|||||||
h->sec_nalu_flag = 1;
|
h->sec_nalu_flag = 1;
|
||||||
else if (SVAC_AUTH_SLICE == h->nalu[h->nalu_num].type)
|
else if (SVAC_AUTH_SLICE == h->nalu[h->nalu_num].type)
|
||||||
h->auth_nalu_flag = 1;
|
h->auth_nalu_flag = 1;
|
||||||
else if (SVAC_PPS_SLICE == h->nalu[h->nalu_num].type)
|
else if (SVAC_PPS_SLICE == h->nalu[h->nalu_num].type) {
|
||||||
{
|
|
||||||
/* pps<70>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>֡<EFBFBD><D6A1> svac2.0 p23 */
|
/* pps<70>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>֡<EFBFBD><D6A1> svac2.0 p23 */
|
||||||
h->frame_num = (unsigned char)start[5];
|
h->frame_num = (unsigned char) start[5];
|
||||||
//PRINT_INFO("frame %d\n", h->frame_num);
|
//PRINT_INFO("frame %d\n", h->frame_num);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,31 +291,27 @@ static int svac_parse_nalu(struct svac_handle *h, char *frame_data, int frame_le
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD> svac <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD> */
|
/* <20><><EFBFBD><EFBFBD> svac <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD> */
|
||||||
static int do_add_racing_Code(char *ptr, int *len, int offset)
|
static int
|
||||||
|
do_add_racing_Code(char *ptr, int *len, int offset)
|
||||||
{
|
{
|
||||||
int i, find = 0, start = offset;
|
int i, find = 0, start = offset;
|
||||||
unsigned int inlen = *len;
|
unsigned int inlen = *len;
|
||||||
|
|
||||||
for (i = start; i < inlen - 2; i++)
|
for (i = start; i < inlen - 2; i++) {
|
||||||
{
|
if (ptr[i] == 0 && ptr[i + 1] == 0 && !(ptr[i + 2] & 0xfc)) {
|
||||||
if (ptr[i] == 0 && ptr[i + 1] == 0 && !(ptr[i + 2] & 0xfc))
|
|
||||||
{
|
|
||||||
find = 1;
|
find = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (find)
|
if (find) {
|
||||||
{
|
|
||||||
unsigned int code_num = 0, zero_count = 0;
|
unsigned int code_num = 0, zero_count = 0;
|
||||||
char *tmp = (char *)malloc(*len + 4096*5);
|
char *tmp = (char *) malloc(*len + 4096 * 5);
|
||||||
|
|
||||||
memcpy(tmp, ptr, start);
|
memcpy(tmp, ptr, start);
|
||||||
|
|
||||||
for (i = start; i < inlen; i++)
|
for (i = start; i < inlen; i++) {
|
||||||
{
|
if (zero_count == 2 && !(ptr[i] & 0xfc)) {
|
||||||
if (zero_count == 2 && !(ptr[i] & 0xfc))
|
|
||||||
{
|
|
||||||
tmp[i + code_num] = 3;
|
tmp[i + code_num] = 3;
|
||||||
code_num++;
|
code_num++;
|
||||||
i--;
|
i--;
|
||||||
@ -352,9 +335,9 @@ static int do_add_racing_Code(char *ptr, int *len, int offset)
|
|||||||
return find;
|
return find;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* ȥ<><C8A5> svac <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD> */
|
/* ȥ<><C8A5> svac <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD> */
|
||||||
static int do_remove_racing_code(char *ptr, int *len, int offset)
|
static int
|
||||||
|
do_remove_racing_code(char *ptr, int *len, int offset)
|
||||||
{
|
{
|
||||||
int i, find = 0, start;
|
int i, find = 0, start;
|
||||||
unsigned int inlen;
|
unsigned int inlen;
|
||||||
@ -362,23 +345,18 @@ static int do_remove_racing_code(char *ptr, int *len, int offset)
|
|||||||
inlen = *len;
|
inlen = *len;
|
||||||
start = offset;
|
start = offset;
|
||||||
|
|
||||||
for (i = start; i < inlen - 2; i++)
|
for (i = start; i < inlen - 2; i++) {
|
||||||
{
|
if (ptr[i] == 0 && ptr[i + 1] == 0 && ptr[i + 2] == 0x3) {
|
||||||
if (ptr[i] == 0 && ptr[i + 1] == 0 && ptr[i + 2] == 0x3)
|
|
||||||
{
|
|
||||||
find = 1;
|
find = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (find)
|
if (find) {
|
||||||
{
|
|
||||||
unsigned int code_num = 0, zero_count = 0;
|
unsigned int code_num = 0, zero_count = 0;
|
||||||
|
|
||||||
for (i = start; i < inlen; i++)
|
for (i = start; i < inlen; i++) {
|
||||||
{
|
if (zero_count == 2 && ptr[i] == 3) {
|
||||||
if (zero_count == 2 && ptr[i] == 3)
|
|
||||||
{
|
|
||||||
code_num++;
|
code_num++;
|
||||||
zero_count = 0;
|
zero_count = 0;
|
||||||
continue;
|
continue;
|
||||||
@ -396,7 +374,10 @@ static int do_remove_racing_code(char *ptr, int *len, int offset)
|
|||||||
return find;
|
return find;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int enc_vek(struct svac_handle *h)
|
void do_sm4_ecb_encrypt(const char *in_buff, char *out_buff, const char *key);
|
||||||
|
|
||||||
|
static int
|
||||||
|
enc_vek(struct svac_handle *h)
|
||||||
{
|
{
|
||||||
do_sm4_ecb_encrypt(h->vek, h->curr_param.evek, h->vkek_list[0].vkek);
|
do_sm4_ecb_encrypt(h->vek, h->curr_param.evek, h->vkek_list[0].vkek);
|
||||||
h->curr_param.vek_len = 16;
|
h->curr_param.vek_len = 16;
|
||||||
@ -404,33 +385,28 @@ static int enc_vek(struct svac_handle *h)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int calc_nalu_hash(struct svac_handle *h)
|
static int
|
||||||
|
calc_nalu_hash(struct svac_handle *h)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
struct sm3_ctx ctx;
|
struct sm3_ctx ctx;
|
||||||
char hash[32];
|
char hash[32];
|
||||||
struct hash_cache *cache;
|
struct hash_cache *cache;
|
||||||
|
|
||||||
if (!h->curr_param.auth_flag)
|
if (!h->curr_param.auth_flag) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
for (i = 0; i < h->nalu_num; i++)
|
for (i = 0; i < h->nalu_num; i++) {
|
||||||
{
|
if (h->nalu[i].authentication_idc) break;
|
||||||
if (h->nalu[i].authentication_idc)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* û<><C3BB>ǩ<EFBFBD><C7A9><EFBFBD><EFBFBD>nalu */
|
/* û<><C3BB>ǩ<EFBFBD><C7A9><EFBFBD><EFBFBD>nalu */
|
||||||
if (i >= h->nalu_num)
|
if (i >= h->nalu_num) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
memset(hash, 0, sizeof(hash));
|
memset(hash, 0, sizeof(hash));
|
||||||
sm3_init(&ctx);
|
sm3_init(&ctx);
|
||||||
|
|
||||||
for (i = 0; i < h->nalu_num; i++)
|
for (i = 0; i < h->nalu_num; i++) {
|
||||||
{
|
if (!h->nalu[i].authentication_idc) continue;
|
||||||
if (!h->nalu[i].authentication_idc)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
sm3_update(&ctx, h->nalu[i].addr + 4, h->nalu[i].len - 4);
|
sm3_update(&ctx, h->nalu[i].addr + 4, h->nalu[i].len - 4);
|
||||||
//printf("calc_nalu_hash nalu %d len %d\n", i, h->nalu[i].len - 4);
|
//printf("calc_nalu_hash nalu %d len %d\n", i, h->nalu[i].len - 4);
|
||||||
@ -438,8 +414,7 @@ static int calc_nalu_hash(struct svac_handle *h)
|
|||||||
|
|
||||||
sm3_final(&ctx, hash);
|
sm3_final(&ctx, hash);
|
||||||
|
|
||||||
if (!h->frame_num)
|
if (!h->frame_num) memcpy(h->idr_hash, hash, 32);
|
||||||
memcpy(h->idr_hash, hash, 32);
|
|
||||||
|
|
||||||
cache = &h->cache[h->cache_idx];
|
cache = &h->cache[h->cache_idx];
|
||||||
memcpy(cache->hash, hash, 32);
|
memcpy(cache->hash, hash, 32);
|
||||||
@ -451,14 +426,18 @@ static int calc_nalu_hash(struct svac_handle *h)
|
|||||||
|
|
||||||
void do_sm4_ofb_encrypt(const char *in_buff, int in_len, char *out_buff, int *out_len, const char *key, const char *iv);
|
void do_sm4_ofb_encrypt(const char *in_buff, int in_len, char *out_buff, int *out_len, const char *key, const char *iv);
|
||||||
|
|
||||||
static int do_dec_and_copy(int encrypt_type, const char *in_buff, int in_len, char *out_buff, int *out_len, const char *key, const char *iv)
|
static int
|
||||||
|
do_dec_and_copy(int encrypt_type,
|
||||||
|
const char *in_buff,
|
||||||
|
int in_len,
|
||||||
|
char *out_buff,
|
||||||
|
int *out_len,
|
||||||
|
const char *key,
|
||||||
|
const char *iv)
|
||||||
{
|
{
|
||||||
if (encrypt_type == SM4_OFB)
|
if (encrypt_type == SM4_OFB) {
|
||||||
{
|
|
||||||
do_sm4_ofb_encrypt(in_buff, in_len, out_buff, out_len, key, iv);
|
do_sm4_ofb_encrypt(in_buff, in_len, out_buff, out_len, key, iv);
|
||||||
}
|
} else if (encrypt_type == SM1_OFB) {
|
||||||
else if (encrypt_type == SM1_OFB)
|
|
||||||
{
|
|
||||||
PRINT_ERR("unsupport SM1 now!\n");
|
PRINT_ERR("unsupport SM1 now!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -466,16 +445,15 @@ static int do_dec_and_copy(int encrypt_type, const char *in_buff, int in_len, ch
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int make_bypass_frame(struct svac_handle *h, char *out_buff, int *out_len)
|
static int
|
||||||
|
make_bypass_frame(struct svac_handle *h, char *out_buff, int *out_len)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
*out_len = 0;
|
*out_len = 0;
|
||||||
|
|
||||||
for (i = 0; i < h->nalu_num; i++)
|
for (i = 0; i < h->nalu_num; i++) {
|
||||||
{
|
if (h->nalu[i].type == SVAC_SEC_SLICE) continue;
|
||||||
if (h->nalu[i].type == SVAC_SEC_SLICE)
|
|
||||||
continue;
|
|
||||||
memcpy(out_buff + *out_len, h->nalu[i].addr, h->nalu[i].len);
|
memcpy(out_buff + *out_len, h->nalu[i].addr, h->nalu[i].len);
|
||||||
*out_len += h->nalu[i].len;
|
*out_len += h->nalu[i].len;
|
||||||
}
|
}
|
||||||
@ -483,14 +461,14 @@ static int make_bypass_frame(struct svac_handle *h, char *out_buff, int *out_len
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int update_new_enc_param(struct svac_handle *h)
|
static int
|
||||||
|
update_new_enc_param(struct svac_handle *h)
|
||||||
{
|
{
|
||||||
memcpy(&h->curr_param, &h->new_param, sizeof(struct sec_param));
|
memcpy(&h->curr_param, &h->new_param, sizeof(struct sec_param));
|
||||||
//printf("h->new_param.enc %d\n", h->new_param.encrypt_flag);
|
//printf("h->new_param.enc %d\n", h->new_param.encrypt_flag);
|
||||||
h->sec_param_update_flag = 0;
|
h->sec_param_update_flag = 0;
|
||||||
|
|
||||||
if (h->curr_param.encrypt_flag)
|
if (h->curr_param.encrypt_flag) {
|
||||||
{
|
|
||||||
char vek[16];
|
char vek[16];
|
||||||
char iv[16];
|
char iv[16];
|
||||||
|
|
||||||
@ -505,32 +483,31 @@ static int update_new_enc_param(struct svac_handle *h)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *SvacEncCreate(void)
|
void *
|
||||||
|
SvacEncCreate(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
struct svac_handle *h;
|
struct svac_handle *h;
|
||||||
|
|
||||||
h = (struct svac_handle *)malloc(sizeof(struct svac_handle));
|
h = (struct svac_handle *) malloc(sizeof(struct svac_handle));
|
||||||
if (!h)
|
if (!h) return NULL;
|
||||||
return NULL;
|
|
||||||
|
|
||||||
memset(h, 0, sizeof(struct svac_handle));
|
memset(h, 0, sizeof(struct svac_handle));
|
||||||
h->magic = MAGIC_VALUE;
|
h->magic = MAGIC_VALUE;
|
||||||
|
|
||||||
for (i = 0; i < HASH_CACHE_NUM; i++)
|
for (i = 0; i < HASH_CACHE_NUM; i++) h->cache[i].frame_num = -1;
|
||||||
h->cache[i].frame_num = -1;
|
|
||||||
|
|
||||||
return (void *)h;
|
return (void *) h;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacEncSetVkek(void *handle, char *vkek, char *version, int verion_len)
|
int
|
||||||
|
SvacEncSetVkek(void *handle, char *vkek, char *version, int verion_len)
|
||||||
{
|
{
|
||||||
struct svac_handle *h = (struct svac_handle *)handle;
|
struct svac_handle *h = (struct svac_handle *) handle;
|
||||||
|
|
||||||
CHECK_HANDLE(h);
|
CHECK_HANDLE(h);
|
||||||
|
|
||||||
if (!vkek || !version)
|
if (!vkek || !version) return -1;
|
||||||
return -1;
|
|
||||||
|
|
||||||
memcpy(h->vkek_list[0].vkek, vkek, 16);
|
memcpy(h->vkek_list[0].vkek, vkek, 16);
|
||||||
memset(h->vkek_list[0].version, 0, sizeof(h->vkek_list[0].version));
|
memset(h->vkek_list[0].version, 0, sizeof(h->vkek_list[0].version));
|
||||||
@ -540,14 +517,14 @@ int SvacEncSetVkek(void *handle, char *vkek, char *version, int verion_len)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacEncSetKeyPair(void *handle, char *sm2_prikey, char *sm2_pubkey)
|
int
|
||||||
|
SvacEncSetKeyPair(void *handle, char *sm2_prikey, char *sm2_pubkey)
|
||||||
{
|
{
|
||||||
struct svac_handle *h = (struct svac_handle *)handle;
|
struct svac_handle *h = (struct svac_handle *) handle;
|
||||||
|
|
||||||
CHECK_HANDLE(h);
|
CHECK_HANDLE(h);
|
||||||
|
|
||||||
if (!sm2_prikey || !sm2_pubkey)
|
if (!sm2_prikey || !sm2_pubkey) return -1;
|
||||||
return -1;
|
|
||||||
|
|
||||||
hexdump("sm2_prikey", sm2_prikey, 32);
|
hexdump("sm2_prikey", sm2_prikey, 32);
|
||||||
hexdump("sm2_pubkey", sm2_pubkey, 64);
|
hexdump("sm2_pubkey", sm2_pubkey, 64);
|
||||||
@ -559,43 +536,42 @@ int SvacEncSetKeyPair(void *handle, char *sm2_prikey, char *sm2_pubkey)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacEncSetIpcId(void *handle, char *id)
|
int
|
||||||
|
SvacEncSetIpcId(void *handle, char *id)
|
||||||
{
|
{
|
||||||
struct svac_handle *h = (struct svac_handle *)handle;
|
struct svac_handle *h = (struct svac_handle *) handle;
|
||||||
|
|
||||||
CHECK_HANDLE(h);
|
CHECK_HANDLE(h);
|
||||||
|
|
||||||
memset(h->camera_id, 0, sizeof(h->camera_id));
|
memset(h->camera_id, 0, sizeof(h->camera_id));
|
||||||
memcpy(h->camera_id, id, strlen(id));
|
memcpy(h->camera_id, id, strlen(id));
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacEncSetCertId(void *handle, char *cert_id)
|
int
|
||||||
|
SvacEncSetCertId(void *handle, char *cert_id)
|
||||||
{
|
{
|
||||||
struct svac_handle *h = (struct svac_handle *)handle;
|
struct svac_handle *h = (struct svac_handle *) handle;
|
||||||
int serial_len = 0;
|
int serial_len = 0;
|
||||||
|
|
||||||
CHECK_HANDLE(h);
|
CHECK_HANDLE(h);
|
||||||
|
|
||||||
if (!cert_id)
|
if (!cert_id) return -1;
|
||||||
return -1;
|
|
||||||
|
|
||||||
serial_len = strlen(cert_id);
|
serial_len = strlen(cert_id);
|
||||||
if (serial_len > 19)
|
if (serial_len > 19) serial_len = 19;
|
||||||
serial_len = 19;
|
|
||||||
|
|
||||||
memset(h->camera_idc, 0, sizeof(h->camera_idc));
|
memset(h->camera_idc, 0, sizeof(h->camera_idc));
|
||||||
memcpy(h->camera_idc + 19 - serial_len, cert_id, serial_len);
|
memcpy(h->camera_idc + 19 - serial_len, cert_id, serial_len);
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacEncSetLevel(void *handle, char level)
|
int
|
||||||
|
SvacEncSetLevel(void *handle, char level)
|
||||||
{
|
{
|
||||||
struct svac_handle *h = (struct svac_handle *)handle;
|
struct svac_handle *h = (struct svac_handle *) handle;
|
||||||
|
|
||||||
h->new_param.encrypt_flag = 0;
|
h->new_param.encrypt_flag = 0;
|
||||||
h->new_param.encrypt_type = SM4_OFB;
|
h->new_param.encrypt_type = SM4_OFB;
|
||||||
@ -605,12 +581,9 @@ int SvacEncSetLevel(void *handle, char level)
|
|||||||
h->new_param.only_IDR = 1;
|
h->new_param.only_IDR = 1;
|
||||||
h->new_param.hash_period = 0;
|
h->new_param.hash_period = 0;
|
||||||
|
|
||||||
if (level == 'b' || level == 'B')
|
if (level == 'b' || level == 'B') {
|
||||||
{
|
|
||||||
h->new_param.auth_flag = 1;
|
h->new_param.auth_flag = 1;
|
||||||
}
|
} else if (level == 'c' || level == 'C') {
|
||||||
else if (level == 'c' || level == 'C')
|
|
||||||
{
|
|
||||||
h->new_param.encrypt_flag = 1;
|
h->new_param.encrypt_flag = 1;
|
||||||
h->new_param.auth_flag = 1;
|
h->new_param.auth_flag = 1;
|
||||||
}
|
}
|
||||||
@ -620,7 +593,8 @@ int SvacEncSetLevel(void *handle, char level)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int write_security_nalu_data(struct svac_handle *h, unsigned char *ptr, unsigned int *len)
|
static int
|
||||||
|
write_security_nalu_data(struct svac_handle *h, unsigned char *ptr, unsigned int *len)
|
||||||
{
|
{
|
||||||
unsigned int offset = 0;
|
unsigned int offset = 0;
|
||||||
unsigned int l = *len;
|
unsigned int l = *len;
|
||||||
@ -630,8 +604,7 @@ static int write_security_nalu_data(struct svac_handle *h, unsigned char *ptr, u
|
|||||||
/* write authentication_flag */
|
/* write authentication_flag */
|
||||||
offset = write_bits(h->curr_param.auth_flag, 1, ptr + l, offset);
|
offset = write_bits(h->curr_param.auth_flag, 1, ptr + l, offset);
|
||||||
|
|
||||||
if (h->curr_param.encrypt_flag)
|
if (h->curr_param.encrypt_flag) {
|
||||||
{
|
|
||||||
unsigned char tmp;
|
unsigned char tmp;
|
||||||
|
|
||||||
/* write encryption_type */
|
/* write encryption_type */
|
||||||
@ -646,8 +619,7 @@ static int write_security_nalu_data(struct svac_handle *h, unsigned char *ptr, u
|
|||||||
/* write iv_flag */
|
/* write iv_flag */
|
||||||
offset = write_bits(1, 1, ptr + l, offset);
|
offset = write_bits(1, 1, ptr + l, offset);
|
||||||
|
|
||||||
if (1)
|
if (1) {
|
||||||
{
|
|
||||||
/* write vek_encryption_type */
|
/* write vek_encryption_type */
|
||||||
tmp = 1;
|
tmp = 1;
|
||||||
offset = write_bits(tmp, 4, ptr + l, offset);
|
offset = write_bits(tmp, 4, ptr + l, offset);
|
||||||
@ -661,8 +633,7 @@ static int write_security_nalu_data(struct svac_handle *h, unsigned char *ptr, u
|
|||||||
offset = write_string_bits(h->vkek_list[0].version, strlen(h->vkek_list[0].version) * 8, ptr + l, offset);
|
offset = write_string_bits(h->vkek_list[0].version, strlen(h->vkek_list[0].version) * 8, ptr + l, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (1)
|
if (1) {
|
||||||
{
|
|
||||||
/* write iv_length_minus1 */
|
/* write iv_length_minus1 */
|
||||||
offset = write_bits(16 - 1, 8, ptr + l, offset);
|
offset = write_bits(16 - 1, 8, ptr + l, offset);
|
||||||
/* write iv */
|
/* write iv */
|
||||||
@ -670,8 +641,7 @@ static int write_security_nalu_data(struct svac_handle *h, unsigned char *ptr, u
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (h->curr_param.auth_flag)
|
if (h->curr_param.auth_flag) {
|
||||||
{
|
|
||||||
offset = write_bits(0, 2, ptr + l, offset);
|
offset = write_bits(0, 2, ptr + l, offset);
|
||||||
offset = write_bits(h->curr_param.only_IDR, 1, ptr + l, offset);
|
offset = write_bits(h->curr_param.only_IDR, 1, ptr + l, offset);
|
||||||
offset = write_bits(0, 2, ptr + l, offset);
|
offset = write_bits(0, 2, ptr + l, offset);
|
||||||
@ -683,8 +653,7 @@ static int write_security_nalu_data(struct svac_handle *h, unsigned char *ptr, u
|
|||||||
offset = write_string_bits(h->camera_id, 160, ptr + l, offset);
|
offset = write_string_bits(h->camera_id, 160, ptr + l, offset);
|
||||||
|
|
||||||
offset = write_bits(1, 1, ptr + l, offset);
|
offset = write_bits(1, 1, ptr + l, offset);
|
||||||
if (offset & 7)
|
if (offset & 7) offset = write_bits(0, 8 - (offset & 7), ptr + l, offset);
|
||||||
offset = write_bits(0, 8 - (offset & 7), ptr + l, offset);
|
|
||||||
|
|
||||||
assert(!(offset & 7));
|
assert(!(offset & 7));
|
||||||
l += offset / 8;
|
l += offset / 8;
|
||||||
@ -694,7 +663,8 @@ static int write_security_nalu_data(struct svac_handle *h, unsigned char *ptr, u
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int create_security_nalu(struct svac_handle *h, char *ptr, int *len)
|
static int
|
||||||
|
create_security_nalu(struct svac_handle *h, char *ptr, int *len)
|
||||||
{
|
{
|
||||||
unsigned int save_len = 0;
|
unsigned int save_len = 0;
|
||||||
unsigned int l = *len;
|
unsigned int l = *len;
|
||||||
@ -719,7 +689,8 @@ static int create_security_nalu(struct svac_handle *h, char *ptr, int *len)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int write_auth_nalu_data(struct svac_handle *h, unsigned char *ptr, unsigned int *len)
|
static int
|
||||||
|
write_auth_nalu_data(struct svac_handle *h, unsigned char *ptr, unsigned int *len)
|
||||||
{
|
{
|
||||||
unsigned int offset = 0;
|
unsigned int offset = 0;
|
||||||
unsigned int l = *len;
|
unsigned int l = *len;
|
||||||
@ -728,15 +699,13 @@ static int write_auth_nalu_data(struct svac_handle *h, unsigned char *ptr, unsig
|
|||||||
/* write frame_num */
|
/* write frame_num */
|
||||||
offset = write_bits(h->sign_frame_num, 8, ptr + l, offset);
|
offset = write_bits(h->sign_frame_num, 8, ptr + l, offset);
|
||||||
/* write authentication_data_length_minus1 */
|
/* write authentication_data_length_minus1 */
|
||||||
offset = write_bits((unsigned int)(h->sign_len - 1), 8, ptr + l, offset);
|
offset = write_bits((unsigned int) (h->sign_len - 1), 8, ptr + l, offset);
|
||||||
/* write authentication_data */
|
/* write authentication_data */
|
||||||
for (i = 0; i < (int)h->sign_len; i++)
|
for (i = 0; i < (int) h->sign_len; i++) offset = write_bits(h->sign[i], 8, ptr + l, offset);
|
||||||
offset = write_bits(h->sign[i], 8, ptr + l, offset);
|
|
||||||
|
|
||||||
/* write rbsp_trailing_bits() */
|
/* write rbsp_trailing_bits() */
|
||||||
offset = write_bits(1, 1, ptr + l, offset);
|
offset = write_bits(1, 1, ptr + l, offset);
|
||||||
if (offset & 7)
|
if (offset & 7) offset = write_bits(0, 8 - (offset & 7), ptr + l, offset);
|
||||||
offset = write_bits(0, 8 - (offset & 7), ptr + l, offset);
|
|
||||||
|
|
||||||
l += offset / 8;
|
l += offset / 8;
|
||||||
*len = l;
|
*len = l;
|
||||||
@ -744,7 +713,8 @@ static int write_auth_nalu_data(struct svac_handle *h, unsigned char *ptr, unsig
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int create_auth_nalu(struct svac_handle *h, char *ptr, int *len)
|
static int
|
||||||
|
create_auth_nalu(struct svac_handle *h, char *ptr, int *len)
|
||||||
{
|
{
|
||||||
unsigned int save_len;
|
unsigned int save_len;
|
||||||
unsigned int l = *len;
|
unsigned int l = *len;
|
||||||
@ -772,7 +742,8 @@ static int create_auth_nalu(struct svac_handle *h, char *ptr, int *len)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int make_enc_frame(struct svac_handle *h, char *out_buff, int *out_data_len)
|
static int
|
||||||
|
make_enc_frame(struct svac_handle *h, char *out_buff, int *out_data_len)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int out_offset = 0;
|
int out_offset = 0;
|
||||||
@ -781,40 +752,30 @@ static int make_enc_frame(struct svac_handle *h, char *out_buff, int *out_data_l
|
|||||||
|
|
||||||
*out_data_len = 0;
|
*out_data_len = 0;
|
||||||
|
|
||||||
if (h->idr_flag)
|
if (h->idr_flag) { create_security_nalu(h, out_buff, &out_offset); }
|
||||||
{
|
|
||||||
create_security_nalu(h, out_buff, &out_offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < h->nalu_num; i++)
|
for (i = 0; i < h->nalu_num; i++) {
|
||||||
{
|
if (((h->nalu[i].type != SVAC_IDR_SLICE) && (h->nalu[i].type != SVAC_NO_IDR_SLICE))
|
||||||
if (((h->nalu[i].type != SVAC_IDR_SLICE) && (h->nalu[i].type != SVAC_NO_IDR_SLICE)) || \
|
|| (!h->curr_param.encrypt_flag)) {
|
||||||
(!h->curr_param.encrypt_flag))
|
|
||||||
{
|
|
||||||
memcpy(out_buff + out_offset, h->nalu[i].addr, h->nalu[i].len);
|
memcpy(out_buff + out_offset, h->nalu[i].addr, h->nalu[i].len);
|
||||||
h->nalu[i].out_addr = out_buff + out_offset;
|
h->nalu[i].out_addr = out_buff + out_offset;
|
||||||
|
|
||||||
//out_buff[out_offset + 4] &= 0xfc;
|
//out_buff[out_offset + 4] &= 0xfc;
|
||||||
out_nalu_len = h->nalu[i].len;
|
out_nalu_len = h->nalu[i].len;
|
||||||
if (h->nalu[i].need_add_racing_code)
|
if (h->nalu[i].need_add_racing_code) do_add_racing_Code(out_buff + out_offset, &out_nalu_len, 5);
|
||||||
do_add_racing_Code(out_buff + out_offset, &out_nalu_len, 5);
|
|
||||||
out_offset += out_nalu_len;
|
out_offset += out_nalu_len;
|
||||||
|
|
||||||
h->nalu[i].out_len = out_nalu_len;
|
h->nalu[i].out_len = out_nalu_len;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
int enc_len;
|
int enc_len;
|
||||||
int out_len;
|
int out_len;
|
||||||
|
|
||||||
if (!h->find_vkek_flag)
|
if (!h->find_vkek_flag) {
|
||||||
{
|
|
||||||
PRINT_ERR("Not find vkek [%s]! skip dec frame!\n", h->curr_param.vkek_version);
|
PRINT_ERR("Not find vkek [%s]! skip dec frame!\n", h->curr_param.vkek_version);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!h->nalu[i].need_add_racing_code)
|
if (!h->nalu[i].need_add_racing_code) {
|
||||||
{
|
|
||||||
do_remove_racing_code(h->nalu[i].addr, &h->nalu[i].len, 5);
|
do_remove_racing_code(h->nalu[i].addr, &h->nalu[i].len, 5);
|
||||||
h->nalu[i].need_add_racing_code = 1;
|
h->nalu[i].need_add_racing_code = 1;
|
||||||
}
|
}
|
||||||
@ -823,9 +784,9 @@ static int make_enc_frame(struct svac_handle *h, char *out_buff, int *out_data_l
|
|||||||
enc_len = h->nalu[i].len - 1 - 5;
|
enc_len = h->nalu[i].len - 1 - 5;
|
||||||
h->nalu[i].out_addr = out_buff + out_offset;
|
h->nalu[i].out_addr = out_buff + out_offset;
|
||||||
|
|
||||||
ret = do_dec_and_copy(h->curr_param.encrypt_type, h->nalu[i].addr + 5, enc_len, out_buff + out_offset + 5, &out_len, h->vek, h->iv);
|
ret = do_dec_and_copy(h->curr_param.encrypt_type, h->nalu[i].addr + 5, enc_len, out_buff + out_offset + 5,
|
||||||
if (ret)
|
&out_len, h->vek, h->iv);
|
||||||
return -1;
|
if (ret) return -1;
|
||||||
|
|
||||||
memcpy(out_buff + out_offset + enc_len + 5, h->nalu[i].addr + 5 + enc_len, 1);
|
memcpy(out_buff + out_offset + enc_len + 5, h->nalu[i].addr + 5 + enc_len, 1);
|
||||||
|
|
||||||
@ -844,17 +805,16 @@ static int make_enc_frame(struct svac_handle *h, char *out_buff, int *out_data_l
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int calc_frame_hash(struct svac_handle *h, char *out_buff, int *out_data_len)
|
static int
|
||||||
|
calc_frame_hash(struct svac_handle *h, char *out_buff, int *out_data_len)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
struct sm3_ctx ctx;
|
struct sm3_ctx ctx;
|
||||||
|
|
||||||
sm3_init(&ctx);
|
sm3_init(&ctx);
|
||||||
|
|
||||||
for (i = 0; i < h->nalu_num; i++)
|
for (i = 0; i < h->nalu_num; i++) {
|
||||||
{
|
if ((h->nalu[i].type == SVAC_IDR_SLICE) || (h->nalu[i].type == SVAC_NO_IDR_SLICE)) {
|
||||||
if ((h->nalu[i].type == SVAC_IDR_SLICE) || (h->nalu[i].type == SVAC_NO_IDR_SLICE))
|
|
||||||
{
|
|
||||||
h->nalu[i].out_addr[4] |= 1;
|
h->nalu[i].out_addr[4] |= 1;
|
||||||
sm3_update(&ctx, h->nalu[i].out_addr + 4, h->nalu[i].out_len - 4);
|
sm3_update(&ctx, h->nalu[i].out_addr + 4, h->nalu[i].out_len - 4);
|
||||||
//printf("calc_frame_hash nalu %d len %d\n", i, h->nalu[i].out_len - 4);
|
//printf("calc_frame_hash nalu %d len %d\n", i, h->nalu[i].out_len - 4);
|
||||||
@ -866,7 +826,8 @@ static int calc_frame_hash(struct svac_handle *h, char *out_buff, int *out_data_
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int calc_idr_sign_data(struct svac_handle *h)
|
static int
|
||||||
|
calc_idr_sign_data(struct svac_handle *h)
|
||||||
{
|
{
|
||||||
char sign[64];
|
char sign[64];
|
||||||
int sign_len = 64;
|
int sign_len = 64;
|
||||||
@ -880,13 +841,12 @@ static int calc_idr_sign_data(struct svac_handle *h)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int make_auth_nal(struct svac_handle *h, char *out_buff, int *out_data_len)
|
static int
|
||||||
|
make_auth_nal(struct svac_handle *h, char *out_buff, int *out_data_len)
|
||||||
{
|
{
|
||||||
if (!h->curr_param.auth_flag)
|
if (!h->curr_param.auth_flag) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (h->curr_param.only_IDR && h->idr_flag)
|
if (h->curr_param.only_IDR && h->idr_flag) {
|
||||||
{
|
|
||||||
calc_frame_hash(h, out_buff, out_data_len);
|
calc_frame_hash(h, out_buff, out_data_len);
|
||||||
calc_idr_sign_data(h);
|
calc_idr_sign_data(h);
|
||||||
create_auth_nalu(h, out_buff, out_data_len);
|
create_auth_nalu(h, out_buff, out_data_len);
|
||||||
@ -895,14 +855,14 @@ static int make_auth_nal(struct svac_handle *h, char *out_buff, int *out_data_le
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacEncProcess(void *handle, char *in_buff, int in_len, char *out_buff, int *out_len)
|
int
|
||||||
|
SvacEncProcess(void *handle, char *in_buff, int in_len, char *out_buff, int *out_len)
|
||||||
{
|
{
|
||||||
struct svac_handle *h = (struct svac_handle *)handle;
|
struct svac_handle *h = (struct svac_handle *) handle;
|
||||||
|
|
||||||
CHECK_HANDLE(h);
|
CHECK_HANDLE(h);
|
||||||
|
|
||||||
if (!in_buff || !out_buff || !out_len)
|
if (!in_buff || !out_buff || !out_len) return -1;
|
||||||
return -1;
|
|
||||||
|
|
||||||
h->nalu_num = 0;
|
h->nalu_num = 0;
|
||||||
h->idr_flag = 0;
|
h->idr_flag = 0;
|
||||||
@ -911,18 +871,15 @@ int SvacEncProcess(void *handle, char *in_buff, int in_len, char *out_buff, int
|
|||||||
|
|
||||||
svac_parse_nalu(h, in_buff, in_len);
|
svac_parse_nalu(h, in_buff, in_len);
|
||||||
|
|
||||||
if (!h->nalu_num)
|
if (!h->nalu_num) {
|
||||||
{
|
|
||||||
PRINT_ERR("Not find nalu start code!\n");
|
PRINT_ERR("Not find nalu start code!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (h->idr_flag && h->sec_param_update_flag)
|
if (h->idr_flag && h->sec_param_update_flag) update_new_enc_param(h);
|
||||||
update_new_enc_param(h);
|
|
||||||
|
|
||||||
/* <20><><EFBFBD><EFBFBD><EFBFBD>a<EFBFBD><61><EFBFBD><EFBFBD><EFBFBD><EFBFBD> bypass */
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>a<EFBFBD><61><EFBFBD><EFBFBD><EFBFBD><EFBFBD> bypass */
|
||||||
if (!h->curr_param.auth_flag && !h->curr_param.encrypt_flag)
|
if (!h->curr_param.auth_flag && !h->curr_param.encrypt_flag) return make_bypass_frame(h, out_buff, out_len);
|
||||||
return make_bypass_frame(h, out_buff, out_len);
|
|
||||||
|
|
||||||
make_enc_frame(h, out_buff, out_len);
|
make_enc_frame(h, out_buff, out_len);
|
||||||
make_auth_nal(h, out_buff, out_len);
|
make_auth_nal(h, out_buff, out_len);
|
||||||
@ -930,9 +887,10 @@ int SvacEncProcess(void *handle, char *in_buff, int in_len, char *out_buff, int
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SvacEncRelease(void *handle)
|
int
|
||||||
|
SvacEncRelease(void *handle)
|
||||||
{
|
{
|
||||||
struct svac_handle *h = (struct svac_handle *)handle;
|
struct svac_handle *h = (struct svac_handle *) handle;
|
||||||
|
|
||||||
CHECK_HANDLE(h);
|
CHECK_HANDLE(h);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user