newaetech / chipwhisperer

ChipWhisperer - the complete open-source toolchain for side-channel power analysis and glitching attacks
http://chipwhisperer.com
Other
1.12k stars 285 forks source link

USB Pipe Error during glitching #454

Open Elec332 opened 1 year ago

Elec332 commented 1 year ago

I ran into the following USB error while trying to find glitch settings with a (pretty standard setup) GlitchController

USBErrorPipe                              Traceback (most recent call last)
File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\hardware\naeusb\naeusb.py:572, in NAEUSB_Backend._cmd_ctrl_send_data(self, pload, cmd)
    571 try:
--> 572     self.sendCtrl(cmd, data=pload)
    573 except usb1.USBErrorPipe:

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\hardware\naeusb\naeusb.py:526, in NAEUSB_Backend.sendCtrl(self, cmd, value, data)
    525     naeusb_logger.error("The naeusb fw ctrl buffer is 128 bytes, but len(data) > 128. If you get a pipe error, this is why.")
--> 526 self.handle.controlWrite(0x41, cmd, value, 0, data, timeout=self._timeout)

File ~\CHIPWH~1\cw\home\portable\WPy64-31080\python-3.10.8.amd64\lib\site-packages\usb1\__init__.py:1330, in USBDeviceHandle.controlWrite(self, request_type, request, value, index, data, timeout)
   1329 data, _ = create_initialised_buffer(data)
-> 1330 return self._controlTransfer(request_type, request, value, index, data,
   1331                              sizeof(data), timeout)

File ~\CHIPWH~1\cw\home\portable\WPy64-31080\python-3.10.8.amd64\lib\site-packages\usb1\__init__.py:1307, in USBDeviceHandle._controlTransfer(self, request_type, request, value, index, data, length, timeout)
   1303 result = libusb1.libusb_control_transfer(
   1304     self.__handle, request_type, request, value, index, data, length,
   1305     timeout,
   1306 )
-> 1307 mayRaiseUSBError(result)
   1308 return result

File ~\CHIPWH~1\cw\home\portable\WPy64-31080\python-3.10.8.amd64\lib\site-packages\usb1\__init__.py:127, in mayRaiseUSBError(value, __raiseUSBError)
    126 if value < 0:
--> 127     __raiseUSBError(value)
    128 return value

File ~\CHIPWH~1\cw\home\portable\WPy64-31080\python-3.10.8.amd64\lib\site-packages\usb1\__init__.py:119, in raiseUSBError(value, __STATUS_TO_EXCEPTION_DICT, __USBError)
    113 def raiseUSBError(
    114         value,
    115         # Avoid globals lookup on call to work during interpreter shutdown.
    116         __STATUS_TO_EXCEPTION_DICT=STATUS_TO_EXCEPTION_DICT,
    117         __USBError=USBError,
    118     ): # pylint: disable=dangerous-default-value
--> 119     raise __STATUS_TO_EXCEPTION_DICT.get(value, __USBError)(value)

USBErrorPipe: LIBUSB_ERROR_PIPE [-9]

During handling of the above exception, another exception occurred:

USBErrorPipe                              Traceback (most recent call last)
Cell In[23], line 28
     26 broken = False
     27 for glitch_setting in gc.glitch_values():
---> 28     reset_target()
     29     scope.glitch.offset = glitch_setting[1]
     30     scope.glitch.width = glitch_setting[0]

Cell In[4], line 5, in reset_target()
      3 scope.io.nrst = False
      4 time.sleep(0.05)
----> 5 scope.io.nrst = True
      6 time.sleep(0.05)
      7 target.flush()

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\common\utils\util.py:397, in DisableNewAttr.__setattr__(self, name, value)
    396 def __setattr__(self, name, value):
--> 397     if hasattr(self, '_new_attributes_disabled') and self._new_attributes_disabled and not hasattr(self, name):  # would this create a new attribute?
    398         #raise AttributeError("Attempt to set unknown attribute in %s"%self.__class__, name)
    399         other_logger.error("Setting unknown attribute {} in {}".format(name, self.__class__))
    400         if hasattr(self, '_new_attributes_disabled_strict') and self._new_attributes_disabled_strict and not hasattr(self, name):

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\capture\scopes\cwhardware\ChipWhispererExtra.py:536, in GPIOSettings.nrst(self)
    531 @property
    532 def nrst(self):
    533     """The state of the NRST pin.
    534 
    535     See pdic for more information."""
--> 536     return self._getGpio(100)

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\capture\scopes\cwhardware\ChipWhispererExtra.py:544, in GPIOSettings._getGpio(self, pinnum)
    542 def _getGpio(self, pinnum):
    543     """GPIO state getter for GPIO settings on 1-4 and for special pins"""
--> 544     state = self.cwe.getGPIOState(pinnum)
    545     if state is None:
    546         return "high_z"

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\capture\scopes\cwhardware\ChipWhispererExtra.py:1381, in CWExtraSettings.getGPIOState(self, IONumber)
   1380 def getGPIOState(self, IONumber):
-> 1381     data = self.oa.sendMessage(CODE_READ, ADDR_IOROUTE, Validate=False, maxResp=8)
   1383     #Catch special modes
   1384     if IONumber >= 100:

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\capture\scopes\_OpenADCInterface.py:294, in OpenADCInterface.sendMessage(self, mode, address, payload, Validate, maxResp, readMask)
    289 """Top level swiss-army knife method for sending a message.  Kept for back-compat
    290 
    291 TODO: Transition API calls to use simplified API and remove this function...
    292 """
    293 if mode != CODE_WRITE:
--> 294     return self.msg_read(address, maxResp)
    295 self.msg_write(address, payload)
    296 if Validate:

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\capture\scopes\_OpenADCInterface.py:231, in OpenADCInterface.msg_read(self, address, max_resp)
    228         scope_logger.warning("The error trace below will show you what led to this:")
    229         raise ValueError
--> 231     data = self.serial.cmdReadMem(address, max_resp)
    232 else:
    233     self._send_msg_hdr(CODE_READ, address, 0)

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\hardware\naeusb\naeusb.py:864, in NAEUSB.cmdReadMem(self, addr, dlen)
    858 def cmdReadMem(self, addr : int, dlen : int) -> bytearray:
    859     """
    860     Send command to read over external memory interface from FPGA. Automatically
    861     decides to use control-transfer or bulk-endpoint transfer based on data length.
    862     """
--> 864     return self.usbserializer.cmdReadMem(addr, dlen)

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\hardware\naeusb\naeusb.py:610, in NAEUSB_Backend.cmdReadMem(self, addr, dlen)
    608 dlen = int(dlen)
    609 if dlen < NAEUSB_CTRL_IO_THRESHOLD:
--> 610     data = self._cmd_readmem_ctrl(addr, dlen)
    611 else:
    612     data = self._cmd_readmem_bulk(addr, dlen)

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\hardware\naeusb\naeusb.py:591, in NAEUSB_Backend._cmd_readmem_ctrl(self, addr, dlen)
    585 def _cmd_readmem_ctrl(self, addr : int, dlen : int):
    586     """Reads data from the external memory interface over the control-transfer endpoint.
    587 
    588     Returns:
    589         The received data.
    590     """
--> 591     self._cmd_ctrl_send_header(addr, dlen, self.CMD_READMEM_CTRL);
    592     return self.readCtrl(self.CMD_READMEM_CTRL, dlen=dlen)

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\hardware\naeusb\naeusb.py:583, in NAEUSB_Backend._cmd_ctrl_send_header(self, addr, dlen, cmd)
    581 # TODO: Alloc header class member? Won't hafta alloc mem every read and writectrl call...
    582 pload = make_len_addr(dlen, addr)
--> 583 self._cmd_ctrl_send_data(pload, cmd)

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\hardware\naeusb\naeusb.py:575, in NAEUSB_Backend._cmd_ctrl_send_data(self, pload, cmd)
    573 except usb1.USBErrorPipe:
    574     naeusb_logger.info("Attempting pipe error fix - typically safe to ignore")
--> 575     self.sendCtrl(0x22, 0x11)
    576     self.sendCtrl(cmd, data=pload)

File c:\users\user\chipwhisperer5_64\cw\home\portable\chipwhisperer\software\chipwhisperer\hardware\naeusb\naeusb.py:526, in NAEUSB_Backend.sendCtrl(self, cmd, value, data)
    524 if len(data) > NAEUSB_CTRL_IO_MAX:
    525     naeusb_logger.error("The naeusb fw ctrl buffer is 128 bytes, but len(data) > 128. If you get a pipe error, this is why.")
--> 526 self.handle.controlWrite(0x41, cmd, value, 0, data, timeout=self._timeout)

File ~\CHIPWH~1\cw\home\portable\WPy64-31080\python-3.10.8.amd64\lib\site-packages\usb1\__init__.py:1330, in USBDeviceHandle.controlWrite(self, request_type, request, value, index, data, timeout)
   1328 # pylint: enable=undefined-variable
   1329 data, _ = create_initialised_buffer(data)
-> 1330 return self._controlTransfer(request_type, request, value, index, data,
   1331                              sizeof(data), timeout)

File ~\CHIPWH~1\cw\home\portable\WPy64-31080\python-3.10.8.amd64\lib\site-packages\usb1\__init__.py:1307, in USBDeviceHandle._controlTransfer(self, request_type, request, value, index, data, length, timeout)
   1301 def _controlTransfer(
   1302         self, request_type, request, value, index, data, length, timeout):
   1303     result = libusb1.libusb_control_transfer(
   1304         self.__handle, request_type, request, value, index, data, length,
   1305         timeout,
   1306     )
-> 1307     mayRaiseUSBError(result)
   1308     return result

File ~\CHIPWH~1\cw\home\portable\WPy64-31080\python-3.10.8.amd64\lib\site-packages\usb1\__init__.py:127, in mayRaiseUSBError(value, __raiseUSBError)
    121 def mayRaiseUSBError(
    122         value,
    123         # Avoid globals lookup on call to work during interpreter shutdown.
    124         __raiseUSBError=raiseUSBError,
    125     ):
    126     if value < 0:
--> 127         __raiseUSBError(value)
    128     return value

`File ~\CHIPWH~1\cw\home\portable\WPy64-31080\python-3.10.8.amd64\lib\site-packages\usb1\__init__.py:119, in raiseUSBError(value, __STATUS_TO_EXCEPTION_DICT, __USBError)
    113 def raiseUSBError(
    114         value,
    115         # Avoid globals lookup on call to work during interpreter shutdown.
    116         __STATUS_TO_EXCEPTION_DICT=STATUS_TO_EXCEPTION_DICT,
    117         __USBError=USBError,
    118     ): # pylint: disable=dangerous-default-value
--> 119     raise __STATUS_TO_EXCEPTION_DICT.get(value, __USBError)(value)

USBErrorPipe: LIBUSB_ERROR_PIPE [-9]

Loop that caused the crash:

for glitch_settings in gc.glitch_values():
    scope.glitch.width = glitch_settings[0]
    scope.glitch.offset = glitch_settings[1]
    scope.glitch.ext_offset = glitch_settings[2]

    if scope.adc.state:
        gc.add("reset")
        reboot_flush()
    scope.arm()
    target.simpleserial_write('p', bytearray([0, 0, 0, 0, 0]))
    ret = scope.capture()

    if ret:
        gc.add("reset")
        reboot_flush()
    else:
        val = target.simpleserial_read_witherrors('r', 1, glitch_timeout=10, timeout=50)
        if val['valid'] is False:
            gc.add("reset")
        else:
            if val['payload'][0] == 1:
                gc.add("success")
            else:
                gc.add("normal")

(Apologies for the bad codeblock formatting, Ill fix that laterTM)

jpcrypt commented 1 year ago

I suspect this may be related to your other issue? Prior to what causes the error, what does scope.sc._fast_fifo_read_active return? If it's True, then it's tied to the other issue.