idnits 2.17.00 (12 Aug 2021) /tmp/idnits54673/draft-eastlake-fnv-03.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- No issues found here. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year == Line 203 has weird spacing: '...ed char hash...' -- The document date (March 26, 2012) is 3707 days in the past. Is this intentional? -- Found something which looks like a code comment -- if you have code sections in the document, please surround them with '' and '' lines. Checking references for intended status: Informational ---------------------------------------------------------------------------- == Missing Reference: 'N' is mentioned on line 203, but not defined Summary: 0 errors (**), 0 flaws (~~), 3 warnings (==), 2 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 1 Network Working Group Glenn Fowler 2 INTERNET-DRAFT AT&T Labs Research 3 Intended Status: Informational Landon Curt Noll 4 Cisco Systems 5 Kiem-Phong Vo 6 AT&T Labs Research 7 Donald Eastlake 8 Huawei Technologies 9 Expires: September 25, 2012 March 26, 2012 11 The FNV Non-Cryptographic Hash Algorithm 12 14 Abstract 16 FNV (Fowler/Noll/Vo) is a fast, non-cryptographic hash algorithm with 17 good dispersion. The purpose of this document is to make information 18 on FNV and open source code performing FNV conveniently available to 19 the Internet community. 21 Status of This Memo 23 This Internet-Draft is submitted to IETF in full conformance with the 24 provisions of BCP 78 and BCP 79. 26 Distribution of this document is unlimited. Comments should be sent 27 to the authors. 29 Internet-Drafts are working documents of the Internet Engineering 30 Task Force (IETF), its areas, and its working groups. Note that 31 other groups may also distribute working documents as Internet- 32 Drafts. 34 Internet-Drafts are draft documents valid for a maximum of six months 35 and may be updated, replaced, or obsoleted by other documents at any 36 time. It is inappropriate to use Internet-Drafts as reference 37 material or to cite them other than as "work in progress." 39 The list of current Internet-Drafts can be accessed at 40 http://www.ietf.org/1id-abstracts.html 42 The list of Internet-Draft Shadow Directories can be accessed at 43 http://www.ietf.org/shadow.html 45 Table of Contents 47 1. Introduction............................................3 49 2. FNV Basics..............................................4 50 2.1 FNV Primes.............................................4 51 2.2 FNV offset_basis.......................................5 52 2.3 FNV Endianism..........................................5 54 3. Other Hash Sizes and XOR Folding........................6 55 4. FNV Constants...........................................7 57 5. The Source Code.........................................9 58 5.1 FNV C Header...........................................9 59 5.2 FNV C Code.............................................9 60 5.3 FNV Test Code..........................................9 62 6. Security Considerations................................10 63 6.1 Why is FNV Non-Cryptographic?.........................10 65 7. IANA Considerations....................................11 66 8. Acknowledgements.......................................11 68 9. References.............................................12 69 9.1 Normative References..................................12 70 9.2 Informative References................................12 72 Appendix A: Work Comparison with SHA-1....................13 73 Appendix B: Previous IETF Reference to FNV................14 74 Appendix C: A Few Test Vectors............................15 76 Appendix Z: Change Summary................................16 77 From -00 to -01...........................................16 78 From -01 to -02...........................................16 79 From -02 to -03...........................................16 81 1. Introduction 83 The FNV hash algorithm is based on an idea sent as reviewer comments 84 to the [IEEE] POSIX P1003.2 committee by Glenn Fowler and Phong Vo in 85 1991. In a subsequent ballot round Landon Curt Noll suggested an 86 improvement on their algorithm. Some people tried this hash and found 87 that it worked rather well. In an EMail message to Landon, they named 88 it the "Fowler/Noll/Vo" or FNV hash. [FNV] 90 FNV hashes are designed to be fast while maintaining a low collision 91 rate. The high dispersion of the FNV hashes makes them well suited 92 for hashing nearly identical strings such as URLs, hostnames, 93 filenames, text, IP addresses, etc. Their speed allows one to quickly 94 hash lots of data while maintaining a reasonably low collision rate. 95 However, they are generally not suitable for cryptographic use. (See 96 Section 6.1.) 98 The FNV hash is widely used, for example in DNS servers, database 99 indexing hashes, major web search / indexing engines, netnews history 100 file Message-ID lookup functions, anti-spam filters, a spellchecker 101 programmed in Ada 95, flatassembler's open source x86 assembler - 102 user-defined symbol hashtree, non-cryptographic file fingerprints, 103 computing Unique IDs in DASM (DTN Applications for Symbian Mobile- 104 phones), Microsoft's hash_map implementation for VC++ 2005, the 105 realpath cache in PHP 5.x (php-5.2.3/TSRM/tsrm_virtual_cwd.c), and 106 many other uses. 108 FNV hash algorithms and source code have been released into the 109 public domain. The authors of the FNV algorithm took deliberate steps 110 to disclose the algorithm in a public forum soon after it was 111 invented. More than a year passed after this public disclosure and 112 the authors deliberately took no steps to patent the FNV algorithm. 113 Therefore, it is safe to say that the FNV authors have no patent 114 claims on the FNV algorithm as published. 116 If you use an FNV function in an application, you are kindly 117 requested to send an EMail about it to: fnv-mail@asthe.com 119 2. FNV Basics 121 This document focuses on the FNV-1a function whose pseudo-code is as 122 follows: 124 hash = offset_basis 125 for each octet_of_data to be hashed 126 hash = hash xor octet_of_data 127 hash = hash * FNV_Prime 128 return hash 130 In the pseudo-code above, hash is a power-of-two number of bits (32, 131 64, ... 1024) and offset_basis and FNV_Prime depend on the size of 132 hash. 134 The FNV-1 algorithm is the same, including the values of offset_basis 135 and FNV_Prime, except that the order of the two lines with the "xor" 136 and multiply operations are reversed. Operational experience 137 indicates better hash dispersion for small amounts of data with 138 FNV-1a. FNV-0 is the same as FNV-1 but with offset_basis set to zero. 139 FNV-1a is suggested for general use. 141 2.1 FNV Primes 143 The theory behind FNV_Prime's is beyond the scope of this document 144 but the basic property to look for is how an FNV_Prime would impact 145 dispersion. Now, consider any n-bit FNV hash where n is >= 32 and 146 also a power of 2. For each such an n-bit FNV hash, an FNV_Prime p is 147 defined as: 149 When s is an integer and 4 < s < 11, then FNV_Prime is the 150 smallest prime p of the form: 152 256**int((5 + 2^s)/12) + 2**8 + b 154 where b is an integer such that: 156 0 < b < 2**8 157 The number of one-bits in b is 4 or 5 159 and where p mod (2**40 - 2**24 - 1) > (2**24 + 2**8 + 2**7). 161 Experimentally, FNV_Primes matching the above constraints tend to 162 have better dispersion properties. They improve the polynomial 163 feedback characteristic when an FNV_Prime multiplies an intermediate 164 hash value. As such, the hash values produced are more scattered 165 throughout the n-bit hash space. 167 The case where s < 5 is not considered because the resulting hash 168 quality is too low. Such small hashes can, if desired, be derived 169 from a 32 bit FNV hash by XOR folding (see Section 3). The case where 170 s > 10 is not considered because of the doubtful utility of such 171 large FNV hashes and because the criteria for such large FNV_Primes 172 is more complex, due to the sparsity of such large primes, and would 173 needlessly clutter the criteria given above. 175 Per the above constraints, an FNV_Prime should have only 6 or 7 one- 176 bits in it. Therefore, some compilers may seek to improve the 177 performance of a multiplication with an FNV_Prime by replacing the 178 multiplication with shifts and adds. However, note that the 179 performance of this substitution is highly hardware-dependent and 180 should be done with care. FNV_Primes were selected primarily for the 181 quality of resulting hash function, not for compiler optimization. 183 2.2 FNV offset_basis 185 The offset_basis values for the n-bit FNV-1a algorithms are computed 186 by applying the n-bit FNV-0 algorithm to the 32 octets representing 187 the following character string in [RFC20]: 189 chongo /\../\ 191 The \'s in the above string are not C-style escape characters. In C- 192 string notation, these 32 octets are: 194 "chongo /\\../\\" 196 2.3 FNV Endianism 198 For persistent storage or interoperability between different hardware 199 platforms, an FNV hash shall be represented in the little endian 200 format. That is, the FNV hash will be stored in an array hash[N] with 201 N bytes such that its integer value can be retrieved as follows: 203 unsigned char hash[N]; 204 for ( i = N-1, value = 0; i >= 0; --i ) 205 value = value << 8 + hash[i]; 207 Of course, when FNV hashes are used in a single process or a group of 208 processes sharing memory on processors with compatible endian-ness, 209 the natural endianness of those processors can be used regardless of 210 its type, little, big, or some other exotic form. 212 3. Other Hash Sizes and XOR Folding 214 Many hash uses require a hash that is not one of the FNV sizes for 215 which constants are provided in Section 4. If a larger hash size is 216 needed, please contact the authors of this document. 218 Most hash applications make use of a hash that is a fixed size binary 219 field. Assume that k bits of hash are desired and k is less than 1024 220 but not one of the sizes for which constants are provided in Section 221 4. The recommended technique is to take the smallest FNV hash of size 222 S, where S is larger than k, and calculate the desired hash using xor 223 folding as shown below. The final bit masking operation is logically 224 unnecessarily if the size of hash is exactly the number of desired 225 bits. 227 temp = FNV_S ( data-to-be-hashed ) 228 hash = ( temp xor temp>>k ) bitwise-and ( 2**k - 1 ) 230 Hash functions are a trade-off between speed and strength. For 231 example, a somewhat stronger hash may be obtained for exact FNV sizes 232 by calculating an FNV twice as long as the desired output ( S = 2*k ) 233 and performing such data folding using a k equal to the size of the 234 desired output. However, if a much stronger hash, for example one 235 suitable for cryptographic applications, is wanted, algorithms 236 designed for that purpose, such as those in [RFC6234], should be 237 used. 239 If it is desired to obtain a hash result that is a value between 0 240 and max, where max is a not a power of two, simply choose an FNV hash 241 size S such that 2**S > max. Then calculate the following: 243 FNV_S mod ( max+1 ) 245 The resulting remainder will be in the range desired but will suffer 246 from a bias against large values with the bias being larger if 2**S 247 is only a little bigger than max. If this bias is acceptable, no 248 further processing is needed. If this bias is unacceptable, it can be 249 avoided by retrying for certain high values of hash, as follows, 250 before applying the mod operation above: 252 X = ( int( ( 2**S - 1 ) / ( max+1 ) ) ) * ( max+1 ) 253 while ( hash >= X ) 254 hash = ( hash * FNV_Prime ) + offset_basis 256 4. FNV Constants 258 The FNV Primes are as follows: 260 32 bit FNV_Prime = 2**24 + 2**8 + 0x93 = 16,777,619 261 = 0x01000193 263 64 bit FNV_Prime = 2**40 + 2**8 + 0xB3 = 1,099,511,628,211 264 = 0x00000100 000001B3 266 128 bit FNV_Prime = 2**88 + 2**8 + 0x3B = 267 309,485,009,821,345,068,724,781,371 268 = 0x00000000 01000000 00000000 0000013B 270 256 bit FNV_Prime = 2**168 + 2**8 + 0x63 = 271 374,144,419,156,711,147,060,143,317,175,368,453,031,918,731,002,211 = 272 0x0000000000000000 0000010000000000 0000000000000000 0000000000000163 274 512 bit FNV_Prime = 2**344 + 2**8 + 0x57 = 35, 275 835,915,874,844,867,368,919,076,489,095,108,449,946,327,955,754,392, 276 558,399,825,615,420,669,938,882,575,126,094,039,892,345,713,852,759 = 277 0x0000000000000000 0000000000000000 0000000001000000 0000000000000000 278 0000000000000000 0000000000000000 0000000000000000 0000000000000157 280 1024 bit FNV_Prime = 2**680 + 2**8 + 0x8D = 5, 281 016,456,510,113,118,655,434,598,811,035,278,955,030,765,345,404,790, 282 744,303,017,523,831,112,055,108,147,451,509,157,692,220,295,382,716, 283 162,651,878,526,895,249,385,292,291,816,524,375,083,746,691,371,804, 284 094,271,873,160,484,737,966,720,260,389,217,684,476,157,468,082,573 = 285 0x0000000000000000 0000000000000000 0000000000000000 0000000000000000 286 0000000000000000 0000010000000000 0000000000000000 0000000000000000 287 0000000000000000 0000000000000000 0000000000000000 0000000000000000 288 0000000000000000 0000000000000000 0000000000000000 000000000000018D 290 The FNV offset_basis values are as follows: 292 32 bit offset_basis = 2,166,136,261 = 0x811C9DC5 294 64 bit offset_basis = 14695981039346656037 = 0xCBF29CE4 84222325 296 128 bit offset_basis = 144066263297769815596495629667062367629 = 297 0x6C62272E 07BB0142 62B82175 6295C58D 299 256 bit offset_basis = 100,029,257,958,052,580,907,070,968, 300 620,625,704,837,092,796,014,241,193,945,225,284,501,741,471,925,557 = 301 0xDD268DBCAAC55036 2D98C384C4E576CC C8B1536847B6BBB3 1023B4C8CAEE0535 302 512 bit offset_basis = 9, 303 659,303,129,496,669,498,009,435,400,716,310,466,090,418,745,672,637, 304 896,108,374,329,434,462,657,994,582,932,197,716,438,449,813,051,892, 305 206,539,805,784,495,328,239,340,083,876,191,928,701,583,869,517,785 = 306 0xB86DB0B1171F4416 DCA1E50F309990AC AC87D059C9000000 0000000000000D21 307 E948F68A34C192F6 2EA79BC942DBE7CE 182036415F56E34B AC982AAC4AFE9FD9 309 1024 bit offset_basis = 14,197,795,064,947,621,068,722,070,641,403, 310 218,320,880,622,795,441,933,960,878,474,914,617,582,723,252,296,732, 311 303,717,722,150,864,096,521,202,355,549,365,628,174,669,108,571,814, 312 760,471,015,076,148,029,755,969,804,077,320,157,692,458,563,003,215, 313 304,957,150,157,403,644,460,363,550,505,412,711,285,966,361,610,267, 314 868,082,893,823,963,790,439,336,411,086,884,584,107,735,010,676,915 = 315 0x0000000000000000 005F7A76758ECC4D 32E56D5A591028B7 4B29FC4223FDADA1 316 6C3BF34EDA3674DA 9A21D90000000000 0000000000000000 0000000000000000 317 0000000000000000 0000000000000000 0000000000000000 000000000004C6D7 318 EB6E73802734510A 555F256CC005AE55 6BDE8CC9C6A93B21 AFF4B16C71EE90B3 320 5. The Source Code 322 The following sub-sections are intended, in later versions, to 323 include reference C source code and a test driver for FNV-1a. 325 5.1 FNV C Header 327 TBD 329 5.2 FNV C Code 331 TBD 333 5.3 FNV Test Code 335 TBD 337 6. Security Considerations 339 This document is intended to provide convenient open source access by 340 the Internet community to the FNV non-cryptographic hash. No 341 assertion of suitability for cryptographic applications is made for 342 the FNV hash algorithms. 344 6.1 Why is FNV Non-Cryptographic? 346 A full discussion of cryptographic hash requirements and strength is 347 beyond the scope of this document. However, here are three 348 characteristics of FNV that would generally be considered to make it 349 non-cryptographic: 351 1. Work Factor - To make brute force inversion hard, a cryptographic 352 hash should be computationally expensive, especially for a general 353 purpose processor. But FNV is designed to be very inexpensive on a 354 general-purpose processor. (See Appendix A.) 356 2. Sticky State - A cryptographic hash should not have a state in 357 which it can stick for a plausible input pattern. But, in the very 358 unlikely event that the FNV hash variable becomes zero and the 359 input is a sequence of zeros, the hash variable will remain at 360 zero until there is a non-zero input byte and the final hash value 361 will be unaffected by the length of that sequence of zero input 362 bytes. Of course, for the common case of fixed length input, this 363 would not be significant because the number of non-zero bytes 364 would vary inversely with the number of zero bytes and for some 365 types of input runs of zeros do not occur. Furthermore, the 366 inclusion of even a little unpredictable input may be sufficient 367 to stop an adversary from inducing a zero hash variable. 369 3. Diffusion - Every output bit of a cryptographic hash should be an 370 equally complex function of every input bit. But it is easy to see 371 that the least significant bit of a direct FNV hash is the XOR of 372 the least significant bits of every input byte and does not depend 373 on any other input bit. If this is considered a problem, it can be 374 easily fixed by XOR folding (see Section 3). 376 Nevertheless, none of the above have proven to be a problem in actual 377 practice for the many applications of FNV. 379 7. IANA Considerations 381 This document requires no IANA Actions. RFC Editor Note: please 382 delete this section before publication. 384 8. Acknowledgements 386 The contributions of the following are gratefully acknowledged: 388 Frank Ellermann, Bob Moskowitz, and Stefan Santesson. 390 9. References 392 Below are the normative and informative references for this document. 394 9.1 Normative References 396 [RFC20] - Cerf, V., "ASCII format for network interchange", RFC 20, 397 October 1969. 399 9.2 Informative References 401 [FNV] - FNV web site: 402 http://www.isthe.com/chongo/tech/comp/fnv/index.html 404 [IEEE] - http://www.ieee.org 406 [RFC3174] - Eastlake 3rd, D. and P. Jones, "US Secure Hash Algorithm 407 1 (SHA1)", RFC 3174, September 2001. 409 [RFC6194] - Polk, T., Chen, L., Turner, S., and P. Hoffman, "Security 410 Considerations for the SHA-0 and SHA-1 Message-Digest 411 Algorithms", RFC 6194, March 2011. 413 [RFC6234] - Eastlake 3rd, D. and T. Hansen, "US Secure Hash 414 Algorithms (SHA and SHA-based HMAC and HKDF)", RFC 6234, May 415 2011. 417 Appendix A: Work Comparison with SHA-1 419 This section provides a simplistic rough comparison of the level of 420 effort required per input byte to compute FNV-1a and SHA-1 [RFC3174]. 422 Ignoring transfer of control and conditional tests and equating all 423 logical and arithmetic operations, FNV requires 2 operations per 424 byte, an XOR and a multiply. 426 SHA-1 is a relatively weak cryptographic hash producing a 160-bit 427 hash. It that has been partially broken [RFC6194]. It is actually 428 designed to accept a bit vector input although almost all computer 429 uses apply it to an integer number of bytes. It processes blocks of 430 512 bits (64 bytes) and we estimate the effort involved in SHA-1 431 processing a full block. Ignoring SHA-1 initial set up, transfer of 432 control, and conditional tests, but counting all logical and 433 arithmetic operations, including counting indexing as an addition, 434 SHA-1 requires 1,744 operations per 64 bytes block or 27.25 435 operations per byte. So by this rough measure, it is a little over 13 436 times the effort of FNV for large amounts of data. However, FNV is 437 commonly used for small inputs. Using the above method, for inputs of 438 N bytes, where N is <= 55 so SHA-1 will take one block (SHA-1 439 includes padding and an 8-byte length at the end of the data in the 440 last block), the ratio of the effort for SHA-1 to the effort for FNV 441 will be 872/N. For example, with an 8 byte input, SHA-1 will take 109 442 times as much effort as FNV. 444 Stronger cryptographic functions than SHA-1 generally have an even 445 high work factor. 447 Appendix B: Previous IETF Reference to FNV 449 FNV-1a was referenced in draft-ietf-tls-cached-info-08.txt that has 450 since expired. It was later decided that it would be better to use a 451 cryptographic hash for that application. 453 Below is the Jave code for FNV64 from that TLS draft include by the 454 kind permission of the author: 456 /** 457 * Java code sample, implementing 64 bit FNV-1a 458 * By Stefan Santesson 459 */ 461 import java.math.BigInteger; 463 public class FNV { 465 static public BigInteger getFNV1aToByte(byte[] inp) { 467 BigInteger m = new BigInteger("2").pow(64); 468 BigInteger fnvPrime = new BigInteger("1099511628211"); 469 BigInteger fnvOffsetBasis = 470 new BigInteger("14695981039346656037"); 472 BigInteger digest = fnvOffsetBasis; 474 for (byte b : inp) { 475 digest = digest.xor(BigInteger.valueOf((int) b & 255)); 476 digest = digest.multiply(fnvPrime).mod(m); 477 } 478 return digest; 480 } 481 } 483 Appendix C: A Few Test Vectors 485 Below are a few test vectors in the form of ASCII strings and their 486 FNV32 and FNV64 hashes using the FNV-1a algorithm. 488 Strings without null (zero byte) termination: 490 String FNV32 FNV64 491 "" 0x811c9dc5 0xcbf29ce484222325 492 "a" 0xe40c292c 0xaf63dc4c8601ec8c 493 "foobar" 0xbf9cf968 0x85944171f73967e8 495 Strings including null (zero byte) termination: 497 String FNV32 FNV64 498 "" 0x050c5d1f 0xaf63bd4c8601b7df 499 "a" 0x2b24d044 0x089be207b544f1e4 500 "foobar" 0x0c1c9eb8 0x34531ca7168b8f38 502 Appendix Z: Change Summary 504 RFC Editor Note: Please delete this appendix on publication. 506 From -00 to -01 508 1. Add Security Considerations section on why FNV is non- 509 cryptographic. 511 2. Add Appendix A on a work factor comparison with SHA-1. 513 3. Add Appendix B concerning previous IETF draft referenced to FNV. 515 4. Minor editorial changes. 517 From -01 to -02 519 1. Correct FNV_Prime determination criteria and add note as to why s 520 < 5 and s > 10 are not considered. 522 2. Add acknowledgements list. 524 3. Add a couple of references. 526 4. Minor editorial changes. 528 From -02 to -03 530 1. Replace direct reference to US-ASCII standard with reference to 531 RFC 20. 533 2. Update dates and verion number. 535 3. Minor editing change. 537 Author's Address 539 Glenn Fowler 540 AT&T Labs Research 541 180 Park Avenue 542 Florham Park, NJ 07932 USA 544 Email: gsf@research.att.com 545 URL: http://www.research.att.com/~gsf/ 547 Landon Curt Noll 548 Cisco Systems 549 170 West Tasman Drive 550 San Jose, CA 95134 USA 552 Telephone: +1-408-424-1102 553 Email: fnv-rfc-mail@asthe.com 554 URL: http://www.isthe.com/chongo/index.html 556 Kiem-Phong Vo 557 AT&T Labs Research 558 180 Park Avenue 559 Florham Park, NJ 07932 USA 561 Email: kpv@research.att.com 562 URL: http://www.research.att.com/info/kpv/ 564 Donald Eastlake 565 Huawei Technologies 566 155 Beaver Street 567 Milford, MA 01757 USA 569 Telephone: +1-508-333-2270 570 EMail: d3e3e3@gmail.com 572 Copyright, Disclaimer, and Additional IPR Provisions 574 Copyright (c) 2012 IETF Trust and the persons identified as the 575 document authors. All rights reserved. 577 This document is subject to BCP 78 and the IETF Trust's Legal 578 Provisions Relating to IETF Documents 579 (http://trustee.ietf.org/license-info) in effect on the date of 580 publication of this document. Please review these documents 581 carefully, as they describe your rights and restrictions with respect 582 to this document. Code Components extracted from this document must 583 include Simplified BSD License text as described in Section 4.e of 584 the Trust Legal Provisions and are provided without warranty as 585 described in the Simplified BSD License. This Internet-Draft is 586 submitted to IETF in full conformance with the provisions of BCP 78 587 and BCP 79.