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