ECCTK (Elliptic Curve Cryptographic ToolKit): encrypt_stream_asym.cpp

# encrypt_stream_asym.cpp

Voici un exemple de programme permettant de chiffrer un buffer mémoire (algorithme asymétrique). 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>
    #include <include/ecctk-ecc.h>

    s2byte encrypt_stream_asym()
    {
        u2byte  ret;

        /* set algo
         */
        s2byte  algo    = ECCTK_ECC_ALGO_PSEC3;

        /* set hash & hmac algorithm
         */
    #ifndef ECCTK_EVALUATION
        s2byte  hash_algo = ECCTK_HASH_ALGO_SHA256,
    #else
        s2byte  hash_algo = ECCTK_HASH_ALGO_SHA160,
    #endif
                hmac_algo = ECCTK_HASH_HMAC_ALGO_MD5;

        /* set input/output file name
         */
        s1byte  kpubfile[]  = "public_key_file",
                cipherfile[]= "cipher_file";

        /* set input/output buffer
         */
        u1byte  in[] = "Elliptic Curve Cryptographic ToolKit",
                *out;

        /* used to get the size of output buffer
         */
        s4byte  outsize;

        /* public key context
         */
        KPUB_CTX *kpub;

        /* cipher context
         */
        ECC_CIPHER_CTX *cipher;

        /* read public key on file
         */
        kpub = ReadFileKpubCtx(kpubfile);
        if (kpub == NULL)
        {
            fprintf(stderr, "Read public key on %s failed\n", kpubfile);

            return EXIT_FAILURE;
        }

        /* read cipher context on file
         */
        cipher = ReadFileEccCipherCtx(cipherfile);
        if (cipher == NULL)
        {
            fprintf(stderr, "Read cipher on %s failed\n", cipherfile);

            KpubCtxFree(kpub);
            return EXIT_FAILURE;
        }

        /* Initialize value for all curve in memory
         */
        ret = InitializeEcctkAll();
        if (ret != ECCTK_ECC_NO_ERROR)
        {
            fprintf(stderr, "InitializeEcctkAll failed (%d)\n", ret);
            return EXIT_FAILURE;
        }

        switch (algo)
        {
        case ECCTK_ECC_ALGO_ECIES:
            out = EncryptStreamEcies(in, sizeof(in), &outsize, hmac_algo, kpub, cipher);
            break;
        case ECCTK_ECC_ALGO_PSEC3:
            out = EncryptStreamPsec3(in, sizeof(in), &outsize, hash_algo, kpub, cipher);
            break;
        default:
            out = NULL;
            break;
        }

        if (out == NULL)
            fprintf(stderr, "Encrypt stream failed\n");
        else
            fprintf(stdout, "Encrypt stream success, result stream size = %ld\n", outsize);

        free(out);

        /* free context
         */
        KpubCtxFree(kpub);
        EccCipherCtxFree(cipher);

        /* remove value for all curve in memory
         */
        FreeEcctkAll();

        return EXIT_SUCCESS;
    }