home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR4 / SECDRV12.ZIP / SDCOMMON.C < prev    next >
C/C++ Source or Header  |  1994-01-04  |  6KB  |  245 lines

  1. /* Secure Drive V1.2 */
  2. /* Code common to CRYPTDSK and LOGIN */
  3.  
  4. #include "secdrv.h"
  5.  
  6. struct tsrdata far *cryptdata;
  7.  
  8. char pass1[MAXPASS];
  9. char pass2[MAXPASS];
  10.  
  11. void invgets(char *input)
  12. {
  13. unsigned i=0;
  14. char c;
  15. while(i<(MAXPASS-1)) {
  16.     c=getch();
  17.     if(c=='\x0d')
  18.         {
  19.         printf("\n");
  20.         *input='\0';
  21.         return;
  22.         }
  23.     else if(c=='\b')
  24.         {
  25.         if(i>0)
  26.             {
  27.             i--;
  28.             input--;
  29.             printf("\b \b");
  30.             }
  31.         }
  32.     else
  33.         {
  34.         printf("*");
  35.         *input++=c;
  36.         i++;
  37.         }
  38.     }
  39. }
  40.  
  41. void readsec(unsigned drive,unsigned head,unsigned track,
  42.              unsigned sector,unsigned nsects,void *buffer)
  43. {
  44. unsigned i;
  45. char c;
  46. for(;;) {
  47.     for(i=0;i<3;i++)
  48.         if(!biosdisk(2,drive,head,track,sector,nsects,buffer)) return;
  49.     printf("\nRead error: drive %02x, head %u, track %u\n",
  50.             drive,head,track);
  51.     printf("Abort, Retry, Ignore? ");
  52.     c=toupper(getch()); printf("%c\n",c);
  53.     if(c=='I') return;
  54.     if(c=='A') exit(1);
  55.     }
  56. }
  57.  
  58. void writesec(unsigned drive,unsigned head,unsigned track,
  59.               unsigned sector,unsigned nsects,void *buffer)
  60. {
  61. unsigned i;
  62. char c;
  63. for(;;) {
  64.     for(i=0;i<3;i++)
  65.         if(!biosdisk(3,drive,head,track,sector,nsects,buffer)) return;
  66.     printf("\nWrite error: drive %02x, head %u, track %u\n",
  67.             drive,head,track);
  68.     printf("Abort, Retry, Ignore? ");
  69.     c=toupper(getch()); printf("%c\n",c);
  70.     if(c=='I') return;
  71.     if(c=='A') exit(1);
  72.     }
  73. }
  74.  
  75. int getyn(void)
  76. {
  77. char c;
  78. for(;;)
  79.     {
  80.     c=getch();
  81.     if(c=='y'||c=='Y') { printf("Yes\n\n"); return(TRUE); }
  82.     if(c=='n'||c=='N') { printf("No\n\n"); return(FALSE); }
  83.     }
  84. }
  85.  
  86. void getkey(unsigned char *key,unsigned char *check,int confirm)
  87. {
  88. unsigned char temp[16];
  89. MD5_CTX md5buf;
  90. unsigned k;
  91.  
  92. if(confirm) printf("\
  93. You need a passphrase to encrypt this disk. Your passphrase should be\n\
  94. fairly long, and should not appear verbatim in any literature or text.\n\
  95. Passphrases are case sensitive - this can be a security feature or it\n\
  96. can be a hazard. If you lose or forget your passphrase, there is no way\n\
  97. to recover the encrypted data!\n");
  98. enterpass:
  99. if(confirm) printf("\nEnter passphrase: ");
  100. invgets(pass1);
  101. if(confirm) {
  102.    int good=TRUE;
  103.    int upper=FALSE;
  104.    int lower=FALSE;
  105.    int other=FALSE;
  106.    unsigned i,j;
  107.    char c;
  108.    printf("\n");
  109.    i=strlen(pass1);
  110.    if(i<8) {
  111.        printf("Your passphrase is only %i characters long.\n",i);
  112.        good=FALSE; }
  113.    for(j=0;j<i;j++) {
  114.        c=pass1[j];
  115.        if(isupper(c)) upper=TRUE;
  116.        else if (islower(c)) lower=TRUE;
  117.        else if (!isspace(c)) other=TRUE; }
  118.    if(upper&&!lower&&!other) {
  119.        printf("Your passphrase is monocase (uppercase only).\n");
  120.        good=FALSE; }
  121.    if(lower&&!upper&&!other) {
  122.        printf("Your passphrase is monocase (lowercase only).\n");
  123.        good=FALSE; }
  124.    if(i>15) good=TRUE;
  125.    if(!good) printf("\nThis passphrase may not be secure.\
  126.  If you want to use it, retype it to\nconfirm. If not, press return to\
  127.  try again.\n\n");
  128.    printf("Re-enter passphrase: ");
  129.    invgets(pass2);
  130.    printf("\n");
  131.    if(strcmp(pass1,pass2)) {
  132.        printf("Passphrases do not match. Try again.\n");
  133.        goto enterpass; }
  134.    }
  135. setkey(key,check);
  136. }
  137.  
  138. void setkey(unsigned char *key,unsigned char *check)
  139.  {
  140.   unsigned char temp[16];
  141.   MD5_CTX md5buf;
  142.  
  143.   MD5Init(&md5buf);
  144.   MD5Update(&md5buf,pass1,strlen(pass1));
  145.   MD5Final(key,&md5buf);
  146.  
  147.   MD5Init(&md5buf);
  148.   MD5Update(&md5buf,key,16);
  149.   MD5Final(temp,&md5buf);
  150.   memcpy(check,temp,4);
  151.  }
  152.  
  153. struct tsrdata far *gettsradr(void)
  154. {
  155. unsigned seg;
  156. unsigned ofs;
  157. struct tsrdata far *ptr;
  158. struct REGPACK rgs;
  159.  
  160. rgs.r_ax=0x0800;
  161. rgs.r_dx=0x00f0;
  162. intr(0x13,&rgs);
  163. if(rgs.r_ax!=0x0edcb)
  164.     return( (struct tsrdata far *) NULL);
  165. ptr=(long) rgs.r_dx+(long) 0x10000*rgs.r_cx;
  166. return(ptr);
  167. }
  168.  
  169. void readptbl(unsigned drive,unsigned head,unsigned track,
  170.               unsigned char letter,unsigned *ptdrive,
  171.               unsigned *pthead,unsigned *pttrack)
  172. {
  173. unsigned i,pt,ph,pc;
  174. unsigned char buf[512];
  175. unsigned char *bufp;
  176. if(biosdisk(2,drive+0x80,head,track,1,1,buf)) return;
  177. for(i=0;i<4;i++) {
  178.     bufp=buf+0x1be+(i*16);
  179.     pt=bufp[4];
  180.     ph=bufp[1];
  181.     pc=bufp[3]+(bufp[2]>>6)*256;
  182.     if((pt==1)||(pt==4)||(pt==6))
  183.         if(letter=='C') {
  184.             *ptdrive=drive;
  185.             *pthead=ph;
  186.             *pttrack=pc;
  187.             return; }
  188.         else(letter--);
  189.     else if(pt==5) readptbl(drive,ph,pc,letter,
  190.                             ptdrive,pthead,pttrack); }
  191. if((drive==0)&&(*ptdrive==255)) readptbl(1,0,0,letter,
  192.                                          ptdrive,pthead,pttrack);
  193. }
  194.  
  195. /*      Compute IDEA encryption subkeys Z */
  196. void en_key_idea(word16 *userkey, word16 *Z)
  197. {
  198.         unsigned i,j;
  199.         word16 *Y=Z;
  200.         /*
  201.          * shifts
  202.          */
  203.         for (j=0; j<8; j++)
  204.                 Z[j] = *userkey++;
  205.  
  206.         for (i=0; j<KEYLEN; j++)
  207.         {       i++;
  208.                 Z[i+7] = Z[i & 7] << 9 | Z[i+1 & 7] >> 7;
  209.                 Z += i & 8;
  210.                 i &= 7;
  211.         }
  212.         for(i=0;i<52;i++)
  213.                 Y[i]^=0x0dae;
  214. }        /* en_key_idea */
  215.  
  216. void calcdiskparams(unsigned char *buf,unsigned *maxtrack,
  217.                     unsigned *maxhead,unsigned *maxsector,
  218.                     unsigned *secsize,unsigned serial[2])
  219. {
  220. unsigned long i,l0,l1,l2,l3;
  221. *maxsector=buf[0x18]+256*buf[0x19];
  222. *maxhead=buf[0x1a]+256*buf[0x1b];
  223. *secsize=buf[0x0b]+256*buf[0x0c];
  224. serial[0]=buf[0x27]+256*buf[0x28];
  225. serial[1]=buf[0x29]+256*buf[0x2a];
  226. l0=(unsigned char)buf[0x20];
  227. l1=(unsigned char)buf[0x21];
  228. l2=(unsigned char)buf[0x22];
  229. l3=(unsigned char)buf[0x23];
  230. i=l0+256*(l1+256*(l2+256*(l3)));
  231. if(i==0) i=(unsigned)buf[0x13]+256*buf[0x14];
  232. *maxtrack=i / *maxsector / *maxhead;
  233. if((i%(*maxsector * *maxhead))==0) (*maxtrack)--;
  234. }
  235.  
  236. void clrbufs(void)
  237.  {
  238.   int k;
  239.   for(k=0;k<MAXPASS;k++)
  240.   {
  241.     pass1[k]='\0';
  242.     pass2[k]='\0';
  243.   }
  244.  }
  245.