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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.