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
37 #include <anna/core/util/EncodedData.hpp>
38 #include <anna/core//DataBlock.hpp>
40 #include <anna/xml/Node.hpp>
41 #include <anna/xml/Attribute.hpp>
43 #include <anna/core/util/Tokenizer.hpp>
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);
53 anna_memcpy(&a_skey[0], a_value.getData(), sizeof(DES_key_schedule));
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);
58 anna_memcpy(&a_skey[1], a_value.getData(), sizeof(DES_key_schedule));
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);
63 anna_memcpy(&a_skey[2], a_value.getData(), sizeof(DES_key_schedule));
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);
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);
74 xml::Node* EncodedData::asXML(xml::Node* parent) const
75 throw(RuntimeException) {
76 parent->createAttribute("Mode", "DES3");
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));
93 const std::string& EncodedData::asBCD(const DataBlock& source, string& result)
96 const char* data = source.getData();
100 for(int len = 0, maxlen = source.getSize(); len < maxlen; len ++, data ++) {
102 byte = (c & 0xf0) >> 4;
103 result += (byte <= 9) ? (byte + '0') : (byte - 0x0a + 'a');
105 result += (byte <= 9) ? (byte + '0') : (byte - 0x0a + 'a');
111 int EncodedData::fromBCD(const string& source, DataBlock& result)
114 const char* data = source.c_str();
115 int len = 0, maxlen = source.length();
119 while(len < maxlen) {
121 byte = (c <= '9') ? (c - '0') : (c - 'a' + 0x0a);
124 if(++ len < maxlen) {
126 byte |= (c <= '9') ? (c - '0') : (c - 'a' + 0x0a);
129 result += char(byte);
133 return result.getSize();