Updated license
[anna.git] / source / core / util / EncodedData.cpp
1 // ANNA - Anna is Not Nothingness 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 <anna/core/util/EncodedData.hpp>
38 #include <anna/core//DataBlock.hpp>
39
40 #include <anna/xml/Node.hpp>
41 #include <anna/xml/Attribute.hpp>
42
43 #include <anna/core/util/Tokenizer.hpp>
44
45 using namespace std;
46 using namespace anna;
47
48 void EncodedData::initialize(const xml::Node* parent)
49 throw(RuntimeException) {
50   if(fromBCD(parent->find("Key1")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_key_schedule))
51     throw RuntimeException("anna::EncodedData::initialize | Key1 no valida", ANNA_FILE_LOCATION);
52
53   anna_memcpy(&a_skey[0], a_value.getData(), sizeof(DES_key_schedule));
54
55   if(fromBCD(parent->find("Key2")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_key_schedule))
56     throw RuntimeException("anna::EncodedData::initialize | Key2 no valida", ANNA_FILE_LOCATION);
57
58   anna_memcpy(&a_skey[1], a_value.getData(), sizeof(DES_key_schedule));
59
60   if(fromBCD(parent->find("Key3")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_key_schedule))
61     throw RuntimeException("anna::EncodedData::initialize | Key3 no valida", ANNA_FILE_LOCATION);
62
63   anna_memcpy(&a_skey[2], a_value.getData(), sizeof(DES_key_schedule));
64
65   if(fromBCD(parent->find("IVector")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_cblock))
66     throw RuntimeException("anna::EncodedData::initialize | IVector no valida", ANNA_FILE_LOCATION);
67
68   anna_memcpy(&a_iv, a_value.getData(), sizeof(DES_cblock));
69   const xml::Node* node = parent->find("DataBlock");
70   a_realSize = node->getAttribute("Length")->getIntegerValue();
71   fromBCD(node->getAttribute("Value")->getValue(), a_value);
72 }
73
74 xml::Node* EncodedData::asXML(xml::Node* parent) const
75 throw(RuntimeException) {
76   parent->createAttribute("Mode", "DES3");
77   xml::Node* node;
78   string aux;
79   DataBlock key1((char*) &a_skey[0], sizeof(DES_key_schedule), false);
80   parent->createChild("Key1")->createAttribute("Value", asBCD(key1, aux));
81   DataBlock key2((char*) &a_skey[1], sizeof(DES_key_schedule), false);
82   parent->createChild("Key2")->createAttribute("Value", asBCD(key2, aux));
83   DataBlock key3((char*) &a_skey[2], sizeof(DES_key_schedule), false);
84   parent->createChild("Key3")->createAttribute("Value", asBCD(key3, aux));
85   DataBlock iv((char*) &a_iv, sizeof(DES_cblock), false);
86   parent->createChild("IVector")->createAttribute("Value", asBCD(iv, aux));
87   node = parent->createChild("DataBlock");
88   node->createAttribute("Length", a_realSize);
89   node->createAttribute("Value", asBCD(a_value, aux));
90   return parent;
91 }
92
93 const std::string& EncodedData::asBCD(const DataBlock& source, string& result)
94 throw() {
95   result = "";
96   const char* data = source.getData();
97   int c;
98   int byte;
99
100   for(int len = 0, maxlen = source.getSize(); len < maxlen; len ++, data ++) {
101     c = *data;
102     byte = (c & 0xf0) >> 4;
103     result += (byte <= 9) ? (byte + '0') : (byte - 0x0a + 'a');
104     byte = c & 0x0f;
105     result += (byte <= 9) ? (byte + '0') : (byte - 0x0a + 'a');
106   }
107
108   return result;
109 }
110
111 int EncodedData::fromBCD(const string& source, DataBlock& result)
112 throw() {
113   result.clear();
114   const char* data = source.c_str();
115   int len = 0, maxlen = source.length();
116   int c;
117   int byte;
118
119   while(len < maxlen) {
120     c = *data ++;
121     byte = (c <= '9') ? (c - '0') : (c - 'a' + 0x0a);
122     byte <<= 4;
123
124     if(++ len < maxlen) {
125       c = *data ++;
126       byte |= (c <= '9') ? (c - '0') : (c - 'a' + 0x0a);
127     }
128
129     result += char(byte);
130     len ++;
131   }
132
133   return result.getSize();
134 }
135
136
137