📄 shaibuttoncopr.java
字号:
* <P>Returns error code matching last error encountered by the * coprocessor. An error code of zero implies NO_ERROR.</P> * * @return the error code, zero for none. */ public int getLastError() { return lastError; } /** * <P>Returns a string representing the Provider name associated * with this coprocessor's service.</P> * * @return the name of the provider's service. */ public String getProviderName() { return providerName; } /** * <P>Returns the 3-byte signing challenge used by this coprocessor * for data validation.</P> * * @return 3-byte challenge */ public byte[] getSigningChallenge() { byte[] data = new byte[3]; System.arraycopy(signingChallenge, 0, data, 0, 3); return data; } /** * <P>Copies the 3-byte signing challenge used by this coprocessor * for data validation into the specified array starting at * the specified offset.</P> * * @param data the array for copying the 3-byte challenge. * @param offset the index at which to start copying. */ public void getSigningChallenge(byte[] data, int offset) { System.arraycopy(signingChallenge, 0, data, offset, 3); } /** * <P>Returns the page number used by this coprocessor for signing * account data.</P> * * @return page number used for signing */ public int getSigningPageNumber() { return this.signPageNumber; } /** * <P>Returns the version number of this service.</P> * * @return version number for service. */ public int getVersion() { return this.version; } /** * <P>Returns the page number used by this coprocessor for regenerating * the user authentication. This page is the target page for * <code>bindSecretToiButton</code> when trying to reproduce a user's * authentication secret.</P> * * @return page number used for regenerating user authentication. * @see OneWireContainer18#bindSecretToiButton(int,byte[],byte[],int) */ public int getWorkspacePageNumber() { return this.wspcPageNumber; } /** * <P>Returns a boolean indicating whether or not this coprocessor's * secret's were formatted for compatibility with the DS1961S.</P> * * @return <code>true</code> if this coprocessor can authenticate a * DS1961S using it's system authentication secret. * @see #reformatFor1961S(byte[]) */ public boolean isDS1961Scompatible() { return DS1961Scompatible; } // *********************************************************************** // End Accessor Methods // *********************************************************************** // *********************************************************************** // Begin SHA iButton Methods // *********************************************************************** /** * <P>Given a 32-byte array for page data and a 32-byte array for * scratchpad content, this function will create a 20-byte signature * for the data based on SHA-1. The format of the calculation of the * data signature is as follows: First 4-bytes of signing secret, * 32-bytes of accountData, 12 bytes of scratchpad data starting at * index 8, last 4-bytes of signing secret, 3 bytes of scratchpad data * starting at index 20, and the rest is padding as specified for * standard SHA-1. This is all laid out, in detail, in the DS1963S * data sheet.</P> * * <P>The resulting 20-byte signature is copied into * <code>mac_buffer</code> starting at <code>macStart</code>. If you're * updating a signature that already exists in the accountData array, * it is acceptable to call the method like so: * <code><pre> * copr.createDataSignature(accountData, spad, accountData, 8); * </pre></code> * assuming that the signature starts at index 8 of the accountData * array.</p> * * @param accountData the 32-byte data page for which the signature is * generated. * @param signScratchpad the 32-byte scratchpad contents for which the * signature is generated. This will contain parameters such * as the user's write cycle counter for the page, the user's * 1-wire address, and the page number where account data is * stored. * @param mac_buffer used to return the 20-byte signature generated * by signing the page using the coprocessor's system signing * secret. * @param macStart the offset into mac_buffer where copying should start. * @return <code>true</code> if successful, <code>false</code> if an error * occurred (use <code>getLastError()</code> for more * information on the type of 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 * * @see OneWireContainer18#SHAFunction(byte,int) * @see #getLastError() */ public boolean createDataSignature(byte[] accountData, byte[] signScratchpad, byte[] mac_buffer, int macStart) throws OneWireException, OneWireIOException { //clear any errors this.lastError = this.NO_ERROR; //maintain local reference to container OneWireContainer18 ibcL = this.ibc; int addr = this.signPageNumber << 5; //now we are ready to make a signature if (!ibcL.writeDataPage(this.signPageNumber, accountData)) { this.lastError = this.WRITE_DATA_PAGE_FAILED; return false; } //allow resume access to coprocessor ibcL.useResume(true); //write the signing information to the scratchpad if (!ibcL.writeScratchPad(0, 0, signScratchpad, 0, 32)) { this.lastError = this.WRITE_SCRATCHPAD_FAILED; ibcL.useResume(false); return false; } //\\//\\//\\//\\//\\//\\//\\////\\////\\////\\////\\////\\////\\// if(DEBUG) { IOHelper.writeLine("-----------------------------------------------------------"); IOHelper.writeLine("COPR DEBUG - createDataSignature"); IOHelper.write("address: "); IOHelper.writeBytesHex(address, 0, 8); IOHelper.writeLine("speed: " + this.ibc.getAdapter().getSpeed()); IOHelper.writeLine("-----------------------------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\////\\////\\////\\////\\////\\////\\// //sign that baby! if (ibcL.SHAFunction(ibc.SIGN_DATA_PAGE, addr)) { //get the MAC from the scratchpad ibcL.readScratchPad(signScratchpad, 0); //copy the MAC into the accountData page System.arraycopy(signScratchpad, 8, mac_buffer, macStart, 20); ibcL.useResume(false); return true; } else this.lastError = this.SHA_FUNCTION_FAILED; ibcL.useResume(false); return false; } /** * <p>Creates a data signature, but instead of using the signing secret, * it uses the authentication secret, bound for a particular button.</p> * * <P>fullBindCode can be null if the secret is already bound and in * the signing page.</p> * * @param accountData the 32-byte data page for which the signature is * generated. * @param signScratchpad the 32-byte scratchpad contents for which the * signature is generated. This will contain parameters such * as the user's write cycle counter for the page, the user's * 1-wire address, and the page number where account data is * stored. * @param mac_buffer used to return the 20-byte signature generated * by signing the page using the coprocessor's system signing * secret. * @param macStart the offset into mac_buffer where copying should start. * @param fullBindCode used to recreate the user iButton's unique secret * @return <code>true</code> if successful, <code>false</code> if an error * occurred (use <code>getLastError()</code> for more * information on the type of 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 * * @see OneWireContainer18#SHAFunction(byte,int) * @see #createDataSignature(byte[],byte[],byte[],int) * @see #getLastError() */ public boolean createDataSignatureAuth(byte[] accountData, byte[] signScratchpad, byte[] mac_buffer, int macStart, byte[] fullBindCode) throws OneWireException, OneWireIOException { //clear any errors this.lastError = this.NO_ERROR; //maintain local reference to container OneWireContainer18 ibcL = this.ibc; int page = this.signPageNumber; int addr = page << 5; //\\//\\//\\//\\//\\//\\//\\////\\////\\////\\////\\////\\////\\// if(DEBUG) { IOHelper.writeLine("-----------------------------------------------------------"); IOHelper.writeLine("COPR DEBUG - createDataSignatureAuth"); IOHelper.writeLine("address:"); IOHelper.writeBytesHex(address, 0, 8); IOHelper.writeLine("accountData"); IOHelper.writeBytesHex(accountData); IOHelper.writeLine("signScratchpad"); IOHelper.writeBytesHex(signScratchpad); IOHelper.writeLine("mac_buffer: "); IOHelper.writeBytesHex(mac_buffer,macStart,20); IOHelper.writeLine("fullBindCode: "); if(fullBindCode!=null) IOHelper.writeBytesHex(fullBindCode); else IOHelper.writeLine("null"); IOHelper.writeLine("-----------------------------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\////\\////\\////\\////\\////\\////\\// if(fullBindCode!=null) { //recreate the user's secret on the coprocessor. if(!ibcL.bindSecretToiButton(this.authPageNumber, this.bindData, fullBindCode, page&7 )) { this.lastError = this.BIND_SECRET_FAILED; //bind secret failed return false; } } //now we are ready to make a signature if (!ibcL.writeDataPage(this.signPageNumber, accountData)) { this.lastError = this.WRITE_DATA_PAGE_FAILED; return false; } //allow resume access to coprocessor ibcL.useResume(true); //write the signing information to the scratchpad if (!ibcL.writeScratchPad(0, 0, signScratchpad, 0, 32)) { this.lastError = this.WRITE_SCRATCHPAD_FAILED; ibcL.useResume(false); return false; } //sign that baby! if (ibcL.SHAFunction(ibc.SIGN_DATA_PAGE, addr)) { //get the MAC from the scratchpad ibcL.readScratchPad(signScratchpad, 0); //copy the MAC into the accountData page System.arraycopy(signScratchpad, 8, mac_buffer, macStart, 20); ibcL.useResume(false); return true; } else this.lastError = this.SHA_FUNCTION_FAILED; ibcL.useResume(false); return false; } //prevent malloc'ing in the critical path private byte[] generateChallenge_scratchpad = new byte[32]; /** * <p>Generates a 3 byte random challenge in the iButton, sufficient to be used * as a challenge to be answered by a User iButton. The user answers the challenge * with an authenticated read of it's account data.</p> * * <p>The DS1963S will generate 20 bytes of pseudo random data, though only * 3 bytes are needed for the challenge. Programs can add more 'randomness' * by selecting different bytes from the 20 bytes of random data using the * <code>offset</code> parameter.</p> * * <p>The random number generator is actually the DS1963S's SHA engine, which requires * page data to compute a hash. Select a page number with the <code>page_number</code> * parameter.</p> * * @param offset offset into the 20 random bytes to draw random data from * (must be in range 0-16) * @param ch buffer for the challenge to be returned (must be of length 3 or more) * @param start the starting index into array <code>ch</code> to begin copying * the challenge bytes. * * @return <code>true</code> if successful, <code>false</code> if an error * occurred (use <code>getLastError()</code> for more * information on the type of 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 * * @see SHAiButtonUser#readAccountData(byte[],int,byte[],int,byte[],int) * @see #getLastError() */ public synchronized boolean generateChallenge (int offset, byte[] ch, int start) throws OneWireIOException, OneWireException { //clear any errors this.lastError = this.NO_ERROR; //maintain local reference to container OneWireContainer18 ibcL = this.ibc; byte[] scratchpad = this.generateChallenge_scratchpad; int addr = authPageNumber << 5; if (ibcL.eraseScratchPad(authPageNumber)) { //ibcL.useResume(true); if (ibcL.SHAFunction(ibcL.COMPUTE_CHALLENGE, addr)) { //get the mac from the scratchpad ibcL.readScratchPad(scratchpad, 0); //copy the requested 3 return bytes
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -