X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/87b537b8e3e393c865cfa5bcb199ab6223a20134..30f74a9a4481804683c3c18dfc17788e274a27b9:/stglibs/crypto.lib/blowfish.c diff --git a/stglibs/crypto.lib/blowfish.c b/stglibs/crypto.lib/blowfish.c index 69c467bd..384d3a10 100644 --- a/stglibs/crypto.lib/blowfish.c +++ b/stglibs/crypto.lib/blowfish.c @@ -8,7 +8,7 @@ #include #include "stg/const.h" -#include "blowfish.h" +#include "stg/blowfish.h" #define ENCRYPT 0 #define DECRYPT 1 @@ -31,7 +31,7 @@ #define N 16 -static uint32_t F(BLOWFISH_CTX *ctx, uint32_t x); +static uint32_t F(const BLOWFISH_CTX *ctx, uint32_t x); static const uint32_t ORIG_P[16 + 2] = { 0x243F6A88L, 0x85A308D3L, 0x13198A2EL, 0x03707344L, 0xA4093822L, 0x299F31D0L, 0x082EFA98L, 0xEC4E6C89L, @@ -302,7 +302,7 @@ static const uint32_t ORIG_S[4][256] = { 0xB74E6132L, 0xCE77E25BL, 0x578FDFE3L, 0x3AC372E6L} }; //----------------------------------------------------------------------------- -uint32_t F(BLOWFISH_CTX *ctx, uint32_t x) +uint32_t F(const BLOWFISH_CTX *ctx, uint32_t x) { unsigned short a, b, c, d; uint32_t y = 0; @@ -326,7 +326,7 @@ y = y + ctx->S[3][d]; return y; } //----------------------------------------------------------------------------- -void Blowfish_Encrypt(BLOWFISH_CTX *ctx, uint32_t *xl, uint32_t *xr) +void Blowfish_Encrypt(const BLOWFISH_CTX *ctx, uint32_t *xl, uint32_t *xr) { uint32_t Xl; uint32_t Xr; @@ -354,7 +354,7 @@ Xl = Xl ^ ctx->P[N + 1]; *xr = Xr; } //----------------------------------------------------------------------------- -void Blowfish_Decrypt(BLOWFISH_CTX *ctx, uint32_t *xl, uint32_t *xr) +void Blowfish_Decrypt(const BLOWFISH_CTX *ctx, uint32_t *xl, uint32_t *xr) { uint32_t Xl; uint32_t Xr; @@ -437,7 +437,7 @@ for (i = 0; i < 4; ++i) } } //----------------------------------------------------------------------------- -void EnDecodeInit(const char * passwd, size_t length, BLOWFISH_CTX *ctx) +void InitContext(const char * passwd, size_t length, BLOWFISH_CTX * ctx) { unsigned char keyL[PASSWD_LEN]; @@ -467,25 +467,62 @@ void block2bytes(uint32_t t, char * c) *c = t >> 24 & 0x000000FF; } //----------------------------------------------------------------------------- -void DecodeString(char * d, const char * s, BLOWFISH_CTX *ctx) +void DecryptBlock(void * d, const void * s, const BLOWFISH_CTX *ctx) { -uint32_t a = bytes2block(s); -uint32_t b = bytes2block(s + 4); +const char * src = s; +char * dest = d; +uint32_t a = bytes2block(src); +uint32_t b = bytes2block(src + 4); Blowfish_Decrypt(ctx, &a, &b); -block2bytes(a, d); -block2bytes(b, d + 4); +block2bytes(a, dest); +block2bytes(b, dest + 4); } //----------------------------------------------------------------------------- -void EncodeString(char * d, const char * s, BLOWFISH_CTX *ctx) +void EncryptBlock(void * d, const void * s, const BLOWFISH_CTX *ctx) { -uint32_t a = bytes2block(s); -uint32_t b = bytes2block(s + 4); +const char * src = s; +char * dest = d; +uint32_t a = bytes2block(src); +uint32_t b = bytes2block(src + 4); Blowfish_Encrypt(ctx, &a, &b); -block2bytes(a, d); -block2bytes(b, d + 4); +block2bytes(a, dest); +block2bytes(b, dest + 4); +} +//----------------------------------------------------------------------------- +void DecryptString(void * d, const void * s, size_t length, const BLOWFISH_CTX * ctx) +{ +const char * src = s; +char * dest = d; +size_t pos = 0; +while (pos < length) + { + DecryptBlock(dest + pos, src + pos, ctx); + pos += 8; + } +} +//----------------------------------------------------------------------------- +void EncryptString(void * d, const void * s, size_t length, const BLOWFISH_CTX * ctx) +{ +const char * src = s; +char * dest = d; +size_t pos = 0; +while (pos < length) + { + if (pos + 8 < length) + EncryptBlock(dest + pos, src + pos, ctx); + else + { + // Short string, use 0-padded buffer. + char buf[8]; + memset(buf, 0, sizeof(buf)); + memcpy(buf, src + pos, length - pos); + EncryptBlock(dest + pos, buf, ctx); + } + pos += 8; + } } //-----------------------------------------------------------------------------