Annotation of rpl/src/chiffrement.c, revision 1.12

1.1       bertrand    1: /*
                      2: ================================================================================
1.12    ! bertrand    3:   RPL/2 (R) version 4.1.16
1.5       bertrand    4:   Copyright (C) 1989-2013 Dr. BERTRAND Joël
1.1       bertrand    5: 
                      6:   This file is part of RPL/2.
                      7: 
                      8:   RPL/2 is free software; you can redistribute it and/or modify it
                      9:   under the terms of the CeCILL V2 License as published by the french
                     10:   CEA, CNRS and INRIA.
                     11:  
                     12:   RPL/2 is distributed in the hope that it will be useful, but WITHOUT
                     13:   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
                     14:   FITNESS FOR A PARTICULAR PURPOSE.  See the CeCILL V2 License
                     15:   for more details.
                     16:  
                     17:   You should have received a copy of the CeCILL License
                     18:   along with RPL/2. If not, write to info@cecill.info.
                     19: ================================================================================
                     20: */
                     21: 
                     22: 
1.4       bertrand   23: #include "rpl-conv.h"
1.1       bertrand   24: 
                     25: 
                     26: /*
                     27: ================================================================================
1.4       bertrand   28:   Fonction de chiffrement d'un séquence d'octets
1.1       bertrand   29: ================================================================================
1.4       bertrand   30:   Entrées : pointeur sur une structure
1.1       bertrand   31: --------------------------------------------------------------------------------
1.4       bertrand   32:   Sorties :
1.1       bertrand   33: --------------------------------------------------------------------------------
                     34:   Effets de bord : néant
                     35: ================================================================================
                     36: */
                     37: 
1.4       bertrand   38: unsigned char *
                     39: chiffrement(const EVP_CIPHER *type_chiffrement, logical1 encodage,
1.9       bertrand   40:        unsigned char *message, integer8 longueur_message,
                     41:        unsigned char *clef, integer8 longueur_clef,
1.4       bertrand   42:        unsigned char *vecteur_initialisation,
1.9       bertrand   43:        integer8 *longueur_message_chiffre)
1.4       bertrand   44: {
1.6       bertrand   45:    int                         i;
                     46:    int                         longueur_bloc_de_chiffrement;
1.4       bertrand   47:    int                         longueur_message_1;
                     48:    int                         longueur_message_2;
                     49: 
                     50:    unsigned char               *message_chiffre;
                     51: 
                     52:    EVP_CIPHER_CTX              contexte;
                     53: 
                     54:    EVP_CIPHER_CTX_init(&contexte);
                     55: 
1.6       bertrand   56:    longueur_bloc_de_chiffrement = EVP_CIPHER_block_size(type_chiffrement);
                     57: 
1.4       bertrand   58:    if (EVP_CipherInit_ex(&contexte, type_chiffrement, NULL, clef,
                     59:            vecteur_initialisation, (encodage == d_vrai) ? 1 : 0) != 1)
                     60:    {
                     61:        EVP_CIPHER_CTX_cleanup(&contexte);
                     62:        return(NULL);
                     63:    }
                     64: 
                     65:    (*longueur_message_chiffre) = ((longueur_message /
                     66:            longueur_bloc_de_chiffrement) + 1) * longueur_bloc_de_chiffrement;
                     67: 
1.9       bertrand   68:    if ((message_chiffre = malloc(((size_t) (*longueur_message_chiffre)) *
1.4       bertrand   69:            sizeof(unsigned char))) == NULL)
                     70:    {
                     71:        return(NULL);
                     72:    }
                     73: 
                     74:    if (EVP_CipherUpdate(&contexte, message_chiffre, &longueur_message_1,
1.9       bertrand   75:            message, (int) longueur_message) != 1)
1.4       bertrand   76:    {
                     77:        free(message_chiffre);
                     78:        EVP_CIPHER_CTX_cleanup(&contexte);
                     79:        return(NULL);
                     80:    }
                     81: 
                     82:    if (EVP_CipherFinal_ex(&contexte, message_chiffre + longueur_message_1,
                     83:            &longueur_message_2) != 1)
                     84:    {
                     85:        free(message_chiffre);
                     86:        EVP_CIPHER_CTX_cleanup(&contexte);
                     87:        return(NULL);
                     88:    }
                     89: 
                     90:    (*longueur_message_chiffre) = longueur_message_1 + longueur_message_2;
1.6       bertrand   91: 
                     92:    // Mise à jour du vecteur d'initialisation
                     93:    
1.7       bertrand   94:    for(i = 0; i < EVP_CIPHER_iv_length(type_chiffrement); i++)
1.6       bertrand   95:    {
                     96:        vecteur_initialisation[i] = contexte.iv[i];
                     97:    }
                     98: 
1.4       bertrand   99:    EVP_CIPHER_CTX_cleanup(&contexte);
                    100: 
                    101:    return(message_chiffre);
                    102: }
                    103: 
1.1       bertrand  104: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>