hamlib/tests/testsecurity.c
Nate Bargmann 3fa91c8771
Document fixing MinGW warnings and linker error
This commit has been left as documentation should these files be moved
to /doc or some such.  However, never of these files are built or
distributed.

----------------

From example.c the following warning was generated by MinGW:

  CC       example.o
example.c: In function ‘main’:
example.c:93:32: warning: format ‘%lX’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘rmode_t’ {aka ‘long long unsigned int’} [-Wformat=]
   93 |     printf("Current mode = 0x%lX = %s, width = %ld\n", mode, rig_strrmode(mode),
      |                              ~~^                       ~~~~
      |                                |                       |
      |                                long unsigned int       rmode_t {aka long long unsigned int}
      |                              %llX

The 'l' was added as suggested to the format specifier but that resulted
in the following warning from Linux:

  CC       example.o
../../hamlib/tests/example.c:93:57: warning: format specifies type 'unsigned long long' but the argument has type 'rmode_t' (aka 'unsigned long') [-Wformat]
    printf("Current mode = 0x%llX = %s, width = %ld\n", mode, rig_strrmode(mode),
                             ~~~~                       ^~~~
                             %lX
  CC       rigctl-dumpcaps.o
1 warning generated.

So casting `mode` to `unsigned long long` quelled both warnings!

From testsecurity.c came this warning:

  CC       testsecurity-testsecurity.o
In file included from ../include/hamlib/rig.h:49,
                 from ../src/misc.h:26,
                 from testsecurity.c:29:
/usr/share/mingw-w64/include/winsock2.h:15:2: warning: #warning Please include winsock2.h before windows.h [-Wcpp]
   15 | #warning Please include winsock2.h before windows.h
      |  ^~~~~~~

Apparently winsock2.h being included through misc.h didn't work.

Finally, the Mingw linker gave the following error:

 CCLD     testsecurity.exe
/usr/bin/x86_64-w64-mingw32-ld: testsecurity-testsecurity.o: in function `main':
/home/nate/builds/hamlib-4.7~git/tests/testsecurity.c:97:(.text.startup+0x87): undefined reference to `AESStringCrypt'
/usr/bin/x86_64-w64-mingw32-ld: /home/nate/builds/hamlib-4.7~git/tests/testsecurity.c:116:(.text.startup+0x128): undefined reference to `AESStringDecrypt'
collect2: error: ld returned 1 exit status

Specifcally add the libsecurity.la file path as a specific library.
Even though the libhamlib.la seems to have the AESStringCrypt symbol
already.
2025-08-18 17:24:08 -05:00

131 lines
3.4 KiB
C

/* Borrowed for Hamlib from:
* String Crypt Test (Linux)
* Copyright (C) 2012, 2015
*
* Author: Paul E. Jones <paulej@packetizer.com>
*
* This software is licensed as "freeware." Permission to distribute
* this software in source and binary forms is hereby granted without a
* fee. THIS SOFTWARE IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* THE AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DAMAGES RESULTING FROM
* THE USE OF THIS SOFTWARE, EITHER DIRECTLY OR INDIRECTLY, INCLUDING,
* BUT NOT LIMITED TO, LOSS OF DATA OR DATA BEING RENDERED INACCURATE.
*
*/
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
//#include <Wincrypt.h>
#else
#include <unistd.h>
#endif
#include "AESStringCrypt.h"
#include "password.h"
#include "../src/misc.h"
#if defined(_WIN32)
// gmtime_r can be defined by mingw
#ifndef gmtime_r
static struct tm *gmtime_r(const time_t *t, struct tm *r)
{
// gmtime is threadsafe in windows because it uses TLS
const struct tm *theTm = gmtime(t);
if (theTm)
{
*r = *theTm;
return r;
}
else
{
return 0;
}
}
#endif // gmtime_r
#endif // _WIN32
// using tv_usec with a sleep gives a fairly good random number
static int my_rand(int max)
{
time_t t;
struct timeval tv;
struct tm result;
t = time(NULL);
gmtime_r(&t, &result);
gettimeofday(&tv, NULL);
hl_usleep(100);
int val = tv.tv_usec % max;
return val;
}
void rig_make_key(char key[33])
{
const char *all =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123467890!@#$%^&*()_=~<>/?";
int max = strlen(all);
int i;
for (i = 0; i < 32; ++i)
{
key[i] = all[my_rand(max)];
}
key[32] = 0;
}
int main()
{
char key1[33];
char key2[33];
char plaintext[33];
unsigned char ciphertext[1024];
int ciphertext_length;
int plaintext_length;
memset(ciphertext, 0, sizeof(ciphertext));
rig_make_key(key1);
rig_make_key(key2);
printf("key1=%s\n", key1);
printf("key2=%s\n", key2);
ciphertext_length = AESStringCrypt((unsigned char *) key1,
strlen(key1),
(unsigned char *) key2,
strlen(key2),
(unsigned char *) ciphertext);
for (int i = 0; i < ciphertext_length; ++i) { printf("%02x", ciphertext[i]); }
printf("\n");
if (ciphertext_length == AESSTRINGCRYPT_ERROR)
{
printf("Error encrypting the string\n");
return 1;
}
printf("Ciphertext length: %d\n", ciphertext_length);
memset(plaintext, 0, sizeof(plaintext));
printf("Decrypting...\n");
plaintext_length = AESStringDecrypt((unsigned char *) key1,
strlen(key1),
(unsigned char *) ciphertext,
ciphertext_length,
(unsigned char *) plaintext);
if (plaintext_length == AESSTRINGCRYPT_ERROR)
{
printf("Error decrypting the string\n");
return 1;
}
printf("Decrypted plaintext length: %d, %s\n", plaintext_length, plaintext);
return 0;
}