Annotation of rpl/src/chiffrement.c, revision 1.45
1.1 bertrand 1: /*
2: ================================================================================
1.41 bertrand 3: RPL/2 (R) version 4.1.36
1.45 ! bertrand 4: Copyright (C) 1989-2025 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:
1.43 bertrand 22:
1.4 bertrand 23: #include "rpl-conv.h"
1.43 bertrand 24:
1.1 bertrand 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 *
1.19 bertrand 39: chiffrement(struct_processus *s_etat_processus,
40: const EVP_CIPHER *type_chiffrement, logical1 encodage,
1.9 bertrand 41: unsigned char *message, integer8 longueur_message,
42: unsigned char *clef, integer8 longueur_clef,
1.4 bertrand 43: unsigned char *vecteur_initialisation,
1.44 bertrand 44: integer8 *longueur_message_chiffre,
45: unsigned char padding)
1.4 bertrand 46: {
1.6 bertrand 47: int longueur_bloc_de_chiffrement;
1.4 bertrand 48: int longueur_message_1;
49: int longueur_message_2;
50:
1.42 bertrand 51: integer8 nombre_blocs;
52:
1.4 bertrand 53: unsigned char *message_chiffre;
54:
1.25 bertrand 55: EVP_CIPHER_CTX *contexte;
1.4 bertrand 56:
1.25 bertrand 57: if ((contexte = EVP_CIPHER_CTX_new()) == NULL)
58: {
59: return(NULL);
60: }
61:
62: EVP_CIPHER_CTX_reset(contexte);
1.4 bertrand 63:
1.6 bertrand 64: longueur_bloc_de_chiffrement = EVP_CIPHER_block_size(type_chiffrement);
65:
1.25 bertrand 66: if (EVP_CipherInit_ex(contexte, type_chiffrement, NULL, clef,
1.4 bertrand 67: vecteur_initialisation, (encodage == d_vrai) ? 1 : 0) != 1)
68: {
1.25 bertrand 69: EVP_CIPHER_CTX_free(contexte);
1.4 bertrand 70: return(NULL);
71: }
72:
1.42 bertrand 73: nombre_blocs = longueur_message / longueur_bloc_de_chiffrement;
74:
75: if ((longueur_message % longueur_bloc_de_chiffrement) != 0)
76: {
77: nombre_blocs++;
78: }
1.44 bertrand 79: else if (padding == 'N')
80: {
81: // Pas de padding si la longueur du message est un multiple
82: // entier de la longueur du bloc.
83:
84: EVP_CIPHER_CTX_set_padding(contexte, 0);
85: }
1.42 bertrand 86:
87: (*longueur_message_chiffre) = nombre_blocs * longueur_bloc_de_chiffrement;
1.4 bertrand 88:
1.44 bertrand 89: if (padding == 'Y')
1.4 bertrand 90: {
1.44 bertrand 91: // On prévoit une zone de garde pour EVP_CipherFinal_ex() qui rajoute
92: // TOUJOURS un bloc complet même s'il la taille chiffrée par
93: // EVP_CipherUpdate() est un nombre exact de blocs !
94:
95: if ((message_chiffre = malloc(((size_t) ((*longueur_message_chiffre)
96: + longueur_bloc_de_chiffrement)) *
97: sizeof(unsigned char))) == NULL)
98: {
99: EVP_CIPHER_CTX_free(contexte);
100: return(NULL);
101: }
102: }
103: else
104: {
105: if ((message_chiffre = malloc(((size_t) (*longueur_message_chiffre)
106: ) * sizeof(unsigned char))) == NULL)
107: {
108: EVP_CIPHER_CTX_free(contexte);
109: return(NULL);
110: }
1.4 bertrand 111: }
112:
1.25 bertrand 113: if (EVP_CipherUpdate(contexte, message_chiffre, &longueur_message_1,
1.9 bertrand 114: message, (int) longueur_message) != 1)
1.4 bertrand 115: {
116: free(message_chiffre);
1.25 bertrand 117: EVP_CIPHER_CTX_free(contexte);
1.4 bertrand 118: return(NULL);
119: }
120:
1.25 bertrand 121: if (EVP_CipherFinal_ex(contexte, message_chiffre + longueur_message_1,
1.4 bertrand 122: &longueur_message_2) != 1)
123: {
124: free(message_chiffre);
1.25 bertrand 125: EVP_CIPHER_CTX_free(contexte);
1.4 bertrand 126: return(NULL);
127: }
128:
129: (*longueur_message_chiffre) = longueur_message_1 + longueur_message_2;
1.6 bertrand 130:
131: // Mise à jour du vecteur d'initialisation
132:
1.38 bertrand 133: EVP_CIPHER_CTX_get_updated_iv(contexte, vecteur_initialisation,
134: (size_t) EVP_CIPHER_iv_length(type_chiffrement));
1.6 bertrand 135:
1.25 bertrand 136: EVP_CIPHER_CTX_free(contexte);
1.4 bertrand 137:
138: return(message_chiffre);
139: }
140:
1.1 bertrand 141: // vim: ts=4
CVSweb interface <joel.bertrand@systella.fr>