1
0
Fork 0
forked from fun/fun

Added LibreSSL as an alternative to OpenSSL. Needs more testing! (0.38.16)

This commit is contained in:
Johannes Findeisen 2026-02-19 23:33:00 +01:00
commit 3e0d47a268
35 changed files with 677 additions and 38 deletions

View file

@ -173,6 +173,10 @@ static const char *opcode_name(OpCode op) {
case OP_OPENSSL_SHA256: return "OPENSSL_SHA256";
case OP_OPENSSL_SHA512: return "OPENSSL_SHA512";
case OP_OPENSSL_RIPEMD160: return "OPENSSL_RIPEMD160";
case OP_LIBRESSL_MD5: return "LIBRESSL_MD5";
case OP_LIBRESSL_SHA256: return "LIBRESSL_SHA256";
case OP_LIBRESSL_SHA512: return "LIBRESSL_SHA512";
case OP_LIBRESSL_RIPEMD160: return "LIBRESSL_RIPEMD160";
case OP_INI_LOAD: return "INI_LOAD";
case OP_INI_FREE: return "INI_FREE";
case OP_INI_GET_STRING: return "INI_GET_STRING";

View file

@ -187,6 +187,12 @@ typedef enum {
OP_OPENSSL_SHA512, // pops data string; pushes sha512 hex string
OP_OPENSSL_RIPEMD160, // pops data string; pushes ripemd160 hex string
// LibreSSL (optional; same API as OpenSSL but different toggle)
OP_LIBRESSL_MD5, // pops data string; pushes md5 hex string
OP_LIBRESSL_SHA256, // pops data string; pushes sha256 hex string
OP_LIBRESSL_SHA512, // pops data string; pushes sha512 hex string
OP_LIBRESSL_RIPEMD160, // pops data string; pushes ripemd160 hex string
// INI (iniparser 4.2.6) optional
OP_INI_LOAD, // pops path; pushes handle (>0) or 0
OP_INI_FREE, // pops handle; pushes 1/0

172
src/external/libressl.c vendored Normal file
View file

@ -0,0 +1,172 @@
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-02-19
*/
/*
* LibreSSL integration helpers (MD5, RIPEMD-160, SHA-256, SHA-512)
*/
#ifdef FUN_WITH_LIBRESSL
/*
* When building with LibreSSL, include the LibreSSL-provided headers explicitly.
* Downstream distros often ship them under /usr/include/libressl/openssl/
* and expect the include path to point at /usr/include/libressl so that any
* nested `#include <openssl/...>` inside these headers resolves to the
* corresponding LibreSSL headers as well (not the system OpenSSL 3.x ones).
*/
#include <libressl/openssl/evp.h>
/* Compatibility: Prefer EVP_MD_get_size universally. If LibreSSL headers
* don't declare it, provide a forward declaration so we can link against
* OpenSSL's libcrypto symbol when that is what CMake found. */
#ifndef EVP_MD_get_size
int EVP_MD_get_size(const EVP_MD *md);
#endif
#endif
#include <stdlib.h>
/* Compute MD5 hex of input buffer; returns malloc'ed C string (lowercase hex). */
static char *fun_libressl_md5_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;
#ifdef FUN_WITH_LIBRESSL
const EVP_MD *md = EVP_md5();
if (!md) return NULL;
int dlen = EVP_MD_get_size(md);
if (dlen <= 0) return NULL;
unsigned char *digest = (unsigned char*)malloc((size_t)dlen);
if (!digest) return NULL;
unsigned int out_len = 0;
int ok;
if (len == 0) {
ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL);
} else {
ok = EVP_Digest(data, len, digest, &out_len, md, NULL);
}
if (ok != 1 || (int)out_len != dlen) { free(digest); return NULL; }
char *hex = (char*)malloc((size_t)dlen * 2 + 1);
if (!hex) { free(digest); return NULL; }
for (int i = 0; i < dlen; ++i) {
hex[2*i] = hexdig[(digest[i] >> 4) & 0xF];
hex[2*i+1] = hexdig[digest[i] & 0xF];
}
hex[dlen * 2] = '\0';
free(digest);
return hex;
#else
char *hex = (char*)malloc(1);
if (hex) hex[0] = '\0';
return hex;
#endif
}
/* Compute SHA-256 hex of input buffer; returns malloc'ed C string (lowercase hex). */
static char *fun_libressl_sha256_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;
#ifdef FUN_WITH_LIBRESSL
const EVP_MD *md = EVP_sha256();
if (!md) return NULL;
int dlen = EVP_MD_get_size(md);
if (dlen <= 0) return NULL;
unsigned char *digest = (unsigned char*)malloc((size_t)dlen);
if (!digest) return NULL;
unsigned int out_len = 0;
int ok;
if (len == 0) {
ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL);
} else {
ok = EVP_Digest(data, len, digest, &out_len, md, NULL);
}
if (ok != 1 || (int)out_len != dlen) { free(digest); return NULL; }
char *hex = (char*)malloc((size_t)dlen * 2 + 1);
if (!hex) { free(digest); return NULL; }
for (int i = 0; i < dlen; ++i) {
hex[2*i] = hexdig[(digest[i] >> 4) & 0xF];
hex[2*i+1] = hexdig[digest[i] & 0xF];
}
hex[dlen * 2] = '\0';
free(digest);
return hex;
#else
char *hex = (char*)malloc(1);
if (hex) hex[0] = '\0';
return hex;
#endif
}
/* Compute SHA-512 hex of input buffer; returns malloc'ed C string (lowercase hex). */
static char *fun_libressl_sha512_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;
#ifdef FUN_WITH_LIBRESSL
const EVP_MD *md = EVP_sha512();
if (!md) return NULL;
int dlen = EVP_MD_get_size(md);
if (dlen <= 0) return NULL;
unsigned char *digest = (unsigned char*)malloc((size_t)dlen);
if (!digest) return NULL;
unsigned int out_len = 0;
int ok;
if (len == 0) {
ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL);
} else {
ok = EVP_Digest(data, len, digest, &out_len, md, NULL);
}
if (ok != 1 || (int)out_len != dlen) { free(digest); return NULL; }
char *hex = (char*)malloc((size_t)dlen * 2 + 1);
if (!hex) { free(digest); return NULL; }
for (int i = 0; i < dlen; ++i) {
hex[2*i] = hexdig[(digest[i] >> 4) & 0xF];
hex[2*i+1] = hexdig[digest[i] & 0xF];
}
hex[dlen * 2] = '\0';
free(digest);
return hex;
#else
char *hex = (char*)malloc(1);
if (hex) hex[0] = '\0';
return hex;
#endif
}
/* Compute RIPEMD-160 hex of input buffer; returns malloc'ed C string (lowercase hex). */
static char *fun_libressl_ripemd160_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;
#ifdef FUN_WITH_LIBRESSL
const EVP_MD *md = EVP_ripemd160();
if (!md) return NULL;
int dlen = EVP_MD_get_size(md);
if (dlen <= 0) return NULL;
unsigned char *digest = (unsigned char*)malloc((size_t)dlen);
if (!digest) return NULL;
unsigned int out_len = 0;
int ok;
if (len == 0) {
ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL);
} else {
ok = EVP_Digest(data, len, digest, &out_len, md, NULL);
}
if (ok != 1 || (int)out_len != dlen) { free(digest); return NULL; }
char *hex = (char*)malloc((size_t)dlen * 2 + 1);
if (!hex) { free(digest); return NULL; }
for (int i = 0; i < dlen; ++i) {
hex[2*i] = hexdig[(digest[i] >> 4) & 0xF];
hex[2*i+1] = hexdig[digest[i] & 0xF];
}
hex[dlen * 2] = '\0';
free(digest);
return hex;
#else
char *hex = (char*)malloc(1);
if (hex) hex[0] = '\0';
return hex;
#endif
}

View file

@ -15,6 +15,11 @@
#ifdef FUN_WITH_OPENSSL
#include <openssl/evp.h>
/* Always use EVP_MD_get_size; if headers don't declare it, provide a
* forward declaration to allow linking against OpenSSL libcrypto. */
#ifndef EVP_MD_get_size
int EVP_MD_get_size(const EVP_MD *md);
#endif
#endif
#include <stdlib.h>

View file

@ -1230,6 +1230,39 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
free(name);
return 1;
}
/* LibreSSL (md5/sha256/sha512/ripemd160) */
if (strcmp(name, "libressl_md5") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "libressl_md5 expects (data)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after libressl_md5 arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_LIBRESSL_MD5, 0);
free(name);
return 1;
}
if (strcmp(name, "libressl_sha256") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "libressl_sha256 expects (data)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after libressl_sha256 arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_LIBRESSL_SHA256, 0);
free(name);
return 1;
}
if (strcmp(name, "libressl_sha512") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "libressl_sha512 expects (data)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after libressl_sha512 arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_LIBRESSL_SHA512, 0);
free(name);
return 1;
}
if (strcmp(name, "libressl_ripemd160") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "libressl_ripemd160 expects (data)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after libressl_ripemd160 arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_LIBRESSL_RIPEMD160, 0);
free(name);
return 1;
}
/* PCSC builtins */
if (strcmp(name, "pcsc_establish") == 0) {
(*pos)++; /* '(' */

View file

@ -74,6 +74,7 @@
#include "external/tcltk.c"
#include "external/xml2.c"
#include "external/openssl.c"
#include "external/libressl.c"
/* forward declarations for include mapping used in error reporting */
extern char *preprocess_includes(const char *src);
@ -870,10 +871,20 @@ void vm_run(VM *vm, Bytecode *entry) {
#endif
/* OpenSSL ops (md5/sha256/sha512/ripemd160) */
#ifdef FUN_WITH_OPENSSL
#include "vm/openssl/md5.c"
#include "vm/openssl/sha256.c"
#include "vm/openssl/sha512.c"
#include "vm/openssl/ripemd160.c"
#endif
/* LibreSSL ops (md5/sha256/sha512/ripemd160) */
#ifdef FUN_WITH_LIBRESSL
#include "vm/libressl/md5.c"
#include "vm/libressl/sha256.c"
#include "vm/libressl/sha512.c"
#include "vm/libressl/ripemd160.c"
#endif
/* Tk (Tcl/Tk) ops */
#ifdef FUN_WITH_TCLTK

27
src/vm/libressl/md5.c Normal file
View file

@ -0,0 +1,27 @@
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-02-19
*/
/**
* LibreSSL MD5 builtin
*/
case OP_LIBRESSL_MD5: {
Value vdata = pop_value(vm);
char *s = value_to_string_alloc(&vdata);
free_value(vdata);
if (!s) { push_value(vm, make_string("")); break; }
char *hex = fun_libressl_md5_hex((const unsigned char*)s, strlen(s));
free(s);
if (!hex) { push_value(vm, make_string("")); break; }
Value out = make_string(hex);
free(hex);
push_value(vm, out);
break;
}

View file

@ -0,0 +1,27 @@
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-02-19
*/
/**
* LibreSSL RIPEMD-160 builtin
*/
case OP_LIBRESSL_RIPEMD160: {
Value vdata = pop_value(vm);
char *s = value_to_string_alloc(&vdata);
free_value(vdata);
if (!s) { push_value(vm, make_string("")); break; }
char *hex = fun_libressl_ripemd160_hex((const unsigned char*)s, strlen(s));
free(s);
if (!hex) { push_value(vm, make_string("")); break; }
Value out = make_string(hex);
free(hex);
push_value(vm, out);
break;
}

27
src/vm/libressl/sha256.c Normal file
View file

@ -0,0 +1,27 @@
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-02-19
*/
/**
* LibreSSL SHA-256 builtin
*/
case OP_LIBRESSL_SHA256: {
Value vdata = pop_value(vm);
char *s = value_to_string_alloc(&vdata);
free_value(vdata);
if (!s) { push_value(vm, make_string("")); break; }
char *hex = fun_libressl_sha256_hex((const unsigned char*)s, strlen(s));
free(s);
if (!hex) { push_value(vm, make_string("")); break; }
Value out = make_string(hex);
free(hex);
push_value(vm, out);
break;
}

27
src/vm/libressl/sha512.c Normal file
View file

@ -0,0 +1,27 @@
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-02-19
*/
/**
* LibreSSL SHA-512 builtin
*/
case OP_LIBRESSL_SHA512: {
Value vdata = pop_value(vm);
char *s = value_to_string_alloc(&vdata);
free_value(vdata);
if (!s) { push_value(vm, make_string("")); break; }
char *hex = fun_libressl_sha512_hex((const unsigned char*)s, strlen(s));
free(s);
if (!hex) { push_value(vm, make_string("")); break; }
Value out = make_string(hex);
free(hex);
push_value(vm, out);
break;
}