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
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__
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
};
33 printbuf(uint8_t *buf
, char *name
, size_t size
)
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
);
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
);
54 (void) fprintf(stderr
, "result matches\n\n");
64 run_test(cryptotest_t
*args
, uint8_t *cmp
, size_t cmplen
,
70 (void) fprintf(stderr
, "%s: run %d\n", args
->mechname
, ++i
);
71 bzero(args
->out
, args
->outlen
);
72 ret
= funcs
->update(args
);
74 (void) fprintf(stderr
, "failure %x\n", ret
);
77 (void) fprintf(stderr
, "fatal error %d\n", ret
);
80 errs
+= bufcmp(cmp
, args
->out
, cmplen
);
82 bzero(args
->out
, args
->outlen
);
83 ret
= funcs
->single(args
);
85 (void) fprintf(stderr
, "failure %x\n", ret
);
88 (void) fprintf(stderr
, "fatal error %d\n", ret
);
91 errs
+= bufcmp(cmp
, args
->out
, cmplen
);
97 test_mac_common(cryptotest_t
*args
, boolean_t AIO
)
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
)
114 if ((ret
= get_hsession_by_mech(crypto_op
)) != CRYPTO_SUCCESS
)
117 if ((ret
= mac_init(crypto_op
)) != CRYPTO_SUCCESS
)
121 if ((ret
= mac_single(crypto_op
)) != CRYPTO_SUCCESS
)
124 for (i
= 0; i
< args
->inlen
; i
+= args
->updatelen
) {
126 if ((ret
= mac_update(crypto_op
, i
)) != CRYPTO_SUCCESS
)
130 if ((ret
= mac_final(crypto_op
)) != CRYPTO_SUCCESS
)
136 (void) cryptotest_close(crypto_op
);
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
));
153 test_encrypt_common(cryptotest_t
*args
, boolean_t AIO
)
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
)
171 if ((ret
= get_hsession_by_mech(crypto_op
)) != CRYPTO_SUCCESS
)
174 if ((ret
= encrypt_init(crypto_op
)) != CRYPTO_SUCCESS
)
178 if ((ret
= encrypt_single(crypto_op
)) != CRYPTO_SUCCESS
)
181 for (i
= 0; i
< args
->inlen
; i
+= args
->updatelen
) {
183 if ((ret
= encrypt_update(crypto_op
, i
,
184 &encrlen
)) != CRYPTO_SUCCESS
)
188 if ((ret
= encrypt_final(crypto_op
, encrlen
)) != CRYPTO_SUCCESS
)
194 (void) cryptotest_close(crypto_op
);
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
));
212 test_decrypt_common(cryptotest_t
*args
, boolean_t AIO
)
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
)
230 if ((ret
= get_hsession_by_mech(crypto_op
)) != CRYPTO_SUCCESS
)
233 if ((ret
= decrypt_init(crypto_op
)) != CRYPTO_SUCCESS
)
237 if ((ret
= decrypt_single(crypto_op
)) != CRYPTO_SUCCESS
)
240 for (i
= 0; i
< args
->inlen
; i
+= args
->updatelen
) {
242 if ((ret
= decrypt_update(crypto_op
, i
,
243 &encrlen
)) != CRYPTO_SUCCESS
)
247 if ((ret
= decrypt_final(crypto_op
, encrlen
)) != CRYPTO_SUCCESS
)
253 (void) cryptotest_close(crypto_op
);
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
));