MCPcopy Create free account
hub / github.com/mscdex/ssh2 / decrypt

Method decrypt

lib/protocol/crypto.js:1134–1274  ·  view source on GitHub ↗
(data, p, dataLen)

Source from the content-addressed store, hash-verified

1132 }
1133 free() {}
1134 decrypt(data, p, dataLen) {
1135 // `data` === encrypted data
1136
1137 while (p < dataLen) {
1138 // Read first encrypted block
1139 if (this._blockPos < this._block.length) {
1140 const nb = Math.min(this._block.length - this._blockPos, dataLen - p);
1141 if (p !== 0 || nb !== dataLen || nb < data.length) {
1142 this._block.set(
1143 new Uint8Array(data.buffer, data.byteOffset + p, nb),
1144 this._blockPos
1145 );
1146 } else {
1147 this._block.set(data, this._blockPos);
1148 }
1149
1150 p += nb;
1151 this._blockPos += nb;
1152 if (this._blockPos < this._block.length)
1153 return;
1154
1155 let decrypted;
1156 let need;
1157 if (this._macETM) {
1158 this._len = need = readUInt32BE(this._block, 0);
1159 } else {
1160 // Decrypt first block to get packet length
1161 decrypted = this._decipherInstance.update(this._block);
1162 this._len = readUInt32BE(decrypted, 0);
1163 need = 4 + this._len - this._blockSize;
1164 }
1165
1166 if (this._len > MAX_PACKET_SIZE
1167 || this._len < 5
1168 || (need & (this._blockSize - 1)) !== 0) {
1169 throw new Error('Bad packet length');
1170 }
1171
1172 // Create MAC up front to calculate in parallel with decryption
1173 this._macInstance = createHmac(this._macSSLName, this._macKey);
1174
1175 writeUInt32BE(BUF_INT, this.inSeqno, 0);
1176 this._macInstance.update(BUF_INT);
1177 if (this._macETM) {
1178 this._macInstance.update(this._block);
1179 } else {
1180 this._macInstance.update(new Uint8Array(decrypted.buffer,
1181 decrypted.byteOffset,
1182 4));
1183 this._pktLen = decrypted.length - 4;
1184 this._packetPos = this._pktLen;
1185 this._packet = Buffer.allocUnsafe(this._len);
1186 this._packet.set(
1187 new Uint8Array(decrypted.buffer,
1188 decrypted.byteOffset + 4,
1189 this._packetPos),
1190 0
1191 );

Callers

nothing calls this directly

Calls 4

readUInt32BEFunction · 0.85
writeUInt32BEFunction · 0.85
timingSafeEqualsFunction · 0.85
updateMethod · 0.80

Tested by

no test coverage detected