Update workflow and bug fix.
This commit is contained in:
parent
a020541c90
commit
fa92fc6a05
|
@ -7,7 +7,7 @@
|
|||
*.lai
|
||||
*.so
|
||||
*.so.*
|
||||
*.dll
|
||||
#*.dll
|
||||
*.dylib
|
||||
|
||||
# Qt-es
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
130
CMakeLists.txt
130
CMakeLists.txt
|
@ -1,127 +1,15 @@
|
|||
cmake_minimum_required(VERSION 3.21)
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
project(ProtocolParser_APP VERSION 1.0.0.0 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
if(APPLE)
|
||||
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)
|
||||
endif()
|
||||
project(ProtocolParser_APP VERSION 0.1 LANGUAGES CXX)
|
||||
|
||||
add_definitions(-DFRAMELESSHELPER_CORE_NO_DEBUG_OUTPUT)
|
||||
add_definitions(-DFRAMELESSHELPER_QUICK_NO_DEBUG_OUTPUT)
|
||||
set(FRAMELESSHELPER_BUILD_STATIC ON)
|
||||
set(FRAMELESSHELPER_BUILD_WIDGETS OFF)
|
||||
set(FRAMELESSHELPER_NO_SUMMARY OFF)
|
||||
set(FRAMELESSHELPER_REPRODUCIBLE_OUTPUT ON)
|
||||
set(RIBBONUI_BUILD_FRAMELESSHEPLER ON)
|
||||
set(RIBBONUI_BUILD_EXAMPLES OFF)
|
||||
set(RIBBONUI_BUILD_STATIC_LIB ON)
|
||||
add_subdirectory(3rdparty/RibbonUI)
|
||||
|
||||
set(PROJECT_COMPANY "Mentalflow's Lab")
|
||||
set(PROJECT_COPYRIGHT "Copyright (c) 2023 Mentalflow's Lab. All rights reserved.")
|
||||
set(PROJECT_DOMAIN "dev.ourdocs.cn.protocolparser")
|
||||
set(PROJECT_BUNDLE_NAME ProtocolParser)
|
||||
|
||||
add_definitions(-DPROTOCOLPARSER_VERSION=1,0,0,0)
|
||||
|
||||
find_package(Qt6 COMPONENTS Quick SerialPort LinguistTools REQUIRED)
|
||||
|
||||
set(
|
||||
sources_files source/main.cpp include/serialportmanager.h source/serialportmanager.cpp include/eventsbus.h source/eventsbus.cpp
|
||||
dlln3x/DLLN3X.h dlln3x/DLLN3X.cpp dlln3x/zigbeeframe.h
|
||||
include/zigbeeparser.h source/zigbeeparser.cpp source/crypto.cpp include/crypto.h
|
||||
include/protocol.h source/protocol.cpp include/protocol_data.h
|
||||
include/config.h source/config.cpp
|
||||
languages/zh_CN.ts languages/en_US.ts
|
||||
# dlln3x_handler.h dlln3x_handler.cpp
|
||||
)
|
||||
|
||||
INCLUDE_DIRECTORIES(dlln3x include)
|
||||
add_subdirectory(sm_crypto)
|
||||
|
||||
if (WIN32)
|
||||
set(app_icon_resource_windows resources/icon.rc)
|
||||
qt_add_executable(${PROJECT_NAME}
|
||||
${sources_files}
|
||||
resources/icon.rc
|
||||
)
|
||||
file(TO_CMAKE_PATH "/" PATH_SEPARATOR)
|
||||
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||
set(DLLPATH 3rdparty/msvc/*.dll)
|
||||
else()
|
||||
set(DLLPATH 3rdparty/mingw/*.dll)
|
||||
endif()
|
||||
string(REPLACE "/" ${PATH_SEPARATOR} DLLPATH "${DLLPATH}")
|
||||
file(GLOB DLL_FILES ${DLLPATH})
|
||||
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${DLL_FILES}
|
||||
"${CMAKE_BINARY_DIR}${PATH_SEPARATOR}${PROJECT_NAME}"
|
||||
)
|
||||
elseif(APPLE)
|
||||
set(MACOSX_BUNDLE_ICON_FILE AppIcon)
|
||||
set(App_ICON "resources/imgs/AppIcon.icns")
|
||||
set_source_files_properties(${App_ICON} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
|
||||
qt_add_executable(${PROJECT_NAME}
|
||||
${sources_files}
|
||||
${App_ICON}
|
||||
)
|
||||
else ()
|
||||
qt_add_executable(${PROJECT_NAME}
|
||||
${sources_files}
|
||||
)
|
||||
endif ()
|
||||
|
||||
qt_add_qml_module(${PROJECT_NAME}
|
||||
URI ${PROJECT_BUNDLE_NAME}
|
||||
VERSION 1.0
|
||||
QML_FILES qml/Main.qml qml/components/RibbonWindow.qml qml/components/ZigBeeFrame.qml
|
||||
qml/components/RibbonMessageListView.qml qml/components/RibbonMessage.qml
|
||||
qml/components/ZigBeeMessage.qml qml/components/SerialPortAssistant.qml
|
||||
qml/components/ZigBeeDataView.qml qml/components/TabBar.qml qml/components/CenterView.qml
|
||||
qml/components/ListTable.qml qml/components/DeviceList.qml qml/components/KeysList.qml
|
||||
qml/components/EventsHistoryList.qml
|
||||
3rdparty/RibbonUI/lib_source/qml/RibbonLineEdit.qml 3rdparty/RibbonUI/lib_source/qml/RibbonTabBar.qml
|
||||
3rdparty/RibbonUI/lib_source/qml/RibbonSwitchButton.qml 3rdparty/RibbonUI/lib_source/qml/RibbonTitleBar.qml
|
||||
3rdparty/RibbonUI/lib_source/qml/RibbonTextBoxMenu.qml
|
||||
)
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
MACOSX_BUNDLE_GUI_IDENTIFIER ${PROJECT_DOMAIN}
|
||||
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
|
||||
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
|
||||
MACOSX_BUNDLE_BUNDLE_NAME ${PROJECT_BUNDLE_NAME}
|
||||
MACOSX_BUNDLE_COPYRIGHT ${PROJECT_COPYRIGHT}
|
||||
MACOSX_BUNDLE TRUE
|
||||
WIN32_EXECUTABLE TRUE
|
||||
OUTPUT_NAME ${PROJECT_BUNDLE_NAME}
|
||||
)
|
||||
|
||||
qt_add_translations(${PROJECT_NAME}
|
||||
TS_FILES languages/zh_CN.ts languages/en_US.ts
|
||||
RESOURCE_PREFIX "/translations"
|
||||
)
|
||||
|
||||
if(RIBBONUI_BUILD_STATIC_LIB)
|
||||
add_definitions(-DRIBBONUI_BUILD_STATIC_LIB)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
Qt::Quick
|
||||
Qt::SerialPort
|
||||
RibbonUIplugin
|
||||
sm_crypto
|
||||
FramelessHelper::Core
|
||||
FramelessHelper::Quick
|
||||
)
|
||||
|
||||
else()
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
Qt::Quick
|
||||
Qt::SerialPort
|
||||
RibbonUI
|
||||
sm_crypto
|
||||
FramelessHelper::Core
|
||||
FramelessHelper::Quick
|
||||
)
|
||||
|
||||
endif()
|
||||
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_lupdate)
|
||||
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_lrelease)
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_subdirectory(app_source)
|
||||
|
|
|
@ -0,0 +1,119 @@
|
|||
cmake_minimum_required(VERSION 3.21)
|
||||
|
||||
project(ProtocolParser VERSION 1.0.0.0 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
if(APPLE)
|
||||
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)
|
||||
endif()
|
||||
|
||||
set(PROJECT_COMPANY "Mentalflow's Lab")
|
||||
set(PROJECT_COPYRIGHT "Copyright (c) 2023 Mentalflow's Lab. All rights reserved.")
|
||||
set(PROJECT_DOMAIN "dev.ourdocs.cn.protocolparser")
|
||||
set(PROJECT_BUNDLE_NAME ${PROJECT_NAME})
|
||||
|
||||
add_definitions(-DPROTOCOLPARSER_VERSION=1,0,0,0)
|
||||
|
||||
find_package(Qt6 COMPONENTS Quick SerialPort LinguistTools REQUIRED)
|
||||
|
||||
set(
|
||||
sources_files source/main.cpp include/serialportmanager.h source/serialportmanager.cpp include/eventsbus.h source/eventsbus.cpp
|
||||
dlln3x/DLLN3X.h dlln3x/DLLN3X.cpp dlln3x/zigbeeframe.h
|
||||
include/zigbeeparser.h source/zigbeeparser.cpp source/crypto.cpp include/crypto.h
|
||||
include/protocol.h source/protocol.cpp include/protocol_data.h
|
||||
include/config.h source/config.cpp
|
||||
languages/zh_CN.ts languages/en_US.ts
|
||||
# dlln3x_handler.h dlln3x_handler.cpp
|
||||
)
|
||||
|
||||
INCLUDE_DIRECTORIES(dlln3x include)
|
||||
add_subdirectory(sm_crypto)
|
||||
|
||||
if (WIN32)
|
||||
set(app_icon_resource_windows resources/icon.rc)
|
||||
qt_add_executable(${PROJECT_NAME}
|
||||
${sources_files}
|
||||
resources/icon.rc
|
||||
)
|
||||
file(TO_CMAKE_PATH "/" PATH_SEPARATOR)
|
||||
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||
set(DLLPATH 3rdparty/msvc/*.dll)
|
||||
else()
|
||||
set(DLLPATH 3rdparty/mingw/*.dll)
|
||||
endif()
|
||||
string(REPLACE "/" ${PATH_SEPARATOR} DLLPATH "${DLLPATH}")
|
||||
file(GLOB DLL_FILES ${DLLPATH})
|
||||
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${DLL_FILES}
|
||||
"${CMAKE_BINARY_DIR}${PATH_SEPARATOR}app_source"
|
||||
)
|
||||
elseif(APPLE)
|
||||
set(MACOSX_BUNDLE_ICON_FILE AppIcon)
|
||||
set(App_ICON "resources/imgs/AppIcon.icns")
|
||||
set_source_files_properties(${App_ICON} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
|
||||
qt_add_executable(${PROJECT_NAME}
|
||||
${sources_files}
|
||||
${App_ICON}
|
||||
)
|
||||
else ()
|
||||
qt_add_executable(${PROJECT_NAME}
|
||||
${sources_files}
|
||||
)
|
||||
endif ()
|
||||
|
||||
qt_add_qml_module(${PROJECT_NAME}
|
||||
URI ${PROJECT_NAME}
|
||||
VERSION 1.0
|
||||
QML_FILES qml/Main.qml qml/components/RibbonWindow.qml qml/components/ZigBeeFrame.qml
|
||||
qml/components/RibbonMessageListView.qml qml/components/RibbonMessage.qml
|
||||
qml/components/ZigBeeMessage.qml qml/components/SerialPortAssistant.qml
|
||||
qml/components/ZigBeeDataView.qml qml/components/TabBar.qml qml/components/CenterView.qml
|
||||
qml/components/ListTable.qml qml/components/DeviceList.qml qml/components/KeysList.qml
|
||||
qml/components/EventsHistoryList.qml
|
||||
)
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
MACOSX_BUNDLE_GUI_IDENTIFIER ${PROJECT_DOMAIN}
|
||||
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
|
||||
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
|
||||
MACOSX_BUNDLE_BUNDLE_NAME ${PROJECT_BUNDLE_NAME}
|
||||
MACOSX_BUNDLE_COPYRIGHT ${PROJECT_COPYRIGHT}
|
||||
MACOSX_BUNDLE TRUE
|
||||
WIN32_EXECUTABLE TRUE
|
||||
OUTPUT_NAME ${PROJECT_BUNDLE_NAME}
|
||||
)
|
||||
|
||||
qt_add_translations(${PROJECT_NAME}
|
||||
TS_FILES languages/zh_CN.ts languages/en_US.ts
|
||||
RESOURCE_PREFIX "/translations"
|
||||
)
|
||||
|
||||
if(RIBBONUI_BUILD_STATIC_LIB)
|
||||
add_definitions(-DRIBBONUI_BUILD_STATIC_LIB)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
Qt::Quick
|
||||
Qt::SerialPort
|
||||
RibbonUIplugin
|
||||
sm_crypto
|
||||
FramelessHelper::Core
|
||||
FramelessHelper::Quick
|
||||
)
|
||||
|
||||
else()
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
Qt::Quick
|
||||
Qt::SerialPort
|
||||
RibbonUI
|
||||
sm_crypto
|
||||
FramelessHelper::Core
|
||||
FramelessHelper::Quick
|
||||
)
|
||||
|
||||
endif()
|
||||
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_lupdate)
|
||||
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_lrelease)
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
|
@ -0,0 +1,336 @@
|
|||
#include "DLLN3X.h"
|
||||
#include <QTime>
|
||||
#include <QCoreApplication>
|
||||
|
||||
using namespace zigbee_protocol;
|
||||
|
||||
DLLN3X::DLLN3X(QObject *parent)
|
||||
: QObject{parent}
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
DLLN3X* DLLN3X::instance(){
|
||||
static DLLN3X instance;
|
||||
return &instance;
|
||||
}
|
||||
|
||||
void DLLN3X::init(QSerialPort* DSerial, uint32_t baudrate)
|
||||
{
|
||||
_DSerial = DSerial;
|
||||
uint8_t i = 0;
|
||||
for (; i < 7; i++)
|
||||
if (baudrate == _baud_rate_list[i])
|
||||
{
|
||||
i = 20;
|
||||
break;
|
||||
}
|
||||
if (i != 20)
|
||||
return;
|
||||
_DSerial->setBaudRate(baudrate);
|
||||
_DSerial->setDataBits(QSerialPort::Data8);
|
||||
_DSerial->setParity(QSerialPort::NoParity);
|
||||
_DSerial->setStopBits(QSerialPort::OneStop);
|
||||
if (!_DSerial->isOpen())
|
||||
{
|
||||
if (!_DSerial->open(QIODevice::ReadWrite))
|
||||
return;
|
||||
}
|
||||
rled_blink();
|
||||
read_addr();
|
||||
read_baudrate();
|
||||
read_channel();
|
||||
read_network_id();
|
||||
}
|
||||
|
||||
ZigbeeFrame DLLN3X::recv(bool non_blocked)
|
||||
{
|
||||
uint8_t buf[66] = "", length = 0;
|
||||
ZigbeeFrame zf;
|
||||
if (non_blocked) {
|
||||
if (_DSerial->bytesAvailable() < 6)
|
||||
return zf;
|
||||
}
|
||||
_recv_lock = true;
|
||||
length = readBytesUntil(0xFF, buf, 65);
|
||||
_recv_lock = false;
|
||||
zf.load_package(buf, length);
|
||||
return zf;
|
||||
}
|
||||
|
||||
bool DLLN3X::send(ZigbeeFrame zf)
|
||||
{
|
||||
bool status = false;
|
||||
if (zf.getSrcPort() < 0x80)
|
||||
return false;
|
||||
status = _DSerial->write((char *)zf.data(),zf.size());
|
||||
return status;
|
||||
}
|
||||
|
||||
bool DLLN3X::send_cmd(uint8_t des_port, uint8_t arg, uint8_t* data, uint8_t data_length)
|
||||
{
|
||||
ZigbeeFrame zf(0x80, des_port, 0x0000);
|
||||
if (data_length - 4 > 63 - 1)
|
||||
return false;
|
||||
zf.append(arg);
|
||||
zf.addData(data, data_length);
|
||||
return send(zf);
|
||||
}
|
||||
|
||||
void DLLN3X::rled_blink(uint8_t time)
|
||||
{
|
||||
uint8_t gap = 5;
|
||||
// for (int i = 0; i < time; i += gap) {
|
||||
// if (i % 2 != 0)
|
||||
// send_cmd(0x20, gap);
|
||||
// else
|
||||
// _sleep(gap * 250);
|
||||
// }
|
||||
send_cmd(0x20, gap);
|
||||
}
|
||||
|
||||
uint16_t DLLN3X::read_addr()
|
||||
{
|
||||
if (_self_addr != 0)
|
||||
return _self_addr;
|
||||
_self_addr = rw_config(CONFIG::ADDR);
|
||||
return _self_addr;
|
||||
}
|
||||
|
||||
uint8_t DLLN3X::set_addr(uint16_t addr)
|
||||
{
|
||||
uint8_t resp = rw_config(CONFIG::ADDR, addr, CONFIG_RW_MASK::WRITE);
|
||||
if (resp == CONFIG_RESPONSE::DONE)
|
||||
{
|
||||
_self_addr = addr;
|
||||
soft_reboot();
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
uint32_t DLLN3X::read_baudrate()
|
||||
{
|
||||
if (_self_baud_rate != 0)
|
||||
return _self_baud_rate;
|
||||
uint8_t arg = rw_config(CONFIG::BAUDRATE);
|
||||
_self_baud_rate = _baud_rate_list[arg];
|
||||
return _self_baud_rate;
|
||||
}
|
||||
|
||||
uint8_t DLLN3X::set_baudrate(uint32_t baud_rate)
|
||||
{
|
||||
uint8_t arg = 0;
|
||||
uint8_t i = 0;
|
||||
for (; i < 13; i++)
|
||||
{
|
||||
if (baud_rate == _baud_rate_list[i])
|
||||
{
|
||||
arg = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (arg != i)
|
||||
return CONFIG_RESPONSE::CMD_ERROR;
|
||||
uint8_t resp = rw_config(CONFIG::BAUDRATE, arg, CONFIG_RW_MASK::WRITE);
|
||||
if (resp == CONFIG_RESPONSE::DONE)
|
||||
{
|
||||
_self_baud_rate = baud_rate;
|
||||
soft_reboot();
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
uint8_t DLLN3X::read_network_id()
|
||||
{
|
||||
if (_self_network_id != 0)
|
||||
return _self_network_id;
|
||||
_self_network_id = rw_config(CONFIG::NETWORKID);
|
||||
return _self_network_id;
|
||||
}
|
||||
|
||||
uint8_t DLLN3X::set_network_id(uint16_t network_id)
|
||||
{
|
||||
uint8_t resp = rw_config(CONFIG::NETWORKID, network_id, CONFIG_RW_MASK::WRITE);
|
||||
if (resp == CONFIG_RESPONSE::DONE)
|
||||
{
|
||||
_self_network_id = network_id;
|
||||
soft_reboot();
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
uint8_t DLLN3X::read_channel()
|
||||
{
|
||||
if (_self_channel != 0)
|
||||
return _self_channel;
|
||||
_self_channel = rw_config(CONFIG::CHANNEL);
|
||||
return _self_channel;
|
||||
}
|
||||
|
||||
uint8_t DLLN3X::set_channel(uint8_t channel)
|
||||
{
|
||||
uint8_t resp = rw_config(CONFIG::CHANNEL, channel, CONFIG_RW_MASK::WRITE);
|
||||
if (resp == CONFIG_RESPONSE::DONE)
|
||||
{
|
||||
_self_channel = channel;
|
||||
soft_reboot();
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
uint16_t DLLN3X::rw_config(CONFIG arg, uint16_t data, CONFIG_RW_MASK mask)
|
||||
{
|
||||
uint8_t cmd_arg = arg|mask;
|
||||
if (mask == CONFIG_RW_MASK::WRITE)
|
||||
{
|
||||
if (arg == CONFIG::ADDR || arg == CONFIG::NETWORKID)
|
||||
{
|
||||
send_cmd(0x21, cmd_arg, (uint8_t *)&data, 2);
|
||||
}
|
||||
else
|
||||
send_cmd(0x21, cmd_arg, (uint8_t *)&data, 1);
|
||||
}
|
||||
else
|
||||
send_cmd(0x21, cmd_arg);
|
||||
ZigbeeFrame zf = recv(false);
|
||||
if (zf.getSrcPort() != 0x21 || zf.getDesPort() != 0x80 || zf.getRemoteAddr() != 0x0000)
|
||||
return 0;
|
||||
switch (arg) {
|
||||
case CONFIG::ADDR: {
|
||||
if (zf.getData()[0] != 0x21) {
|
||||
if (zf.getData()[0] != CONFIG_RESPONSE::DONE)
|
||||
{
|
||||
printf("DLLN3X write config error: 0x");
|
||||
printf("%X\n", zf.getData()[0]);
|
||||
}
|
||||
return zf.getData()[0];
|
||||
} else
|
||||
return (zf.getData()[2] << 8) | zf.getData()[1];
|
||||
}
|
||||
case CONFIG::NETWORKID: {
|
||||
if (zf.getData()[0] != 0x22) {
|
||||
if (zf.getData()[0] != CONFIG_RESPONSE::DONE)
|
||||
{
|
||||
printf("DLLN3X write config error: 0x");
|
||||
printf("%X\n", zf.getData()[0]);
|
||||
}
|
||||
return zf.getData()[0];
|
||||
} else
|
||||
return (zf.getData()[2] << 8) | zf.getData()[1];
|
||||
}
|
||||
case CONFIG::CHANNEL: {
|
||||
if (zf.getData()[0] != 0x23) {
|
||||
if (zf.getData()[0] != CONFIG_RESPONSE::DONE)
|
||||
{
|
||||
printf("DLLN3X write config error: 0x");
|
||||
printf("%X\n", zf.getData()[0]);
|
||||
}
|
||||
return zf.getData()[0];
|
||||
} else
|
||||
return zf.getData()[1];
|
||||
}
|
||||
case CONFIG::BAUDRATE: {
|
||||
if (zf.getData()[0] != 0x24) {
|
||||
if (zf.getData()[0] != CONFIG_RESPONSE::DONE)
|
||||
{
|
||||
printf("DLLN3X write config error: 0x");
|
||||
printf("%X\n", zf.getData()[0]);
|
||||
}
|
||||
return zf.getData()[0];
|
||||
} else
|
||||
return zf.getData()[1];
|
||||
}
|
||||
default:
|
||||
return CONFIG_RESPONSE::CMD_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
void DLLN3X::soft_reboot()
|
||||
{
|
||||
send_cmd(0x21, CONFIG::SOFT_REBOOT);
|
||||
_self_addr = 0;
|
||||
_self_baud_rate = 0;
|
||||
_self_channel = 0;
|
||||
_self_network_id = 0;
|
||||
}
|
||||
|
||||
uint8_t DLLN3X::get_link_quality(uint16_t des_addr, uint16_t src_addr)
|
||||
{
|
||||
if (des_addr == 0x0000 || des_addr == 0xFFFF)
|
||||
return CONFIG_RESPONSE::CMD_ERROR;
|
||||
ZigbeeFrame zf(0x80, 0x23, src_addr), r_zf;
|
||||
zf.addData((uint8_t *)&des_addr, 2);
|
||||
r_zf = recv(false);
|
||||
uint16_t pkg_des_addr = (r_zf.getData()[1] << 8) | r_zf.getData()[0];
|
||||
if (pkg_des_addr != des_addr)
|
||||
return CONFIG_RESPONSE::PKG_DATA_ERROR;
|
||||
else
|
||||
return r_zf.getData()[2];
|
||||
}
|
||||
|
||||
enum DLLN3X::PIN_CONTROL DLLN3X::pin_control(PIN pin, PIN_CONTROL cmd)
|
||||
{
|
||||
send_cmd(pin, cmd);
|
||||
if (cmd == PIN_CONTROL::READ_PIN)
|
||||
{
|
||||
ZigbeeFrame zf = recv(false);
|
||||
return PIN_CONTROL(zf.getData()[0] ^ 0x10);
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
void DLLN3X::loop()
|
||||
{
|
||||
if (_DSerial->bytesAvailable() > 7 && !_recv_lock) {
|
||||
ZigbeeFrame zf = recv();
|
||||
printf("Message: ");
|
||||
for (int i = 0; i < zf.size(); i++) {
|
||||
printf("%02X ", zf[i]);
|
||||
}
|
||||
printf("at port %02X from %04X:%02X.\n", zf.getDesPort(), zf.getRemoteAddr(), zf.getSrcPort());
|
||||
if (zf.getSrcPort() == 0x22)
|
||||
{
|
||||
printf("DLLN3X send msg error: 0x");
|
||||
printf("%X",zf.getData()[0]);
|
||||
printf(", Send data to incorrect port: 0x");
|
||||
printf("%X\n",zf.getData()[1]);
|
||||
}
|
||||
emit recved(zf);
|
||||
}
|
||||
}
|
||||
|
||||
int DLLN3X::readBytesUntil(uint8_t delimiter, uint8_t* buffer, qint64 maxSize)
|
||||
{
|
||||
QByteArray data;
|
||||
qint64 bytesRead = 0;
|
||||
|
||||
while (bytesRead < maxSize)
|
||||
{
|
||||
if (_DSerial->bytesAvailable())
|
||||
{
|
||||
qint64 bytesReadNow = _DSerial->read((char *)(buffer) + bytesRead, 1);
|
||||
bytesRead += bytesReadNow;
|
||||
|
||||
data.append((char *)(buffer) + bytesRead - bytesReadNow, bytesReadNow);
|
||||
|
||||
if (data.contains(delimiter))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
void DLLN3X::_sleep(int msec)
|
||||
{
|
||||
QTime dieTime = QTime::currentTime().addMSecs(msec);
|
||||
while( QTime::currentTime() < dieTime )
|
||||
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
||||
}
|
||||
|
||||
void DLLN3X::setcallback(void (*callback)(ZigbeeFrame& zf)) { _callback = callback; }
|
|
@ -0,0 +1,62 @@
|
|||
#ifndef _DLLN3X_H_
|
||||
#define _DLLN3X_H_
|
||||
|
||||
#include <QObject>
|
||||
#include <QSerialPort>
|
||||
#include "zigbeeframe.h"
|
||||
|
||||
namespace zigbee_protocol {
|
||||
class DLLN3X : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private:
|
||||
bool _online = false, _recv_lock = false;
|
||||
uint8_t _self_channel = 0;
|
||||
uint16_t _self_addr = 0x0000, _self_network_id = 0x0000;
|
||||
uint32_t _self_baud_rate = 0;
|
||||
const QSerialPort::BaudRate _baud_rate_list[7] = {QSerialPort::Baud2400, QSerialPort::Baud4800, QSerialPort::Baud9600,
|
||||
QSerialPort::Baud19200, QSerialPort::Baud38400, QSerialPort::Baud57600, QSerialPort::Baud115200};
|
||||
QSerialPort* _DSerial;
|
||||
void (*_callback)(ZigbeeFrame& zf) = nullptr;
|
||||
int readBytesUntil(uint8_t delimiter, uint8_t* buf, qint64 maxSize);
|
||||
void _sleep(int msec);
|
||||
|
||||
public:
|
||||
enum CONFIG_RW_MASK {READ = 0x00, WRITE = 0x10};
|
||||
enum CONFIG { ADDR = 0x01, NETWORKID = 0x02, CHANNEL = 0x03, BAUDRATE = 0x04 , SOFT_REBOOT = 0x10};
|
||||
enum CONFIG_RESPONSE {
|
||||
DONE = 0x00,
|
||||
PORT_REMOTE_ACCESS_DENIED = 0xF0,
|
||||
CMD_ERROR = 0xF8,
|
||||
PKG_LENGTH_ERROR = 0xF9,
|
||||
PKG_DATA_ERROR = 0xFA
|
||||
};
|
||||
enum PIN_CONTROL{ OUT_HIGH = 0x11, OUT_LOW = 0x10, READ_PIN = 0x12};
|
||||
enum PIN{ DLLN3X_PIN4 = 0x44, DLLN3X_PIN5 = 0x45};
|
||||
explicit DLLN3X(QObject *parent = nullptr);
|
||||
static DLLN3X* instance();
|
||||
void init(QSerialPort* DSerial, uint32_t baudrate = 115200);
|
||||
ZigbeeFrame recv(bool non_blocked = true);
|
||||
bool send(ZigbeeFrame zf);
|
||||
bool send_cmd(uint8_t des_port, uint8_t arg, uint8_t* data = nullptr, uint8_t data_length = 0);
|
||||
void rled_blink(uint8_t time = 50);
|
||||
void loop();
|
||||
void setcallback(void (*callback)(ZigbeeFrame& zf));
|
||||
uint16_t rw_config(CONFIG arg, uint16_t data = 0, CONFIG_RW_MASK mask = CONFIG_RW_MASK::READ);
|
||||
uint8_t read_network_id();
|
||||
uint8_t set_network_id(uint16_t network_id);
|
||||
uint8_t read_channel();
|
||||
uint8_t set_channel(uint8_t channel);
|
||||
uint16_t read_addr();
|
||||
uint8_t set_addr(uint16_t addr);
|
||||
uint32_t read_baudrate();
|
||||
uint8_t set_baudrate(uint32_t baud_rate);
|
||||
void soft_reboot();
|
||||
uint8_t get_link_quality(uint16_t des_addr, uint16_t src_addr = 0x0000);
|
||||
enum PIN_CONTROL pin_control(PIN pin, PIN_CONTROL cmd);
|
||||
|
||||
signals:
|
||||
void recved(ZigbeeFrame zf);
|
||||
};
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,247 @@
|
|||
#ifndef _ZIGBEEFRAME_H_
|
||||
#define _ZIGBEEFRAME_H_
|
||||
#include <QObject>
|
||||
#include <QSerialPort>
|
||||
|
||||
namespace zigbee_protocol {
|
||||
class ZigbeeFrame {
|
||||
private:
|
||||
const uint8_t head = 0xFE, tail = 0xFF;
|
||||
uint8_t _length = 0, _src_port = 0, _des_port = 0;
|
||||
uint8_t _pack_len = 0;
|
||||
uint16_t _remote_addr = 0;
|
||||
QList<uint8_t> _data, _packed_data, _package;
|
||||
|
||||
public:
|
||||
ZigbeeFrame(char* data) { setData((uint8_t*)data, strlen(data)); };
|
||||
ZigbeeFrame(char* data, uint8_t length) { setData((uint8_t*)data, length); };
|
||||
ZigbeeFrame(uint8_t* data, uint8_t length) { setData(data, length); };
|
||||
ZigbeeFrame(uint8_t src_port, uint8_t des_port, uint16_t remote_addr, uint8_t* data, uint8_t data_length)
|
||||
{
|
||||
_length = data_length + 4;
|
||||
_src_port = src_port;
|
||||
_des_port = des_port;
|
||||
_remote_addr = remote_addr;
|
||||
setData(data, data_length);
|
||||
};
|
||||
ZigbeeFrame(uint8_t src_port, uint8_t des_port, uint16_t remote_addr, char* data, uint8_t data_length)
|
||||
{
|
||||
_length = data_length + 4;
|
||||
_src_port = src_port;
|
||||
_des_port = des_port;
|
||||
_remote_addr = remote_addr;
|
||||
setData((uint8_t*)data, data_length);
|
||||
};
|
||||
ZigbeeFrame(uint8_t src_port, uint8_t des_port, uint16_t remote_addr)
|
||||
{
|
||||
_src_port = src_port;
|
||||
_des_port = des_port;
|
||||
_remote_addr = remote_addr;
|
||||
};
|
||||
ZigbeeFrame(const ZigbeeFrame& zf)
|
||||
{
|
||||
_length = zf._length;
|
||||
_src_port = zf._src_port;
|
||||
_des_port = zf._des_port;
|
||||
_pack_len = zf._pack_len;
|
||||
_remote_addr = zf._remote_addr;
|
||||
_data = zf._data;
|
||||
_packed_data = zf._packed_data;
|
||||
_package = zf._package;
|
||||
};
|
||||
ZigbeeFrame(const ZigbeeFrame* zf)
|
||||
{
|
||||
_length = zf->_length;
|
||||
_src_port = zf->_src_port;
|
||||
_des_port = zf->_des_port;
|
||||
_pack_len = zf->_pack_len;
|
||||
_remote_addr = zf->_remote_addr;
|
||||
_data = zf->_data;
|
||||
_packed_data = zf->_packed_data;
|
||||
_package = zf->_package;
|
||||
};
|
||||
ZigbeeFrame() {};
|
||||
void setDesPort(uint8_t des_port) { _des_port = des_port; };
|
||||
void setSrcPort(uint8_t src_port) { _src_port = src_port; };
|
||||
void setRemoteAddr(uint16_t remote_addr) { _remote_addr = remote_addr; };
|
||||
uint8_t getDesPort() { return _des_port; };
|
||||
uint8_t getSrcPort() { return _src_port; };
|
||||
uint16_t getRemoteAddr() { return _remote_addr; };
|
||||
uint8_t getLength() { return _data.size() + 4; };
|
||||
uint8_t getDataLength() { return _data.size(); };
|
||||
QList<uint8_t>& getData() { return _data; };
|
||||
void setData(uint8_t* data, uint8_t length)
|
||||
{
|
||||
_data.clear();
|
||||
addData(data, length);
|
||||
};
|
||||
void setData(char* data) { setData((uint8_t*)data, strlen(data)); };
|
||||
void setData(char* data, uint8_t length) { setData((uint8_t*)data, length); };
|
||||
void setData(QList<uint8_t> data) { _data = data; };
|
||||
void addData(char* data) { addData(data, strlen(data)); };
|
||||
void addData(uint8_t* data, uint8_t length)
|
||||
{
|
||||
for (uint8_t i = 0; i < length; i++) {
|
||||
_data.append(data[i]);
|
||||
}
|
||||
};
|
||||
void addData(char* data, uint8_t length) { addData((uint8_t*)data, length); };
|
||||
void addData(QList<uint8_t> data) { _data.append(data); };
|
||||
void clear()
|
||||
{
|
||||
_package.clear();
|
||||
_packed_data.clear();
|
||||
_data.clear();
|
||||
};
|
||||
const uint8_t* data()
|
||||
{
|
||||
get_package();
|
||||
return _package.constData();
|
||||
};
|
||||
uint8_t data_size() { return _data.size(); };
|
||||
uint8_t size()
|
||||
{
|
||||
get_package();
|
||||
return _package.size();
|
||||
};
|
||||
void append(uint8_t byte)
|
||||
{
|
||||
_data.append(byte);
|
||||
_length = _data.size() + 4;
|
||||
};
|
||||
void pack() { pack(_data, _packed_data); };
|
||||
void pack(QList<uint8_t> data, QList<uint8_t>& pack_data)
|
||||
{
|
||||
pack_data.clear();
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
if (data[i] == 0xFE) {
|
||||
pack_data.append(data[i]);
|
||||
pack_data.append(0xFC);
|
||||
} else if (data[i] == 0xFF) {
|
||||
pack_data.append(0xFE);
|
||||
pack_data.append(0xFD);
|
||||
} else {
|
||||
pack_data.append(data[i]);
|
||||
}
|
||||
}
|
||||
};
|
||||
void depack() { depack(_data, _packed_data); };
|
||||
void depack(QList<uint8_t>& data, QList<uint8_t> pack_data)
|
||||
{
|
||||
data.clear();
|
||||
for (int i = 0; i < pack_data.size(); i++) {
|
||||
if (pack_data[i] != 0xFE) {
|
||||
data.append(pack_data[i]);
|
||||
} else if (pack_data[i + 1] == 0xFD) {
|
||||
data.append(0xFF);
|
||||
i++;
|
||||
} else if (pack_data[i + 1] == 0xFC) {
|
||||
data.append(0xFE);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
};
|
||||
void make_package(uint8_t src_port, uint8_t des_port, uint16_t remote_addr, QList<uint8_t>& package,
|
||||
QList<uint8_t> data, QList<uint8_t>& pack_data)
|
||||
{
|
||||
pack(data, pack_data);
|
||||
package.clear();
|
||||
package.append(head);
|
||||
package.append(data.size() + 4);
|
||||
package.append(src_port);
|
||||
package.append(des_port);
|
||||
package.append(remote_addr & 0xFF);
|
||||
package.append((remote_addr >> 8) & 0xFF);
|
||||
package.append(pack_data);
|
||||
package.append(tail);
|
||||
};
|
||||
QList<uint8_t> get_package()
|
||||
{
|
||||
make_package(_src_port, _des_port, _remote_addr, _package, _data, _packed_data);
|
||||
return _package;
|
||||
};
|
||||
void load_package(QList<uint8_t>& buf) { load_package(buf.constData(), buf.size()); };
|
||||
void load_package(const uint8_t* buf, uint8_t length)
|
||||
{
|
||||
_packed_data.clear();
|
||||
for (uint8_t i = 0; i < length; i++) {
|
||||
_package.append(buf[i]);
|
||||
if (i > 5 && i < length - 1)
|
||||
_packed_data.append(buf[i]);
|
||||
}
|
||||
_length = _package[1];
|
||||
_src_port = _package[2];
|
||||
_des_port = _package[3];
|
||||
_remote_addr = _package[4] | (_package[5] << 8);
|
||||
depack(_data, _packed_data);
|
||||
};
|
||||
void print()
|
||||
{
|
||||
get_package();
|
||||
printf("src_port:");
|
||||
printf("%X\n", _src_port);
|
||||
printf("des_port:");
|
||||
printf("%X\n",_des_port);
|
||||
printf("remote_addr:");
|
||||
printf("%X\n",_remote_addr);
|
||||
printf("data:");
|
||||
for (uint8_t i = 0; i < _data.size(); i++) {
|
||||
printf("%02X ", _data[i]);
|
||||
}
|
||||
printf("\n");
|
||||
printf("packed_data:");
|
||||
for (uint8_t i = 0; i < _packed_data.size(); i++) {
|
||||
printf("%02X ", _packed_data[i]);
|
||||
}
|
||||
printf("\n");
|
||||
printf("package:");
|
||||
for (uint8_t i = 0; i < _package.size(); i++) {
|
||||
printf("%02X ", _package[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
ZigbeeFrame operator+(const ZigbeeFrame& zf)
|
||||
{
|
||||
ZigbeeFrame temp(this);
|
||||
temp.addData(zf._data);
|
||||
temp.get_package();
|
||||
return temp;
|
||||
};
|
||||
void operator+=(const ZigbeeFrame& zf)
|
||||
{
|
||||
addData(zf._data);
|
||||
get_package();
|
||||
};
|
||||
ZigbeeFrame& operator=(const ZigbeeFrame& zf)
|
||||
{
|
||||
if (this != &zf) {
|
||||
_length = zf._length;
|
||||
_src_port = zf._src_port;
|
||||
_des_port = zf._des_port;
|
||||
_pack_len = zf._pack_len;
|
||||
_remote_addr = zf._remote_addr;
|
||||
_data = zf._data;
|
||||
_packed_data = zf._packed_data;
|
||||
_package = zf._package;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
ZigbeeFrame operator+(char* data)
|
||||
{
|
||||
ZigbeeFrame temp(this);
|
||||
temp.addData(data);
|
||||
return temp;
|
||||
};
|
||||
uint8_t& operator[](uint8_t i)
|
||||
{
|
||||
get_package();
|
||||
return _package[i];
|
||||
};
|
||||
void operator+=(char* data)
|
||||
{
|
||||
addData(data, (uint8_t)strlen(data));
|
||||
get_package();
|
||||
};
|
||||
};
|
||||
}
|
||||
#endif
|
|
@ -4,7 +4,7 @@
|
|||
#include <QObject>
|
||||
#include <QQmlEngine>
|
||||
#include <QJsonObject>
|
||||
#include "3rdparty/RibbonUI/lib_source/definitions.h"
|
||||
#include "../3rdparty/RibbonUI/lib_source/definitions.h"
|
||||
|
||||
class Event : public QObject
|
||||
{
|
|
@ -6,7 +6,7 @@
|
|||
#include <QTimer>
|
||||
#include <QSerialPort>
|
||||
#include "DLLN3X.h"
|
||||
#include "3rdparty/RibbonUI/lib_source/definitions.h"
|
||||
#include "../3rdparty/RibbonUI/lib_source/definitions.h"
|
||||
#include "zigbeeparser.h"
|
||||
#include "eventsbus.h"
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
#include <QJsonObject>
|
||||
#include <QQueue>
|
||||
#include <zigbeeframe.h>
|
||||
#include "3rdparty/RibbonUI/lib_source/definitions.h"
|
||||
#include "../3rdparty/RibbonUI/lib_source/definitions.h"
|
||||
#include "protocol.h"
|
||||
#include "eventsbus.h"
|
||||
#include "config.h"
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,74 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Controls
|
||||
import QtQuick.Window
|
||||
import RibbonUI
|
||||
import ProtocolParser
|
||||
import org.wangwenx190.FramelessHelper
|
||||
import "components"
|
||||
|
||||
RibbonWindow {
|
||||
id:root
|
||||
width: 1200
|
||||
height: 800
|
||||
title: qsTr("Protocol Parser")
|
||||
|
||||
TabBar{
|
||||
id: tab_bar
|
||||
center_view: center_view
|
||||
}
|
||||
|
||||
CenterView{
|
||||
id: center_view
|
||||
z:-2
|
||||
anchors{
|
||||
top: parent.top
|
||||
bottom: parent.bottom
|
||||
}
|
||||
width:parent.width
|
||||
tab_bar: tab_bar
|
||||
bottom_bar: bottom_bar
|
||||
}
|
||||
|
||||
RibbonBottomBar{
|
||||
id: bottom_bar
|
||||
anchors{
|
||||
left: parent.left
|
||||
right: parent.right
|
||||
bottom: parent.bottom
|
||||
}
|
||||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
RibbonTheme.modern_style=true
|
||||
}
|
||||
|
||||
RibbonPopup{
|
||||
id:window_popup
|
||||
onClosed: window_popup.contentItem.destroy()
|
||||
}
|
||||
|
||||
function show_popup(content_url, argument)
|
||||
{
|
||||
let component = Qt.createComponent(content_url,window_popup)
|
||||
|
||||
if (component.status === Component.Error) {
|
||||
console.log(qsTr("Error loading component:"), component.errorString());
|
||||
return
|
||||
}
|
||||
else
|
||||
{
|
||||
if (typeof(argument)!='undefined')
|
||||
window_popup.contentItem = component.createObject(window_popup,argument)
|
||||
else
|
||||
window_popup.contentItem = component.createObject(window_popup)
|
||||
}
|
||||
window_popup.open()
|
||||
}
|
||||
|
||||
function close_popup()
|
||||
{
|
||||
window_popup.close()
|
||||
window_popup.contentItem.destroy()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Controls
|
||||
import RibbonUI
|
||||
import ProtocolParser
|
||||
|
||||
SwipeView{
|
||||
id: swipeview
|
||||
currentIndex: 0
|
||||
property var tab_bar
|
||||
property var bottom_bar
|
||||
property alias zigbee_dataview: zigbee_dataview
|
||||
property alias serial_assistant: serial_assistant
|
||||
|
||||
ZigBeeDataView{
|
||||
id: zigbee_dataview
|
||||
show_tooltip: tab_bar.show_tooltip
|
||||
auto_scroll_to_bottom: tab_bar.auto_scroll_to_bottom
|
||||
top_padding: tab_bar.height
|
||||
bottom_padding: bottom_bar.height
|
||||
handle_serial: swipeview.currentIndex === 0
|
||||
Rectangle{
|
||||
color: "transparent"
|
||||
anchors{
|
||||
right: parent.right
|
||||
verticalCenter: parent.verticalCenter
|
||||
}
|
||||
width: 30
|
||||
height: parent.height
|
||||
HoverHandler{
|
||||
id: right_chevron_area_hh
|
||||
}
|
||||
RibbonButton{
|
||||
anchors{
|
||||
right: parent.right
|
||||
verticalCenter: parent.verticalCenter
|
||||
}
|
||||
show_bg: false
|
||||
ribbon_icon.icon_size: 20
|
||||
tip_text: "前往串口助手"
|
||||
width: parent.width
|
||||
height: parent.height * 0.1
|
||||
icon_source: RibbonIcons.ChevronCircleRight
|
||||
visible: right_chevron_area_hh.hovered || hovered
|
||||
onClicked: swipeview.incrementCurrentIndex()
|
||||
}
|
||||
}
|
||||
}
|
||||
SerialPortAssistant{
|
||||
id: serial_assistant
|
||||
show_tooltip: tab_bar.show_tooltip
|
||||
auto_scroll_to_bottom: tab_bar.auto_scroll_to_bottom
|
||||
top_padding: tab_bar.height
|
||||
bottom_padding: bottom_bar.height
|
||||
handle_serial: swipeview.currentIndex === 1
|
||||
Rectangle{
|
||||
color: "transparent"
|
||||
anchors{
|
||||
left: parent.left
|
||||
verticalCenter: parent.verticalCenter
|
||||
}
|
||||
width: 30
|
||||
height: parent.height
|
||||
HoverHandler{
|
||||
id: left_chevron_area_hh
|
||||
}
|
||||
RibbonButton{
|
||||
anchors{
|
||||
left: parent.left
|
||||
verticalCenter: parent.verticalCenter
|
||||
}
|
||||
show_bg: false
|
||||
ribbon_icon.icon_size: 20
|
||||
tip_text: "前往ZigBee数据流"
|
||||
width: parent.width
|
||||
height: parent.height * 0.1
|
||||
icon_source: RibbonIcons.ChevronCircleLeft
|
||||
visible: left_chevron_area_hh.hovered || hovered
|
||||
onClicked: swipeview.decrementCurrentIndex()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,238 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Controls
|
||||
import RibbonUI
|
||||
import ProtocolParser
|
||||
|
||||
Item{
|
||||
id: obj
|
||||
property string title: list_type === 0 ? "等待列表" : list_type === 1 ? "认证列表" : "黑名单列表"
|
||||
property var header_items: ["序号","地址"]
|
||||
property var header_items_width: [100,200]
|
||||
property var data_source: []
|
||||
property int list_type: 0
|
||||
implicitHeight: list_layout.height
|
||||
implicitWidth: list_layout.width
|
||||
|
||||
onData_sourceChanged: {
|
||||
list.data_model.clear()
|
||||
list.data_model.append(data_source)
|
||||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
var data = []
|
||||
switch(list_type){
|
||||
case 0:
|
||||
{
|
||||
for (var i = 0; i < ZigBeeParser.wait_queue.length; i++)
|
||||
{
|
||||
data.push({"序号":i+1,"地址":'0x'+ZigBeeParser.wait_queue[i].toString(16).toUpperCase()})
|
||||
}
|
||||
break
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
for (let i = 0; i < ZigBeeParser.allow_list.length; i++)
|
||||
{
|
||||
data.push({"序号":i+1,"地址":'0x'+ZigBeeParser.allow_list[i].toString(16).toUpperCase()})
|
||||
}
|
||||
break
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
for (let i = 0; i < ZigBeeParser.deny_list.length; i++)
|
||||
{
|
||||
data.push({"序号":i+1,"地址":'0x'+ZigBeeParser.deny_list[i].toString(16).toUpperCase()})
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
data_source = data
|
||||
}
|
||||
|
||||
Connections{
|
||||
target: ZigBeeParser
|
||||
function onWait_queueChanged(){
|
||||
if (list_type !== 0)
|
||||
return
|
||||
let data = []
|
||||
for (let i = 0; i < ZigBeeParser.wait_queue.length; i++)
|
||||
{
|
||||
data.push({"序号":i+1,"地址":'0x'+ZigBeeParser.wait_queue[i].toString(16).toUpperCase()})
|
||||
}
|
||||
data_source = data
|
||||
}
|
||||
|
||||
function onAllow_listChanged(){
|
||||
if (list_type !== 1)
|
||||
return
|
||||
let data = []
|
||||
for (let i = 0; i < ZigBeeParser.allow_list.length; i++)
|
||||
{
|
||||
data.push({"序号":i+1,"地址":'0x'+ZigBeeParser.allow_list[i].toString(16).toUpperCase()})
|
||||
}
|
||||
data_source = data
|
||||
}
|
||||
|
||||
function onDeny_listChanged(){
|
||||
if (list_type !== 2)
|
||||
return
|
||||
let data = []
|
||||
for (let i = 0; i < ZigBeeParser.deny_list.length; i++)
|
||||
{
|
||||
data.push({"序号":i+1,"地址":'0x'+ZigBeeParser.deny_list[i].toString(16).toUpperCase()})
|
||||
}
|
||||
data_source = data
|
||||
}
|
||||
}
|
||||
|
||||
RibbonButton{
|
||||
anchors{
|
||||
top:parent.top
|
||||
margins: 4
|
||||
right:parent.right
|
||||
}
|
||||
show_bg: false
|
||||
show_hovered_bg: false
|
||||
icon_source: RibbonIcons.Dismiss
|
||||
onClicked: window_popup.close()
|
||||
}
|
||||
|
||||
ColumnLayout{
|
||||
id:list_layout
|
||||
anchors{
|
||||
bottom: parent.bottom
|
||||
horizontalCenter: parent.horizontalCenter
|
||||
}
|
||||
|
||||
RibbonText{
|
||||
id: title_text
|
||||
Layout.topMargin: 30
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
text: title
|
||||
view_only: true
|
||||
font.pixelSize: 15
|
||||
}
|
||||
|
||||
ListTable {
|
||||
id: list
|
||||
header_items: obj.header_items
|
||||
header_items_width: obj.header_items_width
|
||||
margins: 0
|
||||
Layout.topMargin: 10
|
||||
Layout.leftMargin: 30
|
||||
Layout.rightMargin: Layout.leftMargin
|
||||
Layout.bottomMargin: Layout.leftMargin
|
||||
Layout.preferredHeight: implicitHeight > 250 ? 250 : implicitHeight
|
||||
}
|
||||
|
||||
RowLayout{
|
||||
spacing: 30
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
Layout.bottomMargin: 30
|
||||
RibbonButton{
|
||||
show_tooltip: false
|
||||
icon_source: RibbonIcons.Add
|
||||
text: list_type !== 2 ? qsTr("加入黑名单") : qsTr("加入认证列表")
|
||||
enabled: list.has_selected
|
||||
onClicked: {
|
||||
let deny = ZigBeeParser.deny_list
|
||||
var data = []
|
||||
switch(list_type){
|
||||
case 0:
|
||||
{
|
||||
deny.push(ZigBeeParser.wait_queue[list.current_index])
|
||||
ZigBeeParser.deny_list = deny
|
||||
for (var i = 0; i < ZigBeeParser.wait_queue.length; i++)
|
||||
{
|
||||
if (i === list.current_index)
|
||||
continue
|
||||
data.push(ZigBeeParser.wait_queue[i])
|
||||
}
|
||||
ZigBeeParser.wait_queue = data
|
||||
break
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
deny.push(ZigBeeParser.allow_list[list.current_index])
|
||||
ZigBeeParser.deny_list = deny
|
||||
for (let i = 0; i < ZigBeeParser.allow_list.length; i++)
|
||||
{
|
||||
if (i === list.current_index)
|
||||
continue
|
||||
data.push(ZigBeeParser.allow_list[i])
|
||||
}
|
||||
ZigBeeParser.allow_list = data
|
||||
break
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
let allow = ZigBeeParser.allow_list
|
||||
allow.push(ZigBeeParser.deny_list[list.current_index])
|
||||
ZigBeeParser.allow_list = allow
|
||||
let data = []
|
||||
for (let i = 0; i < ZigBeeParser.deny_list.length; i++)
|
||||
{
|
||||
if (i === list.current_index)
|
||||
continue
|
||||
data.push(ZigBeeParser.deny_list[i])
|
||||
}
|
||||
ZigBeeParser.deny_list = data
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
RibbonButton{
|
||||
show_tooltip: false
|
||||
icon_source: list_type === 0 ? RibbonIcons.Add : RibbonIcons.Delete
|
||||
text: list_type === 0 ? qsTr("加入认证列表") : list_type === 1 ?
|
||||
qsTr("移出认证列表") : qsTr("移出黑名单列表")
|
||||
enabled: list.has_selected
|
||||
onClicked: {
|
||||
switch(list_type){
|
||||
case 0:
|
||||
{
|
||||
let allow = ZigBeeParser.allow_list
|
||||
allow.push(ZigBeeParser.wait_queue[list.current_index])
|
||||
ZigBeeParser.allow_list = allow
|
||||
var data = []
|
||||
for (var i = 0; i < ZigBeeParser.wait_queue.length; i++)
|
||||
{
|
||||
if (i === list.current_index)
|
||||
continue
|
||||
data.push(ZigBeeParser.wait_queue[i])
|
||||
}
|
||||
ZigBeeParser.wait_queue = data
|
||||
break
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
let data = []
|
||||
for (let i = 0; i < ZigBeeParser.allow_list.length; i++)
|
||||
{
|
||||
if (i === list.current_index)
|
||||
continue
|
||||
data.push(ZigBeeParser.allow_list[i])
|
||||
}
|
||||
ZigBeeParser.allow_list = data
|
||||
break
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
let data = []
|
||||
for (let i = 0; i < ZigBeeParser.deny_list.length; i++)
|
||||
{
|
||||
if (i === list.current_index)
|
||||
continue
|
||||
data.push(ZigBeeParser.deny_list[i])
|
||||
}
|
||||
ZigBeeParser.deny_list = data
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Controls
|
||||
import RibbonUI
|
||||
import ProtocolParser
|
||||
|
||||
Item {
|
||||
id:obj
|
||||
property var header_items: ["序号","事件"]
|
||||
property var header_items_width: [100,400]
|
||||
property var data_source: []
|
||||
implicitHeight: list_layout.height
|
||||
implicitWidth: list_layout.width
|
||||
|
||||
onData_sourceChanged: {
|
||||
list.data_model.clear()
|
||||
list.data_model.append(data_source)
|
||||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
var data = []
|
||||
for (var i = 0; i < EventsBus.event_history.length; i++)
|
||||
{
|
||||
data.push({"序号":i+1,"事件":JSON.stringify(EventsBus.event_history)})
|
||||
}
|
||||
data_source = data
|
||||
}
|
||||
|
||||
Connections{
|
||||
target: EventsBus
|
||||
function onEvent_historyChanged(){
|
||||
var data = []
|
||||
for (var i = 0; i < EventsBus.event_history.length; i++)
|
||||
{
|
||||
data.push({"序号":i+1,"事件":JSON.stringify(EventsBus.event_history)})
|
||||
}
|
||||
data_source = data
|
||||
}
|
||||
}
|
||||
|
||||
RibbonButton{
|
||||
anchors{
|
||||
top:parent.top
|
||||
margins: 4
|
||||
right:parent.right
|
||||
}
|
||||
show_bg: false
|
||||
show_hovered_bg: false
|
||||
icon_source: RibbonIcons.Dismiss
|
||||
onClicked: window_popup.close()
|
||||
}
|
||||
|
||||
ColumnLayout{
|
||||
id:list_layout
|
||||
anchors{
|
||||
bottom: parent.bottom
|
||||
horizontalCenter: parent.horizontalCenter
|
||||
}
|
||||
|
||||
RibbonText{
|
||||
id: title_text
|
||||
Layout.topMargin: 30
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
text: "事件列表"
|
||||
view_only: true
|
||||
font.pixelSize: 15
|
||||
}
|
||||
|
||||
ListTable {
|
||||
id: list
|
||||
header_items: obj.header_items
|
||||
header_items_width: obj.header_items_width
|
||||
margins: 0
|
||||
Layout.topMargin: 10
|
||||
Layout.leftMargin: 30
|
||||
Layout.rightMargin: Layout.leftMargin
|
||||
Layout.bottomMargin: Layout.leftMargin
|
||||
Layout.preferredHeight: implicitHeight > 250 ? 250 : implicitHeight
|
||||
}
|
||||
|
||||
RowLayout{
|
||||
spacing: 30
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
Layout.bottomMargin: 30
|
||||
RibbonButton{
|
||||
show_tooltip: false
|
||||
icon_source: RibbonIcons.HistoryDismiss
|
||||
text: qsTr("清空历史")
|
||||
enabled: EventsBus.event_history.length
|
||||
onClicked: {
|
||||
EventsBus.event_history = []
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,137 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Controls
|
||||
import RibbonUI
|
||||
import ProtocolParser
|
||||
|
||||
Item {
|
||||
id:obj
|
||||
property var header_items: ["序号","密钥"]
|
||||
property var header_items_width: [100,300]
|
||||
property var data_source: []
|
||||
implicitHeight: list_layout.height
|
||||
implicitWidth: list_layout.width
|
||||
|
||||
onData_sourceChanged: {
|
||||
list.data_model.clear()
|
||||
list.data_model.append(data_source)
|
||||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
var data = []
|
||||
for (var i = 0; i < ZigBeeParser.pre_hmac_verify_key.length; i++)
|
||||
{
|
||||
data.push({"序号":i+1,"密钥":key_format(ZigBeeParser.pre_hmac_verify_key[i])})
|
||||
}
|
||||
data_source = data
|
||||
}
|
||||
|
||||
Connections{
|
||||
target: ZigBeeParser
|
||||
function onPre_hmac_verify_keyChanged(){
|
||||
var data = []
|
||||
for (var i = 0; i < ZigBeeParser.pre_hmac_verify_key.length; i++)
|
||||
{
|
||||
data.push({"序号":i+1,"密钥":key_format(ZigBeeParser.pre_hmac_verify_key[i])})
|
||||
}
|
||||
data_source = data
|
||||
}
|
||||
}
|
||||
|
||||
RibbonButton{
|
||||
anchors{
|
||||
top:parent.top
|
||||
margins: 4
|
||||
right:parent.right
|
||||
}
|
||||
show_bg: false
|
||||
show_hovered_bg: false
|
||||
icon_source: RibbonIcons.Dismiss
|
||||
onClicked: window_popup.close()
|
||||
}
|
||||
|
||||
ColumnLayout{
|
||||
id:list_layout
|
||||
anchors{
|
||||
bottom: parent.bottom
|
||||
horizontalCenter: parent.horizontalCenter
|
||||
}
|
||||
|
||||
RibbonText{
|
||||
id: title_text
|
||||
Layout.topMargin: 30
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
text: "密钥列表"
|
||||
view_only: true
|
||||
font.pixelSize: 15
|
||||
}
|
||||
|
||||
RowLayout{
|
||||
Layout.preferredWidth: list.implicitWidth
|
||||
Layout.leftMargin: 30
|
||||
Layout.topMargin: 15
|
||||
RibbonText{
|
||||
id: c_key_text
|
||||
text: "当前密钥:"
|
||||
view_only: true
|
||||
}
|
||||
RibbonText{
|
||||
Layout.preferredWidth: implicitWidth > (parent.width - c_key_text.width) ? parent.width - c_key_text.width : implicitWidth
|
||||
text: key_format(ZigBeeParser.hmac_verify_key)
|
||||
font.bold: true
|
||||
wrapMode: RibbonText.WordWrap
|
||||
}
|
||||
}
|
||||
|
||||
RibbonText{
|
||||
text: `历史密钥(共${ZigBeeParser.pre_hmac_verify_key.length}个)`
|
||||
Layout.leftMargin: 30
|
||||
view_only: true
|
||||
}
|
||||
|
||||
ListTable {
|
||||
id: list
|
||||
header_items: obj.header_items
|
||||
header_items_width: obj.header_items_width
|
||||
margins: 0
|
||||
Layout.topMargin: 10
|
||||
Layout.leftMargin: 30
|
||||
Layout.rightMargin: Layout.leftMargin
|
||||
Layout.bottomMargin: Layout.leftMargin
|
||||
Layout.preferredHeight: implicitHeight > 250 ? 250 : implicitHeight
|
||||
}
|
||||
|
||||
RowLayout{
|
||||
spacing: 30
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
Layout.bottomMargin: 30
|
||||
RibbonButton{
|
||||
show_tooltip: false
|
||||
icon_source: RibbonIcons.Delete
|
||||
text: qsTr("删除")
|
||||
enabled: list.has_selected
|
||||
onClicked: {
|
||||
let data = []
|
||||
for (let i = 0; i < ZigBeeParser.pre_hmac_verify_key.length; i++)
|
||||
{
|
||||
if (i === list.current_index)
|
||||
continue
|
||||
data.push(ZigBeeParser.pre_hmac_verify_key[i])
|
||||
}
|
||||
ZigBeeParser.pre_hmac_verify_key = data
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function key_format(key)
|
||||
{
|
||||
let str = '0x'
|
||||
for (let i = 0; i < key.length; i++)
|
||||
{
|
||||
str += key[i]
|
||||
if ((i + 1) % 2 === 0 && (i + 1) !== key.length)
|
||||
str += ' 0x'
|
||||
}
|
||||
return str
|
||||
}
|
||||
}
|
|
@ -0,0 +1,141 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Controls
|
||||
import RibbonUI
|
||||
|
||||
Item {
|
||||
clip: true
|
||||
property var header_items:[]
|
||||
property var header_items_width:[]
|
||||
property int row_spacing: 1
|
||||
property double margins: 30
|
||||
property int has_selected: 0
|
||||
property alias data_model: list_model
|
||||
property alias current_index: list_table.currentIndex
|
||||
property double max_height: 250
|
||||
ListModel{
|
||||
id: list_model
|
||||
}
|
||||
|
||||
implicitHeight: (list_table.implicitHeight + list_table.anchors.margins * 2) > implicitWidth ?
|
||||
implicitWidth : (list_table.implicitHeight + list_table.anchors.margins * 2)
|
||||
implicitWidth: getWidth()
|
||||
|
||||
ListView{
|
||||
id: list_table
|
||||
anchors{
|
||||
top:parent.top
|
||||
horizontalCenter: parent.horizontalCenter
|
||||
margins: margins
|
||||
}
|
||||
implicitHeight: contentHeight > max_height ? max_height : contentHeight
|
||||
implicitWidth: parent.width - anchors.margins * 2
|
||||
clip: true
|
||||
model: list_model
|
||||
header: Item{
|
||||
implicitHeight: header_row.implicitHeight + 6
|
||||
implicitWidth: header_row.implicitWidth
|
||||
RowLayout{
|
||||
id:header_row
|
||||
spacing: row_spacing
|
||||
Repeater{
|
||||
model: header_items
|
||||
RibbonRectangle{
|
||||
implicitHeight: head.implicitHeight + 10
|
||||
implicitWidth: header_items_width[index]
|
||||
topLeftRadius: index == 0 ? 5 : 0
|
||||
bottomLeftRadius: index == 0 ? 5 : 0
|
||||
topRightRadius: index == (header_items.length - 1) ? 5 : 0
|
||||
bottomRightRadius: index == (header_items.length - 1) ? 5 : 0
|
||||
color: RibbonTheme.dark_mode ? "#5E5C5B" : "#CBCAC9"
|
||||
RibbonText{
|
||||
id:head
|
||||
anchors.centerIn: parent
|
||||
width: implicitWidth > parent.width - 10 ? parent.width - 10 : implicitWidth
|
||||
text: modelData
|
||||
wrapMode: RibbonText.WordWrap
|
||||
view_only: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delegate: Item{
|
||||
implicitHeight: row_bg.implicitHeight + 6
|
||||
implicitWidth: row_bg.implicitWidth
|
||||
Rectangle{
|
||||
id: row_bg
|
||||
implicitHeight: row.implicitHeight
|
||||
implicitWidth: row.implicitWidth
|
||||
radius: 5
|
||||
color: mouse.containsMouse || row_bg.selected ? "#506BBD" : "transparent"
|
||||
property bool selected: false
|
||||
property bool is_focused: list_table.currentIndex === index
|
||||
onIs_focusedChanged: {
|
||||
if (!is_focused && selected)
|
||||
row_bg.selected = !row_bg.selected
|
||||
}
|
||||
onSelectedChanged: has_selected += row_bg.selected ? 1 : -1
|
||||
RowLayout{
|
||||
id: row
|
||||
spacing: row_spacing
|
||||
Repeater{
|
||||
model: getRowValues(index)
|
||||
RibbonRectangle{
|
||||
implicitHeight: text.implicitHeight + 10
|
||||
implicitWidth: modelData.width
|
||||
color: "transparent"
|
||||
RibbonText{
|
||||
id:text
|
||||
anchors.centerIn: parent
|
||||
width: implicitWidth > parent.width - 10 ? parent.width - 10 : implicitWidth
|
||||
view_only: true
|
||||
text: modelData.data
|
||||
wrapMode: RibbonText.WordWrap
|
||||
color: RibbonTheme.dark_mode ? mouse.containsMouse ? 'black' : 'white' : mouse.containsMouse ? 'white':'black'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
MouseArea{
|
||||
id: mouse
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
onClicked:
|
||||
{
|
||||
list_table.currentIndex = index
|
||||
row_bg.selected = !row_bg.selected
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
add: Transition {
|
||||
NumberAnimation { properties: "x,y"; from: list_table.height; duration: 1000 }
|
||||
}
|
||||
}
|
||||
|
||||
function getRowValues(index) {
|
||||
var obj = list_model.get(index)
|
||||
if(!obj)
|
||||
return
|
||||
var data = []
|
||||
|
||||
for(let i=0;i<header_items.length;i++){
|
||||
data.push({data:obj[header_items[i]],width:header_items_width[i]})
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
function getWidth(){
|
||||
let width = 0;
|
||||
for (let i=0;i<header_items_width.length;i++)
|
||||
{
|
||||
width += header_items_width[i]
|
||||
}
|
||||
width += list_table.anchors.margins * 2
|
||||
width += (header_items_width.length - 1) * row_spacing
|
||||
return width
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Controls
|
||||
import RibbonUI
|
||||
|
||||
Rectangle{
|
||||
id: bubble
|
||||
color: "transparent"
|
||||
property double padding: 10
|
||||
default property alias content: message_layout.data
|
||||
property var data_model:model
|
||||
property int font_size: 13
|
||||
property string sender_text: "sender"
|
||||
width: ListView.view.width
|
||||
height: bubble_layout.height + padding*2
|
||||
ColumnLayout{
|
||||
id: bubble_layout
|
||||
anchors{
|
||||
top: parent.top
|
||||
topMargin: parent.padding
|
||||
}
|
||||
layoutDirection: data_model.recieved ? Qt.LeftToRight : Qt.RightToLeft
|
||||
Component.onCompleted: {
|
||||
if (data_model.recieved)
|
||||
{
|
||||
anchors.left = parent.left
|
||||
anchors.leftMargin = parent.padding
|
||||
}
|
||||
else{
|
||||
anchors.right = parent.right
|
||||
anchors.rightMargin = parent.padding
|
||||
}
|
||||
}
|
||||
RibbonText{
|
||||
id: sender_text
|
||||
text: bubble.sender_text
|
||||
padding: bubble.padding
|
||||
color: RibbonTheme.dark_mode ? "white" : "black"
|
||||
}
|
||||
RibbonRectangle{
|
||||
id: bubble_bg
|
||||
color: data_model.recieved ? RibbonTheme.dark_mode ? "#202020" : "#FFFFFF" : RibbonTheme.dark_mode ? "#272727" : "#4397F7"
|
||||
height: message_layout.height + bubble.padding*2
|
||||
width: message_layout.width + bubble.padding*2
|
||||
radius: 10
|
||||
topLeftRadius: data_model.recieved ? 2 : bubble.padding
|
||||
topRightRadius: !data_model.recieved ? 2 : bubble.padding
|
||||
ColumnLayout{
|
||||
id: message_layout
|
||||
anchors.centerIn: parent
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,6 +19,12 @@ Window {
|
|||
return FramelessUtils.defaultSystemLightColor;
|
||||
}
|
||||
FramelessHelper.onReady: {
|
||||
if (Qt.platform.os === 'windows')
|
||||
{
|
||||
FramelessHelper.setSystemButton(titleBar.minimizeBtn, FramelessHelperConstants.Minimize);
|
||||
FramelessHelper.setSystemButton(titleBar.maximizeBtn, FramelessHelperConstants.Maximize);
|
||||
FramelessHelper.setSystemButton(titleBar.closeBtn, FramelessHelperConstants.Close);
|
||||
}
|
||||
FramelessHelper.titleBarItem = titleBar;
|
||||
FramelessHelper.moveWindowToDesktopCenter();
|
||||
window.visible = true;
|
|
@ -0,0 +1,42 @@
|
|||
#include <windows.h>
|
||||
|
||||
IDI_ICON1 ICON "imgs/icon.ico"
|
||||
|
||||
#define STR(x) #x
|
||||
#define VER_JOIN(a,b,c,d) STR(a.b.c.d)
|
||||
#define VER_JOIN_(x) VER_JOIN x
|
||||
#define VER_STR VER_JOIN_((PROTOCOLPARSER_VERSION))
|
||||
|
||||
VS_VERSION_INFO VERSIONINFO
|
||||
FILEVERSION PROTOCOLPARSER_VERSION
|
||||
PRODUCTVERSION PROTOCOLPARSER_VERSION
|
||||
FILEFLAGSMASK 0x3fL
|
||||
#ifdef _DEBUG
|
||||
FILEFLAGS VS_FF_DEBUG
|
||||
#else
|
||||
FILEFLAGS 0x0L
|
||||
#endif
|
||||
FILEOS VOS_NT_WINDOWS32
|
||||
FILETYPE VFT_APP
|
||||
FILESUBTYPE VFT2_UNKNOWN
|
||||
BEGIN
|
||||
BLOCK "StringFileInfo"
|
||||
BEGIN
|
||||
BLOCK "040904b0"
|
||||
BEGIN
|
||||
VALUE "Comments", "Made by Mentalflow."
|
||||
VALUE "CompanyName", "Mentalflow"
|
||||
VALUE "FileDescription", "Protocol Parser"
|
||||
VALUE "FileVersion", VER_STR
|
||||
VALUE "InternalName", ""
|
||||
VALUE "LegalCopyright", "Copyright (C) 2023"
|
||||
VALUE "OriginalFilename", ""
|
||||
VALUE "ProductName", "Protocol Parser"
|
||||
VALUE "ProductVersion", VER_STR
|
||||
END
|
||||
END
|
||||
BLOCK "VarFileInfo"
|
||||
BEGIN
|
||||
VALUE "Translation", 0x409, 1200
|
||||
END
|
||||
END
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 32 KiB |
Binary file not shown.
After Width: | Height: | Size: 39 KiB |
Binary file not shown.
After Width: | Height: | Size: 76 KiB |
Binary file not shown.
After Width: | Height: | Size: 126 KiB |
|
@ -0,0 +1,26 @@
|
|||
cmake_minimum_required(VERSION 3.21)
|
||||
|
||||
project(sm_crypto VERSION 1.0.0.0)
|
||||
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
set(
|
||||
sources_files
|
||||
src/big.c src/debug.c src/ecc.c src/inet.c src/random.c
|
||||
src/sm2.c src/sm3.c src/sm4.c
|
||||
include/big.h include/debug.h include/ecc.h include/inet.h
|
||||
include/random.h include/sm2.h include/sm3.h include/sm4.h
|
||||
include/typedef.h
|
||||
)
|
||||
|
||||
INCLUDE_DIRECTORIES(include)
|
||||
|
||||
add_library(${PROJECT_NAME} STATIC ${sources_files})
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE SM_CRYPTO_LIBRARY)
|
|
@ -0,0 +1,97 @@
|
|||
#ifndef _BIG_H_
|
||||
#define _BIG_H_
|
||||
|
||||
#include "typedef.h"
|
||||
|
||||
#define VLI_DIGIT_BITS 64
|
||||
#define VLI_DIGIT_BYTES (VLI_DIGIT_BITS/8)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void vli_clear(u64 *vli, u8 ndigits);
|
||||
|
||||
/* Returns true if vli == 0, false otherwise. */
|
||||
int vli_is_zero(u64 *vli, u8 ndigits);
|
||||
|
||||
/* Returns nonzero if bit bit of vli is set. */
|
||||
u64 vli_test_bit(u64 *vli, u8 bit, u8 ndigits);
|
||||
|
||||
/* Counts the number of 8-bit "digits" in vli. */
|
||||
u32 vli_num_digits(u64 *vli, u8 ndigits);
|
||||
|
||||
/* Counts the number of bits required for vli. */
|
||||
u32 vli_num_bits(u64 *vli, u8 ndigits);
|
||||
/* Sets dest = src. */
|
||||
|
||||
void vli_set(u64 *dest, u64 *src, u8 ndigits);
|
||||
|
||||
/* Returns sign of left - right. */
|
||||
int vli_cmp(u64 *left, u64 *right, u8 ndigits);
|
||||
|
||||
/* Computes result = in << c, returning carry. Can modify in place
|
||||
* (if result == in). 0 < shift < 8.
|
||||
*/
|
||||
u64 vli_lshift(u64 *result, u64 *in, u32 shift, u8 ndigits);
|
||||
|
||||
/* Computes result = (left * right) % curve->p. */
|
||||
void vli_mod_mult_fast(u64 *result, u64 *left, u64 *right, u64 *mod, u8 ndigits);
|
||||
|
||||
/* Computes result = left^2 % curve->p. */
|
||||
void vli_mod_square_fast(u64 *result, u64 *left, u64 *mod, u8 ndigits);
|
||||
|
||||
/* Computes result = in >> c, returning carry. Can modify in place
|
||||
* (if result == in). 0 < shift < 64.
|
||||
*/
|
||||
u64 vli_rshift(u64 *result, u64 *in, u32 shift, u8 ndigits);
|
||||
|
||||
/* Computes result = left + right, returning carry. Can modify in place. */
|
||||
u64 vli_add(u64 *result, u64 *left, u64 *right, u8 ndigits);
|
||||
|
||||
/* Computes result = left - right, returning borrow. Can modify in place. */
|
||||
u64 vli_sub(u64 *result, u64 *left, u64 *right, u8 ndigits);
|
||||
|
||||
/* Computes result = left * right. */
|
||||
void vli_mult(u64 *result, u64 *left, u64 *right, u8 ndigits);
|
||||
|
||||
/* Computes result = left^2. */
|
||||
void vli_square(u64 *result, u64 *left, u8 ndigits);
|
||||
|
||||
/* Computes result = (left + right) % mod.
|
||||
Assumes that left < mod and right < mod, result != mod. */
|
||||
void vli_mod_add(u64 *result, u64 *left, u64 *right, u64 *mod, u8 ndigits);
|
||||
|
||||
/* Computes result = (left - right) % mod.
|
||||
Assumes that left < mod and right < mod, result != mod. */
|
||||
void vli_mod_sub(u64 *result, u64 *left, u64 *right, u64 *mod, u8 ndigits);
|
||||
|
||||
/* Computes result = (left * right) % mod. */
|
||||
void vli_mod_mult(u64 *result, u64 *left, u64 *right, u64 *mod, u8 ndigits);
|
||||
|
||||
/* Computes result = left^2 % mod. */
|
||||
void vli_mod_square(u64 *result, u64 *left, u64 *mod, u8 ndigits);
|
||||
|
||||
/* Computes result = left^p % mod. */
|
||||
void vli_mod_exp(u64 *result, u64 *left, u64 *p, u64 *mod, u8 ndigits);
|
||||
|
||||
/* Computes result = (product) % mod. */
|
||||
void vli_mod(u64 *result, u64 *product, u64 *mod, u8 ndigits);
|
||||
|
||||
/* Computes result = (1 / input) % mod. All VLIs are the same size.
|
||||
* See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
|
||||
* https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
|
||||
*/
|
||||
void vli_mod_inv(u64 *result, u64 *input, u64 *mod, u8 ndigits);
|
||||
|
||||
/* Computes result = (left / right).
|
||||
* remainder = (left % right).
|
||||
*/
|
||||
void vli_div(u64 *result, u64 *remainder, u64 *left, u64 cdigits, u64 *right, u8 ddigits);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef __DEBUG_H_
|
||||
#define __DEBUG_H_
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define DEBUG 0
|
||||
#define ALG_DEBUG 0
|
||||
|
||||
void printHex(unsigned char *name, unsigned char *c, int n);
|
||||
void speed_test( char *name, int len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,74 @@
|
|||
#ifndef _ECC_H_
|
||||
#define _ECC_H_
|
||||
|
||||
#include "typedef.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ECC_WORDSIZE 8
|
||||
#define ECC_NUMBITS 256
|
||||
#define ECC_NUMWORD (ECC_NUMBITS/ECC_WORDSIZE) /*32*/
|
||||
|
||||
#define ECC_MAX_DIGITS 4
|
||||
|
||||
#define SWAP(a,b) { u32 t = a; a = b; b = t;}
|
||||
|
||||
/*
|
||||
#define digit2str16(x, y) { \
|
||||
(y)[0] = (u64)((x >> 8 ) & 0x000000FF); \
|
||||
(y)[1] = (u64)((x >> 0 ) & 0x000000FF); \
|
||||
}
|
||||
|
||||
#define str2digit16(y, x) { \
|
||||
x = ((((u16)(y)[0]) & 0x000000FF) << 8) | \
|
||||
((((u16)(y)[1]) & 0x000000FF) << 0 ); \
|
||||
}
|
||||
|
||||
#define digit2str32(x, y) { \
|
||||
(y)[0] = (u64)((x >> 24) & 0x000000FF); \
|
||||
(y)[1] = (u64)((x >> 16) & 0x000000FF); \
|
||||
(y)[2] = (u64)((x >> 8 ) & 0x000000FF); \
|
||||
(y)[3] = (u64)((x >> 0 ) & 0x000000FF); \
|
||||
}
|
||||
|
||||
#define str2digit32(y, x) { \
|
||||
x = ((((u32)(y)[0]) & 0x000000FF) << 24) | \
|
||||
((((u32)(y)[1]) & 0x000000FF) << 16) | \
|
||||
((((u32)(y)[2]) & 0x000000FF) << 8 ) | \
|
||||
((((u32)(y)[3]) & 0x000000FF) << 0 ); \
|
||||
}
|
||||
*/
|
||||
|
||||
typedef struct ecc_point
|
||||
{
|
||||
u64 x[ECC_MAX_DIGITS];
|
||||
u64 y[ECC_MAX_DIGITS];
|
||||
} ecc_point;
|
||||
|
||||
struct ecc_curve {
|
||||
u8 ndigits;
|
||||
struct ecc_point g;
|
||||
u64 p[ECC_MAX_DIGITS];
|
||||
u64 n[ECC_MAX_DIGITS];
|
||||
u64 h[ECC_MAX_DIGITS];
|
||||
u64 a[ECC_MAX_DIGITS];
|
||||
u64 b[ECC_MAX_DIGITS];
|
||||
};
|
||||
|
||||
void ecc_bytes2native(u64 *native, void *bytes, u8 ndigits);
|
||||
void ecc_native2bytes(void *bytes, u64 *native, u8 ndigits);
|
||||
|
||||
void ecc_point_add(struct ecc_curve *curve, ecc_point *result, ecc_point *x, ecc_point *y);
|
||||
void ecc_point_mult(struct ecc_curve *curve, ecc_point *result, ecc_point *point, u64 *scalar, u64 *initialZ);
|
||||
void ecc_point_mult2(struct ecc_curve *curve, ecc_point *result, ecc_point *g, ecc_point *p, u64 *s, u64 *t);
|
||||
int ecc_point_is_zero(struct ecc_curve *curve, ecc_point *point);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,36 @@
|
|||
#ifndef __GMALG_INET_H__
|
||||
#define __GMALG_INET_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
u16 _htons(u16 hs);
|
||||
u32 _htonl(u32 hl);
|
||||
|
||||
u32 _htonl64(u64 hl);
|
||||
u16 _ntohs(u16 ns);
|
||||
|
||||
u64 _ntohl(u32 nl);
|
||||
u64 _ntohl64(u64 nl);
|
||||
|
||||
u16 le16_to_cpu(u16 data);
|
||||
u32 le32_to_cpu(u32 data);
|
||||
|
||||
u64 le64_to_cpu(u64 data);
|
||||
u16 cpu_to_le16(u16 data);
|
||||
u32 cpu_to_le32(u32 data);
|
||||
u64 cpu_to_le64(u64 data);
|
||||
u16 be16_to_cpu(u16 data);
|
||||
u32 be32_to_cpu(u32 data);
|
||||
u16 cpu_to_be16(u16 data);
|
||||
u32 cpu_to_be32(u32 data);
|
||||
u64 be64_to_cpu(u64 data);
|
||||
u64 cpu_to_be64(u64 data);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,24 @@
|
|||
|
||||
#ifndef _RANDOM_H_
|
||||
#define _RANDOM_H_
|
||||
|
||||
#include "typedef.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void vli_set_random_mode(u32 mode);
|
||||
|
||||
int vli_get_random_mode(void);
|
||||
|
||||
void vli_set_random_fixed(u8 *data, u32 len);
|
||||
|
||||
int vli_get_random(u8 *p_data, u32 len);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,43 @@
|
|||
#ifndef _SM2_H_
|
||||
#define _SM2_H_
|
||||
|
||||
#include "typedef.h"
|
||||
#include "ecc.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int sm2_make_prikey(u8 *prikey);
|
||||
int sm2_make_pubkey(u8 *prikey, ecc_point *pubkey);
|
||||
int sm2_make_keypair(u8 *prikey, ecc_point *pubkey);
|
||||
int sm2_sign(u8 *r, u8 *s, u8 *pri, u8 *hash);
|
||||
int sm2_verify(ecc_point *pubkey, u8 *hash, u8 *r, u8 *s);
|
||||
|
||||
int sm2_encrypt(ecc_point *pubKey, u8 *M, u32 Mlen, u8 *C, u32 *Clen);
|
||||
int sm2_decrypt(u8 *prikey, u8 *C, u32 Clen, u8 *M, u32 *Mlen);
|
||||
|
||||
void sm3_z(u8 *id, u32 idlen, ecc_point *pub, u8 *hash);
|
||||
int sm2_shared_point(u8* selfPriKey, u8* selfTempPriKey, ecc_point* selfTempPubKey,
|
||||
ecc_point *otherPubKey, ecc_point* otherTempPubKey, ecc_point *key);
|
||||
void sm2_shared_key(ecc_point *point, u8 *ZA, u8 *ZB, u32 keyLen, u8 *key);
|
||||
int sm2_point_mult(ecc_point *G, u8 *k, ecc_point *P);
|
||||
|
||||
int sm2_C1C2C3ConvertToC1C3C2(u8 *pOut, u8 *pIn, s32 len, s32 mode);
|
||||
|
||||
int ECC_KeyEx_Init_I(u8 *pri, ecc_point *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* hash);
|
||||
|
||||
int 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[]);
|
||||
|
||||
int ECC_KeyEx_Re_II(ecc_point *V,ecc_point *RA,ecc_point *RB,u8 ZA[],u8 ZB[],u8 SA[]);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _SM2_H_ */
|
|
@ -0,0 +1,56 @@
|
|||
#ifndef _SM3_H_
|
||||
#define _SM3_H_
|
||||
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include "typedef.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define SM3_DATA_LEN 32
|
||||
|
||||
struct sm3_ctx {
|
||||
u32 total[2]; /*!< number of bytes processed */
|
||||
u32 state[8]; /*!< intermediate digest state */
|
||||
u8 buffer[64]; /*!< data block being processed */
|
||||
u8 ipad[64]; /*!< HMAC: inner padding */
|
||||
u8 opad[64]; /*!< HMAC: outer padding */
|
||||
};
|
||||
|
||||
int sm3_init(struct sm3_ctx *ctx);
|
||||
int sm3_update(struct sm3_ctx *ctx, const u8 *input, u32 ilen);
|
||||
int sm3_final(struct sm3_ctx *ctx, u8 *output);
|
||||
|
||||
int sm3_finup(struct sm3_ctx *ctx, const u8 *data,
|
||||
u32 len, u8 *out);
|
||||
|
||||
|
||||
void sm3_hmac_starts(struct sm3_ctx *ctx, unsigned char *key, int keylen );
|
||||
|
||||
/*
|
||||
* SM3 HMAC process buffer
|
||||
*/
|
||||
void sm3_hmac_update(struct sm3_ctx *ctx, unsigned char *input, int ilen );
|
||||
|
||||
/*
|
||||
* SM3 HMAC final digest
|
||||
*/
|
||||
void sm3_hmac_finish(struct sm3_ctx *ctx, unsigned char output[32] );
|
||||
|
||||
|
||||
/*
|
||||
* output = HMAC-SM#( hmac key, input buffer )
|
||||
*/
|
||||
void sm3_hmac( unsigned char *key, int keylen,
|
||||
unsigned char *input, int ilen,
|
||||
unsigned char output[32] );
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _SM3_H_ */
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef _SM4_H_
|
||||
#define _SM4_H_
|
||||
|
||||
#include "typedef.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct sm4_ctx{
|
||||
u32 sk_enc[32];
|
||||
u32 sk_dec[32];
|
||||
u32 iv[16];
|
||||
};
|
||||
|
||||
void sm4_ecb_encrypt(struct sm4_ctx *ctx, u8 *key, u8 *in, u32 len, u8 *out);
|
||||
void sm4_ecb_decrypt(struct sm4_ctx *ctx, u8 *key, u8 *in, u32 len, u8 *out);
|
||||
void sm4_cbc_encrypt(struct sm4_ctx *ctx, u8 *key, u8 *iv, u8 *in, u32 len, u8 *out);
|
||||
void sm4_cbc_decrypt(struct sm4_ctx *ctx, u8 *key, u8 *iv, u8 *in, u32 len, u8 *out);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _SM4_H_ */
|
|
@ -0,0 +1,174 @@
|
|||
#ifndef __TYPEDEF_H__
|
||||
#define __TYPEDEF_H__
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/****************************Start***************************/
|
||||
|
||||
|
||||
/*#define __BULID_LINUX__*/
|
||||
#define __BUILD_NO_OS__
|
||||
|
||||
|
||||
/*#define __LITTLE_ENDIAN__*/
|
||||
#define __BIG_ENDIAN__
|
||||
|
||||
|
||||
/*#define RANDOM_SOFTWARE*/
|
||||
|
||||
|
||||
/*Big Numer MAX BIT*/
|
||||
#define RSA_MAX_MODULUS_BITS 2048
|
||||
#define RSA_MAX_DIGITS (RSA_MAX_MODULUS_BITS/VLI_DIGIT_BITS)
|
||||
#define BIGNUM_MAX_DIGITS RSA_MAX_DIGITS
|
||||
|
||||
|
||||
/*For C Language Standards */
|
||||
#if defined(__STDC__)
|
||||
#define PREDEF_STANDARD_C_1989 /*C89 , C90*/
|
||||
|
||||
#if defined(__STDC_VERSION__)
|
||||
#if (__STDC_VERSION__ >= 199901L)
|
||||
#define PREDEF_STANDARD_C_1999 /*C99*/
|
||||
#undef PREDEF_STANDARD_C_1989
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989 /*C89 , C90*/
|
||||
#define inline __inline
|
||||
#endif
|
||||
|
||||
/****************************End***************************/
|
||||
|
||||
#ifdef __BULID_LINUX__
|
||||
|
||||
#include <linux/types.h>
|
||||
#include <asm/byteorder.h>
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
typedef __u8 u8;
|
||||
typedef __u16 u16;
|
||||
typedef __u32 u32;
|
||||
typedef __u64 u64;
|
||||
|
||||
typedef __s8 s8;
|
||||
typedef __s16 s16;
|
||||
typedef __s32 s32;
|
||||
typedef __s64 s64;
|
||||
|
||||
|
||||
#define le16_to_cpu __le16_to_cpu
|
||||
#define le32_to_cpu __le32_to_cpu
|
||||
#define le64_to_cpu __le64_to_cpu
|
||||
|
||||
#define cpu_to_le16 __cpu_to_le16
|
||||
#define cpu_to_le32 __cpu_to_le32
|
||||
#define cpu_to_le64 __cpu_to_le64
|
||||
|
||||
#define be16_to_cpu __be16_to_cpu
|
||||
#define be32_to_cpu __be32_to_cpu
|
||||
#define be64_to_cpu __be64_to_cpu
|
||||
|
||||
#define cpu_to_be16 __cpu_to_be16
|
||||
#define cpu_to_be32 __cpu_to_be32
|
||||
#define cpu_to_be64 __cpu_to_be64
|
||||
|
||||
|
||||
#else
|
||||
|
||||
|
||||
#ifdef __BUILD_NO_OS__
|
||||
|
||||
#define RANDOM_SOFTWARE
|
||||
|
||||
typedef unsigned char u8;
|
||||
typedef unsigned short u16;
|
||||
typedef unsigned int u32;
|
||||
typedef unsigned long long u64;
|
||||
|
||||
typedef char s8;
|
||||
typedef short s16;
|
||||
typedef int s32;
|
||||
typedef long s64;
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
#define __swap16(A) ((((u16)(A) & 0xff00) >> 8) | (((u16)(A) & 0x00ff) << 8))
|
||||
|
||||
#define __swap32(A) ((((u32)(A) & 0xff000000) >> 24) | \
|
||||
(((u32)(A) & 0x00ff0000) >> 8) | \
|
||||
(((u32)(A) & 0x0000ff00) << 8) | \
|
||||
(((u32)(A) & 0x000000ff) << 24))
|
||||
|
||||
#define __swap64(A) ((u64)( \
|
||||
(((u64)(A) & (u64)0x00000000000000ffULL) << 56) | \
|
||||
(((u64)(A) & (u64)0x000000000000ff00ULL) << 40) | \
|
||||
(((u64)(A) & (u64)0x0000000000ff0000ULL) << 24) | \
|
||||
(((u64)(A) & (u64)0x00000000ff000000ULL) << 8) | \
|
||||
(((u64)(A) & (u64)0x000000ff00000000ULL) >> 8) | \
|
||||
(((u64)(A) & (u64)0x0000ff0000000000ULL) >> 24)| \
|
||||
(((u64)(A) & (u64)0x00ff000000000000ULL) >> 40) |\
|
||||
(((u64)(A) & (u64)0xff00000000000000ULL) >> 56)))
|
||||
|
||||
/*To Test Little Endian or Big Endian*/
|
||||
static union {
|
||||
char c[4];
|
||||
unsigned long mylong;
|
||||
} endian_test = {{ 'l', '?', '?', 'b' } };
|
||||
|
||||
|
||||
#define ENDIANNESS ((char)endian_test.mylong)
|
||||
|
||||
#include "inet.h"
|
||||
|
||||
#endif /*__BUILD_NO_OS__*/
|
||||
|
||||
#endif/*__BULID_LINUX__*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
u16 __get_unaligned_le16(const u8 *p);
|
||||
u32 __get_unaligned_le32(const u8 *p);
|
||||
u64 __get_unaligned_le64(const u8 *p);
|
||||
void __put_unaligned_le16(u16 val, u8 *p);
|
||||
void __put_unaligned_le32(u32 val, u8 *p);
|
||||
void __put_unaligned_le64(u64 val, u8 *p);
|
||||
u16 get_unaligned_le16(const void *p);
|
||||
u32 get_unaligned_le32(const void *p);
|
||||
u64 get_unaligned_le64(const void *p);
|
||||
void put_unaligned_le16(u16 val, void *p);
|
||||
void put_unaligned_le32(u32 val, void *p);
|
||||
void put_unaligned_le64(u64 val, void *p);
|
||||
u16 __get_unaligned_be16(const u8 *p);
|
||||
u32 __get_unaligned_be32(const u8 *p);
|
||||
u64 __get_unaligned_be64(const u8 *p);
|
||||
void __put_unaligned_be16(u16 val, u8 *p);
|
||||
void __put_unaligned_be32(u32 val, u8 *p);
|
||||
void __put_unaligned_be64(u64 val, u8 *p);
|
||||
u16 get_unaligned_be16(const void *p);
|
||||
u32 get_unaligned_be32(const void *p);
|
||||
u64 get_unaligned_be64(const void *p);
|
||||
void put_unaligned_be16(u16 val, void *p);
|
||||
void put_unaligned_be32(u32 val, void *p);
|
||||
void put_unaligned_be64(u64 val, void *p);
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,989 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "big.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u64 m_low;
|
||||
u64 m_high;
|
||||
} uint128_t;
|
||||
|
||||
void vli_clear(u64 *vli, u8 ndigits)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ndigits; ++i) {
|
||||
vli[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Returns true if vli == 0, false otherwise. */
|
||||
int vli_is_zero(u64 *vli, u8 ndigits)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ndigits; ++i) {
|
||||
if (vli[i])
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Returns nonzero if bit bit of vli is set. */
|
||||
u64 vli_test_bit(u64 *vli, u8 bit, u8 ndigits)
|
||||
{
|
||||
return (vli[bit/64] & ((u64)1 << (bit % 64)));
|
||||
}
|
||||
|
||||
/* Counts the number of 64-bit "digits" in vli. */
|
||||
u32 vli_num_digits(u64 *vli, u8 ndigits)
|
||||
{
|
||||
int i;
|
||||
/* Search from the end until we find a non-zero digit.
|
||||
* We do it in reverse because we expect that most digits will
|
||||
* be nonzero.
|
||||
*/
|
||||
for (i = ndigits - 1; i >= 0 && vli[i] == 0; --i);
|
||||
|
||||
return (i + 1);
|
||||
}
|
||||
|
||||
/* Counts the number of bits required for vli. */
|
||||
u32 vli_num_bits(u64 *vli, u8 ndigits)
|
||||
{
|
||||
u32 i, num_digits;
|
||||
u64 digit;
|
||||
|
||||
num_digits = vli_num_digits(vli, ndigits);
|
||||
if (num_digits == 0)
|
||||
return 0;
|
||||
|
||||
digit = vli[num_digits - 1];
|
||||
for (i = 0; digit; ++i)
|
||||
digit >>= 1;
|
||||
|
||||
return ((num_digits - 1) * 64 + i);
|
||||
}
|
||||
|
||||
/* Sets dest = src. */
|
||||
void vli_set(u64 *dest, u64 *src, u8 ndigits)
|
||||
{
|
||||
u32 i;
|
||||
|
||||
for (i = 0; i < ndigits; ++i)
|
||||
dest[i] = src[i];
|
||||
}
|
||||
|
||||
/* Returns sign of left - right. */
|
||||
int vli_cmp(u64 *left, u64 *right, u8 ndigits)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = ndigits - 1; i >= 0; --i) {
|
||||
if (left[i] > right[i])
|
||||
return 1;
|
||||
else if (left[i] < right[i])
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Computes result = in << c, returning carry. Can modify in place
|
||||
* (if result == in). 0 < shift < 64.
|
||||
*/
|
||||
u64 vli_lshift(u64 *result, u64 *in, u32 shift, u8 ndigits)
|
||||
{
|
||||
u64 carry = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ndigits; ++i) {
|
||||
u64 temp = in[i];
|
||||
result[i] = (temp << shift) | carry;
|
||||
carry = shift ? temp >> (64 - shift) : 0;
|
||||
}
|
||||
|
||||
return carry;
|
||||
}
|
||||
|
||||
/* Computes result = in >> c, returning carry. Can modify in place
|
||||
* (if result == in). 0 < shift < 64.
|
||||
*/
|
||||
u64 vli_rshift(u64 *result, u64 *in, u32 shift, u8 ndigits)
|
||||
{
|
||||
u64 carry = 0;
|
||||
int i;
|
||||
|
||||
for (i = ndigits -1; i >= 0; --i) {
|
||||
u64 temp = in[i];
|
||||
result[i] = (temp >> shift) | carry;
|
||||
carry = shift ? temp << (64 - shift) : 0;
|
||||
}
|
||||
|
||||
return carry;
|
||||
}
|
||||
|
||||
/* Computes result = left + right, returning carry. Can modify in place. */
|
||||
u64 vli_add(u64 *result, u64 *left, u64 *right, u8 ndigits)
|
||||
{
|
||||
u64 carry = 0;
|
||||
u32 i;
|
||||
|
||||
for (i = 0; i < ndigits; ++i) {
|
||||
u64 sum;
|
||||
|
||||
sum = left[i] + right[i] + carry;
|
||||
if (sum != left[i]) {
|
||||
carry = (sum < left[i]);
|
||||
}
|
||||
result[i] = sum;
|
||||
}
|
||||
|
||||
return carry;
|
||||
}
|
||||
|
||||
/* Computes result = left - right, returning borrow. Can modify in place. */
|
||||
u64 vli_sub(u64 *result, u64 *left, u64 *right, u8 ndigits)
|
||||
{
|
||||
u64 borrow = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ndigits; ++i) {
|
||||
u64 diff;
|
||||
|
||||
diff = left[i] - right[i] - borrow;
|
||||
if (diff != left[i])
|
||||
borrow = (diff > left[i]);
|
||||
|
||||
result[i] = diff;
|
||||
}
|
||||
|
||||
return borrow;
|
||||
}
|
||||
|
||||
static uint128_t mul_64_64(u64 left, u64 right)
|
||||
{
|
||||
u64 a0 = left & 0xffffffffull;
|
||||
u64 a1 = left >> 32;
|
||||
u64 b0 = right & 0xffffffffull;
|
||||
u64 b1 = right >> 32;
|
||||
u64 m0 = a0 * b0;
|
||||
u64 m1 = a0 * b1;
|
||||
u64 m2 = a1 * b0;
|
||||
u64 m3 = a1 * b1;
|
||||
uint128_t result;
|
||||
|
||||
m2 += (m0 >> 32);
|
||||
m2 += m1;
|
||||
|
||||
/* Overflow */
|
||||
if (m2 < m1)
|
||||
m3 += 0x100000000ull;
|
||||
|
||||
result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
|
||||
result.m_high = m3 + (m2 >> 32);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static uint128_t add_128_128(uint128_t a, uint128_t b)
|
||||
{
|
||||
uint128_t result;
|
||||
|
||||
result.m_low = a.m_low + b.m_low;
|
||||
result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static u64 vli_add_digit_mul(u64 *result, u64 *b, u64 c, u64 *d, u8 digits)
|
||||
{
|
||||
uint128_t mul;
|
||||
u64 carry, rh, rl;
|
||||
u32 i;
|
||||
|
||||
if (c == 0)
|
||||
return 0;
|
||||
|
||||
carry = 0;
|
||||
for (i = 0; i < digits; i++) {
|
||||
mul = mul_64_64(c, d[i]);
|
||||
if ((result[i] = b[i] + carry) < carry) {
|
||||
carry = 1;
|
||||
} else {
|
||||
carry = 0;
|
||||
}
|
||||
if ((result[i] += mul.m_low) < mul.m_low) {
|
||||
carry++;
|
||||
}
|
||||
carry += mul.m_high;
|
||||
}
|
||||
|
||||
return carry;
|
||||
}
|
||||
|
||||
void bn_mult(u64 *result, u64 *left, u64 *right, u8 ndigits)
|
||||
{
|
||||
u32 bdigits, cdigits, i;
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
u64 *t = (u64*) malloc(2 * ndigits * sizeof(u64));
|
||||
if(t == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
#else
|
||||
u64 t[2*ndigits];
|
||||
#endif
|
||||
|
||||
vli_clear(t, 2*ndigits);
|
||||
|
||||
bdigits = vli_num_digits(left, ndigits);
|
||||
cdigits = vli_num_digits(right, ndigits);
|
||||
|
||||
for(i=0; i<bdigits; i++) {
|
||||
t[i+cdigits] += vli_add_digit_mul(&t[i], &t[i], left[i], right, cdigits);
|
||||
}
|
||||
|
||||
vli_set(result, t, 2*ndigits);
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
free(t);
|
||||
#endif
|
||||
}
|
||||
|
||||
#define BN_DIGIT_BITS 32
|
||||
#define BN_MAX_DIGIT 0xFFFFFFFF
|
||||
static u32 vli_sub_digit_mult(u32 *a, u32 *b, u32 c, u32 *d, u32 digits)
|
||||
{
|
||||
u64 result;
|
||||
u32 borrow, rh, rl;
|
||||
u32 i;
|
||||
|
||||
if(c == 0)
|
||||
return 0;
|
||||
|
||||
borrow = 0;
|
||||
for(i=0; i<digits; i++) {
|
||||
result = (u64)c * d[i];
|
||||
rl = result & BN_MAX_DIGIT;
|
||||
rh = (result >> BN_DIGIT_BITS) & BN_MAX_DIGIT;
|
||||
if((a[i] = b[i] - borrow) > (BN_MAX_DIGIT - borrow)) {
|
||||
borrow = 1;
|
||||
} else {
|
||||
borrow = 0;
|
||||
}
|
||||
if((a[i] -= rl) > (BN_MAX_DIGIT - rl)) {
|
||||
borrow++;
|
||||
}
|
||||
borrow += rh;
|
||||
}
|
||||
|
||||
return borrow;
|
||||
}
|
||||
|
||||
static u32 bn_digit_bits(u32 a)
|
||||
{
|
||||
u32 i;
|
||||
|
||||
for(i = 0; i< sizeof(a) * 8; i++) {
|
||||
if(a == 0) break;
|
||||
a >>= 1;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
void bn_div(u32 *a, u32 *b, u32 *c, u32 cdigits, u32 *d, u32 ddigits)
|
||||
{
|
||||
|
||||
u32 dddigits, shift;
|
||||
u64 tmp;
|
||||
int i;
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
u32 ai, t;
|
||||
|
||||
u32 *cc;
|
||||
u32 *dd;
|
||||
|
||||
cc = (u32*) malloc((cdigits+1) * sizeof(u32));
|
||||
if(cc == NULL)
|
||||
return;
|
||||
|
||||
dd = (u32*) malloc((cdigits/2) * sizeof(u32));
|
||||
if(dd == NULL)
|
||||
{
|
||||
free(cc);
|
||||
return;
|
||||
}
|
||||
#else
|
||||
u32 ai, t, cc[cdigits+1], dd[cdigits/2];
|
||||
#endif
|
||||
|
||||
dddigits = ddigits;
|
||||
|
||||
shift = BN_DIGIT_BITS - bn_digit_bits(d[dddigits-1]);
|
||||
vli_clear((u64*)cc, dddigits/2);
|
||||
cc[cdigits] = vli_lshift((u64*)cc, (u64*)c, shift, cdigits/2);
|
||||
vli_lshift((u64*)dd, (u64*)d, shift, dddigits/2);
|
||||
t = dd[dddigits-1];
|
||||
|
||||
vli_clear((u64*)a, cdigits/2);
|
||||
i = cdigits - dddigits;
|
||||
for(; i>=0; i--) {
|
||||
if(t == BN_MAX_DIGIT) {
|
||||
ai = cc[i+dddigits];
|
||||
} else {
|
||||
tmp = cc[i+dddigits-1];
|
||||
tmp += (u64)cc[i+dddigits] << BN_DIGIT_BITS;
|
||||
ai = tmp / (t + 1);
|
||||
}
|
||||
|
||||
cc[i+dddigits] -= vli_sub_digit_mult(&cc[i], &cc[i], ai, dd, dddigits);
|
||||
while(cc[i+dddigits] || (vli_cmp((u64*)&cc[i], (u64*)dd, dddigits/2) >= 0)) {
|
||||
ai++;
|
||||
cc[i+dddigits] -= vli_sub((u64*)&cc[i], (u64*)&cc[i], (u64*)dd, dddigits/2);
|
||||
}
|
||||
a[i] = ai;
|
||||
}
|
||||
|
||||
vli_rshift((u64*)b, (u64*)cc, shift, dddigits/2);
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
free(cc);
|
||||
free(dd);
|
||||
#endif
|
||||
}
|
||||
|
||||
void vli_div(u64 *result, u64 *remainder, u64 *left, u64 cdigits, u64 *right, u8 ddigits)
|
||||
{
|
||||
bn_div((u32*)result, (u32*)remainder, (u32*)left, cdigits*2, (u32*)right, ddigits*2);
|
||||
}
|
||||
|
||||
void bn_mod(u64 *result, u64 *left, u64 *right, u8 ndigits)
|
||||
{
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
u64 *t = (u64*) malloc(2 * ndigits * sizeof(u64));
|
||||
if(t == NULL)
|
||||
return;
|
||||
#else
|
||||
u64 t[2*ndigits];
|
||||
#endif
|
||||
|
||||
vli_div(t, result, left, ndigits*2, right, ndigits);
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
free(t);
|
||||
#endif
|
||||
}
|
||||
|
||||
void _vli_mult(u64 *result, u64 *left, u64 *right, u8 ndigits)
|
||||
{
|
||||
uint128_t r01 = { 0, 0 };
|
||||
u64 r2 = 0;
|
||||
unsigned int i, k;
|
||||
|
||||
/* Compute each digit of result in sequence, maintaining the
|
||||
* carries.
|
||||
*/
|
||||
for (k = 0; k < ndigits * 2 - 1; k++) {
|
||||
unsigned int min;
|
||||
|
||||
if (k < ndigits)
|
||||
min = 0;
|
||||
else
|
||||
min = (k + 1) - ndigits;
|
||||
|
||||
for (i = min; i <= k && i < ndigits; i++) {
|
||||
uint128_t product;
|
||||
|
||||
product = mul_64_64(left[i], right[k - i]);
|
||||
|
||||
r01 = add_128_128(r01, product);
|
||||
r2 += (r01.m_high < product.m_high);
|
||||
}
|
||||
|
||||
result[k] = r01.m_low;
|
||||
r01.m_low = r01.m_high;
|
||||
r01.m_high = r2;
|
||||
r2 = 0;
|
||||
}
|
||||
|
||||
result[ndigits * 2 - 1] = r01.m_low;
|
||||
}
|
||||
|
||||
void vli_mult(u64 *result, u64 *left, u64 *right, u8 ndigits)
|
||||
{
|
||||
#if 1
|
||||
bn_mult(result, left, right, ndigits);
|
||||
#else
|
||||
_vli_mult(result, left, right, ndigits);
|
||||
#endif
|
||||
}
|
||||
|
||||
void vli_square(u64 *result, u64 *left, u8 ndigits)
|
||||
{
|
||||
uint128_t r01 = { 0, 0 };
|
||||
u64 r2 = 0;
|
||||
int i, k;
|
||||
|
||||
for (k = 0; k < ndigits * 2 - 1; k++) {
|
||||
unsigned int min;
|
||||
|
||||
if (k < ndigits)
|
||||
min = 0;
|
||||
else
|
||||
min = (k + 1) - ndigits;
|
||||
|
||||
for (i = min; i <= k && i <= k - i; i++) {
|
||||
uint128_t product;
|
||||
|
||||
product = mul_64_64(left[i], left[k - i]);
|
||||
|
||||
if (i < k - i) {
|
||||
r2 += product.m_high >> 63;
|
||||
product.m_high = (product.m_high << 1) |
|
||||
(product.m_low >> 63);
|
||||
product.m_low <<= 1;
|
||||
}
|
||||
|
||||
r01 = add_128_128(r01, product);
|
||||
r2 += (r01.m_high < product.m_high);
|
||||
}
|
||||
|
||||
result[k] = r01.m_low;
|
||||
r01.m_low = r01.m_high;
|
||||
r01.m_high = r2;
|
||||
r2 = 0;
|
||||
}
|
||||
|
||||
result[ndigits * 2 - 1] = r01.m_low;
|
||||
}
|
||||
|
||||
/* Computes result = (left + right) % mod.
|
||||
Assumes that left < mod and right < mod, result != mod. */
|
||||
void vli_mod_add(u64 *result, u64 *left, u64 *right, u64 *mod, u8 ndigits)
|
||||
{
|
||||
u64 carry;
|
||||
|
||||
carry = vli_add(result, left, right, ndigits);
|
||||
/* result > mod (result = mod + remainder), so subtract mod to
|
||||
* get remainder.
|
||||
*/
|
||||
|
||||
if(carry || vli_cmp(result, mod, ndigits) >= 0) {
|
||||
/* result > mod (result = mod + remainder), so subtract mod to get remainder. */
|
||||
vli_sub(result, result, mod, ndigits);
|
||||
}
|
||||
}
|
||||
|
||||
/* Computes result = (left - right) % mod.
|
||||
* Assumes that left < mod and right < mod, result != mod.
|
||||
*/
|
||||
void vli_mod_sub(u64 *result, u64 *left, u64 *right, u64 *mod, u8 ndigits)
|
||||
{
|
||||
u64 borrow;
|
||||
|
||||
borrow = vli_sub(result, left, right, ndigits);
|
||||
/* In this case, result == -diff == (max int) - diff.
|
||||
* Since -x % d == d - x, we can get the correct result from
|
||||
* result + mod (with overflow).
|
||||
*/
|
||||
if(borrow)
|
||||
vli_add(result, result, mod, ndigits);
|
||||
}
|
||||
|
||||
/* Computes result = product % curve_prime
|
||||
* from http://www.nsa.gov/ia/_files/nist-routines.pdf
|
||||
*/
|
||||
void vli_mmod_fast_nist_256(u64 *result, u64 *product, u64 *curve_prime, u8 ndigits)
|
||||
{
|
||||
int carry;
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
u64 *tmp = (u64*) malloc(2 * ndigits * sizeof(u64));
|
||||
if(tmp == NULL)
|
||||
return;
|
||||
#else
|
||||
u64 tmp[2 * ndigits];
|
||||
#endif
|
||||
|
||||
/* t */
|
||||
vli_set(result, product, ndigits);
|
||||
|
||||
/* s1 */
|
||||
tmp[0] = 0;
|
||||
tmp[1] = product[5] & 0xffffffff00000000ull;
|
||||
tmp[2] = product[6];
|
||||
tmp[3] = product[7];
|
||||
carry = vli_lshift(tmp, tmp, 1, ndigits);
|
||||
carry += vli_add(result, result, tmp, ndigits);
|
||||
|
||||
/* s2 */
|
||||
tmp[1] = product[6] << 32;
|
||||
tmp[2] = (product[6] >> 32) | (product[7] << 32);
|
||||
tmp[3] = product[7] >> 32;
|
||||
carry += vli_lshift(tmp, tmp, 1, ndigits);
|
||||
carry += vli_add(result, result, tmp, ndigits);
|
||||
|
||||
/* s3 */
|
||||
tmp[0] = product[4];
|
||||
tmp[1] = product[5] & 0xffffffff;
|
||||
tmp[2] = 0;
|
||||
tmp[3] = product[7];
|
||||
carry += vli_add(result, result, tmp, ndigits);
|
||||
|
||||
/* s4 */
|
||||
tmp[0] = (product[4] >> 32) | (product[5] << 32);
|
||||
tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
|
||||
tmp[2] = product[7];
|
||||
tmp[3] = (product[6] >> 32) | (product[4] << 32);
|
||||
carry += vli_add(result, result, tmp, ndigits);
|
||||
|
||||
/* d1 */
|
||||
tmp[0] = (product[5] >> 32) | (product[6] << 32);
|
||||
tmp[1] = (product[6] >> 32);
|
||||
tmp[2] = 0;
|
||||
tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
|
||||
carry -= vli_sub(result, result, tmp, ndigits);
|
||||
|
||||
/* d2 */
|
||||
tmp[0] = product[6];
|
||||
tmp[1] = product[7];
|
||||
tmp[2] = 0;
|
||||
tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
|
||||
carry -= vli_sub(result, result, tmp, ndigits);
|
||||
|
||||
/* d3 */
|
||||
tmp[0] = (product[6] >> 32) | (product[7] << 32);
|
||||
tmp[1] = (product[7] >> 32) | (product[4] << 32);
|
||||
tmp[2] = (product[4] >> 32) | (product[5] << 32);
|
||||
tmp[3] = (product[6] << 32);
|
||||
carry -= vli_sub(result, result, tmp, ndigits);
|
||||
|
||||
/* d4 */
|
||||
tmp[0] = product[7];
|
||||
tmp[1] = product[4] & 0xffffffff00000000ull;
|
||||
tmp[2] = product[5];
|
||||
tmp[3] = product[6] & 0xffffffff00000000ull;
|
||||
carry -= vli_sub(result, result, tmp, ndigits);
|
||||
|
||||
if (carry < 0) {
|
||||
do {
|
||||
carry += vli_add(result, result, curve_prime, ndigits);
|
||||
} while (carry < 0);
|
||||
} else {
|
||||
while (carry || vli_cmp(curve_prime, result, ndigits) != 1){
|
||||
carry -= vli_sub(result, result, curve_prime, ndigits);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
free(tmp);
|
||||
#endif
|
||||
}
|
||||
|
||||
void vli_mmod_fast_sm2_256(u64 *result, u64 *_product, u64 *mod, u8 ndigits)
|
||||
{
|
||||
u32 tmp1[8];
|
||||
u32 tmp2[8];
|
||||
u32 tmp3[8];
|
||||
u32 *product = (u32 *)_product;
|
||||
int carry = 0;
|
||||
|
||||
vli_set(result, (u64 *)product, ndigits);
|
||||
vli_clear((u64 *)tmp1, ndigits);
|
||||
vli_clear((u64 *)tmp2, ndigits);
|
||||
vli_clear((u64 *)tmp3, ndigits);
|
||||
|
||||
/* Y0 */
|
||||
tmp1[0] = tmp1[3] = tmp1[7] = product[8];
|
||||
tmp2[2] = product[8];
|
||||
carry += vli_add(result, result, (u64 *)tmp1, ndigits);
|
||||
carry -= vli_sub(result, result, (u64 *)tmp2, ndigits);
|
||||
|
||||
/* Y1 */
|
||||
tmp1[0] = tmp1[1] = tmp1[4] = tmp1[7] = product[9];
|
||||
tmp1[3] = 0;
|
||||
tmp2[2] = product[9];
|
||||
carry += vli_add(result, result, (u64 *)tmp1, ndigits);
|
||||
carry -= vli_sub(result, result, (u64 *)tmp2, ndigits);
|
||||
|
||||
/* Y2 */
|
||||
tmp1[0] = tmp1[1] = tmp1[5] = tmp1[7] = product[10];
|
||||
tmp1[4] = 0;
|
||||
carry += vli_add(result, result, (u64 *)tmp1, ndigits);
|
||||
|
||||
/* Y3 */
|
||||
tmp1[0] = tmp1[1] = tmp1[3] = tmp1[6] = tmp1[7] = product[11];
|
||||
tmp1[5] = 0;
|
||||
carry += vli_add(result, result, (u64 *)tmp1, ndigits);
|
||||
|
||||
/* Y4 */
|
||||
tmp1[0] = tmp1[1] = tmp1[3] = tmp1[4] = tmp1[7] = tmp3[7] = product[12];
|
||||
tmp1[6] = 0;
|
||||
carry += vli_add(result, result, (u64 *)tmp1, ndigits);
|
||||
carry += vli_add(result, result, (u64 *)tmp3, ndigits);
|
||||
|
||||
/* Y5 */
|
||||
tmp1[0] = tmp1[1] = tmp1[3] = tmp1[4] = tmp1[5] = tmp1[7] = product[13];
|
||||
tmp2[2] = product[13];
|
||||
tmp3[0] = tmp3[3] = tmp3[7] = product[13];
|
||||
carry += vli_add(result, result, (u64 *)tmp1, ndigits);
|
||||
carry += vli_add(result, result, (u64 *)tmp3, ndigits);
|
||||
carry -= vli_sub(result, result, (u64 *)tmp2, ndigits);
|
||||
|
||||
/* Y6 */
|
||||
tmp1[0] = tmp1[1] = tmp1[3] = tmp1[4] = tmp1[5] = tmp1[6] = tmp1[7] = product[14];
|
||||
tmp2[2] = product[14];
|
||||
tmp3[0] = tmp3[1] = tmp3[4] = tmp3[7] = product[14];
|
||||
tmp3[3] = 0;
|
||||
carry += vli_add(result, result, (u64 *)tmp1, ndigits);
|
||||
carry += vli_add(result, result, (u64 *)tmp3, ndigits);
|
||||
carry -= vli_sub(result, result, (u64 *)tmp2, ndigits);
|
||||
|
||||
/* Y7 */
|
||||
tmp1[0] = tmp1[1] = tmp1[3] = tmp1[4] = tmp1[5] = tmp1[6] = tmp1[7] = product[15];
|
||||
tmp3[0] = tmp3[1] = tmp3[5] = product[15];
|
||||
tmp3[4] = 0;
|
||||
tmp3[7] = 0;
|
||||
tmp2[7] = product[15];
|
||||
tmp2[2] = 0;
|
||||
carry += vli_lshift((u64 *)tmp2, (u64 *)tmp2, 1, ndigits);
|
||||
carry += vli_add(result, result, (u64 *)tmp1, ndigits);
|
||||
carry += vli_add(result, result, (u64 *)tmp3, ndigits);
|
||||
carry += vli_add(result, result, (u64 *)tmp2, ndigits);
|
||||
if (carry < 0) {
|
||||
do {
|
||||
carry += vli_add(result, result, mod, ndigits);
|
||||
} while(carry < 0);
|
||||
} else {
|
||||
while (carry || vli_cmp(mod, result, ndigits) != 1)
|
||||
{
|
||||
carry -= vli_sub(result, result, mod, ndigits);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Computes result = (product) % mod. */
|
||||
void _vli_mod(u64 *result, u64 *product, u64 *mod, u8 ndigits)
|
||||
{
|
||||
uint digitShift, bitShift;
|
||||
uint productBits;
|
||||
uint modBits = vli_num_bits(mod, ndigits);
|
||||
|
||||
u64 carry;
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
u64 *modMultiple = (u64*) malloc(2 * ndigits * sizeof(u64));
|
||||
if(modMultiple == NULL)
|
||||
return;
|
||||
#else
|
||||
u64 modMultiple[2 * ndigits];
|
||||
#endif
|
||||
|
||||
productBits = vli_num_bits(product + ndigits, ndigits);
|
||||
if (productBits) {
|
||||
productBits += ndigits * 64;
|
||||
} else {
|
||||
productBits = vli_num_bits(product, ndigits);
|
||||
}
|
||||
|
||||
if (productBits < modBits) {
|
||||
/* product < mod. */
|
||||
vli_set(result, product, ndigits);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Shift mod by (leftBits - modBits). This multiplies mod by the largest
|
||||
power of two possible while still resulting in a number less than left. */
|
||||
vli_clear(modMultiple, ndigits);
|
||||
vli_clear(modMultiple + ndigits, ndigits);
|
||||
digitShift = (productBits - modBits) / 64;
|
||||
bitShift = (productBits - modBits) % 64;
|
||||
if (bitShift) {
|
||||
modMultiple[digitShift + ndigits] = vli_lshift(modMultiple + digitShift, mod, bitShift, ndigits);
|
||||
} else {
|
||||
vli_set(modMultiple + digitShift, mod, ndigits);
|
||||
}
|
||||
|
||||
/* Subtract all multiples of mod to get the remainder. */
|
||||
vli_clear(result, ndigits);
|
||||
result[0] = 1; /* Use result as a temp var to store 1 (for subtraction) */
|
||||
while (productBits > ndigits * 64 || vli_cmp(modMultiple, mod, ndigits) >= 0)
|
||||
{
|
||||
int cmp = vli_cmp(modMultiple + ndigits, product + ndigits, ndigits);
|
||||
if (cmp < 0 || (cmp == 0 && vli_cmp(modMultiple, product, ndigits) <= 0)) {
|
||||
if (vli_sub(product, product, modMultiple, ndigits))
|
||||
{
|
||||
/* borrow */
|
||||
vli_sub(product + ndigits, product + ndigits, result, ndigits);
|
||||
}
|
||||
vli_sub(product + ndigits, product + ndigits, modMultiple + ndigits, ndigits);
|
||||
}
|
||||
|
||||
carry = (modMultiple[ndigits] & 0x01) << 63;
|
||||
vli_rshift(modMultiple + ndigits, modMultiple + ndigits, 1, ndigits);
|
||||
vli_rshift(modMultiple, modMultiple, 1, ndigits);
|
||||
modMultiple[ndigits-1] |= carry;
|
||||
|
||||
--productBits;
|
||||
}
|
||||
vli_set(result, product, ndigits);
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
free(modMultiple);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
/* Computes result = (product) % mod. */
|
||||
void vli_mod(u64 *result, u64 *product, u64 *mod, u8 ndigits)
|
||||
{
|
||||
#if 1
|
||||
bn_mod(result, product, mod, ndigits);
|
||||
#else
|
||||
_vli_mod(result, product, mod, ndigits);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Computes result = (left * right) % curve->p. */
|
||||
void vli_mod_mult_fast(u64 *result, u64 *left, u64 *right, u64 *mod, u8 ndigits)
|
||||
{
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
u64 *product = (u64*) malloc(2 * ndigits * sizeof(u64));
|
||||
#else
|
||||
u64 product[2 * ndigits];
|
||||
#endif
|
||||
|
||||
vli_mult(product, left, right, ndigits);
|
||||
#if 1
|
||||
vli_mod(result, product, mod, ndigits);
|
||||
#else
|
||||
if ( mod[1] == 0xFFFFFFFF00000000ull)
|
||||
vli_mmod_fast_sm2_256(result, product, mod, ndigits);
|
||||
else
|
||||
vli_mmod_fast_nist_256(result, product, mod, ndigits);
|
||||
#endif
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
free(product);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Computes result = left^2 % curve->p. */
|
||||
void vli_mod_square_fast(u64 *result, u64 *left, u64 *mod, u8 ndigits)
|
||||
{
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
u64 *product = (u64*) malloc(2 * ndigits * sizeof(u64));
|
||||
#else
|
||||
u64 product[2 * ndigits];
|
||||
#endif
|
||||
|
||||
vli_square(product, left, ndigits);
|
||||
#if 1
|
||||
vli_mod(result, product, mod, ndigits);
|
||||
|
||||
#else
|
||||
if ( mod[1] == 0xFFFFFFFF00000000ull)
|
||||
vli_mmod_fast_sm2_256(result, product, mod, ndigits);
|
||||
else
|
||||
vli_mmod_fast_nist_256(result, product, mod, ndigits);
|
||||
#endif
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
free(product);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Computes result = (left * right) % mod. */
|
||||
void vli_mod_mult(u64 *result, u64 *left, u64 *right, u64 *mod, u8 ndigits)
|
||||
{
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
u64 *product = (u64*) malloc(2 * ndigits * sizeof(u64));
|
||||
#else
|
||||
u64 product[2 * ndigits];
|
||||
#endif
|
||||
|
||||
vli_mult(product, left, right, ndigits);
|
||||
vli_mod(result, product, mod, ndigits);
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
free(product);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Computes result = left^2 % mod. */
|
||||
void vli_mod_square(u64 *result, u64 *left, u64 *mod, u8 ndigits)
|
||||
{
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
u64 *product = (u64*) malloc(2 * ndigits * sizeof(u64));
|
||||
#else
|
||||
u64 product[2 * ndigits];
|
||||
#endif
|
||||
|
||||
vli_square(product, left, ndigits);
|
||||
vli_mod(result, product, mod, ndigits);
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
free(product);
|
||||
#endif
|
||||
}
|
||||
|
||||
#define DIGIT_2MSB(x) (u64)(((x) >> (VLI_DIGIT_BITS - 2)) & 0x03)
|
||||
/* Computes result = left^p % mod. */
|
||||
void vli_mod_exp(u64 *result, u64 *left, u64 *p, u64 *mod, u8 ndigits)
|
||||
{
|
||||
u64 ci_bits, ci;
|
||||
u32 j, s;
|
||||
u32 digits;
|
||||
int i;
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
u64 *bpower[3]= {NULL, NULL, NULL};
|
||||
u64 *t = calloc(1, ndigits * VLI_DIGIT_BYTES);
|
||||
|
||||
bpower[0] = calloc(1, ndigits* VLI_DIGIT_BYTES);
|
||||
bpower[1] = calloc(1, ndigits* VLI_DIGIT_BYTES);
|
||||
bpower[2] = calloc(1, ndigits* VLI_DIGIT_BYTES);
|
||||
#else
|
||||
u64 bpower[3][ndigits], t[ndigits];
|
||||
#endif
|
||||
|
||||
|
||||
vli_set(bpower[0], left, ndigits);
|
||||
vli_mod_mult(bpower[1], bpower[0], left, mod, ndigits);
|
||||
vli_mod_mult(bpower[2], bpower[1], left, mod, ndigits);
|
||||
vli_clear(t, ndigits);
|
||||
t[0] = 1;
|
||||
|
||||
digits = vli_num_digits(p , ndigits);
|
||||
|
||||
i = digits - 1;
|
||||
for ( ; i >= 0; i--) {
|
||||
ci = p[i];
|
||||
ci_bits = VLI_DIGIT_BITS;
|
||||
|
||||
if (i == (digits - 1)) {
|
||||
while (!DIGIT_2MSB(ci)) {
|
||||
ci <<= 2;
|
||||
ci_bits -= 2;
|
||||
}
|
||||
}
|
||||
|
||||
for( j = 0; j < ci_bits; j += 2) {
|
||||
vli_mod_mult(t, t, t, mod, ndigits);
|
||||
vli_mod_mult(t, t, t, mod, ndigits);
|
||||
if ((s = DIGIT_2MSB(ci)) != 0) {
|
||||
vli_mod_mult(t, t, bpower[s-1], mod, ndigits);
|
||||
}
|
||||
ci <<= 2;
|
||||
}
|
||||
}
|
||||
|
||||
vli_set(result, t, ndigits);
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
free(bpower[0]);
|
||||
free(bpower[1]);
|
||||
free(bpower[2]);
|
||||
|
||||
free(t);
|
||||
#endif
|
||||
}
|
||||
|
||||
#define EVEN(vli) (!(vli[0] & 1))
|
||||
/* Computes result = (1 / p_input) % mod. All VLIs are the same size.
|
||||
* See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
|
||||
* https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
|
||||
*/
|
||||
void vli_mod_inv(u64 *result, u64 *input, u64 *mod, u8 ndigits)
|
||||
{
|
||||
u64 carry;
|
||||
int cmp_result;
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
u64 *a = (u64*) malloc(ndigits * sizeof(u64));
|
||||
u64 *b = (u64*) malloc(ndigits * sizeof(u64));
|
||||
|
||||
u64 *u = (u64*) malloc(ndigits * sizeof(u64));
|
||||
u64 *v = (u64*) malloc(ndigits * sizeof(u64));
|
||||
#else
|
||||
u64 a[ndigits], b[ndigits];
|
||||
u64 u[ndigits], v[ndigits];
|
||||
#endif
|
||||
|
||||
if (vli_is_zero(input, ndigits)) {
|
||||
vli_clear(result, ndigits);
|
||||
return;
|
||||
}
|
||||
|
||||
vli_set(a, input, ndigits);
|
||||
vli_set(b, mod, ndigits);
|
||||
vli_clear(u, ndigits);
|
||||
u[0] = 1;
|
||||
vli_clear(v, ndigits);
|
||||
|
||||
while ((cmp_result = vli_cmp(a, b, ndigits)) != 0) {
|
||||
carry = 0;
|
||||
|
||||
if (EVEN(a)) {
|
||||
vli_rshift(a, a, 1, ndigits);
|
||||
|
||||
if (!EVEN(u))
|
||||
carry = vli_add(u, u, mod, ndigits);
|
||||
|
||||
vli_rshift(u, u, 1, ndigits);
|
||||
if (carry)
|
||||
u[ndigits - 1] |= 0x8000000000000000ull;
|
||||
} else if (EVEN(b)) {
|
||||
vli_rshift(b, b, 1, ndigits);
|
||||
|
||||
if (!EVEN(v))
|
||||
carry = vli_add(v, v, mod, ndigits);
|
||||
|
||||
vli_rshift(v, v, 1, ndigits);
|
||||
if (carry)
|
||||
v[ndigits - 1] |= 0x8000000000000000ull;
|
||||
} else if (cmp_result > 0) {
|
||||
vli_sub(a, a, b, ndigits);
|
||||
vli_rshift(a, a, 1, ndigits);
|
||||
|
||||
if (vli_cmp(u, v, ndigits) < 0)
|
||||
vli_add(u, u, mod, ndigits);
|
||||
|
||||
vli_sub(u, u, v, ndigits);
|
||||
if (!EVEN(u))
|
||||
carry = vli_add(u, u, mod, ndigits);
|
||||
|
||||
vli_rshift(u, u, 1, ndigits);
|
||||
if (carry)
|
||||
u[ndigits - 1] |= 0x8000000000000000ull;
|
||||
} else {
|
||||
vli_sub(b, b, a, ndigits);
|
||||
vli_rshift(b, b, 1, ndigits);
|
||||
|
||||
if (vli_cmp(v, u, ndigits) < 0)
|
||||
vli_add(v, v, mod, ndigits);
|
||||
|
||||
vli_sub(v, v, u, ndigits);
|
||||
if (!EVEN(v))
|
||||
carry = vli_add(v, v, mod, ndigits);
|
||||
|
||||
vli_rshift(v, v, 1, ndigits);
|
||||
if (carry)
|
||||
v[ndigits - 1] |= 0x8000000000000000ull;
|
||||
}
|
||||
}
|
||||
|
||||
vli_set(result, u, ndigits);
|
||||
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
free(a);
|
||||
free(b);
|
||||
free(u);
|
||||
free(v);
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
void printHex(unsigned char *name, unsigned char *c, int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
#define _print printf
|
||||
_print ("\n---------------------[%s ,len = %d, start ]----------------------\n",name,n);
|
||||
for (i = 0; i < n; i++) {
|
||||
_print("0x%02X, ", c[i]);
|
||||
if ((i%4) == 3)
|
||||
_print(" ");
|
||||
|
||||
if ((i%16) == 15)
|
||||
_print("\n");
|
||||
}
|
||||
if ((i%16) != 0)
|
||||
_print("\n");
|
||||
_print("----------------------[%s end ]----------------------\n",name);
|
||||
}
|
||||
|
||||
void speed_test( char *name, int len)
|
||||
{
|
||||
static volatile unsigned long long byte = 0;
|
||||
static volatile unsigned long long count = 0;
|
||||
static time_t t1, t2;
|
||||
static int flag = 0;
|
||||
|
||||
if (!flag) {
|
||||
flag = 3;
|
||||
time(&t1);
|
||||
}
|
||||
|
||||
byte += len;
|
||||
count++;
|
||||
|
||||
time(&t2);
|
||||
if ((t2-t1) >= flag) {
|
||||
|
||||
unsigned long long byte_temp = byte;
|
||||
unsigned long long count_temp = count;
|
||||
|
||||
if (byte_temp)
|
||||
byte_temp = byte_temp*8/flag/1024/1024;
|
||||
|
||||
if (count_temp)
|
||||
count_temp = count_temp/flag;
|
||||
|
||||
printf(" %s speed = %lld Mb, %lld Hz \n", name, byte_temp, count_temp);
|
||||
t1 = t2;
|
||||
byte = 0;
|
||||
count = 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,323 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "ecc.h"
|
||||
#include "big.h"
|
||||
|
||||
/* Returns 1 if point is the point at infinity, 0 otherwise. */
|
||||
int ecc_point_is_zero(struct ecc_curve *curve, ecc_point *point)
|
||||
{
|
||||
return (vli_is_zero(point->x, curve->ndigits)
|
||||
&& vli_is_zero(point->y, curve->ndigits));
|
||||
}
|
||||
|
||||
/* Double in place */
|
||||
void ecc_point_double_jacobian(struct ecc_curve *curve, u64 *X1, u64 *Y1, u64 *Z1)
|
||||
{
|
||||
/* t1 = X, t2 = Y, t3 = Z */
|
||||
u64 t4[ECC_MAX_DIGITS];
|
||||
u64 t5[ECC_MAX_DIGITS];
|
||||
|
||||
if(vli_is_zero(Z1, curve->ndigits))
|
||||
return;
|
||||
|
||||
vli_mod_square_fast(t4, Y1, curve->p, curve->ndigits); /* t4 = y1^2 */
|
||||
vli_mod_mult_fast(t5, X1, t4, curve->p, curve->ndigits); /* t5 = x1*y1^2 = A */
|
||||
vli_mod_square_fast(t4, t4, curve->p, curve->ndigits); /* t4 = y1^4 */
|
||||
vli_mod_mult_fast(Y1, Y1, Z1, curve->p, curve->ndigits); /* t2 = y1*z1 = z3 */
|
||||
vli_mod_square_fast(Z1, Z1, curve->p, curve->ndigits); /* t3 = z1^2 */
|
||||
|
||||
vli_mod_add(X1, X1, Z1, curve->p, curve->ndigits); /* t1 = x1 + z1^2 */
|
||||
vli_mod_add(Z1, Z1, Z1, curve->p, curve->ndigits); /* t3 = 2*z1^2 */
|
||||
vli_mod_sub(Z1, X1, Z1, curve->p, curve->ndigits); /* t3 = x1 - z1^2 */
|
||||
vli_mod_mult_fast(X1, X1, Z1, curve->p, curve->ndigits); /* t1 = x1^2 - z1^4 */
|
||||
|
||||
vli_mod_add(Z1, X1, X1, curve->p, curve->ndigits); /* t3 = 2*(x1^2 - z1^4) */
|
||||
vli_mod_add(X1, X1, Z1, curve->p, curve->ndigits); /* t1 = 3*(x1^2 - z1^4) */
|
||||
if (vli_test_bit(X1, 0, curve->ndigits)) {
|
||||
u64 carry = vli_add(X1, X1, curve->p, curve->ndigits);
|
||||
vli_rshift(X1, X1, 1, curve->ndigits);
|
||||
X1[ECC_MAX_DIGITS-1] |= carry << 63;
|
||||
} else {
|
||||
vli_rshift(X1, X1, 1, curve->ndigits);
|
||||
}
|
||||
|
||||
/* t1 = 3/2*(x1^2 - z1^4) = B */
|
||||
vli_mod_square_fast(Z1, X1, curve->p, curve->ndigits); /* t3 = B^2 */
|
||||
vli_mod_sub(Z1, Z1, t5, curve->p, curve->ndigits); /* t3 = B^2 - A */
|
||||
vli_mod_sub(Z1, Z1, t5, curve->p, curve->ndigits); /* t3 = B^2 - 2A = x3 */
|
||||
vli_mod_sub(t5, t5, Z1, curve->p, curve->ndigits); /* t5 = A - x3 */
|
||||
vli_mod_mult_fast(X1, X1, t5, curve->p, curve->ndigits); /* t1 = B * (A - x3) */
|
||||
vli_mod_sub(t4, X1, t4, curve->p, curve->ndigits); /* t4 = B * (A - x3) - y1^4 = y3 */
|
||||
|
||||
vli_set(X1, Z1, curve->ndigits);
|
||||
vli_set(Z1, Y1, curve->ndigits);
|
||||
vli_set(Y1, t4, curve->ndigits);
|
||||
}
|
||||
|
||||
/* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
|
||||
void apply_z(struct ecc_curve *curve, u64 *X1, u64 *Y1, u64 *Z)
|
||||
{
|
||||
u64 t1[ECC_MAX_DIGITS];
|
||||
|
||||
vli_mod_square_fast(t1, Z, curve->p, curve->ndigits); /* z^2 */
|
||||
vli_mod_mult_fast(X1, X1, t1, curve->p, curve->ndigits); /* x1 * z^2 */
|
||||
vli_mod_mult_fast(t1, t1, Z, curve->p, curve->ndigits); /* z^3 */
|
||||
vli_mod_mult_fast(Y1, Y1, t1, curve->p, curve->ndigits); /* y1 * z^3 */
|
||||
}
|
||||
|
||||
/* P = (x1, y1) => 2P, (x2, y2) => P' */
|
||||
void XYcZ_initial_double(struct ecc_curve *curve, u64 *X1, u64 *Y1, u64 *X2, u64 *Y2, u64 *initialZ)
|
||||
{
|
||||
u64 z[ECC_MAX_DIGITS];
|
||||
|
||||
vli_set(X2, X1, curve->ndigits);
|
||||
vli_set(Y2, Y1, curve->ndigits);
|
||||
|
||||
if (initialZ) {
|
||||
vli_set(z, initialZ, curve->ndigits);
|
||||
} else {
|
||||
vli_clear(z, curve->ndigits);
|
||||
z[0] = 1;
|
||||
}
|
||||
apply_z(curve, X1, Y1, z);
|
||||
|
||||
ecc_point_double_jacobian(curve, X1, Y1, z);
|
||||
|
||||
apply_z(curve, X2, Y2, z);
|
||||
}
|
||||
|
||||
/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
|
||||
Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
|
||||
or P => P', Q => P + Q
|
||||
*/
|
||||
void XYcZ_add(struct ecc_curve *curve, u64 *X1, u64 *Y1, u64 *X2, u64 *Y2)
|
||||
{
|
||||
/* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
|
||||
u64 t5[ECC_MAX_DIGITS];
|
||||
|
||||
vli_mod_sub(t5, X2, X1, curve->p, curve->ndigits); /* t5 = x2 - x1 */
|
||||
vli_mod_square_fast(t5, t5, curve->p, curve->ndigits); /* t5 = (x2 - x1)^2 = A */
|
||||
vli_mod_mult_fast(X1, X1, t5, curve->p, curve->ndigits); /* t1 = x1*A = B */
|
||||
vli_mod_mult_fast(X2, X2, t5, curve->p, curve->ndigits); /* t3 = x2*A = C */
|
||||
vli_mod_sub(Y2, Y2, Y1, curve->p, curve->ndigits); /* t4 = y2 - y1 */
|
||||
vli_mod_square_fast(t5, Y2, curve->p, curve->ndigits); /* t5 = (y2 - y1)^2 = D */
|
||||
|
||||
vli_mod_sub(t5, t5, X1, curve->p, curve->ndigits); /* t5 = D - B */
|
||||
vli_mod_sub(t5, t5, X2, curve->p, curve->ndigits); /* t5 = D - B - C = x3 */
|
||||
vli_mod_sub(X2, X2, X1, curve->p, curve->ndigits); /* t3 = C - B */
|
||||
vli_mod_mult_fast(Y1, Y1, X2, curve->p, curve->ndigits); /* t2 = y1*(C - B) */
|
||||
vli_mod_sub(X2, X1, t5, curve->p, curve->ndigits); /* t3 = B - x3 */
|
||||
vli_mod_mult_fast(Y2, Y2, X2, curve->p, curve->ndigits); /* t4 = (y2 - y1)*(B - x3) */
|
||||
vli_mod_sub(Y2, Y2, Y1, curve->p, curve->ndigits); /* t4 = y3 */
|
||||
|
||||
vli_set(X2, t5, curve->ndigits);
|
||||
}
|
||||
|
||||
/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
|
||||
* Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
|
||||
* or P => P - Q, Q => P + Q
|
||||
*/
|
||||
void XYcZ_addC(struct ecc_curve *curve, u64 *X1, u64 *Y1, u64 *X2, u64 *Y2)
|
||||
{
|
||||
/* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
|
||||
u64 t5[ECC_MAX_DIGITS];
|
||||
u64 t6[ECC_MAX_DIGITS];
|
||||
u64 t7[ECC_MAX_DIGITS];
|
||||
|
||||
vli_mod_sub(t5, X2, X1, curve->p, curve->ndigits); /* t5 = x2 - x1 */
|
||||
vli_mod_square_fast(t5, t5, curve->p, curve->ndigits); /* t5 = (x2 - x1)^2 = A */
|
||||
vli_mod_mult_fast(X1, X1, t5, curve->p, curve->ndigits); /* t1 = x1*A = B */
|
||||
vli_mod_mult_fast(X2, X2, t5, curve->p, curve->ndigits); /* t3 = x2*A = C */
|
||||
vli_mod_add(t5, Y2, Y1, curve->p, curve->ndigits); /* t4 = y2 + y1 */
|
||||
vli_mod_sub(Y2, Y2, Y1, curve->p, curve->ndigits); /* t4 = y2 - y1 */
|
||||
|
||||
vli_mod_sub(t6, X2, X1, curve->p, curve->ndigits); /* t6 = C - B */
|
||||
vli_mod_mult_fast(Y1, Y1, t6, curve->p, curve->ndigits); /* t2 = y1 * (C - B) */
|
||||
vli_mod_add(t6, X1, X2, curve->p, curve->ndigits); /* t6 = B + C */
|
||||
vli_mod_square_fast(X2, Y2, curve->p, curve->ndigits); /* t3 = (y2 - y1)^2 */
|
||||
vli_mod_sub(X2, X2, t6, curve->p, curve->ndigits); /* t3 = x3 */
|
||||
|
||||
vli_mod_sub(t7, X1, X2, curve->p, curve->ndigits); /* t7 = B - x3 */
|
||||
vli_mod_mult_fast(Y2, Y2, t7, curve->p, curve->ndigits); /* t4 = (y2 - y1)*(B - x3) */
|
||||
vli_mod_sub(Y2, Y2, Y1, curve->p, curve->ndigits); /* t4 = y3 */
|
||||
|
||||
vli_mod_square_fast(t7, t5, curve->p, curve->ndigits); /* t7 = (y2 + y1)^2 = F */
|
||||
vli_mod_sub(t7, t7, t6, curve->p, curve->ndigits); /* t7 = x3' */
|
||||
vli_mod_sub(t6, t7, X1, curve->p, curve->ndigits); /* t6 = x3' - B */
|
||||
vli_mod_mult_fast(t6, t6, t5, curve->p, curve->ndigits); /* t6 = (y2 + y1)*(x3' - B) */
|
||||
vli_mod_sub(Y1, t6, Y1, curve->p, curve->ndigits); /* t2 = y3' */
|
||||
|
||||
vli_set(X1, t7, curve->ndigits);
|
||||
}
|
||||
|
||||
void ecc_point_mult(struct ecc_curve *curve, ecc_point *result, ecc_point *point, u64 *scalar, u64 *initialZ)
|
||||
{
|
||||
/* R0 and R1 */
|
||||
u64 Rx[2][ECC_MAX_DIGITS];
|
||||
u64 Ry[2][ECC_MAX_DIGITS];
|
||||
u64 z[ECC_MAX_DIGITS];
|
||||
int i, nb;
|
||||
|
||||
vli_set(Rx[1], point->x, curve->ndigits);
|
||||
vli_set(Ry[1], point->y, curve->ndigits);
|
||||
|
||||
XYcZ_initial_double(curve, Rx[1], Ry[1], Rx[0], Ry[0], initialZ);
|
||||
|
||||
for (i = vli_num_bits(scalar, curve->ndigits) - 2; i > 0; --i) {
|
||||
nb = !vli_test_bit(scalar, i, curve->ndigits);
|
||||
XYcZ_addC(curve, Rx[1-nb], Ry[1-nb], Rx[nb], Ry[nb]);
|
||||
XYcZ_add(curve, Rx[nb], Ry[nb], Rx[1-nb], Ry[1-nb]);
|
||||
}
|
||||
|
||||
nb = !vli_test_bit(scalar, 0, curve->ndigits);
|
||||
XYcZ_addC(curve, Rx[1-nb], Ry[1-nb], Rx[nb], Ry[nb]);
|
||||
|
||||
/* Find final 1/Z value. */
|
||||
vli_mod_sub(z, Rx[1], Rx[0], curve->p, curve->ndigits); /* X1 - X0 */
|
||||
vli_mod_mult_fast(z, z, Ry[1-nb], curve->p, curve->ndigits); /* Yb * (X1 - X0) */
|
||||
vli_mod_mult_fast(z, z, point->x, curve->p, curve->ndigits); /* xP * Yb * (X1 - X0) */
|
||||
vli_mod_inv(z, z, curve->p, curve->ndigits); /* 1 / (xP * Yb * (X1 - X0)) */
|
||||
vli_mod_mult_fast(z, z, point->y, curve->p, curve->ndigits); /* yP / (xP * Yb * (X1 - X0)) */
|
||||
vli_mod_mult_fast(z, z, Rx[1-nb], curve->p, curve->ndigits); /* Xb * yP / (xP * Yb * (X1 - X0)) */
|
||||
/* End 1/Z calculation */
|
||||
|
||||
XYcZ_add(curve, Rx[nb], Ry[nb], Rx[1-nb], Ry[1-nb]);
|
||||
|
||||
apply_z(curve, Rx[0], Ry[0], z);
|
||||
|
||||
vli_set(result->x, Rx[0], curve->ndigits);
|
||||
vli_set(result->y, Ry[0], curve->ndigits);
|
||||
}
|
||||
|
||||
static u32 max(u32 a, u32 b)
|
||||
{
|
||||
return (a > b ? a : b);
|
||||
}
|
||||
|
||||
void ecc_point_mult2(struct ecc_curve *curve, ecc_point *result, ecc_point *g, ecc_point *p, u64 *s, u64 *t)
|
||||
{
|
||||
u64 tx[ECC_MAX_DIGITS];
|
||||
u64 ty[ECC_MAX_DIGITS];
|
||||
u64 tz[ECC_MAX_DIGITS];
|
||||
u64 z[ECC_MAX_DIGITS];
|
||||
ecc_point sum;
|
||||
u64 *rx;
|
||||
u64 *ry;
|
||||
int i;
|
||||
|
||||
/*For C89*/
|
||||
u32 numBits;
|
||||
ecc_point *points[4];
|
||||
int index;
|
||||
ecc_point *point;
|
||||
|
||||
rx = result->x;
|
||||
ry = result->y;
|
||||
|
||||
/* Calculate sum = G + Q. */
|
||||
vli_set(sum.x, p->x, curve->ndigits);
|
||||
vli_set(sum.y, p->y, curve->ndigits);
|
||||
vli_set(tx, g->x, curve->ndigits);
|
||||
vli_set(ty, g->y, curve->ndigits);
|
||||
|
||||
vli_mod_sub(z, sum.x, tx, curve->p, curve->ndigits); /* Z = x2 - x1 */
|
||||
XYcZ_add(curve, tx, ty, sum.x, sum.y);
|
||||
vli_mod_inv(z, z, curve->p, curve->ndigits); /* Z = 1/Z */
|
||||
apply_z(curve, sum.x, sum.y, z);
|
||||
|
||||
/*For C89*/
|
||||
points[0] = NULL;
|
||||
points[1] = g;
|
||||
points[2] = p;
|
||||
points[3] = ∑
|
||||
|
||||
/* Use Shamir's trick to calculate u1*G + u2*Q */
|
||||
|
||||
numBits = max(vli_num_bits(s, curve->ndigits), vli_num_bits(t, curve->ndigits));
|
||||
|
||||
point = points[(!!vli_test_bit(s, numBits-1, curve->ndigits))
|
||||
| ((!!vli_test_bit(t, numBits-1, curve->ndigits)) << 1)];
|
||||
vli_set(rx, point->x, curve->ndigits);
|
||||
vli_set(ry, point->y, curve->ndigits);
|
||||
vli_clear(z, curve->ndigits);
|
||||
z[0] = 1;
|
||||
|
||||
for (i = numBits - 2; i >= 0; --i) {
|
||||
ecc_point_double_jacobian(curve, rx, ry, z);
|
||||
|
||||
index = (!!vli_test_bit(s, i, curve->ndigits)) | ((!!vli_test_bit(t, i, curve->ndigits)) << 1);
|
||||
point = points[index];
|
||||
if(point) {
|
||||
vli_set(tx, point->x, curve->ndigits);
|
||||
vli_set(ty, point->y, curve->ndigits);
|
||||
apply_z(curve, tx, ty, z);
|
||||
vli_mod_sub(tz, rx, tx, curve->p, curve->ndigits); /* Z = x2 - x1 */
|
||||
XYcZ_add(curve, tx, ty, rx, ry);
|
||||
vli_mod_mult_fast(z, z, tz, curve->p, curve->ndigits);
|
||||
}
|
||||
}
|
||||
|
||||
vli_mod_inv(z, z, curve->p, curve->ndigits); /* Z = 1/Z */
|
||||
apply_z(curve, rx, ry, z);
|
||||
}
|
||||
|
||||
void ecc_point_add(struct ecc_curve *curve, ecc_point *result, ecc_point *left, ecc_point *right)
|
||||
{
|
||||
u64 x1[ECC_MAX_DIGITS];
|
||||
u64 y1[ECC_MAX_DIGITS];
|
||||
u64 x2[ECC_MAX_DIGITS];
|
||||
u64 y2[ECC_MAX_DIGITS];
|
||||
u64 z[ECC_MAX_DIGITS];
|
||||
|
||||
vli_set(x1, left->x, curve->ndigits);
|
||||
vli_set(y1, left->y, curve->ndigits);
|
||||
vli_set(x2, right->x, curve->ndigits);
|
||||
vli_set(y2, right->y, curve->ndigits);
|
||||
|
||||
vli_mod_sub(z, x2, x1, curve->p, curve->ndigits); /* Z = x2 - x1 */
|
||||
|
||||
XYcZ_add(curve, x1, y1, x2, y2);
|
||||
vli_mod_inv(z, z, curve->p, curve->ndigits); /* Z = 1/Z */
|
||||
apply_z(curve, x2,y2, z);
|
||||
|
||||
vli_set(result->x, x2, curve->ndigits);
|
||||
vli_set(result->y, y2, curve->ndigits);
|
||||
}
|
||||
|
||||
void ecc_bytes2native(u64 *native, void *bytes, u8 ndigits)
|
||||
{
|
||||
u64 *_bytes = (u64*)bytes;
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < ndigits/2; ++i) {
|
||||
if (native == _bytes) {
|
||||
u64 temp;
|
||||
temp = be64_to_cpu(native[i]);
|
||||
native[i] = be64_to_cpu(_bytes[ndigits - i - 1]);
|
||||
_bytes[ndigits - i - 1] = temp;
|
||||
}else {
|
||||
native[i] = be64_to_cpu(_bytes[ndigits - i - 1]);
|
||||
native[ndigits - i - 1] = be64_to_cpu(_bytes[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ecc_native2bytes(void *bytes, u64 *native, u8 ndigits)
|
||||
{
|
||||
u64 *_bytes = (u64*)bytes;
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < ndigits/2; ++i) {
|
||||
if (_bytes == native) {
|
||||
u64 temp;
|
||||
temp = cpu_to_be64(_bytes[ndigits - i - 1]);
|
||||
_bytes[ndigits - i - 1] = cpu_to_be64(native[i]);
|
||||
native[i] = temp;
|
||||
} else {
|
||||
_bytes[i] = cpu_to_be64(native[ndigits - i - 1]);
|
||||
_bytes[ndigits - i - 1] = cpu_to_be64(native[i]);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,220 @@
|
|||
#include "typedef.h"
|
||||
|
||||
#ifdef __BUILD_NO_OS__
|
||||
|
||||
inline u16 _htons(u16 hs)
|
||||
{
|
||||
return (ENDIANNESS=='l') ? __swap16(hs): hs;
|
||||
}
|
||||
|
||||
inline u32 _htonl(u32 hl)
|
||||
{
|
||||
return (ENDIANNESS=='l') ? __swap32(hl): hl;
|
||||
}
|
||||
|
||||
inline u32 _htonl64(u64 hl)
|
||||
{
|
||||
return (ENDIANNESS=='l') ? __swap64(hl): hl;
|
||||
}
|
||||
|
||||
inline u16 _ntohs(u16 ns)
|
||||
{
|
||||
return (ENDIANNESS=='l') ? __swap16(ns): ns;
|
||||
}
|
||||
|
||||
inline u64 _ntohl(u32 nl)
|
||||
{
|
||||
return (ENDIANNESS=='l') ? __swap32(nl): nl;
|
||||
}
|
||||
|
||||
inline u64 _ntohl64(u64 nl)
|
||||
{
|
||||
return (ENDIANNESS=='l') ? __swap64(nl): nl;
|
||||
}
|
||||
|
||||
inline u16 le16_to_cpu(u16 data)
|
||||
{
|
||||
return (ENDIANNESS=='b') ? __swap16(data):data;
|
||||
}
|
||||
inline u32 le32_to_cpu(u32 data)
|
||||
{
|
||||
return (ENDIANNESS=='b') ? __swap32(data):data;
|
||||
}
|
||||
inline u64 le64_to_cpu(u64 data)
|
||||
{
|
||||
return (ENDIANNESS=='b') ? __swap64(data):data;
|
||||
}
|
||||
inline u16 cpu_to_le16(u16 data)
|
||||
{
|
||||
return (ENDIANNESS=='b') ? __swap16(data):data;
|
||||
}
|
||||
inline u32 cpu_to_le32(u32 data)
|
||||
{
|
||||
return (ENDIANNESS=='b') ? __swap32(data):data;
|
||||
}
|
||||
inline u64 cpu_to_le64(u64 data)
|
||||
{
|
||||
return (ENDIANNESS=='b') ? __swap64(data):data;
|
||||
}
|
||||
|
||||
inline u16 be16_to_cpu(u16 data)
|
||||
{
|
||||
return _ntohs(data);
|
||||
}
|
||||
|
||||
inline u32 be32_to_cpu(u32 data)
|
||||
{
|
||||
return _ntohl(data);
|
||||
}
|
||||
|
||||
inline u16 cpu_to_be16(u16 data)
|
||||
{
|
||||
return _ntohs(data);
|
||||
}
|
||||
|
||||
inline u32 cpu_to_be32(u32 data)
|
||||
{
|
||||
return _ntohl(data);
|
||||
}
|
||||
|
||||
inline u64 be64_to_cpu(u64 data)
|
||||
{
|
||||
return _ntohl64(data);
|
||||
}
|
||||
|
||||
inline u64 cpu_to_be64(u64 data)
|
||||
{
|
||||
return _ntohl64(data);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
inline u16 __get_unaligned_le16(const u8 *p)
|
||||
{
|
||||
return p[0] | p[1] << 8;
|
||||
}
|
||||
|
||||
inline u32 __get_unaligned_le32(const u8 *p)
|
||||
{
|
||||
return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
|
||||
}
|
||||
|
||||
inline u64 __get_unaligned_le64(const u8 *p)
|
||||
{
|
||||
return (u64)__get_unaligned_le32(p + 4) << 32 |
|
||||
__get_unaligned_le32(p);
|
||||
}
|
||||
|
||||
inline void __put_unaligned_le16(u16 val, u8 *p)
|
||||
{
|
||||
*p++ = val;
|
||||
*p++ = val >> 8;
|
||||
}
|
||||
|
||||
inline void __put_unaligned_le32(u32 val, u8 *p)
|
||||
{
|
||||
__put_unaligned_le16(val >> 16, p + 2);
|
||||
__put_unaligned_le16(val, p);
|
||||
}
|
||||
|
||||
inline void __put_unaligned_le64(u64 val, u8 *p)
|
||||
{
|
||||
__put_unaligned_le32(val >> 32, p + 4);
|
||||
__put_unaligned_le32(val, p);
|
||||
}
|
||||
|
||||
inline u16 get_unaligned_le16(const void *p)
|
||||
{
|
||||
return __get_unaligned_le16((const u8 *)p);
|
||||
}
|
||||
|
||||
inline u32 get_unaligned_le32(const void *p)
|
||||
{
|
||||
return __get_unaligned_le32((const u8 *)p);
|
||||
}
|
||||
|
||||
inline u64 get_unaligned_le64(const void *p)
|
||||
{
|
||||
return __get_unaligned_le64((const u8 *)p);
|
||||
}
|
||||
|
||||
inline void put_unaligned_le16(u16 val, void *p)
|
||||
{
|
||||
__put_unaligned_le16(val, (u8*)p);
|
||||
}
|
||||
|
||||
inline void put_unaligned_le32(u32 val, void *p)
|
||||
{
|
||||
__put_unaligned_le32(val, (u8*)p);
|
||||
}
|
||||
|
||||
inline void put_unaligned_le64(u64 val, void *p)
|
||||
{
|
||||
__put_unaligned_le64(val, (u8*)p);
|
||||
}
|
||||
|
||||
inline u16 __get_unaligned_be16(const u8 *p)
|
||||
{
|
||||
return p[0] << 8 | p[1];
|
||||
}
|
||||
|
||||
inline u32 __get_unaligned_be32(const u8 *p)
|
||||
{
|
||||
return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
|
||||
}
|
||||
|
||||
inline u64 __get_unaligned_be64(const u8 *p)
|
||||
{
|
||||
return (u64)__get_unaligned_be32(p) << 32 |
|
||||
__get_unaligned_be32(p + 4);
|
||||
}
|
||||
|
||||
inline void __put_unaligned_be16(u16 val, u8 *p)
|
||||
{
|
||||
*p++ = val >> 8;
|
||||
*p++ = val;
|
||||
}
|
||||
|
||||
inline void __put_unaligned_be32(u32 val, u8 *p)
|
||||
{
|
||||
__put_unaligned_be16(val >> 16, p);
|
||||
__put_unaligned_be16(val, p + 2);
|
||||
}
|
||||
|
||||
inline void __put_unaligned_be64(u64 val, u8 *p)
|
||||
{
|
||||
__put_unaligned_be32(val >> 32, p);
|
||||
__put_unaligned_be32(val, p + 4);
|
||||
}
|
||||
|
||||
inline u16 get_unaligned_be16(const void *p)
|
||||
{
|
||||
return __get_unaligned_be16((const u8 *)p);
|
||||
}
|
||||
|
||||
inline u32 get_unaligned_be32(const void *p)
|
||||
{
|
||||
return __get_unaligned_be32((const u8 *)p);
|
||||
}
|
||||
|
||||
inline u64 get_unaligned_be64(const void *p)
|
||||
{
|
||||
return __get_unaligned_be64((const u8 *)p);
|
||||
}
|
||||
|
||||
inline void put_unaligned_be16(u16 val, void *p)
|
||||
{
|
||||
__put_unaligned_be16(val, (u8*)p);
|
||||
}
|
||||
|
||||
inline void put_unaligned_be32(u32 val, void *p)
|
||||
{
|
||||
__put_unaligned_be32(val, (u8*)p);
|
||||
}
|
||||
|
||||
inline void put_unaligned_be64(u64 val, void *p)
|
||||
{
|
||||
__put_unaligned_be64(val, (u8*)p);
|
||||
}
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef RANDOM_SOFTWARE
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
#include "random.h"
|
||||
|
||||
|
||||
static int gRadomFixedFlag;
|
||||
|
||||
/*SM2 Standard Random*/
|
||||
static u8 gRandomFixed[32]= {0x21,0xBC,0xC1,0xEA,0x0D,0xB8,0x54,0x6D,0xCE,0xE4,0xDB,0x3C,0xFA,0xC1,0x3C,0xEF,
|
||||
0xCC,0x2D,0xC0,0xD9,0x3A,0x0F,0x68,0x16,0x1A,0x86,0x06,0xD5,0x27,0x6E,0x27,0x59
|
||||
};
|
||||
|
||||
|
||||
void vli_set_random_mode(u32 mode)
|
||||
{
|
||||
gRadomFixedFlag = mode;
|
||||
}
|
||||
|
||||
int vli_get_random_mode(void)
|
||||
{
|
||||
return gRadomFixedFlag;
|
||||
}
|
||||
|
||||
void vli_set_random_fixed(u8 *data, u32 len)
|
||||
{
|
||||
vli_set_random_mode(1);
|
||||
|
||||
if(len == sizeof(gRandomFixed))
|
||||
{
|
||||
memcpy(gRandomFixed, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int vli_get_random(u8 *data, u32 len)
|
||||
{
|
||||
|
||||
#ifdef RANDOM_SOFTWARE
|
||||
int i;
|
||||
int j;
|
||||
static int counter;
|
||||
|
||||
if((!gRadomFixedFlag)
|
||||
||(len != sizeof(gRandomFixed)))
|
||||
{
|
||||
srand(counter++);
|
||||
counter +=j ;
|
||||
|
||||
for(i = 0; i < len; i++)
|
||||
{
|
||||
data[i]= rand();
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(data, gRandomFixed, len);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int fd = open("/dev/urandom", O_RDONLY);
|
||||
int ret = -1;
|
||||
|
||||
if (fd > 0) {
|
||||
ret = read(fd, data, len);
|
||||
close(fd);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
memset(data, 0x33, len);
|
||||
*/
|
||||
|
||||
return ret;
|
||||
|
||||
#endif
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,664 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "random.h"
|
||||
#include "big.h"
|
||||
#include "ecc.h"
|
||||
#include "sm2.h"
|
||||
#include "sm3.h"
|
||||
|
||||
|
||||
/*For C89*/
|
||||
#ifdef PREDEF_STANDARD_C_1989
|
||||
|
||||
struct ecc_curve sm2_curve = {
|
||||
ECC_MAX_DIGITS,
|
||||
{
|
||||
{
|
||||
0x715A4589334C74C7ull, 0x8FE30BBFF2660BE1ull,
|
||||
0x5F9904466A39C994ull, 0x32C4AE2C1F198119ull
|
||||
},
|
||||
{
|
||||
0x02DF32E52139F0A0ull, 0xD0A9877CC62A4740ull,
|
||||
0x59BDCEE36B692153ull, 0xBC3736A2F4F6779Cull
|
||||
},
|
||||
},
|
||||
{
|
||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull,
|
||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
||||
},
|
||||
{
|
||||
0x53BBF40939D54123ull, 0x7203DF6B21C6052Bull,
|
||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
||||
},
|
||||
{
|
||||
0x0000000000000001ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull,
|
||||
},
|
||||
{
|
||||
0xFFFFFFFFFFFFFFFCull, 0xFFFFFFFF00000000ull,
|
||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
||||
},
|
||||
{
|
||||
0xDDBCBD414D940E93ull, 0xF39789F515AB8F92ull,
|
||||
0x4D5A9E4BCF6509A7ull, 0x28E9FA9E9D9F5E34ull
|
||||
},
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
struct ecc_curve sm2_curve = {
|
||||
.ndigits = ECC_MAX_DIGITS,
|
||||
.g = {
|
||||
.x = {
|
||||
0x715A4589334C74C7ull, 0x8FE30BBFF2660BE1ull,
|
||||
0x5F9904466A39C994ull, 0x32C4AE2C1F198119ull
|
||||
},
|
||||
.y = {
|
||||
0x02DF32E52139F0A0ull, 0xD0A9877CC62A4740ull,
|
||||
0x59BDCEE36B692153ull, 0xBC3736A2F4F6779Cull
|
||||
},
|
||||
},
|
||||
.p = {
|
||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull,
|
||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
||||
},
|
||||
.n = {
|
||||
0x53BBF40939D54123ull, 0x7203DF6B21C6052Bull,
|
||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
||||
},
|
||||
.h = {
|
||||
0x0000000000000001ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull,
|
||||
},
|
||||
.a = {
|
||||
0xFFFFFFFFFFFFFFFCull, 0xFFFFFFFF00000000ull,
|
||||
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFEFFFFFFFFull
|
||||
},
|
||||
.b = {
|
||||
0xDDBCBD414D940E93ull, 0xF39789F515AB8F92ull,
|
||||
0x4D5A9E4BCF6509A7ull, 0x28E9FA9E9D9F5E34ull
|
||||
},
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
/*x¯2 = 2w + (x2&(2w − 1))*/
|
||||
void sm2_w(u64 *result, u64 *x)
|
||||
{
|
||||
result[0] = x[0];
|
||||
result[1] = x[1];
|
||||
result[1] |= 0x80;
|
||||
result[2] = 0;
|
||||
result[3] = 0;
|
||||
}
|
||||
|
||||
void sm3_kdf(u8 *Z ,u32 zlen, u8 *K, u32 klen)
|
||||
{
|
||||
u32 ct = 0x00000001;
|
||||
u8 ct_char[32];
|
||||
u8 *hash = K ;
|
||||
u32 i, t;
|
||||
struct sm3_ctx md[1];
|
||||
|
||||
t = klen/ECC_NUMWORD;
|
||||
|
||||
/*
|
||||
//s4: K=Ha1||Ha2||...
|
||||
*/
|
||||
|
||||
for (i = 0; i < t; i++) {
|
||||
|
||||
/*
|
||||
//s2: Hai=Hv(Z||ct)
|
||||
*/
|
||||
|
||||
sm3_init(md);
|
||||
sm3_update(md, Z, zlen);
|
||||
put_unaligned_be32(ct, ct_char);
|
||||
sm3_update(md, ct_char, 4);
|
||||
sm3_final(md, hash);
|
||||
hash += 32;
|
||||
ct++;
|
||||
}
|
||||
|
||||
t = klen%ECC_NUMWORD;
|
||||
if (t) {
|
||||
sm3_init(md);
|
||||
sm3_update(md, Z, zlen);
|
||||
put_unaligned_be32(ct, ct_char);
|
||||
sm3_update(md, ct_char, 4);
|
||||
sm3_final(md, ct_char);
|
||||
memcpy(hash, ct_char, t);
|
||||
}
|
||||
}
|
||||
|
||||
void sm3_z(u8 *id, u32 idlen, ecc_point *pub, u8 *hash)
|
||||
{
|
||||
u8 a[ECC_NUMWORD];
|
||||
u8 b[ECC_NUMWORD];
|
||||
u8 x[ECC_NUMWORD];
|
||||
u8 y[ECC_NUMWORD];
|
||||
u8 idlen_char[2];
|
||||
struct sm3_ctx md[1];
|
||||
|
||||
put_unaligned_be16(idlen<<3, idlen_char);
|
||||
|
||||
ecc_bytes2native((u64*)a, sm2_curve.a, 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*)y, sm2_curve.g.y, sm2_curve.ndigits);
|
||||
|
||||
sm3_init(md);
|
||||
sm3_update(md, idlen_char, 2);
|
||||
sm3_update(md, id, idlen);
|
||||
sm3_update(md, a, ECC_NUMWORD);
|
||||
sm3_update(md, b, ECC_NUMWORD);
|
||||
sm3_update(md, x, ECC_NUMWORD);
|
||||
sm3_update(md, y, ECC_NUMWORD);
|
||||
sm3_update(md, (u8*)pub->x, ECC_NUMWORD);
|
||||
sm3_update(md, (u8*)pub->y, ECC_NUMWORD);
|
||||
sm3_final(md, hash);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int sm2_valid_public_key(ecc_point *publicKey)
|
||||
{
|
||||
u64 na[ECC_MAX_DIGITS] = {3}; /* a mod p = (-3) mod p */
|
||||
u64 tmp1[ECC_MAX_DIGITS];
|
||||
u64 tmp2[ECC_MAX_DIGITS];
|
||||
|
||||
if (ecc_point_is_zero(&sm2_curve, publicKey))
|
||||
return 1;
|
||||
|
||||
if (vli_cmp(sm2_curve.p, publicKey->x, sm2_curve.ndigits) != 1
|
||||
|| vli_cmp(sm2_curve.p, publicKey->y, sm2_curve.ndigits) != 1)
|
||||
return 1;
|
||||
|
||||
/* tmp1 = y^2 */
|
||||
vli_mod_square_fast(tmp1, publicKey->y, sm2_curve.p, sm2_curve.ndigits);
|
||||
/* tmp2 = x^2 */
|
||||
vli_mod_square_fast(tmp2, publicKey->x, sm2_curve.p, sm2_curve.ndigits);
|
||||
/* tmp2 = x^2 + a = x^2 - 3 */
|
||||
vli_mod_sub(tmp2, tmp2, na, sm2_curve.p, sm2_curve.ndigits);
|
||||
/* tmp2 = x^3 + ax */
|
||||
vli_mod_mult_fast(tmp2, tmp2, publicKey->x, sm2_curve.p, sm2_curve.ndigits);
|
||||
/* tmp2 = x^3 + ax + b */
|
||||
vli_mod_add(tmp2, tmp2, sm2_curve.b, sm2_curve.p, sm2_curve.ndigits);
|
||||
|
||||
/* Make sure that y^2 == x^3 + ax + b */
|
||||
if (vli_cmp(tmp1, tmp2, sm2_curve.ndigits) != 0)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sm2_make_prikey(u8 *prikey)
|
||||
{
|
||||
ecc_point pub[1];
|
||||
u64 pri[ECC_MAX_DIGITS];
|
||||
int i = 10;
|
||||
|
||||
do {
|
||||
vli_get_random((u8*)pri, ECC_NUMWORD);
|
||||
if(vli_cmp(sm2_curve.n, pri, sm2_curve.ndigits) != 1) {
|
||||
vli_sub(pri, pri, sm2_curve.n, sm2_curve.ndigits);
|
||||
}
|
||||
|
||||
/* The private key cannot be 0 (mod p). */
|
||||
if(!vli_is_zero(pri, sm2_curve.ndigits)) {
|
||||
ecc_native2bytes(prikey, pri, sm2_curve.ndigits);
|
||||
return 0;
|
||||
}
|
||||
} while(i--);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int sm2_make_pubkey(u8 *prikey, ecc_point *pubkey)
|
||||
{
|
||||
ecc_point pub[1];
|
||||
u64 pri[ECC_MAX_DIGITS];
|
||||
|
||||
ecc_bytes2native(pri, prikey, sm2_curve.ndigits);
|
||||
ecc_point_mult(&sm2_curve, pub, &sm2_curve.g, pri, NULL);
|
||||
ecc_native2bytes(pubkey->x, pub->x, sm2_curve.ndigits);
|
||||
ecc_native2bytes(pubkey->y, pub->y, sm2_curve.ndigits);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sm2_make_keypair(u8 *prikey, ecc_point *pubkey)
|
||||
{
|
||||
sm2_make_prikey(prikey);
|
||||
sm2_make_pubkey(prikey, pubkey);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sm2_point_mult(ecc_point *G, u8 *k, ecc_point *P)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
ecc_point G_[1];
|
||||
ecc_point P_[1];
|
||||
u64 k_[ECC_MAX_DIGITS];
|
||||
|
||||
ecc_bytes2native(k_, k, sm2_curve.ndigits);
|
||||
ecc_bytes2native(G_->x, G->x, sm2_curve.ndigits);
|
||||
ecc_bytes2native(G_->y, G->y, sm2_curve.ndigits);
|
||||
|
||||
ecc_point_mult(&sm2_curve, P_, G_, k_, NULL);
|
||||
|
||||
ecc_native2bytes(P->x, P_->x, sm2_curve.ndigits);
|
||||
ecc_native2bytes(P->y, P_->y, sm2_curve.ndigits);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int sm2_sign(u8 *r_, u8 *s_, u8 *prikey, u8 *hash_)
|
||||
{
|
||||
u64 k[ECC_MAX_DIGITS];
|
||||
u64 one[ECC_MAX_DIGITS] = {1};
|
||||
u64 random[ECC_MAX_DIGITS];
|
||||
u64 pri[ECC_MAX_DIGITS];
|
||||
u64 hash[ECC_MAX_DIGITS];
|
||||
u64 r[ECC_MAX_DIGITS];
|
||||
u64 s[ECC_MAX_DIGITS];
|
||||
|
||||
ecc_point p;
|
||||
|
||||
ecc_bytes2native(pri, prikey, sm2_curve.ndigits);
|
||||
ecc_bytes2native(hash, hash_, sm2_curve.ndigits);
|
||||
|
||||
vli_get_random((u8*)random, ECC_NUMWORD);
|
||||
if (vli_is_zero(random, sm2_curve.ndigits)) {
|
||||
/* The random number must not be 0. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
vli_set(k, random, sm2_curve.ndigits);
|
||||
if (vli_cmp(sm2_curve.n, k, sm2_curve.ndigits) != 1) {
|
||||
vli_sub(k, k, sm2_curve.n, sm2_curve.ndigits);
|
||||
}
|
||||
|
||||
/* tmp = k * G */
|
||||
ecc_point_mult(&sm2_curve, &p, &sm2_curve.g, k, NULL);
|
||||
|
||||
/* r = x1 + e (mod n) */
|
||||
vli_mod_add(r, p.x, hash, sm2_curve.n, sm2_curve.ndigits);
|
||||
if (vli_cmp(sm2_curve.n, r, sm2_curve.ndigits) != 1) {
|
||||
vli_sub(r, r, sm2_curve.n, sm2_curve.ndigits);
|
||||
}
|
||||
|
||||
if (vli_is_zero(r, sm2_curve.ndigits)) {
|
||||
/* If r == 0, fail (need a different random number). */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* s = r*d */
|
||||
vli_mod_mult(s, r, pri, sm2_curve.n, sm2_curve.ndigits);
|
||||
/* k-r*d */
|
||||
vli_mod_sub(s, k, s, sm2_curve.n, sm2_curve.ndigits);
|
||||
/* 1+d */
|
||||
vli_mod_add(pri, pri, one, sm2_curve.n, sm2_curve.ndigits);
|
||||
/* (1+d)' */
|
||||
vli_mod_inv(pri, pri, sm2_curve.n, sm2_curve.ndigits);
|
||||
/* (1+d)'*(k-r*d) */
|
||||
vli_mod_mult(s, pri, s, sm2_curve.n, sm2_curve.ndigits);
|
||||
|
||||
ecc_native2bytes(r_, r, sm2_curve.ndigits);
|
||||
ecc_native2bytes(s_, s, sm2_curve.ndigits);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sm2_verify(ecc_point *pubkey, u8 *hash_, u8 *r_, u8 *s_)
|
||||
{
|
||||
ecc_point result;
|
||||
ecc_point pub[1];
|
||||
u64 t[ECC_MAX_DIGITS];
|
||||
u64 r[ECC_MAX_DIGITS];
|
||||
u64 s[ECC_MAX_DIGITS];
|
||||
u64 hash[ECC_MAX_DIGITS];
|
||||
|
||||
ecc_bytes2native(pub->x, pubkey->x, sm2_curve.ndigits);
|
||||
ecc_bytes2native(pub->y, pubkey->y, sm2_curve.ndigits);
|
||||
ecc_bytes2native(r, r_, sm2_curve.ndigits);
|
||||
ecc_bytes2native(s, s_, sm2_curve.ndigits);
|
||||
ecc_bytes2native(hash, hash_, sm2_curve.ndigits);
|
||||
|
||||
if (vli_is_zero(r, sm2_curve.ndigits) || vli_is_zero(s, sm2_curve.ndigits)) {
|
||||
/* r, s must not be 0. */
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (vli_cmp(sm2_curve.n, r, sm2_curve.ndigits) != 1
|
||||
|| vli_cmp(sm2_curve.n, s, sm2_curve.ndigits) != 1) {
|
||||
/* r, s must be < n. */
|
||||
return -1;
|
||||
}
|
||||
|
||||
vli_mod_add(t, r, s, sm2_curve.n, sm2_curve.ndigits); /* r + s */
|
||||
if (t == 0)
|
||||
return -1;
|
||||
|
||||
ecc_point_mult2(&sm2_curve, &result, &sm2_curve.g, pub, s, t);
|
||||
|
||||
/* v = x1 + e (mod n) */
|
||||
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) {
|
||||
vli_sub(result.x, result.x, sm2_curve.n, sm2_curve.ndigits);
|
||||
}
|
||||
|
||||
/* Accept only if v == r. */
|
||||
return vli_cmp(result.x, r, sm2_curve.ndigits);
|
||||
}
|
||||
|
||||
|
||||
/*Output C1|C2|C3*/
|
||||
int sm2_encrypt(ecc_point *pubKey, u8 *M, u32 Mlen, u8 *C, u32 *Clen)
|
||||
{
|
||||
u64 k[ECC_MAX_DIGITS];
|
||||
ecc_point pub[1];
|
||||
ecc_point *C1 = (ecc_point *)C;
|
||||
u8 *C2 = C + ECC_NUMWORD*2;
|
||||
u8 *C3 = C + ECC_NUMWORD*2 + Mlen;
|
||||
|
||||
ecc_point kP;
|
||||
u8 *x2 = (u8*)kP.x;
|
||||
u8 *y2 = (u8*)kP.y;
|
||||
u8 *x2y2 = (u8*)kP.x;
|
||||
struct sm3_ctx md[1];
|
||||
int i=0;
|
||||
ecc_point S;
|
||||
|
||||
ecc_bytes2native(pub->x, pubKey->x, sm2_curve.ndigits);
|
||||
ecc_bytes2native(pub->y, pubKey->y, sm2_curve.ndigits);
|
||||
|
||||
vli_get_random((u8*)k, ECC_NUMWORD);
|
||||
|
||||
/* C1 = k * G */
|
||||
ecc_point_mult(&sm2_curve, C1, &sm2_curve.g, k, NULL);
|
||||
ecc_native2bytes(C1->x, C1->x, sm2_curve.ndigits);
|
||||
ecc_native2bytes(C1->y, C1->y, sm2_curve.ndigits);
|
||||
|
||||
/* S = h * Pb */
|
||||
|
||||
ecc_point_mult(&sm2_curve, &S, pub, sm2_curve.h, NULL);
|
||||
if (sm2_valid_public_key(&S) != 0)
|
||||
return -1;
|
||||
|
||||
/* kP = k * Pb */
|
||||
ecc_point_mult(&sm2_curve, &kP, pub, k, NULL);
|
||||
if (vli_is_zero(kP.x, sm2_curve.ndigits)
|
||||
| vli_is_zero(kP.y, sm2_curve.ndigits)) {
|
||||
return 0;
|
||||
}
|
||||
ecc_native2bytes(kP.x, kP.x, sm2_curve.ndigits);
|
||||
ecc_native2bytes(kP.y, kP.y, sm2_curve.ndigits);
|
||||
|
||||
/* t=KDF(x2 ∥ y2, klen) */
|
||||
sm3_kdf(x2y2, ECC_NUMWORD*2, C2, Mlen);
|
||||
|
||||
/* C2 = M ⊕ t;*/
|
||||
for (i = 0; i < Mlen; i++) {
|
||||
C2[i] = M[i]^C2[+i];
|
||||
}
|
||||
|
||||
/*C3 = Hash(x2 ∥ M ∥ y2)*/
|
||||
sm3_init(md);
|
||||
sm3_update(md, x2, ECC_NUMWORD);
|
||||
sm3_update(md, M, Mlen);
|
||||
sm3_update(md, y2, ECC_NUMWORD);
|
||||
sm3_final(md, C3);
|
||||
|
||||
if (Clen)
|
||||
*Clen = Mlen + ECC_NUMWORD*2 + SM3_DATA_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*Input C1|C2|C3*/
|
||||
int sm2_decrypt(u8 *prikey, u8 *C, u32 Clen, u8 *M, u32 *Mlen)
|
||||
{
|
||||
u8 hash[SM3_DATA_LEN];
|
||||
u64 pri[ECC_MAX_DIGITS];
|
||||
ecc_point *C1 = (ecc_point *)C;
|
||||
u8 *C2 = C + ECC_NUMWORD*2;
|
||||
u8 *C3 = C + Clen - SM3_DATA_LEN;
|
||||
ecc_point dB;
|
||||
u64 *x2 = dB.x;
|
||||
u64 *y2 = dB.y;
|
||||
u64 *x2y2 = x2;
|
||||
struct sm3_ctx md[1];
|
||||
int outlen = Clen - ECC_NUMWORD*2 - SM3_DATA_LEN;
|
||||
int i=0;
|
||||
ecc_point S;
|
||||
|
||||
ecc_bytes2native(pri, prikey, sm2_curve.ndigits);
|
||||
ecc_bytes2native(C1->x, C1->x, sm2_curve.ndigits);
|
||||
ecc_bytes2native(C1->y, C1->y, sm2_curve.ndigits);
|
||||
|
||||
if (sm2_valid_public_key(C1) != 0)
|
||||
return -1;
|
||||
|
||||
|
||||
ecc_point_mult(&sm2_curve, &S, C1, sm2_curve.h, NULL);
|
||||
if (sm2_valid_public_key(&S) != 0)
|
||||
return -1;
|
||||
|
||||
ecc_point_mult(&sm2_curve, &dB, C1, pri, NULL);
|
||||
ecc_native2bytes(x2, x2, sm2_curve.ndigits);
|
||||
ecc_native2bytes(y2, y2, sm2_curve.ndigits);
|
||||
|
||||
sm3_kdf((u8*)x2y2, ECC_NUMWORD*2, M, outlen);
|
||||
if (vli_is_zero(x2, sm2_curve.ndigits)
|
||||
| vli_is_zero(y2, sm2_curve.ndigits)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < outlen; i++)
|
||||
M[i]=M[i]^C2[i];
|
||||
|
||||
sm3_init(md);
|
||||
sm3_update(md, (u8*)x2, ECC_NUMWORD);
|
||||
sm3_update(md, M, outlen);
|
||||
sm3_update(md, (u8*)y2, ECC_NUMWORD);
|
||||
sm3_final(md, hash);
|
||||
|
||||
*Mlen = outlen;
|
||||
if (memcmp(hash , C3, SM3_DATA_LEN) != 0)
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sm2_shared_point(u8* selfPriKey, u8* selfTempPriKey, ecc_point* selfTempPubKey,
|
||||
ecc_point *otherPubKey, ecc_point* otherTempPubKey, ecc_point *key)
|
||||
{
|
||||
ecc_point selfTempPub;
|
||||
ecc_point otherTempPub;
|
||||
ecc_point otherPub;
|
||||
ecc_point U[1];
|
||||
|
||||
u64 selfTempPri[ECC_MAX_DIGITS];
|
||||
u64 selfPri[ECC_MAX_DIGITS];
|
||||
u64 temp1[ECC_MAX_DIGITS];
|
||||
u64 temp2[ECC_MAX_DIGITS];
|
||||
u64 tA[ECC_MAX_DIGITS];
|
||||
|
||||
ecc_bytes2native(selfTempPri, selfTempPriKey, sm2_curve.ndigits);
|
||||
ecc_bytes2native(selfPri, selfPriKey, sm2_curve.ndigits);
|
||||
ecc_bytes2native(selfTempPub.x, selfTempPubKey->x, sm2_curve.ndigits);
|
||||
ecc_bytes2native(selfTempPub.y, selfTempPubKey->y, sm2_curve.ndigits);
|
||||
ecc_bytes2native(otherTempPub.x, otherTempPubKey->x, sm2_curve.ndigits);
|
||||
ecc_bytes2native(otherTempPub.y, otherTempPubKey->y, sm2_curve.ndigits);
|
||||
ecc_bytes2native(otherPub.x, otherPubKey->x, sm2_curve.ndigits);
|
||||
ecc_bytes2native(otherPub.y, otherPubKey->y, sm2_curve.ndigits);
|
||||
|
||||
/***********x1_=2^w+x2 & (2^w-1)*************/
|
||||
sm2_w(temp1, selfTempPub.x);
|
||||
/***********tA=(dA+x1_*rA)mod n *************/
|
||||
vli_mod_mult(temp1, selfTempPri, 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)*************/
|
||||
if(sm2_valid_public_key(&otherTempPub) != 0)
|
||||
return -1;
|
||||
sm2_w(temp2, otherTempPub.x);
|
||||
/**************U=[h*tA](PB+[x2_]RB)**********/
|
||||
/* U=[x2_]RB */
|
||||
ecc_point_mult(&sm2_curve, U, &otherTempPub, temp2, NULL);
|
||||
/*U=PB+U*/
|
||||
ecc_point_add(&sm2_curve, U, &otherPub, U);
|
||||
/*tA=tA*h */
|
||||
vli_mod_mult(tA, tA, sm2_curve.h, sm2_curve.n, sm2_curve.ndigits);
|
||||
ecc_point_mult(&sm2_curve, U, U,tA, NULL);
|
||||
|
||||
ecc_native2bytes(key->x, U->x, sm2_curve.ndigits);
|
||||
ecc_native2bytes(key->y, U->y, sm2_curve.ndigits);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void sm2_shared_key(ecc_point *point, u8 *ZA, u8 *ZB, u32 keyLen, u8 *key)
|
||||
{
|
||||
u8 Z[ECC_NUMWORD*4];
|
||||
memcpy(Z, point->x, ECC_NUMWORD);
|
||||
memcpy(Z + ECC_NUMWORD, point->y, ECC_NUMWORD);
|
||||
memcpy(Z + ECC_NUMWORD*2, ZA, ECC_NUMWORD);
|
||||
memcpy(Z + ECC_NUMWORD*3, ZB, ECC_NUMWORD);
|
||||
sm3_kdf(Z, ECC_NUMWORD*4, key, keyLen);
|
||||
}
|
||||
|
||||
int sm2_C1C2C3ConvertToC1C3C2(u8 *pOut, u8 *pIn, s32 len, s32 mode)
|
||||
{
|
||||
int dataLen = len-64-32;
|
||||
|
||||
if(len < 64 + 32)
|
||||
return -1;
|
||||
|
||||
memcpy(pOut, pIn, 64);/*C1*/
|
||||
|
||||
if(!mode) /*To C1|C3|C2*/
|
||||
{
|
||||
memcpy( pOut + 64,pIn + 64 + dataLen, 32); /*C3*/
|
||||
memcpy( pOut + 96,pIn + 64, dataLen); /*C2*/
|
||||
}
|
||||
else /*To C1|C2|C3*/
|
||||
{
|
||||
memcpy(pOut + 64, pIn + 96, dataLen); /*C2*/
|
||||
memcpy(pOut + 64 + dataLen, pIn + 64, 32); /*C3*/
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/****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)
|
||||
{
|
||||
struct sm3_ctx md[1];
|
||||
|
||||
sm3_init(md);
|
||||
sm3_update(md, x, ECC_NUMWORD);
|
||||
sm3_update(md, ZA, ECC_NUMWORD);
|
||||
sm3_update(md, ZB, ECC_NUMWORD);
|
||||
sm3_update(md, (u8*)RA->x, ECC_NUMWORD);
|
||||
sm3_update(md, (u8*)RA->y, ECC_NUMWORD);
|
||||
sm3_update(md, (u8*)RB->x, ECC_NUMWORD);
|
||||
sm3_update(md, (u8*)RB->y, ECC_NUMWORD);
|
||||
sm3_final(md, (u8*)hash);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/****SA = Hash(temp||Uy||Hash)****/
|
||||
int ECC_Key_ex_hash2(u8 temp, u8* y,u8 *hash, u8* SA)
|
||||
{
|
||||
struct sm3_ctx md[1];
|
||||
|
||||
sm3_init(md);
|
||||
sm3_update(md, &temp,1);
|
||||
sm3_update(md, y,ECC_NUMWORD);
|
||||
sm3_update(md, hash,ECC_NUMWORD);
|
||||
sm3_final(md, SA);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ECC_KeyEx_Init_I(u8 *pri, ecc_point *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)
|
||||
{
|
||||
struct sm3_ctx md[1];
|
||||
u8 Z[ECC_NUMWORD*2 + ECC_NUMBITS/4]={0};
|
||||
u8 hash[ECC_NUMWORD],S1[ECC_NUMWORD];
|
||||
u8 temp=0x02;
|
||||
|
||||
/*--------B2: RB=[rb]G=(x2,y2)--------*/
|
||||
sm2_make_pubkey(rb, RB);
|
||||
/********************************************/
|
||||
sm2_shared_point(dB, rb, RB, PA, RA, V);
|
||||
/*------------B7:KB=KDF(VX,VY,ZA,ZB,KLEN)----------*/
|
||||
memcpy(Z, V->x, ECC_NUMWORD);
|
||||
memcpy(Z+ECC_NUMWORD, (u8*)V->y, ECC_NUMWORD);
|
||||
memcpy(Z+ECC_NUMWORD*2, ZA,ECC_NUMWORD);
|
||||
memcpy(Z+ECC_NUMWORD*3, ZB,ECC_NUMWORD);
|
||||
sm3_kdf(Z,ECC_NUMWORD*4, K, klen);
|
||||
/*---------------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_hash2(temp, (u8*)V->y, hash, SB);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int 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[])
|
||||
{
|
||||
struct sm3_ctx md[1];
|
||||
u8 Z[ECC_NUMWORD*2 + ECC_NUMWORD*2]={0};
|
||||
u8 hash[ECC_NUMWORD],S1[ECC_NUMWORD];
|
||||
u8 temp[2]={0x02,0x03};
|
||||
ecc_point U[1];
|
||||
|
||||
/********************************************/
|
||||
sm2_shared_point(dA, ra, RA, PB, RB, U);
|
||||
/************KA=KDF(UX,UY,ZA,ZB,KLEN)**********/
|
||||
memcpy(Z, U->x,ECC_NUMWORD);
|
||||
memcpy(Z+ECC_NUMWORD, U->y,ECC_NUMWORD);
|
||||
memcpy(Z+ECC_NUMWORD*2,ZA,ECC_NUMWORD);
|
||||
memcpy(Z+ECC_NUMWORD*2 +ECC_NUMWORD ,ZB,ECC_NUMWORD);
|
||||
sm3_kdf(Z,ECC_NUMWORD*2+ECC_NUMWORD*2, K, klen);
|
||||
/****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_hash2(temp[0], (u8*)U->y, hash, S1);
|
||||
/*test S1=SB?*/
|
||||
if( memcmp(S1,SB,ECC_NUMWORD)!=0)
|
||||
return -1;
|
||||
/*SA = Hash(0x03||yU||Hash(xU||ZA||ZB||x1||y1||x2||y2)) */
|
||||
ECC_Key_ex_hash2(temp[1], (u8*)U->y, hash, SA);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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 S2[ECC_NUMWORD];
|
||||
u8 temp=0x03;
|
||||
struct sm3_ctx md[1];
|
||||
|
||||
/*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_hash2(temp, (u8*)V->y, hash, S2);
|
||||
|
||||
if( memcmp(S2,SA,ECC_NUMWORD)!=0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,403 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "sm3.h"
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (big endian)
|
||||
*/
|
||||
#ifndef GET_ULONG_BE
|
||||
#define GET_ULONG_BE(n,b,i) \
|
||||
{ \
|
||||
(n) = ( (u32) (b)[(i) ] << 24 ) \
|
||||
| ( (u32) (b)[(i) + 1] << 16 ) \
|
||||
| ( (u32) (b)[(i) + 2] << 8 ) \
|
||||
| ( (u32) (b)[(i) + 3] ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef PUT_ULONG_BE
|
||||
#define PUT_ULONG_BE(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (u8) ( (n) >> 24 ); \
|
||||
(b)[(i) + 1] = (u8) ( (n) >> 16 ); \
|
||||
(b)[(i) + 2] = (u8) ( (n) >> 8 ); \
|
||||
(b)[(i) + 3] = (u8) ( (n) ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* SM3 context setup
|
||||
*/
|
||||
int sm3_init(struct sm3_ctx *ctx)
|
||||
{
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
ctx->state[0] = 0x7380166F;
|
||||
ctx->state[1] = 0x4914B2B9;
|
||||
ctx->state[2] = 0x172442D7;
|
||||
ctx->state[3] = 0xDA8A0600;
|
||||
ctx->state[4] = 0xA96F30BC;
|
||||
ctx->state[5] = 0x163138AA;
|
||||
ctx->state[6] = 0xE38DEE4D;
|
||||
ctx->state[7] = 0xB0FB0E4E;
|
||||
|
||||
#if DEBUG
|
||||
printf(" function: %s , line= %d \n", __FUNCTION__, __LINE__);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void sm3_process(struct sm3_ctx *ctx, u8 data[64])
|
||||
{
|
||||
u32 SS1, SS2, TT1, TT2, W[68],W1[64];
|
||||
u32 A, B, C, D, E, F, G, H;
|
||||
u32 T[64];
|
||||
u32 Temp1,Temp2,Temp3,Temp4,Temp5;
|
||||
int j;
|
||||
#if ALG_DEBUG
|
||||
int i;
|
||||
#endif
|
||||
|
||||
/*
|
||||
//for(j=0; j < 68; j++)
|
||||
// W[j] = 0;
|
||||
//for(j=0; j < 64; j++)
|
||||
// W1[j] = 0;
|
||||
*/
|
||||
|
||||
for(j = 0; j < 16; j++)
|
||||
T[j] = 0x79CC4519;
|
||||
for(j =16; j < 64; j++)
|
||||
T[j] = 0x7A879D8A;
|
||||
|
||||
GET_ULONG_BE( W[ 0], data, 0 );
|
||||
GET_ULONG_BE( W[ 1], data, 4 );
|
||||
GET_ULONG_BE( W[ 2], data, 8 );
|
||||
GET_ULONG_BE( W[ 3], data, 12 );
|
||||
GET_ULONG_BE( W[ 4], data, 16 );
|
||||
GET_ULONG_BE( W[ 5], data, 20 );
|
||||
GET_ULONG_BE( W[ 6], data, 24 );
|
||||
GET_ULONG_BE( W[ 7], data, 28 );
|
||||
GET_ULONG_BE( W[ 8], data, 32 );
|
||||
GET_ULONG_BE( W[ 9], data, 36 );
|
||||
GET_ULONG_BE( W[10], data, 40 );
|
||||
GET_ULONG_BE( W[11], data, 44 );
|
||||
GET_ULONG_BE( W[12], data, 48 );
|
||||
GET_ULONG_BE( W[13], data, 52 );
|
||||
GET_ULONG_BE( W[14], data, 56 );
|
||||
GET_ULONG_BE( W[15], data, 60 );
|
||||
|
||||
#if ALG_DEBUG
|
||||
printf("Message with padding:\n");
|
||||
for(i=0; i< 8; i++)
|
||||
printf("%08x ",W[i]);
|
||||
printf("\n");
|
||||
for(i=8; i< 16; i++)
|
||||
printf("%08x ",W[i]);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
#define FF0(x,y,z) ( (x) ^ (y) ^ (z))
|
||||
#define FF1(x,y,z) (((x) & (y)) | ( (x) & (z)) | ( (y) & (z)))
|
||||
|
||||
#define GG0(x,y,z) ( (x) ^ (y) ^ (z))
|
||||
#define GG1(x,y,z) (((x) & (y)) | ( (~(x)) & (z)) )
|
||||
|
||||
|
||||
#define SHL(x,n) (((x) & 0xFFFFFFFF) << n)
|
||||
#define ROTL(x,n) (SHL((x),n) | ((x) >> (32 - n)))
|
||||
/*
|
||||
#define ROTL(x,n) (SHL((x),n) | ((x) >> (32 - n%32)))
|
||||
*/
|
||||
|
||||
#define P0(x) ((x) ^ ROTL((x),9) ^ ROTL((x),17))
|
||||
#define P1(x) ((x) ^ ROTL((x),15) ^ ROTL((x),23))
|
||||
|
||||
for(j = 16; j < 68; j++ )
|
||||
{
|
||||
/*
|
||||
//W[j] = P1( W[j-16] ^ W[j-9] ^ ROTL(W[j-3],15)) ^ ROTL(W[j - 13],7 ) ^ W[j-6];
|
||||
//Why thd release's result is different with the debug's ?
|
||||
//Below is okay. Interesting, Perhaps VC6 has a bug of Optimizaiton.
|
||||
*/
|
||||
|
||||
Temp1 = W[j-16] ^ W[j-9];
|
||||
Temp2 = ROTL(W[j-3],15);
|
||||
Temp3 = Temp1 ^ Temp2;
|
||||
Temp4 = P1(Temp3);
|
||||
Temp5 = ROTL(W[j - 13],7 ) ^ W[j-6];
|
||||
W[j] = Temp4 ^ Temp5;
|
||||
}
|
||||
|
||||
#if ALG_DEBUG
|
||||
printf("Expanding message W0-67:\n");
|
||||
for(i=0; i<68; i++)
|
||||
{
|
||||
printf("%08x ",W[i]);
|
||||
if(((i+1) % 8) == 0) printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
for(j = 0; j < 64; j++)
|
||||
{
|
||||
W1[j] = W[j] ^ W[j+4];
|
||||
}
|
||||
|
||||
#if ALG_DEBUG
|
||||
printf("Expanding message W'0-63:\n");
|
||||
for(i=0; i<64; i++)
|
||||
{
|
||||
printf("%08x ",W1[i]);
|
||||
if(((i+1) % 8) == 0) printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
A = ctx->state[0];
|
||||
B = ctx->state[1];
|
||||
C = ctx->state[2];
|
||||
D = ctx->state[3];
|
||||
E = ctx->state[4];
|
||||
F = ctx->state[5];
|
||||
G = ctx->state[6];
|
||||
H = ctx->state[7];
|
||||
#if ALG_DEBUG
|
||||
printf("j A B C D E F G H\n");
|
||||
printf(" %08x %08x %08x %08x %08x %08x %08x %08x\n",A,B,C,D,E,F,G,H);
|
||||
#endif
|
||||
|
||||
for(j =0; j < 16; j++)
|
||||
{
|
||||
SS1 = ROTL((ROTL(A,12) + E + ROTL(T[j],j)), 7);
|
||||
SS2 = SS1 ^ ROTL(A,12);
|
||||
TT1 = FF0(A,B,C) + D + SS2 + W1[j];
|
||||
TT2 = GG0(E,F,G) + H + SS1 + W[j];
|
||||
D = C;
|
||||
C = ROTL(B,9);
|
||||
B = A;
|
||||
A = TT1;
|
||||
H = G;
|
||||
G = ROTL(F,19);
|
||||
F = E;
|
||||
E = P0(TT2);
|
||||
#if ALG_DEBUG
|
||||
printf("%02d %08x %08x %08x %08x %08x %08x %08x %08x\n",j,A,B,C,D,E,F,G,H);
|
||||
#endif
|
||||
}
|
||||
|
||||
for(j =16; j < 64; j++)
|
||||
{
|
||||
SS1 = ROTL((ROTL(A,12) + E + ROTL(T[j],j)), 7);
|
||||
SS2 = SS1 ^ ROTL(A,12);
|
||||
TT1 = FF1(A,B,C) + D + SS2 + W1[j];
|
||||
TT2 = GG1(E,F,G) + H + SS1 + W[j];
|
||||
D = C;
|
||||
C = ROTL(B,9);
|
||||
B = A;
|
||||
A = TT1;
|
||||
H = G;
|
||||
G = ROTL(F,19);
|
||||
F = E;
|
||||
E = P0(TT2);
|
||||
#if ALG_DEBUG
|
||||
printf("%02d %08x %08x %08x %08x %08x %08x %08x %08x\n",j,A,B,C,D,E,F,G,H);
|
||||
printf("%02d %08x %08x %08x %08x %08x %08x %08x %08x\n",j,A,B,C,D,E,F,G,H);
|
||||
#endif
|
||||
}
|
||||
|
||||
ctx->state[0] ^= A;
|
||||
ctx->state[1] ^= B;
|
||||
ctx->state[2] ^= C;
|
||||
ctx->state[3] ^= D;
|
||||
ctx->state[4] ^= E;
|
||||
ctx->state[5] ^= F;
|
||||
ctx->state[6] ^= G;
|
||||
ctx->state[7] ^= H;
|
||||
#if ALG_DEBUG
|
||||
printf(" %08x %08x %08x %08x %08x %08x %08x %08x\n",ctx->state[0],ctx->state[1],ctx->state[2],
|
||||
ctx->state[3],ctx->state[4],ctx->state[5],ctx->state[6],ctx->state[7]);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* SM3 process buffer
|
||||
*/
|
||||
int sm3_update(struct sm3_ctx *ctx, const u8 *input, u32 ilen)
|
||||
{
|
||||
u32 left;
|
||||
int fill;
|
||||
|
||||
#if DEBUG
|
||||
printf(" function: %s , line= %d \n", __FUNCTION__, __LINE__);
|
||||
#endif
|
||||
if( ilen <= 0 )
|
||||
return -1;
|
||||
|
||||
left = ctx->total[0] & 0x3F;
|
||||
fill = 64 - left;
|
||||
|
||||
ctx->total[0] += ilen;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if( ctx->total[0] < (u32) ilen )
|
||||
ctx->total[1]++;
|
||||
|
||||
if( left && ilen >= fill )
|
||||
{
|
||||
memcpy( (void *) (ctx->buffer + left),
|
||||
(void *) input, fill );
|
||||
sm3_process( ctx, ctx->buffer );
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
}
|
||||
|
||||
while( ilen >= 64 )
|
||||
{
|
||||
sm3_process( ctx, (u8*)input );
|
||||
input += 64;
|
||||
ilen -= 64;
|
||||
}
|
||||
|
||||
if( ilen > 0 )
|
||||
{
|
||||
memcpy( (void *) (ctx->buffer + left),
|
||||
(void *) input, ilen );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static u8 sm3_padding[64] =
|
||||
{
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/*
|
||||
* SM3 final digest
|
||||
*/
|
||||
int sm3_final(struct sm3_ctx *ctx, u8 *output)
|
||||
{
|
||||
u32 last, padn;
|
||||
u32 high, low;
|
||||
u8 msglen[8];
|
||||
|
||||
high = ( ctx->total[0] >> 29 )
|
||||
| ( ctx->total[1] << 3 );
|
||||
low = ( ctx->total[0] << 3 );
|
||||
|
||||
PUT_ULONG_BE( high, msglen, 0 );
|
||||
PUT_ULONG_BE( low, msglen, 4 );
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
|
||||
|
||||
sm3_update(ctx, (const u8 *)sm3_padding, padn );
|
||||
sm3_update(ctx, msglen, 8 );
|
||||
|
||||
PUT_ULONG_BE( ctx->state[0], output, 0 );
|
||||
PUT_ULONG_BE( ctx->state[1], output, 4 );
|
||||
PUT_ULONG_BE( ctx->state[2], output, 8 );
|
||||
PUT_ULONG_BE( ctx->state[3], output, 12 );
|
||||
PUT_ULONG_BE( ctx->state[4], output, 16 );
|
||||
PUT_ULONG_BE( ctx->state[5], output, 20 );
|
||||
PUT_ULONG_BE( ctx->state[6], output, 24 );
|
||||
PUT_ULONG_BE( ctx->state[7], output, 28 );
|
||||
|
||||
#if DEBUG
|
||||
printf(" function: %s , line= %d \n", __FUNCTION__, __LINE__);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sm3_finup(struct sm3_ctx *ctx, const u8 *data,
|
||||
u32 len, u8 *out)
|
||||
{
|
||||
#if DEBUG
|
||||
printf(" function: %s , line= %d \n", __FUNCTION__, __LINE__);
|
||||
#endif
|
||||
|
||||
sm3_init(ctx);
|
||||
sm3_update(ctx, data, len);
|
||||
return sm3_final(ctx, out);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* SM3 HMAC context setup
|
||||
*/
|
||||
void sm3_hmac_starts(struct sm3_ctx *ctx, unsigned char *key, int keylen )
|
||||
{
|
||||
int i;
|
||||
unsigned char sum[32];
|
||||
|
||||
struct sm3_ctx ctxTemp;
|
||||
|
||||
if( keylen > 64 )
|
||||
{
|
||||
sm3_finup(&ctxTemp, key, keylen, sum );
|
||||
|
||||
keylen = 32;
|
||||
/*keylen = ( is224 ) ? 28 : 32;*/
|
||||
key = sum;
|
||||
}
|
||||
|
||||
memset( ctx->ipad, 0x36, 64 );
|
||||
memset( ctx->opad, 0x5C, 64 );
|
||||
|
||||
for( i = 0; i < keylen; i++ )
|
||||
{
|
||||
ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
|
||||
ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
|
||||
}
|
||||
|
||||
sm3_init( ctx);
|
||||
sm3_update( ctx, ctx->ipad, 64 );
|
||||
}
|
||||
|
||||
/*
|
||||
* SM3 HMAC process buffer
|
||||
*/
|
||||
void sm3_hmac_update(struct sm3_ctx *ctx, unsigned char *input, int ilen )
|
||||
{
|
||||
sm3_update( ctx, input, ilen );
|
||||
}
|
||||
|
||||
/*
|
||||
* SM3 HMAC final digest
|
||||
*/
|
||||
void sm3_hmac_finish(struct sm3_ctx *ctx, unsigned char output[32] )
|
||||
{
|
||||
int hlen;
|
||||
unsigned char tmpbuf[32];
|
||||
|
||||
/*is224 = ctx->is224;*/
|
||||
hlen = 32;
|
||||
|
||||
sm3_final( ctx, tmpbuf );
|
||||
sm3_init( ctx );
|
||||
sm3_update( ctx, ctx->opad, 64 );
|
||||
sm3_update( ctx, tmpbuf, hlen );
|
||||
sm3_final( ctx, output );
|
||||
}
|
||||
|
||||
/*
|
||||
* output = HMAC-SM#( hmac key, input buffer )
|
||||
*/
|
||||
void sm3_hmac( unsigned char *key, int keylen,
|
||||
unsigned char *input, int ilen,
|
||||
unsigned char output[32] )
|
||||
{
|
||||
struct sm3_ctx ctx;
|
||||
|
||||
sm3_hmac_starts( &ctx, key, keylen);
|
||||
sm3_hmac_update( &ctx, input, ilen );
|
||||
sm3_hmac_finish( &ctx, output );
|
||||
}
|
|
@ -0,0 +1,304 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "sm4.h"
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (big endian)
|
||||
*/
|
||||
#ifndef GET_ULONG_BE
|
||||
#define GET_ULONG_BE(n,b,i) \
|
||||
{ \
|
||||
(n) = ( (u32) (b)[(i) ] << 24 ) \
|
||||
| ( (u32) (b)[(i) + 1] << 16 ) \
|
||||
| ( (u32) (b)[(i) + 2] << 8 ) \
|
||||
| ( (u32) (b)[(i) + 3] ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef PUT_ULONG_BE
|
||||
#define PUT_ULONG_BE(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (u8) ( (n) >> 24 ); \
|
||||
(b)[(i) + 1] = (u8) ( (n) >> 16 ); \
|
||||
(b)[(i) + 2] = (u8) ( (n) >> 8 ); \
|
||||
(b)[(i) + 3] = (u8) ( (n) ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
*rotate shift left marco definition
|
||||
*
|
||||
*/
|
||||
#define SHL(x,n) (((x) & 0xFFFFFFFF) << n)
|
||||
#define ROTL(x,n) (SHL((x),n) | ((x) >> (32 - n)))
|
||||
|
||||
#define SWAP(a,b) { u32 t = a; a = b; b = t;}
|
||||
|
||||
/*
|
||||
* Expanded SM4 S-boxes
|
||||
* Sbox table: 8bits input convert to 8 bits output*/
|
||||
|
||||
static const u8 SboxTable[16][16] =
|
||||
{
|
||||
{0xd6,0x90,0xe9,0xfe,0xcc,0xe1,0x3d,0xb7,0x16,0xb6,0x14,0xc2,0x28,0xfb,0x2c,0x05},
|
||||
{0x2b,0x67,0x9a,0x76,0x2a,0xbe,0x04,0xc3,0xaa,0x44,0x13,0x26,0x49,0x86,0x06,0x99},
|
||||
{0x9c,0x42,0x50,0xf4,0x91,0xef,0x98,0x7a,0x33,0x54,0x0b,0x43,0xed,0xcf,0xac,0x62},
|
||||
{0xe4,0xb3,0x1c,0xa9,0xc9,0x08,0xe8,0x95,0x80,0xdf,0x94,0xfa,0x75,0x8f,0x3f,0xa6},
|
||||
{0x47,0x07,0xa7,0xfc,0xf3,0x73,0x17,0xba,0x83,0x59,0x3c,0x19,0xe6,0x85,0x4f,0xa8},
|
||||
{0x68,0x6b,0x81,0xb2,0x71,0x64,0xda,0x8b,0xf8,0xeb,0x0f,0x4b,0x70,0x56,0x9d,0x35},
|
||||
{0x1e,0x24,0x0e,0x5e,0x63,0x58,0xd1,0xa2,0x25,0x22,0x7c,0x3b,0x01,0x21,0x78,0x87},
|
||||
{0xd4,0x00,0x46,0x57,0x9f,0xd3,0x27,0x52,0x4c,0x36,0x02,0xe7,0xa0,0xc4,0xc8,0x9e},
|
||||
{0xea,0xbf,0x8a,0xd2,0x40,0xc7,0x38,0xb5,0xa3,0xf7,0xf2,0xce,0xf9,0x61,0x15,0xa1},
|
||||
{0xe0,0xae,0x5d,0xa4,0x9b,0x34,0x1a,0x55,0xad,0x93,0x32,0x30,0xf5,0x8c,0xb1,0xe3},
|
||||
{0x1d,0xf6,0xe2,0x2e,0x82,0x66,0xca,0x60,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}
|
||||
};
|
||||
|
||||
/* System parameter */
|
||||
static const u32 FK[4] = {0xa3b1bac6,0x56aa3350,0x677d9197,0xb27022dc};
|
||||
|
||||
/* fixed parameter */
|
||||
static const u32 CK[32] =
|
||||
{
|
||||
0x00070e15,0x1c232a31,0x383f464d,0x545b6269,
|
||||
0x70777e85,0x8c939aa1,0xa8afb6bd,0xc4cbd2d9,
|
||||
0xe0e7eef5,0xfc030a11,0x181f262d,0x343b4249,
|
||||
0x50575e65,0x6c737a81,0x888f969d,0xa4abb2b9,
|
||||
0xc0c7ced5,0xdce3eaf1,0xf8ff060d,0x141b2229,
|
||||
0x30373e45,0x4c535a61,0x686f767d,0x848b9299,
|
||||
0xa0a7aeb5,0xbcc3cad1,0xd8dfe6ed,0xf4fb0209,
|
||||
0x10171e25,0x2c333a41,0x484f565d,0x646b7279
|
||||
};
|
||||
|
||||
/*
|
||||
* private function:
|
||||
* look up in SboxTable and get the related value.
|
||||
* args: [in] inch: 0x00~0xFF (8 bits unsigned value).
|
||||
*/
|
||||
static u8 sm4Sbox(u8 inch)
|
||||
{
|
||||
u8 *pTable = (u8 *)SboxTable;
|
||||
u8 retVal = (u8)(pTable[inch]);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/*
|
||||
* private F(Lt) function:
|
||||
* "T algorithm" == "L algorithm" + "t algorithm".
|
||||
* args: [in] a: a is a 32 bits unsigned value;
|
||||
* return: c: c is calculated with line algorithm "L" and nonline algorithm "t"
|
||||
*/
|
||||
static u32 sm4Lt(u32 ka)
|
||||
{
|
||||
u32 bb = 0;
|
||||
u32 c = 0;
|
||||
u8 a[4];
|
||||
u8 b[4];
|
||||
|
||||
PUT_ULONG_BE(ka,a,0);
|
||||
b[0] = sm4Sbox(a[0]);
|
||||
b[1] = sm4Sbox(a[1]);
|
||||
b[2] = sm4Sbox(a[2]);
|
||||
b[3] = sm4Sbox(a[3]);
|
||||
GET_ULONG_BE(bb,b,0);
|
||||
c =bb^(ROTL(bb, 2))^(ROTL(bb, 10))^(ROTL(bb, 18))^(ROTL(bb, 24));
|
||||
return c;
|
||||
}
|
||||
|
||||
/*
|
||||
* private F function:
|
||||
* Calculating and getting encryption/decryption contents.
|
||||
* args: [in] x0: original contents;
|
||||
* args: [in] x1: original contents;
|
||||
* args: [in] x2: original contents;
|
||||
* args: [in] x3: original contents;
|
||||
* args: [in] rk: encryption/decryption key;
|
||||
* return the contents of encryption/decryption contents.
|
||||
*/
|
||||
static u32 sm4F(u32 x0, u32 x1, u32 x2, u32 x3, u32 rk)
|
||||
{
|
||||
return (x0^sm4Lt(x1^x2^x3^rk));
|
||||
}
|
||||
|
||||
|
||||
/* private function:
|
||||
* Calculating round encryption key.
|
||||
* args: [in] a: a is a 32 bits unsigned value;
|
||||
* return: sk[i]: i{0,1,2,3,...31}.
|
||||
*/
|
||||
static u32 sm4CalciRK(u32 ka)
|
||||
{
|
||||
u32 bb = 0;
|
||||
u32 rk = 0;
|
||||
u8 a[4];
|
||||
u8 b[4];
|
||||
PUT_ULONG_BE(ka,a,0);
|
||||
b[0] = sm4Sbox(a[0]);
|
||||
b[1] = sm4Sbox(a[1]);
|
||||
b[2] = sm4Sbox(a[2]);
|
||||
b[3] = sm4Sbox(a[3]);
|
||||
GET_ULONG_BE(bb,b,0);
|
||||
rk = bb^(ROTL(bb, 13))^(ROTL(bb, 23));
|
||||
return rk;
|
||||
}
|
||||
|
||||
static void sm4_setkey( u32 SK[32], u8 key[16] )
|
||||
{
|
||||
u32 MK[4];
|
||||
u32 k[36];
|
||||
u32 i = 0;
|
||||
|
||||
GET_ULONG_BE( MK[0], key, 0 );
|
||||
GET_ULONG_BE( MK[1], key, 4 );
|
||||
GET_ULONG_BE( MK[2], key, 8 );
|
||||
GET_ULONG_BE( MK[3], key, 12 );
|
||||
k[0] = MK[0]^FK[0];
|
||||
k[1] = MK[1]^FK[1];
|
||||
k[2] = MK[2]^FK[2];
|
||||
k[3] = MK[3]^FK[3];
|
||||
for(; i<32; i++)
|
||||
{
|
||||
k[i+4] = k[i] ^ (sm4CalciRK(k[i+1]^k[i+2]^k[i+3]^CK[i]));
|
||||
SK[i] = k[i+4];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* SM4 standard one round processing
|
||||
*
|
||||
*/
|
||||
static void sm4_one_round( u32 sk[32],
|
||||
u8 input[16],
|
||||
u8 output[16] )
|
||||
{
|
||||
u32 i = 0;
|
||||
u32 ulbuf[36];
|
||||
|
||||
memset(ulbuf, 0, sizeof(ulbuf));
|
||||
GET_ULONG_BE( ulbuf[0], input, 0 );
|
||||
GET_ULONG_BE( ulbuf[1], input, 4 );
|
||||
GET_ULONG_BE( ulbuf[2], input, 8 );
|
||||
GET_ULONG_BE( ulbuf[3], input, 12 );
|
||||
while(i<32) {
|
||||
ulbuf[i+4] = sm4F(ulbuf[i], ulbuf[i+1], ulbuf[i+2], ulbuf[i+3], sk[i]);
|
||||
#if ALG_DEBUG
|
||||
printf("rk(%02d) = 0x%08x, X(%02d) = 0x%08x \n",i,sk[i], i, ulbuf[i+4] );
|
||||
#endif
|
||||
i++;
|
||||
}
|
||||
PUT_ULONG_BE(ulbuf[35],output,0);
|
||||
PUT_ULONG_BE(ulbuf[34],output,4);
|
||||
PUT_ULONG_BE(ulbuf[33],output,8);
|
||||
PUT_ULONG_BE(ulbuf[32],output,12);
|
||||
}
|
||||
|
||||
int sm4_set_key(struct sm4_ctx *ctx, u8 *key, u32 len)
|
||||
{
|
||||
int i;
|
||||
|
||||
#if DEBUG
|
||||
printf(" function: %s , line= %d \n", __FUNCTION__, __LINE__);
|
||||
#endif
|
||||
if( len != 16) {
|
||||
printf(" sm4 set key leng errr \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
sm4_setkey(ctx->sk_enc, key);
|
||||
|
||||
memcpy(ctx->sk_dec, ctx->sk_enc, 32*4);
|
||||
for(i = 0; i < 16; i ++) {
|
||||
SWAP(ctx->sk_dec[i], ctx->sk_dec[31-i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sm4_ecb_encrypt(struct sm4_ctx *ctx, u8 *key, u8 *in, u32 len, u8 *out)
|
||||
{
|
||||
#if DEBUG
|
||||
printf(" function: %s , line= %d \n", __FUNCTION__, __LINE__);
|
||||
#endif
|
||||
sm4_set_key(ctx, key, 16);
|
||||
|
||||
while(len > 0) {
|
||||
sm4_one_round(ctx->sk_enc, in, out);
|
||||
in += 16;
|
||||
out += 16;
|
||||
len -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
void sm4_ecb_decrypt(struct sm4_ctx *ctx, u8 *key, u8 *in, u32 len, u8 *out)
|
||||
{
|
||||
#if DEBUG
|
||||
printf(" function: %s , line= %d \n", __FUNCTION__, __LINE__);
|
||||
#endif
|
||||
|
||||
sm4_set_key(ctx, key, 16);
|
||||
|
||||
while( len > 0 ) {
|
||||
sm4_one_round( ctx->sk_dec, in, out);
|
||||
in += 16;
|
||||
out += 16;
|
||||
len -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
void sm4_cbc_encrypt(struct sm4_ctx *ctx, u8 *key, u8 *iv, u8 *in, u32 len, u8 *out)
|
||||
{
|
||||
u8 temp[16];
|
||||
int i;
|
||||
|
||||
#if DEBUG
|
||||
printf(" function: %s , line= %d \n", __FUNCTION__, __LINE__);
|
||||
#endif
|
||||
|
||||
sm4_set_key(ctx, key, 16);
|
||||
memcpy(temp, iv, 16 );
|
||||
while(len > 0)
|
||||
{
|
||||
for(i = 0; i < 16; i++)
|
||||
out[i] = (u8)(in[i] ^ temp[i]);
|
||||
sm4_one_round(ctx->sk_enc, out, out);
|
||||
memcpy(temp, out, 16);
|
||||
in += 16;
|
||||
out += 16;
|
||||
len -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
void sm4_cbc_decrypt(struct sm4_ctx *ctx, u8 *key, u8 *iv, u8 *in, u32 len, u8 *out)
|
||||
{
|
||||
u8 temp[16];
|
||||
u8 dataIV[16];
|
||||
int i;
|
||||
|
||||
#if DEBUG
|
||||
printf(" function: %s , line= %d \n", __FUNCTION__, __LINE__);
|
||||
#endif
|
||||
|
||||
sm4_set_key(ctx, key, 16);
|
||||
memcpy(temp, iv, 16 );
|
||||
while(len > 0)
|
||||
{
|
||||
memcpy(dataIV,in,16);
|
||||
|
||||
sm4_one_round(ctx->sk_dec, in, out);
|
||||
for(i = 0; i < 16; i++)
|
||||
out[i] = (u8)(out[i] ^ temp[i] );
|
||||
|
||||
memcpy(temp, dataIV, 16);
|
||||
|
||||
in += 16;
|
||||
out += 16;
|
||||
len -= 16;
|
||||
}
|
||||
}
|
|
@ -17,8 +17,8 @@ int main(int argc, char *argv[])
|
|||
|
||||
#ifdef Q_OS_WIN
|
||||
FramelessConfig::instance()->set(Global::Option::ForceHideWindowFrameBorder);
|
||||
FramelessConfig::instance()->set(Global::Option::ForceNonNativeBackgroundBlur);
|
||||
#endif
|
||||
FramelessConfig::instance()->set(Global::Option::DisableLazyInitializationForMicaMaterial);
|
||||
FramelessConfig::instance()->set(Global::Option::CenterWindowBeforeShow);
|
||||
FramelessConfig::instance()->set(Global::Option::EnableBlurBehindWindow);
|
||||
|
||||
|
@ -31,7 +31,7 @@ int main(int argc, char *argv[])
|
|||
#ifdef RIBBONUI_BUILD_STATIC_LIB
|
||||
engine.addImportPath("qrc:/");
|
||||
#endif
|
||||
const QUrl url(u"qrc:/ProtocolParser/Main.qml"_qs);
|
||||
const QUrl url(u"qrc:/ProtocolParser/qml/Main.qml"_qs);
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
|
||||
&app, [url](QObject *obj, const QUrl &objUrl) {
|
||||
if (!obj && url == objUrl)
|
|
@ -17,7 +17,7 @@ function Main() {
|
|||
|
||||
New-Item -ItemType Directory $archiveName
|
||||
# 拷贝exe
|
||||
Copy-Item D:\a\ProtocolParser\ProtocolParser\build\* $archiveName\ -Force -Recurse | Out-Null
|
||||
Copy-Item D:\a\ProtocolParser\ProtocolParser\build\app_source\* $archiveName\ -Force -Recurse | Out-Null
|
||||
# 拷贝依赖
|
||||
windeployqt --qmldir . --plugindir $archiveName\plugins --no-translations --compiler-runtime $archiveName\$targetName
|
||||
# 删除不必要的文件
|
||||
|
|
|
@ -27,7 +27,7 @@ function Main() {
|
|||
|
||||
New-Item -ItemType Directory $archiveName
|
||||
# 拷贝exe
|
||||
Copy-Item D:\a\ProtocolParser\ProtocolParser\build\* $archiveName\ -Force -Recurse | Out-Null
|
||||
Copy-Item D:\a\ProtocolParser\ProtocolParser\build\app_source\* $archiveName\ -Force -Recurse | Out-Null
|
||||
# 拷贝依赖
|
||||
windeployqt --qmldir . --plugindir $archiveName\plugins --no-translations --compiler-runtime $archiveName\$targetName
|
||||
# 删除不必要的文件
|
||||
|
|
Loading…
Reference in New Issue