- ext/liboqs: vendored (ML-KEM/ML-DSA ready), -isystem ext/liboqs/include - node/PQHybrid.*: KDF over X25519 agree + ML-KEM-768 encaps scaffold - wire into IncomingPacket/Node/Bond/selftest/OneService paths - zeroidc ext.rs: URL param parser unit tests - SPEC.md: BackOne quantum-safe task table (T1–T23) - research doc: hybrid PQ fork plan (ML-KEM+X25519, ML-DSA+Ed25519)
90 lines
2.0 KiB
C
90 lines
2.0 KiB
C
// SPDX-License-Identifier: MIT
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/stat.h>
|
|
|
|
#define OQS_STORE_DIR "tmp"
|
|
#define OQS_STORE_PREFIX "/oqs-temp-file-"
|
|
#define MAXPATHLEN 128
|
|
|
|
#if (defined(_WIN32) || defined(__WIN32__))
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
static OQS_STATUS oqs_fstore_init(void) {
|
|
#ifndef _WIN32
|
|
int ret;
|
|
ret = mkdir(OQS_STORE_DIR, 0755);
|
|
if ((!ret) || (errno == EEXIST)) {
|
|
return OQS_SUCCESS;
|
|
} else {
|
|
return OQS_ERROR;
|
|
}
|
|
#else
|
|
if (!CreateDirectory(OQS_STORE_DIR, NULL)) {
|
|
DWORD err = GetLastError();
|
|
if (err == ERROR_ALREADY_EXISTS) {
|
|
return OQS_SUCCESS;
|
|
} else {
|
|
return OQS_ERROR;
|
|
}
|
|
} else {
|
|
return OQS_SUCCESS;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
static OQS_STATUS oqs_fstore(const char *fname, const char *mname, uint8_t *data, size_t len) {
|
|
char fpath[MAXPATHLEN];
|
|
strcpy(fpath, OQS_STORE_DIR);
|
|
strcat(fpath, OQS_STORE_PREFIX);
|
|
strcat(fpath, mname);
|
|
strcat(fpath, fname);
|
|
FILE *fp = fopen(fpath, "wb");
|
|
if (!fp) {
|
|
fprintf(stderr, "Couldn't open %s for writing.\n", fpath);
|
|
return OQS_ERROR;
|
|
}
|
|
fwrite(data, len, 1, fp);
|
|
fclose(fp);
|
|
return OQS_SUCCESS;
|
|
}
|
|
|
|
static OQS_STATUS oqs_fload(const char *fname, const char *mname, uint8_t *data, size_t len, size_t *rcvd) {
|
|
size_t len_read = 0, r = 0;
|
|
uint8_t *dr = NULL;
|
|
char fpath[MAXPATHLEN];
|
|
strcpy(fpath, OQS_STORE_DIR);
|
|
strcat(fpath, OQS_STORE_PREFIX);
|
|
strcat(fpath, mname);
|
|
strcat(fpath, fname);
|
|
FILE *fp = fopen(fpath, "rb");
|
|
if (!fp) {
|
|
fprintf(stderr, "Couldn't open %s for reading.\n", fpath);
|
|
return OQS_ERROR;
|
|
}
|
|
// assume some OSs don't deliver all data in one go...
|
|
while (len_read < len) {
|
|
size_t want = len - len_read;
|
|
dr = (uint8_t *)(data + len_read);
|
|
r = fread(dr, 1, want, fp);
|
|
len_read += r;
|
|
if (r < want) {
|
|
break;
|
|
}
|
|
}
|
|
*rcvd = len_read;
|
|
if (len_read == 0) {
|
|
fprintf(stderr, "Error reading data (operations called in proper sequence?). Expecting %zu. Exiting.\n", len);
|
|
fclose(fp);
|
|
return OQS_ERROR;
|
|
}
|
|
fclose(fp);
|
|
return OQS_SUCCESS;
|
|
}
|
|
|