Fossil SCM

fossil-scm / src / hname.c
Source Blame History 284 lines
fd9b7bd… drh 1 /*
fd9b7bd… drh 2 ** Copyright (c) 2017 D. Richard Hipp
fd9b7bd… drh 3 **
fd9b7bd… drh 4 ** This program is free software; you can redistribute it and/or
fd9b7bd… drh 5 ** modify it under the terms of the Simplified BSD License (also
fd9b7bd… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
fd9b7bd… drh 7 **
fd9b7bd… drh 8 ** This program is distributed in the hope that it will be useful,
fd9b7bd… drh 9 ** but without any warranty; without even the implied warranty of
fd9b7bd… drh 10 ** merchantability or fitness for a particular purpose.
fd9b7bd… drh 11 **
fd9b7bd… drh 12 ** Author contact information:
fd9b7bd… drh 13 ** [email protected]
fd9b7bd… drh 14 ** http://www.hwaci.com/drh/
fd9b7bd… drh 15 **
fd9b7bd… drh 16 *******************************************************************************
fd9b7bd… drh 17 **
fd9b7bd… drh 18 ** This file contains generic code for dealing with hashes used for
fd9b7bd… drh 19 ** naming artifacts. Specific hash algorithms are implemented separately
fd9b7bd… drh 20 ** (for example in sha1.c and sha3.c). This file contains the generic
e92133a… drh 21 ** interface logic.
e92133a… drh 22 **
e92133a… drh 23 ** "hname" is intended to be an abbreviation of "hash name".
fd9b7bd… drh 24 */
fd9b7bd… drh 25 #include "config.h"
fd9b7bd… drh 26 #include "hname.h"
fd9b7bd… drh 27
fd9b7bd… drh 28
fd9b7bd… drh 29 #if INTERFACE
fd9b7bd… drh 30 /*
fd9b7bd… drh 31 ** Code numbers for the allowed hash algorithms.
fd9b7bd… drh 32 */
fd9b7bd… drh 33 #define HNAME_ERROR 0 /* Not a valid hash */
fd9b7bd… drh 34 #define HNAME_SHA1 1 /* SHA1 */
fd9b7bd… drh 35 #define HNAME_K256 2 /* SHA3-256 */
fd9b7bd… drh 36
fd9b7bd… drh 37 /*
fd9b7bd… drh 38 ** Minimum and maximum lengths for a hash value when hex encoded.
fd9b7bd… drh 39 */
fd9b7bd… drh 40 #define HNAME_MIN 40 /* Length for SHA1 */
fd9b7bd… drh 41 #define HNAME_MAX 64 /* Length for SHA3-256 */
fd9b7bd… drh 42
fd9b7bd… drh 43 /*
fd9b7bd… drh 44 ** Hash lengths for the various algorithms
fd9b7bd… drh 45 */
fd9b7bd… drh 46 #define HNAME_LEN_SHA1 40
fd9b7bd… drh 47 #define HNAME_LEN_K256 64
fd9b7bd… drh 48
fd9b7bd… drh 49 /*
fd9b7bd… drh 50 ** The number of distinct hash algorithms:
fd9b7bd… drh 51 */
fd9b7bd… drh 52 #define HNAME_COUNT 2 /* Just SHA1 and SHA3-256. Let's keep it that way! */
e92133a… drh 53
e92133a… drh 54 /*
e92133a… drh 55 ** Hash naming policies
e92133a… drh 56 */
e92133a… drh 57 #define HPOLICY_SHA1 0 /* Use SHA1 hashes */
e92133a… drh 58 #define HPOLICY_AUTO 1 /* SHA1 but auto-promote to SHA3 */
e92133a… drh 59 #define HPOLICY_SHA3 2 /* Use SHA3 hashes */
e92133a… drh 60 #define HPOLICY_SHA3_ONLY 3 /* Use SHA3 hashes exclusively */
e92133a… drh 61 #define HPOLICY_SHUN_SHA1 4 /* Shun all SHA1 objects */
8a76861… jan.nijtmans 62
fd9b7bd… drh 63 #endif /* INTERFACE */
fd9b7bd… drh 64
fd9b7bd… drh 65 /*
26dac28… drh 66 ** Return a human-readable name for the hash algorithm given a hash with
26dac28… drh 67 ** a length of nHash hexadecimal digits.
26dac28… drh 68 */
26dac28… drh 69 const char *hname_alg(int nHash){
26dac28… drh 70 if( nHash==HNAME_LEN_SHA1 ) return "SHA1";
26dac28… drh 71 if( nHash==HNAME_LEN_K256 ) return "SHA3-256";
26dac28… drh 72 return "?";
26dac28… drh 73 }
26dac28… drh 74
26dac28… drh 75 /*
8a76861… jan.nijtmans 76 ** Return the integer hash algorithm code number (ex: HNAME_K256) for
fd9b7bd… drh 77 ** the hash string provided. Or return HNAME_ERROR (0) if the input string
fd9b7bd… drh 78 ** is not a valid artifact hash string.
fd9b7bd… drh 79 */
fd9b7bd… drh 80 int hname_validate(const char *zHash, int nHash){
fd9b7bd… drh 81 int id;
fd9b7bd… drh 82 switch( nHash ){
fd9b7bd… drh 83 case HNAME_LEN_SHA1: id = HNAME_SHA1; break;
fd9b7bd… drh 84 case HNAME_LEN_K256: id = HNAME_K256; break;
fd9b7bd… drh 85 default: return HNAME_ERROR;
fd9b7bd… drh 86 }
fd9b7bd… drh 87 if( !validate16(zHash, nHash) ) return HNAME_ERROR;
fd9b7bd… drh 88 return id;
fd9b7bd… drh 89 }
fd9b7bd… drh 90
fd9b7bd… drh 91 /*
fd9b7bd… drh 92 ** Verify that zHash is a valid hash for the content in pContent.
fd9b7bd… drh 93 ** Return true if the hash is correct. Return false if the content
fd9b7bd… drh 94 ** does not match the hash.
fd9b7bd… drh 95 **
fd9b7bd… drh 96 ** Actually, the returned value is one of the hash algorithm constants
fd9b7bd… drh 97 ** corresponding to the hash that matched if the hash is correct.
8a76861… jan.nijtmans 98 ** (Examples: HNAME_SHA1 or HNAME_K256). And the return is HNAME_ERROR
fd9b7bd… drh 99 ** if the hash does not match.
fd9b7bd… drh 100 */
fd9b7bd… drh 101 int hname_verify_hash(Blob *pContent, const char *zHash, int nHash){
fd9b7bd… drh 102 int id = HNAME_ERROR;
fd9b7bd… drh 103 switch( nHash ){
fd9b7bd… drh 104 case HNAME_LEN_SHA1: {
fd9b7bd… drh 105 Blob hash;
fd9b7bd… drh 106 sha1sum_blob(pContent, &hash);
fd9b7bd… drh 107 if( memcmp(blob_buffer(&hash),zHash,HNAME_LEN_SHA1)==0 ) id = HNAME_SHA1;
fd9b7bd… drh 108 blob_reset(&hash);
fd9b7bd… drh 109 break;
fd9b7bd… drh 110 }
fd9b7bd… drh 111 case HNAME_LEN_K256: {
fd9b7bd… drh 112 sha3sum_init(256);
fd9b7bd… drh 113 sha3sum_step_blob(pContent);
fd9b7bd… drh 114 if( memcmp(sha3sum_finish(0),zHash,64)==0 ) id = HNAME_K256;
fd9b7bd… drh 115 break;
fd9b7bd… drh 116 }
fd9b7bd… drh 117 }
fd9b7bd… drh 118 return id;
fd9b7bd… drh 119 }
fd9b7bd… drh 120
fd9b7bd… drh 121 /*
fd9b7bd… drh 122 ** Verify that zHash is a valid hash for the content of a file on
fd9b7bd… drh 123 ** disk named zFile.
fd9b7bd… drh 124 **
fd9b7bd… drh 125 ** Return true if the hash is correct. Return false if the content
fd9b7bd… drh 126 ** does not match the hash.
fd9b7bd… drh 127 **
fd9b7bd… drh 128 ** Actually, the returned value is one of the hash algorithm constants
fd9b7bd… drh 129 ** corresponding to the hash that matched if the hash is correct.
8a76861… jan.nijtmans 130 ** (Examples: HNAME_SHA1 or HNAME_K256). And the return is HNAME_ERROR
fd9b7bd… drh 131 ** if the hash does not match.
fd9b7bd… drh 132 */
fd9b7bd… drh 133 int hname_verify_file_hash(const char *zFile, const char *zHash, int nHash){
fd9b7bd… drh 134 int id = HNAME_ERROR;
fd9b7bd… drh 135 switch( nHash ){
fd9b7bd… drh 136 case HNAME_LEN_SHA1: {
fd9b7bd… drh 137 Blob hash;
1772357… drh 138 if( sha1sum_file(zFile, RepoFILE, &hash) ) break;
fd9b7bd… drh 139 if( memcmp(blob_buffer(&hash),zHash,HNAME_LEN_SHA1)==0 ) id = HNAME_SHA1;
fd9b7bd… drh 140 blob_reset(&hash);
fd9b7bd… drh 141 break;
fd9b7bd… drh 142 }
fd9b7bd… drh 143 case HNAME_LEN_K256: {
fd9b7bd… drh 144 Blob hash;
1772357… drh 145 if( sha3sum_file(zFile, RepoFILE, 256, &hash) ) break;
fd9b7bd… drh 146 if( memcmp(blob_buffer(&hash),zHash,64)==0 ) id = HNAME_LEN_K256;
fd9b7bd… drh 147 blob_reset(&hash);
fd9b7bd… drh 148 break;
fd9b7bd… drh 149 }
fd9b7bd… drh 150 }
fd9b7bd… drh 151 return id;
fd9b7bd… drh 152 }
fd9b7bd… drh 153
fd9b7bd… drh 154 /*
fd9b7bd… drh 155 ** Compute a hash on blob pContent. Write the hash into blob pHashOut.
fd9b7bd… drh 156 ** This routine assumes that pHashOut is uninitialized.
fd9b7bd… drh 157 **
e92133a… drh 158 ** The preferred hash is used for iHType==0 and the alternative hash is
e92133a… drh 159 ** used if iHType==1. (The interface is designed to accommodate more than
e92133a… drh 160 ** just two hashes, but HNAME_COUNT is currently fixed at 2.)
e92133a… drh 161 **
e92133a… drh 162 ** Depending on the hash policy, the alternative hash may be disallowed.
e92133a… drh 163 ** If the alterative hash is disallowed, the routine returns 0. This
e92133a… drh 164 ** routine returns 1 if iHType>0 and the alternative hash is allowed,
e92133a… drh 165 ** and it always returns 1 when iHType==0.
e92133a… drh 166 **
9a6256c… jan.nijtmans 167 ** Alternative hash is disallowed for all hash policies except auto,
9a6256c… jan.nijtmans 168 ** sha1 and sha3.
e92133a… drh 169 */
e92133a… drh 170 int hname_hash(const Blob *pContent, unsigned int iHType, Blob *pHashOut){
e92133a… drh 171 assert( iHType==0 || iHType==1 );
e92133a… drh 172 if( iHType==1 ){
e92133a… drh 173 switch( g.eHashPolicy ){
9a6256c… jan.nijtmans 174 case HPOLICY_AUTO:
e92133a… drh 175 case HPOLICY_SHA1:
e92133a… drh 176 sha3sum_blob(pContent, 256, pHashOut);
e92133a… drh 177 return 1;
e92133a… drh 178 case HPOLICY_SHA3:
e92133a… drh 179 sha1sum_blob(pContent, pHashOut);
e92133a… drh 180 return 1;
e92133a… drh 181 }
e92133a… drh 182 }
e92133a… drh 183 if( iHType==0 ){
e92133a… drh 184 switch( g.eHashPolicy ){
e92133a… drh 185 case HPOLICY_SHA1:
e92133a… drh 186 case HPOLICY_AUTO:
e92133a… drh 187 sha1sum_blob(pContent, pHashOut);
e92133a… drh 188 return 1;
e92133a… drh 189 case HPOLICY_SHA3:
e92133a… drh 190 case HPOLICY_SHA3_ONLY:
e92133a… drh 191 case HPOLICY_SHUN_SHA1:
e92133a… drh 192 sha3sum_blob(pContent, 256, pHashOut);
e92133a… drh 193 return 1;
e92133a… drh 194 }
e92133a… drh 195 }
e92133a… drh 196 blob_init(pHashOut, 0, 0);
e92133a… drh 197 return 0;
e92133a… drh 198 }
e92133a… drh 199
e92133a… drh 200 /*
e92133a… drh 201 ** Return the default hash policy for repositories that do not currently
e92133a… drh 202 ** have an assigned hash policy.
e92133a… drh 203 **
e2bdc10… danield 204 ** Make the default HPOLICY_AUTO if there are SHA1 artifacts but no SHA3
9a6256c… jan.nijtmans 205 ** artifacts in the repository. Make the default HPOLICY_SHA3 if there
e92133a… drh 206 ** are one or more SHA3 artifacts or if the repository is initially empty.
e92133a… drh 207 */
e92133a… drh 208 int hname_default_policy(void){
9a6256c… jan.nijtmans 209 if( db_exists("SELECT 1 FROM blob WHERE length(uuid)>40")
e92133a… drh 210 || !db_exists("SELECT 1 FROM blob WHERE length(uuid)==40")
e92133a… drh 211 ){
e92133a… drh 212 return HPOLICY_SHA3;
e92133a… drh 213 }else{
e92133a… drh 214 return HPOLICY_AUTO;
e92133a… drh 215 }
e92133a… drh 216 }
e92133a… drh 217
e92133a… drh 218 /*
9a6256c… jan.nijtmans 219 ** Names of the hash policies.
e92133a… drh 220 */
70a94d0… jan.nijtmans 221 static const char *const azPolicy[] = {
e92133a… drh 222 "sha1", "auto", "sha3", "sha3-only", "shun-sha1"
e92133a… drh 223 };
e92133a… drh 224
e92133a… drh 225 /* Return the name of the current hash policy.
e92133a… drh 226 */
e92133a… drh 227 const char *hpolicy_name(void){
e92133a… drh 228 return azPolicy[g.eHashPolicy];
e92133a… drh 229 }
e92133a… drh 230
e92133a… drh 231
e92133a… drh 232 /*
e92133a… drh 233 ** COMMAND: hash-policy*
e92133a… drh 234 **
e92133a… drh 235 ** Usage: fossil hash-policy ?NEW-POLICY?
e92133a… drh 236 **
e92133a… drh 237 ** Query or set the hash policy for the current repository. Available hash
e92133a… drh 238 ** policies are as follows:
e92133a… drh 239 **
e92133a… drh 240 ** sha1 New artifact names are created using SHA1
e92133a… drh 241 **
e92133a… drh 242 ** auto New artifact names are created using SHA1, but
e92133a… drh 243 ** automatically change the policy to "sha3" when
e92133a… drh 244 ** any SHA3 artifact enters the repository.
e92133a… drh 245 **
e92133a… drh 246 ** sha3 New artifact names are created using SHA3, but
e92133a… drh 247 ** older artifacts with SHA1 names may be reused.
e92133a… drh 248 **
e92133a… drh 249 ** sha3-only Use only SHA3 artifact names. Do not reuse legacy
e92133a… drh 250 ** SHA1 names.
e92133a… drh 251 **
e92133a… drh 252 ** shun-sha1 Shun any SHA1 artifacts received by sync operations
e92133a… drh 253 ** other than clones. Older legacy SHA1 artifacts are
5776542… mgagnon 254 ** allowed during a clone.
e92133a… drh 255 **
e92133a… drh 256 ** The default hash policy for existing repositories is "auto", which will
e92133a… drh 257 ** immediately promote to "sha3" if the repository contains one or more
e92133a… drh 258 ** artifacts with SHA3 names. The default hash policy for new repositories
e92133a… drh 259 ** is "shun-sha1".
fd9b7bd… drh 260 */
e92133a… drh 261 void hash_policy_command(void){
e92133a… drh 262 int i;
e92133a… drh 263 db_find_and_open_repository(0, 0);
e92133a… drh 264 if( g.argc!=2 && g.argc!=3 ) usage("?NEW-POLICY?");
e92133a… drh 265 if( g.argc==2 ){
e92133a… drh 266 fossil_print("%s\n", azPolicy[g.eHashPolicy]);
e92133a… drh 267 return;
e92133a… drh 268 }
e92133a… drh 269 for(i=HPOLICY_SHA1; i<=HPOLICY_SHUN_SHA1; i++){
e92133a… drh 270 if( fossil_strcmp(g.argv[2],azPolicy[i])==0 ){
e92133a… drh 271 if( i==HPOLICY_AUTO
9a6256c… jan.nijtmans 272 && db_exists("SELECT 1 FROM blob WHERE length(uuid)>40")
e92133a… drh 273 ){
e92133a… drh 274 i = HPOLICY_SHA3;
9a6256c… jan.nijtmans 275 }
e92133a… drh 276 g.eHashPolicy = i;
e92133a… drh 277 db_set_int("hash-policy", i, 0);
e92133a… drh 278 fossil_print("%s\n", azPolicy[i]);
e92133a… drh 279 return;
e92133a… drh 280 }
e92133a… drh 281 }
e92133a… drh 282 fossil_fatal("unknown hash policy \"%s\" - should be one of: sha1 auto"
e92133a… drh 283 " sha3 sha3-only shun-sha1", g.argv[2]);
fd9b7bd… drh 284 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button