Update workflow.
This commit is contained in:
parent
fa92fc6a05
commit
79a90e585b
|
@ -3,19 +3,13 @@ on:
|
|||
push:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/macos-shared.yml'
|
||||
pull_request:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/macos-shared.yml'
|
||||
|
||||
|
@ -59,19 +53,19 @@ jobs:
|
|||
- name: package
|
||||
run: |
|
||||
# 拷贝依赖
|
||||
macdeployqt /Users/runner/work/ProtocolParser/ProtocolParser/build/${targetName}.app -qmldir=. -verbose=1 -dmg
|
||||
macdeployqt /Users/runner/work/ProtocolParser/ProtocolParser/build/app_source/${targetName}.app -qmldir=. -verbose=1 -dmg
|
||||
|
||||
- uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: ${{ env.targetName }}_${{ matrix.os }}_${{matrix.qt_ver}}_shared.zip
|
||||
path: /Users/runner/work/ProtocolParser/ProtocolParser/build/${{ env.targetName }}.dmg
|
||||
path: /Users/runner/work/ProtocolParser/ProtocolParser/build/app_source/${{ env.targetName }}.dmg
|
||||
|
||||
- name: uploadRelease
|
||||
if: startsWith(github.event.ref, 'refs/tags/')
|
||||
uses: svenstaro/upload-release-action@v2
|
||||
with:
|
||||
repo_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
file: /Users/runner/work/ProtocolParser/ProtocolParser/build/${{ env.targetName }}.dmg
|
||||
file: /Users/runner/work/ProtocolParser/ProtocolParser/build/app_source/${{ env.targetName }}.dmg
|
||||
asset_name: ${{ env.targetName }}_${{ github.ref_name }}_${{ matrix.os }}_Qt${{ matrix.qt_ver }}_shared.dmg
|
||||
tag: ${{ github.ref }}
|
||||
overwrite: true
|
||||
|
|
|
@ -3,19 +3,13 @@ on:
|
|||
push:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/macos-static.yml'
|
||||
pull_request:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/macos-static.yml'
|
||||
|
||||
|
@ -59,19 +53,19 @@ jobs:
|
|||
- name: package
|
||||
run: |
|
||||
# 拷贝依赖
|
||||
macdeployqt /Users/runner/work/ProtocolParser/ProtocolParser/build/${targetName}.app -qmldir=. -verbose=1 -dmg
|
||||
macdeployqt /Users/runner/work/ProtocolParser/ProtocolParser/build/app_source/${targetName}.app -qmldir=. -verbose=1 -dmg
|
||||
|
||||
- uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: ${{ env.targetName }}_${{ matrix.os }}_${{matrix.qt_ver}}_static.zip
|
||||
path: /Users/runner/work/ProtocolParser/ProtocolParser/build/${{ env.targetName }}.dmg
|
||||
path: /Users/runner/work/ProtocolParser/ProtocolParser/build/app_source/${{ env.targetName }}.dmg
|
||||
|
||||
- name: uploadRelease
|
||||
if: startsWith(github.event.ref, 'refs/tags/')
|
||||
uses: svenstaro/upload-release-action@v2
|
||||
with:
|
||||
repo_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
file: /Users/runner/work/ProtocolParser/ProtocolParser/build/${{ env.targetName }}.dmg
|
||||
file: /Users/runner/work/ProtocolParser/ProtocolParser/build/app_source/${{ env.targetName }}.dmg
|
||||
asset_name: ${{ env.targetName }}_${{ github.ref_name }}_${{ matrix.os }}_Qt${{ matrix.qt_ver }}_static.dmg
|
||||
tag: ${{ github.ref }}
|
||||
overwrite: true
|
||||
|
|
|
@ -4,19 +4,13 @@ on:
|
|||
push:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/ubuntu-shared.yml'
|
||||
pull_request:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/ubuntu-shared.yml'
|
||||
|
||||
|
@ -30,7 +24,7 @@ jobs:
|
|||
qt_ver: [6.2.4]
|
||||
qt_arch: [gcc_64]
|
||||
env:
|
||||
targetName: ProtocolParser
|
||||
targetName: Protocol-Parser
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v3
|
||||
|
@ -74,7 +68,7 @@ jobs:
|
|||
# make sure Qt plugin finds QML sources so it can deploy the imported files
|
||||
export QML_SOURCES_PATHS=./
|
||||
# 拷贝依赖
|
||||
linuxdeploy-x86_64.AppImage --plugin=qt --output=appimage --create-desktop-file --icon-file=${targetName}.svg --executable=/home/runner/work/ProtocolParser/ProtocolParser/build/${targetName} --appdir /home/runner/work/ProtocolParser/ProtocolParser/build/
|
||||
linuxdeploy-x86_64.AppImage --plugin=qt --output=appimage --create-desktop-file --icon-file=${targetName}.svg --executable=/home/runner/work/ProtocolParser/ProtocolParser/build/app_source/${targetName} --appdir /home/runner/work/ProtocolParser/ProtocolParser/build/app_source/
|
||||
mv ${{ env.targetName }}-*.AppImage ${{ env.targetName }}_${{ matrix.os }}_${{matrix.qt_ver}}_shared.AppImage
|
||||
|
||||
- uses: actions/upload-artifact@v2
|
||||
|
|
|
@ -4,21 +4,13 @@ on:
|
|||
push:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- 'lib_source/**'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/ubuntu-static.yml'
|
||||
pull_request:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- 'lib_source/**'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/ubuntu-static.yml'
|
||||
|
||||
|
@ -32,7 +24,7 @@ jobs:
|
|||
qt_ver: [6.2.4]
|
||||
qt_arch: [gcc_64]
|
||||
env:
|
||||
targetName: ProtocolParser
|
||||
targetName: Protocol-Parser
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v3
|
||||
|
@ -76,7 +68,7 @@ jobs:
|
|||
# make sure Qt plugin finds QML sources so it can deploy the imported files
|
||||
export QML_SOURCES_PATHS=./
|
||||
# 拷贝依赖
|
||||
linuxdeploy-x86_64.AppImage --plugin=qt --output=appimage --create-desktop-file --icon-file=${targetName}.svg --executable=/home/runner/work/ProtocolParser/ProtocolParser/build/${targetName} --appdir /home/runner/work/ProtocolParser/ProtocolParser/build/
|
||||
linuxdeploy-x86_64.AppImage --plugin=qt --output=appimage --create-desktop-file --icon-file=${targetName}.svg --executable=/home/runner/work/ProtocolParser/ProtocolParser/build/app_source/${targetName} --appdir /home/runner/work/ProtocolParser/ProtocolParser/build/app_source/
|
||||
mv ${{ env.targetName }}-*.AppImage ${{ env.targetName }}_${{ matrix.os }}_Qt${{ matrix.qt_ver }}_static.AppImage
|
||||
|
||||
- uses: actions/upload-artifact@v2
|
||||
|
|
|
@ -3,19 +3,13 @@ on:
|
|||
push:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/windows-mingw-shared.yml'
|
||||
pull_request:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/windows-mingw-shared.yml'
|
||||
|
||||
|
|
|
@ -3,19 +3,13 @@ on:
|
|||
push:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/windows-mingw-static.yml'
|
||||
pull_request:
|
||||
paths:
|
||||
- '*.txt'
|
||||
- '*.qml'
|
||||
- '*.cpp'
|
||||
- '*.h'
|
||||
- '*.hpp'
|
||||
- 'app_source/**'
|
||||
- 'scripts/**'
|
||||
- '.github/workflows/windows-mingw-static.yml'
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 4f733a3316af9cd2e9bea0f448d4b010b8222c0f
|
||||
Subproject commit 1d25452a4b715a95caef554c1178d26261693367
|
|
@ -39,9 +39,9 @@ if (WIN32)
|
|||
)
|
||||
file(TO_CMAKE_PATH "/" PATH_SEPARATOR)
|
||||
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||
set(DLLPATH 3rdparty/msvc/*.dll)
|
||||
set(DLLPATH ../3rdparty/msvc/*.dll)
|
||||
else()
|
||||
set(DLLPATH 3rdparty/mingw/*.dll)
|
||||
set(DLLPATH ../3rdparty/mingw/*.dll)
|
||||
endif()
|
||||
string(REPLACE "/" ${PATH_SEPARATOR} DLLPATH "${DLLPATH}")
|
||||
file(GLOB DLL_FILES ${DLLPATH})
|
||||
|
@ -59,6 +59,7 @@ elseif(APPLE)
|
|||
${App_ICON}
|
||||
)
|
||||
else ()
|
||||
set(PROJECT_BUNDLE_NAME Protocol-Parser)
|
||||
qt_add_executable(${PROJECT_NAME}
|
||||
${sources_files}
|
||||
)
|
||||
|
|
|
@ -1,336 +0,0 @@
|
|||
#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; }
|
|
@ -1,62 +0,0 @@
|
|||
#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
|
|
@ -1,247 +0,0 @@
|
|||
#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
|
|
@ -1,29 +0,0 @@
|
|||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
#include <QQmlEngine>
|
||||
#include <QVariant>
|
||||
#include <QSettings>
|
||||
|
||||
class Config : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
QML_NAMED_ELEMENT(Config)
|
||||
public:
|
||||
static Config* instance();
|
||||
static Config* create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return instance();}
|
||||
Q_INVOKABLE void Set(QString qstrnodename,QString qstrkeyname,QVariant qvarvalue);
|
||||
Q_INVOKABLE void SetArray(QString qstrnodename,QString qstrkeyname,QVariant qvarvalue);
|
||||
Q_INVOKABLE QVariant Get(QString qstrnodename,QString qstrkeyname);
|
||||
Q_INVOKABLE QVariant GetArray(QString qstrnodename,QString qstrkeyname);
|
||||
void Clear();
|
||||
private:
|
||||
Config(QString qstrfilename = "");
|
||||
~Config(void);
|
||||
QString m_qstrFileName;
|
||||
QSettings *m_psetting=nullptr;
|
||||
};
|
||||
|
||||
#endif // CONFIG_H
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
#ifndef CRYPTO_H
|
||||
#define CRYPTO_H
|
||||
|
||||
#include <QObject>
|
||||
#include <string.h>
|
||||
#include <typedef.h>
|
||||
#include <sm2.h>
|
||||
#include <sm3.h>
|
||||
#include <sm4.h>
|
||||
#include "protocol_data.h"
|
||||
|
||||
class Crypto
|
||||
{
|
||||
public:
|
||||
static Crypto* getInstance();
|
||||
static int hex2bytearray(char s[], unsigned char bits[], u32 len);
|
||||
static int hex2bytearray(char s[], unsigned char *bits);
|
||||
static void bytearray2hex(u8 in_s[], char out_b[], u32 len = 32);
|
||||
static void bytearray2hex(ecc_point in_s, char out_b[]);
|
||||
static void pubhex2point(char *in_b, ecc_point *out_s);
|
||||
static void sm2_key_gen(SM2_key *sm2_keypair,bool buffed=true);
|
||||
static void SM2_encrypt(u8 *pub, u8 *M, u32 Mlen, u8 *C, u32 *Clen, s32 mode = 0);
|
||||
static void SM2_decrypt(u8 *pri, u8 *C, u32 Clen, u8 *M, u32 *Mlen, s32 mode = 0);
|
||||
static void SM2_sign(u8 *pri, u8 *data, u32 datalen, u8 *out);
|
||||
static bool SM2_verify(u8 *pub, u8 *data, u32 data_len, u8 *sig);
|
||||
static void SM3_HMAC(u8 *key, int keylen,u8 *input, int ilen,u8 output[32] );
|
||||
static bool SM4_encrypt(u8 *key_origin, u32 key_len, u8 *in_origin, u32 in_len, u8 *out, u32 *out_len);
|
||||
static bool SM4_decrypt(u8 *key_origin, u32 key_len, u8 *in, u32 in_len, u8 *out, u32 *out_len);
|
||||
static uint8_t get_rand();
|
||||
private:
|
||||
Crypto();
|
||||
};
|
||||
|
||||
#endif // CRYPTO_H
|
|
@ -1,31 +0,0 @@
|
|||
#ifndef DLLN3X_HANDLER_H
|
||||
#define DLLN3X_HANDLER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QQmlEngine>
|
||||
#include <QSerialPort>
|
||||
#include "DLLN3X.h"
|
||||
#include "3rdparty/RibbonUI/lib_source/definitions.h"
|
||||
|
||||
class DLLN3X_Handler : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
QML_SINGLETON
|
||||
QML_NAMED_ELEMENT(DLLN3XHandler)
|
||||
Q_PROPERTY_RW(bool, handle_serial);
|
||||
Q_PROPERTY_RW(QSerialPort*, serial);
|
||||
public:
|
||||
explicit DLLN3X_Handler(QObject *parent = nullptr);
|
||||
Q_INVOKABLE bool send(QString data);
|
||||
|
||||
signals:
|
||||
void zigbee_recved(QByteArray data, QByteArray sender);
|
||||
|
||||
private:
|
||||
zigbee_protocol::DLLN3X* _zigbee=nullptr;
|
||||
void zigbee_callback(zigbee_protocol::ZigbeeFrame zframe);
|
||||
void close();
|
||||
};
|
||||
|
||||
#endif // DLLN3X_HANDLER_H
|
|
@ -1,39 +0,0 @@
|
|||
#ifndef EVENTSBUS_H
|
||||
#define EVENTSBUS_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QQmlEngine>
|
||||
#include <QJsonObject>
|
||||
#include "3rdparty/RibbonUI/lib_source/definitions.h"
|
||||
|
||||
class Event : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
QML_NAMED_ELEMENT(Event)
|
||||
Q_PROPERTY_RW(QByteArray,type)
|
||||
public:
|
||||
explicit Event(QObject *parent = nullptr){};
|
||||
signals:
|
||||
void triggered(QJsonObject data);
|
||||
};
|
||||
|
||||
class EventsBus : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
QML_SINGLETON
|
||||
QML_NAMED_ELEMENT(EventsBus)
|
||||
Q_PROPERTY_RW(QList<QJsonObject>, event_history)
|
||||
public:
|
||||
static EventsBus* instance();
|
||||
static EventsBus* create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return instance();}
|
||||
Q_INVOKABLE void push_data(QString type, QJsonObject data);
|
||||
Q_INVOKABLE void reg_event(Event* event);
|
||||
Q_INVOKABLE void unreg_event(Event* event);
|
||||
private:
|
||||
explicit EventsBus(QObject *parent = nullptr);
|
||||
QList<Event*> _events;
|
||||
};
|
||||
|
||||
#endif // EVENTSBUS_H
|
|
@ -1,40 +0,0 @@
|
|||
#ifndef _M_PROTOCOL_H_
|
||||
#define _M_PROTOCOL_H_
|
||||
#include "protocol_data.h"
|
||||
#include "zigbeeframe.h"
|
||||
#include "config.h"
|
||||
|
||||
#define SELF_ADDR 0xEEEE //按照实际修改
|
||||
|
||||
namespace zigbee_protocol {
|
||||
class Protocol
|
||||
{
|
||||
public:
|
||||
static Protocol* getInstance();
|
||||
void protocal_wrapper(data_frame *frame, u8 type, u16 length, u8 *data, bool use_crc);
|
||||
void base_frame_maker(void *in_frame, base_frame *out_frame, u16 dest_addr,device *dev,u16 node_addr=0);
|
||||
bool base_frame_parser(base_frame *in_frame, void **out_frame, device *dev);
|
||||
uint16_t crc16_xmodem(const uint8_t *buffer, uint32_t buffer_length);
|
||||
bool bytecmp(u8 *a, u8 *b, u16 length);
|
||||
bool data_frame_verify(data_frame *frame);
|
||||
void load_internal_keypair(int (*hex2bytearray)(char s[], unsigned char bits[], u32 len));
|
||||
void HMAC_identify(device *self, device *node, hmac_frame *hframe,
|
||||
void (*sendToserver)(ZigbeeFrame &data),
|
||||
void (*SM3_HMAC)(u8 *key, int keylen,u8 *input, int ilen,u8 output[32] ));
|
||||
void HMAC_changeVerifykey(u8 key[16], device* self, device *node, void (*sendTonode)(ZigbeeFrame &data),
|
||||
bool (* SM4_encrypt)(u8 *key_origin, u32 key_len, u8 *in_origin, u32 in_len, u8 *out, u32 *out_len));
|
||||
void zigbee_data_encrypt(data_frame *data, crypto_zdata_frame *zdata,
|
||||
bool (* SM4_encrypt)(u8 *key_origin, u32 key_len, u8 *in_origin, u32 in_len, u8 *out, u32 *out_len),
|
||||
QString en_key = "");
|
||||
bool zigbee_data_dectypt(data_frame *data, crypto_zdata_frame *zdata,
|
||||
bool (* SM4_decrypt)(u8 *key_origin, u32 key_len, u8 *in, u32 in_len, u8 *out, u32 *out_len),
|
||||
QString en_key = "");
|
||||
QString hmac_verify_key = "11223344556677888877665544332211";
|
||||
QStringList pre_hmac_verify_key;
|
||||
private:
|
||||
Protocol();
|
||||
~Protocol();
|
||||
Config* _config = nullptr;
|
||||
};
|
||||
}
|
||||
#endif
|
|
@ -1,334 +0,0 @@
|
|||
#ifndef PROTOCOL_DATA_H
|
||||
#define PROTOCOL_DATA_H
|
||||
#include <QTcpSocket>
|
||||
#include "typedef.h"
|
||||
|
||||
typedef struct SM2_KEY
|
||||
{
|
||||
uint8_t pub_key[129]={0};
|
||||
uint8_t pri_key[65]={0};
|
||||
} SM2_key;
|
||||
|
||||
typedef struct SM2_KEY_EXCHANGE
|
||||
{
|
||||
uint8_t session_key[17]={0};
|
||||
uint8_t peer_pub_key[65]={0};
|
||||
} SM2_keyexc;
|
||||
|
||||
typedef struct data_frame
|
||||
{
|
||||
u16 head;
|
||||
u8 type;
|
||||
u8 use_crc;
|
||||
u16 data_length;
|
||||
u16 crc;
|
||||
u8 data[1024];
|
||||
} data_frame;
|
||||
|
||||
#define DATA_FRAME_PREFIX_LEN (sizeof(u16)*3+sizeof(u8)*2)
|
||||
#define DATA_FRAME_HEAD 0xAAAA
|
||||
#define new_data_frame(num) \
|
||||
struct \
|
||||
{ \
|
||||
u16 head; \
|
||||
u8 type; \
|
||||
u8 use_crc; \
|
||||
u16 data_length; \
|
||||
u16 crc; \
|
||||
u8 data[num]; \
|
||||
}
|
||||
|
||||
typedef struct digi_env
|
||||
{
|
||||
u16 head;
|
||||
u16 length;
|
||||
u8 crypted_session_key[112];
|
||||
u8 crypted_data[1024];
|
||||
} digi_env;
|
||||
|
||||
#define SM4_PADDING_LEN sizeof(u8)*16
|
||||
#define DIGI_ENV_PREFIX_LEN sizeof(u16)*2
|
||||
#define DIGI_ENV_SESSION_KEY_LEN sizeof(u8)*112
|
||||
#define DIGI_ENV_HEAD 0xAAAB
|
||||
#define new_digi_env(num) \
|
||||
struct \
|
||||
{ \
|
||||
u16 head; \
|
||||
u16 length; \
|
||||
u8 crypted_session_key[112]; \
|
||||
u8 crypted_data[num]; \
|
||||
}
|
||||
|
||||
typedef struct ssl_frame
|
||||
{
|
||||
u16 head;
|
||||
u16 length;
|
||||
u8 data[1024];
|
||||
} ssl_frame;
|
||||
|
||||
#define SSL_FRAME_PREFIX_LEN sizeof(u16)*2
|
||||
#define SSL_FRAME_HEAD 0xAAAC
|
||||
#define new_ssl_frame(num) \
|
||||
struct \
|
||||
{ \
|
||||
u16 head; \
|
||||
u16 length; \
|
||||
u8 data[num]; \
|
||||
}
|
||||
|
||||
typedef struct base_frame
|
||||
{
|
||||
u16 head;
|
||||
u16 ori_addr;
|
||||
u16 des_addr;
|
||||
u16 node_addr;
|
||||
u16 id;
|
||||
u16 length;
|
||||
u8 reset_num;
|
||||
u8 rand_num;
|
||||
u8 data[2048];
|
||||
} base_frame;
|
||||
|
||||
#define BASE_FRAME_PREFIX_LEN (sizeof(u8)*2+sizeof(u16)*6)
|
||||
#define BASE_FRAME_HEAD 0xAAAD
|
||||
#define BASE_FRAME_RESET_NUM 10000
|
||||
#define new_base_frame(num) \
|
||||
struct \
|
||||
{ \
|
||||
u16 head; \
|
||||
u16 ori_addr; \
|
||||
u16 des_addr; \
|
||||
u16 node_addr; \
|
||||
u16 id; \
|
||||
u16 length; \
|
||||
u8 reset_num; \
|
||||
u8 rand_num; \
|
||||
u8 data[num]; \
|
||||
}
|
||||
|
||||
#define HMAC_FRAME_PREFIX_LEN (sizeof(u16)*2+sizeof(u8))
|
||||
#define HMAC_FRAME_HEAD 0xAAAE
|
||||
typedef struct hmac_frame
|
||||
{
|
||||
u16 head;
|
||||
u16 length;
|
||||
u8 value;
|
||||
u8 hmac[33];
|
||||
} hmac_frame;
|
||||
|
||||
#define CRYPTO_ZDATA_FRAME_HEAD 0xAAAF
|
||||
typedef struct crypto_zdata_frame
|
||||
{
|
||||
u16 head;
|
||||
u16 length;
|
||||
u8 data[100];
|
||||
}crypto_zdata_frame;
|
||||
|
||||
#define CRYPTO_ZDATA_FRAME_PREFIX_LEN sizeof(u16)*2
|
||||
#define new_crypto_zdata_frame(num) \
|
||||
struct \
|
||||
{ \
|
||||
u16 head; \
|
||||
u16 length; \
|
||||
u8 data[num]; \
|
||||
}
|
||||
|
||||
#define RAND_DATA_PACK_PREFIX_LEN sizeof(u16)
|
||||
typedef struct rand_data_pack
|
||||
{
|
||||
u16 rand_num;
|
||||
u8 *data;
|
||||
}rand_data_pack;
|
||||
|
||||
typedef struct device
|
||||
{
|
||||
u16 addr;
|
||||
u16 id;
|
||||
u8 rand_num;
|
||||
u8 verified;
|
||||
u8 online;
|
||||
u8 logined;
|
||||
u8 stage;
|
||||
u8 chlg_buf[8];
|
||||
u8 ip[30];
|
||||
u16 port;
|
||||
SM2_keyexc key_pair;
|
||||
bool operator == (const device &e){
|
||||
return this->addr == e.addr;
|
||||
}
|
||||
} device;
|
||||
|
||||
#define MQTT_CLIENT_OFFLINE_DATA_TYPE 0
|
||||
#define MQTT_CLIENT_OFFLINE_DATA_LENGTH sizeof(u16)
|
||||
typedef struct mqtt_client_offline_data
|
||||
{
|
||||
u16 addr;
|
||||
}mqtt_client_offline_data;
|
||||
|
||||
#define LOGIN_DATA_TYPE 0
|
||||
#define LOGIN_DATA_LENGTH (sizeof(int)+sizeof(u8)*32)
|
||||
typedef struct login_data
|
||||
{
|
||||
int user_id;
|
||||
unsigned char passwd_sm3[32];
|
||||
}login_data;
|
||||
|
||||
#define LOGIN_RESP_LENGTH (sizeof(int)*3+sizeof(u8)*20)
|
||||
typedef struct login_resp
|
||||
{
|
||||
int user_id;
|
||||
int gate_addr;
|
||||
int identity;
|
||||
unsigned char user_name[20];
|
||||
}login_resp;
|
||||
|
||||
#define SENSOR_DATA_TYPE 1
|
||||
#define SENSOR_DATA_LENGTN sizeof(double)*4
|
||||
typedef struct sensor_data
|
||||
{
|
||||
double ppm;
|
||||
double temp;
|
||||
double humi;
|
||||
double flare;
|
||||
}sensor_data;
|
||||
|
||||
#define THRESHOLD_DATA_TYPE 2
|
||||
typedef struct threshold_data
|
||||
{
|
||||
int air[2];
|
||||
int temp[2];
|
||||
int humi[2];
|
||||
int flare[2];
|
||||
}threshold_data;
|
||||
|
||||
typedef struct thumb_member
|
||||
{
|
||||
int id;
|
||||
int identity;
|
||||
char name[20];
|
||||
bool lock;
|
||||
time_t time;
|
||||
}thumb_member;
|
||||
|
||||
#define THUMB_MEMBERS_DATA_TYPE 3
|
||||
#define THUMB_MEMBERS_DATA_PREFIX sizeof(int)*2
|
||||
typedef struct thumb_members_data
|
||||
{
|
||||
int count;
|
||||
thumb_member members[100];
|
||||
}thumb_members_data;
|
||||
|
||||
typedef struct face_member
|
||||
{
|
||||
int id;
|
||||
int identity;
|
||||
char name[20];
|
||||
time_t signin_time;
|
||||
time_t signout_time;
|
||||
}face_member;
|
||||
|
||||
#define FACE_MEMBERS_DATA_TYPE 4
|
||||
#define FACE_MEMBERS_DATA_PREFIX sizeof(int)*2
|
||||
typedef struct face_members_data
|
||||
{
|
||||
int count;
|
||||
int online;
|
||||
face_member members[100];
|
||||
}face_members_data;
|
||||
|
||||
#define WARN_IMAGE_FIRE_TYPE 1
|
||||
#define WARN_IMAGE_PEOPLE_TYPE 2
|
||||
|
||||
#define WARN_DATA_TYPE 5
|
||||
typedef struct warn_data
|
||||
{
|
||||
time_t time;
|
||||
int gate;
|
||||
int alert_id;
|
||||
struct alert
|
||||
{
|
||||
unsigned char type;
|
||||
double threshold_data;
|
||||
double now_data;
|
||||
bool is_predict;
|
||||
}alert;
|
||||
}warn_data;
|
||||
|
||||
#define CLIENT_PHOTO_DATA_TYPE 7
|
||||
#define CLIENT_PHOTO_DATA_PREFIX (sizeof(int)*3+sizeof(char)*30+sizeof(u8))
|
||||
typedef struct client_photo_data
|
||||
{
|
||||
int data_length;
|
||||
int gate;
|
||||
int image_id;
|
||||
char name[30];
|
||||
u8 type;
|
||||
u8 data[51200];
|
||||
}client_photo_data;
|
||||
|
||||
#define new_client_photo_data(num) \
|
||||
struct client_photo_data \
|
||||
{ \
|
||||
int gate; \
|
||||
int image_id; \
|
||||
int data_length; \
|
||||
char name[30]; \
|
||||
u8 type; \
|
||||
u8 data[num]; \
|
||||
}
|
||||
|
||||
#define ORI_FACE_MEMBER_DATA_TYPE 50
|
||||
#define ORI_FACE_MEMBER_DATA_PREFIX 2
|
||||
typedef struct ori_face_members_data
|
||||
{
|
||||
u8 count;
|
||||
bool is_offline;
|
||||
u8 members[256];
|
||||
} ori_face_members_data;
|
||||
|
||||
#define new_ori_face_members_data(num) \
|
||||
struct \
|
||||
{ \
|
||||
u8 count; \
|
||||
bool is_offline; \
|
||||
u8 members[num]; \
|
||||
}
|
||||
|
||||
#define ORI_THUMB_MEMBER_DATA_TYPE 51
|
||||
typedef struct ori_thumb_member_data
|
||||
{
|
||||
u8 id;
|
||||
bool is_open;
|
||||
}ori_thumb_member_data;
|
||||
|
||||
#define PHOTO_DATA_TYPE 52
|
||||
#define PHOTO_DATA_PREFIX (sizeof(int)+sizeof(char)*30+sizeof(u8))
|
||||
typedef struct photo_data
|
||||
{
|
||||
int data_length;
|
||||
char name[30];
|
||||
u8 type;
|
||||
u8 data[51200];
|
||||
}photo_data;
|
||||
|
||||
#define new_photo_data(num) \
|
||||
struct photo_data \
|
||||
{ \
|
||||
int data_length; \
|
||||
char name[30]; \
|
||||
u8 type; \
|
||||
u8 data[num]; \
|
||||
}
|
||||
|
||||
#define FUNC_DATA_TYPE 99
|
||||
typedef struct func_data
|
||||
{
|
||||
bool sensor;
|
||||
bool threshold;
|
||||
bool warns;
|
||||
bool face_member;
|
||||
bool thumb_member;
|
||||
}func_data;
|
||||
|
||||
|
||||
#endif // PROTOCOL_DATA_H
|
|
@ -1,65 +0,0 @@
|
|||
#ifndef SERIALPORTMANAGER_H
|
||||
#define SERIALPORTMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QQmlEngine>
|
||||
#include <QTimer>
|
||||
#include <QSerialPort>
|
||||
#include "DLLN3X.h"
|
||||
#include "3rdparty/RibbonUI/lib_source/definitions.h"
|
||||
#include "zigbeeparser.h"
|
||||
#include "eventsbus.h"
|
||||
|
||||
class SerialPortManager : public QObject
|
||||
{
|
||||
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
QML_SINGLETON
|
||||
QML_NAMED_ELEMENT(SerialPortManager)
|
||||
Q_PROPERTY(QList<QString> available_ports READ available_ports() NOTIFY available_portsChanged FINAL)
|
||||
|
||||
public:
|
||||
enum class SendType{
|
||||
Blank=0,
|
||||
WithCarriageEnter=1,
|
||||
WithLineFeed=2,
|
||||
WithCarriageEnterAndLineFeed=3
|
||||
};
|
||||
Q_ENUM(SendType)
|
||||
|
||||
Q_PROPERTY_RW(int, available_ports_count)
|
||||
Q_PROPERTY_RW(bool, opened)
|
||||
Q_PROPERTY_RW(bool, closed)
|
||||
Q_PROPERTY_RW(bool, readyRead)
|
||||
Q_PROPERTY_RW(bool, handledBymanager)
|
||||
Q_PROPERTY_RW(bool, recv_hex)
|
||||
Q_PROPERTY_RW(bool, send_hex)
|
||||
Q_PROPERTY_RW(SendType, send_type)
|
||||
|
||||
public:
|
||||
static SerialPortManager* instance();
|
||||
static SerialPortManager* create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return instance();}
|
||||
QList<QString> available_ports();
|
||||
Q_INVOKABLE bool open(QString port_name, QString baudrate, QString databits,
|
||||
QString parity, QString stopbits);
|
||||
Q_INVOKABLE void close();
|
||||
Q_INVOKABLE QByteArray read();
|
||||
Q_INVOKABLE bool write(QString data);
|
||||
Q_INVOKABLE QSerialPort* get_serial();
|
||||
|
||||
signals:
|
||||
void available_portsChanged(QList<QString> ports);
|
||||
private:
|
||||
explicit SerialPortManager(QObject *parent = nullptr);
|
||||
~SerialPortManager();
|
||||
QTimer _serial_port_scanner;
|
||||
QSerialPort _serial_port;
|
||||
zigbee_protocol::DLLN3X* _zigbee=nullptr;
|
||||
void _ready_for_read();
|
||||
void zigbee_callback(zigbee_protocol::ZigbeeFrame zframe);
|
||||
Event _event;
|
||||
EventsBus* _bus = nullptr;
|
||||
};
|
||||
|
||||
#endif // SERIALPORTMANAGER_H
|
|
@ -1,53 +0,0 @@
|
|||
#ifndef ZIGBEEPARSER_H
|
||||
#define ZIGBEEPARSER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QQmlEngine>
|
||||
#include <QMap>
|
||||
#include <QJsonObject>
|
||||
#include <QQueue>
|
||||
#include <zigbeeframe.h>
|
||||
#include "3rdparty/RibbonUI/lib_source/definitions.h"
|
||||
#include "protocol.h"
|
||||
#include "eventsbus.h"
|
||||
#include "config.h"
|
||||
|
||||
class ZigBeeParser : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
QML_SINGLETON
|
||||
QML_NAMED_ELEMENT(ZigBeeParser)
|
||||
Q_PROPERTY_RW(QList<uint16_t>, allow_list)
|
||||
Q_PROPERTY_RW(QList<uint16_t>, deny_list)
|
||||
Q_PROPERTY_RW(QList<uint16_t>, wait_queue)
|
||||
Q_PROPERTY(QString self_addr READ self_addr FINAL)
|
||||
Q_PROPERTY(QString hmac_verify_key READ hmac_verify_key WRITE setHmac_verify_key NOTIFY hmac_verify_keyChanged FINAL)
|
||||
Q_PROPERTY(QStringList pre_hmac_verify_key READ pre_hmac_verify_key WRITE setPre_hmac_verify_key NOTIFY pre_hmac_verify_keyChanged FINAL)
|
||||
public:
|
||||
Q_INVOKABLE QString self_addr(){ return _self_addr;};
|
||||
QString hmac_verify_key(){ return _protocol->hmac_verify_key;};
|
||||
void setHmac_verify_key(QString key){ _config->Set("Protocol","hmac_verify_key",key); _protocol->hmac_verify_key = key;emit hmac_verify_keyChanged();};
|
||||
QStringList pre_hmac_verify_key(){ return _protocol->pre_hmac_verify_key;};
|
||||
void setPre_hmac_verify_key(QStringList keys){ _config->SetArray("Protocol","pre_hmac_verify_key",keys); _protocol->pre_hmac_verify_key = keys;emit pre_hmac_verify_keyChanged();};
|
||||
void data_parser(zigbee_protocol::ZigbeeFrame &zframe, bool is_demo = false);
|
||||
void message_parser(QJsonObject message);
|
||||
static ZigBeeParser* instance();
|
||||
static ZigBeeParser* create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return instance();}
|
||||
signals:
|
||||
void hmac_verify_keyChanged();
|
||||
void pre_hmac_verify_keyChanged();
|
||||
private:
|
||||
explicit ZigBeeParser(QObject *parent = nullptr);
|
||||
~ZigBeeParser();
|
||||
void src_port_parser(zigbee_protocol::ZigbeeFrame &zframe, bool is_demo = false);
|
||||
void des_port_parser(zigbee_protocol::ZigbeeFrame &zframe, bool is_demo = false);
|
||||
void remote_addr_parser(zigbee_protocol::ZigbeeFrame &zframe, bool is_demo = false);
|
||||
QMap<uint16_t, QPair<device,device>> nodes;
|
||||
Event _event;
|
||||
EventsBus *_bus = nullptr;
|
||||
zigbee_protocol::Protocol* _protocol = nullptr;
|
||||
QString _self_addr = QString::number(SELF_ADDR,16).toUpper();
|
||||
Config* _config = nullptr;
|
||||
};
|
||||
#endif // ZIGBEEPARSER_H
|
23367
languages/en_US.ts
23367
languages/en_US.ts
File diff suppressed because it is too large
Load Diff
23367
languages/zh_CN.ts
23367
languages/zh_CN.ts
File diff suppressed because it is too large
Load Diff
74
qml/Main.qml
74
qml/Main.qml
|
@ -1,74 +0,0 @@
|
|||
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()
|
||||
}
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,238 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,96 +0,0 @@
|
|||
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 = []
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,137 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
|
@ -1,141 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
|
@ -1,54 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Controls
|
||||
import RibbonUI
|
||||
|
||||
RibbonView{
|
||||
id: view
|
||||
anchors{
|
||||
top: parent.top
|
||||
bottom: parent.bottom
|
||||
}
|
||||
spacing: 0
|
||||
|
||||
property alias delegate: message_list.delegate
|
||||
property alias message_model: message_model
|
||||
property alias view: message_list
|
||||
|
||||
ListModel{
|
||||
id: message_model
|
||||
}
|
||||
|
||||
ListView{
|
||||
id: message_list
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
Layout.preferredHeight: contentHeight
|
||||
Layout.preferredWidth: parent.width
|
||||
interactive: false
|
||||
model: message_model
|
||||
add: Transition {
|
||||
NumberAnimation { properties: "x,y"; from: message_list.height; duration: 1000 }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
import QtQuick
|
||||
import RibbonUI
|
||||
import org.wangwenx190.FramelessHelper
|
||||
|
||||
Window {
|
||||
id:window
|
||||
default property alias content: container.data
|
||||
property alias title_bar: titleBar
|
||||
property alias popup: pop
|
||||
property bool comfirmed_quit: false
|
||||
visible: false
|
||||
color: {
|
||||
if (FramelessHelper.blurBehindWindowEnabled) {
|
||||
return "transparent";
|
||||
}
|
||||
if (FramelessUtils.systemTheme === FramelessHelperConstants.Dark) {
|
||||
return FramelessUtils.defaultSystemDarkColor;
|
||||
}
|
||||
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;
|
||||
}
|
||||
RibbonTitleBar {
|
||||
id: titleBar
|
||||
}
|
||||
Item{
|
||||
id:container
|
||||
anchors{
|
||||
top: titleBar.bottom
|
||||
left: parent.left
|
||||
right: parent.right
|
||||
bottom: parent.bottom
|
||||
}
|
||||
clip: true
|
||||
}
|
||||
Connections{
|
||||
target: RibbonTheme
|
||||
function onTheme_modeChanged() {
|
||||
if (RibbonTheme.dark_mode)
|
||||
FramelessUtils.systemTheme = FramelessHelperConstants.Dark
|
||||
else
|
||||
FramelessUtils.systemTheme = FramelessHelperConstants.Light
|
||||
}
|
||||
}
|
||||
Rectangle{
|
||||
z:99
|
||||
anchors.fill: parent
|
||||
color: !RibbonTheme.dark_mode ? Qt.rgba(255,255,255,0.3) : Qt.rgba(0,0,0,0.3)
|
||||
visible: !Window.active
|
||||
}
|
||||
RibbonPopup{
|
||||
id: pop
|
||||
}
|
||||
RibbonPopupDialog{
|
||||
id: dialog
|
||||
positiveText: qsTr("Quit")
|
||||
neutralText: qsTr("Minimize")
|
||||
negativeText: qsTr("Cancel")
|
||||
message: qsTr("Do you want to quit the APP?")
|
||||
title: qsTr("Please note")
|
||||
buttonFlags: RibbonPopupDialogType.NegativeButton | RibbonPopupDialogType.PositiveButton | RibbonPopupDialogType.NeutralButton
|
||||
onNeutralClicked: window.visibility = Window.Minimized
|
||||
onPositiveClicked: {
|
||||
comfirmed_quit = true
|
||||
Qt.quit()
|
||||
}
|
||||
}
|
||||
onClosing:function(event){
|
||||
window.raise()
|
||||
event.accepted = comfirmed_quit
|
||||
if (!comfirmed_quit)
|
||||
dialog.open()
|
||||
}
|
||||
}
|
|
@ -1,210 +0,0 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Controls
|
||||
import QtQuick.Window
|
||||
import RibbonUI
|
||||
import ProtocolParser
|
||||
import "."
|
||||
|
||||
Item{
|
||||
id: control
|
||||
property bool show_tooltip: false
|
||||
property bool auto_scroll_to_bottom: false
|
||||
property double top_padding: 0
|
||||
property double bottom_padding: 0
|
||||
property bool handle_serial: true
|
||||
|
||||
onAuto_scroll_to_bottomChanged: if(auto_scroll_to_bottom) serial_view.scroll_to_bottom()
|
||||
|
||||
RibbonMessageListView{
|
||||
id: serial_view
|
||||
anchors.fill: parent
|
||||
top_padding: serial_title_bar.height + control.top_padding + (!RibbonTheme.modern_style ? 10 : 0)
|
||||
bottom_padding: message_sender.height + control.bottom_padding
|
||||
delegate: ZigBeeMessage{
|
||||
sender_text: `${Qt.formatDateTime(new Date(), "yyyy-MM-dd hh:mm:ss.zzz")} ${model.recieved ? '收' : '发'}`
|
||||
show_tooltip: control.show_tooltip
|
||||
component_width: serial_view.width / 2
|
||||
}
|
||||
view.onHeightChanged: {
|
||||
if (control.auto_scroll_to_bottom)
|
||||
scroll_to_bottom()
|
||||
}
|
||||
Connections{
|
||||
target: SerialPortManager
|
||||
function onReadyReadChanged(){
|
||||
if (control.handle_serial && SerialPortManager.readyRead)
|
||||
{
|
||||
var d = SerialPortManager.read()
|
||||
serial_view.message_model.append({
|
||||
note_text: d.toString(),
|
||||
recieved: true,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
RibbonRectangle{
|
||||
id: serial_title_bar
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
anchors.top: parent.top
|
||||
anchors.topMargin: control.top_padding + (!RibbonTheme.modern_style ? 10 : 0)
|
||||
implicitWidth: parent.width - 20
|
||||
implicitHeight: serial_title.contentHeight + 10
|
||||
radius: 5
|
||||
color: RibbonTheme.dark_mode ? Qt.rgba(0, 0, 0, 0.5) : Qt.rgba(255, 255, 255, 0.5)
|
||||
RibbonText {
|
||||
id: serial_title
|
||||
anchors.centerIn: parent
|
||||
text: "串口助手"
|
||||
color: RibbonTheme.dark_mode ? "white" : "black"
|
||||
font.pixelSize: 15
|
||||
view_only: true
|
||||
}
|
||||
}
|
||||
RibbonRectangle{
|
||||
id: message_sender
|
||||
property double padding: 10
|
||||
property bool folded: false
|
||||
anchors{
|
||||
bottom: parent.bottom
|
||||
bottomMargin: serial_view.bottom_padding - height
|
||||
horizontalCenter: parent.horizontalCenter
|
||||
}
|
||||
color: RibbonTheme.dark_mode ? Qt.rgba(0, 0, 0, 0.5) : Qt.rgba(255, 255, 255, 0.5)
|
||||
radius: 10
|
||||
bottomLeftRadius: 0
|
||||
bottomRightRadius: bottomLeftRadius
|
||||
width: serial_view.width
|
||||
height: message_sender_btn.height + fold_btn.height + padding*2
|
||||
states: [
|
||||
State{
|
||||
name:"fold"
|
||||
when: message_sender.folded
|
||||
PropertyChanges {
|
||||
target: message_sender
|
||||
height:fold_btn.height
|
||||
}
|
||||
},
|
||||
State{
|
||||
name:"unfold"
|
||||
when: !message_sender.folded
|
||||
PropertyChanges {
|
||||
target: message_sender
|
||||
height: (message_sender_btn.height + fold_btn.height + padding*2)
|
||||
}
|
||||
}
|
||||
]
|
||||
transitions: [
|
||||
Transition {
|
||||
from: "fold"
|
||||
to:"unfold"
|
||||
NumberAnimation {
|
||||
properties: "height"
|
||||
duration: 167
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
},
|
||||
Transition {
|
||||
from: "unfold"
|
||||
to:"fold"
|
||||
NumberAnimation {
|
||||
properties: "height"
|
||||
duration: 167
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
RibbonButton{
|
||||
id: fold_btn
|
||||
anchors{
|
||||
right: parent.right
|
||||
top: parent.top
|
||||
}
|
||||
icon_source: RibbonIcons.ChevronUp
|
||||
Behavior on rotation {
|
||||
NumberAnimation{
|
||||
duration: 100
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
rotation: message_sender.folded ? 0 : 180
|
||||
onClicked: message_sender.folded = !message_sender.folded
|
||||
show_bg: false
|
||||
show_hovered_bg: false
|
||||
tip_text: message_sender.folded ? qsTr("显示信息发送栏") : qsTr("隐藏信息发送栏")
|
||||
text_color: dark_mode ? "white" : "black"
|
||||
}
|
||||
RowLayout{
|
||||
anchors{
|
||||
top: fold_btn.bottom
|
||||
left: parent.left
|
||||
right: parent.right
|
||||
}
|
||||
RowLayout{
|
||||
Layout.preferredWidth: 200
|
||||
Layout.fillHeight: true
|
||||
Layout.margins: 10
|
||||
RibbonText{
|
||||
text: "发送结束符:"
|
||||
view_only: true
|
||||
}
|
||||
RibbonComboBox{
|
||||
id: serial_send_type_combo
|
||||
Layout.fillHeight: true
|
||||
model: ListModel {
|
||||
ListElement { text: "无" }
|
||||
ListElement { text: "换行" }
|
||||
ListElement { text: "回车" }
|
||||
ListElement { text: "换行和回车" }
|
||||
}
|
||||
enabled: SerialPortManager.opened
|
||||
icon_source: RibbonIcons.DataWhisker
|
||||
}
|
||||
}
|
||||
RibbonTextEdit{
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: max_height
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
id:message_sender_textbox
|
||||
max_height: message_sender_btn.height
|
||||
icon_source: RibbonIcons.TextField
|
||||
placeholderText: SerialPortManager.send_hex ? "请输入需要发送的信息(以16进制的形式输入, 空格分隔):" : "请输入需要发送的信息:"
|
||||
enabled: SerialPortManager.opened
|
||||
onCommit: {
|
||||
if (text)
|
||||
message_sender_btn.clicked()
|
||||
}
|
||||
}
|
||||
RibbonPushButton{
|
||||
Layout.alignment: Qt.AlignVCenter
|
||||
id:message_sender_btn
|
||||
text: "发送"
|
||||
show_tooltip: false
|
||||
icon_source: RibbonIcons.Send
|
||||
onClicked: {
|
||||
SerialPortManager.send_hex = tab_bar.send_hex
|
||||
SerialPortManager.send_type = serial_send_type_combo.currentText === "无" ? SerialPortManager.Blank
|
||||
: serial_send_type_combo.currentText === "回车" ? SerialPortManager.WithCarriageEnter
|
||||
: serial_send_type_combo.currentText === "换行" ? SerialPortManager.WithLineFeed
|
||||
: SerialPortManager.WithCarriageEnterAndLineFeed
|
||||
SerialPortManager.write(message_sender_textbox.text)
|
||||
serial_view.message_model.append({
|
||||
note_text: message_sender_textbox.text,
|
||||
recieved: false,
|
||||
})
|
||||
message_sender_textbox.textedit.clear()
|
||||
}
|
||||
|
||||
enabled: SerialPortManager.opened
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function clear_all_message()
|
||||
{
|
||||
serial_view.message_model.clear()
|
||||
}
|
||||
}
|
|
@ -1,432 +0,0 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import RibbonUI
|
||||
import ProtocolParser
|
||||
|
||||
RibbonTabBar {
|
||||
property bool send_hex: serial_send_hex_checkbox.checked
|
||||
property bool show_tooltip: message_parse_checkbox.checked
|
||||
property bool auto_scroll_to_bottom: view_scroll_bottom_checkbox.checked
|
||||
property var center_view
|
||||
|
||||
RibbonTabPage{
|
||||
id: serial_page
|
||||
title: qsTr("串口设置")
|
||||
RibbonTabGroup{
|
||||
text: qsTr("参数配置")
|
||||
width: serial_layout.width + 30
|
||||
RowLayout{
|
||||
id: serial_layout
|
||||
anchors.centerIn: parent
|
||||
height: parent.height
|
||||
spacing: 20
|
||||
ColumnLayout{
|
||||
Layout.fillHeight: true
|
||||
spacing: 20
|
||||
RowLayout{
|
||||
Layout.preferredWidth: 340 + spacing
|
||||
RibbonText{
|
||||
text: qsTr("端 口:")
|
||||
view_only: true
|
||||
}
|
||||
RibbonComboBox{
|
||||
id: serial_port_combo
|
||||
Layout.fillWidth: true
|
||||
model: ListModel {
|
||||
id: serial_model
|
||||
}
|
||||
icon_source: RibbonIcons.SerialPort
|
||||
currentIndex: 0
|
||||
enabled: SerialPortManager.closed
|
||||
Connections{
|
||||
target: SerialPortManager
|
||||
function onAvailable_portsChanged(ports){
|
||||
serial_model.clear()
|
||||
for (let i=0;i<SerialPortManager.available_ports_count;i++)
|
||||
serial_model.append({text:ports[i]})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
RowLayout{
|
||||
Layout.preferredWidth: 340 + spacing
|
||||
spacing: 10
|
||||
RowLayout{
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
RibbonText{
|
||||
text: qsTr("波特率:")
|
||||
view_only: true
|
||||
}
|
||||
RibbonComboBox{
|
||||
id: serial_baudrate_combo
|
||||
Layout.fillWidth: true
|
||||
model: ListModel {
|
||||
ListElement { text: "2400" }
|
||||
ListElement { text: "4800" }
|
||||
ListElement { text: "9600" }
|
||||
ListElement { text: "19200" }
|
||||
ListElement { text: "38400" }
|
||||
ListElement { text: "57600" }
|
||||
ListElement { text: "115200" }
|
||||
}
|
||||
enabled: SerialPortManager.closed
|
||||
currentIndex: 6
|
||||
icon_source: RibbonIcons.NumberSymbol
|
||||
}
|
||||
}
|
||||
RowLayout{
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
RibbonText{
|
||||
text: qsTr("数据位:")
|
||||
view_only: true
|
||||
}
|
||||
RibbonComboBox{
|
||||
id: serial_databits_combo
|
||||
Layout.fillWidth: true
|
||||
model: ListModel {
|
||||
ListElement { text: "5" }
|
||||
ListElement { text: "6" }
|
||||
ListElement { text: "7" }
|
||||
ListElement { text: "8" }
|
||||
}
|
||||
enabled: SerialPortManager.closed
|
||||
currentIndex: 3
|
||||
icon_source: RibbonIcons.DataWhisker
|
||||
}
|
||||
RibbonText{
|
||||
text: qsTr("位")
|
||||
view_only: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ColumnLayout{
|
||||
Layout.fillHeight: true
|
||||
Layout.preferredWidth: 140 + spacing
|
||||
spacing: 20
|
||||
RowLayout{
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
RibbonText{
|
||||
text: qsTr("校验码:")
|
||||
view_only: true
|
||||
}
|
||||
RibbonComboBox{
|
||||
id: serial_parity_combo
|
||||
Layout.fillWidth: true
|
||||
model: ListModel {
|
||||
ListElement { text: qsTr("NONE") }
|
||||
ListElement { text: qsTr("EVEN") }
|
||||
ListElement { text: qsTr("ODD") }
|
||||
}
|
||||
enabled: SerialPortManager.closed
|
||||
icon_source: RibbonIcons.DataWhisker
|
||||
}
|
||||
}
|
||||
RowLayout{
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
RibbonText{
|
||||
text: qsTr("停止位:")
|
||||
view_only: true
|
||||
}
|
||||
RibbonComboBox{
|
||||
id: serial_stopbits_combo
|
||||
Layout.fillWidth: true
|
||||
model: ListModel {
|
||||
ListElement { text: "1" }
|
||||
ListElement { text: "1.5" }
|
||||
ListElement { text: "2" }
|
||||
}
|
||||
enabled: SerialPortManager.closed
|
||||
icon_source: RibbonIcons.DataWhisker
|
||||
}
|
||||
}
|
||||
}
|
||||
RibbonPushButton{
|
||||
id: serial_open_btn
|
||||
Layout.alignment: Qt.AlignVCenter
|
||||
icon_source: RibbonIcons.Open
|
||||
show_tooltip: false
|
||||
text: SerialPortManager.closed ? qsTr("打开串口") : qsTr("关闭串口")
|
||||
onClicked: SerialPortManager.open(serial_port_combo.currentText,
|
||||
serial_baudrate_combo.currentText,
|
||||
serial_databits_combo.currentText,
|
||||
serial_parity_combo.currentText,
|
||||
serial_stopbits_combo.currentText)
|
||||
}
|
||||
}
|
||||
}
|
||||
RibbonTabGroup{
|
||||
text: qsTr("串口消息设置")
|
||||
width: serial_message_layout.width + 30
|
||||
RowLayout{
|
||||
id: serial_message_layout
|
||||
anchors.centerIn: parent
|
||||
height: parent.height - 20
|
||||
ColumnLayout{
|
||||
RibbonCheckBox{
|
||||
id: message_parse_checkbox
|
||||
text: qsTr("显示消息解析")
|
||||
checked: true
|
||||
}
|
||||
RibbonCheckBox{
|
||||
id: view_scroll_bottom_checkbox
|
||||
text: qsTr("自动定位最新消息")
|
||||
checked: true
|
||||
}
|
||||
RibbonButton{
|
||||
Layout.fillWidth: true
|
||||
icon_source: RibbonIcons.DismissCircle
|
||||
text: qsTr("清除旧消息")
|
||||
show_tooltip: false
|
||||
onClicked: {
|
||||
if (center_view.currentIndex === 0)
|
||||
center_view.zigbee_dataview.clear_all_message()
|
||||
else
|
||||
center_view.serial_assistant.clear_all_message()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
RibbonTabGroup{
|
||||
text: qsTr("串口助手设置")
|
||||
width: serial_assistant_layout.width + 30
|
||||
ColumnLayout{
|
||||
id: serial_assistant_layout
|
||||
anchors.centerIn: parent
|
||||
height: parent.height - 20
|
||||
spacing: 10
|
||||
RibbonCheckBox{
|
||||
id: serial_recv_hex_checkbox
|
||||
text: qsTr("十六进制接收")
|
||||
checked: SerialPortManager.recv_hex
|
||||
onCheckedChanged: SerialPortManager.recv_hex = checked
|
||||
}
|
||||
RibbonCheckBox{
|
||||
id: serial_send_hex_checkbox
|
||||
text: qsTr("十六进制发送")
|
||||
checked: SerialPortManager.send_hex
|
||||
onCheckedChanged: SerialPortManager.send_hex = checked
|
||||
}
|
||||
}
|
||||
}
|
||||
RibbonTabGroup{
|
||||
text: qsTr("串口数据流向")
|
||||
width: current_serial_indicator.contentWidth + 30
|
||||
RibbonText{
|
||||
anchors.centerIn: parent
|
||||
id: current_serial_indicator
|
||||
text: qsTr("传输至:") + (center_view.currentIndex === 0 ? qsTr("ZigBee") : qsTr("串口助手"))
|
||||
}
|
||||
}
|
||||
}
|
||||
RibbonTabPage{
|
||||
id: zigbee_protocol_page
|
||||
title: qsTr("ZigBee协议")
|
||||
RibbonTabGroup{
|
||||
width: device_list_layout.width + 30
|
||||
text: qsTr("设备列表")
|
||||
ColumnLayout{
|
||||
id: device_list_layout
|
||||
anchors.centerIn: parent
|
||||
height: parent.height
|
||||
spacing: 10
|
||||
RibbonButton{
|
||||
Layout.fillWidth: true
|
||||
icon_source: RibbonIcons.List
|
||||
text: qsTr("等待队列")
|
||||
show_tooltip: false
|
||||
onClicked: {
|
||||
show_popup("components/DeviceList.qml",{list_type:0})
|
||||
}
|
||||
}
|
||||
RibbonButton{
|
||||
Layout.fillWidth: true
|
||||
icon_source: RibbonIcons.List
|
||||
text: qsTr("认证列表")
|
||||
show_tooltip: false
|
||||
onClicked: {
|
||||
show_popup("components/DeviceList.qml",{list_type:1})
|
||||
}
|
||||
}
|
||||
RibbonButton{
|
||||
Layout.fillWidth: true
|
||||
icon_source: RibbonIcons.List
|
||||
text: qsTr("黑名单列表")
|
||||
show_tooltip: false
|
||||
onClicked: {
|
||||
show_popup("components/DeviceList.qml",{list_type:2})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
RibbonTabGroup{
|
||||
width: key_manage_layout.width + 30
|
||||
text: qsTr("密钥管理")
|
||||
ColumnLayout{
|
||||
id: key_manage_layout
|
||||
anchors.centerIn: parent
|
||||
height: parent.height
|
||||
spacing: 0
|
||||
RowLayout{
|
||||
spacing: 5
|
||||
RibbonText{
|
||||
text: qsTr("修改密钥:")
|
||||
}
|
||||
RibbonLineEdit{
|
||||
id: key_manage_edit
|
||||
placeholderText: qsTr("HEX形式16字节密钥")
|
||||
onCommit: key_manage_save_btn.clicked()
|
||||
}
|
||||
RibbonButton{
|
||||
id: key_manage_save_btn
|
||||
icon_source: RibbonIcons.Save
|
||||
text: qsTr("保存")
|
||||
show_tooltip: false
|
||||
onClicked: {
|
||||
var keys = []
|
||||
keys = ZigBeeParser.pre_hmac_verify_key
|
||||
keys.push(ZigBeeParser.hmac_verify_key)
|
||||
ZigBeeParser.hmac_verify_key = key_manage_edit.text
|
||||
ZigBeeParser.pre_hmac_verify_key = keys
|
||||
key_manage_edit.clear()
|
||||
}
|
||||
}
|
||||
}
|
||||
RibbonButton{
|
||||
Layout.fillWidth: true
|
||||
icon_source: RibbonIcons.List
|
||||
text: qsTr("历史密钥列表")
|
||||
show_tooltip: false
|
||||
onClicked: {
|
||||
show_popup("components/KeysList.qml")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
RibbonTabGroup{
|
||||
text: qsTr("调试选项")
|
||||
width: zigbee_debug_layout.width + 30
|
||||
RowLayout{
|
||||
id: zigbee_debug_layout
|
||||
anchors.centerIn: parent
|
||||
height: parent.height
|
||||
spacing: 10
|
||||
ColumnLayout{
|
||||
spacing: 10
|
||||
RibbonButton{
|
||||
Layout.fillWidth: true
|
||||
icon_source: RibbonIcons.Mail
|
||||
text: qsTr("生成接收数据")
|
||||
tip_text: qsTr("在接收数据流中生成一条测试接收数据")
|
||||
onClicked: {
|
||||
EventsBus.push_data("zigbee_parser",{
|
||||
type: "demo_recv_data",
|
||||
data_hex:zigbee_debug_layout.generate_value()
|
||||
})
|
||||
}
|
||||
}
|
||||
RibbonButton{
|
||||
Layout.fillWidth: true
|
||||
icon_source: RibbonIcons.Mail
|
||||
text: qsTr("生成验证数据")
|
||||
tip_text: qsTr("在验证信息流中生成一组测试验证数据")
|
||||
onClicked: {
|
||||
EventsBus.push_data("zigbee_parser",{
|
||||
type: "demo_verify_request"
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
ColumnLayout{
|
||||
spacing: 10
|
||||
RibbonButton{
|
||||
Layout.fillWidth: true
|
||||
icon_source: RibbonIcons.Mail
|
||||
text: qsTr("生成更换密钥数据")
|
||||
tip_text: qsTr("在验证信息流中生成一组测试更换密钥数据,需要有至少一条过期密钥")
|
||||
enabled: ZigBeeParser.pre_hmac_verify_key.length
|
||||
onClicked: {
|
||||
EventsBus.push_data("zigbee_parser",{
|
||||
type: "demo_verify_key_update"
|
||||
})
|
||||
}
|
||||
}
|
||||
RibbonButton{
|
||||
Layout.fillWidth: true
|
||||
icon_source: RibbonIcons.History
|
||||
text: qsTr("事件历史信息")
|
||||
tip_text: qsTr("查看所有事件历史信息(包含调试信息)")
|
||||
onClicked: {
|
||||
show_popup("components/EventsHistoryList.qml")
|
||||
}
|
||||
}
|
||||
}
|
||||
function generate_value(){
|
||||
function get_rand_byte()
|
||||
{
|
||||
return Math.floor(Math.random()*16).toString(16).toUpperCase()
|
||||
}
|
||||
|
||||
let repeat_count = 16
|
||||
let t = `FF 29 ${get_rand_byte()}${get_rand_byte()} 83 `+
|
||||
`${get_rand_byte()}${get_rand_byte()} ${get_rand_byte()}${get_rand_byte()} `+
|
||||
`AA AD 23 32 52 48 23 32 01 00 17 00 00 00 AA AA 01 00 10 00 00 `
|
||||
for (let j=0;j<repeat_count;j++){
|
||||
t+=` ${get_rand_byte()}${get_rand_byte()}`
|
||||
}
|
||||
t+=" FE"
|
||||
return t
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
RibbonTabPage{
|
||||
id: other_page
|
||||
title: qsTr("其他")
|
||||
RibbonTabGroup{
|
||||
width: theme_layout.width + 30
|
||||
text: qsTr("主题")
|
||||
RowLayout{
|
||||
id: theme_layout
|
||||
anchors.centerIn: parent
|
||||
height: parent.height
|
||||
spacing: 10
|
||||
RibbonComboBox{
|
||||
id: theme_combobox
|
||||
component_width: 120
|
||||
model: ListModel {
|
||||
id: model_theme
|
||||
ListElement { text: qsTr("浅色") }
|
||||
ListElement { text: qsTr("深色") }
|
||||
ListElement { text: qsTr("跟随系统") }
|
||||
}
|
||||
icon_source: RibbonIcons.DarkTheme
|
||||
Component.onCompleted: {
|
||||
let str = (RibbonTheme.theme_mode === RibbonThemeType.System ? qsTr("跟随系统") : RibbonTheme.theme_mode === RibbonThemeType.Light ? qsTr("浅色") : qsTr("深色"))
|
||||
currentIndex = find(str)
|
||||
}
|
||||
onActivated: {
|
||||
if (currentText === qsTr("跟随系统"))
|
||||
RibbonTheme.theme_mode = RibbonThemeType.System
|
||||
else if (currentText === qsTr("浅色"))
|
||||
RibbonTheme.theme_mode = RibbonThemeType.Light
|
||||
else
|
||||
RibbonTheme.theme_mode = RibbonThemeType.Dark
|
||||
}
|
||||
Connections{
|
||||
target: RibbonTheme
|
||||
function onTheme_modeChanged(){
|
||||
let str = (RibbonTheme.theme_mode === RibbonThemeType.System ? qsTr("跟随系统") : RibbonTheme.theme_mode === RibbonThemeType.Light ? qsTr("浅色") : qsTr("深色"))
|
||||
theme_combobox.currentIndex = theme_combobox.find(str)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,189 +0,0 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Controls
|
||||
import QtQuick.Window
|
||||
import RibbonUI
|
||||
import ProtocolParser
|
||||
import "."
|
||||
|
||||
Item{
|
||||
id: control
|
||||
property bool show_tooltip: false
|
||||
property bool auto_scroll_to_bottom: false
|
||||
property double top_padding: 0
|
||||
property double bottom_padding: 0
|
||||
property bool handle_serial: true
|
||||
|
||||
Connections{
|
||||
target: SerialPortManager
|
||||
function onOpenedChanged(){
|
||||
if (handle_serial&&SerialPortManager.opened)
|
||||
{
|
||||
SerialPortManager.handledBymanager = false
|
||||
}
|
||||
else
|
||||
SerialPortManager.handledBymanager = true
|
||||
}
|
||||
}
|
||||
|
||||
onHandle_serialChanged: {
|
||||
if (handle_serial)
|
||||
{
|
||||
SerialPortManager.handledBymanager = false
|
||||
}
|
||||
else
|
||||
{
|
||||
SerialPortManager.handledBymanager = true
|
||||
}
|
||||
}
|
||||
|
||||
onAuto_scroll_to_bottomChanged: {
|
||||
if(auto_scroll_to_bottom)
|
||||
{
|
||||
identify_view.scroll_to_bottom()
|
||||
data_view.scroll_to_bottom()
|
||||
}
|
||||
}
|
||||
Item{
|
||||
anchors{
|
||||
top:parent.top
|
||||
left: parent.left
|
||||
}
|
||||
width: parent.width / 2
|
||||
height: parent.height
|
||||
RibbonMessageListView{
|
||||
id: identify_view
|
||||
anchors.fill: parent
|
||||
top_padding: control.top_padding + identify_title_bar.height + (!RibbonTheme.modern_style ? 10 : 0)
|
||||
bottom_padding: control.bottom_padding
|
||||
delegate: ZigBeeMessage{
|
||||
show_tooltip: control.show_tooltip
|
||||
component_width: identify_view.width / 2
|
||||
}
|
||||
view.onHeightChanged: {
|
||||
if (control.auto_scroll_to_bottom)
|
||||
scroll_to_bottom()
|
||||
}
|
||||
Event{
|
||||
id:identify_view_event
|
||||
type: "zigbee_identify_data_view"
|
||||
onTriggered: function(data){
|
||||
identify_view.message_model.append(data)
|
||||
}
|
||||
}
|
||||
Component.onCompleted: {
|
||||
EventsBus.reg_event(identify_view_event)
|
||||
}
|
||||
}
|
||||
RibbonRectangle{
|
||||
id: identify_title_bar
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
anchors.top: parent.top
|
||||
anchors.topMargin: control.top_padding + (!RibbonTheme.modern_style ? 10 : 0)
|
||||
implicitWidth: parent.width - 20
|
||||
implicitHeight: identify_title.contentHeight + 10
|
||||
radius: 5
|
||||
color: RibbonTheme.dark_mode ? Qt.rgba(0, 0, 0, 0.5) : Qt.rgba(255, 255, 255, 0.5)
|
||||
RibbonText {
|
||||
id: identify_title
|
||||
anchors.centerIn: parent
|
||||
text: "认证信息流"
|
||||
color: RibbonTheme.dark_mode ? "white" : "black"
|
||||
font.pixelSize: 15
|
||||
view_only: true
|
||||
}
|
||||
}
|
||||
}
|
||||
Item{
|
||||
anchors{
|
||||
top:parent.top
|
||||
right: parent.right
|
||||
}
|
||||
width: parent.width / 2
|
||||
height: parent.height
|
||||
RibbonMessageListView{
|
||||
id: data_view
|
||||
top_padding: control.top_padding + data_title_bar.height
|
||||
bottom_padding: bottom_bar.height
|
||||
width: parent.width / 2
|
||||
delegate: ZigBeeMessage{
|
||||
show_tooltip: control.show_tooltip
|
||||
component_width: data_view.width / 2
|
||||
}
|
||||
view.onHeightChanged: {
|
||||
if (control.auto_scroll_to_bottom)
|
||||
scroll_to_bottom()
|
||||
}
|
||||
Event{
|
||||
id:data_view_event
|
||||
type: "zigbee_recv_data_view"
|
||||
onTriggered: function(data){
|
||||
data_view.message_model.append(data)
|
||||
}
|
||||
}
|
||||
Component.onCompleted: {
|
||||
EventsBus.reg_event(data_view_event)
|
||||
}
|
||||
|
||||
// Component.onCompleted: {
|
||||
// for(let i = 0; i<20;i++)
|
||||
// {
|
||||
// function generate_value(){
|
||||
// let repeat_count = Math.floor(Math.random()*40) + 6
|
||||
// let t = `FF ${repeat_count.toString(16).toUpperCase()} ${Math.floor(Math.random()*16).toString(16).toUpperCase()}${Math.floor(Math.random()*16).toString(16).toUpperCase()} ${Math.floor(Math.random()*16).toString(16).toUpperCase()}${Math.floor(Math.random()*16).toString(16).toUpperCase()} ${Math.floor(Math.random()*16).toString(16).toUpperCase()}${Math.floor(Math.random()*16).toString(16).toUpperCase()} ${Math.floor(Math.random()*16).toString(16).toUpperCase()}${Math.floor(Math.random()*16).toString(16).toUpperCase()}`
|
||||
// for (let j=0;j<repeat_count;j++){
|
||||
// t+=` ${Math.floor(Math.random()*16).toString(16).toUpperCase()}${Math.floor(Math.random()*16).toString(16).toUpperCase()}`
|
||||
// }
|
||||
// t+=" FE"
|
||||
// return t
|
||||
// }
|
||||
// if (Math.floor(Math.random()*2) === 0)
|
||||
// {
|
||||
// let v = generate_value()
|
||||
// let sender = v.split(" ").slice(4, 6).join("")
|
||||
// message_model.append({
|
||||
// text: v,
|
||||
// recieved: true,
|
||||
// sender: sender
|
||||
// })
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// let v = generate_value()
|
||||
// let sender = v.split(" ").slice(4, 6).join("")
|
||||
// message_model.append({
|
||||
// text: v,
|
||||
// decrypted_text: v,
|
||||
// recieved: true,
|
||||
// sender: sender
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
RibbonRectangle{
|
||||
id: data_title_bar
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
anchors.top: parent.top
|
||||
anchors.topMargin: control.top_padding + (!RibbonTheme.modern_style ? 10 : 0)
|
||||
implicitWidth: parent.width - 20
|
||||
implicitHeight: data_title.contentHeight + 10
|
||||
radius: 5
|
||||
color: RibbonTheme.dark_mode ? Qt.rgba(0, 0, 0, 0.5) : Qt.rgba(255, 255, 255, 0.5)
|
||||
RibbonText {
|
||||
id: data_title
|
||||
anchors.centerIn: parent
|
||||
text: "接收数据流"
|
||||
color: RibbonTheme.dark_mode ? "white" : "black"
|
||||
font.pixelSize: 15
|
||||
view_only: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function clear_all_message()
|
||||
{
|
||||
identify_view.message_model.clear()
|
||||
data_view.message_model.clear()
|
||||
}
|
||||
}
|
|
@ -1,366 +0,0 @@
|
|||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import RibbonUI
|
||||
|
||||
/* 仅适用于DL-LN3X系列 */
|
||||
|
||||
Flow{
|
||||
id: control
|
||||
property alias text: label.text
|
||||
property bool show_text: true
|
||||
property bool dark_mode: RibbonTheme.dark_mode
|
||||
property bool show_tooltip: true
|
||||
property bool is_recieved: false
|
||||
property double padding: 5
|
||||
property int font_size: 13
|
||||
property double component_width: 300
|
||||
property int data_width: component_width - (show_text ? label.width : 0) - z_head.width - z_length.width - z_src_port.width -
|
||||
z_des_port.width - z_address.width - z_tail.width - spacing*(show_text ? 13 : 12) - 6
|
||||
property var data_list
|
||||
|
||||
|
||||
RibbonText {
|
||||
id: label
|
||||
|
||||
height: z_data.height + control.padding * 2
|
||||
color: control.dark_mode ? "white" : control.is_recieved ? "black" : "white"
|
||||
font.pixelSize: control.font_size
|
||||
visible: control.show_text
|
||||
horizontalAlignment: RibbonText.AlignHCenter
|
||||
verticalAlignment: RibbonText.AlignVCenter
|
||||
}
|
||||
|
||||
RibbonRectangle{
|
||||
radius: 5
|
||||
topRightRadius: 0
|
||||
bottomRightRadius: topRightRadius
|
||||
implicitWidth: z_head.contentWidth + control.padding * 2
|
||||
implicitHeight: z_data.height + control.padding * 2
|
||||
|
||||
color: z_head_hh.hovered ? control.is_recieved ? control.dark_mode ? "#262626" : "#F0F0F0" : control.dark_mode ? "#313131" : "#6099AA" : "transparent"
|
||||
RibbonText {
|
||||
id: z_head
|
||||
anchors.centerIn: parent
|
||||
color: control.dark_mode ? "white" : control.is_recieved ? "black" : "white"
|
||||
text: typeof(data_list[0]) !== "undefined" ? data_list[0] : ""
|
||||
font.pixelSize: control.font_size
|
||||
horizontalAlignment: RibbonText.AlignHCenter
|
||||
verticalAlignment: RibbonText.AlignVCenter
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonToolTip{
|
||||
text: "ZigBee帧 头"
|
||||
visible: z_head_hh.hovered && control.show_tooltip
|
||||
}
|
||||
|
||||
HoverHandler{
|
||||
id: z_head_hh
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Item {
|
||||
height: z_data.height + control.padding * 2
|
||||
width: 1
|
||||
Rectangle{
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
width: 1
|
||||
height: z_data.height - control.padding * 2
|
||||
color: control.dark_mode ? "#525252" : "#D4D4D4"
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonRectangle{
|
||||
radius: 0
|
||||
color: z_length_hh.hovered ? control.is_recieved ? control.dark_mode ? "#262626" : "#F0F0F0" : control.dark_mode ? "#313131" : "#6099AA" : "transparent"
|
||||
implicitWidth: z_length.contentWidth + control.padding * 2
|
||||
implicitHeight: z_data.height + control.padding * 2
|
||||
|
||||
RibbonText {
|
||||
id: z_length
|
||||
anchors.centerIn: parent
|
||||
color: control.dark_mode ? "white" : control.is_recieved ? "black" : "white"
|
||||
text: typeof(data_list[0]) !== "undefined" ? data_list[1] : ""
|
||||
font.pixelSize: control.font_size
|
||||
horizontalAlignment: RibbonText.AlignHCenter
|
||||
verticalAlignment: RibbonText.AlignVCenter
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonToolTip{
|
||||
text: "ZigBee帧 长"
|
||||
visible: z_length_hh.hovered && control.show_tooltip
|
||||
}
|
||||
|
||||
HoverHandler{
|
||||
id: z_length_hh
|
||||
}
|
||||
}
|
||||
|
||||
Item {
|
||||
height: z_data.height + control.padding * 2
|
||||
width: 1
|
||||
Rectangle{
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
width: 1
|
||||
height: z_data.height - control.padding * 2
|
||||
color: control.dark_mode ? "#525252" : "#D4D4D4"
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonRectangle{
|
||||
radius: 0
|
||||
color: z_src_port_hh.hovered ? control.is_recieved ? control.dark_mode ? "#262626" : "#F0F0F0" : control.dark_mode ? "#313131" : "#6099AA" : "transparent"
|
||||
implicitWidth: z_src_port.contentWidth + control.padding * 2
|
||||
implicitHeight: z_data.height + control.padding * 2
|
||||
|
||||
|
||||
RibbonText {
|
||||
id: z_src_port
|
||||
anchors.centerIn: parent
|
||||
color: control.dark_mode ? "white" : control.is_recieved ? "black" : "white"
|
||||
text: typeof(data_list[0]) !== "undefined" ? data_list[2] : ""
|
||||
font.pixelSize: control.font_size
|
||||
horizontalAlignment: RibbonText.AlignHCenter
|
||||
verticalAlignment: RibbonText.AlignVCenter
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonToolTip{
|
||||
text: "ZigBee帧 源端口"
|
||||
visible: z_src_port_hh.hovered && control.show_tooltip
|
||||
}
|
||||
|
||||
HoverHandler{
|
||||
id: z_src_port_hh
|
||||
}
|
||||
}
|
||||
|
||||
Item {
|
||||
height: z_data.height + control.padding * 2
|
||||
width: 1
|
||||
Rectangle{
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
width: 1
|
||||
height: z_data.height - control.padding * 2
|
||||
color: control.dark_mode ? "#525252" : "#D4D4D4"
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonRectangle{
|
||||
radius: 0
|
||||
color: z_des_port_hh.hovered ? control.is_recieved ? control.dark_mode ? "#262626" : "#F0F0F0" : control.dark_mode ? "#313131" : "#6099AA" : "transparent"
|
||||
implicitWidth: z_des_port.contentWidth + control.padding * 2
|
||||
implicitHeight: z_data.height + control.padding * 2
|
||||
|
||||
|
||||
RibbonText {
|
||||
id: z_des_port
|
||||
anchors.centerIn: parent
|
||||
color: control.dark_mode ? "white" : control.is_recieved ? "black" : "white"
|
||||
text: typeof(data_list[0]) !== "undefined" ? data_list[3] : ""
|
||||
font.pixelSize: control.font_size
|
||||
horizontalAlignment: RibbonText.AlignHCenter
|
||||
verticalAlignment: RibbonText.AlignVCenter
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonToolTip{
|
||||
text: "ZigBee帧 目的端口"
|
||||
visible: z_des_port_hh.hovered && control.show_tooltip
|
||||
}
|
||||
|
||||
HoverHandler{
|
||||
id: z_des_port_hh
|
||||
}
|
||||
}
|
||||
|
||||
Item {
|
||||
height: z_data.height + control.padding * 2
|
||||
width: 1
|
||||
Rectangle{
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
width: 1
|
||||
height: z_data.height - control.padding * 2
|
||||
color: control.dark_mode ? "#525252" : "#D4D4D4"
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonRectangle{
|
||||
radius: 0
|
||||
color: z_address_hh.hovered ? control.is_recieved ? control.dark_mode ? "#262626" : "#F0F0F0" : control.dark_mode ? "#313131" : "#6099AA" : "transparent"
|
||||
implicitWidth: z_address.contentWidth + control.padding * 2
|
||||
implicitHeight: z_data.height + control.padding * 2
|
||||
|
||||
RibbonText {
|
||||
id: z_address
|
||||
anchors.centerIn: parent
|
||||
color: control.dark_mode ? "white" : control.is_recieved ? "black" : "white"
|
||||
text: typeof(data_list[0]) !== "undefined" ? data_list.slice(4,6).join(" ") : ""
|
||||
font.pixelSize: control.font_size
|
||||
horizontalAlignment: RibbonText.AlignHCenter
|
||||
verticalAlignment: RibbonText.AlignVCenter
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonToolTip{
|
||||
text: "ZigBee帧 源地址"
|
||||
visible: z_address_hh.hovered && control.show_tooltip
|
||||
}
|
||||
|
||||
HoverHandler{
|
||||
id: z_address_hh
|
||||
}
|
||||
}
|
||||
|
||||
Item {
|
||||
height: z_data.height + control.padding * 2
|
||||
width: 1
|
||||
Rectangle{
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
width: 1
|
||||
height: z_data.height - control.padding * 2
|
||||
color: control.dark_mode ? "#525252" : "#D4D4D4"
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonRectangle{
|
||||
radius: 0
|
||||
color: z_data_hh.hovered ? control.is_recieved ? control.dark_mode ? "#262626" : "#F0F0F0" : control.dark_mode ? "#313131" : "#6099AA" : "transparent"
|
||||
implicitWidth: z_data.width + control.padding * 2
|
||||
implicitHeight: z_data.height + control.padding * 2
|
||||
|
||||
RibbonText {
|
||||
id: z_data
|
||||
anchors.centerIn: parent
|
||||
color: control.dark_mode ? "white" : control.is_recieved ? "black" : "white"
|
||||
width: implicitWidth < control.data_width ? implicitWidth : control.data_width
|
||||
wrapMode: RibbonText.Wrap
|
||||
text: typeof(data_list[0]) !== "undefined" ? data_list.slice(6,data_list.length-1).join(" ") : ""
|
||||
font.pixelSize: control.font_size
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonToolTip{
|
||||
text: "ZigBee帧 数据"
|
||||
visible: z_data_hh.hovered && control.show_tooltip
|
||||
}
|
||||
|
||||
HoverHandler{
|
||||
id: z_data_hh
|
||||
}
|
||||
}
|
||||
|
||||
Item {
|
||||
height: z_data.height + control.padding * 2
|
||||
width: 1
|
||||
Rectangle{
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
width: 1
|
||||
height: z_data.height - control.padding * 2
|
||||
color: control.dark_mode ? "#525252" : "#D4D4D4"
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonRectangle{
|
||||
radius: parent.radius ? parent.radius / 2 : 5
|
||||
color: z_tail_hh.hovered ? control.is_recieved ? control.dark_mode ? "#262626" : "#F0F0F0" : control.dark_mode ? "#313131" : "#6099AA" : "transparent"
|
||||
topLeftRadius: 0
|
||||
bottomLeftRadius: topLeftRadius
|
||||
implicitWidth: z_tail.contentWidth + control.padding * 2
|
||||
implicitHeight: z_data.height + control.padding * 2
|
||||
|
||||
RibbonText {
|
||||
id: z_tail
|
||||
anchors.centerIn: parent
|
||||
color: control.dark_mode ? "white" : control.is_recieved ? "black" : "white"
|
||||
text: typeof(data_list[0]) !== "undefined" ? data_list[data_list.length-1] : ""
|
||||
font.pixelSize: control.font_size
|
||||
horizontalAlignment: RibbonText.AlignHCenter
|
||||
verticalAlignment: RibbonText.AlignVCenter
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonToolTip{
|
||||
text: "ZigBee帧 尾"
|
||||
visible: z_tail_hh.hovered && control.show_tooltip
|
||||
}
|
||||
|
||||
HoverHandler{
|
||||
id: z_tail_hh
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,74 +0,0 @@
|
|||
import QtQuick
|
||||
import QtQuick.Controls
|
||||
import QtQuick.Layouts
|
||||
import RibbonUI
|
||||
|
||||
RibbonMessage{
|
||||
id: message_style
|
||||
sender_text: data_model.recieved ? `${Qt.formatDateTime(new Date(), "yyyy-MM-dd hh:mm:ss ")} Node 0x${data_model.sender}` : `${Qt.formatDateTime(new Date(), "yyyy-MM-dd hh:mm:ss ")} Computer 0x${data_model.sender}`
|
||||
property var text_list: data_model.text ? data_model.text.split(' ') : []
|
||||
property var decrypted_text_list: data_model.decrypted_text ? data_model.decrypted_text.split(' ') : []
|
||||
property bool show_tooltip: true
|
||||
property double component_width: 300
|
||||
ZigBeeFrame{
|
||||
id: message_raw_layout
|
||||
text: "原始数据:"
|
||||
show_text: model.recieved
|
||||
show_tooltip: message_style.show_tooltip
|
||||
data_list: message_style.text_list
|
||||
is_recieved: model.recieved
|
||||
component_width: message_style.component_width
|
||||
font_size: message_style.font_size
|
||||
visible: model.text ? true : false
|
||||
}
|
||||
|
||||
Rectangle{
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
implicitWidth: parent.width - 20
|
||||
implicitHeight: 1
|
||||
color: RibbonTheme.dark_mode ? "#525252" : "#D4D4D4"
|
||||
visible: message_decrypted_layout.visible
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ZigBeeFrame{
|
||||
id: message_decrypted_layout
|
||||
text: "解密数据:"
|
||||
show_tooltip: message_style.show_tooltip
|
||||
data_list: message_style.decrypted_text_list
|
||||
visible: model.decrypted_text ? true : false
|
||||
is_recieved: model.recieved
|
||||
component_width: message_style.component_width
|
||||
font_size: message_style.font_size
|
||||
}
|
||||
|
||||
Rectangle{
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
implicitWidth: parent.width - 20
|
||||
implicitHeight: 1
|
||||
color: RibbonTheme.dark_mode ? "#525252" : "#D4D4D4"
|
||||
visible: note.visible && message_raw_layout.visible
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 60
|
||||
easing.type: Easing.OutSine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RibbonText {
|
||||
id: note
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
font.pixelSize: message_style.font_size
|
||||
color: RibbonTheme.dark_mode ? "white" : !model.recieved ? "white" : "black"
|
||||
text: model.note_text ? model.note_text : ""
|
||||
visible: model.note_text ? true : false
|
||||
Layout.preferredWidth: implicitWidth < message_style.component_width ? implicitWidth : message_style.component_width
|
||||
wrapMode: RibbonText.Wrap
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
#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.
Binary file not shown.
Before Width: | Height: | Size: 32 KiB |
Binary file not shown.
Before Width: | Height: | Size: 39 KiB |
Binary file not shown.
Before Width: | Height: | Size: 76 KiB |
Binary file not shown.
Before Width: | Height: | Size: 126 KiB |
|
@ -1,26 +0,0 @@
|
|||
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)
|
|
@ -1,97 +0,0 @@
|
|||
#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
|
|
@ -1,19 +0,0 @@
|
|||
#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
|
|
@ -1,74 +0,0 @@
|
|||
#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
|
|
@ -1,36 +0,0 @@
|
|||
#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
|
|
@ -1,24 +0,0 @@
|
|||
|
||||
#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
|
|
@ -1,43 +0,0 @@
|
|||
#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_ */
|
|
@ -1,56 +0,0 @@
|
|||
#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_ */
|
|
@ -1,25 +0,0 @@
|
|||
#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_ */
|
|
@ -1,174 +0,0 @@
|
|||
#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
|
|
@ -1,989 +0,0 @@
|
|||
#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
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
#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;
|
||||
}
|
||||
}
|
|
@ -1,323 +0,0 @@
|
|||
#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]);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,220 +0,0 @@
|
|||
#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);
|
||||
}
|
||||
|
|
@ -1,89 +0,0 @@
|
|||
|
||||
#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;
|
||||
}
|
|
@ -1,664 +0,0 @@
|
|||
#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;
|
||||
}
|
|
@ -1,403 +0,0 @@
|
|||
#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 );
|
||||
}
|
|
@ -1,304 +0,0 @@
|
|||
#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;
|
||||
}
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
#include "config.h"
|
||||
#include <QtCore/QtCore>
|
||||
#include <QDebug>
|
||||
|
||||
Config::Config(QString qstrfilename)
|
||||
: QObject(nullptr)
|
||||
{
|
||||
if (qstrfilename.isEmpty())
|
||||
{
|
||||
m_qstrFileName = QCoreApplication::applicationDirPath() + "/config.ini";
|
||||
}
|
||||
else
|
||||
{
|
||||
m_qstrFileName = qstrfilename;
|
||||
}
|
||||
|
||||
m_psetting = new QSettings(m_qstrFileName, QSettings::IniFormat);
|
||||
}
|
||||
|
||||
Config::~Config()
|
||||
{
|
||||
delete m_psetting;
|
||||
m_psetting = nullptr;
|
||||
}
|
||||
|
||||
Config* Config::instance()
|
||||
{
|
||||
static QMutex mutex;
|
||||
QMutexLocker locker(&mutex);
|
||||
|
||||
static Config *singleton = nullptr;
|
||||
if (!singleton) {
|
||||
singleton = new Config();
|
||||
}
|
||||
return singleton;
|
||||
}
|
||||
|
||||
void Config::Set(QString qstrnodename,QString qstrkeyname,QVariant qvarvalue)
|
||||
{
|
||||
m_psetting->setValue(QString("/%1/%2").arg(qstrnodename).arg(qstrkeyname), qvarvalue);
|
||||
}
|
||||
|
||||
void Config::SetArray(QString qstrnodename,QString qstrkeyname,QVariant qvarvalue)
|
||||
{
|
||||
m_psetting->beginWriteArray(QString("/%1/%2").arg(qstrnodename).arg(qstrkeyname));
|
||||
QList<QVariant> list = qvarvalue.toList();
|
||||
for (int i = 0; i< list.length(); i++)
|
||||
{
|
||||
m_psetting->setArrayIndex(i);
|
||||
m_psetting->setValue(qstrkeyname, list.at(i));
|
||||
}
|
||||
m_psetting->endArray();
|
||||
}
|
||||
|
||||
QVariant Config::Get(QString qstrnodename,QString qstrkeyname)
|
||||
{
|
||||
QVariant qvar = m_psetting->value(QString("/%1/%2").arg(qstrnodename).arg(qstrkeyname));
|
||||
return qvar;
|
||||
}
|
||||
|
||||
QVariant Config::GetArray(QString qstrnodename,QString qstrkeyname)
|
||||
{
|
||||
QList<QVariant> list;
|
||||
int size = m_psetting->beginReadArray(QString("/%1/%2").arg(qstrnodename).arg(qstrkeyname));
|
||||
for (int i = 0; i< size; i++)
|
||||
{
|
||||
m_psetting->setArrayIndex(i);
|
||||
list.append(m_psetting->value(qstrkeyname));
|
||||
}
|
||||
m_psetting->endArray();
|
||||
return list;
|
||||
}
|
||||
|
||||
void Config::Clear()
|
||||
{
|
||||
m_psetting->clear();
|
||||
}
|
|
@ -1,259 +0,0 @@
|
|||
#include "crypto.h"
|
||||
#include <math.h>
|
||||
|
||||
Crypto::Crypto()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Crypto* Crypto::getInstance()
|
||||
{
|
||||
static Crypto crypto_instance;
|
||||
return &crypto_instance;
|
||||
}
|
||||
|
||||
int Crypto::hex2bytearray(char s[], unsigned char bits[], u32 len)
|
||||
{
|
||||
u32 i, n = 0;
|
||||
for (i = 0; i < len; i += 2) {
|
||||
if (s[i] >= 'a' && s[i] <= 'f') bits[n] = s[i] - 'a' + 10;
|
||||
else if (s[i] >= 'A' && s[i] <= 'F') bits[n] = s[i] - 'A' + 10;
|
||||
else bits[n] = s[i] - '0';
|
||||
if (s[i + 1] >= 'a' && s[i + 1] <= 'f') bits[n] = (bits[n] << 4) | (s[i + 1] - 'a' + 10);
|
||||
else if (s[i + 1] >= 'A' && s[i + 1] <= 'F') bits[n] = (bits[n] << 4) | (s[i + 1] - 'A' + 10);
|
||||
else bits[n] = (bits[n] << 4) | (s[i + 1] - '0');
|
||||
++n;
|
||||
}
|
||||
bits[n] = 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
int Crypto::hex2bytearray(char s[], unsigned char *bits)
|
||||
{
|
||||
int i=0, n = 0;
|
||||
if (s[i] >= 'a' && s[i] <= 'f') *bits = s[i] - 'a' + 10;
|
||||
else if (s[i] >= 'A' && s[i] <= 'F') *bits = s[i] - 'A' + 10;
|
||||
else *bits = s[i] - '0';
|
||||
if (s[i + 1] >= 'a' && s[i + 1] <= 'f') *bits = (*bits << 4) | (s[i + 1] - 'a' + 10);
|
||||
else if (s[i + 1] >= 'A' && s[i + 1] <= 'F') *bits = (*bits << 4) | (s[i + 1] - 'A' + 10);
|
||||
else *bits = (*bits << 4) | (s[i + 1] - '0');
|
||||
++n;
|
||||
return n;
|
||||
}
|
||||
|
||||
void Crypto::bytearray2hex(u8 in_s[], char out_b[], u32 len)
|
||||
{
|
||||
u32 i;
|
||||
for (i = 0; i < len; i++) {
|
||||
snprintf(out_b + i * 2, 2, "%02X", in_s[i]);
|
||||
}
|
||||
out_b[len * 2] = 0;
|
||||
}
|
||||
|
||||
void Crypto::bytearray2hex(ecc_point in_s, char out_b[])
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 8; j++) {
|
||||
snprintf(out_b + i * 8 * 2 + j * 2, 2, "%02X", (u8)(in_s.x[i] >> (j * 8)));
|
||||
}
|
||||
}
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 8; j++) {
|
||||
snprintf(out_b + i * 8 * 2 + j * 2 + 64, 2, "%02X", (u8)(in_s.y[i] >> (j * 8)));
|
||||
}
|
||||
}
|
||||
out_b[128] = 0;
|
||||
}
|
||||
|
||||
void Crypto::pubhex2point(char *in_b, ecc_point *out_s)
|
||||
{
|
||||
u64 temp = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
out_s->x[i] = 0;
|
||||
for (int j = 0; j < 8; j++) {
|
||||
sscanf(in_b + i * 8 * 2 + j * 2, "%2llX", &temp);
|
||||
out_s->x[i] += temp << (j * 8);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 4; i++) {
|
||||
out_s->y[i] = 0;
|
||||
for (int j = 0; j < 8; j++) {
|
||||
sscanf(in_b + i * 8 * 2 + j * 2 + 64, "%2llX", &temp);
|
||||
out_s->y[i] += temp << (j * 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Crypto::sm2_key_gen(SM2_key *sm2_keypair,bool buffed)
|
||||
{
|
||||
SM2_key temp_keypair;
|
||||
ecc_point temp_pub_p;
|
||||
u8 temp_pub[129]={0},temp_pri[65]={0};
|
||||
sm2_make_keypair(temp_pri,&temp_pub_p);
|
||||
bytearray2hex(temp_pub_p, (char *)temp_pub);
|
||||
hex2bytearray((char *)temp_pub,temp_keypair.pub_key,128);
|
||||
strncpy((char *)temp_keypair.pri_key,(char *)temp_pri, 32);
|
||||
strncpy((char *)sm2_keypair->pri_key,(char *)temp_keypair.pri_key, 32);
|
||||
strncpy((char *)sm2_keypair->pub_key,(char *)temp_keypair.pub_key, 64);
|
||||
if (buffed)
|
||||
{
|
||||
u8 pri_buf[65];
|
||||
bytearray2hex(sm2_keypair->pri_key, (char *)pri_buf);
|
||||
strncpy((char *)sm2_keypair->pri_key,(char *)pri_buf, 64);
|
||||
strncpy((char *)sm2_keypair->pub_key,(char *)temp_pub, 128);
|
||||
}
|
||||
}
|
||||
|
||||
void Crypto::SM2_encrypt(u8 *pub, u8 *M, u32 Mlen, u8 *C, u32 *Clen, s32 mode)
|
||||
{
|
||||
ecc_point pub_point;
|
||||
memcpy(pub_point.x, pub, 32);
|
||||
memcpy(pub_point.y, pub+32, 32);
|
||||
sm2_encrypt(&pub_point, (u8 *)M, Mlen, (u8 *)C, Clen);
|
||||
if (!mode) {
|
||||
u8 *temp_C = (u8 *)malloc(sizeof(u8) * (*Clen));
|
||||
memset(temp_C, 0, sizeof(u8) * (*Clen));
|
||||
sm2_C1C2C3ConvertToC1C3C2(temp_C, (u8 *)C, *Clen, mode);
|
||||
memset(C, 0, sizeof(u8) * (*Clen));
|
||||
memcpy(C, temp_C, sizeof(u8) * (*Clen));
|
||||
free(temp_C);
|
||||
}
|
||||
}
|
||||
|
||||
// Default Input C1|C3|C2
|
||||
void Crypto::SM2_decrypt(u8 *pri, u8 *C, u32 Clen, u8 *M, u32 *Mlen, s32 mode)
|
||||
{
|
||||
u8 *temp_C = (u8 *)malloc(sizeof(u8) * Clen);
|
||||
if (!mode) {
|
||||
sm2_C1C2C3ConvertToC1C3C2(temp_C, C, Clen, !mode);
|
||||
}
|
||||
sm2_decrypt(pri, temp_C, Clen, (u8 *)M, Mlen);
|
||||
free(temp_C);
|
||||
}
|
||||
|
||||
// Output r | s
|
||||
void Crypto::SM2_sign(u8 *pri, u8 *data, u32 datalen, u8 *out)
|
||||
{
|
||||
struct sm3_ctx h;
|
||||
u8 hash[33] = { 0 };
|
||||
u8 r[33] = { 0 }, s[33] = { 0 };
|
||||
sm3_finup(&h, data, datalen, hash);
|
||||
sm2_sign(r, s, pri, hash);
|
||||
memcpy(out, r, 32);
|
||||
memcpy(out+32, s, 32);
|
||||
}
|
||||
|
||||
bool Crypto::SM2_verify(u8 *pub, u8 *data,u32 data_len, u8 *sig)
|
||||
{
|
||||
struct sm3_ctx h;
|
||||
u8 hash[33] = { 0 };
|
||||
u8 r[33] = { 0 }, s[33] = { 0 };
|
||||
ecc_point pub_point;
|
||||
memcpy(pub_point.x, pub, 32);
|
||||
memcpy(pub_point.y, pub+32, 32);
|
||||
sm3_finup(&h, data, data_len, hash);
|
||||
memcpy(r, sig, 32);
|
||||
memcpy(s, sig+32, 32);
|
||||
return (sm2_verify(&pub_point, hash, r, s) == 0 ? true : false);
|
||||
}
|
||||
|
||||
void Crypto::SM3_HMAC(u8 *key, int keylen, u8 *input, int ilen, u8 *output)
|
||||
{
|
||||
sm3_hmac(key, keylen, input, ilen, output);
|
||||
}
|
||||
|
||||
bool Crypto::SM4_encrypt(u8 *key_origin, u32 key_len, u8 *in_origin, u32 in_len, u8 *out, u32 *out_len)
|
||||
{
|
||||
struct sm4_ctx a;
|
||||
u32 max_len;
|
||||
u8 key[17] = { 0 }, *in = (u8 *)malloc(sizeof(u8) * (in_len + 16 * 2 + 1)); // 为保证C兼容性,不使用智能指针 两个16分别是为首末尾填充留足留足空间,1是冗余量
|
||||
u8 iv[17] = {
|
||||
0x21, 0xBC, 0xC1, 0xEA, 0x0D, 0xB8, 0x54, 0x6D, 0xCE, 0xE4, 0xDB, 0x3C, 0xFA, 0xC1, 0x3C, 0xEF }, fix_len; // 此处为省时才用固定IV,实际上此处IV必须是随机的才可真正保证SM4 CBC模式下加解密的强度
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
iv[i] = Crypto::getInstance()->get_rand(); // 使用随机数IV,如果想节省时间可以注释
|
||||
}
|
||||
if (key_len > 16) {
|
||||
free(in);
|
||||
return false;
|
||||
}
|
||||
fix_len = 16 - key_len;
|
||||
memcpy(key, key_origin, key_len);
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
in[i] = Crypto::getInstance()->get_rand(); // 首填充
|
||||
}
|
||||
memcpy(in + 16, in_origin, in_len);
|
||||
if (key_len < 16) {
|
||||
for (int i = key_len; i < 16; i++) {
|
||||
key[i] = fix_len;
|
||||
}
|
||||
}
|
||||
if (in_len % 16 != 0) {
|
||||
max_len = ceil(in_len / 16.0)* 16 + 16;
|
||||
fix_len = max_len - 16 - in_len;
|
||||
for (u32 i = in_len + 16; i < max_len; i++) {
|
||||
in[i] = fix_len;
|
||||
}
|
||||
in_len = max_len;
|
||||
} else {
|
||||
for (u32 i = in_len + 16; i < in_len + 16 * 2; i++) {
|
||||
in[i] = 16;
|
||||
}
|
||||
in_len += 16 * 2;
|
||||
}
|
||||
*out_len = in_len;
|
||||
sm4_cbc_encrypt(&a, key, iv, in, in_len, out);
|
||||
free(in);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Crypto::SM4_decrypt(u8 *key_origin, u32 key_len, u8 *in, u32 in_len, u8 *out, u32 *out_len)
|
||||
{
|
||||
struct sm4_ctx b;
|
||||
u32 fix_len;
|
||||
u8 key[17] = { 0 };
|
||||
u8 iv[17] = {
|
||||
0x21, 0xBC, 0xC1, 0xEA, 0x0D, 0xB8, 0x54, 0x6D, 0xCE, 0xE4, 0xDB, 0x3C, 0xFA, 0xC1, 0x3C, 0xEF }; // 此处为省时才用固定IV,实际上此处IV必须是随机的才可真正保证SM4 CBC模式下加解密的强度
|
||||
u8* out_buf = (u8* )malloc(in_len * sizeof(u8)); // 为保证C兼容性,不使用智能指针
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
iv[i] = Crypto::getInstance()->get_rand(); // 使用随机数IV,如果想节省时间可以注释
|
||||
}
|
||||
if (key_len > 16) {
|
||||
free(out_buf);
|
||||
return false;
|
||||
}
|
||||
fix_len = 16 - key_len;
|
||||
memcpy(key, key_origin, key_len);
|
||||
if (in_len % 16 != 0) {
|
||||
free(out_buf);
|
||||
return false;
|
||||
}
|
||||
if (key_len < 16) {
|
||||
for (int i = key_len; i < 16; i++) {
|
||||
key[i] = fix_len;
|
||||
}
|
||||
}
|
||||
sm4_cbc_decrypt(&b, key, iv, in, in_len, out_buf);
|
||||
fix_len = out_buf[in_len - 1];
|
||||
if (fix_len>in_len)
|
||||
{
|
||||
free(out_buf);
|
||||
return false;
|
||||
}
|
||||
*out_len = in_len - fix_len - 16;
|
||||
memcpy(out, out_buf + 16, *out_len);
|
||||
free(out_buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t Crypto::get_rand()
|
||||
{
|
||||
uint8_t num = 0;
|
||||
static time_t seed = 0;
|
||||
srand(time(nullptr) + seed);
|
||||
seed = rand();
|
||||
return rand() % 256;
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
#include "dlln3x_handler.h"
|
||||
|
||||
DLLN3X_Handler::DLLN3X_Handler(QObject *parent)
|
||||
: QObject{parent}
|
||||
{
|
||||
_zigbee = zigbee_protocol::DLLN3X::instance();
|
||||
handle_serial(false);
|
||||
serial(nullptr);
|
||||
connect(this, &DLLN3X_Handler::handle_serialChanged, this, [=](){
|
||||
if (_handle_serial&&_serial!=nullptr)
|
||||
{
|
||||
_zigbee->init(_serial, _serial->baudRate());
|
||||
connect(_zigbee, &zigbee_protocol::DLLN3X::recved, this, &DLLN3X_Handler::zigbee_callback);
|
||||
connect(_serial, &QSerialPort::readyRead, _zigbee, &zigbee_protocol::DLLN3X::loop);
|
||||
connect(_serial, &QSerialPort::aboutToClose, this, &DLLN3X_Handler::close);
|
||||
}
|
||||
else
|
||||
{
|
||||
close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void DLLN3X_Handler::zigbee_callback(zigbee_protocol::ZigbeeFrame zframe)
|
||||
{
|
||||
QByteArray zf = QByteArray((char *)zframe.data(),zframe.size());
|
||||
QByteArray hex = zf.toHex(' ');
|
||||
QByteArray data = hex.toUpper();
|
||||
QByteArray num = QByteArray::number(zframe.getRemoteAddr(),16);
|
||||
QByteArray sender = num.toUpper();
|
||||
emit zigbee_recved(data, sender);
|
||||
}
|
||||
|
||||
void DLLN3X_Handler::close()
|
||||
{
|
||||
disconnect(_zigbee, &zigbee_protocol::DLLN3X::recved, this, &DLLN3X_Handler::zigbee_callback);
|
||||
disconnect(_serial, &QSerialPort::readyRead, _zigbee, &zigbee_protocol::DLLN3X::loop);
|
||||
disconnect(_serial, &QSerialPort::aboutToClose, this, &DLLN3X_Handler::close);
|
||||
}
|
||||
|
||||
bool DLLN3X_Handler::send(QString data)
|
||||
{
|
||||
if (_handle_serial)
|
||||
{
|
||||
QByteArray bdata;
|
||||
QStringList td = data.split(' ');
|
||||
for (auto item : td)
|
||||
bdata += QByteArray::fromHex(item.toLatin1());
|
||||
|
||||
zigbee_protocol::ZigbeeFrame zf(bdata.data(),bdata.length());
|
||||
|
||||
return _zigbee->send(zf);
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
#include "eventsbus.h"
|
||||
#include <QMutex>
|
||||
|
||||
EventsBus::EventsBus(QObject *parent)
|
||||
: QObject{parent}
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
EventsBus* EventsBus::instance()
|
||||
{
|
||||
static QMutex mutex;
|
||||
QMutexLocker locker(&mutex);
|
||||
|
||||
static EventsBus *singleton = nullptr;
|
||||
if (!singleton) {
|
||||
singleton = new EventsBus();
|
||||
}
|
||||
return singleton;
|
||||
}
|
||||
|
||||
void EventsBus::reg_event(Event* event)
|
||||
{
|
||||
_events.append(event);
|
||||
}
|
||||
|
||||
void EventsBus::unreg_event(Event* event)
|
||||
{
|
||||
_events.removeOne(event);
|
||||
}
|
||||
|
||||
void EventsBus::push_data(QString type, QJsonObject data)
|
||||
{
|
||||
|
||||
if (_event_history.count() >= 100)
|
||||
_event_history.pop_front();
|
||||
if (data["time"].toString()=="")
|
||||
data.insert("time",QJsonValue(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")));
|
||||
_event_history.append(data);
|
||||
for (auto event : _events) {
|
||||
if (event->type() == type)
|
||||
emit event->triggered(data);
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QtQml/qqmlextensionplugin.h>
|
||||
#include <FramelessHelper/Quick/framelessquickmodule.h>
|
||||
#include <FramelessHelper/Core/private/framelessconfig_p.h>
|
||||
#include <QTranslator>
|
||||
|
||||
FRAMELESSHELPER_USE_NAMESPACE
|
||||
#ifdef RIBBONUI_BUILD_STATIC_LIB
|
||||
Q_IMPORT_QML_PLUGIN(RibbonUIPlugin)
|
||||
#endif
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
qputenv("QT_QUICK_CONTROLS_STYLE","Basic");
|
||||
FramelessHelper::Quick::initialize();
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
FramelessConfig::instance()->set(Global::Option::ForceHideWindowFrameBorder);
|
||||
FramelessConfig::instance()->set(Global::Option::ForceNonNativeBackgroundBlur);
|
||||
#endif
|
||||
FramelessConfig::instance()->set(Global::Option::CenterWindowBeforeShow);
|
||||
FramelessConfig::instance()->set(Global::Option::EnableBlurBehindWindow);
|
||||
|
||||
QTranslator translator;
|
||||
bool result = translator.load(QLocale::system(), u""_qs, u""_qs, u":/translations"_qs);
|
||||
if (result)
|
||||
app.installTranslator(&translator);
|
||||
QQmlApplicationEngine engine;
|
||||
FramelessHelper::Quick::registerTypes(&engine);
|
||||
#ifdef RIBBONUI_BUILD_STATIC_LIB
|
||||
engine.addImportPath("qrc:/");
|
||||
#endif
|
||||
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)
|
||||
QCoreApplication::exit(-1);
|
||||
}, Qt::QueuedConnection);
|
||||
engine.load(url);
|
||||
|
||||
return app.exec();
|
||||
}
|
|
@ -1,227 +0,0 @@
|
|||
#include "protocol.h"
|
||||
|
||||
|
||||
using namespace zigbee_protocol;
|
||||
|
||||
Protocol::Protocol(){
|
||||
_config = Config::instance();
|
||||
if (_config->Get("Protocol","hmac_verify_key").toString().isEmpty())
|
||||
_config->Set("Protocol","hmac_verify_key",hmac_verify_key);
|
||||
else
|
||||
hmac_verify_key = _config->Get("Protocol","hmac_verify_key").toString();
|
||||
if (_config->GetArray("Protocol","pre_hmac_verify_key").toStringList().isEmpty())
|
||||
_config->SetArray("Protocol","pre_hmac_verify_key",pre_hmac_verify_key);
|
||||
else
|
||||
pre_hmac_verify_key = _config->GetArray("Protocol","pre_hmac_verify_key").toStringList();
|
||||
}
|
||||
|
||||
Protocol::~Protocol()
|
||||
{
|
||||
_config->Set("Protocol","hmac_verify_key",hmac_verify_key);
|
||||
_config->SetArray("Protocol","pre_hmac_verify_key",pre_hmac_verify_key);
|
||||
}
|
||||
|
||||
Protocol* Protocol::getInstance()
|
||||
{
|
||||
static Protocol protocol_instance;
|
||||
return &protocol_instance;
|
||||
}
|
||||
|
||||
void Protocol::protocal_wrapper(data_frame *frame, u8 type, u16 length, u8 *data, bool use_crc)
|
||||
{
|
||||
frame->head=DATA_FRAME_HEAD;
|
||||
frame->type=type;
|
||||
frame->data_length = length;
|
||||
if (use_crc)
|
||||
{
|
||||
frame->use_crc=0xDD;
|
||||
u16 crc = crc16_xmodem(data, length);
|
||||
frame->crc = crc;
|
||||
}
|
||||
else
|
||||
{
|
||||
frame->use_crc=0xFF;
|
||||
frame->crc = 0xFFFF;
|
||||
}
|
||||
memcpy(frame->data,data,length);
|
||||
}
|
||||
|
||||
void Protocol::base_frame_maker(void *in_frame, base_frame *out_frame, u16 dest_addr,device *dev,u16 node_addr)
|
||||
{
|
||||
out_frame->head = BASE_FRAME_HEAD;
|
||||
out_frame->ori_addr= dev->addr;
|
||||
out_frame->des_addr = dest_addr;
|
||||
out_frame->node_addr = node_addr;
|
||||
if (dev->id < BASE_FRAME_RESET_NUM)
|
||||
{
|
||||
if (dev->id == 0)
|
||||
{
|
||||
out_frame->reset_num = 0xFF;
|
||||
do{
|
||||
srand(static_cast<u16>(time(nullptr)));
|
||||
out_frame->rand_num = rand() & 0xFF;
|
||||
}while(out_frame->rand_num==0);
|
||||
dev->rand_num = out_frame->rand_num;
|
||||
out_frame->reset_num = 0xDD;
|
||||
}
|
||||
else
|
||||
{
|
||||
out_frame->rand_num = dev->rand_num;
|
||||
out_frame->reset_num = 0xFF;
|
||||
}
|
||||
out_frame->id = ++(dev->id);
|
||||
}
|
||||
else
|
||||
{
|
||||
out_frame->id = dev->id = 0;
|
||||
out_frame->reset_num = 0xDD;
|
||||
do{
|
||||
srand(static_cast<u16>(time(nullptr)));
|
||||
out_frame->rand_num = rand() & 0xFF;
|
||||
}while(out_frame->rand_num==0);
|
||||
}
|
||||
switch (*(u16 *)in_frame)
|
||||
{
|
||||
case 0xAAAB:
|
||||
{
|
||||
out_frame->length = ((digi_env *)in_frame)->length + BASE_FRAME_PREFIX_LEN;
|
||||
break;
|
||||
}
|
||||
case 0xAAAC:
|
||||
{
|
||||
out_frame->length = ((ssl_frame *)in_frame)->length + BASE_FRAME_PREFIX_LEN;
|
||||
break;
|
||||
}
|
||||
case 0xAAAA:
|
||||
{
|
||||
out_frame->length = ((data_frame *)in_frame)->data_length + DATA_FRAME_PREFIX_LEN + BASE_FRAME_PREFIX_LEN;
|
||||
break;
|
||||
}
|
||||
case 0xAAAE:
|
||||
{
|
||||
out_frame->length = ((hmac_frame *)in_frame)->length + BASE_FRAME_PREFIX_LEN;
|
||||
break;
|
||||
}
|
||||
case 0xAAAF:
|
||||
{
|
||||
out_frame->length = ((crypto_zdata_frame *)in_frame)->length + BASE_FRAME_PREFIX_LEN;
|
||||
break;
|
||||
}
|
||||
}
|
||||
memcpy(out_frame->data, in_frame ,out_frame->length - BASE_FRAME_PREFIX_LEN);
|
||||
}
|
||||
|
||||
bool Protocol::base_frame_parser(base_frame *in_frame, void **out_frame , device *dev)
|
||||
{
|
||||
if (in_frame->des_addr == SELF_ADDR && (dev->id == 0||in_frame->reset_num==0xDD||((in_frame->id > dev->id && in_frame->id<=BASE_FRAME_RESET_NUM))))
|
||||
{
|
||||
dev->addr = in_frame->ori_addr;
|
||||
dev->id = in_frame->id;
|
||||
dev->rand_num = in_frame->rand_num;
|
||||
*out_frame = (void *)in_frame->data;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t Protocol::crc16_xmodem(const uint8_t *buffer, uint32_t buffer_length)
|
||||
{
|
||||
uint8_t c, treat, bcrc;
|
||||
uint16_t wcrc = 0;
|
||||
|
||||
for(uint32_t i = 0; i < buffer_length; i++)
|
||||
{
|
||||
c = buffer[i];
|
||||
for(uint8_t j = 0; j < 8; j++)
|
||||
{
|
||||
treat = c & 0x80;
|
||||
c <<= 1;
|
||||
bcrc = (wcrc >> 8) & 0x80;
|
||||
wcrc <<= 1;
|
||||
if(treat != bcrc)
|
||||
wcrc ^= 0x1021;
|
||||
}
|
||||
}
|
||||
return wcrc;
|
||||
}
|
||||
|
||||
bool Protocol::bytecmp(u8 *a, u8 *b, u16 length)
|
||||
{
|
||||
return memcmp(a,b,length)==0;
|
||||
}
|
||||
|
||||
bool Protocol::data_frame_verify(data_frame *frame)
|
||||
{
|
||||
if(frame->use_crc!=0xFF)
|
||||
return !(frame->crc-crc16_xmodem(frame->data,frame->data_length));
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
void Protocol::zigbee_data_encrypt(data_frame *data, crypto_zdata_frame *zdata,
|
||||
bool (* SM4_encrypt)(u8 *key_origin, u32 key_len, u8 *in_origin, u32 in_len, u8 *out, u32 *out_len),
|
||||
QString en_key)
|
||||
{
|
||||
u32 len;
|
||||
zdata->head=CRYPTO_ZDATA_FRAME_HEAD;
|
||||
QByteArray key = QByteArray::fromHex(en_key == "" ? hmac_verify_key.toLatin1() : en_key.toLatin1());
|
||||
SM4_encrypt((u8 *)key.data(), 16, (u8 *)data, data->data_length + DATA_FRAME_PREFIX_LEN, zdata->data,&len);
|
||||
zdata->length = len + CRYPTO_ZDATA_FRAME_PREFIX_LEN;
|
||||
}
|
||||
|
||||
bool Protocol::zigbee_data_dectypt(data_frame *data, crypto_zdata_frame *zdata,
|
||||
bool (* SM4_decrypt)(u8 *key_origin, u32 key_len, u8 *in, u32 in_len, u8 *out, u32 *out_len),
|
||||
QString en_key)
|
||||
{
|
||||
int total_len = zdata->length;
|
||||
if (total_len - CRYPTO_ZDATA_FRAME_PREFIX_LEN<=0)
|
||||
return false;
|
||||
u32 len, msglen = total_len - CRYPTO_ZDATA_FRAME_PREFIX_LEN;
|
||||
QByteArray key = QByteArray::fromHex(en_key == "" ? hmac_verify_key.toLatin1() : en_key.toLatin1());
|
||||
SM4_decrypt((u8 *)key.data(), 16, zdata->data, msglen, (u8 *)data, &len);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Protocol::HMAC_identify(device *self, device *node, hmac_frame *hframe,
|
||||
void (*sendTonode)(ZigbeeFrame &data),
|
||||
void (*SM3_HMAC)(u8 *key, int keylen,u8 *input, int ilen,u8 output[32] ))
|
||||
{
|
||||
if (hframe==NULL)
|
||||
return;
|
||||
u8 hmac[33]="";
|
||||
ZigbeeFrame zf(0x81,0x81,node->addr);
|
||||
QByteArray key = QByteArray::fromHex(hmac_verify_key.toLatin1());
|
||||
SM3_HMAC((u8 *)key.data(),16,&hframe->value,1,hmac);
|
||||
if (bytecmp(hframe->hmac,hmac,32))
|
||||
{
|
||||
new_data_frame(15) data;
|
||||
memset(&data, 0, sizeof(data));
|
||||
protocal_wrapper((data_frame *)&data, 0, 10, (u8 *)"Identified", 0);
|
||||
node->verified = 1;
|
||||
new_base_frame(15 + BASE_FRAME_PREFIX_LEN) bframe;
|
||||
memset(&bframe, 0, sizeof(bframe));
|
||||
base_frame_maker(&data, (base_frame *)&bframe, node->addr, self);
|
||||
zf.setData((u8 *)&bframe, bframe.length);
|
||||
sendTonode(zf);
|
||||
printf("Node %X had passed HMAC identification.", node->addr);
|
||||
}
|
||||
}
|
||||
|
||||
void Protocol::HMAC_changeVerifykey(u8 key[16], device* self, device *node, void (*sendTonode)(ZigbeeFrame &data),
|
||||
bool (* SM4_encrypt)(u8 *key_origin, u32 key_len, u8 *in_origin, u32 in_len, u8 *out, u32 *out_len))
|
||||
{
|
||||
ZigbeeFrame zf(0x82,0x82,node->addr);
|
||||
new_data_frame(16) data;
|
||||
memset(&data, 0, sizeof(data));
|
||||
protocal_wrapper((data_frame *)&data, 0, 16, key, 0);
|
||||
new_crypto_zdata_frame(48) zdata;
|
||||
memset(&zdata, 0, sizeof(zdata));
|
||||
zigbee_data_encrypt((data_frame *)&data, (crypto_zdata_frame *)&zdata, SM4_encrypt);
|
||||
new_base_frame(48 + BASE_FRAME_PREFIX_LEN) bframe;
|
||||
memset(&bframe, 0, sizeof(bframe));
|
||||
base_frame_maker(&zdata, (base_frame *)&bframe, node->addr, self);
|
||||
zf.setData((u8 *)&bframe, bframe.length);
|
||||
sendTonode(zf);
|
||||
printf("Node %X's verify key has changed.", node->addr);
|
||||
}
|
|
@ -1,241 +0,0 @@
|
|||
#include "serialportmanager.h"
|
||||
#include <QSerialPortInfo>
|
||||
#include <QMutex>
|
||||
|
||||
SerialPortManager::SerialPortManager(QObject *parent)
|
||||
: QObject{parent}
|
||||
{
|
||||
_zigbee = zigbee_protocol::DLLN3X::instance();
|
||||
_bus = EventsBus::instance();
|
||||
ZigBeeParser::instance(); // 让ZigBeeParser在QML引擎中实例化
|
||||
_event.type("serial_port");
|
||||
_bus->reg_event(&_event);
|
||||
closed(true);
|
||||
opened(false);
|
||||
handledBymanager(true);
|
||||
recv_hex(false);
|
||||
send_hex(false);
|
||||
send_type(SendType::Blank);
|
||||
|
||||
_serial_port_scanner.setInterval(1000);
|
||||
_serial_port_scanner.start();
|
||||
connect(&_serial_port_scanner, &QTimer::timeout, this, [=](){
|
||||
available_ports();
|
||||
});
|
||||
connect(this, &SerialPortManager::handledBymanagerChanged, this, [=](){
|
||||
if (!_handledBymanager && _serial_port.isOpen())
|
||||
{
|
||||
_zigbee->init(&_serial_port, _serial_port.baudRate());
|
||||
connect(_zigbee, &zigbee_protocol::DLLN3X::recved, this, &SerialPortManager::zigbee_callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
disconnect(_zigbee, &zigbee_protocol::DLLN3X::recved, this, &SerialPortManager::zigbee_callback);
|
||||
}
|
||||
});
|
||||
connect(&_event, &Event::triggered, this, [=](QJsonObject data){
|
||||
write(data["text"].toString());
|
||||
});
|
||||
}
|
||||
|
||||
SerialPortManager::~SerialPortManager()
|
||||
{
|
||||
close();
|
||||
_bus->unreg_event(&_event);
|
||||
}
|
||||
|
||||
SerialPortManager* SerialPortManager::instance(){
|
||||
static QMutex mutex;
|
||||
QMutexLocker locker(&mutex);
|
||||
|
||||
static SerialPortManager *singleton = nullptr;
|
||||
if (!singleton) {
|
||||
singleton = new SerialPortManager();
|
||||
}
|
||||
return singleton;
|
||||
}
|
||||
|
||||
QList<QString> SerialPortManager::available_ports(){
|
||||
QList<QString> newPortStringList;
|
||||
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
|
||||
newPortStringList += info.portName();
|
||||
if (_available_ports_count != newPortStringList.count())
|
||||
{
|
||||
available_ports_count(newPortStringList.count());
|
||||
emit available_portsChanged(newPortStringList);
|
||||
}
|
||||
return newPortStringList;
|
||||
}
|
||||
|
||||
bool SerialPortManager::open(QString port_name, QString baudrate, QString databits,
|
||||
QString parity, QString stopbits)
|
||||
{
|
||||
_serial_port.setPortName(port_name);
|
||||
if (_serial_port.isOpen())
|
||||
{
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_serial_port.open(QIODevice::ReadWrite))
|
||||
{
|
||||
_serial_port.setBaudRate(baudrate.toInt());
|
||||
switch (databits.toInt())
|
||||
{
|
||||
case 5:
|
||||
_serial_port.setDataBits( QSerialPort::Data5 ); // 设置数据位(数据位为5位)
|
||||
break;
|
||||
case 6:
|
||||
_serial_port.setDataBits( QSerialPort::Data6 ); // 设置数据位(数据位为6位)
|
||||
break;
|
||||
case 7:
|
||||
_serial_port.setDataBits( QSerialPort::Data7 ); // 设置数据位(数据位为7位)
|
||||
break;
|
||||
case 8:
|
||||
_serial_port.setDataBits( QSerialPort::Data8 ); // 设置数据位(数据位为8位)
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
if (parity=="NONE")
|
||||
{
|
||||
_serial_port.setParity( QSerialPort::NoParity ); // 设置校验位(无校验位)
|
||||
}
|
||||
else if (parity=="EVEN")
|
||||
{
|
||||
_serial_port.setParity( QSerialPort::EvenParity ); // 设置校验位(奇校验位)
|
||||
}
|
||||
else
|
||||
{
|
||||
_serial_port.setParity( QSerialPort::OddParity ); // 设置校验位(偶校验位)
|
||||
}
|
||||
if (stopbits=="1")
|
||||
{
|
||||
_serial_port.setStopBits( QSerialPort::OneStop ); // 设置停止位(停止位为1)
|
||||
}
|
||||
else if (stopbits=="1.5")
|
||||
{
|
||||
_serial_port.setStopBits( QSerialPort::OneAndHalfStop ); // 设置停止位(停止位为1.5)
|
||||
}
|
||||
else
|
||||
{
|
||||
_serial_port.setStopBits( QSerialPort::TwoStop ); // 设置停止位(停止位为2)
|
||||
}
|
||||
_serial_port.setFlowControl( QSerialPort::HardwareControl ); // 设置流控制(硬件数据流控制)
|
||||
// 当下位机中有数据发送过来时就会响应这个槽函数
|
||||
connect(&_serial_port,&QSerialPort::readyRead,this,&SerialPortManager::_ready_for_read);
|
||||
connect(&_serial_port,&QSerialPort::aboutToClose,this,&SerialPortManager::close);
|
||||
opened(true);
|
||||
closed(false);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void SerialPortManager::close()
|
||||
{
|
||||
disconnect(&_serial_port,&QSerialPort::aboutToClose,this,&SerialPortManager::close);
|
||||
disconnect(&_serial_port,&QSerialPort::readyRead,this,&SerialPortManager::_ready_for_read);
|
||||
disconnect(_zigbee, &zigbee_protocol::DLLN3X::recved, this, &SerialPortManager::zigbee_callback);
|
||||
if(_opened)
|
||||
{
|
||||
_serial_port.clear();
|
||||
_serial_port.close();
|
||||
}
|
||||
readyRead(false);
|
||||
closed(true);
|
||||
opened(false);
|
||||
}
|
||||
|
||||
QByteArray SerialPortManager::read()
|
||||
{
|
||||
QByteArray bdata;
|
||||
if (_opened && _handledBymanager)
|
||||
{
|
||||
readyRead(false);
|
||||
bdata = _serial_port.readAll();
|
||||
if (_recv_hex)
|
||||
{
|
||||
return bdata.toHex(' ').toUpper();
|
||||
}
|
||||
else
|
||||
return bdata;
|
||||
}
|
||||
else
|
||||
return bdata;
|
||||
}
|
||||
|
||||
void SerialPortManager::_ready_for_read()
|
||||
{
|
||||
if (_handledBymanager)
|
||||
readyRead(true);
|
||||
if (!_handledBymanager)
|
||||
_zigbee->loop();
|
||||
}
|
||||
|
||||
bool SerialPortManager::write(QString data)
|
||||
{
|
||||
if (_closed)
|
||||
return false;
|
||||
|
||||
if ( !_handledBymanager)
|
||||
{
|
||||
QByteArray bdata;
|
||||
QStringList td = data.split(' ');
|
||||
for (auto item : td)
|
||||
bdata += QByteArray::fromHex(item.toLatin1());
|
||||
|
||||
zigbee_protocol::ZigbeeFrame zf(bdata[2],bdata[3],(uint16_t)bdata[4] | bdata[5]<<8, bdata.data()+6,bdata.length()-7);
|
||||
|
||||
return _zigbee->send(zf);
|
||||
}
|
||||
|
||||
QByteArray bdata;
|
||||
if(_send_hex)
|
||||
{
|
||||
QStringList td = data.split(' ');
|
||||
for (auto item : td)
|
||||
bdata += QByteArray::fromHex(item.toLatin1());
|
||||
}
|
||||
else
|
||||
bdata = data.toUtf8();
|
||||
switch(_send_type)
|
||||
{
|
||||
case SendType::WithCarriageEnter:
|
||||
bdata += "\n";
|
||||
break;
|
||||
case SendType::WithLineFeed:
|
||||
bdata += "\r";
|
||||
break;
|
||||
case SendType::WithCarriageEnterAndLineFeed:
|
||||
bdata += "\r\n";
|
||||
break;
|
||||
case SendType::Blank:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return _serial_port.write(bdata);
|
||||
}
|
||||
|
||||
void SerialPortManager::zigbee_callback(zigbee_protocol::ZigbeeFrame zframe)
|
||||
{
|
||||
QJsonObject object;
|
||||
QByteArray zf = QByteArray((char *)zframe.data(),zframe.size());
|
||||
QByteArray hex = zf.toHex(' ');
|
||||
QByteArray data = hex.toUpper();
|
||||
readyRead(false);
|
||||
object.insert("data_hex", QJsonValue(QString(data)));
|
||||
object.insert("type","zigbee_raw_data");
|
||||
_bus->push_data("zigbee_parser", object);
|
||||
}
|
||||
|
||||
QSerialPort* SerialPortManager::get_serial()
|
||||
{
|
||||
if (!_handledBymanager)
|
||||
return &_serial_port;
|
||||
else
|
||||
return nullptr;
|
||||
}
|
|
@ -1,294 +0,0 @@
|
|||
#include "zigbeeparser.h"
|
||||
#include "crypto.h"
|
||||
#include <QMutex>
|
||||
#include <QRandomGenerator>
|
||||
|
||||
ZigBeeParser::ZigBeeParser(QObject *parent)
|
||||
: QObject{parent}
|
||||
{
|
||||
_config = Config::instance();
|
||||
_protocol = zigbee_protocol::Protocol::getInstance();
|
||||
_bus = EventsBus::instance();
|
||||
_event.type("zigbee_parser");
|
||||
_bus->reg_event(&_event);
|
||||
connect(&_event,&Event::triggered,this,&ZigBeeParser::message_parser);
|
||||
}
|
||||
|
||||
ZigBeeParser::~ZigBeeParser()
|
||||
{
|
||||
_bus->unreg_event(&_event);
|
||||
disconnect(&_event,&Event::triggered,this,&ZigBeeParser::message_parser);
|
||||
}
|
||||
|
||||
ZigBeeParser* ZigBeeParser::instance()
|
||||
{
|
||||
static QMutex mutex;
|
||||
QMutexLocker locker(&mutex);
|
||||
|
||||
static ZigBeeParser *singleton = nullptr;
|
||||
if (!singleton) {
|
||||
singleton = new ZigBeeParser();
|
||||
}
|
||||
return singleton;
|
||||
}
|
||||
|
||||
void ZigBeeParser::data_parser(zigbee_protocol::ZigbeeFrame &zframe, bool is_demo)
|
||||
{
|
||||
src_port_parser(zframe,is_demo);
|
||||
des_port_parser(zframe, is_demo);
|
||||
remote_addr_parser(zframe, is_demo);
|
||||
}
|
||||
|
||||
void ZigBeeParser::src_port_parser(zigbee_protocol::ZigbeeFrame &zframe, bool is_demo)
|
||||
{
|
||||
switch (zframe.getSrcPort()) {
|
||||
case 0x81:
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ZigBeeParser::des_port_parser(zigbee_protocol::ZigbeeFrame &zframe, bool is_demo)
|
||||
{
|
||||
void *frame = nullptr;
|
||||
base_frame *bframe=nullptr;
|
||||
hmac_frame *hframe=nullptr;
|
||||
crypto_zdata_frame *czdata = nullptr;
|
||||
data_frame *ddata = nullptr;
|
||||
QByteArray zdata((char *)zframe.data(), zframe.size());
|
||||
QString sender = QString::number(((base_frame *)zframe.getData().data())->ori_addr,16).toUpper();
|
||||
device temp,self;
|
||||
QPair<device,device>* node = nullptr;
|
||||
memset(&temp,0,sizeof(temp));
|
||||
memset(&temp,0,sizeof(self));
|
||||
self.addr = SELF_ADDR;
|
||||
if (!nodes.contains(((base_frame *)zframe.getData().data())->ori_addr))
|
||||
{
|
||||
temp.addr=((base_frame *)zframe.getData().data())->ori_addr;
|
||||
temp.id = 0;
|
||||
temp.online = 1;
|
||||
temp.rand_num = 0;
|
||||
temp.verified = 0;
|
||||
nodes.insert(temp.addr,qMakePair(temp,self));
|
||||
}
|
||||
node = &nodes[((base_frame *)zframe.getData().data())->ori_addr];
|
||||
bframe = (base_frame*)zframe.getData().data();
|
||||
if (is_demo || bframe->des_addr == SELF_ADDR && (node->first.id == 0||bframe->reset_num==0xDD||((bframe->id > node->first.id && bframe->id<=BASE_FRAME_RESET_NUM))))
|
||||
{
|
||||
node->first.addr = bframe->ori_addr;
|
||||
node->first.id = bframe->id;
|
||||
node->first.rand_num = bframe->rand_num;
|
||||
frame = (void*)bframe->data;
|
||||
}
|
||||
else
|
||||
{
|
||||
QJsonObject object;
|
||||
object.insert("text",QJsonValue(QString(zdata.toHex(' ').toUpper())));
|
||||
object.insert("note_text",QJsonValue("请注意,节点0x"+QString::number(node->first.addr,16).toUpper()+"发送的该数据包损坏"));
|
||||
object.insert("recieved", true);
|
||||
object.insert("sender", sender);
|
||||
object.insert("type","zigbee_identify_data");
|
||||
_bus->push_data("zigbee_identify_data_view",object);
|
||||
return;
|
||||
}
|
||||
switch (zframe.getDesPort()) {
|
||||
case 0x81:
|
||||
{
|
||||
hframe = (hmac_frame*)frame;
|
||||
QJsonObject object;
|
||||
object.insert("text",QJsonValue(QString(zdata.toHex(' ').toUpper())));
|
||||
object.insert("sender", sender);
|
||||
object.insert("note_text",QJsonValue("收到节点0x"+ sender +"发送的验证信息"));
|
||||
object.insert("recieved", true);
|
||||
object.insert("type","zigbee_identify_data");
|
||||
_bus->push_data("zigbee_identify_data_view",object);
|
||||
if (_allow_list.contains(node->first.addr))
|
||||
{
|
||||
u8 hmac[33]="";
|
||||
QByteArray key = QByteArray::fromHex(_protocol->hmac_verify_key.toLatin1());
|
||||
Crypto::getInstance()->SM3_HMAC((u8 *)key.data(),16,&hframe->value,1,hmac);
|
||||
if (_protocol->bytecmp(hframe->hmac,hmac,32))
|
||||
{
|
||||
new_data_frame(15) data;
|
||||
memset(&data, 0, sizeof(data));
|
||||
_protocol->protocal_wrapper((data_frame *)&data, 0, 10, (u8 *)"Identified", 0);
|
||||
node->first.verified = 1;
|
||||
new_base_frame(15 + BASE_FRAME_PREFIX_LEN) bframe;
|
||||
memset(&bframe, 0, sizeof(bframe));
|
||||
_protocol->base_frame_maker(&data, (base_frame *)&bframe, node->first.addr,&node->second);
|
||||
zigbee_protocol::ZigbeeFrame zf(zframe.getSrcPort(),zframe.getDesPort(),zframe.getRemoteAddr(),(uint8_t *)&bframe,bframe.length);
|
||||
QByteArray bdata((char *)&bframe, bframe.length);
|
||||
QByteArray zdata((char *)zf.data(), zf.size());
|
||||
QJsonObject object;
|
||||
object.insert("baseframe_data",QJsonValue(QString(bdata.toHex(' ').toUpper())));
|
||||
object.insert("text",QJsonValue(QString(zdata.toHex(' ').toUpper())));
|
||||
object.insert("note_text",QJsonValue("向0x"+QString::number(node->first.addr,16).toUpper()+"节点发送验证通过回复信息"));
|
||||
object.insert("recieved", false);
|
||||
object.insert("sender", QString::number(SELF_ADDR,16).toUpper());
|
||||
object.insert("type","zigbee_identify_data");
|
||||
_bus->push_data("zigbee_identify_data_view",object);
|
||||
if(!is_demo)
|
||||
_bus->push_data("serial_port",object);
|
||||
}
|
||||
else
|
||||
{
|
||||
u8 hmac[33]="";
|
||||
QJsonObject object;
|
||||
QByteArray latest_key = QByteArray::fromHex(_protocol->hmac_verify_key.toLatin1());
|
||||
for (auto key_str : _protocol->pre_hmac_verify_key)
|
||||
{
|
||||
QByteArray key = QByteArray::fromHex(key_str.toLatin1());
|
||||
Crypto::getInstance()->SM3_HMAC((u8 *)key.data(),16,&hframe->value,1,hmac);
|
||||
if (_protocol->bytecmp(hframe->hmac,hmac,32))
|
||||
{
|
||||
zigbee_protocol::ZigbeeFrame zf(0x82,0x82,node->first.addr);
|
||||
new_data_frame(16) data;
|
||||
memset(&data, 0, sizeof(data));
|
||||
_protocol->protocal_wrapper((data_frame *)&data, 0, 16, (u8*)latest_key.data(), 0);
|
||||
new_crypto_zdata_frame(48) zdata;
|
||||
memset(&zdata, 0, sizeof(zdata));
|
||||
_protocol->zigbee_data_encrypt((data_frame *)&data, (crypto_zdata_frame *)&zdata, Crypto::SM4_encrypt, key_str);
|
||||
new_base_frame(48 + BASE_FRAME_PREFIX_LEN) bframe;
|
||||
memset(&bframe, 0, sizeof(bframe));
|
||||
_protocol->base_frame_maker(&zdata, (base_frame *)&bframe, node->first.addr, &node->second);
|
||||
zf.setData((u8 *)&bframe, bframe.length);
|
||||
QByteArray bdata((char *)&bframe, bframe.length);
|
||||
QByteArray zfdata((char *)zf.data(), zf.size());
|
||||
object.insert("baseframe_data",QJsonValue(QString(bdata.toHex(' ').toUpper())));
|
||||
object.insert("text",QJsonValue(QString(zfdata.toHex(' ').toUpper())));
|
||||
object.insert("note_text",QJsonValue("节点0x"+QString::number(node->first.addr,16).toUpper()+"正在使用旧密钥,发送密钥更新指令"));
|
||||
object.insert("recieved", false);
|
||||
object.insert("sender", QString::number(SELF_ADDR,16).toUpper());
|
||||
object.insert("type","zigbee_identify_data");
|
||||
_bus->push_data("zigbee_identify_data_view",object);
|
||||
if(!is_demo)
|
||||
_bus->push_data("serial_port",object);
|
||||
return;
|
||||
}
|
||||
}
|
||||
object.insert("note_text",QJsonValue("节点0x"+QString::number(node->first.addr,16).toUpper()+"验证不通过,请考虑移出认证列表"));
|
||||
object.insert("recieved", true);
|
||||
object.insert("sender", sender);
|
||||
object.insert("type","zigbee_identify_data");
|
||||
_bus->push_data("zigbee_identify_data_view",object);
|
||||
}
|
||||
}
|
||||
else if(_deny_list.contains(node->first.addr))
|
||||
{
|
||||
object.insert("sender", sender);
|
||||
object.insert("text","");
|
||||
object.insert("note_text",QJsonValue("节点0x"+QString::number(((base_frame *)zframe.getData().data())->ori_addr,16).toUpper()+"被禁止接入"));
|
||||
object.insert("recieved", true);
|
||||
object.insert("type","zigbee_identify_data");
|
||||
_bus->push_data("zigbee_identify_data_view",object);
|
||||
}
|
||||
else {
|
||||
if (!_wait_queue.contains(node->first.addr))
|
||||
{
|
||||
_wait_queue.append(node->first.addr);
|
||||
object.insert("sender", sender);
|
||||
object.insert("text","");
|
||||
object.insert("note_text",QJsonValue("节点0x"+QString::number(((base_frame *)zframe.getData().data())->ori_addr,16).toUpper()+"进入等待队列"));
|
||||
object.insert("recieved", true);
|
||||
object.insert("type","zigbee_identify_data");
|
||||
_bus->push_data("zigbee_identify_data_view",object);
|
||||
}
|
||||
else
|
||||
{
|
||||
object.insert("sender", sender);
|
||||
object.insert("text","");
|
||||
object.insert("note_text",QJsonValue("节点0x"+QString::number(((base_frame *)zframe.getData().data())->ori_addr,16).toUpper()+"已在等待队列"));
|
||||
object.insert("recieved", true);
|
||||
object.insert("type","zigbee_identify_data");
|
||||
_bus->push_data("zigbee_identify_data_view",object);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0x82:
|
||||
{
|
||||
|
||||
}
|
||||
case 0x83:
|
||||
{
|
||||
zigbee_protocol::ZigbeeFrame dzf = zframe;
|
||||
new_data_frame(72) ndata;
|
||||
QJsonObject object;
|
||||
if (*(u16 *)frame == CRYPTO_ZDATA_FRAME_HEAD)
|
||||
{
|
||||
czdata = (crypto_zdata_frame*)frame;
|
||||
_protocol->zigbee_data_dectypt((data_frame*)&ndata, czdata, Crypto::SM4_encrypt);
|
||||
dzf.setData((char*)&ndata,ndata.data_length + DATA_FRAME_PREFIX_LEN);
|
||||
zdata = QByteArray((char *)dzf.data(), dzf.size());
|
||||
object.insert("decrypted_text", QJsonValue(QString(zdata.toHex(' ').toUpper())));
|
||||
}
|
||||
object.insert("sender", sender);
|
||||
object.insert("text",QJsonValue(QString(zdata.toHex(' ').toUpper())));
|
||||
object.insert("note_text",QJsonValue("收到节点0x"+sender+"发送的数据"));
|
||||
object.insert("recieved", true);
|
||||
object.insert("type","zigbee_recv_data");
|
||||
if (QRandomGenerator::global()->bounded(2) && is_demo)
|
||||
object.insert("decrypted_text", QJsonValue(QString(zdata.toHex(' ').toUpper())));
|
||||
_bus->push_data("zigbee_recv_data_view",object);
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ZigBeeParser::remote_addr_parser(zigbee_protocol::ZigbeeFrame &zframe, bool is_demo)
|
||||
{
|
||||
switch (zframe.getRemoteAddr()) {
|
||||
case 0xAAAA:
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ZigBeeParser::message_parser(QJsonObject message)
|
||||
{
|
||||
if (message["type"] == "demo_verify_request" || message["type"] == "zigbee_verify_request")
|
||||
{
|
||||
hmac_frame frame;
|
||||
frame.value = QRandomGenerator::global()->bounded(256);
|
||||
frame.head = HMAC_FRAME_HEAD;
|
||||
frame.length = 32 + HMAC_FRAME_PREFIX_LEN;
|
||||
QByteArray key = QByteArray::fromHex(_protocol->hmac_verify_key.toLatin1());
|
||||
Crypto::getInstance()->SM3_HMAC((u8 *)key.data(), 16, &frame.value, 1, frame.hmac);
|
||||
new_base_frame(50 + BASE_FRAME_PREFIX_LEN) bframe;
|
||||
device test;
|
||||
test.addr = 0x5656;
|
||||
_protocol->base_frame_maker(&frame, (base_frame *)&bframe, 0x5656, &test);
|
||||
zigbee_protocol::ZigbeeFrame zf(0x81,0x81,0x5656,(char*)&bframe,bframe.length);
|
||||
data_parser(zf, message["type"] == "demo_verify_request");
|
||||
}
|
||||
if (message["type"] == "demo_verify_key_update" || message["type"] == "zigbee_verify_key_update")
|
||||
{
|
||||
hmac_frame frame;
|
||||
frame.value = QRandomGenerator::global()->bounded(256);
|
||||
frame.head = HMAC_FRAME_HEAD;
|
||||
frame.length = 32 + HMAC_FRAME_PREFIX_LEN;
|
||||
QByteArray key = QByteArray::fromHex(_protocol->pre_hmac_verify_key[QRandomGenerator::global()->bounded(_protocol->pre_hmac_verify_key.length())].toLatin1());
|
||||
Crypto::getInstance()->SM3_HMAC((u8 *)key.data(), 16, &frame.value, 1, frame.hmac);
|
||||
new_base_frame(50 + BASE_FRAME_PREFIX_LEN) bframe;
|
||||
device test;
|
||||
test.addr = 0x5656;
|
||||
_protocol->base_frame_maker(&frame, (base_frame *)&bframe, 0x5656, &test);
|
||||
zigbee_protocol::ZigbeeFrame zf(0x81,0x81,0x5656,(char*)&bframe,bframe.length);
|
||||
data_parser(zf, message["type"] == "demo_verify_key_update");
|
||||
}
|
||||
if (message["type"] == "demo_recv_data" || message["type"] == "zigbee_raw_data")
|
||||
{
|
||||
QString data = message["data_hex"].toString();
|
||||
QByteArray bdata;
|
||||
QStringList td = data.split(' ');
|
||||
for (auto item : td)
|
||||
bdata += QByteArray::fromHex(item.toLatin1());
|
||||
zigbee_protocol::ZigbeeFrame zf(bdata[2],bdata[3],(uint16_t)bdata[4] | bdata[5]<<8, bdata.data()+6,bdata.length()-7);
|
||||
data_parser(zf, message["type"] == "demo_recv_data");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue