[BACK]Return to gmprsatest.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_ntl / crypt / rsa

Annotation of OpenXM/src/ox_ntl/crypt/rsa/gmprsatest.c, Revision 1.1

1.1     ! iwane       1: /* $OpenXM$ */
        !             2:
        !             3: #include <stdio.h>
        !             4: #include <stdlib.h>
        !             5: #include <string.h>
        !             6: #include <gmp.h>
        !             7:
        !             8: #include "gmprsa.h"
        !             9:
        !            10: /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
        !            11:  * ALLOC
        !            12:  *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
        !            13: #define RSA_KEY_DEBUG 0
        !            14: #define RSA_KEY_PRINT 1
        !            15:
        !            16:
        !            17: static int
        !            18: rsa_alloc_cnt(int n)
        !            19: {
        !            20:        static int cnt = 0;
        !            21:
        !            22:        cnt += n;
        !            23:
        !            24:        return (cnt);
        !            25: }
        !            26:
        !            27:
        !            28: static void *
        !            29: rsa_malloc(size_t size)
        !            30: {
        !            31:        void *ptr;
        !            32:
        !            33:        ptr = malloc(size);
        !            34:        if (ptr) {
        !            35:                rsa_alloc_cnt(1);
        !            36: #if RSA_KEY_DEBUG
        !            37:                printf("%2d: %p: alloc  [%d]\n", rsa_alloc_cnt(0), ptr, size);
        !            38: #endif
        !            39:        }
        !            40:
        !            41:        return (ptr);
        !            42: }
        !            43:
        !            44: static void
        !            45: rsa_free(void *ptr, size_t size)
        !            46: {
        !            47:        free(ptr);
        !            48:        rsa_alloc_cnt(-1);
        !            49: #if RSA_KEY_DEBUG
        !            50:        printf("%2d: %p: free\n", rsa_alloc_cnt(0) + 1, ptr);
        !            51: #endif
        !            52: }
        !            53:
        !            54:
        !            55: void *
        !            56: rsa_realloc(void *org, size_t old, size_t size)
        !            57: {
        !            58:        void *ptr;
        !            59:
        !            60:        ptr = malloc(size);
        !            61:        memcpy(ptr, org, old);
        !            62:
        !            63: #if RSA_KEY_DEBUG
        !            64:        printf("%2d: -%p: realloc [%d]\n", rsa_alloc_cnt(0), org, old);
        !            65:        printf("%2d: +%p: realloc [%d]\n", rsa_alloc_cnt(0), ptr, size);
        !            66: #endif
        !            67:
        !            68:        free(org);
        !            69:        return (ptr);
        !            70: }
        !            71:
        !            72: void
        !            73: rsa_set_memory_functions(void)
        !            74: {
        !            75:        mp_set_memory_functions(rsa_malloc, rsa_realloc, rsa_free);
        !            76: }
        !            77:
        !            78:
        !            79:
        !            80: int
        !            81: main(int argc, char *argv[])
        !            82: {
        !            83:        mpz_t a, b;
        !            84:        int add = 1024;
        !            85:        rsa_key rsa;
        !            86:
        !            87:        char *msg = "OpenXM Project.";
        !            88:
        !            89:        char *msgp = msg;
        !            90:        int total = strlen(msg) + 1;
        !            91:        unsigned char eb[9024], *ebp = eb;
        !            92:        unsigned char db[9024], *dbp = db;
        !            93:        int x;
        !            94:        int a1, a2;
        !            95:
        !            96:        if (argc >= 2)
        !            97:                add = atoi(argv[1]);
        !            98:
        !            99:        rsa_set_memory_functions();
        !           100:
        !           101:        mpz_init(a);
        !           102:        mpz_init(b);
        !           103:
        !           104:        rsa_init(&rsa);
        !           105:
        !           106:        x = rsa_keygen(&rsa, a, b, 1024, 10);
        !           107:        printf("\nkeygen = %d\n", x); fflush(stdout);
        !           108:
        !           109: #if 0
        !           110:        gmp_printf("n=%Zx\n", rsa.mod);
        !           111:        gmp_printf("p=%Zx\n", rsa.p);
        !           112:        gmp_printf("q=%Zx\n", rsa.q);
        !           113: #endif
        !           114:
        !           115:
        !           116: #define ENC 0
        !           117:
        !           118:
        !           119:        for (x = 0; total > 0; x++) {
        !           120: #if ENC
        !           121:                a1 = rsa_encrypt_by_public_key(&rsa, ebp, msgp, strlen(msgp) + 1, RSA_PKCS_1_PADDING);
        !           122: #else
        !           123:                a1 = rsa_encrypt_by_private_key(&rsa, ebp, msgp, strlen(msgp) + 1, RSA_PKCS_1_PADDING);
        !           124: #endif
        !           125:
        !           126:                printf("a1 = %d, total = %d\n", a1, total);
        !           127:                if (a1 < 0)
        !           128:                        exit(0);
        !           129:
        !           130:                msgp += a1;
        !           131:                ebp += rsa.k;
        !           132:
        !           133:                total -= a1;
        !           134:        }
        !           135:
        !           136:        ebp = eb;
        !           137:        for (; x > 0; x--) {
        !           138: #if ENC
        !           139:                a2 = rsa_decrypt_by_private_key(&rsa, dbp, ebp, RSA_PKCS_1_PADDING);
        !           140: #else
        !           141:                a2 = rsa_decrypt_by_public_key(&rsa, dbp, ebp, RSA_PKCS_1_PADDING);
        !           142: #endif
        !           143:
        !           144: printf("a2 = %d: x = %d\n", a2, x);
        !           145:                if (a2 < 0)
        !           146:                        exit(0);
        !           147:
        !           148:                dbp += a2;
        !           149:                ebp += rsa.k;
        !           150:        }
        !           151:
        !           152:        printf("db = %s\n", db);
        !           153:
        !           154:        mpz_clear(a);
        !           155:        mpz_clear(b);
        !           156:
        !           157:        rsa_clear(&rsa);
        !           158:
        !           159:        printf("%d, %d -- %d\n", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, rsa_alloc_cnt(0));
        !           160:
        !           161:        return (0);
        !           162: }
        !           163:
        !           164:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>