@@ -195,10 +195,9 @@ _sha3_sha3_224_digest_impl(SHA3object *self)
195195/*[clinic end generated code: output=fd531842e20b2d5b input=5b2a659536bbd248]*/
196196{
197197 unsigned char digest [SHA3_MAX_DIGESTSIZE ];
198- // The only potential error here is an API misuse, such as trying to specify
199- // a user-provided length when using a non-Shake algorithm. We thus ignore
200- // the return code.
201- Hacl_Streaming_Keccak_finish (self -> hash_state , digest , 0 );
198+ // This function errors out if the algorithm is Shake. Here, we know this
199+ // not to be the case, and therefore do not perform error checking.
200+ Hacl_Streaming_Keccak_finish (self -> hash_state , digest );
202201 return PyBytes_FromStringAndSize ((const char * )digest ,
203202 Hacl_Streaming_Keccak_hash_len (self -> hash_state ));
204203}
@@ -215,7 +214,7 @@ _sha3_sha3_224_hexdigest_impl(SHA3object *self)
215214/*[clinic end generated code: output=75ad03257906918d input=2d91bb6e0d114ee3]*/
216215{
217216 unsigned char digest [SHA3_MAX_DIGESTSIZE ];
218- Hacl_Streaming_Keccak_finish (self -> hash_state , digest , 0 );
217+ Hacl_Streaming_Keccak_finish (self -> hash_state , digest );
219218 return _Py_strhex ((const char * )digest ,
220219 Hacl_Streaming_Keccak_hash_len (self -> hash_state ));
221220}
@@ -397,8 +396,12 @@ _SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex)
397396 return PyErr_NoMemory ();
398397 }
399398
400- /* Get the raw (binary) digest value */
401- Hacl_Streaming_Keccak_finish (self -> hash_state , digest , digestlen );
399+ /* Get the raw (binary) digest value. The HACL functions errors out if:
400+ * - the algorith is not shake -- not the case here
401+ * - the output length is zero -- we follow the existing behavior and return
402+ * an empty digest, without raising an error */
403+ if (digestlen > 0 )
404+ Hacl_Streaming_Keccak_squeeze (self -> hash_state , digest , digestlen );
402405 if (hex ) {
403406 result = _Py_strhex ((const char * )digest , digestlen );
404407 } else {
0 commit comments