Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / lib / crypto / crypto_tests / t_prng.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/crypto_tests/t_prng.c */
3 /*
4  * Copyright (C) 2001 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26
27 /*
28  * This file contains tests for the PRNG code in Kerberos.  It reads
29  * an input file, and writes an output file.  It is assumed that the
30  * output file will be diffed against expected output to see whether
31  * regression tests pass.  The input file has a very primitive format.
32  * It is composed of alternating seeds and outputs.  The first line in
33  * the file is an integer source id from the krb5_c_randsource enum in
34  * krb5.h.  Then an integer seed length is read.  Then that many bytes
35  * (encoded in hex) are read; whitespace or newlines may be inserted
36  * between bytes.  Then after the seed data is an integer describing
37  * how many bytes of output should be written.  Then another source ID
38  * and seed length is read.  If the seed length is 0, the source id is
39  * ignored and the seed is not seeded.
40  */
41
42 #include "k5-int.h"
43 #include <assert.h>
44
45 int main () {
46     krb5_error_code ret;
47     krb5_data input, output;
48     unsigned int source_id, seed_length;
49     unsigned int i;
50     while (1) {
51         /* Read source*/
52         if (scanf ("%u", &source_id ) == EOF )
53             break;
54         /* Read seed length*/
55         if (scanf ("%u", &seed_length) == EOF)
56             break;
57         if (seed_length ) {
58             unsigned int lc;
59             ret = alloc_data(&input, seed_length);
60             assert(!ret);
61             for (lc = seed_length; lc > 0; lc--) {
62                 scanf ("%2x",  &i);
63                 input.data[seed_length-lc] = (unsigned) (i&0xff);
64             }
65             ret = krb5_c_random_add_entropy (0, source_id, &input);
66             assert(!ret);
67             free (input.data);
68             input.data = NULL;
69         }
70         if (scanf ("%u", &i) == EOF)
71             break;
72         if (i) {
73             ret = alloc_data(&output, i);
74             assert(!ret);
75             ret = krb5_c_random_make_octets (0, &output);
76             if (ret)
77                 printf ("failed\n");
78             else {
79                 for (; i > 0; i--) {
80                     printf ("%02x",
81                             (unsigned int) ((unsigned char ) output.data[output.length-i]));
82                 }
83                 printf ("\n");
84             }
85             free (output.data);
86             output.data = NULL;
87         }
88     }
89     return (0);
90 }