File: arjcrypt.c

package info (click to toggle)
arj 3.10.22-15
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,396 kB
  • sloc: ansic: 32,993; makefile: 2,033; sh: 1,547; asm: 436
file content (411 lines) | stat: -rw-r--r-- 10,125 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * $Id: arjcrypt.c,v 1.5 2003/06/22 11:12:28 andrew_belov Exp $
 * ---------------------------------------------------------------------------
 * This file is a small module that  performs stand-alone strong GOST 28147-89
 * encryption.
 *
 */

#include "arj.h"

#if TARGET==DOS
 #include <dos.h>
 #include "gost_asm.h"
 #include "det_x86.h"
#elif TARGET==OS2
#endif
#include "msg_crp.h"

#include "arjcrypt.h"

DEBUGHDR(__FILE__)                      /* Debug information block */

/* OS/2 DLL variable */

#if TARGET==OS2&&COMPILER==MSC
 int _acrtused=0;
#endif

/* To identify ourselves, we must have a signature: */

static char id[]="NortheastXXXXXXXX";   /* Currently unused */
#if TARGET==DOS
 static char signature[]=ARJCRYPT_SIG;
 static void entry();
 static unsigned short entry_point=(unsigned short)&entry;
 static int use_32=0;                   /* Allow 32-bit instructions */
#endif

/* Local data */

static unsigned long default_key[8]={3, 10, 6, 12, 5, 9, 0, 7};
static int last_bytes=0;                /* Number of significant bytes in the
					   last block */
static unsigned long back_code[2]={0L}; /* Recently encrypted data */
static unsigned long ext_code[2]={0L};  /* The code used by gost_cipher() */
static unsigned long gost_key[8]={0L};  /* Encryption key */
static unsigned long gost64_key[16]={0L};/* ARJCRYPT v 2.0 encryption key */
static int flags=0;                     /* Encryption type */
static int key64_len=0;                 /* Length of ARJCRYPT v 2 intial
					   encryption password */

#ifdef WORDS_BIGENDIAN
static const int ord[8]={3,2,1,0,7,6,5,4};

#define bf(x) ord[x]

static void adjust_byte_order(char *p,const int len)
{
 int l4;

 for (l4=len>>2;l4;l4--)
   {
   char tmp,*p1,*p2;

    p1   =  p +1;
    p2   =  p1+1;
    tmp  = *p2;
   *p2++ = *p1;
   *p1-- =  tmp;
    tmp  = *p1;
   *p1   = *p2;
   *p2   =  tmp;
    p    =  p2+1;
   }
}

static void codec(void (*fct)(unsigned char FAR *, unsigned char FAR *, int), unsigned char FAR *buf, int len)
{
if (!(len&7) && !last_bytes) adjust_byte_order(buf,len);
(*fct)(buf,buf,len);
if (!(len&7) && !last_bytes) adjust_byte_order(buf,len);
}
#else

#define bf(x) (x)

#define codec(fct,buf,len) (fct(buf,buf,len))
#endif


/* GOST encoding/decoding loop */

static void gost_loop(unsigned long *src, unsigned long *dest, unsigned long *key)
{
 unsigned long mod1, mod2;
 int i;

 #if TARGET==DOS
  if(use_32)
  {
   gost_loop_32(src, dest, key);
   return;
  }
 #endif
 mod1=src[0];
 mod2=src[1];
 for(i=0; i<3; i++)
 {
  mod2^=gost_term(mod1+key[0]);
  mod1^=gost_term(mod2+key[1]);
  mod2^=gost_term(mod1+key[2]);
  mod1^=gost_term(mod2+key[3]);
  mod2^=gost_term(mod1+key[4]);
  mod1^=gost_term(mod2+key[5]);
  mod2^=gost_term(mod1+key[6]);
  mod1^=gost_term(mod2+key[7]);
 }
 mod2^=gost_term(mod1+key[7]);
 mod1^=gost_term(mod2+key[6]);
 mod2^=gost_term(mod1+key[5]);
 mod1^=gost_term(mod2+key[4]);
 mod2^=gost_term(mod1+key[3]);
 mod1^=gost_term(mod2+key[2]);
 mod2^=gost_term(mod1+key[1]);
 mod1^=gost_term(mod2+key[0]);
 dest[0]=mod2;
 dest[1]=mod1;
}

/* So-called "gamma"-ciphering that does both encoding and decoding */

static void gost_cipher_proc(unsigned char FAR *src, unsigned char FAR *dest, int len)
{
 unsigned long FAR *tmp_sptr;           /* Pointer to source area */
 unsigned long FAR *tmp_dptr;           /* Pointer to target area */
 int remainder;                         /* Number of bytes in the last block */

 remainder=len%8;
 if(remainder==0&&last_bytes==0)
 {
  tmp_sptr=(unsigned long *)src;
  tmp_dptr=(unsigned long *)dest;
  len>>=3;
  while(len--!=0)
  {
   back_code[0]+=GOST_I_PAT_LO;
   if(back_code[0]<GOST_I_PAT_LO)
    back_code[0]++;
   back_code[1]+=GOST_I_PAT_HI;
   if(back_code[1]<GOST_I_PAT_HI)
    back_code[1]++;
   gost_loop(back_code, ext_code, gost_key);
   tmp_dptr[0]=tmp_sptr[0]^ext_code[0];
   tmp_dptr[1]=tmp_sptr[1]^ext_code[1];
   tmp_sptr+=2;
   tmp_dptr+=2;
  }
 }
 else
 {
  while(len--!=0)
  {
   if(last_bytes==0)
   {
    back_code[0]+=GOST_I_PAT_LO;
    if(back_code[0]<GOST_I_PAT_LO)
     back_code[0]++;
    back_code[1]+=GOST_I_PAT_HI;
    if(back_code[1]<GOST_I_PAT_HI)
     back_code[1]++;
    gost_loop(back_code, ext_code, gost_key);
   }
   *(dest++)=*(src++)^ext_code[bf(last_bytes)];
   last_bytes%=8;
  }
 }
}

/* Encoding sequence */

static void gost_encode(unsigned char FAR *src, unsigned char FAR *dest, int len)
{
 unsigned long FAR *tmp_sptr;           /* Pointer to source area */
 unsigned long FAR *tmp_dptr;           /* Pointer to target area */
 int remainder;                         /* Number of bytes in the last block */
 unsigned char *bc_offset;              /* Offset within back_code */

 remainder=len%8;
 if(remainder==0&&last_bytes==0)
 {
  tmp_sptr=(unsigned long FAR *)src;
  tmp_dptr=(unsigned long FAR *)dest;
  len>>=3;
  while(len--!=0)
  {
   gost_loop(back_code, back_code, gost_key);
   back_code[0]=tmp_dptr[0]=tmp_sptr[0]^back_code[0];
   back_code[1]=tmp_dptr[1]=tmp_sptr[1]^back_code[1];
   tmp_sptr+=2;
   tmp_dptr+=2;
  }
 }
 else
 {
  bc_offset=(unsigned char *)back_code;
  while(len--!=0)
  {
   if(last_bytes==0)
    gost_loop(back_code, back_code, gost_key);
   bc_offset[bf(last_bytes)]=*(dest++)=*(src++)^bc_offset[bf(last_bytes)];
   last_bytes++;
   last_bytes%=8;
  }
 }
}

/* Decoding sequence */

static void gost_decode(unsigned char FAR *src, unsigned char FAR *dest, int len)
{
 unsigned long FAR *tmp_sptr;           /* Pointer to source area */
 unsigned long FAR *tmp_dptr;           /* Pointer to target area */
 int remainder;                         /* Number of bytes in the last block */
 unsigned long d_data;                  /* Decoded data collector */
 unsigned char *bc_offset;              /* Offset within back_code */
 unsigned char dec_sym;                 /* Currently processed symbol */

 remainder=len%8;
 if(remainder==0&&last_bytes==0)
 {
  tmp_sptr=(unsigned long FAR *)src;
  tmp_dptr=(unsigned long FAR *)dest;
  len>>=3;
  while(len--!=0)
  {
   gost_loop(back_code, back_code, gost_key);
   d_data=tmp_sptr[0];
   tmp_dptr[0]=d_data^back_code[0];
   back_code[0]=d_data;
   d_data=tmp_sptr[1];
   tmp_dptr[1]=d_data^back_code[1];
   back_code[1]=d_data;
   tmp_sptr+=2;
   tmp_dptr+=2;
  }
 }
 else
 {
  bc_offset=(unsigned char *)back_code;
  while(len--!=0)
  {
   if(last_bytes==0)
    gost_loop(back_code, back_code, gost_key);
   dec_sym=*(src++);
   *(dest++)=dec_sym^bc_offset[bf(last_bytes)];
   bc_offset[bf(last_bytes++)]=dec_sym;
   last_bytes%=8;
  }
 }
}

/* Copies <len> characters of a string, appending a null byte to the result */

static int far_strncpy(char FAR *dest, char FAR *src, int limit)
{
 int k;
 char *d;

 d=dest;
 for(k=0; k<limit; k++)
 {
  if(*src!='\0')
   *(d++)=*(src++);
  else
  {
   *d='\0';
   break;
  }
 }
 #ifdef WORDS_BIGENDIAN
 adjust_byte_order(dest,limit);
 #endif
 return(k);
}

/* Key creation */

static void gost_crtkey(unsigned long *seed)
{
 unsigned long tmp_key[8];
 int i;

 memcpy(tmp_key, gost_key, sizeof(tmp_key));
 gost_loop(seed, back_code, default_key);
 for(i=0; i<KEYGEN_ITERATIONS; i++)
  gost_encode((unsigned char FAR *)tmp_key, (unsigned char FAR *)tmp_key, sizeof(tmp_key));
 if(flags!=ENCRYPT_GOST256&&key64_len>sizeof(gost_key))
 {
  for(i=0; i<8; i++)
   gost_key[i]=gost64_key[i+8];
  for(i=0; i<KEYGEN_ITERATIONS; i++)
   gost_encode((unsigned char FAR *)tmp_key, (unsigned char FAR *)tmp_key, sizeof(tmp_key));
 }
 memcpy(gost_key, tmp_key, sizeof(gost_key));
}

/* Simplified string output routine */

#if TARGET==DOS
static void out_str(char *str)
{
 union REGS r;

 r.h.ah=0x40;
 r.x.bx=1;
 r.x.cx=strlen(str);
 r.x.dx=(unsigned short)str;
 intdos(&r, &r);
}
#endif

/* Main routine - just a stub. Don't even need it in an OS/2 DLL. */

#if TARGET==DOS
int main()
{
 out_str(M_ARJCRYPT_BANNER);
 return(0);
}
#endif

/* External entry */

#if TARGET==DOS
static void entry()
#elif TARGET==OS2
EXPENTRY entry(struct arjcrypt_exblock FAR *exblock_ptr)
#elif TARGET==WIN32
VOID entry(struct arjcrypt_exblock FAR *exblock_ptr)
#else
void entry(struct arjcrypt_exblock FAR *exblock_ptr)
#endif
{
 #if TARGET==DOS
  static unsigned short rcx, rdx;
  struct arjcrypt_exblock FAR *exblock_ptr;
 #endif
 unsigned long modifier[2];

 #if TARGET==DOS
  asm{
   mov word ptr rcx, cx
   mov word ptr rdx, dx
  }
  exblock_ptr=MK_FP(rcx, rdx);
 #endif
 switch(exblock_ptr->mode)
 {
  case ARJCRYPT_INIT:
   #if TARGET==DOS
    use_32=detect_x86()==0x386;
   #endif
   memset(gost_key, 0, sizeof(gost_key));
   far_strncpy((char FAR *)gost_key, exblock_ptr->password, sizeof(gost_key));
   modifier[0]=exblock_ptr->l_modifier[0];
   modifier[1]=exblock_ptr->l_modifier[1];
   flags=ENCRYPT_GOST256;
   last_bytes=0;
   calc_gost_pattern();
   gost_crtkey(modifier);
   gost_loop(modifier, back_code, gost_key);
   exblock_ptr->rc=ARJCRYPT_RC_INITIALIZED;
   break;
  case ARJCRYPT_V2_INIT:
   #if TARGET==DOS
    use_32=detect_x86()==0x386;
   #endif
   memset(gost_key, 0, sizeof(gost_key));
   far_strncpy((char FAR *)gost_key, exblock_ptr->password, sizeof(gost_key));
   memset(gost64_key, 0, sizeof(gost64_key));
   key64_len=far_strncpy((char FAR *)gost64_key, exblock_ptr->password, sizeof(gost64_key));
   modifier[0]=exblock_ptr->l_modifier[0];
   modifier[1]=exblock_ptr->l_modifier[1];
   flags=exblock_ptr->flags;
   last_bytes=0;
   calc_gost_pattern();
   gost_crtkey(modifier);
   gost_loop(modifier, back_code, gost_key);
   exblock_ptr->rc=(flags==ENCRYPT_GOST256||key64_len<=32)?ARJCRYPT_RC_INITIALIZED:ARJCRYPT_RC_INIT_V2;
   break;
  case ARJCRYPT_ENCODE:
   codec(gost_encode, exblock_ptr->data, exblock_ptr->len);
   exblock_ptr->rc=ARJCRYPT_RC_OK;
   break;
  case ARJCRYPT_DECODE:
   codec(gost_decode, exblock_ptr->data, exblock_ptr->len);
   exblock_ptr->rc=ARJCRYPT_RC_OK;
   break;
  case ARJCRYPT_CIPHER:
  case ARJCRYPT_DECIPHER:
   codec(gost_cipher_proc, exblock_ptr->data, exblock_ptr->len);
   exblock_ptr->rc=ARJCRYPT_RC_OK;
   break;
  default:
   exblock_ptr->rc=ARJCRYPT_RC_ERROR;
   break;
 }
 #if TARGET==DOS
  exblock_ptr->ret_addr();
 #endif
}