So I was following this tutorial: https://nickfarrow.com/Cryptography-in-Bitcoin-with-C/ I installed libsecp256k1 from https://www.howtoinstall.me/ubuntu/18-04/libsecp256k1-dev/ but while compiling my program:
#include <secp256k1.h>
#include <stdio.h>
static secp256k1_context *ctx = NULL;
int main()
{
ctx = secp256k1_context_create(
SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
/* Declare the private variable as a 32 byte unsigned char */
unsigned char seckey[32];
/* Load private key (seckey) from random bytes */
FILE *frand = fopen("/dev/urandom", "r");
/* Read 32 bytes from frand */
fread(seckey, 32, 1, frand);
/* Close the file */
fclose(frand);
/* Loop through and print each byte of the private key, */
printf("Private Key: ");
for (int i = 0; i < 32; i++)
{
printf("%02X", seckey[i]);
}
printf("\n");
}
i get:
josh#pc:~/Code$ gcc prvkey.c -o exec
/tmp/cc5OVPMJ.o: In function `main':
prvkey.c:(.text+0x1d): undefined reference to `secp256k1_context_create'
collect2: error: ld returned 1 exit status
Thanks in advance!
Try:
gcc prvkey.c -o exec -lcrypto -lsecp256k1
gcc -l links with a library file.
Let me know if that works or any questions let me know.
Related
I am writing a program in C that contains two source files, one containing main() and one containing function definitions. I also have a header file declaring the functions and variables so I don't have to pass variables back and forth. Here is the content of the files.
main.c:
#include <stdio.h>
#include "defs.h"
#include "sorting.c"
int main(int argc, char **argv){
listLength = 0;/*this is a load of barnacles*/
EOFFlag = 1;
input = fopen(argv[1], "r");
if(input == NULL){
printf("Could not open requested file.\n");
return -1;
}
while(EOFFlag){
sortNextWord();
}
if(FULLLISTCONSEQUENCES){
for(genericIterator = 0; genericIterator < USHRT_MAX + 1; genericIterator++){
printf("%d: %s; %d", genericIterator + 1, wordlist[genericIterator], wordcount[genericIterator]);
}
}else{
for(genericIterator = 0; genericIterator < listLength; genericIterator++){
printf("%d: %s; %d", genericIterator + 1, wordlist[genericIterator], wordcount[genericIterator]);
}
}
return 1;
}
sorting.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "defs.h"
int sortNextWord(){
getNextWord();
if(wordHolder[0] != '\0')/*checks to ensure that getNextWord didn't get an empty string*/
listMatch();
return 1;
}
int getNextWord(){
char charStore;
char returnFlag = 0;
genericIterator = 0;
while(genericIterator != USHRT_MAX){
switch(charStore = getc(input)){
case EOF:
EOFFlag = 0;
goto exitcond;/* checks for EOF or whitespace, exits loop if found*/
case ' ':
case '\n':
case '\t':
goto exitcond;
default:
wordHolder[genericIterator++] = charStore;/* otherwise it writes the character it read to the next available spot */
break;
}
}
exitcond:
wordHolder[genericIterator] = '\0';
wordlen = genericIterator;
return 1;
}
int listMatch(){/* runs through wordlist to find a matching word. If found, increments the respective wordcount entry, otherwise, creates new entry in wordlist */
for(genericIterator = 0; genericIterator < listLength; genericIterator++){
if(strcmp(wordHolder, wordlist[genericIterator])){
++wordcount[genericIterator];
goto foundmatch;
}
}
addToList();
foundmatch:
return 1;
}
int addToList(){ /*adds word to next available spot in wordlist*/
char *string;
if(FULLLISTCONSEQUENCES) /*doesnt do anything if wordlist is full */
return 0; /* John Freeman, who was Go */
if(listLength == USHRT_MAX)
FULLLISTCONSEQUENCES = 1;
string = malloc((wordlen + 1) * sizeof(char));
string[0] = '\0';
strncat(string, wordHolder, USHRT_MAX + 1);
wordcount[listLength] = 1;
wordlist[listLength++] = string;
return 1;`
}
defs.h (ignore the stuff about heapsort. I'm going to implement that to sort the wordlist, but i need to make sure this works first.
#ifndef DEFS_H
#define DEFS_H
#include <limits.h>
#include <stdio.h>
char *wordlist[USHRT_MAX + 1];
int wordcount [USHRT_MAX + 1];
unsigned short listLength;
unsigned short genericIterator;
FILE *input;
/*heapsort goes here */
char wordHolder[USHRT_MAX + 1];
unsigned short wordlen;
char EOFFlag;
char FULLLISTCONSEQUENCES;
int heapsort();
int buildMaxHeap();
int restoreMaxHeap();
int swap(unsigned short, unsigned short);
unsigned short getParent(unsigned short);
unsigned short getRightChild(unsigned short);
unsigned short getLeftChild(unsigned short);
int sortNextWord();
int getNextWord();
int listMatch();
int addToList();
#endif`
When I run those through gcc with the command
gcc -o wordsort main.c sorting.c
I get the following error:
/usr/bin/ld: /tmp/cc8X0W8R.o:(.bss+0x0): multiple definition of `wordlist'; /tmp/cc3dFw3Q.o:(.bss+0x0): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o:(.bss+0x80000): multiple definition of `wordcount'; /tmp/cc3dFw3Q.o:(.bss+0x80000): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o:(.bss+0xc0000): multiple definition of `listLength'; /tmp/cc3dFw3Q.o:(.bss+0xc0000): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o:(.bss+0xc0002): multiple definition of `genericIterator'; /tmp/cc3dFw3Q.o:(.bss+0xc0002): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o:(.bss+0xc0008): multiple definition of `input'; /tmp/cc3dFw3Q.o:(.bss+0xc0008): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o:(.bss+0xc0020): multiple definition of `wordHolder'; /tmp/cc3dFw3Q.o:(.bss+0xc0020): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o:(.bss+0xd0020): multiple definition of `wordlen'; /tmp/cc3dFw3Q.o:(.bss+0xd0020): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o:(.bss+0xd0022): multiple definition of `EOFFlag'; /tmp/cc3dFw3Q.o:(.bss+0xd0022): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o:(.bss+0xd0023): multiple definition of `FULLLISTCONSEQUENCES'; /tmp/cc3dFw3Q.o:(.bss+0xd0023): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o: in function `sortNextWord':
main.c:(.text+0x0): multiple definition of `sortNextWord'; /tmp/cc3dFw3Q.o:sorting.c:(.text+0x0): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o: in function `getNextWord':
main.c:(.text+0x2a): multiple definition of `getNextWord'; /tmp/cc3dFw3Q.o:sorting.c:(.text+0x2a): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o: in function `listMatch':
main.c:(.text+0xdf): multiple definition of `listMatch'; /tmp/cc3dFw3Q.o:sorting.c:(.text+0xdf): first defined here
/usr/bin/ld: /tmp/cc8X0W8R.o: in function `addToList':
main.c:(.text+0x191): multiple definition of `addToList'; /tmp/cc3dFw3Q.o:sorting.c:(.text+0x191): first defined here
collect2: error: ld returned 1 exit status
What is happening? I've only defined these functions once in sorting.c. Where did this error come from?
I've tried removing some of the #include headers, and I added the #ifndef statement to defs.h. Neither made any difference. I've also tried compiling and then linking as suggested in https://stackoverflow.com/questions/18777326/compiling-multiple-c-files-with-gcc`your text`
Even though we set currentMethod.bytes with local function to generate random numbers, the RAND_bytes is not invoking. After we set RAND_set_rand_method(&cuurentMethod).
Here I attached link [https://github.com/openssl/openssl/blob/master/test/sm2_internal_test.c] which I already tried.
int main()
{
unsigned char rand[16];
int ret;
RAND_METHOD *oldMethod,currentMethod,*temp;
oldMethod = RAND_get_rand_method();/*getting default method*/
currentMethod = *oldMethod;
currentMethod.bytes = local_function_rand;
if((ret = RAND_set_rand_method(¤tMethod))!= 1)
return 0;
/* Now we are printing both address of local_function_method_rand() and
temp->bytes , those address are same after getting. */
temp = RAND_get_rand_method();
/* after we are comparing with RAND_SSLeay() function , to find default or not*/
if((ret = RAND_bytes(rand,16)) != 1)
return 0;
return 1;
}
Expecting result is our local function should invoke. Also, to invoke RAND_bytes() is it required to set fips mode in Linux system?
After cleaning up and minimizing your test program and filling in the missing parts:
#include <openssl/rand.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int local_function_rand(unsigned char *buf, int num) {
printf("in local_function_rand(); requested %d bytes\n", num);
memset(buf, 4, num); // RFC 1149.5 standard random number
return 1;
}
int main(void) {
unsigned char rand[16];
RAND_METHOD currentMethod = {.bytes = local_function_rand};
RAND_set_rand_method(¤tMethod);
if (RAND_bytes(rand, sizeof rand) != 1) {
return EXIT_FAILURE;
}
return 0;
}
and running it (With OpenSSL 1.1.1):
$ gcc -Wall -Wextra rand.c -lcrypto
$ ./a.out
in local_function_rand(); requested 16 bytes
it works as expected; the user-supplied function is being called by RAND_bytes(). If you're getting different results from your code, there's probably a problem in the bits you didn't include in your question.
I was following an os tutorial. I could compile the code successfully but encountered an issue while linking the object files binaries.
ld: error: loader.o:1:1: invalid character
Here is my code for your reference:
Loader in nasm
; Loader.asm
bits 32
extern _main
global start
start:
call _main ; Call our kernel's main() function
cli ; Stop interrupts (thats another article?)
hlt ; Stop all instructions
My C code
//main.c
int main( void )
{
puts("Hello, world!"); /* Print our welcome message */
for(;;); /* Keep the OS running */
}
/* video.c */
int x, y; /* Our global 'x' and 'y' */
char color; /* Our global color attribute */
void putc( unsigned char c )
{
char *vidmem = (char*)0xB8000; /* pointer to video memory */
int pos = ( y * 2 ) + x; /* Get the position */
vidmem[pos] = c; /* print the character */
vidmem[pos++] = color; /* Set the color attribute */
if (c == '\n') // newline
{
y++;
x = 0;
}
else
x += 2; // 2 bytes per char
}
int puts( char *message )
{
int length;
while(*message)
{
putc(*message++);
length++;
}
return length;
}
I compiled these by running:
gcc -ffreestanding -fno-builtin -nostdlib -c *.c // (that's main.c and video.c)
nasm -f aout loader.asm -o loader.o
You should ask nasm to create elf output, not aout. That is, use -f elf.
I wanted to compare AES algorithm performance from libtomcrypt in Windows and Ubuntu by creating a benchmark-like file, but I have got errors while coding it. Please help me. Below is my file for comparing:
Compare.c:
`#include <time.h> `
#include <tomcrypt.h>
#define MIN_TIME 10.0
#define MIN_ITERS 20 `
double test_rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) {
int iterations = 0;
clock_t start;
double elapsed=0.0;
int out;
start=clock();
do{
out = rijndael_ecb_encrypt(pt, ct, skey);
iterations++;
elapsed=(clock()-start)/(double)CLOCKS_PER_SEC;
} while(elapsed<MIN_TIME || iterations<MIN_ITERS);
elapsed=1000.0*elapsed/iterations;
printf("%s \n",pt);
printf("%s \n",skey->data);
printf("%s \n",ct);
printf("iterations: %8d \n",iterations);
printf("%8.2lf ms per iteration \n",elapsed);
printf("out: %d \n",out);
return elapsed;
}
int main() {
unsigned char pt[22]="-K4)<i50~'APg2fa7DiV";
unsigned char ct[22];
unsigned char key[16]="EDB1C6D13FC72";
symmetric_key *skey;
int err;
double tout1;
printf("%x",sizeof(pt));
printf("%l",sizeof(key));
if((err=rijndael_setup(key,16,0,skey))!=CRYPT_OK) {
printf("%s",error_to_string(err));
return -1;
}
tout1=test_rijndael_ecb_encrypt(pt,ct,skey);
printf("%s \n",ct);
printf("%f",tout1);
return 0;
}
But when I compile this it shows runtime errors as:
gcc -o "TestC" ./src/TestC.o
./src/TestC.o: In function `test_rijndael_ecb_encrypt':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:27: undefined reference to `rijndael_ecb_encrypt'
./src/TestC.o: In function `test_rijndael_ecb_decrypt':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:53: undefined reference to `rijndael_ecb_decrypt'
./src/TestC.o: In function `main':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:82: undefined reference to `rijndael_setup'
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:83: undefined reference to `error_to_string'
collect2: error: ld returned 1 exit status
make: *** [TestC] Error 1
Where did I go wrong?
You forgot to link with tomcrypt library. Compile with -ltomcrypt to link the library:
gcc file.c -ltomcrypt
I am trying to compile a simple introductory program using ffmpeg that tries to check if the mp3 codec is available. While the code compiles OK, I am facing difficulty in solving linker errors. Here is the code:
#include <stdio.h>
#include <math.h>
#include <libavcodec/avcodec.h>
float *generateSinusoid(unsigned int sampleRate, unsigned int nSecondsAudio) {
unsigned int nsamples = (nSecondsAudio * sampleRate);
float *arr;
arr = (float*) malloc(sizeof(float) * nsamples);
int i = 0;
for(i = 0; i < nsamples; i++) {
arr[i] = 20 * sin(2.f * (M_PI) * (330/sampleRate) * i); /*frequency of 330H
z*/
}
return arr;
}
int main(int argc, char *argv[]) {
avcodec_register_all();
AVCodec *codec;
unsigned int sampleRate = 22050; /*assumed.*/
unsigned int nSecondsAudio = 4;
float *arr;
arr = (float *) malloc(sizeof(float) * nSecondsAudio * sampleRate);
/*Step 1. Generate sinusoid.*/
arr = generateSinusoid(sampleRate, nSecondsAudio);
/* Step 2. See if encoder exists.*/
/*codec = avcodec_find_encoder(AV_CODEC_ID_MP3);*/
if(!codec) { /*codec = NULL.*/
printf("MP3 codec not found!!!!");
} else {
printf("MP3 codec found!!!");
}
return 0;
}
The code is compiled and linked like so:
encoding_mp3: encoding_mp3.o
gcc encoding_mp3.o -o encoding_mp3 -L/cygdrive/c/Users/Desktop/webserver/cygnus/lib/w32api -L/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/lib -lm -luser32 -lpthread -lavcodec
encoding_mp3.o: encoding_mp3.c
gcc -I/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/include -I/cygdrive/c/Users/Desktop/webserver/cygnus/usr/include -g -c encoding_mp3.c -o encoding_mp3.o
clean:
rm encoding_mp3.o encoding_mp3
Linking gives the following error:
gcc -I/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/include -I/cygdrive/c/Users/Desktop/webserver/cygnus/usr/include -g -c encoding_mp3.c -o encoding_mp3.o
gcc encoding_mp3.o -o encoding_mp3 -L/cygdrive/c/Users/Desktop/webserver/cygnus/lib/w32api -L/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/lib -lm -luser32 -lpthread -lavcodec
encoding_mp3.o: In function `main':
/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/work/encoding_mp3.c:31: undefined reference to `_avcodec_register_all'
collect2: ld returned 1 exit status
make: *** [encoding_mp3] Error 1
I have gone through most of the threads on SO regarding this problem and here is what I have tried so far:
- Put libraries at the end of all non-option arguments
- Commented out code that references functions. This seems to work. The undefined reference errors go away after all function calls are removed, though the presence of a struct AVCodec does not cause any problems.
Any help on this is most welcome.