⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tinysecm.nc

📁 无线通信的主要编程软件,是无线通信工作人员的必备工具,关天相关教程我会在后续传上.
💻 NC
📖 第 1 页 / 共 3 页
字号:
// $Id: TinySecM.nc,v 1.12 2004/06/16 23:26:23 ckarlof Exp $/*									tab:4 * "Copyright (c) 2000-2003 The Regents of the University  of California.   * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice, the following * two paragraphs and the author appear in all copies of this software. *  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Copyright (c) 2002-2003 Intel Corporation * All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE      * file. If you do not find these files, copies can be found by writing to * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,  * 94704.  Attention:  Intel License Inquiry. *//* Authors: Chris Karlof * Date:    8/1/03 *//** * @author Chris Karlof */module TinySecM{  provides {    interface TinySec;    interface BareSendMsg as Send;    interface ReceiveMsg as Receive;    interface TinySecMode;    interface TinySecControl;    interface StdControl;  }  uses {    interface BareSendMsg as RadioSend;    interface ReceiveMsg as RadioReceive;    interface BlockCipherMode;    interface MAC;    interface Random;    interface BlockCipherInfo;    interface TinySecRadio;    interface Leds;  }}implementation{  bool initialized = FALSE;   enum {    // we allocate some static buffers on the stack; they have to be less    // than this size    TINYSECM_MAX_BLOCK_SIZE = 16,    COMPUTE_MAC_IDLE, // no verify in progress    COMPUTE_MAC_INITIALIZED, // verify has been initialized and                             //ready for incremental computation    COMPUTE_MAC_BUSY, // verify in the middle of incremental computation    DECRYPT_IDLE, // no decrypt in progress    DECRYPT_INITIALIZED, // decrypt has been initialized and                         //is ready for incremental decryption    DECRYPT_BUSY, // incremental decrypt in progress    ENCRYPT_IDLE, // no encryption running    ENCRYPT_BUSY }; // encryption running  int16_t txlength = 0;  int16_t rxlength = 0;  bool txencrypt = TRUE;  bool rxdecrypt = TRUE;    int16_t TxByteCnt = 0;  int16_t RxByteCnt = 0;  int16_t recDataLength = 0;  int16_t sendDataLength = 0;    uint8_t compute_mac_state;  uint8_t decrypt_state;  uint8_t encrypt_state;  uint8_t blockSize;  // buffer for posting mac opration for receive  struct computeMACBuffer {    bool computeMACWaiting;    bool computeMACInitWaiting;    uint8_t position;    uint8_t amount;    bool finishMACWaiting;  } computeMACBuffer;  // buffer for posting decrypt operations on receive  struct decryptBuffer {    bool decryptWaiting;    bool decryptInitWaiting;    uint8_t position;    uint8_t amount;  } decryptBuffer;  CipherModeContext cipherModeContext;  MACContext macContext;  uint8_t iv[TINYSECM_MAX_BLOCK_SIZE];  // TinySec buffers  TinySec_Msg tinysec_rec_buffer;  TinySec_Msg tinysec_send_buffer;  TinySec_Msg* ciphertext_send_ptr;  TinySec_Msg* ciphertext_rec_ptr;  TOS_Msg_TinySecCompat* cleartext_send_ptr;  TOS_Msg_TinySecCompat* cleartext_rec_ptr;  uint8_t encryptionKey[TINYSEC_KEYSIZE];  uint8_t MACKey[TINYSEC_KEYSIZE];  uint8_t sendMode = TINYSEC_AUTH_ONLY;  uint8_t receiveMode = TINYSEC_RECEIVE_AUTHENTICATED;    /**   * Initializes TinySec. BlockCipherMode and MAC contexts are initialized    * with respective keys, key size, and block size.   *   * @param blockSize block size of the block cipher in bytes   * @param keySize key size of the block cipher in bytes   * @param encryptionKey pointer to an array of keySize bytes    *        representing the key used for encryption   * @param MACKey pointer to an array of keySize bytes    *        representing the key used for calculating MAC's   * @return Whether TinySec initialization was successful. Reasons    *         for failure include BlockCipherMode or MAC init()   */  command result_t StdControl.init() {    result_t r1, r2, r3, r4;    int i, local_addr;    uint8_t tmp = call BlockCipherInfo.getPreferredBlockSize();    uint8_t key_tmp[2*TINYSEC_KEYSIZE] = {TINYSEC_KEY};    memcpy(encryptionKey,key_tmp,TINYSEC_KEYSIZE);    memcpy(MACKey,key_tmp+TINYSEC_KEYSIZE,TINYSEC_KEYSIZE);            atomic {      if(tmp > TINYSECM_MAX_BLOCK_SIZE) {	blockSize = 0;	r3 = FAIL;      }      else {	blockSize = tmp;	r3 = SUCCESS;      }          computeMACBuffer.computeMACWaiting = FALSE;      computeMACBuffer.computeMACInitWaiting = FALSE;      compute_mac_state = COMPUTE_MAC_IDLE;      decrypt_state = DECRYPT_IDLE;      encrypt_state = ENCRYPT_IDLE;      decryptBuffer.decryptWaiting = FALSE;      decryptBuffer.decryptInitWaiting = FALSE;            computeMACBuffer.finishMACWaiting = FALSE;          // FIXME: replace this with EEPROM read or random IV      // since IV is reset on reboots, this is bad      for(i=0;i<blockSize;i++) {	iv[i] = 0;      }      if(TINYSEC_IV_LENGTH < TINYSEC_NODE_ID_SIZE)	r4 = FAIL;      else	r4 = SUCCESS;      // write the source address into the 3rd and 4th bytes of iv      local_addr = TOS_LOCAL_ADDRESS;      iv[TINYSEC_IV_LENGTH-TINYSEC_NODE_ID_SIZE] = local_addr & 0xff;      for(i=1;i<TINYSEC_NODE_ID_SIZE;i++) {	local_addr = local_addr >> 8;	iv[TINYSEC_IV_LENGTH-TINYSEC_NODE_ID_SIZE+i] = local_addr & 0xff;      }    }    r1 = call BlockCipherMode.init(&cipherModeContext,TINYSEC_KEYSIZE,encryptionKey);    r2 = call MAC.init(&macContext,TINYSEC_KEYSIZE,MACKey);    if(rcombine(rcombine(r1,r2),rcombine(r3,r4)) == FAIL)      return FAIL;    else {      atomic {	initialized = TRUE;      }      return SUCCESS;    }  }  command result_t StdControl.start() {    return SUCCESS;  }  command result_t StdControl.stop() {    return SUCCESS;  }  command result_t TinySecControl.updateMACKey(uint8_t * newMACKey) {    result_t result = FAIL;        atomic {      if(!initialized ||	 compute_mac_state != COMPUTE_MAC_IDLE ||	 encrypt_state != ENCRYPT_IDLE ||	 decrypt_state != DECRYPT_IDLE)	  result = FAIL;      else { // TinySec is idle	memcpy(MACKey,newMACKey,TINYSEC_KEYSIZE); 	result = call MAC.init(&macContext,TINYSEC_KEYSIZE,MACKey);      }    }    return result;  }  command result_t TinySecControl.getMACKey(uint8_t * result) {    if(!initialized)      return FAIL;    atomic memcpy(result,MACKey,TINYSEC_KEYSIZE);    return SUCCESS;  }    command result_t TinySecControl.updateEncryptionKey(uint8_t * newEncryptionKey) {    result_t result = FAIL;        atomic {      if(!initialized ||	 compute_mac_state != COMPUTE_MAC_IDLE ||	 encrypt_state != ENCRYPT_IDLE ||	 decrypt_state != DECRYPT_IDLE)	  result = FAIL;      else { // TinySec is idle	memcpy(encryptionKey,newEncryptionKey,TINYSEC_KEYSIZE);	// reset IV 	result = call BlockCipherMode.init(&cipherModeContext,TINYSEC_KEYSIZE,					   encryptionKey);      }    }    return result;     }  command result_t TinySecControl.getEncryptionKey(uint8_t * result) {    if(!initialized)      return FAIL;    atomic memcpy(result,encryptionKey,TINYSEC_KEYSIZE);    return SUCCESS;  }  command result_t TinySecControl.resetIV() {    result_t result = FAIL;    atomic {      if(!initialized ||	 compute_mac_state != COMPUTE_MAC_IDLE ||	 encrypt_state != ENCRYPT_IDLE ||	 decrypt_state != DECRYPT_IDLE)	result = FAIL;      else {	memset(iv,0,TINYSEC_IV_LENGTH-TINYSEC_NODE_ID_SIZE);	result = SUCCESS;      }    }    // need to reupdate ID part of IV if we ever use anything    // other TOS_LOCAL_ADDRESS    return result;  }  command result_t TinySecControl.getIV(uint8_t * result) {    if(!initialized)      return FAIL;    atomic memcpy(result,iv,TINYSEC_IV_LENGTH);    return SUCCESS;  }    command result_t Send.send(TOS_MsgPtr msg) {    if(sendMode == TINYSEC_AUTH_ONLY)      msg->length = msg->length | TINYSEC_ENABLED_BIT;    else if(sendMode == TINYSEC_ENCRYPT_AND_AUTH)      msg->length = msg->length | TINYSEC_ENABLED_BIT |	TINYSEC_ENCRYPT_ENABLED_BIT;    else if(sendMode != TINYSEC_DISABLED)      return FAIL;       return call RadioSend.send(msg);  }  event result_t RadioSend.sendDone(TOS_MsgPtr msg, result_t success) {    msg->length = msg->length & ~(TINYSEC_ENABLED_BIT |				  TINYSEC_ENCRYPT_ENABLED_BIT);    return signal Send.sendDone(msg,success);  }      event TOS_MsgPtr RadioReceive.receive(TOS_MsgPtr msg) {    if(msg->length & TINYSEC_ENABLED_BIT) {      if(msg->length & TINYSEC_ENCRYPT_ENABLED_BIT) {	msg->receiveSecurityMode = TINYSEC_ENCRYPT_AND_AUTH;      }      else {	msg->receiveSecurityMode = TINYSEC_AUTH_ONLY;      }    } else {      msg->receiveSecurityMode = TINYSEC_DISABLED;    }        if(receiveMode == TINYSEC_RECEIVE_ANY) {      if(msg->length & TINYSEC_ENABLED_BIT) {	msg->length = msg->length & ~(TINYSEC_ENABLED_BIT |				      TINYSEC_ENCRYPT_ENABLED_BIT);      }      return signal Receive.receive(msg);    }    // not sure if this is the right way to do this. should we trust    // higher level to check crc?    if(msg->length & TINYSEC_ENABLED_BIT) {      msg->length = msg->length & ~(TINYSEC_ENABLED_BIT |				    TINYSEC_ENCRYPT_ENABLED_BIT);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -