diff options
author | Amaury Pouly <amaury.pouly@gmail.com> | 2012-11-27 00:01:35 +0100 |
---|---|---|
committer | Amaury Pouly <amaury.pouly@gmail.com> | 2012-11-27 00:01:35 +0100 |
commit | 9716d1f1f964a29c5087a2861e2b94afd140dcbb (patch) | |
tree | 023c54c3903e4f3d48234099c5ce833adb148eb6 /utils/imxtools/sbtools/xorcrypt.c | |
parent | 4e95b72ecbd972b9e1eb162c40a559b94eebac69 (diff) | |
download | rockbox-9716d1f1f964a29c5087a2861e2b94afd140dcbb.tar.gz rockbox-9716d1f1f964a29c5087a2861e2b94afd140dcbb.zip |
sbtools: add forgotten file
Change-Id: I701a301efa369c9e1247e10d226ba69e6064d1b2
Diffstat (limited to 'utils/imxtools/sbtools/xorcrypt.c')
-rw-r--r-- | utils/imxtools/sbtools/xorcrypt.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/utils/imxtools/sbtools/xorcrypt.c b/utils/imxtools/sbtools/xorcrypt.c new file mode 100644 index 0000000000..89dde07ddc --- /dev/null +++ b/utils/imxtools/sbtools/xorcrypt.c | |||
@@ -0,0 +1,97 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2012 Amaury Pouly | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include "crypto.h" | ||
22 | #include <stdlib.h> | ||
23 | #include "misc.h" | ||
24 | |||
25 | static uint32_t do_round(union xorcrypt_key_t *key) | ||
26 | { | ||
27 | uint32_t k7 = key->k[7]; | ||
28 | uint32_t k2 = key->k[2]; | ||
29 | uint32_t k5_3_1 = key->k[5] ^ key->k[3] ^ key->k[1]; | ||
30 | key->k[1] = k2; | ||
31 | uint32_t k_11_7_5_3_1 = key->k[11] ^ k7 ^ k5_3_1; | ||
32 | key->k[2] = key->k[3] ^ k2; | ||
33 | key->k[3] = key->k[4]; | ||
34 | key->k[4] = key->k[5]; | ||
35 | key->k[5] = key->k[6]; | ||
36 | uint32_t k0 = key->k[0]; | ||
37 | key->k[7] = k7 ^ key->k[8]; | ||
38 | uint32_t k13 = key->k[13]; | ||
39 | key->k[8] = key->k[9]; | ||
40 | uint32_t k10 = key->k[10]; | ||
41 | key->k[6] = k7; | ||
42 | key->k[9] = k10; | ||
43 | uint32_t k11 = key->k[11]; | ||
44 | key->k[0] = k0 ^ k13 ^ k_11_7_5_3_1; | ||
45 | key->k[10] = (k11 >> 1) | (k11 << 31); | ||
46 | uint32_t k11_12 = k11 ^ key->k[12]; | ||
47 | uint32_t k14 = key->k[14]; | ||
48 | key->k[12] = k13; | ||
49 | key->k[13] = k14; | ||
50 | uint32_t k15 = key->k[15]; | ||
51 | key->k[15] = k0; | ||
52 | key->k[11] = k11_12; | ||
53 | key->k[14] = k15; | ||
54 | return key->k[0]; | ||
55 | } | ||
56 | |||
57 | uint32_t xor_encrypt(union xorcrypt_key_t keys[2], void *_data, int size) | ||
58 | { | ||
59 | if(size % 4) | ||
60 | bugp("xor_encrypt: size is not a multiple of 4 !\n"); | ||
61 | size /= 4; | ||
62 | uint32_t *data = _data; | ||
63 | uint32_t final_xor = 0; | ||
64 | for(int i = 0; i < size; i += 4) | ||
65 | { | ||
66 | uint32_t x = do_round(&keys[1]); | ||
67 | /* xor first key's first word with data (at most 4 words of data) */ | ||
68 | for(int j = i; j < i + 4 && j < size; j++) | ||
69 | { | ||
70 | keys[0].k[0] ^= data[j]; | ||
71 | data[j] ^= x; | ||
72 | } | ||
73 | final_xor = do_round(&keys[0]); | ||
74 | } | ||
75 | return final_xor ^ do_round(&keys[1]); | ||
76 | } | ||
77 | |||
78 | uint32_t xor_decrypt(union xorcrypt_key_t keys[2], void *_data, int size) | ||
79 | { | ||
80 | if(size % 4) | ||
81 | bugp("xor_decrypt: size is not a multiple of 4 !\n"); | ||
82 | size /= 4; | ||
83 | uint32_t *data = _data; | ||
84 | uint32_t final_xor = 0; | ||
85 | for(int i = 0; i < size; i += 4) | ||
86 | { | ||
87 | uint32_t x = do_round(&keys[1]); | ||
88 | /* xor first key's first word with data (at most 4 words of data) */ | ||
89 | for(int j = i; j < i + 4 && j < size; j++) | ||
90 | { | ||
91 | data[j] ^= x; | ||
92 | keys[0].k[0] ^= data[j]; | ||
93 | } | ||
94 | final_xor = do_round(&keys[0]); | ||
95 | } | ||
96 | return final_xor ^ do_round(&keys[1]); | ||
97 | } \ No newline at end of file | ||