agievich / bee2
1
/*
2
*******************************************************************************
3
\file belt_bde.c
4
\brief STB 34.101.31 (belt): BDE (Blockwise Disk Encryption)
5
\project bee2 [cryptographic library]
6
\author (C) Sergey Agievich [agievich@{bsu.by|gmail.com}]
7
\created 2018.06.28
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
Шифрование в режиме BDE
25
*******************************************************************************
26
*/
27
typedef struct
28
{
29
	u32 key[8];			/*< форматированный ключ */
30
	u32 s[4];			/*< переменная s */
31
	octet block[16];	/*< вспомогательный блок */
32
} belt_bde_st;
33

34 1
size_t beltBDE_keep()
35
{
36 1
	return sizeof(belt_bde_st);
37
}
38

39 1
void beltBDEStart(void* state, const octet key[], size_t len, 
40
	const octet iv[16])
41
{
42 1
	belt_bde_st* st = (belt_bde_st*)state;
43 1
	ASSERT(memIsDisjoint2(iv, 16, state, beltBDE_keep()));
44 1
	beltKeyExpand2(st->key, key, len);
45 1
	u32From(st->s, iv, 16);
46 1
	beltBlockEncr2(st->s, st->key);
47
}
48

49 1
void beltBDEStepE(void* buf, size_t count, void* state)
50
{
51 1
	belt_bde_st* st = (belt_bde_st*)state;
52 1
	ASSERT(count % 16 == 0);
53 1
	ASSERT(memIsDisjoint2(buf, count, state, beltBDE_keep()));
54
	// цикл по блокам
55 1
	while(count >= 16)
56
	{
57 1
		beltBlockMulC(st->s);
58 1
		u32To(st->block, 16, st->s);
59 1
		beltBlockXor2(buf, st->block);
60 1
		beltBlockEncr(buf, st->key);
61 1
		beltBlockXor2(buf, st->block);
62 1
		buf = (octet*)buf + 16;
63 1
		count -= 16;
64
	}
65
}
66

67 1
void beltBDEStepD(void* buf, size_t count, void* state)
68
{
69 1
	belt_bde_st* st = (belt_bde_st*)state;
70 1
	ASSERT(count % 16 == 0);
71 1
	ASSERT(memIsDisjoint2(buf, count, state, beltBDE_keep()));
72
	// цикл по блокам
73 1
	while(count >= 16)
74
	{
75 1
		beltBlockMulC(st->s);
76 1
		u32To(st->block, 16, st->s);
77 1
		beltBlockXor2(buf, st->block);
78 1
		beltBlockDecr(buf, st->key);
79 1
		beltBlockXor2(buf, st->block);
80 1
		buf = (octet*)buf + 16;
81 1
		count -= 16;
82
	}
83
}
84

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

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

Read our documentation on viewing source code .

Loading