Advertisement
captmicro

Untitled

May 30th, 2011
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.13 KB | None | 0 0
  1. #include "cryptovm.h"
  2.  
  3. typedef unsigned int    CDWORD; //32 bits
  4. typedef unsigned short  CWORD;  //16 bits
  5. typedef unsigned char   CBYTE;  //8  bits
  6.  
  7. #define CRYPTOVM_FN(n)      cryptovm_##n(CVM_STATE *state)
  8. #define CRYPTOVM_FN1(n)     cryptovm_##n(CVM_STATE *state, void *a0)
  9. #define CRYPTOVM_FN2(n)     cryptovm_##n(CVM_STATE *state, void *a0, void *a1)
  10. #define CRYPTOVM_CALL(n)    cryptovm_##n(state)
  11. #define CRYPTOVM_ROTPRNG    (state->rPRNG = ((start->rPRNG>>1)^ \
  12.     (CDWORD)(0-(state->rPRNG&1)&0xD0103101)))
  13.  
  14. static unsigned int chip8_prng_lfsr = 0xBC83E7A0;
  15.     chip8_prng_lfsr = (chip8_prng_lfsr >> 1) ^
  16.         (unsigned int)(0 - (chip8_prng_lfsr & 1) & 0xD0103101);
  17.        
  18. #define CRYPTOVM_ALLOC(sz)  ((void*)malloc((sz)))
  19. #define CRYPTOVM_FREE(ptr)  (free((void*)(ptr))
  20.  
  21. #define CRYPTOVM_ENTRYP     (0x00000000) //entrypoint in memory
  22. #define CRYPTOVM_REGCOUNT   (0x0F)
  23. #define CRYPTOVM_STACKDEPTH (0xFF)
  24. #define CRYPTOVM_RBIGSZ     (1024 / sizeof(CDWORD)) //1kb
  25. #define CRYPTOVM_MEMSZ      (10240 / sizeof(CDWORD)) //10kb
  26.  
  27. #define CRYPTOVM_CHKFLG(f)  (state->flags&(f))
  28. #define CRYPTOVM_FNONE      (0x00000000)
  29. #define CRYPTOVM_FIARG      (0x04000000)
  30. #define CRYPTOVM_FIOP       (0x08000000)
  31. #define CRYPTOVM_FHALT      (0x10000000)
  32. #define CRYPTOVM_FRESET     (0x20000000)
  33. #define CRYPTOVM_FEXIT      (0x40000000)
  34. #define CRYPTOVM_FINIT      (0x80000000)
  35.  
  36. /**INSTRUCTION LAYOUT******************/
  37. /* INSTRUCTION:          0x 0000 0000 */
  38. /*                          |||| |||| */
  39. /* MODIFIERS UPPER 4 BYTES ----/ |||| */
  40. /* OPCODE LOWER 4 BYTES ------------/ */
  41. /**************************************/
  42. /**INSTRUCTION MODIFIERS***************/
  43. /* MODIFIERS:     0x0000              */
  44. /* ARG0 TYPE    >> 12 (0x?000)        */
  45. /*              = 0xF000 NONE         */
  46. /*              & 0x1000 REGISTER     */
  47. /*              & 0x2000 IMMEDIATE    */
  48. /*              & 0x4000 PTR TO REG   */
  49. /*              & 0x8000 PTR TO IMM   */
  50. /* ARG0 LENGTH  >> 8  (0x0?00)        */
  51. /* ARG1 TYPE    >> 4  (0x00?0)        */
  52. /*              = 0x00F0 NONE         */
  53. /*              & 0x0010 REGISTER     */
  54. /*              & 0x0020 IMMEDIATE    */
  55. /*              & 0x0040 PTR TO REG   */
  56. /*              & 0x0080 PTR TO IMM   */
  57. /* ARG1 LENGTH  >> 0  (0x000?)        */
  58. /**************************************/
  59. #define CRYPTOVM_IGETARG(iptr,an,al) \
  60.     (((DWORD*)(iptr))[(an)+1])
  61.  
  62. #define CRYPTOVM_MODCHKFLG(f)   (((mods)&(f))>0)
  63. #define CRYPTOVM_MODA0REG   (0x1000)
  64. #define CRYPTOVM_MODA0IMM   (0x2000)
  65. #define CRYPTOVM_MODA0P2R   (0x4000)
  66. #define CRYPTOVM_MODA0P2I   (0x8000)
  67. #define CRYPTOVM_MODA0LEN   (((mods)&0x0F00) >> 8)
  68. #define CRYPTOVM_MODA1REG   (0x0010)
  69. #define CRYPTOVM_MODA1IMM   (0x0020)
  70. #define CRYPTOVM_MODA1P2R   (0x0040)
  71. #define CRYPTOVM_MODA1P2I   (0x0080)
  72. #define CRYPTOVM_MODA1LEN   ((mods)&0x000F)
  73.  
  74. #define CRYPTOVM_MODISA0REG (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0REG))
  75. #define CRYPTOVM_MODISA0REGEX   (CRYPTOVM_MODISA0REG & (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2R)))
  76. #define CRYPTOVM_MODISA0IMM (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0IMM))
  77. #define CRYPTOVM_MODISA0IMMEX   (CRYPTOVM_MODISA0IMM & (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2I)))
  78. #define CRYPTOVM_MODISA0P2R (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2R))
  79. #define CRYPTOVM_MODISA0P2I (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2I))
  80.  
  81. #define CRYPTOVM_MODISA1REG (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1REG))
  82. #define CRYPTOVM_MODISA1REGEX   (CRYPTOVM_MODISA1REG & (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2R)))
  83. #define CRYPTOVM_MODISA1IMM (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1IMM))
  84. #define CRYPTOVM_MODISA1IMMEX   (CRYPTOVM_MODISA1IMM & (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2I)))
  85. #define CRYPTOVM_MODISA1P2R (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2R))
  86. #define CRYPTOVM_MODISA1P2I (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2I))
  87.  
  88. #define CRYPTOVM_IHALT      (0x0000)
  89. #define CRYPTOVM_IMOV       (0x0001)
  90.  
  91. typedef struct
  92. {
  93.     CDWORD  pc;
  94.     CDWORD  rPRNG;
  95.     CDWORD  r[CRYPTOVM_REGCOUNT];
  96.     CDWORD  rBIG[CRYPTOVM_RBIGSZ];
  97.     CDWORD  stack[CRYPTOVM_STACKDEPTH];
  98.     CDWORD  flags;
  99.     CDWORD  *mem;
  100. } CVM_STATE;
  101.  
  102.    
  103. void CRYPTOVM_FN(reset)
  104. {
  105.     CDWORD i = 0;
  106.     state->pc = CRYPTOVM_ENTRYP;
  107.     state->rPRNG = 0xBC83E7A0;
  108.     for (i = 0; i < CRYPTOVM_REGCOUNT; i++) state->r[i] = 0x00000000;
  109.     for (i = 0; i < CRYPTOVM_RBIGSZ; i++) state->rBIG[i] = 0x00000000;
  110.     for (i = 0; i < CRYPTOVM_STACKDEPTH; i++) state->stack[i] = 0x00000000;
  111.     state->flags = CRYPTOVM_FINIT | CRYPTOVM_FRESET;
  112. }
  113.  
  114. void CRYPTOVM_FN(init)
  115. {
  116.     if (CRYPTOVM_CHKFLG(CRYPTOVM_FINIT) == 1) return;
  117.    
  118.     CRYPTOVM_CALL(reset);
  119.     state->mem = CRYPTOVM_ALLOC(CRYPTOVM_MEMSZ);
  120.    
  121.     UINT i = 0;
  122.     for (i = 0; i < CRYPTOVM_MEMSZ; i++) state->mem[i] = 0x00000000;
  123. }
  124.  
  125. void CRYPTOVM_FN(exit)
  126. {
  127.     if (CRYPTOVM_CHKFLG(CRYPTOVM_FINIT) == 0) return;
  128.    
  129.     CRYPTOVM_CALL(reset);
  130.     state->flags = CRYPTOVM_FEXIT;
  131.     CRYPTOVM_FREE(state->mem);
  132. }
  133.  
  134. void CRYPTOVM_FN2(program)
  135. {
  136.     if (CRYPTOVM_CHKFLG(CRYPTOVM_FINIT) == 0) return;
  137.    
  138.     if ((arg0==0) || (arg1==0)) return;
  139.     CDWORD *prgm = (CDWORD*)arg0;
  140.     CDWORD prgmsz = *((CDWORD*)arg1);
  141.     if ((prgm==0) || (prgmsz==0)) return;
  142.    
  143.     CDWORD i = 0;
  144.     for (i = 0; i < prgmsz; i++) state->mem[i] = prgm[i];
  145. }
  146.  
  147. void CRYPTOVM_FN(step)
  148. {
  149.     if (CRYPTOVM_CHKFLG(CRYPTOVM_FHALT) > 0) return;
  150.     if (CRYPTOVM_CHKFLG(CRYPTOVM_FINIT) == 0) return;
  151.    
  152.     CDWORD *iptr = &state->mem[state->pc];
  153.     CDWORD inst = *iptr;
  154.     CWORD opcode = (CWORD)inst;
  155.     CWORD mods = (CWORD)(inst>>15);
  156.    
  157.     CDWORD addr0 = 0, addr1 = 0;
  158.     CDWORD reg0 = 0, reg1 = 0, imm0 = 0, imm1 = 0;
  159.     CBYTE arg0len = CRYPTOVM_MODA0LEN();
  160.     CBYTE arg1len = CRYPTOVM_MODA1LEN();
  161.    
  162.     if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0REG)) {
  163.         reg0 = state->r[CRYPTOVM_IGETARG(iptr,0,arg0len)];
  164.     } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0IMM)) {
  165.         imm0 = CRYPTOVM_IGETARG(iptr,0,arg0len);
  166.     } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2R)) {
  167.         addr0 = CRYPTOVM_IGETARG(iptr,0,arg0len);
  168.         reg0 = state->r[state->mem[addr0]];
  169.     } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2I)) {
  170.         addr0 = CRYPTOVM_IGETARG(iptr,0,arg0len);
  171.         imm0 = state->mem[addr0];
  172.     }
  173.    
  174.     if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1REG)) {
  175.         reg1 = state->r[CRYPTOVM_IGETARG(iptr,1,arg1len)];
  176.     } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1IMM)) {
  177.         imm1 = CRYPTOVM_IGETARG(iptr,1,arg1len);
  178.     } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2R)) {
  179.         addr1 = CRYPTOVM_IGETARG(iptr,1,arg1len);
  180.         reg1 = state->r[state->mem[addr1]];
  181.     } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2I)) {
  182.         addr1 = CRYPTOVM_IGETARG(iptr,1,arg1len);
  183.         imm1 = state->mem[addr1];
  184.     }
  185.    
  186.     CBYTE calcLen = (arg0len + arg1len);
  187.     calcLen = (calcLen>0)?1:calcLen;
  188.     state->pc += calcLen;
  189.    
  190.     //WARNING: pc at next instruction already
  191.     switch (op)
  192.     {
  193.         case CRYPTOVM_IHALT:
  194.             state->flags |= CRYPTOVM_FHALT;
  195.             break;
  196.         case CRYPTOVM_IMOV:
  197.             if (CRYPTOVM_MODISA0REGEX) {
  198.                 if (CRPTOVM_MODISA1REGEX) {
  199.                     state->r[reg0] = state->r[reg1];
  200.                 } else if (CRYPOVMP_MODISA1IMMEX) {
  201.                     state->r[reg0] = imm1;
  202.                 }
  203.             } else if (CRYPTOVM_MODISA0P2I) {
  204.                 if (CRPTOVM_MODISA1REGEX) {
  205.                     state->mem[addr0] = state->r[reg1];
  206.                 } else if (CRYPOVMP_MODISA1IMMEX) {
  207.                     state->mem[addr0] = imm1;
  208.                 }
  209.             } else {
  210.                 state->flags |= (CRYPTOVM_FHALT | CRYPTOVM_FIARG);
  211.                 break;
  212.             }
  213.             break;
  214.         default:
  215.             state->flags |= (CRYPTOVM_FHALT | CRYPTOVM_FIOP);
  216.             break;
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement