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

📄 setupservergm.nc

📁 tinyos密钥管理组件 用在tinyos仿真里的
💻 NC
字号:

includes PolyOne;

module SetupServerGM { 
  provides {
	 interface SetupServer;
  }
  uses {
	 interface Primitive;
	 interface Random;
  }
}

implementation {  
    	
    uint16_t seedr[4];
    uint16_t seedc[4];
    
    uint16_t Af_poly16(uint16_t *b,uint16_t x){         
     
	uint16_t b2,s1=0,s2=0;	
	uint32_t s;
	uint8_t i,j;
	uint16_t md0,mdv;

	for(i=T+1;i>0;i--){             
       
	j=i-1;
	
	if(s2) s=(uint32_t )x<<16;
	else s=x*s1;

	md0=s&0xffff;
	mdv=s>>16;

	if(md0>=mdv) s=md0-mdv;
	else         s=md0-mdv+65536u+1;   

	if(b[j]!=0){  /* b[j] is between 1 and 65535 */
		s = s+b[j];
	}
	else{  /* b[j] is 0 or 65536  */     
		b2=j>>4;  /* j div 16 is the right shift j by 4 bits */
		j=j&15;   /* j mod 16, is the least 4 bits of j  */         
		b2=b[T+1+b2]>>j;  /* get the 9th bit of b[j] */
		b2 = b2&1; 
	 
		if(b2){  /* b[j] is 65536  */
	  		s = s+65536u;
		}
	 
	/* else b[i-1]=0, nothing need to do. */
	} 

	md0=s&65535u;
	mdv=s>>16;
	
	if(md0>=mdv) s=md0-mdv;
	else s=md0-mdv+65536u+1;   

	s1=s&0xffff;
	s2=s>>16;                  
      }                     
      return s;                    
    }   
 	
   /* this function is to generate a set of random numbers, which are the 
      coefficients of a bivariate t-degree polynomials f(x,y)  */

    void generate_cefts(BivariatePoly *bp, uint8_t *seed) {

	uint16_t i,j;
	uint16_t cefts[4];
	uint16_t highs[4];

	memset((uint8_t *)bp,0x00,sizeof(BivariatePoly));

	for(i=0;i<=T;i++){
	    for(j=0;j<=i;j++){
	    	call Primitive.PRF(seed,i*T+j,(uint8_t *)cefts);
	    	call Primitive.PRF((uint8_t *)cefts,i*T+j,(uint8_t *)highs);
		
		if(highs[0]==0) {highs[0]=1;cefts[0]=0;}
		else highs[0]=0;
		if(highs[1]==0) {highs[1]=1;cefts[1]=0;}
		else highs[1]=0;
		if(highs[2]==0) {highs[2]=1;cefts[2]=0;}
		else highs[2]=0;
		if(highs[3]==0) {highs[3]=1;cefts[3]=0;}
		else highs[3]=0;

		bp->poly_ceft1[i*(T+1+tt)+j] = cefts[0];
		bp->poly_ceft2[i*(T+1+tt)+j] = cefts[1];
		bp->poly_ceft3[i*(T+1+tt)+j] = cefts[2];
		bp->poly_ceft4[i*(T+1+tt)+j] = cefts[3];

		bp->poly_ceft1[i*(T+1+tt)+T+(j>>4)+1] += (highs[0]<<(j&15));
		bp->poly_ceft2[i*(T+1+tt)+T+(j>>4)+1] += (highs[1]<<(j&15));
		bp->poly_ceft3[i*(T+1+tt)+T+(j>>4)+1] += (highs[2]<<(j&15));
		bp->poly_ceft4[i*(T+1+tt)+T+(j>>4)+1] += (highs[3]<<(j&15));

		bp->poly_ceft1[j*(T+1+tt)+i] = bp->poly_ceft1[i*(T+1+tt)+j];
		bp->poly_ceft2[j*(T+1+tt)+i] = bp->poly_ceft2[i*(T+1+tt)+j];
		bp->poly_ceft3[j*(T+1+tt)+i] = bp->poly_ceft3[i*(T+1+tt)+j];
		bp->poly_ceft4[j*(T+1+tt)+i] = bp->poly_ceft4[i*(T+1+tt)+j];
		
		bp->poly_ceft1[j*(T+1+tt)+T+(i>>4)+1] += (highs[0]<<(i&15));
		bp->poly_ceft2[j*(T+1+tt)+T+(i>>4)+1] += (highs[1]<<(i&15));
		bp->poly_ceft3[j*(T+1+tt)+T+(i>>4)+1] += (highs[2]<<(i&15));
		bp->poly_ceft4[j*(T+1+tt)+T+(i>>4)+1] += (highs[3]<<(i&15));
	    }
	}
/*
	for(i=0;i<=T;i++){
		for(j=0;j<=T+tt;j++){
			dbg(DBG_USR1,"%X%X%X%X",bp->poly_ceft1[i*(T+1+tt)+j],bp->poly_ceft2[i*(T+1+tt)+j],bp->poly_ceft3[i*(T+1+tt)+j],bp->poly_ceft4[i*(T+1+tt)+j]);
		}
		dbg(DBG_USR1,"\n");
	}
*/	
  	return ;
    }

    void ceft_assign(uint8_t sid, uint8_t pid, uint8_t *seed, PolyShare *a){
	uint16_t i,t2,t3;
	uint16_t m;
	uint32_t mod1,mod2,mod3,mod4;
	BivariatePoly bp;
	uint8_t cefts_seed[8];

	bp.poly_id=pid;
	call Primitive.PRF(seed, pid, cefts_seed);
	generate_cefts(&bp,cefts_seed);
	
//	dbg(DBG_USR1,"%X%X%X%X%X%X%X%X:%d:%X%X%X%X%X%X%X%X\n",seed[0],seed[1],seed[2],seed[3],seed[4],seed[5],seed[6],seed[7],sid,cefts_seed[0],cefts_seed[1],cefts_seed[2],cefts_seed[3],cefts_seed[4],cefts_seed[5],cefts_seed[6],cefts_seed[7]);

	for(i=0;i<=T;i++){
		 
		mod1 = Af_poly16(&(bp.poly_ceft1[i*(T+1+tt)]),sid);
		mod2 = Af_poly16(&(bp.poly_ceft2[i*(T+1+tt)]),sid);
		mod3 = Af_poly16(&(bp.poly_ceft3[i*(T+1+tt)]),sid);
		mod4 = Af_poly16(&(bp.poly_ceft4[i*(T+1+tt)]),sid);

	  	t2 = i&15;
	 	t3 = (i>>4)+1;

	 	a->poly_ceft1[i] = mod1&65535L;
	 	m = mod1>>16;
	 	a->poly_ceft1[T+t3] += (m<<t2);

	 	a->poly_ceft2[i] = mod2&65535L;
	 	m = mod2>>16;
	 	a->poly_ceft2[T+t3] += (m<<t2);

	 	a->poly_ceft3[i] = mod3&65535L;
	 	m = mod3>>16;
	 	a->poly_ceft3[T+t3] += (m<<t2);

	 	a->poly_ceft4[i] = mod4&65535L;
	 	m = mod4>>16;
	 	a->poly_ceft4[T+t3] += (m<<t2);
	}
	return;  
    }
  
    command result_t SetupServer.init() {
	
	call Random.init();
	
	seedr[0]=call Random.rand();
	seedr[1]=call Random.rand();
	seedr[2]=call Random.rand();
	seedr[3]=call Random.rand();
	seedc[0]=call Random.rand();
	seedc[1]=call Random.rand();
	seedc[2]=call Random.rand();
	seedc[3]=call Random.rand();
	
        return SUCCESS;
    }

    command result_t SetupServer.SecretAssign(uint16_t ID, uint8_t *secret) {
    
	ceft_assign(ID&0xff,ID>>8,(uint8_t *)seedc, &((Secret_Cefts *)secret)->subc);
	ceft_assign(ID>>8,ID&0xff,(uint8_t *)seedr, &((Secret_Cefts *)secret)->subr);
	
	return SUCCESS;

    }

}

⌨️ 快捷键说明

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