agievich / bee2
1
/*
2
*******************************************************************************
3
\file belt_sde.c
4
\brief STB 34.101.31 (belt): SDE (Sectorwise Disk Encryption)
5
\project bee2 [cryptographic library]
6
\author (C) Sergey Agievich [agievich@{bsu.by|gmail.com}]
7
\created 2018.09.01
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
Шифрование в режиме SDE
25
*******************************************************************************
26
*/
27
typedef struct
28
{
29
	belt_wbl_st wbl[1];	/*< состояние механизма WBL */
30
	octet s[16];		/*< переменная s */
31
} belt_sde_st;
32

33 1
size_t beltSDE_keep()
34
{
35 1
	return sizeof(belt_sde_st);
36
}
37

38 1
void beltSDEStart(void* state, const octet key[], size_t len)
39
{
40 1
	belt_sde_st* st = (belt_sde_st*)state;
41 1
	ASSERT(memIsValid(state, beltSDE_keep()));
42 1
	beltWBLStart(st->wbl, key, len);
43
}
44

45 1
void beltSDEStepE(void* buf, size_t count, const octet iv[16], void* state)
46
{
47 1
	belt_sde_st* st = (belt_sde_st*)state;
48 1
	ASSERT(count % 16 == 0 && count >= 16);
49 1
	ASSERT(memIsDisjoint2(buf, count, state, beltSDE_keep()));
50 1
	ASSERT(memIsValid(iv, 16));
51
	// зашифровать синхропосылку
52 1
	memCopy(st->s, iv, 16);
53 1
	beltBlockEncr(st->s, st->wbl->key);
54
	// каскад XEX
55 1
	beltBlockXor2(buf, st->s);
56 1
	beltWBLStepE(buf, count, st->wbl);
57 1
	beltBlockXor2(buf, st->s);
58
}
59

60 1
void beltSDEStepD(void* buf, size_t count, const octet iv[16], void* state)
61
{
62 1
	belt_sde_st* st = (belt_sde_st*)state;
63 1
	ASSERT(count % 16 == 0 && count >= 32);
64 1
	ASSERT(memIsDisjoint2(buf, count, state, beltSDE_keep()));
65 1
	ASSERT(memIsValid(iv, 16));
66
	// зашифровать синхропосылку
67 1
	memCopy(st->s, iv, 16);
68 1
	beltBlockEncr(st->s, st->wbl->key);
69
	// каскад XEX
70 1
	beltBlockXor2(buf, st->s);
71 1
	beltWBLStepD(buf, count, st->wbl);
72 1
	beltBlockXor2(buf, st->s);
73
}
74

75 1
err_t beltSDEEncr(void* dest, const void* src, size_t count,
76
	const octet key[], size_t len, const octet iv[16])
77
{
78
	void* state;
79
	// проверить входные данные
80 1
	if (count % 16 != 0 || count < 32 ||
81 1
		len != 16 && len != 24 && len != 32 ||
82 1
		!memIsValid(src, count) ||
83 1
		!memIsValid(key, len) ||
84 1
		!memIsValid(iv, 16) ||
85 1
		!memIsValid(dest, count))
86 0
		return ERR_BAD_INPUT;
87
	// создать состояние
88 1
	state = blobCreate(beltSDE_keep());
89 1
	if (state == 0)
90 0
		return ERR_OUTOFMEMORY;
91
	// зашифровать
92 1
	beltSDEStart(state, key, len);
93 1
	memMove(dest, src, count);
94 1
	beltSDEStepE(dest, count, iv, state);
95
	// завершить
96 1
	blobClose(state);
97 1
	return ERR_OK;
98
}
99

100 1
err_t beltSDEDecr(void* dest, const void* src, size_t count,
101
	const octet key[], size_t len, const octet iv[16])
102
{
103
	void* state;
104
	// проверить входные данные
105 1
	if (count % 16 != 0 || count < 32 ||
106 1
		len != 16 && len != 24 && len != 32 ||
107 1
		!memIsValid(src, count) ||
108 1
		!memIsValid(key, len) ||
109 1
		!memIsValid(iv, 16) ||
110 1
		!memIsValid(dest, count))
111 0
		return ERR_BAD_INPUT;
112
	// создать состояние
113 1
	state = blobCreate(beltSDE_keep());
114 1
	if (state == 0)
115 0
		return ERR_OUTOFMEMORY;
116
	// расшифровать
117 1
	beltSDEStart(state, key, len);
118 1
	memMove(dest, src, count);
119 1
	beltSDEStepD(dest, count, iv, state);
120
	// завершить
121 1
	blobClose(state);
122 1
	return ERR_OK;
123
}

Read our documentation on viewing source code .

Loading