Added OpenSSL RIPEMD-160 fun. (0.38.14)
This commit is contained in:
parent
38968a9a51
commit
6fa95f0d47
15 changed files with 144 additions and 9 deletions
|
|
@ -172,6 +172,7 @@ static const char *opcode_name(OpCode op) {
|
|||
case OP_OPENSSL_MD5: return "OPENSSL_MD5";
|
||||
case OP_OPENSSL_SHA256: return "OPENSSL_SHA256";
|
||||
case OP_OPENSSL_SHA512: return "OPENSSL_SHA512";
|
||||
case OP_OPENSSL_RIPEMD160: return "OPENSSL_RIPEMD160";
|
||||
case OP_INI_LOAD: return "INI_LOAD";
|
||||
case OP_INI_FREE: return "INI_FREE";
|
||||
case OP_INI_GET_STRING: return "INI_GET_STRING";
|
||||
|
|
|
|||
|
|
@ -185,6 +185,7 @@ typedef enum {
|
|||
OP_OPENSSL_MD5, // pops data string; pushes md5 hex string
|
||||
OP_OPENSSL_SHA256, // pops data string; pushes sha256 hex string
|
||||
OP_OPENSSL_SHA512, // pops data string; pushes sha512 hex string
|
||||
OP_OPENSSL_RIPEMD160, // pops data string; pushes ripemd160 hex string
|
||||
|
||||
// INI (iniparser 4.2.6) optional
|
||||
OP_INI_LOAD, // pops path; pushes handle (>0) or 0
|
||||
|
|
|
|||
44
src/external/openssl.c
vendored
44
src/external/openssl.c
vendored
|
|
@ -2,7 +2,7 @@
|
|||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
|
|
@ -10,11 +10,11 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* OpenSSL integration helpers (MD5, SHA-256, SHA-512)
|
||||
* OpenSSL integration helpers (MD5, RIPEMD-160, SHA-256, SHA-512)
|
||||
*/
|
||||
|
||||
#ifdef FUN_WITH_OPENSSL
|
||||
# include <openssl/evp.h>
|
||||
#include <openssl/evp.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
|
||||
|
|
@ -128,3 +128,41 @@ static char *fun_openssl_sha512_hex(const unsigned char *data, size_t len) {
|
|||
return hex;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Compute RIPEMD-160 hex of input buffer; returns malloc'ed C string (lowercase hex).
|
||||
* Note: On OpenSSL 3.x, RIPEMD160 may require the legacy provider; if unavailable,
|
||||
* EVP_ripemd160() can return NULL. In that case we return NULL and the VM opcode
|
||||
* will fall back to empty string behavior. When OpenSSL is disabled, return empty string. */
|
||||
static char *fun_openssl_ripemd160_hex(const unsigned char *data, size_t len) {
|
||||
static const char hexdig[] = "0123456789abcdef";
|
||||
if (!data && len != 0) return NULL;
|
||||
#ifdef FUN_WITH_OPENSSL
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1222,6 +1222,14 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
|
|||
free(name);
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(name, "openssl_ripemd160") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "openssl_ripemd160 expects (data)"); free(name); return 0; }
|
||||
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after openssl_ripemd160 arg"); free(name); return 0; }
|
||||
bytecode_add_instruction(bc, OP_OPENSSL_RIPEMD160, 0);
|
||||
free(name);
|
||||
return 1;
|
||||
}
|
||||
/* PCSC builtins */
|
||||
if (strcmp(name, "pcsc_establish") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
|
|
|
|||
3
src/vm.c
3
src/vm.c
|
|
@ -869,10 +869,11 @@ void vm_run(VM *vm, Bytecode *entry) {
|
|||
#include "vm/curl/download.c"
|
||||
#endif
|
||||
|
||||
/* OpenSSL ops (md5/sha256/sha512) */
|
||||
/* OpenSSL ops (md5/sha256/sha512/ripemd160) */
|
||||
#include "vm/openssl/md5.c"
|
||||
#include "vm/openssl/sha256.c"
|
||||
#include "vm/openssl/sha512.c"
|
||||
#include "vm/openssl/ripemd160.c"
|
||||
|
||||
/* Tk (Tcl/Tk) ops */
|
||||
#ifdef FUN_WITH_TCLTK
|
||||
|
|
|
|||
|
|
@ -1,4 +1,15 @@
|
|||
/**
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/**
|
||||
* OpenSSL MD5 builtin
|
||||
*/
|
||||
case OP_OPENSSL_MD5: {
|
||||
|
|
|
|||
16
src/vm/openssl/ripemd160.c
Normal file
16
src/vm/openssl/ripemd160.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* OpenSSL RIPEMD-160 builtin
|
||||
*/
|
||||
case OP_OPENSSL_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_openssl_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;
|
||||
}
|
||||
|
|
@ -1,3 +1,14 @@
|
|||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/**
|
||||
* OpenSSL SHA-256 builtin
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,3 +1,14 @@
|
|||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/**
|
||||
* OpenSSL SHA-512 builtin
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue