Remove dependency on base64c. Use libsodium's. Also consolidate the calls, as the usage is quite special.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4175 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-02-14 03:15:24 +00:00
parent fa87462405
commit 1f77df7a90
26 changed files with 43 additions and 771 deletions

View File

@ -8,7 +8,6 @@
#include "util.js.h"
#include <assert.h>
#include <base64c.h>
#include <quickjs.h>
#include <sodium/crypto_auth.h>
#include <sodium/crypto_box.h>
@ -884,7 +883,7 @@ void tf_ssb_calculate_message_id(JSContext* context, JSValue message, char* out_
crypto_hash_sha256(id, (uint8_t*)latin1, latin1_len);
char id_base64[k_id_base64_len];
base64c_encode(id, sizeof(id), (uint8_t*)id_base64, sizeof(id_base64));
tf_base64_encode(id, sizeof(id), id_base64, sizeof(id_base64));
snprintf(out_id, out_id_size, "%%%s.sha256", id_base64);
@ -932,11 +931,11 @@ static bool _tf_ssb_verify_and_strip_signature_internal(JSContext* context, JSVa
const char* type = strstr(author_id, ".ed25519");
uint8_t publickey[crypto_box_PUBLICKEYBYTES];
int r = base64c_decode((const uint8_t*)author_id, type - author_id, publickey, sizeof(publickey));
int r = tf_base64_decode(author_id, type - author_id, publickey, sizeof(publickey));
if (r != -1)
{
uint8_t binsig[crypto_sign_BYTES];
r = base64c_decode((const uint8_t*)str, sigkind - str, binsig, sizeof(binsig));
r = tf_base64_decode(str, sigkind - str, binsig, sizeof(binsig));
if (r != -1)
{
r = crypto_sign_verify_detached(binsig, (const uint8_t*)sigstr, strlen(sigstr), publickey);
@ -1028,7 +1027,7 @@ void tf_ssb_send_close(tf_ssb_t* ssb)
bool tf_ssb_id_bin_to_str(char* str, size_t str_size, const uint8_t* bin)
{
char buffer[k_id_base64_len - 9];
base64c_encode(bin, crypto_sign_PUBLICKEYBYTES, (uint8_t*)buffer, sizeof(buffer));
tf_base64_encode(bin, crypto_sign_PUBLICKEYBYTES, buffer, sizeof(buffer));
return snprintf(str, str_size, "@%s.ed25519", buffer) < (int)str_size;
}
@ -1036,7 +1035,7 @@ bool tf_ssb_id_str_to_bin(uint8_t* bin, const char* str)
{
const char* author_id = str && *str == '@' ? str + 1 : str;
const char* type = strstr(str, ".ed25519");
return base64c_decode((const uint8_t*)author_id, type - author_id, bin, crypto_box_PUBLICKEYBYTES) != 0;
return tf_base64_decode(author_id, type - author_id, bin, crypto_box_PUBLICKEYBYTES) != 0;
}
static void _tf_ssb_notify_connections_changed(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection)
@ -1685,7 +1684,7 @@ void tf_ssb_append_message_with_keys(tf_ssb_t* ssb, const char* author, const ui
JS_FreeValue(context, jsonval);
char signature_base64[crypto_sign_BYTES * 2];
base64c_encode(signature, sizeof(signature), (uint8_t*)signature_base64, sizeof(signature_base64));
tf_base64_encode(signature, sizeof(signature), signature_base64, sizeof(signature_base64));
strcat(signature_base64, ".sig.ed25519");
JSValue sigstr = JS_NewString(context, signature_base64);
JS_SetPropertyStr(context, root, "signature", sigstr);
@ -2170,11 +2169,11 @@ void tf_ssb_generate_keys_buffer(char* out_public, size_t public_size, char* out
uint8_t private[crypto_sign_SECRETKEYBYTES];
crypto_sign_ed25519_keypair(public, private);
uint8_t buffer[512];
base64c_encode(public, sizeof(public), buffer, sizeof(buffer));
char buffer[512];
tf_base64_encode(public, sizeof(public), buffer, sizeof(buffer));
snprintf(out_public, public_size, "%s.ed25519", buffer);
base64c_encode(private, sizeof(private), buffer, sizeof(buffer));
tf_base64_encode(private, sizeof(private), buffer, sizeof(buffer));
snprintf(out_private, private_size, "%s.ed25519", buffer);
}
@ -2601,7 +2600,7 @@ static void _tf_ssb_send_broadcast(tf_ssb_t* ssb, struct sockaddr_in* address)
}
char fullid[k_id_base64_len];
base64c_encode(ssb->pub, sizeof(ssb->pub), (uint8_t*)fullid, sizeof(fullid));
tf_base64_encode(ssb->pub, sizeof(ssb->pub), fullid, sizeof(fullid));
char message[512];
snprintf(message, sizeof(message), "net:%s:%d~shs:%s", address_str, ntohs(((struct sockaddr_in*)&server_addr)->sin_port), fullid);
@ -2700,7 +2699,7 @@ static bool _tf_ssb_parse_broadcast(const char* in_broadcast, tf_ssb_broadcast_t
{
out_broadcast->addr.sin_family = AF_INET;
out_broadcast->addr.sin_port = htons((uint16_t)port);
int r = base64c_decode((const uint8_t*)public_key_str, strlen(public_key_str), out_broadcast->pub, crypto_sign_PUBLICKEYBYTES);
int r = tf_base64_decode(public_key_str, strlen(public_key_str), out_broadcast->pub, crypto_sign_PUBLICKEYBYTES);
return r != -1;
}
else if (strncmp(in_broadcast, "ws:", 3) == 0)