Blame src/plugins/preauth/pkinit/pkinit_kdf_test.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* plugins/preauth/pkinit/pkinit_kdf_test.c */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 2011 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
/*
Packit fd8b60
 * pkinit_kdf_test.c -- Test to verify the correctness of the function
Packit fd8b60
 * pkinit_alg_agility_kdf() in pkinit_crypto_openssl, which implements
Packit fd8b60
 * the Key Derivation Function from the PKInit Algorithm Agility
Packit fd8b60
 * document, currently draft-ietf-krb-wg-pkinit-alg-agility-04.txt.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include "k5-platform.h"
Packit fd8b60
#include "pkinit.h"
Packit fd8b60
Packit fd8b60
/**
Packit fd8b60
 * Initialize a krb5_data from @a s, a constant string. Note @a s is evaluated
Packit fd8b60
 * multiple times; this is acceptable for constants.
Packit fd8b60
 */
Packit fd8b60
#define DATA_FROM_STRING(s)                     \
Packit fd8b60
    {0, sizeof(s)-1, (char *) s}
Packit fd8b60
Packit fd8b60
Packit fd8b60
/* values from test vectors in the pkinit-alg-agility draft */
Packit fd8b60
int secret_len = 256;
Packit fd8b60
char twenty_as[10];
Packit fd8b60
char eighteen_bs[9];
Packit fd8b60
char party_u_name[] = "lha@SU.SE";
Packit fd8b60
char party_v_name[] = "krbtgt/SU.SE@SU.SE";
Packit fd8b60
int enctype_aes = ENCTYPE_AES256_CTS_HMAC_SHA1_96;
Packit Service a81408
int enctype_des3 = ENCTYPE_DES3_CBC_SHA1;
Packit fd8b60
const krb5_data lha_data = DATA_FROM_STRING("lha");
Packit fd8b60
Packit fd8b60
krb5_octet key1_hex[] =
Packit fd8b60
{0xe6, 0xAB, 0x38, 0xC9, 0x41, 0x3E, 0x03, 0x5B,
Packit fd8b60
 0xB0, 0x79, 0x20, 0x1E, 0xD0, 0xB6, 0xB7, 0x3D,
Packit fd8b60
 0x8D, 0x49, 0xA8, 0x14, 0xA7, 0x37, 0xC0, 0x4E,
Packit fd8b60
 0xE6, 0x64, 0x96, 0x14, 0x20, 0x6F, 0x73, 0xAD};
Packit fd8b60
Packit fd8b60
krb5_octet key2_hex[] =
Packit fd8b60
{0x77, 0xEF, 0x4E, 0x48, 0xC4, 0x20, 0xAE, 0x3F,
Packit fd8b60
 0xEC, 0x75, 0x10, 0x9D, 0x79, 0x81, 0x69, 0x7E,
Packit fd8b60
 0xED, 0x5D, 0x29, 0x5C, 0x90, 0xc6, 0x25, 0x64,
Packit fd8b60
 0xF7, 0xBF, 0xD1, 0x01, 0xFA, 0x9b, 0xC1, 0xD5};
Packit fd8b60
Packit fd8b60
krb5_octet key3_hex[] =
Packit fd8b60
{0xD3, 0xC7, 0x8A, 0x79, 0xD6, 0x52, 0x13, 0xEF,
Packit fd8b60
 0xE9, 0xA8, 0x26, 0xF7, 0x5D, 0xFB, 0x01, 0xF7,
Packit fd8b60
 0x23, 0x62, 0xFB, 0x16, 0xFB, 0x01, 0xDA, 0xD6};
Packit fd8b60
Packit fd8b60
int
Packit fd8b60
main(int argc, char **argv)
Packit fd8b60
{
Packit fd8b60
    /* arguments for calls to pkinit_alg_agility_kdf() */
Packit fd8b60
    krb5_context context = 0;
Packit fd8b60
    krb5_data secret;
Packit fd8b60
    krb5_algorithm_identifier alg_id;
Packit fd8b60
    krb5_data as_req;
Packit fd8b60
    krb5_data pk_as_rep;
Packit fd8b60
    krb5_keyblock key_block;
Packit fd8b60
Packit fd8b60
    /* other local variables */
Packit fd8b60
    int retval = 0;
Packit fd8b60
    krb5_enctype enctype = 0;
Packit fd8b60
    krb5_principal u_principal = NULL;
Packit fd8b60
    krb5_principal v_principal = NULL;
Packit fd8b60
Packit fd8b60
    /* initialize variables that get malloc'ed, so cleanup is safe */
Packit fd8b60
    krb5_init_context (&context);
Packit fd8b60
    memset(&alg_id, 0, sizeof(alg_id));
Packit fd8b60
    memset(&as_req, 0, sizeof(as_req));
Packit fd8b60
    memset(&pk_as_rep, 0, sizeof(pk_as_rep));
Packit fd8b60
    memset(&key_block, 0, sizeof(key_block));
Packit fd8b60
Packit fd8b60
    /* set up a 256-byte, ALL-ZEROS secret */
Packit fd8b60
    if (NULL == (secret.data = malloc(secret_len))) {
Packit fd8b60
        printf("ERROR in pkinit_kdf_test: Memory allocation failed.");
Packit fd8b60
        retval = ENOMEM;
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
    secret.length = secret_len;
Packit fd8b60
    memset(secret.data, 0, secret_len);
Packit fd8b60
Packit fd8b60
    /* set-up the partyUInfo and partyVInfo principals */
Packit fd8b60
    if ((0 != (retval = krb5_parse_name(context, party_u_name,
Packit fd8b60
                                        &u_principal))) ||
Packit fd8b60
        (0 != (retval = krb5_parse_name(context, party_v_name,
Packit fd8b60
                                        &v_principal)))) {
Packit fd8b60
        printf("ERROR in pkinit_kdf_test: Error parsing names, retval = %d",
Packit fd8b60
               retval);
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* set-up the as_req and and pk_as_rep data */
Packit fd8b60
    memset(twenty_as, 0xaa, sizeof(twenty_as));
Packit fd8b60
    memset(eighteen_bs, 0xbb, sizeof(eighteen_bs));
Packit fd8b60
    as_req.length = sizeof(twenty_as);
Packit fd8b60
    as_req.data = twenty_as;
Packit fd8b60
Packit fd8b60
    pk_as_rep.length = sizeof(eighteen_bs);
Packit fd8b60
    pk_as_rep.data = eighteen_bs;
Packit fd8b60
Packit fd8b60
    /* TEST 1:  SHA-1/AES */
Packit fd8b60
    /* set up algorithm id */
Packit fd8b60
    alg_id.algorithm.data = (char *)krb5_pkinit_sha1_oid;
Packit fd8b60
    alg_id.algorithm.length = krb5_pkinit_sha1_oid_len;
Packit fd8b60
Packit fd8b60
    enctype = enctype_aes;
Packit fd8b60
Packit fd8b60
    /* call pkinit_alg_agility_kdf() with test vector values*/
Packit fd8b60
    if (0 != (retval = pkinit_alg_agility_kdf(context, &secret,
Packit fd8b60
                                              &alg_id.algorithm,
Packit fd8b60
                                              u_principal, v_principal,
Packit fd8b60
                                              enctype, &as_req, &pk_as_rep,
Packit fd8b60
                                              &key_block))) {
Packit fd8b60
        printf("ERROR in pkinit_kdf_test: kdf call failed, retval = %d",
Packit fd8b60
               retval);
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* compare key to expected key value */
Packit fd8b60
Packit fd8b60
    if ((key_block.length == sizeof(key1_hex)) &&
Packit fd8b60
        (0 == memcmp(key_block.contents, key1_hex, key_block.length))) {
Packit fd8b60
        printf("SUCCESS: TEST 1 (SHA-1/AES), Correct key value generated.\n");
Packit fd8b60
        retval = 0;
Packit fd8b60
        /* free the keyblock contents, so we can use it for the next test */
Packit fd8b60
        krb5_free_keyblock_contents(context, &key_block);
Packit fd8b60
    } else {
Packit fd8b60
        printf("FAILURE: TEST 1 (SHA-1/AES), Incorrect key value generated!\n");
Packit fd8b60
        retval = 1;
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* TEST 2: SHA-256/AES */
Packit fd8b60
    /* set up algorithm id */
Packit fd8b60
    alg_id.algorithm.data = (char *)krb5_pkinit_sha256_oid;
Packit fd8b60
    alg_id.algorithm.length = krb5_pkinit_sha256_oid_len;
Packit fd8b60
Packit fd8b60
    enctype = enctype_aes;
Packit fd8b60
Packit fd8b60
    /* call pkinit_alg_agility_kdf() with test vector values*/
Packit fd8b60
    if (0 != (retval = pkinit_alg_agility_kdf(context, &secret,
Packit fd8b60
                                              &alg_id.algorithm,
Packit fd8b60
                                              u_principal, v_principal,
Packit fd8b60
                                              enctype, &as_req, &pk_as_rep,
Packit fd8b60
                                              &key_block))) {
Packit fd8b60
        printf("ERROR in pkinit_kdf_test: kdf call failed, retval = %d",
Packit fd8b60
               retval);
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* compare key to expected key value */
Packit fd8b60
Packit fd8b60
    if ((key_block.length == sizeof(key2_hex)) &&
Packit fd8b60
        (0 == memcmp(key_block.contents, key2_hex, key_block.length))) {
Packit fd8b60
        printf("SUCCESS: TEST 2 (SHA-256/AES), Correct key value generated.\n");
Packit fd8b60
        retval = 0;
Packit fd8b60
        /* free the keyblock contents, so we can use it for the next test */
Packit fd8b60
        krb5_free_keyblock_contents(context, &key_block);
Packit fd8b60
    } else {
Packit fd8b60
        printf("FAILURE: TEST 2 (SHA-256/AES), Incorrect key value generated!\n");
Packit fd8b60
        retval = 1;
Packit fd8b60
        goto cleanup;
Packit fd8b60
    }
Packit fd8b60
Packit Service a81408
    /* TEST 3: SHA-512/DES3 */
Packit Service a81408
    /* set up algorithm id */
Packit Service a81408
    alg_id.algorithm.data = (char *)krb5_pkinit_sha512_oid;
Packit Service a81408
    alg_id.algorithm.length = krb5_pkinit_sha512_oid_len;
Packit Service a81408
Packit Service a81408
    enctype = enctype_des3;
Packit Service a81408
Packit Service a81408
    /* call pkinit_alg_agility_kdf() with test vector values*/
Packit Service a81408
    if (0 != (retval = pkinit_alg_agility_kdf(context, &secret,
Packit Service a81408
                                              &alg_id.algorithm,
Packit Service a81408
                                              u_principal, v_principal,
Packit Service a81408
                                              enctype, &as_req, &pk_as_rep,
Packit Service a81408
                                              &key_block))) {
Packit Service a81408
        printf("ERROR in pkinit_kdf_test: kdf call failed, retval = %d",
Packit Service a81408
               retval);
Packit Service a81408
        goto cleanup;
Packit Service a81408
    }
Packit Service a81408
Packit Service a81408
    /* compare key to expected key value */
Packit Service a81408
Packit Service a81408
    if ((key_block.length == sizeof(key3_hex)) &&
Packit Service a81408
        (0 == memcmp(key_block.contents, key3_hex, key_block.length))) {
Packit Service a81408
        printf("SUCCESS: TEST 3 (SHA-512/DES3), Correct key value generated.\n");
Packit Service a81408
        retval = 0;
Packit Service a81408
    } else {
Packit Service a81408
        printf("FAILURE: TEST 2 (SHA-512/DES3), Incorrect key value generated!\n");
Packit Service a81408
        retval = 1;
Packit Service a81408
        goto cleanup;
Packit Service a81408
    }
Packit Service a81408
Packit fd8b60
cleanup:
Packit fd8b60
    /* release all allocated resources, whether good or bad return */
Packit fd8b60
    free(secret.data);
Packit fd8b60
    krb5_free_principal(context, u_principal);
Packit fd8b60
    krb5_free_principal(context, v_principal);
Packit fd8b60
    krb5_free_keyblock_contents(context, &key_block);
Packit fd8b60
    exit(retval);
Packit fd8b60
}