|
8 | 8 | using Org.BouncyCastle.Crypto.Digests;
|
9 | 9 | using Org.BouncyCastle.Crypto.Encodings;
|
10 | 10 | using Org.BouncyCastle.Crypto.Engines;
|
| 11 | +using Org.BouncyCastle.Crypto.Generators; |
11 | 12 | using Org.BouncyCastle.Crypto.Parameters;
|
12 | 13 | using Org.BouncyCastle.Math;
|
13 | 14 | using Org.BouncyCastle.Security;
|
@@ -780,8 +781,10 @@ public override void PerformTest()
|
780 | 781 | OaepVecTest(1027, 5, pubParam, privParam, seed_1027_5, input_1027_5, output_1027_5);
|
781 | 782 | OaepVecTest(1027, 6, pubParam, privParam, seed_1027_6, input_1027_6, output_1027_6);
|
782 | 783 |
|
| 784 | + TestForHighByteError("invalidCiphertextOaepTest 1024", 1024); |
| 785 | + |
783 | 786 | //
|
784 |
| - // OAEP - public encrypt, private decrypt differring hashes |
| 787 | + // OAEP - public encrypt, private decrypt, differing hashes |
785 | 788 | //
|
786 | 789 | IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha256Digest(), new Sha1Digest(), new byte[10]);
|
787 | 790 |
|
@@ -822,8 +825,78 @@ public override void PerformTest()
|
822 | 825 | }
|
823 | 826 | }
|
824 | 827 |
|
825 |
| - public static void Main( |
826 |
| - string[] args) |
| 828 | + private void TestForHighByteError(string label, int keySizeBits) |
| 829 | + { |
| 830 | + // draw a key of the size asked |
| 831 | + BigInteger e = BigInteger.One.ShiftLeft(16).Add(BigInteger.One); |
| 832 | + |
| 833 | + IAsymmetricCipherKeyPairGenerator kpGen = new RsaKeyPairGenerator(); |
| 834 | + |
| 835 | + kpGen.Init(new RsaKeyGenerationParameters(e, new SecureRandom(), keySizeBits, 100)); |
| 836 | + |
| 837 | + AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); |
| 838 | + |
| 839 | + IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine()); |
| 840 | + |
| 841 | + // obtain a known good ciphertext |
| 842 | + cipher.Init(true, new ParametersWithRandom(kp.Public, new VecRand(seed))); |
| 843 | + byte[] m = { 42 }; |
| 844 | + byte[] c = cipher.ProcessBlock(m, 0, m.Length); |
| 845 | + int keySizeBytes = (keySizeBits + 7) / 8; |
| 846 | + if (c.Length != keySizeBytes) |
| 847 | + { |
| 848 | + Fail(label + " failed ciphertext size"); |
| 849 | + } |
| 850 | + |
| 851 | + BigInteger n = ((RsaPrivateCrtKeyParameters)kp.Private).Modulus; |
| 852 | + |
| 853 | + // decipher |
| 854 | + cipher.Init(false, kp.Private); |
| 855 | + byte[] r = cipher.ProcessBlock(c, 0, keySizeBytes); |
| 856 | + if (r.Length != 1 || r[0] != 42) |
| 857 | + { |
| 858 | + Fail(label + " failed first decryption of test message"); |
| 859 | + } |
| 860 | + |
| 861 | + // decipher again |
| 862 | + r = cipher.ProcessBlock(c, 0, keySizeBytes); |
| 863 | + if (r.Length != 1 || r[0] != 42) |
| 864 | + { |
| 865 | + Fail(label + " failed second decryption of test message"); |
| 866 | + } |
| 867 | + |
| 868 | + // check hapazard incorrect ciphertexts |
| 869 | + for (int i = keySizeBytes * 8; --i >= 0; ) |
| 870 | + { |
| 871 | + c[i / 8] ^= (byte)(1 << (i & 7)); |
| 872 | + bool ko = true; |
| 873 | + try |
| 874 | + { |
| 875 | + BigInteger cV = new BigInteger(1, c); |
| 876 | + |
| 877 | + // don't pass in c if it will be rejected trivially |
| 878 | + if (cV.CompareTo(n) < 0) |
| 879 | + { |
| 880 | + r = cipher.ProcessBlock(c, 0, keySizeBytes); |
| 881 | + } |
| 882 | + else |
| 883 | + { |
| 884 | + ko = false; // size errors are picked up at start |
| 885 | + } |
| 886 | + } |
| 887 | + catch (InvalidCipherTextException) |
| 888 | + { |
| 889 | + ko = false; |
| 890 | + } |
| 891 | + if (ko) |
| 892 | + { |
| 893 | + Fail(label + " invalid ciphertext caused no exception"); |
| 894 | + } |
| 895 | + c[i / 8] ^= (byte)(1 << (i & 7)); |
| 896 | + } |
| 897 | + } |
| 898 | + |
| 899 | + public static void Main(string[] args) |
827 | 900 | {
|
828 | 901 | RunTest(new OaepTest());
|
829 | 902 | }
|
|
0 commit comments