Exemplo n.º 1
0
void DES_ede3_cfb_encrypt(const unsigned char *in, unsigned char *out,
                          int numbits, long length, DES_key_schedule *ks1,
                          DES_key_schedule *ks2, DES_key_schedule *ks3,
                          DES_cblock *ivec, int enc)
{
    DES_LONG d0, d1, v0, v1;
    unsigned long l = length, n = ((unsigned int)numbits + 7) / 8;
    int num = numbits, i;
    DES_LONG ti[2];
    unsigned char *iv;
    unsigned char ovec[16];

    if (num > 64)
        return;
    iv = &(*ivec)[0];
    c2l(iv, v0);
    c2l(iv, v1);
    if (enc) {
        while (l >= n) {
            l -= n;
            ti[0] = v0;
            ti[1] = v1;
            DES_encrypt3(ti, ks1, ks2, ks3);
            c2ln(in, d0, d1, n);
            in += n;
            d0 ^= ti[0];
            d1 ^= ti[1];
            l2cn(d0, d1, out, n);
            out += n;
            /*
             * 30-08-94 - eay - changed because l>>32 and l<<32 are bad under
             * gcc :-(
             */
            if (num == 32) {
                v0 = v1;
                v1 = d0;
            } else if (num == 64) {
                v0 = d0;
                v1 = d1;
            } else {
                iv = &ovec[0];
                l2c(v0, iv);
                l2c(v1, iv);
                l2c(d0, iv);
                l2c(d1, iv);
                /* shift ovec left most of the bits... */
                memmove(ovec, ovec + num / 8, 8 + (num % 8 ? 1 : 0));
                /* now the remaining bits */
                if (num % 8 != 0)
                    for (i = 0; i < 8; ++i) {
                        ovec[i] <<= num % 8;
                        ovec[i] |= ovec[i + 1] >> (8 - num % 8);
                    }
                iv = &ovec[0];
                c2l(iv, v0);
                c2l(iv, v1);
            }
        }
    } else {
        while (l >= n) {
Exemplo n.º 2
0
/* The input and output encrypted as though 64bit cfb mode is being used. The
 * extra state information to record how much of the 64bit block we have used
 * is contained in *num; */
void DES_ede3_cfb64_encrypt(const uint8_t *in, uint8_t *out,
                            long length, DES_key_schedule *ks1,
                            DES_key_schedule *ks2, DES_key_schedule *ks3,
                            DES_cblock *ivec, int *num, int enc) {
  uint32_t v0, v1;
  long l = length;
  int n = *num;
  uint32_t ti[2];
  uint8_t *iv, c, cc;

  iv = ivec->bytes;
  if (enc) {
    while (l--) {
      if (n == 0) {
        c2l(iv, v0);
        c2l(iv, v1);

        ti[0] = v0;
        ti[1] = v1;
        DES_encrypt3(ti, ks1, ks2, ks3);
        v0 = ti[0];
        v1 = ti[1];

        iv = ivec->bytes;
        l2c(v0, iv);
        l2c(v1, iv);
        iv = ivec->bytes;
      }
      c = *(in++) ^ iv[n];
      *(out++) = c;
      iv[n] = c;
      n = (n + 1) & 0x07;
    }
  } else {
    while (l--) {
      if (n == 0) {
        c2l(iv, v0);
        c2l(iv, v1);

        ti[0] = v0;
        ti[1] = v1;
        DES_encrypt3(ti, ks1, ks2, ks3);
        v0 = ti[0];
        v1 = ti[1];

        iv = ivec->bytes;
        l2c(v0, iv);
        l2c(v1, iv);
        iv = ivec->bytes;
      }
      cc = *(in++);
      c = iv[n];
      iv[n] = cc;
      *(out++) = c ^ cc;
      n = (n + 1) & 0x07;
    }
  }
  v0 = v1 = ti[0] = ti[1] = c = cc = 0;
  *num = n;
}
Exemplo n.º 3
0
static void
cryptodev_des_encrypt(
	DES_LONG *data,
	des_key_schedule ks,
	int enc)
{
	if (cryptodev_fd != -1) {
		char key[8];
		des_cblock iv;
		des_cblock datac;
		register DES_LONG l;
		unsigned char *p;

		p=&datac[0];
		l=data[0]; l2c(l,p);
		l=data[1]; l2c(l,p);

		memcpy(key, ks, 8);
		memset(&iv, 0, sizeof(des_cblock));
		/* single block ecb == single block cbc with iv=0 */
		cryptodev_des_cryptodev_internal(
			CRYPTO_DES_CBC,
			key,
			enc ? COP_ENCRYPT : COP_DECRYPT,
			&datac,
			&datac,
			sizeof(des_cblock),
			&iv,
			sizeof(des_cblock));

		p=datac;
		c2l(p,l); data[0]=l;
		c2l(p,l); data[1]=l;
	}
}
Exemplo n.º 4
0
void
des_ecb3_encrypt(des_cblock (*input), des_cblock (*output),
    des_key_schedule ks1, des_key_schedule ks2, des_key_schedule ks3,
    int encrypt)
{
	register u_int32_t l0, l1;
	register unsigned char *in, *out;
	u_int32_t ll[2];

	in = (unsigned char *) input;
	out = (unsigned char *) output;
	c2l(in, l0);
	c2l(in, l1);
	IP(l0, l1);
	ll[0] = l0;
	ll[1] = l1;
	des_encrypt2(ll, ks1, encrypt);
	des_encrypt2(ll, ks2, !encrypt);
	des_encrypt2(ll, ks3, encrypt);
	l0 = ll[0];
	l1 = ll[1];
	FP(l1, l0);
	l2c(l0, out);
	l2c(l1, out);
}
Exemplo n.º 5
0
/* The input and output are loaded in multiples of 8 bits.
 * What this means is that if you hame numbits=12 and length=2
 * the first 12 bits will be retrieved from the first byte and half
 * the second.  The second 12 bits will come from the 3rd and half the 4th
 * byte.
 */
void des_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
	     long length, des_key_schedule schedule, des_cblock *ivec, int enc)
	{
	register DES_LONG d0,d1,v0,v1,n=(numbits+7)/8;
	register DES_LONG mask0,mask1;
	register unsigned long l=length;
	register int num=numbits;
	DES_LONG ti[2];
	unsigned char *iv;

	if (num > 64) return;
	if (num > 32)
		{
		mask0=0xffffffffL;
		if (num == 64)
			mask1=mask0;
		else	mask1=(1L<<(num-32))-1;
		}
	else
		{
		if (num == 32)
			mask0=0xffffffffL;
		else	mask0=(1L<<num)-1;
		mask1=0x00000000L;
		}

	iv = &(*ivec)[0];
	c2l(iv,v0);
	c2l(iv,v1);
	if (enc)
		{
		while (l >= n)
			{
			l-=n;
			ti[0]=v0;
			ti[1]=v1;
			des_encrypt((DES_LONG *)ti,schedule,DES_ENCRYPT);
			c2ln(in,d0,d1,n);
			in+=n;
			d0=(d0^ti[0])&mask0;
			d1=(d1^ti[1])&mask1;
			l2cn(d0,d1,out,n);
			out+=n;
			/* 30-08-94 - eay - changed because l>>32 and
			 * l<<32 are bad under gcc :-( */
			if (num == 32)
				{ v0=v1; v1=d0; }
			else if (num == 64)
				{ v0=d0; v1=d1; }
			else if (num > 32) /* && num != 64 */
				{
				v0=((v1>>(num-32))|(d0<<(64-num)))&0xffffffffL;
				v1=((d0>>(num-32))|(d1<<(64-num)))&0xffffffffL;
				}
			else /* num < 32 */
				{
				v0=((v0>>num)|(v1<<(32-num)))&0xffffffffL;
				v1=((v1>>num)|(d0<<(32-num)))&0xffffffffL;
				}
			}
Exemplo n.º 6
0
void MD5_Update(MD5_CTX *c, register unsigned char *data, MD5_LONG len)
{
	register ULONG *p;
	int sw,sc;
	ULONG l;

	if (len == 0) return;

	l=(c->Nl+(len<<3))&0xffffffffL;
	/* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
	 * Wei Dai <*****@*****.**> for pointing it out. */
	if (l < c->Nl) /* overflow */
		c->Nh++;
	c->Nh+=(len>>29);
	c->Nl=l;

	if (c->num != 0) {
		p=c->data;
		sw=c->num>>2;
		sc=c->num&0x03;

		if ((c->num+len) >= MD5_CBLOCK) {
			l= p[sw];
			p_c2l(data,l,sc);
			p[sw++]=l;
			for (; sw<MD5_LBLOCK; sw++) {
				c2l(data,l);
				p[sw]=l;
			}
			len-=(MD5_CBLOCK-c->num);

			md5_block(c,p);
			c->num=0;
			/* drop through and do the rest */
		} else {
			int ew,ec;

			c->num+=(int)len;
			if ((sc+len) < 4) { /* ugly, add char's to a word */
				l= p[sw];
				p_c2l_p(data,l,sc,len);
				p[sw]=l;
			} else {
				ew=(c->num>>2);
				ec=(c->num&0x03);
				l= p[sw];
				p_c2l(data,l,sc);
				p[sw++]=l;
				for (; sw < ew; sw++)
					{ c2l(data,l); p[sw]=l; }
				if (ec) {
					c2l_p(data,l,ec);
					p[sw]=l;
				}
			}
			return;
		}
	}
Exemplo n.º 7
0
void DES_cfb64_encrypt(const unsigned char *in, unsigned char *out,
                       long length, DES_key_schedule *schedule,
                       DES_cblock *ivec, int *num, int enc)
{
    register DES_LONG v0, v1;
    register long l = length;
    register int n = *num;
    DES_LONG ti[2];
    unsigned char *iv, c, cc;

    iv = &(*ivec)[0];
    if (enc) {
        while (l--) {
            if (n == 0) {
                c2l(iv, v0);
                ti[0] = v0;
                c2l(iv, v1);
                ti[1] = v1;
                DES_encrypt1(ti, schedule, DES_ENCRYPT);
                iv = &(*ivec)[0];
                v0 = ti[0];
                l2c(v0, iv);
                v0 = ti[1];
                l2c(v0, iv);
                iv = &(*ivec)[0];
            }
            c = *(in++) ^ iv[n];
            *(out++) = c;
            iv[n] = c;
            n = (n + 1) & 0x07;
        }
    } else {
        while (l--) {
            if (n == 0) {
                c2l(iv, v0);
                ti[0] = v0;
                c2l(iv, v1);
                ti[1] = v1;
                DES_encrypt1(ti, schedule, DES_ENCRYPT);
                iv = &(*ivec)[0];
                v0 = ti[0];
                l2c(v0, iv);
                v0 = ti[1];
                l2c(v0, iv);
                iv = &(*ivec)[0];
            }
            cc = *(in++);
            c = iv[n];
            iv[n] = cc;
            *(out++) = c ^ cc;
            n = (n + 1) & 0x07;
        }
    }
    v0 = v1 = ti[0] = ti[1] = c = cc = 0;
    *num = n;
}
Exemplo n.º 8
0
void RC2_cfb64_encrypt(const unsigned char *in, unsigned char *out,
		       long length, RC2_KEY *schedule, unsigned char *ivec,
		       int *num, int encrypt)
	{
	register unsigned long v0,v1,t;
	register int n= *num;
	register long l=length;
	unsigned long ti[2];
	unsigned char *iv,c,cc;

	iv=(unsigned char *)ivec;
	if (encrypt)
		{
		while (l--)
			{
			if (n == 0)
				{
				c2l(iv,v0); ti[0]=v0;
				c2l(iv,v1); ti[1]=v1;
				RC2_encrypt((unsigned long *)ti,schedule);
				iv=(unsigned char *)ivec;
				t=ti[0]; l2c(t,iv);
				t=ti[1]; l2c(t,iv);
				iv=(unsigned char *)ivec;
				}
			c= *(in++)^iv[n];
			*(out++)=c;
			iv[n]=c;
			n=(n+1)&0x07;
			}
		}
	else
		{
		while (l--)
			{
			if (n == 0)
				{
				c2l(iv,v0); ti[0]=v0;
				c2l(iv,v1); ti[1]=v1;
				RC2_encrypt((unsigned long *)ti,schedule);
				iv=(unsigned char *)ivec;
				t=ti[0]; l2c(t,iv);
				t=ti[1]; l2c(t,iv);
				iv=(unsigned char *)ivec;
				}
			cc= *(in++);
			c=iv[n];
			iv[n]=cc;
			*(out++)=c^cc;
			n=(n+1)&0x07;
			}
		}
	v0=v1=ti[0]=ti[1]=t=c=cc=0;
	*num=n;
	}
/* The input and output encrypted as though 64bit ofb mode is being
 * used.  The extra state information to record how much of the
 * 64bit block we have used is contained in *num;
 */
void DES_ede3_ofb64_encrypt(register const unsigned char *in,
			    register unsigned char *out, long length,
			    DES_key_schedule *k1, DES_key_schedule *k2,
			    DES_key_schedule *k3, DES_cblock *ivec,
			    int *num)
	{
	register DES_LONG v0,v1;
	register int n= *num;
	register long l=length;
	DES_cblock d;
	register char *dp;
	DES_LONG ti[2];
	unsigned char *iv;
	int save=0;

	iv = &(*ivec)[0];
	c2l(iv,v0);
	c2l(iv,v1);
	ti[0]=v0;
	ti[1]=v1;
	dp=(char *)d;
	l2c(v0,dp);
	l2c(v1,dp);
	while (l--)
		{
		if (n == 0)
			{
			/* ti[0]=v0; */
			/* ti[1]=v1; */
			DES_encrypt3(ti,k1,k2,k3);
			v0=ti[0];
			v1=ti[1];

			dp=(char *)d;
			l2c(v0,dp);
			l2c(v1,dp);
			save++;
			}
		*(out++)= *(in++)^d[n];
		n=(n+1)&0x07;
		}
	if (save)
		{
/*		v0=ti[0];
		v1=ti[1];*/
		iv = &(*ivec)[0];
		l2c(v0,iv);
		l2c(v1,iv);
		}
	v0=v1=ti[0]=ti[1]=0;
	*num=n;
	}
Exemplo n.º 10
0
void RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
		    int rounds)
#endif
	{
	RC5_32_INT L[64],l,ll,A,B,*S,k;
	int i,j,m,c,t,ii,jj;

	if (	(rounds != RC5_16_ROUNDS) &&
		(rounds != RC5_12_ROUNDS) &&
		(rounds != RC5_8_ROUNDS))
		rounds=RC5_16_ROUNDS;

	key->rounds=rounds;
	S= &(key->data[0]);
	j=0;
	for (i=0; i<=(len-8); i+=8)
		{
		c2l(data,l);
		L[j++]=l;
		c2l(data,l);
		L[j++]=l;
		}
	ii=len-i;
	if (ii)
		{
		k=len&0x07;
		c2ln(data,l,ll,k);
		L[j+0]=l;
		L[j+1]=ll;
		}

	c=(len+3)/4;
	t=(rounds+1)*2;
	S[0]=RC5_32_P;
	for (i=1; i<t; i++)
		S[i]=(S[i-1]+RC5_32_Q)&RC5_32_MASK;

	j=(t>c)?t:c;
	j*=3;
	ii=jj=0;
	A=B=0;
	for (i=0; i<j; i++)
		{
		k=(S[ii]+A+B)&RC5_32_MASK;
		A=S[ii]=ROTATE_l32(k,3);
		m=(int)(A+B);
		k=(L[jj]+A+B)&RC5_32_MASK;
		B=L[jj]=ROTATE_l32(k,m);
		if (++ii >= t) ii=0;
		if (++jj >= c) jj=0;
		}
	}
Exemplo n.º 11
0
void RC2_ecb_encrypt(const unsigned char *in, unsigned char *out, RC2_KEY *ks,
		     int encrypt)
	{
	unsigned long l,d[2];

	c2l(in,l); d[0]=l;
	c2l(in,l); d[1]=l;
	if (encrypt)
		RC2_encrypt(d,ks);
	else
		RC2_decrypt(d,ks);
	l=d[0]; l2c(l,out);
	l=d[1]; l2c(l,out);
	l=d[0]=d[1]=0;
	}
Exemplo n.º 12
0
void DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output,
		     DES_key_schedule *ks, int enc)
	{
	register DES_LONG l;
	DES_LONG ll[2];
	const unsigned char *in = &(*input)[0];
	unsigned char *out = &(*output)[0];

	c2l(in,l); ll[0]=l;
	c2l(in,l); ll[1]=l;
	DES_encrypt1(ll,ks,enc);
	l=ll[0]; l2c(l,out);
	l=ll[1]; l2c(l,out);
	l=ll[0]=ll[1]=0;
	}
Exemplo n.º 13
0
void osDesDecrypt(DES_key_schedule *ks, 
	const_DES_cblock *input, 
	DES_cblock *output)
{
	/* copied from openssl's DES_ecb_encrypt() */
	register DES_LONG l;
	DES_LONG ll[2];
	const unsigned char *in = &(*input)[0];
	unsigned char *out = &(*output)[0];

	c2l(in,l); ll[0]=l;
	c2l(in,l); ll[1]=l;
	DES_encrypt1(ll,ks,0);
	l=ll[0]; l2c(l,out);
	l=ll[1]; l2c(l,out);
	l=ll[0]=ll[1]=0;
}
Exemplo n.º 14
0
void rc5_key_setup(uint8_t *key, int dummy, rc5key_info *key_info) {
        
  uint32_t *L, l, A, B, *S, k;
  uint8_t  ii, jj, m;
  int8_t   i;
  uint8_t  tmp[16];
 
  S = key_info->vector;
  c2l(key, l);
  L = (uint32_t *) tmp;
  L[0] = l;
  key += 4;
  c2l(key, l);
  L[1] = l;

  key += 4;
  c2l(key, l);
  L[2] = l;
  key += 4;
  c2l(key, l);
  L[3] = l;

  S[0] = RC5_32_P;
  for (i = 1; i < RC5_KEYSPACES; i ++) {
    S[i] = (S[i - 1] + RC5_32_Q);
  }
  ii = jj = 0;
  A = B = 0;
  S = key_info->vector;
  for (i = 3 * ( RC5_KEYSPACES) - 1; i >= 0; i --) {
    k = (*S + A + B) & RC5_32_MASK;
    rotl32((k), (3));
    A = *S = k;
    S ++;
    m = ((char)(A+B)) & 0x1f;
    k = (*L + A + B) & RC5_32_MASK;
    rotl32((k), (m));
    B = *L = k;
    if (++ii >= RC5_KEYSPACES) {
      ii = 0;
      S = key_info->vector;
    }
    jj = (jj + 4) & 0x0f;
    L = (uint32_t *) (&tmp[jj]);
   }
}
Exemplo n.º 15
0
/* The input and output encrypted as though 64bit ofb mode is being
 * used.  The extra state information to record how much of the
 * 64bit block we have used is contained in *num;
 */
void DES_ofb64_encrypt(register const unsigned char *in,
		       register unsigned char *out, long length,
		       DES_key_schedule *schedule, DES_cblock *ivec, int *num)
	{
	register DES_LONG v0,v1,t;
	register int n= *num;
	register long l=length;
	DES_cblock d;
	register unsigned char *dp;
	DES_LONG ti[2];
	unsigned char *iv;
	int save=0;

	iv = &(*ivec)[0];
	c2l(iv,v0);
	c2l(iv,v1);
	ti[0]=v0;
	ti[1]=v1;
	dp=d;
	l2c(v0,dp);
	l2c(v1,dp);
	while (l--)
		{
		if (n == 0)
			{
			DES_encrypt1(ti,schedule,DES_ENCRYPT);
			dp=d;
			t=ti[0]; l2c(t,dp);
			t=ti[1]; l2c(t,dp);
			save++;
			}
		*(out++)= *(in++)^d[n];
		n=(n+1)&0x07;
		}
	if (save)
		{
		v0=ti[0];
		v1=ti[1];
		iv = &(*ivec)[0];
		l2c(v0,iv);
		l2c(v1,iv);
		}
	t=v0=v1=ti[0]=ti[1]=0;
	*num=n;
	}
Exemplo n.º 16
0
void DES_pcbc_encrypt(const unsigned char *input, unsigned char *output,
                      long length, DES_key_schedule *schedule,
                      DES_cblock *ivec, int enc)
{
    register DES_LONG sin0, sin1, xor0, xor1, tout0, tout1;
    DES_LONG tin[2];
    const unsigned char *in;
    unsigned char *out, *iv;

    in = input;
    out = output;
    iv = &(*ivec)[0];

    if (enc) {
        c2l(iv, xor0);
        c2l(iv, xor1);
        for (; length > 0; length -= 8) {
            if (length >= 8) {
                c2l(in, sin0);
                c2l(in, sin1);
            } else
                c2ln(in, sin0, sin1, length);
            tin[0] = sin0 ^ xor0;
            tin[1] = sin1 ^ xor1;
            DES_encrypt1((DES_LONG *)tin, schedule, DES_ENCRYPT);
            tout0 = tin[0];
            tout1 = tin[1];
            xor0 = sin0 ^ tout0;
            xor1 = sin1 ^ tout1;
            l2c(tout0, out);
            l2c(tout1, out);
        }
    } else {
        c2l(iv, xor0);
        c2l(iv, xor1);
        for (; length > 0; length -= 8) {
            c2l(in, sin0);
            c2l(in, sin1);
            tin[0] = sin0;
            tin[1] = sin1;
            DES_encrypt1((DES_LONG *)tin, schedule, DES_DECRYPT);
            tout0 = tin[0] ^ xor0;
            tout1 = tin[1] ^ xor1;
            if (length >= 8) {
                l2c(tout0, out);
                l2c(tout1, out);
            } else
                l2cn(tout0, tout1, out, length);
            xor0 = tout0 ^ sin0;
            xor1 = tout1 ^ sin1;
        }
    }
    tin[0] = tin[1] = 0;
    sin0 = sin1 = xor0 = xor1 = tout0 = tout1 = 0;
}
Exemplo n.º 17
0
/* The input and output encrypted as though 64bit ofb mode is being
 * used.  The extra state information to record how much of the
 * 64bit block we have used is contained in *num;
 */
void RC5_32_ofb64_encrypt(const unsigned char *in, unsigned char *out,
			  long length, RC5_32_KEY *schedule,
			  unsigned char *ivec, int *num)
	{
	unsigned long v0,v1,t;
	int n= *num;
	long l=length;
	unsigned char d[8];
	char *dp;
	unsigned long ti[2];
	unsigned char *iv;
	int save=0;

	iv=(unsigned char *)ivec;
	c2l(iv,v0);
	c2l(iv,v1);
	ti[0]=v0;
	ti[1]=v1;
	dp=(char *)d;
	l2c(v0,dp);
	l2c(v1,dp);
	while (l--)
		{
		if (n == 0)
			{
			RC5_32_encrypt((unsigned long *)ti,schedule);
			dp=(char *)d;
			t=ti[0]; l2c(t,dp);
			t=ti[1]; l2c(t,dp);
			save++;
			}
		*(out++)= *(in++)^d[n];
		n=(n+1)&0x07;
		}
	if (save)
		{
		v0=ti[0];
		v1=ti[1];
		iv=(unsigned char *)ivec;
		l2c(v0,iv);
		l2c(v1,iv);
		}
	t=v0=v1=ti[0]=ti[1]=0;
	*num=n;
	}
Exemplo n.º 18
0
void osDes3Decrypt(DES3_Schedule *ks, 
	const_DES_cblock *input, 
	DES_cblock *output)
{
	register DES_LONG l;
	DES_LONG ll[2];
	const unsigned char *in = &(*input)[0];
	unsigned char *out = &(*output)[0];

	c2l(in,l); ll[0]=l;
	c2l(in,l); ll[1]=l;
	DES_encrypt1(ll,&ks->ks[2],0);
	DES_encrypt1(ll,&ks->ks[1],1);
	DES_encrypt1(ll,&ks->ks[0],0);
	l=ll[0]; l2c(l,out);
	l=ll[1]; l2c(l,out);
	l=ll[0]=ll[1]=0;
}
Exemplo n.º 19
0
void rtsmb_des_ecb_encrypt(const_rtsmb_des_cblock *input, rtsmb_des_cblock *output,
	     rtsmb_des_key_schedule ks,
	     int enc)
	{
	register RTSMB_DES_LONG l;
	RTSMB_DES_LONG ll[2];
	const unsigned char *in;
	unsigned char *out;

	in = &(*input)[0];
	out = &(*output)[0];
	
	c2l(in,l); ll[0]=l;
	c2l(in,l); ll[1]=l;
	rtsmb_des_encrypt(ll,ks,enc);
	l=ll[0]; l2c(l,out);
	l=ll[1]; l2c(l,out);
	l=ll[0]=ll[1]=0;
	}
Exemplo n.º 20
0
void DES_ecb3_encrypt(const unsigned char *in, unsigned char *out,
		      DES_key_schedule *ks1, DES_key_schedule *ks2,
		      DES_key_schedule *ks3,
	     int enc)
	{
	register DES_LONG l0,l1;
	DES_LONG ll[2];

	c2l(in,l0);
	c2l(in,l1);
	ll[0]=l0;
	ll[1]=l1;
	if (enc)
		DES_encrypt3(ll,ks1,ks2,ks3);
	else
		DES_decrypt3(ll,ks1,ks2,ks3);
	l0=ll[0];
	l1=ll[1];
	l2c(l0,out);
	l2c(l1,out);
	}
Exemplo n.º 21
0
static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len)
{
    register DES_LONG tin0, tin1;
    register DES_LONG ttin0, ttin1;
    DES_LONG d[2], dd[2];
    DES_key_schedule k;
    unsigned char *p;
    size_t i;

    for (i = 0; i < len; i += 8) {
        c2l(in, tin0);
        d[0] = dd[0] = tin0;
        c2l(in, tin1);
        d[1] = dd[1] = tin1;
        c->h[0] = (c->h[0] & 0x9f) | 0x40;
        c->hh[0] = (c->hh[0] & 0x9f) | 0x20;

        DES_set_odd_parity(&c->h);
        DES_set_key_unchecked(&c->h, &k);
        DES_encrypt1(d, &k, 1);

        DES_set_odd_parity(&c->hh);
        DES_set_key_unchecked(&c->hh, &k);
        DES_encrypt1(dd, &k, 1);

        ttin0 = tin0 ^ dd[0];
        ttin1 = tin1 ^ dd[1];
        tin0 ^= d[0];
        tin1 ^= d[1];

        p = c->h;
        l2c(tin0, p);
        l2c(ttin1, p);
        p = c->hh;
        l2c(ttin0, p);
        l2c(tin1, p);
    }
}
Exemplo n.º 22
0
DES_LONG des_cbc_cksum(const unsigned char *in, des_cblock *output,
		long length,
		des_key_schedule schedule, const_des_cblock *ivec)
	{
	register DES_LONG tout0,tout1,tin0,tin1;
	register long l=length;
	DES_LONG tin[2];
	unsigned char *out = &(*output)[0];
	const unsigned char *iv = &(*ivec)[0];

	c2l(iv,tout0);
	c2l(iv,tout1);
	for (; l>0; l-=8)
		{
		if (l >= 8)
			{
			c2l(in,tin0);
			c2l(in,tin1);
			}
		else
			c2ln(in,tin0,tin1,l);
			
		tin0^=tout0; tin[0]=tin0;
		tin1^=tout1; tin[1]=tin1;
		des_encrypt1((DES_LONG *)tin,schedule,DES_ENCRYPT);
		/* fix 15/10/91 eay - thanks to [email protected] */
		tout0=tin[0];
		tout1=tin[1];
		}
	if (out != NULL)
		{
		l2c(tout0,out);
		l2c(tout1,out);
		}
	tout0=tin0=tin1=tin[0]=tin[1]=0;
	return(tout1);
	}
Exemplo n.º 23
0
void des_ecb3_encrypt(des_cblock *input, des_cblock *output,
             des_key_schedule ks1, des_key_schedule ks2, des_key_schedule ks3,
             int enc)
{
	register DES_LONG l0,l1;
	DES_LONG ll[2];
	const unsigned char *in = &(*input)[0];
	unsigned char *out = &(*output)[0];
 
	c2l(in,l0); 
	c2l(in,l1);
	ll[0]=l0; 
	ll[1]=l1;

	if (enc)
		des_encrypt3(ll,ks1,ks2,ks3);
	else
		des_decrypt3(ll,ks1,ks2,ks3);

	l0=ll[0];
	l1=ll[1];
	l2c(l0,out);
	l2c(l1,out);
}
Exemplo n.º 24
0
void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
                          long length, DES_key_schedule *ks1,
                          DES_key_schedule *ks2, DES_key_schedule *ks3,
                          DES_cblock *ivec, int enc)
{
    register DES_LONG tin0, tin1;
    register DES_LONG tout0, tout1, xor0, xor1;
    register const unsigned char *in;
    unsigned char *out;
    register long l = length;
    DES_LONG tin[2];
    unsigned char *iv;

    in = input;
    out = output;
    iv = &(*ivec)[0];

    if (enc) {
        c2l(iv, tout0);
        c2l(iv, tout1);
        for (l -= 8; l >= 0; l -= 8) {
            c2l(in, tin0);
            c2l(in, tin1);
            tin0 ^= tout0;
            tin1 ^= tout1;

            tin[0] = tin0;
            tin[1] = tin1;
            DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
            tout0 = tin[0];
            tout1 = tin[1];

            l2c(tout0, out);
            l2c(tout1, out);
        }
        if (l != -8) {
            c2ln(in, tin0, tin1, l + 8);
            tin0 ^= tout0;
            tin1 ^= tout1;

            tin[0] = tin0;
            tin[1] = tin1;
            DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
            tout0 = tin[0];
            tout1 = tin[1];

            l2c(tout0, out);
            l2c(tout1, out);
        }
        iv = &(*ivec)[0];
        l2c(tout0, iv);
        l2c(tout1, iv);
    } else {
        register DES_LONG t0, t1;

        c2l(iv, xor0);
        c2l(iv, xor1);
        for (l -= 8; l >= 0; l -= 8) {
            c2l(in, tin0);
            c2l(in, tin1);

            t0 = tin0;
            t1 = tin1;

            tin[0] = tin0;
            tin[1] = tin1;
            DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
            tout0 = tin[0];
            tout1 = tin[1];

            tout0 ^= xor0;
            tout1 ^= xor1;
            l2c(tout0, out);
            l2c(tout1, out);
            xor0 = t0;
            xor1 = t1;
        }
        if (l != -8) {
            c2l(in, tin0);
            c2l(in, tin1);

            t0 = tin0;
            t1 = tin1;

            tin[0] = tin0;
            tin[1] = tin1;
            DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
            tout0 = tin[0];
            tout1 = tin[1];

            tout0 ^= xor0;
            tout1 ^= xor1;
            l2cn(tout0, tout1, out, l + 8);
            xor0 = t0;
            xor1 = t1;
        }

        iv = &(*ivec)[0];
        l2c(xor0, iv);
        l2c(xor1, iv);
    }
    tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
    tin[0] = tin[1] = 0;
}
Exemplo n.º 25
0
void RC2_cbc_encrypt(unsigned char *in, unsigned char *out, long length,
	     RC2_KEY *ks, unsigned char *iv, int encrypt)
	{
	register unsigned long tin0,tin1;
	register unsigned long tout0,tout1,xor0,xor1;
	register long l=length;
	unsigned long tin[2];

	if (encrypt)
		{
		c2l(iv,tout0);
		c2l(iv,tout1);
		iv-=8;
		for (l-=8; l>=0; l-=8)
			{
			c2l(in,tin0);
			c2l(in,tin1);
			tin0^=tout0;
			tin1^=tout1;
			tin[0]=tin0;
			tin[1]=tin1;
			RC2_encrypt(tin,ks);
			tout0=tin[0]; l2c(tout0,out);
			tout1=tin[1]; l2c(tout1,out);
			}
		if (l != -8)
			{
			c2ln(in,tin0,tin1,l+8);
			tin0^=tout0;
			tin1^=tout1;
			tin[0]=tin0;
			tin[1]=tin1;
			RC2_encrypt(tin,ks);
			tout0=tin[0]; l2c(tout0,out);
			tout1=tin[1]; l2c(tout1,out);
			}
		l2c(tout0,iv);
		l2c(tout1,iv);
		}
	else
		{
		c2l(iv,xor0);
		c2l(iv,xor1);
		iv-=8;
		for (l-=8; l>=0; l-=8)
			{
			c2l(in,tin0); tin[0]=tin0;
			c2l(in,tin1); tin[1]=tin1;
			RC2_decrypt(tin,ks);
			tout0=tin[0]^xor0;
			tout1=tin[1]^xor1;
			l2c(tout0,out);
			l2c(tout1,out);
			xor0=tin0;
			xor1=tin1;
			}
		if (l != -8)
			{
			c2l(in,tin0); tin[0]=tin0;
			c2l(in,tin1); tin[1]=tin1;
			RC2_decrypt(tin,ks);
			tout0=tin[0]^xor0;
			tout1=tin[1]^xor1;
			l2cn(tout0,tout1,out,l+8);
			xor0=tin0;
			xor1=tin1;
			}
		l2c(xor0,iv);
		l2c(xor1,iv);
		}
	tin0=tin1=tout0=tout1=xor0=xor1=0;
	tin[0]=tin[1]=0;
	}
Exemplo n.º 26
0
void DES_xcbc_encrypt(const unsigned char *in, unsigned char *out,
                      long length, DES_key_schedule *schedule,
                      DES_cblock *ivec, const_DES_cblock *inw,
                      const_DES_cblock *outw, int enc)
{
    register DES_LONG tin0,tin1;
    register DES_LONG tout0,tout1,xor0,xor1;
    register DES_LONG inW0,inW1,outW0,outW1;
    register const unsigned char *in2;
    register long l=length;
    DES_LONG tin[2];
    unsigned char *iv;

    in2 = &(*inw)[0];
    c2l(in2,inW0);
    c2l(in2,inW1);
    in2 = &(*outw)[0];
    c2l(in2,outW0);
    c2l(in2,outW1);

    iv = &(*ivec)[0];

    if (enc)
    {
        c2l(iv,tout0);
        c2l(iv,tout1);
        for (l-=8; l>=0; l-=8)
        {
            c2l(in,tin0);
            c2l(in,tin1);
            tin0^=tout0^inW0;
            tin[0]=tin0;
            tin1^=tout1^inW1;
            tin[1]=tin1;
            DES_encrypt1(tin,schedule,DES_ENCRYPT);
            tout0=tin[0]^outW0;
            l2c(tout0,out);
            tout1=tin[1]^outW1;
            l2c(tout1,out);
        }
        if (l != -8)
        {
            c2ln(in,tin0,tin1,l+8);
            tin0^=tout0^inW0;
            tin[0]=tin0;
            tin1^=tout1^inW1;
            tin[1]=tin1;
            DES_encrypt1(tin,schedule,DES_ENCRYPT);
            tout0=tin[0]^outW0;
            l2c(tout0,out);
            tout1=tin[1]^outW1;
            l2c(tout1,out);
        }
        iv = &(*ivec)[0];
        l2c(tout0,iv);
        l2c(tout1,iv);
    }
    else
    {
        c2l(iv,xor0);
        c2l(iv,xor1);
        for (l-=8; l>0; l-=8)
        {
            c2l(in,tin0);
            tin[0]=tin0^outW0;
            c2l(in,tin1);
            tin[1]=tin1^outW1;
            DES_encrypt1(tin,schedule,DES_DECRYPT);
            tout0=tin[0]^xor0^inW0;
            tout1=tin[1]^xor1^inW1;
            l2c(tout0,out);
            l2c(tout1,out);
            xor0=tin0;
            xor1=tin1;
        }
        if (l != -8)
        {
            c2l(in,tin0);
            tin[0]=tin0^outW0;
            c2l(in,tin1);
            tin[1]=tin1^outW1;
            DES_encrypt1(tin,schedule,DES_DECRYPT);
            tout0=tin[0]^xor0^inW0;
            tout1=tin[1]^xor1^inW1;
            l2cn(tout0,tout1,out,l+8);
            xor0=tin0;
            xor1=tin1;
        }

        iv = &(*ivec)[0];
        l2c(xor0,iv);
        l2c(xor1,iv);
    }
    tin0=tin1=tout0=tout1=xor0=xor1=0;
    inW0=inW1=outW0=outW1=0;
    tin[0]=tin[1]=0;
}
Exemplo n.º 27
0
/* Until Aug 1 2003 this function did not correctly implement CFB-r, so it
 * will not be compatible with any encryption prior to that date. Ben. */
void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
		     long length, DES_key_schedule *schedule, DES_cblock *ivec,
		     int enc)
	{
	register DES_LONG d0,d1,v0,v1;
	register unsigned long l=length;
	register int num=numbits/8,n=(numbits+7)/8,i,rem=numbits%8;
	DES_LONG ti[2];
	unsigned char *iv;
#ifndef L_ENDIAN
	unsigned char ovec[16];
#else
	unsigned int  sh[4];
	unsigned char *ovec=(unsigned char *)sh;

	/* I kind of count that compiler optimizes away this assertioni,*/
	assert (sizeof(sh[0])==4);	/* as this holds true for all,	*/
					/* but 16-bit platforms...	*/
					
#endif

	if (numbits<=0 || numbits > 64) return;
	iv = &(*ivec)[0];
	c2l(iv,v0);
	c2l(iv,v1);
	if (enc)
		{
		while (l >= (unsigned long)n)
			{
			l-=n;
			ti[0]=v0;
			ti[1]=v1;
			DES_encrypt1((DES_LONG *)ti,schedule,DES_ENCRYPT);
			c2ln(in,d0,d1,n);
			in+=n;
			d0^=ti[0];
			d1^=ti[1];
			l2cn(d0,d1,out,n);
			out+=n;
			/* 30-08-94 - eay - changed because l>>32 and
			 * l<<32 are bad under gcc :-( */
			if (numbits == 32)
				{ v0=v1; v1=d0; }
			else if (numbits == 64)
				{ v0=d0; v1=d1; }
			else
				{
#ifndef L_ENDIAN
				iv=&ovec[0];
				l2c(v0,iv);
				l2c(v1,iv);
				l2c(d0,iv);
				l2c(d1,iv);
#else
				sh[0]=v0, sh[1]=v1, sh[2]=d0, sh[3]=d1;
#endif
				if (rem==0)
					memmove(ovec,ovec+num,8);
				else
					for(i=0 ; i < 8 ; ++i)
						ovec[i]=ovec[i+num]<<rem |
							ovec[i+num+1]>>(8-rem);
#ifdef L_ENDIAN
				v0=sh[0], v1=sh[1];
#else
				iv=&ovec[0];
				c2l(iv,v0);
				c2l(iv,v1);
#endif
				}
			}
		}
	else
		{
		while (l >= (unsigned long)n)
Exemplo n.º 28
0
void DES_ede3_cbcm_encrypt(const unsigned char *in, unsigned char *out,
	     long length, DES_key_schedule *ks1, DES_key_schedule *ks2,
	     DES_key_schedule *ks3, DES_cblock *ivec1, DES_cblock *ivec2,
	     int enc)
    {
    DES_LONG tin0,tin1;
    DES_LONG tout0,tout1,xor0,xor1,m0,m1;
    long l=length;
    DES_LONG tin[2];
    unsigned char *iv1,*iv2;

    iv1 = &(*ivec1)[0];
    iv2 = &(*ivec2)[0];

    if (enc)
	{
	c2l(iv1,m0);
	c2l(iv1,m1);
	c2l(iv2,tout0);
	c2l(iv2,tout1);
	for (l-=8; l>=-7; l-=8)
	    {
	    tin[0]=m0;
	    tin[1]=m1;
	    DES_encrypt1(tin,ks3,1);
	    m0=tin[0];
	    m1=tin[1];

	    if(l < 0)
		{
		c2ln(in,tin0,tin1,l+8);
		}
	    else
		{
		c2l(in,tin0);
		c2l(in,tin1);
		}
	    tin0^=tout0;
	    tin1^=tout1;

	    tin[0]=tin0;
	    tin[1]=tin1;
	    DES_encrypt1(tin,ks1,1);
	    tin[0]^=m0;
	    tin[1]^=m1;
	    DES_encrypt1(tin,ks2,0);
	    tin[0]^=m0;
	    tin[1]^=m1;
	    DES_encrypt1(tin,ks1,1);
	    tout0=tin[0];
	    tout1=tin[1];

	    l2c(tout0,out);
	    l2c(tout1,out);
	    }
	iv1=&(*ivec1)[0];
	l2c(m0,iv1);
	l2c(m1,iv1);

	iv2=&(*ivec2)[0];
	l2c(tout0,iv2);
	l2c(tout1,iv2);
	}
    else
	{
	DES_LONG t0,t1;

	c2l(iv1,m0);
	c2l(iv1,m1);
	c2l(iv2,xor0);
	c2l(iv2,xor1);
	for (l-=8; l>=-7; l-=8)
	    {
	    tin[0]=m0;
	    tin[1]=m1;
	    DES_encrypt1(tin,ks3,1);
	    m0=tin[0];
	    m1=tin[1];

	    c2l(in,tin0);
	    c2l(in,tin1);

	    t0=tin0;
	    t1=tin1;

	    tin[0]=tin0;
	    tin[1]=tin1;
	    DES_encrypt1(tin,ks1,0);
	    tin[0]^=m0;
	    tin[1]^=m1;
	    DES_encrypt1(tin,ks2,1);
	    tin[0]^=m0;
	    tin[1]^=m1;
	    DES_encrypt1(tin,ks1,0);
	    tout0=tin[0];
	    tout1=tin[1];

	    tout0^=xor0;
	    tout1^=xor1;
	    if(l < 0)
		{
		l2cn(tout0,tout1,out,l+8);
		}
	    else
		{
		l2c(tout0,out);
		l2c(tout1,out);
		}
	    xor0=t0;
	    xor1=t1;
	    }

	iv1=&(*ivec1)[0];
	l2c(m0,iv1);
	l2c(m1,iv1);

	iv2=&(*ivec2)[0];
	l2c(xor0,iv2);
	l2c(xor1,iv2);
	}
    tin0=tin1=tout0=tout1=xor0=xor1=0;
    tin[0]=tin[1]=0;
    }
Exemplo n.º 29
0
void DES_ncbc_encrypt(const unsigned char *in, unsigned char *out, long length,
		     DES_key_schedule *_schedule, DES_cblock *ivec, int enc)
#endif
	{
	register DES_LONG tin0,tin1;
	register DES_LONG tout0,tout1,xor0,xor1;
	register long l=length;
	DES_LONG tin[2];
	unsigned char *iv;

	iv = &(*ivec)[0];

	if (enc)
		{
		c2l(iv,tout0);
		c2l(iv,tout1);
		for (l-=8; l>=0; l-=8)
			{
			c2l(in,tin0);
			c2l(in,tin1);
			tin0^=tout0; tin[0]=tin0;
			tin1^=tout1; tin[1]=tin1;
			DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT);
			tout0=tin[0]; l2c(tout0,out);
			tout1=tin[1]; l2c(tout1,out);
			}
		if (l != -8)
			{
			c2ln(in,tin0,tin1,l+8);
			tin0^=tout0; tin[0]=tin0;
			tin1^=tout1; tin[1]=tin1;
			DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT);
			tout0=tin[0]; l2c(tout0,out);
			tout1=tin[1]; l2c(tout1,out);
			}
#ifndef CBC_ENC_C__DONT_UPDATE_IV
		iv = &(*ivec)[0];
		l2c(tout0,iv);
		l2c(tout1,iv);
#endif
		}
	else
		{
		c2l(iv,xor0);
		c2l(iv,xor1);
		for (l-=8; l>=0; l-=8)
			{
			c2l(in,tin0); tin[0]=tin0;
			c2l(in,tin1); tin[1]=tin1;
			DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT);
			tout0=tin[0]^xor0;
			tout1=tin[1]^xor1;
			l2c(tout0,out);
			l2c(tout1,out);
			xor0=tin0;
			xor1=tin1;
			}
		if (l != -8)
			{
			c2l(in,tin0); tin[0]=tin0;
			c2l(in,tin1); tin[1]=tin1;
			DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT);
			tout0=tin[0]^xor0;
			tout1=tin[1]^xor1;
			l2cn(tout0,tout1,out,l+8);
#ifndef CBC_ENC_C__DONT_UPDATE_IV
			xor0=tin0;
			xor1=tin1;
#endif
			}
#ifndef CBC_ENC_C__DONT_UPDATE_IV 
		iv = &(*ivec)[0];
		l2c(xor0,iv);
		l2c(xor1,iv);
#endif
		}
	tin0=tin1=tout0=tout1=xor0=xor1=0;
	tin[0]=tin[1]=0;
	}
Exemplo n.º 30
0
void MD5_Update(MD5_CTX *c, register unsigned char *data, unsigned long len)
{
	register ULONG *p;
	int sw,sc;
	ULONG l;

	if (len == 0) return;

	l=(c->Nl+(len<<3))&0xffffffffL;
	if (l < c->Nl) /* overflow */
		c->Nh++;
	c->Nh+=(len>>29);
	c->Nl=l;

	if (c->num != 0)
		{
		p=c->data;
		sw=c->num>>2;
		sc=c->num&0x03;

		if ((c->num+len) >= MD5_CBLOCK)
			{
			l= p[sw];
			p_c2l(data,l,sc);
			p[sw++]=l;
			for (; sw<MD5_LBLOCK; sw++)
				{
				c2l(data,l);
				p[sw]=l;
				}
			len-=(MD5_CBLOCK-c->num);

			md5_block(c,p);
			c->num=0;
			/* drop through and do the rest */
			}
		else
			{
			int ew,ec;

			c->num+=(int)len;
			if ((sc+len) < 4) /* ugly, add char's to a word */
				{
				l= p[sw];
				p_c2l_p(data,l,sc,len);
				p[sw]=l;
				}
			else
				{
				ew=(c->num>>2);
				ec=(c->num&0x03);
				l= p[sw];
				p_c2l(data,l,sc);
				p[sw++]=l;
				for (; sw < ew; sw++)
					{ c2l(data,l); p[sw]=l; }
				if (ec)
					{
					c2l_p(data,l,ec);
					p[sw]=l;
					}
				}
			return;
			}
		}