5869 Need AES CMAC support in KCF+PKCS11
[unleashed.git] / usr / src / test / crypto-tests / tests / common / testfuncs.c
blobf4aacce111a60ac42db2126a0ad2339c4dddb629
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
16 #define __EXTENSIONS__
17 #include <strings.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include "cryptotest.h"
24 test_fg_t cryptotest_decr_fg = {test_decrypt_single, test_decrypt};
25 test_fg_t cryptotest_encr_fg = {test_encrypt_single, test_encrypt};
26 test_fg_t cryptotest_mac_fg = {test_mac_single, test_mac};
29 * Utils
32 void
33 printbuf(uint8_t *buf, char *name, size_t size)
35 size_t i;
37 flockfile(stderr);
38 (void) fprintf(stderr, "%s%s", name, (size > 0) ? " " : "");
39 for (i = 0; i < size; i++)
40 (void) fprintf(stderr, "%02x", buf[i]);
41 (void) fputc('\n', stderr);
42 funlockfile(stderr);
45 int
46 bufcmp(uint8_t *auth, uint8_t *cmp, size_t size)
48 if (memcmp(cmp, auth, size) != 0) {
49 (void) fprintf(stderr, "mismatched result\n\n");
50 printbuf(cmp, "calc", size);
51 printbuf(auth, "orig", size);
52 return (1);
53 } else {
54 (void) fprintf(stderr, "result matches\n\n");
55 return (0);
60 * Wrapper functions
63 int
64 run_test(cryptotest_t *args, uint8_t *cmp, size_t cmplen,
65 test_fg_t *funcs)
67 int ret, errs = 0;
68 static int i = 0;
70 (void) fprintf(stderr, "%s: run %d\n", args->mechname, ++i);
71 bzero(args->out, args->outlen);
72 ret = funcs->update(args);
73 if (ret > 0) {
74 (void) fprintf(stderr, "failure %x\n", ret);
75 errs += 1;
76 } else if (ret < 0) {
77 (void) fprintf(stderr, "fatal error %d\n", ret);
78 exit(1);
79 } else
80 errs += bufcmp(cmp, args->out, cmplen);
82 bzero(args->out, args->outlen);
83 ret = funcs->single(args);
84 if (ret > 0) {
85 (void) fprintf(stderr, "failure %x\n", ret);
86 errs += 1;
87 } else if (ret < 0) {
88 (void) fprintf(stderr, "fatal error %d\n", ret);
89 exit(2);
90 } else
91 errs += bufcmp(cmp, args->out, cmplen);
93 return (errs);
96 static int
97 test_mac_common(cryptotest_t *args, boolean_t AIO)
99 int ret, i;
100 crypto_op_t *crypto_op;
102 if (args->in == NULL || args->key == NULL)
103 return (CRYPTO_FAILED);
105 if ((crypto_op = cryptotest_init(args, CRYPTO_FG_MAC)) == NULL) {
106 (void) fprintf(stderr, "Error occured during initialization\n");
107 (void) cryptotest_close(NULL);
108 return (CTEST_INIT_FAILED);
111 if ((ret = get_mech_info(crypto_op)) != CRYPTO_SUCCESS)
112 goto out;
114 if ((ret = get_hsession_by_mech(crypto_op)) != CRYPTO_SUCCESS)
115 goto out;
117 if ((ret = mac_init(crypto_op)) != CRYPTO_SUCCESS)
118 goto out;
120 if (AIO) {
121 if ((ret = mac_single(crypto_op)) != CRYPTO_SUCCESS)
122 goto out;
123 } else {
124 for (i = 0; i < args->inlen; i += args->updatelen) {
126 if ((ret = mac_update(crypto_op, i)) != CRYPTO_SUCCESS)
127 goto out;
130 if ((ret = mac_final(crypto_op)) != CRYPTO_SUCCESS)
131 goto out;
135 out:
136 (void) cryptotest_close(crypto_op);
137 return (ret);
141 test_mac_single(cryptotest_t *args)
143 return (test_mac_common(args, B_TRUE));
147 test_mac(cryptotest_t *args)
149 return (test_mac_common(args, B_FALSE));
152 static int
153 test_encrypt_common(cryptotest_t *args, boolean_t AIO)
155 int ret, i;
156 size_t encrlen = 0;
157 crypto_op_t *crypto_op;
159 if (args->key == NULL)
160 return (CRYPTO_FAILED);
162 if ((crypto_op = cryptotest_init(args, CRYPTO_FG_ENCRYPT)) == NULL) {
163 (void) fprintf(stderr, "Error occured during initialization\n");
164 (void) cryptotest_close(NULL);
165 return (CTEST_INIT_FAILED);
168 if ((ret = get_mech_info(crypto_op)) != CRYPTO_SUCCESS)
169 goto out;
171 if ((ret = get_hsession_by_mech(crypto_op)) != CRYPTO_SUCCESS)
172 goto out;
174 if ((ret = encrypt_init(crypto_op)) != CRYPTO_SUCCESS)
175 goto out;
177 if (AIO) {
178 if ((ret = encrypt_single(crypto_op)) != CRYPTO_SUCCESS)
179 goto out;
180 } else {
181 for (i = 0; i < args->inlen; i += args->updatelen) {
183 if ((ret = encrypt_update(crypto_op, i,
184 &encrlen)) != CRYPTO_SUCCESS)
185 goto out;
188 if ((ret = encrypt_final(crypto_op, encrlen)) != CRYPTO_SUCCESS)
189 goto out;
193 out:
194 (void) cryptotest_close(crypto_op);
195 return (ret);
199 test_encrypt_single(cryptotest_t *args)
201 return (test_encrypt_common(args, B_TRUE));
206 test_encrypt(cryptotest_t *args)
208 return (test_encrypt_common(args, B_FALSE));
211 static int
212 test_decrypt_common(cryptotest_t *args, boolean_t AIO)
214 int ret, i;
215 size_t encrlen = 0;
216 crypto_op_t *crypto_op;
218 if (args->key == NULL)
219 return (CRYPTO_FAILED);
221 if ((crypto_op = cryptotest_init(args, CRYPTO_FG_DECRYPT)) == NULL) {
222 (void) fprintf(stderr, "Error occured during initialization\n");
223 (void) cryptotest_close(NULL);
224 return (CTEST_INIT_FAILED);
227 if ((ret = get_mech_info(crypto_op)) != CRYPTO_SUCCESS)
228 goto out;
230 if ((ret = get_hsession_by_mech(crypto_op)) != CRYPTO_SUCCESS)
231 goto out;
233 if ((ret = decrypt_init(crypto_op)) != CRYPTO_SUCCESS)
234 goto out;
236 if (AIO) {
237 if ((ret = decrypt_single(crypto_op)) != CRYPTO_SUCCESS)
238 goto out;
239 } else {
240 for (i = 0; i < args->inlen; i += args->updatelen) {
242 if ((ret = decrypt_update(crypto_op, i,
243 &encrlen)) != CRYPTO_SUCCESS)
244 goto out;
247 if ((ret = decrypt_final(crypto_op, encrlen)) != CRYPTO_SUCCESS)
248 goto out;
252 out:
253 (void) cryptotest_close(crypto_op);
254 return (ret);
258 test_decrypt_single(cryptotest_t *args)
260 return (test_decrypt_common(args, B_TRUE));
265 test_decrypt(cryptotest_t *args)
267 return (test_decrypt_common(args, B_FALSE));