File:  [local] / rpl / src / chiffrement.c
Revision 1.35: download - view: text, annotated - select for diffs - revision graph
Fri Jan 10 11:15:41 2020 UTC (4 years, 3 months ago) by bertrand
Branches: MAIN
CVS tags: rpl-4_1_32, HEAD
Modification du copyright.

    1: /*
    2: ================================================================================
    3:   RPL/2 (R) version 4.1.32
    4:   Copyright (C) 1989-2020 Dr. BERTRAND Joël
    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: 
   23: #include "rpl-conv.h"
   24: 
   25: 
   26: /*
   27: ================================================================================
   28:   Fonction de chiffrement d'un séquence d'octets
   29: ================================================================================
   30:   Entrées : pointeur sur une structure
   31: --------------------------------------------------------------------------------
   32:   Sorties :
   33: --------------------------------------------------------------------------------
   34:   Effets de bord : néant
   35: ================================================================================
   36: */
   37: 
   38: unsigned char *
   39: chiffrement(struct_processus *s_etat_processus,
   40:         const EVP_CIPHER *type_chiffrement, logical1 encodage,
   41:         unsigned char *message, integer8 longueur_message,
   42:         unsigned char *clef, integer8 longueur_clef,
   43:         unsigned char *vecteur_initialisation,
   44:         integer8 *longueur_message_chiffre)
   45: {
   46:     int                         i;
   47:     int                         longueur_bloc_de_chiffrement;
   48:     int                         longueur_message_1;
   49:     int                         longueur_message_2;
   50: 
   51:     unsigned char               *message_chiffre;
   52: 
   53:     EVP_CIPHER_CTX              *contexte;
   54: 
   55:     if ((contexte = EVP_CIPHER_CTX_new()) == NULL)
   56:     {
   57:         return(NULL);
   58:     }
   59: 
   60:     EVP_CIPHER_CTX_reset(contexte);
   61: 
   62:     longueur_bloc_de_chiffrement = EVP_CIPHER_block_size(type_chiffrement);
   63: 
   64:     if (EVP_CipherInit_ex(contexte, type_chiffrement, NULL, clef,
   65:             vecteur_initialisation, (encodage == d_vrai) ? 1 : 0) != 1)
   66:     {
   67:         EVP_CIPHER_CTX_free(contexte);
   68:         return(NULL);
   69:     }
   70: 
   71:     (*longueur_message_chiffre) = ((longueur_message /
   72:             longueur_bloc_de_chiffrement) + 1) * longueur_bloc_de_chiffrement;
   73: 
   74:     if ((message_chiffre = malloc(((size_t) (*longueur_message_chiffre)) *
   75:             sizeof(unsigned char))) == NULL)
   76:     {
   77:         EVP_CIPHER_CTX_free(contexte);
   78:         return(NULL);
   79:     }
   80: 
   81:     if (EVP_CipherUpdate(contexte, message_chiffre, &longueur_message_1,
   82:             message, (int) longueur_message) != 1)
   83:     {
   84:         free(message_chiffre);
   85:         EVP_CIPHER_CTX_free(contexte);
   86:         return(NULL);
   87:     }
   88: 
   89:     if (EVP_CipherFinal_ex(contexte, message_chiffre + longueur_message_1,
   90:             &longueur_message_2) != 1)
   91:     {
   92:         free(message_chiffre);
   93:         EVP_CIPHER_CTX_free(contexte);
   94:         return(NULL);
   95:     }
   96: 
   97:     (*longueur_message_chiffre) = longueur_message_1 + longueur_message_2;
   98: 
   99:     // Mise à jour du vecteur d'initialisation
  100:     
  101:     for(i = 0; i < EVP_CIPHER_iv_length(type_chiffrement); i++)
  102:     {
  103:         vecteur_initialisation[i] = EVP_CIPHER_CTX_iv(contexte)[i];
  104:     }
  105: 
  106:     EVP_CIPHER_CTX_free(contexte);
  107: 
  108:     return(message_chiffre);
  109: }
  110: 
  111: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>