1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
39 #include <anna/core/functions.hpp>
41 #include <anna/core/util/Encoder.hpp>
47 void Encoder::initialize()
49 srand(functions::second());
52 const EncodedData& Encoder::encode(const DataBlock& data)
53 throw(RuntimeException) {
55 DES_key_schedule* skey [3];
57 for(int i = 0; i < 3; i ++) {
58 DES_random_key(&key[i]);
59 skey [i] = &a_data.a_skey [i];
61 if(DES_set_key_checked(&key[i], skey [i]) < 0) {
62 string msg("anna::Encoder::encode | DataBlock: ");
63 msg += functions::asString(data);
64 msg += " | Error during key generation";
65 throw RuntimeException(msg, ANNA_FILE_LOCATION);
70 anna_memset(&a_data.a_iv, 0, sizeof(DES_cblock));
71 const DataBlock& output = setDataBlock(data);
72 unsigned char* aux = (unsigned char*) output.getData();
73 DES_ede3_cbc_encrypt(aux, aux, output.getSize(), skey[0], skey[1], skey[2], &a_data.a_iv, DES_ENCRYPT);
76 string msg("anna::Encoder::encode | DataBlock: ");
77 msg += functions::asString(data);
78 msg += " | Encoding error";
79 throw RuntimeException(msg, ANNA_FILE_LOCATION);
85 //-------------------------------------------------------------------------------------------------
86 // (1) Elimina los 8 primeros bytes que tuvimos que poner de relleno en la codificacion.
87 //-------------------------------------------------------------------------------------------------
88 const DataBlock& Encoder::decode(const EncodedData& data)
89 throw(RuntimeException) {
90 if(data.a_realSize <= 0) {
91 string msg("anna::Encoder::encode | Cannot decode an uninitialized data");
92 throw RuntimeException(msg, ANNA_FILE_LOCATION);
95 unsigned char* text = (unsigned char*) data.a_value.getData();
96 DES_key_schedule* skey [3];
98 skey [0] = const_cast <DES_key_schedule*>(&data.a_skey [0]);
99 skey [1] = const_cast <DES_key_schedule*>(&data.a_skey [1]);
100 skey [2] = const_cast <DES_key_schedule*>(&data.a_skey [2]);
101 iv = const_cast <DES_cblock*>(&data.a_iv);
103 DES_ede3_cbc_encrypt(text, text, data.a_value.getSize(), skey[0], skey[1], skey[2], iv, DES_DECRYPT);
106 string msg("anna::Encoder::encode | Decoding error | Block: ");
107 msg += functions::asString(data.a_value);
108 throw RuntimeException(msg, ANNA_FILE_LOCATION);
111 const_cast <EncodedData&>(data).a_value.remove(sizeof(DES_cblock)); // (1)
113 const_cast <EncodedData&>(data).a_value.allocate(data.a_realSize);
118 //-------------------------------------------------------------------------------------------------
119 // (1) Rellena los 8 primeros bytes que no sabemos interpretar correctamente, lo ponemos para
120 // poder ignorarlos al decodificar. Ver
121 // 'const_cast <EncodedData&> (data).a_value.remove (sizeof (DES_cblock));'
122 //-------------------------------------------------------------------------------------------------
123 DataBlock& Encoder::setDataBlock(const DataBlock& other)
124 throw(RuntimeException) {
125 DataBlock& value = a_data.a_value;
129 for(r = 0; r < sizeof(DES_cblock); r ++) // (1)
133 r = (a_data.a_realSize = other.getSize()) % 8;
136 for(r = 8 - r; r > 0; r --)