GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/crypto/../../libssl/src/crypto/rsa/rsa_pk1.c Lines: 56 78 71.8 %
Date: 2016-12-06 Branches: 23 38 60.5 %

Line Branch Exec Source
1
/* $OpenBSD: rsa_pk1.c,v 1.14 2014/10/22 13:02:04 jsing Exp $ */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
59
#include <stdio.h>
60
#include <stdlib.h>
61
#include <string.h>
62
63
#include <openssl/bn.h>
64
#include <openssl/err.h>
65
#include <openssl/rsa.h>
66
67
int
68
RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
69
    const unsigned char *from, int flen)
70
2
{
71
	int j;
72
	unsigned char *p;
73
74
2
	if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
75
		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,
76
		    RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
77
		return 0;
78
	}
79
80
2
	p = (unsigned char *)to;
81
82
2
	*(p++) = 0;
83
2
	*(p++) = 1; /* Private Key BT (Block Type) */
84
85
	/* pad out with 0xff data */
86
2
	j = tlen - 3 - flen;
87
2
	memset(p, 0xff, j);
88
2
	p += j;
89
2
	*(p++) = '\0';
90
2
	memcpy(p, from, flen);
91
92
2
	return 1;
93
}
94
95
int
96
RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen,
97
    const unsigned char *from, int flen, int num)
98
8
{
99
	int i, j;
100
	const unsigned char *p;
101
102
8
	p = from;
103

8
	if (num != flen + 1 || *(p++) != 01) {
104
		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
105
		    RSA_R_BLOCK_TYPE_IS_NOT_01);
106
		return -1;
107
	}
108
109
	/* scan over padding data */
110
8
	j = flen - 1; /* one for type. */
111
1624
	for (i = 0; i < j; i++) {
112
1624
		if (*p != 0xff) {
113
			/* should decrypt to 0xff */
114
8
			if (*p == 0) {
115
8
				p++;
116
8
				break;
117
			} else {
118
				RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
119
				    RSA_R_BAD_FIXED_HEADER_DECRYPT);
120
				return -1;
121
			}
122
		}
123
1616
		p++;
124
	}
125
126
8
	if (i == j) {
127
		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
128
		    RSA_R_NULL_BEFORE_BLOCK_MISSING);
129
		return -1;
130
	}
131
132
8
	if (i < 8) {
133
		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
134
		    RSA_R_BAD_PAD_BYTE_COUNT);
135
		return -1;
136
	}
137
8
	i++; /* Skip over the '\0' */
138
8
	j -= i;
139
8
	if (j > tlen) {
140
		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
141
		    RSA_R_DATA_TOO_LARGE);
142
		return -1;
143
	}
144
8
	memcpy(to, p, j);
145
146
8
	return j;
147
}
148
149
int
150
RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
151
    const unsigned char *from, int flen)
152
1
{
153
	int i, j;
154
	unsigned char *p;
155
156
1
	if (flen > tlen - 11) {
157
		RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,
158
		    RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
159
		return 0;
160
	}
161
162
1
	p = (unsigned char *)to;
163
164
1
	*(p++) = 0;
165
1
	*(p++) = 2; /* Public Key BT (Block Type) */
166
167
	/* pad out with non-zero random data */
168
1
	j = tlen - 3 - flen;
169
170
1
	arc4random_buf(p, j);
171
222
	for (i = 0; i < j; i++) {
172
222
		while (*p == '\0')
173
1
			arc4random_buf(p, 1);
174
221
		p++;
175
	}
176
177
1
	*(p++) = '\0';
178
179
1
	memcpy(p, from, flen);
180
1
	return 1;
181
}
182
183
int
184
RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
185
    const unsigned char *from, int flen, int num)
186
1
{
187
	int i, j;
188
	const unsigned char *p;
189
190
1
	p = from;
191

1
	if (num != flen + 1 || *(p++) != 02) {
192
		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
193
		    RSA_R_BLOCK_TYPE_IS_NOT_02);
194
		return -1;
195
	}
196
197
	/* scan over padding data */
198
1
	j = flen - 1; /* one for type. */
199
222
	for (i = 0; i < j; i++)
200
222
		if (*(p++) == 0)
201
1
			break;
202
203
1
	if (i == j) {
204
		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
205
		    RSA_R_NULL_BEFORE_BLOCK_MISSING);
206
		return -1;
207
	}
208
209
1
	if (i < 8) {
210
		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
211
		    RSA_R_BAD_PAD_BYTE_COUNT);
212
		return -1;
213
	}
214
1
	i++; /* Skip over the '\0' */
215
1
	j -= i;
216
1
	if (j > tlen) {
217
		RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
218
		    RSA_R_DATA_TOO_LARGE);
219
		return -1;
220
	}
221
1
	memcpy(to, p, j);
222
223
1
	return j;
224
}