First commit
[anna.git] / source / core / util / Encoder.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <stdlib.h>
38
39 #include <anna/core/functions.hpp>
40
41 #include <anna/core/util/Encoder.hpp>
42
43
44 using namespace std;
45 using namespace anna;
46
47 void Encoder::initialize()
48 throw() {
49   srand(functions::second());
50 }
51
52 const EncodedData& Encoder::encode(const DataBlock& data)
53 throw(RuntimeException) {
54   DES_cblock key [3];
55   DES_key_schedule* skey [3];
56
57   for(int i = 0; i < 3; i ++) {
58     DES_random_key(&key[i]);
59     skey [i] = &a_data.a_skey [i];
60
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);
66     }
67   }
68
69   errno = 0;
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);
74
75   if(errno != 0) {
76     string msg("anna::Encoder::encode | DataBlock: ");
77     msg += functions::asString(data);
78     msg += " | Encoding error";
79     throw RuntimeException(msg, ANNA_FILE_LOCATION);
80   }
81
82   return a_data;
83 }
84
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);
93   }
94
95   unsigned char* text = (unsigned char*) data.a_value.getData();
96   DES_key_schedule* skey [3];
97   DES_cblock* iv;
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);
102   errno = 0;
103   DES_ede3_cbc_encrypt(text, text, data.a_value.getSize(), skey[0], skey[1], skey[2], iv, DES_DECRYPT);
104
105   if(errno != 0) {
106     string msg("anna::Encoder::encode | Decoding error | Block: ");
107     msg += functions::asString(data.a_value);
108     throw RuntimeException(msg, ANNA_FILE_LOCATION);
109   }
110
111   const_cast <EncodedData&>(data).a_value.remove(sizeof(DES_cblock));        // (1)
112
113   const_cast <EncodedData&>(data).a_value.allocate(data.a_realSize);
114
115   return data.a_value;
116 }
117
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;
126   int r;
127   value.clear();
128
129   for(r = 0; r < sizeof(DES_cblock); r ++)                                      // (1)
130     value += char(0xaa);
131
132   value += other;
133   r = (a_data.a_realSize = other.getSize()) % 8;
134
135   if(r > 0) {
136     for(r = 8 - r; r > 0; r --)
137       value += char(0xee);
138   }
139
140   return value;
141 }
142
143
144