/* ================================================================================ RPL/2 (R) version 4.1.33 Copyright (C) 1989-2021 Dr. BERTRAND Joël This file is part of RPL/2. RPL/2 is free software; you can redistribute it and/or modify it under the terms of the CeCILL V2 License as published by the french CEA, CNRS and INRIA. RPL/2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL V2 License for more details. You should have received a copy of the CeCILL License along with RPL/2. If not, write to info@cecill.info. ================================================================================ */ #include "rpl-conv.h" /* ================================================================================ Fonction de chiffrement d'un séquence d'octets ================================================================================ Entrées : pointeur sur une structure -------------------------------------------------------------------------------- Sorties : -------------------------------------------------------------------------------- Effets de bord : néant ================================================================================ */ unsigned char * chiffrement(struct_processus *s_etat_processus, const EVP_CIPHER *type_chiffrement, logical1 encodage, unsigned char *message, integer8 longueur_message, unsigned char *clef, integer8 longueur_clef, unsigned char *vecteur_initialisation, integer8 *longueur_message_chiffre) { int i; int longueur_bloc_de_chiffrement; int longueur_message_1; int longueur_message_2; unsigned char *message_chiffre; EVP_CIPHER_CTX *contexte; if ((contexte = EVP_CIPHER_CTX_new()) == NULL) { return(NULL); } EVP_CIPHER_CTX_reset(contexte); longueur_bloc_de_chiffrement = EVP_CIPHER_block_size(type_chiffrement); if (EVP_CipherInit_ex(contexte, type_chiffrement, NULL, clef, vecteur_initialisation, (encodage == d_vrai) ? 1 : 0) != 1) { EVP_CIPHER_CTX_free(contexte); return(NULL); } (*longueur_message_chiffre) = ((longueur_message / longueur_bloc_de_chiffrement) + 1) * longueur_bloc_de_chiffrement; if ((message_chiffre = malloc(((size_t) (*longueur_message_chiffre)) * sizeof(unsigned char))) == NULL) { EVP_CIPHER_CTX_free(contexte); return(NULL); } if (EVP_CipherUpdate(contexte, message_chiffre, &longueur_message_1, message, (int) longueur_message) != 1) { free(message_chiffre); EVP_CIPHER_CTX_free(contexte); return(NULL); } if (EVP_CipherFinal_ex(contexte, message_chiffre + longueur_message_1, &longueur_message_2) != 1) { free(message_chiffre); EVP_CIPHER_CTX_free(contexte); return(NULL); } (*longueur_message_chiffre) = longueur_message_1 + longueur_message_2; // Mise à jour du vecteur d'initialisation for(i = 0; i < EVP_CIPHER_iv_length(type_chiffrement); i++) { vecteur_initialisation[i] = EVP_CIPHER_CTX_iv(contexte)[i]; } EVP_CIPHER_CTX_free(contexte); return(message_chiffre); } // vim: ts=4