|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <cstring> |
| 4 | +#include <unistd.h> |
| 5 | +#include <ctime> |
| 6 | +#include <algorithm> |
| 7 | + |
| 8 | +#include "logauth.h" |
| 9 | +#include "cryptopp/hex.h" |
| 10 | +#include "cryptopp/integer.h" |
| 11 | +#include "cryptopp/dsa.h" |
| 12 | +#include "cryptopp/osrng.h" |
| 13 | +#include "cryptopp/files.h" |
| 14 | + |
| 15 | +#ifndef A |
| 16 | +#define A 4 |
| 17 | +#endif |
| 18 | + |
| 19 | +#ifndef B |
| 20 | +#define B 4096 |
| 21 | +#endif |
| 22 | + |
| 23 | +#ifndef C |
| 24 | +#define C 2048 |
| 25 | +#endif |
| 26 | + |
| 27 | +#ifndef N |
| 28 | +#define N 4097 |
| 29 | +#endif |
| 30 | + |
| 31 | +using std::string; |
| 32 | +using std::ofstream; |
| 33 | +using std::ifstream; |
| 34 | +using std::time; |
| 35 | +using namespace CryptoPP; |
| 36 | + |
| 37 | +static string oldLine = ""; |
| 38 | +static struct timespec oldTime; |
| 39 | + |
| 40 | +static AutoSeededRandomPool rng; |
| 41 | +static DSA::PublicKey pubA; |
| 42 | +static DSA::PrivateKey privA; |
| 43 | + |
| 44 | + |
| 45 | +// This method tries to retrieve the next event. |
| 46 | +// When implemented correct, it should return an pointer to an event OR NULL when no event happened. |
| 47 | +// Here with 'new' we allocate memory, so it has to be freed later. |
| 48 | + |
| 49 | +// static event* getNextEvent(){ |
| 50 | +// string line = "a simple mock event"; |
| 51 | +// return new event(line); |
| 52 | +// } |
| 53 | + |
| 54 | +/* This method computes the Authenticator (signature) on some elements of buffer. |
| 55 | +*/ |
| 56 | +static string computeAuth(string buffer[], int start, int end){ |
| 57 | + string concatString; |
| 58 | + for (int i = start; i <= end; ++i){ |
| 59 | + concatString += buffer[i]; |
| 60 | + } |
| 61 | + string signature; |
| 62 | + DSA::Signer signer(privA); |
| 63 | + StringSource ss1(concatString, true, |
| 64 | + new SignerFilter(rng, signer, |
| 65 | + new HexEncoder(new StringSink(signature)) |
| 66 | + ) |
| 67 | + ); |
| 68 | + return signature; |
| 69 | +} |
| 70 | + |
| 71 | +/* This method is used to signal that a timeout has happened. |
| 72 | +I am unsure whether this will be final as a method or if we find a better implementation. |
| 73 | +*/ |
| 74 | +static bool timeout(){ |
| 75 | + struct timespec newTime; |
| 76 | + clock_gettime(CLOCK_MONOTONIC, &newTime); |
| 77 | + |
| 78 | + if(newTime.tv_sec - oldTime.tv_sec >= 2.0){ // every two seconds a timeout occurs |
| 79 | + oldTime = newTime; |
| 80 | + return true; |
| 81 | + } |
| 82 | + else{ |
| 83 | + return false; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +int main(int argc, char** argv){ |
| 90 | + struct timespec start; |
| 91 | + struct timespec finish, globalstart; |
| 92 | + double elapsed; |
| 93 | + |
| 94 | + //get start times for measurements |
| 95 | + clock_gettime(CLOCK_MONOTONIC, &start); |
| 96 | + clock_gettime(CLOCK_MONOTONIC, &globalstart); |
| 97 | + |
| 98 | + int j; |
| 99 | + const int a = A; |
| 100 | + const int b = B; |
| 101 | + const int c = C; |
| 102 | + |
| 103 | + ofstream Log; |
| 104 | + EVENT* currentEvent; |
| 105 | + |
| 106 | + string buffer[b]; |
| 107 | + string abuffer[(int) b/a]; |
| 108 | + string vbuffer[(int)b/c]; |
| 109 | + string vbuffer2[(int)b/c]; |
| 110 | + |
| 111 | + // Generate Private Key |
| 112 | + privA.GenerateRandomWithKeySize(rng, 2048); |
| 113 | + |
| 114 | + // Generate Public Key |
| 115 | + pubA.AssignFrom(privA); |
| 116 | + if (!privA.Validate(rng, 3) || !pubA.Validate(rng, 3)){ |
| 117 | + throw std::runtime_error("DSA key generation failed"); |
| 118 | + } |
| 119 | + |
| 120 | + //write first public key to a file |
| 121 | + { |
| 122 | + FileSink ouput("firstkey.dat"); |
| 123 | + pubA.DEREncode(ouput); |
| 124 | + } |
| 125 | + |
| 126 | + //used to track whether one of the main events was already handled |
| 127 | + int oldJa = 0; |
| 128 | + int oldJb = 0; |
| 129 | + int oldJc = 0; |
| 130 | + |
| 131 | + //empty all buffers beforehand |
| 132 | + std::fill(buffer,buffer+b,""); |
| 133 | + std::fill(abuffer,abuffer+b/a,""); |
| 134 | + std::fill(vbuffer,vbuffer+b/c,""); |
| 135 | + std::fill(vbuffer2,vbuffer+b/c,""); |
| 136 | + |
| 137 | + j = 0; |
| 138 | + |
| 139 | + // used to measure the time it takes to initialize the program |
| 140 | + clock_gettime(CLOCK_MONOTONIC, &finish); |
| 141 | + elapsed = (finish.tv_sec - start.tv_sec); |
| 142 | + elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; |
| 143 | + std::cout << elapsed << std::endl; |
| 144 | + |
| 145 | + //used to measure the time it takes to handle one block of size b |
| 146 | + clock_gettime(CLOCK_MONOTONIC, &start); |
| 147 | + |
| 148 | + // the main loop |
| 149 | + // the order in which the following if clauses are, is CRUCIAL |
| 150 | + while(j<N){ |
| 151 | + if ((j % a) == 0 && j != oldJa){ |
| 152 | + string Z = computeAuth(buffer,((j-a) % b), ((j-1) % b) ); |
| 153 | + abuffer[(int)(((j-1)%b)/a)] = Z; |
| 154 | + oldJa = j; |
| 155 | + } |
| 156 | + |
| 157 | + if((j % c) == 0 && j != 0 && j != oldJc){ |
| 158 | + // Generate Private Key |
| 159 | + DSA::PrivateKey privateKey; |
| 160 | + privateKey.GenerateRandomWithKeySize(rng, 2048); |
| 161 | + |
| 162 | + // Generate Public Key |
| 163 | + DSA::PublicKey publicKey; |
| 164 | + publicKey.AssignFrom(privateKey); |
| 165 | + if (!privateKey.Validate(rng, 3) || !publicKey.Validate(rng, 3)){ |
| 166 | + throw std::runtime_error("DSA key generation failed"); |
| 167 | + } |
| 168 | + |
| 169 | + //generate encoded public key |
| 170 | + string encodedPubKey; |
| 171 | + publicKey.Save(HexEncoder( |
| 172 | + new StringSink(encodedPubKey)).Ref() |
| 173 | + ); |
| 174 | + |
| 175 | + //generate encoded signature of encoded public key |
| 176 | + string signature; |
| 177 | + DSA::Signer signer(privA); |
| 178 | + StringSource ss1(encodedPubKey, true, |
| 179 | + new SignerFilter(rng, signer, |
| 180 | + new HexEncoder(new StringSink(signature)) |
| 181 | + ) |
| 182 | + ); |
| 183 | + |
| 184 | + //overwrite keys and store the new key and signature in vbuffers |
| 185 | + privA = privateKey; |
| 186 | + pubA = publicKey; |
| 187 | + vbuffer[(((j-1) % b)/c)] = signature; |
| 188 | + vbuffer2[(((j-1) % b)/c)] = encodedPubKey; |
| 189 | + oldJc = j; |
| 190 | + } |
| 191 | + |
| 192 | + if ((j % b) == 0 && j != 0 && j != oldJb){ |
| 193 | + //used to measure the time it takes to handle one block of size b |
| 194 | + clock_gettime(CLOCK_MONOTONIC, &finish); |
| 195 | + elapsed = (finish.tv_sec - start.tv_sec); |
| 196 | + elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; |
| 197 | + std::cout << elapsed << std::endl; |
| 198 | + |
| 199 | + Log.open("output.txt", std::ofstream::out | std::ofstream::app); |
| 200 | + Log << "##### j is now " << j << " #############################################" << std::endl; |
| 201 | + Log << "The content of buffer:" << std::endl; |
| 202 | + for (int i = 0; i< b; i++){ |
| 203 | + Log << buffer[i] << std::endl; |
| 204 | + } |
| 205 | + Log << "The content of abuffer:" << std::endl; |
| 206 | + for (int i = 0; i< (int)b/a; i++){ |
| 207 | + Log <<abuffer[i] << std::endl; |
| 208 | + } |
| 209 | + Log << "The content of vbuffer:" << std::endl; |
| 210 | + for (int i = 0; i< (int)b/c; i++){ |
| 211 | + Log << vbuffer[i] << std::endl; |
| 212 | + } |
| 213 | + Log << "The content of vbuffer2:" << std::endl; |
| 214 | + for (int i = 0; i< (int)b/c; i++){ |
| 215 | + Log << vbuffer2[i] << std::endl; |
| 216 | + } |
| 217 | + Log.close(); |
| 218 | + |
| 219 | + std::fill(buffer,buffer+b,""); |
| 220 | + std::fill(abuffer,abuffer+b/a,""); |
| 221 | + std::fill(vbuffer,vbuffer+b/c,""); |
| 222 | + std::fill(vbuffer2,vbuffer2+b/c,""); |
| 223 | + oldJb = j; |
| 224 | + |
| 225 | + //initialize time for measuring a block b |
| 226 | + clock_gettime(CLOCK_MONOTONIC, &start); |
| 227 | + } |
| 228 | + |
| 229 | + if(timeout()){// if timeout then write metronome message with current time |
| 230 | + string output; |
| 231 | + char buf[27]; |
| 232 | + struct timespec ts; |
| 233 | + clock_gettime(CLOCK_REALTIME, &ts); |
| 234 | + ctime_r(&ts.tv_sec, buf); |
| 235 | + output = buf; |
| 236 | + output.erase(std::remove(output.begin(), output.end(), '\n'), output.end()); |
| 237 | + buffer[(j % b)] = output; |
| 238 | + j++; |
| 239 | + } |
| 240 | + else{ |
| 241 | + currentEvent = getNextEvent(); |
| 242 | + if(currentEvent){ |
| 243 | + string l = currentEvent->getLog(); |
| 244 | + buffer[(j % b)] = l; |
| 245 | + j++; |
| 246 | + delete currentEvent; |
| 247 | + } |
| 248 | + else{ |
| 249 | + sleep(1); |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + // used to measure the time it takes to execute the whole program |
| 255 | + clock_gettime(CLOCK_MONOTONIC, &finish); |
| 256 | + elapsed = (finish.tv_sec - globalstart.tv_sec); |
| 257 | + elapsed += (finish.tv_nsec - globalstart.tv_nsec) / 1000000000.0; |
| 258 | + std::cout << elapsed << std::endl; |
| 259 | + |
| 260 | + exit(EXIT_SUCCESS); |
| 261 | +} |
| 262 | + |
0 commit comments