Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XXTEA implementation gives an incorrect answer #1056

Open
alvoskov opened this issue Nov 19, 2024 · 2 comments
Open

XXTEA implementation gives an incorrect answer #1056

alvoskov opened this issue Nov 19, 2024 · 2 comments

Comments

@alvoskov
Copy link

The next XXTEA implementation gives incorrect result when compiled by OCC 6.0.73.
Compilation in MinGW (both 32-bit and 64-bit), Open Watcom and Pelles C gives correct results.

The program output in OCC:

Test result: 0
0: C4CC7F1CC007378C FFFFFFFFC007378C

The source code:

#include < stdio.h>
#include < stdlib.h>
#include < stdint.h>


#define MX(p) (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (key[((p)&3)^e] ^ z)))

uint64_t xxtea_encrypt(const uint64_t inp, const uint32_t *key)
{
    static const uint32_t DELTA = 0x9e3779b9;
    static const unsigned int nrounds = 32;
    uint32_t y, z, sum = 0;
    union {
        uint32_t v[2];
        uint64_t x;
    } out;
    out.x = inp;
    z = out.v[1];
    for (unsigned int i = 0; i < nrounds; i++) {
        sum += DELTA;
        unsigned int e = (sum >> 2) & 3;
        y = out.v[1]; 
        z = out.v[0] += MX(0);
        y = out.v[0];
        z = out.v[1] += MX(1);
    }
    return out.x;
}

int xxtea_test()
{
    static const uint64_t OUT0 = 0x575d8c80053704ab;
    static const uint64_t OUT1 = 0xc4cc7f1cc007378c;
    uint32_t key[4] = {0, 0, 0, 0};
    // Test 1
    if (xxtea_encrypt(0, key) != OUT0) {
        fprintf(stderr, "0: %llX %llX", xxtea_encrypt(0, key), OUT0);
        return 0;
    }
    // Test 2
    key[0] = 0x08040201; key[1] = 0x80402010;
    key[2] = 0xf8fcfeff; key[3] = 0x80c0e0f0; 
    if (xxtea_encrypt(0x80c0e0f0f8fcfeff, key) != OUT1) {
        fprintf(stderr, "0: %llX %llX", xxtea_encrypt(0x80c0e0f0f8fcfeff, key), OUT1);
        return 0;
    }
    return 1;
}

int main()
{
    printf("Test result: %d\n", xxtea_test());
}
@chuggafan
Copy link
Contributor

chuggafan commented Nov 19, 2024 via email

@LADSoft
Copy link
Owner

LADSoft commented Nov 26, 2024

thanks @chuggafan. if i have to guess there is a bug in the constant optimizer and it is sign extending lol... i will look at it soon...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants