michajlo / libless_crypto

My shot at implementing common crypto/hash algorithms form scratch
1 stars 0 forks source link

if char 'd' 0x64 first in block - crypt string is broken #1

Open ghost opened 10 years ago

ghost commented 10 years ago

Hi!, if char 'd' 0x64 first in block - next crypt string is broken.. where is fix? in rcon or sbox table?

michajlo commented 10 years ago

Hi Den, do you happen to have any more details? Maybe some sample code to reproduce failure? I haven't touched this project in a while but I'll see what I remember.

ghost commented 10 years ago

ok, problem is char expect to replace 0x00, string end : )

0x02 0x2E 0x16 0xD9 0x68 0x56 0x06 0x55 0xE7 0xDB 0xB2 0x8F 0xA7 0x2F 0x70 0xA4 0xED 0xF8 0x4A 0x2D 0x25 0x61 0x18 0x30 0x34 0xC0 0x5D 0x9E 0x0F 0x47 0xB6 0x3A 0xFF 0xD9 0x68 0x8F 0x53 0x6E 0xE9 0x8B 0x20 0x01 0xD7 0x16 0xD0 0x7A 0xE7 0xA2 0xDE 0x28 0x37 0x8C 0x0C 0x0F 0x59 0x43 0x55 0x3B 0x6E 0xD6 0x44 0x23 0x82 0x7A 0x5B 0x50 0xAA 0x4C 0x31 0x76 0x5E 0xC7 0xE1 0x17 0x90 0x30 0x0D 0xD8 0xCB 0x00 0x2F 0xED 0x95

is 0x00 0x2F 0xED 0x95 - trouble, is copy result to unsigned char or convert to char* decode is broken..

is code:


#define AES_PAGE_SIZE 16
#define AES_PAGE_BLOCK(X) (X + ( (X % AES_PAGE_SIZE) ? (AES_PAGE_SIZE - (X % AES_PAGE_SIZE)): 0))

...

char * aes_encrypt(AESCRYPT_t *aes, char *in) {
    int i, j, len, ilen;
    char *out;

    ilen = strlen((const char *)in);
    len = AES_PAGE_BLOCK(ilen);
    uint8_t *tob64 = (uint8_t *)calloc_mem(1,((len * 2) * sizeof(char)) );

    memset(aes->state, 0xFF, AES_PAGE_SIZE);
    for (i = 0; i < len; i += AES_PAGE_SIZE) {
    for (j = 0; j < AES_PAGE_SIZE && i+j < ilen; j++) {
        aes->state[j] = (uint8_t)in[i+j];
        }
        for (; j < AES_PAGE_SIZE; j++) {
        aes->state[j] = 0xFF;
        }

// Hack - if insert uninitalize element - result is OK :(
// aes->state[17 or hi level] = '\0';
// experement only ...

        encrypt_block(aes);

    for( j = 0; j < AES_PAGE_SIZE; j++) {
        tob64[i+j] = (unsigned char)aes->state[j];
    }
    memset(aes->state, 0xFF, AES_PAGE_SIZE);
    }
    tob64[ilen] = '\0';
 return tob64;
}
ghost commented 10 years ago

And where is plan of key size - 128,192,256 bits ?

in code:

    if (aes_type == AES_KEY_128) {
    } else {
        return 1;
    }
    if (key_len > aes->key_size) {
        key_len = aes->key_size;
    }

where is use 256 bit key ?

michajlo commented 10 years ago

Looks like i didn't bother to implement any more than 128 bit key :/. Shouldn't be too terrible to add the rest, I don't think.

With respect to the sample above, do you have any more details? Inputs & expected outputs?

Also note I tink the following code from your sample (when uncommented) overflows state and would break the pointer to the key.

// Hack - if insert uninitalize element - result is OK :(
// aes->state[17 or hi level] = '\0';
// experement only ...
ghost commented 10 years ago

Ok, test input string:

char *tocrypt = "{\"id\":94220,ddddddddddddddd\"\"dddddddddddddd,\"ip\"a:\"5,6,1,1,1,5,6\"ddddddddddddddddddddddddddddddddddddddddddddddddddda";
michajlo commented 10 years ago

Gave the following a quick test and it seemed to work fine:

void test_encrypt_decrypt() {
    aes_t aes;
    uint8_t key[] = "Hello world12345";
    uint8_t plaintext[] = "{\"id\":94220,ddddddddddddddd\"\"dddddddddddddd,\"ip\"a:\"5,6,1,1,1,5,6\"ddddddddddddddddddddddddddddddddddddddddddddddddddda";
    uint8_t encd[128];
    uint8_t result[128];

    aes_init(&aes, AES_128, key, sizeof(key)-1);
    aes_encrypt(&aes, plaintext, sizeof(plaintext)-1, encd);

    aes_init(&aes, AES_128, key, sizeof(key)-1);
    aes_decrypt(&aes, encd, 128, result);

    assert(memcmp(plaintext, result, sizeof(plaintext) - 1) == 0); 
}

That being said my C is a bit rusty so it could be wrong. Do you have any more details or perhaps an idea what the exact issue is? I'm open to pull requests.