IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Linux Discussion :

EVP_EncryptInit [undefined reference to]


Sujet :

Linux

  1. #1
    Membre du Club
    Inscrit en
    Janvier 2008
    Messages
    52
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 52
    Points : 41
    Points
    41
    Par défaut EVP_EncryptInit [undefined reference to]
    Bonjour à tous,

    Je souhaite réaliser un programme en C me permettant de chiffrer un fichier avec une clé passé en paramètre. Pour cela, je souhaite utiliser l'API EVP. Débutant dans ce domaine, je me suis inspiré de l'exemple donné dans le man. Toutefois, voici mon problème :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    [Iloyo@localhost ~]$ gcc -o chiffrer chiffrer.c
    chiffrer.c:12:1: attention : « EVP_CIPHER_mode » redéfini
    Dans le fichier inclus à partir de chiffrer.c:3:
    /usr/include/openssl/evp.h:436:1: attention : ceci est la localisation d'une précédente définition
    chiffrer.c:22:1: attention : « EVP_CIPHER_CTX_mode » redéfini
    /usr/include/openssl/evp.h:447:1: attention : ceci est la localisation d'une précédente définition
    /tmp/ccy3sCkR.o: In function `main':
    chiffrer.c:(.text+0x135): undefined reference to `EVP_CIPHER_CTX_init'
    chiffrer.c:(.text+0x13a): undefined reference to `EVP_des_ede3_cbc'
    chiffrer.c:(.text+0x16d): undefined reference to `EVP_CipherInit_ex'
    chiffrer.c:(.text+0x183): undefined reference to `EVP_CIPHER_CTX_set_key_length'
    chiffrer.c:(.text+0x1b9): undefined reference to `EVP_CipherInit_ex'
    chiffrer.c:(.text+0x21e): undefined reference to `EVP_CipherUpdate'
    chiffrer.c:(.text+0x230): undefined reference to `EVP_CIPHER_CTX_cleanup'
    chiffrer.c:(.text+0x290): undefined reference to `EVP_CipherFinal_ex'
    chiffrer.c:(.text+0x2a2): undefined reference to `EVP_CIPHER_CTX_cleanup'
    chiffrer.c:(.text+0x2e5): undefined reference to `EVP_CIPHER_CTX_cleanup'
    collect2: ld a retourné 1 code d'état d'exécution
    Et si besoins, voici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    #include<stdio.h>
    #include<stdlib.h>
    #include<openssl/evp.h>
     
    #define EVP_get_cipherbynid(a) 			EVP_get_cipherbyname(OBJ_nid2sn(a))
    #define EVP_get_cipherbyobj(a) 			EVP_get_cipherbynid(OBJ_obj2nid(a))
    #define EVP_CIPHER_nid(e)              		((e)->nid)
    #define EVP_CIPHER_block_size(e)       		((e)->block_size)
    #define EVP_CIPHER_key_length(e)       		((e)->key_len)
    #define EVP_CIPHER_iv_length(e)                	((e)->iv_len)
    #define EVP_CIPHER_flags(e)            		((e)->flags)
    #define EVP_CIPHER_mode(e)             		((e)->flags) & EVP_CIPH_MODE)
    #define EVP_CIPHER_CTX_cipher(e)       		((e)->cipher)
    #define EVP_CIPHER_CTX_nid(e)          		((e)->cipher->nid)
    #define EVP_CIPHER_CTX_block_size(e)   		((e)->cipher->block_size)
    #define EVP_CIPHER_CTX_key_length(e)   		((e)->key_len)
    #define EVP_CIPHER_CTX_iv_length(e)    		((e)->cipher->iv_len)
    #define EVP_CIPHER_CTX_get_app_data(e) 		((e)->app_data)
    #define EVP_CIPHER_CTX_set_app_data(e,d) 	((e)->app_data=(char *)(d))
    #define EVP_CIPHER_CTX_type(c)         		EVP_CIPHER_type(EVP_CIPHER_CTX_cipher(c))
    #define EVP_CIPHER_CTX_flags(e)                	((e)->cipher->flags)
    #define EVP_CIPHER_CTX_mode(e)         		((e)->cipher->flags & EVP_CIPH_MODE)
     
    int main(int argc, char * argv[]) {
     
    	/* Test number of parameters */
    	if ( argc < 3 ) { printf("Parametres manquants : %s [ Key ] [ filename ]\n",argv[0]); exit(1); }
    	if (argc > 3 ) printf("%d Parametres en trop!\n",argc-3);
     
    	FILE * in;
    	FILE * out;
    	int do_encrypt = 0;
    	unsigned char * key = argv[1];
    	const char * infile = argv[2];
    	char * outfile;
    	EVP_CIPHER_CTX ctx;
     
     
    	sprintf(outfile,"%s.sign",argv[2]);
    	if ( (in = fopen(infile,"r")) == NULL ) perror("ERREUR : Ouverture du fichier source");
    	if ( (out = fopen(outfile,"w+")) == NULL ) perror("ERREUR : Ouverture du fichier source");
     
     
    	/* Allow enough space in output buffer for additional block */
    	char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
    	int inlen, outlen;
    	/* Bogus key and IV: we'd normally set these from
    	 * another source.
    	 */
    	unsigned char iv[] = "12345678";
    	/* Don't set key or IV because we will modify the parameters */
    	EVP_CIPHER_CTX_init(&ctx);
    	EVP_CipherInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, NULL, NULL, do_encrypt);
    	EVP_CIPHER_CTX_set_key_length(&ctx, 10);
    	/* We finished modifying parameters so now we can set key and IV */
    	EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
     
    	for(;;) {
    		if ( (inlen = fread(inbuf, 1, 1024, in)) <= 0 ) break;
    		if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen)) {
    			/* Error */
    			EVP_CIPHER_CTX_cleanup(&ctx);
    			return 0;
    		}
    		fwrite(outbuf, 1, outlen, out);
    	}
    	if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen)) {
    		/* Error */
    		EVP_CIPHER_CTX_cleanup(&ctx);
    		return 0;
    	}
    	fwrite(outbuf, 1, outlen, out);
    	EVP_CIPHER_CTX_cleanup(&ctx);
    	return 1;
    }
    Si quelqu'un pouvait m'aider, merci par avance !

  2. #2
    Membre éclairé
    Inscrit en
    Avril 2007
    Messages
    667
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Avril 2007
    Messages : 667
    Points : 870
    Points
    870
    Par défaut
    Salut,
    Citation Envoyé par Iloyo Voir le message
    ....
    je me suis inspiré de l'exemple donné dans le man.
    Tu as recopie le man. Les messages d'erreur sont tres explicites:
    Citation Envoyé par Iloyo Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    [Iloyo@localhost ~]
    ...
    chiffrer.c:12:1: attention : « EVP_CIPHER_mode » redéfini
    ...
    /usr/include/openssl/evp.h:436:1: attention : ceci est la localisation d'une précédente définition
    chiffrer.c:22:1: attention : « EVP_CIPHER_CTX_mode » redéfini
    /usr/include/openssl/evp.h:447:1: attention : ceci est la localisation d'une précédente définition
    ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #include<stdio.h>
    #include<stdlib.h>
    #include<openssl/evp.h>
     
    #define EVP_get_cipherbynid(a) 			EVP_get_cipherbyname(OBJ_nid2sn(a))
    #define EVP_get_cipherbyobj(a) 			EVP_get_cipherbynid(OBJ_obj2nid(a))
    #define EVP_CIPHER_nid(e)              		((e)->nid)
    #define EVP_CIPHER_block_size(e)       		((e)->block_size)
    #define EVP_CIPHER_key_length(e)       		((e)->key_len)
    #define EVP_CIPHER_iv_length(e)                	((e)->iv_len)
    #define EVP_CIPHER_flags(e)            		((e)->flags)
    #define EVP_CIPHER_mode(e)             		((e)->flags) & EVP_CIPH_MODE)
    #define EVP_CIPHER_CTX_cipher(e)       		((e)->cipher)
    #define EVP_CIPHER_CTX_nid(e)          		((e)->cipher->nid)
    #define EVP_CIPHER_CTX_block_size(e)   		((e)->cipher->block_size)
    #define EVP_CIPHER_CTX_key_length(e)   		((e)->key_len)
    #define EVP_CIPHER_CTX_iv_length(e)    		((e)->cipher->iv_len)
    #define EVP_CIPHER_CTX_get_app_data(e) 		((e)->app_data)
    #define EVP_CIPHER_CTX_set_app_data(e,d) 	((e)->app_data=(char *)(d))
    #define EVP_CIPHER_CTX_type(c)         		EVP_CIPHER_type(EVP_CIPHER_CTX_cipher(c))
    #define EVP_CIPHER_CTX_flags(e)                	((e)->cipher->flags)
    #define EVP_CIPHER_CTX_mode(e)         		((e)->cipher->flags & EVP_CIPH_MODE)
    La page de man te donne la declaration des fonctions et macros definies dans l'include openssl/evp.h. Si tu recopies ces declaration dans ton code, tu redeclare ce qui a deja ete declare et BOUM

  3. #3
    Membre du Club
    Inscrit en
    Janvier 2008
    Messages
    52
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 52
    Points : 41
    Points
    41
    Par défaut
    Hum, ok merci j'avais pas fait attention à cela !!

    Cependant, même en retirant les #define, j'obtiens les erreurs suivantes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [Iloyo@localhost ~]$ gcc -o chiffrer chiffrer.c
    /tmp/ccOTiuGW.o: In function `main':
    chiffrer.c:(.text+0x135): undefined reference to `EVP_CIPHER_CTX_init'
    chiffrer.c:(.text+0x13a): undefined reference to `EVP_des_ede3_cbc'
    chiffrer.c:(.text+0x16d): undefined reference to `EVP_CipherInit_ex'
    chiffrer.c:(.text+0x183): undefined reference to `EVP_CIPHER_CTX_set_key_length'
    chiffrer.c:(.text+0x1b9): undefined reference to `EVP_CipherInit_ex'
    chiffrer.c:(.text+0x21e): undefined reference to `EVP_CipherUpdate'
    chiffrer.c:(.text+0x230): undefined reference to `EVP_CIPHER_CTX_cleanup'
    chiffrer.c:(.text+0x290): undefined reference to `EVP_CipherFinal_ex'
    chiffrer.c:(.text+0x2a2): undefined reference to `EVP_CIPHER_CTX_cleanup'
    chiffrer.c:(.text+0x2e5): undefined reference to `EVP_CIPHER_CTX_cleanup'
    collect2: ld a retourné 1 code d'état d'exécution

  4. #4
    Membre émérite Avatar de nicolas.sitbon
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2 015
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2 015
    Points : 2 280
    Points
    2 280
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    gcc -o chiffrer chiffrer.c -lcrypto

  5. #5
    Membre du Club
    Inscrit en
    Janvier 2008
    Messages
    52
    Détails du profil
    Informations forums :
    Inscription : Janvier 2008
    Messages : 52
    Points : 41
    Points
    41
    Par défaut
    Merci, c'était bien la réponse... c'est sûr ça compile mieux !!

    Toutefois, après recherches j'en suis arrivé au code suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <openssl/aes.h>
    #include <openssl/evp.h>
    #include <openssl/sha.h>
    #include <openssl/rand.h>
    #include <openssl/err.h>
    #include <assert.h>
    #include <errno.h>
     
    #define TAILLE_BUFF 1024
     
     
    int main(int argc, char* argv[])
    {
    	if ( argc < 3 ) { printf("Parametres manquants : %s [ Key ] [ filename ]\n",argv[0]); exit(1); }
    	if (argc > 3 ) printf("%d Parametres en trop!\n",argc-3);
    	int outlength;
    	int outlen2;
    	int tmplen;
    	int cread =0;
    	int cwrite = 0;
    	char * inpath;
    	char * outpath;
    	unsigned char key[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    	unsigned char iv[] = {1,2,3,4,5,6,7,8};
    	unsigned char inbuffer[TAILLE_BUFF];
    	unsigned char outbuffer[TAILLE_BUFF];
    	EVP_CIPHER_CTX x;
    	FILE * instream;
    	FILE * outstream;
     
    	/* Ouverture des fichiers entrée - sortie */
    	inpath = malloc ( sizeof(argv[2]) );
    	sprintf(inpath,"%s\0",argv[2]);
    	if ( (instream = fopen(inpath, "r")) == NULL ) { printf("[%d] ERREUR : Ouverture du fichier source %s\n",errno,inpath); exit(1); }
    	outpath = malloc( strlen(inpath) + strlen(".crypt.txt") + 1 );
    	sprintf(outpath,"%s.crypt.txt\0",inpath);
    	if ( (outstream = fopen(outpath, "a")) == NULL ) { printf("[%d] ERREUR : Création du fichier cible %s\n",errno,outpath); exit(1); }
     
    	/* Chiffrement */
    	EVP_CIPHER_CTX_init(&x);
    	if (!EVP_EncryptInit_ex(&x, EVP_des_ede3(), NULL, key, iv)) {
    		printf("[%d] ERREUR : EVP_EncryptInit_ex()\n",errno);
    		exit(1);
    	}
    	for(;;) {
    		while ( cread < sizeof(inbuffer) && (feof(instream) != 0) ) cread += fread(inbuffer, 1, sizeof(inbuffer), instream);
    		if (!EVP_EncryptUpdate(&x, outbuffer, &outlength,(const unsigned char*) inbuffer, strlen(inbuffer))) {
    			printf("[%d] ERREUR : EVP_EncryptUpdate()\n",errno);
    			exit(1);
    		}
    		if (!EVP_EncryptFinal_ex(&x,outbuffer+outlength,&tmplen)) {
    			printf("[%d] ERREUR : EVP_EncryptFinal_ex()\n",errno);
    			exit(1);
    		}
    		while ( cwrite < sizeof(inbuffer) ) cwrite += fwrite(outbuffer, 1, sizeof(outbuffer), outstream);
    		if ( feof(instream) == 0 ) break;
    	}
    	EVP_CIPHER_CTX_cleanup(&x);
    	fclose(instream);
    	fclose(outstream);
    	free(inpath);
    	free(outpath);
    }
    ... le test de 2 paramètres est un peu tôt mais je pense par la suite fournir la clé en argument.

    Bref, ce code compile bien, et le programme semble chiffrer correctement mon fichier d'entrée :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    [Iloyo@localhost ~]$ more test
    Coucou tout le monde !!
    [Iloyo@localhost ~]$ gcc -o chiffrer chiffrer.c
    [Iloyo@localhost ~]$ ./chiffrer key test
    [Iloyo@localhost ~]$
    J'ai bien dit semble car lorsque je lance la commande openssl pour le déchiffrage, j'obtiens...


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    [Iloyo@localhost ~]$ openssl enc -des-ede3 -d -in test.crypt.txt -K 123456789101112131415 -iv 12345678 -out test.decrypt.txt
    bad decrypt
    10007:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:461:
     
    ou en hexa :
     
    [Iloyo@localhost ~]$ openssl enc -des-ede3 -d -in test.crypt.txt -K 123456789ABCDEF -iv 12345678 -out test.decrypt.txt
    bad decrypt
    9997:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:461:
    Est-ce que quelqu'un aurait une idée ?
    Merci !

Discussions similaires

  1. winsock.h : undefined reference...
    Par abraxas dans le forum Dev-C++
    Réponses: 14
    Dernier message: 06/08/2012, 13h42
  2. Undefined reference compilation réseau
    Par Ren97 dans le forum Dev-C++
    Réponses: 11
    Dernier message: 08/03/2005, 09h46
  3. Compilation de xmms : undefined reference to...
    Par Michaël dans le forum Applications et environnements graphiques
    Réponses: 4
    Dernier message: 04/02/2005, 19h05
  4. undefined reference to `xmlParseFile'
    Par Clemaster dans le forum Autres éditeurs
    Réponses: 2
    Dernier message: 25/06/2004, 20h38
  5. g++ : undefined reference to ...
    Par le_barbu dans le forum Autres éditeurs
    Réponses: 16
    Dernier message: 14/05/2004, 07h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo