| 1 | // Package sm3 implements the SM-3 hash algorithm as defined in "SM3 Hash |
| 2 | // function draft-shen-sm3-hash-01" draft |
| 3 | // |
| 4 | package sm3scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/ |
| 5 | |
| 6 | import ( |
| 7 | "hashscip-go gomod github.com/golang/go/src go1.12 hash/" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | init0scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init0. = 0x7380166F |
| 12 | init1scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init1. = 0x4914B2B9 |
| 13 | init2scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init2. = 0x172442D7 |
| 14 | init3scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init3. = 0xDA8A0600 |
| 15 | init4scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init4. = 0xA96F30BC |
| 16 | init5scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init5. = 0x163138AA |
| 17 | init6scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init6. = 0xE38DEE4D |
| 18 | init7scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init7. = 0xB0FB0E4E |
| 19 | ) |
| 20 | |
| 21 | // The size of a SM-3 checksum in bytes. |
| 22 | const Sizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/Size. = 32 |
| 23 | |
| 24 | // The blocksize of SM-3 in bytes. |
| 25 | const BlockSizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/BlockSize. int = 64 |
| 26 | |
| 27 | // digest represents the partial evaluation of a checksum. |
| 28 | type digestscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest# struct { |
| 29 | hscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#h. [8]uint32 |
| 30 | lenscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#len. uint64 |
| 31 | bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b. [BlockSizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/BlockSize.]byte |
| 32 | } |
| 33 | |
| 34 | func Newscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/New().() hashscip-go gomod github.com/golang/go/src go1.12 hash/.Hashscip-go gomod github.com/golang/go/src go1.12 hash/Hash# { |
| 35 | d := new(digestscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#) |
| 36 | d.Resetscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#Reset().() |
| 37 | return d |
| 38 | } |
| 39 | |
| 40 | func (d *digestscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#) BlockSizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#BlockSize().() int { return BlockSizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/BlockSize. } |
| 41 | |
| 42 | func (d *digestscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#) Sizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#Size().() int { return Sizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/Size. } |
| 43 | |
| 44 | func (d *digestscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#) Initscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#Init().() { d.Resetscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#Reset().() } |
| 45 | |
| 46 | func (d *digestscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#) Resetscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#Reset().() { |
| 47 | d.hscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#h.[0] = init0scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init0. |
| 48 | d.hscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#h.[1] = init1scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init1. |
| 49 | d.hscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#h.[2] = init2scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init2. |
| 50 | d.hscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#h.[3] = init3scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init3. |
| 51 | d.hscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#h.[4] = init4scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init4. |
| 52 | d.hscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#h.[5] = init5scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init5. |
| 53 | d.hscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#h.[6] = init6scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init6. |
| 54 | d.hscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#h.[7] = init7scip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/init7. |
| 55 | d.lenscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#len. = 0 |
| 56 | } |
| 57 | |
| 58 | func (d *digestscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#) Writescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#Write().(input []byte) (nn int, err error) { |
| 59 | |
| 60 | // current possition in the buffer |
| 61 | idx := int(d.lenscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#len. & uint64((d.BlockSizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#BlockSize().() - 1))) |
| 62 | d.lenscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#len. += uint64(len(input)) |
| 63 | |
| 64 | if len(input)+idx < d.BlockSizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#BlockSize().() { |
| 65 | copy(d.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b.[idx:], input) |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | c := d.BlockSizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#BlockSize().() - idx |
| 70 | copy(d.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b.[idx:], input[:c]) |
| 71 | d.compressscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#compress().(d.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b.[:], 1) |
| 72 | |
| 73 | input = input[c:] |
| 74 | nblocks := int(len(input) / d.BlockSizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#BlockSize().()) |
| 75 | d.compressscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#compress().(input[:], nblocks) |
| 76 | |
| 77 | // this eventually could be done in d.compress |
| 78 | copy(d.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b.[:], input[nblocks*d.BlockSizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#BlockSize().():]) |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | func (d *digestscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#) Sumscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#Sum().(in []byte) []byte { |
| 83 | var output [32]byte |
| 84 | |
| 85 | // Copy context so that caller can keep updating |
| 86 | dc := *d |
| 87 | |
| 88 | dc.Writescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#Write().(in) |
| 89 | |
| 90 | idx := int(dc.lenscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#len. & uint64(dc.BlockSizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#BlockSize().()-1)) |
| 91 | for i := idx + 1; i < len(dc.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b.); i++ { |
| 92 | dc.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b.[i] = 0 |
| 93 | } |
| 94 | dc.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b.[idx] = 0x80 |
| 95 | if idx >= 56 { |
| 96 | dc.compressscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#compress().(dc.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b.[:], 1) |
| 97 | for i := range dc.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b. { |
| 98 | dc.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b.[i] = 0 |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // add total bits |
| 103 | store64Bescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/store64Be().(dc.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b.[56:], dc.lenscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#len.*8) |
| 104 | |
| 105 | dc.compressscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#compress().(dc.bscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#b.[:], 1) |
| 106 | for i := 0; i < Sizescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/Size./4; i++ { |
| 107 | store32Bescip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/store32Be().(output[4*i:], dc.hscip-go gomod github.com/henrydcase/nobs . `github.com/henrydcase/nobs/hash/sm3`/digest#h.[i]) |
| 108 | } |
| 109 | return output[:] |
| 110 | } |