SIde-Channel Analysis toolKit (SICAK)
Software toolkit for side-channel analysis
filehandling.hpp
Go to the documentation of this file.
1 /*
2 * SICAK - SIde-Channel Analysis toolKit
3 * Copyright (C) 2018 Petr Socha, FIT, CTU in Prague
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18 
30 #ifndef FILEHANDLING_HPP
31 #define FILEHANDLING_HPP
32 
33 #include <fstream>
34 #include <string>
35 #include "types_basic.hpp"
36 #include "types_power.hpp"
37 #include "types_stat.hpp"
38 #include "exceptions.hpp"
39 #include <iostream>
40 
47 std::fstream openOutFile(const char * filename){
48 
49  std::fstream fs(filename, std::ios_base::binary | std::ios_base::out);
50 
51  if (fs.fail())
52  throw RuntimeException("Could not open the file. Wrong filename or permissions?");
53 
54  return fs;
55 
56 }
57 
64 std::fstream openInFile(const char * filename){
65 
66  std::fstream fs(filename, std::ios_base::binary | std::ios_base::in);
67 
68  if (fs.fail())
69  throw RuntimeException("Could not open the file. Wrong filename or permissions?");
70 
71  return fs;
72 
73 }
74 
81 void closeFile(std::fstream & fs){
82 
83  fs.flush();
84 
85  if (fs.fail())
86  throw RuntimeException("Failed to flush the ouput buffers on file.");
87 
88  fs.close();
89 
90 }
91 
98 template<class T>
99 void fillArrayFromFile(std::fstream & fs, ArrayType<T> & arr){
100 
101  fs.read(reinterpret_cast<char *>(arr.data()), arr.size());
102 
103  if(fs.fail())
104  throw RuntimeException("Could not read the data from the file. Not enough data?");
105 
106 }
107 
114 template<class T>
115 Vector<T> loadPowerTraceFromFile(std::fstream & fs, size_t samplesPerTrace, size_t trace){
116 
117  fs.seekg(sizeof(T) * samplesPerTrace * trace);
118 
119  if(fs.fail())
120  throw RuntimeException("Could not skip offset. Not enough data?");
121 
122  Vector<T> arr;
123  arr.init(samplesPerTrace);
124 
125  fillArrayFromFile(fs, arr);
126 
127  return arr;
128 
129 }
130 
137 template<class T>
138 Vector<T> loadCorrelationTraceFromFile(std::fstream & fs, size_t samplesPerTrace, size_t noOfCandidates, size_t matrix, size_t candidate){
139 
140  fs.seekg(sizeof(T) * samplesPerTrace * noOfCandidates * matrix + sizeof(T) * samplesPerTrace * candidate);
141 
142  if(fs.fail())
143  throw RuntimeException("Could not skip offset. Not enough data?");
144 
145  Vector<T> arr;
146  arr.init(samplesPerTrace);
147 
148  fillArrayFromFile(fs, arr);
149 
150  return arr;
151 
152 }
153 
160 template<class T>
161 Vector<T> loadTValuesFromFile(std::fstream & fs, size_t samplesPerTrace){
162 
163  fs.seekg(0);
164 
165  if(fs.fail())
166  throw RuntimeException("Could not skip offset. Not enough data?");
167 
168  Vector<T> arr;
169  arr.init(samplesPerTrace);
170 
171  fillArrayFromFile(fs, arr);
172 
173  return arr;
174 
175 }
176 
183 template<class T>
184 void writeArrayToFile(std::fstream & fs, const T * buffer, size_t len){
185 
186  fs.write(reinterpret_cast<const char *>(buffer), len * sizeof(T));
187 
188  if(fs.fail())
189  throw RuntimeException("Could not write the data to the file. Not enough space?");
190 
191 }
192 
199 template<class T>
200 void writeArrayToFile(std::fstream & fs, const ArrayType<T> & arr){
201 
202  fs.write(reinterpret_cast<const char *>(arr.data()), arr.size());
203 
204  if(fs.fail())
205  throw RuntimeException("Could not write the data to the file. Not enough space?");
206 
207 }
208 
215 template<class T>
217 
218  uint64_t ctxAttrs[7];
219 
220  fs.read(reinterpret_cast<char *>(ctxAttrs), sizeof(uint64_t) * 7);
221 
222  if(fs.fail())
223  throw RuntimeException("Failed to read context from file");
224 
225  UnivariateContext<T> ret(ctxAttrs[0], ctxAttrs[1], ctxAttrs[4], ctxAttrs[5], ctxAttrs[6]);
226  ret.p1Card() = ctxAttrs[2];
227  ret.p2Card() = ctxAttrs[3];
228 
229  for(size_t order = 1; order <= ret.mOrder(); order++){
230  fillArrayFromFile(fs, ret.p1M(order));
231  fillArrayFromFile(fs, ret.p2M(order));
232  }
233 
234  for(size_t order = 2; order <= ret.csOrder(); order++){
235  fillArrayFromFile(fs, ret.p1CS(order));
236  fillArrayFromFile(fs, ret.p2CS(order));
237  }
238 
239  for(size_t order = 1; order <= ret.acsOrder(); order++){
240  fillArrayFromFile(fs, ret.p12ACS(order));
241  }
242 
243  return ret;
244 
245 }
246 
253 template<class T>
254 void writeContextToFile(std::fstream & fs, const UnivariateContext<T> & ctx){
255 
256  uint64_t ctxAttrs[7];
257  ctxAttrs[0] = ctx.p1Width();
258  ctxAttrs[1] = ctx.p2Width();
259  ctxAttrs[2] = ctx.p1Card();
260  ctxAttrs[3] = ctx.p2Card();
261  ctxAttrs[4] = ctx.mOrder();
262  ctxAttrs[5] = ctx.csOrder();
263  ctxAttrs[6] = ctx.acsOrder();
264 
265  fs.write(reinterpret_cast<char *>(ctxAttrs), sizeof(uint64_t) * 7);
266 
267  if(fs.fail())
268  throw RuntimeException("Failed to write context to file");
269 
270  for(size_t order = 1; order <= ctx.mOrder(); order++){
271  writeArrayToFile(fs, ctx.p1M(order));
272  writeArrayToFile(fs, ctx.p2M(order));
273  }
274 
275  for(size_t order = 2; order <= ctx.csOrder(); order++){
276  writeArrayToFile(fs, ctx.p1CS(order));
277  writeArrayToFile(fs, ctx.p2CS(order));
278  }
279 
280  for(size_t order = 1; order <= ctx.acsOrder(); order++){
281  writeArrayToFile(fs, ctx.p12ACS(order));
282  }
283 
284 }
285 
286 
287 #endif /* FILEHANDLING_HPP */
UnivariateContext< T > readContextFromFile(std::fstream &fs)
Reads context from file, based on the context's file format.
Definition: filehandling.hpp:216
Vector< T > loadTValuesFromFile(std::fstream &fs, size_t samplesPerTrace)
Loads a t-values trace from file, based on parameters given.
Definition: filehandling.hpp:161
virtual size_t size() const =0
Returns the size of the contained data (i.e. length * sizeof(T))
void closeFile(std::fstream &fs)
Flushes and closes the filestream.
Definition: filehandling.hpp:81
A class representing a vector, stored in the machine's free space.
Definition: types_basic.hpp:217
A class representing a Two-population Univariate Moment-based statistical context.
Definition: types_stat.hpp:43
virtual Matrix< T > & p12ACS(size_t order)
Adjusted central moment sum both populations, order 1 upto acsOrder.
Definition: types_stat.hpp:220
This header file contains exceptions.
virtual T * data()=0
Returns a pointer to the contained data.
virtual size_t mOrder() const
Maximum order of the raw moments, 1 upto mOrder.
Definition: types_stat.hpp:183
virtual size_t csOrder() const
Maximum order of the central moment sums, 2 upto csOrder.
Definition: types_stat.hpp:185
std::fstream openInFile(const char *filename)
Opens filestream for reading.
Definition: filehandling.hpp:64
void writeContextToFile(std::fstream &fs, const UnivariateContext< T > &ctx)
Writes context to file.
Definition: filehandling.hpp:254
virtual size_t p2Width() const
Width of the second population.
Definition: types_stat.hpp:180
virtual size_t & p1Card()
Cardinality of the first population.
Definition: types_stat.hpp:190
Vector< T > loadPowerTraceFromFile(std::fstream &fs, size_t samplesPerTrace, size_t trace)
Loads a power trace from file, based on parameters given.
Definition: filehandling.hpp:115
virtual size_t p1Width() const
Width of the first population.
Definition: types_stat.hpp:178
virtual size_t acsOrder() const
Maximum order of the adjusted central moment sums, 1 upto acsOrder.
Definition: types_stat.hpp:187
virtual void init(size_t length)
Initializes the vector with a specified number of elements.
Definition: types_basic.hpp:249
void writeArrayToFile(std::fstream &fs, const T *buffer, size_t len)
Writes array to file.
Definition: filehandling.hpp:184
Vector< T > loadCorrelationTraceFromFile(std::fstream &fs, size_t samplesPerTrace, size_t noOfCandidates, size_t matrix, size_t candidate)
Loads a correlation trace from file, based on parameters given.
Definition: filehandling.hpp:138
virtual Vector< T > & p2CS(size_t order)
Central moment sum of the second population, order 2 upto csOrder.
Definition: types_stat.hpp:215
This header file contains class templates of basic data containers.
This header file contains class templates of power traces and power consumption containers.
virtual size_t & p2Card()
Cardinality of the second population.
Definition: types_stat.hpp:195
std::fstream openOutFile(const char *filename)
Opens filestream for writing.
Definition: filehandling.hpp:47
virtual Vector< T > & p1CS(size_t order)
Central moment sum of the first population, order 2 upto csOrder.
Definition: types_stat.hpp:210
virtual Vector< T > & p2M(size_t order)
Raw moment of the second population, order 1 upto mOrder.
Definition: types_stat.hpp:205
An exception which cannot be directly influenced by the user, or predicted beforehand.
Definition: exceptions.hpp:76
This header file contains class templates of statistical computational contexts.
void fillArrayFromFile(std::fstream &fs, ArrayType< T > &arr)
Fills array from file, based on the given Array's size.
Definition: filehandling.hpp:99
virtual Vector< T > & p1M(size_t order)
Raw moment of the first population, order 1 upto mOrder.
Definition: types_stat.hpp:200
An abstract class, representing all the array-like data types.
Definition: types_basic.hpp:67