6969#include <linux/unaligned.h>
7070#include <crypto/kpp.h>
7171#include <crypto/ecdh.h>
72- #include <crypto/hash .h>
73- #include <crypto/hmac .h>
72+ #include <crypto/sha2 .h>
73+ #include <crypto/utils .h>
7474
7575/* maximum number of names the TPM must remember for authorization */
7676#define AUTH_MAX_NAMES 3
@@ -384,51 +384,6 @@ EXPORT_SYMBOL_GPL(tpm_buf_append_hmac_session);
384384static int tpm2_create_primary (struct tpm_chip * chip , u32 hierarchy ,
385385 u32 * handle , u8 * name );
386386
387- /*
388- * It turns out the crypto hmac(sha256) is hard for us to consume
389- * because it assumes a fixed key and the TPM seems to change the key
390- * on every operation, so we weld the hmac init and final functions in
391- * here to give it the same usage characteristics as a regular hash
392- */
393- static void tpm2_hmac_init (struct sha256_ctx * sctx , u8 * key , u32 key_len )
394- {
395- u8 pad [SHA256_BLOCK_SIZE ];
396- int i ;
397-
398- sha256_init (sctx );
399- for (i = 0 ; i < sizeof (pad ); i ++ ) {
400- if (i < key_len )
401- pad [i ] = key [i ];
402- else
403- pad [i ] = 0 ;
404- pad [i ] ^= HMAC_IPAD_VALUE ;
405- }
406- sha256_update (sctx , pad , sizeof (pad ));
407- }
408-
409- static void tpm2_hmac_final (struct sha256_ctx * sctx , u8 * key , u32 key_len ,
410- u8 * out )
411- {
412- u8 pad [SHA256_BLOCK_SIZE ];
413- int i ;
414-
415- for (i = 0 ; i < sizeof (pad ); i ++ ) {
416- if (i < key_len )
417- pad [i ] = key [i ];
418- else
419- pad [i ] = 0 ;
420- pad [i ] ^= HMAC_OPAD_VALUE ;
421- }
422-
423- /* collect the final hash; use out as temporary storage */
424- sha256_final (sctx , out );
425-
426- sha256_init (sctx );
427- sha256_update (sctx , pad , sizeof (pad ));
428- sha256_update (sctx , out , SHA256_DIGEST_SIZE );
429- sha256_final (sctx , out );
430- }
431-
432387/*
433388 * assume hash sha256 and nonces u, v of size SHA256_DIGEST_SIZE but
434389 * otherwise standard tpm2_KDFa. Note output is in bytes not bits.
@@ -440,16 +395,16 @@ static void tpm2_KDFa(u8 *key, u32 key_len, const char *label, u8 *u,
440395 const __be32 bits = cpu_to_be32 (bytes * 8 );
441396
442397 while (bytes > 0 ) {
443- struct sha256_ctx sctx ;
398+ struct hmac_sha256_ctx hctx ;
444399 __be32 c = cpu_to_be32 (counter );
445400
446- tpm2_hmac_init ( & sctx , key , key_len );
447- sha256_update ( & sctx , (u8 * )& c , sizeof (c ));
448- sha256_update ( & sctx , label , strlen (label )+ 1 );
449- sha256_update ( & sctx , u , SHA256_DIGEST_SIZE );
450- sha256_update ( & sctx , v , SHA256_DIGEST_SIZE );
451- sha256_update ( & sctx , (u8 * )& bits , sizeof (bits ));
452- tpm2_hmac_final ( & sctx , key , key_len , out );
401+ hmac_sha256_init_usingrawkey ( & hctx , key , key_len );
402+ hmac_sha256_update ( & hctx , (u8 * )& c , sizeof (c ));
403+ hmac_sha256_update ( & hctx , label , strlen (label ) + 1 );
404+ hmac_sha256_update ( & hctx , u , SHA256_DIGEST_SIZE );
405+ hmac_sha256_update ( & hctx , v , SHA256_DIGEST_SIZE );
406+ hmac_sha256_update ( & hctx , (u8 * )& bits , sizeof (bits ));
407+ hmac_sha256_final ( & hctx , out );
453408
454409 bytes -= SHA256_DIGEST_SIZE ;
455410 counter ++ ;
@@ -593,6 +548,7 @@ void tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
593548 u32 attrs ;
594549 u8 cphash [SHA256_DIGEST_SIZE ];
595550 struct sha256_ctx sctx ;
551+ struct hmac_sha256_ctx hctx ;
596552
597553 if (!auth )
598554 return ;
@@ -704,14 +660,14 @@ void tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
704660 sha256_final (& sctx , cphash );
705661
706662 /* now calculate the hmac */
707- tpm2_hmac_init ( & sctx , auth -> session_key , sizeof ( auth -> session_key )
708- + auth -> passphrase_len );
709- sha256_update ( & sctx , cphash , sizeof ( cphash ) );
710- sha256_update ( & sctx , auth -> our_nonce , sizeof (auth -> our_nonce ));
711- sha256_update ( & sctx , auth -> tpm_nonce , sizeof (auth -> tpm_nonce ));
712- sha256_update ( & sctx , & auth -> attrs , 1 );
713- tpm2_hmac_final ( & sctx , auth -> session_key , sizeof ( auth -> session_key )
714- + auth -> passphrase_len , hmac );
663+ hmac_sha256_init_usingrawkey ( & hctx , auth -> session_key ,
664+ sizeof ( auth -> session_key ) +
665+ auth -> passphrase_len );
666+ hmac_sha256_update ( & hctx , cphash , sizeof (cphash ));
667+ hmac_sha256_update ( & hctx , auth -> our_nonce , sizeof (auth -> our_nonce ));
668+ hmac_sha256_update ( & hctx , auth -> tpm_nonce , sizeof ( auth -> tpm_nonce ) );
669+ hmac_sha256_update ( & hctx , & auth -> attrs , 1 );
670+ hmac_sha256_final ( & hctx , hmac );
715671}
716672EXPORT_SYMBOL (tpm_buf_fill_hmac_session );
717673
@@ -751,6 +707,7 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
751707 u8 rphash [SHA256_DIGEST_SIZE ];
752708 u32 attrs , cc ;
753709 struct sha256_ctx sctx ;
710+ struct hmac_sha256_ctx hctx ;
754711 u16 tag = be16_to_cpu (head -> tag );
755712 int parm_len , len , i , handles ;
756713
@@ -820,21 +777,20 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
820777 sha256_final (& sctx , rphash );
821778
822779 /* now calculate the hmac */
823- tpm2_hmac_init (& sctx , auth -> session_key , sizeof (auth -> session_key )
824- + auth -> passphrase_len );
825- sha256_update (& sctx , rphash , sizeof (rphash ));
826- sha256_update (& sctx , auth -> tpm_nonce , sizeof (auth -> tpm_nonce ));
827- sha256_update (& sctx , auth -> our_nonce , sizeof (auth -> our_nonce ));
828- sha256_update (& sctx , & auth -> attrs , 1 );
780+ hmac_sha256_init_usingrawkey (& hctx , auth -> session_key ,
781+ sizeof (auth -> session_key ) +
782+ auth -> passphrase_len );
783+ hmac_sha256_update (& hctx , rphash , sizeof (rphash ));
784+ hmac_sha256_update (& hctx , auth -> tpm_nonce , sizeof (auth -> tpm_nonce ));
785+ hmac_sha256_update (& hctx , auth -> our_nonce , sizeof (auth -> our_nonce ));
786+ hmac_sha256_update (& hctx , & auth -> attrs , 1 );
829787 /* we're done with the rphash, so put our idea of the hmac there */
830- tpm2_hmac_final (& sctx , auth -> session_key , sizeof (auth -> session_key )
831- + auth -> passphrase_len , rphash );
832- if (memcmp (rphash , & buf -> data [offset_s ], SHA256_DIGEST_SIZE ) == 0 ) {
833- rc = 0 ;
834- } else {
788+ hmac_sha256_final (& hctx , rphash );
789+ if (crypto_memneq (rphash , & buf -> data [offset_s ], SHA256_DIGEST_SIZE )) {
835790 dev_err (& chip -> dev , "TPM: HMAC check failed\n" );
836791 goto out ;
837792 }
793+ rc = 0 ;
838794
839795 /* now do response decryption */
840796 if (auth -> attrs & TPM2_SA_ENCRYPT ) {
0 commit comments