/* 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; }
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) {
void EncryptData_model(BYTE * inData, size_t len, const BYTE* key1, const BYTE* key2, const BYTE* key3, t_DES_SPtrans * sp) { assert((len & 0x7) == 0); #ifdef _DEBUG BYTE * oldData = new BYTE[len]; memcpy(oldData, inData, len); #endif DES_key_schedule sKey[3]; DES_set_key((const_DES_cblock*)key1, &sKey[0]); DES_set_key((const_DES_cblock*)key2, &sKey[1]); DES_set_key((const_DES_cblock*)key3, &sKey[2]); for (size_t i = 0; i < len; i += 8) { DES_encrypt3((DWORD*)(inData + i), &sKey[0], &sKey[1], &sKey[2], sp); } #ifdef _DEBUG BYTE* newData = new BYTE[len]; memcpy(newData, inData, len); DecryptData(newData, len, key1, key2, key3); int cmp = memcmp(newData, oldData, len); assert(cmp == 0); CC_SAFE_DELETE_ARRAY(newData); CC_SAFE_DELETE_ARRAY(oldData); #endif }
/* 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; }
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); }
//============================================================================ // Triple-DES //============================================================================ static int ssh_des3_ctr(EVP_CIPHER_CTX *ctx, unsigned char *dest, const unsigned char *src, unsigned int len) { struct ssh_des3_ctr_ctx *c; unsigned int n = 0; unsigned char buf[DES_BLOCK_SIZE]; if (len == 0) return (1); if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) return (0); while ((len--) > 0) { if (n == 0) { memcpy(buf, (unsigned char *)(c->des3_counter), DES_BLOCK_SIZE); DES_encrypt3((DES_LONG *)buf, &c->des3_ctx[0], &c->des3_ctx[1], &c->des3_ctx[2]); ssh_ctr_inc(c->des3_counter, DES_BLOCK_SIZE); } *(dest++) = *(src++) ^ buf[n]; n = (n + 1) % DES_BLOCK_SIZE; } return (1); }
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; }
void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length, DES_key_schedule *ks1, DES_key_schedule *ks2, DES_key_schedule *ks3, 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); c2l(iv,v1); ti[0]=v0; ti[1]=v1; DES_encrypt3(ti,ks1,ks2,ks3); v0=ti[0]; v1=ti[1]; iv = &(*ivec)[0]; l2c(v0,iv); l2c(v1,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); c2l(iv,v1); ti[0]=v0; ti[1]=v1; DES_encrypt3(ti,ks1,ks2,ks3); v0=ti[0]; v1=ti[1]; iv = &(*ivec)[0]; l2c(v0,iv); l2c(v1,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; }
void _ossl_old_des_encrypt3(DES_LONG *data, des_key_schedule ks1, des_key_schedule ks2, des_key_schedule ks3) { DES_encrypt3(data, (DES_key_schedule *)ks1, (DES_key_schedule *)ks2, (DES_key_schedule *)ks3); }