def getAndSetSessionNone(self):
     # read the nonce
     rawNonce = self.core.ble.readCharacteristicWithoutEncryption(CSServices.CrownstoneService, CrownstoneCharacteristics.SessionNonce)
     
     # decrypt it
     decryptedNonce = EncryptionHandler.decryptSessionNonce(rawNonce, self.core.settings.guestKey)
     
     # load into the settings object
     self.core.settings.setSessionNonce(decryptedNonce)
 def test_switchPacket(self):
     rawData = [0, 0, 1, 0, 100]
     sessionNonce = Conversion.string_to_uint8_array("12345")
     encryptionData = EncryptionHandler.encryptCTR(
         rawData, [128, 128, 128], sessionNonce,
         Conversion.string_to_uint8_array("AdminKeyOf16Byte"))
     self.assertEqual(
         encryptionData,
         bytes([
             171, 199, 94, 51, 230, 26, 253, 144, 182, 105, 56, 210, 94,
             165, 184, 243
         ]))
    def test_switchPacketHexKey(self):
        rawData = [0, 0, 100, 0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        key = Conversion.ascii_or_hex_string_to_16_byte_array(
            "9e34c5a7da5c2b8d36e9fc5cf7497a6b")
        expectedKey = [
            158, 52, 197, 167, 218, 92, 43, 141, 54, 233, 252, 92, 247, 73,
            122, 107
        ]

        self.assertEqual(key, bytes(expectedKey))

        encryptionData = EncryptionHandler.encryptECB(rawData, key)

        encryptedDataExpected = [
            115, 162, 142, 55, 70, 208, 171, 55, 225, 152, 108, 53, 144, 169,
            172, 179
        ]

        decryptedData = EncryptionHandler.decryptECB(encryptionData, key)

        self.assertEqual(encryptionData, bytes(encryptedDataExpected))
        self.assertEqual(decryptedData, bytes(rawData))
Beispiel #4
0
    def decrypt(self, keyHexString):
        if self.validData and len(
                self.encryptedData) == 16 and len(keyHexString) >= 16:
            if not self.setupMode:
                result = EncryptionHandler.decryptECB(self.encryptedData,
                                                      keyHexString)

                for i in range(0, len(self.encryptedData)):
                    self.data[i + self.encryptedDataStartIndex] = result[i]

                self.parse()
            self.dataReadyForUse = True
        else:
            self.dataReadyForUse = False
    def decrypt(self, keyHexString):
        if len(self.data) == 17:
            encryptedArray = []
            self.opCode = self.data[0]

            for i in range(1, len(self.data)):
                encryptedArray.append(self.data[i])

            result = EncryptionHandler.decryptECB(encryptedArray, keyHexString)

            self.data = []
            self.data.append(self.opCode)

            self.data += result

            self.parse()
        else:
            print("Invalid data length")
Beispiel #6
0
 def readCharacteristic(self, serviceUUID, characteristicUUID):
     data = self.readCharacteristicWithoutEncryption(serviceUUID, characteristicUUID)
     if self.settings.isEncryptionEnabled():
         return EncryptionHandler.decrypt(data, self.settings)
Beispiel #7
0
 def writeToCharacteristic(self, serviceUUID, characteristicUUID, content):
     targetCharacteristic = self.getCharacteristic(serviceUUID, characteristicUUID)
     encryptedContent = EncryptionHandler.encrypt(content, self.settings)
     targetCharacteristic.write(encryptedContent, withResponse=True)
Beispiel #8
0
 def checkPayload(self):
     try:
         return EncryptionHandler.decrypt(self.dataCollected, self.settings)
     except BluenetBleException as err:
         print(err)