Advertisement
Amrin12

Functions that relates to mac

May 16th, 2024
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.      *
  3.      * @param {number[]} cmd
  4.      * @param {number[]} options
  5.      * @returns {number[]}
  6.      */
  7. getMacInput = (cmd, options) => {
  8.     console.log("Getting MAC Input");
  9.  
  10.     console.log("CMD Counter: ", this.CmdCounter);
  11.     console.log("Transaction ID: ", this.TransactionIdentifier);
  12.  
  13.     // get cmdCounter
  14.     const commandCounter = DesfireUtils.intToHexArray(this.CmdCounter, 1);
  15.  
  16.     console.log("Command Counter: ", commandCounter);
  17.     const transactionId = DesfireUtils.hexStringToByte(this.TransactionIdentifier);
  18.     console.log("transactionId: ", transactionId);
  19.  
  20.     let macInput = cmd.concat(commandCounter).concat(transactionId);
  21.  
  22.     if (options != null)
  23.         macInput = macInput.concat(options);
  24.  
  25.     return macInput;
  26. }
  27.    
  28. getCardVersion = async () => {
  29.     let cmd = DESFIRE_INS.GET_UID;
  30.  
  31.     const macInput = this.getMacInput(cmd, null);
  32.     const macInputString = DesfireUtils.getHexFromDecArray(macInput);
  33.     console.log("Mac Input: ", macInputString);
  34.  
  35.     const macFull = CMAC.calculateCmac(this.sessionAuthMac, macInputString);
  36.     console.log("Mac Full: ", macFull);
  37.  
  38.     const macTruncated = CMAC.macTruncate(macFull);
  39.     console.log("MAC Truncated: ", macTruncated);
  40.  
  41.     const param = DesfireUtils.hexStringToByte(macTruncated);
  42.     cmd = cmd.concat(param);
  43.  
  44.     let response = await this.sendCommand(cmd);
  45.     console.log("Get Card Version: ", response)
  46.  
  47. }
  48.  
  49. CMAC::macTruncate = (message) => {
  50.     const fullMAC = this.hexStringToByte(message);
  51.     const fullMACLength = fullMAC.length;
  52.  
  53.     const truncatedMAC = new Uint8Array(fullMACLength / 2);
  54.     let truncatedMACPos = 0;
  55.     for (let i = 1; i < fullMACLength; i += 2) {
  56.         truncatedMAC[truncatedMACPos] = fullMAC[i];
  57.         truncatedMACPos++;
  58.     }
  59.  
  60.     const truncatedMACString = Array.from(truncatedMAC).map(byte => {
  61.         // Convert each byte to its hexadecimal representation
  62.         return ('0' + byte.toString(16)).slice(-2); // Ensure two characters for each byte
  63.     }).join('');
  64.  
  65.     console.log('Truncated MAC: ', truncatedMACString);
  66.     return truncatedMACString;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement