@@ -83,46 +83,63 @@ public void Decrypt_HandlesEmptyCiphertextPointerFixup()
8383 Assert . Equal ( new byte [ ] { 0x20 , 0x21 , 0x22 } , retVal ) ;
8484 }
8585
86- internal abstract class MockableEncryptor : IOptimizedAuthenticatedEncryptor, ISpanAuthenticatedEncryptor , IDisposable
86+ internal abstract unsafe class MockableEncryptor : IOptimizedAuthenticatedEncryptor, ISpanAuthenticatedEncryptor , IDisposable
8787 {
8888 public abstract byte [ ] DecryptHook ( IntPtr pbCiphertext , uint cbCiphertext , IntPtr pbAdditionalAuthenticatedData , uint cbAdditionalAuthenticatedData ) ;
8989 public abstract byte [ ] EncryptHook ( IntPtr pbPlaintext , uint cbPlaintext , IntPtr pbAdditionalAuthenticatedData , uint cbAdditionalAuthenticatedData , uint cbPreBuffer , uint cbPostBuffer ) ;
9090
91- public int GetEncryptedSize ( int plainTextLength )
92- {
93- throw new NotImplementedException ( ) ;
94- }
91+ public int GetEncryptedSize ( int plainTextLength ) => 1000 ;
92+ public int GetDecryptedSize ( int cipherTextLength ) => 1000 ;
9593
96- public int GetDecryptedSize ( int cipherTextLength )
94+ public byte [ ] Decrypt ( ArraySegment < byte > ciphertext , ArraySegment < byte > additionalAuthenticatedData )
9795 {
98- throw new NotImplementedException ( ) ;
99- }
96+ fixed ( byte * pbCiphertext = ciphertext . Array )
97+ fixed ( byte * pbAAD = additionalAuthenticatedData . Array )
98+ {
99+ IntPtr ptrCiphertext = ( IntPtr ) ( pbCiphertext + ciphertext . Offset ) ;
100+ IntPtr ptrAAD = ( IntPtr ) ( pbAAD + additionalAuthenticatedData . Offset ) ;
100101
101- public bool TryEncrypt ( ReadOnlySpan < byte > plaintext , ReadOnlySpan < byte > additionalAuthenticatedData , Span < byte > destination , out int bytesWritten )
102- {
103- throw new NotImplementedException ( ) ;
102+ return DecryptHook ( ptrCiphertext , ( uint ) ciphertext . Count , ptrAAD , ( uint ) additionalAuthenticatedData . Count ) ;
103+ }
104104 }
105105
106- public bool TryDecrypt ( ReadOnlySpan < byte > cipherText , ReadOnlySpan < byte > additionalAuthenticatedData , Span < byte > destination , out int bytesWritten )
106+ public byte [ ] Encrypt ( ArraySegment < byte > plaintext , ArraySegment < byte > additionalAuthenticatedData , uint preBufferSize , uint postBufferSize )
107107 {
108- throw new NotImplementedException ( ) ;
109- }
108+ fixed ( byte * pbPlaintext = plaintext . Array )
109+ fixed ( byte * pbAAD = additionalAuthenticatedData . Array )
110+ {
111+ IntPtr ptrPlaintext = ( IntPtr ) ( pbPlaintext + plaintext . Offset ) ;
112+ IntPtr ptrAAD = ( IntPtr ) ( pbAAD + additionalAuthenticatedData . Offset ) ;
110113
111- public byte [ ] Decrypt ( ArraySegment < byte > ciphertext , ArraySegment < byte > additionalAuthenticatedData )
112- {
113- throw new NotImplementedException ( ) ;
114+ return EncryptHook ( ptrPlaintext , ( uint ) plaintext . Count , ptrAAD , ( uint ) additionalAuthenticatedData . Count , preBufferSize , postBufferSize ) ;
115+ }
114116 }
115117
116118 public byte [ ] Encrypt ( ArraySegment < byte > plaintext , ArraySegment < byte > additionalAuthenticatedData )
119+ => Encrypt ( plaintext , additionalAuthenticatedData , 0 , 0 ) ;
120+
121+ public bool TryEncrypt ( ReadOnlySpan < byte > plaintext , ReadOnlySpan < byte > additionalAuthenticatedData , Span < byte > destination , out int bytesWritten )
117122 {
118- throw new NotImplementedException ( ) ;
123+ var encrypted = Encrypt ( ToArraySegment ( plaintext ) , ToArraySegment ( additionalAuthenticatedData ) ) ;
124+ encrypted . CopyTo ( destination ) ;
125+ bytesWritten = encrypted . Length ;
126+ return true ;
119127 }
120128
121- public byte [ ] Encrypt ( ArraySegment < byte > plaintext , ArraySegment < byte > additionalAuthenticatedData , uint preBufferSize , uint postBufferSize )
129+ public bool TryDecrypt ( ReadOnlySpan < byte > cipherText , ReadOnlySpan < byte > additionalAuthenticatedData , Span < byte > destination , out int bytesWritten )
122130 {
123- throw new NotImplementedException ( ) ;
131+ var encrypted = Decrypt ( ToArraySegment ( cipherText ) , ToArraySegment ( additionalAuthenticatedData ) ) ;
132+ encrypted . CopyTo ( destination ) ;
133+ bytesWritten = encrypted . Length ;
134+ return true ;
124135 }
125136
126137 public void Dispose ( ) { }
138+
139+ ArraySegment < byte > ToArraySegment ( ReadOnlySpan < byte > span )
140+ {
141+ var array = span . ToArray ( ) ;
142+ return new ArraySegment < byte > ( array ) ;
143+ }
127144 }
128145}
0 commit comments