I'm trying to get the Keygen function of CyaSSL to work using the example in section 7.7 from here: http://www.yassl.com/yaSSL/Docs-cyassl-manual-7-keys-and-certificates.html
I'm using CyaSSL 3.2.0 with the --enable-keygen option, but couldn't get it working with 3.1.0 either.
This is the code:
#include <stdio.h>
#include <cyassl/ctaocrypt/asn.h>
#include <cyassl/ctaocrypt/rsa.h>
int main() {
RsaKey genKey;
RNG rng;
int ret;
printf("%d\n",InitRng(&rng));
printf("%d\n",InitRsaKey(&genKey, 0));
ret = MakeRsaKey(&genKey, 1024, 65537, &rng);
printf("ret: %d\n",ret);
return 0;
}
I get a segmentation fault in the line with InitRsaKey, presumably because of an invalid write or something.
Anyone got an idea where my issue may be? Any help is appreciated
Good morning, please do not forget to include options.h header. This will ensure that you get the proper configuration settings in your project for example if you configure CyaSSL with --enable-keygen then view cyassl/options.h you will see the line #undef CYASSL_KEY_GEN followed by #define CYASSL_KEY_GEN. Also in your makefile do not forget to include the cyassl library. This can be accomplished using -lcyassl in your build line. See code below for reference:
#include <stdio.h>
#include <cyassl/options.h> //pull in the define for CYASSL_KEY_GEN
#include <cyassl/ctaocrypt/asn.h>
#include <cyassl/ctaocrypt/rsa.h>
int main() {
RsaKey genKey;
RNG rng;
int ret;
printf("%d\n",InitRng(&rng));
printf("%d\n",InitRsaKey(&genKey, 0));
ret = MakeRsaKey(&genKey, 1024, 65537, &rng);
printf("ret: %d\n",ret);
return 0;
}
Makefile:
CC=gcc #you can use clang or other instead of gcc
CFLAGS=-Wall
LIBS=-lpthread -lcyassl #must have -lcyassl in makefile
all: run
#NOTE: arrows denote a hard tab, replace them with hard tab in makefile
run: test.o
→→→→$(CC) -o $# $(LIBS) $^ $(CFLAGS) #put $(LIBS) into build command
.PHONY: clean all
clean:
→→→→rm -f *.o test.o run
Related
I have an application that runs fine on Ubuntu but immediately exit at startup, WITHOUT ANY ERRORS, on Windows.
It seems that main() function is not entered.
Application has been compiled without errors and it uses SDL_image.h. When (in the same application) the code that uses SDL Image was not present the application was running fine.
In the .exe directory there are all the needed dlls, SDL2.dll, libjpeg-9.dll, libpng16-16.dll, libtiff-5.dll, libwebp-7.dll, SDL2_image.dll and zlib1.dll.
IMG_Init() is called before any other image functions in this way:
(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG)
and, as I said before, tha application runs without problems on Ubuntu.
This is a reproducible example.
This code works:
makefile
CC = gcc -fno-pie -no-pie -m32
CFLAGS = -IC:/sdl2/include -O0 -ggdb
LIBS = -LC:/sdl2/lib/x86 -lSDL2main -lSDL2
all:
$(CC) $(CFLAGS) main.c $(LIBS) -o application.exe
main.c
#include <stdio.h>
#include "SDL.h"
int main(int argc, char **argv)
{
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
{
SDL_Log("SDL fails to initialize video subsystem!\n%s", SDL_GetError());
return -1;
}
else
printf("SDL Video was initialized fine!\n");
return 0;
}
This code, where I use SDL Image library, doesn't do nothing, no errors, immediately exits:
makefile
CC = gcc -fno-pie -no-pie -m32
CFLAGS = -IC:/sdl2/include -IC:/sdl2_image/include -O0 -ggdb
LIBS = -LC:/sdl2/lib/x86 -LC:/sdl2_image/lib/x86 -lSDL2main -lSDL2 -lSDL2_image
all:
$(CC) $(CFLAGS) main.c $(LIBS) -o application.exe
main.c
#include <stdio.h>
#include "SDL.h"
#include "SDL_image.h"
int main(int argc, char **argv)
{
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
{
SDL_Log("SDL fails to initialize video subsystem!\n%s", SDL_GetError());
return -1;
}
else
printf("SDL Video was initialized fine!\n");
if((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG)
{
SDL_Log("SDL fails to initialize image handler!\n%s", IMG_GetError());
return -1;
}
else
printf("SDL Image was initialized fine!\n");
return 0;
}
As I said before, it seems that the problem is the windows library (or the environment) and this is why I didn't add a reproducible code before
After 2 days I found a solution.
First of all (but it isn't my problem) the SDL Image library is not so "smart" so it is important that SDL2_image.dll library and the used ones from libjpeg-9.dll, libpng16-16.dll, libtiff-5.dll, libwebp-7.dll and zlib1.dll are present. You'll not receive any error if one of needed library is missing.
I had a problem in my environment, on this machine I use Visual Studio and Eclipse. I made a mistake and my makefile pointed to Visual Studio SDL libraries and not to MinGW ones.
I don't know why but the first reproducible example works fine also with this mistake (and without -lmingw32), so it pointed me out of the correct way.
I did this change on makefile and now it works:
CFLAGS = -IC:/sdl2_mingw/i686-w64-mingw32/include/SDL2 -IC:/sdl2_image_mingw/i686-w64-mingw32/include/SDL2 -O0 -ggdb
LIBS = -lmingw32 -LC:/sdl2_mingw/i686-w64-mingw32/lib -LC:/sdl2_image_mingw/i686-w64-mingw32/lib -lSDL2main -lSDL2 -lSDL2_image
I have simplified the code to the minimum
#include "frozen.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
int main()
{
char *json = "{ \"a\": 123, \"b\": \"hi\", c: true }";
int value = 0;
json_scanf(json, strlen(json), "{c: %B}", &value);
printf("Hello World\n");
// assert( json != NULL );
printf( "json: %s\n", json );
printf( "json.c: %s\n", value );
// free( json );
return 0;
}
directory structure:
/home/projects/json-test/main.c
/home/projects/json-test/frozen/{contents of https://github.com/cesanta/frozen repo}
What I do:
gcc main.c -Ifrozen -o main
What is being displayed in output:
main /tmp/ccsYWNAP.o: In function `main': main.c:(.text+0x43):
undefined reference to `json_scanf' collect2: error: ld returned 1
exit status
I have very limited knowledge in C, thus I may be missing some steps, so take into account that I literally did not do anything else than written above, maybe I should have. I am used to loosely typed php/js/python kind of languages, but I was reading that just including file does not tell gcc that "you should search for json_scanf inside frozen.h". Should there be some sort of a "glue", or "linking" step I am missing?
UPDATE: Based on responses, I have created this Makefile:
CC = gcc
FLAGS = -std=c99
DEST_DIR = ./bin
DEST_PATH = "$(DEST_DIR)/main"
BUILD_DIR = ./build
all: clean directories json main.o
$(CC) $(BUILD_DIR)/*.o -o $(DEST_PATH) $(FLAGS)
main.o: src/main.c $(BUILD_DIR)/frozen.o
$(CC) src/main.c -c -o $(BUILD_DIR)/main.o $(FLAGS)
json: json.o
json.o: src/frozen/frozen.c src/frozen/frozen.h
$(CC) src/frozen/frozen.c -c -o $(BUILD_DIR)/frozen.o $(FLAGS)
clean:
rm -rf $(BUILD_DIR)
directories:
mkdir -p $(DEST_DIR)/
mkdir -p $(BUILD_DIR)/
And changing #include "frozen.h" to #include "frozen/frozen.h", and running make, creates build/main file that can be successfully ran with ./bin/main command. Thank you!
Ugh. You don't have a mistake. The library developer has some really bad coding practices. Basically, for whatever reason, his header is not sufficient for compilation. If you look at the unit_test.c in the repository of frozen, you will see he's actually including frozen.c instead of frozen.h. If you change your #include "frozen.h" to #include "frozen.c" it will work fine. The other option is to provide the .c file explicitly:
gcc frozen/frozen.c main.c -Ifrozen
Normally, you'd put everything in the header, or require the library to be compiled, as a .a file and then linked when you use it, but he hasn't provided a makefile that does that.
EDIT: You can also compile frozen.o beforehand, but the library's author should've really provided a makefile to do that...
gcc -c frozen.c -o ../frozen.o
cd ..
gcc main.c frozen.o -Ifrozen
I have a problem with message queues in C in Ubuntu. I use VirtualBox to run the Ubuntu.
I took error which is "undefined reference to mq_open. ld returned 1 exit status".
I know there is a same question as this but I tried that solution but it did not worked, so I want to ask again. Please help!
Here is my code, it is really simple but I can not even compile it.
this is my deneme.c
#include <stdlib.h>
#include <mqueue.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "sharedMsg.h"
int main()
{
mqd_t mq;
mq = mq_open(MQNAME, O_RDWR | O_CREAT, 0666, NULL);
}
this is my sharedMsg.h
struct item{
char *word;
int lineNum;
};
#define MQNAME "/sentmsg"
and finally this is my MakeFile
all: deneme
deneme: deneme.c
gcc -g -Wall -o deneme deneme.c -lrt
clean:
rm -fr *~ *.o
The problem with your Makefile is that your all target depends on demene but the target that specifies -lrt is called demene1. Thus, the default inference rules kick in and demene is attempted to be linked without -lrt. The rule for deneme1 is also wrong in that it attempts to create a binary named deneme, even though the rule should create deneme1. To resolve this, change the first to third lines to
all: deneme1
deneme1: deneme.c
gcc -g -Wall -o deneme1 deneme.c -lrt
or the second line to
deneme: deneme.c
(Note: The OP has edited the question and fixed the Makefile after this answer)
You have a broken Makefile.
The reason why you've even seen the linker error is that you have probably executed make deneme. In that case make will try to compile dename.c with default compiler options (because it did not find a target called deneme) and the default options do not include -lrt.
You have to fix your Makefile (replace deneme1: with deneme:)
all: deneme
deneme: deneme.c
gcc -g -Wall -o deneme deneme.c -lrt
clean:
rm -f deneme *~ *.o
Is there any way I can compile a poorly designed header file to a object file without changing file extension or content using gcc, or do I have to copy the file/edit it? (This because I am using a public SDK, i.e. I do not have permission to edit the header file, and because using cp in my Makefile seems like a major hack, and time consuming too)
Example
main.c
#include <print.h>
#include <app.h>
int main(void) {
print("Starting app . . . ");
run();
}
app.h
#ifndef APP_H
#define APP_H
int runApp(void);
#endif
app.c
#include <print.h>
#include <app.h>
int runApp(void) {
print("This is my app!");
return 0
}
print.h
#ifndef PRINT_H
#define PRINT_H
int print(char* str) {
printf(str);
return 0;
}
#endif
Which is compiled using:
$ gcc -o main.o main.c
$ gcc -o app.o app.c
$ gcc -o main main.o app.o
The SDK example programs use a single object file (gcc -o main.o main.c & gcc -o main main.o), but that would just get really messy in my case.
Create
_print.h
int print(char* str);
print.cpp
#include <print.h>
and change your includes to "_print.h"
I have these five source
main.c src_print1.c src_print2.c header_print1.h header_print2.h
the contents are simple and are as following for respective files:
main.c
#include "header_print1.h"
#include "header_print2.h"
int main(int argc, char** argv) {
print1();
print2();
return 0;
}
header_print1.h
#ifndef PRINT_1
#define PRINT_1
#include <stdio.h>
#include <stdlib.h>
void print1();
#endif
header_print2.h
#ifndef PRINT_2
#define PRINT_2
#include <stdio.h>
#include <stdlib.h>
void print2();
#endif
src_print1.c
#include "header_print1.h"
void print1() {
printf("Hello 1\n");
}
src_print2.c
#include "header_print2.h"
void print2() {
printf("Hello 2\n");
}
Using gcc I have tried to compile using the following command line:
gcc -I ./ -o test -c main.c src_print1.c src_print2.c
Everything is in the same folder.
The error I get is:
gcc: cannot specify -o with -c or -S with multiple files
I looked up at gcc manual, but actually I don't understand what to do in this case, since usually I use IDE and not the command line.
IMHO, if you rewrite your compilation statement like
gcc -I./ -o test main.c src_print1.c src_print2.c
You'll be good to go. There is no need for -c flag [NOTE] when you're specifying the output binary using -o.
Also, as mentioned here, all the files are in same directory, you can even shorten the statement as
gcc -o test main.c src_print1.c src_print2.c
Suggestion: While the above change(s) will do the job, this is not considered an elegant way of doing so. Please consider creating a makefile which will make your life easier.
[Note]:
Regarding the -c option, as per the online gcc manual, (emphasis mine)
-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
So, it should be clear by now, why you got the error.