Main step of GOST 28147-89: Difference between revisions

Replaced the code by something which actually works. Replaced Sboxes by those used in Go implementation. Avoided problems with endianness. Added an example.
m (→‎{{header|Phix}}: no_empty now defaults to true)
(Replaced the code by something which actually works. Replaced Sboxes by those used in Go implementation. Avoided problems with endianness. Added an example.)
Line 549:
 
=={{header|Nim}}==
Algorithm inspired from C, Go, etc.
{{trans|C}}
The Sboxes are computed at compile time.
<lang nim>var
<lang nim>import sequtils, strutils
k8 = [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7]
k7 = [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10]
k6 = [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8]
k5 = [ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15]
k4 = [ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9]
k3 = [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11]
k2 = [ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1]
k1 = [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7]
 
const
k87, k65, k43, k21 = newSeq[int64](256)
k3K1 = [12,byte 14, 10, 15, 9, 2, 613, 8, 0, 1314, 36, 11, 4 1, 1412, 7, 15, 5, 11 3]
k6K2 = [10byte 14, 011, 94, 1412, 6, 313, 15, 510, 12, 13 3, 12 8, 71, 11 0, 47, 25, 89]
k7K3 = [15,byte 15, 8, 14 1, 613, 1110, 3, 4, 92, 14, 715, 212, 13 7, 12 6, 0, 59, 1011]
k5K4 = [byte 7, 13, 1410, 31, 0, 68, 9, 1015, 114, 24, 86, 512, 11, 12 2, 45, 15 3]
k8K5 = [14,byte 46, 1312, 7, 1, 25, 15, 1113, 8, 34, 10, 69, 1214, 50, 93, 011, 72]
k2K6 = [byte 4, 11, 210, 14 0, 15 7, 02, 81, 13, 3, 12 6, 98, 75, 59, 1012, 615, 114]
k4K7 = [byte 213, 1211, 4, 1, 73, 1015, 11 5, 69, 80, 10, 514, 37, 15, 136, 08, 14 2, 912]
k1K8 = [13,byte 21, 15, 813, 40, 65, 15 7, 1110, 14, 10 9, 92, 3, 14, 56, 011, 12 8, 712]
 
 
proc kboxInit =
proc kboxInit: tuple[k87, k65, k43, k21: array[256, byte]] {.compileTime.} =
for i in 0 .. 255:
result.k87[i] = k8K8[i shr 4] shl 4 or k7K7[i and 15]
result.k65[i] = k6K6[i shr 4] shl 4 or k5K5[i and 15]
result.k43[i] = k4K4[i shr 4] shl 4 or k3K3[i and 15]
result.k21[i] = k2K2[i shr 4] shl 4 or k1K1[i and 15]
 
const (K87, K65, K43, K21) = kboxInit()
 
template rol(x: uint32; n: typed): uint32 =
x shl 11n or x shr (32 - 11n)</lang>
 
proc f(x: uint32): int64uint32 =
let x = k87K87[x shr 24 and 255].uint32 shl 24 or k65K65[x shr 16 and 255].uint32 shl 16 or
k43K43[x shr 8 and 255].uint32 shl 8 or k21K21[x and 255].uint32
result = x.rol(11)
 
proc mainStep(input: array[8, byte]; key: array[4, byte]): array[8, byte] =
let input32 = cast[array[2, uint32]](input)
let key = cast[uint32](key)
let val = f(key + input32[0]) xor input32[1]
result[0..3] = cast[array[4, byte]](val)
result[4..7] = input[0..3]
 
when isMainModule:
const
Input = [byte 0x21, 0x04, 0x3B, 0x04, 0x30, 0x04, 0x32, 0x04]
Key = [byte 0xF9, 0x04, 0xC1, 0xE2]
 
let output = mainStep(Input, Key)
echo mapIt(output, it.toHex).join(" ")</lang>
 
{{out}}
proc f(x): int64 =
<pre>1F 88 CF 07 21 04 3B 04</pre>
let x = k87[x shr 24 and 255] shl 24 or k65[x shr 16 and 255] shl 16 or
k43[x shr 8 and 255] shl 8 or k21[x and 255]
x shl 11 or x shr (32 - 11)</lang>
 
=={{header|Perl}}==
Anonymous user