Skip to content

Commit 423d9e5

Browse files
Ricky Ubuntu 24.04Ricky Ubuntu 24.04
authored andcommitted
Working on cpp project
1 parent 4bab015 commit 423d9e5

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "dlms_crypto_tool.h"
2+
#include <openssl/evp.h>
3+
#include <openssl/rand.h>
4+
#include <sstream>
5+
#include <iomanip>
6+
#include <stdexcept>
7+
8+
std::string generate_key()
9+
{
10+
11+
unsigned char key[16];
12+
13+
if(!RAND_bytes(key, sizeof(key)))
14+
{
15+
16+
throw std::runtime_error("Failed to generate encryption key");
17+
18+
}
19+
20+
std::ostringstream oss;
21+
22+
for (int i = 0; i < 16; ++i)
23+
{
24+
25+
oss << std::hex << std::setw(2) << std::setfill('0') << (int)key[i];
26+
27+
}
28+
29+
return oss.str();
30+
31+
}
32+
33+
std::vector<unsigned char> create_iv(const std::string& system_title, const std::string& frame_counter)
34+
{
35+
36+
std::vector<unsigned char> iv;
37+
38+
iv.insert(iv.end(), system_title.begin(), system_title.end());
39+
iv.insert(iv.end(), frame_counter.begin(), frame_counter.end());
40+
41+
return iv;
42+
43+
}
44+
45+
std::string encrypt_apdu(const std::string& system_title, const std::string& frame_counter,
46+
const std::string& encryption_key, const std::string& additional_auth_data,
47+
const std::string& plaintext)
48+
{
49+
50+
// Prepare key and iv
51+
unsigned char key[16];
52+
53+
std::vector<unsigned char> iv = create_iv(system_title, frame_counter);
54+
55+
if(iv.size() != 12)
56+
{
57+
58+
throw std::invalid_argument("IV must be 12 bytes");
59+
60+
}
61+
62+
for (size_t i = 0; i < encryption_key.length() / 2; ++i)
63+
{
64+
65+
key[i] = std::stoi(encryption_key.substr(2 * i, 2), nullptr, 16);
66+
67+
}
68+
69+
// Prepare plaitext and AAD
70+
std::vector<unsigned char> plaintext_bytes(plaintext.begin(), plaintext.end());
71+
std::vector<unsigned char> aad = SECURITY_HEADER_DATA + additional_auth_data; // TODO fix error
72+
73+
// Prepare buffers for ciphertext and tag
74+
std::vector<unsigned char> cipheretext(plaintext_bytes.size());
75+
unsigned char tag[16];
76+
77+
78+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef DLMS_CRYPTO_TOOL_H
2+
#define DLMS_CRYPTO_TOOL_H
3+
4+
#include <string>
5+
#include <vector>
6+
7+
// Security header constants
8+
const std::string SECURITY_HEADER_AUTH = "10";
9+
const std::string SECURITY_HEADER_DATA = "30";
10+
11+
// Function to generate a random 16 byte encryption key
12+
std::string generate_key();
13+
14+
// Function to create an IV by concatenating system_title and frame_counter
15+
std::vector<unsigned char> create_iv(const std::string& system_title, const std::string& frame_counter);
16+
17+
// Function to encrypt an APDU message
18+
std::string encrypt_apdu(const std::string& system_title, const std::string& frame_counter,
19+
const std::string& encryption_key, const std::string& additional_auth_data,
20+
const std::string& plaintext);
21+
22+
// Function to decrypt an APDU message
23+
std::string decrypt_apdu(const std::string& system_title, const std::string& frame_counter,
24+
const std::string& encryption_key, const std::string& additional_auth_data,
25+
const std::string& ciphertext_with_tag);
26+
27+
// Function to authenticate an APDU
28+
std::string decrypt_apdu(const std::string& system_title, const std::string& frame_counter,
29+
const std::string& encryption_key, const std::string& authentication_key,
30+
const std::string& stoc);
31+
32+
#endif // DLMS_CRYPTO_TOOL_H

0 commit comments

Comments
 (0)