-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf03214
commit 24145a0
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
From: HiGarfield <HiGarfield@126.com> | ||
Date: Sat, 18 Jan 2025 22:23:53 +0800 | ||
Subject: [PATCH] fix crypto | ||
|
||
--- | ||
src/crypto/crypto.c | 49 ++++++++++++++++++++++++--------------------- | ||
1 file changed, 26 insertions(+), 23 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,54 +67,53 @@ 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 block_size = gcry_cipher_get_algo_blklen(GCRY_CIPHER); | ||
+ size_t padded_length = msg_length + block_size - msg_length % block_size; | ||
|
||
- char *out = dawn_malloc(msg_length); | ||
- if (!out){ | ||
+ char *out = dawn_calloc(1, padded_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, msg_length); | ||
+ | ||
+ gcry_error_handle = gcry_cipher_encrypt(gcry_cipher_hd, out, padded_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; | ||
+ | ||
+ *out_length = padded_length; | ||
return out; | ||
} | ||
|
||
// 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 block_size = gcry_cipher_get_algo_blklen(GCRY_CIPHER); | ||
+ size_t padded_length = msg_length + block_size - msg_length % block_size; | ||
|
||
- char *out_buffer = dawn_malloc(msg_length); | ||
- if (!out_buffer){ | ||
+ char *out = dawn_calloc(1, padded_length); | ||
+ if (!out) { | ||
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, msg, msg_length); | ||
+ | ||
+ gcry_error_handle = gcry_cipher_decrypt(gcry_cipher_hd, out, padded_length, NULL, 0); | ||
if (gcry_error_handle) { | ||
dawnlog_error("gcry_cipher_decrypt failed: %s/%s\n", | ||
gcry_strsource(gcry_error_handle), | ||
gcry_strerror(gcry_error_handle)); | ||
- dawn_free(out_buffer); | ||
- out_buffer = NULL; | ||
+ dawn_free(out); | ||
return NULL; | ||
} | ||
- 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; | ||
+ | ||
+ out[msg_length] = '\0'; | ||
return out; | ||
} | ||
|