xref: /titanic_41/usr/src/cmd/mdb/common/modules/crypto/spi.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * mdb dcmds for selected structures from
31  * usr/src/uts/common/sys/crypto/spi.h
32  *
33  * Also the mdb module housekeeping
34  */
35 
36 #include <sys/mdb_modapi.h>
37 #include <sys/modctl.h>
38 #include <sys/crypto/api.h>
39 #include <sys/crypto/common.h>
40 #include <sys/crypto/spi.h>
41 #include <sys/crypto/impl.h>
42 #include "crypto_cmds.h"
43 
44 
45 const mdb_bitmask_t extf_flags[] = {
46 	{ "NIL", (ulong_t)-1, 0L },
47 	{ "CRYPTO_EXTF_RNG", CRYPTO_EXTF_RNG, CRYPTO_EXTF_RNG },
48 	{ "CRYPTO_EXTF_WRITE_PROTECTED", CRYPTO_EXTF_WRITE_PROTECTED,
49 		CRYPTO_EXTF_WRITE_PROTECTED },
50 	{ "CRYPTO_EXTF_LOGIN_REQUIRED", CRYPTO_EXTF_LOGIN_REQUIRED,
51 		CRYPTO_EXTF_LOGIN_REQUIRED },
52 	{ "CRYPTO_EXTF_USER_PIN_INITIALIZED", CRYPTO_EXTF_USER_PIN_INITIALIZED,
53 		CRYPTO_EXTF_USER_PIN_INITIALIZED },
54 	{ "CRYPTO_EXTF_CLOCK_ON_TOKEN", CRYPTO_EXTF_CLOCK_ON_TOKEN,
55 		CRYPTO_EXTF_CLOCK_ON_TOKEN },
56 	{ "CRYPTO_EXTF_PROTECTED_AUTHENTICATION_PATH",
57 		CRYPTO_EXTF_PROTECTED_AUTHENTICATION_PATH,
58 		CRYPTO_EXTF_PROTECTED_AUTHENTICATION_PATH },
59 	{ "CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS",
60 		CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS,
61 		CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS },
62 	{ "CRYPTO_EXTF_TOKEN_INITIALIZED", CRYPTO_EXTF_TOKEN_INITIALIZED,
63 		CRYPTO_EXTF_TOKEN_INITIALIZED },
64 	{ "CRYPTO_EXTF_USER_PIN_COUNT_LOW", CRYPTO_EXTF_USER_PIN_COUNT_LOW,
65 		CRYPTO_EXTF_USER_PIN_COUNT_LOW },
66 	{ "CRYPTO_EXTF_USER_PIN_FINAL_TRY", CRYPTO_EXTF_USER_PIN_FINAL_TRY,
67 		CRYPTO_EXTF_USER_PIN_FINAL_TRY },
68 	{ "CRYPTO_EXTF_USER_PIN_LOCKED", CRYPTO_EXTF_USER_PIN_LOCKED,
69 		CRYPTO_EXTF_USER_PIN_LOCKED },
70 	{ "CRYPTO_EXTF_USER_PIN_TO_BE_CHANGED",
71 		CRYPTO_EXTF_USER_PIN_TO_BE_CHANGED,
72 		CRYPTO_EXTF_USER_PIN_TO_BE_CHANGED },
73 	{ "CRYPTO_EXTF_SO_PIN_COUNT_LOW", CRYPTO_EXTF_SO_PIN_COUNT_LOW,
74 		CRYPTO_EXTF_SO_PIN_COUNT_LOW },
75 	{ "CRYPTO_EXTF_SO_PIN_FINAL_TRY", CRYPTO_EXTF_SO_PIN_FINAL_TRY,
76 		CRYPTO_EXTF_SO_PIN_FINAL_TRY },
77 	{ "CRYPTO_EXTF_SO_PIN_LOCKED", CRYPTO_EXTF_SO_PIN_LOCKED,
78 		CRYPTO_EXTF_SO_PIN_LOCKED },
79 	{ "CRYPTO_EXTF_SO_PIN_TO_BE_CHANGED", CRYPTO_EXTF_SO_PIN_TO_BE_CHANGED,
80 		CRYPTO_EXTF_SO_PIN_TO_BE_CHANGED },
81 	{ NULL, 0, 0 }
82 };
83 
84 /*ARGSUSED*/
85 int
86 crypto_provider_ext_info(uintptr_t addr, uint_t flags, int argc,
87     const mdb_arg_t *argv)
88 {
89 	crypto_provider_ext_info_t ext_prov;
90 	/*
91 	 * 33 is 1 + MAX(CRYPTO_EXT_SIZE_LABEL, CRYPTO_EXT_SIZE_MANUF,
92 	 *		 CRYPTO_EXT_SIZE_MODEL, CRYPTO_EXT_SIZE_SERIAL)
93 	 */
94 	char scratch[33];
95 
96 	if (!(flags && DCMD_ADDRSPEC))
97 		return (DCMD_USAGE);
98 
99 	if (mdb_vread(&ext_prov, sizeof (crypto_provider_ext_info_t), addr)
100 		== -1) {
101 		mdb_warn("cannot read addr");
102 		return (DCMD_ERR);
103 	}
104 	bcopy(ext_prov.ei_label, scratch, CRYPTO_EXT_SIZE_LABEL);
105 	scratch[CRYPTO_EXT_SIZE_LABEL] = '\0';
106 	mdb_printf("ei_label\t\t%s\n", scratch);
107 
108 	bcopy(ext_prov.ei_manufacturerID, scratch, CRYPTO_EXT_SIZE_MANUF);
109 	scratch[CRYPTO_EXT_SIZE_MANUF] = '\0';
110 	mdb_printf("ei_manufacturerID\t%s\n", scratch);
111 
112 	bcopy(ext_prov.ei_model, scratch, CRYPTO_EXT_SIZE_MODEL);
113 	scratch[CRYPTO_EXT_SIZE_MODEL] = '\0';
114 	mdb_printf("ei_model\t\t%s\n", scratch);
115 
116 	bcopy(ext_prov.ei_serial_number, scratch, CRYPTO_EXT_SIZE_SERIAL);
117 	scratch[CRYPTO_EXT_SIZE_SERIAL] = '\0';
118 	mdb_printf("ei_serial_number\t%s\n", scratch);
119 
120 	mdb_printf("ei_flags\t0x%x:\t<%lb>\n", ext_prov.ei_flags,
121 	    ext_prov.ei_flags, extf_flags);
122 	mdb_printf("ei_max_session_count\t%lu\n",
123 	    ext_prov.ei_max_session_count);
124 	mdb_printf("ei_max_pin_len\t\t%lu\n", ext_prov.ei_max_pin_len);
125 	mdb_printf("ei_min_pin_len\t\t%lu\n", ext_prov.ei_min_pin_len);
126 	mdb_printf("ei_total_public_memory\t%lu\n",
127 	    ext_prov.ei_total_public_memory);
128 	mdb_printf("ei_free_public_memory\t%lu\n",
129 	    ext_prov.ei_free_public_memory);
130 	mdb_printf("ei_total_private_memory\t%lu\n",
131 	    ext_prov.ei_total_private_memory);
132 	mdb_printf("ei_free_private_memory\t%lu\n",
133 	    ext_prov.ei_free_private_memory);
134 	mdb_printf("ei_hardware_version\tmajor %c minor %c\n",
135 	    ext_prov.ei_hardware_version.cv_major,
136 	    ext_prov.ei_hardware_version.cv_minor);
137 	mdb_printf("ei_firmware_version\tmajor %c minor %c\n",
138 	    ext_prov.ei_firmware_version.cv_major,
139 	    ext_prov.ei_firmware_version.cv_minor);
140 	mdb_printf("ei_time\t%s\n", ext_prov.ei_time);
141 	return (DCMD_OK);
142 }
143 
144 const mdb_bitmask_t mech_bits[] = {
145 	{ "NIL", (uint32_t)-1, 0 },
146 	{ "CRYPTO_FG_ENCRYPT", CRYPTO_FG_ENCRYPT, CRYPTO_FG_ENCRYPT },
147 	{ "CRYPTO_FG_DECRYPT", CRYPTO_FG_DECRYPT, CRYPTO_FG_DECRYPT },
148 	{ "CRYPTO_FG_DIGEST", CRYPTO_FG_DIGEST, CRYPTO_FG_DIGEST },
149 	{ "CRYPTO_FG_SIGN", CRYPTO_FG_SIGN, CRYPTO_FG_SIGN },
150 	{ "CRYPTO_FG_SIGN_RECOVER", CRYPTO_FG_SIGN_RECOVER,
151 		CRYPTO_FG_SIGN_RECOVER },
152 	{ "CRYPTO_FG_VERIFY", CRYPTO_FG_VERIFY, CRYPTO_FG_VERIFY },
153 	{ "CRYPTO_FG_VERIFY_RECOVER", CRYPTO_FG_VERIFY_RECOVER,
154 		CRYPTO_FG_VERIFY_RECOVER },
155 	{ "CRYPTO_FG_GENERATE", CRYPTO_FG_GENERATE, CRYPTO_FG_GENERATE },
156 	{ "CRYPTO_FG_GENERATE_KEY_PAIR", CRYPTO_FG_GENERATE_KEY_PAIR,
157 		CRYPTO_FG_GENERATE_KEY_PAIR },
158 	{ "CRYPTO_FG_WRAP", CRYPTO_FG_WRAP, CRYPTO_FG_WRAP },
159 	{ "CRYPTO_FG_UNWRAP", CRYPTO_FG_UNWRAP, CRYPTO_FG_UNWRAP },
160 	{ "CRYPTO_FG_DERIVE", CRYPTO_FG_DERIVE, CRYPTO_FG_DERIVE },
161 	{ "CRYPTO_FG_MAC", CRYPTO_FG_MAC, CRYPTO_FG_MAC },
162 	{ "CRYPTO_FG_ENCRYPT_MAC", CRYPTO_FG_ENCRYPT_MAC,
163 		CRYPTO_FG_ENCRYPT_MAC },
164 	{ "CRYPTO_FG_MAC_DECRYPT", CRYPTO_FG_MAC_DECRYPT,
165 		CRYPTO_FG_MAC_DECRYPT },
166 	{ "CRYPTO_FG_ENCRYPT_ATOMIC", CRYPTO_FG_ENCRYPT_ATOMIC,
167 		CRYPTO_FG_ENCRYPT_ATOMIC },
168 	{ "CRYPTO_FG_DECRYPT_ATOMIC", CRYPTO_FG_DECRYPT_ATOMIC,
169 		CRYPTO_FG_DECRYPT_ATOMIC },
170 	{ "CRYPTO_FG_MAC_ATOMIC", CRYPTO_FG_MAC_ATOMIC, CRYPTO_FG_MAC_ATOMIC },
171 	{ "CRYPTO_FG_DIGEST_ATOMIC", CRYPTO_FG_DIGEST_ATOMIC,
172 		CRYPTO_FG_DIGEST_ATOMIC },
173 	{ "CRYPTO_FG_SIGN_ATOMIC", CRYPTO_FG_SIGN_ATOMIC,
174 		CRYPTO_FG_SIGN_ATOMIC },
175 	{ "CRYPTO_FG_SIGN_RECOVER_ATOMIC", CRYPTO_FG_SIGN_RECOVER_ATOMIC,
176 		CRYPTO_FG_SIGN_RECOVER_ATOMIC },
177 	{ "CRYPTO_FG_VERIFY_ATOMIC", CRYPTO_FG_VERIFY_ATOMIC,
178 		CRYPTO_FG_VERIFY_ATOMIC },
179 	{ "CRYPTO_FG_VERIFY_RECOVER_ATOMIC", CRYPTO_FG_VERIFY_RECOVER_ATOMIC,
180 		CRYPTO_FG_VERIFY_RECOVER_ATOMIC },
181 	{ "CRYPTO_FG_ENCRYPT_MAC_ATOMIC", CRYPTO_FG_ENCRYPT_MAC_ATOMIC,
182 		CRYPTO_FG_ENCRYPT_MAC_ATOMIC },
183 	{ "CRYPTO_FG_MAC_DECRYPT_ATOMIC", CRYPTO_FG_MAC_DECRYPT_ATOMIC,
184 		CRYPTO_FG_MAC_DECRYPT_ATOMIC },
185 	{ "CRYPTO_FG_RANDOM", CRYPTO_FG_RANDOM, CRYPTO_FG_RANDOM},
186 	{ NULL, 0, 0 }
187 };
188 
189 /*ARGSUSED*/
190 int
191 crypto_mech_info(uintptr_t addr, uint_t flags, int argc,
192     const mdb_arg_t *argv)
193 {
194 	crypto_mech_info_t minfo;
195 	const char *unit = "bits";
196 
197 	if (! flags & DCMD_ADDRSPEC)
198 		return (DCMD_USAGE);
199 
200 	if (mdb_vread(&minfo, sizeof (crypto_mech_info_t), addr)
201 		== -1) {
202 		mdb_warn("cannot read addr %p", addr);
203 		return (DCMD_ERR);
204 	}
205 	mdb_printf("cm_mech_name_t\t%s\n", minfo.cm_mech_name);
206 	mdb_printf("cm_mech_number\t%lld\n", minfo.cm_mech_number);
207 	mdb_printf("cm_func_group_mask\t0x%x:\t<%b>\n",
208 	    minfo.cm_func_group_mask, minfo.cm_func_group_mask, mech_bits);
209 	if (minfo.cm_keysize_unit != CRYPTO_KEYSIZE_UNIT_IN_BITS)
210 		unit = "bytes";
211 	mdb_printf("cm_min_key_length\t%lu %s\n", minfo.cm_min_key_length,
212 	    unit);
213 	mdb_printf("cm_max_key_length\t%lu %s\n", minfo.cm_max_key_length,
214 	    unit);
215 
216 	return (DCMD_OK);
217 }
218 
219 /*
220  * MDB module linkage information:
221  *
222  * We declare a list of structures describing our dcmds, and a function
223  * named _mdb_init to return a pointer to our module information.
224  */
225 
226 static const mdb_dcmd_t dcmds[] = {
227 
228 	/* spi.c */
229 	{ "crypto_provider_ext_info", ":",
230 	    "module-private crypto provider info",
231 	    crypto_provider_ext_info, NULL },
232 	{ "crypto_mech_info", ":",
233 	    "print as crypto_mech_info",
234 	    crypto_mech_info, NULL },
235 
236 	/* common.c */
237 	{ "crypto_mechanism", ":",
238 	    "details about a crypto mechanism", crypto_mechanism, NULL },
239 	{ "crypto_data", ":",
240 	    "print as crypto_data",
241 	    crypto_data, NULL },
242 	{ "crypto_dual_data", ":",
243 	    "print as crypto_dual_data",
244 	    crypto_dual_data, NULL },
245 	{ "crypto_key", ":",
246 	    "print as crypto_key", crypto_key, NULL },
247 
248 
249 	/* impl.c */
250 	{ "kcf_sched_info", ":",
251 	    "scheduling data for a crypto request", kcf_sched_info, NULL },
252 
253 	{ "kcf_provider_desc", ":",
254 	    "crypto provider description struct", kcf_provider_desc, NULL },
255 
256 	{ "prov_tab", "",
257 	    "global table of crypto providers ", prov_tab, NULL },
258 
259 	{ "policy_tab", "",
260 	    "print global policy_tab", policy_tab, NULL },
261 
262 	/* sched_impl.c */
263 	{ "kcf_areq_node", ":[-v]",
264 	    "print asynchronous crypto request struct, [ verbose ]",
265 		kcf_areq_node, NULL },
266 
267 	{ "kcf_global_swq", "?[-v]",
268 	    "global or addr global crypto queue.  [ -v = verbose ]",
269 		kcf_global_swq, NULL },
270 	{ "crypto_find_reqid", "?[-v] reqid",
271 	    "look for reqid, print if found [ -v = verbose ]",
272 		crypto_find_reqid, NULL },
273 
274 	{ "kcf_reqid_table", ":[-v]",
275 	    "print contents of a request ID hash table [ -v = verbose ]",
276 		kcf_reqid_table_dcmd, NULL },
277 
278 	{ "kcf_soft_conf_entry", "?",
279 	    "head or addr of configured software crypto providers",
280 		kcf_soft_conf_entry, NULL },
281 
282 	{ "kcf_policy_desc", ":", "policy descriptors for crypto",
283 		kcf_policy_desc, NULL },
284 	{ NULL }
285 };
286 
287 static const mdb_walker_t walkers[] = {
288 	{ "an_next", "walk kcf_areq_node's by an_next",
289 		areq_first_walk_init, an_next_walk_step, areq_walk_fini },
290 	{ "an_prev", "walk kcf_areq_node's by an_prev",
291 		areq_last_walk_init, an_prev_walk_step, areq_walk_fini },
292 	{ "an_idnext", "walk kcf_areq_node's by an_idnext",
293 		an_idnext_walk_init, an_idnext_walk_step, areq_walk_fini },
294 	{ "an_idprev", "walk kcf_areq_node's by an_idprev",
295 		an_idprev_walk_init, an_idprev_walk_step, areq_walk_fini },
296 	{ "an_ctxchain_next",
297 		"walk kcf_areq_node's by an_ctxchain_next",
298 		an_ctxchain_walk_init, an_ctxchain_walk_step, areq_walk_fini },
299 	{ "kcf_reqid_table", "table of asynchronous crypto requests",
300 		reqid_table_walk_init, reqid_table_walk_step,
301 		    reqid_table_walk_fini },
302 	{ "soft_conf_entry", "table of software providers or addr",
303 		soft_conf_walk_init, soft_conf_walk_step,
304 		    soft_conf_walk_fini },
305 	{ NULL }
306 };
307 
308 
309 static const mdb_modinfo_t modinfo = {
310 	MDB_API_VERSION, dcmds, walkers
311 };
312 
313 const mdb_modinfo_t *
314 _mdb_init(void)
315 {
316 	return (&modinfo);
317 }
318