Wiselib
wiselib.testing/algorithms/group-key/keylevels_share.h
Go to the documentation of this file.
00001 #ifndef _KEYLEVELS_SHARE_H
00002 #define _KEYLEVELS_SHARE_H
00003 
00004 // undef only if using custom made keys! (don't do this :))
00005 #define KEYLEVELS_DEPLOY
00006 
00007 #define COMMON_SEED 0
00008 #ifndef SHAWN
00009   #define KEYSHARE_SIZE 9
00010   #define KEYPOOL_SIZE 120
00011 #else
00012   #warning tutaj czary zeby sie z pliku czytalo
00013   #define KEYSHARE_SIZE _getFileKeyshareSize()
00014   #define KEYPOOL_SIZE _getFileKeypoolSize()
00015 #endif
00016 
00017 #define KEY_LEVELS 4
00018 #define SMALL_KEY_SIZE 16
00019 #define KEY_INFO_SIZE 3 //key index, key level, key value
00020 
00021 
00022 #include "util/pstl/map_static_vector.h"
00023 #include "keylevels_main.h"
00024 
00025 #ifdef SHAWN
00026 #include <string>
00027 #include "sys/simulation/simulation_controller.h"
00028 #include "sys/processor.h"
00029 #include "external_interface/shawn/shawn_types.h"
00030 #include "sys/taggings/basic_tags.h"
00031 #include "sys/taggings/group_tag.h"
00032 using namespace shawn;
00033 
00034 #endif
00035 
00036 namespace wiselib {
00037 
00038 typedef wiselib::OSMODEL Os;
00039 typedef Os::Rand Random;
00040 
00041 typedef struct p {
00042    uint16_t key_index;
00043    uint8_t key_level;
00044    uint8_t key_value[SMALL_KEY_SIZE];
00045 } key;
00046 
00047 typedef struct ki {
00048    uint16_t key_index;
00049    uint8_t key_level;
00050 } key_info;
00051 
00052 typedef struct sk {
00053    uint8_t key_data[GROUP_KEY_SIZE];
00054    bool have_key;
00055    Random *random_;
00056 
00057    void leader_init(Random* random) {
00058 //    random->srand(0);
00059       init(random);
00060       have_key = true;
00061       for (int i = 0; i < GROUP_KEY_SIZE; i++) {
00062          key_data[i] = (uint8_t) (*random)() % 256;
00063       }
00064    }
00065 
00066    void init(Random* random) {
00067       random_ = random;
00068       have_key = false;
00069    }
00070 
00071    void set_key(uint8_t* key_address) {
00072       memcpy(&key_data, key_address, GROUP_KEY_SIZE);
00073       have_key = true;
00074    }
00075 } group_key;
00076 
00077 template<typename OsModel_P, typename Radio_P,
00078 typename Debug_P = typename OsModel_P::Debug>
00079 class KeyShare {
00080 
00081 public:
00082    typedef OsModel_P OsModel;
00083    typedef Radio_P Radio;
00084    typedef Debug_P Debug;
00085 
00086    typedef typename Radio::node_id_t node_id_t;
00087    typedef typename wiselib::MapStaticVector<OsModel, node_id_t, key, NODES_MAX> trusted_links_t;
00088    typedef typename trusted_links_t::iterator trusted_links_it;
00089 
00090    enum ReturnValues 
00091    {
00092       SUCCESS = OsModel::SUCCESS,
00093       ERR_UNSPEC = OsModel::ERR_UNSPEC
00094    };
00095 
00096    KeyShare() 
00097    { 
00098 #ifdef SHAWN
00099    // PTB czytanie danych z pliku
00100    if(!(keyshare = (key*) malloc(KEYSHARE_SIZE * sizeof(key))))
00101    {
00102       fprintf(0, "No memory for keyshare!\n");
00103       exit(-1);
00104    }
00105    for(int i=0;i<KEYSHARE_SIZE;i++){
00106       keyshare[i].key_index = -1;
00107    }
00108 #endif
00109    }
00110 
00111 
00112    trusted_links_it tl_start()
00113    {
00114       return trusted_links_.begin();
00115    }
00116 
00117    trusted_links_it tl_end()
00118    {
00119       return trusted_links_.end();
00120    }
00121 
00122 
00123    void variation_on_SDBMHash(uint8_t* data, unsigned int len) {
00124       uint8_t hash = 0;
00125       int i = 0;
00126 
00127 #ifdef HASH_DEBUG
00128       debug_->debug("[KLS] {%d} HASH: in: ",radio_->id());
00129       print_key_value(data,len);
00130       debug_->debug("\n");
00131 #endif
00132       for(unsigned int j=0;j<len;j++) {
00133          i=j;
00134          hash = data[i] + (hash << 6) + (hash << 16) - hash;
00135          (++i)%=len;
00136          hash = data[i] + (hash << 6) + (hash << 16) - hash;
00137          data[j] = hash;
00138       }
00139 #ifdef KEYLEVELS_SHARE_DEBUG
00140       debug_->debug("[KLS] {%d} HASH: out: ",radio_->id());
00141       print_key_value(data,len);
00142       debug_->debug("\n");
00143 #endif
00144    }
00145 
00146    int init(Radio& radio, Debug& debug, Random& random) {
00147       radio_ = &radio;
00148       debug_ = &debug;
00149       random_ = &random;
00150 #ifdef KEYLEVELS_DEPLOY
00151       fillKeyshareWithKeys(random_);
00152 #else
00153 #ifdef SHAWN
00154 //      readKeysFromXML();
00155 #else
00156 //    fillKeyshareWithFakeKeys();
00157 //    fillKeyshareWithTestKeys(random_);
00158 #endif
00159 #endif
00160       return SUCCESS;
00161    }
00162 
00163    void fillKeyshareWithKeys(Random* random) {
00164       fillKeyshareWithKeyIndexesAndLevels(random);
00165       fillKeyshareWithKeyValues(random);
00166 #ifdef KEYLEVELS_SHARE_DEBUG
00167       listKeyshare();
00168 #endif
00169    }
00170 
00171 #ifdef KEYLEVELS_DEPLOY
00172    void fillKeyshareWithKeyIndexesAndLevels(Random* random) {
00173       bool already;
00174       random->srand(radio_->id()+1);
00175       uint16_t key_index;
00176       uint8_t key_level;
00177       for (unsigned int i = 0; i < KEYSHARE_SIZE; i++) {
00178          already = true;
00179          while (already) {
00180             key_index = (*random)() % KEYPOOL_SIZE;
00181             already = false;
00182             if (owns_key(key_index)) {
00183                already = true;
00184             }
00185          }
00186 
00187          key_level = (*random)() % KEY_LEVELS;
00188          keyshare[i].key_index = key_index;
00189          keyshare[i].key_level = key_level;
00190       }
00191    }
00192 
00193    void fillKeyshareWithKeyValues(Random* random) {
00194 
00195       random->srand(COMMON_SEED);
00196       uint8_t temp_key[SMALL_KEY_SIZE];
00197       int i=0; //key pool iterator
00198       int k=0; //key value iterator
00199       int l=0; //key level iterator
00200       for (i = 0; i < KEYPOOL_SIZE; i++) {
00201          for(k=0;k<SMALL_KEY_SIZE;k++) {
00202             temp_key[k] = (*random)();
00203          }
00204 #ifdef KEYLEVELS_SHARE_DEBUG
00205          debug_->debug("[KLS] {%d} pool key[%d] = ", radio_->id(), i);
00206          print_key_value(temp_key, SMALL_KEY_SIZE);
00207          debug_->debug("\n");
00208 #endif
00209          if (owns_key(i)) {
00210             for(k=0;k<SMALL_KEY_SIZE;k++) {
00211                get_key(i)->key_value[k]=temp_key[k];
00212             }
00213             for(l=0; l<get_key(i)->key_level; l++) {
00214 #ifdef HASH_DEBUG
00215                debug_->debug("[KLS] {%d} pool key[%d] level %d = ",radio_->id(),i,l);
00216                print_key_value(get_key(i)->key_value,SMALL_KEY_SIZE);
00217                debug_->debug("\n");
00218 #endif
00219                variation_on_SDBMHash(get_key(i)->key_value,SMALL_KEY_SIZE);
00220             }
00221 #ifdef HASH_DEBUG
00222             debug_->debug("[KLS] {%d} pool key[%d] level %d = ",radio_->id(),i,get_key(i)->key_level);
00223             print_key_value(get_key(i)->key_value,SMALL_KEY_SIZE);
00224             debug_->debug("\n");
00225 #endif
00226          }
00227       }
00228    }
00229 #else
00230 
00231    void fillKeyshareWithTestKeyIndexesAndLevels() {
00232 #ifndef SHAWN
00233       if(radio_->id() == 3261)
00234       {
00235          keyshare[0].key_index = 1;
00236          keyshare[0].key_level = 0;
00237          keyshare[1].key_index = 2;
00238          keyshare[1].key_level = 1;
00239       }else if(radio_->id() == 3234)
00240       {
00241          keyshare[0].key_index = 2;
00242          keyshare[0].key_level = 4;
00243          keyshare[1].key_index = 3;
00244          keyshare[1].key_level = 0;
00245       }else if(radio_->id() == 0x1c2c)
00246       {
00247          keyshare[0].key_index = 5;
00248          keyshare[0].key_level = 0;
00249          keyshare[1].key_index = 3;
00250          keyshare[1].key_level = 0;
00251       }
00252       return;
00253 #endif
00254 #ifdef SHAWN
00255       switch (radio_->id()) {
00256          case 0:
00257          keyshare[0].key_index = 1;
00258          keyshare[0].key_level = 0;
00259          keyshare[1].key_index = 2;
00260          keyshare[1].key_level = 7;
00261          break;
00262          case 1:
00263          keyshare[0].key_index = 2;
00264          keyshare[0].key_level = 4;
00265          keyshare[1].key_index = 3;
00266          keyshare[1].key_level = 0;
00267          break;
00268          case 2:
00269          keyshare[0].key_index = 5;
00270          keyshare[0].key_level = 0;
00271          keyshare[1].key_index = 3;
00272          keyshare[1].key_level = 0;
00273          break;
00274          case 3:
00275          keyshare[0].key_index = 5;
00276          keyshare[0].key_level = 0;
00277          keyshare[1].key_index = 6;
00278          keyshare[1].key_level = 0;
00279          break;
00280          case 4:
00281          keyshare[0].key_index = 2;
00282          keyshare[0].key_level = 0;
00283          keyshare[1].key_index = 8;
00284          keyshare[1].key_level = 0;
00285          break;
00286          case 5:
00287          keyshare[0].key_index = 2;
00288          keyshare[0].key_level = 0;
00289          keyshare[1].key_index = 9;
00290          keyshare[1].key_level = 0;
00291          break;
00292          case 6:
00293          keyshare[0].key_index = 6;
00294          keyshare[0].key_level = 0;
00295          keyshare[1].key_index = 10;
00296          keyshare[1].key_level = 0;
00297          break;
00298          case 7:
00299          keyshare[0].key_index = 4;
00300          keyshare[0].key_level = 0;
00301          keyshare[1].key_index = 3;
00302          keyshare[1].key_level = 0;
00303          break;
00304          case 8:
00305          keyshare[0].key_index = 4;
00306          keyshare[0].key_level = 0;
00307          keyshare[1].key_index = 5;
00308          keyshare[1].key_level = 0;
00309          break;
00310          case 9:
00311          keyshare[0].key_index = 1;
00312          keyshare[0].key_level = 0;
00313          keyshare[1].key_index = 11;
00314          keyshare[1].key_level = 0;
00315          break;
00316       }
00317 #endif
00318 
00319    }
00320 
00321    void fillKeyshareWithFakeKeys() {
00322 
00323       for (unsigned int i = 0; i < KEYSHARE_SIZE; i++) {
00324          uint16_t key_index, key_level;
00325          bool already = true;
00326 
00327          while (already) {
00328             key_index = random() % KEYPOOL_SIZE;
00329             already = false;
00330 
00331             for (unsigned int j = 0; j < i; j++) {
00332                if (keyshare[j].key_index == key_index) {
00333                   already = true;
00334                   break;
00335                }
00336             }
00337          }
00338 
00339          key_level = random() % KEY_LEVELS;
00340          keyshare[i].key_index = key_index;
00341          keyshare[i].key_level = key_level;
00342       }
00343    }
00344 
00345    void fillKeyshareWithTestKeys(Random* random)
00346    {
00347       fillKeyshareWithTestKeyIndexesAndLevels();
00348       fillKeyshareWithKeyValues(random);
00349    }
00350 
00351 #ifndef SHAWN
00352    int random() { return 1; }
00353 #endif
00354 
00355 #endif
00356 
00357    unsigned int get_keyshare_size() { return KEYSHARE_SIZE; }
00358 
00359 #ifdef KEYLEVELS_SOFT
00360    void listKeyshare() 
00361    {
00362       for (unsigned int i = 0; i < KEYSHARE_SIZE; i++) 
00363       {
00364          debug_->debug("[KLS] Key: (%d,%d): ", keyshare[i].key_index,keyshare[i].key_level);
00365          print_key(keyshare[i].key_value,SMALL_KEY_SIZE);
00366          debug_->debug("\n");
00367       }
00368    }
00369 #endif
00370 
00371 #ifdef KEYLEVELS_DEMO
00372    void compactListKeyshare(int radio){
00373                 char buf[8*KEYSHARE_SIZE+15];
00374                 int written = sprintf(buf, "GKE_KEY;%x;", radio);
00375 
00376                 for (unsigned int i=0; i < KEYSHARE_SIZE; i++)
00377                 {
00378                         if (i < KEYSHARE_SIZE - 1){
00379                                 written += sprintf(buf + written, "%d;%d;", keyshare[i].key_index, keyshare[i].key_level);
00380                         }else{
00381                                 written += sprintf(buf + written, "%d;%d", keyshare[i].key_index, keyshare[i].key_level);
00382                         }
00383                 }       
00384       
00385       debug_->debug("%s", buf);
00386    }
00387 #endif
00388 
00389    key* get_key(uint16_t key_index)
00390    {
00391       for (unsigned int i = 0; i < KEYSHARE_SIZE; i++) {
00392          if (keyshare[i].key_index == key_index) {
00393             return &keyshare[i];
00394          }
00395       }
00396       return NULL;
00397    }
00398 
00399    bool owns_key(uint16_t key_index)
00400    {
00401       return get_key(key_index) != NULL;
00402    }
00403 
00404    bool trusted_link_exists(node_id_t node)
00405    {
00406       return trusted_links_.find(node) != trusted_links_.end();
00407    }
00408 
00409    void put_trusted_link(node_id_t node, key link)
00410    {
00411 #ifdef KEYLEVELS_SHARE_DEBUG
00412       debug_->debug("[KLS] {%d} put_trusted_link to Node %d ",radio_->id(), node);
00413       print_key_value(link.key_value,SMALL_KEY_SIZE);
00414 #endif
00415       trusted_links_[node] = link;
00416 
00417    }
00418 
00419    key* get_key_info(node_id_t node) 
00420    {
00421       if (trusted_link_exists(node)) {
00422          return &trusted_links_[node];
00423       }
00424       return NULL;
00425    }
00426 
00427    void print_key(uint8_t* key, int size) 
00428    {
00429       for (int i = 0; i < size; i++, key++) {
00430          debug_->debug("%d", *key);
00431       }
00432    }
00433 
00434    void print_key_value(uint8_t* value, uint8_t size) 
00435    {
00436          for (int i = 0; i < size; i++) {
00437             debug_->debug("%d:", value[i]);
00438          }
00439          debug_->debug("\n");
00440    }
00441 
00442    void print_all_key_info(key* k) 
00443    {
00444          debug_->debug("[KLS] {%d} KEY: (%d,%d) ", radio_->id(), k->key_index,
00445                k->key_level);
00446          print_key_value(k->key_value, SMALL_KEY_SIZE);
00447    }
00448 
00449 #ifdef SHAWN
00450    void readKeysFromXML()
00451    {
00452       ShawnOs& os_ = radio_->os();
00453       const shawn::GroupTag *gt = NULL;
00454       int _lvl, _id, keyRdy = 0;
00455       std::string _kstr;
00456 
00457       shawn::ConstTagHandle th = os_.proc->owner().find_tag("keyset");
00458       if(th != NULL)
00459       {
00460          gt = dynamic_cast<const shawn::GroupTag*>(th.get());
00461       } else { }
00462       if(gt != NULL)
00463       {
00464          shawn::TagContainer::tag_iterator ti = gt->begin_tags();
00465          unsigned int i = 0;
00466          while(ti != gt->end_tags())
00467          {
00468             shawn::TagHandle tth = ti->second;
00469             const shawn::GroupTag* ggt = dynamic_cast<const shawn::GroupTag*>(tth.get());
00470             if(ggt != NULL)
00471             {
00472                shawn::TagContainer::tag_iterator tti = ggt->begin_tags();
00473                while(tti != ggt->end_tags())
00474                {
00475                   TagHandle ttth = tti->second;
00476                   if(ttth->name() == "key")
00477                   {
00478                      _kstr = ttth->encoded_content().c_str();
00479                      keyRdy++;
00480                   }
00481                   else if(ttth->name() == "level")
00482                   {
00483                      _lvl = atoi(ttth->encoded_content().c_str());
00484                      keyRdy++;
00485                   }
00486                   else if(ttth->name() == "id")
00487                   {
00488                      _id = atoi(ttth->encoded_content().c_str());
00489                      keyRdy++;
00490                   }
00491                   else
00492                   {}
00493                   tti++;
00494                }
00495                if(keyRdy == 3)
00496                {
00497                   if(i < KEYSHARE_SIZE)
00498                   {
00499                      keyshare[i].key_index = _id;
00500                      keyshare[i].key_level = _lvl;
00501                      i++;
00502                   }
00503                }
00504                keyRdy = 0;
00505             }
00506             ti++;
00507          }
00508       }
00509    }
00510 #endif
00511 
00512 // PTB czytanie danych z pliku
00513 #ifndef SHAWN
00514    key keyshare[KEYSHARE_SIZE];
00515 #else
00516    key* keyshare;
00517 #endif
00518 
00519 private:
00520    typename Radio::self_pointer_t radio_;
00521    typename Debug::self_pointer_t debug_;
00522    typename Random::self_pointer_t random_;
00523 
00524 
00525    trusted_links_t trusted_links_;
00526 };
00527 
00528 } /* namespace wiselib */
00529 
00530 #endif  /* _KEYLEVELS_SHARE_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines