Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Романов Никита Сергеевич
libakrypt-stb34
Commits
8c7350be
Commit
8c7350be
authored
4 months ago
by
idsilin
Browse files
Options
Download
Patches
Plain Diff
add belt-mac
parent
667004c0
dev
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/tests/test-belt-mac.c
+13
-5
examples/tests/test-belt-mac.c
source/ak_belt_mac.c
+5
-28
source/ak_belt_mac.c
with
18 additions
and
33 deletions
+18
-33
examples/tests/test-belt-mac.c
+
13
−
5
View file @
8c7350be
/* --------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------- */
/* Контрольны
й
пример
зашифрования одного блока из
стандарта (STB 34.101.31-2020) */
/* Контрольны
е
пример
ы выроботки Иммитоставки
стандарта (STB 34.101.31-2020) */
/* --------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------- */
#include
<stdio.h>
#include
<stdio.h>
#include
<libakrypt.h>
#include
<libakrypt.h>
...
@@ -35,6 +35,11 @@
...
@@ -35,6 +35,11 @@
};
};
size_t
i
;
size_t
i
;
printf
(
"
\n
TEST1: "
);
printf
(
"
\n
X: "
);
for
(
i
=
0
;
i
<
sizeof
(
data
);
++
i
)
{
printf
(
"%02X "
,
data
[
i
]);
}
printf
(
"
\n
Key: "
);
for
(
i
=
0
;
i
<
32
;
++
i
)
{
printf
(
"%02X "
,
key
[
i
]);
}
/* инициализируем библиотеку */
/* инициализируем библиотеку */
if
(
ak_libakrypt_create
(
NULL
)
!=
ak_true
)
{
if
(
ak_libakrypt_create
(
NULL
)
!=
ak_true
)
{
...
@@ -53,8 +58,7 @@
...
@@ -53,8 +58,7 @@
printf
(
"MAC:
\n
"
);
printf
(
"
\n
mac: "
);
printf
(
"
\n
mac: "
);
for
(
i
=
0
;
i
<
8
;
++
i
)
{
printf
(
"%02X "
,
mac
[
i
]);
}
for
(
i
=
0
;
i
<
8
;
++
i
)
{
printf
(
"%02X "
,
mac
[
i
]);
}
printf
(
"
\n
"
);
printf
(
"
\n
"
);
...
@@ -88,10 +92,14 @@
...
@@ -88,10 +92,14 @@
ak_uint8
mac1
[
16
]
=
{
0
};
ak_uint8
mac1
[
16
]
=
{
0
};
ak_bckey_create_belt
(
&
ctx1
);
ak_bckey_create_belt
(
&
ctx1
);
ak_bckey_set_key
(
&
ctx1
,
key1
,
32
);
ak_bckey_set_key
(
&
ctx1
,
key1
,
32
);
printf
(
"
\n
TEST2: "
);
printf
(
"
\n
X: "
);
for
(
i
=
0
;
i
<
sizeof
(
data1
);
++
i
)
{
printf
(
"%02X "
,
data1
[
i
]);
}
printf
(
"
\n
Key: "
);
for
(
i
=
0
;
i
<
32
;
++
i
)
{
printf
(
"%02X "
,
key1
[
i
]);
}
ak_belt_mac
(
&
ctx1
,
data1
,
sizeof
(
data1
),
mac1
,
8
);
ak_belt_mac
(
&
ctx1
,
data1
,
sizeof
(
data1
),
mac1
,
8
);
printf
(
"MAC:
\n
"
);
printf
(
"
\n
mac: "
);
printf
(
"
\n
mac: "
);
for
(
i
=
0
;
i
<
8
;
++
i
)
{
printf
(
"%02X "
,
mac1
[
i
]);
}
for
(
i
=
0
;
i
<
8
;
++
i
)
{
printf
(
"%02X "
,
mac1
[
i
]);
}
printf
(
"
\n
"
);
printf
(
"
\n
"
);
...
...
This diff is collapsed.
Click to expand it.
source/ak_belt_mac.c
+
5
−
28
View file @
8c7350be
...
@@ -85,7 +85,6 @@ static inline void psi(const ak_uint8 *u, size_t u_len_bytes, ak_uint8 *result)
...
@@ -85,7 +85,6 @@ static inline void psi(const ak_uint8 *u, size_t u_len_bytes, ak_uint8 *result)
int
ak_belt_mac
(
ak_bckey
bkey
,
const
ak_uint8
*
in
,
const
size_t
size
,
ak_uint8
*
out
,
const
size_t
out_size
)
int
ak_belt_mac
(
ak_bckey
bkey
,
const
ak_uint8
*
in
,
const
size_t
size
,
ak_uint8
*
out
,
const
size_t
out_size
)
{
{
int
error
=
ak_error_ok
;
int
error
=
ak_error_ok
;
printf
(
"Starting ak_belt_mac...
\n
"
);
// Проверяем корректность указателей
// Проверяем корректность указателей
if
(
bkey
==
NULL
)
{
if
(
bkey
==
NULL
)
{
...
@@ -101,7 +100,6 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
...
@@ -101,7 +100,6 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
return
ak_error_message
(
ak_error_zero_length
,
__func__
,
"using zero length of output buffer"
);
return
ak_error_message
(
ak_error_zero_length
,
__func__
,
"using zero length of output buffer"
);
}
}
printf
(
"Pointers and buffer sizes are valid.
\n
"
);
// Проверка контрольной суммы ключа
// Проверка контрольной суммы ключа
if
(
bkey
->
key
.
check_icode
(
&
bkey
->
key
)
!=
ak_true
)
{
if
(
bkey
->
key
.
check_icode
(
&
bkey
->
key
)
!=
ak_true
)
{
...
@@ -109,8 +107,6 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
...
@@ -109,8 +107,6 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
return
ak_error_message
(
ak_error_wrong_key_icode
,
__func__
,
return
ak_error_message
(
ak_error_wrong_key_icode
,
__func__
,
"incorrect integrity code of secret key value"
);
"incorrect integrity code of secret key value"
);
}
}
printf
(
"Integrity code of the key is valid.
\n
"
);
// Проверяем размер блока, для BELT это 128 бит (16 байт)
// Проверяем размер блока, для BELT это 128 бит (16 байт)
if
(
bkey
->
bsize
!=
16
)
{
if
(
bkey
->
bsize
!=
16
)
{
...
@@ -119,7 +115,6 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
...
@@ -119,7 +115,6 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
"belt-mac requires 128-bit block cipher"
);
"belt-mac requires 128-bit block cipher"
);
}
}
printf
(
"Block size is valid.
\n
"
);
// Подсчет ресурса ключа:
// Подсчет ресурса ключа:
size_t
n
=
(
size
==
0
)
?
1
:
(
(
size
+
15
)
/
16
);
size_t
n
=
(
size
==
0
)
?
1
:
(
(
size
+
15
)
/
16
);
...
@@ -130,7 +125,6 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
...
@@ -130,7 +125,6 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
}
}
bkey
->
key
.
resource
.
value
.
counter
-=
n
;
bkey
->
key
.
resource
.
value
.
counter
-=
n
;
printf
(
"Key resource updated. Remaining counter: %zd
\n
"
,
bkey
->
key
.
resource
.
value
.
counter
);
// Переменные
// Переменные
ak_uint8
s
[
16
];
// 128-битное состояние
ak_uint8
s
[
16
];
// 128-битное состояние
...
@@ -138,22 +132,17 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
...
@@ -138,22 +132,17 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
ak_uint8
temp
[
16
];
// временный буфер
ak_uint8
temp
[
16
];
// временный буфер
memset
(
s
,
0
,
16
);
memset
(
s
,
0
,
16
);
printf
(
"Initial state initialized.
\n
"
);
// r = belt-block(s, K)
// r = belt-block(s, K)
bkey
->
encrypt
(
&
bkey
->
key
,
s
,
r
);
bkey
->
encrypt
(
&
bkey
->
key
,
s
,
r
);
printf
(
"Initial encryption done. r:
\n
"
);
for
(
int
i
=
0
;
i
<
16
;
i
++
)
printf
(
"%02X "
,
r
[
i
]);
printf
(
"
\n
"
);
// Если X = ⊥ (пустое сообщение), то n = 1 и X1 = ⊥
// Если X = ⊥ (пустое сообщение), то n = 1 и X1 = ⊥
if
(
size
==
0
)
{
if
(
size
==
0
)
{
n
=
1
;
n
=
1
;
printf
(
"Message is empty. Setting n = 1.
\n
"
);
}
else
{
}
else
{
// Вычисляем n = количество 128-битных блоков (округление вверх)
// Вычисляем n = количество 128-битных блоков (округление вверх)
n
=
(
size
+
15
)
/
16
;
// 16 байт = 128 бит
n
=
(
size
+
15
)
/
16
;
// 16 байт = 128 бит
printf
(
"Message length: %zu bytes. Calculated n = %zu blocks.
\n
"
,
size
,
n
);
}
}
// Обрабатываем блоки от X1 до X_{n-1}
// Обрабатываем блоки от X1 до X_{n-1}
...
@@ -165,21 +154,14 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
...
@@ -165,21 +154,14 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
for
(
size_t
j
=
0
;
j
<
16
;
j
++
)
{
for
(
size_t
j
=
0
;
j
<
16
;
j
++
)
{
temp
[
j
]
=
s
[
j
]
^
block
[
j
];
temp
[
j
]
=
s
[
j
]
^
block
[
j
];
}
}
printf
(
"Block %zu: temp (s XOR X_i):
\n
"
,
i
+
1
);
for
(
int
j
=
0
;
j
<
16
;
j
++
)
printf
(
"%02X "
,
temp
[
j
]);
printf
(
"
\n
"
);
// s = belt-block(temp, K)
// s = belt-block(temp, K)
bkey
->
encrypt
(
&
bkey
->
key
,
temp
,
s
);
bkey
->
encrypt
(
&
bkey
->
key
,
temp
,
s
);
printf
(
"Block %zu: s after encryption:
\n
"
,
i
+
1
);
for
(
int
j
=
0
;
j
<
16
;
j
++
)
printf
(
"%02X "
,
s
[
j
]);
printf
(
"
\n
"
);
}
}
// Обработка последнего блока
// Обработка последнего блока
size_t
last_block_len
=
size
-
(
n
-
1
)
*
16
;
size_t
last_block_len
=
size
-
(
n
-
1
)
*
16
;
printf
(
"Last block length: %zu bytes.
\n
"
,
last_block_len
);
if
(
last_block_len
==
16
)
{
if
(
last_block_len
==
16
)
{
const
ak_uint8
*
block
=
in
+
(
n
-
1
)
*
16
;
const
ak_uint8
*
block
=
in
+
(
n
-
1
)
*
16
;
...
@@ -189,7 +171,6 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
...
@@ -189,7 +171,6 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
for
(
size_t
j
=
0
;
j
<
16
;
j
++
)
{
for
(
size_t
j
=
0
;
j
<
16
;
j
++
)
{
s
[
j
]
=
s
[
j
]
^
block
[
j
]
^
phi1_r
[
j
];
s
[
j
]
=
s
[
j
]
^
block
[
j
]
^
phi1_r
[
j
];
}
}
printf
(
"Last block processed (full). s:
\n
"
);
}
else
{
}
else
{
ak_uint8
psi_Xn
[
16
];
ak_uint8
psi_Xn
[
16
];
psi
(
in
+
(
n
-
1
)
*
16
,
last_block_len
,
psi_Xn
);
psi
(
in
+
(
n
-
1
)
*
16
,
last_block_len
,
psi_Xn
);
...
@@ -199,20 +180,16 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
...
@@ -199,20 +180,16 @@ int ak_belt_mac( ak_bckey bkey, const ak_uint8 *in, const size_t size, ak_uint8
for
(
size_t
j
=
0
;
j
<
16
;
j
++
)
{
for
(
size_t
j
=
0
;
j
<
16
;
j
++
)
{
s
[
j
]
=
s
[
j
]
^
psi_Xn
[
j
]
^
phi2_r
[
j
];
s
[
j
]
=
s
[
j
]
^
psi_Xn
[
j
]
^
phi2_r
[
j
];
}
}
printf
(
"Last block processed (partial). s:
\n
"
);
}
}
for
(
int
i
=
0
;
i
<
16
;
i
++
)
printf
(
"%02X "
,
s
[
i
]);
printf
(
"
\n
"
);
// Вычисляем belt-block(s, K)
// Вычисляем belt-block(s, K)
bkey
->
encrypt
(
&
bkey
->
key
,
s
,
temp
);
bkey
->
encrypt
(
&
bkey
->
key
,
s
,
temp
);
printf
(
"Final encryption done. temp:
\n
"
);
for
(
int
i
=
0
;
i
<
16
;
i
++
)
printf
(
"%02X "
,
temp
[
i
]);
printf
(
"
\n
"
);
// Копируем нужное количество байт в out
// Копируем нужное количество байт в out
memcpy
(
out
,
temp
,
(
out_size
>
bkey
->
bsize
)
?
bkey
->
bsize
:
out_size
);
memcpy
(
out
,
temp
,
(
out_size
>
bkey
->
bsize
)
?
bkey
->
bsize
:
out_size
);
printf
(
"Output written.
\n
"
);
return
ak_error_ok
;
return
ak_error_ok
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets