📄 onewirecontainer18.java
字号:
//the secret page is 16 for secrets 0-3, page 17 for secrets 4-7 int secret_page = (secret_number > 3) ? 17 : 16; //each page has 4 secrets, so look at 2 LS bits int secret_offset = (secret_number & 3) << 3; int offset = 0; //the current offset into the input_secret buffer byte[] sp_buffer = new byte [32]; while (offset < input_secret.length) { if (!eraseScratchPad(page)) return false; if (!writeScratchPad(page, 0, input_secret, offset, 32)) return false; //first write the whole page if (readScratchPad(buffer, 0) < 0) return false; //get the address registers if (!copyScratchPad()) return false; //copy the page into memory System.arraycopy(input_secret, offset + 32, sp_buffer, 8, 15); if (!writeScratchPad(page, 0, sp_buffer, 0, 32)) return false; //now write the scratchpad data if (!SHAFunction((offset == 0) ? COMPUTE_FIRST_SECRET : COMPUTE_NEXT_SECRET)) return false; //means a failure //here we have to write the scratchpad with 32 bytes of dummy data if (!write_read_copy_quick(secret_page, secret_offset)) return false; offset += 47; } //now lets clean up - erase the scratchpad and data page writeDataPage(page, FF); /* //This fails for some parts, due to "well-known" problem with //erase scratchpad not setting up TA1,TA2 properly eraseScratchPad(page); readScratchPad(buffer, 0); copyScratchPad();*/ return true; } //local cahce to make TINI fast private byte[] bind_code_temp = new byte [32]; private byte[] bind_code_alt_temp = new byte [32]; private byte[] bind_data_temp = new byte [32]; /** * <p>Binds an installed secret to a DS1963S by using * well-known binding data and the DS1963S's unique * address. This makes the secret unique * for this iButton. Coprocessor iButtons use this method * to recreate the iButton's secret to verify authentication. * Roving iButtons use this method to finalize their secret keys.</p> * * <p>Note that unlike in the <code>installMasterSecret()</code> method, * the page number does not need to be equivalent to the <code>secret_number</code> * modulo 8. The new secret (installed secret + binding code) is generated * from this page but can be copied into another secret. User iButtons should * bind to the same page the secret was installed on. Coprocessor iButtons * must copy to a new secret to preserve the general system authentication * secret.</p> * * <p>The binding should be either 7 bytes long or 15 bytes long. * A 15-length <code>byte</code> array is unaltered and placed in the scratchpad * for the binding. A 7-length <code>byte</code> array is combined with the page * number and DS1963S unique address and then placed in the scratchpad. * Coprocessors should use a pre-formatted 15-length <code>byte</code> array. * User iButtons should let the method format for them (i.e. * use the 7-length <code>byte</code> array option).</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 * @param bind_code the 7-byte or 15-byte binding code * @param secret_number secret number to copy the resulting secret to * * @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[],int) */ public synchronized boolean bindSecretToiButton (int page, byte[] bind_data, byte[] bind_code, int secret_number) throws OneWireIOException, OneWireException { if (bind_data.length != 32) { System.arraycopy(bind_data, 0, bind_data_temp, 0, (bind_data.length > 32 ? 32 : bind_data.length)); bind_data = bind_data_temp; } if (bind_code.length != 15) { if (bind_code.length == 7) { System.arraycopy(bind_code, 0, bind_code_alt_temp, 0, 4); bind_code_alt_temp [4] = ( byte ) page; System.arraycopy(address, 0, bind_code_alt_temp, 5, 7); System.arraycopy(bind_code, 4, bind_code_alt_temp, 12, 3); } else { System.arraycopy(bind_code, 0, bind_code_alt_temp, 0, (bind_code.length > 15 ? 15 : bind_code.length)); } bind_code = bind_code_alt_temp; } System.arraycopy(bind_code, 0, bind_code_temp, 8, 15); bind_code = bind_code_temp; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("------------------------------------"); IOHelper.writeLine("Binding Secret to iButton"); IOHelper.writeLine("address"); IOHelper.writeBytesHex(address); IOHelper.writeLine("page: " + page); IOHelper.writeLine("secret_number: " + (secret_number&7)); IOHelper.writeLine("bind_data"); IOHelper.writeBytesHex(bind_data); IOHelper.writeLine("bind_code"); IOHelper.writeBytesHex(bind_code); IOHelper.writeLine("------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if (!writeDataPage(page, bind_data)) return false; resume = true; if (!writeScratchPad(page, 0, bind_code, 0, 32)) { resume = false; return false; } if (!SHAFunction(COMPUTE_NEXT_SECRET)) { resume = false; return false; //means a failure } //go ahead and set resume = false, but write_read_copy_quick doesn't //check resume, it automatically assumes it can resume resume = false; //make sure the secret number is between 0 and 7 secret_number = secret_number & 7; //the secret page is 16 for secrets 0-3, page 17 for secrets 4-7 int secret_page = (secret_number > 3) ? 17 : 16; //each page has 4 secrets, so look at 2 LS bits int secret_offset = (secret_number & 3) << 3; return (write_read_copy_quick(secret_page, secret_offset)); } //used when copying secrets from the scratchpad to a secret page private synchronized boolean write_read_copy_quick (int secret_page, int secret_offset) throws OneWireIOException, OneWireException { //don't worry about doSpeed here, this should never be called before something else //that would call doSpeed int addr = (secret_page << 5) + secret_offset; byte[] buffer = byte_buffer; //assume we can resume buffer [0] = ( byte ) RESUME; buffer [1] = ( byte ) WRITE_SCRATCHPAD; buffer [2] = ( byte ) addr; //(secret_page << 5); buffer [3] = ( byte ) (addr >> 8); // secret_offset; int length = 32 - secret_offset; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("------------------------------------"); IOHelper.writeLine("write_read_copy_quick"); IOHelper.writeLine("address"); IOHelper.writeBytesHex(address); IOHelper.writeLine("write scratchpad"); IOHelper.write("target address: 0x"); IOHelper.writeHex((byte)(addr>>8)); IOHelper.writeLineHex((byte)addr); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ //write the scratchpad adapter.reset(); System.arraycopy(FF, 0, buffer, 4, length + 2); adapter.dataBlock(buffer, 0, length + 6); if (CRC16.compute(buffer, 1, length + 5, 0) != 0xB001) { return false; } //here we want to read the scratchpad WITHOUT reading the rest of the data buffer [1] = ( byte ) READ_SCRATCHPAD; System.arraycopy(FF, 0, buffer, 2, 8); adapter.reset(); adapter.dataBlock(buffer, 0, 5); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("read scratchpad"); IOHelper.write("target address: 0x"); IOHelper.writeHex((byte)buffer[3]); IOHelper.writeLineHex((byte)buffer[2]); IOHelper.write("ES: 0x"); IOHelper.writeLineHex((byte)buffer[4]); IOHelper.writeLine("------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ //here we just shoot the buffer back out to call copyScratchpad buffer [1] = COPY_SCRATCHPAD; adapter.reset(); //adapter.dataBlock(buffer,0,5); adapter.dataBlock(buffer, 0, 8); if (buffer [7] == ( byte ) 0x0ff) return waitForSuccessfulFinish(); else return true; } /** * <p>Writes a data page to the DS1963S. This method is the equivalent of calling: * <ul> * <li><code>eraseScratchPad(0); </code></li> * <li><code>writeScratchPad(page_number,0,page_data_array,0,32); </code></li> * <li><code>readScratchPad(buffer,0); </code></li> * <li><code>copyScratchPad(); </code></li> * </ul> * However, this method makes several optimizations to help it * run faster. Because of the optimizations, this is the preferred * way of writing data to a normal memory page on the DS1963S.</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 on a CRC error or 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 * * @see #eraseScratchPad(int) * @see #writeScratchPad(int,int,byte[],int,int) * @see #readScratchPad(byte[],int) * @see #copyScratchPad() */ public synchronized boolean writeDataPage (int page_number, byte[] page_data) throws OneWireIOException, OneWireException { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("------------------------------------"); IOHelper.writeLine("Writing Data Page to iButton"); IOHelper.writeLine("address"); IOHelper.writeBytesHex(address); IOHelper.writeLine("page_number: " + page_number); IOHelper.writeLine("page_data"); IOHelper.writeBytesHex(page_data); IOHelper.writeLine("------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if (doSpeedEnable && (!resume)) doSpeed(); //first we need to erase the scratchpad if (!resume) adapter.select(address); else { adapter.reset(); adapter.putByte(RESUME); } byte[] buffer = byte_buffer; buffer [1] = ERASE_SCRATCHPAD; buffer [2] = ( byte ) 0; buffer [3] = ( byte ) 0; // send block (check copy indication complete) System.arraycopy(FF, 0, buffer, 4, 3); adapter.dataBlock(buffer, 1, 6); if (buffe
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -