mac_stream.cpp

Voici un exemple de programme permettant d'effectuer le calcul du MAC d'un buffer mémoire. Ce code est générique et permet d'utiliser tous les algorithmes présent dans la librairie. Les éléments fixés ne le sont qu'à titre d'exemple.

/* -----------------------------------------------------------------------
 *
 * Copyright (c) 2002-2005 Mr Ludovic FLAMENT <ludovic.flament@free.fr>, LIEVIN, FRANCE
 *
 * Termes :
 *
 * L'utilisation et/ou la redistribution de ce code (avec ou sans modification),
 * ainsi que de tous les composants du produit (librairies, documentation, exemples, ...)
 * est soumise aux termes de la licence qui vous est attribuée par son auteur.
 *
 * -----------------------------------------------------------------------
 */

#include <stdlib.h>
#include <stdio.h>

#include <include/ecctk-prototype.h>
#include <include/ecctk-hash.h>

void mac_stream()
{
    s2byte  ret     = -1;

    /* set MAC algorithm
     */
#ifndef ECCTK_EVALUATION
    s2byte  algo    = ECCTK_HASH_MAC_ALGO_RMD160;
#else
    s2byte  algo    = ECCTK_HASH_MAC_ALGO_RMD128;
#endif
    
    /* set stream to MAC and create output stream with maximum size for the hash value
     */
    u1byte  in[]    = "Elliptic Curve Cryptographic ToolKit";
    
#ifndef ECCTK_EVALUATION
    u1byte  hash[ECCTK_HASH_MAC_ALGO_RMD160];
#else
    u1byte  hash[ECCTK_HASH_MAC_ALGO_RMD128];
#endif
    
    /* set the value of the key
     */
    u1byte  key[] = {0x34, 0x12, 0x45, 0xa3, 0x1b, 0x59, 0x4f, 0x9d, 0xfc, 0x45,
                     0xa3, 0x1b, 0x67, 0x35, 0x34, 0x12, 0xde, 0xa8, 0xf9, 0x5c};

    /* the size of hash value, only use to print the result
     */
    u2byte  hashsize;

    switch (algo)
    {
    case ECCTK_HASH_MAC_ALGO_RMD128:
        hashsize = ECCTK_HASH_SIZE_RMD128;
        ret = RIPEMD128_StreamMAC(in, sizeof(in), key, sizeof(key), hash, hashsize);
        break;
#ifndef ECCTK_EVALUATION
    case ECCTK_HASH_MAC_ALGO_RMD160:
        hashsize = ECCTK_HASH_SIZE_RMD160;
        ret = RIPEMD160_StreamMAC(in, sizeof(in), key, sizeof(key), hash, hashsize);
        break;
#endif
    default:
        break;
    }

    if (ret != ECCTK_HASH_NO_ERROR)
        printf("MAC stream failed (Error : %d)\n", ret);
    else
    {
        u4byte i;

        printf("MAC of stream = ");
        for (i = 0; i < hashsize; i++)
            printf("%02x", hash[i]);
        printf("\n");
    }
}

Généré pour ECCTK (Elliptic Curve Cryptographic ToolKit) avec  doxygen