2 * Copyright 2005-2016 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/rand.h>
15 #include <openssl/objects.h>
17 int RSA_padding_add_X931(unsigned char *to, int tlen,
18 const unsigned char *from, int flen)
24 * Absolute minimum amount of padding is 1 header nibble, 1 padding
25 * nibble and 2 trailer bytes: but 1 hash if is already in 'from'.
31 RSAerr(RSA_F_RSA_PADDING_ADD_X931, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
35 p = (unsigned char *)to;
37 /* If no padding start and end nibbles are in one byte */
43 memset(p, 0xBB, j - 1);
48 memcpy(p, from, (unsigned int)flen);
54 int RSA_padding_check_X931(unsigned char *to, int tlen,
55 const unsigned char *from, int flen, int num)
58 const unsigned char *p;
61 if ((num != flen) || ((*p != 0x6A) && (*p != 0x6B))) {
62 RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_HEADER);
68 for (i = 0; i < j; i++) {
69 unsigned char c = *p++;
73 RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING);
81 RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING);
89 RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_TRAILER);
93 memcpy(to, p, (unsigned int)j);
98 /* Translate between X931 hash ids and NIDs */
100 int RSA_X931_hash_id(int nid)