1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
11 #include <anna/core/functions.hpp>
13 #include <anna/core/util/Encoder.hpp>
19 void Encoder::initialize()
21 srand(functions::second());
24 const EncodedData& Encoder::encode(const DataBlock& data)
27 DES_key_schedule* skey [3];
29 for(int i = 0; i < 3; i ++) {
30 DES_random_key(&key[i]);
31 skey [i] = &a_data.a_skey [i];
33 if(DES_set_key_checked(&key[i], skey [i]) < 0) {
34 string msg("anna::Encoder::encode | DataBlock: ");
35 msg += functions::asString(data);
36 msg += " | Error during key generation";
37 throw RuntimeException(msg, ANNA_FILE_LOCATION);
42 anna_memset(&a_data.a_iv, 0, sizeof(DES_cblock));
43 const DataBlock& output = setDataBlock(data);
44 unsigned char* aux = (unsigned char*) output.getData();
45 DES_ede3_cbc_encrypt(aux, aux, output.getSize(), skey[0], skey[1], skey[2], &a_data.a_iv, DES_ENCRYPT);
48 string msg("anna::Encoder::encode | DataBlock: ");
49 msg += functions::asString(data);
50 msg += " | Encoding error";
51 throw RuntimeException(msg, ANNA_FILE_LOCATION);
57 //-------------------------------------------------------------------------------------------------
58 // (1) Elimina los 8 primeros bytes que tuvimos que poner de relleno en la codificacion.
59 //-------------------------------------------------------------------------------------------------
60 const DataBlock& Encoder::decode(const EncodedData& data)
62 if(data.a_realSize <= 0) {
63 string msg("anna::Encoder::encode | Cannot decode an uninitialized data");
64 throw RuntimeException(msg, ANNA_FILE_LOCATION);
67 unsigned char* text = (unsigned char*) data.a_value.getData();
68 DES_key_schedule* skey [3];
70 skey [0] = const_cast <DES_key_schedule*>(&data.a_skey [0]);
71 skey [1] = const_cast <DES_key_schedule*>(&data.a_skey [1]);
72 skey [2] = const_cast <DES_key_schedule*>(&data.a_skey [2]);
73 iv = const_cast <DES_cblock*>(&data.a_iv);
75 DES_ede3_cbc_encrypt(text, text, data.a_value.getSize(), skey[0], skey[1], skey[2], iv, DES_DECRYPT);
78 string msg("anna::Encoder::encode | Decoding error | Block: ");
79 msg += functions::asString(data.a_value);
80 throw RuntimeException(msg, ANNA_FILE_LOCATION);
83 const_cast <EncodedData&>(data).a_value.remove(sizeof(DES_cblock)); // (1)
85 const_cast <EncodedData&>(data).a_value.allocate(data.a_realSize);
90 //-------------------------------------------------------------------------------------------------
91 // (1) Rellena los 8 primeros bytes que no sabemos interpretar correctamente, lo ponemos para
92 // poder ignorarlos al decodificar. Ver
93 // 'const_cast <EncodedData&> (data).a_value.remove (sizeof (DES_cblock));'
94 //-------------------------------------------------------------------------------------------------
95 DataBlock& Encoder::setDataBlock(const DataBlock& other)
97 DataBlock& value = a_data.a_value;
101 for(r = 0; r < sizeof(DES_cblock); r ++) // (1)
105 r = (a_data.a_realSize = other.getSize()) % 8;
108 for(r = 8 - r; r > 0; r --)