📄 shaibuttonuser33.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 1999-2001 Dallas Semiconductor Corporation, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Dallas Semiconductor * shall not be used except as stated in the Dallas Semiconductor * Branding Policy. *--------------------------------------------------------------------------- */package com.dalsemi.onewire.application.sha;import com.dalsemi.onewire.adapter.DSPortAdapter;import com.dalsemi.onewire.container.OneWireContainer33;import com.dalsemi.onewire.container.OneWireContainer18;import com.dalsemi.onewire.adapter.OneWireIOException;import com.dalsemi.onewire.OneWireException;import com.dalsemi.onewire.utils.IOHelper;import java.io.IOException;/** * <P>Class representing DS1961S (or DS2432), family-code 0x33, SHA iButtons as a user * token in SHA Transactions.</P> * * @see SHATransaction * @see SHAiButtonCopr * @see SHAiButtonUser18 * * @version 1.00 * @author SKH */public class SHAiButtonUser33 extends SHAiButtonUser{ /** * For fast 0xFF fills of byte arrays */ private static final byte[] ffBlock = new byte[] { (byte)0x0FF,(byte)0x0FF,(byte)0x0FF,(byte)0x0FF, (byte)0x0FF,(byte)0x0FF,(byte)0x0FF,(byte)0x0FF, (byte)0x0FF,(byte)0x0FF,(byte)0x0FF,(byte)0x0FF, (byte)0x0FF,(byte)0x0FF,(byte)0x0FF,(byte)0x0FF, (byte)0x0FF,(byte)0x0FF,(byte)0x0FF,(byte)0x0FF, (byte)0x0FF,(byte)0x0FF,(byte)0x0FF,(byte)0x0FF, (byte)0x0FF,(byte)0x0FF,(byte)0x0FF,(byte)0x0FF, (byte)0x0FF,(byte)0x0FF,(byte)0x0FF,(byte)0x0FF }; /** * Reference to the OneWireContainer */ protected OneWireContainer33 ibc33 = null; /** * Must maintain a reference to the coprocessor * for generating the COPY_SCRATCHPAD authentication. * This is what is referred to as the write-authorization coprocessor. */ protected SHAiButtonCopr copr = null; /** * <P>No default constructor for user apps. At bare minimum, you need * a reference to a <code>SHAiButtonCopr</code> for the transaction system * and a <code>SHAiButtonCopr</code> for generating the DS1961S write * authorization for the copy-scratchpad command.</P> * * <P>Note: These can be the same coprocessor if you're transaction system * is using unsigned transaction data.</P> * * @see #SHAiButtonUser33(SHAiButtonCopr,OneWireContainer33,boolean,byte[]) * @see #SHAiButtonUser33(SHAiButtonCopr,SHAiButtonCopr,OneWireContainer33) * @see #SHAiButtonUser33(SHAiButtonCopr,SHAiButtonCopr) */ protected SHAiButtonUser33() {;} /** * <P>Initialize a DS1961S as a fresh user iButton for a given SHA service. * This constructor not only creates the service file for the user iButton * using the TMEX file structure, but it also installs the master * authentication secret and binds it to the iButton (making it unique for * a particular button). Optionally, the device can be formatted before * the service file is installed.</P> * * <P>Note: With this constructor, the master secret is installed and bound * to the iButton, so the final secret is none by the object. For that * reason, a hardware coprocessor is not necessary for generating the * write-authorization MAC.</P> * * @param copr The SHAiButtonCopr to which the user object is tied. This * Coprocessor contains the necessary binding code and service * filename, necessary for both locating a user and recreating his * unique secret. * @param owc The DS1963S iButton that this object will refer to. * @param formatDevice If <code>true</code>, the TMEX filesystem will be * formatted before the account service file is created. * @param authSecret The master authentication secret for the systm. * * @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 #SHAiButtonUser33(SHAiButtonCopr,SHAiButtonCopr,OneWireContainer33) * @see #SHAiButtonUser33(SHAiButtonCopr,SHAiButtonCopr) */ public SHAiButtonUser33(SHAiButtonCopr copr, OneWireContainer33 owc, boolean formatDevice, byte[] authSecret) throws OneWireException, OneWireIOException { //setup service filename this(copr,copr); //hold container reference this.ibc33 = owc; //and address this.address = owc.getAddress(); //clear out old secret first byte[] NullSecret = new byte[8]; for(int i=0; i<8; i++) NullSecret[i] = 0x00; if(!this.ibc33.loadFirstSecret(NullSecret, 0)) throw new OneWireException("Failed to null out device secret."); if(!owc.installMasterSecret(0, authSecret)) throw new OneWireException("Install Master Secret failed"); if(!createServiceFile(owc, strServiceFilename, formatDevice)) throw new OneWireException("Failed to create service file."); //setup the fullBindCode with rest of info this.fullBindCode[4] = (byte)this.accountPageNumber; System.arraycopy(this.address,0, this.fullBindCode,5,7); if(!owc.bindSecretToiButton(this.accountPageNumber, copr.getBindData())) throw new OneWireException("Bind Secret to iButton failed"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("------------------------------------"); IOHelper.writeLine("Initialized DS1961S User"); IOHelper.writeLine("address"); IOHelper.writeBytesHex(owc.getAddress()); IOHelper.writeLine("serviceFilename: " + strServiceFilename); IOHelper.writeLine("accountPageNumber: " + accountPageNumber); IOHelper.writeLine("authSecret"); IOHelper.writeBytesHex(authSecret); IOHelper.writeLine("bindData"); IOHelper.writeBytesHex(copr.bindData); IOHelper.writeLine("bindCode"); IOHelper.writeBytesHex(copr.bindCode); IOHelper.writeLine("------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ } /** * <P>Initialize a DS1961S as a fresh user iButton for a given SHA service. * This constructor not only creates the service file for the user iButton * using the TMEX file structure, but it also installs the master * authentication secret and binds it to the iButton (making it unique for * a particular button). Optionally, the device can be formatted before * the service file is installed.</P> * * <P>Note: With this constructor, the master secret is installed and bound * to the iButton, so the final secret is none by the object. For that * reason, a hardware coprocessor is not necessary for generating the * write-authorization MAC.</P> * * @param coprBindCode The Coprocessor Bind Code without the information. * @param fileName The file name from the Coprocessor. * @param fileNameExt The file extenstion from the Coprocessor * @param owc The DS1963S iButton that this object will refer to. * @param formatDevice If <code>true</code>, the TMEX filesystem will be * formatted before the account service file is created. * @param authSecret The master authentication secret for the systm. * * @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 #SHAiButtonUser33(SHAiButtonCopr,SHAiButtonCopr,OneWireContainer33) * @see #SHAiButtonUser33(SHAiButtonCopr,SHAiButtonCopr) */ public SHAiButtonUser33(byte[] coprBindCode, byte[] fileName, int fileNameExt, OneWireContainer33 owc, boolean formatDevice, byte[] authSecret) throws OneWireException, OneWireIOException { //create string representation of service filename this.strServiceFilename = new String(fileName) + "." + (int)fileNameExt; //hold container reference this.ibc33 = owc; //and address this.address = owc.getAddress(); //clear out old secret first byte[] NullSecret = new byte[8]; for(int i=0; i<8; i++) NullSecret[i] = 0x00; if(!this.ibc33.loadFirstSecret(NullSecret, 0)) throw new OneWireException("Failed to null out device secret."); if(!owc.installMasterSecret(0, authSecret)) throw new OneWireException("Install Master Secret failed"); if(!createServiceFile(owc, strServiceFilename, formatDevice)) throw new OneWireException("Failed to create service file."); //setup the fullBindCode with rest of info this.fullBindCode[4] = (byte)this.accountPageNumber; System.arraycopy(this.address,0, this.fullBindCode,5,7); if(!owc.bindSecretToiButton(this.accountPageNumber, copr.getBindData())) throw new OneWireException("Bind Secret to iButton failed"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("------------------------------------"); IOHelper.writeLine("Initialized DS1961S User"); IOHelper.writeLine("address"); IOHelper.writeBytesHex(owc.getAddress()); IOHelper.writeLine("serviceFilename: " + strServiceFilename); IOHelper.writeLine("accountPageNumber: " + accountPageNumber); IOHelper.writeLine("authSecret"); IOHelper.writeBytesHex(authSecret); IOHelper.writeLine("bindCode"); IOHelper.writeBytesHex(coprBindCode); IOHelper.writeLine("------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ } /** * <P>Creates a valid SHAiButtonUser object. If the service file, * whose name is taken from the <code>SHAiButtonCopr</code>, is not * found on the user iButton, a OneWireException is thrown with the * message "Invalid SHA user".</P> * * <P>Note: The same coprocessor can be used for write-authorization as * authentication if you're transaction system is using unsigned transaction * data.</P> * * @param copr The SHAiButtonCopr to which the user object is tied. This * Coprocessor contains the necessary binding code and service * filename, necessary for both locating a user and recreating his * unique secret. * @param authCopr The SHAiButtonCopr used to generate the write-authorization * MAC for the copy-scratchpad command of the DS1961S.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -