Skip to content

Commit

Permalink
Remove non-constant-time PKCS#1 v1.5 depadding function
Browse files Browse the repository at this point in the history
  • Loading branch information
xhanulik committed Dec 18, 2023
1 parent ca121e1 commit 47d2340
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 43 deletions.
2 changes: 0 additions & 2 deletions src/libopensc/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ int _sc_card_add_xeddsa_alg(struct sc_card *card, unsigned int key_length,

int sc_pkcs1_strip_01_padding(struct sc_context *ctx, const u8 *in_dat, size_t in_len,
u8 *out_dat, size_t *out_len);
int sc_pkcs1_strip_02_padding(struct sc_context *ctx, const u8 *data, size_t len,
u8 *out_dat, size_t *out_len);
int sc_pkcs1_strip_02_padding_constant_time(sc_context_t *ctx, unsigned int n, const u8 *data,
unsigned int data_len, u8 *out, unsigned int *out_len);
int sc_pkcs1_strip_digest_info_prefix(unsigned int *algorithm,
Expand Down
44 changes: 3 additions & 41 deletions src/libopensc/padding.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,47 +147,9 @@ sc_pkcs1_strip_01_padding(struct sc_context *ctx, const u8 *in_dat, size_t in_le
}


/* remove pkcs1 BT02 padding (adding BT02 padding is currently not
* needed/implemented) */
int
sc_pkcs1_strip_02_padding(sc_context_t *ctx, const u8 *data, size_t len, u8 *out, size_t *out_len)
{
unsigned int n = 0;

LOG_FUNC_CALLED(ctx);
if (data == NULL || len < 3)
LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL);

/* skip leading zero byte */
if (*data == 0) {
data++;
len--;
}
if (data[0] != 0x02)
LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_PADDING);
/* skip over padding bytes */
for (n = 1; n < len && data[n]; n++)
;
/* Must be at least 8 pad bytes */
if (n >= len || n < 9)
LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_PADDING);
n++;
if (out == NULL)
/* just check the padding */
LOG_FUNC_RETURN(ctx, SC_SUCCESS);

/* Now move decrypted contents to head of buffer */
if (*out_len < len - n)
LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL);
*out_len = len - n;
memmove(out, data + n, *out_len);

sc_log(ctx, "stripped output(%"SC_FORMAT_LEN_SIZE_T"u): %s", len - n,
sc_dump_hex(out, len - n));
LOG_FUNC_RETURN(ctx, len - n);
}

/* Original source: https://github.com/openssl/openssl/blob/9890cc42daff5e2d0cad01ac4bf78c391f599a6e/crypto/rsa/rsa_pk1.c#L171 */
/* Remove pkcs1 BT02 padding (adding BT02 padding is currently not
* needed/implemented) in constant-time.
* Original source: https://github.com/openssl/openssl/blob/9890cc42daff5e2d0cad01ac4bf78c391f599a6e/crypto/rsa/rsa_pk1.c#L171 */
int
sc_pkcs1_strip_02_padding_constant_time(sc_context_t *ctx, unsigned int n, const u8 *data, unsigned int data_len, u8 *out, unsigned int *out_len)
{
Expand Down

0 comments on commit 47d2340

Please sign in to comment.