Main step of GOST 28147-89: Difference between revisions

m
(New post.)
 
(7 intermediate revisions by 4 users not shown)
Line 88:
return x<<11 | x>>(32-11);
}</syntaxhighlight>
 
=={{header|C#}}==
{{trans|Go}}
<syntaxhighlight lang="C#">
using System;
 
class Gost
{
private byte[,] sBox = new byte[8, 16];
private byte[] k87 = new byte[256];
private byte[] k65 = new byte[256];
private byte[] k43 = new byte[256];
private byte[] k21 = new byte[256];
private byte[] enc = new byte[8];
 
public Gost(byte[,] s)
{
sBox = s;
for (int i = 0; i < 256; i++)
{
k87[i] = (byte)((sBox[7, i >> 4] << 4) | sBox[6, i & 15]);
k65[i] = (byte)((sBox[5, i >> 4] << 4) | sBox[4, i & 15]);
k43[i] = (byte)((sBox[3, i >> 4] << 4) | sBox[2, i & 15]);
k21[i] = (byte)((sBox[1, i >> 4] << 4) | sBox[0, i & 15]);
}
}
 
private uint F(uint x)
{
x = (uint)(k87[x >> 24 & 255] << 24) | (uint)(k65[x >> 16 & 255] << 16) |
(uint)(k43[x >> 8 & 255] << 8) | (uint)(k21[x & 255]);
return x << 11 | x >> (32 - 11);
}
 
private static uint U32(byte[] b)
{
return (uint)(b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24);
}
 
private static void B4(uint u, byte[] b)
{
b[0] = (byte)u;
b[1] = (byte)(u >> 8);
b[2] = (byte)(u >> 16);
b[3] = (byte)(u >> 24);
}
 
public void MainStep(byte[] input, byte[] key)
{
uint key32 = U32(key);
uint input1 = U32(input, 0);
uint input2 = U32(input, 4);
B4(F(key32 + input1) ^ input2, enc, 0);
Array.Copy(input, 0, enc, 4, 4);
}
 
public byte[] Enc => enc;
 
private static uint U32(byte[] b, int index)
{
return (uint)(b[index] | b[index + 1] << 8 | b[index + 2] << 16 | b[index + 3] << 24);
}
 
private static void B4(uint u, byte[] b, int index)
{
b[index] = (byte)u;
b[index + 1] = (byte)(u >> 8);
b[index + 2] = (byte)(u >> 16);
b[index + 3] = (byte)(u >> 24);
}
}
 
class Program
{
static void Main(string[] args)
{
byte[,] cbrf = {
{4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3},
{14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9},
{5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11},
{7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3},
{6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2},
{4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14},
{13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12},
{1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12},
};
 
byte[] input = { 0x21, 0x04, 0x3B, 0x04, 0x30, 0x04, 0x32, 0x04 };
byte[] key = { 0xF9, 0x04, 0xC1, 0xE2 };
 
Gost g = new Gost(cbrf);
g.MainStep(input, key);
 
foreach (var b in g.Enc)
{
Console.Write("[{0:x2}]", b);
}
Console.WriteLine();
}
}
</syntaxhighlight>
{{out}}
<pre>
[1f][88][cf][07][21][04][3b][04]
 
</pre>
 
=={{header|C++}}==
Line 634 ⟶ 740:
for (let сч = 0; сч < 4; сч++) {
const яч = (S >>> (сч << 3)) & 0xFF;
нов_S += ТЗ[сч * 2][яч & 0x0F] + (ТЗ[сч * 2 + 1][яч >>> 4] << 4) << (сч << 3);
нов_S +=
ТЗ[сч * 2][яч & 0x0F]
+ (ТЗ[сч * 2 + 1][яч >>> 4] << 4)
<< (сч << 3);
}
нов_S = (нов_S << 11) + (нов_S >>> 21) & 0xFFFFFFFF ^ N[1];
нов_S =
(нов_S << 11)
+ (нов_S >>> 21)
& 0xFFFFFFFF
^ N[1];
N[1] = N[0]; N[0] = нов_S;
return N;
Line 681 ⟶ 780:
end
 
bytes2int(arr) = arr[1] + reinterpret(UInt32(arr[2]), << 8) + (UInt32(arr[3]) << 16) + (UInt32(arr[4begin])) << 24
int2bytes(x) = [UInt8reinterpret(x&0xff), UInt8((x&0xff00)>>8), UInt8(([x&0xff0000])>>16), UInt8(x>>24)]
 
function mainstep(inputbytes, keybytes)
Line 1,178 ⟶ 1,277:
<pre>
7CF881F 43B0421
</pre>
 
=={{header|RPL}}==
{{ trans|Nim}}
« { { 4 10 9 2 13 8 0 14 6 11 1 12 7 15 5 3 }
{ 14 11 4 12 6 13 15 10 2 3 8 1 0 7 5 9 }
{ 5 8 1 13 10 3 4 2 14 15 12 7 6 0 9 11 }
{ 7 13 10 1 0 8 9 15 14 4 6 12 11 2 5 3 }
{ 6 12 7 1 5 15 13 8 4 10 9 14 0 3 11 2 }
{ 4 11 10 0 7 2 1 13 3 6 8 5 9 12 15 14 }
{ 13 11 4 1 3 15 5 9 0 10 14 7 6 8 2 12 }
{ 1 15 13 0 5 7 10 4 9 2 3 14 6 11 8 12 } }
« R→B » DOLIST 'K' STO
{ K21 K43 K65 K87 } 1 « { } SWAP STO » DOLIST
0 255 '''FOR''' j
1 4 '''FOR''' n
'K' n 2 * GET j 16 / IP 1 + GET 16 *
'K' n 2 * 1 - GET j 16 MOD 1 + GET OR
'''NEXT'''
4 →LIST { K21 K43 K65 K87 } SWAP STO+
'''NEXT'''
» '<span style="color:blue">KBOXINIT</span>' STO
« { }
'''WHILE''' #0h ≠ '''REPEAT'''
OVER #255d AND SWAP OVER + SWAP SRB
'''END''' SWAP DROP
» '<span style="color:blue">B→LIST</span>' STO
« « SWAP SLB OR » → input key cast
« key REVLIST cast STREAM <span style="color:grey">@ convert key into a little-endian integer</span>
input 1 4 SUB REVLIST cast STREAM + <span style="color:blue">B→LIST</span> <span style="color:grey">@ same for input part 1, then add and back to list format</span>
{ K21 K43 K65 K87 } SWAP B→R 1 ADD GET <span style="color:grey">@ replace bytes according to the table</span>
REVLIST cast STREAM <span style="color:grey">@ back to little-endian format</span>
RLB RL RL RL <span style="color:blue">B→LIST</span> <span style="color:grey">@ roll 11 bits, back to list format</span>
input 5 8 SUB XOR <span style="color:grey">@ add part 2</span>
input 1 4 SUB + <span style="color:grey">@ append part 1</span>
» » '<span style="color:blue">MAINSTEP</span>' STO
« 32 STWS HEX
<span style="color:blue">KBOXINIT</span>
{ #21h #4h #3Bh #4h #30h #4h #32h #4h } { #F9h #4h #C1h #E2h } <span style="color:blue">MAINSTEP</span>
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { # 1Fh # 88h # CFh # 7h # 21h # 4h # 3Bh # 4h }
</pre>
 
Line 1,419 ⟶ 1,564:
{{libheader|Wren-fmt}}
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
class GOST {
Line 1,477 ⟶ 1,622:
var g = GOST.new(cbrf)
g.mainStep(input, key)
for (b in g.enc) SystemFmt.write("[%(Fmt.xz(2$02x]", b))]")
System.print()</syntaxhighlight>