OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [software/] [app/] [xtea.c] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 serginhofr
#include <hf-risc.h>
2
 
3
/*
4
XTEA encryption algorithm
5
 
6
based on reference code released into the public domain by David Wheeler and Roger Needham
7
the code takes 64 bits of data in v[0] and v[1] and 128 bits of key in key[0] - key[3]
8
 
9
recommended number of rounds is 32 (2 Feistel-network rounds are performed on each iteration).
10
*/
11
 
12
const uint32_t xtea_key[4] = {0xf0e1d2c3, 0xb4a59687, 0x78695a4b, 0x3c2d1e0f};
13
 
14
void encipher(uint32_t num_rounds, uint32_t v[2], uint32_t const key[4]){
15
        uint32_t i;
16
        uint32_t v0 = v[0], v1 = v[1], sum = 0, delta = 0x9E3779B9;
17
 
18
        for (i = 0; i < num_rounds; i++){
19
                v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
20
                sum += delta;
21
                v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
22
        }
23
        v[0] = v0; v[1] = v1;
24
}
25
 
26
void decipher(uint32_t num_rounds, uint32_t v[2], uint32_t const key[4]){
27
        uint32_t i;
28
        uint32_t v0 = v[0], v1 = v[1], delta = 0x9E3779B9, sum = delta * num_rounds;
29
 
30
        for (i = 0; i < num_rounds; i++){
31
                v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
32
                sum -= delta;
33
                v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
34
        }
35
        v[0] = v0; v[1] = v1;
36
}
37
 
38
int main(void){
39
        int msg[2] = {0x12345678, 0x90123456};
40
        int cycles;
41
 
42
        printf("message: %8x%8x\n", msg[0], msg[1]);
43
        cycles = COUNTER;
44
        encipher(32, msg, xtea_key);
45
        cycles = COUNTER - cycles;
46
        printf("encipher: %8x%8x, %d cycles\n", msg[0], msg[1], cycles);
47
        cycles = COUNTER;
48
        decipher(32, msg, xtea_key);
49
        cycles = COUNTER - cycles;
50
        printf("decipher: %8x%8x, %d cycles\n", msg[0], msg[1], cycles);
51
 
52
        return 0;
53
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.