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 //
9 #include <anna/core/util/EncodedData.hpp>
10 #include <anna/core//DataBlock.hpp>
12 #include <anna/xml/Node.hpp>
13 #include <anna/xml/Attribute.hpp>
15 #include <anna/core/util/Tokenizer.hpp>
20 void EncodedData::initialize(const xml::Node* parent)
22 if(fromBCD(parent->find("Key1")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_key_schedule))
23 throw RuntimeException("anna::EncodedData::initialize | Key1 no valida", ANNA_FILE_LOCATION);
25 anna_memcpy(&a_skey[0], a_value.getData(), sizeof(DES_key_schedule));
27 if(fromBCD(parent->find("Key2")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_key_schedule))
28 throw RuntimeException("anna::EncodedData::initialize | Key2 no valida", ANNA_FILE_LOCATION);
30 anna_memcpy(&a_skey[1], a_value.getData(), sizeof(DES_key_schedule));
32 if(fromBCD(parent->find("Key3")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_key_schedule))
33 throw RuntimeException("anna::EncodedData::initialize | Key3 no valida", ANNA_FILE_LOCATION);
35 anna_memcpy(&a_skey[2], a_value.getData(), sizeof(DES_key_schedule));
37 if(fromBCD(parent->find("IVector")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_cblock))
38 throw RuntimeException("anna::EncodedData::initialize | IVector no valida", ANNA_FILE_LOCATION);
40 anna_memcpy(&a_iv, a_value.getData(), sizeof(DES_cblock));
41 const xml::Node* node = parent->find("DataBlock");
42 a_realSize = node->getAttribute("Length")->getIntegerValue();
43 fromBCD(node->getAttribute("Value")->getValue(), a_value);
46 xml::Node* EncodedData::asXML(xml::Node* parent) const
48 parent->createAttribute("Mode", "DES3");
51 DataBlock key1((char*) &a_skey[0], sizeof(DES_key_schedule), false);
52 parent->createChild("Key1")->createAttribute("Value", asBCD(key1, aux));
53 DataBlock key2((char*) &a_skey[1], sizeof(DES_key_schedule), false);
54 parent->createChild("Key2")->createAttribute("Value", asBCD(key2, aux));
55 DataBlock key3((char*) &a_skey[2], sizeof(DES_key_schedule), false);
56 parent->createChild("Key3")->createAttribute("Value", asBCD(key3, aux));
57 DataBlock iv((char*) &a_iv, sizeof(DES_cblock), false);
58 parent->createChild("IVector")->createAttribute("Value", asBCD(iv, aux));
59 node = parent->createChild("DataBlock");
60 node->createAttribute("Length", a_realSize);
61 node->createAttribute("Value", asBCD(a_value, aux));
65 const std::string& EncodedData::asBCD(const DataBlock& source, string& result)
68 const char* data = source.getData();
72 for(int len = 0, maxlen = source.getSize(); len < maxlen; len ++, data ++) {
74 byte = (c & 0xf0) >> 4;
75 result += (byte <= 9) ? (byte + '0') : (byte - 0x0a + 'a');
77 result += (byte <= 9) ? (byte + '0') : (byte - 0x0a + 'a');
83 int EncodedData::fromBCD(const string& source, DataBlock& result)
86 const char* data = source.c_str();
87 int len = 0, maxlen = source.length();
93 byte = (c <= '9') ? (c - '0') : (c - 'a' + 0x0a);
98 byte |= (c <= '9') ? (c - '0') : (c - 'a' + 0x0a);
101 result += char(byte);
105 return result.getSize();