ECCTK (Elliptic Curve Cryptographic ToolKit): generate_keypair.cpp

# generate_keypair.cpp

Voici un exemple de programme permettant de générer un bi-clef (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-ecc.h>

    s2byte generate_keypair()
    {
        s4byte  ret     = -1;

        /* set curve
         */
    #ifndef ECCTK_EVALUATION
        s2byte  curve   = ECCTK_CURVE_SEC_PRIME_256;
    #else
        s2byte  curve   = ECCTK_CURVE_SEC_PRIME_160;
    #endif

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

        /* private key context
         */
        KPRV_CTX *kprv;

        /* public key context
         */
        KPUB_CTX *kpub;

        /* 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;
        }

        /* generate new private key protected by password "g-4!yAv2"
         */
        kprv = GeneratePrivateKey(curve, "g-4!yAv2");
        if (kprv == NULL)
        {
            /* remove value for all curve in memory
            */
            FreeEcctkAll();

            fprintf(stderr, "Generate private key failed\n");
            return EXIT_FAILURE;
        }

        /* compute public key from private key
         */
        kpub = GeneratePublicKey(kprv, "g-4!yAv2");
        if (kpub == NULL)
        {
            /* remove value for all curve in memory
            */
            FreeEcctkAll();
            KprvCtxFree(kprv);

            fprintf(stderr, "Generate public key failed\n");
            return EXIT_FAILURE;
        }

        /* save public key on file
         */
        ret = WriteFileKpubCtx(kpubfile, kpub);
        if (ret != ECCTK_ECC_NO_ERROR)
        {
            fprintf(stderr, "Write public key on %s failed (Error : %ld)\n", kpubfile, ret);

            /* remove value for all curve in memory
             */
            FreeEcctkAll();
            KpubCtxFree(kpub);
            KprvCtxFree(kprv);
            return EXIT_FAILURE;
        }

        /* save private key on file
         */
        ret = WriteFileKprvCtx(kprvfile, kprv);
        if (ret != ECCTK_ECC_NO_ERROR)
        {
            fprintf(stderr, "Write private key on %s failed (%ld)\n", kprvfile, ret);

            /* remove value for all curve in memory
            */
            FreeEcctkAll();
            KpubCtxFree(kpub);
            KprvCtxFree(kprv);
            return EXIT_FAILURE;
        }

        fprintf(stdout, "Generate key pair success\n");

        /* free context
         */
        KpubCtxFree(kpub);
        KprvCtxFree(kprv);

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

        return EXIT_SUCCESS;
    }