-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch by: michaelortmann Useful for tcl scripts that add SASL SCRAM mechanism which, when implemented in Tcl, are very slow. This PR will help with a fast replacement function exported by eggdrop. The new tcl function pbkdf2() returns as hexadecimal string by default and -bin by option, which is similar, to what tcllibs sha256() does (older tcllibs md5 had it the other way around), see https://core.tcl-lang.org/tcllib/doc/trunk/embedded/md/tcllib/files/modules/sha1/sha256.md
- Loading branch information
1 parent
1e47285
commit 6649e8d
Showing
3 changed files
with
92 additions
and
16 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
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
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,70 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-only */ | ||
/* | ||
* tclpbkdf2.c -- tcl functions for pbkdf2.mod | ||
* | ||
* Written by thommey and Michael Ortmann | ||
* | ||
* Copyright (C) 2017 - 2024 Eggheads Development Team | ||
*/ | ||
|
||
#include <openssl/err.h> | ||
#include <string.h> | ||
|
||
static char *pbkdf2_encrypt(const char *); | ||
|
||
static int tcl_encpass2 STDVAR | ||
{ | ||
BADARGS(2, 2, " string"); | ||
Tcl_SetResult(irp, pbkdf2_encrypt(argv[1]), TCL_STATIC); | ||
return TCL_OK; | ||
} | ||
|
||
static int tcl_pbkdf2 STDVAR | ||
{ | ||
int hex, digestlen, i; | ||
unsigned int rounds; | ||
const EVP_MD *digest; | ||
unsigned char buf[256]; | ||
char buf_hex[256]; | ||
Tcl_Obj *result = 0; | ||
|
||
BADARGS(5, 6, " ?-bin? pass salt rounds digest"); | ||
if (argc == 6) { | ||
if (!strcmp(argv[1], "-bin")) | ||
hex = 0; | ||
else { | ||
Tcl_AppendResult(irp, "bad option ", argv[1], ": must be -bin", NULL); | ||
return TCL_ERROR; | ||
} | ||
} | ||
else | ||
hex = 1; | ||
rounds = atoi(argv[3 + !hex]); | ||
digest = EVP_get_digestbyname(argv[4 + !hex]); | ||
if (!digest) { | ||
Tcl_AppendResult(irp, "PBKDF2 error: Unknown message digest '", argv[4 + !hex], "'.", NULL); | ||
return TCL_ERROR; | ||
} | ||
digestlen = EVP_MD_size(digest); | ||
if (!PKCS5_PBKDF2_HMAC(argv[1 + !hex], strlen(argv[1 + !hex]), (const unsigned char *) argv[2+ !hex], strlen(argv[2 + !hex]), rounds, digest, digestlen, buf)) { | ||
Tcl_AppendResult(irp, "PBKDF2 key derivation error: ", ERR_error_string(ERR_get_error(), NULL), ".", NULL); | ||
return TCL_ERROR; | ||
} | ||
if (hex) { | ||
for (i = 0; i < digestlen; i++) | ||
sprintf(buf_hex + (i * 2), "%.2X", buf[i]); | ||
result = Tcl_NewByteArrayObj((unsigned char *) buf_hex, digestlen * 2); | ||
explicit_bzero(buf_hex, digestlen * 2); | ||
} | ||
else | ||
result = Tcl_NewByteArrayObj(buf, digestlen); | ||
explicit_bzero(buf, digestlen); | ||
Tcl_SetObjResult(irp, result); | ||
return TCL_OK; | ||
} | ||
|
||
static tcl_cmds my_tcl_cmds[] = { | ||
{"encpass2", tcl_encpass2}, | ||
{"pbkdf2", tcl_pbkdf2}, | ||
{NULL, NULL} | ||
}; |