|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace System.Security.Cryptography.Tests |
| 7 | +{ |
| 8 | + internal sealed class CompositeMLKemMockImplementation : CompositeMLKem |
| 9 | + { |
| 10 | + internal static CompositeMLKemMockImplementation Create(CompositeMLKemAlgorithm algorithm) => |
| 11 | + new CompositeMLKemMockImplementation(algorithm); |
| 12 | + |
| 13 | + public CompositeMLKemMockImplementation(CompositeMLKemAlgorithm algorithm) |
| 14 | + : base(algorithm) |
| 15 | + { |
| 16 | + } |
| 17 | + |
| 18 | + internal delegate void EncapsulateAction(Span<byte> ciphertext, Span<byte> sharedSecret); |
| 19 | + internal delegate void DecapsulateAction(ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret); |
| 20 | + internal delegate int ExportFunc(Span<byte> destination); |
| 21 | + internal delegate bool TryExportFunc(Span<byte> destination, out int written); |
| 22 | + internal delegate void DisposeAction(bool disposing); |
| 23 | + |
| 24 | + public int EncapsulateCoreCallCount; |
| 25 | + public int DecapsulateCoreCallCount; |
| 26 | + public int ExportEncapsulationKeyCoreCallCount; |
| 27 | + public int ExportDecapsulationKeyCoreCallCount; |
| 28 | + public int TryExportPkcs8PrivateKeyCoreCallCount; |
| 29 | + public int DisposeCallCount; |
| 30 | + |
| 31 | + public EncapsulateAction EncapsulateCoreHook { get; set; } = (_, _) => Assert.Fail(); |
| 32 | + public DecapsulateAction DecapsulateCoreHook { get; set; } = (_, _) => Assert.Fail(); |
| 33 | + public ExportFunc ExportEncapsulationKeyCoreHook { get; set; } = _ => { Assert.Fail(); return 0; }; |
| 34 | + public ExportFunc ExportDecapsulationKeyCoreHook { get; set; } = _ => { Assert.Fail(); return 0; }; |
| 35 | + public TryExportFunc TryExportPkcs8PrivateKeyCoreHook { get; set; } = (Span<byte> destination, out int bytesWritten) => { Assert.Fail(); bytesWritten = 0; return false; }; |
| 36 | + public DisposeAction DisposeHook { get; set; } = _ => { }; |
| 37 | + |
| 38 | + protected override void EncapsulateCore(Span<byte> ciphertext, Span<byte> sharedSecret) |
| 39 | + { |
| 40 | + EncapsulateCoreCallCount++; |
| 41 | + EncapsulateCoreHook(ciphertext, sharedSecret); |
| 42 | + } |
| 43 | + |
| 44 | + protected override void DecapsulateCore(ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret) |
| 45 | + { |
| 46 | + DecapsulateCoreCallCount++; |
| 47 | + DecapsulateCoreHook(ciphertext, sharedSecret); |
| 48 | + } |
| 49 | + |
| 50 | + protected override int ExportEncapsulationKeyCore(Span<byte> destination) |
| 51 | + { |
| 52 | + ExportEncapsulationKeyCoreCallCount++; |
| 53 | + return ExportEncapsulationKeyCoreHook(destination); |
| 54 | + } |
| 55 | + |
| 56 | + protected override int ExportDecapsulationKeyCore(Span<byte> destination) |
| 57 | + { |
| 58 | + ExportDecapsulationKeyCoreCallCount++; |
| 59 | + return ExportDecapsulationKeyCoreHook(destination); |
| 60 | + } |
| 61 | + |
| 62 | + protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) |
| 63 | + { |
| 64 | + TryExportPkcs8PrivateKeyCoreCallCount++; |
| 65 | + return TryExportPkcs8PrivateKeyCoreHook(destination, out bytesWritten); |
| 66 | + } |
| 67 | + |
| 68 | + protected override void Dispose(bool disposing) |
| 69 | + { |
| 70 | + DisposeCallCount++; |
| 71 | + DisposeHook(disposing); |
| 72 | + } |
| 73 | + |
| 74 | + public void AddLengthAssertion() |
| 75 | + { |
| 76 | + EncapsulateAction oldEncapsulateCoreHook = EncapsulateCoreHook; |
| 77 | + EncapsulateCoreHook = (Span<byte> ciphertext, Span<byte> sharedSecret) => |
| 78 | + { |
| 79 | + oldEncapsulateCoreHook(ciphertext, sharedSecret); |
| 80 | + Assert.Equal(Algorithm.CiphertextSizeInBytes, ciphertext.Length); |
| 81 | + Assert.Equal(Algorithm.SharedSecretSizeInBytes, sharedSecret.Length); |
| 82 | + }; |
| 83 | + |
| 84 | + DecapsulateAction oldDecapsulateCoreHook = DecapsulateCoreHook; |
| 85 | + DecapsulateCoreHook = (ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret) => |
| 86 | + { |
| 87 | + oldDecapsulateCoreHook(ciphertext, sharedSecret); |
| 88 | + Assert.Equal(Algorithm.CiphertextSizeInBytes, ciphertext.Length); |
| 89 | + Assert.Equal(Algorithm.SharedSecretSizeInBytes, sharedSecret.Length); |
| 90 | + }; |
| 91 | + |
| 92 | + ExportFunc oldExportEncapsulationKeyCoreHook = ExportEncapsulationKeyCoreHook; |
| 93 | + ExportEncapsulationKeyCoreHook = (Span<byte> destination) => |
| 94 | + { |
| 95 | + int ret = oldExportEncapsulationKeyCoreHook(destination); |
| 96 | + AssertExtensions.GreaterThanOrEqualTo( |
| 97 | + destination.Length, |
| 98 | + CompositeMLKemTestData.ExpectedEncapsulationKeySizeLowerBound(Algorithm)); |
| 99 | + return ret; |
| 100 | + }; |
| 101 | + |
| 102 | + ExportFunc oldExportDecapsulationKeyCoreHook = ExportDecapsulationKeyCoreHook; |
| 103 | + ExportDecapsulationKeyCoreHook = (Span<byte> destination) => |
| 104 | + { |
| 105 | + int ret = oldExportDecapsulationKeyCoreHook(destination); |
| 106 | + AssertExtensions.GreaterThanOrEqualTo( |
| 107 | + destination.Length, |
| 108 | + CompositeMLKemTestData.ExpectedDecapsulationKeySizeLowerBound(Algorithm)); |
| 109 | + return ret; |
| 110 | + }; |
| 111 | + |
| 112 | + TryExportFunc oldTryExportPkcs8PrivateKeyCoreHook = TryExportPkcs8PrivateKeyCoreHook; |
| 113 | + TryExportPkcs8PrivateKeyCoreHook = (Span<byte> destination, out int bytesWritten) => |
| 114 | + { |
| 115 | + bool ret = oldTryExportPkcs8PrivateKeyCoreHook(destination, out bytesWritten); |
| 116 | + AssertExtensions.GreaterThanOrEqualTo( |
| 117 | + destination.Length, |
| 118 | + CompositeMLKemTestData.ExpectedDecapsulationKeySizeLowerBound(Algorithm)); |
| 119 | + return ret; |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + public void AddDestinationBufferIsSameAssertion(ReadOnlyMemory<byte> buffer) |
| 124 | + { |
| 125 | + ExportFunc oldExportEncapsulationKeyCoreHook = ExportEncapsulationKeyCoreHook; |
| 126 | + ExportEncapsulationKeyCoreHook = (Span<byte> destination) => |
| 127 | + { |
| 128 | + int ret = oldExportEncapsulationKeyCoreHook(destination); |
| 129 | + AssertExtensions.Same(buffer.Span, destination); |
| 130 | + return ret; |
| 131 | + }; |
| 132 | + |
| 133 | + ExportFunc oldExportDecapsulationKeyCoreHook = ExportDecapsulationKeyCoreHook; |
| 134 | + ExportDecapsulationKeyCoreHook = (Span<byte> destination) => |
| 135 | + { |
| 136 | + int ret = oldExportDecapsulationKeyCoreHook(destination); |
| 137 | + AssertExtensions.Same(buffer.Span, destination); |
| 138 | + return ret; |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + public void AddCiphertextBufferIsSameAssertion(ReadOnlyMemory<byte> buffer) |
| 143 | + { |
| 144 | + EncapsulateAction oldEncapsulateCoreHook = EncapsulateCoreHook; |
| 145 | + EncapsulateCoreHook = (Span<byte> ciphertext, Span<byte> sharedSecret) => |
| 146 | + { |
| 147 | + oldEncapsulateCoreHook(ciphertext, sharedSecret); |
| 148 | + AssertExtensions.Same(buffer.Span, ciphertext); |
| 149 | + }; |
| 150 | + |
| 151 | + DecapsulateAction oldDecapsulateCoreHook = DecapsulateCoreHook; |
| 152 | + DecapsulateCoreHook = (ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret) => |
| 153 | + { |
| 154 | + oldDecapsulateCoreHook(ciphertext, sharedSecret); |
| 155 | + AssertExtensions.Same(buffer.Span, ciphertext); |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + public void AddSharedSecretBufferIsSameAssertion(ReadOnlyMemory<byte> buffer) |
| 160 | + { |
| 161 | + EncapsulateAction oldEncapsulateCoreHook = EncapsulateCoreHook; |
| 162 | + EncapsulateCoreHook = (Span<byte> ciphertext, Span<byte> sharedSecret) => |
| 163 | + { |
| 164 | + oldEncapsulateCoreHook(ciphertext, sharedSecret); |
| 165 | + AssertExtensions.Same(buffer.Span, sharedSecret); |
| 166 | + }; |
| 167 | + |
| 168 | + DecapsulateAction oldDecapsulateCoreHook = DecapsulateCoreHook; |
| 169 | + DecapsulateCoreHook = (ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret) => |
| 170 | + { |
| 171 | + oldDecapsulateCoreHook(ciphertext, sharedSecret); |
| 172 | + AssertExtensions.Same(buffer.Span, sharedSecret); |
| 173 | + }; |
| 174 | + } |
| 175 | + |
| 176 | + public void AddFillDestination(byte b) |
| 177 | + { |
| 178 | + EncapsulateAction oldEncapsulateCoreHook = EncapsulateCoreHook; |
| 179 | + EncapsulateCoreHook = (Span<byte> ciphertext, Span<byte> sharedSecret) => |
| 180 | + { |
| 181 | + oldEncapsulateCoreHook(ciphertext, sharedSecret); |
| 182 | + ciphertext.Fill(b); |
| 183 | + sharedSecret.Fill(b); |
| 184 | + }; |
| 185 | + |
| 186 | + DecapsulateAction oldDecapsulateCoreHook = DecapsulateCoreHook; |
| 187 | + DecapsulateCoreHook = (ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret) => |
| 188 | + { |
| 189 | + oldDecapsulateCoreHook(ciphertext, sharedSecret); |
| 190 | + sharedSecret.Fill(b); |
| 191 | + }; |
| 192 | + |
| 193 | + ExportFunc oldExportEncapsulationKeyCoreHook = ExportEncapsulationKeyCoreHook; |
| 194 | + ExportEncapsulationKeyCoreHook = (Span<byte> destination) => |
| 195 | + { |
| 196 | + _ = oldExportEncapsulationKeyCoreHook(destination); |
| 197 | + destination.Fill(b); |
| 198 | + return destination.Length; |
| 199 | + }; |
| 200 | + |
| 201 | + ExportFunc oldExportDecapsulationKeyCoreHook = ExportDecapsulationKeyCoreHook; |
| 202 | + ExportDecapsulationKeyCoreHook = (Span<byte> destination) => |
| 203 | + { |
| 204 | + _ = oldExportDecapsulationKeyCoreHook(destination); |
| 205 | + destination.Fill(b); |
| 206 | + return destination.Length; |
| 207 | + }; |
| 208 | + |
| 209 | + TryExportFunc oldTryExportPkcs8PrivateKeyCoreHook = TryExportPkcs8PrivateKeyCoreHook; |
| 210 | + TryExportPkcs8PrivateKeyCoreHook = (Span<byte> destination, out int bytesWritten) => |
| 211 | + { |
| 212 | + bool ret = oldTryExportPkcs8PrivateKeyCoreHook(destination, out bytesWritten); |
| 213 | + destination.Fill(b); |
| 214 | + return ret; |
| 215 | + }; |
| 216 | + } |
| 217 | + |
| 218 | + public void SetNoOpHooks() |
| 219 | + { |
| 220 | + EncapsulateCoreHook = (_, _) => { }; |
| 221 | + DecapsulateCoreHook = (_, _) => { }; |
| 222 | + ExportEncapsulationKeyCoreHook = destination => destination.Length; |
| 223 | + ExportDecapsulationKeyCoreHook = destination => destination.Length; |
| 224 | + TryExportPkcs8PrivateKeyCoreHook = (Span<byte> destination, out int bytesWritten) => |
| 225 | + { |
| 226 | + bytesWritten = destination.Length; |
| 227 | + return true; |
| 228 | + }; |
| 229 | + } |
| 230 | + |
| 231 | + public void AddFillDestination(byte[] fillContents) |
| 232 | + { |
| 233 | + ExportFunc oldExportEncapsulationKeyCoreHook = ExportEncapsulationKeyCoreHook; |
| 234 | + ExportEncapsulationKeyCoreHook = (Span<byte> destination) => |
| 235 | + { |
| 236 | + _ = oldExportEncapsulationKeyCoreHook(destination); |
| 237 | + |
| 238 | + if (fillContents.AsSpan().TryCopyTo(destination)) |
| 239 | + { |
| 240 | + return fillContents.Length; |
| 241 | + } |
| 242 | + |
| 243 | + return 0; |
| 244 | + }; |
| 245 | + |
| 246 | + ExportFunc oldExportDecapsulationKeyCoreHook = ExportDecapsulationKeyCoreHook; |
| 247 | + ExportDecapsulationKeyCoreHook = (Span<byte> destination) => |
| 248 | + { |
| 249 | + _ = oldExportDecapsulationKeyCoreHook(destination); |
| 250 | + |
| 251 | + if (fillContents.AsSpan().TryCopyTo(destination)) |
| 252 | + { |
| 253 | + return fillContents.Length; |
| 254 | + } |
| 255 | + |
| 256 | + return 0; |
| 257 | + }; |
| 258 | + |
| 259 | + TryExportFunc oldTryExportPkcs8PrivateKeyCoreHook = TryExportPkcs8PrivateKeyCoreHook; |
| 260 | + TryExportPkcs8PrivateKeyCoreHook = (Span<byte> destination, out int bytesWritten) => |
| 261 | + { |
| 262 | + _ = oldTryExportPkcs8PrivateKeyCoreHook(destination, out _); |
| 263 | + |
| 264 | + if (fillContents.AsSpan().TryCopyTo(destination)) |
| 265 | + { |
| 266 | + bytesWritten = fillContents.Length; |
| 267 | + return true; |
| 268 | + } |
| 269 | + |
| 270 | + bytesWritten = 0; |
| 271 | + return false; |
| 272 | + }; |
| 273 | + } |
| 274 | + } |
| 275 | +} |
0 commit comments