Blame


1 d1085492 2020-11-07 stsp /* $OpenBSD: softraid_crypto.c,v 1.139 2020/07/13 00:06:22 kn Exp $ */
2 d1085492 2020-11-07 stsp /*
3 d1085492 2020-11-07 stsp * Copyright (c) 2007 Marco Peereboom <marco@peereboom.us>
4 d1085492 2020-11-07 stsp * Copyright (c) 2008 Hans-Joerg Hoexer <hshoexer@openbsd.org>
5 d1085492 2020-11-07 stsp * Copyright (c) 2008 Damien Miller <djm@mindrot.org>
6 d1085492 2020-11-07 stsp * Copyright (c) 2009 Joel Sing <jsing@openbsd.org>
7 d1085492 2020-11-07 stsp *
8 d1085492 2020-11-07 stsp * Permission to use, copy, modify, and distribute this software for any
9 d1085492 2020-11-07 stsp * purpose with or without fee is hereby granted, provided that the above
10 d1085492 2020-11-07 stsp * copyright notice and this permission notice appear in all copies.
11 d1085492 2020-11-07 stsp *
12 d1085492 2020-11-07 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 d1085492 2020-11-07 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 d1085492 2020-11-07 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 d1085492 2020-11-07 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 d1085492 2020-11-07 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 d1085492 2020-11-07 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 d1085492 2020-11-07 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 d1085492 2020-11-07 stsp */
20 d1085492 2020-11-07 stsp
21 d1085492 2020-11-07 stsp #include "bio.h"
22 d1085492 2020-11-07 stsp
23 d1085492 2020-11-07 stsp #include <sys/param.h>
24 d1085492 2020-11-07 stsp #include <sys/systm.h>
25 d1085492 2020-11-07 stsp #include <sys/buf.h>
26 d1085492 2020-11-07 stsp #include <sys/device.h>
27 d1085492 2020-11-07 stsp #include <sys/ioctl.h>
28 d1085492 2020-11-07 stsp #include <sys/malloc.h>
29 d1085492 2020-11-07 stsp #include <sys/pool.h>
30 d1085492 2020-11-07 stsp #include <sys/kernel.h>
31 d1085492 2020-11-07 stsp #include <sys/disk.h>
32 d1085492 2020-11-07 stsp #include <sys/rwlock.h>
33 d1085492 2020-11-07 stsp #include <sys/queue.h>
34 d1085492 2020-11-07 stsp #include <sys/fcntl.h>
35 d1085492 2020-11-07 stsp #include <sys/disklabel.h>
36 d1085492 2020-11-07 stsp #include <sys/vnode.h>
37 d1085492 2020-11-07 stsp #include <sys/mount.h>
38 d1085492 2020-11-07 stsp #include <sys/sensors.h>
39 d1085492 2020-11-07 stsp #include <sys/stat.h>
40 d1085492 2020-11-07 stsp #include <sys/conf.h>
41 d1085492 2020-11-07 stsp #include <sys/uio.h>
42 d1085492 2020-11-07 stsp #include <sys/dkio.h>
43 d1085492 2020-11-07 stsp
44 d1085492 2020-11-07 stsp #include <crypto/cryptodev.h>
45 d1085492 2020-11-07 stsp #include <crypto/rijndael.h>
46 d1085492 2020-11-07 stsp #include <crypto/md5.h>
47 d1085492 2020-11-07 stsp #include <crypto/sha1.h>
48 d1085492 2020-11-07 stsp #include <crypto/sha2.h>
49 d1085492 2020-11-07 stsp #include <crypto/hmac.h>
50 d1085492 2020-11-07 stsp
51 d1085492 2020-11-07 stsp #include <scsi/scsi_all.h>
52 d1085492 2020-11-07 stsp #include <scsi/scsiconf.h>
53 d1085492 2020-11-07 stsp #include <scsi/scsi_disk.h>
54 d1085492 2020-11-07 stsp
55 d1085492 2020-11-07 stsp #include <dev/softraidvar.h>
56 d1085492 2020-11-07 stsp
57 d1085492 2020-11-07 stsp /*
58 d1085492 2020-11-07 stsp * The per-I/O data that we need to preallocate. We cannot afford to allow I/O
59 d1085492 2020-11-07 stsp * to start failing when memory pressure kicks in. We can store this in the WU
60 d1085492 2020-11-07 stsp * because we assert that only one ccb per WU will ever be active.
61 d1085492 2020-11-07 stsp */
62 d1085492 2020-11-07 stsp struct sr_crypto_wu {
63 d1085492 2020-11-07 stsp struct sr_workunit cr_wu; /* Must be first. */
64 d1085492 2020-11-07 stsp struct uio cr_uio;
65 d1085492 2020-11-07 stsp struct iovec cr_iov;
66 d1085492 2020-11-07 stsp struct cryptop *cr_crp;
67 d1085492 2020-11-07 stsp void *cr_dmabuf;
68 d1085492 2020-11-07 stsp };
69 d1085492 2020-11-07 stsp
70 d1085492 2020-11-07 stsp
71 d1085492 2020-11-07 stsp struct sr_crypto_wu *sr_crypto_prepare(struct sr_workunit *, int);
72 d1085492 2020-11-07 stsp int sr_crypto_create_keys(struct sr_discipline *);
73 d1085492 2020-11-07 stsp int sr_crypto_get_kdf(struct bioc_createraid *,
74 d1085492 2020-11-07 stsp struct sr_discipline *);
75 d1085492 2020-11-07 stsp int sr_crypto_decrypt(u_char *, u_char *, u_char *, size_t, int);
76 d1085492 2020-11-07 stsp int sr_crypto_encrypt(u_char *, u_char *, u_char *, size_t, int);
77 d1085492 2020-11-07 stsp int sr_crypto_decrypt_key(struct sr_discipline *);
78 d1085492 2020-11-07 stsp int sr_crypto_change_maskkey(struct sr_discipline *,
79 d1085492 2020-11-07 stsp struct sr_crypto_kdfinfo *, struct sr_crypto_kdfinfo *);
80 d1085492 2020-11-07 stsp int sr_crypto_create(struct sr_discipline *,
81 d1085492 2020-11-07 stsp struct bioc_createraid *, int, int64_t);
82 d1085492 2020-11-07 stsp int sr_crypto_assemble(struct sr_discipline *,
83 d1085492 2020-11-07 stsp struct bioc_createraid *, int, void *);
84 d1085492 2020-11-07 stsp int sr_crypto_alloc_resources(struct sr_discipline *);
85 d1085492 2020-11-07 stsp void sr_crypto_free_resources(struct sr_discipline *);
86 d1085492 2020-11-07 stsp int sr_crypto_ioctl(struct sr_discipline *,
87 d1085492 2020-11-07 stsp struct bioc_discipline *);
88 d1085492 2020-11-07 stsp int sr_crypto_meta_opt_handler(struct sr_discipline *,
89 d1085492 2020-11-07 stsp struct sr_meta_opt_hdr *);
90 d1085492 2020-11-07 stsp void sr_crypto_write(struct cryptop *);
91 d1085492 2020-11-07 stsp int sr_crypto_rw(struct sr_workunit *);
92 d1085492 2020-11-07 stsp int sr_crypto_dev_rw(struct sr_workunit *, struct sr_crypto_wu *);
93 d1085492 2020-11-07 stsp void sr_crypto_done(struct sr_workunit *);
94 d1085492 2020-11-07 stsp void sr_crypto_read(struct cryptop *);
95 d1085492 2020-11-07 stsp void sr_crypto_calculate_check_hmac_sha1(u_int8_t *, int,
96 d1085492 2020-11-07 stsp u_int8_t *, int, u_char *);
97 d1085492 2020-11-07 stsp void sr_crypto_hotplug(struct sr_discipline *, struct disk *, int);
98 d1085492 2020-11-07 stsp
99 d1085492 2020-11-07 stsp #ifdef SR_DEBUG0
100 d1085492 2020-11-07 stsp void sr_crypto_dumpkeys(struct sr_discipline *);
101 d1085492 2020-11-07 stsp #endif
102 d1085492 2020-11-07 stsp
103 d1085492 2020-11-07 stsp /* Discipline initialisation. */
104 d1085492 2020-11-07 stsp void
105 d1085492 2020-11-07 stsp sr_crypto_discipline_init(struct sr_discipline *sd)
106 d1085492 2020-11-07 stsp {
107 d1085492 2020-11-07 stsp int i;
108 d1085492 2020-11-07 stsp
109 d1085492 2020-11-07 stsp /* Fill out discipline members. */
110 d1085492 2020-11-07 stsp sd->sd_wu_size = sizeof(struct sr_crypto_wu);
111 d1085492 2020-11-07 stsp sd->sd_type = SR_MD_CRYPTO;
112 d1085492 2020-11-07 stsp strlcpy(sd->sd_name, "CRYPTO", sizeof(sd->sd_name));
113 d1085492 2020-11-07 stsp sd->sd_capabilities = SR_CAP_SYSTEM_DISK | SR_CAP_AUTO_ASSEMBLE;
114 d1085492 2020-11-07 stsp sd->sd_max_wu = SR_CRYPTO_NOWU;
115 d1085492 2020-11-07 stsp
116 d1085492 2020-11-07 stsp for (i = 0; i < SR_CRYPTO_MAXKEYS; i++)
117 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_sid[i] = (u_int64_t)-1;
118 d1085492 2020-11-07 stsp
119 d1085492 2020-11-07 stsp /* Setup discipline specific function pointers. */
120 d1085492 2020-11-07 stsp sd->sd_alloc_resources = sr_crypto_alloc_resources;
121 d1085492 2020-11-07 stsp sd->sd_assemble = sr_crypto_assemble;
122 d1085492 2020-11-07 stsp sd->sd_create = sr_crypto_create;
123 d1085492 2020-11-07 stsp sd->sd_free_resources = sr_crypto_free_resources;
124 d1085492 2020-11-07 stsp sd->sd_ioctl_handler = sr_crypto_ioctl;
125 d1085492 2020-11-07 stsp sd->sd_meta_opt_handler = sr_crypto_meta_opt_handler;
126 d1085492 2020-11-07 stsp sd->sd_scsi_rw = sr_crypto_rw;
127 d1085492 2020-11-07 stsp sd->sd_scsi_done = sr_crypto_done;
128 d1085492 2020-11-07 stsp }
129 d1085492 2020-11-07 stsp
130 d1085492 2020-11-07 stsp int
131 d1085492 2020-11-07 stsp sr_crypto_create(struct sr_discipline *sd, struct bioc_createraid *bc,
132 d1085492 2020-11-07 stsp int no_chunk, int64_t coerced_size)
133 d1085492 2020-11-07 stsp {
134 d1085492 2020-11-07 stsp struct sr_meta_opt_item *omi;
135 d1085492 2020-11-07 stsp int rv = EINVAL;
136 d1085492 2020-11-07 stsp
137 d1085492 2020-11-07 stsp if (no_chunk != 1) {
138 d1085492 2020-11-07 stsp sr_error(sd->sd_sc, "%s requires exactly one chunk",
139 d1085492 2020-11-07 stsp sd->sd_name);
140 d1085492 2020-11-07 stsp goto done;
141 d1085492 2020-11-07 stsp }
142 d1085492 2020-11-07 stsp
143 d1085492 2020-11-07 stsp if (coerced_size > SR_CRYPTO_MAXSIZE) {
144 d1085492 2020-11-07 stsp sr_error(sd->sd_sc, "%s exceeds maximum size (%lli > %llu)",
145 d1085492 2020-11-07 stsp sd->sd_name, coerced_size, SR_CRYPTO_MAXSIZE);
146 d1085492 2020-11-07 stsp goto done;
147 d1085492 2020-11-07 stsp }
148 d1085492 2020-11-07 stsp
149 d1085492 2020-11-07 stsp /* Create crypto optional metadata. */
150 d1085492 2020-11-07 stsp omi = malloc(sizeof(struct sr_meta_opt_item), M_DEVBUF,
151 d1085492 2020-11-07 stsp M_WAITOK | M_ZERO);
152 d1085492 2020-11-07 stsp omi->omi_som = malloc(sizeof(struct sr_meta_crypto), M_DEVBUF,
153 d1085492 2020-11-07 stsp M_WAITOK | M_ZERO);
154 d1085492 2020-11-07 stsp omi->omi_som->som_type = SR_OPT_CRYPTO;
155 d1085492 2020-11-07 stsp omi->omi_som->som_length = sizeof(struct sr_meta_crypto);
156 d1085492 2020-11-07 stsp SLIST_INSERT_HEAD(&sd->sd_meta_opt, omi, omi_link);
157 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta = (struct sr_meta_crypto *)omi->omi_som;
158 d1085492 2020-11-07 stsp sd->sd_meta->ssdi.ssd_opt_no++;
159 d1085492 2020-11-07 stsp
160 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.key_disk = NULL;
161 d1085492 2020-11-07 stsp
162 d1085492 2020-11-07 stsp if (bc->bc_key_disk != NODEV) {
163 d1085492 2020-11-07 stsp
164 d1085492 2020-11-07 stsp /* Create a key disk. */
165 d1085492 2020-11-07 stsp if (sr_crypto_get_kdf(bc, sd))
166 d1085492 2020-11-07 stsp goto done;
167 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.key_disk =
168 d1085492 2020-11-07 stsp sr_crypto_create_key_disk(sd, bc->bc_key_disk);
169 d1085492 2020-11-07 stsp if (sd->mds.mdd_crypto.key_disk == NULL)
170 d1085492 2020-11-07 stsp goto done;
171 d1085492 2020-11-07 stsp sd->sd_capabilities |= SR_CAP_AUTO_ASSEMBLE;
172 d1085492 2020-11-07 stsp
173 d1085492 2020-11-07 stsp } else if (bc->bc_opaque_flags & BIOC_SOOUT) {
174 d1085492 2020-11-07 stsp
175 d1085492 2020-11-07 stsp /* No hint available yet. */
176 d1085492 2020-11-07 stsp bc->bc_opaque_status = BIOC_SOINOUT_FAILED;
177 d1085492 2020-11-07 stsp rv = EAGAIN;
178 d1085492 2020-11-07 stsp goto done;
179 d1085492 2020-11-07 stsp
180 d1085492 2020-11-07 stsp } else if (sr_crypto_get_kdf(bc, sd))
181 d1085492 2020-11-07 stsp goto done;
182 d1085492 2020-11-07 stsp
183 d1085492 2020-11-07 stsp /* Passphrase volumes cannot be automatically assembled. */
184 d1085492 2020-11-07 stsp if (!(bc->bc_flags & BIOC_SCNOAUTOASSEMBLE) && bc->bc_key_disk == NODEV)
185 d1085492 2020-11-07 stsp goto done;
186 d1085492 2020-11-07 stsp
187 d1085492 2020-11-07 stsp sd->sd_meta->ssdi.ssd_size = coerced_size;
188 d1085492 2020-11-07 stsp
189 d1085492 2020-11-07 stsp sr_crypto_create_keys(sd);
190 d1085492 2020-11-07 stsp
191 d1085492 2020-11-07 stsp sd->sd_max_ccb_per_wu = no_chunk;
192 d1085492 2020-11-07 stsp
193 d1085492 2020-11-07 stsp rv = 0;
194 d1085492 2020-11-07 stsp done:
195 d1085492 2020-11-07 stsp return (rv);
196 d1085492 2020-11-07 stsp }
197 d1085492 2020-11-07 stsp
198 d1085492 2020-11-07 stsp int
199 d1085492 2020-11-07 stsp sr_crypto_assemble(struct sr_discipline *sd, struct bioc_createraid *bc,
200 d1085492 2020-11-07 stsp int no_chunk, void *data)
201 d1085492 2020-11-07 stsp {
202 d1085492 2020-11-07 stsp int rv = EINVAL;
203 d1085492 2020-11-07 stsp
204 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.key_disk = NULL;
205 d1085492 2020-11-07 stsp
206 d1085492 2020-11-07 stsp /* Crypto optional metadata must already exist... */
207 d1085492 2020-11-07 stsp if (sd->mds.mdd_crypto.scr_meta == NULL)
208 d1085492 2020-11-07 stsp goto done;
209 d1085492 2020-11-07 stsp
210 d1085492 2020-11-07 stsp if (data != NULL) {
211 d1085492 2020-11-07 stsp /* Kernel already has mask key. */
212 d1085492 2020-11-07 stsp memcpy(sd->mds.mdd_crypto.scr_maskkey, data,
213 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_maskkey));
214 d1085492 2020-11-07 stsp } else if (bc->bc_key_disk != NODEV) {
215 d1085492 2020-11-07 stsp /* Read the mask key from the key disk. */
216 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.key_disk =
217 d1085492 2020-11-07 stsp sr_crypto_read_key_disk(sd, bc->bc_key_disk);
218 d1085492 2020-11-07 stsp if (sd->mds.mdd_crypto.key_disk == NULL)
219 d1085492 2020-11-07 stsp goto done;
220 d1085492 2020-11-07 stsp } else if (bc->bc_opaque_flags & BIOC_SOOUT) {
221 d1085492 2020-11-07 stsp /* provide userland with kdf hint */
222 d1085492 2020-11-07 stsp if (bc->bc_opaque == NULL)
223 d1085492 2020-11-07 stsp goto done;
224 d1085492 2020-11-07 stsp
225 d1085492 2020-11-07 stsp if (sizeof(sd->mds.mdd_crypto.scr_meta->scm_kdfhint) <
226 d1085492 2020-11-07 stsp bc->bc_opaque_size)
227 d1085492 2020-11-07 stsp goto done;
228 d1085492 2020-11-07 stsp
229 d1085492 2020-11-07 stsp if (copyout(sd->mds.mdd_crypto.scr_meta->scm_kdfhint,
230 d1085492 2020-11-07 stsp bc->bc_opaque, bc->bc_opaque_size))
231 d1085492 2020-11-07 stsp goto done;
232 d1085492 2020-11-07 stsp
233 d1085492 2020-11-07 stsp /* we're done */
234 d1085492 2020-11-07 stsp bc->bc_opaque_status = BIOC_SOINOUT_OK;
235 d1085492 2020-11-07 stsp rv = EAGAIN;
236 d1085492 2020-11-07 stsp goto done;
237 d1085492 2020-11-07 stsp } else if (bc->bc_opaque_flags & BIOC_SOIN) {
238 d1085492 2020-11-07 stsp /* get kdf with maskkey from userland */
239 d1085492 2020-11-07 stsp if (sr_crypto_get_kdf(bc, sd))
240 d1085492 2020-11-07 stsp goto done;
241 d1085492 2020-11-07 stsp } else
242 d1085492 2020-11-07 stsp goto done;
243 d1085492 2020-11-07 stsp
244 d1085492 2020-11-07 stsp sd->sd_max_ccb_per_wu = sd->sd_meta->ssdi.ssd_chunk_no;
245 d1085492 2020-11-07 stsp
246 d1085492 2020-11-07 stsp rv = 0;
247 d1085492 2020-11-07 stsp done:
248 d1085492 2020-11-07 stsp return (rv);
249 d1085492 2020-11-07 stsp }
250 d1085492 2020-11-07 stsp
251 d1085492 2020-11-07 stsp struct sr_crypto_wu *
252 d1085492 2020-11-07 stsp sr_crypto_prepare(struct sr_workunit *wu, int encrypt)
253 d1085492 2020-11-07 stsp {
254 d1085492 2020-11-07 stsp struct scsi_xfer *xs = wu->swu_xs;
255 d1085492 2020-11-07 stsp struct sr_discipline *sd = wu->swu_dis;
256 d1085492 2020-11-07 stsp struct sr_crypto_wu *crwu;
257 d1085492 2020-11-07 stsp struct cryptodesc *crd;
258 d1085492 2020-11-07 stsp int flags, i, n;
259 d1085492 2020-11-07 stsp daddr_t blkno;
260 d1085492 2020-11-07 stsp u_int keyndx;
261 d1085492 2020-11-07 stsp
262 d1085492 2020-11-07 stsp DNPRINTF(SR_D_DIS, "%s: sr_crypto_prepare wu %p encrypt %d\n",
263 d1085492 2020-11-07 stsp DEVNAME(sd->sd_sc), wu, encrypt);
264 d1085492 2020-11-07 stsp
265 d1085492 2020-11-07 stsp crwu = (struct sr_crypto_wu *)wu;
266 d1085492 2020-11-07 stsp crwu->cr_uio.uio_iovcnt = 1;
267 d1085492 2020-11-07 stsp crwu->cr_uio.uio_iov->iov_len = xs->datalen;
268 d1085492 2020-11-07 stsp if (xs->flags & SCSI_DATA_OUT) {
269 d1085492 2020-11-07 stsp crwu->cr_uio.uio_iov->iov_base = crwu->cr_dmabuf;
270 d1085492 2020-11-07 stsp memcpy(crwu->cr_uio.uio_iov->iov_base, xs->data, xs->datalen);
271 d1085492 2020-11-07 stsp } else
272 d1085492 2020-11-07 stsp crwu->cr_uio.uio_iov->iov_base = xs->data;
273 d1085492 2020-11-07 stsp
274 d1085492 2020-11-07 stsp blkno = wu->swu_blk_start;
275 d1085492 2020-11-07 stsp n = xs->datalen >> DEV_BSHIFT;
276 d1085492 2020-11-07 stsp
277 d1085492 2020-11-07 stsp /*
278 d1085492 2020-11-07 stsp * We preallocated enough crypto descs for up to MAXPHYS of I/O.
279 d1085492 2020-11-07 stsp * Since there may be less than that we need to tweak the amount
280 d1085492 2020-11-07 stsp * of crypto desc structures to be just long enough for our needs.
281 d1085492 2020-11-07 stsp */
282 d1085492 2020-11-07 stsp KASSERT(crwu->cr_crp->crp_ndescalloc >= n);
283 d1085492 2020-11-07 stsp crwu->cr_crp->crp_ndesc = n;
284 d1085492 2020-11-07 stsp flags = (encrypt ? CRD_F_ENCRYPT : 0) |
285 d1085492 2020-11-07 stsp CRD_F_IV_PRESENT | CRD_F_IV_EXPLICIT;
286 d1085492 2020-11-07 stsp
287 d1085492 2020-11-07 stsp /*
288 d1085492 2020-11-07 stsp * Select crypto session based on block number.
289 d1085492 2020-11-07 stsp *
290 d1085492 2020-11-07 stsp * XXX - this does not handle the case where the read/write spans
291 d1085492 2020-11-07 stsp * across a different key blocks (e.g. 0.5TB boundary). Currently
292 d1085492 2020-11-07 stsp * this is already broken by the use of scr_key[0] below.
293 d1085492 2020-11-07 stsp */
294 d1085492 2020-11-07 stsp keyndx = blkno >> SR_CRYPTO_KEY_BLKSHIFT;
295 d1085492 2020-11-07 stsp crwu->cr_crp->crp_sid = sd->mds.mdd_crypto.scr_sid[keyndx];
296 d1085492 2020-11-07 stsp
297 d1085492 2020-11-07 stsp crwu->cr_crp->crp_opaque = crwu;
298 d1085492 2020-11-07 stsp crwu->cr_crp->crp_ilen = xs->datalen;
299 d1085492 2020-11-07 stsp crwu->cr_crp->crp_alloctype = M_DEVBUF;
300 d1085492 2020-11-07 stsp crwu->cr_crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_NOQUEUE;
301 d1085492 2020-11-07 stsp crwu->cr_crp->crp_buf = &crwu->cr_uio;
302 d1085492 2020-11-07 stsp for (i = 0; i < crwu->cr_crp->crp_ndesc; i++, blkno++) {
303 d1085492 2020-11-07 stsp crd = &crwu->cr_crp->crp_desc[i];
304 d1085492 2020-11-07 stsp crd->crd_skip = i << DEV_BSHIFT;
305 d1085492 2020-11-07 stsp crd->crd_len = DEV_BSIZE;
306 d1085492 2020-11-07 stsp crd->crd_inject = 0;
307 d1085492 2020-11-07 stsp crd->crd_flags = flags;
308 d1085492 2020-11-07 stsp crd->crd_alg = sd->mds.mdd_crypto.scr_alg;
309 d1085492 2020-11-07 stsp crd->crd_klen = sd->mds.mdd_crypto.scr_klen;
310 d1085492 2020-11-07 stsp crd->crd_key = sd->mds.mdd_crypto.scr_key[0];
311 d1085492 2020-11-07 stsp memcpy(crd->crd_iv, &blkno, sizeof(blkno));
312 d1085492 2020-11-07 stsp }
313 d1085492 2020-11-07 stsp
314 d1085492 2020-11-07 stsp return (crwu);
315 d1085492 2020-11-07 stsp }
316 d1085492 2020-11-07 stsp
317 d1085492 2020-11-07 stsp int
318 d1085492 2020-11-07 stsp sr_crypto_get_kdf(struct bioc_createraid *bc, struct sr_discipline *sd)
319 d1085492 2020-11-07 stsp {
320 d1085492 2020-11-07 stsp int rv = EINVAL;
321 d1085492 2020-11-07 stsp struct sr_crypto_kdfinfo *kdfinfo;
322 d1085492 2020-11-07 stsp
323 d1085492 2020-11-07 stsp if (!(bc->bc_opaque_flags & BIOC_SOIN))
324 d1085492 2020-11-07 stsp return (rv);
325 d1085492 2020-11-07 stsp if (bc->bc_opaque == NULL)
326 d1085492 2020-11-07 stsp return (rv);
327 d1085492 2020-11-07 stsp if (bc->bc_opaque_size != sizeof(*kdfinfo))
328 d1085492 2020-11-07 stsp return (rv);
329 d1085492 2020-11-07 stsp
330 d1085492 2020-11-07 stsp kdfinfo = malloc(bc->bc_opaque_size, M_DEVBUF, M_WAITOK | M_ZERO);
331 d1085492 2020-11-07 stsp if (copyin(bc->bc_opaque, kdfinfo, bc->bc_opaque_size))
332 d1085492 2020-11-07 stsp goto out;
333 d1085492 2020-11-07 stsp
334 d1085492 2020-11-07 stsp if (kdfinfo->len != bc->bc_opaque_size)
335 d1085492 2020-11-07 stsp goto out;
336 d1085492 2020-11-07 stsp
337 d1085492 2020-11-07 stsp /* copy KDF hint to disk meta data */
338 d1085492 2020-11-07 stsp if (kdfinfo->flags & SR_CRYPTOKDF_HINT) {
339 d1085492 2020-11-07 stsp if (sizeof(sd->mds.mdd_crypto.scr_meta->scm_kdfhint) <
340 d1085492 2020-11-07 stsp kdfinfo->genkdf.len)
341 d1085492 2020-11-07 stsp goto out;
342 d1085492 2020-11-07 stsp memcpy(sd->mds.mdd_crypto.scr_meta->scm_kdfhint,
343 d1085492 2020-11-07 stsp &kdfinfo->genkdf, kdfinfo->genkdf.len);
344 d1085492 2020-11-07 stsp }
345 d1085492 2020-11-07 stsp
346 d1085492 2020-11-07 stsp /* copy mask key to run-time meta data */
347 d1085492 2020-11-07 stsp if ((kdfinfo->flags & SR_CRYPTOKDF_KEY)) {
348 d1085492 2020-11-07 stsp if (sizeof(sd->mds.mdd_crypto.scr_maskkey) <
349 d1085492 2020-11-07 stsp sizeof(kdfinfo->maskkey))
350 d1085492 2020-11-07 stsp goto out;
351 d1085492 2020-11-07 stsp memcpy(sd->mds.mdd_crypto.scr_maskkey, &kdfinfo->maskkey,
352 d1085492 2020-11-07 stsp sizeof(kdfinfo->maskkey));
353 d1085492 2020-11-07 stsp }
354 d1085492 2020-11-07 stsp
355 d1085492 2020-11-07 stsp bc->bc_opaque_status = BIOC_SOINOUT_OK;
356 d1085492 2020-11-07 stsp rv = 0;
357 d1085492 2020-11-07 stsp out:
358 d1085492 2020-11-07 stsp explicit_bzero(kdfinfo, bc->bc_opaque_size);
359 d1085492 2020-11-07 stsp free(kdfinfo, M_DEVBUF, bc->bc_opaque_size);
360 d1085492 2020-11-07 stsp
361 d1085492 2020-11-07 stsp return (rv);
362 d1085492 2020-11-07 stsp }
363 d1085492 2020-11-07 stsp
364 d1085492 2020-11-07 stsp int
365 d1085492 2020-11-07 stsp sr_crypto_encrypt(u_char *p, u_char *c, u_char *key, size_t size, int alg)
366 d1085492 2020-11-07 stsp {
367 d1085492 2020-11-07 stsp rijndael_ctx ctx;
368 d1085492 2020-11-07 stsp int i, rv = 1;
369 d1085492 2020-11-07 stsp
370 d1085492 2020-11-07 stsp switch (alg) {
371 d1085492 2020-11-07 stsp case SR_CRYPTOM_AES_ECB_256:
372 d1085492 2020-11-07 stsp if (rijndael_set_key_enc_only(&ctx, key, 256) != 0)
373 d1085492 2020-11-07 stsp goto out;
374 d1085492 2020-11-07 stsp for (i = 0; i < size; i += RIJNDAEL128_BLOCK_LEN)
375 d1085492 2020-11-07 stsp rijndael_encrypt(&ctx, &p[i], &c[i]);
376 d1085492 2020-11-07 stsp rv = 0;
377 d1085492 2020-11-07 stsp break;
378 d1085492 2020-11-07 stsp default:
379 d1085492 2020-11-07 stsp DNPRINTF(SR_D_DIS, "%s: unsupported encryption algorithm %d\n",
380 d1085492 2020-11-07 stsp "softraid", alg);
381 d1085492 2020-11-07 stsp rv = -1;
382 d1085492 2020-11-07 stsp goto out;
383 d1085492 2020-11-07 stsp }
384 d1085492 2020-11-07 stsp
385 d1085492 2020-11-07 stsp out:
386 d1085492 2020-11-07 stsp explicit_bzero(&ctx, sizeof(ctx));
387 d1085492 2020-11-07 stsp return (rv);
388 d1085492 2020-11-07 stsp }
389 d1085492 2020-11-07 stsp
390 d1085492 2020-11-07 stsp int
391 d1085492 2020-11-07 stsp sr_crypto_decrypt(u_char *c, u_char *p, u_char *key, size_t size, int alg)
392 d1085492 2020-11-07 stsp {
393 d1085492 2020-11-07 stsp rijndael_ctx ctx;
394 d1085492 2020-11-07 stsp int i, rv = 1;
395 d1085492 2020-11-07 stsp
396 d1085492 2020-11-07 stsp switch (alg) {
397 d1085492 2020-11-07 stsp case SR_CRYPTOM_AES_ECB_256:
398 d1085492 2020-11-07 stsp if (rijndael_set_key(&ctx, key, 256) != 0)
399 d1085492 2020-11-07 stsp goto out;
400 d1085492 2020-11-07 stsp for (i = 0; i < size; i += RIJNDAEL128_BLOCK_LEN)
401 d1085492 2020-11-07 stsp rijndael_decrypt(&ctx, &c[i], &p[i]);
402 d1085492 2020-11-07 stsp rv = 0;
403 d1085492 2020-11-07 stsp break;
404 d1085492 2020-11-07 stsp default:
405 d1085492 2020-11-07 stsp DNPRINTF(SR_D_DIS, "%s: unsupported encryption algorithm %d\n",
406 d1085492 2020-11-07 stsp "softraid", alg);
407 d1085492 2020-11-07 stsp rv = -1;
408 d1085492 2020-11-07 stsp goto out;
409 d1085492 2020-11-07 stsp }
410 d1085492 2020-11-07 stsp
411 d1085492 2020-11-07 stsp out:
412 d1085492 2020-11-07 stsp explicit_bzero(&ctx, sizeof(ctx));
413 d1085492 2020-11-07 stsp return (rv);
414 d1085492 2020-11-07 stsp }
415 d1085492 2020-11-07 stsp
416 d1085492 2020-11-07 stsp void
417 d1085492 2020-11-07 stsp sr_crypto_calculate_check_hmac_sha1(u_int8_t *maskkey, int maskkey_size,
418 d1085492 2020-11-07 stsp u_int8_t *key, int key_size, u_char *check_digest)
419 d1085492 2020-11-07 stsp {
420 d1085492 2020-11-07 stsp u_char check_key[SHA1_DIGEST_LENGTH];
421 d1085492 2020-11-07 stsp HMAC_SHA1_CTX hmacctx;
422 d1085492 2020-11-07 stsp SHA1_CTX shactx;
423 d1085492 2020-11-07 stsp
424 d1085492 2020-11-07 stsp bzero(check_key, sizeof(check_key));
425 d1085492 2020-11-07 stsp bzero(&hmacctx, sizeof(hmacctx));
426 d1085492 2020-11-07 stsp bzero(&shactx, sizeof(shactx));
427 d1085492 2020-11-07 stsp
428 d1085492 2020-11-07 stsp /* k = SHA1(mask_key) */
429 d1085492 2020-11-07 stsp SHA1Init(&shactx);
430 d1085492 2020-11-07 stsp SHA1Update(&shactx, maskkey, maskkey_size);
431 d1085492 2020-11-07 stsp SHA1Final(check_key, &shactx);
432 d1085492 2020-11-07 stsp
433 d1085492 2020-11-07 stsp /* mac = HMAC_SHA1_k(unencrypted key) */
434 d1085492 2020-11-07 stsp HMAC_SHA1_Init(&hmacctx, check_key, sizeof(check_key));
435 d1085492 2020-11-07 stsp HMAC_SHA1_Update(&hmacctx, key, key_size);
436 d1085492 2020-11-07 stsp HMAC_SHA1_Final(check_digest, &hmacctx);
437 d1085492 2020-11-07 stsp
438 d1085492 2020-11-07 stsp explicit_bzero(check_key, sizeof(check_key));
439 d1085492 2020-11-07 stsp explicit_bzero(&hmacctx, sizeof(hmacctx));
440 d1085492 2020-11-07 stsp explicit_bzero(&shactx, sizeof(shactx));
441 d1085492 2020-11-07 stsp }
442 d1085492 2020-11-07 stsp
443 d1085492 2020-11-07 stsp int
444 d1085492 2020-11-07 stsp sr_crypto_decrypt_key(struct sr_discipline *sd)
445 d1085492 2020-11-07 stsp {
446 d1085492 2020-11-07 stsp u_char check_digest[SHA1_DIGEST_LENGTH];
447 d1085492 2020-11-07 stsp int rv = 1;
448 d1085492 2020-11-07 stsp
449 d1085492 2020-11-07 stsp DNPRINTF(SR_D_DIS, "%s: sr_crypto_decrypt_key\n", DEVNAME(sd->sd_sc));
450 d1085492 2020-11-07 stsp
451 d1085492 2020-11-07 stsp if (sd->mds.mdd_crypto.scr_meta->scm_check_alg != SR_CRYPTOC_HMAC_SHA1)
452 d1085492 2020-11-07 stsp goto out;
453 d1085492 2020-11-07 stsp
454 d1085492 2020-11-07 stsp if (sr_crypto_decrypt((u_char *)sd->mds.mdd_crypto.scr_meta->scm_key,
455 d1085492 2020-11-07 stsp (u_char *)sd->mds.mdd_crypto.scr_key,
456 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_maskkey, sizeof(sd->mds.mdd_crypto.scr_key),
457 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta->scm_mask_alg) == -1)
458 d1085492 2020-11-07 stsp goto out;
459 d1085492 2020-11-07 stsp
460 d1085492 2020-11-07 stsp #ifdef SR_DEBUG0
461 d1085492 2020-11-07 stsp sr_crypto_dumpkeys(sd);
462 d1085492 2020-11-07 stsp #endif
463 d1085492 2020-11-07 stsp
464 d1085492 2020-11-07 stsp /* Check that the key decrypted properly. */
465 d1085492 2020-11-07 stsp sr_crypto_calculate_check_hmac_sha1(sd->mds.mdd_crypto.scr_maskkey,
466 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_maskkey),
467 d1085492 2020-11-07 stsp (u_int8_t *)sd->mds.mdd_crypto.scr_key,
468 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_key),
469 d1085492 2020-11-07 stsp check_digest);
470 d1085492 2020-11-07 stsp if (memcmp(sd->mds.mdd_crypto.scr_meta->chk_hmac_sha1.sch_mac,
471 d1085492 2020-11-07 stsp check_digest, sizeof(check_digest)) != 0) {
472 d1085492 2020-11-07 stsp explicit_bzero(sd->mds.mdd_crypto.scr_key,
473 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_key));
474 d1085492 2020-11-07 stsp goto out;
475 d1085492 2020-11-07 stsp }
476 d1085492 2020-11-07 stsp
477 d1085492 2020-11-07 stsp rv = 0; /* Success */
478 d1085492 2020-11-07 stsp out:
479 d1085492 2020-11-07 stsp /* we don't need the mask key anymore */
480 d1085492 2020-11-07 stsp explicit_bzero(&sd->mds.mdd_crypto.scr_maskkey,
481 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_maskkey));
482 d1085492 2020-11-07 stsp
483 d1085492 2020-11-07 stsp explicit_bzero(check_digest, sizeof(check_digest));
484 d1085492 2020-11-07 stsp
485 d1085492 2020-11-07 stsp return rv;
486 d1085492 2020-11-07 stsp }
487 d1085492 2020-11-07 stsp
488 d1085492 2020-11-07 stsp int
489 d1085492 2020-11-07 stsp sr_crypto_create_keys(struct sr_discipline *sd)
490 d1085492 2020-11-07 stsp {
491 d1085492 2020-11-07 stsp
492 d1085492 2020-11-07 stsp DNPRINTF(SR_D_DIS, "%s: sr_crypto_create_keys\n",
493 d1085492 2020-11-07 stsp DEVNAME(sd->sd_sc));
494 d1085492 2020-11-07 stsp
495 d1085492 2020-11-07 stsp if (AES_MAXKEYBYTES < sizeof(sd->mds.mdd_crypto.scr_maskkey))
496 d1085492 2020-11-07 stsp return (1);
497 d1085492 2020-11-07 stsp
498 d1085492 2020-11-07 stsp /* XXX allow user to specify */
499 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta->scm_alg = SR_CRYPTOA_AES_XTS_256;
500 d1085492 2020-11-07 stsp
501 d1085492 2020-11-07 stsp /* generate crypto keys */
502 d1085492 2020-11-07 stsp arc4random_buf(sd->mds.mdd_crypto.scr_key,
503 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_key));
504 d1085492 2020-11-07 stsp
505 d1085492 2020-11-07 stsp /* Mask the disk keys. */
506 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta->scm_mask_alg = SR_CRYPTOM_AES_ECB_256;
507 d1085492 2020-11-07 stsp sr_crypto_encrypt((u_char *)sd->mds.mdd_crypto.scr_key,
508 d1085492 2020-11-07 stsp (u_char *)sd->mds.mdd_crypto.scr_meta->scm_key,
509 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_maskkey, sizeof(sd->mds.mdd_crypto.scr_key),
510 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta->scm_mask_alg);
511 d1085492 2020-11-07 stsp
512 d1085492 2020-11-07 stsp /* Prepare key decryption check code. */
513 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta->scm_check_alg = SR_CRYPTOC_HMAC_SHA1;
514 d1085492 2020-11-07 stsp sr_crypto_calculate_check_hmac_sha1(sd->mds.mdd_crypto.scr_maskkey,
515 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_maskkey),
516 d1085492 2020-11-07 stsp (u_int8_t *)sd->mds.mdd_crypto.scr_key,
517 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_key),
518 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta->chk_hmac_sha1.sch_mac);
519 d1085492 2020-11-07 stsp
520 d1085492 2020-11-07 stsp /* Erase the plaintext disk keys */
521 d1085492 2020-11-07 stsp explicit_bzero(sd->mds.mdd_crypto.scr_key,
522 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_key));
523 d1085492 2020-11-07 stsp
524 d1085492 2020-11-07 stsp #ifdef SR_DEBUG0
525 d1085492 2020-11-07 stsp sr_crypto_dumpkeys(sd);
526 d1085492 2020-11-07 stsp #endif
527 d1085492 2020-11-07 stsp
528 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta->scm_flags = SR_CRYPTOF_KEY |
529 d1085492 2020-11-07 stsp SR_CRYPTOF_KDFHINT;
530 d1085492 2020-11-07 stsp
531 d1085492 2020-11-07 stsp return (0);
532 d1085492 2020-11-07 stsp }
533 d1085492 2020-11-07 stsp
534 d1085492 2020-11-07 stsp int
535 d1085492 2020-11-07 stsp sr_crypto_change_maskkey(struct sr_discipline *sd,
536 d1085492 2020-11-07 stsp struct sr_crypto_kdfinfo *kdfinfo1, struct sr_crypto_kdfinfo *kdfinfo2)
537 d1085492 2020-11-07 stsp {
538 d1085492 2020-11-07 stsp u_char check_digest[SHA1_DIGEST_LENGTH];
539 d1085492 2020-11-07 stsp u_char *c, *p = NULL;
540 d1085492 2020-11-07 stsp size_t ksz;
541 d1085492 2020-11-07 stsp int rv = 1;
542 d1085492 2020-11-07 stsp
543 d1085492 2020-11-07 stsp DNPRINTF(SR_D_DIS, "%s: sr_crypto_change_maskkey\n",
544 d1085492 2020-11-07 stsp DEVNAME(sd->sd_sc));
545 d1085492 2020-11-07 stsp
546 d1085492 2020-11-07 stsp if (sd->mds.mdd_crypto.scr_meta->scm_check_alg != SR_CRYPTOC_HMAC_SHA1)
547 d1085492 2020-11-07 stsp goto out;
548 d1085492 2020-11-07 stsp
549 d1085492 2020-11-07 stsp c = (u_char *)sd->mds.mdd_crypto.scr_meta->scm_key;
550 d1085492 2020-11-07 stsp ksz = sizeof(sd->mds.mdd_crypto.scr_key);
551 d1085492 2020-11-07 stsp p = malloc(ksz, M_DEVBUF, M_WAITOK | M_CANFAIL | M_ZERO);
552 d1085492 2020-11-07 stsp if (p == NULL)
553 d1085492 2020-11-07 stsp goto out;
554 d1085492 2020-11-07 stsp
555 d1085492 2020-11-07 stsp if (sr_crypto_decrypt(c, p, kdfinfo1->maskkey, ksz,
556 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta->scm_mask_alg) == -1)
557 d1085492 2020-11-07 stsp goto out;
558 d1085492 2020-11-07 stsp
559 d1085492 2020-11-07 stsp #ifdef SR_DEBUG0
560 d1085492 2020-11-07 stsp sr_crypto_dumpkeys(sd);
561 d1085492 2020-11-07 stsp #endif
562 d1085492 2020-11-07 stsp
563 d1085492 2020-11-07 stsp sr_crypto_calculate_check_hmac_sha1(kdfinfo1->maskkey,
564 d1085492 2020-11-07 stsp sizeof(kdfinfo1->maskkey), p, ksz, check_digest);
565 d1085492 2020-11-07 stsp if (memcmp(sd->mds.mdd_crypto.scr_meta->chk_hmac_sha1.sch_mac,
566 d1085492 2020-11-07 stsp check_digest, sizeof(check_digest)) != 0) {
567 d1085492 2020-11-07 stsp sr_error(sd->sd_sc, "incorrect key or passphrase");
568 d1085492 2020-11-07 stsp rv = EPERM;
569 d1085492 2020-11-07 stsp goto out;
570 d1085492 2020-11-07 stsp }
571 d1085492 2020-11-07 stsp
572 d1085492 2020-11-07 stsp /* Copy new KDF hint to metadata, if supplied. */
573 d1085492 2020-11-07 stsp if (kdfinfo2->flags & SR_CRYPTOKDF_HINT) {
574 d1085492 2020-11-07 stsp if (kdfinfo2->genkdf.len >
575 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_meta->scm_kdfhint))
576 d1085492 2020-11-07 stsp goto out;
577 d1085492 2020-11-07 stsp explicit_bzero(sd->mds.mdd_crypto.scr_meta->scm_kdfhint,
578 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_meta->scm_kdfhint));
579 d1085492 2020-11-07 stsp memcpy(sd->mds.mdd_crypto.scr_meta->scm_kdfhint,
580 d1085492 2020-11-07 stsp &kdfinfo2->genkdf, kdfinfo2->genkdf.len);
581 d1085492 2020-11-07 stsp }
582 d1085492 2020-11-07 stsp
583 d1085492 2020-11-07 stsp /* Mask the disk keys. */
584 d1085492 2020-11-07 stsp c = (u_char *)sd->mds.mdd_crypto.scr_meta->scm_key;
585 d1085492 2020-11-07 stsp if (sr_crypto_encrypt(p, c, kdfinfo2->maskkey, ksz,
586 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta->scm_mask_alg) == -1)
587 d1085492 2020-11-07 stsp goto out;
588 d1085492 2020-11-07 stsp
589 d1085492 2020-11-07 stsp /* Prepare key decryption check code. */
590 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta->scm_check_alg = SR_CRYPTOC_HMAC_SHA1;
591 d1085492 2020-11-07 stsp sr_crypto_calculate_check_hmac_sha1(kdfinfo2->maskkey,
592 d1085492 2020-11-07 stsp sizeof(kdfinfo2->maskkey), (u_int8_t *)sd->mds.mdd_crypto.scr_key,
593 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_key), check_digest);
594 d1085492 2020-11-07 stsp
595 d1085492 2020-11-07 stsp /* Copy new encrypted key and HMAC to metadata. */
596 d1085492 2020-11-07 stsp memcpy(sd->mds.mdd_crypto.scr_meta->chk_hmac_sha1.sch_mac, check_digest,
597 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_meta->chk_hmac_sha1.sch_mac));
598 d1085492 2020-11-07 stsp
599 d1085492 2020-11-07 stsp rv = 0; /* Success */
600 d1085492 2020-11-07 stsp
601 d1085492 2020-11-07 stsp out:
602 d1085492 2020-11-07 stsp if (p) {
603 d1085492 2020-11-07 stsp explicit_bzero(p, ksz);
604 d1085492 2020-11-07 stsp free(p, M_DEVBUF, ksz);
605 d1085492 2020-11-07 stsp }
606 d1085492 2020-11-07 stsp
607 d1085492 2020-11-07 stsp explicit_bzero(check_digest, sizeof(check_digest));
608 d1085492 2020-11-07 stsp explicit_bzero(&kdfinfo1->maskkey, sizeof(kdfinfo1->maskkey));
609 d1085492 2020-11-07 stsp explicit_bzero(&kdfinfo2->maskkey, sizeof(kdfinfo2->maskkey));
610 d1085492 2020-11-07 stsp
611 d1085492 2020-11-07 stsp return (rv);
612 d1085492 2020-11-07 stsp }
613 d1085492 2020-11-07 stsp
614 d1085492 2020-11-07 stsp struct sr_chunk *
615 d1085492 2020-11-07 stsp sr_crypto_create_key_disk(struct sr_discipline *sd, dev_t dev)
616 d1085492 2020-11-07 stsp {
617 d1085492 2020-11-07 stsp struct sr_softc *sc = sd->sd_sc;
618 d1085492 2020-11-07 stsp struct sr_discipline *fakesd = NULL;
619 d1085492 2020-11-07 stsp struct sr_metadata *sm = NULL;
620 d1085492 2020-11-07 stsp struct sr_meta_chunk *km;
621 d1085492 2020-11-07 stsp struct sr_meta_opt_item *omi = NULL;
622 d1085492 2020-11-07 stsp struct sr_meta_keydisk *skm;
623 d1085492 2020-11-07 stsp struct sr_chunk *key_disk = NULL;
624 d1085492 2020-11-07 stsp struct disklabel label;
625 d1085492 2020-11-07 stsp struct vnode *vn;
626 d1085492 2020-11-07 stsp char devname[32];
627 d1085492 2020-11-07 stsp int c, part, open = 0;
628 d1085492 2020-11-07 stsp
629 d1085492 2020-11-07 stsp /*
630 d1085492 2020-11-07 stsp * Create a metadata structure on the key disk and store
631 d1085492 2020-11-07 stsp * keying material in the optional metadata.
632 d1085492 2020-11-07 stsp */
633 d1085492 2020-11-07 stsp
634 d1085492 2020-11-07 stsp sr_meta_getdevname(sc, dev, devname, sizeof(devname));
635 d1085492 2020-11-07 stsp
636 d1085492 2020-11-07 stsp /* Make sure chunk is not already in use. */
637 d1085492 2020-11-07 stsp c = sr_chunk_in_use(sc, dev);
638 d1085492 2020-11-07 stsp if (c != BIOC_SDINVALID && c != BIOC_SDOFFLINE) {
639 d1085492 2020-11-07 stsp sr_error(sc, "%s is already in use", devname);
640 d1085492 2020-11-07 stsp goto done;
641 d1085492 2020-11-07 stsp }
642 d1085492 2020-11-07 stsp
643 d1085492 2020-11-07 stsp /* Open device. */
644 d1085492 2020-11-07 stsp if (bdevvp(dev, &vn)) {
645 d1085492 2020-11-07 stsp sr_error(sc, "cannot open key disk %s", devname);
646 d1085492 2020-11-07 stsp goto done;
647 d1085492 2020-11-07 stsp }
648 d1085492 2020-11-07 stsp if (VOP_OPEN(vn, FREAD | FWRITE, NOCRED, curproc)) {
649 d1085492 2020-11-07 stsp DNPRINTF(SR_D_META,"%s: sr_crypto_create_key_disk cannot "
650 d1085492 2020-11-07 stsp "open %s\n", DEVNAME(sc), devname);
651 d1085492 2020-11-07 stsp vput(vn);
652 d1085492 2020-11-07 stsp goto done;
653 d1085492 2020-11-07 stsp }
654 d1085492 2020-11-07 stsp open = 1; /* close dev on error */
655 d1085492 2020-11-07 stsp
656 d1085492 2020-11-07 stsp /* Get partition details. */
657 d1085492 2020-11-07 stsp part = DISKPART(dev);
658 d1085492 2020-11-07 stsp if (VOP_IOCTL(vn, DIOCGDINFO, (caddr_t)&label,
659 d1085492 2020-11-07 stsp FREAD, NOCRED, curproc)) {
660 d1085492 2020-11-07 stsp DNPRINTF(SR_D_META, "%s: sr_crypto_create_key_disk ioctl "
661 d1085492 2020-11-07 stsp "failed\n", DEVNAME(sc));
662 d1085492 2020-11-07 stsp goto done;
663 d1085492 2020-11-07 stsp }
664 d1085492 2020-11-07 stsp if (label.d_partitions[part].p_fstype != FS_RAID) {
665 d1085492 2020-11-07 stsp sr_error(sc, "%s partition not of type RAID (%d)",
666 d1085492 2020-11-07 stsp devname, label.d_partitions[part].p_fstype);
667 d1085492 2020-11-07 stsp goto done;
668 d1085492 2020-11-07 stsp }
669 d1085492 2020-11-07 stsp
670 d1085492 2020-11-07 stsp /*
671 d1085492 2020-11-07 stsp * Create and populate chunk metadata.
672 d1085492 2020-11-07 stsp */
673 d1085492 2020-11-07 stsp
674 d1085492 2020-11-07 stsp key_disk = malloc(sizeof(struct sr_chunk), M_DEVBUF, M_WAITOK | M_ZERO);
675 d1085492 2020-11-07 stsp km = &key_disk->src_meta;
676 d1085492 2020-11-07 stsp
677 d1085492 2020-11-07 stsp key_disk->src_dev_mm = dev;
678 d1085492 2020-11-07 stsp key_disk->src_vn = vn;
679 d1085492 2020-11-07 stsp strlcpy(key_disk->src_devname, devname, sizeof(km->scmi.scm_devname));
680 d1085492 2020-11-07 stsp key_disk->src_size = 0;
681 d1085492 2020-11-07 stsp
682 d1085492 2020-11-07 stsp km->scmi.scm_volid = sd->sd_meta->ssdi.ssd_level;
683 d1085492 2020-11-07 stsp km->scmi.scm_chunk_id = 0;
684 d1085492 2020-11-07 stsp km->scmi.scm_size = 0;
685 d1085492 2020-11-07 stsp km->scmi.scm_coerced_size = 0;
686 d1085492 2020-11-07 stsp strlcpy(km->scmi.scm_devname, devname, sizeof(km->scmi.scm_devname));
687 d1085492 2020-11-07 stsp memcpy(&km->scmi.scm_uuid, &sd->sd_meta->ssdi.ssd_uuid,
688 d1085492 2020-11-07 stsp sizeof(struct sr_uuid));
689 d1085492 2020-11-07 stsp
690 d1085492 2020-11-07 stsp sr_checksum(sc, km, &km->scm_checksum,
691 d1085492 2020-11-07 stsp sizeof(struct sr_meta_chunk_invariant));
692 d1085492 2020-11-07 stsp
693 d1085492 2020-11-07 stsp km->scm_status = BIOC_SDONLINE;
694 d1085492 2020-11-07 stsp
695 d1085492 2020-11-07 stsp /*
696 d1085492 2020-11-07 stsp * Create and populate our own discipline and metadata.
697 d1085492 2020-11-07 stsp */
698 d1085492 2020-11-07 stsp
699 d1085492 2020-11-07 stsp sm = malloc(sizeof(struct sr_metadata), M_DEVBUF, M_WAITOK | M_ZERO);
700 d1085492 2020-11-07 stsp sm->ssdi.ssd_magic = SR_MAGIC;
701 d1085492 2020-11-07 stsp sm->ssdi.ssd_version = SR_META_VERSION;
702 d1085492 2020-11-07 stsp sm->ssd_ondisk = 0;
703 d1085492 2020-11-07 stsp sm->ssdi.ssd_vol_flags = 0;
704 d1085492 2020-11-07 stsp memcpy(&sm->ssdi.ssd_uuid, &sd->sd_meta->ssdi.ssd_uuid,
705 d1085492 2020-11-07 stsp sizeof(struct sr_uuid));
706 d1085492 2020-11-07 stsp sm->ssdi.ssd_chunk_no = 1;
707 d1085492 2020-11-07 stsp sm->ssdi.ssd_volid = SR_KEYDISK_VOLID;
708 d1085492 2020-11-07 stsp sm->ssdi.ssd_level = SR_KEYDISK_LEVEL;
709 d1085492 2020-11-07 stsp sm->ssdi.ssd_size = 0;
710 d1085492 2020-11-07 stsp strlcpy(sm->ssdi.ssd_vendor, "OPENBSD", sizeof(sm->ssdi.ssd_vendor));
711 d1085492 2020-11-07 stsp snprintf(sm->ssdi.ssd_product, sizeof(sm->ssdi.ssd_product),
712 d1085492 2020-11-07 stsp "SR %s", "KEYDISK");
713 d1085492 2020-11-07 stsp snprintf(sm->ssdi.ssd_revision, sizeof(sm->ssdi.ssd_revision),
714 d1085492 2020-11-07 stsp "%03d", SR_META_VERSION);
715 d1085492 2020-11-07 stsp
716 d1085492 2020-11-07 stsp fakesd = malloc(sizeof(struct sr_discipline), M_DEVBUF,
717 d1085492 2020-11-07 stsp M_WAITOK | M_ZERO);
718 d1085492 2020-11-07 stsp fakesd->sd_sc = sd->sd_sc;
719 d1085492 2020-11-07 stsp fakesd->sd_meta = sm;
720 d1085492 2020-11-07 stsp fakesd->sd_meta_type = SR_META_F_NATIVE;
721 d1085492 2020-11-07 stsp fakesd->sd_vol_status = BIOC_SVONLINE;
722 d1085492 2020-11-07 stsp strlcpy(fakesd->sd_name, "KEYDISK", sizeof(fakesd->sd_name));
723 d1085492 2020-11-07 stsp SLIST_INIT(&fakesd->sd_meta_opt);
724 d1085492 2020-11-07 stsp
725 d1085492 2020-11-07 stsp /* Add chunk to volume. */
726 d1085492 2020-11-07 stsp fakesd->sd_vol.sv_chunks = malloc(sizeof(struct sr_chunk *), M_DEVBUF,
727 d1085492 2020-11-07 stsp M_WAITOK | M_ZERO);
728 d1085492 2020-11-07 stsp fakesd->sd_vol.sv_chunks[0] = key_disk;
729 d1085492 2020-11-07 stsp SLIST_INIT(&fakesd->sd_vol.sv_chunk_list);
730 d1085492 2020-11-07 stsp SLIST_INSERT_HEAD(&fakesd->sd_vol.sv_chunk_list, key_disk, src_link);
731 d1085492 2020-11-07 stsp
732 d1085492 2020-11-07 stsp /* Generate mask key. */
733 d1085492 2020-11-07 stsp arc4random_buf(sd->mds.mdd_crypto.scr_maskkey,
734 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_maskkey));
735 d1085492 2020-11-07 stsp
736 d1085492 2020-11-07 stsp /* Copy mask key to optional metadata area. */
737 d1085492 2020-11-07 stsp omi = malloc(sizeof(struct sr_meta_opt_item), M_DEVBUF,
738 d1085492 2020-11-07 stsp M_WAITOK | M_ZERO);
739 d1085492 2020-11-07 stsp omi->omi_som = malloc(sizeof(struct sr_meta_keydisk), M_DEVBUF,
740 d1085492 2020-11-07 stsp M_WAITOK | M_ZERO);
741 d1085492 2020-11-07 stsp omi->omi_som->som_type = SR_OPT_KEYDISK;
742 d1085492 2020-11-07 stsp omi->omi_som->som_length = sizeof(struct sr_meta_keydisk);
743 d1085492 2020-11-07 stsp skm = (struct sr_meta_keydisk *)omi->omi_som;
744 d1085492 2020-11-07 stsp memcpy(&skm->skm_maskkey, sd->mds.mdd_crypto.scr_maskkey,
745 d1085492 2020-11-07 stsp sizeof(skm->skm_maskkey));
746 d1085492 2020-11-07 stsp SLIST_INSERT_HEAD(&fakesd->sd_meta_opt, omi, omi_link);
747 d1085492 2020-11-07 stsp fakesd->sd_meta->ssdi.ssd_opt_no++;
748 d1085492 2020-11-07 stsp
749 d1085492 2020-11-07 stsp /* Save metadata. */
750 d1085492 2020-11-07 stsp if (sr_meta_save(fakesd, SR_META_DIRTY)) {
751 d1085492 2020-11-07 stsp sr_error(sc, "could not save metadata to %s", devname);
752 d1085492 2020-11-07 stsp goto fail;
753 d1085492 2020-11-07 stsp }
754 d1085492 2020-11-07 stsp
755 d1085492 2020-11-07 stsp goto done;
756 d1085492 2020-11-07 stsp
757 d1085492 2020-11-07 stsp fail:
758 d1085492 2020-11-07 stsp free(key_disk, M_DEVBUF, sizeof(struct sr_chunk));
759 d1085492 2020-11-07 stsp key_disk = NULL;
760 d1085492 2020-11-07 stsp
761 d1085492 2020-11-07 stsp done:
762 d1085492 2020-11-07 stsp free(omi, M_DEVBUF, sizeof(struct sr_meta_opt_item));
763 d1085492 2020-11-07 stsp if (fakesd && fakesd->sd_vol.sv_chunks)
764 d1085492 2020-11-07 stsp free(fakesd->sd_vol.sv_chunks, M_DEVBUF,
765 d1085492 2020-11-07 stsp sizeof(struct sr_chunk *));
766 d1085492 2020-11-07 stsp free(fakesd, M_DEVBUF, sizeof(struct sr_discipline));
767 d1085492 2020-11-07 stsp free(sm, M_DEVBUF, sizeof(struct sr_metadata));
768 d1085492 2020-11-07 stsp if (open) {
769 d1085492 2020-11-07 stsp VOP_CLOSE(vn, FREAD | FWRITE, NOCRED, curproc);
770 d1085492 2020-11-07 stsp vput(vn);
771 d1085492 2020-11-07 stsp }
772 d1085492 2020-11-07 stsp
773 d1085492 2020-11-07 stsp return key_disk;
774 d1085492 2020-11-07 stsp }
775 d1085492 2020-11-07 stsp
776 d1085492 2020-11-07 stsp struct sr_chunk *
777 d1085492 2020-11-07 stsp sr_crypto_read_key_disk(struct sr_discipline *sd, dev_t dev)
778 d1085492 2020-11-07 stsp {
779 d1085492 2020-11-07 stsp struct sr_softc *sc = sd->sd_sc;
780 d1085492 2020-11-07 stsp struct sr_metadata *sm = NULL;
781 d1085492 2020-11-07 stsp struct sr_meta_opt_item *omi, *omi_next;
782 d1085492 2020-11-07 stsp struct sr_meta_opt_hdr *omh;
783 d1085492 2020-11-07 stsp struct sr_meta_keydisk *skm;
784 d1085492 2020-11-07 stsp struct sr_meta_opt_head som;
785 d1085492 2020-11-07 stsp struct sr_chunk *key_disk = NULL;
786 d1085492 2020-11-07 stsp struct disklabel label;
787 d1085492 2020-11-07 stsp struct vnode *vn = NULL;
788 d1085492 2020-11-07 stsp char devname[32];
789 d1085492 2020-11-07 stsp int c, part, open = 0;
790 d1085492 2020-11-07 stsp
791 d1085492 2020-11-07 stsp /*
792 d1085492 2020-11-07 stsp * Load a key disk and load keying material into memory.
793 d1085492 2020-11-07 stsp */
794 d1085492 2020-11-07 stsp
795 d1085492 2020-11-07 stsp SLIST_INIT(&som);
796 d1085492 2020-11-07 stsp
797 d1085492 2020-11-07 stsp sr_meta_getdevname(sc, dev, devname, sizeof(devname));
798 d1085492 2020-11-07 stsp
799 d1085492 2020-11-07 stsp /* Make sure chunk is not already in use. */
800 d1085492 2020-11-07 stsp c = sr_chunk_in_use(sc, dev);
801 d1085492 2020-11-07 stsp if (c != BIOC_SDINVALID && c != BIOC_SDOFFLINE) {
802 d1085492 2020-11-07 stsp sr_error(sc, "%s is already in use", devname);
803 d1085492 2020-11-07 stsp goto done;
804 d1085492 2020-11-07 stsp }
805 d1085492 2020-11-07 stsp
806 d1085492 2020-11-07 stsp /* Open device. */
807 d1085492 2020-11-07 stsp if (bdevvp(dev, &vn)) {
808 d1085492 2020-11-07 stsp sr_error(sc, "cannot open key disk %s", devname);
809 d1085492 2020-11-07 stsp goto done;
810 d1085492 2020-11-07 stsp }
811 d1085492 2020-11-07 stsp if (VOP_OPEN(vn, FREAD, NOCRED, curproc)) {
812 d1085492 2020-11-07 stsp DNPRINTF(SR_D_META,"%s: sr_crypto_read_key_disk cannot "
813 d1085492 2020-11-07 stsp "open %s\n", DEVNAME(sc), devname);
814 d1085492 2020-11-07 stsp vput(vn);
815 d1085492 2020-11-07 stsp goto done;
816 d1085492 2020-11-07 stsp }
817 d1085492 2020-11-07 stsp open = 1; /* close dev on error */
818 d1085492 2020-11-07 stsp
819 d1085492 2020-11-07 stsp /* Get partition details. */
820 d1085492 2020-11-07 stsp part = DISKPART(dev);
821 d1085492 2020-11-07 stsp if (VOP_IOCTL(vn, DIOCGDINFO, (caddr_t)&label, FREAD,
822 d1085492 2020-11-07 stsp NOCRED, curproc)) {
823 d1085492 2020-11-07 stsp DNPRINTF(SR_D_META, "%s: sr_crypto_read_key_disk ioctl "
824 d1085492 2020-11-07 stsp "failed\n", DEVNAME(sc));
825 d1085492 2020-11-07 stsp goto done;
826 d1085492 2020-11-07 stsp }
827 d1085492 2020-11-07 stsp if (label.d_partitions[part].p_fstype != FS_RAID) {
828 d1085492 2020-11-07 stsp sr_error(sc, "%s partition not of type RAID (%d)",
829 d1085492 2020-11-07 stsp devname, label.d_partitions[part].p_fstype);
830 d1085492 2020-11-07 stsp goto done;
831 d1085492 2020-11-07 stsp }
832 d1085492 2020-11-07 stsp
833 d1085492 2020-11-07 stsp /*
834 d1085492 2020-11-07 stsp * Read and validate key disk metadata.
835 d1085492 2020-11-07 stsp */
836 d1085492 2020-11-07 stsp sm = malloc(SR_META_SIZE * DEV_BSIZE, M_DEVBUF, M_WAITOK | M_ZERO);
837 d1085492 2020-11-07 stsp if (sr_meta_native_read(sd, dev, sm, NULL)) {
838 d1085492 2020-11-07 stsp sr_error(sc, "native bootprobe could not read native metadata");
839 d1085492 2020-11-07 stsp goto done;
840 d1085492 2020-11-07 stsp }
841 d1085492 2020-11-07 stsp
842 d1085492 2020-11-07 stsp if (sr_meta_validate(sd, dev, sm, NULL)) {
843 d1085492 2020-11-07 stsp DNPRINTF(SR_D_META, "%s: invalid metadata\n",
844 d1085492 2020-11-07 stsp DEVNAME(sc));
845 d1085492 2020-11-07 stsp goto done;
846 d1085492 2020-11-07 stsp }
847 d1085492 2020-11-07 stsp
848 d1085492 2020-11-07 stsp /* Make sure this is a key disk. */
849 d1085492 2020-11-07 stsp if (sm->ssdi.ssd_level != SR_KEYDISK_LEVEL) {
850 d1085492 2020-11-07 stsp sr_error(sc, "%s is not a key disk", devname);
851 d1085492 2020-11-07 stsp goto done;
852 d1085492 2020-11-07 stsp }
853 d1085492 2020-11-07 stsp
854 d1085492 2020-11-07 stsp /* Construct key disk chunk. */
855 d1085492 2020-11-07 stsp key_disk = malloc(sizeof(struct sr_chunk), M_DEVBUF, M_WAITOK | M_ZERO);
856 d1085492 2020-11-07 stsp key_disk->src_dev_mm = dev;
857 d1085492 2020-11-07 stsp key_disk->src_vn = vn;
858 d1085492 2020-11-07 stsp key_disk->src_size = 0;
859 d1085492 2020-11-07 stsp
860 d1085492 2020-11-07 stsp memcpy(&key_disk->src_meta, (struct sr_meta_chunk *)(sm + 1),
861 d1085492 2020-11-07 stsp sizeof(key_disk->src_meta));
862 d1085492 2020-11-07 stsp
863 d1085492 2020-11-07 stsp /* Read mask key from optional metadata. */
864 d1085492 2020-11-07 stsp sr_meta_opt_load(sc, sm, &som);
865 d1085492 2020-11-07 stsp SLIST_FOREACH(omi, &som, omi_link) {
866 d1085492 2020-11-07 stsp omh = omi->omi_som;
867 d1085492 2020-11-07 stsp if (omh->som_type == SR_OPT_KEYDISK) {
868 d1085492 2020-11-07 stsp skm = (struct sr_meta_keydisk *)omh;
869 d1085492 2020-11-07 stsp memcpy(sd->mds.mdd_crypto.scr_maskkey, &skm->skm_maskkey,
870 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_maskkey));
871 d1085492 2020-11-07 stsp } else if (omh->som_type == SR_OPT_CRYPTO) {
872 d1085492 2020-11-07 stsp /* Original keydisk format with key in crypto area. */
873 d1085492 2020-11-07 stsp memcpy(sd->mds.mdd_crypto.scr_maskkey,
874 d1085492 2020-11-07 stsp omh + sizeof(struct sr_meta_opt_hdr),
875 d1085492 2020-11-07 stsp sizeof(sd->mds.mdd_crypto.scr_maskkey));
876 d1085492 2020-11-07 stsp }
877 d1085492 2020-11-07 stsp }
878 d1085492 2020-11-07 stsp
879 d1085492 2020-11-07 stsp open = 0;
880 d1085492 2020-11-07 stsp
881 d1085492 2020-11-07 stsp done:
882 d1085492 2020-11-07 stsp for (omi = SLIST_FIRST(&som); omi != NULL; omi = omi_next) {
883 d1085492 2020-11-07 stsp omi_next = SLIST_NEXT(omi, omi_link);
884 d1085492 2020-11-07 stsp free(omi->omi_som, M_DEVBUF, 0);
885 d1085492 2020-11-07 stsp free(omi, M_DEVBUF, sizeof(struct sr_meta_opt_item));
886 d1085492 2020-11-07 stsp }
887 d1085492 2020-11-07 stsp
888 d1085492 2020-11-07 stsp free(sm, M_DEVBUF, SR_META_SIZE * DEV_BSIZE);
889 d1085492 2020-11-07 stsp
890 d1085492 2020-11-07 stsp if (vn && open) {
891 d1085492 2020-11-07 stsp VOP_CLOSE(vn, FREAD, NOCRED, curproc);
892 d1085492 2020-11-07 stsp vput(vn);
893 d1085492 2020-11-07 stsp }
894 d1085492 2020-11-07 stsp
895 d1085492 2020-11-07 stsp return key_disk;
896 d1085492 2020-11-07 stsp }
897 d1085492 2020-11-07 stsp
898 d1085492 2020-11-07 stsp static void
899 d1085492 2020-11-07 stsp sr_crypto_free_sessions(struct sr_discipline *sd)
900 d1085492 2020-11-07 stsp {
901 d1085492 2020-11-07 stsp u_int i;
902 d1085492 2020-11-07 stsp
903 d1085492 2020-11-07 stsp for (i = 0; i < SR_CRYPTO_MAXKEYS; i++) {
904 d1085492 2020-11-07 stsp if (sd->mds.mdd_crypto.scr_sid[i] != (u_int64_t)-1) {
905 d1085492 2020-11-07 stsp crypto_freesession(sd->mds.mdd_crypto.scr_sid[i]);
906 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_sid[i] = (u_int64_t)-1;
907 d1085492 2020-11-07 stsp }
908 d1085492 2020-11-07 stsp }
909 d1085492 2020-11-07 stsp }
910 d1085492 2020-11-07 stsp
911 d1085492 2020-11-07 stsp int
912 d1085492 2020-11-07 stsp sr_crypto_alloc_resources(struct sr_discipline *sd)
913 d1085492 2020-11-07 stsp {
914 d1085492 2020-11-07 stsp struct sr_workunit *wu;
915 d1085492 2020-11-07 stsp struct sr_crypto_wu *crwu;
916 d1085492 2020-11-07 stsp struct cryptoini cri;
917 d1085492 2020-11-07 stsp u_int num_keys, i;
918 d1085492 2020-11-07 stsp
919 d1085492 2020-11-07 stsp DNPRINTF(SR_D_DIS, "%s: sr_crypto_alloc_resources\n",
920 d1085492 2020-11-07 stsp DEVNAME(sd->sd_sc));
921 d1085492 2020-11-07 stsp
922 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_alg = CRYPTO_AES_XTS;
923 d1085492 2020-11-07 stsp switch (sd->mds.mdd_crypto.scr_meta->scm_alg) {
924 d1085492 2020-11-07 stsp case SR_CRYPTOA_AES_XTS_128:
925 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_klen = 256;
926 d1085492 2020-11-07 stsp break;
927 d1085492 2020-11-07 stsp case SR_CRYPTOA_AES_XTS_256:
928 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_klen = 512;
929 d1085492 2020-11-07 stsp break;
930 d1085492 2020-11-07 stsp default:
931 d1085492 2020-11-07 stsp sr_error(sd->sd_sc, "unknown crypto algorithm");
932 d1085492 2020-11-07 stsp return (EINVAL);
933 d1085492 2020-11-07 stsp }
934 d1085492 2020-11-07 stsp
935 d1085492 2020-11-07 stsp for (i = 0; i < SR_CRYPTO_MAXKEYS; i++)
936 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_sid[i] = (u_int64_t)-1;
937 d1085492 2020-11-07 stsp
938 d1085492 2020-11-07 stsp if (sr_wu_alloc(sd)) {
939 d1085492 2020-11-07 stsp sr_error(sd->sd_sc, "unable to allocate work units");
940 d1085492 2020-11-07 stsp return (ENOMEM);
941 d1085492 2020-11-07 stsp }
942 d1085492 2020-11-07 stsp if (sr_ccb_alloc(sd)) {
943 d1085492 2020-11-07 stsp sr_error(sd->sd_sc, "unable to allocate CCBs");
944 d1085492 2020-11-07 stsp return (ENOMEM);
945 d1085492 2020-11-07 stsp }
946 d1085492 2020-11-07 stsp if (sr_crypto_decrypt_key(sd)) {
947 d1085492 2020-11-07 stsp sr_error(sd->sd_sc, "incorrect key or passphrase");
948 d1085492 2020-11-07 stsp return (EPERM);
949 d1085492 2020-11-07 stsp }
950 d1085492 2020-11-07 stsp
951 d1085492 2020-11-07 stsp /*
952 d1085492 2020-11-07 stsp * For each work unit allocate the uio, iovec and crypto structures.
953 d1085492 2020-11-07 stsp * These have to be allocated now because during runtime we cannot
954 d1085492 2020-11-07 stsp * fail an allocation without failing the I/O (which can cause real
955 d1085492 2020-11-07 stsp * problems).
956 d1085492 2020-11-07 stsp */
957 d1085492 2020-11-07 stsp TAILQ_FOREACH(wu, &sd->sd_wu, swu_next) {
958 d1085492 2020-11-07 stsp crwu = (struct sr_crypto_wu *)wu;
959 d1085492 2020-11-07 stsp crwu->cr_uio.uio_iov = &crwu->cr_iov;
960 d1085492 2020-11-07 stsp crwu->cr_dmabuf = dma_alloc(MAXPHYS, PR_WAITOK);
961 d1085492 2020-11-07 stsp crwu->cr_crp = crypto_getreq(MAXPHYS >> DEV_BSHIFT);
962 d1085492 2020-11-07 stsp if (crwu->cr_crp == NULL)
963 d1085492 2020-11-07 stsp return (ENOMEM);
964 d1085492 2020-11-07 stsp }
965 d1085492 2020-11-07 stsp
966 d1085492 2020-11-07 stsp memset(&cri, 0, sizeof(cri));
967 d1085492 2020-11-07 stsp cri.cri_alg = sd->mds.mdd_crypto.scr_alg;
968 d1085492 2020-11-07 stsp cri.cri_klen = sd->mds.mdd_crypto.scr_klen;
969 d1085492 2020-11-07 stsp
970 d1085492 2020-11-07 stsp /* Allocate a session for every 2^SR_CRYPTO_KEY_BLKSHIFT blocks. */
971 d1085492 2020-11-07 stsp num_keys = ((sd->sd_meta->ssdi.ssd_size - 1) >>
972 d1085492 2020-11-07 stsp SR_CRYPTO_KEY_BLKSHIFT) + 1;
973 d1085492 2020-11-07 stsp if (num_keys > SR_CRYPTO_MAXKEYS)
974 d1085492 2020-11-07 stsp return (EFBIG);
975 d1085492 2020-11-07 stsp for (i = 0; i < num_keys; i++) {
976 d1085492 2020-11-07 stsp cri.cri_key = sd->mds.mdd_crypto.scr_key[i];
977 d1085492 2020-11-07 stsp if (crypto_newsession(&sd->mds.mdd_crypto.scr_sid[i],
978 d1085492 2020-11-07 stsp &cri, 0) != 0) {
979 d1085492 2020-11-07 stsp sr_crypto_free_sessions(sd);
980 d1085492 2020-11-07 stsp return (EINVAL);
981 d1085492 2020-11-07 stsp }
982 d1085492 2020-11-07 stsp }
983 d1085492 2020-11-07 stsp
984 d1085492 2020-11-07 stsp sr_hotplug_register(sd, sr_crypto_hotplug);
985 d1085492 2020-11-07 stsp
986 d1085492 2020-11-07 stsp return (0);
987 d1085492 2020-11-07 stsp }
988 d1085492 2020-11-07 stsp
989 d1085492 2020-11-07 stsp void
990 d1085492 2020-11-07 stsp sr_crypto_free_resources(struct sr_discipline *sd)
991 d1085492 2020-11-07 stsp {
992 d1085492 2020-11-07 stsp struct sr_workunit *wu;
993 d1085492 2020-11-07 stsp struct sr_crypto_wu *crwu;
994 d1085492 2020-11-07 stsp
995 d1085492 2020-11-07 stsp DNPRINTF(SR_D_DIS, "%s: sr_crypto_free_resources\n",
996 d1085492 2020-11-07 stsp DEVNAME(sd->sd_sc));
997 d1085492 2020-11-07 stsp
998 d1085492 2020-11-07 stsp if (sd->mds.mdd_crypto.key_disk != NULL) {
999 d1085492 2020-11-07 stsp explicit_bzero(sd->mds.mdd_crypto.key_disk,
1000 d1085492 2020-11-07 stsp sizeof(*sd->mds.mdd_crypto.key_disk));
1001 d1085492 2020-11-07 stsp free(sd->mds.mdd_crypto.key_disk, M_DEVBUF,
1002 d1085492 2020-11-07 stsp sizeof(*sd->mds.mdd_crypto.key_disk));
1003 d1085492 2020-11-07 stsp }
1004 d1085492 2020-11-07 stsp
1005 d1085492 2020-11-07 stsp sr_hotplug_unregister(sd, sr_crypto_hotplug);
1006 d1085492 2020-11-07 stsp
1007 d1085492 2020-11-07 stsp sr_crypto_free_sessions(sd);
1008 d1085492 2020-11-07 stsp
1009 d1085492 2020-11-07 stsp TAILQ_FOREACH(wu, &sd->sd_wu, swu_next) {
1010 d1085492 2020-11-07 stsp crwu = (struct sr_crypto_wu *)wu;
1011 d1085492 2020-11-07 stsp if (crwu->cr_dmabuf)
1012 d1085492 2020-11-07 stsp dma_free(crwu->cr_dmabuf, MAXPHYS);
1013 d1085492 2020-11-07 stsp if (crwu->cr_crp)
1014 d1085492 2020-11-07 stsp crypto_freereq(crwu->cr_crp);
1015 d1085492 2020-11-07 stsp }
1016 d1085492 2020-11-07 stsp
1017 d1085492 2020-11-07 stsp sr_wu_free(sd);
1018 d1085492 2020-11-07 stsp sr_ccb_free(sd);
1019 d1085492 2020-11-07 stsp }
1020 d1085492 2020-11-07 stsp
1021 d1085492 2020-11-07 stsp int
1022 d1085492 2020-11-07 stsp sr_crypto_ioctl(struct sr_discipline *sd, struct bioc_discipline *bd)
1023 d1085492 2020-11-07 stsp {
1024 d1085492 2020-11-07 stsp struct sr_crypto_kdfpair kdfpair;
1025 d1085492 2020-11-07 stsp struct sr_crypto_kdfinfo kdfinfo1, kdfinfo2;
1026 d1085492 2020-11-07 stsp int size, rv = 1;
1027 d1085492 2020-11-07 stsp
1028 d1085492 2020-11-07 stsp DNPRINTF(SR_D_IOCTL, "%s: sr_crypto_ioctl %u\n",
1029 d1085492 2020-11-07 stsp DEVNAME(sd->sd_sc), bd->bd_cmd);
1030 d1085492 2020-11-07 stsp
1031 d1085492 2020-11-07 stsp switch (bd->bd_cmd) {
1032 d1085492 2020-11-07 stsp case SR_IOCTL_GET_KDFHINT:
1033 d1085492 2020-11-07 stsp
1034 d1085492 2020-11-07 stsp /* Get KDF hint for userland. */
1035 d1085492 2020-11-07 stsp size = sizeof(sd->mds.mdd_crypto.scr_meta->scm_kdfhint);
1036 d1085492 2020-11-07 stsp if (bd->bd_data == NULL || bd->bd_size > size)
1037 d1085492 2020-11-07 stsp goto bad;
1038 d1085492 2020-11-07 stsp if (copyout(sd->mds.mdd_crypto.scr_meta->scm_kdfhint,
1039 d1085492 2020-11-07 stsp bd->bd_data, bd->bd_size))
1040 d1085492 2020-11-07 stsp goto bad;
1041 d1085492 2020-11-07 stsp
1042 d1085492 2020-11-07 stsp rv = 0;
1043 d1085492 2020-11-07 stsp
1044 d1085492 2020-11-07 stsp break;
1045 d1085492 2020-11-07 stsp
1046 d1085492 2020-11-07 stsp case SR_IOCTL_CHANGE_PASSPHRASE:
1047 d1085492 2020-11-07 stsp
1048 d1085492 2020-11-07 stsp /* Attempt to change passphrase. */
1049 d1085492 2020-11-07 stsp
1050 d1085492 2020-11-07 stsp size = sizeof(kdfpair);
1051 d1085492 2020-11-07 stsp if (bd->bd_data == NULL || bd->bd_size > size)
1052 d1085492 2020-11-07 stsp goto bad;
1053 d1085492 2020-11-07 stsp if (copyin(bd->bd_data, &kdfpair, size))
1054 d1085492 2020-11-07 stsp goto bad;
1055 d1085492 2020-11-07 stsp
1056 d1085492 2020-11-07 stsp size = sizeof(kdfinfo1);
1057 d1085492 2020-11-07 stsp if (kdfpair.kdfinfo1 == NULL || kdfpair.kdfsize1 > size)
1058 d1085492 2020-11-07 stsp goto bad;
1059 d1085492 2020-11-07 stsp if (copyin(kdfpair.kdfinfo1, &kdfinfo1, size))
1060 d1085492 2020-11-07 stsp goto bad;
1061 d1085492 2020-11-07 stsp
1062 d1085492 2020-11-07 stsp size = sizeof(kdfinfo2);
1063 d1085492 2020-11-07 stsp if (kdfpair.kdfinfo2 == NULL || kdfpair.kdfsize2 > size)
1064 d1085492 2020-11-07 stsp goto bad;
1065 d1085492 2020-11-07 stsp if (copyin(kdfpair.kdfinfo2, &kdfinfo2, size))
1066 d1085492 2020-11-07 stsp goto bad;
1067 d1085492 2020-11-07 stsp
1068 d1085492 2020-11-07 stsp if (sr_crypto_change_maskkey(sd, &kdfinfo1, &kdfinfo2))
1069 d1085492 2020-11-07 stsp goto bad;
1070 d1085492 2020-11-07 stsp
1071 d1085492 2020-11-07 stsp /* Save metadata to disk. */
1072 d1085492 2020-11-07 stsp rv = sr_meta_save(sd, SR_META_DIRTY);
1073 d1085492 2020-11-07 stsp
1074 d1085492 2020-11-07 stsp break;
1075 d1085492 2020-11-07 stsp }
1076 d1085492 2020-11-07 stsp
1077 d1085492 2020-11-07 stsp bad:
1078 d1085492 2020-11-07 stsp explicit_bzero(&kdfpair, sizeof(kdfpair));
1079 d1085492 2020-11-07 stsp explicit_bzero(&kdfinfo1, sizeof(kdfinfo1));
1080 d1085492 2020-11-07 stsp explicit_bzero(&kdfinfo2, sizeof(kdfinfo2));
1081 d1085492 2020-11-07 stsp
1082 d1085492 2020-11-07 stsp return (rv);
1083 d1085492 2020-11-07 stsp }
1084 d1085492 2020-11-07 stsp
1085 d1085492 2020-11-07 stsp int
1086 d1085492 2020-11-07 stsp sr_crypto_meta_opt_handler(struct sr_discipline *sd, struct sr_meta_opt_hdr *om)
1087 d1085492 2020-11-07 stsp {
1088 d1085492 2020-11-07 stsp int rv = EINVAL;
1089 d1085492 2020-11-07 stsp
1090 d1085492 2020-11-07 stsp if (om->som_type == SR_OPT_CRYPTO) {
1091 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta = (struct sr_meta_crypto *)om;
1092 d1085492 2020-11-07 stsp rv = 0;
1093 d1085492 2020-11-07 stsp }
1094 d1085492 2020-11-07 stsp
1095 d1085492 2020-11-07 stsp return (rv);
1096 d1085492 2020-11-07 stsp }
1097 d1085492 2020-11-07 stsp
1098 d1085492 2020-11-07 stsp int
1099 d1085492 2020-11-07 stsp sr_crypto_rw(struct sr_workunit *wu)
1100 d1085492 2020-11-07 stsp {
1101 d1085492 2020-11-07 stsp struct sr_crypto_wu *crwu;
1102 d1085492 2020-11-07 stsp daddr_t blkno;
1103 d1085492 2020-11-07 stsp int rv = 0;
1104 d1085492 2020-11-07 stsp
1105 d1085492 2020-11-07 stsp DNPRINTF(SR_D_DIS, "%s: sr_crypto_rw wu %p\n",
1106 d1085492 2020-11-07 stsp DEVNAME(wu->swu_dis->sd_sc), wu);
1107 d1085492 2020-11-07 stsp
1108 d1085492 2020-11-07 stsp if (sr_validate_io(wu, &blkno, "sr_crypto_rw"))
1109 d1085492 2020-11-07 stsp return (1);
1110 d1085492 2020-11-07 stsp
1111 d1085492 2020-11-07 stsp if (wu->swu_xs->flags & SCSI_DATA_OUT) {
1112 d1085492 2020-11-07 stsp crwu = sr_crypto_prepare(wu, 1);
1113 d1085492 2020-11-07 stsp crwu->cr_crp->crp_callback = sr_crypto_write;
1114 d1085492 2020-11-07 stsp rv = crypto_dispatch(crwu->cr_crp);
1115 d1085492 2020-11-07 stsp if (rv == 0)
1116 d1085492 2020-11-07 stsp rv = crwu->cr_crp->crp_etype;
1117 d1085492 2020-11-07 stsp } else
1118 d1085492 2020-11-07 stsp rv = sr_crypto_dev_rw(wu, NULL);
1119 d1085492 2020-11-07 stsp
1120 d1085492 2020-11-07 stsp return (rv);
1121 d1085492 2020-11-07 stsp }
1122 d1085492 2020-11-07 stsp
1123 d1085492 2020-11-07 stsp void
1124 d1085492 2020-11-07 stsp sr_crypto_write(struct cryptop *crp)
1125 d1085492 2020-11-07 stsp {
1126 d1085492 2020-11-07 stsp struct sr_crypto_wu *crwu = crp->crp_opaque;
1127 d1085492 2020-11-07 stsp struct sr_workunit *wu = &crwu->cr_wu;
1128 d1085492 2020-11-07 stsp int s;
1129 d1085492 2020-11-07 stsp
1130 d1085492 2020-11-07 stsp DNPRINTF(SR_D_INTR, "%s: sr_crypto_write: wu %p xs: %p\n",
1131 d1085492 2020-11-07 stsp DEVNAME(wu->swu_dis->sd_sc), wu, wu->swu_xs);
1132 d1085492 2020-11-07 stsp
1133 d1085492 2020-11-07 stsp if (crp->crp_etype) {
1134 d1085492 2020-11-07 stsp /* fail io */
1135 d1085492 2020-11-07 stsp wu->swu_xs->error = XS_DRIVER_STUFFUP;
1136 d1085492 2020-11-07 stsp s = splbio();
1137 d1085492 2020-11-07 stsp sr_scsi_done(wu->swu_dis, wu->swu_xs);
1138 d1085492 2020-11-07 stsp splx(s);
1139 d1085492 2020-11-07 stsp }
1140 d1085492 2020-11-07 stsp
1141 d1085492 2020-11-07 stsp sr_crypto_dev_rw(wu, crwu);
1142 d1085492 2020-11-07 stsp }
1143 d1085492 2020-11-07 stsp
1144 d1085492 2020-11-07 stsp int
1145 d1085492 2020-11-07 stsp sr_crypto_dev_rw(struct sr_workunit *wu, struct sr_crypto_wu *crwu)
1146 d1085492 2020-11-07 stsp {
1147 d1085492 2020-11-07 stsp struct sr_discipline *sd = wu->swu_dis;
1148 d1085492 2020-11-07 stsp struct scsi_xfer *xs = wu->swu_xs;
1149 d1085492 2020-11-07 stsp struct sr_ccb *ccb;
1150 d1085492 2020-11-07 stsp struct uio *uio;
1151 d1085492 2020-11-07 stsp daddr_t blkno;
1152 d1085492 2020-11-07 stsp
1153 d1085492 2020-11-07 stsp blkno = wu->swu_blk_start;
1154 d1085492 2020-11-07 stsp
1155 d1085492 2020-11-07 stsp ccb = sr_ccb_rw(sd, 0, blkno, xs->datalen, xs->data, xs->flags, 0);
1156 d1085492 2020-11-07 stsp if (!ccb) {
1157 d1085492 2020-11-07 stsp /* should never happen but handle more gracefully */
1158 d1085492 2020-11-07 stsp printf("%s: %s: too many ccbs queued\n",
1159 d1085492 2020-11-07 stsp DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname);
1160 d1085492 2020-11-07 stsp goto bad;
1161 d1085492 2020-11-07 stsp }
1162 d1085492 2020-11-07 stsp if (!ISSET(xs->flags, SCSI_DATA_IN)) {
1163 d1085492 2020-11-07 stsp uio = crwu->cr_crp->crp_buf;
1164 d1085492 2020-11-07 stsp ccb->ccb_buf.b_data = uio->uio_iov->iov_base;
1165 d1085492 2020-11-07 stsp ccb->ccb_opaque = crwu;
1166 d1085492 2020-11-07 stsp }
1167 d1085492 2020-11-07 stsp sr_wu_enqueue_ccb(wu, ccb);
1168 d1085492 2020-11-07 stsp sr_schedule_wu(wu);
1169 d1085492 2020-11-07 stsp
1170 d1085492 2020-11-07 stsp return (0);
1171 d1085492 2020-11-07 stsp
1172 d1085492 2020-11-07 stsp bad:
1173 d1085492 2020-11-07 stsp /* wu is unwound by sr_wu_put */
1174 d1085492 2020-11-07 stsp if (crwu)
1175 d1085492 2020-11-07 stsp crwu->cr_crp->crp_etype = EINVAL;
1176 d1085492 2020-11-07 stsp return (1);
1177 d1085492 2020-11-07 stsp }
1178 d1085492 2020-11-07 stsp
1179 d1085492 2020-11-07 stsp void
1180 d1085492 2020-11-07 stsp sr_crypto_done(struct sr_workunit *wu)
1181 d1085492 2020-11-07 stsp {
1182 d1085492 2020-11-07 stsp struct scsi_xfer *xs = wu->swu_xs;
1183 d1085492 2020-11-07 stsp struct sr_crypto_wu *crwu;
1184 d1085492 2020-11-07 stsp int s;
1185 d1085492 2020-11-07 stsp
1186 d1085492 2020-11-07 stsp /* If this was a successful read, initiate decryption of the data. */
1187 d1085492 2020-11-07 stsp if (ISSET(xs->flags, SCSI_DATA_IN) && xs->error == XS_NOERROR) {
1188 d1085492 2020-11-07 stsp crwu = sr_crypto_prepare(wu, 0);
1189 d1085492 2020-11-07 stsp crwu->cr_crp->crp_callback = sr_crypto_read;
1190 d1085492 2020-11-07 stsp DNPRINTF(SR_D_INTR, "%s: sr_crypto_done: crypto_dispatch %p\n",
1191 d1085492 2020-11-07 stsp DEVNAME(wu->swu_dis->sd_sc), crwu->cr_crp);
1192 d1085492 2020-11-07 stsp crypto_dispatch(crwu->cr_crp);
1193 d1085492 2020-11-07 stsp return;
1194 d1085492 2020-11-07 stsp }
1195 d1085492 2020-11-07 stsp
1196 d1085492 2020-11-07 stsp s = splbio();
1197 d1085492 2020-11-07 stsp sr_scsi_done(wu->swu_dis, wu->swu_xs);
1198 d1085492 2020-11-07 stsp splx(s);
1199 d1085492 2020-11-07 stsp }
1200 d1085492 2020-11-07 stsp
1201 d1085492 2020-11-07 stsp void
1202 d1085492 2020-11-07 stsp sr_crypto_read(struct cryptop *crp)
1203 d1085492 2020-11-07 stsp {
1204 d1085492 2020-11-07 stsp struct sr_crypto_wu *crwu = crp->crp_opaque;
1205 d1085492 2020-11-07 stsp struct sr_workunit *wu = &crwu->cr_wu;
1206 d1085492 2020-11-07 stsp int s;
1207 d1085492 2020-11-07 stsp
1208 d1085492 2020-11-07 stsp DNPRINTF(SR_D_INTR, "%s: sr_crypto_read: wu %p xs: %p\n",
1209 d1085492 2020-11-07 stsp DEVNAME(wu->swu_dis->sd_sc), wu, wu->swu_xs);
1210 d1085492 2020-11-07 stsp
1211 d1085492 2020-11-07 stsp if (crp->crp_etype)
1212 d1085492 2020-11-07 stsp wu->swu_xs->error = XS_DRIVER_STUFFUP;
1213 d1085492 2020-11-07 stsp
1214 d1085492 2020-11-07 stsp s = splbio();
1215 d1085492 2020-11-07 stsp sr_scsi_done(wu->swu_dis, wu->swu_xs);
1216 d1085492 2020-11-07 stsp splx(s);
1217 d1085492 2020-11-07 stsp }
1218 d1085492 2020-11-07 stsp
1219 d1085492 2020-11-07 stsp void
1220 d1085492 2020-11-07 stsp sr_crypto_hotplug(struct sr_discipline *sd, struct disk *diskp, int action)
1221 d1085492 2020-11-07 stsp {
1222 d1085492 2020-11-07 stsp DNPRINTF(SR_D_MISC, "%s: sr_crypto_hotplug: %s %d\n",
1223 d1085492 2020-11-07 stsp DEVNAME(sd->sd_sc), diskp->dk_name, action);
1224 d1085492 2020-11-07 stsp }
1225 d1085492 2020-11-07 stsp
1226 d1085492 2020-11-07 stsp #ifdef SR_DEBUG0
1227 d1085492 2020-11-07 stsp void
1228 d1085492 2020-11-07 stsp sr_crypto_dumpkeys(struct sr_discipline *sd)
1229 d1085492 2020-11-07 stsp {
1230 d1085492 2020-11-07 stsp int i, j;
1231 d1085492 2020-11-07 stsp
1232 d1085492 2020-11-07 stsp printf("sr_crypto_dumpkeys:\n");
1233 d1085492 2020-11-07 stsp for (i = 0; i < SR_CRYPTO_MAXKEYS; i++) {
1234 d1085492 2020-11-07 stsp printf("\tscm_key[%d]: 0x", i);
1235 d1085492 2020-11-07 stsp for (j = 0; j < SR_CRYPTO_KEYBYTES; j++) {
1236 d1085492 2020-11-07 stsp printf("%02x",
1237 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_meta->scm_key[i][j]);
1238 d1085492 2020-11-07 stsp }
1239 d1085492 2020-11-07 stsp printf("\n");
1240 d1085492 2020-11-07 stsp }
1241 d1085492 2020-11-07 stsp printf("sr_crypto_dumpkeys: runtime data keys:\n");
1242 d1085492 2020-11-07 stsp for (i = 0; i < SR_CRYPTO_MAXKEYS; i++) {
1243 d1085492 2020-11-07 stsp printf("\tscr_key[%d]: 0x", i);
1244 d1085492 2020-11-07 stsp for (j = 0; j < SR_CRYPTO_KEYBYTES; j++) {
1245 d1085492 2020-11-07 stsp printf("%02x",
1246 d1085492 2020-11-07 stsp sd->mds.mdd_crypto.scr_key[i][j]);
1247 d1085492 2020-11-07 stsp }
1248 d1085492 2020-11-07 stsp printf("\n");
1249 d1085492 2020-11-07 stsp }
1250 d1085492 2020-11-07 stsp }
1251 d1085492 2020-11-07 stsp #endif /* SR_DEBUG */