@@ -145,13 +145,19 @@ int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
145
145
uint8_t * out_data , int out_len , int is_decryption )
146
146
{
147
147
const int byte_size = ctx -> num_octets ;
148
- int i = 0 , size ;
148
+ int i = 0 , size = -1 ;
149
149
bigint * decrypted_bi , * dat_bi ;
150
- uint8_t * block = ( uint8_t * ) malloc ( byte_size ) ;
150
+ uint8_t * block = NULL ;
151
151
int pad_count = 0 ;
152
152
153
+ do
154
+ {
153
155
if (out_len < byte_size ) /* check output has enough size */
154
- return -1 ;
156
+ break ;
157
+
158
+ block = (uint8_t * )malloc (byte_size );
159
+ if (!block )
160
+ break ;
155
161
156
162
memset (out_data , 0 , out_len ); /* initialise */
157
163
@@ -168,13 +174,13 @@ int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
168
174
bi_export (ctx -> bi_ctx , decrypted_bi , block , byte_size );
169
175
170
176
if (block [i ++ ] != 0 ) /* leading 0? */
171
- return -1 ;
177
+ break ;
172
178
173
179
#ifdef CONFIG_SSL_CERT_VERIFICATION
174
180
if (is_decryption == 0 ) /* PKCS1.5 signing pads with "0xff"s */
175
181
{
176
182
if (block [i ++ ] != 0x01 ) /* BT correct? */
177
- return -1 ;
183
+ break ;
178
184
179
185
while (block [i ++ ] == 0xff && i < byte_size )
180
186
pad_count ++ ;
@@ -183,21 +189,25 @@ int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
183
189
#endif
184
190
{
185
191
if (block [i ++ ] != 0x02 ) /* BT correct? */
186
- return -1 ;
192
+ break ;
187
193
188
194
while (block [i ++ ] && i < byte_size )
189
195
pad_count ++ ;
190
196
}
191
197
192
198
/* check separator byte 0x00 - and padding must be 8 or more bytes */
193
199
if (i == byte_size || pad_count < 8 )
194
- return -1 ;
200
+ break ;
195
201
196
202
size = byte_size - i ;
197
203
198
204
/* get only the bit we want */
199
205
memcpy (out_data , & block [i ], size );
200
- free (block );
206
+ } while (false);
207
+
208
+ if (block )
209
+ free (block );
210
+
201
211
return size ;
202
212
}
203
213
0 commit comments