nano second clock

Miscellaneous tools
Post Reply
Message
Author
User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

nano second clock

#1 Post by rufwoof »

Post edited for clarity.

C program (you need to load your devx sfs and run gcc nano.c -o nano; chmod +x nano ... assuming you save the following as nano.c. Copy that compiled nano to /usr/local/bin/nano (or elsewhere in your systems setup $PATH)

Code: Select all

/*
	nano seconds since Since Epoch (1st Jan 1970)

	Rufwoof. Dec 2019
*/

#include <stdio.h>	/* for printf 				*/
#include <stdint.h>	/* for uint64 definition 	*/
#include <stdlib.h>	/* for exit() definition 	*/
#include <time.h>	/* for clock_gettime		*/
#include <signal.h> /* for ctrl-C signal trap 	*/

void     INThandler(int);

#define BILLION 1000000000L

int main(int argc, char **argv)
{
	uint64_t diff;
	struct timespec start;
	
	signal(SIGINT, INThandler);

    printf("\e[1;1H\e[2J"); /* clear screen */
	printf("Time in nanoseconds since Epoch (Jan 1970)\n");
	printf("Press Enter to snap a time, Ctrl-C to quit\n\n");
	printf("\e[?25l"); /* hide cursor */
	while (1) {	
		clock_gettime(CLOCK_REALTIME, &start);
		diff = BILLION * (start.tv_sec) + start.tv_nsec;
		printf("%llu\r", (long long unsigned int) diff);
	}
}

void INThandler(int sig)
{
    signal(sig, SIG_IGN);
    printf("\n\e[?25h"); 	/* reshow cursor */
    printf("\e[1;1H\e[2J"); /* clear screen */
    exit(0);
}
See also : https://linux.die.net/man/2/clock_gettime
Last edited by rufwoof on Sat 28 Dec 2019, 11:28, edited 1 time in total.
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#2 Post by Flash »

Here's what I got when I pasted the code into LXTerminal in BionicPup64:

Code: Select all

# /*
bash: /bin: Is a directory
#    nano seconds since Since Epoch (1st Jan 1970)
bash: syntax error near unexpected token `('
# 
#    Rufwoof. Dec 2019
bash: Rufwoof.: command not found
# */
bash: Choices/: Is a directory
# 
# #include <stdio.h>   /* for printf             */
# #include <stdint.h>   /* for uint64 definition    */
# #include <stdlib.h>   /* for exit() definition    */
# #include <time.h>   /* for clock_gettime      */
# #include <signal.h> /* for ctrl-C signal trap    */
# 
# void     INThandler(int);
bash: syntax error near unexpected token `('
# 
# #define BILLION 1000000000L
# 
# int main(int argc, char **argv)
bash: syntax error near unexpected token `('
# {
>    uint64_t diff;
>    struct timespec start;
>    
>    signal(SIGINT, INThandler);
bash: syntax error near unexpected token `SIGINT,'
# 
#     printf("\e[1;1H\e[2J"); /* clear screen */
bash: syntax error near unexpected token `"\e[1;1H\e[2J"'
#    printf("Time in nanoseconds since Epoch (Jan 1970)\n");
bash: syntax error near unexpected token `"Time in nanoseconds since Epoch (Jan 1970)\n"'
#    printf("Press Enter to snap a time, Ctrl-C to quit\n\n");
bash: syntax error near unexpected token `"Press Enter to snap a time, Ctrl-C to quit\n\n"'
#    printf("\e[?25l"); /* hide cursor */
bash: syntax error near unexpected token `"\e[?25l"'
#    while (1) {   
bash: syntax error near unexpected token `{'
#       clock_gettime(CLOCK_REALTIME, &start);
bash: syntax error near unexpected token `CLOCK_REALTIME,'
#       diff = BILLION * (start.tv_sec) + start.tv_nsec;
bash: syntax error near unexpected token `('
#       printf("%llu\r", (long long unsigned int) diff);
bash: syntax error near unexpected token `"%llu\r",'
#    }
bash: syntax error near unexpected token `}'
# }
bash: syntax error near unexpected token `}'
# 
# void INThandler(int sig)
bash: syntax error near unexpected token `('
# {
>     signal(sig, SIG_IGN);
bash: syntax error near unexpected token `sig,'
#     printf("\n\e[?25h");    /* reshow cursor */
bash: syntax error near unexpected token `"\n\e[?25h"'
#     printf("\e[1;1H\e[2J"); /* clear screen */
bash: syntax error near unexpected token `"\e[1;1H\e[2J"'
#     exit(0);
bash: syntax error near unexpected token `0'
# }

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#3 Post by musher0 »

Hi Flash.

I don't know C, except to recognize its code as different from a bash script.
Rufwoof please corect me --, the code in the OP is in C language, not bash.
If you feed a bash console C code, it will indeed complain!

@rufwoof
Why a nano-second clock? Are you thinking about applying to the CERN or similar? :)

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#4 Post by rufwoof »

Edited the OP for clarity.
Why a nano-second clock? Are you thinking about applying to the CERN or similar? :)
Nah! Just for use in Spectre/Meltdown type exploits Musher ... systems still with pre-emptive processing enabled ... only kidding :) Actually I was looking at using the least significant digit(s) as a means to generate my own version of some random data (bytes). Even running the exact same code repeatedly will have different nano-second timings, so the LSD is in effect a real random value (odd digit : output '1' bit, even digit output : '0' bit; shift right 1 and repeat - to build up sequences of random bytes) and where the code/activity being run is that program. If that takes around 1000 nano-seconds per bit produced then 1 million real random bits produced per second (120KB of random bytes/second). But in practice it was too slow for my needs, so for the time being I've opted to revert to using around a 160 random characters (base64 encoding of 128 byte /dev/random value) key encrypted version of /dev/urandom instead.

Code: Select all

dd status=none if=/dev/urandom bs=65536 count=$C | pv --bytes | \
		openssl enc -aes-256-ctr -pass \
		pass:"$(dd if=/dev/random bs=128 count=1 2>/dev/null | base64)" \
		-nosalt > outfile
(dd blocksize of 65536 - because that's the default buffer size that Linux uses for pipe's).

A factor however is that's not appropriate for longer term encrypted storage. Some have already brought cracking aes-256 down by some considerable factors, so perhaps a shelf life for stored data of maybe 5 years or less before having to be decrypted and re-encrypted again using a 'better' method.

/dev/random alone is OK'ish

Code: Select all

dd if=/dev/random bs=32 iflag=fullblock | pv --bytes | head -c $FILESIZE >outfile
possibly more so if mixed in with the above (nano second LSD), as that blocks output of /dev/random bytes until the pool is approximated to have high entropy (low predictability). But when using Vernam Cipher (One Time Pad style) you need a random stream at least as large as the data being encoded, and /dev/random is very slow at producing such large amounts of random data. It's also more open to side channel attacks such as embedded 'distortions/predictable bias'.

Off shelf methods could be used, but again they're subject to back-doors and the algorithms are widely known. Better to use a bespoke algorithm which in itself is part of the 'secret' (keys on one server, pads on another, algorithms on yet another and all three have to be brought together in order to be able to 'open' the data).
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#5 Post by Flash »

Oops. :oops:

Post Reply