From bedc89e7af1b016994a63e4f3391d7cc7a08a8a6 Mon Sep 17 00:00:00 2001 From: Ben Laurie Date: Fri, 9 Apr 2004 14:55:43 +0000 Subject: [PATCH] Use HMAC instead of straight SHA-1. --- apps/dgst.c | 29 ++++++++------ crypto/bio/bio.h | 1 + crypto/evp/bio_md.c | 6 +++ crypto/evp/evp.h | 1 + fips/aes/fingerprint.sha1 | 6 +-- fips/des/fingerprint.sha1 | 8 ++-- fips/dsa/fingerprint.sha1 | 6 +-- fips/fingerprint.sha1 | 8 ++-- fips/fips.c | 31 ++++++++------- fips/openssl_fips_fingerprint | 6 +-- fips/rand/fingerprint.sha1 | 4 +- fips/rsa/fingerprint.sha1 | 6 +-- fips/sha1/fingerprint.sha1 | 8 ++-- fips/sha1/fips_standalone_sha1.c | 68 +++++++++++++++++++++++++++++--- fips/sha1/standalone.sha1 | 10 ++--- 15 files changed, 135 insertions(+), 63 deletions(-) diff --git a/apps/dgst.c b/apps/dgst.c index 7d6acc6546..ea923a6fbd 100644 --- a/apps/dgst.c +++ b/apps/dgst.c @@ -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; } diff --git a/crypto/bio/bio.h b/crypto/bio/bio.h index fbbc16d00c..2eb703830f 100644 --- a/crypto/bio/bio.h +++ b/crypto/bio/bio.h @@ -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) diff --git a/crypto/evp/bio_md.c b/crypto/evp/bio_md.c index c632dfb202..53f1f56cad 100644 --- a/crypto/evp/bio_md.c +++ b/crypto/evp/bio_md.c @@ -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); diff --git a/crypto/evp/evp.h b/crypto/evp/evp.h index 81b52fb4b6..115878ff17 100644 --- a/crypto/evp/evp.h +++ b/crypto/evp/evp.h @@ -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) diff --git a/fips/aes/fingerprint.sha1 b/fips/aes/fingerprint.sha1 index 60ebbb010d..b16b9c5cef 100644 --- a/fips/aes/fingerprint.sha1 +++ b/fips/aes/fingerprint.sha1 @@ -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 diff --git a/fips/des/fingerprint.sha1 b/fips/des/fingerprint.sha1 index 043639e3a9..d5d8654723 100644 --- a/fips/des/fingerprint.sha1 +++ b/fips/des/fingerprint.sha1 @@ -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 diff --git a/fips/dsa/fingerprint.sha1 b/fips/dsa/fingerprint.sha1 index 9a4d3fe900..d760ed4f4a 100644 --- a/fips/dsa/fingerprint.sha1 +++ b/fips/dsa/fingerprint.sha1 @@ -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 diff --git a/fips/fingerprint.sha1 b/fips/fingerprint.sha1 index 94d96b7ca1..30502c2dfe 100644 --- a/fips/fingerprint.sha1 +++ b/fips/fingerprint.sha1 @@ -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 diff --git a/fips/fips.c b/fips/fips.c index ddfe0f5358..c3f677217c 100644 --- a/fips/fips.c +++ b/fips/fips.c @@ -52,7 +52,7 @@ #include #include #include -#include +#include #include #include #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); diff --git a/fips/openssl_fips_fingerprint b/fips/openssl_fips_fingerprint index 41db3c9a6d..3ab62de5cf 100755 --- a/fips/openssl_fips_fingerprint +++ b/fips/openssl_fips_fingerprint @@ -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 diff --git a/fips/rand/fingerprint.sha1 b/fips/rand/fingerprint.sha1 index ffb57a6cc4..2983f14f6c 100644 --- a/fips/rand/fingerprint.sha1 +++ b/fips/rand/fingerprint.sha1 @@ -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 diff --git a/fips/rsa/fingerprint.sha1 b/fips/rsa/fingerprint.sha1 index 14caeb1d23..56f73808b6 100644 --- a/fips/rsa/fingerprint.sha1 +++ b/fips/rsa/fingerprint.sha1 @@ -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 diff --git a/fips/sha1/fingerprint.sha1 b/fips/sha1/fingerprint.sha1 index a6ac8cdb1a..a43dccb423 100644 --- a/fips/sha1/fingerprint.sha1 +++ b/fips/sha1/fingerprint.sha1 @@ -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 diff --git a/fips/sha1/fips_standalone_sha1.c b/fips/sha1/fips_standalone_sha1.c index a896925ab3..b65f0c1da7 100644 --- a/fips/sha1/fips_standalone_sha1.c +++ b/fips/sha1/fips_standalone_sha1.c @@ -48,15 +48,70 @@ */ #include +#include #include #include #include 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; } + + diff --git a/fips/sha1/standalone.sha1 b/fips/sha1/standalone.sha1 index 430a341d61..64facda7c5 100644 --- a/fips/sha1/standalone.sha1 +++ b/fips/sha1/standalone.sha1 @@ -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 -- 2.25.1