commit a2ba4134748996e8a3603bd43a897aac7986bcc1
parent 00468ff84665a6fabf1678db2461fa8eece6a675
Author: lash <dev@holbrook.no>
Date: Sat, 1 Mar 2025 17:24:23 +0000
Implement msg handling, less digest
Diffstat:
5 files changed, 67 insertions(+), 13 deletions(-)
diff --git a/src/crypto/dummy.c b/src/crypto/dummy.c
@@ -64,9 +64,9 @@ LQPubKey* lq_publickey_from_privatekey(LQPrivKey *pk) {
return pubk;
}
-LQSig* lq_privatekey_sign(LQPrivKey *pk, char *msg, size_t msg_len, const char *salt, size_t salt_len) {
+LQSig* lq_privatekey_sign(LQPrivKey *pk, const char *msg, size_t msg_len, const char *salt, size_t salt_len) {
int i;
- char *src;
+ const char *src;
char *dst;
LQSig *sig;
diff --git a/src/lq/crypto.h b/src/lq/crypto.h
@@ -27,7 +27,7 @@ typedef struct lq_signature_t LQSig;
LQPrivKey* lq_privatekey_new(char *seed, size_t seed_len);
LQPubKey* lq_publickey_new(char *full, size_t full_len);
LQPubKey* lq_publickey_from_privatekey(LQPrivKey *pk);
-LQSig* lq_privatekey_sign(LQPrivKey *pk, char *msg, size_t msg_len, const char *salt, size_t salt_len);
+LQSig* lq_privatekey_sign(LQPrivKey *pk, const char *msg, size_t msg_len, const char *salt, size_t salt_len);
void lq_publickey_free(LQPubKey *pubk);
void lq_privatekey_free(LQPrivKey *pk);
void lq_signature_free(LQSig *sig);
diff --git a/src/lq/msg.c b/src/lq/msg.c
@@ -0,0 +1,47 @@
+#include <time.h>
+
+#include "lq/msg.h"
+#include "lq/mem.h"
+#include "lq/crypto.h"
+
+
+LQMsg* lq_msg_new(const char *msg_data, size_t msg_len) {
+ LQMsg *msg;
+
+ msg = lq_alloc(sizeof(LQMsg));
+ lq_set(msg, 0, sizeof(LQMsg));
+ msg->timestamp = (int)time(NULL);
+
+ return msg;
+}
+
+void lq_msg_set_domain(LQMsg *msg, const char *domain) {
+ lq_cpy(msg->domain, (void*)domain, LQ_MSG_DOMAIN_LEN);
+}
+
+int lq_msg_sign(LQMsg *msg, LQPrivKey *pk) {
+ return lq_msg_sign_salted(msg, pk, 0, 0);
+}
+
+int lq_msg_sign_salted(LQMsg *msg, LQPrivKey *pk, const char *salt, size_t salt_len) {
+ char *data;
+
+ data = lq_alloc(LQ_MSG_DOMAIN_LEN + msg->len);
+ lq_cpy(data, msg->domain, LQ_MSG_DOMAIN_LEN);
+ lq_cpy(data + LQ_MSG_DOMAIN_LEN, msg->data, msg->len);
+ msg->pubkey = lq_publickey_from_privatekey(pk);
+
+ // TODO: digest msg
+ msg->signature = lq_privatekey_sign(pk, msg->data, msg->len, salt, salt_len);
+ return 0;
+}
+
+void lq_msg_free(LQMsg *msg) {
+ if (msg->pubkey != 0) {
+ lq_free(msg->pubkey);
+ }
+ if (msg->signature != 0) {
+ lq_free(msg->signature);
+ }
+ lq_free(msg);
+}
diff --git a/src/lq/msg.h b/src/lq/msg.h
@@ -5,16 +5,23 @@
#include "lq/crypto.h"
+#ifndef LQ_MSG_DOMAIN_LEN
+#define LQ_MSG_DOMAIN_LEN 8
+#endif
+
struct lq_msg_t {
- const char *msg_data;
- size_t msg_len;
- const char *msg_domain;
- size_t *msg_domain_len;
- int msg_timestamp;
- struct LQPubKey *msg_pubkey;
- struct LQSig *msg_signature;
+ const char *data;
+ size_t len;
+ char domain[LQ_MSG_DOMAIN_LEN];
+ int timestamp;
+ LQPubKey *pubkey;
+ LQSig *signature;
};
typedef struct lq_msg_t LQMsg;
+LQMsg* lq_msg_new(const char *msg_data, size_t msg_len);
+void lq_msg_set_domain(LQMsg *msg, const char *domain);
+int lq_msg_sign(LQMsg *msg, LQPrivKey *pk);
+int lq_msg_sign_salted(LQMsg *msg, LQPrivKey *pk, const char *salt, size_t salt_len);
+void lq_msg_free(LQMsg *msg);
#endif // LIBQAEDA_MSG_H_
-
diff --git a/src/mem/std.c b/src/mem/std.c
@@ -10,10 +10,10 @@ void lq_free(void *o) {
free(o);
}
-void* lq_cpy(void *dst, void *src, size_t len) {
+void* lq_cpy(void *dst, const void *src, size_t len) {
return memcpy(dst, src, len);
}
-void* lq_set(void *dst, char b, size_t len) {
+void* lq_set(void *dst, const char b, size_t len) {
return memset(dst, (int)b, len);
}