mirror of
				https://github.com/cuberite/polarssl.git
				synced 2025-11-04 04:32:24 -05:00 
			
		
		
		
	cmac: more cosmetic changes
This commit is contained in:
		
							parent
							
								
									ab9c5fd9b3
								
							
						
					
					
						commit
						7b555f2928
					
				@ -108,6 +108,7 @@ int mbedtls_cmac_verify( mbedtls_cmac_context *ctx,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * \brief           AES-CMAC-128-PRF
 | 
					 * \brief           AES-CMAC-128-PRF
 | 
				
			||||||
 | 
					 * TODO: add reference to the standard
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * \param ctx       CMAC context
 | 
					 * \param ctx       CMAC context
 | 
				
			||||||
 * \param key       PRF key
 | 
					 * \param key       PRF key
 | 
				
			||||||
@ -115,6 +116,7 @@ int mbedtls_cmac_verify( mbedtls_cmac_context *ctx,
 | 
				
			|||||||
 * \param input     buffer holding the input data
 | 
					 * \param input     buffer holding the input data
 | 
				
			||||||
 * \param in_len    length of the input data in bytes
 | 
					 * \param in_len    length of the input data in bytes
 | 
				
			||||||
 * \param tag       buffer holding the tag to verify (16 bytes)
 | 
					 * \param tag       buffer holding the tag to verify (16 bytes)
 | 
				
			||||||
 | 
					 *                  TODO: update description of tag
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * \return          0 if successful
 | 
					 * \return          0 if successful
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
				
			|||||||
@ -69,7 +69,8 @@ void mbedtls_cmac_init( mbedtls_cmac_context *ctx )
 | 
				
			|||||||
 *
 | 
					 *
 | 
				
			||||||
 * Input and output MUST not point to the same buffer
 | 
					 * Input and output MUST not point to the same buffer
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static void multiply_by_u( unsigned char *output, const unsigned char *input )
 | 
					static void cmac_multiply_by_u( unsigned char *output,
 | 
				
			||||||
 | 
					                                const unsigned char *input )
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    const unsigned char Rb = 0x87; /* block size 16 only */
 | 
					    const unsigned char Rb = 0x87; /* block size 16 only */
 | 
				
			||||||
    unsigned char mask;
 | 
					    unsigned char mask;
 | 
				
			||||||
@ -101,7 +102,7 @@ static void multiply_by_u( unsigned char *output, const unsigned char *input )
 | 
				
			|||||||
/*
 | 
					/*
 | 
				
			||||||
 * Generate subkeys
 | 
					 * Generate subkeys
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static int generate_subkeys( mbedtls_cmac_context *ctx )
 | 
					static int cmac_generate_subkeys( mbedtls_cmac_context *ctx )
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    int ret;
 | 
					    int ret;
 | 
				
			||||||
    unsigned char L[16];
 | 
					    unsigned char L[16];
 | 
				
			||||||
@ -118,14 +119,17 @@ static int generate_subkeys( mbedtls_cmac_context *ctx )
 | 
				
			|||||||
    /*
 | 
					    /*
 | 
				
			||||||
     * Generate K1 and K2
 | 
					     * Generate K1 and K2
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    multiply_by_u( ctx->K1, L );
 | 
					    cmac_multiply_by_u( ctx->K1, L );
 | 
				
			||||||
    multiply_by_u( ctx->K2, ctx->K1 );
 | 
					    cmac_multiply_by_u( ctx->K2, ctx->K1 );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    mbedtls_zeroize( L, sizeof( L ) );
 | 
					    mbedtls_zeroize( L, sizeof( L ) );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return( 0 );
 | 
					    return( 0 );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Set key and prepare context for use
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
int mbedtls_cmac_setkey( mbedtls_cmac_context *ctx,
 | 
					int mbedtls_cmac_setkey( mbedtls_cmac_context *ctx,
 | 
				
			||||||
                         mbedtls_cipher_id_t cipher,
 | 
					                         mbedtls_cipher_id_t cipher,
 | 
				
			||||||
                         const unsigned char *key,
 | 
					                         const unsigned char *key,
 | 
				
			||||||
@ -153,7 +157,7 @@ int mbedtls_cmac_setkey( mbedtls_cmac_context *ctx,
 | 
				
			|||||||
        return( ret );
 | 
					        return( ret );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return( generate_subkeys( ctx ) );
 | 
					    return( cmac_generate_subkeys( ctx ) );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
@ -171,9 +175,9 @@ void mbedtls_cmac_free( mbedtls_cmac_context *ctx )
 | 
				
			|||||||
 * We can't use the padding option from the cipher layer, as it only works for
 | 
					 * We can't use the padding option from the cipher layer, as it only works for
 | 
				
			||||||
 * CBC and we use ECB mode, and anyway we need to XOR K1 or K2 in addition.
 | 
					 * CBC and we use ECB mode, and anyway we need to XOR K1 or K2 in addition.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static void padding( unsigned char padded_block[16],
 | 
					static void cmac_pad( unsigned char padded_block[16],
 | 
				
			||||||
                     const unsigned char *last_block,
 | 
					                      const unsigned char *last_block,
 | 
				
			||||||
                     size_t length )
 | 
					                      size_t length )
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    size_t j;
 | 
					    size_t j;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -237,7 +241,7 @@ int mbedtls_cmac_generate( mbedtls_cmac_context *ctx,
 | 
				
			|||||||
    /* Calculate last block */
 | 
					    /* Calculate last block */
 | 
				
			||||||
    if( needs_padding )
 | 
					    if( needs_padding )
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        padding( M_last, input + 16 * ( n - 1 ), in_len % 16 );
 | 
					        cmac_pad( M_last, input + 16 * ( n - 1 ), in_len % 16 );
 | 
				
			||||||
        XOR_128( M_last, M_last, ctx->K2 );
 | 
					        XOR_128( M_last, M_last, ctx->K2 );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
@ -262,7 +266,7 @@ int mbedtls_cmac_generate( mbedtls_cmac_context *ctx,
 | 
				
			|||||||
#undef UPDATE_CMAC
 | 
					#undef UPDATE_CMAC
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * Authenticated decryption
 | 
					 * Verify tag on complete message
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
int mbedtls_cmac_verify( mbedtls_cmac_context *ctx,
 | 
					int mbedtls_cmac_verify( mbedtls_cmac_context *ctx,
 | 
				
			||||||
                         const unsigned char *input, size_t in_len,
 | 
					                         const unsigned char *input, size_t in_len,
 | 
				
			||||||
@ -281,18 +285,19 @@ int mbedtls_cmac_verify( mbedtls_cmac_context *ctx,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* Check tag in "constant-time" */
 | 
					    /* Check tag in "constant-time" */
 | 
				
			||||||
    for( diff = 0, i = 0; i < tag_len; i++ )
 | 
					    for( diff = 0, i = 0; i < tag_len; i++ )
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        diff |= tag[i] ^ check_tag[i];
 | 
					        diff |= tag[i] ^ check_tag[i];
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if( diff != 0 )
 | 
					    if( diff != 0 )
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        return( MBEDTLS_ERR_CMAC_VERIFY_FAILED );
 | 
					        return( MBEDTLS_ERR_CMAC_VERIFY_FAILED );
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return( 0 );
 | 
					    return( 0 );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * PRF based on CMAC with AES-128
 | 
				
			||||||
 | 
					 * TODO: add reference to the standard
 | 
				
			||||||
 | 
					 * TODO: do we need to take a cmac_context as an argument here?
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
int mbedtls_aes_cmac_prf_128( mbedtls_cmac_context *ctx,
 | 
					int mbedtls_aes_cmac_prf_128( mbedtls_cmac_context *ctx,
 | 
				
			||||||
                              const unsigned char *key, size_t key_length,
 | 
					                              const unsigned char *key, size_t key_length,
 | 
				
			||||||
                              const unsigned char *input, size_t in_len,
 | 
					                              const unsigned char *input, size_t in_len,
 | 
				
			||||||
@ -317,22 +322,17 @@ int mbedtls_aes_cmac_prf_128( mbedtls_cmac_context *ctx,
 | 
				
			|||||||
        ret = mbedtls_cmac_setkey( &zero_ctx, MBEDTLS_CIPHER_ID_AES,
 | 
					        ret = mbedtls_cmac_setkey( &zero_ctx, MBEDTLS_CIPHER_ID_AES,
 | 
				
			||||||
                                   zero_key, 8 * sizeof zero_key );
 | 
					                                   zero_key, 8 * sizeof zero_key );
 | 
				
			||||||
        if( ret != 0 )
 | 
					        if( ret != 0 )
 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            return( ret );
 | 
					            return( ret );
 | 
				
			||||||
        }
 | 
					
 | 
				
			||||||
        ret = mbedtls_cmac_generate( &zero_ctx, key, key_length, int_key, 16 );
 | 
					        ret = mbedtls_cmac_generate( &zero_ctx, key, key_length, int_key, 16 );
 | 
				
			||||||
        if( ret != 0 )
 | 
					        if( ret != 0 )
 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            return( ret );
 | 
					            return( ret );
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ret = mbedtls_cmac_setkey( ctx, MBEDTLS_CIPHER_ID_AES,
 | 
					    ret = mbedtls_cmac_setkey( ctx, MBEDTLS_CIPHER_ID_AES,
 | 
				
			||||||
                               int_key, 8 * sizeof int_key );
 | 
					                               int_key, 8 * sizeof int_key );
 | 
				
			||||||
    if( ret != 0 )
 | 
					    if( ret != 0 )
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        return( ret );
 | 
					        return( ret );
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    mbedtls_zeroize( int_key, sizeof( int_key ) );
 | 
					    mbedtls_zeroize( int_key, sizeof( int_key ) );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -341,7 +341,7 @@ int mbedtls_aes_cmac_prf_128( mbedtls_cmac_context *ctx,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
 | 
					#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * Examples 1 to 4 from SP800-3B corrected Appendix D.1
 | 
					 * Examples 1 to 4 from SP800-38B corrected Appendix D.1
 | 
				
			||||||
 * http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf
 | 
					 * http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -456,7 +456,7 @@ int mbedtls_cmac_self_test( int verbose )
 | 
				
			|||||||
    if( mbedtls_cmac_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
 | 
					    if( mbedtls_cmac_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if( verbose != 0 )
 | 
					        if( verbose != 0 )
 | 
				
			||||||
            mbedtls_printf( "  CMAC: setup failed" );
 | 
					            mbedtls_printf( "  CMAC: setup failed\n" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return( 1 );
 | 
					        return( 1 );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -465,7 +465,7 @@ int mbedtls_cmac_self_test( int verbose )
 | 
				
			|||||||
        ( memcmp( ctx.K2, K2, 16 ) != 0 ) )
 | 
					        ( memcmp( ctx.K2, K2, 16 ) != 0 ) )
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if( verbose != 0 )
 | 
					        if( verbose != 0 )
 | 
				
			||||||
            mbedtls_printf( "  CMAC: subkey generation failed" );
 | 
					            mbedtls_printf( "  CMAC: subkey generation failed\n" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return( 1 );
 | 
					        return( 1 );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user