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

Annotation of OpenXM/src/ox_ntl/crypt/des/destest.c, Revision 1.1

1.1     ! iwane       1: /* $OpenXM$ */
        !             2: /*
        !             3:  * FIPS 81 - Des Modes of Operation
        !             4:  */
        !             5:
        !             6: #include <stdio.h>
        !             7: #include <string.h>
        !             8:
        !             9: #include "des.h"
        !            10: #define N 4096
        !            11:
        !            12: int
        !            13: main()
        !            14: {
        !            15:        des_key dkey;
        !            16:        unsigned char key[] = "\x01\x23\x45\x67\x89\xab\xcd\xef"; /* secret key */
        !            17:        unsigned char *iv = (unsigned char *)"\x12\x34\x56\x78\x90\xab\xcd\xef";   /* initial vector */
        !            18:        unsigned char enc[N], dec[N];
        !            19:        int i, k;
        !            20:        int ret;
        !            21:        int cnt = 0;
        !            22:
        !            23:        unsigned char ecb[] =
        !            24:                "\x3f\xa4\x0e\x8a\x98\x4d\x48\x15"
        !            25:                "\x6a\x27\x17\x87\xab\x88\x83\xf9"
        !            26:                "\x89\x3d\x51\xec\x4b\x56\x3b\x53";
        !            27:        unsigned char cbc[] =
        !            28:                "\xe5\xc7\xcd\xde\x87\x2b\xf2\x7c"
        !            29:                "\x43\xe9\x34\x00\x8c\x38\x9c\x0f"
        !            30:                "\x68\x37\x88\x49\x9a\x7c\x05\xf6";
        !            31:        unsigned char cfb8[] =
        !            32:                "\xf3\x1f\xda\x07\x01\x14\x62\xee"
        !            33:                "\x18\x7f\x43\xd8\x0a\x7c\xd9\xb5"
        !            34:                "\xb0\xd2\x90\xda\x6e\x5b\x9a\x87";
        !            35:        unsigned char cfb16[] =
        !            36:                "\xf3\x09\x87\x87\x7f\x57\xf7\x3c"
        !            37:                "\x36\xb6\xdb\x70\xd8\xd5\x34\x19"
        !            38:                "\xd3\x86\xb2\x23\xb7\xb2\xad\x1b";
        !            39:        unsigned char cfb32[] =
        !            40:                "\xf3\x09\x62\x49\xa4\xdf\xa4\x9f"
        !            41:                "\x33\xdc\x7b\xad\x4c\xc8\x9f\x64"
        !            42:                "\xe4\x53\xe5\xec\x67\x20\xda\xb6";
        !            43:        unsigned char cfb48[] =
        !            44:                "\xf3\x09\x62\x49\xc7\xf4\x30\xb5"
        !            45:                "\x15\xec\xbb\x85\x97\x5a\x13\x8c"
        !            46:                "\x68\x60\xe2\x38\x34\x3c\xdc\x1f";
        !            47:        unsigned char cfb64[] =
        !            48:                "\xf3\x09\x62\x49\xc7\xf4\x6e\x51"
        !            49:                "\xa6\x9e\x83\x9b\x1a\x92\xf7\x84"
        !            50:                "\x03\x46\x71\x33\x89\x8e\xa6\x22";
        !            51: /*
        !            52:        unsigned char cfb56[] =
        !            53:                "\xf3\x09\x62\x49\xc7\xf4\x6e\x2f"
        !            54:                "\xfb\xe9\x55\x13\xa4\xc9\x5a\xa7"
        !            55:                "\xac\x61\x21\x62\x52";
        !            56: */
        !            57:        unsigned char ofb8[] =
        !            58:                "\xf3\x4a\x28\x50\xc9\xc6\x49\x85"
        !            59:                "\xd6\x84";
        !            60:        unsigned char ofb64[] =
        !            61:                "\xf3\x09\x62\x49\xc7\xf4\x6e\x51"
        !            62:                "\x35\xf2\x4a\x24\x2e\xeb\x3d\x3f"
        !            63:                "\x3d\x6d\x5b\xe3\x25\x5a\xf8\xc3";
        !            64:        unsigned char cbc_2[] =
        !            65:                "\xb9\x91\x6b\x8e\xe4\xc3\xda\x64"
        !            66:                "\xb4\xf4\x4e\x3c\xbe\xfb\x99\x48"
        !            67:                "\x45\x21\x38\x8f\xa5\x9a\xe6\x7d"
        !            68:                "\x58\xd2\xe7\x7e\x86\x06\x27\x33";
        !            69:        unsigned char cfb8_2[] =
        !            70:                "\x8a\x87\x01\xd9\x21\x30\x8f\x35"
        !            71:                "\x74\xbd\x1e\x74\xf4\x32\x14\x77"
        !            72:                "\x0c\x7a\x34\x8f\x84\x3d\x0d\x08"
        !            73:                "\x34\xb2\xc4\xfa";
        !            74:        const unsigned char *plain = (unsigned char *)"Now is the time for all ";
        !            75:        const unsigned char *plain2 = (unsigned char *)"7654321 Now is the time for \0\0\0\0\0\0\0\0";
        !            76:        const unsigned char *p;
        !            77:
        !            78:        des_keyset(key, &dkey);
        !            79:
        !            80:        k = 24;
        !            81:        ret = des_enc_ecb(&dkey, k, plain, enc);
        !            82:
        !            83:        if (memcmp(ecb, enc, k)) {
        !            84:                printf("ECB enc failed.\n");
        !            85:                printf("ret = %d\n", ret);
        !            86:                goto _CBC;
        !            87:        }
        !            88:
        !            89:        ret = des_dec_ecb(&dkey, k, enc, dec);
        !            90:        if (memcmp(plain, dec, k)) {
        !            91:                printf("ECB dec failed.\n");
        !            92:                printf("ret = %d\n", ret);
        !            93:                goto _CBC;
        !            94:        }
        !            95:
        !            96:        printf("ECB ok.\n");
        !            97:        cnt++;
        !            98:
        !            99: _CBC:
        !           100:        for (i = 0; i < 2; i++) {
        !           101:                const unsigned char *ci[] = {cbc, cbc_2};
        !           102:                int ks[] = {24, 32};
        !           103:                const unsigned char *d[] = {plain, plain2};
        !           104:
        !           105:                k = ks[i];
        !           106:                p = d[i];
        !           107:
        !           108:                ret = des_enc_cbc(&dkey, iv, k, p, enc);
        !           109:                if (memcmp(ci[i], enc, k)) {
        !           110:                        printf("CBC enc failed.\n");
        !           111:                        printf("ret = %d, i = %d\n", ret, i);
        !           112:                        goto _CFB;
        !           113:                }
        !           114:
        !           115:                ret = des_dec_cbc(&dkey, iv, k, enc, dec);
        !           116:                if (memcmp(p, dec, k)) {
        !           117:                        printf("CBC dec failed.\n");
        !           118:                        printf("ret = %d, i = %d\n", ret, i);
        !           119:                        goto _CFB;
        !           120:                }
        !           121:        }
        !           122:
        !           123:        printf("CBC ok.\n");
        !           124:        cnt++;
        !           125:
        !           126: _CFB:
        !           127:        for (i = 0; i < 6; i++) {
        !           128:                int bit[] = {8, 16, 32, 48, 64, 8};
        !           129:                const unsigned char *ci[] = {cfb8, cfb16, cfb32, cfb48, cfb64, cfb8_2};
        !           130:                int ks[] = {24, 24, 24, 24, 24, 28};
        !           131:                const unsigned char *d[] = {plain, plain, plain, plain, plain, plain2};
        !           132:
        !           133:
        !           134:                k = ks[i];
        !           135:                p = d[i];
        !           136:                ret = des_enc_cfb(&dkey, bit[i], iv, k, p, enc);
        !           137:                if (memcmp(ci[i], enc, k)) {
        !           138:                        printf("CFB 8-bit enc failed.\n");
        !           139:                        printf("ret = %d, i = %d\n", ret, i);
        !           140:                        goto _OFB;
        !           141:                }
        !           142:
        !           143:                ret = des_dec_cfb(&dkey, bit[i], iv, k, enc, dec);
        !           144:                if (memcmp(p, dec, k)) {
        !           145:                        printf("CFB 8-bit dec failed.\n");
        !           146:                        printf("ret = %d, i = %d\n", ret, i);
        !           147:                        goto _OFB;
        !           148:                }
        !           149:        }
        !           150:
        !           151:        printf("CFB ok.\n");
        !           152:        cnt++;
        !           153:
        !           154: _OFB:
        !           155:        for (i = 0; i < 1; i++) {
        !           156:                int bit[] = {8, 64};
        !           157:                const unsigned char *ci[] = {ofb8, ofb64};
        !           158:                int ks[] = {10, 24};
        !           159:                const unsigned char *d[] = {plain, plain};
        !           160:
        !           161:                k = ks[i];
        !           162:                p = d[i];
        !           163:                ret = des_ofb(&dkey, bit[i], iv, k, p, enc);
        !           164:                if (memcmp(ci[i], enc, k)) {
        !           165:                        printf("CFB 8-bit enc failed.\n");
        !           166:                        printf("ret = %d, i = %d\n", ret, i);
        !           167:                        goto _END;
        !           168:                }
        !           169:
        !           170:                ret = des_ofb(&dkey, bit[i], iv, k, enc, dec);
        !           171:                if (memcmp(p, dec, k)) {
        !           172:                        printf("CFB 8-bit dec failed.\n");
        !           173:                        printf("ret = %d, i = %d\n", ret, i);
        !           174:                        goto _END;
        !           175:                }
        !           176:        }
        !           177:
        !           178:        printf("OFB ok.\n");
        !           179:        cnt++;
        !           180:
        !           181: _END:
        !           182:        if (cnt == 4) {
        !           183:                printf("all test is ok.\n");
        !           184:        } else {
        !           185:                printf("error has found.\n");
        !           186:        }
        !           187:
        !           188:        return (0);
        !           189: }
        !           190:
        !           191:
        !           192:

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