]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/cc2420/transmit/CC2420TransmitP.nc
Adding in cc2420 security support codes
[tinyos-2.x.git] / tos / chips / cc2420 / transmit / CC2420TransmitP.nc
index a8367e54c99f0342df952fd65fda1dc59baf2e5c..d51cd72dccc3415fcd2c052b1b7efe2bdfc48e5b 100644 (file)
@@ -33,6 +33,8 @@
  * @author Jonathan Hui <jhui@archrock.com>
  * @author David Moss
  * @author Jung Il Choi Initial SACK implementation
+ * @author JeongGil Ko
+ * @author Razvan Musaloiu-E
  * @version $Revision$ $Date$
  */
 
@@ -71,6 +73,13 @@ module CC2420TransmitP @safe() {
   uses interface CC2420Strobe as SFLUSHTX;
   uses interface CC2420Register as MDMCTRL1;
 
+  uses interface CC2420Strobe as STXENC;
+  uses interface CC2420Register as SECCTRL0;
+  uses interface CC2420Register as SECCTRL1;
+  uses interface CC2420Ram as KEY0;
+  uses interface CC2420Ram as KEY1;
+  uses interface CC2420Ram as TXNONCE;
+
   uses interface CC2420Receive;
   uses interface Leds;
 }
@@ -96,6 +105,12 @@ implementation {
   enum {
     CC2420_ABORT_PERIOD = 320
   };
+
+  uint16_t startTime = 0;
+  norace uint8_t secCtrlMode = 0;
+  norace uint8_t nonceValue[16] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
+  norace uint8_t skip;
+  norace uint16_t CTR_SECCTRL0, CTR_SECCTRL1;
   
   norace message_t * ONE_NOK m_msg;
   
@@ -104,6 +119,8 @@ implementation {
   norace uint8_t m_tx_power;
   
   cc2420_transmit_state_t m_state = S_STOPPED;
+
+  uint8_t securityChecked = 0;
   
   bool m_receiving = FALSE;
   
@@ -130,6 +147,7 @@ implementation {
   error_t resend( bool cca );
   void loadTXFIFO();
   void attemptSend();
+  void securityCheck();
   void congestionBackoff();
   error_t acquireSpiResource();
   error_t releaseSpiResource();
@@ -374,7 +392,6 @@ implementation {
     if ( type == IEEE154_TYPE_ACK && m_msg) {
       ack_header = call CC2420PacketBody.getHeader( ack_msg );
       msg_header = call CC2420PacketBody.getHeader( m_msg );
-
       
       if ( m_state == S_ACK_WAIT && msg_header->dsn == ack_header->dsn ) {
         call BackoffTimer.stop();
@@ -533,6 +550,7 @@ implementation {
         return FAIL;
       }
       
+      securityChecked = 0;
       m_state = S_LOAD;
       m_cca = cca;
       m_msg = p_msg;
@@ -577,7 +595,131 @@ implementation {
     
     return SUCCESS;
   }
-  
+#ifdef CC2420_HW_SECURITY
+
+  /*
+  inline void uwait(uint16_t u) {
+    uint16_t t0 = TAR;
+    while((TAR - t0) <= u);
+  }
+  */
+
+  task void waitTask(){
+    call Leds.led2Toggle();
+    if(SECURITYLOCK == 1){
+      post waitTask();
+    }else{
+      securityCheck();
+    }
+  }
+
+  void securityCheck(){
+
+    cc2420_header_t* msg_header;
+    cc2420_status_t status;
+    security_header_t* secHdr;
+    uint8_t mode;
+    uint8_t key;
+    uint8_t micLength;
+    uint16_t currentStatus;
+
+    msg_header = call CC2420PacketBody.getHeader( m_msg );
+
+    if(!(msg_header->fcf & (1 << IEEE154_FCF_SECURITY_ENABLED))){
+      return;
+    }
+
+    if(SECURITYLOCK == 1){
+      post waitTask();
+    }else {
+      //Will perform encryption lock registers
+      atomic SECURITYLOCK = 1;
+
+      secHdr = (security_header_t*) &msg_header->secHdr;
+      memcpy(&nonceValue[3], &(secHdr->frameCounter), 4);
+
+      skip = secHdr->reserved;
+      key = secHdr->keyID[0]; // For now this is the only key selection mode.
+
+      if (secHdr->secLevel == NO_SEC){
+       mode = CC2420_NO_SEC;
+       micLength = 4;
+      }else if (secHdr->secLevel == CBC_MAC_4){
+       mode = CC2420_CBC_MAC;
+       micLength = 4;
+      }else if (secHdr->secLevel == CBC_MAC_8){
+       mode = CC2420_CBC_MAC;
+       micLength = 8;
+      }else if (secHdr->secLevel == CBC_MAC_16){
+       mode = CC2420_CBC_MAC;
+       micLength = 16;
+      }else if (secHdr->secLevel == CTR){
+       mode = CC2420_CTR;
+       micLength = 4;
+      }else if (secHdr->secLevel == CCM_4){
+       mode = CC2420_CCM;
+       micLength = 4;
+      }else if (secHdr->secLevel == CCM_8){
+       mode = CC2420_CCM;
+       micLength = 8;
+      }else if (secHdr->secLevel == CCM_16){
+       mode = CC2420_CCM;
+       micLength = 16;
+      }else{
+       return;
+      }
+
+      CTR_SECCTRL0 = ((mode << CC2420_SECCTRL0_SEC_MODE) |
+                     ((micLength-2)/2 << CC2420_SECCTRL0_SEC_M) |
+                     (key << CC2420_SECCTRL0_SEC_TXKEYSEL) |
+                     (1 << CC2420_SECCTRL0_SEC_CBC_HEAD)) ;
+#ifndef TFRAMES_ENABLED
+      CTR_SECCTRL1 = (skip+11+sizeof(security_header_t))+((skip+11+sizeof(security_header_t))<<8);
+#else
+      CTR_SECCTRL1 = (skip+10+sizeof(security_header_t))+((skip+10+sizeof(security_header_t))<<8);
+#endif
+
+      call CSN.clr();
+      call SECCTRL0.write(CTR_SECCTRL0);
+      call CSN.set();
+
+      call CSN.clr();
+      call SECCTRL1.write(CTR_SECCTRL1);
+      call CSN.set();
+
+      call CSN.clr();
+      call TXNONCE.write(0, nonceValue, 16);
+      call CSN.set();
+
+      call CSN.clr();
+      status = call SNOP.strobe();
+      call CSN.set();
+
+      while(status & CC2420_STATUS_ENC_BUSY){
+       //uwait(1*1024);
+       call CSN.clr();
+       status = call SNOP.strobe();
+       call CSN.set();
+      }
+
+      call CSN.clr();
+      call STXENC.strobe();
+      call CSN.set();
+
+      call CSN.clr();
+      call SECCTRL0.read(&currentStatus);
+      call CSN.set();
+
+      call CSN.clr();
+      call SECCTRL0.write(currentStatus && ~(3 << CC2420_SECCTRL0_SEC_MODE));
+      call CSN.set();
+
+      atomic SECURITYLOCK = 0;
+
+    }
+  }
+#endif
+
   /**
    * Attempt to send the packet we have loaded into the tx buffer on 
    * the radio chip.  The STXONCCA will send the packet immediately if
@@ -604,10 +746,13 @@ implementation {
         signal Send.sendDone( m_msg, ECANCEL );
         return;
       }
-      
-      
+#ifdef CC2420_HW_SECURITY
+      if(securityChecked != 1){
+       securityCheck();
+      }
+      securityChecked = 1;
+#endif
       call CSN.clr();
-
       status = m_cca ? call STXONCCA.strobe() : call STXON.strobe();
       if ( !( status & CC2420_STATUS_TX_ACTIVE ) ) {
         status = call SNOP.strobe();
@@ -615,11 +760,11 @@ implementation {
           congestion = FALSE;
         }
       }
-      
+
       m_state = congestion ? S_SAMPLE_CCA : S_SFD;
       call CSN.set();
     }
-    
+
     if ( congestion ) {
       totalCcaChecks = 0;
       releaseSpiResource();
@@ -701,5 +846,6 @@ implementation {
     call ChipSpiResource.attemptRelease();
     signal Send.sendDone( m_msg, err );
   }
+
 }