rsa_ssl.c revision 344604
1/* crypto/rsa/rsa_ssl.c */ 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 "cryptlib.h" 61#include <openssl/bn.h> 62#include <openssl/rsa.h> 63#include <openssl/rand.h> 64#include "constant_time_locl.h" 65 66int RSA_padding_add_SSLv23(unsigned char *to, int tlen, 67 const unsigned char *from, int flen) 68{ 69 int i, j; 70 unsigned char *p; 71 72 if (flen > (tlen - 11)) { 73 RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23, 74 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); 75 return (0); 76 } 77 78 p = (unsigned char *)to; 79 80 *(p++) = 0; 81 *(p++) = 2; /* Public Key BT (Block Type) */ 82 83 /* pad out with non-zero random data */ 84 j = tlen - 3 - 8 - flen; 85 86 if (RAND_bytes(p, j) <= 0) 87 return (0); 88 for (i = 0; i < j; i++) { 89 if (*p == '\0') 90 do { 91 if (RAND_bytes(p, 1) <= 0) 92 return (0); 93 } while (*p == '\0'); 94 p++; 95 } 96 97 memset(p, 3, 8); 98 p += 8; 99 *(p++) = '\0'; 100 101 memcpy(p, from, (unsigned int)flen); 102 return (1); 103} 104 105/* 106 * Copy of RSA_padding_check_PKCS1_type_2 with a twist that rejects padding 107 * if nul delimiter is preceded by 8 consecutive 0x03 bytes. It also 108 * preserves error code reporting for backward compatibility. 109 */ 110int RSA_padding_check_SSLv23(unsigned char *to, int tlen, 111 const unsigned char *from, int flen, int num) 112{ 113 int i; 114 /* |em| is the encoded message, zero-padded to exactly |num| bytes */ 115 unsigned char *em = NULL; 116 unsigned int good, found_zero_byte, mask, threes_in_row; 117 int zero_index = 0, msg_index, mlen = -1, err; 118 119 if (flen < 10) { 120 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_SMALL); 121 return (-1); 122 } 123 124 em = OPENSSL_malloc(num); 125 if (em == NULL) { 126 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, ERR_R_MALLOC_FAILURE); 127 return -1; 128 } 129 /* 130 * Caller is encouraged to pass zero-padded message created with 131 * BN_bn2binpad. Trouble is that since we can't read out of |from|'s 132 * bounds, it's impossible to have an invariant memory access pattern 133 * in case |from| was not zero-padded in advance. 134 */ 135 for (from += flen, em += num, i = 0; i < num; i++) { 136 mask = ~constant_time_is_zero(flen); 137 flen -= 1 & mask; 138 from -= 1 & mask; 139 *--em = *from & mask; 140 } 141 from = em; 142 143 good = constant_time_is_zero(from[0]); 144 good &= constant_time_eq(from[1], 2); 145 err = constant_time_select_int(good, 0, RSA_R_BLOCK_TYPE_IS_NOT_02); 146 mask = ~good; 147 148 /* scan over padding data */ 149 found_zero_byte = 0; 150 threes_in_row = 0; 151 for (i = 2; i < num; i++) { 152 unsigned int equals0 = constant_time_is_zero(from[i]); 153 154 zero_index = constant_time_select_int(~found_zero_byte & equals0, 155 i, zero_index); 156 found_zero_byte |= equals0; 157 158 threes_in_row += 1 & ~found_zero_byte; 159 threes_in_row &= found_zero_byte | constant_time_eq(from[i], 3); 160 } 161 162 /* 163 * PS must be at least 8 bytes long, and it starts two bytes into |from|. 164 * If we never found a 0-byte, then |zero_index| is 0 and the check 165 * also fails. 166 */ 167 good &= constant_time_ge(zero_index, 2 + 8); 168 err = constant_time_select_int(mask | good, err, 169 RSA_R_NULL_BEFORE_BLOCK_MISSING); 170 mask = ~good; 171 172 good &= constant_time_lt(threes_in_row, 8); 173 err = constant_time_select_int(mask | good, err, 174 RSA_R_SSLV3_ROLLBACK_ATTACK); 175 mask = ~good; 176 177 /* 178 * Skip the zero byte. This is incorrect if we never found a zero-byte 179 * but in this case we also do not copy the message out. 180 */ 181 msg_index = zero_index + 1; 182 mlen = num - msg_index; 183 184 /* 185 * For good measure, do this check in constant time as well. 186 */ 187 good &= constant_time_ge(tlen, mlen); 188 err = constant_time_select_int(mask | good, err, RSA_R_DATA_TOO_LARGE); 189 190 /* 191 * Even though we can't fake result's length, we can pretend copying 192 * |tlen| bytes where |mlen| bytes would be real. Last |tlen| of |num| 193 * bytes are viewed as circular buffer with start at |tlen|-|mlen'|, 194 * where |mlen'| is "saturated" |mlen| value. Deducing information 195 * about failure or |mlen| would take attacker's ability to observe 196 * memory access pattern with byte granularity *as it occurs*. It 197 * should be noted that failure is indistinguishable from normal 198 * operation if |tlen| is fixed by protocol. 199 */ 200 tlen = constant_time_select_int(constant_time_lt(num, tlen), num, tlen); 201 msg_index = constant_time_select_int(good, msg_index, num - tlen); 202 mlen = num - msg_index; 203 for (from += msg_index, mask = good, i = 0; i < tlen; i++) { 204 unsigned int equals = constant_time_eq(i, mlen); 205 206 from -= tlen & equals; /* if (i == mlen) rewind */ 207 mask &= mask ^ equals; /* if (i == mlen) mask = 0 */ 208 to[i] = constant_time_select_8(mask, from[i], to[i]); 209 } 210 211 OPENSSL_cleanse(em, num); 212 OPENSSL_free(em); 213 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, err); 214 err_clear_last_constant_time(1 & good); 215 216 return constant_time_select_int(good, mlen, -1); 217} 218