summaryrefslogtreecommitdiffstats
path: root/src/lib/crypto/crypto_tests/t_kperf.c
blob: 68956bfaea092bae07c57818a79dc5ef1bef97ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* lib/crypto/crypto_tests/t_kperf.c */
/*
 * Copyright (C) 2009 by the Massachusetts Institute of Technology.
 * All rights reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

/*
 * This file contains a harness to measure the performance improvement
 * of using the the krb5_k functions (which cache derived keys) over
 * the equivalent krb5_c functions which do not.  Sample usages:
 *
 *     ./t_kperf ce aes128-cts 10 100000
 *     ./t_kperf kv aes256-cts 1024 10000
 *
 * The first usage encrypts ('e') a hundred thousand ten-byte blobs
 * with aes128-cts, using the non-caching APIs ('c').  The second
 * usage verifies ('v') ten thousand checksums over 1K blobs with the
 * first available keyed checksum type for aes256-cts, using the
 * caching APIs ('k').  Run commands under "time" to measure how much
 * time is used by the operations.
 */

#include "k5-int.h"

int
main(int argc, char **argv)
{
    krb5_keyblock kblock;
    krb5_key key;
    krb5_enctype enctype;
    krb5_cksumtype cktype;
    int blocksize, num_blocks, intf, op, i;
    size_t outlen, cklen;
    krb5_data block;
    krb5_enc_data outblock;
    krb5_checksum sum;
    krb5_boolean val;

    if (argc != 5) {
        fprintf(stderr, "Usage: t_kperf {c|k}{e|d|m|v} type size nblocks\n");
        exit(1);
    }
    intf = argv[1][0];
    assert(intf == 'c' || intf =='k');
    op = argv[1][1];
    assert(krb5_string_to_enctype(argv[2], &enctype) == 0);
    blocksize = atoi(argv[3]);
    num_blocks = atoi(argv[4]);

    block.data = "notrandom";
    block.length = 9;
    krb5_c_random_seed(NULL, &block);

    krb5_c_make_random_key(NULL, enctype, &kblock);
    krb5_k_create_key(NULL, &kblock, &key);

    block.length = blocksize;
    block.data = calloc(1, blocksize);

    krb5_c_encrypt_length(NULL, enctype, blocksize, &outlen);
    outblock.enctype = enctype;
    outblock.ciphertext.length = outlen;
    outblock.ciphertext.data = calloc(1, outlen);

    krb5int_c_mandatory_cksumtype(NULL, enctype, &cktype);
    krb5_c_checksum_length(NULL, cktype, &cklen);
    sum.checksum_type = cktype;
    sum.length = cklen;
    sum.contents = calloc(1, cklen);

    /*
     * Decrypting typically involves copying the output after checking the
     * hash, so we need to create a valid ciphertext to correctly measure its
     * performance.
     */
    if (op == 'd')
        krb5_c_encrypt(NULL, &kblock, 0, NULL, &block, &outblock);

    for (i = 0; i < num_blocks; i++) {
        if (intf == 'c') {
            if (op == 'e')
                krb5_c_encrypt(NULL, &kblock, 0, NULL, &block, &outblock);
            else if (op == 'd')
                krb5_c_decrypt(NULL, &kblock, 0, NULL, &outblock, &block);
            else if (op == 'm')
                krb5_c_make_checksum(NULL, cktype, &kblock, 0, &block, &sum);
            else if (op == 'v')
                krb5_c_verify_checksum(NULL, &kblock, 0, &block, &sum, &val);
        } else {
            if (op == 'e')
                krb5_k_encrypt(NULL, key, 0, NULL, &block, &outblock);
            else if (op == 'd')
                krb5_k_decrypt(NULL, key, 0, NULL, &outblock, &block);
            else if (op == 'm')
                krb5_k_make_checksum(NULL, cktype, key, 0, &block, &sum);
            else if (op == 'v')
                krb5_k_verify_checksum(NULL, key, 0, &block, &sum, &val);
        }
    }
    return 0;
}