Skip to content

Commit

Permalink
Skip a level of indirection in inline BCrypt, and avoid clobbering th…
Browse files Browse the repository at this point in the history
…e stack in some cases.
  • Loading branch information
Alex Cole committed Apr 24, 2024
1 parent 8938b7e commit 755bd94
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 54 deletions.
99 changes: 55 additions & 44 deletions YSI_Extra/y_bcrypt/y_bcrypt_entry.inc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Optional plugins:
#include "..\y_bcrypt\y_bcrypt_natives"

forward _YSI_BCryptCheckDone(extra, ptr);
forward _YSI_BCryptHashDone(hashid, ptr, extra);
forward _YSI_BCryptHashDone(hashid, arg2, arg3);

static stock const
YSI_gscProp1[] = "_@yT%d",
Expand Down Expand Up @@ -110,8 +110,11 @@ public _YSI_BCryptCheckDone(extra, ptr)
#emit RETN
}

public _YSI_BCryptHashDone(hashid, ptr, extra)
public _YSI_BCryptHashDone(hashid, arg2, arg3)
{
new
ptr,
extra;
switch (numargs())
{
case 1:
Expand All @@ -131,7 +134,7 @@ public _YSI_BCryptHashDone(hashid, ptr, extra)
// V1.
new
prop[16];
_YSI_gLastHash = ptr,
_YSI_gLastHash = arg2,
format(prop, sizeof (prop), YSI_gscProp1, hashid),
extra = getproperty(0, prop),
deleteproperty(0, prop),
Expand All @@ -152,6 +155,8 @@ public _YSI_BCryptHashDone(hashid, ptr, extra)
deleteproperty(0, prop),
BCrypt_SetPlugin(BCRYPT_PLUGIN_VERSION_SREYAS_3);
}
ptr = arg2;
extra = arg3;
}
}
{}
Expand Down Expand Up @@ -192,7 +197,7 @@ stock bool:BCrypt_Check(extra, const callback[], const password[], const hash[])
case BCRYPT_PLUGIN_VERSION_LASSIR:
{
new ptr;
return AMX_GetPublicPointer(0, ptr, callback, true) && _YSI_BCryptCheckA(password, hash, "_YSI_BCryptCheckDone", "ii", extra, ptr);
return AMX_GetPublicPointer(0, ptr, callback, true) && _YSI_BCryptCheckA(password, hash, "_YSI_BCryptCheckDone", YSI_gcII, extra, ptr);
}
case BCRYPT_PLUGIN_VERSION_SREYAS_1, BCRYPT_PLUGIN_VERSION_SREYAS_2, BCRYPT_PLUGIN_VERSION_SREYAS_3, BCRYPT_PLUGIN_VERSION_SREYAS_4:
return _YSI_BCryptCheckB(extra, callback, password, hash);
Expand All @@ -202,53 +207,59 @@ stock bool:BCrypt_Check(extra, const callback[], const password[], const hash[])
}
}

stock BCrypt_Hash(extra, const callback[], const password[], cost = 12)
stock _BCrypt_Hash(extra, ptr, const password[], cost)
{
++_YSI_gHashID;
new ptr;
if (AMX_GetPublicPointer(0, ptr, callback, true))
switch (BCrypt_GetPlugin())
{
switch (BCrypt_GetPlugin())
{
case BCRYPT_PLUGIN_VERSION_LASSIR:
{
return _YSI_BCryptHashA(password, cost, YSI_gscBCryptHashDone, YSI_gcIII, _YSI_gHashID, ptr, extra) ? _YSI_gHashID : 0;
}
case BCRYPT_PLUGIN_VERSION_SREYAS_1:
{
if (_YSI_BCryptHashB(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost))
{
new
prop[16];
format(prop, sizeof (prop), YSI_gscProp1, _YSI_gHashID),
setproperty(0, prop, extra),
format(prop, sizeof (prop), YSI_gscProp2, _YSI_gHashID),
setproperty(0, prop, ptr);
return _YSI_gHashID;
}
}
case BCRYPT_PLUGIN_VERSION_SREYAS_2:
{
if (_YSI_BCryptHashE(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost, YSI_gcII, ptr, extra))
{
new
prop[16];
format(prop, sizeof (prop), YSI_gscProp1, _YSI_gHashID),
setproperty(0, prop, extra),
format(prop, sizeof (prop), YSI_gscProp2, _YSI_gHashID),
setproperty(0, prop, ptr);
return _YSI_gHashID;
}
}
case BCRYPT_PLUGIN_VERSION_SREYAS_3, BCRYPT_PLUGIN_VERSION_SREYAS_4:
case BCRYPT_PLUGIN_VERSION_LASSIR:
{
return _YSI_BCryptHashA(password, cost, YSI_gscBCryptHashDone, YSI_gcIII, _YSI_gHashID, ptr, extra) ? _YSI_gHashID : 0;
}
case BCRYPT_PLUGIN_VERSION_SREYAS_1:
{
if (_YSI_BCryptHashB(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost))
{
// May have extra data.
return _YSI_BCryptHashE(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost, YSI_gcII, ptr, extra) ? _YSI_gHashID : 0;
new
prop[16];
format(prop, sizeof (prop), YSI_gscProp1, _YSI_gHashID),
setproperty(0, prop, extra),
format(prop, sizeof (prop), YSI_gscProp2, _YSI_gHashID),
setproperty(0, prop, ptr);
return _YSI_gHashID;
}
default:
}
case BCRYPT_PLUGIN_VERSION_SREYAS_2:
{
if (_YSI_BCryptHashE(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost, YSI_gcII, ptr, extra))
{
new
prop[16];
format(prop, sizeof (prop), YSI_gscProp1, _YSI_gHashID),
setproperty(0, prop, extra),
format(prop, sizeof (prop), YSI_gscProp2, _YSI_gHashID),
setproperty(0, prop, ptr);
return _YSI_gHashID;
}
}
}
case BCRYPT_PLUGIN_VERSION_SREYAS_3, BCRYPT_PLUGIN_VERSION_SREYAS_4:
{
// May have extra data.
return _YSI_BCryptHashE(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost, YSI_gcII, ptr, extra) ? _YSI_gHashID : 0;
}
default:
{
}
}
return 0;
}

stock BCrypt_Hash(extra, const callback[], const password[], cost = 12)
{
new ptr;
if (AMX_GetPublicPointer(0, ptr, callback, true))
{
return _BCrypt_Hash(extra, ptr, password, cost);
}
return 0;
}
Expand Down
31 changes: 21 additions & 10 deletions YSI_Extra/y_inline_bcrypt/y_inline_bcrypt_entry.inc
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,23 @@ Optional plugins:
#include "..\..\YSI_Server\y_thirdpartyinclude\y3_indirection"
#include "..\y_bcrypt\y_bcrypt_entry"

static stock const
YSI_gsc_Inline_BCryptCheckDone[] = "_Inline_BCryptCheckDone";

forward _Inline_BCryptCheckDone(Func:cb<i>, success);
forward _Inline_BCryptHashDone(Func:cb<si>, hashid);

public _Inline_BCryptCheckDone(Func:cb<i>, success)
{
if (success == -1)
{
success = _YSI_BCryptIsEqualA();
}
success = @.cb(success);
Indirect_Release(cb);
return _:success;
return success;
}

public _Inline_BCryptHashDone(Func:cb<si>, hashid)
static stock _Inline_BCryptHashDone(Func:cb<si>, hashid)
{
new hash[61];
// If the plugin is anything other than `1` or `2` this can't be called
Expand All @@ -108,20 +114,25 @@ public _Inline_BCryptHashDone(Func:cb<si>, hashid)
return hashid;
}

#define CALL@_Inline_BCryptHashDone%8() _Inline_BCryptHashDone%8(Func:0<si>, 0)

stock bool:BCrypt_CheckInline(const password[], const hash[], Func:cb<i>)
{
if (BCrypt_Check(_:cb, "_Inline_BCryptCheckDone", password, hash))
switch (BCrypt_GetPlugin())
{
Indirect_Claim(cb);
return true;
case BCRYPT_PLUGIN_VERSION_LASSIR:
return _YSI_BCryptCheckA(password, hash, YSI_gsc_Inline_BCryptCheckDone, YSI_gcII, _:cb, -1);
case BCRYPT_PLUGIN_VERSION_SREYAS_1, BCRYPT_PLUGIN_VERSION_SREYAS_2, BCRYPT_PLUGIN_VERSION_SREYAS_3, BCRYPT_PLUGIN_VERSION_SREYAS_4:
return _YSI_BCryptCheckB(_:cb, YSI_gsc_Inline_BCryptCheckDone, password, hash);
default:
// None found.
return false;
}
return false;
}

stock BCrypt_HashInline(const password[], cost = 12, {F@_@s, F@_@si}:cb, tag = tagof (cb)) // Func:cb<s> | Func:cb<si>
stock BCrypt_HashInline(const password[], cost = 12, {F@_@s, F@_@si}:cb, tag = tagof (cb))
{
printf("tag: %d", tag);
cost = BCrypt_Hash(_:cb, "_Inline_BCryptHashDone", password, cost);
cost = _BCrypt_Hash(_:cb, _:__addressof(_Inline_BCryptHashDone), password, cost);
if (cost)
{
Indirect_SetTag(cb, tag);
Expand Down

0 comments on commit 755bd94

Please sign in to comment.