Skip to content

Commit

Permalink
dawn: fix crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
HiGarfield committed Jan 18, 2025
1 parent bf03214 commit 93fc49e
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions package/extra/dawn/patches/0004-fix-crypto.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
From 1cdd6790533aa1245ff9f995f26b53d9d057f0a2 Mon Sep 17 00:00:00 2001
From: HiGarfield <HiGarfield@126.com>
Date: Sat, 18 Jan 2025 22:23:53 +0800
Subject: [PATCH] fix crypto

---
src/crypto/crypto.c | 32 ++++++++++++++++++++++----------
1 file changed, 22 insertions(+), 10 deletions(-)

--- a/src/crypto/crypto.c
+++ b/src/crypto/crypto.c
@@ -2,6 +2,8 @@
// https://github.com/vedantk/gcrypt-example/blob/master/gcry.cc

#include <gcrypt.h>
+#include <stdlib.h>
+#include <string.h>

#include "utils.h"
#include "memory_utils.h"
@@ -16,6 +18,7 @@ gcry_cipher_hd_t gcry_cipher_hd;
void gcrypt_init() {
if (!gcry_check_version(GCRYPT_VERSION)) {
dawnlog_error("gcrypt: library version mismatch");
+ abort();
}
gcry_error_t err = 0;
err = gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
@@ -25,6 +28,7 @@ void gcrypt_init() {

if (err) {
dawnlog_error("gcrypt: failed initialization");
+ abort();
}
}

@@ -63,19 +67,24 @@ void gcrypt_set_key_and_iv(const char *k

// free out buffer after using!
char *gcrypt_encrypt_msg(char *msg, size_t msg_length, int *out_length) {
- if (0U != (msg_length & 0xfU))
- msg_length += 0x10U - (msg_length & 0xfU);
+ size_t orig_msg_length = msg_length;
+ size_t block_size = gcry_cipher_get_algo_blklen(GCRY_CIPHER);
+ msg_length = msg_length + block_size - msg_length % block_size;

- char *out = dawn_malloc(msg_length);
+ char *out = dawn_calloc(1, msg_length);
if (!out){
dawnlog_error("gcry_cipher_encrypt error: not enough memory\n");
return NULL;
}
- gcry_error_handle = gcry_cipher_encrypt(gcry_cipher_hd, out, msg_length, msg, msg_length);
+
+ memcpy(out, msg, orig_msg_length);
+
+ gcry_error_handle = gcry_cipher_encrypt(gcry_cipher_hd, out, msg_length, NULL, 0);
if (gcry_error_handle) {
dawnlog_error("gcry_cipher_encrypt failed: %s/%s\n",
gcry_strsource(gcry_error_handle),
gcry_strerror(gcry_error_handle));
+ dawn_free(out);
return NULL;
}
*out_length = msg_length;
@@ -84,15 +93,19 @@ char *gcrypt_encrypt_msg(char *msg, size

// free out buffer after using!
char *gcrypt_decrypt_msg(char *msg, size_t msg_length) {
- if (0U != (msg_length & 0xfU))
- msg_length += 0x10U - (msg_length & 0xfU);
+ size_t orig_msg_length = msg_length;
+ size_t block_size = gcry_cipher_get_algo_blklen(GCRY_CIPHER);
+ msg_length = msg_length + block_size - msg_length % block_size;

- char *out_buffer = dawn_malloc(msg_length);
+ char *out_buffer = dawn_calloc(1, msg_length);
if (!out_buffer){
dawnlog_error("gcry_cipher_decrypt error: not enough memory\n");
return NULL;
}
- gcry_error_handle = gcry_cipher_decrypt(gcry_cipher_hd, out_buffer, msg_length, msg, msg_length);
+
+ memcpy(out_buffer, msg, orig_msg_length);
+
+ gcry_error_handle = gcry_cipher_decrypt(gcry_cipher_hd, out_buffer, msg_length, NULL, 0);
if (gcry_error_handle) {
dawnlog_error("gcry_cipher_decrypt failed: %s/%s\n",
gcry_strsource(gcry_error_handle),
@@ -101,16 +114,15 @@ char *gcrypt_decrypt_msg(char *msg, size
out_buffer = NULL;
return NULL;
}
+ out_buffer[orig_msg_length] = '\0';
char *out = dawn_malloc(strlen(out_buffer) + 1);
if (!out){
dawn_free(out_buffer);
- out_buffer = NULL;
dawnlog_error("gcry_cipher_decrypt error: not enough memory\n");
return NULL;
}
strcpy(out, out_buffer);
dawn_free(out_buffer);
- out_buffer = NULL;
return out;
}

0 comments on commit 93fc49e

Please sign in to comment.