agievich / bee2
1
/*
2
*******************************************************************************
3
\file belt_kwp.c
4
\brief STB 34.101.31 (belt): KWP (keywrap = key encryption + authentication)
5
\project bee2 [cryptographic library]
6
\author (C) Sergey Agievich [agievich@{bsu.by|gmail.com}]
7
\created 2012.12.18
8
\version 2019.06.26
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
#include "belt_lcl.h"
20

21
/*
22
*******************************************************************************
23
Шифрование и имитозащита ключей (KWP)
24
*******************************************************************************
25
*/
26

27 1
err_t beltKWPWrap(octet dest[], const octet src[], size_t count,
28
	const octet header[16], const octet key[], size_t len)
29
{
30
	void* state;
31
	// проверить входные данные
32 1
	if (count < 16 ||
33 1
		len != 16 && len != 24 && len != 32 ||
34 1
		!memIsValid(src, count) ||
35 1
		!memIsNullOrValid(header, 16) ||
36 1
		header && !memIsDisjoint2(src, count, header, 16) ||
37 1
		!memIsValid(key, len) ||
38 1
		!memIsValid(dest, count + 16))
39 0
		return ERR_BAD_INPUT;
40
	// создать состояние
41 1
	state = blobCreate(beltKWP_keep());
42 1
	if (state == 0)
43 0
		return ERR_OUTOFMEMORY;
44
	// установить защиту
45 1
	beltKWPStart(state, key, len);
46 1
	memMove(dest, src, count);
47 1
	if (header)
48 1
		memJoin(dest, src, count, header, 16);
49
	else
50 1
		memSetZero(dest + count, 16);
51 1
	beltKWPStepE(dest, count + 16, state);
52
	// завершить
53 1
	blobClose(state);
54 1
	return ERR_OK;
55
}
56

57 1
err_t beltKWPUnwrap(octet dest[], const octet src[], size_t count,
58
	const octet header[16], const octet key[], size_t len)
59
{
60
	void* state;
61
	octet* header2;
62
	// проверить входные данные
63 1
	if (count < 32 ||
64 1
		len != 16 && len != 24 && len != 32 ||
65 1
		!memIsValid(src, count) ||
66 1
		!memIsNullOrValid(header, 16) ||
67 1
		!memIsValid(key, len) ||
68 1
		!memIsValid(dest, count - 16))
69 0
		return ERR_BAD_INPUT;
70
	// создать состояние
71 1
	state = blobCreate(beltKWP_keep() + 16);
72 1
	if (state == 0)
73 0
		return ERR_OUTOFMEMORY;
74 1
	header2 = (octet*)state + beltKWP_keep();
75
	// снять защиту
76 1
	beltKWPStart(state, key, len);
77 1
	memCopy(header2, src + count - 16, 16);
78 1
	memMove(dest, src, count - 16);
79 1
	beltKWPStepD2(dest, header2, count, state);
80 1
	if (header && !memEq(header, header2, 16) ||
81 0
		header == 0 && !memIsZero(header2, 16))
82
	{
83 0
		memSetZero(dest, count - 16);
84 0
		blobClose(state);
85 0
		return ERR_BAD_KEYTOKEN;
86
	}
87
	// завершить
88 1
	blobClose(state);
89 1
	return ERR_OK;
90
}

Read our documentation on viewing source code .

Loading