proper conversion of unsigned char [16] -> unsigned long [4] [modified]
-
I am stuck actually, im trying to write a md5 / blowfish function im not sure if i am correctly converting the MD5[16] buffer into a unsigned long[4] this doesnt really make too much sense too me
typedef struct _tag2Long {
unsigned long _1;
unsigned long _2;
unsigned long _3;
unsigned long _4;
} _2LONG;void SignBuffer(unsigned char *pbzKey, unsigned char *pbzBuffer,
unsigned int iSize, unsigned long pSignature[4])
{
MD5_CTX md5;
BLOWFISH_CTX bf;MD5Init(&md5);
MD5Update(&md5, pbzBuffer, iSize);
MD5Final(&md5);_2LONG *tl = (_2LONG*) md5.digest;
printf("BLE 1\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);
Blowfish_Init(&bf, pbzKey, 16);
Blowfish_Encrypt(&bf, &tl->_1, &tl->_2);
Blowfish_Encrypt(&bf, &tl->_3, &tl->_4);pSignature[0] = tl->_1;
pSignature[1] = tl->_2;
pSignature[2] = tl->_3;
pSignature[3] = tl->_4;printf("BLE 2\t\t %x %x %x %x\n", pSignature[0], pSignature[1],
pSignature[2], pSignature[3]);
}bool VerifyBuffer(unsigned char *pbzKey, unsigned char *pSignature,
unsigned int iSize, unsigned long pBuffer[4])
{
BLOWFISH_CTX bf;
MD5_CTX md5;
bool bResult = false;printf("BLD 1\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
pBuffer[2], pBuffer[3]);Blowfish_Init(&bf, pbzKey, 16);
Blowfish_Decrypt(&bf, &pBuffer[0], &pBuffer[1]);
Blowfish_Decrypt(&bf, &pBuffer[2], &pBuffer[3]);printf("BLD 2\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
pBuffer[2], pBuffer[3]);MD5Init(&md5);
MD5Update(&md5, pSignature, iSize);
MD5Final(&md5);_2LONG *tl = (_2LONG*) md5.digest;
// printf("BLD 3\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);if (tl->_1 == pBuffer[0] && tl->_2 == pBuffer[1] &&
tl->_3 == pBuffer[2] && tl->_4 == pBuffer[4])
bResult = true;return bResult;
}void main(void)
{
unsigned char bszBuffer[2] = {'A', 'B'};
unsigned long ulSig[4];
SignBuffer((unsigned char*)"key", bszBuffer, 2, ulSig);
printf("\n");
if (VerifyBuffer((unsigned char*)"key", bszBuffer, 2, ulSig))
printf("\nok");
else printf("\nerr");
}and this is the output of them
BLE 1 b0c66fb8 733df651 4c2d26de a9a0e334
BLE 2 f12b0ed9 782fae18 -
I am stuck actually, im trying to write a md5 / blowfish function im not sure if i am correctly converting the MD5[16] buffer into a unsigned long[4] this doesnt really make too much sense too me
typedef struct _tag2Long {
unsigned long _1;
unsigned long _2;
unsigned long _3;
unsigned long _4;
} _2LONG;void SignBuffer(unsigned char *pbzKey, unsigned char *pbzBuffer,
unsigned int iSize, unsigned long pSignature[4])
{
MD5_CTX md5;
BLOWFISH_CTX bf;MD5Init(&md5);
MD5Update(&md5, pbzBuffer, iSize);
MD5Final(&md5);_2LONG *tl = (_2LONG*) md5.digest;
printf("BLE 1\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);
Blowfish_Init(&bf, pbzKey, 16);
Blowfish_Encrypt(&bf, &tl->_1, &tl->_2);
Blowfish_Encrypt(&bf, &tl->_3, &tl->_4);pSignature[0] = tl->_1;
pSignature[1] = tl->_2;
pSignature[2] = tl->_3;
pSignature[3] = tl->_4;printf("BLE 2\t\t %x %x %x %x\n", pSignature[0], pSignature[1],
pSignature[2], pSignature[3]);
}bool VerifyBuffer(unsigned char *pbzKey, unsigned char *pSignature,
unsigned int iSize, unsigned long pBuffer[4])
{
BLOWFISH_CTX bf;
MD5_CTX md5;
bool bResult = false;printf("BLD 1\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
pBuffer[2], pBuffer[3]);Blowfish_Init(&bf, pbzKey, 16);
Blowfish_Decrypt(&bf, &pBuffer[0], &pBuffer[1]);
Blowfish_Decrypt(&bf, &pBuffer[2], &pBuffer[3]);printf("BLD 2\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
pBuffer[2], pBuffer[3]);MD5Init(&md5);
MD5Update(&md5, pSignature, iSize);
MD5Final(&md5);_2LONG *tl = (_2LONG*) md5.digest;
// printf("BLD 3\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);if (tl->_1 == pBuffer[0] && tl->_2 == pBuffer[1] &&
tl->_3 == pBuffer[2] && tl->_4 == pBuffer[4])
bResult = true;return bResult;
}void main(void)
{
unsigned char bszBuffer[2] = {'A', 'B'};
unsigned long ulSig[4];
SignBuffer((unsigned char*)"key", bszBuffer, 2, ulSig);
printf("\n");
if (VerifyBuffer((unsigned char*)"key", bszBuffer, 2, ulSig))
printf("\nok");
else printf("\nerr");
}and this is the output of them
BLE 1 b0c66fb8 733df651 4c2d26de a9a0e334
BLE 2 f12b0ed9 782fae18the conversion looks fine. but doesn't Blowfish use 64-bit (8-byte) blocks ? so this
Blowfish_Encrypt(&bf, &tl->_1, &tl->_2);
will encrypt the 8 bytes starting at tl->_1 ? since _1 is only four bytes, it's going to run over into _2. so, you're doing BF(_1 + _2) -> _2 + _3. or maybe not. i don't know what Blowfish_Encrypt looks like. -
I am stuck actually, im trying to write a md5 / blowfish function im not sure if i am correctly converting the MD5[16] buffer into a unsigned long[4] this doesnt really make too much sense too me
typedef struct _tag2Long {
unsigned long _1;
unsigned long _2;
unsigned long _3;
unsigned long _4;
} _2LONG;void SignBuffer(unsigned char *pbzKey, unsigned char *pbzBuffer,
unsigned int iSize, unsigned long pSignature[4])
{
MD5_CTX md5;
BLOWFISH_CTX bf;MD5Init(&md5);
MD5Update(&md5, pbzBuffer, iSize);
MD5Final(&md5);_2LONG *tl = (_2LONG*) md5.digest;
printf("BLE 1\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);
Blowfish_Init(&bf, pbzKey, 16);
Blowfish_Encrypt(&bf, &tl->_1, &tl->_2);
Blowfish_Encrypt(&bf, &tl->_3, &tl->_4);pSignature[0] = tl->_1;
pSignature[1] = tl->_2;
pSignature[2] = tl->_3;
pSignature[3] = tl->_4;printf("BLE 2\t\t %x %x %x %x\n", pSignature[0], pSignature[1],
pSignature[2], pSignature[3]);
}bool VerifyBuffer(unsigned char *pbzKey, unsigned char *pSignature,
unsigned int iSize, unsigned long pBuffer[4])
{
BLOWFISH_CTX bf;
MD5_CTX md5;
bool bResult = false;printf("BLD 1\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
pBuffer[2], pBuffer[3]);Blowfish_Init(&bf, pbzKey, 16);
Blowfish_Decrypt(&bf, &pBuffer[0], &pBuffer[1]);
Blowfish_Decrypt(&bf, &pBuffer[2], &pBuffer[3]);printf("BLD 2\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
pBuffer[2], pBuffer[3]);MD5Init(&md5);
MD5Update(&md5, pSignature, iSize);
MD5Final(&md5);_2LONG *tl = (_2LONG*) md5.digest;
// printf("BLD 3\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);if (tl->_1 == pBuffer[0] && tl->_2 == pBuffer[1] &&
tl->_3 == pBuffer[2] && tl->_4 == pBuffer[4])
bResult = true;return bResult;
}void main(void)
{
unsigned char bszBuffer[2] = {'A', 'B'};
unsigned long ulSig[4];
SignBuffer((unsigned char*)"key", bszBuffer, 2, ulSig);
printf("\n");
if (VerifyBuffer((unsigned char*)"key", bszBuffer, 2, ulSig))
printf("\nok");
else printf("\nerr");
}and this is the output of them
BLE 1 b0c66fb8 733df651 4c2d26de a9a0e334
BLE 2 f12b0ed9 782fae18Hello! It might only be me but it is not obvious (to me) where you are trying to do this conversion you are talking about, you should at least highlight the part of interest in your code so people who want to help don't have to study your whole program trying to grasp the way you think.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
-
the conversion looks fine. but doesn't Blowfish use 64-bit (8-byte) blocks ? so this
Blowfish_Encrypt(&bf, &tl->_1, &tl->_2);
will encrypt the 8 bytes starting at tl->_1 ? since _1 is only four bytes, it's going to run over into _2. so, you're doing BF(_1 + _2) -> _2 + _3. or maybe not. i don't know what Blowfish_Encrypt looks like. -
Hello! It might only be me but it is not obvious (to me) where you are trying to do this conversion you are talking about, you should at least highlight the part of interest in your code so people who want to help don't have to study your whole program trying to grasp the way you think.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
BLE 2 f12b0ed9 782fae18 a87960ac f37b342e
is passed into VerifyBuffer, but when it is decrypted it doesnt match
BLE 1 b0c66fb8 733df651 4c2d26de a9a0e334
when i pass a MD5 comparision fromunsigned char *pSignature
hope that makes sense? it seems to lose data some how, not sure -
well it would be total 8 blocks for both _1 and _2, because a unsigned long is 4 bytes i am using Paul Kocher Blowfish Algorithm http://www.schneier.com/code/bfsh-koc.zip[^]
ah, ok. it's expecting 2 32-bit parts. i thought the params were input and output.
-
ah, ok. it's expecting 2 32-bit parts. i thought the params were input and output.
-
you're doing this, right: Sign: Sig = BFE(MD5(data)) Verify: X = BFD(Sig) Sig2 = MD5(Sig) Compare: X == Sig2 ? but shouldn't that bold line be Sig2 = MD5(data) ? the MD5 of the signature doesn't get you anything.
-
you're doing this, right: Sign: Sig = BFE(MD5(data)) Verify: X = BFD(Sig) Sig2 = MD5(Sig) Compare: X == Sig2 ? but shouldn't that bold line be Sig2 = MD5(data) ? the MD5 of the signature doesn't get you anything.
i fixed it, thank you very much for your help, i noticed
if (tl->_1 == pBuffer[0] && tl->_2 == pBuffer[1] &&
tl->_3 == pBuffer[2] && tl->_4 == pBuffer[4])
bResult = true;pBuffer[4] should of been pBuffer[3] i was going out of range of the array which didnt exist here is the updated code
int _ustrlen(unsigned char *pbz)
{
const unsigned char *ptr;
for (ptr = pbz; *ptr; ++ptr);
return (ptr - pbz);
}void SignBuffer(unsigned char *pbzKey, unsigned char *pbzBuffer,
unsigned int iSize, unsigned long *pSignature)
{
MD5_CTX md5, md5key;
BLOWFISH_CTX bf;MD5Init(&md5);
MD5Update(&md5, pbzBuffer, iSize);
MD5Final(&md5);_2LONG *tl = (_2LONG*) md5.digest;
printf("BLE 1\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);
MD5Init(&md5key);
MD5Update(&md5key, pbzKey, _ustrlen(pbzKey));
MD5Final(&md5key);Blowfish_Init(&bf, md5key.digest, 16);
Blowfish_Encrypt(&bf, &tl->_1, &tl->_2);
Blowfish_Encrypt(&bf, &tl->_3, &tl->_4);pSignature[0] = tl->_1;
pSignature[1] = tl->_2;
pSignature[2] = tl->_3;
pSignature[3] = tl->_4;printf("BLE 2\t\t %x %x %x %x\n", pSignature[0], pSignature[1],
pSignature[2], pSignature[3]);
}bool VerifyBuffer(unsigned char *pbzKey, unsigned char *pSignature,
unsigned int iSize, unsigned long *pBuffer)
{
BLOWFISH_CTX bf;
MD5_CTX md5, md5key;
bool bResult = false;printf("BLD 1\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
pBuffer[2], pBuffer[3]);MD5Init(&md5key);
MD5Update(&md5key, pbzKey, _ustrlen(pbzKey));
MD5Final(&md5key);Blowfish_Init(&bf, md5key.digest, 16);
Blowfish_Decrypt(&bf, &pBuffer[0], &pBuffer[1]);
Blowfish_Decrypt(&bf, &pBuffer[2], &pBuffer[3]);printf("BLD 2\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
pBuffer[2], pBuffer[3]);MD5Init(&md5);
MD5Update(&md5, pSignature, iSize);
MD5Final(&md5);_2LONG *tl = (_2LONG*) md5.digest;
printf("BLD 3\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);if (tl->_1 == pBuffer[0] && tl->_2 == pBuffer[1] &&
tl->_3 == pBuffer[2] && tl->_4 == pBuffer[3])
bResult = true;return bResult;