summaryrefslogtreecommitdiff
path: root/bsp/apm32/libraries/APM32F4xx_Library/APM32F4xx_StdPeriphDriver/src/apm32f4xx_cryp_des.c
blob: 9a7aa82475ba3b3603fbc5161bb0f5be1f24af95 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*!
 * @file        apm32f4xx_cryp_des.c
 *
 * @brief       This file provides high level functions to encrypt and decrypt,
 *              Input message using DES in ECB/CBC modes.
 *
 * @version     V1.0.2
 *
 * @date        2022-06-23
 *
 * @attention
 *
 *  Copyright (C) 2021-2022 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be usefull and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

#include "apm32f4xx_cryp.h"

/** @addtogroup APM32F4xx_StdPeriphDriver
  @{
*/

/** @defgroup CRYP_DES_Driver
  * @brief CRYP DES driver modules
  @{
*/

/** @defgroup CRYP_DES_Functions
  @{
*/

/*!
 * @brief     Encrypt and decrypt using DES in EBC Mode
 *
 * @param     mode: Pointer to a CRYP_MODE_T structure.
 *            This parameter can be one of the following values:
 *                @arg CRYP_MODE_DECRYPT: Encryption
 *                @arg CRYP_MODE_ENCRYPT: Decryption
 *
 * @param     key:    Key used for DES algorithm.
 * @param     input:  Input buffer.
 * @param     length: length of the Input buffer, must be a multiple of 8.
 * @param     output: Output buffer.
 *
 * @retval    SUCCESS or ERROR
 */
uint8_t CRYP_DES_ECB(CRYP_MODE_T mode, uint8_t key[8], uint8_t *input,
                     uint32_t length, uint8_t *output)
{
    CRYP_Config_T    DEC_crypConfig;
    CRYP_KeyConfig_T DEC_keyConfig;

    uint32_t keyAddr    = (uint32_t)key;
    uint32_t inputAddr  = (uint32_t)input;
    uint32_t outputAddr = (uint32_t)output;

    uint32_t i = 0;
    uint32_t flag = 0;
    uint32_t status = SUCCESS;
    __IO uint32_t counter = 0;

    CRYP_ConfigKeyStructInit(&DEC_keyConfig);

    if (mode == CRYP_MODE_ENCRYPT)
    {
        DEC_crypConfig.algoDir = CRYP_ALGODIR_ENCRYPT;
    }
    else
    {
        DEC_crypConfig.algoDir = CRYP_ALGODIR_DECRYPT;
    }
    DEC_crypConfig.algoMode = CRYP_ALGOMODE_DES_ECB;
    DEC_crypConfig.dataType = CRYP_DATATYPE_8B;
    CRYP_Config(&DEC_crypConfig);

    DEC_keyConfig.key1Left = __REV(*(uint32_t *)(keyAddr));
    keyAddr += 0x04;
    DEC_keyConfig.key1Right = __REV(*(uint32_t *)(keyAddr));
    CRYP_ConfigKey(&DEC_keyConfig);

    CRYP_FlushFIFO();
    CRYP_Enable();

    if (CRYP_ReadCmdStatus() == DISABLE)
    {
        status = ERROR;
    }
    else
    {
        for (i = 0; i < length; i += 8)
        {
            CRYP_InData(*(uint32_t *)(inputAddr));
            inputAddr += 0x04;
            CRYP_InData(*(uint32_t *)(inputAddr));
            inputAddr += 0x04;

            counter = 0;
            do
            {
                flag = CRYP_ReadStatusFlag(CRYP_FLAG_BUSY);
                counter++;
            }
            while ((counter != 0x00010000) && (flag != RESET));

            if (flag == SET)
            {
                status = ERROR;
            }
            else
            {
                *(uint32_t *)(outputAddr) = CRYP_OutData();
                outputAddr += 0x04;
                *(uint32_t *)(outputAddr) = CRYP_OutData();
                outputAddr += 0x04;
            }
        }
        CRYP_Disable();
    }
    return status;
}

/*!
 * @brief     Encrypt and decrypt using DES in CBC Mode
 *
 * @param     mode: Pointer to a CRYP_MODE_T structure.
 *            This parameter can be one of the following values:
 *                @arg CRYP_MODE_DECRYPT: Encryption
 *                @arg CRYP_MODE_ENCRYPT: Decryption
 *
 * @param     key:    Key used for DES algorithm.
 * @param     input:  Input buffer.
 * @param     IV:     Initialisation Vectors used for DES algorithm.
 * @param     length: length of the Input buffer, must be a multiple of 8.
 * @param     output: Output buffer.
 *
 * @retval    None
 */
uint8_t CRYP_DES_CBC(CRYP_MODE_T mode, uint8_t key[8],  uint8_t *input,
                     uint8_t IV[8], uint32_t length, uint8_t *output)
{
    CRYP_Config_T    DEC_crypConfig;
    CRYP_KeyConfig_T DEC_keyConfig;
    CRYP_IVConfig_T  DEC_IVConfig;

    uint32_t keyAddr    = (uint32_t)key;
    uint32_t inputAddr  = (uint32_t)input;
    uint32_t outputAddr = (uint32_t)output;
    uint32_t IVAddr     = (uint32_t)IV;

    uint32_t i = 0;
    uint32_t flag = 0;
    uint32_t status = SUCCESS;
    __IO uint32_t counter = 0;

    CRYP_ConfigKeyStructInit(&DEC_keyConfig);

    if (mode == CRYP_MODE_ENCRYPT)
    {
        DEC_crypConfig.algoDir = CRYP_ALGODIR_ENCRYPT;
    }
    else
    {
        DEC_crypConfig.algoDir = CRYP_ALGODIR_DECRYPT;
    }
    DEC_crypConfig.algoMode = CRYP_ALGOMODE_DES_CBC;
    DEC_crypConfig.dataType = CRYP_DATATYPE_8B;
    CRYP_Config(&DEC_crypConfig);

    DEC_keyConfig.key1Left = __REV(*(uint32_t *)(keyAddr));
    keyAddr += 0x04;
    DEC_keyConfig.key1Right = __REV(*(uint32_t *)(keyAddr));
    CRYP_ConfigKey(&DEC_keyConfig);

    DEC_IVConfig.IV0Left  = __REV(*(uint32_t *)(IVAddr));
    keyAddr += 0x04;
    DEC_IVConfig.IV0Right = __REV(*(uint32_t *)(IVAddr));
    CRYP_ConfigIV(&DEC_IVConfig);

    CRYP_FlushFIFO();
    CRYP_Enable();

    if (CRYP_ReadCmdStatus() == DISABLE)
    {
        status = ERROR;
    }
    else
    {
        for (i = 0; i < length; i += 8)
        {
            CRYP_InData(*(uint32_t *)(inputAddr));
            inputAddr += 0x04;
            CRYP_InData(*(uint32_t *)(inputAddr));
            inputAddr += 0x04;

            counter = 0;
            do
            {
                flag = CRYP_ReadStatusFlag(CRYP_FLAG_BUSY);
                counter++;
            }
            while ((counter != 0x00010000) && (flag != RESET));

            if (flag == SET)
            {
                status = ERROR;
            }
            else
            {
                *(uint32_t *)(outputAddr) = CRYP_OutData();
                outputAddr += 0x04;
                *(uint32_t *)(outputAddr) = CRYP_OutData();
                outputAddr += 0x04;
            }
        }
        CRYP_Disable();
    }
    return status;
}

/**@} end of group CRYP_DES_Functions */
/**@} end of group CRYP_DES_Driver */
/**@} end of group APM32F4xx_StdPeriphDriver */