Integrity Error on ChangeKey desfire ev3

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Integrity Error on ChangeKey desfire ev3

Jump to solution
1,434 Views
amrin-hexondata
Contributor II

Hi, I'm trying to change default aes key on keyNo = 0, to a different aes key. However, I keep getting 

0x1E integrity error on sending the command, which i think indicates wrong crc.
I wrote in javascript. This is my crc32 calculation function

 

static crc32 = (input) => {
        const crcTable = new Array(256);
        for (let i = 0; i < 256; i++) {
          let c = i;
          for (let j = 0; j < 8; j++) {
            c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
          }
          crcTable[i] = c >>> 0;
        }
      
        let crc = 0 ^ (-1);
      
        for (let i = 0; i < input.length; i++) {
          crc = (crc >>>  ^ crcTable[(crc ^ input.charCodeAt(i)) & 0xFF];
        }
      
        crc = crc ^ (-1);  
        // Convert the integer to an array of bytes  
        const crcBytes = [];  for (let i = 0; i < 4; i++) { 
            crcBytes.push((crc >>> (i * 8)) & 0xFF);  
        }  
        
        return crcBytes.reverse();
    }​


From my understanding, CRC is calculated on Cmd + keyNo + (newKey xor oldkey) + keyversion. Something like this.

let data_to_crc = cmd.concat(_keyNo).concat(_newKey).concat(_keyVersion);
let data_to_crc_string = DesfireUtils.getHexFromDecArray(data_to_crc);
let crc_data = DesfireUtils.crc32(data_to_crc_string);

 

If I run my crc32 function on the data, i'd get the same result when using this online crc32 calculator 
Then cryptogram = newkey + keyversion + crc_data + padding
Then i send over <C4 00 Cryptogram>
Sending over encrypted cryptogram yields same result as not encrypted, 0x1E. encryption key is session key, iv = 0
0 Kudos
Reply
1 Solution
1,415 Views
amrin-hexondata
Contributor II
Fixed the issue. The problem lies in CRC32 algorithm.
I used this C code and transcribed / implement it in javascript
http://www.rfidiot.org/crc32.c

View solution in original post

0 Kudos
Reply
1 Reply
1,416 Views
amrin-hexondata
Contributor II
Fixed the issue. The problem lies in CRC32 algorithm.
I used this C code and transcribed / implement it in javascript
http://www.rfidiot.org/crc32.c
0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-1864587%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EIntegrity%20Error%20on%20ChangeKey%20desfire%20ev3%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1864587%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%2C%20I'm%20trying%20to%20change%20default%20aes%20key%20on%20keyNo%20%3D%200%2C%20to%20a%20different%20aes%20key.%20However%2C%20I%20keep%20getting%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3E0x1E%20integrity%20error%20on%20sending%20the%20command%2C%20which%20i%20think%20indicates%20wrong%20crc.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EI%20wrote%20in%20javascript.%20This%20is%20my%20crc32%20calculation%20function%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Estatic%20crc32%20%3D%20(input)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20const%20crcTable%20%3D%20new%20Array(256)%3B%0A%20%20%20%20%20%20%20%20for%20(let%20i%20%3D%200%3B%20i%20%26lt%3B%20256%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20let%20c%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20%20%20for%20(let%20j%20%3D%200%3B%20j%20%26lt%3B%208%3B%20j%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20c%20%3D%20((c%20%26amp%3B%201)%20%3F%20(0xEDB88320%20%5E%20(c%20%26gt%3B%26gt%3B%26gt%3B%201))%20%3A%20(c%20%26gt%3B%26gt%3B%26gt%3B%201))%3B%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20crcTable%5Bi%5D%20%3D%20c%20%26gt%3B%26gt%3B%26gt%3B%200%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20let%20crc%20%3D%200%20%5E%20(-1)%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20for%20(let%20i%20%3D%200%3B%20i%20%26lt%3B%20input.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20crc%20%3D%20(crc%20%26gt%3B%26gt%3B%26gt%3B%20%3CLI-EMOJI%20id%3D%22lia_smiling-face-with-sunglasses%22%20title%3D%22%3Asmiling_face_with_sunglasses%3A%22%3E%3C%2FLI-EMOJI%3E%20%5E%20crcTable%5B(crc%20%5E%20input.charCodeAt(i))%20%26amp%3B%200xFF%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20crc%20%3D%20crc%20%5E%20(-1)%3B%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Convert%20the%20integer%20to%20an%20array%20of%20bytes%20%20%0A%20%20%20%20%20%20%20%20const%20crcBytes%20%3D%20%5B%5D%3B%20%20for%20(let%20i%20%3D%200%3B%20i%20%26lt%3B%204%3B%20i%2B%2B)%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20crcBytes.push((crc%20%26gt%3B%26gt%3B%26gt%3B%20(i%20*%208))%20%26amp%3B%200xFF)%3B%20%20%0A%20%20%20%20%20%20%20%20%7D%20%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20crcBytes.reverse()%3B%0A%20%20%20%20%7D%E2%80%8B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3CBR%20%2F%3EFrom%20my%20understanding%2C%20CRC%20is%20calculated%20on%20Cmd%20%2B%20keyNo%20%2B%20(newKey%20xor%20oldkey)%20%2B%20keyversion.%20Something%20like%20this.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Elet%20data_to_crc%20%3D%20cmd.concat(_keyNo).concat(_newKey).concat(_keyVersion)%3B%0Alet%20data_to_crc_string%20%3D%20DesfireUtils.getHexFromDecArray(data_to_crc)%3B%0Alet%20crc_data%20%3D%20DesfireUtils.crc32(data_to_crc_string)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CBR%20%2F%3E%3CDIV%3EIf%20I%20run%20my%20crc32%20function%20on%20the%20data%2C%20i'd%20get%20the%20same%20result%20when%20using%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcrccalc.com%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ethis%20online%20crc32%20calculator%3C%2FA%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EThen%20cryptogram%20%3D%20newkey%20%2B%20keyversion%20%2B%20crc_data%20%2B%20padding%3C%2FDIV%3E%3CDIV%3EThen%20i%20send%20over%20%3CC4%2000%3D%22%22%20cryptogram%3D%22%22%3E%3CBR%20%2F%3ESending%20over%20encrypted%20cryptogram%20yields%20same%20result%20as%20not%20encrypted%2C%200x1E.%20encryption%20key%20is%20session%20key%2C%20iv%20%3D%200%3C%2FC4%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1864913%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Integrity%20Error%20on%20ChangeKey%20desfire%20ev3%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1864913%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EFixed%20the%20issue.%20The%20problem%20lies%20in%20CRC32%20algorithm.%3CBR%20%2F%3EI%20used%20this%20C%20code%20and%20transcribed%20%2F%20implement%20it%20in%20javascript%3CBR%20%2F%3E%3CA%20href%3D%22http%3A%2F%2Fwww.rfidiot.org%2Fcrc32.c%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.rfidiot.org%2Fcrc32.c%3C%2FA%3E%3C%2FLINGO-BODY%3E