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

📄 disk1.lst

📁 用于Slave I2C得读写,此代码针对特定得芯片,但对其他得芯片也有借鉴作用.
💻 LST
📖 第 1 页 / 共 2 页
字号:
 262          int encrypt;                     /* encrypt flag (1 = encrypt, 0 = decrypt) */
 263          {  
 264   1        /* Copy encrypt flag to context.
 265   1         */
 266   1        context->encrypt = encrypt;
 267   1      
 268   1        /* Pack initializing vector into context.
 269   1         */
 270   1        Pack (context->iv, iv);
 271   1      
 272   1        /* Save the IV for use in Restart */
 273   1        context->originalIV[0] = context->iv[0];
 274   1        context->originalIV[1] = context->iv[1];
 275   1      
 276   1        /* Precompute key schedule
 277   1         */
 278   1        DoLock (context->subkeys, key, encrypt);
 279   1      }
 280          
 281          /* DES-CBC block update operation. Continues a DES-CBC encryption
 282             operation, procesSDAg eight-byte message blocks, and updating
 283             the context.
 284           */
 285          int Disk2_Update (context, output, input, len)        //DES_CBCUpdate
 286          DES_CBC_CTX *context;                                            /* context */
 287          unsigned char *output;                                      /* output block */
 288          unsigned char *input;                                        /* input block */
 289          unsigned int len;                      /* length of input and output blocks */
 290          {
 291   1        DWORD inputBlock[2], work[2];
 292   1        unsigned int i;
 293   1        
 294   1        if (len % 8)
 295   1          return (1);
 296   1      
 297   1        for (i = 0; i < len/8; i++) {
 298   2          Pack (inputBlock, &input[8*i]);
 299   2              
C51 COMPILER V7.02b   DISK1                                                                03/16/2006 13:52:41 PAGE 6   

 300   2          /* Chain if encrypting.
 301   2           */
 302   2          if (context->encrypt) {
 303   3            work[0] = inputBlock[0] ^ context->iv[0];
 304   3            work[1] = inputBlock[1] ^ context->iv[1];
 305   3          }
 306   2          else {
 307   3            work[0] = inputBlock[0];
 308   3            work[1] = inputBlock[1];         
 309   3          }
 310   2      
 311   2          Function (work, context->subkeys);
 312   2      
 313   2          /* Chain if decrypting, then update IV.
 314   2           */
 315   2          if (context->encrypt) {
 316   3            context->iv[0] = work[0];
 317   3            context->iv[1] = work[1];
 318   3          }
 319   2          else {
 320   3            work[0] ^= context->iv[0];
 321   3            work[1] ^= context->iv[1];
 322   3            context->iv[0] = inputBlock[0];
 323   3            context->iv[1] = inputBlock[1];
 324   3          }
 325   2          Unpack (&output[8*i], work);
 326   2        }
 327   1        
 328   1        /* Zeroize sensitive information.
 329   1         */
 330   1        memset (inputBlock, 0, sizeof (inputBlock));
 331   1        memset (work, 0, sizeof (work));
 332   1        
 333   1        return (0);
 334   1      }
 335          
 336          void Disk2_Restart (context)  //DES_CBCRestart
 337          DES_CBC_CTX *context;
 338          {
 339   1        /* Reset to the original IV */
 340   1        context->iv[0] = context->originalIV[0];
 341   1        context->iv[1] = context->originalIV[1];
 342   1      }
 343          
 344          static void Pack (into, outof)
 345          DWORD *into;
 346          unsigned char *outof;
 347          {
 348   1        *into    = (*outof++ & 0xffL) << 24;
 349   1        *into   |= (*outof++ & 0xffL) << 16;
 350   1        *into   |= (*outof++ & 0xffL) << 8;
 351   1        *into++ |= (*outof++ & 0xffL);
 352   1        *into    = (*outof++ & 0xffL) << 24;
 353   1        *into   |= (*outof++ & 0xffL) << 16;
 354   1        *into   |= (*outof++ & 0xffL) << 8;
 355   1        *into   |= (*outof   & 0xffL);
 356   1      }
 357          
 358          static void Unpack (into, outof)
 359          unsigned char *into;
 360          DWORD *outof;
 361          {
C51 COMPILER V7.02b   DISK1                                                                03/16/2006 13:52:41 PAGE 7   

 362   1        *into++ = (unsigned char)((*outof >> 24) & 0xffL);
 363   1        *into++ = (unsigned char)((*outof >> 16) & 0xffL);
 364   1        *into++ = (unsigned char)((*outof >>  8) & 0xffL);
 365   1        *into++ = (unsigned char)( *outof++      & 0xffL);
 366   1        *into++ = (unsigned char)((*outof >> 24) & 0xffL);
 367   1        *into++ = (unsigned char)((*outof >> 16) & 0xffL);
 368   1        *into++ = (unsigned char)((*outof >>  8) & 0xffL);
 369   1        *into   = (unsigned char)( *outof        & 0xffL);
 370   1      }
 371          
 372          static void DoLock (subkeys, key, encrypt)
 373          DWORD subkeys[32];
 374          unsigned char key[8];
 375          int encrypt;
 376          {
 377   1        DWORD kn[32];
 378   1        int i, j, l, m, n;
 379   1        unsigned char pc1m[56], pcr[56];
 380   1      
 381   1        for (j = 0; j < 56; j++) {
 382   2          l = PC1[j];
 383   2          m = l & 07;
 384   2          pc1m[j] = (unsigned char)((key[l >> 3] & BYTE_BIT[m]) ? 1 : 0);
 385   2        }
 386   1        for (i = 0; i < 16; i++) {
 387   2          m = i << 1;
 388   2          n = m + 1;
 389   2          kn[m] = kn[n] = 0L;
 390   2          for (j = 0; j < 28; j++) {
 391   3            l = j + TOTAL_ROTATIONS[i];
 392   3            if (l < 28)
 393   3              pcr[j] = pc1m[l];
 394   3            else
 395   3              pcr[j] = pc1m[l - 28];
 396   3          }
 397   2          for (j = 28; j < 56; j++) {
 398   3            l = j + TOTAL_ROTATIONS[i];
 399   3            if (l < 56)
 400   3              pcr[j] = pc1m[l];
 401   3            else
 402   3              pcr[j] = pc1m[l - 28];
 403   3          }
 404   2          for (j = 0; j < 24; j++) {
 405   3            if (pcr[PC2[j]])
 406   3              kn[m] |= BIG_BYTE[j];
 407   3            if (pcr[PC2[j+24]])
 408   3              kn[n] |= BIG_BYTE[j];
 409   3          }
 410   2        }
 411   1        UnLock (subkeys, kn, encrypt);
 412   1      
 413   1        /* Zeroize sensitive information.
 414   1         */
 415   1        memset (pc1m, 0, sizeof (pc1m));
 416   1        memset (pcr, 0, sizeof (pcr));
 417   1        memset (kn, 0, sizeof (kn));
 418   1      }
 419          
 420          static void UnLock (subkeys, kn, encrypt)
 421          DWORD *subkeys;
 422          DWORD *kn;
 423          int encrypt;
C51 COMPILER V7.02b   DISK1                                                                03/16/2006 13:52:41 PAGE 8   

 424          {
 425   1        DWORD *cooked, *raw0, *raw1;
 426   1        int increment;
 427   1        unsigned int i;
 428   1      
 429   1        raw1 = kn;
 430   1        cooked = encrypt ? subkeys : &subkeys[30];
 431   1        increment = encrypt ? 1 : -3;
 432   1      
 433   1        for (i = 0; i < 16; i++, raw1++) {
 434   2          raw0 = raw1++;
 435   2          *cooked    = (*raw0 & 0x00fc0000L) << 6;
 436   2          *cooked   |= (*raw0 & 0x00000fc0L) << 10;
 437   2          *cooked   |= (*raw1 & 0x00fc0000L) >> 10;
 438   2          *cooked++ |= (*raw1 & 0x00000fc0L) >> 6;
 439   2          *cooked    = (*raw0 & 0x0003f000L) << 12;
 440   2          *cooked   |= (*raw0 & 0x0000003fL) << 16;
 441   2          *cooked   |= (*raw1 & 0x0003f000L) >> 4;
 442   2          *cooked   |= (*raw1 & 0x0000003fL);
 443   2          cooked += increment;
 444   2        }
 445   1      }
 446          
 447          static void Function (block, subkeys)
 448          DWORD *block;
 449          DWORD *subkeys;
 450          {
 451   1        register DWORD fval, work, right, left;
 452   1        register int round;
 453   1        
 454   1        left = block[0];
 455   1        right = block[1];
 456   1        work = ((left >> 4) ^ right) & 0x0f0f0f0fL;
 457   1        right ^= work;
 458   1        left ^= (work << 4);
 459   1        work = ((left >> 16) ^ right) & 0x0000ffffL;
 460   1        right ^= work;
 461   1        left ^= (work << 16);
 462   1        work = ((right >> 2) ^ left) & 0x33333333L;
 463   1        left ^= work;
 464   1        right ^= (work << 2);
 465   1        work = ((right >> 8) ^ left) & 0x00ff00ffL;
 466   1        left ^= work;
 467   1        right ^= (work << 8);
 468   1        right = ((right << 1) | ((right >> 31) & 1L)) & 0xffffffffL;
 469   1        work = (left ^ right) & 0xaaaaaaaaL;
 470   1        left ^= work;
 471   1        right ^= work;
 472   1        left = ((left << 1) | ((left >> 31) & 1L)) & 0xffffffffL;
 473   1        
 474   1        for (round = 0; round < 8; round++) {
 475   2          work  = (right << 28) | (right >> 4);
 476   2          work ^= *subkeys++;
 477   2          fval  = SP7[ work        & 0x3fL];
 478   2          fval |= SP5[(work >>  8) & 0x3fL];
 479   2          fval |= SP3[(work >> 16) & 0x3fL];
 480   2          fval |= SP1[(work >> 24) & 0x3fL];
 481   2          work  = right ^ *subkeys++;
 482   2          fval |= SP8[ work        & 0x3fL];
 483   2          fval |= SP6[(work >>  8) & 0x3fL];
 484   2          fval |= SP4[(work >> 16) & 0x3fL];
 485   2          fval |= SP2[(work >> 24) & 0x3fL];
C51 COMPILER V7.02b   DISK1                                                                03/16/2006 13:52:41 PAGE 9   

 486   2          left ^= fval;
 487   2          work  = (left << 28) | (left >> 4);
 488   2          work ^= *subkeys++;
 489   2          fval  = SP7[ work        & 0x3fL];
 490   2          fval |= SP5[(work >>  8) & 0x3fL];
 491   2          fval |= SP3[(work >> 16) & 0x3fL];
 492   2          fval |= SP1[(work >> 24) & 0x3fL];
 493   2          work  = left ^ *subkeys++;
 494   2          fval |= SP8[ work        & 0x3fL];
 495   2          fval |= SP6[(work >>  8) & 0x3fL];
 496   2          fval |= SP4[(work >> 16) & 0x3fL];
 497   2          fval |= SP2[(work >> 24) & 0x3fL];
 498   2          right ^= fval;
 499   2        }
 500   1        
 501   1        right = (right << 31) | (right >> 1);
 502   1        work = (left ^ right) & 0xaaaaaaaaL;
 503   1        left ^= work;
 504   1        right ^= work;
 505   1        left = (left << 31) | (left >> 1);
 506   1        work = ((left >> 8) ^ right) & 0x00ff00ffL;
 507   1        right ^= work;
 508   1        left ^= (work << 8);
 509   1        work = ((left >> 2) ^ right) & 0x33333333L;
 510   1        right ^= work;
 511   1        left ^= (work << 2);
 512   1        work = ((right >> 16) ^ left) & 0x0000ffffL;
 513   1        left ^= work;
 514   1        right ^= (work << 16);
 515   1        work = ((right >> 4) ^ left) & 0x0f0f0f0fL;
 516   1        left ^= work;
 517   1        right ^= (work << 4);
 518   1        *block++ = right;
 519   1        *block = left;
 520   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   7127    ----
   CONSTANT SIZE    =   2280    ----
   XDATA SIZE       =   ----     355
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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