Mbed OS Reference
Loading...
Searching...
No Matches
TARGET_M480/des/des_alt.h
1/*
2 * Copyright (c) 2015-2016, Nuvoton Technology Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef MBEDTLS_DES_ALT_H
20#define MBEDTLS_DES_ALT_H
21
22#include "mbedtls/des.h"
23
24#if defined(MBEDTLS_DES_ALT)
25
26#include <stddef.h>
27#include <stdint.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33/**
34 * \brief DES context structure
35 */
36typedef struct {
37 int enc; /* 0: dec, 1: enc */
38 uint16_t keyopt; /* 1: All three keys are independent.
39 * 2: K1 and K2 are independent, and K3 = K1.
40 * 3: All three keys are identical, i.e. K1 = K2 = K3. */
41 uint8_t key[3][MBEDTLS_DES_KEY_SIZE]; /* 3DES keys */
42}
44
45/**
46 * \brief Triple-DES context structure
47 */
48typedef struct {
49 int enc; /*!< 0: dec, 1: enc */
50 uint16_t keyopt;
51 uint8_t key[3][MBEDTLS_DES_KEY_SIZE]; /*!< 3DES keys */
52}
54
55/**
56 * \brief Initialize DES context
57 *
58 * \param ctx DES context to be initialized
59 */
61
62/**
63 * \brief Clear DES context
64 *
65 * \param ctx DES context to be cleared
66 */
68
69/**
70 * \brief Initialize Triple-DES context
71 *
72 * \param ctx DES3 context to be initialized
73 */
75
76/**
77 * \brief Clear Triple-DES context
78 *
79 * \param ctx DES3 context to be cleared
80 */
82
83/**
84 * \brief Set key parity on the given key to odd.
85 *
86 * DES keys are 56 bits long, but each byte is padded with
87 * a parity bit to allow verification.
88 *
89 * \param key 8-byte secret key
90 */
91void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
92
93/**
94 * \brief Check that key parity on the given key is odd.
95 *
96 * DES keys are 56 bits long, but each byte is padded with
97 * a parity bit to allow verification.
98 *
99 * \param key 8-byte secret key
100 *
101 * \return 0 is parity was ok, 1 if parity was not correct.
102 */
103int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
104
105/**
106 * \brief Check that key is not a weak or semi-weak DES key
107 *
108 * \param key 8-byte secret key
109 *
110 * \return 0 if no weak key was found, 1 if a weak key was identified.
111 */
112int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
113
114/**
115 * \brief DES key schedule (56-bit, encryption)
116 *
117 * \param ctx DES context to be initialized
118 * \param key 8-byte secret key
119 *
120 * \return 0
121 */
122int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
123
124/**
125 * \brief DES key schedule (56-bit, decryption)
126 *
127 * \param ctx DES context to be initialized
128 * \param key 8-byte secret key
129 *
130 * \return 0
131 */
132int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
133
134/**
135 * \brief Triple-DES key schedule (112-bit, encryption)
136 *
137 * \param ctx 3DES context to be initialized
138 * \param key 16-byte secret key
139 *
140 * \return 0
141 */
143 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
144
145/**
146 * \brief Triple-DES key schedule (112-bit, decryption)
147 *
148 * \param ctx 3DES context to be initialized
149 * \param key 16-byte secret key
150 *
151 * \return 0
152 */
154 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
155
156/**
157 * \brief Triple-DES key schedule (168-bit, encryption)
158 *
159 * \param ctx 3DES context to be initialized
160 * \param key 24-byte secret key
161 *
162 * \return 0
163 */
165 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
166
167/**
168 * \brief Triple-DES key schedule (168-bit, decryption)
169 *
170 * \param ctx 3DES context to be initialized
171 * \param key 24-byte secret key
172 *
173 * \return 0
174 */
176 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
177
178/**
179 * \brief DES-ECB block encryption/decryption
180 *
181 * \param ctx DES context
182 * \param input 64-bit input block
183 * \param output 64-bit output block
184 *
185 * \return 0 if successful
186 */
188 const unsigned char input[8],
189 unsigned char output[8] );
190
191#if defined(MBEDTLS_CIPHER_MODE_CBC)
192/**
193 * \brief DES-CBC buffer encryption/decryption
194 *
195 * \note Upon exit, the content of the IV is updated so that you can
196 * call the function same function again on the following
197 * block(s) of data and get the same result as if it was
198 * encrypted in one call. This allows a "streaming" usage.
199 * If on the other hand you need to retain the contents of the
200 * IV, you should either save it manually or use the cipher
201 * module instead.
202 *
203 * \param ctx DES context
204 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
205 * \param length length of the input data
206 * \param iv initialization vector (updated after use)
207 * \param input buffer holding the input data
208 * \param output buffer holding the output data
209 */
210int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
211 int mode,
212 size_t length,
213 unsigned char iv[8],
214 const unsigned char *input,
215 unsigned char *output );
216#endif /* MBEDTLS_CIPHER_MODE_CBC */
217
218/**
219 * \brief 3DES-ECB block encryption/decryption
220 *
221 * \param ctx 3DES context
222 * \param input 64-bit input block
223 * \param output 64-bit output block
224 *
225 * \return 0 if successful
226 */
228 const unsigned char input[8],
229 unsigned char output[8] );
230
231#if defined(MBEDTLS_CIPHER_MODE_CBC)
232/**
233 * \brief 3DES-CBC buffer encryption/decryption
234 *
235 * \note Upon exit, the content of the IV is updated so that you can
236 * call the function same function again on the following
237 * block(s) of data and get the same result as if it was
238 * encrypted in one call. This allows a "streaming" usage.
239 * If on the other hand you need to retain the contents of the
240 * IV, you should either save it manually or use the cipher
241 * module instead.
242 *
243 * \param ctx 3DES context
244 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
245 * \param length length of the input data
246 * \param iv initialization vector (updated after use)
247 * \param input buffer holding the input data
248 * \param output buffer holding the output data
249 *
250 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
251 */
252int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
253 int mode,
254 size_t length,
255 unsigned char iv[8],
256 const unsigned char *input,
257 unsigned char *output );
258#endif /* MBEDTLS_CIPHER_MODE_CBC */
259
260/**
261 * \brief Internal function for key expansion.
262 * (Only exposed to allow overriding it,
263 * see MBEDTLS_DES_SETKEY_ALT)
264 *
265 * \param SK Round keys
266 * \param key Base key
267 */
268void mbedtls_des_setkey( uint32_t SK[32],
269 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
270#ifdef __cplusplus
271}
272#endif
273
274#endif /* MBEDTLS_DES_ALT */
275
276#endif /* des_alt.h */
void mbedtls_des_setkey(uint32_t SK[32], const unsigned char key[8])
Internal function for key expansion.
int mbedtls_des3_set3key_dec(mbedtls_des3_context *ctx, const unsigned char key[8 *3])
Triple-DES key schedule (168-bit, decryption)
void mbedtls_des_init(mbedtls_des_context *ctx)
Initialize DES context.
void mbedtls_des3_free(mbedtls_des3_context *ctx)
Clear Triple-DES context.
int mbedtls_des_key_check_key_parity(const unsigned char key[8])
Check that key parity on the given key is odd.
int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[8])
DES key schedule (56-bit, decryption)
int mbedtls_des3_set2key_enc(mbedtls_des3_context *ctx, const unsigned char key[8 *2])
Triple-DES key schedule (112-bit, encryption)
int mbedtls_des3_crypt_ecb(mbedtls_des3_context *ctx, const unsigned char input[8], unsigned char output[8])
3DES-ECB block encryption/decryption
void mbedtls_des_key_set_parity(unsigned char key[8])
Set key parity on the given key to odd.
int mbedtls_des_crypt_ecb(mbedtls_des_context *ctx, const unsigned char input[8], unsigned char output[8])
DES-ECB block encryption/decryption.
int mbedtls_des3_set3key_enc(mbedtls_des3_context *ctx, const unsigned char key[8 *3])
Triple-DES key schedule (168-bit, encryption)
int mbedtls_des3_set2key_dec(mbedtls_des3_context *ctx, const unsigned char key[8 *2])
Triple-DES key schedule (112-bit, decryption)
void mbedtls_des3_init(mbedtls_des3_context *ctx)
Initialize Triple-DES context.
int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[8])
DES key schedule (56-bit, encryption)
int mbedtls_des_key_check_weak(const unsigned char key[8])
Check that key is not a weak or semi-weak DES key.
void mbedtls_des_free(mbedtls_des_context *ctx)
Clear DES context.
DES block cipher.
Triple-DES context structure.
DES context structure.