agievich / bee2
1
/*
2
*******************************************************************************
3
\file belt_krp.c
4
\brief STB 34.101.31 (belt): KRP (keyrep = key diversification + meshing)
5
\project bee2 [cryptographic library]
6
\author (C) Sergey Agievich [agievich@{bsu.by|gmail.com}]
7
\created 2012.12.18
8
\version 2020.03.24
9
\license This program is released under the GNU General Public License 
10
version 3. See Copyright Notices in bee2/info.h.
11
*******************************************************************************
12
*/
13

14
#include "bee2/core/blob.h"
15
#include "bee2/core/err.h"
16
#include "bee2/core/mem.h"
17
#include "bee2/core/u32.h"
18
#include "bee2/core/util.h"
19
#include "bee2/crypto/belt.h"
20
#include "belt_lcl.h"
21

22
/*
23
*******************************************************************************
24
Преобразование ключа
25
*******************************************************************************
26
*/
27

28
typedef struct {
29
	u32 key[8];			/*< форматированный первоначальный ключ */
30
	size_t len;			/*< длина первоначального ключа */
31
	u32 block[8];		/*< блок r || level || header */
32
	u32 key_new[8];		/*< форматированный преобразованный ключ */
33
	octet stack[];		/*< стек beltCompr */
34
} belt_krp_st;
35

36 1
size_t beltKRP_keep()
37
{
38 1
	return sizeof(belt_krp_st) + beltCompr_deep();
39
}
40

41 1
void beltKRPStart(void* state, const octet key[], size_t len, 
42
	const octet level[12])
43
{
44 1
	belt_krp_st* st = (belt_krp_st*)state;
45 1
	ASSERT(memIsDisjoint2(level, 12, state, beltKRP_keep()));
46
	// block <- ... || level || ...
47 1
	u32From(st->block + 1, level, 12);
48
	// сохранить ключ
49 1
	beltKeyExpand2(st->key, key, st->len = len);
50
}
51

52 1
void beltKRPStepG(octet key_[], size_t key_len, const octet header[16],
53
	void* state)
54
{
55 1
	belt_krp_st* st = (belt_krp_st*)state;
56
	// pre
57 1
	ASSERT(memIsValid(state, beltKRP_keep()));
58 1
	ASSERT(key_len == 16 || key_len == 24 || key_len == 32);
59 1
	ASSERT(key_len <= st->len);
60 1
	ASSERT(memIsDisjoint2(key_, key_len, state, beltKRP_keep()));
61 1
	ASSERT(memIsDisjoint2(header, 16, state, beltKRP_keep()));
62
	// полностью определить st->block
63 1
	u32From(st->block, beltH() + 4 * (st->len - 16) + 2 * (key_len - 16), 4);
64 1
	u32From(st->block + 4, header, 16);
65
	// применить belt-compr2
66 1
	beltBlockCopy(st->key_new, st->key);
67 1
	beltBlockCopy(st->key_new + 4, st->key + 4);
68 1
	beltCompr(st->key_new, st->block, st->stack);
69
	// выгрузить ключ
70 1
	u32To(key_, key_len, st->key_new);
71
}
72

73 1
err_t beltKRP(octet dest[], size_t m, const octet src[], size_t n,
74
	const octet level[12], const octet header[16])
75
{
76
	void* state;
77
	// проверить входные данные
78 1
	if (m > n ||
79 1
		m != 16 && m != 24 && m != 32 ||
80 1
		n != 16 && n != 24 && n != 32 ||
81 1
		!memIsValid(src, n) ||
82 1
		!memIsValid(level, 12) ||
83 1
		!memIsValid(header, 16) ||
84 1
		!memIsValid(dest, m))
85 0
		return ERR_BAD_INPUT;
86
	// создать состояние
87 1
	state = blobCreate(beltKRP_keep());
88 1
	if (state == 0)
89 0
		return ERR_OUTOFMEMORY;
90
	// преобразовать ключ
91 1
	beltKRPStart(state, src, n, level);
92 1
	beltKRPStepG(dest, m, header, state);
93
	// завершить
94 1
	blobClose(state);
95 1
	return ERR_OK;
96
}

Read our documentation on viewing source code .

Loading