Blame src/lib/crypto/builtin/des/t_verify.c

Packit Bot 805b76
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit Bot 805b76
/* lib/crypto/builtin/des/t_verify.c */
Packit Bot 805b76
/*
Packit Bot 805b76
 * Copyright 1988, 1990 by the Massachusetts Institute of Technology.
Packit Bot 805b76
 * All Rights Reserved.
Packit Bot 805b76
 *
Packit Bot 805b76
 * Export of this software from the United States of America may
Packit Bot 805b76
 *   require a specific license from the United States Government.
Packit Bot 805b76
 *   It is the responsibility of any person or organization contemplating
Packit Bot 805b76
 *   export to obtain such a license before exporting.
Packit Bot 805b76
 *
Packit Bot 805b76
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit Bot 805b76
 * distribute this software and its documentation for any purpose and
Packit Bot 805b76
 * without fee is hereby granted, provided that the above copyright
Packit Bot 805b76
 * notice appear in all copies and that both that copyright notice and
Packit Bot 805b76
 * this permission notice appear in supporting documentation, and that
Packit Bot 805b76
 * the name of M.I.T. not be used in advertising or publicity pertaining
Packit Bot 805b76
 * to distribution of the software without specific, written prior
Packit Bot 805b76
 * permission.  Furthermore if you modify this software you must label
Packit Bot 805b76
 * your software as modified software and not distribute it in such a
Packit Bot 805b76
 * fashion that it might be confused with the original M.I.T. software.
Packit Bot 805b76
 * M.I.T. makes no representations about the suitability of
Packit Bot 805b76
 * this software for any purpose.  It is provided "as is" without express
Packit Bot 805b76
 * or implied warranty.
Packit Bot 805b76
 */
Packit Bot 805b76
/*
Packit Bot 805b76
 * Copyright (C) 1998 by the FundsXpress, INC.
Packit Bot 805b76
 *
Packit Bot 805b76
 * All rights reserved.
Packit Bot 805b76
 *
Packit Bot 805b76
 * Export of this software from the United States of America may require
Packit Bot 805b76
 * a specific license from the United States Government.  It is the
Packit Bot 805b76
 * responsibility of any person or organization contemplating export to
Packit Bot 805b76
 * obtain such a license before exporting.
Packit Bot 805b76
 *
Packit Bot 805b76
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit Bot 805b76
 * distribute this software and its documentation for any purpose and
Packit Bot 805b76
 * without fee is hereby granted, provided that the above copyright
Packit Bot 805b76
 * notice appear in all copies and that both that copyright notice and
Packit Bot 805b76
 * this permission notice appear in supporting documentation, and that
Packit Bot 805b76
 * the name of FundsXpress. not be used in advertising or publicity pertaining
Packit Bot 805b76
 * to distribution of the software without specific, written prior
Packit Bot 805b76
 * permission.  FundsXpress makes no representations about the suitability of
Packit Bot 805b76
 * this software for any purpose.  It is provided "as is" without express
Packit Bot 805b76
 * or implied warranty.
Packit Bot 805b76
 *
Packit Bot 805b76
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
Packit Bot 805b76
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
Packit Bot 805b76
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit Bot 805b76
 */
Packit Bot 805b76
Packit Bot 805b76
/*
Packit Bot 805b76
 *
Packit Bot 805b76
 * Program to test the correctness of the DES library
Packit Bot 805b76
 * implementation.
Packit Bot 805b76
 *
Packit Bot 805b76
 * exit returns  0 ==> success
Packit Bot 805b76
 *              -1 ==> error
Packit Bot 805b76
 */
Packit Bot 805b76
Packit Bot 805b76
#include "k5-int.h"
Packit Bot 805b76
#include "des_int.h"
Packit Bot 805b76
#include <stdio.h>
Packit Bot 805b76
#include "com_err.h"
Packit Bot 805b76
Packit Bot 805b76
static void do_encrypt(unsigned char *, unsigned char *);
Packit Bot 805b76
static void do_decrypt(unsigned char *, unsigned char *);
Packit Bot 805b76
Packit Bot 805b76
char *progname;
Packit Bot 805b76
int nflag = 2;
Packit Bot 805b76
int vflag;
Packit Bot 805b76
int mflag;
Packit Bot 805b76
int zflag;
Packit Bot 805b76
int pid;
Packit Bot 805b76
int mit_des_debug;
Packit Bot 805b76
Packit Bot 805b76
unsigned char cipher_text[64];
Packit Bot 805b76
unsigned char clear_text[64] = "Now is the time for all " ;
Packit Bot 805b76
unsigned char clear_text2[64] = "7654321 Now is the time for ";
Packit Bot 805b76
unsigned char clear_text3[64] = {2,0,0,0, 1,0,0,0};
Packit Bot 805b76
unsigned char output[64];
Packit Bot 805b76
unsigned char zero_text[8] = {0x0,0,0,0,0,0,0,0};
Packit Bot 805b76
unsigned char msb_text[8] = {0x0,0,0,0, 0,0,0,0x40}; /* to ANSI MSB */
Packit Bot 805b76
unsigned char *input;
Packit Bot 805b76
Packit Bot 805b76
/* 0x0123456789abcdef */
Packit Bot 805b76
unsigned char default_key[8] = {
Packit Bot 805b76
    0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
Packit Bot 805b76
};
Packit Bot 805b76
unsigned char key2[8] = { 0x08,0x19,0x2a,0x3b,0x4c,0x5d,0x6e,0x7f };
Packit Bot 805b76
unsigned char key3[8] = { 0x80,1,1,1,1,1,1,1 };
Packit Bot 805b76
mit_des_cblock s_key;
Packit Bot 805b76
unsigned char default_ivec[8] = {
Packit Bot 805b76
    0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
Packit Bot 805b76
};
Packit Bot 805b76
unsigned char *ivec;
Packit Bot 805b76
unsigned char zero_key[8] = {1,1,1,1,1,1,1,1}; /* just parity bits */
Packit Bot 805b76
Packit Bot 805b76
unsigned char cipher1[8] = {
Packit Bot 805b76
    0x25,0xdd,0xac,0x3e,0x96,0x17,0x64,0x67
Packit Bot 805b76
};
Packit Bot 805b76
unsigned char cipher2[8] = {
Packit Bot 805b76
    0x3f,0xa4,0x0e,0x8a,0x98,0x4d,0x48,0x15
Packit Bot 805b76
};
Packit Bot 805b76
unsigned char cipher3[64] = {
Packit Bot 805b76
    0xe5,0xc7,0xcd,0xde,0x87,0x2b,0xf2,0x7c,
Packit Bot 805b76
    0x43,0xe9,0x34,0x00,0x8c,0x38,0x9c,0x0f,
Packit Bot 805b76
    0x68,0x37,0x88,0x49,0x9a,0x7c,0x05,0xf6
Packit Bot 805b76
};
Packit Bot 805b76
unsigned char checksum[8] = {
Packit Bot 805b76
    0x58,0xd2,0xe7,0x7e,0x86,0x06,0x27,0x33
Packit Bot 805b76
};
Packit Bot 805b76
Packit Bot 805b76
unsigned char zresult[8] = {
Packit Bot 805b76
    0x8c, 0xa6, 0x4d, 0xe9, 0xc1, 0xb1, 0x23, 0xa7
Packit Bot 805b76
};
Packit Bot 805b76
Packit Bot 805b76
unsigned char mresult[8] = {
Packit Bot 805b76
    0xa3, 0x80, 0xe0, 0x2a, 0x6b, 0xe5, 0x46, 0x96
Packit Bot 805b76
};
Packit Bot 805b76
Packit Bot 805b76
Packit Bot 805b76
/*
Packit Bot 805b76
 * Can also add :
Packit Bot 805b76
 * plaintext = 0, key = 0, cipher = 0x8ca64de9c1b123a7 (or is it a 1?)
Packit Bot 805b76
 */
Packit Bot 805b76
Packit Bot 805b76
mit_des_key_schedule sched;
Packit Bot 805b76
Packit Bot 805b76
int
Packit Bot 805b76
main(argc,argv)
Packit Bot 805b76
    int argc;
Packit Bot 805b76
    char *argv[];
Packit Bot 805b76
{
Packit Bot 805b76
    /* Local Declarations */
Packit Bot 805b76
    size_t  in_length;
Packit Bot 805b76
    int  retval;
Packit Bot 805b76
    int i, j;
Packit Bot 805b76
Packit Bot 805b76
#ifdef WINDOWS
Packit Bot 805b76
    /* Set screen window buffer to infinite size -- MS default is tiny.  */
Packit Bot 805b76
    _wsetscreenbuf (fileno (stdout), _WINBUFINF);
Packit Bot 805b76
#endif
Packit Bot 805b76
    progname=argv[0];           /* salt away invoking program */
Packit Bot 805b76
Packit Bot 805b76
    while (--argc > 0 && (*++argv)[0] == '-')
Packit Bot 805b76
        for (i=1; argv[0][i] != '\0'; i++) {
Packit Bot 805b76
            switch (argv[0][i]) {
Packit Bot 805b76
Packit Bot 805b76
                /* debug flag */
Packit Bot 805b76
            case 'd':
Packit Bot 805b76
                mit_des_debug=3;
Packit Bot 805b76
                continue;
Packit Bot 805b76
Packit Bot 805b76
            case 'z':
Packit Bot 805b76
                zflag = 1;
Packit Bot 805b76
                continue;
Packit Bot 805b76
Packit Bot 805b76
            case 'm':
Packit Bot 805b76
                mflag = 1;
Packit Bot 805b76
                continue;
Packit Bot 805b76
Packit Bot 805b76
            default:
Packit Bot 805b76
                printf("%s: illegal flag \"%c\" ",
Packit Bot 805b76
                       progname,argv[0][i]);
Packit Bot 805b76
                exit(1);
Packit Bot 805b76
            }
Packit Bot 805b76
        };
Packit Bot 805b76
Packit Bot 805b76
    if (argc) {
Packit Bot 805b76
        fprintf(stderr, "Usage: %s [-dmz]\n", progname);
Packit Bot 805b76
        exit(1);
Packit Bot 805b76
    }
Packit Bot 805b76
Packit Bot 805b76
    /* do some initialisation */
Packit Bot 805b76
Packit Bot 805b76
    /* use known input and key */
Packit Bot 805b76
Packit Bot 805b76
    /* ECB zero text zero key */
Packit Bot 805b76
    if (zflag) {
Packit Bot 805b76
        input = zero_text;
Packit Bot 805b76
        mit_des_key_sched(zero_key, sched);
Packit Bot 805b76
        printf("plaintext = key = 0, cipher = 0x8ca64de9c1b123a7\n");
Packit Bot 805b76
        do_encrypt(input,cipher_text);
Packit Bot 805b76
        printf("\tcipher  = (low to high bytes)\n\t\t");
Packit Bot 805b76
        for (j = 0; j<=7; j++)
Packit Bot 805b76
            printf("%02x ",cipher_text[j]);
Packit Bot 805b76
        printf("\n");
Packit Bot 805b76
        do_decrypt(output,cipher_text);
Packit Bot 805b76
        if ( memcmp((char *)cipher_text, (char *)zresult, 8) ) {
Packit Bot 805b76
            printf("verify: error in zero key test\n");
Packit Bot 805b76
            exit(-1);
Packit Bot 805b76
        }
Packit Bot 805b76
Packit Bot 805b76
        exit(0);
Packit Bot 805b76
    }
Packit Bot 805b76
Packit Bot 805b76
    if (mflag) {
Packit Bot 805b76
        input = msb_text;
Packit Bot 805b76
        mit_des_key_sched(key3, sched);
Packit Bot 805b76
        printf("plaintext = 0x00 00 00 00 00 00 00 40, ");
Packit Bot 805b76
        printf("key = 0x80 01 01 01 01 01 01 01\n");
Packit Bot 805b76
        printf("        cipher = 0xa380e02a6be54696\n");
Packit Bot 805b76
        do_encrypt(input,cipher_text);
Packit Bot 805b76
        printf("\tcipher  = (low to high bytes)\n\t\t");
Packit Bot 805b76
        for (j = 0; j<=7; j++) {
Packit Bot 805b76
            printf("%02x ",cipher_text[j]);
Packit Bot 805b76
        }
Packit Bot 805b76
        printf("\n");
Packit Bot 805b76
        do_decrypt(output,cipher_text);
Packit Bot 805b76
        if ( memcmp((char *)cipher_text, (char *)mresult, 8) ) {
Packit Bot 805b76
            printf("verify: error in msb test\n");
Packit Bot 805b76
            exit(-1);
Packit Bot 805b76
        }
Packit Bot 805b76
        exit(0);
Packit Bot 805b76
    }
Packit Bot 805b76
Packit Bot 805b76
    /* ECB mode Davies and Price */
Packit Bot 805b76
    {
Packit Bot 805b76
        input = zero_text;
Packit Bot 805b76
        mit_des_key_sched(key2, sched);
Packit Bot 805b76
        printf("Examples per FIPS publication 81, keys ivs and cipher\n");
Packit Bot 805b76
        printf("in hex.  These are the correct answers, see below for\n");
Packit Bot 805b76
        printf("the actual answers.\n\n");
Packit Bot 805b76
        printf("Examples per Davies and Price.\n\n");
Packit Bot 805b76
        printf("EXAMPLE ECB\tkey = 08192a3b4c5d6e7f\n");
Packit Bot 805b76
        printf("\tclear = 0\n");
Packit Bot 805b76
        printf("\tcipher = 25 dd ac 3e 96 17 64 67\n");
Packit Bot 805b76
        printf("ACTUAL ECB\n");
Packit Bot 805b76
        printf("\tclear \"%s\"\n", input);
Packit Bot 805b76
        do_encrypt(input,cipher_text);
Packit Bot 805b76
        printf("\tcipher  = (low to high bytes)\n\t\t");
Packit Bot 805b76
        for (j = 0; j<=7; j++)
Packit Bot 805b76
            printf("%02x ",cipher_text[j]);
Packit Bot 805b76
        printf("\n\n");
Packit Bot 805b76
        do_decrypt(output,cipher_text);
Packit Bot 805b76
        if ( memcmp((char *)cipher_text, (char *)cipher1, 8) ) {
Packit Bot 805b76
            printf("verify: error in ECB encryption\n");
Packit Bot 805b76
            exit(-1);
Packit Bot 805b76
        }
Packit Bot 805b76
        else
Packit Bot 805b76
            printf("verify: ECB encryption is correct\n\n");
Packit Bot 805b76
    }
Packit Bot 805b76
Packit Bot 805b76
    /* ECB mode */
Packit Bot 805b76
    {
Packit Bot 805b76
        mit_des_key_sched(default_key, sched);
Packit Bot 805b76
        input = clear_text;
Packit Bot 805b76
        ivec = default_ivec;
Packit Bot 805b76
        printf("EXAMPLE ECB\tkey = 0123456789abcdef\n");
Packit Bot 805b76
        printf("\tclear = \"Now is the time for all \"\n");
Packit Bot 805b76
        printf("\tcipher = 3f a4 0e 8a 98 4d 48 15 ...\n");
Packit Bot 805b76
        printf("ACTUAL ECB\n\tclear \"%s\"",input);
Packit Bot 805b76
        do_encrypt(input,cipher_text);
Packit Bot 805b76
        printf("\n\tcipher      = (low to high bytes)\n\t\t");
Packit Bot 805b76
        for (j = 0; j<=7; j++) {
Packit Bot 805b76
            printf("%02x ",cipher_text[j]);
Packit Bot 805b76
        }
Packit Bot 805b76
        printf("\n\n");
Packit Bot 805b76
        do_decrypt(output,cipher_text);
Packit Bot 805b76
        if ( memcmp((char *)cipher_text, (char *)cipher2, 8) ) {
Packit Bot 805b76
            printf("verify: error in ECB encryption\n");
Packit Bot 805b76
            exit(-1);
Packit Bot 805b76
        }
Packit Bot 805b76
        else
Packit Bot 805b76
            printf("verify: ECB encryption is correct\n\n");
Packit Bot 805b76
    }
Packit Bot 805b76
Packit Bot 805b76
    /* CBC mode */
Packit Bot 805b76
    printf("EXAMPLE CBC\tkey = 0123456789abcdef");
Packit Bot 805b76
    printf("\tiv = 1234567890abcdef\n");
Packit Bot 805b76
    printf("\tclear = \"Now is the time for all \"\n");
Packit Bot 805b76
    printf("\tcipher =\te5 c7 cd de 87 2b f2 7c\n");
Packit Bot 805b76
    printf("\t\t\t43 e9 34 00 8c 38 9c 0f\n");
Packit Bot 805b76
    printf("\t\t\t68 37 88 49 9a 7c 05 f6\n");
Packit Bot 805b76
Packit Bot 805b76
    printf("ACTUAL CBC\n\tclear \"%s\"\n",input);
Packit Bot 805b76
    in_length =  strlen((char *)input);
Packit Bot 805b76
    if ((retval = mit_des_cbc_encrypt((const mit_des_cblock *) input,
Packit Bot 805b76
                                      (mit_des_cblock *) cipher_text,
Packit Bot 805b76
                                      (size_t) in_length,
Packit Bot 805b76
                                      sched,
Packit Bot 805b76
                                      ivec,
Packit Bot 805b76
                                      MIT_DES_ENCRYPT))) {
Packit Bot 805b76
        com_err("des verify", retval, "can't encrypt");
Packit Bot 805b76
        exit(-1);
Packit Bot 805b76
    }
Packit Bot 805b76
    printf("\tciphertext = (low to high bytes)\n");
Packit Bot 805b76
    for (i = 0; i <= 2; i++) {
Packit Bot 805b76
        printf("\t\t");
Packit Bot 805b76
        for (j = 0; j <= 7; j++) {
Packit Bot 805b76
            printf("%02x ",cipher_text[i*8+j]);
Packit Bot 805b76
        }
Packit Bot 805b76
        printf("\n");
Packit Bot 805b76
    }
Packit Bot 805b76
    if ((retval = mit_des_cbc_encrypt((const mit_des_cblock *) cipher_text,
Packit Bot 805b76
                                      (mit_des_cblock *) clear_text,
Packit Bot 805b76
                                      (size_t) in_length,
Packit Bot 805b76
                                      sched,
Packit Bot 805b76
                                      ivec,
Packit Bot 805b76
                                      MIT_DES_DECRYPT))) {
Packit Bot 805b76
        com_err("des verify", retval, "can't decrypt");
Packit Bot 805b76
        exit(-1);
Packit Bot 805b76
    }
Packit Bot 805b76
    printf("\tdecrypted clear_text = \"%s\"\n",clear_text);
Packit Bot 805b76
Packit Bot 805b76
    if ( memcmp((char *)cipher_text, (char *)cipher3, in_length) ) {
Packit Bot 805b76
        printf("verify: error in CBC encryption\n");
Packit Bot 805b76
        exit(-1);
Packit Bot 805b76
    }
Packit Bot 805b76
    else
Packit Bot 805b76
        printf("verify: CBC encryption is correct\n\n");
Packit Bot 805b76
Packit Bot 805b76
    printf("EXAMPLE CBC checksum");
Packit Bot 805b76
    printf("\tkey =  0123456789abcdef\tiv =  1234567890abcdef\n");
Packit Bot 805b76
    printf("\tclear =\t\t\"7654321 Now is the time for \"\n");
Packit Bot 805b76
    printf("\tchecksum\t58 d2 e7 7e 86 06 27 33, ");
Packit Bot 805b76
    printf("or some part thereof\n");
Packit Bot 805b76
    input = clear_text2;
Packit Bot 805b76
    mit_des_cbc_cksum(input,cipher_text, strlen((char *)input),
Packit Bot 805b76
                      sched,ivec);
Packit Bot 805b76
    printf("ACTUAL CBC checksum\n");
Packit Bot 805b76
    printf("\t\tencrypted cksum = (low to high bytes)\n\t\t");
Packit Bot 805b76
    for (j = 0; j<=7; j++)
Packit Bot 805b76
        printf("%02x ",cipher_text[j]);
Packit Bot 805b76
    printf("\n\n");
Packit Bot 805b76
    if ( memcmp((char *)cipher_text, (char *)checksum, 8) ) {
Packit Bot 805b76
        printf("verify: error in CBC cheksum\n");
Packit Bot 805b76
        exit(-1);
Packit Bot 805b76
    }
Packit Bot 805b76
    else
Packit Bot 805b76
        printf("verify: CBC checksum is correct\n\n");
Packit Bot 805b76
Packit Bot 805b76
    exit(0);
Packit Bot 805b76
}
Packit Bot 805b76
Packit Bot 805b76
static void
Packit Bot 805b76
do_encrypt(in,out)
Packit Bot 805b76
    unsigned char *in;
Packit Bot 805b76
    unsigned char *out;
Packit Bot 805b76
{
Packit Bot 805b76
    int i, j;
Packit Bot 805b76
    for (i =1; i<=nflag; i++) {
Packit Bot 805b76
        mit_des_cbc_encrypt((const mit_des_cblock *)in,
Packit Bot 805b76
                            (mit_des_cblock *)out,
Packit Bot 805b76
                            8,
Packit Bot 805b76
                            sched,
Packit Bot 805b76
                            zero_text,
Packit Bot 805b76
                            MIT_DES_ENCRYPT);
Packit Bot 805b76
        if (mit_des_debug) {
Packit Bot 805b76
            printf("\nclear %s\n",in);
Packit Bot 805b76
            for (j = 0; j<=7; j++)
Packit Bot 805b76
                printf("%02X ",in[j] & 0xff);
Packit Bot 805b76
            printf("\tcipher ");
Packit Bot 805b76
            for (j = 0; j<=7; j++)
Packit Bot 805b76
                printf("%02X ",out[j] & 0xff);
Packit Bot 805b76
        }
Packit Bot 805b76
    }
Packit Bot 805b76
}
Packit Bot 805b76
Packit Bot 805b76
static void
Packit Bot 805b76
do_decrypt(in,out)
Packit Bot 805b76
    unsigned char *out;
Packit Bot 805b76
    unsigned char *in;
Packit Bot 805b76
    /* try to invert it */
Packit Bot 805b76
{
Packit Bot 805b76
    int i, j;
Packit Bot 805b76
    for (i =1; i<=nflag; i++) {
Packit Bot 805b76
        mit_des_cbc_encrypt((const mit_des_cblock *)out,
Packit Bot 805b76
                            (mit_des_cblock *)in,
Packit Bot 805b76
                            8,
Packit Bot 805b76
                            sched,
Packit Bot 805b76
                            zero_text,
Packit Bot 805b76
                            MIT_DES_DECRYPT);
Packit Bot 805b76
        if (mit_des_debug) {
Packit Bot 805b76
            printf("clear %s\n",in);
Packit Bot 805b76
            for (j = 0; j<=7; j++)
Packit Bot 805b76
                printf("%02X ",in[j] & 0xff);
Packit Bot 805b76
            printf("\tcipher ");
Packit Bot 805b76
            for (j = 0; j<=7; j++)
Packit Bot 805b76
                printf("%02X ",out[j] & 0xff);
Packit Bot 805b76
        }
Packit Bot 805b76
    }
Packit Bot 805b76
}
Packit Bot 805b76
Packit Bot 805b76
/*
Packit Bot 805b76
 * Fake out the DES library, for the purposes of testing.
Packit Bot 805b76
 */
Packit Bot 805b76
Packit Bot 805b76
int
Packit Bot 805b76
mit_des_is_weak_key(key)
Packit Bot 805b76
    mit_des_cblock key;
Packit Bot 805b76
{
Packit Bot 805b76
    return 0;                           /* fake it out for testing */
Packit Bot 805b76
}