Blame src/lib/crypto/crypto_tests/t_nfold.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* lib/crypto/crypto_tests/t_nfold.c - Test nfold implementation correctness */
Packit fd8b60
/*
Packit fd8b60
 * Copyright 1988, 1990 by the Massachusetts Institute of Technology.
Packit fd8b60
 * All Rights Reserved.
Packit fd8b60
 *
Packit fd8b60
 * Export of this software from the United States of America may
Packit fd8b60
 *   require a specific license from the United States Government.
Packit fd8b60
 *   It is the responsibility of any person or organization contemplating
Packit fd8b60
 *   export to obtain such a license before exporting.
Packit fd8b60
 *
Packit fd8b60
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit fd8b60
 * distribute this software and its documentation for any purpose and
Packit fd8b60
 * without fee is hereby granted, provided that the above copyright
Packit fd8b60
 * notice appear in all copies and that both that copyright notice and
Packit fd8b60
 * this permission notice appear in supporting documentation, and that
Packit fd8b60
 * the name of M.I.T. not be used in advertising or publicity pertaining
Packit fd8b60
 * to distribution of the software without specific, written prior
Packit fd8b60
 * permission.  Furthermore if you modify this software you must label
Packit fd8b60
 * your software as modified software and not distribute it in such a
Packit fd8b60
 * fashion that it might be confused with the original M.I.T. software.
Packit fd8b60
 * M.I.T. makes no representations about the suitability of
Packit fd8b60
 * this software for any purpose.  It is provided "as is" without express
Packit fd8b60
 * or implied warranty.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include <stdio.h>
Packit fd8b60
#include <stdlib.h>
Packit fd8b60
#include <string.h>
Packit fd8b60
#include <assert.h>
Packit fd8b60
Packit fd8b60
#include "crypto_int.h"
Packit fd8b60
Packit fd8b60
#define ASIZE(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0]))
Packit fd8b60
Packit fd8b60
static void printhex (size_t len, const unsigned char *p)
Packit fd8b60
{
Packit fd8b60
    while (len--)
Packit fd8b60
        printf ("%02x", 0xff & *p++);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
static void printstringhex (const unsigned char *p) {
Packit fd8b60
    printhex (strlen ((const char *) p), p);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
static void rfc_tests ()
Packit fd8b60
{
Packit fd8b60
    unsigned i;
Packit fd8b60
    struct {
Packit fd8b60
        char *input;
Packit fd8b60
        unsigned int n;
Packit fd8b60
        unsigned char exp[192/8];
Packit fd8b60
    } tests[] = {
Packit fd8b60
        { "012345", 64,
Packit fd8b60
          { 0xbe,0x07,0x26,0x31,0x27,0x6b,0x19,0x55, }
Packit fd8b60
        },
Packit fd8b60
        { "password", 56,
Packit fd8b60
          { 0x78,0xa0,0x7b,0x6c,0xaf,0x85,0xfa, }
Packit fd8b60
        },
Packit fd8b60
        { "Rough Consensus, and Running Code", 64,
Packit fd8b60
          { 0xbb,0x6e,0xd3,0x08,0x70,0xb7,0xf0,0xe0, }
Packit fd8b60
        },
Packit fd8b60
        { "password", 168,
Packit fd8b60
          { 0x59,0xe4,0xa8,0xca,0x7c,0x03,0x85,0xc3,
Packit fd8b60
            0xc3,0x7b,0x3f,0x6d,0x20,0x00,0x24,0x7c,
Packit fd8b60
            0xb6,0xe6,0xbd,0x5b,0x3e, }
Packit fd8b60
        },
Packit fd8b60
        { "MASSACHVSETTS INSTITVTE OF TECHNOLOGY", 192,
Packit fd8b60
          { 0xdb,0x3b,0x0d,0x8f,0x0b,0x06,0x1e,0x60,
Packit fd8b60
            0x32,0x82,0xb3,0x08,0xa5,0x08,0x41,0x22,
Packit fd8b60
            0x9a,0xd7,0x98,0xfa,0xb9,0x54,0x0c,0x1b, }
Packit fd8b60
        },
Packit fd8b60
    };
Packit fd8b60
    unsigned char outbuf[192/8];
Packit fd8b60
Packit fd8b60
    printf ("RFC tests:\n");
Packit fd8b60
    for (i = 0; i < ASIZE (tests); i++) {
Packit fd8b60
        unsigned char *p = (unsigned char *) tests[i].input;
Packit fd8b60
        assert (tests[i].n / 8 <= sizeof (outbuf));
Packit fd8b60
        krb5int_nfold (8 * strlen ((char *) p), p, tests[i].n, outbuf);
Packit fd8b60
        printf ("%d-fold(\"%s\") =\n", tests[i].n, p);
Packit fd8b60
        printf ("%d-fold(", tests[i].n);
Packit fd8b60
        printstringhex (p);
Packit fd8b60
        printf (") =\n\t");
Packit fd8b60
        printhex (tests[i].n / 8, outbuf);
Packit fd8b60
        printf ("\n\n");
Packit fd8b60
        if (memcmp (outbuf, tests[i].exp, tests[i].n/8) != 0) {
Packit fd8b60
            printf ("wrong value! expected:\n\t");
Packit fd8b60
            printhex (tests[i].n / 8, tests[i].exp);
Packit fd8b60
            exit (1);
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
static void fold_kerberos(unsigned int nbytes)
Packit fd8b60
{
Packit fd8b60
    unsigned char cipher_text[300];
Packit fd8b60
    unsigned int j;
Packit fd8b60
Packit fd8b60
    if (nbytes > 300)
Packit fd8b60
        abort();
Packit fd8b60
Packit fd8b60
    printf("%d-fold(\"kerberos\") =\n\t", nbytes*8);
Packit fd8b60
    krb5int_nfold(64, (unsigned char *) "kerberos", 8*nbytes, cipher_text);
Packit fd8b60
    for (j=0; j
Packit fd8b60
        printf("%s%02x", (j&3) ? "" : " ", cipher_text[j]);
Packit fd8b60
    printf("\n");
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
unsigned char *nfold_in[] = {
Packit fd8b60
    (unsigned char *) "basch",
Packit fd8b60
    (unsigned char *) "eichin",
Packit fd8b60
    (unsigned char *) "sommerfeld",
Packit fd8b60
    (unsigned char *) "MASSACHVSETTS INSTITVTE OF TECHNOLOGY" };
Packit fd8b60
Packit fd8b60
unsigned char nfold_192[4][24] = {
Packit fd8b60
    { 0x1a, 0xab, 0x6b, 0x42, 0x96, 0x4b, 0x98, 0xb2, 0x1f, 0x8c, 0xde, 0x2d,
Packit fd8b60
      0x24, 0x48, 0xba, 0x34, 0x55, 0xd7, 0x86, 0x2c, 0x97, 0x31, 0x64, 0x3f },
Packit fd8b60
    { 0x65, 0x69, 0x63, 0x68, 0x69, 0x6e, 0x4b, 0x73, 0x2b, 0x4b, 0x1b, 0x43,
Packit fd8b60
      0xda, 0x1a, 0x5b, 0x99, 0x5a, 0x58, 0xd2, 0xc6, 0xd0, 0xd2, 0xdc, 0xca },
Packit fd8b60
    { 0x2f, 0x7a, 0x98, 0x55, 0x7c, 0x6e, 0xe4, 0xab, 0xad, 0xf4, 0xe7, 0x11,
Packit fd8b60
      0x92, 0xdd, 0x44, 0x2b, 0xd4, 0xff, 0x53, 0x25, 0xa5, 0xde, 0xf7, 0x5c },
Packit fd8b60
    { 0xdb, 0x3b, 0x0d, 0x8f, 0x0b, 0x06, 0x1e, 0x60, 0x32, 0x82, 0xb3, 0x08,
Packit fd8b60
      0xa5, 0x08, 0x41, 0x22, 0x9a, 0xd7, 0x98, 0xfa, 0xb9, 0x54, 0x0c, 0x1b }
Packit fd8b60
};
Packit fd8b60
Packit fd8b60
int
Packit fd8b60
main(argc, argv)
Packit fd8b60
    int argc;
Packit fd8b60
    char *argv[];
Packit fd8b60
{
Packit fd8b60
    unsigned char cipher_text[64];
Packit fd8b60
    unsigned int i, j;
Packit fd8b60
Packit fd8b60
    printf("N-fold\n");
Packit fd8b60
    for (i=0; i
Packit fd8b60
        printf("\tInput:\t\"%.*s\"\n", (int) strlen((char *) nfold_in[i]),
Packit fd8b60
               nfold_in[i]);
Packit fd8b60
        printf("\t192-Fold:\t");
Packit fd8b60
        krb5int_nfold(strlen((char *) nfold_in[i])*8, nfold_in[i], 24*8,
Packit fd8b60
                      cipher_text);
Packit fd8b60
        for (j=0; j<24; j++)
Packit fd8b60
            printf("%s%02x", (j&3) ? "" : " ", cipher_text[j]);
Packit fd8b60
        printf("\n");
Packit fd8b60
        if (memcmp(cipher_text, nfold_192[i], 24)) {
Packit fd8b60
            printf("verify: error in n-fold\n");
Packit fd8b60
            exit(-1);
Packit fd8b60
        };
Packit fd8b60
    }
Packit fd8b60
    rfc_tests ();
Packit fd8b60
Packit fd8b60
    printf("verify: N-fold is correct\n\n");
Packit fd8b60
Packit fd8b60
    fold_kerberos(8);
Packit fd8b60
    fold_kerberos(16);
Packit fd8b60
    fold_kerberos(21);
Packit fd8b60
    fold_kerberos(32);
Packit fd8b60
Packit fd8b60
    exit(0);
Packit fd8b60
}