1
/*
2
*******************************************************************************
3
\file belt_ecb.c
4
\brief STB 34.101.31 (belt): ECB encryption
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/util.h"
18
#include "bee2/crypto/belt.h"
19

20
/*
21
*******************************************************************************
22
Шифрование в режиме ECB
23
*******************************************************************************
24
*/
25
typedef struct
26
{
27
	u32 key[8];		/*< форматированный ключ */
28
} belt_ecb_st;
29

30 1
size_t beltECB_keep()
31
{
32 1
	return sizeof(belt_ecb_st);
33
}
34

35 1
void beltECBStart(void* state, const octet key[], size_t len)
36
{
37 1
	belt_ecb_st* st = (belt_ecb_st*)state;
38 1
	ASSERT(memIsValid(state, beltECB_keep()));
39 1
	beltKeyExpand2(st->key, key, len);
40
}
41

42 1
void beltECBStepE(void* buf, size_t count, void* state)
43
{
44 1
	belt_ecb_st* st = (belt_ecb_st*)state;
45 1
	ASSERT(count >= 16);
46 1
	ASSERT(memIsDisjoint2(buf, count, state, beltECB_keep()));
47
	// цикл по полным блокам
48 1
	while(count >= 16)
49
	{
50 1
		beltBlockEncr(buf, st->key);
51 1
		buf = (octet*)buf + 16;
52 1
		count -= 16;
53
	}
54
	// неполный блок? кража блока
55 1
	if (count)
56
	{
57 1
		memSwap((octet*)buf - 16, buf, count);
58 1
		beltBlockEncr((octet*)buf - 16, st->key);
59
	}
60
}
61

62 1
void beltECBStepD(void* buf, size_t count, void* state)
63
{
64 1
	belt_ecb_st* st = (belt_ecb_st*)state;
65 1
	ASSERT(count >= 16);
66 1
	ASSERT(memIsDisjoint2(buf, count, state, beltECB_keep()));
67
	// цикл по полным блокам
68 1
	while(count >= 16)
69
	{
70 1
		beltBlockDecr(buf, st->key);
71 1
		buf = (octet*)buf + 16;
72 1
		count -= 16;
73
	}
74
	// неполный блок? кража блока
75 1
	if (count)
76
	{
77 1
		memSwap((octet*)buf - 16, buf, count);
78 1
		beltBlockDecr((octet*)buf - 16, st->key);
79
	}
80
}
81

82 1
err_t beltECBEncr(void* dest, const void* src, size_t count,
83
	const octet key[], size_t len)
84
{
85
	void* state;
86
	// проверить входные данные
87 1
	if (count < 16 ||
88 1
		len != 16 && len != 24 && len != 32 ||
89 1
		!memIsValid(src, count) ||
90 1
		!memIsValid(key, len) ||
91 1
		!memIsValid(dest, count))
92 0
		return ERR_BAD_INPUT;
93
	// создать состояние
94 1
	state = blobCreate(beltECB_keep());
95 1
	if (state == 0)
96 0
		return ERR_OUTOFMEMORY;
97
	// зашифровать
98 1
	beltECBStart(state, key, len);
99 1
	memMove(dest, src, count);
100 1
	beltECBStepE(dest, count, state);
101
	// завершить
102 1
	blobClose(state);
103 1
	return ERR_OK;
104
}
105

106 1
err_t beltECBDecr(void* dest, const void* src, size_t count,
107
	const octet key[], size_t len)
108
{
109
	void* state;
110
	// проверить входные данные
111 1
	if (count < 16 ||
112 1
		len != 16 && len != 24 && len != 32 ||
113 1
		!memIsValid(src, count) ||
114 1
		!memIsValid(key, len) ||
115 1
		!memIsValid(dest, count))
116 0
		return ERR_BAD_INPUT;
117
	// создать состояние
118 1
	state = blobCreate(beltECB_keep());
119 1
	if (state == 0)
120 0
		return ERR_OUTOFMEMORY;
121
	// расшифровать
122 1
	beltECBStart(state, key, len);
123 1
	memMove(dest, src, count);
124 1
	beltECBStepD(dest, count, state);
125
	// завершить
126 1
	blobClose(state);
127 1
	return ERR_OK;
128
}
129

Read our documentation on viewing source code .

Loading