use generalise mac API for SSL key generation
authorDr. Stephen Henson <steve@openssl.org>
Wed, 24 Nov 2010 13:16:59 +0000 (13:16 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Wed, 24 Nov 2010 13:16:59 +0000 (13:16 +0000)
ssl/dtls1.h
ssl/t1_enc.c

index f85acf59d444d5b59f3b5fdcc99ab3d9fec5a91c..2900d1d8ae85fd22303ae48b316f4295740c5c6f 100644 (file)
  *
  */
 
-#ifndef HEADER_DTLS1_H
-#define HEADER_DTLS1_H
-
-/* Unless _XOPEN_SOURCE_EXTENDED is defined, struct timeval will not be
-   properly defined with DEC C, at least on VMS */
-#if defined(__DECC) || defined(__DECCXX)
-#define _XOPEN_SOURCE_EXTENDED
-#endif
+#ifndef HEADER_DTLS1_H 
+#define HEADER_DTLS1_H 
 
 #include <openssl/buffer.h>
 #include <openssl/pqueue.h>
index 34b300161d5fcc97495456cae98de8b393f5a9c5..2b3fd30e428882cb8a291ef1fc40e6756c780c5e 100644 (file)
@@ -159,68 +159,73 @@ static int tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
                        unsigned char *out, int olen)
        {
        int chunk;
-       unsigned int j;
-       HMAC_CTX ctx;
-       HMAC_CTX ctx_tmp;
+       size_t j;
+       EVP_MD_CTX ctx, ctx_tmp;
+       EVP_PKEY *mac_key;
        unsigned char A1[EVP_MAX_MD_SIZE];
-       unsigned int A1_len;
+       size_t A1_len;
        int ret = 0;
        
        chunk=EVP_MD_size(md);
        OPENSSL_assert(chunk >= 0);
 
-       HMAC_CTX_init(&ctx);
-       HMAC_CTX_init(&ctx_tmp);
-       if (!HMAC_Init_ex(&ctx,sec,sec_len,md, NULL))
+       EVP_MD_CTX_init(&ctx);
+       EVP_MD_CTX_init(&ctx_tmp);
+       mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len);
+       if (!mac_key)
+               goto err;
+       if (!EVP_DigestSignInit(&ctx,NULL,md, NULL, mac_key))
                goto err;
-       if (!HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL))
+       if (!EVP_DigestSignInit(&ctx_tmp,NULL,md, NULL, mac_key))
                goto err;
-       if (seed1 != NULL && !HMAC_Update(&ctx,seed1,seed1_len))
+       if (seed1 && !EVP_DigestSignUpdate(&ctx,seed1,seed1_len))
                goto err;
-       if (seed2 != NULL && !HMAC_Update(&ctx,seed2,seed2_len))
+       if (seed2 && !EVP_DigestSignUpdate(&ctx,seed2,seed2_len))
                goto err;
-       if (seed3 != NULL && !HMAC_Update(&ctx,seed3,seed3_len))
+       if (seed3 && !EVP_DigestSignUpdate(&ctx,seed3,seed3_len))
                goto err;
-       if (seed4 != NULL && !HMAC_Update(&ctx,seed4,seed4_len))
+       if (seed4 && !EVP_DigestSignUpdate(&ctx,seed4,seed4_len))
                goto err;
-       if (seed5 != NULL && !HMAC_Update(&ctx,seed5,seed5_len))
+       if (seed5 && !EVP_DigestSignUpdate(&ctx,seed5,seed5_len))
                goto err;
-       if (!HMAC_Final(&ctx,A1,&A1_len))
+       if (!EVP_DigestSignFinal(&ctx,A1,&A1_len))
                goto err;
 
        for (;;)
                {
-               if (!HMAC_Init_ex(&ctx,NULL,0,NULL,NULL)) /* re-init */
+               /* Reinit mac contexts */
+               if (!EVP_DigestSignInit(&ctx,NULL,md, NULL, mac_key))
                        goto err;
-               if (!HMAC_Init_ex(&ctx_tmp,NULL,0,NULL,NULL)) /* re-init */
+               if (!EVP_DigestSignInit(&ctx_tmp,NULL,md, NULL, mac_key))
                        goto err;
-               if (!HMAC_Update(&ctx,A1,A1_len))
+               if (!EVP_DigestSignUpdate(&ctx,A1,A1_len))
                        goto err;
-               if (!HMAC_Update(&ctx_tmp,A1,A1_len))
+               if (!EVP_DigestSignUpdate(&ctx_tmp,A1,A1_len))
                        goto err;
-               if (seed1 != NULL && !HMAC_Update(&ctx,seed1,seed1_len))
+               if (seed1 && !EVP_DigestSignUpdate(&ctx,seed1,seed1_len))
                        goto err;
-               if (seed2 != NULL && !HMAC_Update(&ctx,seed2,seed2_len))
+               if (seed2 && !EVP_DigestSignUpdate(&ctx,seed2,seed2_len))
                        goto err;
-               if (seed3 != NULL && !HMAC_Update(&ctx,seed3,seed3_len))
+               if (seed3 && !EVP_DigestSignUpdate(&ctx,seed3,seed3_len))
                        goto err;
-               if (seed4 != NULL && !HMAC_Update(&ctx,seed4,seed4_len))
+               if (seed4 && !EVP_DigestSignUpdate(&ctx,seed4,seed4_len))
                        goto err;
-               if (seed5 != NULL && !HMAC_Update(&ctx,seed5,seed5_len))
+               if (seed5 && !EVP_DigestSignUpdate(&ctx,seed5,seed5_len))
                        goto err;
 
                if (olen > chunk)
                        {
-                       if (!HMAC_Final(&ctx,out,&j))
+                       if (!EVP_DigestSignFinal(&ctx,out,&j))
                                goto err;
                        out+=j;
                        olen-=j;
-                       if (!HMAC_Final(&ctx_tmp,A1,&A1_len)) /* calc the next A1 value */
+                       /* calc the next A1 value */
+                       if (!EVP_DigestSignFinal(&ctx_tmp,A1,&A1_len))
                                goto err;
                        }
                else    /* last one */
                        {
-                       if (!HMAC_Final(&ctx,A1,&A1_len))
+                       if (!EVP_DigestSignFinal(&ctx,A1,&A1_len))
                                goto err;
                        memcpy(out,A1,olen);
                        break;
@@ -228,8 +233,9 @@ static int tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
                }
        ret = 1;
 err:
-       HMAC_CTX_cleanup(&ctx);
-       HMAC_CTX_cleanup(&ctx_tmp);
+       EVP_PKEY_free(mac_key);
+       EVP_MD_CTX_cleanup(&ctx);
+       EVP_MD_CTX_cleanup(&ctx_tmp);
        OPENSSL_cleanse(A1,sizeof(A1));
        return ret;
        }