This topic contains 15 replies, has 4 voices, and was last updated by  redneonglow 4 months, 1 week ago.

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #1993

    dgv
    Member

    Hello all,

    I have written a library encapsulating operations for TrueRNG devices, and a binary pgm which is only a shell interface for that library.
    You may download the package at https://sites.google.com/site/hawksoftwarepackages/truerng

    Please notice that the testing was far from exhaustive. Comments are welcome.

    cheers,

    Denis/

    #1994

    Ubld.it Staff
    Moderator

    This is great, thanks Denis!

    #2030

    ekinox777
    Member

    Hello!
    Thank you for your work. I’m trying to test your library but without succes. I know some c++ but lack skills in compiling and linking. I couldn’t successfully build a simple program dynamically or statically linking.
    I’m trying with gcc, under Ubuntu.
    Here is the code main.cpp:

    #include <cstdlib>
    #include "truerng.h"
    #include <iostream>
    
    using namespace std;
    
    /*
     * 
     */
    int main(int argc, char** argv) {
        unsigned int nb_bytes =10;
        unsigned char buf[(nb_bytes)];
        int rc;
        //rc = truerng(buf, sizeof(buf));
        rc = truerng_bytes(TRUERNG_PATH_DEFAULT, TRUERNG_ID_ANY, TRUERNG_ID_ANY, TRUERNG_SERIAL_ANY, buf, sizeof(buf));
       for (int i=0;i<nb_bytes;i++) cout << buf[i];
        return 0;
    }

    Trying to link statically I get:

    g++ -Wl,-static libtruerng.a main.o -o trng 
    /usr/bin/ld: cannot find -lgcc_s
    /usr/bin/ld: cannot find -lgcc_s
    collect2: error: ld returned 1 exit status
    

    Trying to link dynamically I get:

    
    g++  -ltruerng main.cpp -o trng 
    main.cpp: In function ‘int main(int, char**)’:
    main.cpp:28:114: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
        rc = truerng_bytes(TRUERNG_PATH_DEFAULT, TRUERNG_ID_ANY, TRUERNG_ID_ANY, TRUERNG_SERIAL_ANY, buf, sizeof(buf));
                                                                                                                     ^
    /tmp/ccRtZRGs.o: In function 
    'main':
    main.cpp:(.text+0xaf): undefined reference to 'truerng_bytes(char*, unsigned short, unsigned short, char*, unsigned char*, unsigned long)'
    collect2: error: ld returned 1 exit status

    All the files (truerng.h, main.cpp, libtruerng.a, libtruerng.so, libtruerng.so.2.0.0) are in the same folder.

    I tried to understand the compiling and linking of the binary pgm “truerng” but it seems too complicated for my skills. Is there a simple way to compile and link my example, without using makefile and libtool?

    cheers,
    Mihai

    • This reply was modified 2 years, 8 months ago by  ekinox777.
    • This reply was modified 2 years, 8 months ago by  ekinox777.
    • This reply was modified 2 years, 8 months ago by  ekinox777.
    #2034

    dgv
    Member

    Hello Mihai,

    Short answer (long answer below):

    0/ I assume you have built the package with libudev. If not, please look at the configure output transcript and
    replace below “-ludev” with the flags found by “configure”.

    1/ in your file “main.cpp”, replace
    #include “truerng.h”
    by

    // tell the C++ compiler we are using C code:
    extern "C" {
    #include "truerng.h"
    }

    2.a/ link with static library (libtruerng.so in the directory):
    g++ -Wno-write-strings -o main main.cpp ./libtruerng.a -ludev

    2.b/ link with static library (libtruerng.so removed from the directory):
    g++ -Wno-write-strings -o main main.cpp -L. -ltruerng -ludev

    2.c/ link with shared library:
    g++ -Wno-write-strings -Wl,-rpath=pwd-o main main.cpp -Lpwd-ltruerng -ludev

    Explainations (long answer): It seems to me that you have several issues, which I will discuss separately.

    A/ You write your program in C++
    The library is written in C, not in C++. So the fundamental problem is to call an external function written in a foreign language.
    The basic principle is to tell the C++ main pgm that the library is in C.
    This is why you should apply step 1/ above.

    B/ warning: deprecated conversion from string constant to ‘char*’
    This is because the prototypes are declared with “char *” and the compiler wants “const char *”.
    I admit I should have done that :(
    The easy solution is to add an option telling the compiler to shut up because you know what you’re doing.
    Use -Wno-write-strings when compiling.

    C/ linking with dynamic versus static library
    You should put the “-ltruerng” option *after* “main”; because the linker goes from left to right for unresolved symbols.
    I recommend to link with the static lib, which is easier for testing. So either you build the package
    with “./configure … –disable-shared”, or you “rm libtruerng.so*”, or you explicitly use the static library filepath.

    D/ the output of your main program
    When you call “./main”, you should see 10 “scrambled” characters, since the main pgm prints directly the random characters.
    You may prefer to pipe the output into “od(1)” in order to have a human-readable output:
    ./main | od -x

    E/ I warmly recommend using a Makefile. This permit encapsulating flags, and easy modification.
    Indeed, a Makefile is an integral part of your testing procedure. Here is a proposal:

    PROG =		main
    CXXFLAGS =	-Wno-write-strings
    LOADLIBES =	./libtruerng.a
    LDLIBS =	-ludev
    
    .PHONY:		all check clean
    
    all: $(PROG)
    
    check: all
    	./$(PROG) | od -x
    
    clean:
    	$(RM) $(PROG).o $(PROG) $(PROG).core core

    Best regards,

    Denis/

    #2035

    ekinox777
    Member

    Thank you for your help!
    The linking is working now without errors.

    Regarding libudev:
    I built the library without libudev or libusb.
    When I try to build the library WITH libudev “make all” works w/o errors but “make check” fails.
    when running the binary file I get this error:

    mihai@bhairava:~/Soft/TrueRNG/truerng-2.0$ src.bin/truerng --list
    
    Searching...
    lt-truerng: truerng_open.c:196: truerng_libudev: Assertion 'Serial_found != NULL' failed.
    Aborted (core dumped)

    Built without libudev or libusb I get no errors, but it is unable to detect ids and serial:

    mihai@bhairava:~/Soft/TrueRNG/truerng-2.0$ src.bin/truerng --list
    
    Searching...
    file path : /dev/TrueRNG
    idVendor  : <unknown>
    idProduct : <unknown>
    Product   : <unknown>
    Serial    : <unknown>
    
    Configuring...
    state     : ON
    mode      : NORMAL  300 baud

    Perhaps it is because I use a TrueRNG v3(?)

    Anyway, I can use now the library. , which seems to give a higher bitrate than using /dev/random (via rng-tools).

    gratefully yours,
    Mihai

    #2036

    ekinox777
    Member

    Using this library I get an output speed of ~406kbs, while using /dev/random (enabling truerng through rng-tool) I get only ~80kbs. (without truerng the speed of /dev/random is very low, ~3-4 bytes/s).
    Why there is such a big diference? Why does entropy pool not use all available output of TrueRNG?

    #2037

    dgv
    Member

    Hello Mihai,

    Thanks for your feedback.

    The “assertion failed” problem is concerning; the only way I see to fix this is to have a V3 device to play with.
    If the friendly staff at UBLD are reading this, I declare myself volunteer to adapt this package to the V3 device <wink> <wink>.

    Also, I have put the C++ compatibility in my TODO list.

    With regards to the difference of throughput
    – I did not write “True”put :) — the explanation is not simple. In order to really understand the reason, you should understand the code of the Linux /dev/random driver.
    In a nutshell, Linux adds entropy into the pool *when needed*. It has its own whitening, and does not assume the entropy pool is fast, verbose, and really “random”; i.e. it does not assume there is a TRNG available.
    In order to solve this issue, one may write a kernel-resident driver for TrueRNG devices, in the same spirit than the specific OpenBSD driver for the AleaII device (www.Araneus.fi).

    Best regards,

    Denis/

    #2038

    Ubld.it Staff
    Moderator

    Denis, e-mail us directly and we’ll help you out.
    sales@ubld.it

    #2054

    dgv
    Member

    Hello all,

    The package truerng-2.1 is now available at https://sites.google.com/site/hawksoftwarepackages/truerng

    It fixes several bugs raised by Mihai in earlier postings:
    1/ the library is now C++ compatible
    2/ “char *” replaced by “const char *” in function prototypes where needed.
    3/ supporting V3 model, fully tested.
    4/ generation of the shared library is now disabled by default.

    With regards to the V3 model, I discovered with great surprise that it does not have a built-in unique serial number. This raises the question on how to uniquely identify a specific device if several V3 are plugged-in. A promising approach is to use the USB “busnum:devnum”, and I have put this on my TODO list for a forthcoming version 3.0 of the package.

    I have tested the package with 2 TrueRNG devices plugged-in: one TrueRNGpro and one V3 dongle. This showed that the udev rules provided by UBLD do not permit to handle correctly several devices; the /dev/TrueRNG soft link being unique.
    Therefore, I propose to modify the file 99-TrueRNG.rules with:
    SYMLINK+="TrueRNG TrueRNG.%n"
    and 99-TrueRNGpro.rules with:
    SYMLINK+="TrueRNG TrueRNGpro.%n"

    This permits /dev/TrueRNG to be always created, pointing to any TrueRNG device, thus being used by rng-tools. Another unique soft link is also created, permitting access to a specific device. Here is a listing of my own system:

    $ ls -ld /dev/TrueRNG* /dev/ttyACM*
    lrwxrwxrwx 1 root root         7 Mar  7 20:57 /dev/TrueRNG -> ttyACM1
    lrwxrwxrwx 1 root root         7 Mar  7 20:57 /dev/TrueRNG.1 -> ttyACM1
    lrwxrwxrwx 1 root root         7 Mar  7 11:33 /dev/TrueRNGpro.0 -> ttyACM0
    crw-rw---- 1 root dialout 166, 0 Mar  7 11:33 /dev/ttyACM0
    crw-rw---- 1 root dialout 166, 1 Mar  7 20:57 /dev/ttyACM1

    Best regards,

    Denis/

    #2055

    dgv
    Member

    Hello random ppls,

    The package truerng-3.0 is now available at https://sites.google.com/site/hawksoftwarepackages/truerng

    It now takes into account the USB bus:dev number to select a specific device. It can also provide a listing of all connected devices.
    Testing and feedback are welcome.

    cheers,

    Denis/

    #2144

    redneonglow
    Member

    Since I bought the Pro model I’ve been editing my favorite old Roguelikes to use /dev/random via getrandom() as that will allow me to use something else instead of/together with /dev/TrueRNG. However, the Web instructions to change the mode of TrueRNG Pro don’t seem to work at all for me, while your program does. Thank you!

    #2233

    dgv
    Member

    Hello random colleagues,

    I have written (some time ago) an “engine” interface for OpenSSL, interfacing with a TrueRNG device. Now I took the time to wrap this up into the library, and I make it available to anyone who wants to use *directly* a TrueRNG device via OpenSSL (or similar packages such as LibreSSL or BoringSSL). One can have a look at the OpenSSL engine handling the Intel RDRAND; the engine I wrote is similar. It is quite easy to use in any application pgm.

    The package is available at

    I have tested on a Linux box with a TrueRNGpro and a TrueRNG V3 device. I have compiled successfully on an OpenBSD system. I have tried OpenSSL versions 1.0 and 1.1 and LibreSSL.

    These tests are far from complete and I welcome any comment or feedback, in particular on Solaris, FreeBSD and NetBSD.

    cheers,

    Denis/

    #2234

    dgv
    Member

    Oh ohhh… the missing link in the prev post is

    https://sites.google.com/site/hawksoftwarepackages/truerng

    Denis/

    #2235

    redneonglow
    Member

    I’m having problems with the OpenSSL engine.

    * openssl rand -engine truerng -hex 16
    * demo_openssl

    work as expected, but when i try

    * openssl engine -c -t -vvvv truerng

    I get:

    139865069244800:error:25066067:DSO support routines:DLFCN_LOAD:could not load the shared library:dso_dlfcn.c:187:filename(/usr/lib64/engines/libtruerng.so): /usr/lib64/engines/libtruerng.so: cannot open shared object file: No such file or directory
    139865069244800:error:25070067:DSO support routines:DSO_load:could not load the shared library:dso_lib.c:233:
    139865069244800:error:260B6084:engine routines:DYNAMIC_LOAD:dso not found:eng_dyn.c:467:
    139865069244800:error:2606A074:engine routines:ENGINE_by_id:no such engine:eng_list.c:391:id=truerng

    A note that truerng.so is in /usr/local/lib, not /usr/lib64/engines.

    I tried copying and pasting the contents of truerng.cnf into /etc/ssl/openssl.cnf, but it didn’t make a difference.

    #2236

    dgv
    Member

    Hello redneonglow,

    You’re touching here a delicate issue: make your OpenSSL installation be
    aware of a custom engine.

    The bad side is that openssl 1.0 has a different setting than version 1.1

    The good side is that your openssl installation is telling you what it expects.
    The first line of the error msg says it is looking for
    /usr/lib64/engines/libtruerng.so

    So try to define a soft-link “libtruerng.so” in /usr/lib64/engines
    pointing to /usr/local/lib/libtruerng.so

    Denis/

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic.