Blame


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