Skip to content

Commit

Permalink
fix: updated to be close to v11 draft
Browse files Browse the repository at this point in the history
  • Loading branch information
nahojkap committed Oct 22, 2024
1 parent 3a9e655 commit 9c93d16
Show file tree
Hide file tree
Showing 27 changed files with 615 additions and 762 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [push]

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release
BUILD_TYPE: Debug

jobs:
build:
Expand Down
73 changes: 3 additions & 70 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,74 +50,7 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
.idea
build
cmake-build-debug/
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

5 changes: 0 additions & 5 deletions .idea/codeStyles/codeStyleConfig.xml

This file was deleted.

573 changes: 238 additions & 335 deletions .idea/editor.xml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
#

cmake_minimum_required(VERSION 3.17)
cmake_minimum_required(VERSION 3.12)
project(craggy C)

set(CMAKE_C_STANDARD 11)
Expand All @@ -25,7 +25,7 @@ option(CRAGGY_WITH_ORLP_ED25519_BINDINGS "Use ORLPs ED25519 cryptographic operat
add_subdirectory(library)

enable_testing()
add_subdirectory(test)
add_subdirectory(tests)

add_subdirectory(cli)

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

Craggy is a Roughtime secure time synchronization client implementation in C. It draws both code and inspiration from the Google C++ client implementation.

![CMake Build](https://github.com/nahojkap/craggy/workflows/CMake/badge.svg)

### Dependencies

Craggy requires two cryptographic operations to work, ED 25519 signature validation and SHA512. Current build can be configured to use either OpenSSL or the ED 25519 implementation from https://github.com/orlp/ed25519 (which also provides SHA512).
Craggy requires two cryptographic operations to work, ED 25519 signature validation and SHA512.

Current build can be configured to use either OpenSSL or the ED 25519 implementation from https://github.com/orlp/ed25519 (which also provides SHA512).

To configure the crypto provider, use '-DCRAGGY_WITH_OPENSSL_BINDINGS=ON' or '-DCRAGGY_WITH_ORLP_ED25519_BINDINGS=ON' respectively.

Expand Down
4 changes: 2 additions & 2 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
project(craggy-cli C)

set(SOURCES
base64
main)
base64.c
main.c)

add_executable(craggy-cli ${SOURCES})
target_link_libraries(craggy-cli craggy)
Expand Down
59 changes: 26 additions & 33 deletions cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <inttypes.h>
#include <getopt.h>
#include <assert.h>
#include <stdlib.h>

#include "base64.h"
#include "CraggyTransport.h"
Expand Down Expand Up @@ -50,7 +51,7 @@ int main(int argc, char *argv[]) {

static struct option long_options[] = {
{"host", required_argument, 0, 'h'},
{"key", required_argument, 0, 'k'},
{"key", required_argument, 0, 'k'},
{"nonce", optional_argument, 0, 'n'},
{0, 0, 0, 0}
};
Expand Down Expand Up @@ -82,18 +83,15 @@ int main(int argc, char *argv[]) {
break;

case 'h':
hostname = malloc(strlen(optarg)+1);
hostname = strcpy(hostname, optarg);
hostname = strdup(optarg);
break;

case 'n':
nonce = malloc(strlen(optarg)+1);
nonce = strcpy(hostname, optarg);
nonce = strdup(optarg);
break;

case 'k':
publicKey = malloc(strlen(optarg)+1);
publicKey = strcpy(publicKey, optarg);
publicKey = strdup(optarg);
break;

case '?':
Expand All @@ -114,26 +112,24 @@ int main(int argc, char *argv[]) {
craggy_rough_time_public_key_t rootPublicKey;
size_t base64DecodedRootPublicKeyLen = 0;
unsigned char *base64DecodedRootPublicKey = base64_decode((const unsigned char *) publicKey, strlen(publicKey), &base64DecodedRootPublicKeyLen);

if (base64DecodedRootPublicKeyLen != CRAGGY_ROUGH_TIME_PUBLIC_KEY_LENGTH) {
printf("Public key length must be %d byte(s) (got %zu after base64 decoding)", CRAGGY_ROUGH_TIME_PUBLIC_KEY_LENGTH, base64DecodedRootPublicKeyLen);
if (base64DecodedRootPublicKeyLen != CRAGGY_ROUGHTIME_PUBLIC_KEY_LENGTH) {
printf("Public key length must be %d byte(s) (got %zu after base64 decoding)", CRAGGY_ROUGHTIME_PUBLIC_KEY_LENGTH, base64DecodedRootPublicKeyLen);
goto error;
}
memcpy(&rootPublicKey, base64DecodedRootPublicKey, CRAGGY_ROUGH_TIME_PUBLIC_KEY_LENGTH);
memcpy(&rootPublicKey, base64DecodedRootPublicKey, CRAGGY_ROUGHTIME_PUBLIC_KEY_LENGTH);
free(base64DecodedRootPublicKey);

craggy_rough_time_request_t requestBuf;
memset(requestBuf, 0, sizeof(craggy_rough_time_request_t));

craggy_rough_time_nonce_t nonceBytes;
memset(nonceBytes, 1, CRAGGY_ROUGH_TIME_NONCE_LENGTH);

if (nonce != NULL)
{
size_t outLen = 0;
unsigned char *decodedNonceBytes = base64_decode((unsigned char*)nonce, strlen(nonce), &outLen);
if (outLen != CRAGGY_ROUGH_TIME_NONCE_LENGTH) {
printf("Nonce length must be %d byte(s) (got %zu after base64 decoding)", CRAGGY_ROUGH_TIME_NONCE_LENGTH, outLen);
if (outLen != CRAGGY_ROUGHTIME_NONCE_LENGTH) {
printf("Nonce length must be %d byte(s) (got %zu after base64 decoding)", CRAGGY_ROUGHTIME_NONCE_LENGTH, outLen);
goto error;
}
memcpy(nonceBytes, decodedNonceBytes, outLen);
Expand All @@ -151,35 +147,30 @@ int main(int argc, char *argv[]) {
{
const uint64_t start_us = MonotonicUs();

craggy_rough_time_t timestamp;
uint32_t radius;

size_t responseBufLen = CRAGGY_ROUGH_TIME_MIN_REQUEST_SIZE *3;
craggy_rough_time_response_t responseBuf[responseBufLen];
size_t responseBufLen = 0;
craggy_rough_time_response_t responseBuf;

if (craggy_makeRequest(hostname, requestBuf, &craggyResult, responseBuf, &responseBufLen)) {

if (!craggy_processResponse(nonceBytes, rootPublicKey, responseBuf, responseBufLen, &craggyResult, &timestamp, &radius)) {
const uint64_t end_us = MonotonicUs();
const uint64_t roundtripElapsedTimeUs = (end_us - start_us) / 2;
const uint64_t endRealtimeUs = RealtimeUs();

craggy_roughtime_result roughtimeResult;

if (!craggy_processResponse(nonceBytes, rootPublicKey, responseBuf, responseBufLen, &craggyResult, &roughtimeResult)) {
printf("Error parsing response: %d", craggyResult);
goto error;
}

const uint64_t end_us = MonotonicUs();
const uint64_t end_realtime_us = RealtimeUs();

// We assume that the path to the Roughtime server is symmetric and thus add
// half the round-trip time to the server's timestamp to produce our estimate
// of the current time.
timestamp += (end_us - start_us) / 2;
printf("Received reply in %" PRIu64 "μs.\n", end_us - start_us);
printf("Current time is %" PRIu64 "μs from the epoch, ±%uμs \n", timestamp, radius);
int64_t system_offset = timestamp - end_realtime_us;
printf("System clock differs from that estimate by %" PRId64 "μs.\n", system_offset);
static const int64_t kTenMinutes = 10 * 60 * 1000000;

if (imaxabs(system_offset) > kTenMinutes) {
goto error;
}
printf("Received reply in %" PRIu64 "μs. (%dms)\n", end_us - start_us, (uint32_t)(end_us - start_us)/1000);
printf("Current time is %" PRIu64 "ms from the epoch, ±%us \n", (roughtimeResult.midpoint + (roundtripElapsedTimeUs/1000)), roughtimeResult.radius);
int64_t systemOffsetUs = (roughtimeResult.midpoint*1000000) - endRealtimeUs;
printf("System clock differs from that estimate by %" PRId64 "μs. (%dms)\n", systemOffsetUs, (int32_t)(systemOffsetUs/1000));

}
else {
printf("Error making request: %d", craggyResult);
Expand All @@ -190,6 +181,8 @@ int main(int argc, char *argv[]) {

goto exit;
error:
printf("Error request %d\n", result);

assert (result != 0);

exit:
Expand Down
15 changes: 0 additions & 15 deletions gtest/CMakeLists.txt.in

This file was deleted.

18 changes: 8 additions & 10 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@
project(craggy C)

set(SOURCES
CraggyProtocol
CraggyProtocol
CraggyClient
CraggyClient
CraggyCrypto
CraggyOS
CraggyTypes)
CraggyProtocol.c
CraggyProtocol.c
CraggyClient.c
CraggyClient.c
)

if (UNIX)
set(SOURCES ${SOURCES} crypto/CraggyCrypto-Linux.c)
endif()

if (CRAGGY_WITH_UDP_TRANSPORT)
set(SOURCES ${SOURCES} CraggyUDPTransport)
set(SOURCES ${SOURCES} CraggyUDPTransport.c)
endif ()

if (CRAGGY_WITH_ORLP_ED25519_BINDINGS)
Expand All @@ -55,14 +53,14 @@ if (CRAGGY_WITH_ORLP_ED25519_BINDINGS)
${CMAKE_CURRENT_BINARY_DIR}/craggy-orlp-ed25519-build
EXCLUDE_FROM_ALL)

set(SOURCES ${SOURCES} crypto/CraggyCrypto-ORLP-ED25519)
set(SOURCES ${SOURCES} crypto/CraggyCrypto-ORLP-ED25519.c)

endif()

if (CRAGGY_WITH_OPENSSL_BINDINGS)
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})
set(SOURCES ${SOURCES} crypto/CraggyCrypto-OpenSSL CraggyTypes.h CraggyOS.h)
set(SOURCES ${SOURCES} crypto/CraggyCrypto-OpenSSL.c)
endif()

add_library(craggy STATIC ${SOURCES})
Expand Down
Loading

0 comments on commit 9c93d16

Please sign in to comment.