summaryrefslogtreecommitdiffstats
path: root/libopencm3/lib/stm32/common/hash_common_f24.c
blob: 24d489c1009e87aa1686487fe7866cf4dcf56c9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/** @addtogroup hash_file
 *
 * @author @htmlonly © @endhtmlonly 2013
 * Mikhail Avkhimenia <mikhail@avkhimenia.net>
 *
 * This library supports the HASH processor in the STM32F2 and STM32F4
 * series of ARM Cortex Microcontrollers by ST Microelectronics.
 *
 * LGPL License Terms @ref lgpl_license
 *  */

/*
 * This file is part of the libopencm3 project.
 *
 * Copyright (C) 2013 Mikhail Avkhimenia <mikhail@avkhimenia.net>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

/**@{*/

#include <libopencm3/stm32/hash.h>

/*---------------------------------------------------------------------------*/
/** @brief HASH Set Mode

Sets up the specified mode - either HASH or HMAC.

@param[in] mode unsigned int8. Hash processor mode: @ref hash_mode
*/

void hash_set_mode(uint8_t mode)
{
	HASH_CR &= ~HASH_CR_MODE;
	HASH_CR |= mode;
}

/*---------------------------------------------------------------------------*/
/** @brief HASH Set Algorithm

Sets up the specified algorithm - either MD5 or SHA1.

@param[in] algorithm unsigned int8. Hash algorithm: @ref hash_algorithm
*/

void hash_set_algorithm(uint8_t algorithm)
{
	HASH_CR &= ~HASH_CR_ALGO;
	HASH_CR |= algorithm;
}

/*---------------------------------------------------------------------------*/
/** @brief HASH Set Data Type

Sets up the specified data type: 32Bit, 16Bit, 8Bit, Bitstring.

@param[in] datatype unsigned int8. Hash data type: @ref hash_data_type
*/

void hash_set_data_type(uint8_t datatype)
{
	HASH_CR &= ~HASH_CR_DATATYPE;
	HASH_CR |= datatype;
}

/*---------------------------------------------------------------------------*/
/** @brief HASH Set Key Length

Sets up the specified key length: Long, Short.

@param[in] keylength unsigned int8. Hash data type: @ref hash_key_length
*/

void hash_set_key_length(uint8_t keylength)
{
	HASH_CR &= ~HASH_CR_LKEY;
	HASH_CR |= keylength;
}

/*---------------------------------------------------------------------------*/
/** @brief HASH Set Last Word Valid Bits

Specifies the number of valid bits in the last word.

@param[in] validbits unsigned int8. Number of valid bits.
*/

void hash_set_last_word_valid_bits(uint8_t validbits)
{
	HASH_STR &= ~(HASH_STR_NBW);
	HASH_STR |= validbits;
}

/*---------------------------------------------------------------------------*/
/** @brief HASH Init

Initializes the HASH processor.

*/

void hash_init()
{
	HASH_CR |= HASH_CR_INIT;
}

/*---------------------------------------------------------------------------*/
/** @brief HASH Add data

Puts data into the HASH processor's queue.

@param[in] data unsigned int32. Hash input data.
*/

void hash_add_data(uint32_t data)
{
	HASH_DIN = data;
}

/*---------------------------------------------------------------------------*/
/** @brief HASH Digest

Starts the processing of the last data block.

*/

void hash_digest()
{
	HASH_STR |= HASH_STR_DCAL;
}

/*---------------------------------------------------------------------------*/
/** @brief HASH Get Hash Result

Makes a copy of the resulting hash.

@param[out] data unsigned int32. Hash 4\5 words long depending on the algorithm.
@param[in] algorithm unsigned int8. Hash algorithm: @ref hash_algorithm
*/

void hash_get_result(uint32_t *data)
{
	data[0] = HASH_HR[0];
	data[1] = HASH_HR[1];
	data[2] = HASH_HR[2];
	data[3] = HASH_HR[3];

	if ((HASH_CR & HASH_CR_ALGO) == HASH_ALGO_SHA1) {
		data[4] = HASH_HR[4];
	}
}
/**@}*/