1 /*!
2     \file    gd32l23x_cau_des.c
3     \brief   CAU DES driver
4 
5     \version 2021-08-04, V1.0.0, firmware for GD32L23x
6 */
7 
8 /*
9     Copyright (c) 2021, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #include "gd32l23x_cau.h"
36 
37 #define DESBUSY_TIMEOUT    ((uint32_t)0x00010000U)
38 
39 /* DES calculate process */
40 static ErrStatus cau_des_calculate(uint8_t *input, uint32_t in_length, uint8_t *output);
41 
42 /*!
43     \brief      encrypt and decrypt using DES in ECB mode
44     \param[in]  cau_parameter: pointer to the input structure
45                   alg_dir: algorithm dirctory
46                     CAU_ENCRYPT, CAU_DECRYPT
47                   key: key, 8 bytes
48                   input: input data
49                   in_length: input data length in bytes, must be a multiple of 8 bytes
50     \param[out] output: pointer to the output buffer
51     \retval     ErrStatus: SUCCESS or ERROR
52 */
cau_des_ecb(cau_parameter_struct * cau_parameter,uint8_t * output)53 ErrStatus cau_des_ecb(cau_parameter_struct *cau_parameter, uint8_t *output)
54 {
55     ErrStatus ret = ERROR;
56     cau_key_parameter_struct key_initpara;
57     uint32_t keyaddr    = (uint32_t)(cau_parameter->key);
58     uint32_t inputaddr  = (uint32_t)(cau_parameter->input);
59     uint32_t outputaddr = (uint32_t)output;
60 
61     /* key structure initialization */
62     cau_key_struct_para_init(&key_initpara);
63     /* initialize the CAU peripheral */
64     cau_init(cau_parameter->alg_dir, CAU_MODE_DES_ECB, CAU_SWAPPING_8BIT);
65 
66     /* key initialisation */
67     key_initpara.key_1_high = __REV(*(uint32_t *)(keyaddr));
68     keyaddr += 4U;
69     key_initpara.key_1_low = __REV(*(uint32_t *)(keyaddr));
70     cau_key_init(&key_initpara);
71 
72     /* flush the IN and OUT FIFOs */
73     cau_fifo_flush();
74     /* enable the CAU peripheral */
75     cau_enable();
76     /* DES calculate process */
77     ret = cau_des_calculate((uint8_t *)inputaddr, cau_parameter->in_length, (uint8_t *)outputaddr);
78     /* disable the CAU peripheral */
79     cau_disable();
80 
81     return ret;
82 }
83 
84 /*!
85     \brief      encrypt and decrypt using DES in CBC mode
86     \param[in]  cau_parameter: pointer to the input structure
87                   alg_dir: algorithm dirctory
88                     CAU_ENCRYPT, CAU_DECRYPT
89                   key: key, 8 bytes
90                   iv: initialization vector, 8 bytes
91                   input: input data
92                   in_length: input data length in bytes, must be a multiple of 8 bytes
93     \param[out] output: pointer to the output structure
94     \retval     ErrStatus: SUCCESS or ERROR
95 */
cau_des_cbc(cau_parameter_struct * cau_parameter,uint8_t * output)96 ErrStatus cau_des_cbc(cau_parameter_struct *cau_parameter, uint8_t *output)
97 {
98     ErrStatus ret = ERROR;
99     cau_key_parameter_struct key_initpara;
100     cau_iv_parameter_struct iv_initpara;
101     uint32_t keyaddr    = (uint32_t)(cau_parameter->key);
102     uint32_t inputaddr  = (uint32_t)(cau_parameter->input);
103     uint32_t outputaddr = (uint32_t)output;
104     uint32_t ivaddr     = (uint32_t)(cau_parameter->iv);
105 
106     /* key structure initialization */
107     cau_key_struct_para_init(&key_initpara);
108     /* initialize the CAU peripheral */
109     cau_init(cau_parameter->alg_dir, CAU_MODE_DES_CBC, CAU_SWAPPING_8BIT);
110 
111     /* key initialisation */
112     key_initpara.key_1_high = __REV(*(uint32_t *)(keyaddr));
113     keyaddr += 4U;
114     key_initpara.key_1_low = __REV(*(uint32_t *)(keyaddr));
115     cau_key_init(&key_initpara);
116 
117     /* vectors initialization */
118     iv_initpara.iv_0_high = __REV(*(uint32_t *)(ivaddr));
119     ivaddr += 4U;
120     iv_initpara.iv_0_low = __REV(*(uint32_t *)(ivaddr));
121     cau_iv_init(&iv_initpara);
122 
123     /* flush the IN and OUT FIFOs */
124     cau_fifo_flush();
125 
126     /* enable the CAU peripheral */
127     cau_enable();
128     /* DES calculate process */
129     ret = cau_des_calculate((uint8_t *)inputaddr, cau_parameter->in_length, (uint8_t *)outputaddr);
130     /* disable the CAU peripheral */
131     cau_disable();
132 
133     return ret;
134 }
135 
136 /*!
137     \brief      DES calculate process
138     \param[in]  input: pointer to the input buffer
139     \param[in]  in_length: length of the input buffer in bytes, must be a multiple of 8 bytes
140     \param[in]  output: pointer to the returned buffer
141     \param[out] none
142     \retval     ErrStatus: SUCCESS or ERROR
143 */
cau_des_calculate(uint8_t * input,uint32_t in_length,uint8_t * output)144 static ErrStatus cau_des_calculate(uint8_t *input, uint32_t in_length, uint8_t *output)
145 {
146     uint32_t inputaddr  = (uint32_t)input;
147     uint32_t outputaddr = (uint32_t)output;
148     uint32_t i = 0U;
149     __IO uint32_t counter = 0U;
150     uint32_t busystatus = 0U;
151 
152     /* the clock is not enabled or there is no embeded CAU peripheral */
153     if(DISABLE == cau_enable_state_get()) {
154         return ERROR;
155     }
156 
157     for(i = 0U; i < in_length; i += 8U) {
158         /* write data to the IN FIFO */
159         cau_data_write(*(uint32_t *)(inputaddr));
160         inputaddr += 4U;
161         cau_data_write(*(uint32_t *)(inputaddr));
162         inputaddr += 4U;
163 
164         /* wait until the complete message has been processed */
165         counter = 0U;
166         do {
167             busystatus = cau_flag_get(CAU_FLAG_BUSY);
168             counter++;
169         } while((DESBUSY_TIMEOUT != counter) && (RESET != busystatus));
170 
171         if(RESET != busystatus) {
172             return ERROR;
173         } else {
174             /* read the output block from the output FIFO */
175             *(uint32_t *)(outputaddr) = cau_data_read();
176             outputaddr += 4U;
177             *(uint32_t *)(outputaddr) = cau_data_read();
178             outputaddr += 4U;
179         }
180     }
181 
182     return SUCCESS;
183 }
184