@@ -583,6 +583,33 @@ static inline void ahash_request_set_crypt(struct ahash_request *req,
583583 req -> result = result ;
584584}
585585
586+ /**
587+ * DOC: Synchronous Message Digest API
588+ *
589+ * The synchronous message digest API is used with the ciphers of type
590+ * CRYPTO_ALG_TYPE_SHASH (listed as type "shash" in /proc/crypto)
591+ *
592+ * The message digest API is able to maintain state information for the
593+ * caller.
594+ *
595+ * The synchronous message digest API can store user-related context in in its
596+ * shash_desc request data structure.
597+ */
598+
599+ /**
600+ * crypto_alloc_shash() - allocate message digest handle
601+ * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
602+ * message digest cipher
603+ * @type: specifies the type of the cipher
604+ * @mask: specifies the mask for the cipher
605+ *
606+ * Allocate a cipher handle for a message digest. The returned &struct
607+ * crypto_shash is the cipher handle that is required for any subsequent
608+ * API invocation for that message digest.
609+ *
610+ * Return: allocated cipher handle in case of success; IS_ERR() is true in case
611+ * of an error, PTR_ERR() returns the error code.
612+ */
586613struct crypto_shash * crypto_alloc_shash (const char * alg_name , u32 type ,
587614 u32 mask );
588615
@@ -591,6 +618,10 @@ static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
591618 return & tfm -> base ;
592619}
593620
621+ /**
622+ * crypto_free_shash() - zeroize and free the message digest handle
623+ * @tfm: cipher handle to be freed
624+ */
594625static inline void crypto_free_shash (struct crypto_shash * tfm )
595626{
596627 crypto_destroy_tfm (tfm , crypto_shash_tfm (tfm ));
@@ -602,6 +633,15 @@ static inline unsigned int crypto_shash_alignmask(
602633 return crypto_tfm_alg_alignmask (crypto_shash_tfm (tfm ));
603634}
604635
636+ /**
637+ * crypto_shash_blocksize() - obtain block size for cipher
638+ * @tfm: cipher handle
639+ *
640+ * The block size for the message digest cipher referenced with the cipher
641+ * handle is returned.
642+ *
643+ * Return: block size of cipher
644+ */
605645static inline unsigned int crypto_shash_blocksize (struct crypto_shash * tfm )
606646{
607647 return crypto_tfm_alg_blocksize (crypto_shash_tfm (tfm ));
@@ -617,6 +657,15 @@ static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
617657 return __crypto_shash_alg (crypto_shash_tfm (tfm )-> __crt_alg );
618658}
619659
660+ /**
661+ * crypto_shash_digestsize() - obtain message digest size
662+ * @tfm: cipher handle
663+ *
664+ * The size for the message digest created by the message digest cipher
665+ * referenced with the cipher handle is returned.
666+ *
667+ * Return: digest size of cipher
668+ */
620669static inline unsigned int crypto_shash_digestsize (struct crypto_shash * tfm )
621670{
622671 return crypto_shash_alg (tfm )-> digestsize ;
@@ -642,6 +691,21 @@ static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
642691 crypto_tfm_clear_flags (crypto_shash_tfm (tfm ), flags );
643692}
644693
694+ /**
695+ * crypto_shash_descsize() - obtain the operational state size
696+ * @tfm: cipher handle
697+ *
698+ * The size of the operational state the cipher needs during operation is
699+ * returned for the hash referenced with the cipher handle. This size is
700+ * required to calculate the memory requirements to allow the caller allocating
701+ * sufficient memory for operational state.
702+ *
703+ * The operational state is defined with struct shash_desc where the size of
704+ * that data structure is to be calculated as
705+ * sizeof(struct shash_desc) + crypto_shash_descsize(alg)
706+ *
707+ * Return: size of the operational state
708+ */
645709static inline unsigned int crypto_shash_descsize (struct crypto_shash * tfm )
646710{
647711 return tfm -> descsize ;
@@ -652,29 +716,129 @@ static inline void *shash_desc_ctx(struct shash_desc *desc)
652716 return desc -> __ctx ;
653717}
654718
719+ /**
720+ * crypto_shash_setkey() - set key for message digest
721+ * @tfm: cipher handle
722+ * @key: buffer holding the key
723+ * @keylen: length of the key in bytes
724+ *
725+ * The caller provided key is set for the keyed message digest cipher. The
726+ * cipher handle must point to a keyed message digest cipher in order for this
727+ * function to succeed.
728+ *
729+ * Return: 0 if the setting of the key was successful; < 0 if an error occurred
730+ */
655731int crypto_shash_setkey (struct crypto_shash * tfm , const u8 * key ,
656732 unsigned int keylen );
733+
734+ /**
735+ * crypto_shash_digest() - calculate message digest for buffer
736+ * @desc: see crypto_shash_final()
737+ * @data: see crypto_shash_update()
738+ * @len: see crypto_shash_update()
739+ * @out: see crypto_shash_final()
740+ *
741+ * This function is a "short-hand" for the function calls of crypto_shash_init,
742+ * crypto_shash_update and crypto_shash_final. The parameters have the same
743+ * meaning as discussed for those separate three functions.
744+ *
745+ * Return: 0 if the message digest creation was successful; < 0 if an error
746+ * occurred
747+ */
657748int crypto_shash_digest (struct shash_desc * desc , const u8 * data ,
658749 unsigned int len , u8 * out );
659750
751+ /**
752+ * crypto_shash_export() - extract operational state for message digest
753+ * @desc: reference to the operational state handle whose state is exported
754+ * @out: output buffer of sufficient size that can hold the hash state
755+ *
756+ * This function exports the hash state of the operational state handle into the
757+ * caller-allocated output buffer out which must have sufficient size (e.g. by
758+ * calling crypto_shash_descsize).
759+ *
760+ * Return: 0 if the export creation was successful; < 0 if an error occurred
761+ */
660762static inline int crypto_shash_export (struct shash_desc * desc , void * out )
661763{
662764 return crypto_shash_alg (desc -> tfm )-> export (desc , out );
663765}
664766
767+ /**
768+ * crypto_shash_import() - import operational state
769+ * @desc: reference to the operational state handle the state imported into
770+ * @in: buffer holding the state
771+ *
772+ * This function imports the hash state into the operational state handle from
773+ * the input buffer. That buffer should have been generated with the
774+ * crypto_ahash_export function.
775+ *
776+ * Return: 0 if the import was successful; < 0 if an error occurred
777+ */
665778static inline int crypto_shash_import (struct shash_desc * desc , const void * in )
666779{
667780 return crypto_shash_alg (desc -> tfm )-> import (desc , in );
668781}
669782
783+ /**
784+ * crypto_shash_init() - (re)initialize message digest
785+ * @desc: operational state handle that is already filled
786+ *
787+ * The call (re-)initializes the message digest referenced by the
788+ * operational state handle. Any potentially existing state created by
789+ * previous operations is discarded.
790+ *
791+ * Return: 0 if the message digest initialization was successful; < 0 if an
792+ * error occurred
793+ */
670794static inline int crypto_shash_init (struct shash_desc * desc )
671795{
672796 return crypto_shash_alg (desc -> tfm )-> init (desc );
673797}
674798
799+ /**
800+ * crypto_shash_update() - add data to message digest for processing
801+ * @desc: operational state handle that is already initialized
802+ * @data: input data to be added to the message digest
803+ * @len: length of the input data
804+ *
805+ * Updates the message digest state of the operational state handle.
806+ *
807+ * Return: 0 if the message digest update was successful; < 0 if an error
808+ * occurred
809+ */
675810int crypto_shash_update (struct shash_desc * desc , const u8 * data ,
676811 unsigned int len );
812+
813+ /**
814+ * crypto_shash_final() - calculate message digest
815+ * @desc: operational state handle that is already filled with data
816+ * @out: output buffer filled with the message digest
817+ *
818+ * Finalize the message digest operation and create the message digest
819+ * based on all data added to the cipher handle. The message digest is placed
820+ * into the output buffer. The caller must ensure that the output buffer is
821+ * large enough by using crypto_shash_digestsize.
822+ *
823+ * Return: 0 if the message digest creation was successful; < 0 if an error
824+ * occurred
825+ */
677826int crypto_shash_final (struct shash_desc * desc , u8 * out );
827+
828+ /**
829+ * crypto_shash_finup() - calculate message digest of buffer
830+ * @desc: see crypto_shash_final()
831+ * @data: see crypto_shash_update()
832+ * @len: see crypto_shash_update()
833+ * @out: see crypto_shash_final()
834+ *
835+ * This function is a "short-hand" for the function calls of
836+ * crypto_shash_update and crypto_shash_final. The parameters have the same
837+ * meaning as discussed for those separate functions.
838+ *
839+ * Return: 0 if the message digest creation was successful; < 0 if an error
840+ * occurred
841+ */
678842int crypto_shash_finup (struct shash_desc * desc , const u8 * data ,
679843 unsigned int len , u8 * out );
680844
0 commit comments