Skip to content

Commit 15eb713

Browse files
author
Andres AG
committed
Add CMAC to the benchmark example
1 parent 630bfa2 commit 15eb713

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

benchmark/main.cpp

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "mbedtls/arc4.h"
4242
#include "mbedtls/des.h"
4343
#include "mbedtls/aes.h"
44+
#include "mbedtls/cmac.h"
4445
#include "mbedtls/blowfish.h"
4546
#include "mbedtls/camellia.h"
4647
#include "mbedtls/gcm.h"
@@ -164,7 +165,8 @@
164165

165166
#define OPTIONS \
166167
"md4, md5, ripemd160, sha1, sha256, sha512,\r\n" \
167-
"arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\r\n" \
168+
"arc4, camellia, blowfish,\r\n" \
169+
"des3, des, aes_cmac, des3_cmac, aes_cbc, aes_gcm, aes_ccm,\r\n" \
168170
"havege, ctr_drbg, hmac_drbg\r\n" \
169171
"rsa, dhm, ecdsa, ecdh.\r\n"
170172

@@ -311,7 +313,8 @@ unsigned char buf[BUFSIZE];
311313

312314
typedef struct {
313315
char md4, md5, ripemd160, sha1, sha256, sha512,
314-
arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,
316+
arc4, des3, des, aes_cbc, aes_gcm, aes_ccm,
317+
aes_cmac, des3_cmac, camellia, blowfish,
315318
havege, ctr_drbg, hmac_drbg,
316319
rsa, dhm, ecdsa, ecdh;
317320
} todo_list;
@@ -360,6 +363,10 @@ static int benchmark( int argc, char *argv[] )
360363
todo.aes_gcm = 1;
361364
else if( strcmp( argv[i], "aes_ccm" ) == 0 )
362365
todo.aes_ccm = 1;
366+
else if( strcmp( argv[i], "aes_cmac" ) == 0 )
367+
todo.aes_cmac = 1;
368+
else if( strcmp( argv[i], "des3_cmac" ) == 0 )
369+
todo.des3_cmac = 1;
363370
else if( strcmp( argv[i], "camellia" ) == 0 )
364371
todo.camellia = 1;
365372
else if( strcmp( argv[i], "blowfish" ) == 0 )
@@ -455,6 +462,22 @@ static int benchmark( int argc, char *argv[] )
455462
mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
456463
mbedtls_des_free( &des );
457464
}
465+
#if defined(MBEDTLS_CMAC_C)
466+
if( todo.des3_cmac )
467+
{
468+
unsigned char output[8];
469+
const mbedtls_cipher_info_t *cipher_info;
470+
471+
memset( buf, 0, sizeof( buf ) );
472+
memset( tmp, 0, sizeof( tmp ) );
473+
474+
cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
475+
476+
TIME_AND_TSC( "3DES-CMAC",
477+
mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
478+
BUFSIZE, output ) );
479+
}
480+
#endif /* MBEDTLS_CMAC_C */
458481
#endif
459482

460483
#if defined(MBEDTLS_AES_C)
@@ -524,6 +547,37 @@ static int benchmark( int argc, char *argv[] )
524547
}
525548
}
526549
#endif
550+
#if defined(MBEDTLS_CMAC_C)
551+
if( todo.aes_cmac )
552+
{
553+
unsigned char output[16];
554+
const mbedtls_cipher_info_t *cipher_info;
555+
mbedtls_cipher_type_t cipher_type;
556+
int keysize;
557+
558+
cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
559+
for( keysize = 128; keysize <= 256; keysize += 64 )
560+
{
561+
mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
562+
563+
memset( buf, 0, sizeof( buf ) );
564+
memset( tmp, 0, sizeof( tmp ) );
565+
566+
cipher_info = mbedtls_cipher_info_from_type( cipher_type );
567+
568+
TIME_AND_TSC( title,
569+
mbedtls_cipher_cmac( cipher_info, tmp, keysize,
570+
buf, BUFSIZE, output ) );
571+
cipher_type = (mbedtls_cipher_type_t)( cipher_type + 1 );
572+
}
573+
574+
memset( buf, 0, sizeof( buf ) );
575+
memset( tmp, 0, sizeof( tmp ) );
576+
TIME_AND_TSC( "AES-CMAC-PRF-128",
577+
mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
578+
output ) );
579+
}
580+
#endif /* MBEDTLS_CMAC_C */
527581
#endif
528582

529583
#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)

0 commit comments

Comments
 (0)