📄 shaibuttonuser18.java
字号:
public synchronized boolean hasWriteCycleCounter() { return (this.accountPageNumber>7); } /** * <P>This function creates the full 15-byte binding data for the * coprocessor to use to recreate this user's secret on the copr's * workspace page. This function is located in the SHAiButtonUser * class to support binding codes for user buttons who use alternate * techniques (such as the DS1961S) for secret computation.</P> * * <P>For the DS1963S user iButton, the format of the full bind code is * as follows: * <PRE> * (bindCode+0), (bindCode+1), (bindCode+2), (bindCode+3), * (svcPageNum), (deviceAN+0), (deviceAN+1), (deviceAN+2), * (deviceAN+3), (deviceAN+4), (deviceAN+5), (deviceAN+6), * (bindCode+4), (bindCode+5), (bindCode+6) * </PRE></P> * * @param bindCode the 7-byte binding code from coprocessor's service file * @param fullBindCode the 15-byte full binding code to to be copied into * the coprocessor's scratchpad. There should be 15 * bytes available starting from the offset. * @param offset the offset into fullBindCode where copying should begin. */ public void getFullBindCode(byte[] l_fullBindCode, int offset) { System.arraycopy(this.fullBindCode,0, l_fullBindCode,offset, 15); } /** * <P>Returns a byte representing the appropriate authorization command * for the coprocessor to use to authenticate this user. For a DS1963S, * the authentication command is VALIDATE_PAGE.</P> * * @return byte indicating appropriate command for authenticating user * */ public byte getAuthorizationCommand() { return OneWireContainer18.VALIDATE_DATA_PAGE; } /** * <P>Writes the account data to the SHAiButton. First, this function * asserts that the account page number is known. The account data is * copied from dataBuffer starting at the offset. If there are less * than 32 bytes available to copy, this function only copies the bytes * that are available.</P> * * @param dataBuffer the buffer to copy the account data from * @param offset the index into the buffer where copying should begin * @return whether or not the data write succeeded * * @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 synchronized boolean writeAccountData(byte[] dataBuffer, int offset) throws OneWireException, OneWireIOException { //init local vars OneWireContainer18 ibcL = this.ibc; //make sure account info is properly setup if(!checkAccountPageInfo(ibcL)) return false; int numBytes = Math.min(32, dataBuffer.length-offset); System.arraycopy(dataBuffer, offset, this.accountData, 0, numBytes); if(ibcL.writeDataPage(this.accountPageNumber, this.accountData)) { if(this.writeCycleCounter>=0) this.writeCycleCounter++; return true; } //if write failed, we don't know what the write cycle counter is this.writeCycleCounter = -1; //and this cache should be marked dirty this.accountData[0] = 0; return false; } /** * <P>Reads the account data off the SHAiButton using a standard READ * command. First, this function asserts that the account page number is * known as well as the length of the account file. The 32 byte account * data page is copied into dataBuffer starting at the given offset.</P> * * @param dataBuffer the buffer to copy the account data into * @param offset the index into the buffer where copying should begin * * @return whether or not the read was 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 */ public synchronized boolean readAccountData(byte[] dataBuffer, int offset) throws OneWireException, OneWireIOException { //init local vars OneWireContainer18 ibcL = this.ibc; //make sure account info is properly setup if(!checkAccountPageInfo(ibcL)) { return false; } //if the cache is empty if(this.accountData[0]==0) { //read directly into local cache ibcL.readMemoryPage(this.accountPageNumber, this.accountData, 0); } //copy cached data into user's buffer System.arraycopy(this.accountData, 0, dataBuffer, offset, 32); //had to work, right? return true; } //prevent malloc'ing in critical path private byte[] readAccountData_rawData = new byte[42]; private byte[] readAccountData_scratchpad = new byte[32]; /** * <P>Reads the account data off the SHAiButton using a READ_AUTHENTICATE * command. First, this function asserts that the account page number is * known as well as the length of the account file. Then it copies the * 3 byte challenge to the scratchpad before sending the command for * READ_AUTHENTICATE. The 32 byte account data page is copied into * dataBuffer starting at dataStart.</P> * * <P>In addition to the account data, this function also returns a * calculated MAC. The MAC requires 20 bytes after the start index. * The return value is the write cycle counter value for the account * data page<p> * * @param chlg the buffer containing a 3-byte random challenge. * @param chlgStart the index into the buffer where the 3 byte * challenge begins. * @param dataBuffer the buffer to copy the account data into * @param dataStart the index into the buffer where copying should begin * @param mac the buffer to copy the resulting Message Authentication Code * @param macStart the index into the mac buffer to start copying * * @return the value of the write cycle counter for the page * * @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 synchronized int readAccountData(byte[] chlg, int chlgStart, byte[] dataBuffer, int dataStart, byte[] mac, int macStart) throws OneWireException, OneWireIOException { //init local variables OneWireContainer18 ibcL = this.ibc; byte[] rawData = this.readAccountData_rawData; byte[] scratchpad = this.readAccountData_scratchpad; //make sure account info is properly setup if(this.accountPageNumber<0) { //user not setup return -1; } //copy challenge into scratchpad buffer System.arraycopy(chlg, 0, scratchpad, 20, 3); if(ibcL.eraseScratchPad(this.accountPageNumber)) { //send 1 byte RESUME, instead of 9 byte SELECT ibcL.useResume(true); //write the challenge to the device scratchpad if(ibcL.writeScratchPad(this.accountPageNumber, 0, scratchpad, 0, 32)) { //reads 42 bytes = 32 bytes of page data // + 4 bytes page counter // + 4 bytes secret counter // + 2 bytes CRC boolean readOK = ibcL.readAuthenticatedPage(this.accountPageNumber, rawData, 0); //read the scratchpad for mac int len = ibcL.readScratchPad(scratchpad, 0); //disable RESUME ibcL.useResume(false); if ((!readOK) || (len < 0)) { //read authenticate failed return -1; } //get the value of the write cycle counter int wcc = (rawData[35]&0x0ff); wcc = (wcc << 8) | (rawData[34]&0x0ff); wcc = (wcc << 8) | (rawData[33]&0x0ff); wcc = (wcc << 8) | (rawData[32]&0x0ff); //put the accountData in our local cache System.arraycopy(rawData, 0, this.accountData, 0, 32); //put the account data into return buffer System.arraycopy(rawData, 0, dataBuffer, dataStart, 32); //copy the mac into the return buffer System.arraycopy(scratchpad, 8, mac, macStart, 20); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("----------------------------------------------------------"); IOHelper.writeLine("User's ReadAuthPage"); IOHelper.write("address: "); IOHelper.writeBytesHex(this.address); IOHelper.writeLine("speed: " + this.ibc.getAdapter().getSpeed()); IOHelper.writeLine("RawData: "); IOHelper.writeBytesHex(rawData); IOHelper.writeLine("mac: "); IOHelper.writeBytesHex(mac, macStart, 20); IOHelper.writeLine("wcc: " + wcc); IOHelper.writeLine("----------------------------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ //cache the write cycle counter this.writeCycleCounter = wcc; return wcc; } //write scratchpad failed return -1; } //erase scratchpad failed return -1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -