Use HMAC instead of straight SHA-1.
authorBen Laurie <ben@openssl.org>
Fri, 9 Apr 2004 14:55:43 +0000 (14:55 +0000)
committerBen Laurie <ben@openssl.org>
Fri, 9 Apr 2004 14:55:43 +0000 (14:55 +0000)
15 files changed:
apps/dgst.c
crypto/bio/bio.h
crypto/evp/bio_md.c
crypto/evp/evp.h
fips/aes/fingerprint.sha1
fips/des/fingerprint.sha1
fips/dsa/fingerprint.sha1
fips/fingerprint.sha1
fips/fips.c
fips/openssl_fips_fingerprint
fips/rand/fingerprint.sha1
fips/rsa/fingerprint.sha1
fips/sha1/fingerprint.sha1
fips/sha1/fips_standalone_sha1.c
fips/sha1/standalone.sha1

index 7d6acc65463730781a4acad6dfd7158682b1fb75..ea923a6fbdbbb271f5d88e1889f928c69563006a 100644 (file)
@@ -78,7 +78,7 @@ static HMAC_CTX hmac_ctx;
 
 int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
          EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title,
-         const char *file,const char *hmac_key);
+         const char *file,BIO *bmd,const char *hmac_key);
 
 int MAIN(int, char **);
 
@@ -331,15 +331,13 @@ int MAIN(int argc, char **argv)
 
        /* we use md as a filter, reading from 'in' */
        BIO_set_md(bmd,md);
-       if (hmac_key)
-               HMAC_Init(&hmac_ctx,hmac_key,strlen(hmac_key),md);
        inp=BIO_push(bmd,in);
 
        if (argc == 0)
                {
                BIO_set_fp(in,stdin,BIO_NOCLOSE);
                err=do_fp(out, buf,inp,separator, out_bin, sigkey, sigbuf,
-                         siglen,"","(stdin)",hmac_key);
+                         siglen,"","(stdin)",bmd,hmac_key);
                }
        else
                {
@@ -357,15 +355,15 @@ int MAIN(int argc, char **argv)
                                }
                        if(!out_bin)
                                {
-                               size_t len = strlen(name)+strlen(argv[i])+(hmac_key ? 4 : 0)+5;
+                               size_t len = strlen(name)+strlen(argv[i])+(hmac_key ? 5 : 0)+5;
                                tmp=tofree=OPENSSL_malloc(len);
                                BIO_snprintf(tmp,len,"%s%s(%s)= ",
-                                                        hmac_key ? "HMAC_" : "",name,argv[i]);
+                                                        hmac_key ? "HMAC-" : "",name,argv[i]);
                                }
                        else
                                tmp="";
                        r=do_fp(out,buf,inp,separator,out_bin,sigkey,sigbuf,
-                               siglen,tmp,argv[i],hmac_key);
+                               siglen,tmp,argv[i],bmd,hmac_key);
                        if(r)
                            err=r;
                        if(tofree)
@@ -390,11 +388,21 @@ end:
 
 int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
          EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title,
-         const char *file,const char *hmac_key)
+         const char *file,BIO *bmd,const char *hmac_key)
        {
        int len;
        int i;
+       EVP_MD_CTX *md_ctx;
+
+       if (hmac_key)
+               {
+               EVP_MD *md;
 
+               BIO_get_md(bmd,&md);
+               HMAC_Init(&hmac_ctx,hmac_key,strlen(hmac_key),md);
+               BIO_get_md_ctx(bmd,&md_ctx);
+               BIO_set_md_ctx(bmd,&hmac_ctx.md_ctx);
+               }
        for (;;)
                {
                i=BIO_read(bp,(char *)buf,BUFSIZE);
@@ -438,11 +446,7 @@ int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
                        }
                }
        else if(hmac_key)
-               {
-               EVP_MD_CTX *ctx;
-               BIO_get_md_ctx(bp, &ctx);
                HMAC_Final(&hmac_ctx,buf,&len);
-               }
        else
                len=BIO_gets(bp,(char *)buf,BUFSIZE);
 
@@ -458,6 +462,7 @@ int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
                        }
                BIO_printf(out, "\n");
                }
+       BIO_set_md_ctx(bmd,md_ctx);
        return 0;
        }
 
index fbbc16d00c52c55e1cd605a44603bee726e54c85..2eb703830f4a905c643ab6bba8cd871e96d7971d 100644 (file)
@@ -347,6 +347,7 @@ typedef struct bio_f_buffer_ctx_struct
 #define BIO_C_NWRITE0                          145
 #define BIO_C_NWRITE                           146
 #define BIO_C_RESET_READ_REQUEST               147
+#define BIO_C_SET_MD_CTX                       148
 
 
 #define BIO_set_app_data(s,arg)                BIO_set_ex_data(s,0,arg)
index c632dfb20227fec1d6d4bc22dd35267a0b10bd88..53f1f56cade650f091d01acda083ee16dee47e4f 100644 (file)
@@ -199,6 +199,12 @@ static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
                else
                        ret=0;
                break;
+       case BIO_C_SET_MD_CTX:
+               if (b->init)
+                       b->ptr=ptr;
+               else
+                       ret=0;
+               break;
        case BIO_C_DO_STATE_MACHINE:
                BIO_clear_retry_flags(b);
                ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
index 81b52fb4b67872478fdccef67b77f96d00ee482e..115878ff179928dca04912899d25ce4a2168d15b 100644 (file)
@@ -499,6 +499,7 @@ void BIO_set_md(BIO *,const EVP_MD *md);
 #endif
 #define BIO_get_md(b,mdp)              BIO_ctrl(b,BIO_C_GET_MD,0,(char *)mdp)
 #define BIO_get_md_ctx(b,mdcp)     BIO_ctrl(b,BIO_C_GET_MD_CTX,0,(char *)mdcp)
+#define BIO_set_md_ctx(b,mdcp)     BIO_ctrl(b,BIO_C_SET_MD_CTX,0,(char *)mdcp)
 #define BIO_get_cipher_status(b)       BIO_ctrl(b,BIO_C_GET_CIPHER_STATUS,0,NULL)
 #define BIO_get_cipher_ctx(b,c_pp)     BIO_ctrl(b,BIO_C_GET_CIPHER_CTX,0,(char *)c_pp)
 
index 60ebbb010dc00b7eb99c14f8fe9626234fdd486f..b16b9c5cef70d770bc5dfc541fc4b99e82bcc722 100644 (file)
@@ -1,3 +1,3 @@
-SHA1(fips_aes_core.c)= 4cad001926dce3593181541ea19207256593171a
-SHA1(fips_aes_selftest.c)= 8f270e559d34a18b3771d7f0098b77dd7bf168c5
-SHA1(fips_aes_locl.h)= a3c01d9a4f9d5211e9e785852f6f1a2febfd73b6
+HMAC-SHA1(fips_aes_core.c)= 979e9a3084dc8e15d9f222bf721e6faccf6bcd18
+HMAC-SHA1(fips_aes_selftest.c)= 0bf32b515e2ee39332aa6563b45d7d44d88f4ed4
+HMAC-SHA1(fips_aes_locl.h)= ded58f0cda8cb967dc5f5f3a860601c0b8744623
index 043639e3a910c8cf5a0167cc910ee3d881f06d03..d5d86547237b93a4ad9dee357045fba1bcc667ef 100644 (file)
@@ -1,4 +1,4 @@
-SHA1(fips_des_enc.c)= 41388beadcafe125a8025968ff91b7dc60b96c49
-SHA1(fips_des_selftest.c)= 1236ecc25bcbd5ad6af8c396426d6c7783cfe941
-SHA1(fips_set_key.c)= 1e3dc1e0d02f0ab4d8fdd5e1f4db284cad1510f4
-SHA1(fips_des_locl.h)= a4cf60ca32476a2483b3e4460ec9a19c0444fd20
+HMAC-SHA1(fips_des_enc.c)= ea89417ba58c148c3d72d29438cd0bedc2315f7f
+HMAC-SHA1(fips_des_selftest.c)= 65498d95dcb502f6b506dd8804771c3d0de17f81
+HMAC-SHA1(fips_set_key.c)= e55cabd0cbe5cd17710e11270d9594c0d7237064
+HMAC-SHA1(fips_des_locl.h)= 7053848e884df47f06de9f2248380b92e58ef4e5
index 9a4d3fe900e11eac071764e4517da8a75aab1e56..d760ed4f4ad4df5a736e74f89e7e3f0b711ad463 100644 (file)
@@ -1,3 +1,3 @@
-SHA1(fips_dsa_ossl.c)= 592cd23f6e63bc08b9c960014d52aad05594f913
-SHA1(fips_dsa_gen.c)= 418cbd83675130cf7c45f3ea669b96167a1d65aa
-SHA1(fips_dsa_selftest.c)= d638e2d13912befe42e0ed6efa8a27719b6689d5
+HMAC-SHA1(fips_dsa_ossl.c)= 1295b5241620e1d5449aaeeb7084b181fc197003
+HMAC-SHA1(fips_dsa_gen.c)= e347a0c5b19cdcde37c73f6abcdc52c794d5dd4e
+HMAC-SHA1(fips_dsa_selftest.c)= 5a42b699e44b68e765fd6a1e9bcc2b923512a198
index 94d96b7ca1531a03136c0fd905f5e64e3634c810..30502c2dfed15766a6c08a2ddfa9ab1af9cc9e42 100644 (file)
@@ -1,4 +1,4 @@
-SHA1(fips.c)= 3ce5c4660e56e1a1c1ef177f3536b3098bb65290
-SHA1(fips_err_wrapper.c)= ad4a2ffa18743c83827de398c811eb6124ba0b27
-SHA1(fips.h)= da5e4f1bb957eb808c818507a76c8dcaa06dcec0
-SHA1(fips_err.h)= 8a6c9283e478afae4b30c033c5f885b1d20e75c1
+HMAC-SHA1(fips.c)= 7d28c5e09fa4e217f8618d63303e944a67cdff44
+HMAC-SHA1(fips_err_wrapper.c)= d3e2be316062510312269e98f964cb87e7577898
+HMAC-SHA1(fips.h)= 30200a267be0bed23d4a93a9e242e181e0ee2b44
+HMAC-SHA1(fips_err.h)= f4203a47100a815c21cf3a97092f91a595938f7c
index ddfe0f53587cd6c897ce3c890e1c77f735e303dc..c3f677217c9200ccb0da847e24da4be5c5f8c5e7 100644 (file)
@@ -52,7 +52,7 @@
 #include <openssl/fips_rand.h>
 #include <openssl/err.h>
 #include <openssl/bio.h>
-#include <openssl/evp.h>
+#include <openssl/hmac.h>
 #include <string.h>
 #include <limits.h>
 #include "fips_locl.h"
@@ -75,42 +75,43 @@ int FIPS_selftest()
 
 static int FIPS_check_exe(const char *path)
     {
-    BIO *bio, *md;
     char buf[1024];
     char p2[PATH_MAX];
     int n;
     char mdbuf[EVP_MAX_MD_SIZE];
+    FILE *f;
+    static char key[]="etaonrishdlcupfm";
+    HMAC_CTX hmac;
 
-    bio=BIO_new_file(path,"rb");
-    if(!bio)
+    f=fopen(path,"rb");
+    if(!f)
        {
        FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE);
        return 0;
        }
-    md=BIO_new(BIO_f_md());
-    BIO_set_md(md,EVP_sha1());
-    bio=BIO_push(md,bio);
+    HMAC_Init(&hmac,key,strlen(key),EVP_sha1());
     do
        {
-       n=BIO_read(bio,buf,sizeof buf);
+       n=fread(buf,1,sizeof buf,f);
        if(n < 0)
            {
-           BIO_free_all(bio);
+           fclose(f);
            FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE);
            return 0;
            }
+       HMAC_Update(&hmac,buf,n);
        } while(n > 0);
-    BIO_gets(md,mdbuf,EVP_MAX_MD_SIZE);
-    BIO_free_all(bio);
+    fclose(f);
+    HMAC_Final(&hmac,mdbuf,&n);
     BIO_snprintf(p2,sizeof p2,"%s.sha1",path);
-    bio=BIO_new_file(p2,"rb");
-    if(!bio || BIO_read(bio,buf,20) != 20)
+    f=fopen(p2,"rb");
+    if(!f || !fread(buf,1,20,f) != 20)
        {
-       BIO_free(bio);
+       fclose(f);
        FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE_DIGEST);
        return 0;
        }
-    BIO_free(bio);
+    fclose(f);
     if(memcmp(buf,mdbuf,20))
        {
        FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_EXE_DIGEST_DOES_NOT_MATCH);
index 41db3c9a6dec33af666f25cff646659d8814af9e..3ab62de5cf9ffe47dd323f0ee2ea7b07e34d8304 100755 (executable)
@@ -10,12 +10,12 @@ exe=$2
 # not yet installed
 if [ "X$TOP" != "X" ]
 then
-    PATH=$PATH:$TOP/apps
+    PATH=$TOP/apps:$PATH
 fi
 
 elib=`echo $lib| sed 's/\//\\\\\//g'`
 echo "Checking library fingerprint for $lib"
-openssl sha1 $lib | sed "s/$elib/libcrypto.a/" | diff ${lib%.a}.sha1 - || { echo "$libs fingerprint mismatch"; exit 1; }
+openssl sha1 -hmac etaonrishdlcupfm $lib | sed "s/$elib/libcrypto.a/" | diff ${lib%.a}.sha1 - || { echo "$libs fingerprint mismatch"; exit 1; }
 
 echo "Making fingerprint for $exe"
-openssl sha1 -binary $exe > $exe.sha1
+openssl sha1 -hmac etaonrishdlcupfm -binary $exe > $exe.sha1
index ffb57a6cc49eef47d050448b82e19168d971fbe1..2983f14f6c44c2e6457302b4a851168b579325bc 100644 (file)
@@ -1,2 +1,2 @@
-SHA1(fips_rand.c)= af88aebd4897e9d2ef593969b50f80c2cdf120d9
-SHA1(fips_rand.h)= da5ee76588070c97d47c939d069d250a1476a080
+HMAC-SHA1(fips_rand.c)= f65f82a78988b284668d51497f06a0fd029b17f8
+HMAC-SHA1(fips_rand.h)= 72cff1a7ca7f33fe9df6b9da30e6420874eeb623
index 14caeb1d23a3ffa45ccd936c9822390bcbbc3ad7..56f73808b6b154c09e9a47eac059bea033350087 100644 (file)
@@ -1,3 +1,3 @@
-SHA1(fips_rsa_eay.c)= eacbcc656f1f046509abb9cc0207880b58ae8b90
-SHA1(fips_rsa_gen.c)= eb47b6add96f4fe2396538b8ef394d16c4b1e87f
-SHA1(fips_rsa_selftest.c)= 0106c4c565833ad2c8975b7d38765038a58f037c
+HMAC-SHA1(fips_rsa_eay.c)= 3808fed15de958768052db96ff710699ca9afd92
+HMAC-SHA1(fips_rsa_gen.c)= bdc892485913c405f9e0459eb4e9b7e94881a4dc
+HMAC-SHA1(fips_rsa_selftest.c)= b3cd2e2ff2bcf910ec8dfc891168761cb5dca0d6
index a6ac8cdb1aac123139374e27b2acf824a0114152..a43dccb423a0a842dec2138e6dfec511795f351d 100644 (file)
@@ -1,4 +1,4 @@
-SHA1(fips_sha1dgst.c)= c197020b866c1ab32f1e30f5599592e14fcaa925
-SHA1(fips_sha1_selftest.c)= 55fef82fa5f4aec6b1359847fe50555ff77cec49
-SHA1(fips_sha_locl.h)= 623de315f9a7e320a19bce0fad25d4ca1ab085c3
-SHA1(fips_md32_common.h)= 4f41bcde24750b3b8c99a06bcba2fe06ff8db4d0
+HMAC-SHA1(fips_sha1dgst.c)= 87dd5198b5ab3df38549422d3313126085589b60
+HMAC-SHA1(fips_sha1_selftest.c)= 9719f7e68950250ce4b7726be3e560c24cd72a32
+HMAC-SHA1(fips_sha_locl.h)= 199ceca9016ba5514997ce1fcd22be7d4f66e9b5
+HMAC-SHA1(fips_md32_common.h)= 4dd5d27e9bedbbaab6c6c5536b254e040137471e
index a896925ab3b13ff57e24841c645e58d7f257b1f0..b65f0c1da76df7bae28756e3b5cb5dd8651872eb 100644 (file)
  */
 
 #include <openssl/sha.h>
+#include <openssl/hmac.h>
 #include <openssl/opensslconf.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 int FIPS_selftest_fail;
 
+static void hmac_init(SHA_CTX *md_ctx,SHA_CTX *i_ctx,SHA_CTX *o_ctx,
+                     const char *key)
+    {
+    int len=strlen(key);
+    int i;
+    unsigned char keymd[HMAC_MAX_MD_CBLOCK];
+    unsigned char pad[HMAC_MAX_MD_CBLOCK];
+
+    if (len > SHA_CBLOCK)
+       {
+       //      EVP_DigestInit_ex(&ctx->md_ctx,md, impl);
+       SHA1_Init(md_ctx);
+       //      EVP_DigestUpdate(&ctx->md_ctx,key,len);
+       SHA1_Update(md_ctx,key,len);
+       //      EVP_DigestFinal_ex(&(ctx->md_ctx),ctx->key,&ctx->key_length);
+       SHA1_Final(keymd,md_ctx);
+       len=20;
+       }
+    else
+       memcpy(keymd,key,len);
+    memset(&keymd[len],'\0',HMAC_MAX_MD_CBLOCK-len);
+
+    for(i=0 ; i < HMAC_MAX_MD_CBLOCK ; i++)
+       pad[i]=0x36^keymd[i];
+    //    EVP_DigestInit_ex(&ctx->i_ctx,md, impl);
+    SHA1_Init(md_ctx);
+    //    EVP_DigestUpdate(&ctx->i_ctx,pad,EVP_MD_block_size(md));
+    SHA1_Update(md_ctx,pad,SHA_CBLOCK);
+
+    for(i=0 ; i < HMAC_MAX_MD_CBLOCK ; i++)
+       pad[i]=0x5c^keymd[i];
+    //    EVP_DigestInit_ex(&ctx->o_ctx,md, impl);
+    SHA1_Init(o_ctx);
+    //    EVP_DigestUpdate(&ctx->o_ctx,pad,EVP_MD_block_size(md));
+    SHA1_Update(o_ctx,pad,SHA_CBLOCK);
+    //    EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->i_ctx);
+    //    memcpy(md_ctx,i_ctx,sizeof *md_ctx);
+    }
+
+static void hmac_final(unsigned char *md,SHA_CTX *md_ctx,SHA_CTX *o_ctx)
+    {
+    unsigned char buf[20];
+
+    //EVP_DigestFinal_ex(&ctx->md_ctx,buf,&i);
+    SHA1_Final(buf,md_ctx);
+    //EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->o_ctx);
+    //    memcpy(md_ctx,o_ctx,sizeof *md_ctx);
+    //EVP_DigestUpdate(&ctx->md_ctx,buf,i);
+    SHA1_Update(o_ctx,buf,sizeof buf);
+    //EVP_DigestFinal_ex(&ctx->md_ctx,md,len);
+    SHA1_Final(md,o_ctx);
+    }
+
 int main(int argc,char **argv)
     {
 #ifdef OPENSSL_FIPS
+    static char key[]="etaonrishdlcupfm";
     int n;
 
     if(argc < 2)
@@ -68,7 +123,7 @@ int main(int argc,char **argv)
     for(n=1 ; n < argc ; ++n)
        {
        FILE *f=fopen(argv[n],"rb");
-       SHA_CTX sha;
+       SHA_CTX md_ctx,i_ctx,o_ctx;
        unsigned char md[20];
        int i;
 
@@ -78,7 +133,7 @@ int main(int argc,char **argv)
            exit(2);
            }
 
-       SHA1_Init(&sha);
+       hmac_init(&md_ctx,&i_ctx,&o_ctx,key);
        for( ; ; )
            {
            char buf[1024];
@@ -94,10 +149,11 @@ int main(int argc,char **argv)
                else
                    break;
                }
-           SHA1_Update(&sha,buf,l);
+           SHA1_Update(&md_ctx,buf,l);
            }
-       SHA1_Final(md,&sha);
-       printf("SHA1(%s)= ",argv[n]);
+       hmac_final(md,&md_ctx,&o_ctx);
+
+       printf("HMAC-SHA1(%s)= ",argv[n]);
        for(i=0 ; i < 20 ; ++i)
            printf("%02x",md[i]);
        printf("\n");
@@ -105,3 +161,5 @@ int main(int argc,char **argv)
 #endif
     return 0;
     }
+
+
index 430a341d6104f611234b65f39791156508ad6ba3..64facda7c5ec6c92cba3ed49e06bff36eec8cfef 100644 (file)
@@ -1,5 +1,5 @@
-SHA1(fips_sha1dgst.c)= c197020b866c1ab32f1e30f5599592e14fcaa925
-SHA1(fips_sha1_selftest.c)= 55fef82fa5f4aec6b1359847fe50555ff77cec49
-SHA1(fips_standalone_sha1.c)= 77a4d5f4a1b39f186cffc12342cc14eb80664acf
-SHA1(fips_sha_locl.h)= 623de315f9a7e320a19bce0fad25d4ca1ab085c3
-SHA1(fips_md32_common.h)= 4f41bcde24750b3b8c99a06bcba2fe06ff8db4d0
+HMAC-SHA1(fips_sha1dgst.c)= 87dd5198b5ab3df38549422d3313126085589b60
+HMAC-SHA1(fips_sha1_selftest.c)= 9719f7e68950250ce4b7726be3e560c24cd72a32
+HMAC-SHA1(fips_standalone_sha1.c)= a66055fef7d4dcc59479746e53183738d01b8e6d
+HMAC-SHA1(fips_sha_locl.h)= 199ceca9016ba5514997ce1fcd22be7d4f66e9b5
+HMAC-SHA1(fips_md32_common.h)= 4dd5d27e9bedbbaab6c6c5536b254e040137471e