2 * Copyright 2005-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
13 #include <openssl/rsa.h>
14 #include <openssl/objects.h>
16 int RSA_padding_add_X931(unsigned char *to
, int tlen
,
17 const unsigned char *from
, int flen
)
23 * Absolute minimum amount of padding is 1 header nibble, 1 padding
24 * nibble and 2 trailer bytes: but 1 hash if is already in 'from'.
30 RSAerr(RSA_F_RSA_PADDING_ADD_X931
, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
);
34 p
= (unsigned char *)to
;
36 /* If no padding start and end nibbles are in one byte */
42 memset(p
, 0xBB, j
- 1);
47 memcpy(p
, from
, (unsigned int)flen
);
53 int RSA_padding_check_X931(unsigned char *to
, int tlen
,
54 const unsigned char *from
, int flen
, int num
)
57 const unsigned char *p
;
60 if ((num
!= flen
) || ((*p
!= 0x6A) && (*p
!= 0x6B))) {
61 RSAerr(RSA_F_RSA_PADDING_CHECK_X931
, RSA_R_INVALID_HEADER
);
67 for (i
= 0; i
< j
; i
++) {
68 unsigned char c
= *p
++;
72 RSAerr(RSA_F_RSA_PADDING_CHECK_X931
, RSA_R_INVALID_PADDING
);
80 RSAerr(RSA_F_RSA_PADDING_CHECK_X931
, RSA_R_INVALID_PADDING
);
88 RSAerr(RSA_F_RSA_PADDING_CHECK_X931
, RSA_R_INVALID_TRAILER
);
92 memcpy(to
, p
, (unsigned int)j
);
97 /* Translate between X931 hash ids and NIDs */
99 int RSA_X931_hash_id(int nid
)