Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

I have a problem with my sha256 java implementation.

The registers (a…h) are correct until j=24; j=25 … j=63 is wrong. What could be the fault and why is it always that number 24 where it stops to be correct?

Here is my output of the registers for the string “abc”:

t=0 a: 5d6aebcd b: 6a09e667 c: bb67ae85 d: 3c6ef372 e: fa2a4622 f: 510e527f g: 9b05688c h: 1f83d9ab
t=1 a: 5a6ad9ad b: 5d6aebcd c: 6a09e667 d: bb67ae85 e: 78ce7989 f: fa2a4622 g: 510e527f h: 9b05688c
t=2 a: c8c347a7 b: 5a6ad9ad c: 5d6aebcd d: 6a09e667 e: f92939eb f: 78ce7989 g: fa2a4622 h: 510e527f
t=3 a: d550f666 b: c8c347a7 c: 5a6ad9ad d: 5d6aebcd e: 24e00850 f: f92939eb g: 78ce7989 h: fa2a4622
t=4 a: 4409a6a b: d550f666 c: c8c347a7 d: 5a6ad9ad e: 43ada245 f: 24e00850 g: f92939eb h: 78ce7989
t=5 a: 2b4209f5 b: 4409a6a c: d550f666 d: c8c347a7 e: 714260ad f: 43ada245 g: 24e00850 h: f92939eb
t=6 a: e5030380 b: 2b4209f5 c: 4409a6a d: d550f666 e: 9b27a401 f: 714260ad g: 43ada245 h: 24e00850
t=7 a: 85a07b5f b: e5030380 c: 2b4209f5 d: 4409a6a e: c657a79 f: 9b27a401 g: 714260ad h: 43ada245
t=8 a: 8e04ecb9 b: 85a07b5f c: e5030380 d: 2b4209f5 e: 32ca2d8c f: c657a79 g: 9b27a401 h: 714260ad
t=9 a: 8c87346b b: 8e04ecb9 c: 85a07b5f d: e5030380 e: 1cc92596 f: 32ca2d8c g: c657a79 h: 9b27a401
t=10 a: 4798a3f4 b: 8c87346b c: 8e04ecb9 d: 85a07b5f e: 436b23e8 f: 1cc92596 g: 32ca2d8c h: c657a79
t=11 a: f71fc5a9 b: 4798a3f4 c: 8c87346b d: 8e04ecb9 e: 816fd6e9 f: 436b23e8 g: 1cc92596 h: 32ca2d8c
t=12 a: 87912990 b: f71fc5a9 c: 4798a3f4 d: 8c87346b e: 1e578218 f: 816fd6e9 g: 436b23e8 h: 1cc92596
t=13 a: d932eb16 b: 87912990 c: f71fc5a9 d: 4798a3f4 e: 745a48de f: 1e578218 g: 816fd6e9 h: 436b23e8
t=14 a: c0645fde b: d932eb16 c: 87912990 d: f71fc5a9 e: b92f20c f: 745a48de g: 1e578218 h: 816fd6e9
t=15 a: b0fa238e b: c0645fde c: d932eb16 d: 87912990 e: 7590dcd f: b92f20c g: 745a48de h: 1e578218
t=16 a: 21da9a9b b: b0fa238e c: c0645fde d: d932eb16 e: 8034229c f: 7590dcd g: b92f20c h: 745a48de
t=17 a: c2fbd9d1 b: 21da9a9b c: b0fa238e d: c0645fde e: 846ee454 f: 8034229c g: 7590dcd h: b92f20c
t=18 a: fe777bbf b: c2fbd9d1 c: 21da9a9b d: b0fa238e e: cc899961 f: 846ee454 g: 8034229c h: 7590dcd
t=19 a: e1f20c33 b: fe777bbf c: c2fbd9d1 d: 21da9a9b e: b0638179 f: cc899961 g: 846ee454 h: 8034229c
t=20 a: 9dc68b63 b: e1f20c33 c: fe777bbf d: c2fbd9d1 e: 8ada8930 f: b0638179 g: cc899961 h: 846ee454
t=21 a: c2606d6d b: 9dc68b63 c: e1f20c33 d: fe777bbf e: e1257970 f: 8ada8930 g: b0638179 h: cc899961
t=22 a: a7a3623f b: c2606d6d c: 9dc68b63 d: e1f20c33 e: 49f5114a f: e1257970 g: 8ada8930 h: b0638179
t=23 a: c5d53d8d b: a7a3623f c: c2606d6d d: 9dc68b63 e: aa47c347 f: 49f5114a g: e1257970 h: 8ada8930
t=24 a: 1c2c2838 b: c5d53d8d c: a7a3623f d: c2606d6d e: 2823ef91 f: aa47c347 g: 49f5114a h: e1257970
t=25 a: 5aa8037d b: 1c2c2838 c: c5d53d8d d: a7a3623f e: a0f83d8e f: 2823ef91 g: aa47c347 h: 49f5114a
t=26 a: 3fdee4af b: 5aa8037d c: 1c2c2838 d: c5d53d8d e: 9d88fcbc f: a0f83d8e g: 2823ef91 h: aa47c347
t=27 a: 8e447a31 b: 3fdee4af c: 5aa8037d d: 1c2c2838 e: a4509c5e f: 9d88fcbc g: a0f83d8e h: 2823ef91
t=28 a: 7551f2a2 b: 8e447a31 c: 3fdee4af d: 5aa8037d e: cebc0e07 f: a4509c5e g: 9d88fcbc h: a0f83d8e
t=29 a: 156b5e39 b: 7551f2a2 c: 8e447a31 d: 3fdee4af e: e1310f21 f: cebc0e07 g: a4509c5e h: 9d88fcbc
t=30 a: f021731c b: 156b5e39 c: 7551f2a2 d: 8e447a31 e: d44519 f: e1310f21 g: cebc0e07 h: a4509c5e
t=31 a: 97ba6ffb b: f021731c c: 156b5e39 d: 7551f2a2 e: 8f79c9e8 f: d44519 g: e1310f21 h: cebc0e07
t=32 a: b31e385c b: 97ba6ffb c: f021731c d: 156b5e39 e: 1fb6e352 f: 8f79c9e8 g: d44519 h: e1310f21
t=33 a: 694f14cd b: b31e385c c: 97ba6ffb d: f021731c e: 34bc9380 f: 1fb6e352 g: 8f79c9e8 h: d44519
t=34 a: 197dcc57 b: 694f14cd c: b31e385c d: 97ba6ffb e: 961547a8 f: 34bc9380 g: 1fb6e352 h: 8f79c9e8
t=35 a: 859a55ed b: 197dcc57 c: 694f14cd d: b31e385c e: 901ec4ed f: 961547a8 g: 34bc9380 h: 1fb6e352
t=36 a: 1776d288 b: 859a55ed c: 197dcc57 d: 694f14cd e: 19d8a658 f: 901ec4ed g: 961547a8 h: 34bc9380
t=37 a: c9088dec b: 1776d288 c: 859a55ed d: 197dcc57 e: d2019ea3 f: 19d8a658 g: 901ec4ed h: 961547a8
t=38 a: 88ddfe21 b: c9088dec c: 1776d288 d: 859a55ed e: 9e2d1c71 f: d2019ea3 g: 19d8a658 h: 901ec4ed
t=39 a: a37debd8 b: 88ddfe21 c: c9088dec d: 1776d288 e: baf7a3d9 f: 9e2d1c71 g: d2019ea3 h: 19d8a658
t=40 a: 866004bb b: a37debd8 c: 88ddfe21 d: c9088dec e: 92c3e3c7 f: baf7a3d9 g: 9e2d1c71 h: d2019ea3
t=41 a: 9b6d1109 b: 866004bb c: a37debd8 d: 88ddfe21 e: 9da0d405 f: 92c3e3c7 g: baf7a3d9 h: 9e2d1c71
t=42 a: 544f878a b: 9b6d1109 c: 866004bb d: a37debd8 e: feeccacb f: 9da0d405 g: 92c3e3c7 h: baf7a3d9
t=43 a: cf90f10 b: 544f878a c: 9b6d1109 d: 866004bb e: 82aa8a8e f: feeccacb g: 9da0d405 h: 92c3e3c7
t=44 a: 47b726e5 b: cf90f10 c: 544f878a d: 9b6d1109 e: 1227c059 f: 82aa8a8e g: feeccacb h: 9da0d405
t=45 a: 81c46372 b: 47b726e5 c: cf90f10 d: 544f878a e: 1dd60bdd f: 1227c059 g: 82aa8a8e h: feeccacb
t=46 a: c9ca63dd b: 81c46372 c: 47b726e5 d: cf90f10 e: 6dbbe6ff f: 1dd60bdd g: 1227c059 h: 82aa8a8e
t=47 a: ad2b7b53 b: c9ca63dd c: 81c46372 d: 47b726e5 e: b34a84eb f: 6dbbe6ff g: 1dd60bdd h: 1227c059
t=48 a: 87519e49 b: ad2b7b53 c: c9ca63dd d: 81c46372 e: a90368a0 f: b34a84eb g: 6dbbe6ff h: 1dd60bdd
t=49 a: 793b5cd0 b: 87519e49 c: ad2b7b53 d: c9ca63dd e: 97d2c9e6 f: a90368a0 g: b34a84eb h: 6dbbe6ff
t=50 a: 9d8558b3 b: 793b5cd0 c: 87519e49 d: ad2b7b53 e: a455ff35 f: 97d2c9e6 g: a90368a0 h: b34a84eb
t=51 a: 59b455e4 b: 9d8558b3 c: 793b5cd0 d: 87519e49 e: 322efff6 f: a455ff35 g: 97d2c9e6 h: a90368a0
t=52 a: ff32d864 b: 59b455e4 c: 9d8558b3 d: 793b5cd0 e: c4b6d000 f: 322efff6 g: a455ff35 h: 97d2c9e6
t=53 a: 6dc12651 b: ff32d864 c: 59b455e4 d: 9d8558b3 e: d1bd4dca f: c4b6d000 g: 322efff6 h: a455ff35
t=54 a: 1b21b2b1 b: 6dc12651 c: ff32d864 d: 59b455e4 e: cd9454d6 f: d1bd4dca g: c4b6d000 h: 322efff6
t=55 a: 66cc94b2 b: 1b21b2b1 c: 6dc12651 d: ff32d864 e: ebd4c658 f: cd9454d6 g: d1bd4dca h: c4b6d000
t=56 a: 23a0f90a b: 66cc94b2 c: 1b21b2b1 d: 6dc12651 e: a49f3fea f: ebd4c658 g: cd9454d6 h: d1bd4dca
t=57 a: dd7803ca b: 23a0f90a c: 66cc94b2 d: 1b21b2b1 e: 643b6d9e f: a49f3fea g: ebd4c658 h: cd9454d6
t=58 a: 7e0f1ca1 b: dd7803ca c: 23a0f90a d: 66cc94b2 e: e8407d81 f: 643b6d9e g: a49f3fea h: ebd4c658
t=59 a: 4fbd5729 b: 7e0f1ca1 c: dd7803ca d: 23a0f90a e: b0671fa9 f: e8407d81 g: 643b6d9e h: a49f3fea
t=60 a: b3b9231 b: 4fbd5729 c: 7e0f1ca1 d: dd7803ca e: afa5e674 f: b0671fa9 g: e8407d81 h: 643b6d9e
t=61 a: 65b76c7f b: b3b9231 c: 4fbd5729 d: 7e0f1ca1 e: b6e1e0ac f: afa5e674 g: b0671fa9 h: e8407d81
t=62 a: 289619b6 b: 65b76c7f c: b3b9231 d: 4fbd5729 e: efbed4ec f: b6e1e0ac g: afa5e674 h: b0671fa9
t=63 a: 8818b44e b: 289619b6 c: 65b76c7f d: b3b9231 e: 8e2cd8c1 f: efbed4ec g: b6e1e0ac h: afa5e674
share|improve this question
    
I'm not sure this is on-topic. I'm not familiar with the inner workings of SHA256 but I'm pretty sure without some code the error can't be traced. Putting code here though would most likely make the question off-topic. Whatever you've done though seems to partially work which makes this interesting. –  rath Mar 6 at 0:06

2 Answers 2

If I were to guess on what might be wrong on code I haven't seen, well, with SHA256, you stir in a per-round constant. Maybe the constant you have for round 24 (which should be 0x983e5152 if you count rounds from 0) is wrong...

share|improve this answer
    
Since only A and E changed you are correct, although the problem starts on r=25. I ran a debug path and recalculated some of the round constants based on his register output, i got k(25) = 0x34F1C66D, k(26) = 0xA4C327C8, and k(27) = 0xBF8652FF. At this point i stopped because something is seriously wrong with the round constants he is using. –  Richie Frame Mar 6 at 10:13
    
Given the changes in bits of the round constants, I would make an assumption that something is touching the memory space of the round constants during runtime, and that the constants were originally correct. In the 25th and 26th rounds, the constants that give the values in the question differ by at most 5 high bits, but after that it gets worse and expands to the entire constant. –  Richie Frame Mar 6 at 10:36

A good and productive way to debug crypto implementations is to place printlns() with the state of the registers and intermediate in the code at some places.

You then get a working open source implementation and place the printlns() in the same place.

Then feed the same input to both implementations (including randomness if it applies) and look for differences until you get to the source of the problem.

This process converges quickly as long as you place the printlns() in proper places.

My advice is to do a binary search in your code by narrowing the error between successive calls to printlns().

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.