OpenCores
URL https://opencores.org/ocsvn/cryptosorter/cryptosorter/trunk

Subversion Repositories cryptosorter

[/] [cryptosorter/] [trunk/] [memocodeDesignContest2008/] [ctrl/] [ref_pc/] [recordio.c] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 kfleming
/*
2
 * MEMOCODE Hardware/Software CoDesign Contest 2008
3
 *
4
 * Everyone is granted permission to copy, modify and redistribute
5
 * this tool set under the following conditions:
6
 *
7
 *    This source code is distributed for non-commercial use only.
8
 *    Please contact the maintainer for restrictions applying to
9
 *    commercial use.
10
 *
11
 *    Permission is granted to anyone to make or distribute copies
12
 *    of this source code, either as received or modified, in any
13
 *    medium, provided that all copyright notices, permission and
14
 *    nonwarranty notices are preserved, and that the distributor
15
 *    grants the recipient permission for further redistribution as
16
 *    permitted by this document.
17
 *
18
 *    Permission is granted to distribute this file in compiled
19
 *    or executable form under the same conditions that apply for
20
 *    source code, provided that either:
21
 *
22
 *    A. it is accompanied by the corresponding machine-readable
23
 *       source code,
24
 *    B. it is accompanied by a written offer, with no time limit,
25
 *       to give anyone a machine-readable copy of the corresponding
26
 *       source code in return for reimbursement of the cost of
27
 *       distribution.  This written offer must permit verbatim
28
 *       duplication by anyone, or
29
 *    C. it is distributed by someone who received only the
30
 *       executable form, and is accompanied by a copy of the
31
 *       written offer of source code that they received concurrently.
32
 *
33
 * In other words, you are welcome to use, share and improve this
34
 * source file.  You are forbidden to forbid anyone else to use, share
35
 * and improve what you give them.
36
 *
37
 * THE SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY WARRANTY OF ANY KIND, EITHER
38
 * EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO ANY WARRANTY
39
 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS OR BE ERROR-FREE AND ANY
40
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
41
 * TITLE, OR NON-INFRINGEMENT.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
42
 * BE LIABLE FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO DIRECT, INDIRECT,
43
 * SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, OR IN
44
 * ANY WAY CONNECTED WITH THIS SOFTWARE (WHETHER OR NOT BASED UPON WARRANTY,
45
 * CONTRACT, TORT OR OTHERWISE).
46
 *
47
 */
48
 
49
#include "recordio.h"
50
#include "aes_core.h"
51
#include <assert.h>
52
#include <stdio.h>
53
 
54
RECORD  db[MAXRECORD];
55
static AES_KEY  db_encrypt_key;
56
static unsigned db_chk;
57
FILE* sortedplain;
58
 
59
unsigned nextrng(unsigned rng) {
60
  return (rng >> 1) ^ (-(signed int)(rng & 1) & 0xd0000001u);
61
}
62
 
63
void getkey(unsigned idx, unsigned *f) {
64
  unsigned char plaindata[16] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
65
                                 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
66
  unsigned char cryptdata[16];
67
  int i;
68
  PUTU32(plaindata+12, idx);
69
  AES_encrypt(plaindata, cryptdata, &db_encrypt_key);
70
  for (i=0; i<4; i++)
71
    f[i] = GETU32(cryptdata + 4*i);
72
}
73
 
74
void initializedb(const unsigned char *gkey) {
75
  unsigned rng = RNGSEED;
76
  unsigned i;
77
  unsigned record_key[4];
78
  assert(sortedplain = fopen("sortedplain.hex","w"));
79
  static int n = MAXRECORD;
80
  assert(gkey != 0);
81
 
82
  AES_set_encrypt_key(gkey, 128, &db_encrypt_key);
83
 
84
  db_chk = 0; // reference checksum for non-encrypted data
85
 
86
  for (i=0; i<MAXRECORD; i++) {
87
    getkey(i, record_key);
88
 
89
    db[i].f1 = n^ record_key[0];
90
    db[i].f2 = 0^ record_key[1];
91
    db[i].f3 = 0^ record_key[2];
92
    db[i].f4 = 0^ record_key[3];
93
    n--;
94
    /*rng = nextrng(rng);
95
    if (rng < 0x80000000)
96
           db[i].f1 = record_key[0];
97
    else {
98
      db[i].f1  = rng ^ record_key[0];
99
      db_chk   += rng;
100
    }
101
 
102
    rng = nextrng(rng);
103
    if (rng < 0x40000000)
104
      db[i].f2 = record_key[1];
105
    else {
106
      db[i].f2  = rng ^ record_key[1];
107
      db_chk   += rng;
108
    }
109
 
110
    rng = nextrng(rng);
111
    if (rng < 0x20000000)
112
      db[i].f3 = record_key[2];
113
    else {
114
      db[i].f3  = rng ^ record_key[2];
115
      db_chk   += rng;
116
         }
117
 
118
    rng = nextrng(rng);
119
    db[i].f4  = rng ^ record_key[3];
120
    db_chk   += rng;*/
121
    }
122
 
123
}
124
 
125
void readrecord(RECORD *r, unsigned idx) {
126
  unsigned record_key[4];
127
  assert(r != 0);
128
  assert(idx < MAXRECORD);
129
  getkey(idx, record_key);
130
  r->f1 = db[idx].f1 ^ record_key[0];
131
  r->f2 = db[idx].f2 ^ record_key[1];
132
  r->f3 = db[idx].f3 ^ record_key[2];
133
  r->f4 = db[idx].f4 ^ record_key[3];
134
}
135
 
136
void writerecord(RECORD *r, unsigned idx) {
137
  unsigned record_key[4];
138
  assert(r != 0);
139
  assert(idx < MAXRECORD);
140
  getkey(idx, record_key);
141
  db[idx].f1 = r->f1 ^ record_key[0];
142
  db[idx].f2 = r->f2 ^ record_key[1];
143
  db[idx].f3 = r->f3 ^ record_key[2];
144
  db[idx].f4 = r->f4 ^ record_key[3];
145
}
146
 
147
int comparerecord(unsigned idx1, unsigned idx2) {
148
  RECORD r1, r2;
149
  unsigned r;
150
  readrecord(&r1, idx1);
151
  readrecord(&r2, idx2);
152
  if ( (r1.f1) >  (r2.f1)) r = 0;
153
  else if (((r1.f1) == (r2.f1)) &&
154
           ((r1.f2)  > (r2.f2))) r = 0;
155
  else if (((r1.f1) == (r2.f1)) &&
156
           ((r1.f2) == (r2.f2)) &&
157
                          ((r1.f3)  > (r2.f3))) r = 0;
158
  else if (((r1.f1) == (r2.f1)) &&
159
           ((r1.f2) == (r2.f2)) &&
160
                          ((r1.f3) == (r2.f3)) &&
161
                          ((r1.f4)  > (r2.f4))) r = 0;
162
  else r = 1;
163
  return r;
164
}
165
 
166
void swaprecord(unsigned idx1, unsigned idx2) {
167
  RECORD r1, r2;
168
  readrecord(&r1, idx1);
169
  readrecord(&r2, idx2);
170
  writerecord(&r2, idx1);
171
  writerecord(&r1, idx2);
172
}
173
 
174
int verifydb() {
175
  unsigned i;
176
  unsigned ok = 1;
177
  unsigned chk;
178
  RECORD   R;
179
  // verify if the ordering is correct
180
 
181
  for (i=0; i<MAXRECORD-1; i++){
182
    readrecord(&R, i);
183
    fprintf(sortedplain,"%08x%08x%08x%08x\n", R.f1,  R.f2,  R.f3,  R.f4);
184
    ok = ok & (comparerecord(i+1, i) == 0);
185
  }
186
  // evaluate checksum
187
  chk = 0;
188
  for (i=0; i<MAXRECORD; i++) {
189
    readrecord(&R, i);
190
    chk += R.f1;
191
    chk += R.f2;
192
    chk += R.f3;
193
    chk += R.f4;
194
  }
195
 
196
  return ok & (chk == db_chk);
197
}
198
 
199
void sortrecord() {
200
  unsigned i, swapped = 1, gap;
201
  gap = MAXRECORD * 10 / 13;
202
 
203
  while ((gap > 1) || (swapped)) {
204
    swapped = 0;
205
    if (gap > 1) {
206
      gap = gap * 10 / 13;
207
      gap = (gap == 10) ? 11 : gap;
208
      gap = (gap ==  9) ? 11 : gap;
209
    }
210
    for (i=0; (i + gap) <= MAXRECORD-1; i++)
211
      if (comparerecord(i+gap, i)) {
212
        swaprecord(i,i+gap);
213
        swapped = 1;
214
      }
215
  }
216
}
217
 
218
void showdb() {
219
  unsigned i;
220
  unsigned record_key[4];
221
  for (i=0; i<MAXRECORD; i++) {
222
    printf("%3x ", i);
223
 
224
    printf("%8x ",db[i].f1);
225
    printf("%8x ",db[i].f2);
226
    printf("%8x ",db[i].f3);
227
    printf("%8x ",db[i].f4);
228
 
229
    getkey(i, record_key);
230
 
231
    printf("%8x ",(db[i].f1 ^ record_key[0]));
232
    printf("%8x ",(db[i].f2 ^ record_key[1]));
233
    printf("%8x ",(db[i].f3 ^ record_key[2]));
234
    printf("%8x\n",(db[i].f4 ^ record_key[3]));
235
  }
236
 
237
}

powered by: WebSVN 2.1.0

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