public override PaddingResult Pad(int nlen, BitString message) { // 1. Message Hashing var hashedMessage = Sha.HashMessage(message).Digest; // 2. Hash Encapsulation var trailer = GetTrailer(); // Header is always 4, trailer is always 16 var paddingLen = nlen - Header.BitLength - Sha.HashFunction.OutputLen - trailer.BitLength; var padding = GetPadding(paddingLen); // ERROR: Split the padding into two chunks and put the hashed message in the middle var firstChunkPadding = padding.GetMostSignificantBits(paddingLen - 8); var secondChunkPadding = padding.GetLeastSignificantBits(8); var IR = Header.GetDeepCopy(); IR = BitString.ConcatenateBits(IR, firstChunkPadding); IR = BitString.ConcatenateBits(IR, hashedMessage); IR = BitString.ConcatenateBits(IR, secondChunkPadding); IR = BitString.ConcatenateBits(IR, trailer); if (IR.BitLength != nlen) { return(new PaddingResult("Improper length for IR")); } return(new PaddingResult(IR)); }
public override PaddingResult Pad(int nlen, BitString message) { // 1. Message Hashing var hashedMessage = Sha.HashMessage(message).Digest; // 2. Hash Encapsulation var trailer = new BitString("66CC"); // ERROR: The first byte of the trailer is unexpected // Header is always 4, trailer is always 16 var paddingLen = nlen - Header.BitLength - Sha.HashFunction.OutputLen - trailer.BitLength; var padding = GetPadding(paddingLen); var IR = Header.GetDeepCopy(); IR = BitString.ConcatenateBits(IR, padding); IR = BitString.ConcatenateBits(IR, hashedMessage); IR = BitString.ConcatenateBits(IR, trailer); // ERROR: Change the trailer to something else if (IR.BitLength != nlen) { return(new PaddingResult("Improper length for IR")); } return(new PaddingResult(IR)); }
public override PaddingResult Pad(int nlen, BitString message) { var emBits = nlen - 1; var mHash = Sha.HashMessage(message).Digest; var emLen = emBits.CeilingDivide(8); // All byte values if (emLen < mHash.BitLength / 8 + SaltLength + 2) { return(new PaddingResult("Encoding error")); } var salt = EntropyProvider.GetEntropy(SaltLength * 8); var mPrime = BitString.Zeroes(64); mPrime = BitString.ConcatenateBits(mPrime, mHash); mPrime = BitString.ConcatenateBits(mPrime, salt); var H = Sha.HashMessage(mPrime).Digest; // All bit values var PS = BitString.Zeroes(emLen * 8 - SaltLength * 8 - H.BitLength - 2 * 8); var DB = PS.GetDeepCopy(); DB = BitString.ConcatenateBits(DB, ZeroOne); DB = BitString.ConcatenateBits(DB, salt); // All bit values var dbMask = Mask.Mask(H, (emLen * 8) - H.BitLength - (1 * 8)); var maskedDB = BitString.XOR(DB, dbMask); // Set leftmost bits to 0 for (var i = 0; i < 8 * emLen - emBits; i++) { maskedDB.Set(maskedDB.BitLength - i - 1, false); } // ERROR: split maskedDB into two chunks and insert hashed message in the middle var firstChunkMask = maskedDB.GetMostSignificantBits(maskedDB.BitLength - 8); var secondChunkMask = maskedDB.GetLeastSignificantBits(8); var EM = firstChunkMask.GetDeepCopy(); EM = BitString.ConcatenateBits(EM, H); EM = BitString.ConcatenateBits(EM, secondChunkMask); EM = BitString.ConcatenateBits(EM, Bc); return(new PaddingResult(EM)); }
public override PaddingResult Pad(int nlen, BitString message) { var emBits = nlen - 1; var mHash = Sha.HashMessage(message).Digest; var emLen = emBits.CeilingDivide(8); // All byte values if (emLen < mHash.BitLength / 8 + SaltLength + 2) { return(new PaddingResult("Encoding error")); } var salt = EntropyProvider.GetEntropy(SaltLength * 8); var mPrime = BitString.Zeroes(64); mPrime = BitString.ConcatenateBits(mPrime, mHash); mPrime = BitString.ConcatenateBits(mPrime, salt); var H = Sha.HashMessage(mPrime).Digest; // All bit values var PS = BitString.Zeroes(emLen * 8 - SaltLength * 8 - H.BitLength - 2 * 8); var DB = PS.GetDeepCopy(); DB = BitString.ConcatenateBits(DB, ZeroOne); DB = BitString.ConcatenateBits(DB, salt); // All bit values var dbMask = Mask.Mask(H, (emLen * 8) - H.BitLength - (1 * 8)); var maskedDB = BitString.XOR(DB, dbMask); // Set leftmost bits to 0 for (var i = 0; i < 8 * emLen - emBits; i++) { maskedDB.Set(maskedDB.BitLength - i - 1, false); } var EM = maskedDB.GetDeepCopy(); EM = BitString.ConcatenateBits(EM, H); EM = BitString.ConcatenateBits(EM, new BitString("4C")); // ERROR: should be 'BC' return(new PaddingResult(EM)); }
public override PaddingResult Pad(int nlen, BitString message) { if (message.BitLength < GetHashAlgId().BitLength + 11 * 8) { return(new PaddingResult("Message length too short")); } var H = Sha.HashMessage(message).Digest; var T = BitString.ConcatenateBits(GetHashAlgId(), H); var psLen = nlen - (GetHashAlgId().BitLength + Sha.HashFunction.OutputLen) - 24; var PS = BitString.Ones(psLen); var EM = new BitString("00"); EM = BitString.ConcatenateBits(EM, new BitString("01")); EM = BitString.ConcatenateBits(EM, PS); EM = BitString.ConcatenateBits(EM, new BitString("44")); // ERROR: Should be 00 EM = BitString.ConcatenateBits(EM, T); return(new PaddingResult(EM)); }