📄 comment
字号:
From: Peter Gutmann (pgut1@cs.aukuni.ac.nz)Newsgroups: sci.cryptSubject: SHS/SHA source code + random thoughtsSummary: Source code for SHS/SHA message digest algorithm + comparison withMD5Keywords: SHS SHA message digestMessage-ID: <1992Sep4.060230.28313@cs.aukuni.ac.nz>Date: 4 Sep 92 06:02:30 GMTSender: pgut1@cs.aukuni.ac.nz (PeterClaus Gutmann )Organization: HPACK Conspiracy Secret LaboratoryLines: 503 The following is a C implementation of the NIST SHA (Secure HashAlgorithm) -this is sometimes also referred to as SHS (Secure Hash Standard), but I'veusedSHA for now since the standard hasn't been adopted yet. It's called SHSandSHA rather interchangeably just to confuse people, I've called it SHA inthetext but SHS in the code in anticipation of it becoming a standard. Onceit'sadopted, you can feed the text through sed and change all the names. I'llassume everyone has a copy of the SHS document and won't bother includingit inthis posting (it's rather long).The SHS/SHA Code================ It's a fairly straightforward implementation which has been tested underMSDOS and OS/2 on a PClone, and Unix on a DECstation. Some of theoptimizations used are mentioned in the code. One of the results of theseoptimizations is that the code isn't endianness-independant, meaning thatonlittle-endian machines a byteReverse() function has to be used forendianness-reversal. This entails defining LITTLE_ENDIAN on alittle-endiansystem (it's currently defined by default in SHS.H). Being able to assumeagiven data endianness makes getting data to the SHA transform() routine alotfaster.SHA Speed========= I ran the SHA code against the distributed version of MD5 (which issignificantly less optimised than the SHA code). The results were asfollows: 25 MHz PClone DECstation 2100 DECstation 5000 SHA 31 K/sec 120 K/sec 208 K/sec MD5 55 K/sec 169 K/sec 278 K/secThis comparison isn't 100% fair since the standard MD5 distribution is asomewhat pessimal implementation (in fact an optimised PC version runsaround 5times faster). An implementation of MD5 optimised to the level of SHA runsaround 2 times faster.Therefore with similar levels of optimisation it appears MD5 is aroundthreetimes as fast on the PClone as SHA. Even the pessimal MD5 on theDECstationsis around a third as fast again as SHA, and would probably also be 2 ormoretimes as fast if optimized (I used the standard MD5 distribution ratherthanan optimized custom one to allow others to verify the results).SHA's Awkward Block Size (Some Flamage)========================Will SHA be weakened by taking out h4 and E and reducing the number ofroundsto create a 128-bit MD algorithm? This seems a lot nicer than the currentonesince it's now a power-of-two size which fits in a lot better with mostcurrentsoftware. Removing h4 and E and reducing the total number of rounds by 16doesn't seem to weaken it any, and IMHO the decision to force SHA to fit anawkward DSS data size wasn't such a hot idea, especially if it's to be usedwith non-DSS code or if q is ever changed.SHA vs MD5==========When implementing SHA I noticed how very similar it was to MD4. The mainchanges were the addition of an the 'expand' transformation (Step B), theaddition of the previous round's output into the next round for a fasteravalance effect, and the increasing of the whole transformation to fit theDSSblock size. SHA is very much an MD4-variant rather than a redesign likeMD5.The design decisions behind MD5 were given in the MD5 document, the designfor SHA is never gone into in the SHS/SHA document (mind you it's prettyobvious what's going on - everything except how the Mysterious Constantswere chosen can be seen at a glance). Presumably some of the changes madewere to avoid the known attacks for MD4, but again no design details aregiven. Anyway, using what I had available I took the points raised in theMD5 design and compared them with what SHA did:- A fourth round has been added. SHA does this too. However in SHA the fourth round uses the samef-function as the second round (not obvious whether this is a problem or not, I'llhave to look at it a bit more).- Each step now has a unique additive constant. SHA keeps the MD4 scheme where it reuses the constants for each group of rounds (in this case for 20 rounds at a time). Actually MD4 only has the additive constants in the last two rounds, not for all 4, but theprincipal is the same - the constants are reused many times.- The function g in round 2 was changed from ( XY v XZ v YZ ) to ( XZ v Y not( Z ) ) to make g less symmetric. SHA uses the MD4 version ( XY v XZ v YZ ) (SHA calls it f2 rather thang).- Each step now adds in the result of the previous step. This promotes afaster "avalanche effect". This change has been made in SHA as well.- The order in which input words are accessed in rounds 2 and 3 is changed,to make these patterns less like each other. SHA always access the words the same way, like MD4.- The shift amounts in each round have been approximately optimized, toyield a faster "avalanche effect". The shifts in different rounds are distinct. SHA uses a constant shift amount in each round. This shift amount is relatively prime to the word size (as in MD4).If you take SHA and remove h4, E, and a few rounds, the result is(basically)MD4 with one more round, the addition of the output of step n-1 to step n(giving a faster avalanche effect), and the addition of the initial"expand"transformation. This initial transformation is important, since it spreadstheinput data bits out over an area four times as large as the original, andthenmixes in the expanded version of the data in each round. This means thatinstead of reusing the input data in each group of rounds, SHA usesdifferentpermutations of the input data in each group of rounds. This is definitelyAGood Thing.This leads to the following situation: SHA = MD4 + 'expand' transformation + extra round + better-avalanche MD5 = MD4 + improved bit-bashing + extra round + better-avalancheWhich is stronger, MD5 with its improved bit-bashing or SHA with it's'expand'transformation? (Ain't no way I'm going to answer this one :-).One point is that the only "extra" in SHA which MD5 doesn't have, namelythe'expand' transformation, can be easily added to MD5, but that the MD5improvements can't be added to SHA without redesigning the whole algorithm.Thus the paranoid types can hedge their bets by adding 'expand' to MD5,givingthem the best of both worlds (its very easy to simply change MD5 to havethe'expand' transformation - maybe this is an MD6 in the making?). Conclusion========== This positing is beginning to sound as if I'm the official net.apologistforMD5 :-). Maybe I'm being a bit harsh on SHA, but I think someone shouldpointout that it may not be the be-all and end-all of message digest algorithms. Iwelcome any email on the subject, or post your flames here....Peter.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -