1: /*
2: ================================================================================
3: RPL/2 (R) version 4.1.36
4: Copyright (C) 1989-2024 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: #define DEBUG_RETURN
23: #include "rpl-conv.h"
24: #include <openssl/err.h>
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 longueur_bloc_de_chiffrement;
47: int longueur_message_1;
48: int longueur_message_2;
49:
50: integer8 nombre_blocs;
51:
52: unsigned char *message_chiffre;
53:
54: EVP_CIPHER_CTX *contexte;
55:
56: if ((contexte = EVP_CIPHER_CTX_new()) == NULL)
57: {
58: return(NULL);
59: }
60:
61: EVP_CIPHER_CTX_reset(contexte);
62:
63: longueur_bloc_de_chiffrement = EVP_CIPHER_block_size(type_chiffrement);
64:
65: if (EVP_CipherInit_ex(contexte, type_chiffrement, NULL, clef,
66: vecteur_initialisation, (encodage == d_vrai) ? 1 : 0) != 1)
67: {
68: EVP_CIPHER_CTX_free(contexte);
69: return(NULL);
70: }
71:
72: nombre_blocs = longueur_message / longueur_bloc_de_chiffrement;
73:
74: if ((longueur_message % longueur_bloc_de_chiffrement) != 0)
75: {
76: nombre_blocs++;
77: }
78:
79: (*longueur_message_chiffre) = nombre_blocs * longueur_bloc_de_chiffrement;
80:
81: // On prévoit une zone de garde pour EVP_CipherFinal_ex() en espérant
82: // qu'il ne faille pas plus qu'une longueur de bloc de chiffrement.
83: if ((message_chiffre = malloc(((size_t) ((*longueur_message_chiffre)
84: + longueur_bloc_de_chiffrement)) *
85: sizeof(unsigned char))) == NULL)
86: {
87: EVP_CIPHER_CTX_free(contexte);
88: return(NULL);
89: }
90:
91: if (EVP_CipherUpdate(contexte, message_chiffre, &longueur_message_1,
92: message, (int) longueur_message) != 1)
93: {
94: free(message_chiffre);
95: EVP_CIPHER_CTX_free(contexte);
96: return(NULL);
97: }
98:
99: if (EVP_CipherFinal_ex(contexte, message_chiffre + longueur_message_1,
100: &longueur_message_2) != 1)
101: {
102: free(message_chiffre);
103: EVP_CIPHER_CTX_free(contexte);
104: return(NULL);
105: }
106:
107: (*longueur_message_chiffre) = longueur_message_1 + longueur_message_2;
108:
109: // Mise à jour du vecteur d'initialisation
110:
111: EVP_CIPHER_CTX_get_updated_iv(contexte, vecteur_initialisation,
112: (size_t) EVP_CIPHER_iv_length(type_chiffrement));
113:
114: EVP_CIPHER_CTX_free(contexte);
115:
116: return(message_chiffre);
117: }
118:
119: // vim: ts=4
CVSweb interface <joel.bertrand@systella.fr>