mirror of
https://github.com/henrydcase/nobs.git
synced 2024-11-29 10:21:23 +00:00
go fmt
This commit is contained in:
parent
8cf7cfdc8d
commit
4b06c1b314
@ -1,246 +1,245 @@
|
|||||||
package sm3
|
package sm3
|
||||||
|
|
||||||
func rotl32(count uint32, val uint32) uint32 {
|
func rotl32(count uint32, val uint32) uint32 {
|
||||||
return (val << count) | (val >> (32 - count))
|
return (val << count) | (val >> (32 - count))
|
||||||
}
|
}
|
||||||
|
|
||||||
// compression
|
// compression
|
||||||
func p0(X uint32) uint32 {
|
func p0(X uint32) uint32 {
|
||||||
return X ^ rotl32(9, X) ^ rotl32(17, X)
|
return X ^ rotl32(9, X) ^ rotl32(17, X)
|
||||||
}
|
}
|
||||||
|
|
||||||
// expansion
|
// expansion
|
||||||
func p1(X uint32) uint32 {
|
func p1(X uint32) uint32 {
|
||||||
return X ^ rotl32(15, X) ^ rotl32(23, X)
|
return X ^ rotl32(15, X) ^ rotl32(23, X)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ff1(X uint32, Y uint32, Z uint32) uint32 {
|
func ff1(X uint32, Y uint32, Z uint32) uint32 {
|
||||||
return (X & Y) | ((X | Y) & Z)
|
return (X & Y) | ((X | Y) & Z)
|
||||||
}
|
}
|
||||||
|
|
||||||
func gg1(X uint32, Y uint32, Z uint32) uint32 {
|
func gg1(X uint32, Y uint32, Z uint32) uint32 {
|
||||||
return (X & Y) ^ ((^X) & Z) // Can be also (Z ^ (X & (Y ^ Z)))
|
return (X & Y) ^ ((^X) & Z) // Can be also (Z ^ (X & (Y ^ Z)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func r1(
|
func r1(
|
||||||
A uint32, B *uint32, C uint32, D *uint32, E uint32, F *uint32,
|
A uint32, B *uint32, C uint32, D *uint32, E uint32, F *uint32,
|
||||||
G uint32, H *uint32, TJ uint32, Wi uint32, Wj uint32) {
|
G uint32, H *uint32, TJ uint32, Wi uint32, Wj uint32) {
|
||||||
|
|
||||||
A12 := rotl32(12, A)
|
A12 := rotl32(12, A)
|
||||||
SS1 := rotl32(7, A12+E+TJ)
|
SS1 := rotl32(7, A12+E+TJ)
|
||||||
TT1 := (A ^ *B ^ C) + *D + (SS1 ^ A12) + Wj
|
TT1 := (A ^ *B ^ C) + *D + (SS1 ^ A12) + Wj
|
||||||
TT2 := (E ^ *F ^ G) + *H + SS1 + Wi
|
TT2 := (E ^ *F ^ G) + *H + SS1 + Wi
|
||||||
|
|
||||||
*B = rotl32(9, *B)
|
*B = rotl32(9, *B)
|
||||||
*D = TT1
|
*D = TT1
|
||||||
*F = rotl32(19, *F)
|
*F = rotl32(19, *F)
|
||||||
*H = p0(TT2)
|
*H = p0(TT2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func r2(
|
func r2(
|
||||||
A uint32, B *uint32, C uint32, D *uint32, E uint32, F *uint32,
|
A uint32, B *uint32, C uint32, D *uint32, E uint32, F *uint32,
|
||||||
G uint32, H *uint32, TJ uint32, Wi uint32, Wj uint32) {
|
G uint32, H *uint32, TJ uint32, Wi uint32, Wj uint32) {
|
||||||
|
|
||||||
A12 := rotl32(12, A)
|
A12 := rotl32(12, A)
|
||||||
SS1 := rotl32(7, A12+E+TJ)
|
SS1 := rotl32(7, A12+E+TJ)
|
||||||
TT1 := ff1(A, *B, C) + *D + (SS1 ^ A12) + Wj
|
TT1 := ff1(A, *B, C) + *D + (SS1 ^ A12) + Wj
|
||||||
TT2 := gg1(E, *F, G) + *H + SS1 + Wi
|
TT2 := gg1(E, *F, G) + *H + SS1 + Wi
|
||||||
|
|
||||||
*B = rotl32(9, *B)
|
*B = rotl32(9, *B)
|
||||||
*D = TT1
|
*D = TT1
|
||||||
*F = rotl32(19, *F)
|
*F = rotl32(19, *F)
|
||||||
*H = p0(TT2)
|
*H = p0(TT2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sm3e(W0 uint32, W7 uint32, W13 uint32, W3 uint32, W10 uint32) uint32 {
|
func sm3e(W0 uint32, W7 uint32, W13 uint32, W3 uint32, W10 uint32) uint32 {
|
||||||
return p1(W0 ^ W7 ^ rotl32(15, W13)) ^ rotl32(7, W3) ^ W10
|
return p1(W0^W7^rotl32(15, W13)) ^ rotl32(7, W3) ^ W10
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadBe32(x []byte) uint32 {
|
func loadBe32(x []byte) uint32 {
|
||||||
return uint32(x[3]) | (uint32(x[2]) << 8) | (uint32(x[1]) << 16) | (uint32(x[0]) << 24)
|
return uint32(x[3]) | (uint32(x[2]) << 8) | (uint32(x[1]) << 16) | (uint32(x[0]) << 24)
|
||||||
}
|
}
|
||||||
|
|
||||||
func store64Be(val []byte, x uint64) {
|
func store64Be(val []byte, x uint64) {
|
||||||
val[0] = byte(x>>56)
|
val[0] = byte(x >> 56)
|
||||||
val[1] = byte(x>>48)
|
val[1] = byte(x >> 48)
|
||||||
val[2] = byte(x>>40)
|
val[2] = byte(x >> 40)
|
||||||
val[3] = byte(x>>32)
|
val[3] = byte(x >> 32)
|
||||||
val[4] = byte(x>>24)
|
val[4] = byte(x >> 24)
|
||||||
val[5] = byte(x>>16)
|
val[5] = byte(x >> 16)
|
||||||
val[6] = byte(x>>8 )
|
val[6] = byte(x >> 8)
|
||||||
val[7] = byte(x>>0 )
|
val[7] = byte(x >> 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func store32Be(val []byte, x uint32) {
|
func store32Be(val []byte, x uint32) {
|
||||||
val[0] = byte(x>>24)
|
val[0] = byte(x >> 24)
|
||||||
val[1] = byte(x>>16)
|
val[1] = byte(x >> 16)
|
||||||
val[2] = byte(x>>8 )
|
val[2] = byte(x >> 8)
|
||||||
val[3] = byte(x>>0 )
|
val[3] = byte(x >> 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *digest) compress(input []byte, blocks int) {
|
func (d *digest) compress(input []byte, blocks int) {
|
||||||
A := d.h[0]
|
A := d.h[0]
|
||||||
B := d.h[1]
|
B := d.h[1]
|
||||||
C := d.h[2]
|
C := d.h[2]
|
||||||
D := d.h[3]
|
D := d.h[3]
|
||||||
E := d.h[4]
|
E := d.h[4]
|
||||||
F := d.h[5]
|
F := d.h[5]
|
||||||
G := d.h[6]
|
G := d.h[6]
|
||||||
H := d.h[7]
|
H := d.h[7]
|
||||||
|
|
||||||
for i := 0; i < blocks; i++ {
|
for i := 0; i < blocks; i++ {
|
||||||
next64Block := input[i*64:]
|
next64Block := input[i*64:]
|
||||||
|
|
||||||
W00 := loadBe32(next64Block[0:])
|
W00 := loadBe32(next64Block[0:])
|
||||||
W01 := loadBe32(next64Block[4:])
|
W01 := loadBe32(next64Block[4:])
|
||||||
W02 := loadBe32(next64Block[8:])
|
W02 := loadBe32(next64Block[8:])
|
||||||
W03 := loadBe32(next64Block[12:])
|
W03 := loadBe32(next64Block[12:])
|
||||||
W04 := loadBe32(next64Block[16:])
|
W04 := loadBe32(next64Block[16:])
|
||||||
W05 := loadBe32(next64Block[20:])
|
W05 := loadBe32(next64Block[20:])
|
||||||
W06 := loadBe32(next64Block[24:])
|
W06 := loadBe32(next64Block[24:])
|
||||||
W07 := loadBe32(next64Block[28:])
|
W07 := loadBe32(next64Block[28:])
|
||||||
W08 := loadBe32(next64Block[32:])
|
W08 := loadBe32(next64Block[32:])
|
||||||
W09 := loadBe32(next64Block[36:])
|
W09 := loadBe32(next64Block[36:])
|
||||||
W10 := loadBe32(next64Block[40:])
|
W10 := loadBe32(next64Block[40:])
|
||||||
W11 := loadBe32(next64Block[44:])
|
W11 := loadBe32(next64Block[44:])
|
||||||
W12 := loadBe32(next64Block[48:])
|
W12 := loadBe32(next64Block[48:])
|
||||||
W13 := loadBe32(next64Block[52:])
|
W13 := loadBe32(next64Block[52:])
|
||||||
W14 := loadBe32(next64Block[56:])
|
W14 := loadBe32(next64Block[56:])
|
||||||
W15 := loadBe32(next64Block[60:])
|
W15 := loadBe32(next64Block[60:])
|
||||||
r1(A, &B, C, &D, E, &F, G, &H, 0x79CC4519, W00, W00^W04)
|
r1(A, &B, C, &D, E, &F, G, &H, 0x79CC4519, W00, W00^W04)
|
||||||
W00 = sm3e(W00, W07, W13, W03, W10)
|
W00 = sm3e(W00, W07, W13, W03, W10)
|
||||||
r1(D, &A, B, &C, H, &E, F, &G, 0xF3988A32, W01, W01^W05)
|
r1(D, &A, B, &C, H, &E, F, &G, 0xF3988A32, W01, W01^W05)
|
||||||
W01 = sm3e(W01, W08, W14, W04, W11)
|
W01 = sm3e(W01, W08, W14, W04, W11)
|
||||||
r1(C, &D, A, &B, G, &H, E, &F, 0xE7311465, W02, W02^W06)
|
r1(C, &D, A, &B, G, &H, E, &F, 0xE7311465, W02, W02^W06)
|
||||||
W02 = sm3e(W02, W09, W15, W05, W12)
|
W02 = sm3e(W02, W09, W15, W05, W12)
|
||||||
r1(B, &C, D, &A, F, &G, H, &E, 0xCE6228CB, W03, W03^W07)
|
r1(B, &C, D, &A, F, &G, H, &E, 0xCE6228CB, W03, W03^W07)
|
||||||
W03 = sm3e(W03, W10, W00, W06, W13)
|
W03 = sm3e(W03, W10, W00, W06, W13)
|
||||||
r1(A, &B, C, &D, E, &F, G, &H, 0x9CC45197, W04, W04^W08)
|
r1(A, &B, C, &D, E, &F, G, &H, 0x9CC45197, W04, W04^W08)
|
||||||
W04 = sm3e(W04, W11, W01, W07, W14)
|
W04 = sm3e(W04, W11, W01, W07, W14)
|
||||||
r1(D, &A, B, &C, H, &E, F, &G, 0x3988A32F, W05, W05^W09)
|
r1(D, &A, B, &C, H, &E, F, &G, 0x3988A32F, W05, W05^W09)
|
||||||
W05 = sm3e(W05, W12, W02, W08, W15)
|
W05 = sm3e(W05, W12, W02, W08, W15)
|
||||||
r1(C, &D, A, &B, G, &H, E, &F, 0x7311465E, W06, W06^W10)
|
r1(C, &D, A, &B, G, &H, E, &F, 0x7311465E, W06, W06^W10)
|
||||||
W06 = sm3e(W06, W13, W03, W09, W00)
|
W06 = sm3e(W06, W13, W03, W09, W00)
|
||||||
r1(B, &C, D, &A, F, &G, H, &E, 0xE6228CBC, W07, W07^W11)
|
r1(B, &C, D, &A, F, &G, H, &E, 0xE6228CBC, W07, W07^W11)
|
||||||
W07 = sm3e(W07, W14, W04, W10, W01)
|
W07 = sm3e(W07, W14, W04, W10, W01)
|
||||||
r1(A, &B, C, &D, E, &F, G, &H, 0xCC451979, W08, W08^W12)
|
r1(A, &B, C, &D, E, &F, G, &H, 0xCC451979, W08, W08^W12)
|
||||||
W08 = sm3e(W08, W15, W05, W11, W02)
|
W08 = sm3e(W08, W15, W05, W11, W02)
|
||||||
r1(D, &A, B, &C, H, &E, F, &G, 0x988A32F3, W09, W09^W13)
|
r1(D, &A, B, &C, H, &E, F, &G, 0x988A32F3, W09, W09^W13)
|
||||||
W09 = sm3e(W09, W00, W06, W12, W03)
|
W09 = sm3e(W09, W00, W06, W12, W03)
|
||||||
r1(C, &D, A, &B, G, &H, E, &F, 0x311465E7, W10, W10^W14)
|
r1(C, &D, A, &B, G, &H, E, &F, 0x311465E7, W10, W10^W14)
|
||||||
W10 = sm3e(W10, W01, W07, W13, W04)
|
W10 = sm3e(W10, W01, W07, W13, W04)
|
||||||
r1(B, &C, D, &A, F, &G, H, &E, 0x6228CBCE, W11, W11^W15)
|
r1(B, &C, D, &A, F, &G, H, &E, 0x6228CBCE, W11, W11^W15)
|
||||||
W11 = sm3e(W11, W02, W08, W14, W05)
|
W11 = sm3e(W11, W02, W08, W14, W05)
|
||||||
r1(A, &B, C, &D, E, &F, G, &H, 0xC451979C, W12, W12^W00)
|
r1(A, &B, C, &D, E, &F, G, &H, 0xC451979C, W12, W12^W00)
|
||||||
W12 = sm3e(W12, W03, W09, W15, W06)
|
W12 = sm3e(W12, W03, W09, W15, W06)
|
||||||
r1(D, &A, B, &C, H, &E, F, &G, 0x88A32F39, W13, W13^W01)
|
r1(D, &A, B, &C, H, &E, F, &G, 0x88A32F39, W13, W13^W01)
|
||||||
W13 = sm3e(W13, W04, W10, W00, W07)
|
W13 = sm3e(W13, W04, W10, W00, W07)
|
||||||
r1(C, &D, A, &B, G, &H, E, &F, 0x11465E73, W14, W14^W02)
|
r1(C, &D, A, &B, G, &H, E, &F, 0x11465E73, W14, W14^W02)
|
||||||
W14 = sm3e(W14, W05, W11, W01, W08)
|
W14 = sm3e(W14, W05, W11, W01, W08)
|
||||||
r1(B, &C, D, &A, F, &G, H, &E, 0x228CBCE6, W15, W15^W03)
|
r1(B, &C, D, &A, F, &G, H, &E, 0x228CBCE6, W15, W15^W03)
|
||||||
W15 = sm3e(W15, W06, W12, W02, W09)
|
W15 = sm3e(W15, W06, W12, W02, W09)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0x9D8A7A87, W00, W00^W04)
|
r2(A, &B, C, &D, E, &F, G, &H, 0x9D8A7A87, W00, W00^W04)
|
||||||
W00 = sm3e(W00, W07, W13, W03, W10)
|
W00 = sm3e(W00, W07, W13, W03, W10)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0x3B14F50F, W01, W01^W05)
|
r2(D, &A, B, &C, H, &E, F, &G, 0x3B14F50F, W01, W01^W05)
|
||||||
W01 = sm3e(W01, W08, W14, W04, W11)
|
W01 = sm3e(W01, W08, W14, W04, W11)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0x7629EA1E, W02, W02^W06)
|
r2(C, &D, A, &B, G, &H, E, &F, 0x7629EA1E, W02, W02^W06)
|
||||||
W02 = sm3e(W02, W09, W15, W05, W12)
|
W02 = sm3e(W02, W09, W15, W05, W12)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0xEC53D43C, W03, W03^W07)
|
r2(B, &C, D, &A, F, &G, H, &E, 0xEC53D43C, W03, W03^W07)
|
||||||
W03 = sm3e(W03, W10, W00, W06, W13)
|
W03 = sm3e(W03, W10, W00, W06, W13)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0xD8A7A879, W04, W04^W08)
|
r2(A, &B, C, &D, E, &F, G, &H, 0xD8A7A879, W04, W04^W08)
|
||||||
W04 = sm3e(W04, W11, W01, W07, W14)
|
W04 = sm3e(W04, W11, W01, W07, W14)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0xB14F50F3, W05, W05^W09)
|
r2(D, &A, B, &C, H, &E, F, &G, 0xB14F50F3, W05, W05^W09)
|
||||||
W05 = sm3e(W05, W12, W02, W08, W15)
|
W05 = sm3e(W05, W12, W02, W08, W15)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0x629EA1E7, W06, W06^W10)
|
r2(C, &D, A, &B, G, &H, E, &F, 0x629EA1E7, W06, W06^W10)
|
||||||
W06 = sm3e(W06, W13, W03, W09, W00)
|
W06 = sm3e(W06, W13, W03, W09, W00)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0xC53D43CE, W07, W07^W11)
|
r2(B, &C, D, &A, F, &G, H, &E, 0xC53D43CE, W07, W07^W11)
|
||||||
W07 = sm3e(W07, W14, W04, W10, W01)
|
W07 = sm3e(W07, W14, W04, W10, W01)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0x8A7A879D, W08, W08^W12)
|
r2(A, &B, C, &D, E, &F, G, &H, 0x8A7A879D, W08, W08^W12)
|
||||||
W08 = sm3e(W08, W15, W05, W11, W02)
|
W08 = sm3e(W08, W15, W05, W11, W02)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0x14F50F3B, W09, W09^W13)
|
r2(D, &A, B, &C, H, &E, F, &G, 0x14F50F3B, W09, W09^W13)
|
||||||
W09 = sm3e(W09, W00, W06, W12, W03)
|
W09 = sm3e(W09, W00, W06, W12, W03)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0x29EA1E76, W10, W10^W14)
|
r2(C, &D, A, &B, G, &H, E, &F, 0x29EA1E76, W10, W10^W14)
|
||||||
W10 = sm3e(W10, W01, W07, W13, W04)
|
W10 = sm3e(W10, W01, W07, W13, W04)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0x53D43CEC, W11, W11^W15)
|
r2(B, &C, D, &A, F, &G, H, &E, 0x53D43CEC, W11, W11^W15)
|
||||||
W11 = sm3e(W11, W02, W08, W14, W05)
|
W11 = sm3e(W11, W02, W08, W14, W05)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0xA7A879D8, W12, W12^W00)
|
r2(A, &B, C, &D, E, &F, G, &H, 0xA7A879D8, W12, W12^W00)
|
||||||
W12 = sm3e(W12, W03, W09, W15, W06)
|
W12 = sm3e(W12, W03, W09, W15, W06)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0x4F50F3B1, W13, W13^W01)
|
r2(D, &A, B, &C, H, &E, F, &G, 0x4F50F3B1, W13, W13^W01)
|
||||||
W13 = sm3e(W13, W04, W10, W00, W07)
|
W13 = sm3e(W13, W04, W10, W00, W07)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0x9EA1E762, W14, W14^W02)
|
r2(C, &D, A, &B, G, &H, E, &F, 0x9EA1E762, W14, W14^W02)
|
||||||
W14 = sm3e(W14, W05, W11, W01, W08)
|
W14 = sm3e(W14, W05, W11, W01, W08)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0x3D43CEC5, W15, W15^W03)
|
r2(B, &C, D, &A, F, &G, H, &E, 0x3D43CEC5, W15, W15^W03)
|
||||||
W15 = sm3e(W15, W06, W12, W02, W09)
|
W15 = sm3e(W15, W06, W12, W02, W09)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0x7A879D8A, W00, W00^W04)
|
r2(A, &B, C, &D, E, &F, G, &H, 0x7A879D8A, W00, W00^W04)
|
||||||
W00 = sm3e(W00, W07, W13, W03, W10)
|
W00 = sm3e(W00, W07, W13, W03, W10)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0xF50F3B14, W01, W01^W05)
|
r2(D, &A, B, &C, H, &E, F, &G, 0xF50F3B14, W01, W01^W05)
|
||||||
W01 = sm3e(W01, W08, W14, W04, W11)
|
W01 = sm3e(W01, W08, W14, W04, W11)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0xEA1E7629, W02, W02^W06)
|
r2(C, &D, A, &B, G, &H, E, &F, 0xEA1E7629, W02, W02^W06)
|
||||||
W02 = sm3e(W02, W09, W15, W05, W12)
|
W02 = sm3e(W02, W09, W15, W05, W12)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0xD43CEC53, W03, W03^W07)
|
r2(B, &C, D, &A, F, &G, H, &E, 0xD43CEC53, W03, W03^W07)
|
||||||
W03 = sm3e(W03, W10, W00, W06, W13)
|
W03 = sm3e(W03, W10, W00, W06, W13)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0xA879D8A7, W04, W04^W08)
|
r2(A, &B, C, &D, E, &F, G, &H, 0xA879D8A7, W04, W04^W08)
|
||||||
W04 = sm3e(W04, W11, W01, W07, W14)
|
W04 = sm3e(W04, W11, W01, W07, W14)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0x50F3B14F, W05, W05^W09)
|
r2(D, &A, B, &C, H, &E, F, &G, 0x50F3B14F, W05, W05^W09)
|
||||||
W05 = sm3e(W05, W12, W02, W08, W15)
|
W05 = sm3e(W05, W12, W02, W08, W15)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0xA1E7629E, W06, W06^W10)
|
r2(C, &D, A, &B, G, &H, E, &F, 0xA1E7629E, W06, W06^W10)
|
||||||
W06 = sm3e(W06, W13, W03, W09, W00)
|
W06 = sm3e(W06, W13, W03, W09, W00)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0x43CEC53D, W07, W07^W11)
|
r2(B, &C, D, &A, F, &G, H, &E, 0x43CEC53D, W07, W07^W11)
|
||||||
W07 = sm3e(W07, W14, W04, W10, W01)
|
W07 = sm3e(W07, W14, W04, W10, W01)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0x879D8A7A, W08, W08^W12)
|
r2(A, &B, C, &D, E, &F, G, &H, 0x879D8A7A, W08, W08^W12)
|
||||||
W08 = sm3e(W08, W15, W05, W11, W02)
|
W08 = sm3e(W08, W15, W05, W11, W02)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0x0F3B14F5, W09, W09^W13)
|
r2(D, &A, B, &C, H, &E, F, &G, 0x0F3B14F5, W09, W09^W13)
|
||||||
W09 = sm3e(W09, W00, W06, W12, W03)
|
W09 = sm3e(W09, W00, W06, W12, W03)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0x1E7629EA, W10, W10^W14)
|
r2(C, &D, A, &B, G, &H, E, &F, 0x1E7629EA, W10, W10^W14)
|
||||||
W10 = sm3e(W10, W01, W07, W13, W04)
|
W10 = sm3e(W10, W01, W07, W13, W04)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0x3CEC53D4, W11, W11^W15)
|
r2(B, &C, D, &A, F, &G, H, &E, 0x3CEC53D4, W11, W11^W15)
|
||||||
W11 = sm3e(W11, W02, W08, W14, W05)
|
W11 = sm3e(W11, W02, W08, W14, W05)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0x79D8A7A8, W12, W12^W00)
|
r2(A, &B, C, &D, E, &F, G, &H, 0x79D8A7A8, W12, W12^W00)
|
||||||
W12 = sm3e(W12, W03, W09, W15, W06)
|
W12 = sm3e(W12, W03, W09, W15, W06)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0xF3B14F50, W13, W13^W01)
|
r2(D, &A, B, &C, H, &E, F, &G, 0xF3B14F50, W13, W13^W01)
|
||||||
W13 = sm3e(W13, W04, W10, W00, W07)
|
W13 = sm3e(W13, W04, W10, W00, W07)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0xE7629EA1, W14, W14^W02)
|
r2(C, &D, A, &B, G, &H, E, &F, 0xE7629EA1, W14, W14^W02)
|
||||||
W14 = sm3e(W14, W05, W11, W01, W08)
|
W14 = sm3e(W14, W05, W11, W01, W08)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0xCEC53D43, W15, W15^W03)
|
r2(B, &C, D, &A, F, &G, H, &E, 0xCEC53D43, W15, W15^W03)
|
||||||
W15 = sm3e(W15, W06, W12, W02, W09)
|
W15 = sm3e(W15, W06, W12, W02, W09)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0x9D8A7A87, W00, W00^W04)
|
r2(A, &B, C, &D, E, &F, G, &H, 0x9D8A7A87, W00, W00^W04)
|
||||||
W00 = sm3e(W00, W07, W13, W03, W10)
|
W00 = sm3e(W00, W07, W13, W03, W10)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0x3B14F50F, W01, W01^W05)
|
r2(D, &A, B, &C, H, &E, F, &G, 0x3B14F50F, W01, W01^W05)
|
||||||
W01 = sm3e(W01, W08, W14, W04, W11)
|
W01 = sm3e(W01, W08, W14, W04, W11)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0x7629EA1E, W02, W02^W06)
|
r2(C, &D, A, &B, G, &H, E, &F, 0x7629EA1E, W02, W02^W06)
|
||||||
W02 = sm3e(W02, W09, W15, W05, W12)
|
W02 = sm3e(W02, W09, W15, W05, W12)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0xEC53D43C, W03, W03^W07)
|
r2(B, &C, D, &A, F, &G, H, &E, 0xEC53D43C, W03, W03^W07)
|
||||||
W03 = sm3e(W03, W10, W00, W06, W13)
|
W03 = sm3e(W03, W10, W00, W06, W13)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0xD8A7A879, W04, W04^W08)
|
r2(A, &B, C, &D, E, &F, G, &H, 0xD8A7A879, W04, W04^W08)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0xB14F50F3, W05, W05^W09)
|
r2(D, &A, B, &C, H, &E, F, &G, 0xB14F50F3, W05, W05^W09)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0x629EA1E7, W06, W06^W10)
|
r2(C, &D, A, &B, G, &H, E, &F, 0x629EA1E7, W06, W06^W10)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0xC53D43CE, W07, W07^W11)
|
r2(B, &C, D, &A, F, &G, H, &E, 0xC53D43CE, W07, W07^W11)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0x8A7A879D, W08, W08^W12)
|
r2(A, &B, C, &D, E, &F, G, &H, 0x8A7A879D, W08, W08^W12)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0x14F50F3B, W09, W09^W13)
|
r2(D, &A, B, &C, H, &E, F, &G, 0x14F50F3B, W09, W09^W13)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0x29EA1E76, W10, W10^W14)
|
r2(C, &D, A, &B, G, &H, E, &F, 0x29EA1E76, W10, W10^W14)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0x53D43CEC, W11, W11^W15)
|
r2(B, &C, D, &A, F, &G, H, &E, 0x53D43CEC, W11, W11^W15)
|
||||||
r2(A, &B, C, &D, E, &F, G, &H, 0xA7A879D8, W12, W12^W00)
|
r2(A, &B, C, &D, E, &F, G, &H, 0xA7A879D8, W12, W12^W00)
|
||||||
r2(D, &A, B, &C, H, &E, F, &G, 0x4F50F3B1, W13, W13^W01)
|
r2(D, &A, B, &C, H, &E, F, &G, 0x4F50F3B1, W13, W13^W01)
|
||||||
r2(C, &D, A, &B, G, &H, E, &F, 0x9EA1E762, W14, W14^W02)
|
r2(C, &D, A, &B, G, &H, E, &F, 0x9EA1E762, W14, W14^W02)
|
||||||
r2(B, &C, D, &A, F, &G, H, &E, 0x3D43CEC5, W15, W15^W03)
|
r2(B, &C, D, &A, F, &G, H, &E, 0x3D43CEC5, W15, W15^W03)
|
||||||
|
|
||||||
d.h[0] ^= A
|
d.h[0] ^= A
|
||||||
d.h[1] ^= B
|
d.h[1] ^= B
|
||||||
d.h[2] ^= C
|
d.h[2] ^= C
|
||||||
d.h[3] ^= D
|
d.h[3] ^= D
|
||||||
d.h[4] ^= E
|
d.h[4] ^= E
|
||||||
d.h[5] ^= F
|
d.h[5] ^= F
|
||||||
d.h[6] ^= G
|
d.h[6] ^= G
|
||||||
d.h[7] ^= H
|
d.h[7] ^= H
|
||||||
|
|
||||||
A = d.h[0]
|
A = d.h[0]
|
||||||
B = d.h[1]
|
B = d.h[1]
|
||||||
C = d.h[2]
|
C = d.h[2]
|
||||||
D = d.h[3]
|
D = d.h[3]
|
||||||
E = d.h[4]
|
E = d.h[4]
|
||||||
F = d.h[5]
|
F = d.h[5]
|
||||||
G = d.h[6]
|
G = d.h[6]
|
||||||
H = d.h[7]
|
H = d.h[7]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
120
hash/sm3/sm3.go
120
hash/sm3/sm3.go
@ -4,18 +4,18 @@
|
|||||||
package sm3
|
package sm3
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"hash"
|
"hash"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
init0 = 0x7380166F
|
init0 = 0x7380166F
|
||||||
init1 = 0x4914B2B9
|
init1 = 0x4914B2B9
|
||||||
init2 = 0x172442D7
|
init2 = 0x172442D7
|
||||||
init3 = 0xDA8A0600
|
init3 = 0xDA8A0600
|
||||||
init4 = 0xA96F30BC
|
init4 = 0xA96F30BC
|
||||||
init5 = 0x163138AA
|
init5 = 0x163138AA
|
||||||
init6 = 0xE38DEE4D
|
init6 = 0xE38DEE4D
|
||||||
init7 = 0xB0FB0E4E
|
init7 = 0xB0FB0E4E
|
||||||
)
|
)
|
||||||
|
|
||||||
// The size of a SM-3 checksum in bytes.
|
// The size of a SM-3 checksum in bytes.
|
||||||
@ -26,15 +26,15 @@ const BlockSize int = 64
|
|||||||
|
|
||||||
// digest represents the partial evaluation of a checksum.
|
// digest represents the partial evaluation of a checksum.
|
||||||
type digest struct {
|
type digest struct {
|
||||||
h [8]uint32
|
h [8]uint32
|
||||||
len uint64
|
len uint64
|
||||||
b [BlockSize]byte
|
b [BlockSize]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() hash.Hash {
|
func New() hash.Hash {
|
||||||
d := new(digest)
|
d := new(digest)
|
||||||
d.Reset()
|
d.Reset()
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *digest) BlockSize() int { return BlockSize }
|
func (d *digest) BlockSize() int { return BlockSize }
|
||||||
@ -44,63 +44,67 @@ func (d *digest) Size() int { return Size }
|
|||||||
func (d *digest) Init() { d.Reset() }
|
func (d *digest) Init() { d.Reset() }
|
||||||
|
|
||||||
func (d *digest) Reset() {
|
func (d *digest) Reset() {
|
||||||
d.h[0] = init0
|
d.h[0] = init0
|
||||||
d.h[1] = init1
|
d.h[1] = init1
|
||||||
d.h[2] = init2
|
d.h[2] = init2
|
||||||
d.h[3] = init3
|
d.h[3] = init3
|
||||||
d.h[4] = init4
|
d.h[4] = init4
|
||||||
d.h[5] = init5
|
d.h[5] = init5
|
||||||
d.h[6] = init6
|
d.h[6] = init6
|
||||||
d.h[7] = init7
|
d.h[7] = init7
|
||||||
d.len = 0
|
d.len = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *digest) Write(input []byte) (nn int, err error) {
|
func (d *digest) Write(input []byte) (nn int, err error) {
|
||||||
|
|
||||||
// current possition in the buffer
|
// current possition in the buffer
|
||||||
idx := int(d.len & uint64((d.BlockSize() - 1)))
|
idx := int(d.len & uint64((d.BlockSize() - 1)))
|
||||||
d.len += uint64(len(input))
|
d.len += uint64(len(input))
|
||||||
|
|
||||||
if len(input) + idx < d.BlockSize() {
|
if len(input)+idx < d.BlockSize() {
|
||||||
copy(d.b[idx:], input)
|
copy(d.b[idx:], input)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c := d.BlockSize() - idx
|
c := d.BlockSize() - idx
|
||||||
copy(d.b[idx:], input[:c])
|
copy(d.b[idx:], input[:c])
|
||||||
d.compress(d.b[:], 1)
|
d.compress(d.b[:], 1)
|
||||||
|
|
||||||
input = input[c:]
|
input = input[c:]
|
||||||
nblocks := int(len(input) / d.BlockSize())
|
nblocks := int(len(input) / d.BlockSize())
|
||||||
d.compress(input[:], nblocks)
|
d.compress(input[:], nblocks)
|
||||||
|
|
||||||
// this eventually could be done in d.compress
|
// this eventually could be done in d.compress
|
||||||
copy(d.b[:], input[nblocks*d.BlockSize():])
|
copy(d.b[:], input[nblocks*d.BlockSize():])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *digest) Sum(in []byte) []byte {
|
func (d *digest) Sum(in []byte) []byte {
|
||||||
var output [32]byte
|
var output [32]byte
|
||||||
|
|
||||||
// Copy context so that caller can keep updating
|
// Copy context so that caller can keep updating
|
||||||
dc := *d
|
dc := *d
|
||||||
|
|
||||||
dc.Write(in)
|
dc.Write(in)
|
||||||
|
|
||||||
idx := int(dc.len & uint64(dc.BlockSize() - 1))
|
idx := int(dc.len & uint64(dc.BlockSize()-1))
|
||||||
for i:=idx+1; i<len(dc.b); i++ {dc.b[i] = 0}
|
for i := idx + 1; i < len(dc.b); i++ {
|
||||||
dc.b[idx] = 0x80
|
dc.b[i] = 0
|
||||||
if idx >= 56 {
|
}
|
||||||
dc.compress(dc.b[:], 1)
|
dc.b[idx] = 0x80
|
||||||
for i:=range (dc.b) {dc.b[i] = 0}
|
if idx >= 56 {
|
||||||
}
|
dc.compress(dc.b[:], 1)
|
||||||
|
for i := range dc.b {
|
||||||
|
dc.b[i] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// add total bits
|
// add total bits
|
||||||
store64Be(dc.b[56:], dc.len * 8)
|
store64Be(dc.b[56:], dc.len*8)
|
||||||
|
|
||||||
dc.compress(dc.b[:], 1)
|
dc.compress(dc.b[:], 1)
|
||||||
for i:=0; i<Size/4; i++ {
|
for i := 0; i < Size/4; i++ {
|
||||||
store32Be(output[4*i:], dc.h[i])
|
store32Be(output[4*i:], dc.h[i])
|
||||||
}
|
}
|
||||||
return output[:]
|
return output[:]
|
||||||
}
|
}
|
||||||
|
1084
hash/sm3/sm3_test.go
1084
hash/sm3/sm3_test.go
File diff suppressed because it is too large
Load Diff
53
rand/ctr_drbg.go
Normal file
53
rand/ctr_drbg.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import rand
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Constants below correspond to AES-256, which is currently
|
||||||
|
// the only block cipher supported.
|
||||||
|
const {
|
||||||
|
Blocklen = 16
|
||||||
|
Keylen = 32
|
||||||
|
}
|
||||||
|
|
||||||
|
type CtrDrbg struct {
|
||||||
|
v uint
|
||||||
|
keylen uint // OZAPTF: is it needed?
|
||||||
|
counter uint
|
||||||
|
strength uint
|
||||||
|
resistance bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CtrDrbg) update(data []byte) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *CtrDrbg {
|
||||||
|
c = new(CtrDrbg)
|
||||||
|
c.key = make([]byte, 0, Keylen)
|
||||||
|
c.v = make([]byte, 0, Blocklen)
|
||||||
|
// Security strength for AES-256 as per SP800-57, 5.6.1
|
||||||
|
c.strength = 256
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CtrDrbg) Init(entropy []byte, personalization []byte, strength uint) bool {
|
||||||
|
|
||||||
|
if len(entropy) < (c.strength/8) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// does enropyt needs to have some minimal length?
|
||||||
|
seed := make([]byte, 0, c.strength / 8)
|
||||||
|
|
||||||
|
c.update(seed)
|
||||||
|
c.counter = 1
|
||||||
|
return c
|
||||||
|
|
||||||
|
}
|
||||||
|
func (c *CtrDrbg) Update() {}
|
||||||
|
func (c *CtrDrbg) Read(b []byte) (n int, err error) {
|
||||||
|
|
||||||
|
}
|
21
rand/ctr_drbg_test.go
Normal file
21
rand/ctr_drbg_test.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import rand
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNominal(t* testing.T) {
|
||||||
|
block, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stream := cipher.NewCTR(block, iv)
|
||||||
|
stream.XORKeyStream(pt, ct)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user