Skip to content

Commit

Permalink
Add utility
Browse files Browse the repository at this point in the history
  • Loading branch information
ReimuNotMoe committed Nov 18, 2021
1 parent 2fa7659 commit 3957537
Show file tree
Hide file tree
Showing 21 changed files with 2,250 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Software/PICoRNG_Utility/CMake/FindLibUSB.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#[==============================================[
FindLibUSB
-----------
Searching libusb-1.0 library and creating imported
target LibUSB::LibUSB
#]==============================================]

# TODO Append parts for Version compasion and REQUIRED support

if (MSVC OR MINGW)
return()
endif()

if (NOT TARGET LibUSB::LibUSB)
find_package(PkgConfig)
pkg_check_modules(LibUSB REQUIRED
libusb-1.0
)

if(LibUSB_FOUND)
message(STATUS "libusb-1.0 found using pkgconfig")

add_library(LibUSB::LibUSB
UNKNOWN IMPORTED
)
if (DEFINED LibUSB_INCLUDE_DIRS AND NOT LibUSB_INCLUDE_DIRS STREQUAL "")
set_target_properties(LibUSB::LibUSB PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${LibUSB_INCLUDE_DIRS}
)
endif()

if(LibUSB_LIBRARIES)
find_library(LibUSB_LIBRARY
NAMES ${LibUSB_LIBRARIES}
PATHS ${LibUSB_LIBDIR} ${LibUSB_LIBRARY_DIRS}
)
if(LibUSB_LIBRARY)
set_target_properties(LibUSB::LibUSB PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION ${LibUSB_LIBRARY}
)
else()
message(WARNING "Could not found libusb-1.0 library file")
endif()
endif()
endif()
else()
message(WARNING "libusb-1.0 could not be found using pkgconfig")
endif()
47 changes: 47 additions & 0 deletions Software/PICoRNG_Utility/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
cmake_minimum_required(VERSION 3.14)
project(PICoRNG_Utility)

set(CMAKE_CXX_STANDARD 17)

set(CPM_DOWNLOAD_VERSION 0.27.5)
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
message(STATUS "Downloading CPM.cmake")
file(DOWNLOAD https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake ${CPM_DOWNLOAD_LOCATION})
endif()

include(${CPM_DOWNLOAD_LOCATION})

CPMAddPackage(
NAME cxxopts
GITHUB_REPOSITORY SudoMaker/cxxopts
VERSION 3.0.0
GIT_TAG 5eca8a30012b69b76316b71fa391a89fe09256cb
GIT_SHALLOW ON
)

CPMAddPackage(
NAME EntropyCheck
GITHUB_REPOSITORY SudoMaker/EntropyCheck
VERSION 0.0.0
GIT_SHALLOW ON
)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake)

find_package(LibUSB)
include_directories(${LibUSB_INCLUDE_DIRS})

add_executable(PICoRNG
tinyECDH/ecdh.c tinyECDH/ecdh.h
CommonIncludes.hpp
main.cpp
Components/USB.hpp Components/USB.cpp Components/Protocol.h
Commands/Commands.hpp Commands/Cat.cpp Commands/Quality.cpp Commands/ListDevs.cpp
Components/Verification.cpp Components/Verification.hpp Components/Utilities.cpp Components/Utilities.hpp Commands/Info.cpp Commands/Pair.cpp Commands/Verify.cpp Commands/RngD.cpp)
target_link_libraries(PICoRNG cxxopts EntropyCheck ${LibUSB_LIBRARIES})

if(UNIX AND NOT APPLE)
target_link_libraries(PICoRNG stdc++fs)
endif()
58 changes: 58 additions & 0 deletions Software/PICoRNG_Utility/Commands/Cat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
This file is part of PICoRNG Utility.
Copyright (C) 2021 ReimuNotMoe <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "Commands.hpp"

int Command_Cat(const std::vector<std::string>& args) {
auto *usbdev_handle = USB_OpenCurrentDevice();

TRUSB_Packet pkt_tx, pkt_rx;
pkt_tx.type = TRUSB_PacketType_RandomData_Request;

int r;

ssize_t blocks = -1;

if (args.size() == 2) {
blocks = strtol(args[1].c_str(), nullptr, 10);
}

while (1) {
r = USB_SendPacket(usbdev_handle, &pkt_tx);
assert(r);
// std::cerr << "send: " << r << "\n";
r = USB_Recv(usbdev_handle, &pkt_rx, sizeof(pkt_rx));
assert(r);
// std::cerr << "recv: " << r << "\n";

write(STDOUT_FILENO, pkt_rx.payload.rnd_rsp.random_data, 32);

if (blocks > 0) {
blocks--;
}

if (blocks == 0) {
break;
}
}

USB_CloseDevice(usbdev_handle);

return 0;
}
31 changes: 31 additions & 0 deletions Software/PICoRNG_Utility/Commands/Commands.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
This file is part of PICoRNG Utility.
Copyright (C) 2021 ReimuNotMoe <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include "../CommonIncludes.hpp"
#include "../Components/USB.hpp"

extern void Command_ListDevices();
extern int Command_Cat(const std::vector<std::string>& args);
extern int Command_Quality(const std::vector<std::string>& args);
extern int Command_Info(const std::vector<std::string>& args);
extern int Command_Pair(const std::vector<std::string>& args);
extern int Command_Verify(const std::vector<std::string>& args);
extern int Command_RngD(const std::vector<std::string>& args);
40 changes: 40 additions & 0 deletions Software/PICoRNG_Utility/Commands/Info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
This file is part of PICoRNG Utility.
Copyright (C) 2021 ReimuNotMoe <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "Commands.hpp"

int Command_Info(const std::vector<std::string>& args) {
auto *usbdev_handle = USB_OpenCurrentDevice();

TRUSB_Packet pkt_tx, pkt_rx;
pkt_tx.type = TRUSB_PacketType_Info_Request;


USB_SendPacket(usbdev_handle, &pkt_tx);
USB_Recv(usbdev_handle, &pkt_rx, sizeof(pkt_rx));

printf("Version: 0x%08x\n", pkt_rx.payload.info_rsp.version);
puts("");
printf("Configured: %s\n", pkt_rx.payload.info_rsp.status.flags.configured ? "true" : "false");


USB_CloseDevice(usbdev_handle);

return 0;
}
63 changes: 63 additions & 0 deletions Software/PICoRNG_Utility/Commands/ListDevs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
This file is part of PICoRNG Utility.
Copyright (C) 2021 ReimuNotMoe <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "Commands.hpp"

void Command_ListDevices() {
uint8_t path[8];

for (size_t i=0; i<tr_usb_devices.size(); i++) {
auto &dev = tr_usb_devices[i];

struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);

if (r < 0) {
fprintf(stderr, "failed to get device descriptor");
return;
}


printf("#%zu (bus %d, device %d", i, libusb_get_bus_number(dev), libusb_get_device_address(dev));

r = libusb_get_port_numbers(dev, path, sizeof(path));

if (r > 0) {
printf(", path %d", path[0]);
for (size_t j = 1; j < r; j++)
printf(".%d", path[j]);
}

printf(") ");

libusb_device_handle *handle;

r = libusb_open(dev, &handle);

if (r == 0) {
uint8_t buf[32] = {0};

libusb_get_string_descriptor_ascii(handle, 1, buf, 31);
puts((char *) buf);

libusb_close(handle);
}

}
}
42 changes: 42 additions & 0 deletions Software/PICoRNG_Utility/Commands/Pair.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
This file is part of PICoRNG Utility.
Copyright (C) 2021 ReimuNotMoe <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "Commands.hpp"

#include "../Components/Verification.hpp"

int Command_Pair(const std::vector<std::string>& args) {
auto *usbdev_handle = USB_OpenCurrentDevice();

TRUSB_Packet pkt_tx, pkt_rx;
pkt_tx.type = TRUSB_PacketType_Info_Request;


bool rc = Verification_DoConfigureDevice(usbdev_handle, true);

if (rc) {
std::cerr << "Pair completed" << "\n";
} else {
std::cerr << "Pair failed" << "\n";
}

USB_CloseDevice(usbdev_handle);

return 0;
}
Loading

0 comments on commit 3957537

Please sign in to comment.