// Decode pIntput into pOutput. Input length in lSize. Input buffer and // output buffer can be the same, but be sure buffer length is even MOD 8. void CBlowFish::Decode (BYTE * pInput, BYTE * pOutput, DWORD lSize) { DWORD lCount ; BYTE *pi, *po ; int i ; int SameDest = (pInput == pOutput ? 1 : 0) ; for (lCount = 0 ; lCount < lSize ; lCount += 8) { if (SameDest) // if encoded data is being written into input buffer { Blowfish_decipher ((DWORD *) pInput, (DWORD *) (pInput + 4)) ; pInput += 8 ; } else // output buffer not equal to input buffer { // so copy input to output before decoding pi = pInput ; po = pOutput ; for (i = 0 ; i < 8 ; i++) *po++ = *pi++ ; Blowfish_decipher ((DWORD *) pOutput, (DWORD *) (pOutput + 4)) ; pInput += 8 ; pOutput += 8 ; } } }
void blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len) { u_int32_t l, r, d[2]; u_int8_t *iv; u_int32_t i, j; iv = data + len - 16; data = data + len - 8; for (i = len - 8; i >= 8; i -= 8) { l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; d[0] = l; d[1] = r; Blowfish_decipher(c, d); l = d[0]; r = d[1]; data[0] = l >> 24 & 0xff; data[1] = l >> 16 & 0xff; data[2] = l >> 8 & 0xff; data[3] = l & 0xff; data[4] = r >> 24 & 0xff; data[5] = r >> 16 & 0xff; data[6] = r >> 8 & 0xff; data[7] = r & 0xff; for (j = 0; j < 8; j++) data[j] ^= iv[j]; iv -= 8; data -= 8; } l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; d[0] = l; d[1] = r; Blowfish_decipher(c, d); l = d[0]; r = d[1]; data[0] = l >> 24 & 0xff; data[1] = l >> 16 & 0xff; data[2] = l >> 8 & 0xff; data[3] = l & 0xff; data[4] = r >> 24 & 0xff; data[5] = r >> 16 & 0xff; data[6] = r >> 8 & 0xff; data[7] = r & 0xff; for (j = 0; j < 8; j++) data[j] ^= iva[j]; }
void BlowFish::Decrypt(const unsigned char *in, unsigned char *out) { for (int x = 0; x < 8; x++) out[x] = in[x]; Blowfish_decipher(reinterpret_cast<uint32 *>(out), reinterpret_cast<uint32 *>(out + sizeof(uint32))); }
void blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) { u_int32_t l, r, d[2]; u_int32_t i; for (i = 0; i < len; i += 8) { l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; d[0] = l; d[1] = r; Blowfish_decipher(c, d); l = d[0]; r = d[1]; data[0] = l >> 24 & 0xff; data[1] = l >> 16 & 0xff; data[2] = l >> 8 & 0xff; data[3] = l & 0xff; data[4] = r >> 24 & 0xff; data[5] = r >> 16 & 0xff; data[6] = r >> 8 & 0xff; data[7] = r & 0xff; data += 8; } }
// Decode pIntput into pOutput. Input length in lSize. Input buffer and // output buffer can be the same, but be sure buffer length is even MOD 8. DWORD CBlowFish::Decode(BYTE * pInput, BYTE * pOutput, DWORD lSize) { DWORD lCount, *dwOutput = (DWORD *)pOutput; _int64 CBCval = 0, prevCBCval = 0; if (pInput != pOutput) memcpy(pOutput, pInput, lSize); for (lCount = 0; lCount < lSize; lCount += 8) { if (mode == CBC) { prevCBCval = CBCval; CBCval = *(_int64 *)dwOutput; } change_order(dwOutput); change_order(dwOutput + 1); Blowfish_decipher(dwOutput, dwOutput + 1); change_order(dwOutput); change_order(dwOutput + 1); if (mode == CBC) *(_int64 *)dwOutput ^= prevCBCval; dwOutput += 2; } if (mode == CBC) { DWORD paddingLen = pOutput[lCount - 1]; if (paddingLen <= 8 && pOutput[lCount - paddingLen] == paddingLen) pOutput[lCount -= paddingLen] = 0; } return lCount; }
// Decode pIntput into pOutput. Input length in lSize. Input buffer and // output buffer can be the same, but be sure buffer length is even MOD 8. bool BlowfishPIMPL::Decode(const void * const input_ptr, uint64_t input_size, void * output_ptr, uint64_t output_size) { uint64_t lCount; uint8_t *pi, *po; uint64_t i; bool sameDest = input_ptr == output_ptr; uint8_t * pInput = reinterpret_cast<uint8_t *>(const_cast<void *>(input_ptr)); uint8_t * pOutput = reinterpret_cast<uint8_t *>(output_ptr); if(!input_ptr || !output_ptr) { return false; } if(input_size % 8 != 0) { return false; } if(output_size < input_size) { return false; } for(lCount = 0; lCount < input_size; lCount += 8) { if(sameDest) // if encoded data is being written into input buffer { Blowfish_decipher(reinterpret_cast<uint32_t *>(pInput), reinterpret_cast<uint32_t *>(pInput + 4)); pInput += 8; } else // output buffer not equal to input buffer { // so copy input to output before decoding pi = pInput; po = pOutput; for(i = 0; i < 8; i++) { *po++ = *pi++; } Blowfish_decipher(reinterpret_cast<uint32_t *>(pOutput), reinterpret_cast<uint32_t *>(pOutput + 4)); pInput += 8; pOutput += 8; } } return true; }
void blf_dec(blf_ctx *c,unsigned long *data,int blocks){ unsigned long *d; int i; d=data; for (i=0;i<blocks;i++){ Blowfish_decipher(c,d,d+1); d+=2; } }
void ssh_blf_dec(ssh_blf_ctx *c, uint32_t *data, uint16_t blocks) { uint32_t *d; uint16_t i; d = data; for (i = 0; i < blocks; i++) { Blowfish_decipher(c, d, d + 1); d += 2; } }
void blf_dec(blf_ctx *c, u_int32_t *data, u_int16_t blocks) { u_int32_t *d; u_int16_t i; d = data; for (i = 0; i < blocks; i++) { Blowfish_decipher(c, d); d += 2; } }
// Decode pIntput into pOutput. Input length in lSize. Input buffer and // output buffer can be the same, but be sure buffer length is even MOD8. void CBlowFish::Decode (BYTE * pInput, BYTE * pOutput, DWORD lSize) { DWORD lCount ; BYTE *pi, *po ; int i ; int SameDest = (pInput == pOutput ? 1 : 0) ; #ifdef ORDER_ABCD //force data to little endian form BYTE *pszStart = pOutput; ChangeEndian(pszStart, lSize); #endif for (lCount = 0 ; lCount < lSize ; lCount += 8) { if (SameDest) // if encoded data is being written into input buffer { Blowfish_decipher ((DWORD *) pInput, (DWORD *) (pInput + 4)); pInput += 8 ; } else // output buffer not equal to input buffer { // so copy input to output before decoding pi = pInput ; po = pOutput ; for (i = 0 ; i < 8 ; i++) *po++ = *pi++ ; Blowfish_decipher ((DWORD *) pOutput, (DWORD *) (pOutput + 4)); pInput += 8 ; pOutput += 8 ; } } #ifdef ORDER_ABCD // change back to the big endian form ChangeEndian(pszStart, lSize); #endif }
void blf_decrypt(struct keystate *ks, u_int8_t *data, u_int16_t len) { u_int16_t i, blocksize = ks->xf->blocksize; u_int32_t xl, xr; data += len - blocksize; for (i = len - blocksize; i >= blocksize; data -= blocksize, i -= blocksize) { xl = GET_32BIT_BIG(data); xr = GET_32BIT_BIG(data + 4); Blowfish_decipher(&ks->ks_blf, &xl, &xr); SET_32BIT_BIG(data, xl); SET_32BIT_BIG(data + 4, xr); XOR64(data, data - blocksize); } xl = GET_32BIT_BIG(data); xr = GET_32BIT_BIG(data + 4); Blowfish_decipher(&ks->ks_blf, &xl, &xr); SET_32BIT_BIG(data, xl); SET_32BIT_BIG(data + 4, xr); XOR64(data, ks->riv); }
PacketData CPacketUtils::DecryptSubPacket(const PacketData& packet) { PacketData result(packet); SUBPACKETHEADER subHeader = *reinterpret_cast<const SUBPACKETHEADER*>(result.data()); if(subHeader.unknown0 != 0x03) return result; uint8* subPacketData = result.data() + 8; uint32 decryptSize = result.size() - 8; for(uint32 i = 0; i < decryptSize; i += 8) { Blowfish_decipher( reinterpret_cast<uint32*>(subPacketData + i), reinterpret_cast<uint32*>(subPacketData + i + 4)); } return result; }
// Decode pIntput into pOutput. Input length in lSize. Input buffer and // output buffer can be the same, but be sure buffer length is even MOD 8. void CBlowFish::Decode (BYTE * pInput, BYTE * pOutput, DWORD lSize) { DWORD lCount,dx; int SameDest=(pInput==pOutput ? 1 : 0); if (!SameDest) for (dx=0;dx<lSize;dx++) pOutput[dx]=pInput[dx]; #ifdef HAVE_BIG_ENDIAN BYTE *temp=pOutput; mix(pOutput,lSize); #endif for (lCount=0;lCount<lSize;lCount+=8) { Blowfish_decipher((DWORD *)pOutput, (DWORD *)(pOutput+4)); pOutput+=8; } #ifdef HAVE_BIG_ENDIAN mix(temp,lSize); #endif }
void ssh_blf_ecb_decrypt(ssh_blf_ctx *c, uint8_t *data, uint32_t len) { uint32_t l, r; uint32_t i; for (i = 0; i < len; i += 8) { l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; Blowfish_decipher(c, &l, &r); data[0] = l >> 24 & 0xff; data[1] = l >> 16 & 0xff; data[2] = l >> 8 & 0xff; data[3] = l & 0xff; data[4] = r >> 24 & 0xff; data[5] = r >> 16 & 0xff; data[6] = r >> 8 & 0xff; data[7] = r & 0xff; data += 8; } }
// Decrypt pIntput into pOutput. Input length in lSize. Input buffer and // output buffer can be the same, but be sure buffer length is even MOD 8. DWORD CBlowFish::Decrypt(const BYTE *pInput, BYTE *pOutput, DWORD lSize, int mode, BYTE *_IV) { DWORD lCount, *dwOutput = (DWORD *)pOutput; _int64 prevIV = 0; _int64 IV = 0; if (_IV) memcpy(&IV, _IV, sizeof(IV)); if (pInput != pOutput) memcpy(pOutput, pInput, lSize); for (lCount = 0; lCount < lSize; lCount += BF_BLKSIZE) { if (mode & BF_CBC) { prevIV = IV; IV = *(_int64 *)dwOutput; } change_order(dwOutput); change_order(dwOutput + 1); Blowfish_decipher(dwOutput, dwOutput + 1); change_order(dwOutput); change_order(dwOutput + 1); if (mode & BF_CBC) *(_int64 *)dwOutput ^= prevIV; dwOutput += 2; } if (mode & BF_PKCS5) { DWORD paddingLen = pOutput[lCount - 1]; if (paddingLen <= 0 || paddingLen > BF_BLKSIZE || pOutput[lCount - paddingLen] != paddingLen) return 0; // error pOutput[lCount -= paddingLen] = 0; } return lCount; }
void CCryptMgr::Decrypt(istream& is, ostream& os) #endif // VC7 { int n = 0; char buf[8]; char tbuf[8]; bool bFirstTime = true; // I know theres a better way - will fix later [Jpl] while (!is.eof()) { is.read(buf, 8); n = is.gcount(); if (n == 1) n = (int)buf[0]; if (!bFirstTime) os.write(tbuf, n); else bFirstTime = false; Blowfish_decipher((UWORD_32bits*)buf, (UWORD_32bits*)&buf[4]); memcpy(tbuf, buf, 8); } }