Annotation of OpenXM/src/ox_ntl/crypt/cast5/casttest.c, Revision 1.1
1.1 ! iwane 1: /* $OpenXM$ */
! 2: /*
! 3: * rfc 2144
! 4: * The CAST-128 Encryption Algorithm
! 5: * Appendix B.
! 6: */
! 7:
! 8: #include <stdio.h>
! 9:
! 10: #include "cast5.h"
! 11:
! 12:
! 13: int
! 14: main()
! 15: {
! 16: cast128_key ckey;
! 17: unsigned char key0[] = "\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9a";
! 18: unsigned char a[] = "\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9a";
! 19: unsigned char b[] = "\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9a";
! 20: int bits[] = {16, 10, 5};
! 21: unsigned char ans[][9] = {
! 22: "\x23\x8b\x4f\xe5\x84\x7e\x44\xb2",
! 23: "\xeb\x6a\x71\x1a\x2c\x02\x27\x1b",
! 24: "\x7a\xc8\x16\xd1\x6e\x9b\x30\x2e" };
! 25: unsigned char plain[] = "\x01\x23\x45\x67\x89\xab\xcd\xef";
! 26: unsigned char enc[1024], dec[1024];
! 27: unsigned char ax[] = "\xee\xa9\xd0\xa2\x49\xfd\x3b\xa6\xb3\x43\x6f\xb8\x9d\x6d\xca\x92";
! 28: unsigned char bx[] = "\xb2\xc9\x5e\xb0\x0c\x31\xad\x71\x80\xac\x05\xb8\xe8\x3d\x69\x6e";
! 29:
! 30: uint32_t an[4], bn[4], cn[4], dn[4];
! 31:
! 32: int i, j;
! 33: int bit;
! 34:
! 35: for (i = 0; i < 3; i++) {
! 36:
! 37: bit = bits[i];
! 38: cast128_keyset(key0, bit, &ckey);
! 39:
! 40: cast128_enc_c(&ckey, plain, enc);
! 41:
! 42: if (memcmp(ans[i], enc, 8) != 0) {
! 43: printf("error. %d - enc\n", i);
! 44: exit(1);
! 45: }
! 46:
! 47: cast128_dec_c(&ckey, enc, dec);
! 48: if (memcmp(plain, dec, 8) != 0) {
! 49: printf("error. %d - dec\n", i);
! 50: exit(1);
! 51: }
! 52:
! 53: printf("%3d-bit is ok.\n", bit * 8);
! 54: }
! 55:
! 56:
! 57: bit = 128;
! 58: an[0] = bn[0] = 0x01234567;
! 59: an[1] = bn[1] = 0x12345678;
! 60: an[2] = bn[2] = 0x23456789;
! 61: an[3] = bn[3] = 0x3456789a;
! 62: for (i = 0; i < 1000000; i++) {
! 63: cast128_keyset(b, bit, &ckey);
! 64:
! 65: memcpy(dn, an, sizeof(an));
! 66:
! 67: cast128_enc_i(&ckey, an[0], an[1], &an[0], &an[1]);
! 68: cast128_enc_i(&ckey, an[2], an[3], &an[2], &an[3]);
! 69:
! 70: cast128_dec_i(&ckey, an[0], an[1], &cn[0], &cn[1]);
! 71: cast128_dec_i(&ckey, an[2], an[3], &cn[2], &cn[3]);
! 72:
! 73: if (dn[0] != cn[0] || dn[1] != cn[1] || dn[2] != cn[2] || dn[3] != cn[3]) {
! 74: fprintf(stderr, "dec error. a. %d\n", i);
! 75: }
! 76:
! 77: for (j = 0; j < 16; j++) {
! 78: a[j] = (an[j / 4] >> (8 * (3 - j % 4))) & 0xff;
! 79: }
! 80:
! 81: memcpy(dn, bn, sizeof(bn));
! 82:
! 83: cast128_keyset(a, bit, &ckey);
! 84: cast128_enc_i(&ckey, bn[0], bn[1], &bn[0], &bn[1]);
! 85: cast128_enc_i(&ckey, bn[2], bn[3], &bn[2], &bn[3]);
! 86:
! 87: cast128_dec_i(&ckey, bn[0], bn[1], &cn[0], &cn[1]);
! 88: cast128_dec_i(&ckey, bn[2], bn[3], &cn[2], &cn[3]);
! 89:
! 90: if (dn[0] != cn[0] || dn[1] != cn[1] || dn[2] != cn[2] || dn[3] != cn[3]) {
! 91: fprintf(stderr, "dec error. a. %d\n", i);
! 92: }
! 93:
! 94: for (j = 0; j < 16; j++) {
! 95: b[j] = (bn[j / 4] >> (8 * (3 - j % 4))) & 0xff;
! 96: }
! 97: }
! 98:
! 99: if (memcmp(a, ax, 16) != 0) {
! 100: fprintf(stderr, "error: a\n");
! 101: exit(1);
! 102: }
! 103: if (memcmp(b, bx, 16) != 0) {
! 104: fprintf(stderr, "error: b\n");
! 105: exit(1);
! 106: }
! 107:
! 108:
! 109: fprintf(stderr, "all test is ok\n");
! 110:
! 111: return (0);
! 112: }
! 113:
! 114:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>