Implement bright_ambassador

This commit is contained in:
Florian Märkl 2018-11-17 11:43:36 +01:00
commit 1162b26759
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
7 changed files with 155 additions and 4 deletions

View file

@ -6,7 +6,8 @@ set(HEADER_FILES
include/chiaki/base64.h
include/chiaki/http.h
include/chiaki/log.h
include/chiaki/ctrl.h)
include/chiaki/ctrl.h
include/chiaki/rpcrypt.h)
set(SOURCE_FILES
src/common.c
@ -15,7 +16,9 @@ set(SOURCE_FILES
src/base64.c
src/http.c
src/log.c
src/ctrl.c)
src/ctrl.c
src/rpcrypt.c)
add_library(chiaki-lib ${HEADER_FILES} ${SOURCE_FILES})
set_target_properties(chiaki-lib PROPERTIES OUTPUT_NAME chiaki)
@ -24,3 +27,6 @@ target_include_directories(chiaki-lib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/includ
find_package(Threads)
target_link_libraries(chiaki-lib Threads::Threads)
find_package(OpenSSL REQUIRED)
target_link_libraries(chiaki-lib OpenSSL::Crypto)

View file

@ -0,0 +1,42 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_RPCRYPT_H
#define CHIAKI_RPCRYPT_H
#include "common.h"
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define CHIAKI_KEY_BYTES 0x10
typedef struct chiaki_rpcrypt_t
{
} ChiakiRPCrypt;
CHIAKI_EXPORT void chiaki_rpcrypt_bright_ambassador(uint8_t *bright, uint8_t *ambassador, const uint8_t *nonce, const uint8_t *morning);
#ifdef __cplusplus
}
#endif
#endif // CHIAKI_RPCRYPT_H

View file

@ -22,6 +22,7 @@
#include "thread.h"
#include "log.h"
#include "ctrl.h"
#include "rpcrypt.h"
#include <stdint.h>
#include <netdb.h>
@ -70,7 +71,6 @@ typedef struct chiaki_event_t
typedef void (*ChiakiEventCallback)(ChiakiEvent *event, void *user);
#define CHIAKI_KEY_BYTES 0x10
typedef struct chiaki_session_t
{

45
lib/src/rpcrypt.c Normal file
View file

@ -0,0 +1,45 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#include <chiaki/rpcrypt.h>
CHIAKI_EXPORT void chiaki_rpcrypt_bright_ambassador(uint8_t *bright, uint8_t *ambassador, const uint8_t *nonce, const uint8_t *morning)
{
static const uint8_t echo_a[] = { 0x01, 0x49, 0x87, 0x9b, 0x65, 0x39, 0x8b, 0x39, 0x4b, 0x3a, 0x8d, 0x48, 0xc3, 0x0a, 0xef, 0x51 };
static const uint8_t echo_b[] = { 0xe1, 0xec, 0x9c, 0x3a, 0xdd, 0xbd, 0x08, 0x85, 0xfc, 0x0e, 0x1d, 0x78, 0x90, 0x32, 0xc0, 0x04 };
for(uint8_t i=0; i<CHIAKI_KEY_BYTES; i++)
{
uint8_t v = nonce[i];
v -= i;
v -= 0x27;
v ^= echo_a[i];
ambassador[i] = v;
}
for(uint8_t i=0; i<CHIAKI_KEY_BYTES; i++)
{
uint8_t v = morning[i];
v -= i;
v += 0x34;
v ^= echo_b[i];
v ^= nonce[i];
bright[i] = v;
}
}

View file

@ -4,7 +4,8 @@ target_include_directories(munit PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/munit")
add_executable(chiaki-unit
main.c
http.c)
http.c
rpcrypt.c)
target_link_libraries(chiaki-unit chiaki-lib munit)

View file

@ -18,6 +18,7 @@
#include <munit.h>
extern MunitTest tests_http[];
extern MunitTest tests_rpcrypt[];
static MunitSuite suites[] = {
{
@ -27,6 +28,13 @@ static MunitSuite suites[] = {
1,
MUNIT_SUITE_OPTION_NONE
},
{
"/rpcrypt",
tests_rpcrypt,
NULL,
1,
MUNIT_SUITE_OPTION_NONE
},
{ NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE }
};

49
test/rpcrypt.c Normal file
View file

@ -0,0 +1,49 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#include <munit.h>
#include <chiaki/rpcrypt.h>
static MunitResult test_bright_ambassador(const MunitParameter params[], void *user)
{
static const uint8_t nonce[] = { 0x43, 0x9, 0x67, 0xae, 0x36, 0x4b, 0x1c, 0x45, 0x26, 0x62, 0x37, 0x7a, 0xbf, 0x3f, 0xe9, 0x39 };
static const uint8_t morning[] = { 0xd2, 0x78, 0x9f, 0x51, 0x85, 0xa7, 0x99, 0xa2, 0x44, 0x52, 0x77, 0x9c, 0x2b, 0x83, 0xcf, 0x7 };
static const uint8_t bright_expected[] = { 0xa4, 0x4e, 0x2a, 0x16, 0x5e, 0x20, 0xd3, 0xf, 0xaa, 0x11, 0x8b, 0xc7, 0x7c, 0xa7, 0xdc, 0x11 };
static const uint8_t ambassador_expected[] = { 0x1d, 0xa8, 0xb9, 0x1f, 0x6e, 0x26, 0x64, 0x2e, 0xbc, 0x8, 0x8b, 0x0, 0x4f, 0x1, 0x5b, 0x52 };
uint8_t bright[CHIAKI_KEY_BYTES];
uint8_t ambassador[CHIAKI_KEY_BYTES];
chiaki_rpcrypt_bright_ambassador(bright, ambassador, nonce, morning);
munit_assert_memory_equal(CHIAKI_KEY_BYTES, bright, bright_expected);
munit_assert_memory_equal(CHIAKI_KEY_BYTES, ambassador, ambassador_expected);
return MUNIT_OK;
}
MunitTest tests_rpcrypt[] = {
{
"/bright_ambassador",
test_bright_ambassador,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};