From: Michihiro NAKAJIMA Date: Thu, 29 Jan 2009 02:38:07 +0000 (-0500) Subject: In mtree format, add support sha256/sha256digest, X-Git-Tag: v2.7.0~389 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7a01054d5f2b242511922507d582cdb6bdbeda2f;p=thirdparty%2Flibarchive.git In mtree format, add support sha256/sha256digest, sha384/sha384digest and sha512/sha512digest keywords. SVN-Revision: 504 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 95f356c6a..35894db28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -202,9 +202,9 @@ IF(MD_LIBRARY) LIST(APPEND ADDITIONAL_LIBS ${MD_LIBRARY}) ENDIF(MD_LIBRARY) # -# Find SHA1 headers +# Find SHA headers # -CHECK_HEADERS(sha.h sha1.h) +CHECK_HEADERS(sha.h sha1.h sha2.h sha256.h) # # Check functions diff --git a/cmake/config.h.in b/cmake/config.h.in index b22965a1c..48d395a6a 100644 --- a/cmake/config.h.in +++ b/cmake/config.h.in @@ -317,6 +317,12 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SHA1_H 1 +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SHA256_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SHA2_H 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SHA_H 1 diff --git a/configure.ac b/configure.ac index ad1075f67..cb602c8b2 100644 --- a/configure.ac +++ b/configure.ac @@ -197,7 +197,7 @@ fi AC_CHECK_HEADERS([md5.h md5global.h]) AC_CHECK_LIB(md,MD5Init) -AC_CHECK_HEADERS([sha.h sha1.h]) +AC_CHECK_HEADERS([sha.h sha1.h sha2.h sha256.h]) # TODO: Give the user the option of using a pre-existing system # libarchive. This will define HAVE_LIBARCHIVE which will cause diff --git a/libarchive/archive_write_set_format_mtree.c b/libarchive/archive_write_set_format_mtree.c index 8789bd479..79e670bb7 100644 --- a/libarchive/archive_write_set_format_mtree.c +++ b/libarchive/archive_write_set_format_mtree.c @@ -45,6 +45,12 @@ __FBSDID("$FreeBSD$"); #ifdef HAVE_SHA1_H #include #endif +#ifdef HAVE_SHA2_H +#include +#endif +#ifdef HAVE_SHA256_H +#include +#endif #include "archive.h" #include "archive_entry.h" @@ -65,6 +71,13 @@ struct mtree_writer { #endif #if defined(HAVE_SHA_H) || defined(HAVE_SHA1_H) SHA_CTX sha1ctx; +#endif +#if defined(HAVE_SHA2_H) || defined(HAVE_SHA256_H) + SHA256_CTX sha256ctx; +#endif +#ifdef HAVE_SHA2_H + SHA384_CTX sha384ctx; + SHA512_CTX sha512ctx; #endif /* Keyword options */ int keys; @@ -244,6 +257,28 @@ archive_write_mtree_header(struct archive_write *a, } else mtree->compute_sum &= ~F_SHA1; #endif +#if defined(HAVE_SHA2_H) || defined(HAVE_SHA256_H) + if ((mtree->keys & F_SHA256) != 0 && + archive_entry_filetype(entry) == AE_IFREG) { + mtree->compute_sum |= F_SHA256; + SHA256_Init(&mtree->sha256ctx); + } else + mtree->compute_sum &= ~F_SHA256; +#endif +#ifdef HAVE_SHA2_H + if ((mtree->keys & F_SHA384) != 0 && + archive_entry_filetype(entry) == AE_IFREG) { + mtree->compute_sum |= F_SHA384; + SHA384_Init(&mtree->sha384ctx); + } else + mtree->compute_sum &= ~F_SHA384; + if ((mtree->keys & F_SHA512) != 0 && + archive_entry_filetype(entry) == AE_IFREG) { + mtree->compute_sum |= F_SHA512; + SHA512_Init(&mtree->sha512ctx); + } else + mtree->compute_sum &= ~F_SHA512; +#endif return (ARCHIVE_OK); } @@ -387,6 +422,31 @@ archive_write_mtree_finish_entry(struct archive_write *a) archive_strcat(&mtree->buf, " sha1digest="); strappend_bin(&mtree->buf, buf, sizeof(buf)); } +#endif +#if defined(HAVE_SHA2_H) || defined(HAVE_SHA256_H) + if (mtree->compute_sum & F_SHA256) { + unsigned char buf[32]; + + SHA256_Final(buf, &mtree->sha256ctx); + archive_strcat(&mtree->buf, " sha256digest="); + strappend_bin(&mtree->buf, buf, sizeof(buf)); + } +#endif +#if defined(HAVE_SHA2_H) + if (mtree->compute_sum & F_SHA384) { + unsigned char buf[48]; + + SHA384_Final(buf, &mtree->sha384ctx); + archive_strcat(&mtree->buf, " sha384digest="); + strappend_bin(&mtree->buf, buf, sizeof(buf)); + } + if (mtree->compute_sum & F_SHA512) { + unsigned char buf[64]; + + SHA512_Final(buf, &mtree->sha512ctx); + archive_strcat(&mtree->buf, " sha512digest="); + strappend_bin(&mtree->buf, buf, sizeof(buf)); + } #endif archive_strcat(&mtree->buf, "\n"); @@ -436,6 +496,16 @@ archive_write_mtree_data(struct archive_write *a, const void *buff, size_t n) #if defined(HAVE_SHA_H) || defined(HAVE_SHA1_H) if (mtree->compute_sum & F_SHA1) SHA1_Update(&mtree->sha1ctx, buff, n); +#endif +#if defined(HAVE_SHA2_H) || defined(HAVE_SHA256_H) + if (mtree->compute_sum & F_SHA256) + SHA256_Update(&mtree->sha256ctx, buff, n); +#endif +#ifdef HAVE_SHA2_H + if (mtree->compute_sum & F_SHA384) + SHA384_Update(&mtree->sha384ctx, buff, n); + if (mtree->compute_sum & F_SHA512) + SHA384_Update(&mtree->sha512ctx, buff, n); #endif return n; } @@ -507,6 +577,19 @@ archive_write_mtree_options(struct archive_write *a, const char *key, if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0) keybit = F_SHA1; +#endif +#if defined(HAVE_SHA2_H) || defined(HAVE_SHA256_H) + if (strcmp(key, "sha256") == 0 || + strcmp(key, "sha256digest") == 0) + keybit = F_SHA256; +#endif +#ifdef HAVE_SHA2_H + if (strcmp(key, "sha384") == 0 || + strcmp(key, "sha384digest") == 0) + keybit = F_SHA384; + if (strcmp(key, "sha512") == 0 || + strcmp(key, "sha512digest") == 0) + keybit = F_SHA512; #endif if (strcmp(key, "size") == 0) keybit = F_SIZE;