[5959435] | 1 | |
---|
| 2 | // $Id$ |
---|
| 3 | |
---|
| 4 | #include <stdio.h> |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <string.h> |
---|
[e81d1af] | 7 | #include <inttypes.h> |
---|
[5959435] | 8 | #include "panon.h" |
---|
| 9 | |
---|
| 10 | static uint8_t m_key[16]; |
---|
| 11 | static uint8_t m_pad[16]; |
---|
| 12 | |
---|
| 13 | #define CACHEBITS 20 |
---|
| 14 | #define CACHESIZE (1 << CACHEBITS) |
---|
| 15 | |
---|
| 16 | //static uint32_t enc_cache[CACHESIZE]; |
---|
| 17 | |
---|
| 18 | static uint32_t *enc_cache = 0; |
---|
| 19 | static uint32_t fullcache[2][2]; |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | void panon_init_cache() { |
---|
| 24 | if (enc_cache == 0) { |
---|
| 25 | enc_cache = (uint32_t *)malloc(CACHESIZE * sizeof(uint32_t)); |
---|
| 26 | } |
---|
| 27 | memset(enc_cache,0,(CACHESIZE * sizeof(uint32_t))); |
---|
| 28 | fullcache[0][0] = 0; |
---|
| 29 | fullcache[0][1] = 0; |
---|
| 30 | fullcache[1][0] = 0; |
---|
| 31 | fullcache[1][1] = 0; |
---|
| 32 | } |
---|
| 33 | static void cache_update(uint32_t scan) { |
---|
| 34 | uint8_t rin_output[16]; |
---|
| 35 | uint8_t rin_input[16]; |
---|
| 36 | uint32_t orig_addr = 0; |
---|
| 37 | uint32_t result = 0; |
---|
| 38 | uint32_t first4bytes_pad, first4bytes_input; |
---|
| 39 | int pos; |
---|
| 40 | |
---|
| 41 | memcpy(rin_input, m_pad, 16); |
---|
| 42 | first4bytes_pad = (((uint32_t) m_pad[0]) << 24) + |
---|
| 43 | (((uint32_t) m_pad[1]) << 16 ) + |
---|
| 44 | (((uint32_t) m_pad[2]) << 8) + |
---|
| 45 | (uint32_t) m_pad[3]; |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | memcpy(rin_input, m_pad, 16); |
---|
| 49 | orig_addr = (scan << (32 - CACHEBITS)); |
---|
| 50 | result = 0; |
---|
| 51 | for (pos = 0; pos < CACHEBITS; pos++) { |
---|
| 52 | |
---|
| 53 | if (pos == 0) { |
---|
| 54 | first4bytes_input = first4bytes_pad; |
---|
| 55 | } else { |
---|
| 56 | first4bytes_input = |
---|
| 57 | ((orig_addr >> (32 - pos)) << (32 - pos)) | |
---|
| 58 | ((first4bytes_pad << pos) >> pos); |
---|
| 59 | } |
---|
| 60 | rin_input[0] = (uint8_t) (first4bytes_input >> 24); |
---|
| 61 | rin_input[1] = (uint8_t) ((first4bytes_input << 8) >> 24); |
---|
| 62 | rin_input[2] = (uint8_t) ((first4bytes_input << 16) >> 24); |
---|
| 63 | rin_input[3] = (uint8_t) ((first4bytes_input << 24) >> 24); |
---|
| 64 | |
---|
| 65 | blockEncrypt(rin_input, 128, rin_output); |
---|
| 66 | |
---|
| 67 | result |= (rin_output[0] >> 7) << (31 - pos); |
---|
| 68 | } |
---|
| 69 | enc_cache[scan] = (result >> (32 - CACHEBITS)); |
---|
| 70 | |
---|
| 71 | } |
---|
| 72 | static uint32_t lookup_cache(uint32_t orig_addr) { |
---|
| 73 | uint32_t lookup_addr = (orig_addr >> (32 - CACHEBITS)); |
---|
| 74 | if (enc_cache[lookup_addr] == 0) { |
---|
| 75 | cache_update(lookup_addr); |
---|
| 76 | } |
---|
| 77 | return enc_cache[lookup_addr]; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | void panon_init(const uint8_t * key) { |
---|
| 81 | // initialise the 128-bit secret key |
---|
| 82 | memcpy(m_key, key, 16); |
---|
| 83 | // initialise the Rijndael cipher |
---|
| 84 | rijndael_init(ECB, Encrypt, key, Key16Bytes,0); |
---|
| 85 | blockEncrypt(key + 16, 128, m_pad); |
---|
| 86 | panon_init_cache(); |
---|
| 87 | } |
---|
| 88 | void panon_init_decrypt(const uint8_t * key) { |
---|
| 89 | memcpy(m_key, key, 16); |
---|
| 90 | rijndael_init(ECB, Decrypt, key, Key16Bytes,0); |
---|
| 91 | blockEncrypt(key + 16, 128, m_pad); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | uint32_t pp_anonymize(const uint32_t orig_addr) { |
---|
| 95 | uint8_t rin_output[16]; |
---|
| 96 | uint8_t rin_input[16]; |
---|
| 97 | |
---|
| 98 | uint32_t result = 0; |
---|
| 99 | uint32_t first4bytes_pad, first4bytes_input; |
---|
| 100 | int pos; |
---|
| 101 | |
---|
| 102 | memcpy(rin_input, m_pad, 16); |
---|
| 103 | first4bytes_pad = (((uint32_t) m_pad[0]) << 24) + |
---|
| 104 | (((uint32_t) m_pad[1]) << 16 ) + |
---|
| 105 | (((uint32_t) m_pad[2]) << 8) + |
---|
| 106 | (uint32_t) m_pad[3]; |
---|
| 107 | |
---|
| 108 | // For each prefix with length 0 to 31, generate a bit using the |
---|
| 109 | // rijndael cipher, which is used as a pseudorandom function here. |
---|
| 110 | // The bits generated in every round are combined into a pseudorandom |
---|
| 111 | // one-time-pad. |
---|
| 112 | |
---|
| 113 | for (pos = 0; pos <= 31; pos++) { |
---|
| 114 | // Padding: The most significant pos bits are taken from orig_addr. |
---|
| 115 | // The other 128-pos bits are taken from m_pad. The variables |
---|
| 116 | // first4bytes_pad and first4bytes_input are used to handle the annoying |
---|
| 117 | // byte order problem |
---|
| 118 | |
---|
| 119 | if (pos == 0) { |
---|
| 120 | first4bytes_input = first4bytes_pad; |
---|
| 121 | } else { |
---|
| 122 | first4bytes_input = ((orig_addr >> (32 - pos)) << (32 - pos)) | |
---|
| 123 | ((first4bytes_pad << pos) >> pos); |
---|
| 124 | } |
---|
| 125 | rin_input[0] = (uint8_t) (first4bytes_input >> 24); |
---|
| 126 | rin_input[1] = (uint8_t) ((first4bytes_input << 8) >> 24); |
---|
| 127 | rin_input[2] = (uint8_t) ((first4bytes_input << 16) >> 24); |
---|
| 128 | rin_input[3] = (uint8_t) ((first4bytes_input << 24) >> 24); |
---|
| 129 | |
---|
| 130 | // Encryption: The rijndael cipher is used as a pseudorandom function. |
---|
| 131 | // During each round, only the first bit of rin_output is used. |
---|
| 132 | blockEncrypt(rin_input, 128, rin_output); |
---|
| 133 | |
---|
| 134 | // Combination: the bits are combined into a pseudorandom one-time-pad. |
---|
| 135 | result |= (rin_output[0] >> 7) << (31 - pos); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | return result ^ orig_addr; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | uint32_t cpp_anonymize(const uint32_t orig_addr) { |
---|
| 143 | uint8_t rin_output[16]; |
---|
| 144 | uint8_t rin_input[16]; |
---|
| 145 | |
---|
| 146 | uint32_t firstnbits; |
---|
| 147 | |
---|
| 148 | uint32_t result = 0; |
---|
| 149 | uint32_t first4bytes_pad, first4bytes_input; |
---|
| 150 | int pos; |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | if (fullcache[0][0] == orig_addr) { |
---|
| 154 | return fullcache[0][1]; |
---|
| 155 | } else if (fullcache[1][0] == orig_addr) { |
---|
| 156 | uint32_t tmp = fullcache[1][1]; |
---|
| 157 | // move to "top" of "cache" |
---|
| 158 | fullcache[1][0] = fullcache[0][0]; |
---|
| 159 | fullcache[1][1] = fullcache[0][1]; |
---|
| 160 | fullcache[0][0] = orig_addr; |
---|
| 161 | fullcache[0][1] = tmp; |
---|
| 162 | return tmp; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | memcpy(rin_input, m_pad, 16); |
---|
| 166 | first4bytes_pad = (((uint32_t) m_pad[0]) << 24) + |
---|
| 167 | (((uint32_t) m_pad[1]) << 16 ) + |
---|
| 168 | (((uint32_t) m_pad[2]) << 8) + |
---|
| 169 | (uint32_t) m_pad[3]; |
---|
| 170 | |
---|
| 171 | // Look up the first CACHESIZE bits from enc_cache and start the |
---|
| 172 | // result with this, then proceed |
---|
| 173 | |
---|
| 174 | firstnbits = (uint32_t) orig_addr >> (32 - CACHEBITS); |
---|
| 175 | //result = (enc_cache[firstnbits] << (32 - CACHEBITS)); |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | result = (lookup_cache(orig_addr) << (32 - CACHEBITS)); |
---|
| 179 | // For each prefix with length CACHEBITS to 31, generate a bit using the |
---|
| 180 | // rijndael cipher, which is used as a pseudorandom function here. |
---|
| 181 | // The bits generated in every round are combined into a pseudorandom |
---|
| 182 | // one-time-pad. |
---|
| 183 | |
---|
| 184 | for (pos = CACHEBITS ; pos <= 31; pos++) { |
---|
| 185 | // Padding: The most significant pos bits are taken from orig_addr. |
---|
| 186 | // The other 128-pos bits are taken from m_pad. The variables |
---|
| 187 | // first4bytes_pad and first4bytes_input are used to handle the annoying |
---|
| 188 | // byte order problem |
---|
| 189 | |
---|
| 190 | if (pos == 0) { |
---|
| 191 | first4bytes_input = first4bytes_pad; |
---|
| 192 | } else { |
---|
| 193 | first4bytes_input = ((orig_addr >> (32 - pos)) << (32 - pos)) | |
---|
| 194 | ((first4bytes_pad << pos) >> pos); |
---|
| 195 | } |
---|
| 196 | rin_input[0] = (uint8_t) (first4bytes_input >> 24); |
---|
| 197 | rin_input[1] = (uint8_t) ((first4bytes_input << 8) >> 24); |
---|
| 198 | rin_input[2] = (uint8_t) ((first4bytes_input << 16) >> 24); |
---|
| 199 | rin_input[3] = (uint8_t) ((first4bytes_input << 24) >> 24); |
---|
| 200 | |
---|
| 201 | // Encryption: The rijndael cipher is used as a pseudorandom function. |
---|
| 202 | // During each round, only the first bit of rin_output is used. |
---|
| 203 | blockEncrypt(rin_input, 128, rin_output); |
---|
| 204 | |
---|
| 205 | // Combination: the bits are combined into a pseudorandom one-time-pad. |
---|
| 206 | result |= (rin_output[0] >> 7) << (31 - pos); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | fullcache[1][0] = fullcache[0][0]; |
---|
| 210 | fullcache[1][1] = fullcache[0][1]; |
---|
| 211 | fullcache[0][0] = orig_addr; |
---|
| 212 | fullcache[0][1] = result ^ orig_addr; |
---|
| 213 | |
---|
| 214 | return result ^ orig_addr; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | uint32_t anonymize(const uint32_t orig_addr) { |
---|
| 218 | uint8_t rin_output[16]; |
---|
| 219 | uint8_t rin_input[16]; |
---|
| 220 | |
---|
| 221 | uint32_t result = 0; |
---|
| 222 | |
---|
| 223 | memcpy(rin_input, m_pad, 16); |
---|
| 224 | |
---|
| 225 | rin_input[0] = (uint8_t) (orig_addr >> 24); |
---|
| 226 | rin_input[1] = (uint8_t) ((orig_addr << 8) >> 24); |
---|
| 227 | rin_input[2] = (uint8_t) ((orig_addr << 16) >> 24); |
---|
| 228 | rin_input[3] = (uint8_t) ((orig_addr << 24) >> 24); |
---|
| 229 | |
---|
| 230 | blockEncrypt(rin_input, 128, rin_output); |
---|
| 231 | |
---|
| 232 | result = 0; |
---|
| 233 | result += (rin_output[0] <<24); |
---|
| 234 | result += (rin_output[1] <<16); |
---|
| 235 | result += (rin_output[2] <<8); |
---|
| 236 | result += (rin_output[3]); |
---|
| 237 | return result; |
---|
| 238 | } |
---|
| 239 | |
---|