1*0a6a1f1dSLionel Sambuc /* $NetBSD: test_cfx.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 2006 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of KTH nor the names of its contributors may be
20ebfedea0SLionel Sambuc * used to endorse or promote products derived from this software without
21ebfedea0SLionel Sambuc * specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ebfedea0SLionel Sambuc * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ebfedea0SLionel Sambuc * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ebfedea0SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ebfedea0SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ebfedea0SLionel Sambuc * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ebfedea0SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ebfedea0SLionel Sambuc * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ebfedea0SLionel Sambuc * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "gsskrb5_locl.h"
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc struct range {
39ebfedea0SLionel Sambuc size_t lower;
40ebfedea0SLionel Sambuc size_t upper;
41ebfedea0SLionel Sambuc };
42ebfedea0SLionel Sambuc
43ebfedea0SLionel Sambuc struct range tests[] = {
44ebfedea0SLionel Sambuc { 0, 1040 },
45ebfedea0SLionel Sambuc { 2040, 2080 },
46ebfedea0SLionel Sambuc { 4080, 5000 },
47ebfedea0SLionel Sambuc { 8180, 8292 },
48ebfedea0SLionel Sambuc { 9980, 10010 }
49ebfedea0SLionel Sambuc };
50ebfedea0SLionel Sambuc
51ebfedea0SLionel Sambuc static void
test_range(const struct range * r,int integ,krb5_context context,krb5_crypto crypto)52ebfedea0SLionel Sambuc test_range(const struct range *r, int integ,
53ebfedea0SLionel Sambuc krb5_context context, krb5_crypto crypto)
54ebfedea0SLionel Sambuc {
55ebfedea0SLionel Sambuc krb5_error_code ret;
56ebfedea0SLionel Sambuc size_t size, rsize;
57ebfedea0SLionel Sambuc struct gsskrb5_ctx ctx;
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc for (size = r->lower; size < r->upper; size++) {
60ebfedea0SLionel Sambuc size_t cksumsize;
61ebfedea0SLionel Sambuc uint16_t padsize;
62ebfedea0SLionel Sambuc OM_uint32 minor;
63ebfedea0SLionel Sambuc OM_uint32 max_wrap_size;
64ebfedea0SLionel Sambuc
65ebfedea0SLionel Sambuc ctx.crypto = crypto;
66ebfedea0SLionel Sambuc
67ebfedea0SLionel Sambuc ret = _gssapi_wrap_size_cfx(&minor,
68ebfedea0SLionel Sambuc &ctx,
69ebfedea0SLionel Sambuc context,
70ebfedea0SLionel Sambuc integ,
71ebfedea0SLionel Sambuc 0,
72ebfedea0SLionel Sambuc size,
73ebfedea0SLionel Sambuc &max_wrap_size);
74ebfedea0SLionel Sambuc if (ret)
75ebfedea0SLionel Sambuc krb5_errx(context, 1, "_gsskrb5cfx_max_wrap_length_cfx: %d", ret);
76ebfedea0SLionel Sambuc if (max_wrap_size == 0)
77ebfedea0SLionel Sambuc continue;
78ebfedea0SLionel Sambuc
79ebfedea0SLionel Sambuc ret = _gsskrb5cfx_wrap_length_cfx(context,
80ebfedea0SLionel Sambuc crypto,
81ebfedea0SLionel Sambuc integ,
82ebfedea0SLionel Sambuc 0,
83ebfedea0SLionel Sambuc max_wrap_size,
84ebfedea0SLionel Sambuc &rsize, &cksumsize, &padsize);
85ebfedea0SLionel Sambuc if (ret)
86ebfedea0SLionel Sambuc krb5_errx(context, 1, "_gsskrb5cfx_wrap_length_cfx: %d", ret);
87ebfedea0SLionel Sambuc
88ebfedea0SLionel Sambuc if (size < rsize)
89ebfedea0SLionel Sambuc krb5_errx(context, 1,
90ebfedea0SLionel Sambuc "size (%d) < rsize (%d) for max_wrap_size %d",
91ebfedea0SLionel Sambuc (int)size, (int)rsize, (int)max_wrap_size);
92ebfedea0SLionel Sambuc }
93ebfedea0SLionel Sambuc }
94ebfedea0SLionel Sambuc
95ebfedea0SLionel Sambuc static void
test_special(krb5_context context,krb5_crypto crypto,int integ,size_t testsize)96ebfedea0SLionel Sambuc test_special(krb5_context context, krb5_crypto crypto,
97ebfedea0SLionel Sambuc int integ, size_t testsize)
98ebfedea0SLionel Sambuc {
99ebfedea0SLionel Sambuc krb5_error_code ret;
100ebfedea0SLionel Sambuc size_t rsize;
101ebfedea0SLionel Sambuc OM_uint32 max_wrap_size;
102ebfedea0SLionel Sambuc size_t cksumsize;
103ebfedea0SLionel Sambuc uint16_t padsize;
104ebfedea0SLionel Sambuc struct gsskrb5_ctx ctx;
105ebfedea0SLionel Sambuc OM_uint32 minor;
106ebfedea0SLionel Sambuc
107ebfedea0SLionel Sambuc ctx.crypto = crypto;
108ebfedea0SLionel Sambuc
109ebfedea0SLionel Sambuc ret = _gssapi_wrap_size_cfx(&minor,
110ebfedea0SLionel Sambuc &ctx,
111ebfedea0SLionel Sambuc context,
112ebfedea0SLionel Sambuc integ,
113ebfedea0SLionel Sambuc 0,
114ebfedea0SLionel Sambuc testsize,
115ebfedea0SLionel Sambuc &max_wrap_size);
116ebfedea0SLionel Sambuc if (ret)
117ebfedea0SLionel Sambuc krb5_errx(context, 1, "_gsskrb5cfx_max_wrap_length_cfx: %d", ret);
118ebfedea0SLionel Sambuc if (ret)
119ebfedea0SLionel Sambuc krb5_errx(context, 1, "_gsskrb5cfx_max_wrap_length_cfx: %d", ret);
120ebfedea0SLionel Sambuc
121ebfedea0SLionel Sambuc ret = _gsskrb5cfx_wrap_length_cfx(context,
122ebfedea0SLionel Sambuc crypto,
123ebfedea0SLionel Sambuc integ,
124ebfedea0SLionel Sambuc 0,
125ebfedea0SLionel Sambuc max_wrap_size,
126ebfedea0SLionel Sambuc &rsize, &cksumsize, &padsize);
127ebfedea0SLionel Sambuc if (ret)
128ebfedea0SLionel Sambuc krb5_errx(context, 1, "_gsskrb5cfx_wrap_length_cfx: %d", ret);
129ebfedea0SLionel Sambuc
130ebfedea0SLionel Sambuc if (testsize < rsize)
131ebfedea0SLionel Sambuc krb5_errx(context, 1,
132ebfedea0SLionel Sambuc "testsize (%d) < rsize (%d) for max_wrap_size %d",
133ebfedea0SLionel Sambuc (int)testsize, (int)rsize, (int)max_wrap_size);
134ebfedea0SLionel Sambuc }
135ebfedea0SLionel Sambuc
136ebfedea0SLionel Sambuc
137ebfedea0SLionel Sambuc
138ebfedea0SLionel Sambuc
139ebfedea0SLionel Sambuc int
main(int argc,char ** argv)140ebfedea0SLionel Sambuc main(int argc, char **argv)
141ebfedea0SLionel Sambuc {
142ebfedea0SLionel Sambuc krb5_keyblock keyblock;
143ebfedea0SLionel Sambuc krb5_error_code ret;
144ebfedea0SLionel Sambuc krb5_context context;
145ebfedea0SLionel Sambuc krb5_crypto crypto;
146ebfedea0SLionel Sambuc int i;
147ebfedea0SLionel Sambuc
148ebfedea0SLionel Sambuc ret = krb5_init_context(&context);
149ebfedea0SLionel Sambuc if (ret)
150ebfedea0SLionel Sambuc errx(1, "krb5_context_init: %d", ret);
151ebfedea0SLionel Sambuc
152ebfedea0SLionel Sambuc ret = krb5_generate_random_keyblock(context,
153ebfedea0SLionel Sambuc ENCTYPE_AES256_CTS_HMAC_SHA1_96,
154ebfedea0SLionel Sambuc &keyblock);
155ebfedea0SLionel Sambuc if (ret)
156ebfedea0SLionel Sambuc krb5_err(context, 1, ret, "krb5_generate_random_keyblock");
157ebfedea0SLionel Sambuc
158ebfedea0SLionel Sambuc ret = krb5_crypto_init(context, &keyblock, 0, &crypto);
159ebfedea0SLionel Sambuc if (ret)
160ebfedea0SLionel Sambuc krb5_err(context, 1, ret, "krb5_crypto_init");
161ebfedea0SLionel Sambuc
162ebfedea0SLionel Sambuc test_special(context, crypto, 1, 60);
163ebfedea0SLionel Sambuc test_special(context, crypto, 0, 60);
164ebfedea0SLionel Sambuc
165ebfedea0SLionel Sambuc for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
166ebfedea0SLionel Sambuc test_range(&tests[i], 1, context, crypto);
167ebfedea0SLionel Sambuc test_range(&tests[i], 0, context, crypto);
168ebfedea0SLionel Sambuc }
169ebfedea0SLionel Sambuc
170ebfedea0SLionel Sambuc krb5_free_keyblock_contents(context, &keyblock);
171ebfedea0SLionel Sambuc krb5_crypto_destroy(context, crypto);
172ebfedea0SLionel Sambuc krb5_free_context(context);
173ebfedea0SLionel Sambuc
174ebfedea0SLionel Sambuc return 0;
175ebfedea0SLionel Sambuc }
176