25 #include <linux/types.h>
26 #include <linux/slab.h>
27 #include <linux/usb/ch9.h>
28 #include <linux/random.h>
29 #include <linux/export.h>
32 static void wusbhc_set_gtk_callback(
struct urb *
urb);
37 wusbhc->gtk.
descr.bLength =
sizeof(wusbhc->gtk.
descr) +
sizeof(wusbhc->gtk.
data);
39 wusbhc->gtk.
descr.bReserved = 0;
73 if (wusb_dev ==
NULL) {
77 tkid = &wusb_port_by_idx(wusbhc, wusb_dev->
port_idx)->ptk_tkid;
78 addr = wusb_dev->
addr & 0x7f;
81 *tkid = (addr << 8) | ((*tkid + 1) & 0xff);
86 static void wusbhc_generate_gtk(
struct wusbhc *wusbhc)
91 tkid = wusbhc_next_tkid(wusbhc,
NULL);
93 wusbhc->gtk.
descr.tTKID[0] = (tkid >> 0) & 0xff;
94 wusbhc->gtk.
descr.tTKID[1] = (tkid >> 8) & 0xff;
95 wusbhc->gtk.
descr.tTKID[2] = (tkid >> 16) & 0xff;
113 wusbhc_generate_gtk(wusbhc);
116 &wusbhc->gtk.
descr.bKeyData, key_size);
118 dev_err(wusbhc->
dev,
"cannot set GTK for the host: %d\n",
144 default:
return "unknown";
155 static int wusb_dev_set_encryption(
struct usb_device *usb_dev,
int value)
159 struct wusb_dev *wusb_dev = usb_dev->wusb_dev;
162 value = wusb_dev->
ccm1_etd.bEncryptionValue;
171 value, 0,
NULL, 0, 1000 );
173 dev_err(dev,
"Can't set device's WUSB encryption to "
174 "%s (value %d): %d\n",
176 wusb_dev->
ccm1_etd.bEncryptionValue, result);
185 static int wusb_dev_set_gtk(
struct wusbhc *wusbhc,
struct wusb_dev *wusb_dev)
187 struct usb_device *usb_dev = wusb_dev->
usb_dev;
190 usb_dev, usb_sndctrlpipe(usb_dev, 0),
194 &wusbhc->gtk.
descr, wusbhc->gtk.
descr.bLength,
201 struct usb_device *usb_dev,
struct wusb_dev *wusb_dev)
204 struct device *dev = &usb_dev->dev;
207 const void *itr, *
top;
217 0, secd,
sizeof(*secd));
218 if (result <
sizeof(*secd)) {
219 dev_err(dev,
"Can't read security descriptor or "
220 "not enough data: %d\n", result);
225 if (new_secd ==
NULL) {
226 dev_err(dev,
"Can't allocate space for security descriptors\n");
232 if (result < secd_size) {
233 dev_err(dev,
"Can't read security descriptor or "
234 "not enough data: %d\n", result);
239 top = (
void *)secd + result;
242 if (top - itr <
sizeof(*etd)) {
243 dev_err(dev,
"BUG: bad device security descriptor; "
244 "not enough data (%zu vs %zu bytes left)\n",
245 top - itr,
sizeof(*etd));
248 if (etd->
bLength <
sizeof(*etd)) {
249 dev_err(dev,
"BUG: bad device encryption descriptor; "
250 "descriptor is too short "
251 "(%u vs %zu needed)\n",
256 bytes +=
snprintf(buf + bytes,
sizeof(buf) - bytes,
266 if (ccm1_etd ==
NULL) {
267 dev_err(dev,
"WUSB device doesn't support CCM1 encryption, "
273 dev_dbg(dev,
"supported encryption: %s; using %s (0x%02x/%02x)\n",
299 struct usb_device *usb_dev = wusb_dev->
usb_dev;
300 struct device *dev = &usb_dev->dev;
301 u8 new_address = wusb_dev->
addr & 0x7F;
306 0, 0,
NULL, 0, 1000 );
308 dev_err(dev,
"auth failed: can't set address 0: %d\n",
321 new_address, 0,
NULL, 0,
324 dev_err(dev,
"auth failed: can't set address %u: %d\n",
325 new_address, result);
333 usb_dev->authenticated = 1;
348 struct usb_device *usb_dev = wusb_dev->
usb_dev;
349 struct device *dev = &usb_dev->dev;
360 dev_err(dev,
"can't allocate handshake data\n");
366 result = wusb_dev_set_encryption(usb_dev, 1);
368 goto error_dev_set_encryption;
370 tkid = wusbhc_next_tkid(wusbhc, wusb_dev);
373 hs[0].bMessageNumber = 1;
375 memcpy(hs[0].
tTKID, &tkid_le,
sizeof(hs[0].tTKID));
382 usb_dev, usb_sndctrlpipe(usb_dev, 0),
385 1, 0, &hs[0],
sizeof(hs[0]), 1000 );
387 dev_err(dev,
"Handshake1: request failed: %d\n", result);
393 usb_dev, usb_rcvctrlpipe(usb_dev, 0),
396 2, 0, &hs[1],
sizeof(hs[1]), 1000 );
398 dev_err(dev,
"Handshake2: request failed: %d\n", result);
404 dev_err(dev,
"Handshake2 failed: bad message number %u\n",
409 dev_err(dev,
"Handshake2 failed: bad status %u\n",
413 if (
memcmp(hs[0].tTKID, hs[1].tTKID,
sizeof(hs[0].tTKID))) {
414 dev_err(dev,
"Handshake2 failed: TKID mismatch "
415 "(#1 0x%02x%02x%02x vs #2 0x%02x%02x%02x)\n",
416 hs[0].tTKID[0], hs[0].tTKID[1], hs[0].tTKID[2],
417 hs[1].tTKID[0], hs[1].tTKID[1], hs[1].tTKID[2]);
420 if (
memcmp(hs[0].CDID, hs[1].CDID,
sizeof(hs[0].CDID))) {
421 dev_err(dev,
"Handshake2 failed: CDID mismatch\n");
435 result = wusb_key_derive(&keydvt_out, ck->
data, &ccm_n, &keydvt_in);
437 dev_err(dev,
"Handshake2 failed: cannot derive keys: %d\n",
443 result = wusb_oob_mic(mic, keydvt_out.
kck, &ccm_n, &hs[1]);
445 dev_err(dev,
"Handshake2 failed: cannot compute MIC: %d\n",
450 if (
memcmp(hs[1].MIC, mic,
sizeof(hs[1].MIC))) {
451 dev_err(dev,
"Handshake2 failed: MIC mismatch\n");
456 hs[2].bMessageNumber = 3;
458 memcpy(hs[2].tTKID, &tkid_le,
sizeof(hs[2].tTKID));
460 memcpy(hs[2].CDID, &wusb_dev->
cdid,
sizeof(hs[2].CDID));
461 memcpy(hs[2].nonce, hs[0].nonce,
sizeof(hs[2].nonce));
462 result = wusb_oob_mic(hs[2].MIC, keydvt_out.
kck, &ccm_n, &hs[2]);
464 dev_err(dev,
"Handshake3 failed: cannot compute MIC: %d\n",
470 usb_dev, usb_sndctrlpipe(usb_dev, 0),
473 3, 0, &hs[2],
sizeof(hs[2]), 1000 );
475 dev_err(dev,
"Handshake3: request failed: %d\n", result);
480 keydvt_out.
ptk,
sizeof(keydvt_out.
ptk));
482 goto error_wusbhc_set_ptk;
484 result = wusb_dev_set_gtk(wusbhc, wusb_dev);
486 dev_err(dev,
"Set GTK for device: request failed: %d\n",
488 goto error_wusbhc_set_gtk;
492 if (usb_dev->authenticated == 0) {
495 goto error_dev_update_address;
498 dev_info(dev,
"device authenticated\n");
500 error_dev_update_address:
501 error_wusbhc_set_gtk:
502 error_wusbhc_set_ptk:
506 memset(hs, 0, 3*
sizeof(hs[0]));
507 memset(&keydvt_out, 0,
sizeof(keydvt_out));
508 memset(&keydvt_in, 0,
sizeof(keydvt_in));
509 memset(&ccm_n, 0,
sizeof(ccm_n));
510 memset(mic, 0,
sizeof(mic));
512 wusb_dev_set_encryption(usb_dev, 0);
513 error_dev_set_encryption:
536 static void wusbhc_set_gtk_callback(
struct urb *
urb)
538 struct wusbhc *wusbhc = urb->context;
556 static const size_t key_size =
sizeof(wusbhc->gtk.
data);
559 wusbhc_generate_gtk(wusbhc);
561 for (p = 0; p < wusbhc->
ports_max; p++) {
562 struct wusb_dev *wusb_dev;
564 wusb_dev = wusbhc->
port[
p].wusb_dev;
565 if (!wusb_dev || !wusb_dev->
usb_dev || !wusb_dev->
usb_dev->authenticated)
569 usb_sndctrlpipe(wusb_dev->
usb_dev, 0),
571 &wusbhc->gtk.
descr, wusbhc->gtk.
descr.bLength,
572 wusbhc_set_gtk_callback, wusbhc);