📄 onewirecontainer33.java
字号:
mbScratchpad = new MemoryBankScratchSHAEE(this); // Set memory bank variables for the status and secret page memstatus = new MemoryBankSHAEE(this, mbScratchpad); memstatus.bankDescription = "Status Page that contains the secret and the status."; memstatus.generalPurposeMemory = true; memstatus.startPhysicalAddress = 128; memstatus.size = 32; memstatus.numberPages = 1; memstatus.pageLength = 32; memstatus.maxPacketDataLength = 32-3; memstatus.extraInfo = false; memstatus.extraInfoLength = 20; memstatus.readWrite = false; memstatus.writeOnce = false; memstatus.pageCRC = false; memstatus.readOnly = false; memstatus.checked = false; // Set memory bank variables memoryPages[0] = new MemoryBankSHAEE(this, mbScratchpad); memoryPages[0].bankDescription = "Page Zero with write protection."; memoryPages[0].generalPurposeMemory = true; memoryPages[0].startPhysicalAddress = 0; memoryPages[0].size = 32; memoryPages[0].numberPages = 1; memoryPages[0].pageLength = 32; memoryPages[0].maxPacketDataLength = 32 - 3; memoryPages[0].extraInfo = true; memoryPages[0].extraInfoLength = 20; memoryPages[0].writeOnce = false; memoryPages[0].pageCRC = true; memoryPages[0].checked = false; // Set memory bank varialbes memoryPages[1] = new MemoryBankSHAEE(this, mbScratchpad); memoryPages[1].bankDescription = "Page One with EPROM mode and write protection."; memoryPages[1].generalPurposeMemory = true; memoryPages[1].startPhysicalAddress = 32; memoryPages[1].size = 32; memoryPages[1].numberPages = 1; memoryPages[1].pageLength = 32; memoryPages[1].maxPacketDataLength = 32 - 3; memoryPages[1].extraInfo = true; memoryPages[1].extraInfoLength = 20; memoryPages[1].pageCRC = true; memoryPages[1].checked = false; // Set memory bank varialbes memoryPages[2] = new MemoryBankSHAEE(this, mbScratchpad); memoryPages[2].bankDescription = "Page Two and Three with write protection."; memoryPages[2].generalPurposeMemory = true; memoryPages[2].startPhysicalAddress = 64; memoryPages[2].size = 64; memoryPages[2].numberPages = 2; memoryPages[2].pageLength = 32; memoryPages[2].maxPacketDataLength = 32 - 3; memoryPages[2].extraInfo = true; memoryPages[2].extraInfoLength = 20; memoryPages[2].writeOnce = false; memoryPages[2].pageCRC = true; memoryPages[2].checked = false; memoryPages[3] = memoryPages[2]; } /** * Authenticates page data given a MAC. * * @param addr address of the data to be read * @param memory the memory read from the page * @param mac the MAC calculated for this function given back as the extra info * @param challenge the 3 bytes written to the scratch pad used in calculating the mac * */ public static boolean isMACValid(int addr, byte[] SerNum, byte[] memory, byte[] mac, byte[] challenge, byte[] secret) { byte[] MT = new byte [64]; System.arraycopy(secret,0,MT,0,4); System.arraycopy(memory,0,MT,4,32); System.arraycopy(ffBlock,0,MT,36,4); MT[40] = (byte) (0x40 | (((addr) << 3) & 0x08) | (((addr) >>> 5) & 0x07)); System.arraycopy(SerNum,0,MT,41,7); System.arraycopy(secret,4,MT,48,4); System.arraycopy(challenge,0,MT,52,3); // finish up with proper padding MT[55] = ( byte ) 0x80; for(int i=56;i<62;i++) MT[i] = ( byte ) 0x00; MT[62] = ( byte ) 0x01; MT[63] = ( byte ) 0xB8; int[] AtoE = new int [5]; com.dalsemi.onewire.utils.SHA.ComputeSHA(MT,AtoE); int cnt = 0; for(int i=0; i<5; i++) { int temp = AtoE[4-i]; for(int j=0;j<4;j++) { if(mac[cnt++] != ( byte ) (temp & 0x0FF)) { return false; } temp >>>= 8; } } return true; } /** * <p>Installs a secret on a DS1961S/DS2432. The secret is written in partial phrases * of 47 bytes (32 bytes to a memory page, 8 bytes to the scratchpad, 7 bytes are * discarded (but included for compatibility with DS193S)) and * is cumulative until the entire secret is processed.</p> * * <p>On TINI, this method will be slightly faster if the secret's length is divisible by 47. * However, since secret key generation is a part of initialization, it is probably * not necessary.</p> * * @param page the page number used to write the partial secrets to * @param secret the entire secret, in partial phrases, to be installed * * @return <code>true</code> if successful * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter * * @see #bindSecretToiButton(int,byte[]) */ public boolean installMasterSecret (int page, byte[] newSecret) throws OneWireIOException, OneWireException { for(int i=0; i<secret.length; i++) secret[i] = 0x00; if(loadFirstSecret(secret, 0)) { if (newSecret.length == 0) return false; byte[] input_secret = null; byte[] sp_buffer = new byte[8]; int secret_mod_length = newSecret.length % 47; if (secret_mod_length == 0) //if the length of the secret is divisible by 40 input_secret = newSecret; else { input_secret = new byte [newSecret.length + (47 - secret_mod_length)]; System.arraycopy(newSecret, 0, input_secret, 0, newSecret.length); } for(int i=0; i<input_secret.length; i+=47) { writeDataPage(page,input_secret,i); System.arraycopy(input_secret,i+36,sp_buffer,0,8); mbScratchpad.computeNextSecret(page*32, sp_buffer, 0); } return true; } else throw new OneWireException("Load first secret failed"); } /** * <p>Binds an installed secret to a DS1961S/DS2432 by using * well-known binding data and the DS1961S/DS2432's unique * address. This makes the secret unique * for this iButton.</p> * * @param page the page number that has the master secret already installed * @param bind_data 32 bytes of binding data used to bind the iButton to the system * * @return <code>true</code> if successful * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter * * @see #installMasterSecret(int,byte[]) */ public boolean bindSecretToiButton(int pageNum, byte[] bindData) throws OneWireIOException, OneWireException { if(!writeDataPage(pageNum,bindData)) return false; byte[] bind_code = new byte[8]; bind_code[0] = (byte)pageNum; System.arraycopy(address,0,bind_code,1,7); mbScratchpad.computeNextSecret(pageNum*32, bind_code, 0); return true; } /** * <p>Writes a data page to the DS1961S/DS2432.</p> * * @param page_number page number to write * @param page_data page data to write (must be at least 32 bytes long) * * @return <code>true</code> if successful, <code>false</code> if the operation * failed * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter */ public boolean writeDataPage(int targetPage, byte[] pageData) throws OneWireIOException, OneWireException { return writeDataPage(targetPage, pageData, 0); } /** * <p>Writes a data page to the DS1961S/DS2432.</p> * * @param page_number page number to write * @param page_data page data to write (must be at least 32 bytes long) * @param offset the offset to start copying the 32-bytes of page data. * * @return <code>true</code> if successful, <code>false</code> if the operation * failed * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter */ public boolean writeDataPage(int targetPage, byte[] pageData, int offset) throws OneWireIOException, OneWireException { int addr = 0; if(targetPage==3) addr = 32; memoryPages[targetPage].write(addr,pageData,offset,32); return true; } /** * <p>Writes data to the scratchpad. In order to write to a data page using this method, * next call <code>readScratchPad()</code>, and then <code>copyScratchPad()</code>. * Note that the addresses passed to this method will be the addresses the data is * copied to if the <code>copyScratchPad()</code> method is called afterward.</p> * * <p>Also note that if too many bytes are written, this method will truncate the * data so that only a valid number of bytes will be sent.</p> * * @param targetPage the page number this data will eventually be copied to * @param targetPageOffset the offset on the page to copy this data to * @param inputbuffer the data that will be copied into the scratchpad * @param start offset into the input buffer for the data to write * @param length number of bytes to write * * @return <code>true</code> if successful, <code>false</code> on a CRC error * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter */ public boolean writeScratchpad (int targetPage, int targetPageOffset, byte[] inputbuffer, int start, int length) throws OneWireIOException, OneWireException { int addr = (targetPage<<5) + targetPageOffset; mbScratchpad.writeScratchpad(addr,inputbuffer,start,length); return true; } /** * Read from the Scratch Pad, which is a max of 8 bytes. * * @param scratchpad byte array to place read data into * length of array is always pageLength. * @param offset offset into readBuf to pug data * @param extraInfo byte array to put extra info read into * (TA1, TA2, e/s byte) * Can be 'null' if extra info is not needed. * * @throws OneWireIOException * @throws OneWireException */ public void readScratchpad (byte[] scratchpad, int offset, byte[] extraInfo) throws OneWireIOException, OneWireException { mbScratchpad.readScratchpad(scratchpad, offset, 8, extraInfo); } /** * Copy all 8 bytes of the Sratch Pad to a certain page and offset in memory. * * @param targetPage the page to copy the data to * @param targetPageOffset the offset into the page to copy to * @param copy_auth byte[] containing write authorization * @param authStart the offset into the copy_auth array where the authorization begins. * * @throws OneWireIOException * @throws OneWireException */ public boolean copyScratchpad (int targetPage, int targetPageOffset, byte[] copy_auth, int authStart) throws OneWireIOException, OneWireException { int addr = (targetPage<<5) + targetPageOffset; mbScratchpad.copyScratchpadWithMAC(addr, copy_auth, authStart); return true; } /** * Copy all 8 bytes of the Sratch Pad to a certain page and offset in memory. * * The container secret must be set so that the container can produce the * correct MAC. * * @param targetPage the page to copy the data to * @param targetPageOffset the offset into the page to copy to * * @throws OneWireIOException * @throws OneWireException */ public boolean copyScratchpad (int targetPage,int targetPageOffset) throws OneWireIOException, OneWireException { int addr = (targetPage<<5) + targetPageOffset; mbScratchpad.copyScratchpad(addr, 8); return true; } /** * Reads a page of memory.. * * @param page page number to read packet from * @param pageData byte array to place read data into * @param offset offset into readBuf to place data * * @throws OneWireIOException * @throws OneWireException */ public boolean readMemoryPage(int page, byte[] pageData, int offset) throws OneWireIOException, OneWireException { int addr = 0; if(page==3) addr = 32; memoryPages[page].read(addr, false, pageData, offset, 32); return true; } /** * <p>Reads and authenticates a page. See <code>readMemoryPage()</code> for a description * of page numbers and their contents. This method will also generate a signature for the * selected page, used in the authentication of roving (User) iButtons.</p> * * @param pageNum page number to read and authenticate * @param pagedata array for the page data. * @param offset offset to copy into the array * @param computed_mac array for the MAC returned by the device. * @param macStart offset to copy into the mac array * * @return <code>true</code> if successful, <code>false</code> if the operation * failed while waiting for the DS1963S's output to alternate * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter */ public boolean readAuthenticatedPage(int page, byte[] pagedata, int offset, byte[] computed_mac, int macStart) throws OneWireIOException, OneWireException { int mbPage = 0; if(page==3) { mbPage = 1; page = 2; } return memoryPages[page].readAuthenticatedPage(mbPage, pagedata, offset, computed_mac, macStart); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -