@@ -593,34 +593,82 @@ static void cc_setup_cipher_data(struct crypto_tfm *tfm,
593593 }
594594}
595595
596+ /*
597+ * Update a CTR-AES 128 bit counter
598+ */
599+ static void cc_update_ctr (u8 * ctr , unsigned int increment )
600+ {
601+ if (IS_ENABLED (CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS ) ||
602+ IS_ALIGNED ((unsigned long )ctr , 8 )) {
603+
604+ __be64 * high_be = (__be64 * )ctr ;
605+ __be64 * low_be = high_be + 1 ;
606+ u64 orig_low = __be64_to_cpu (* low_be );
607+ u64 new_low = orig_low + (u64 )increment ;
608+
609+ * low_be = __cpu_to_be64 (new_low );
610+
611+ if (new_low < orig_low )
612+ * high_be = __cpu_to_be64 (__be64_to_cpu (* high_be ) + 1 );
613+ } else {
614+ u8 * pos = (ctr + AES_BLOCK_SIZE );
615+ u8 val ;
616+ unsigned int size ;
617+
618+ for (; increment ; increment -- )
619+ for (size = AES_BLOCK_SIZE ; size ; size -- ) {
620+ val = * -- pos + 1 ;
621+ * pos = val ;
622+ if (val )
623+ break ;
624+ }
625+ }
626+ }
627+
596628static void cc_cipher_complete (struct device * dev , void * cc_req , int err )
597629{
598630 struct skcipher_request * req = (struct skcipher_request * )cc_req ;
599631 struct scatterlist * dst = req -> dst ;
600632 struct scatterlist * src = req -> src ;
601633 struct cipher_req_ctx * req_ctx = skcipher_request_ctx (req );
602- struct crypto_skcipher * tfm = crypto_skcipher_reqtfm (req );
603- unsigned int ivsize = crypto_skcipher_ivsize (tfm );
634+ struct crypto_skcipher * sk_tfm = crypto_skcipher_reqtfm (req );
635+ struct crypto_tfm * tfm = crypto_skcipher_tfm (sk_tfm );
636+ struct cc_cipher_ctx * ctx_p = crypto_tfm_ctx (tfm );
637+ unsigned int ivsize = crypto_skcipher_ivsize (sk_tfm );
638+ unsigned int len ;
604639
605- cc_unmap_cipher_request (dev , req_ctx , ivsize , src , dst );
606- kzfree (req_ctx -> iv );
640+ switch (ctx_p -> cipher_mode ) {
641+ case DRV_CIPHER_CBC :
642+ /*
643+ * The crypto API expects us to set the req->iv to the last
644+ * ciphertext block. For encrypt, simply copy from the result.
645+ * For decrypt, we must copy from a saved buffer since this
646+ * could be an in-place decryption operation and the src is
647+ * lost by this point.
648+ */
649+ if (req_ctx -> gen_ctx .op_type == DRV_CRYPTO_DIRECTION_DECRYPT ) {
650+ memcpy (req -> iv , req_ctx -> backup_info , ivsize );
651+ kzfree (req_ctx -> backup_info );
652+ } else if (!err ) {
653+ len = req -> cryptlen - ivsize ;
654+ scatterwalk_map_and_copy (req -> iv , req -> dst , len ,
655+ ivsize , 0 );
656+ }
657+ break ;
607658
608- /*
609- * The crypto API expects us to set the req->iv to the last
610- * ciphertext block. For encrypt, simply copy from the result.
611- * For decrypt, we must copy from a saved buffer since this
612- * could be an in-place decryption operation and the src is
613- * lost by this point.
614- */
615- if (req_ctx -> gen_ctx .op_type == DRV_CRYPTO_DIRECTION_DECRYPT ) {
616- memcpy (req -> iv , req_ctx -> backup_info , ivsize );
617- kzfree (req_ctx -> backup_info );
618- } else if (!err ) {
619- scatterwalk_map_and_copy (req -> iv , req -> dst ,
620- (req -> cryptlen - ivsize ),
621- ivsize , 0 );
659+ case DRV_CIPHER_CTR :
660+ /* Compute the counter of the last block */
661+ len = ALIGN (req -> cryptlen , AES_BLOCK_SIZE ) / AES_BLOCK_SIZE ;
662+ cc_update_ctr ((u8 * )req -> iv , len );
663+ break ;
664+
665+ default :
666+ break ;
622667 }
623668
669+ cc_unmap_cipher_request (dev , req_ctx , ivsize , src , dst );
670+ kzfree (req_ctx -> iv );
671+
624672 skcipher_request_complete (req , err );
625673}
626674
@@ -752,20 +800,29 @@ static int cc_cipher_encrypt(struct skcipher_request *req)
752800static int cc_cipher_decrypt (struct skcipher_request * req )
753801{
754802 struct crypto_skcipher * sk_tfm = crypto_skcipher_reqtfm (req );
803+ struct crypto_tfm * tfm = crypto_skcipher_tfm (sk_tfm );
804+ struct cc_cipher_ctx * ctx_p = crypto_tfm_ctx (tfm );
755805 struct cipher_req_ctx * req_ctx = skcipher_request_ctx (req );
756806 unsigned int ivsize = crypto_skcipher_ivsize (sk_tfm );
757807 gfp_t flags = cc_gfp_flags (& req -> base );
808+ unsigned int len ;
758809
759- /*
760- * Allocate and save the last IV sized bytes of the source, which will
761- * be lost in case of in-place decryption and might be needed for CTS.
762- */
763- req_ctx -> backup_info = kmalloc (ivsize , flags );
764- if (!req_ctx -> backup_info )
765- return - ENOMEM ;
810+ if (ctx_p -> cipher_mode == DRV_CIPHER_CBC ) {
811+
812+ /* Allocate and save the last IV sized bytes of the source,
813+ * which will be lost in case of in-place decryption.
814+ */
815+ req_ctx -> backup_info = kzalloc (ivsize , flags );
816+ if (!req_ctx -> backup_info )
817+ return - ENOMEM ;
818+
819+ len = req -> cryptlen - ivsize ;
820+ scatterwalk_map_and_copy (req_ctx -> backup_info , req -> src , len ,
821+ ivsize , 0 );
822+ } else {
823+ req_ctx -> backup_info = NULL ;
824+ }
766825
767- scatterwalk_map_and_copy (req_ctx -> backup_info , req -> src ,
768- (req -> cryptlen - ivsize ), ivsize , 0 );
769826 req_ctx -> is_giv = false;
770827
771828 return cc_cipher_process (req , DRV_CRYPTO_DIRECTION_DECRYPT );
0 commit comments