Path: blob/main/crypto/krb5/src/plugins/preauth/pkinit/pkinit_kdf_test.c
34923 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* plugins/preauth/pkinit/pkinit_kdf_test.c */2/*3* Copyright (C) 2011 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526/* Verify the correctness of pkinit_kdf() in pkinit_crypto_openssl, which27* implements the key derivation function from RFC 8636. */2829#include "k5-platform.h"30#include "pkinit.h"3132/**33* Initialize a krb5_data from @a s, a constant string. Note @a s is evaluated34* multiple times; this is acceptable for constants.35*/36#define DATA_FROM_STRING(s) \37{0, sizeof(s)-1, (char *) s}383940/* values from test vectors in the pkinit-alg-agility draft */41int secret_len = 256;42char twenty_as[10];43char eighteen_bs[9];44char party_u_name[] = "[email protected]";45char party_v_name[] = "krbtgt/[email protected]";46int enctype_aes = ENCTYPE_AES256_CTS_HMAC_SHA1_96;47int enctype_des3 = ENCTYPE_DES3_CBC_SHA1;48const krb5_data lha_data = DATA_FROM_STRING("lha");4950krb5_octet key1_hex[] =51{0xe6, 0xAB, 0x38, 0xC9, 0x41, 0x3E, 0x03, 0x5B,520xB0, 0x79, 0x20, 0x1E, 0xD0, 0xB6, 0xB7, 0x3D,530x8D, 0x49, 0xA8, 0x14, 0xA7, 0x37, 0xC0, 0x4E,540xE6, 0x64, 0x96, 0x14, 0x20, 0x6F, 0x73, 0xAD};5556krb5_octet key2_hex[] =57{0x77, 0xEF, 0x4E, 0x48, 0xC4, 0x20, 0xAE, 0x3F,580xEC, 0x75, 0x10, 0x9D, 0x79, 0x81, 0x69, 0x7E,590xED, 0x5D, 0x29, 0x5C, 0x90, 0xc6, 0x25, 0x64,600xF7, 0xBF, 0xD1, 0x01, 0xFA, 0x9b, 0xC1, 0xD5};6162krb5_octet key3_hex[] =63{0xD3, 0xC7, 0x8A, 0x79, 0xD6, 0x52, 0x13, 0xEF,640xE9, 0xA8, 0x26, 0xF7, 0x5D, 0xFB, 0x01, 0xF7,650x23, 0x62, 0xFB, 0x16, 0xFB, 0x01, 0xDA, 0xD6};6667int68main(int argc, char **argv)69{70krb5_context context = 0;71krb5_data secret;72krb5_algorithm_identifier alg_id;73krb5_data as_req;74krb5_data pk_as_rep;75krb5_keyblock key_block;7677/* other local variables */78int retval = 0;79krb5_enctype enctype = 0;80krb5_principal u_principal = NULL;81krb5_principal v_principal = NULL;8283/* initialize variables that get malloc'ed, so cleanup is safe */84krb5_init_context (&context);85memset(&alg_id, 0, sizeof(alg_id));86memset(&as_req, 0, sizeof(as_req));87memset(&pk_as_rep, 0, sizeof(pk_as_rep));88memset(&key_block, 0, sizeof(key_block));8990/* set up a 256-byte, ALL-ZEROS secret */91if (NULL == (secret.data = malloc(secret_len))) {92printf("ERROR in pkinit_kdf_test: Memory allocation failed.");93retval = ENOMEM;94goto cleanup;95}96secret.length = secret_len;97memset(secret.data, 0, secret_len);9899/* set-up the partyUInfo and partyVInfo principals */100if ((0 != (retval = krb5_parse_name(context, party_u_name,101&u_principal))) ||102(0 != (retval = krb5_parse_name(context, party_v_name,103&v_principal)))) {104printf("ERROR in pkinit_kdf_test: Error parsing names, retval = %d",105retval);106goto cleanup;107}108109/* The test vectors in RFC 8636 implicitly use NT-PRINCIPAL names. */110u_principal->type = KRB5_NT_PRINCIPAL;111v_principal->type = KRB5_NT_PRINCIPAL;112113/* set-up the as_req and and pk_as_rep data */114memset(twenty_as, 0xaa, sizeof(twenty_as));115memset(eighteen_bs, 0xbb, sizeof(eighteen_bs));116as_req.length = sizeof(twenty_as);117as_req.data = twenty_as;118119pk_as_rep.length = sizeof(eighteen_bs);120pk_as_rep.data = eighteen_bs;121122/* TEST 1: SHA-1/AES */123/* set up algorithm id */124alg_id.algorithm = kdf_sha1_id;125126enctype = enctype_aes;127128retval = pkinit_kdf(context, &secret, &alg_id.algorithm, u_principal,129v_principal, enctype, &as_req, &pk_as_rep, &key_block);130if (retval) {131printf("ERROR in pkinit_kdf_test: kdf call failed, retval = %d\n",132retval);133goto cleanup;134}135136/* compare key to expected key value */137138if ((key_block.length == sizeof(key1_hex)) &&139(0 == memcmp(key_block.contents, key1_hex, key_block.length))) {140printf("SUCCESS: TEST 1 (SHA-1/AES), Correct key value generated.\n");141retval = 0;142/* free the keyblock contents, so we can use it for the next test */143krb5_free_keyblock_contents(context, &key_block);144} else {145printf("FAILURE: TEST 1 (SHA-1/AES), Incorrect key value generated!\n");146retval = 1;147goto cleanup;148}149150/* TEST 2: SHA-256/AES */151/* set up algorithm id */152alg_id.algorithm = kdf_sha256_id;153154enctype = enctype_aes;155156retval = pkinit_kdf(context, &secret, &alg_id.algorithm, u_principal,157v_principal, enctype, &as_req, &pk_as_rep, &key_block);158if (retval) {159printf("ERROR in pkinit_kdf_test: kdf call failed, retval = %d\n",160retval);161goto cleanup;162}163164/* compare key to expected key value */165166if ((key_block.length == sizeof(key2_hex)) &&167(0 == memcmp(key_block.contents, key2_hex, key_block.length))) {168printf("SUCCESS: TEST 2 (SHA-256/AES), Correct key value generated.\n");169retval = 0;170/* free the keyblock contents, so we can use it for the next test */171krb5_free_keyblock_contents(context, &key_block);172} else {173printf("FAILURE: TEST 2 (SHA-256/AES), Incorrect key value generated!\n");174retval = 1;175goto cleanup;176}177178/* TEST 3: SHA-512/DES3 */179/* set up algorithm id */180alg_id.algorithm = kdf_sha512_id;181182enctype = enctype_des3;183184retval = pkinit_kdf(context, &secret, &alg_id.algorithm, u_principal,185v_principal, enctype, &as_req, &pk_as_rep, &key_block);186if (retval) {187printf("ERROR in pkinit_kdf_test: kdf call failed, retval = %d\n",188retval);189goto cleanup;190}191192/* compare key to expected key value */193194if ((key_block.length == sizeof(key3_hex)) &&195(0 == memcmp(key_block.contents, key3_hex, key_block.length))) {196printf("SUCCESS: TEST 3 (SHA-512/DES3), Correct key value generated.\n");197retval = 0;198} else {199printf("FAILURE: TEST 2 (SHA-512/DES3), Incorrect key value generated!\n");200retval = 1;201goto cleanup;202}203204cleanup:205/* release all allocated resources, whether good or bad return */206free(secret.data);207krb5_free_principal(context, u_principal);208krb5_free_principal(context, v_principal);209krb5_free_keyblock_contents(context, &key_block);210krb5_free_context(context);211return retval ? 1 : 0;212}213214215