首页 > 解决方案 > How is sum constant in TEA calculated

问题描述

I'm taking a look at TEA algorithm on encryption. I have one problem understanding implementation from here:

https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm

#include <stdint.h>

void encrypt (uint32_t v[2], uint32_t k[4]) {
    uint32_t v0=v[0], v1=v[1], sum=0, i;           /* set up */
    uint32_t delta=0x9E3779B9;                     /* a key schedule constant */
    uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
    for (i=0; i<32; i++) {                         /* basic cycle start */
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
    }                                              /* end cycle */
    v[0]=v0; v[1]=v1;
}

void decrypt (uint32_t v[2], uint32_t k[4]) {
    uint32_t v0=v[0], v1=v[1], sum=0xC6EF3720, i;  /* set up; sum is 32*delta */
    uint32_t delta=0x9E3779B9;                     /* a key schedule constant */
    uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
    for (i=0; i<32; i++) {                         /* basic cycle start */
        v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
        v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        sum -= delta;
    }                                              /* end cycle */
    v[0]=v0; v[1]=v1;
}

In decrypt() function it clearly says sum is 32 times delta, 32 being number of rounds, however... 32(0x20) times 2654435769(0x9E3779B9) is 84941944608(0x13C6EF3720), not 3337565984(0xC6EF3720)!

What am I failing to grasp here? The algorithm is correct and I tested it myself.

Thanks for any help in advance,

Cheers

标签: cencryption

解决方案


sum是总和的低 32 位。TEA 在 32 位空间中执行。


推荐阅读