TReK ANSI-C  5.3.3
All ANSI-C APIs
network_encrypt_decrypt/main.c
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program creates a public/private Elliptic Curve Cryptography (ECC) key
// pair and a peer public and peer private ECC key pair. The program creates,
// sends and receives four encrpyted packets via two UDP socket prior to
// decrypting the packets and logs info and error messages. Program uses
// RegisterPrintMessage, StartLoggingMessages, CreateUnicastUDPSocketDevice,
// GeneratePublicPrivateKeyPair, GeneratePublicPrivateKeyPairWithPassphrase,
// AddCipherToDevice, AddCipherToDeviceWithPeerIPAddress,
// RegisterReceivePacketFromSocket and SendPacketFromUDPSocketDevice API
// functions. Note, TReK encryption library support is provided on 32 bit
// and 64 bit Linux operating systems and 64 bit Windows operating systems.
// TReK encryption library support is not available on 32 bit Window operating
// systems.
//
// ////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include "ds_shared.h"
#include "trek_error.h"
#ifdef __linux__
#include <unistd.h>
#else
#include <windows.h>
#endif
// ////////////////////////////////////////////////////////////////////////////
//
// PrintTheMessage controls how messages are processed and displayed to
// the user.
//
// ////////////////////////////////////////////////////////////////////////////
void PrintTheMessage(message_struct_type *mess_struct_ptr)
{
if (mess_struct_ptr->category == MSG_CAT_INFO ||
mess_struct_ptr->category == MSG_CAT_INFO_ALERT ||
mess_struct_ptr->category == MSG_CAT_ERROR ||
mess_struct_ptr->category == MSG_CAT_ERROR_ALERT)
{
printf("%s\t%s\n",GetMessageCategoryAsString(mess_struct_ptr->category),
mess_struct_ptr->message);
}
}
// ////////////////////////////////////////////////////////////////////////////
//
// ReceivePacket's packet_buffer_ptr points to the newly received packet.
//
// ////////////////////////////////////////////////////////////////////////////
void ReceivePacket (const char *device_key,
int packet_length,
unsigned char *packet_buffer_ptr,
const char *received_from_ip_address,
unsigned short recieved_from_port)
{
// Print the data zone packets.
printf("Data\t%s\n",(char *)packet_buffer_ptr + 5);
}
// ////////////////////////////////////////////////////////////////////////////
//
// CreateDataPackets creates the data packets with a three byte header.
// The first three header bytes contains a sync hex pattern = fafbfc.
// The fourth and fifth header bytes contain the overall packet length in big
// endian byte order.
//
// ////////////////////////////////////////////////////////////////////////////
void CreateDataPackets(unsigned char *packet1_ptr,
unsigned char *packet2_ptr,
unsigned char *packet3_ptr,
unsigned char *packet4_ptr,
unsigned short *length1_ptr,
unsigned short *length2_ptr,
unsigned short *length3_ptr,
unsigned short *length4_ptr)
{
// Determine if this is a little endian or big endian platform .
unsigned short tester= 1;
unsigned char tester_byte[1];
unsigned char sync_pattern[3];
unsigned short big_endian_order = 1;
memcpy(tester_byte,&tester,1);
if (tester_byte[0] == 0x01)
{
big_endian_order = 0;
}
// Zero fill the packets prior to populating.
memset(packet1_ptr,0x00,50);
memset(packet2_ptr,0x00,50);
memset(packet3_ptr,0x00,50);
memset(packet4_ptr,0x00,50);
// Create a five byte header for all the packets. The header is populated
// with the sync bytes and a big endian two byte overall packet length.
// Populate packet data zone
strcpy((char *)packet1_ptr+5,"Mary had a little lamb");
strcpy((char *)packet2_ptr+5,"Its feet were black as soot");
strcpy((char *)packet3_ptr+5,"And into Mary's bread and jam ");
strcpy((char *)packet4_ptr+5,"Its sooty foot it put");
// Get packet length including 5 byte header and null terminator.
*length1_ptr = strlen((char *)packet1_ptr+5) + 6;
*length2_ptr = strlen((char *)packet2_ptr+5) + 6;
*length3_ptr = strlen((char *)packet3_ptr+5) + 6;
*length4_ptr = strlen((char *)packet4_ptr+5) + 6;
// Populate headers.
// Populate packet length field.
if (big_endian_order == 1)
{
memmove(packet1_ptr+3,length1_ptr,2);
memmove(packet2_ptr+3,length2_ptr,2);
memmove(packet3_ptr+3,length3_ptr,2);
memmove(packet4_ptr+3,length4_ptr,2);
}
else
{
memmove(packet1_ptr+4,length1_ptr,1);
memmove(packet2_ptr+4,length2_ptr,1);
memmove(packet3_ptr+4,length3_ptr,1);
memmove(packet4_ptr+4,length4_ptr,1);
}
// Populate sync field.
sync_pattern[0] = 0xfa;
sync_pattern[1] = 0xfb;
sync_pattern[2] = 0xfc;
memcpy(packet1_ptr,sync_pattern,3);
memcpy(packet2_ptr,sync_pattern,3);
memcpy(packet3_ptr,sync_pattern,3);
memcpy(packet4_ptr,sync_pattern,3);
}
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program creates a public/private Elliptic Curve Cryptography (ECC) key
// pair and a peer public and peer private ECC key pair. The program creates,
// sends and receives four encrypted packets via two UDP socket prior to
// decrypting the packets and logs info and error messages. Program uses
// RegisterPrintMessage, StartLoggingMessages, CreateUnicastUDPSocketDevice,
// GeneratePublicPrivateKeyPair, GeneratePublicPrivateKeyPairWithPassphrase,
// AddCipherToDevice, AddCipherToDeviceWithPeerIPAddress,
// RegisterReceivePacketFromSocket and SendPacketFromUDPSocketDevice API
// functions. Note, TReK encryption library support is provided on 32 bit
// and 64 bit Linux operating systems and 64 bit Windows operating systems.
// TReK encryption library support is not available on 32 bit Window operating
// systems.
//
// ////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
char ip_address[32];
unsigned short source_port;
unsigned short receive_port;
unsigned int receive_queue_size;
unsigned int receive_buffer_size;
char source_device_key[50];
char receive_device_key[50];
unsigned char packet1[50];
unsigned char packet2[50];
unsigned char packet3[50];
unsigned char packet4[50];
unsigned short length1;
unsigned short length2;
unsigned short length3;
unsigned short length4;
unsigned int key_buffer_size;
char path[256];
char log_filename[256];
char home_path[256];
char public_key_pathname[256];
char private_key_pathname[256];
char user_passphrase[64];
char peer_public_key_pathname[256];
char peer_private_key_pathname[256];
char peer_user_passphrase[64];
cipher_class_type cipher_class;
int pkt_key_encrypt_time_interval_sec;
char peer_ip_address[32];
// Create data packets.
CreateDataPackets(packet1,
packet2,
packet3,
packet4,
&length1,
&length2,
&length3,
&length4);
// Register the PrintMessage callback function prior to InitToolkitCfdp
// to process error messages that may be generated during initialization.
RegisterMessage(&PrintTheMessage);
printf("\nNetwork Encrypt Decrypt\n\n");
// Create a log file in the home/<username> or Users/<username> directory
// by passing a empty string for directory path. Only log error and
// information messages.
strcpy(path,"");
strcpy(log_filename,"network_encrypt_decrypt_log_file");
log_filename,
if (TCAACGetHomeDirectory(home_path, sizeof(home_path)) != SUCCESS)
{
printf("\nFailed to get the home directory\n");
return(FAIL);
}
// Create a ECC public/private key pair
sprintf(public_key_pathname, "%s/example_public.key", home_path);
sprintf(private_key_pathname, "%s/example_private.key", home_path);
if (GeneratePublicPrivateKeyPair(public_key_pathname, private_key_pathname) != SUCCESS)
{
return 0;
}
// Create a ECC peer_public/peer_private key pair
sprintf(peer_public_key_pathname, "%s/example_peer_public.key", home_path);
sprintf(peer_private_key_pathname, "%s/example_peer_private.key", home_path);
strcpy(peer_user_passphrase, "qwert12345");
if (GeneratePublicPrivateKeyPairWithPassphrase(peer_public_key_pathname,
peer_private_key_pathname, peer_user_passphrase) != SUCCESS)
{
return 0;
}
// Create a UDP source socket device.
strcpy(ip_address,"127.0.0.1"); // Use loopback address.
source_port = 20130;
// A receive queue or buffer is not required since this socket is only
// sending packets.
receive_queue_size = 0;
receive_buffer_size = 0;
key_buffer_size = sizeof(source_device_key);
source_port,
receive_queue_size,
receive_buffer_size,
&key_buffer_size,
source_device_key) != SUCCESS)
{
return 0;
}
// Attach a cipher to the socket device. The cipher will encyrpt
// all the packets of data that are sent by the socket and decrypt
// all the packets of data that are received by the socket.
// The same cipher must be used for both the send and recieve devices
cipher_class = AES_256_GCM;
// Set the time interval defining how often the encryption key
// is changed (A zero value will change the encryption key
// with every packet though this could impact performance for
// high packet rate streams). The default value is 10 seconds.
pkt_key_encrypt_time_interval_sec = 0;
// No passphrase was used to encrypt the private key so pass
// and empty string
strcpy(user_passphrase, "");
if (AddCipherToDevice(source_device_key,
cipher_class,
public_key_pathname,
private_key_pathname,
peer_public_key_pathname,
pkt_key_encrypt_time_interval_sec,
user_passphrase) != SUCCESS)
{
return 0;
}
// Create a UDP receive socket device
receive_port = 20140;
// If the receive socket is receiving hundreds of packets a second
// and receive queue minimizes the chances of packets being dropped.
receive_queue_size = 100;
receive_buffer_size = 100;
key_buffer_size = sizeof(receive_device_key);
receive_port,
receive_queue_size,
receive_buffer_size,
&key_buffer_size,
receive_device_key) != SUCCESS)
{
return 0;
}
// Attach a cipher to the socket device. The cipher will encyrpt
// the packets of data that are sent by the socket to the
// peer IP address and decrypt the packets of data that are
// received by the socket from the peer IP address.
strcpy(peer_ip_address, "127.0.0.1");
if (AddCipherToDeviceWithPeerIPAddress(receive_device_key,
cipher_class,
peer_public_key_pathname,
peer_private_key_pathname,
public_key_pathname,
pkt_key_encrypt_time_interval_sec,
peer_ip_address,
peer_user_passphrase) != SUCCESS)
{
return 0;
}
// Associate the RecieveDeviceData callback function with the receive
// socket device so all packets received by the device will
// be processed by RecieveDeviceData callback function.
if (RegisterReceivePacketFromSocket (receive_device_key,
&ReceivePacket) != SUCCESS)
{
return 0;
}
// Send the four data packets to the receive socket via the source socket.
// The source socket was not connected to the receive socket so the
// SendPacket function requires a destination IP address and port for each
// transmission.
if (SendPacketFromUDPSocketDevice (source_device_key,
length1,packet1,
ip_address,
receive_port) == SUCCESS)
{
if (SendPacketFromUDPSocketDevice (source_device_key,
length2,
packet2,
ip_address,
receive_port) == SUCCESS)
{
if (SendPacketFromUDPSocketDevice (source_device_key,
length3,
packet3,
ip_address,
receive_port) == SUCCESS)
{
SendPacketFromUDPSocketDevice (source_device_key,
length4,
packet4,
ip_address,
receive_port);
}
}
}
// Allow time to print and log messages.
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
// Close the log file. A timetag is appended to the log file name.
return 0;
}
Message codes and structure definition for the Device Service library.
cipher_class_type
Cipher class enumeration.
Definition: ds_shared.h:118
@ FALSE_OR_NO
False.
Definition: ds_shared.h:105
Structure of parameters needed for message support.
Definition: trek_toolkit_common_api_ansi_c.h:74
char message[MAX_MESSAGE_SIZE]
Message.
Definition: trek_toolkit_common_api_ansi_c.h:77
enum message_category category
Message category (e.g., MSG_CAT_ERROR, MSG_CAT_ERROR_ALERT, MSG_CAT_WARNING, MSG_CAT_WARNING_ALERT,...
Definition: trek_toolkit_common_api_ansi_c.h:76
An ANSI C Data Service API.
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION CreateUnicastUDPSocketDevice(const char *ip_address, unsigned short port, unsigned int receive_queue_size, unsigned int receive_buffer_size, unsigned int *device_key_buffer_size_ptr, char *device_key)
Creates a UDP socket to send and receive packets at the specified IP address and port....
Definition: toolkit_ds_api_ansi_c.cpp:126
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION GeneratePublicPrivateKeyPairWithPassphrase(const char *public_key_pathname, const char *private_key_pathname, const char *crypt_user_passphrase)
Generates a public/private key pair based on Elliptic Curve Cryptography (ECC) using curve P-256.
Definition: toolkit_ds_api_ansi_c.cpp:2886
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION DSCleanUp()
Initiates a graceful shutdown of the Device Service library and all supporting device libraries,...
Definition: toolkit_ds_api_ansi_c.cpp:3843
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION SendPacketFromUDPSocketDevice(const char *device_key, int packet_length, unsigned char *packet_buffer_ptr, const char *send_to_ip_address, unsigned short send_to_port)
Sends a packet from UDP socket device to the IP address and port number of a corresponding UDP socket...
Definition: toolkit_ds_api_ansi_c.cpp:1921
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION AddCipherToDevice(const char *device_key, cipher_class_type cipher_class, const char *public_key_pathname, const char *private_key_pathname, const char *peer_public_key_pathname, int pkt_key_encrypt_time_interval_sec, const char *crypt_user_passphrase)
The AddCipherToDevice function encrypts all data being sent by a device and decrypts all data prior t...
Definition: toolkit_ds_api_ansi_c.cpp:2996
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION StopLoggingMessages()
Stops logging messages to a file, closes the log file and renames the log file by concatenating the l...
Definition: toolkit_ds_api_ansi_c.cpp:3479
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION RegisterMessage(void(*function_ptr)(message_struct_type *message_struct_ptr))
Register a callback function to receive and process messages issued by the DS library.
Definition: toolkit_ds_api_ansi_c.cpp:3951
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION RegisterReceivePacketFromSocket(const char *device_key, void(*function_ptr)(const char *device_key, int packet_length, unsigned char *packet_buffer_ptr, const char *received_from_ip_address, unsigned short received_from_port))
Register a callback function to receive packets from a socket device.
Definition: toolkit_ds_api_ansi_c.cpp:4088
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION GeneratePublicPrivateKeyPair(const char *public_key_pathname, const char *private_key_pathname)
Generates a public/private key pair based on Elliptic Curve Cryptography (ECC) using curve P-256.
Definition: toolkit_ds_api_ansi_c.cpp:2832
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION StartLoggingMessages(const char *log_file_path, const char *log_filename, boolean_type log_debug_messages)
Starts logging messages to a file.
Definition: toolkit_ds_api_ansi_c.cpp:3339
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION AddCipherToDeviceWithPeerIPAddress(const char *device_key, cipher_class_type cipher_class, const char *public_key_pathname, const char *private_key_pathname, const char *peer_public_key_pathname, int pkt_key_encrypt_time_interval_sec, const char *peer_ip_address, const char *crypt_user_passphrase)
The AddCipherToDeviceWithPeerIPAddress function encrypts all data being sent by a device to the peer ...
Definition: toolkit_ds_api_ansi_c.cpp:3120
Error codes for the DS API (starts at 50001)
Command codes for TReK.
#define SUCCESS
The function completed successfully.
Definition: trek_error.h:8
#define FAIL
The function failed for an unknown reason.
Definition: trek_error.h:9
The commonly shared macros, structures and functions.
@ MSG_CAT_INFO_ALERT
Information alert message (supports information alert pop up dialog box)
Definition: trek_toolkit_common_api_ansi_c.h:61
@ MSG_CAT_INFO
Information message.
Definition: trek_toolkit_common_api_ansi_c.h:60
@ MSG_CAT_ERROR
Error message.
Definition: trek_toolkit_common_api_ansi_c.h:56
@ MSG_CAT_ERROR_ALERT
Error alert message (supports error alert pop up dialog box)
Definition: trek_toolkit_common_api_ansi_c.h:57
int EXPORT_THIS_TREK_TOOLKIT_COMMON_API_FUNCTION TCAACGetHomeDirectory(char *directory_buffer, int buffer_size)
Gets the path to the users home directory.
Definition: trek_toolkit_common_api_ansi_c.cpp:142
const char EXPORT_THIS_TREK_TOOLKIT_COMMON_API_FUNCTION * GetMessageCategoryAsString(enum message_category input)
Converts an enumerated message category value into its equivalent character string.
Definition: trek_toolkit_common_api_ansi_c.cpp:45