@@ -101,11 +101,15 @@ public int GetProtectedSize(ReadOnlySpan<byte> plainText)
101101 // Get the current key ring to access the encryptor
102102 var currentKeyRing = _keyRingProvider . GetCurrentKeyRing ( ) ;
103103 var defaultEncryptor = currentKeyRing . DefaultAuthenticatedEncryptor ;
104- CryptoUtil . Assert ( defaultEncryptor != null , "defaultEncryptorInstance != null" ) ;
104+ if ( defaultEncryptor is not IOptimizedAuthenticatedEncryptor optimizedAuthenticatedEncryptor )
105+ {
106+ throw new NotSupportedException ( "The current default encryptor does not support optimized protection." ) ;
107+ }
108+ CryptoUtil . Assert ( optimizedAuthenticatedEncryptor != null , "optimizedAuthenticatedEncryptor != null" ) ;
105109
106110 // We allocate a 20-byte pre-buffer so that we can inject the magic header and key id into the return value.
107111 // See Protect() / TryProtect() for details
108- return _magicHeaderKeyIdSize + defaultEncryptor . GetEncryptedSize ( plainText . Length ) ;
112+ return _magicHeaderKeyIdSize + optimizedAuthenticatedEncryptor . GetEncryptedSize ( plainText . Length ) ;
109113 }
110114
111115 public bool TryProtect ( ReadOnlySpan < byte > plaintext , Span < byte > destination , out int bytesWritten )
@@ -115,8 +119,14 @@ public bool TryProtect(ReadOnlySpan<byte> plaintext, Span<byte> destination, out
115119 // Perform the encryption operation using the current default encryptor.
116120 var currentKeyRing = _keyRingProvider . GetCurrentKeyRing ( ) ;
117121 var defaultKeyId = currentKeyRing . DefaultKeyId ;
118- var defaultEncryptorInstance = currentKeyRing . DefaultAuthenticatedEncryptor ;
119- CryptoUtil . Assert ( defaultEncryptorInstance != null , "defaultEncryptorInstance != null" ) ;
122+ var defaultEncryptor = currentKeyRing . DefaultAuthenticatedEncryptor ;
123+ if ( defaultEncryptor is not IOptimizedAuthenticatedEncryptor optimizedAuthenticatedEncryptor )
124+ {
125+ throw new NotSupportedException ( "The current default encryptor does not support optimized protection." ) ;
126+ }
127+ CryptoUtil . Assert ( optimizedAuthenticatedEncryptor != null , "optimizedAuthenticatedEncryptor != null" ) ;
128+
129+
120130
121131 if ( _logger . IsDebugLevelEnabled ( ) )
122132 {
@@ -130,7 +140,7 @@ public bool TryProtect(ReadOnlySpan<byte> plaintext, Span<byte> destination, out
130140 var preBufferSize = _magicHeaderKeyIdSize ;
131141 var postBufferSize = 0 ;
132142 var destinationBufferOffsets = destination . Slice ( preBufferSize , destination . Length - ( preBufferSize + postBufferSize ) ) ;
133- var success = defaultEncryptorInstance . TryEncrypt ( plaintext , aad , destinationBufferOffsets , out bytesWritten ) ;
143+ var success = optimizedAuthenticatedEncryptor . TryEncrypt ( plaintext , aad , destinationBufferOffsets , out bytesWritten ) ;
134144
135145 // At this point: destination := { 000..000 || encryptorSpecificProtectedPayload },
136146 // where 000..000 is a placeholder for our magic header and key id.
0 commit comments