libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
crypto_common_f24.c
Go to the documentation of this file.
1/** @addtogroup crypto_file
2 *
3 * @brief <b>libopencm3 STM32 Cryptographic controller</b>
4 *
5 * @version 1.0.0
6 *
7 * @date 17 Jun 2013
8 *
9 * This library supports the cryptographic coprocessor system for the
10 * STM32 series of ARM Cortex Microcontrollers
11 *
12 * LGPL License Terms @ref lgpl_license
13 */
14/*
15 * This file is part of the libopencm3 project.
16 *
17 * Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
18 *
19 * This library is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU Lesser General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
23 *
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU Lesser General Public License for more details.
28 *
29 * You should have received a copy of the GNU Lesser General Public License
30 * along with this library. If not, see <http://www.gnu.org/licenses/>.
31 */
32
33/**@{*/
34
36
37#define CRYP_CR_ALGOMODE_MASK ((1 << 19) | CRYP_CR_ALGOMODE)
38
39/**
40 * @brief Wait, if the Controller is busy
41 */
43{
44 while (CRYP_SR & CRYP_SR_BUSY);
45}
46
47/**
48 * @brief Set key value to the controller
49 * @param[in] keysize enum crypto_keysize Specified size of the key.
50 * @param[in] key uint64_t[] Key value (array of 4 items)
51 */
52void crypto_set_key(enum crypto_keysize keysize, uint64_t key[])
53{
54 int i;
55
57
58 CRYP_CR = (CRYP_CR & ~CRYP_CR_KEYSIZE) |
59 (keysize << CRYP_CR_KEYSIZE_SHIFT);
60
61 for (i = 0; i < 4; i++) {
62 CRYP_KR(i) = key[i];
63 }
64}
65
66/**
67 * @brief Set Initialization Vector
68 *
69 * @param[in] iv uint64_t[] Initialization vector (array of 4 items)
70
71 * @note Cryptographic controller must be in disabled state
72 */
73void crypto_set_iv(uint64_t iv[])
74{
75 int i;
76
78
79 for (i = 0; i < 4; i++) {
80 CRYP_IVR(i) = iv[i];
81 }
82}
83
84/**
85 * @brief Set the order of the data to be crypted
86 *
87 * @param[in] datatype enum crypto_datatype Specified datatype of the key.
88 */
90{
91 CRYP_CR = (CRYP_CR & ~CRYP_CR_DATATYPE) |
92 (datatype << CRYP_CR_DATATYPE_SHIFT);
93}
94
95/**
96 * @brief Set the algorithm for Encryption/decryption
97 *
98 *@param[in] mode enum crypto_mode Mode of execution
99 */
101{
102 mode &= ~CRYP_CR_ALGOMODE_MASK;
103
104 if ((mode == DECRYPT_AES_ECB) || (mode == DECRYPT_AES_CBC)) {
105 /* Unroll keys for the AES encoder for the user automatically */
106
107 CRYP_CR = (CRYP_CR & ~CRYP_CR_ALGOMODE_MASK) |
109
110 crypto_start();
112 /* module switches to DISABLE automatically */
113 }
114 /* set algo mode */
115 CRYP_CR = (CRYP_CR & ~CRYP_CR_ALGOMODE_MASK) | mode;
116
117 /* flush buffers */
119}
120
121/**
122 * @brief Enable the cryptographic controller and start processing
123 */
124void crypto_start(void)
125{
127}
128
129/**
130 * @brief Disable the cryptographic controller and stop processing
131 */
132
133void crypto_stop(void)
134{
135 CRYP_CR &= ~CRYP_CR_CRYPEN;
136}
137
138/**
139 * @brief Start of encryption or decryption on data buffers
140 *
141 * This blocking method transfers input buffer of specified length to the
142 * cryptographic coprocessor, and instructs him to begin of ciphering or
143 * deciphering. It waits for data to be ready, and then fills the processed
144 * data to output buffer.
145 *
146 * @param[in] inp uint32_t* Input array to crypt/decrypt.
147 * @param[in] outp uint32_t* Output array with crypted/encrypted data.
148 * @param[in] length uint32_t Length of the arrays
149 *
150 * @returns uint32_t Number of written words
151 */
152uint32_t crypto_process_block(uint32_t *inp, uint32_t *outp, uint32_t length)
153{
154 uint32_t rd = 0, wr = 0;
155
156 /* Transfer the data */
157 while (rd != length) {
158 if ((wr < length) && (CRYP_SR & CRYP_SR_IFNF)) {
159 CRYP_DIN = *inp++;
160 wr++;
161 }
162
163 if (CRYP_SR & CRYP_SR_OFNE) {
164 *outp++ = CRYP_DOUT;
165 rd++;
166 }
167 }
168
169 /* Wait to finish - Not needed ? */
171
172 return wr;
173}
174
175/**@}*/
crypto_datatype
crypto_keysize
crypto_mode
@ DECRYPT_AES_ECB
@ DECRYPT_AES_CBC
void crypto_set_algorithm(enum crypto_mode mode)
Set the algorithm for Encryption/decryption.
void crypto_set_iv(uint64_t iv[])
Set Initialization Vector.
void crypto_start(void)
Enable the cryptographic controller and start processing.
uint32_t crypto_process_block(uint32_t *inp, uint32_t *outp, uint32_t length)
Start of encryption or decryption on data buffers.
void crypto_set_key(enum crypto_keysize keysize, uint64_t key[])
Set key value to the controller.
void crypto_stop(void)
Disable the cryptographic controller and stop processing.
void crypto_set_datatype(enum crypto_datatype datatype)
Set the order of the data to be crypted.
void crypto_wait_busy(void)
Wait, if the Controller is busy.
#define CRYP_CR_ALGOMODE_AES_PREP
#define CRYP_CR_FFLUSH
#define CRYP_CR_CRYPEN
#define CRYP_IVR(i)
#define CRYP_SR
#define CRYP_CR_KEYSIZE_SHIFT
#define CRYP_SR_OFNE
#define CRYP_CR_DATATYPE_SHIFT
#define CRYP_CR
#define CRYP_DOUT
CRYP Data Output Register (CRYP_DOUT)
#define CRYP_KR(i)
#define CRYP_SR_IFNF
#define CRYP_SR_BUSY
#define CRYP_DIN