--- rpl/src/instructions_d6.c 2012/12/19 09:58:24 1.38 +++ rpl/src/instructions_d6.c 2013/02/23 18:31:16 1.39 @@ -589,4 +589,771 @@ instruction_diag_fleche(struct_processus return; } + +/* +================================================================================ + Fonction 'digest' +================================================================================ + Entrées : pointeur sur une structure struct_processus +-------------------------------------------------------------------------------- + Sorties : +-------------------------------------------------------------------------------- + Effets de bord : néant +================================================================================ +*/ + +void +instruction_digest(struct_processus *s_etat_processus) +{ + EVP_MD_CTX contexte; + + const EVP_MD * (*EVP_sum)(); + + logical1 somme_invalide; + + long longueur_chaine; + + struct_objet *s_objet_argument_1; + struct_objet *s_objet_argument_2; + struct_objet *s_objet_resultat; + + unsigned char *chaine; + unsigned char *fonction; + unsigned char somme[EVP_MAX_MD_SIZE]; + + unsigned int longueur_somme; + + (*s_etat_processus).erreur_execution = d_ex; + + if ((*s_etat_processus).affichage_arguments == 'Y') + { + printf("\n DIGEST "); + + if ((*s_etat_processus).langue == 'F') + { + printf("(somme d'authentification)\n\n"); + } + else + { + printf("(hash algorithm)\n\n"); + } + + printf(" 2: %s\n", d_CHN); + printf(" 1: %s\n", d_CHN); + printf("-> 1: %s\n\n", d_CHN); + + printf(" 2: %s\n", d_CHN); + printf(" 1: %s\n", d_LST); + printf("-> 1: %s\n\n", d_CHN); + + if ((*s_etat_processus).langue == 'F') + { + printf(" Algorithmes :\n\n"); + } + else + { + printf(" Algorithms:\n\n"); + } + +# ifndef OPENSSL_NO_SHA + printf(" - DSS\n"); + printf(" - DSS1\n"); + printf(" - ECDSA\n"); +# endif +# ifndef OPENSSL_NO_MD2 + printf(" - MD2\n"); +# endif +# ifndef OPENSSL_NO_MD4 + printf(" - MD4\n"); +# endif +# ifndef OPENSSL_NO_MD5 + printf(" - MD5\n"); +# endif +# ifndef OPENSSL_NO_SHA + printf(" - SHA\n"); + printf(" - SHA1\n"); +# endif + + return; + } + else if ((*s_etat_processus).test_instruction == 'Y') + { + (*s_etat_processus).nombre_arguments = -1; + return; + } + + if (test_cfsf(s_etat_processus, 31) == d_vrai) + { + if (empilement_pile_last(s_etat_processus, 2) == d_erreur) + { + return; + } + } + + if (depilement(s_etat_processus, &((*s_etat_processus).l_base_pile), + &s_objet_argument_1) == d_erreur) + { + (*s_etat_processus).erreur_execution = d_ex_manque_argument; + return; + } + + if (depilement(s_etat_processus, &((*s_etat_processus).l_base_pile), + &s_objet_argument_2) == d_erreur) + { + liberation(s_etat_processus, s_objet_argument_1); + + (*s_etat_processus).erreur_execution = d_ex_manque_argument; + return; + } + + if (((*s_objet_argument_1).type == CHN) && + ((*s_objet_argument_2).type == CHN)) + { + // Liste des sommes disponibles : + // - EVP_dss + // - EVP_dss1 + // - EVP_ecdsa + // - EVP_md2 + // - EVP_md4 + // - EVP_md5 + // - EVP_mdc2 + // - EVP_ripemd160 + // - EVP_sha + // - EVP_sha1 + // - EVP_sha224 + // - EVP_sha256 + // - EVP_sha384 + // - EVP_sha512 + // - EVP_whirlpool + + if ((fonction = conversion_majuscule((unsigned char *) + (*s_objet_argument_1).objet)) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + somme_invalide = d_faux; + + switch(fonction[0]) + { + case 'D': + { + switch(fonction[1]) + { + case 'S': // ds + { + switch(fonction[2]) + { +# ifndef OPENSSL_NO_SHA + case 'S': // dss + { + switch(fonction[3]) + { + case d_code_fin_chaine: + { + EVP_sum = EVP_dss; + break; + } + + case '1': // dss1 + { + if (fonction[4] == d_code_fin_chaine) + { + EVP_sum = EVP_dss1; + } + else + { + somme_invalide = d_vrai; + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } +# endif + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + case 'E': + { + switch(fonction[1]) + { + case 'C': // ec + { + switch(fonction[2]) + { + case 'D': // ecd + { + switch(fonction[3]) + { + case 'S': // ecds + { + switch(fonction[4]) + { +# ifndef OPENSSL_NO_SHA + case 'A': // ecdsa + { + if (fonction[5] == + d_code_fin_chaine) + { + EVP_sum = EVP_ecdsa; + } + else + { + somme_invalide = d_vrai; + } + + break; + } +# endif + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + case 'M': + { + switch(fonction[1]) + { + case 'D': // md + { + switch(fonction[2]) + { +# ifndef OPENSSL_NO_MD2 + case '2': // md2 + { + if (fonction[3] == d_code_fin_chaine) + { + EVP_sum = EVP_md2; + } + else + { + somme_invalide = d_vrai; + } + + break; + } +# endif + +# ifndef OPENSSL_NO_MD4 + case '4': // md4 + { + if (fonction[3] == d_code_fin_chaine) + { + EVP_sum = EVP_md4; + } + else + { + somme_invalide = d_vrai; + } + + break; + } +# endif + +# ifndef OPENSSL_NO_MD4 + case '5': // md5 + { + if (fonction[3] == d_code_fin_chaine) + { + EVP_sum = EVP_md5; + } + else + { + somme_invalide = d_vrai; + } + + break; + } +# endif + + case 'C': // mdc + { + switch(fonction[3]) + { +# ifndef OPENSSL_NO_MDC2 + case '2': // mdc2 + { + if (fonction[4] == d_code_fin_chaine) + { + EVP_sum = EVP_mdc2; + } + else + { + somme_invalide = d_vrai; + } + + break; + } +# endif + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + +# ifndef OPENSSL_NO_RIPEMD + case 'R': + { + if (strcmp(fonction, "RIPEMD160") == 0) + { + EVP_sum = EVP_ripemd160; + } + else + { + somme_invalide = d_vrai; + } + + break; + } +# endif + + case 'S': + { + switch(fonction[1]) + { + case 'H': // sh + { + switch(fonction[2]) + { +# ifndef OPENSSL_NO_SHA + case 'A': + { + switch(fonction[3]) + { + case d_code_fin_chaine: + { + EVP_sum = EVP_sha; + break; + } + + case '1': // sha1 + { + if (fonction[4] == d_code_fin_chaine) + { + EVP_sum = EVP_sha1; + } + else + { + somme_invalide = d_vrai; + } + + break; + } + +# ifndef OPENSSL_NO_SHA256 + case '2': // sha2 + { + switch(fonction[4]) + { + case '2': // sha22 + { + switch(fonction[5]) + { + case '4': // sha224 + { + if (fonction[6] == + d_code_fin_chaine) + { + EVP_sum = + EVP_sha224; + } + else + { + somme_invalide = + d_vrai; + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + case '5': + { + switch(fonction[5]) + { + case '6': // sha256 + { + if (fonction[6] == + d_code_fin_chaine) + { + EVP_sum = + EVP_sha256; + } + else + { + somme_invalide = + d_vrai; + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } +# endif + +# ifndef OPENSSL_NO_SHA512 + case '3': // sha3 + { + switch(fonction[4]) + { + case '8': // sha38 + { + switch(fonction[5]) + { + case '4': // sha384 + { + if (fonction[6] == + d_code_fin_chaine) + { + EVP_sum = + EVP_sha384; + } + else + { + somme_invalide = + d_vrai; + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + case '5': // sha5 + { + switch(fonction[4]) + { + case '1': // sha51 + { + switch(fonction[5]) + { + case '2': // sha512 + { + if (fonction[6] == + d_code_fin_chaine) + { + EVP_sum = + EVP_sha512; + } + else + { + somme_invalide = + d_vrai; + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } +# endif + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } +# endif + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + + default: + { + somme_invalide = d_vrai; + break; + } + } + + break; + } + +# ifndef OPENSSL_NO_WHIRLPOOL + case 'W': + { + if (strcmp(fonction, "WHIRLPOOL") == 0) + { + EVP_sum = EVP_whirlpool; + } + else + { + somme_invalide = d_vrai; + } + + break; + } +# endif + + default: + { + somme_invalide = d_vrai; + break; + } + } + + free(fonction); + + if (somme_invalide == d_vrai) + { + liberation(s_etat_processus, s_objet_argument_1); + liberation(s_etat_processus, s_objet_argument_2); + + (*s_etat_processus).erreur_execution = + d_ex_chiffrement_indisponible; + return; + } + + if (EVP_DigestInit(&contexte, EVP_sum()) != 1) + { + EVP_MD_CTX_cleanup(&contexte); + + liberation(s_etat_processus, s_objet_argument_1); + liberation(s_etat_processus, s_objet_argument_2); + + (*s_etat_processus).erreur_execution = d_ex_chiffrement; + return; + } + + if ((chaine = formateur_flux(s_etat_processus, (unsigned char *) + (*s_objet_argument_2).objet, &longueur_chaine)) == NULL) + { + EVP_MD_CTX_cleanup(&contexte); + + liberation(s_etat_processus, s_objet_argument_1); + liberation(s_etat_processus, s_objet_argument_2); + + return; + } + + if (EVP_DigestUpdate(&contexte, chaine, longueur_chaine) != 1) + { + free(chaine); + EVP_MD_CTX_cleanup(&contexte); + + liberation(s_etat_processus, s_objet_argument_1); + liberation(s_etat_processus, s_objet_argument_2); + + (*s_etat_processus).erreur_execution = d_ex_chiffrement; + return; + } + + if (EVP_DigestFinal_ex(&contexte, somme, &longueur_somme) != 1) + { + free(chaine); + EVP_MD_CTX_cleanup(&contexte); + + liberation(s_etat_processus, s_objet_argument_1); + liberation(s_etat_processus, s_objet_argument_2); + + (*s_etat_processus).erreur_execution = d_ex_chiffrement; + return; + } + + free(chaine); + EVP_MD_CTX_cleanup(&contexte); + + if ((s_objet_resultat = allocation(s_etat_processus, CHN)) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (((*s_objet_resultat).objet = analyse_flux(s_etat_processus, + somme, longueur_somme)) == NULL) + { + liberation(s_etat_processus, s_objet_argument_1); + liberation(s_etat_processus, s_objet_argument_2); + + return; + } + } + else if (((*s_objet_argument_1).type == CHN) && + ((*s_objet_argument_2).type == LST)) + { + BUG(1, uprintf("Oops\n")); + s_objet_resultat = NULL; + } + else + { + liberation(s_etat_processus, s_objet_argument_1); + liberation(s_etat_processus, s_objet_argument_2); + + (*s_etat_processus).erreur_execution = d_ex_erreur_type_argument; + return; + } + + liberation(s_etat_processus, s_objet_argument_1); + liberation(s_etat_processus, s_objet_argument_2); + + if (empilement(s_etat_processus, &((*s_etat_processus).l_base_pile), + s_objet_resultat) == d_erreur) + { + return; + } + + return; +} + // vim: ts=4