0
0
mirror of https://github.com/cjdelisle/cjdns synced 2025-10-06 00:32:50 +02:00

Crypto: Switch from calling directly to libsodium to calling into Rust code first. Fixes IDE errors, and libsodium will be replaced Eventually™

This commit is contained in:
Caleb James DeLisle
2024-09-19 12:44:15 +00:00
parent 669544d3ee
commit bae1708439
7 changed files with 41 additions and 18 deletions

View File

@@ -15,8 +15,7 @@
#include "util/Bits.h"
#include "util/Endian.h"
#include "crypto/AddressCalc.h"
#include <sodium/crypto_hash_sha512.h>
#include "rust/cjdns_sys/Rffi.h"
#include <stdint.h>
#include <stdbool.h>
@@ -49,9 +48,9 @@ void AddressCalc_makeValidAddress(uint8_t address[16])
bool AddressCalc_addressForPublicKey(uint8_t addressOut[16], const uint8_t key[32])
{
uint8_t hash[crypto_hash_sha512_BYTES];
crypto_hash_sha512(hash, key, 32);
crypto_hash_sha512(hash, hash, crypto_hash_sha512_BYTES);
uint8_t hash[64];
Rffi_crypto_hash_sha512(hash, key, 32);
Rffi_crypto_hash_sha512(hash, hash, sizeof hash);
if (addressOut) {
Bits_memcpy(addressOut, hash, 16);
}

View File

@@ -56,7 +56,7 @@ Linker_require("crypto/sign/sc_muladd.c")
Linker_require("crypto/sign/sc_reduce.c")
Linker_require("crypto/sign/open.c")
#include <sodium/crypto_hash_sha512.h>
#include "rust/cjdns_sys/Rffi.h"
// This is fairly streight forward, we're taking a curve25519 private key and
// interpreting it as an ed25519 key. This works in conjunction with the public
@@ -131,7 +131,7 @@ void Sign_signMsg(uint8_t keyPair[64], Message_t* msg, struct Random* rand)
// the hash of the secret key that is input to it.
Bits_memcpy(az, keyPair, 32);
Random_bytes(rand, &az[32], 32);
crypto_hash_sha512(az,az,64);
Rffi_crypto_hash_sha512(az,az,64);
// Ok, now az contains 64 bytes of unique random value, the upper 32 bytes needs to
// be set to the actual secret key that we're going to use for signing.
@@ -146,7 +146,7 @@ void Sign_signMsg(uint8_t keyPair[64], Message_t* msg, struct Random* rand)
// hash message + secret number, this is the same as crypto_sign()
// If there isn't enough space in the message, we abort the process
Err_assert(Message_epush(msg, &az[32], 32));
crypto_hash_sha512(r, Message_bytes(msg), Message_getLength(msg));
Rffi_crypto_hash_sha512(r, Message_bytes(msg), Message_getLength(msg));
// Replace secret number with public key, this is the same as crypto_sign()
Bits_memcpy(Message_bytes(msg), &keyPair[32], 32);
@@ -162,7 +162,7 @@ void Sign_signMsg(uint8_t keyPair[64], Message_t* msg, struct Random* rand)
// This final step is the same as crypto_sign()
// Overwrite the public key which the verifier will replace in order to recompute
// the hash.
crypto_hash_sha512(hram, Message_bytes(msg), Message_getLength(msg));
Rffi_crypto_hash_sha512(hram, Message_bytes(msg), Message_getLength(msg));
sc_reduce(hram);
sc_muladd(&Message_bytes(msg)[32], hram, az, r);
}

View File

@@ -17,7 +17,7 @@
#include "util/log/Log.h"
#include "util/Bits.h"
#include <sodium/crypto_hash_sha512.h>
#include "rust/cjdns_sys/Rffi.h"
struct RandomSeed_pvt
{
@@ -48,7 +48,7 @@ static int get(RandomSeed_t* rs, uint64_t buffer[8])
for (int i = 0; i < ctx->rsCount; i++) {
if (!ctx->rsList[i]->get(ctx->rsList[i], buff.input)) {
Log_info(ctx->logger, "Trying random seed [%s] Success", ctx->rsList[i]->name);
crypto_hash_sha512((uint8_t*)buff.output,
Rffi_crypto_hash_sha512((uint8_t*)buff.output,
(uint8_t*)&buff,
RandomSeed_Buffer_SIZE);
successCount++;

View File

@@ -2,7 +2,8 @@
#include "ge.h"
#include "sc.h"
#include <sodium/crypto_hash_sha512.h>
#include "rust/cjdns_sys/Rffi.h"
#include <sodium/crypto_verify_32.h>
int crypto_sign_open(
@@ -24,7 +25,7 @@ int crypto_sign_open(
for (i = 0;i < smlen;++i) m[i] = sm[i];
for (i = 0;i < 32;++i) m[32 + i] = pk[i];
crypto_hash_sha512(h,m,smlen);
Rffi_crypto_hash_sha512(h,m,smlen);
sc_reduce(h);
ge_double_scalarmult_vartime(&R,h,&A,sm + 32);

View File

@@ -96,6 +96,10 @@ void Rffi_CryptoAuth2_stats(const RTypes_CryptoAuth2_Session_t *session,
uint32_t Rffi_CryptoAuth2_cjdnsVer(const RTypes_CryptoAuth2_Session_t *session);
int Rffi_crypto_hash_sha512(unsigned char *out,
const unsigned char *input,
unsigned long long inlen);
void Rffi_stopEventLoop(Rffi_EventLoop *event_loop);
void Rffi_startEventLoop(Rffi_EventLoop *event_loop);

View File

@@ -1,3 +1,8 @@
use std::sync::Arc;
use libc::{c_char, c_int, c_uchar, c_ulonglong};
use sodiumoxide::crypto::hash::sha512;
use super::allocator::file_line;
use super::{cstr, strc};
use crate::bytestring::ByteString;
@@ -10,8 +15,6 @@ use crate::external::interface::cif;
use crate::rffi::allocator;
use crate::interface::wire::message::Message;
use crate::rtypes::*;
use std::os::raw::{c_char, c_int};
use std::sync::Arc;
#[repr(C)]
pub struct Rffi_CryptoAuth2_Session_t {
@@ -284,3 +287,20 @@ pub unsafe extern "C" fn Rffi_CryptoAuth2_cjdnsVer(
) -> u32 {
ffi_sess(session).s.cjdns_ver()
}
#[no_mangle]
pub unsafe extern "C" fn Rffi_crypto_hash_sha512(
out: *mut c_uchar, // Output buffer (hash result)
input: *const c_uchar, // Input buffer (data to hash)
inlen: c_ulonglong // Length of input data
) -> c_int {
let input_slice = std::slice::from_raw_parts(input, inlen as usize);
// Perform the SHA-512 hash
let hash = sha512::hash(input_slice);
// Copy the result to the output buffer
std::ptr::copy_nonoverlapping(hash.0.as_ptr(), out, hash.0.len());
0 // Success
}

View File

@@ -23,8 +23,7 @@
#include "util/AddrTools.h"
#include "util/Hex.h"
#include "util/Hash.h"
#include <sodium/crypto_hash_sha512.h>
#include "rust/cjdns_sys/Rffi.h"
#include <inttypes.h>
@@ -110,7 +109,7 @@ static void hashMsgList(struct ArrayList_OfMessages* msgList, uint8_t out[64])
for (int i = 0; i < msgList->length; i++) {
Message_t* msg = ArrayList_OfMessages_get(msgList, i);
Err_assert(Message_epush(msg, hash, 64));
crypto_hash_sha512(hash, Message_bytes(msg), Message_getLength(msg));
Rffi_crypto_hash_sha512(hash, Message_bytes(msg), Message_getLength(msg));
Err_assert(Message_epop(msg, NULL, 64));
}
Bits_memcpy(out, hash, 64);