2012-09-22 18 views

risposta

5

... Qual è la differenza tra l'EVP e HMAC

EVP_* funzioni sono un'interfaccia di alto livello. HMAC_*, AES_* e gli amici sono primitive di livello inferiore. È possibile lavorare con entrambi, ma si consiglia di lavorare con le funzioni EVP_*. Le routine HMAC_* sono basate su software e non utilizzano hardware.

Le funzioni EVP_* consentono di scambiare facilmente diversi hash e il codice rimane essenzialmente lo stesso. E sfrutterai l'accelerazione hardware, come AES-NI per un AES-CMAC, se disponibile.

Ecco un esempio OpenSSL basato su https://www.openssl.org/docs/crypto/EVP_DigestInit.html.

EVP_MD_CTX* mdctx = NULL; 
const EVP_MD* md = NULL; 

unsigned char md_value[EVP_MAX_MD_SIZE]; 
int md_len = 0; 

char message[] = "Now is the time for all good men to " 
    "come to the aide of their country\n"; 

OpenSSL_add_all_digests(); 

md = EVP_get_digestbyname("SHA1"); 
mdctx = EVP_MD_CTX_create(); 

if(!EVP_DigestInit_ex(mdctx, md, NULL)) 
    handleError(); 

if(!EVP_DigestUpdate(mdctx, message, strlen(message))) 
    handleError(); 

if(!EVP_DigestFinal_ex(mdctx, md_value, &md_len)) 
    handleError(); 

if(!EVP_MD_CTX_destroy(mdctx)) 
    handleError(); 

printf("Digest is: "); 
for(int i = 0; i < md_len; i++) 
    printf("%02x", md_value[i]); 
printf("\n"); 

Ora, e HMAC è leggermente diverso da un Hash. L'HMAC è un hash con chiave, mentre l'hash non è codificato. È inoltre possibile utilizzare le funzioni EVP_* per HMACing. Di seguito è riportato dalla pagina wiki del OpenSSL EVP Signing and Verifying:

EVP_MD_CTX* mdctx = NULL; 
const EVP_MD* md = NULL; 
EVP_PKEY *pkey = NULL; 

unsigned char md_value[EVP_MAX_MD_SIZE]; 
int md_len = 0; 

char message[] = "Now is the time for all good men to " 
    "come to the aide of their country\n"; 

OpenSSL_add_all_digests(); 

if(!(mdctx = EVP_MD_CTX_create())) 
    handleError(); 

if(!(md = EVP_get_digestbyname("SHA1"))) 
    handleError(); 

if(!(pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, "password", strlen("password")))) 
    handleError(); 

if(1 != EVP_DigestSignInit(mdctx, NULL, md, NULL, pkey)) 
    handleError(); 

/* Call update with the message */ 
if(1 != EVP_DigestSignUpdate(mdctx, message, strlen(message))) 
    handleError(); 

if(1 != EVP_DigestSignFinal(mdctx, md_value, &md_len)) 
    handleError(); 

printf("HMAC is: "); 
for(int i = 0; i < md_len; i++) 
    printf("%02x", md_value[i]); 
printf("\n"); 

L'interfaccia di basso livello sarà simile a:

EVP_MD_CTX* mdctx = NULL; 
const EVP_MD* md = NULL; 

unsigned char md_value[EVP_MAX_MD_SIZE]; 
int md_len = 0; 

char message[] = "Now is the time for all good men to " 
"come to the aide of their country\n"; 

OpenSSL_add_all_digests(); 

md = EVP_get_digestbyname("SHA1"); 
mdctx = EVP_MD_CTX_create(); 

if(!HMAC_Init_ex(mdctx, key, sizeof(key), md, NULL)) 
    handleError(); 

if(!HMAC_Update(mdctx, message, strlen(message))) 
    handleError(); 

if(!HMAC_Final(mdctx, md_value, &md_len)) 
    handleError(); 

if(!HMAC_CTX_cleanup(mdctx)) 
    handleError(); 

printf("HMAC is: "); 
for(int i = 0; i < md_len; i++) 
    printf("%02x", md_value[i]); 
printf("\n"); 
+0

C'è un grosso problema con il tuo codice. '' 'key''' non è un' '' unsigned char''' array è un '' ''EVP_PKEY'''. Inoltre, ci sono alcuni errori di battitura. – jcoffland

+1

Ho scoperto come creare l'EVP_PKEY (altro su http://wiki.openssl.org/index.php/EVP_Key_and_Parameter_Generation) –

+0

Come usare questo per calcolare CMAC ??? – user489152

1

È necessario utilizzare EVP_PKEY_new_mac_key funzione per ottenere la struttura chiave giusta per HMAC. E non dimenticare di liberarlo con EVP_PKEY_free.