|
| 1 | +/* |
| 2 | + * Copyright (c) 2014, Peter Haag |
| 3 | + * Copyright (c) 2009, Peter Haag |
| 4 | + * Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * * Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * * Neither the name of the author nor the names of its contributors may be |
| 16 | + * used to endorse or promote products derived from this software without |
| 17 | + * specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * |
| 31 | + * $Author: haag $ |
| 32 | + * |
| 33 | + * $Id: panonymizer.c 39 2009-11-25 08:11:15Z haag $ |
| 34 | + * |
| 35 | + * $LastChangedRevision: 39 $ |
| 36 | + * |
| 37 | + */ |
| 38 | + |
| 39 | +/* Original disclaimer |
| 40 | + * Atlanta, Georgia 30332. |
| 41 | + * All Rights Reserved |
| 42 | + * |
| 43 | + * The following Software is posted on the Internet by the Georgia |
| 44 | + * Tech Research Corporation (GTRC). It was developed by employees |
| 45 | + * of the Georgia Institute of Technology in the College of Computing. |
| 46 | + * GTRC hereby grants to the user a non-exclusive, royalty-free |
| 47 | + * license to utilize such Software for the User's own purposes |
| 48 | + * pursuant to the following conditions. |
| 49 | + * |
| 50 | + * |
| 51 | + * THE SOFTWARE IS LICENSED ON AN "AS IS" BASIS. GTRC MAKES NO WARRANTY |
| 52 | + * THAT ALL ERRORS CAN BE OR HAVE BEEN ELIMINATED FROM THE SOFTWARE. |
| 53 | + * GTRC SHALL NOT BE RESPONSIBLE FOR LOSSES OF ANY KIND RESULTING FROM |
| 54 | + * THE USE OF THE SOFTWARE AND ITS ACCOMPANYING DOCUMENTATION, AND CAN |
| 55 | + * IN NO WAY PROVIDE COMPENSATION FOR ANY LOSSES SUSTAINED, INCLUDING |
| 56 | + * BUT NOT LIMITED TO ANY OBLIGATION, LIABILITY, RIGHT, CLAIM OR REMEDY |
| 57 | + * FOR TORT, OF FOR ANY ACTUAL OR ALLEGED INFRINGEMENT OF PATENTS, COPYRIGHTS, |
| 58 | + * TRADE SECRETS, OR SIMILAR RIGHTS OF THIRD PARTIES, NOR ANY BUSINESS |
| 59 | + * EXPENSE, MACHINE DOWNTIME, OR DAMAGES CAUSED LICENSEE BY ANY DEFICIENCY, |
| 60 | + * DEFECT OR ERROR IN THE SOFTWARE OR MALFUNCTION THEREOF, NOR ANY |
| 61 | + * INCIDENTAL OR CONSEQUENTIAL DAMAGES, HOWEVER CAUSED. GTRC DISCLAIMS |
| 62 | + * ALL WARRANTIES, BOTH EXPRESS AND IMPLIED RESPECTING THE USE AND |
| 63 | + * OPERATION OF THE SOFTWARE AND ANY ACCOMPANYING DOCUMENTATION, |
| 64 | + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 65 | + * PARTICULAR PURPOSE AND ANY IMPLIED WARRANTY ARISING FROM COURSE |
| 66 | + * OF PERFORMANCE, COURSE OF DEALING OR USAGE OF TRADE. GTRC MAKES NO |
| 67 | + * WARRANTY THAT THE SOFTWARE IS ADEQUATELY OR COMPLETELY DESCRIBED |
| 68 | + * IN, OR BEHAVES IN ACCORDANCE WITH ANY OF THE ACCOMPANYING |
| 69 | + * DOCUMENTATION. THE USER OF THE SOFTWARE IS EXPECTED TO MAKE THE FINAL |
| 70 | + * EVALUATION OF THE SOFTWARE'S USEFULNESS IN USER'S OWN ENVIRONMENT. |
| 71 | + * |
| 72 | + * |
| 73 | + * Package: Crypto-PAn 1.0 |
| 74 | + * File: panonymizer.cpp |
| 75 | + * Last Update: April 17, 2002 |
| 76 | + * Author: Jinliang Fan |
| 77 | + * |
| 78 | + */ |
| 79 | + |
| 80 | +#include <stdio.h> |
| 81 | +#include <sys/types.h> |
| 82 | +#include <stdlib.h> |
| 83 | +#include <string.h> |
| 84 | +#include <ctype.h> |
| 85 | +#include <stdint.h> |
| 86 | + |
| 87 | +#ifdef HAVE_STDINT_H |
| 88 | +#include <stdint.h> |
| 89 | +#endif |
| 90 | + |
| 91 | +#include "panonymizer.h" |
| 92 | + |
| 93 | +static uint8_t m_key[16]; //128 bit secret key |
| 94 | +static uint8_t m_pad[16]; //128 bit secret pad |
| 95 | + |
| 96 | +// Init |
| 97 | +void PAnonymizer_Init(uint8_t * key) { |
| 98 | + //initialize the 128-bit secret key. |
| 99 | + memcpy(m_key, key, 16); |
| 100 | + //initialize the Rijndael cipher. |
| 101 | + Rijndael_init(ECB, Encrypt, key, Key16Bytes, NULL); |
| 102 | + //initialize the 128-bit secret pad. The pad is encrypted before being used for padding. |
| 103 | + Rijndael_blockEncrypt(key + 16, 128, m_pad); |
| 104 | +} |
| 105 | + |
| 106 | +int ParseCryptoPAnKey ( char *s, char *key ) { |
| 107 | +int i, j; |
| 108 | +char numstr[3]; |
| 109 | +uint32_t len = strlen(s); |
| 110 | + |
| 111 | + if ( len < 32 || len > 66 ) { |
| 112 | + fprintf(stderr, "*** CryptoPAnKey error: size: %u\n", len); |
| 113 | + fprintf(stderr, "*** Need either a plain 32 char string, or a 32 byte hex key starting with 0x..\n"); |
| 114 | + return 0; |
| 115 | + } |
| 116 | + |
| 117 | + if ( strlen(s) == 32 ) { |
| 118 | + // Key is a string |
| 119 | + strncpy(key, s, 32); |
| 120 | + return 1; |
| 121 | + } |
| 122 | + |
| 123 | + s[1] = tolower(s[1]); |
| 124 | + numstr[2] = 0; |
| 125 | + if ( strlen(s) == 66 && s[0] == '0' && s[1] == 'x' ) { |
| 126 | + j = 2; |
| 127 | + for ( i=0; i<32; i++ ) { |
| 128 | + if ( !isxdigit((int)s[j]) || !isxdigit((int)s[j+1]) ) |
| 129 | + return 0; |
| 130 | + numstr[0] = s[j++]; |
| 131 | + numstr[1] = s[j++]; |
| 132 | + key[i] = strtol(numstr, NULL, 16); |
| 133 | + } |
| 134 | + return 1; |
| 135 | + } |
| 136 | + |
| 137 | + // It's an invalid key |
| 138 | + fprintf(stderr, "*** CryptoPAnKey error: size: %u\n", len); |
| 139 | + fprintf(stderr, "*** Need either a plain 32 char string, or a 32 byte hex key starting with 0x..\n"); |
| 140 | + return 0; |
| 141 | + |
| 142 | +} // End of ParseCryptoPAnKey |
| 143 | + |
| 144 | +//Anonymization funtion |
| 145 | +uint32_t anonymize(const uint32_t orig_addr) { |
| 146 | + uint8_t rin_output[16]; |
| 147 | + uint8_t rin_input[16]; |
| 148 | + |
| 149 | + uint32_t result = 0; |
| 150 | + uint32_t first4bytes_pad, first4bytes_input; |
| 151 | + int pos; |
| 152 | + |
| 153 | + memcpy(rin_input, m_pad, 16); |
| 154 | + first4bytes_pad = (((uint32_t) m_pad[0]) << 24) + (((uint32_t) m_pad[1]) << 16) + |
| 155 | + (((uint32_t) m_pad[2]) << 8) + (uint32_t) m_pad[3]; |
| 156 | + |
| 157 | + // For each prefixes with length from 0 to 31, generate a bit using the Rijndael cipher, |
| 158 | + // which is used as a pseudorandom function here. The bits generated in every rounds |
| 159 | + // are combineed into a pseudorandom one-time-pad. |
| 160 | + for (pos = 0; pos <= 31 ; pos++) { |
| 161 | + |
| 162 | + //Padding: The most significant pos bits are taken from orig_addr. The other 128-pos |
| 163 | + //bits are taken from m_pad. The variables first4bytes_pad and first4bytes_input are used |
| 164 | + //to handle the annoying byte order problem. |
| 165 | + if (pos==0) { |
| 166 | + first4bytes_input = first4bytes_pad; |
| 167 | + } |
| 168 | + else { |
| 169 | + first4bytes_input = ((orig_addr >> (32-pos)) << (32-pos)) | ((first4bytes_pad<<pos) >> pos); |
| 170 | + } |
| 171 | + rin_input[0] = (uint8_t) (first4bytes_input >> 24); |
| 172 | + rin_input[1] = (uint8_t) ((first4bytes_input << 8) >> 24); |
| 173 | + rin_input[2] = (uint8_t) ((first4bytes_input << 16) >> 24); |
| 174 | + rin_input[3] = (uint8_t) ((first4bytes_input << 24) >> 24); |
| 175 | + |
| 176 | + //Encryption: The Rijndael cipher is used as pseudorandom function. During each |
| 177 | + //round, only the first bit of rin_output is used. |
| 178 | + Rijndael_blockEncrypt(rin_input, 128, rin_output); |
| 179 | + |
| 180 | + //Combination: the bits are combined into a pseudorandom one-time-pad |
| 181 | + result |= (rin_output[0] >> 7) << (31-pos); |
| 182 | + } |
| 183 | + //XOR the orginal address with the pseudorandom one-time-pad |
| 184 | + return result ^ orig_addr; |
| 185 | +} |
| 186 | + |
| 187 | +/* little endian CPU's are boring! - but give it a try |
| 188 | + * orig_addr is a ptr to memory, return by inet_pton for IPv6 |
| 189 | + * anon_addr return the result in the same order |
| 190 | + */ |
| 191 | +void anonymize_v6(const uint64_t orig_addr[2], uint64_t *anon_addr) { |
| 192 | + uint8_t rin_output[16], *orig_bytes, *result; |
| 193 | + uint8_t rin_input[16]; |
| 194 | + |
| 195 | + int pos, i, bit_num, left_byte; |
| 196 | + |
| 197 | + anon_addr[0] = anon_addr[1] = 0; |
| 198 | + result = (uint8_t *)anon_addr; |
| 199 | + orig_bytes = (uint8_t *)orig_addr; |
| 200 | + |
| 201 | + // For each prefixes with length from 0 to 127, generate a bit using the Rijndael cipher, |
| 202 | + // which is used as a pseudorandom function here. The bits generated in every rounds |
| 203 | + // are combineed into a pseudorandom one-time-pad. |
| 204 | + for (pos = 0; pos <= 127 ; pos++) { |
| 205 | + bit_num = pos & 0x7; |
| 206 | + left_byte = (pos >> 3); |
| 207 | + |
| 208 | + for ( i=0; i<left_byte; i++ ) { |
| 209 | + rin_input[i] = orig_bytes[i]; |
| 210 | + } |
| 211 | + rin_input[left_byte] = orig_bytes[left_byte] >> (7-bit_num) << (7-bit_num) | (m_pad[left_byte]<<bit_num) >> bit_num; |
| 212 | + for ( i=left_byte+1; i<16; i++ ) { |
| 213 | + rin_input[i] = m_pad[i]; |
| 214 | + } |
| 215 | + |
| 216 | + //Encryption: The Rijndael cipher is used as pseudorandom function. During each |
| 217 | + //round, only the first bit of rin_output is used. |
| 218 | + Rijndael_blockEncrypt(rin_input, 128, rin_output); |
| 219 | + |
| 220 | + //Combination: the bits are combined into a pseudorandom one-time-pad |
| 221 | + result[left_byte] |= (rin_output[0] >> 7) << bit_num; |
| 222 | + |
| 223 | + } |
| 224 | + //XOR the orginal address with the pseudorandom one-time-pad |
| 225 | + anon_addr[0] ^= orig_addr[0]; |
| 226 | + anon_addr[1] ^= orig_addr[1]; |
| 227 | + |
| 228 | +} |
0 commit comments