forked from strongswan/strongswan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completing plugin template + adding ETSI 004 template to API
fixing etsi_api template retrieving lost code fix improving key management
- Loading branch information
Showing
10 changed files
with
388 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
* Copyright (C) 2024 Javier Blanco-Romero @fj-blanco (UC3M, QURSA project) | ||
* | ||
* This program 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 2 of the License, or (at your | ||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | ||
* | ||
* 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 General Public License | ||
* for more details. | ||
*/ | ||
|
||
/* | ||
* qkd_kex.c | ||
*/ | ||
/* | ||
* qkd_etsi_api.c | ||
*/ | ||
#include "qkd_etsi_api.h" | ||
#include <utils/debug.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
|
||
/* Hardcoded test key */ | ||
static u_char test_key[QKD_KEY_SIZE] = { | ||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | ||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, | ||
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, | ||
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f | ||
}; | ||
|
||
bool qkd_generate_random_key_id(chunk_t *key_id) | ||
{ | ||
int fd; | ||
u_char random_id[QKD_KEY_ID_SIZE]; | ||
ssize_t bytes_read; | ||
|
||
// Open /dev/urandom | ||
fd = open("/dev/urandom", O_RDONLY); | ||
if (fd < 0) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: failed to open /dev/urandom"); | ||
return FALSE; | ||
} | ||
|
||
// Read random bytes | ||
bytes_read = read(fd, random_id, QKD_KEY_ID_SIZE); | ||
close(fd); | ||
|
||
if (bytes_read != QKD_KEY_ID_SIZE) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: failed to read random bytes"); | ||
return FALSE; | ||
} | ||
|
||
*key_id = chunk_clone(chunk_create(random_id, QKD_KEY_ID_SIZE)); | ||
qkd_print_key_id("Generated random", *key_id); | ||
return TRUE; | ||
} | ||
void qkd_print_key(const char *prefix, chunk_t key) | ||
{ | ||
char hex[2048] = ""; // Make sure this is large enough | ||
chunk_to_hex(key, hex, FALSE); | ||
DBG1(DBG_LIB, "QKD_plugin: %s key: %s", prefix, hex); | ||
} | ||
|
||
void qkd_print_key_id(const char *prefix, chunk_t key_id) | ||
{ | ||
char hex[256] = ""; | ||
chunk_to_hex(key_id, hex, FALSE); | ||
DBG1(DBG_LIB, "QKD_plugin: %s key ID: %s", prefix, hex); | ||
} | ||
|
||
bool qkd_set_key_id(qkd_handle_t handle, chunk_t key_id) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: qkd_set_key_id()"); | ||
if (!handle || !handle->is_open || key_id.len != QKD_KEY_ID_SIZE) | ||
{ | ||
return FALSE; | ||
} | ||
|
||
chunk_clear(&handle->key_id); | ||
handle->key_id = chunk_clone(key_id); | ||
|
||
qkd_print_key_id("Bob received", key_id); | ||
return TRUE; | ||
} | ||
|
||
bool qkd_get_key_id(qkd_handle_t handle, chunk_t *key_id) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: qkd_get_key_id()"); | ||
if (!handle || !handle->is_open || !key_id) | ||
{ | ||
return FALSE; | ||
} | ||
|
||
// Generate random key ID for Alice | ||
if (!qkd_generate_random_key_id(key_id)) | ||
{ | ||
return FALSE; | ||
} | ||
|
||
// Replace handle's key_id with new one | ||
chunk_free(&handle->key_id); | ||
handle->key_id = chunk_clone(*key_id); | ||
|
||
qkd_print_key_id("QKD_plugin: Alice sending", *key_id); | ||
return TRUE; | ||
} | ||
|
||
bool qkd_get_key(qkd_handle_t handle, chunk_t *key) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: qkd_get_key called"); | ||
|
||
if (!handle || !handle->is_open || !key) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: invalid parameters in get_key"); | ||
return FALSE; | ||
} | ||
|
||
if (handle->key_id.len == 0) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: no key ID set"); | ||
return FALSE; | ||
} | ||
|
||
// Print the key_id we're actually using from the exchange | ||
qkd_print_key_id("Using", handle->key_id); | ||
|
||
// For demo, return the test key when given a valid key_id | ||
// In a real implementation, we would use the key_id to look up the correct key | ||
*key = chunk_clone(handle->key); | ||
qkd_print_key("Retrieved", *key); | ||
|
||
return TRUE; | ||
} | ||
|
||
bool qkd_open(qkd_handle_t *handle) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: qkd_open called"); | ||
|
||
if (!handle) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: invalid handle pointer"); | ||
return FALSE; | ||
} | ||
|
||
*handle = malloc_thing(struct qkd_handle_t); | ||
if (!*handle) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: memory allocation failed for handle"); | ||
return FALSE; | ||
} | ||
|
||
(*handle)->id = 1; | ||
(*handle)->is_open = TRUE; | ||
(*handle)->key = chunk_clone(chunk_create(test_key, QKD_KEY_SIZE)); | ||
// Initialize key_id as empty - will be set during exchange | ||
(*handle)->key_id = chunk_empty; | ||
|
||
DBG1(DBG_LIB, "QKD_plugin: opened QKD connection with id %d", (*handle)->id); | ||
return TRUE; | ||
} | ||
|
||
bool qkd_close(qkd_handle_t handle) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: qkd_close called"); | ||
|
||
if (!handle) | ||
{ | ||
DBG1(DBG_LIB, "QKD_plugin: invalid handle in close"); | ||
return FALSE; | ||
} | ||
|
||
// Properly free chunks before freeing handle | ||
chunk_clear(&handle->key); | ||
chunk_clear(&handle->key_id); | ||
|
||
handle->is_open = FALSE; | ||
free(handle); | ||
DBG1(DBG_LIB, "QKD_plugin: connection closed successfully"); | ||
return TRUE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (C) 2024 Javier Blanco-Romero @fj-blanco (UC3M, QURSA project) | ||
* | ||
* This program 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 2 of the License, or (at your | ||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | ||
* | ||
* 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 General Public License | ||
* for more details. | ||
*/ | ||
|
||
/* | ||
* qkd_etsi_api.h | ||
*/ | ||
|
||
#ifndef QKD_ETSI_API_H_ | ||
#define QKD_ETSI_API_H_ | ||
|
||
#include <library.h> | ||
|
||
#define QKD_KEY_SIZE 32 | ||
#define QKD_KEY_ID_SIZE 8 | ||
|
||
typedef struct qkd_handle_t { | ||
int id; | ||
bool is_open; | ||
chunk_t key; | ||
chunk_t key_id; | ||
} *qkd_handle_t; | ||
|
||
// Declare all functions | ||
bool qkd_open(qkd_handle_t *handle); | ||
bool qkd_close(qkd_handle_t handle); | ||
bool qkd_get_key(qkd_handle_t handle, chunk_t *key); | ||
bool qkd_get_key_id(qkd_handle_t handle, chunk_t *key_id); | ||
bool qkd_set_key_id(qkd_handle_t handle, chunk_t key_id); | ||
bool qkd_generate_random_key_id(chunk_t *key_id); | ||
void qkd_print_key(const char *prefix, chunk_t key); | ||
void qkd_print_key_id(const char *prefix, chunk_t key_id); | ||
|
||
#endif |
Oops, something went wrong.