home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Disk&HD / DCN-DP12.LHA / DiskProtection / ExampleXPK / xpkSICR.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-23  |  4.8 KB  |  207 lines

  1. /*
  2. ** xpkSICR - SImple CRypt library
  3. ** example code for a DiskProtection compatible XPK sublibrary
  4. ** xor every longword with the password value
  5. **
  6. ** This code is based on various other XPK examples and modified 1996 by 
  7. ** Patrick Ohly. It can be compiled with SAS/C. It may be used for your
  8. ** own libraries, as long as you mention DiskProtection and my name.
  9. ** You should clearly state that a DiskProtection compatible xpklibrary
  10. ** should not be used with XPK when distributing it!
  11. **
  12. ** DiskProtection compatible means:
  13. ** - a valid XPK sublibrary
  14. ** - stored in libs:compressors
  15. ** - XPKIF_NEEDPASSWD set
  16. ** - XPKIF_LOOSY not set
  17. ** - size of a chunk does not increase when encrypted
  18. **
  19. ** The last point is a problem with the XPK standard:
  20. ** xpkmaster.library expects sublibraries to save the mode
  21. ** with every encrypted/packed chunk and to be able to recognize
  22. ** wrong passwords, i.e. checksums are nescessary, too.
  23. ** Both is not possible with DiskProtection, because every diskblock
  24. ** has to be stored in place and there is no space for any extra byte.
  25. **
  26. ** However, you _can_ use different modes with DiskProtection:
  27. ** In contrast to the xpkmaster.library DiskProtection provides the
  28. ** correct mode on decryption, too. You also don't have to bother with checking
  29. ** the password. DiskProtection does this itself.
  30. */
  31.  
  32. #include <proto/exec.h>
  33. #include <exec/memory.h>
  34. #include <limits.h>
  35.  
  36. #include <libraries/xpksub.h>
  37.  
  38. /*
  39. ** only one mode in this example
  40. ** speed is guessed
  41. */
  42. static struct XpkMode SicrMode =
  43. {
  44.     NULL, 100, XPKMF_A3000SPEED, 0,0, 500,500, 0, USHRT_MAX/1024, "simple" 
  45. };
  46.  
  47. static struct XpkInfo SicrInfo = {
  48.     0,            /* info version */
  49.     1,            /* lib  version */
  50.     0,            /* master vers  */
  51.     0,            /* ModesVersion    */
  52.     "SICR",            /* short name   */
  53.     "Simple Crypt 0.1",    /* long name    */
  54.     "simply xor's every long word",    /* description  */
  55.     (ULONG)'SICR',            /* 4 letter ID  */
  56.     XPKIF_PK_CHUNK  |    /* flags        */
  57.     XPKIF_UP_CHUNK  |
  58.     XPKIF_ENCRYPTION|
  59.     XPKIF_NEEDPASSWD,
  60.     0x40000000, /* 1GB */    /* max in chunk */
  61.     0,            /* min in chunk */
  62.     32768,            /* def in chunk */
  63.     "Encrypting",        /* pk message   */
  64.     "Decrypting",        /* up message   */
  65.     "Encrypted",        /* pk past msg  */
  66.     "Decrypted",        /* up past msg  */
  67.     0,            /* def mode     */
  68.     0,            /* pad          */
  69.     &SicrMode        /* modes        */
  70. };
  71.  
  72.  
  73. /*
  74. ** Returns the info structure of our encryptor
  75. **/
  76. struct XpkInfo * __saveds __asm
  77. SicrXpksPackerInfo(void)
  78. {
  79.     return &SicrInfo;
  80. }
  81.  
  82.  
  83. /*
  84. ** This forces the next chunk to be decryptable independent from the
  85. ** previous one. This is always the case in SICR.
  86. **/
  87. long __saveds __asm
  88. SicrXpksPackReset(register __a0 XPARAMS* xpar)
  89. {
  90.     return XPKERR_OK;
  91. }
  92.  
  93. /*
  94. ** help function:
  95. ** calculate value from password
  96. ** taken from FEAL (special thanks to Christian von Roques)
  97. */
  98.  
  99. typedef unsigned char    Word8;
  100. typedef unsigned short   Word16;
  101. typedef unsigned long    Word32;
  102. typedef struct { unsigned long lft, rgt; } Word64;
  103.  
  104. static Word64 password (APTR *s)
  105. {
  106.     Word8* p=(Word8*)s;
  107.     Word64 x;
  108.     Word32 t;
  109.  
  110.     x.lft = 0;
  111.     x.rgt = 0;
  112.  
  113.     while (p && *p)
  114.     {
  115.         t = x.lft;
  116.         x.lft = x.rgt;
  117.         x.rgt = ((t << 9) | (t >> 23)) + (Word32) *p;
  118.         p++;
  119.     }
  120.     return(x);
  121. }
  122.  
  123.  
  124. /*
  125. ** Encrypt a chunk
  126. **
  127. ** xpar->Sub[0-3] can be stored to save private data,
  128. ** e.g. pointers to allocated resources
  129. **
  130. ** This fields will be zero for the first chunk, so
  131. ** you can initialize encryption then, store your data
  132. ** there and set them to zero again when cleaning up.
  133. **
  134. ** One xpar block will never be used for both encryption
  135. ** and decryption without cleaning up before changing the
  136. ** mode of operation.
  137. **/
  138. long __saveds __asm
  139. SicrXpksPackChunk(register __a0 XPARAMS *xpar)
  140. {
  141.     if(xpar->InLen <= xpar->OutBufLen)
  142.     {
  143.         ULONG  *r = (ULONG*)xpar->InBuf, 
  144.                *w = (ULONG*)xpar->OutBuf,
  145.                *e = (ULONG *)&(((UBYTE *)r)[xpar->InLen]);
  146.         ULONG xor = xpar->Sub[0];
  147.         Word64 x;
  148.         
  149.         if(!xor)
  150.         {
  151.             /* first hunk, calculate password */
  152.             x = password(xpar->Password);
  153.             xor  = x.lft ^ x.rgt;
  154.             /* should better not be zero :-) */
  155.             if(!xor)
  156.                 xor = 0xFFFFFFFF;
  157.             xpar->Sub[0] = xor;
  158.         }
  159.   
  160.         /* encrypt */
  161.         for(;r < e; r++, w++)
  162.             *w = *r ^ xor;
  163.  
  164.         /* must be set! */
  165.         xpar->OutLen = xpar->InLen;
  166.         return XPKERR_OK;
  167.     }
  168.     else
  169.     {
  170.         /* error, should never occur with DiskProtection */
  171.         xpar->OutLen = 0;
  172.         return XPKERR_SMALLBUF;
  173.     }
  174. }
  175.  
  176. /*
  177. ** free all resource allocated in XircXpksPackChunk
  178. */
  179. void __saveds __asm
  180. SicrXpksPackFree(register __a0 XPARAMS* xpar)
  181. {
  182.     /* xpar block could be used again with different password,
  183.     ** thus this must be set to zero again
  184.     */
  185.     xpar->Sub[0] = NULL;
  186. }
  187.  
  188. /*
  189. ** Decrypt a chunk
  190. **/
  191. long __saveds __asm
  192. SicrXpksUnpackChunk(register __a0 XPARAMS *xpar)
  193. {
  194.     /* with SICR encryption is the same as decryption */
  195.     return SicrXpksPackChunk(xpar);
  196. }
  197.  
  198. /*
  199. ** free all resource allocated in XircXpksPackChunk
  200. */
  201. void __saveds __asm
  202. SicrXpksUnpackFree(register __a0 XPARAMS* xpar)
  203. {
  204.     /* see above... */
  205.     xpar->Sub[0] = NULL;
  206. }
  207.