| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/rsa.h> |
| 16 |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <gtest/gtest.h> |
| 21 |
| 22 | #include <openssl/bn.h> |
| 23 | #include <openssl/bytestring.h> |
| 24 | #include <openssl/crypto.h> |
| 25 | #include <openssl/digest.h> |
| 26 | #include <openssl/err.h> |
| 27 | #include <openssl/nid.h> |
| 28 |
| 29 | #include "../fipsmodule/bn/internal.h" |
| 30 | #include "../fipsmodule/rsa/internal.h" |
| 31 | #include "../internal.h" |
| 32 | #include "../test/test_util.h" |
| 33 |
| 34 | #if defined(OPENSSL_THREADS) |
| 35 | #include <thread> |
| 36 | #include <vector> |
| 37 | #endif |
| 38 |
| 39 |
| 40 | namespace { |
| 41 |
| 42 | // kPlaintext is a sample plaintext. |
| 43 | static const uint8_t kPlaintext[] = {0x54, 0x85, 0x9b, 0x34, |
| 44 | 0x2c, 0x49, 0xea, 0x2a}; |
| 45 |
| 46 | // kKey1 is a DER-encoded 1024-bit RSAPrivateKey with e = 65537. |
| 47 | static const uint8_t kKey1[] = { |
| 48 | 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xa1, |
| 49 | 0x71, 0x90, 0x77, 0x86, 0x8a, 0xc7, 0xb8, 0xfc, 0x2a, 0x45, 0x82, 0x6d, |
| 50 | 0xee, 0xeb, 0x35, 0x3a, 0x18, 0x3f, 0xb6, 0xb0, 0x1e, 0xb1, 0xd3, 0x09, |
| 51 | 0x6b, 0x05, 0x4d, 0xec, 0x1c, 0x37, 0x6f, 0x09, 0x31, 0x32, 0xda, 0x21, |
| 52 | 0x8a, 0x49, 0x0e, 0x16, 0x28, 0xed, 0x9a, 0x30, 0xf3, 0x14, 0x53, 0xfd, |
| 53 | 0x5b, 0xb0, 0xf6, 0x4a, 0x5d, 0x52, 0xe1, 0xda, 0xe1, 0x40, 0x6e, 0x65, |
| 54 | 0xbf, 0xca, 0x45, 0xd9, 0x62, 0x96, 0x4a, 0x1e, 0x11, 0xc4, 0x61, 0x83, |
| 55 | 0x1f, 0x58, 0x8d, 0x5e, 0xd0, 0x12, 0xaf, 0xa5, 0xec, 0x9b, 0x97, 0x2f, |
| 56 | 0x6c, 0xb2, 0x82, 0x4a, 0x73, 0xd0, 0xd3, 0x9a, 0xc9, 0x69, 0x6b, 0x24, |
| 57 | 0x3c, 0x82, 0x6f, 0xee, 0x4d, 0x0c, 0x7e, 0xdf, 0xd7, 0xae, 0xea, 0x3a, |
| 58 | 0xeb, 0x04, 0x27, 0x8d, 0x43, 0x81, 0x59, 0xa7, 0x90, 0x56, 0xc1, 0x69, |
| 59 | 0x42, 0xb3, 0xaf, 0x1c, 0x8d, 0x4e, 0xbf, 0x02, 0x03, 0x01, 0x00, 0x01, |
| 60 | 0x02, 0x81, 0x80, 0x60, 0x82, 0xcd, 0x44, 0x46, 0xcf, 0xeb, 0xf9, 0x6f, |
| 61 | 0xf5, 0xad, 0x3b, 0xfd, 0x90, 0x18, 0x57, 0xe7, 0x74, 0xdb, 0x91, 0xd0, |
| 62 | 0xd3, 0x68, 0xa6, 0xaa, 0x38, 0xaa, 0x21, 0x1d, 0x06, 0xf9, 0x34, 0x8d, |
| 63 | 0xa0, 0x35, 0xb0, 0x24, 0xe0, 0xd0, 0x2f, 0x75, 0x9b, 0xdd, 0xfe, 0x91, |
| 64 | 0x48, 0x9f, 0x5c, 0x5e, 0x57, 0x54, 0x00, 0xc8, 0x0f, 0xe6, 0x1e, 0x52, |
| 65 | 0x84, 0xd9, 0xc9, 0xa5, 0x55, 0xf4, 0x0a, 0xbe, 0x88, 0x46, 0x7a, 0xfb, |
| 66 | 0x18, 0x37, 0x8e, 0xe6, 0x6e, 0xa2, 0x5f, 0x80, 0x48, 0x34, 0x3f, 0x5c, |
| 67 | 0xbe, 0x0e, 0x1e, 0xe8, 0x2f, 0x50, 0xba, 0x14, 0x96, 0x3c, 0xea, 0xfb, |
| 68 | 0xd2, 0x49, 0x33, 0xdc, 0x12, 0xb8, 0xa7, 0x8a, 0xb5, 0x27, 0xf9, 0x00, |
| 69 | 0x4b, 0xf5, 0xd2, 0x2a, 0xd0, 0x2c, 0x1d, 0x9b, 0xd5, 0x6c, 0x3e, 0x4b, |
| 70 | 0xb9, 0x7e, 0x39, 0xf7, 0x3e, 0x39, 0xc9, 0x47, 0x5e, 0xbe, 0x91, 0x02, |
| 71 | 0x41, 0x00, 0xcd, 0x33, 0xcf, 0x37, 0x01, 0xd7, 0x59, 0xcc, 0xbe, 0xa0, |
| 72 | 0x1c, 0xb9, 0xf5, 0xe7, 0x44, 0x9f, 0x62, 0x91, 0xa7, 0xa7, 0x7b, 0x0c, |
| 73 | 0x52, 0xcd, 0x7e, 0xe6, 0x31, 0x11, 0x8b, 0xd8, 0x2c, 0x8a, 0x63, 0xe1, |
| 74 | 0x07, 0xc9, 0xcb, 0xce, 0x01, 0x45, 0x63, 0xf5, 0x5d, 0x44, 0xfb, 0xeb, |
| 75 | 0x8d, 0x74, 0x16, 0x20, 0x7d, 0x3b, 0xb4, 0xa1, 0x61, 0xb0, 0xa8, 0x29, |
| 76 | 0x51, 0xc9, 0xef, 0xb6, 0xa1, 0xd5, 0x02, 0x41, 0x00, 0xc9, 0x68, 0xa6, |
| 77 | 0xd3, 0x88, 0xd5, 0x49, 0x9d, 0x6b, 0x44, 0x96, 0xfd, 0xbf, 0x66, 0x27, |
| 78 | 0xb4, 0x1f, 0x90, 0x76, 0x86, 0x2f, 0xe2, 0xce, 0x20, 0x5d, 0xee, 0x9b, |
| 79 | 0xeb, 0xc4, 0xb4, 0x62, 0x47, 0x79, 0x99, 0xb1, 0x99, 0xbc, 0xa2, 0xa6, |
| 80 | 0xb6, 0x96, 0x64, 0xd5, 0x77, 0x9b, 0x45, 0xd4, 0xf0, 0x99, 0xb5, 0x9e, |
| 81 | 0x61, 0x4d, 0xf5, 0x12, 0xdd, 0x84, 0x14, 0xaf, 0x1e, 0xdd, 0x83, 0x24, |
| 82 | 0x43, 0x02, 0x40, 0x60, 0x29, 0x7f, 0x59, 0xcf, 0xcb, 0x13, 0x92, 0x17, |
| 83 | 0x63, 0x01, 0x13, 0x44, 0x61, 0x74, 0x8f, 0x1c, 0xaa, 0x15, 0x5f, 0x2f, |
| 84 | 0x12, 0xbf, 0x5a, 0xfd, 0xb4, 0xf2, 0x19, 0xbe, 0xe7, 0x37, 0x38, 0x43, |
| 85 | 0x46, 0x19, 0x58, 0x3f, 0xe1, 0xf2, 0x46, 0x8a, 0x69, 0x59, 0xa4, 0x12, |
| 86 | 0x4a, 0x78, 0xa7, 0x86, 0x17, 0x03, 0x99, 0x0f, 0x34, 0xf1, 0x8a, 0xcf, |
| 87 | 0xc3, 0x4d, 0x48, 0xcc, 0xc5, 0x51, 0x61, 0x02, 0x41, 0x00, 0xc2, 0x12, |
| 88 | 0xb3, 0x5d, 0xf5, 0xe5, 0xff, 0xcf, 0x4e, 0x43, 0x83, 0x72, 0xf2, 0xf1, |
| 89 | 0x4e, 0xa4, 0xc4, 0x1d, 0x81, 0xf7, 0xff, 0x40, 0x7e, 0xfa, 0xb5, 0x48, |
| 90 | 0x6c, 0xba, 0x1c, 0x8a, 0xec, 0x80, 0x8e, 0xed, 0xc8, 0x32, 0xa9, 0x8f, |
| 91 | 0xd9, 0x30, 0xeb, 0x6e, 0x32, 0x3b, 0xd4, 0x44, 0xcf, 0xd1, 0x1f, 0x6b, |
| 92 | 0xe0, 0x37, 0x46, 0xd5, 0x35, 0xde, 0x79, 0x9d, 0x2c, 0xb9, 0x83, 0x1d, |
| 93 | 0x10, 0xdd, 0x02, 0x40, 0x0f, 0x14, 0x95, 0x96, 0xa0, 0xe2, 0x6c, 0xd4, |
| 94 | 0x88, 0xa7, 0x0b, 0x82, 0x14, 0x10, 0xad, 0x26, 0x0d, 0xe4, 0xa1, 0x5e, |
| 95 | 0x01, 0x3d, 0x21, 0xd2, 0xfb, 0x0e, 0xf9, 0x58, 0xa5, 0xca, 0x1e, 0x21, |
| 96 | 0xb3, 0xf5, 0x9a, 0x6c, 0x3d, 0x5a, 0x72, 0xb1, 0x2d, 0xfe, 0xac, 0x09, |
| 97 | 0x4f, 0xdd, 0xe5, 0x44, 0xd1, 0x4e, 0xf8, 0x59, 0x85, 0x3a, 0x65, 0xe2, |
| 98 | 0xcd, 0xbc, 0x27, 0x1d, 0x9b, 0x48, 0x9f, 0xb9}; |
| 99 |
| 100 | static const uint8_t kKey1Public[] = { |
| 101 | 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xa1, 0x71, 0x90, 0x77, 0x86, |
| 102 | 0x8a, 0xc7, 0xb8, 0xfc, 0x2a, 0x45, 0x82, 0x6d, 0xee, 0xeb, 0x35, 0x3a, |
| 103 | 0x18, 0x3f, 0xb6, 0xb0, 0x1e, 0xb1, 0xd3, 0x09, 0x6b, 0x05, 0x4d, 0xec, |
| 104 | 0x1c, 0x37, 0x6f, 0x09, 0x31, 0x32, 0xda, 0x21, 0x8a, 0x49, 0x0e, 0x16, |
| 105 | 0x28, 0xed, 0x9a, 0x30, 0xf3, 0x14, 0x53, 0xfd, 0x5b, 0xb0, 0xf6, 0x4a, |
| 106 | 0x5d, 0x52, 0xe1, 0xda, 0xe1, 0x40, 0x6e, 0x65, 0xbf, 0xca, 0x45, 0xd9, |
| 107 | 0x62, 0x96, 0x4a, 0x1e, 0x11, 0xc4, 0x61, 0x83, 0x1f, 0x58, 0x8d, 0x5e, |
| 108 | 0xd0, 0x12, 0xaf, 0xa5, 0xec, 0x9b, 0x97, 0x2f, 0x6c, 0xb2, 0x82, 0x4a, |
| 109 | 0x73, 0xd0, 0xd3, 0x9a, 0xc9, 0x69, 0x6b, 0x24, 0x3c, 0x82, 0x6f, 0xee, |
| 110 | 0x4d, 0x0c, 0x7e, 0xdf, 0xd7, 0xae, 0xea, 0x3a, 0xeb, 0x04, 0x27, 0x8d, |
| 111 | 0x43, 0x81, 0x59, 0xa7, 0x90, 0x56, 0xc1, 0x69, 0x42, 0xb3, 0xaf, 0x1c, |
| 112 | 0x8d, 0x4e, 0xbf, 0x02, 0x03, 0x01, 0x00, 0x01}; |
| 113 |
| 114 | // kOAEPCiphertext1 is a sample encryption of |kPlaintext| with |kKey1| using |
| 115 | // RSA OAEP, SHA-1, and no label. It was generated with: |
| 116 | // |
| 117 | // clang-format off |
| 118 | // openssl pkeyutl -encrypt -inkey key1.pem -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha1 -in plaintext | xxd -i |
| 119 | // clang-format on |
| 120 | static const uint8_t kOAEPCiphertext1[] = { |
| 121 | 0x53, 0xa3, 0x0e, 0xc7, 0x95, 0x52, 0x80, 0x6f, 0x9d, 0x4c, 0xd2, 0x87, |
| 122 | 0xa0, 0x5d, 0x4b, 0xee, 0x78, 0x7d, 0xaa, 0x2a, 0xf6, 0x48, 0x5e, 0x83, |
| 123 | 0xb4, 0xc7, 0xd5, 0x82, 0xa7, 0xe9, 0x3e, 0x4c, 0x54, 0xa0, 0x1e, 0x5f, |
| 124 | 0x49, 0x17, 0x26, 0x36, 0x37, 0x22, 0x09, 0xe8, 0xde, 0x8d, 0x51, 0x49, |
| 125 | 0x0b, 0x34, 0x27, 0x30, 0x1f, 0x12, 0xbd, 0xf4, 0x2f, 0xed, 0x9e, 0xcf, |
| 126 | 0x9e, 0xda, 0x41, 0x69, 0xf7, 0x86, 0x64, 0xfc, 0x7d, 0x0a, 0x4b, 0x28, |
| 127 | 0x28, 0x9e, 0x38, 0x97, 0xae, 0x01, 0x86, 0xbe, 0xb0, 0x92, 0xfd, 0xa0, |
| 128 | 0x5c, 0x75, 0xaf, 0x01, 0x88, 0xf7, 0x24, 0xa8, 0xcd, 0x44, 0x3c, 0x13, |
| 129 | 0x42, 0xf7, 0x03, 0x9b, 0x88, 0x1c, 0x65, 0xf4, 0x83, 0xba, 0xc8, 0x10, |
| 130 | 0xe2, 0x9a, 0x37, 0x79, 0x77, 0xef, 0x20, 0x3d, 0x2d, 0xa4, 0xe5, 0x3e, |
| 131 | 0xd4, 0x18, 0x3e, 0x12, 0xc1, 0xc3, 0x68, 0x65}; |
| 132 |
| 133 | // kKey2 is a 2048-bit RSA private key, with e = 3. |
| 134 | static const uint8_t kKey2[] = { |
| 135 | 0x30, 0x82, 0x04, 0xa1, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, |
| 136 | 0x93, 0x3a, 0x4f, 0xc9, 0x6a, 0x0a, 0x6b, 0x28, 0x04, 0xfa, 0xb7, 0x05, |
| 137 | 0x56, 0xdf, 0xa0, 0xaa, 0x4f, 0xaa, 0xab, 0x94, 0xa0, 0xa9, 0x25, 0xef, |
| 138 | 0xc5, 0x96, 0xd2, 0xd4, 0x66, 0x16, 0x62, 0x2c, 0x13, 0x7b, 0x91, 0xd0, |
| 139 | 0x36, 0x0a, 0x10, 0x11, 0x6d, 0x7a, 0x91, 0xb6, 0xe4, 0x74, 0x57, 0xc1, |
| 140 | 0x3d, 0x7a, 0xbe, 0x24, 0x05, 0x3a, 0x04, 0x0b, 0x73, 0x91, 0x53, 0xb1, |
| 141 | 0x74, 0x10, 0xe1, 0x87, 0xdc, 0x91, 0x28, 0x9c, 0x1e, 0xe5, 0xf2, 0xb9, |
| 142 | 0xfc, 0xa2, 0x48, 0x34, 0xb6, 0x78, 0xed, 0x6d, 0x95, 0xfb, 0xf2, 0xc0, |
| 143 | 0x4e, 0x1c, 0xa4, 0x15, 0x00, 0x3c, 0x8a, 0x68, 0x2b, 0xd6, 0xce, 0xd5, |
| 144 | 0xb3, 0x9f, 0x66, 0x02, 0xa7, 0x0d, 0x08, 0xa3, 0x23, 0x9b, 0xe5, 0x36, |
| 145 | 0x96, 0x13, 0x22, 0xf9, 0x69, 0xa6, 0x87, 0x88, 0x9b, 0x85, 0x3f, 0x83, |
| 146 | 0x9c, 0xab, 0x1a, 0x1b, 0x6d, 0x8d, 0x16, 0xf4, 0x5e, 0xbd, 0xee, 0x4b, |
| 147 | 0x59, 0x56, 0xf8, 0x9d, 0x58, 0xcd, 0xd2, 0x83, 0x85, 0x59, 0x43, 0x84, |
| 148 | 0x63, 0x4f, 0xe6, 0x1a, 0x86, 0x66, 0x0d, 0xb5, 0xa0, 0x87, 0x89, 0xb6, |
| 149 | 0x13, 0x82, 0x43, 0xda, 0x34, 0x92, 0x3b, 0x68, 0xc4, 0x95, 0x71, 0x2f, |
| 150 | 0x15, 0xc2, 0xe0, 0x43, 0x67, 0x3c, 0x08, 0x00, 0x36, 0x10, 0xc3, 0xb4, |
| 151 | 0x46, 0x4c, 0x4e, 0x6e, 0xf5, 0x44, 0xa9, 0x04, 0x44, 0x9d, 0xce, 0xc7, |
| 152 | 0x05, 0x79, 0xee, 0x11, 0xcf, 0xaf, 0x2c, 0xd7, 0x9a, 0x32, 0xd3, 0xa5, |
| 153 | 0x30, 0xd4, 0x3a, 0x78, 0x43, 0x37, 0x74, 0x22, 0x90, 0x24, 0x04, 0x11, |
| 154 | 0xd7, 0x95, 0x08, 0x52, 0xa4, 0x71, 0x41, 0x68, 0x94, 0xb0, 0xa0, 0xc3, |
| 155 | 0xec, 0x4e, 0xd2, 0xc4, 0x30, 0x71, 0x98, 0x64, 0x9c, 0xe3, 0x7c, 0x76, |
| 156 | 0xef, 0x33, 0xa3, 0x2b, 0xb1, 0x87, 0x63, 0xd2, 0x5c, 0x09, 0xfc, 0x90, |
| 157 | 0x2d, 0x92, 0xf4, 0x57, 0x02, 0x01, 0x03, 0x02, 0x82, 0x01, 0x00, 0x62, |
| 158 | 0x26, 0xdf, 0xdb, 0x9c, 0x06, 0xf2, 0x1a, 0xad, 0xfc, 0x7a, 0x03, 0x8f, |
| 159 | 0x3f, 0xc0, 0x71, 0x8a, 0x71, 0xc7, 0xb8, 0x6b, 0x1b, 0x6e, 0x9f, 0xd9, |
| 160 | 0x0f, 0x37, 0x38, 0x44, 0x0e, 0xec, 0x1d, 0x62, 0x52, 0x61, 0x35, 0x79, |
| 161 | 0x5c, 0x0a, 0xb6, 0x48, 0xfc, 0x61, 0x24, 0x98, 0x4d, 0x8f, 0xd6, 0x28, |
| 162 | 0xfc, 0x7e, 0xc2, 0xae, 0x26, 0xad, 0x5c, 0xf7, 0xb6, 0x37, 0xcb, 0xa2, |
| 163 | 0xb5, 0xeb, 0xaf, 0xe8, 0x60, 0xc5, 0xbd, 0x69, 0xee, 0xa1, 0xd1, 0x53, |
| 164 | 0x16, 0xda, 0xcd, 0xce, 0xfb, 0x48, 0xf3, 0xb9, 0x52, 0xa1, 0xd5, 0x89, |
| 165 | 0x68, 0x6d, 0x63, 0x55, 0x7d, 0xb1, 0x9a, 0xc7, 0xe4, 0x89, 0xe3, 0xcd, |
| 166 | 0x14, 0xee, 0xac, 0x6f, 0x5e, 0x05, 0xc2, 0x17, 0xbd, 0x43, 0x79, 0xb9, |
| 167 | 0x62, 0x17, 0x50, 0xf1, 0x19, 0xaf, 0xb0, 0x67, 0xae, 0x2a, 0x57, 0xbd, |
| 168 | 0xc7, 0x66, 0xbc, 0xf3, 0xb3, 0x64, 0xa1, 0xe3, 0x16, 0x74, 0x9e, 0xea, |
| 169 | 0x02, 0x5c, 0xab, 0x94, 0xd8, 0x97, 0x02, 0x42, 0x0c, 0x2c, 0xba, 0x54, |
| 170 | 0xb9, 0xaf, 0xe0, 0x45, 0x93, 0xad, 0x7f, 0xb3, 0x10, 0x6a, 0x96, 0x50, |
| 171 | 0x4b, 0xaf, 0xcf, 0xc8, 0x27, 0x62, 0x2d, 0x83, 0xe9, 0x26, 0xc6, 0x94, |
| 172 | 0xc1, 0xef, 0x5c, 0x8e, 0x06, 0x42, 0x53, 0xe5, 0x56, 0xaf, 0xc2, 0x99, |
| 173 | 0x01, 0xaa, 0x9a, 0x71, 0xbc, 0xe8, 0x21, 0x33, 0x2a, 0x2d, 0xa3, 0x36, |
| 174 | 0xac, 0x1b, 0x86, 0x19, 0xf8, 0xcd, 0x1f, 0x80, 0xa4, 0x26, 0x98, 0xb8, |
| 175 | 0x9f, 0x62, 0x62, 0xd5, 0x1a, 0x7f, 0xee, 0xdb, 0xdf, 0x81, 0xd3, 0x21, |
| 176 | 0xdb, 0x33, 0x92, 0xee, 0xff, 0xe2, 0x2f, 0x32, 0x77, 0x73, 0x6a, 0x58, |
| 177 | 0xab, 0x21, 0xf3, 0xe3, 0xe1, 0xbc, 0x4f, 0x12, 0x72, 0xa6, 0xb5, 0xc2, |
| 178 | 0xfb, 0x27, 0x9e, 0xc8, 0xca, 0xab, 0x64, 0xa0, 0x87, 0x07, 0x9d, 0xef, |
| 179 | 0xca, 0x0f, 0xdb, 0x02, 0x81, 0x81, 0x00, 0xe6, 0xd3, 0x4d, 0xc0, 0xa1, |
| 180 | 0x91, 0x0e, 0x62, 0xfd, 0xb0, 0xdd, 0xc6, 0x30, 0xb8, 0x8c, 0xcb, 0x14, |
| 181 | 0xc1, 0x4b, 0x69, 0x30, 0xdd, 0xcd, 0x86, 0x67, 0xcb, 0x37, 0x14, 0xc5, |
| 182 | 0x03, 0xd2, 0xb4, 0x69, 0xab, 0x3d, 0xe5, 0x16, 0x81, 0x0f, 0xe5, 0x50, |
| 183 | 0xf4, 0x18, 0xb1, 0xec, 0xbc, 0x71, 0xe9, 0x80, 0x99, 0x06, 0xe4, 0xa3, |
| 184 | 0xfe, 0x44, 0x84, 0x4a, 0x2d, 0x1e, 0x07, 0x7f, 0x22, 0x70, 0x6d, 0x4f, |
| 185 | 0xd4, 0x93, 0x0b, 0x8b, 0x99, 0xce, 0x1e, 0xab, 0xcd, 0x4c, 0xd2, 0xd3, |
| 186 | 0x10, 0x47, 0x5c, 0x09, 0x9f, 0x6d, 0x82, 0xc0, 0x08, 0x75, 0xe3, 0x3d, |
| 187 | 0x83, 0xc2, 0x19, 0x50, 0x29, 0xec, 0x1f, 0x84, 0x29, 0xcc, 0xf1, 0x56, |
| 188 | 0xee, 0xbd, 0x54, 0x5d, 0xe6, 0x19, 0xdf, 0x0d, 0x1c, 0xa4, 0xbb, 0x0a, |
| 189 | 0xfe, 0x84, 0x44, 0x29, 0x1d, 0xf9, 0x5c, 0x80, 0x96, 0x5b, 0x24, 0xb4, |
| 190 | 0xf7, 0x02, 0x1b, 0x02, 0x81, 0x81, 0x00, 0xa3, 0x48, 0xf1, 0x9c, 0x58, |
| 191 | 0xc2, 0x5f, 0x38, 0xfb, 0xd8, 0x12, 0x39, 0xf1, 0x8e, 0x73, 0xa1, 0xcf, |
| 192 | 0x78, 0x12, 0xe0, 0xed, 0x2a, 0xbb, 0xef, 0xac, 0x23, 0xb2, 0xbf, 0xd6, |
| 193 | 0x0c, 0xe9, 0x6e, 0x1e, 0xab, 0xea, 0x3f, 0x68, 0x36, 0xa7, 0x1f, 0xe5, |
| 194 | 0xab, 0xe0, 0x86, 0xa5, 0x76, 0x32, 0x98, 0xdd, 0x75, 0xb5, 0x2b, 0xbc, |
| 195 | 0xcb, 0x8a, 0x03, 0x00, 0x7c, 0x2e, 0xca, 0xf8, 0xbc, 0x19, 0xe4, 0xe3, |
| 196 | 0xa3, 0x31, 0xbd, 0x1d, 0x20, 0x2b, 0x09, 0xad, 0x6f, 0x4c, 0xed, 0x48, |
| 197 | 0xd4, 0xdf, 0x87, 0xf9, 0xf0, 0x46, 0xb9, 0x86, 0x4c, 0x4b, 0x71, 0xe7, |
| 198 | 0x48, 0x78, 0xdc, 0xed, 0xc7, 0x82, 0x02, 0x44, 0xd3, 0xa6, 0xb3, 0x10, |
| 199 | 0x5f, 0x62, 0x81, 0xfc, 0xb8, 0xe4, 0x0e, 0xf4, 0x1a, 0xdd, 0xab, 0x3f, |
| 200 | 0xbc, 0x63, 0x79, 0x5b, 0x39, 0x69, 0x5e, 0xea, 0xa9, 0x15, 0xfe, 0x90, |
| 201 | 0xec, 0xda, 0x75, 0x02, 0x81, 0x81, 0x00, 0x99, 0xe2, 0x33, 0xd5, 0xc1, |
| 202 | 0x0b, 0x5e, 0xec, 0xa9, 0x20, 0x93, 0xd9, 0x75, 0xd0, 0x5d, 0xdc, 0xb8, |
| 203 | 0x80, 0xdc, 0xf0, 0xcb, 0x3e, 0x89, 0x04, 0x45, 0x32, 0x24, 0xb8, 0x83, |
| 204 | 0x57, 0xe1, 0xcd, 0x9b, 0xc7, 0x7e, 0x98, 0xb9, 0xab, 0x5f, 0xee, 0x35, |
| 205 | 0xf8, 0x10, 0x76, 0x9d, 0xd2, 0xf6, 0x9b, 0xab, 0x10, 0xaf, 0x43, 0x17, |
| 206 | 0xfe, 0xd8, 0x58, 0x31, 0x73, 0x69, 0x5a, 0x54, 0xc1, 0xa0, 0x48, 0xdf, |
| 207 | 0xe3, 0x0c, 0xb2, 0x5d, 0x11, 0x34, 0x14, 0x72, 0x88, 0xdd, 0xe1, 0xe2, |
| 208 | 0x0a, 0xda, 0x3d, 0x5b, 0xbf, 0x9e, 0x57, 0x2a, 0xb0, 0x4e, 0x97, 0x7e, |
| 209 | 0x57, 0xd6, 0xbb, 0x8a, 0xc6, 0x9d, 0x6a, 0x58, 0x1b, 0xdd, 0xf6, 0x39, |
| 210 | 0xf4, 0x7e, 0x38, 0x3e, 0x99, 0x66, 0x94, 0xb3, 0x68, 0x6d, 0xd2, 0x07, |
| 211 | 0x54, 0x58, 0x2d, 0x70, 0xbe, 0xa6, 0x3d, 0xab, 0x0e, 0xe7, 0x6d, 0xcd, |
| 212 | 0xfa, 0x01, 0x67, 0x02, 0x81, 0x80, 0x6c, 0xdb, 0x4b, 0xbd, 0x90, 0x81, |
| 213 | 0x94, 0xd0, 0xa7, 0xe5, 0x61, 0x7b, 0xf6, 0x5e, 0xf7, 0xc1, 0x34, 0xfa, |
| 214 | 0xb7, 0x40, 0x9e, 0x1c, 0x7d, 0x4a, 0x72, 0xc2, 0x77, 0x2a, 0x8e, 0xb3, |
| 215 | 0x46, 0x49, 0x69, 0xc7, 0xf1, 0x7f, 0x9a, 0xcf, 0x1a, 0x15, 0x43, 0xc7, |
| 216 | 0xeb, 0x04, 0x6e, 0x4e, 0xcc, 0x65, 0xe8, 0xf9, 0x23, 0x72, 0x7d, 0xdd, |
| 217 | 0x06, 0xac, 0xaa, 0xfd, 0x74, 0x87, 0x50, 0x7d, 0x66, 0x98, 0x97, 0xc2, |
| 218 | 0x21, 0x28, 0xbe, 0x15, 0x72, 0x06, 0x73, 0x9f, 0x88, 0x9e, 0x30, 0x8d, |
| 219 | 0xea, 0x5a, 0xa6, 0xa0, 0x2f, 0x26, 0x59, 0x88, 0x32, 0x4b, 0xef, 0x85, |
| 220 | 0xa5, 0xe8, 0x9e, 0x85, 0x01, 0x56, 0xd8, 0x8d, 0x19, 0xcc, 0xb5, 0x94, |
| 221 | 0xec, 0x56, 0xa8, 0x7b, 0x42, 0xb4, 0xa2, 0xbc, 0x93, 0xc7, 0x7f, 0xd2, |
| 222 | 0xec, 0xfb, 0x92, 0x26, 0x46, 0x3f, 0x47, 0x1b, 0x63, 0xff, 0x0b, 0x48, |
| 223 | 0x91, 0xa3, 0x02, 0x81, 0x80, 0x2c, 0x4a, 0xb9, 0xa4, 0x46, 0x7b, 0xff, |
| 224 | 0x50, 0x7e, 0xbf, 0x60, 0x47, 0x3b, 0x2b, 0x66, 0x82, 0xdc, 0x0e, 0x53, |
| 225 | 0x65, 0x71, 0xe9, 0xda, 0x2a, 0xb8, 0x32, 0x93, 0x42, 0xb7, 0xff, 0xea, |
| 226 | 0x67, 0x66, 0xf1, 0xbc, 0x87, 0x28, 0x65, 0x29, 0x79, 0xca, 0xab, 0x93, |
| 227 | 0x56, 0xda, 0x95, 0xc1, 0x26, 0x44, 0x3d, 0x27, 0xc1, 0x91, 0xc6, 0x9b, |
| 228 | 0xd9, 0xec, 0x9d, 0xb7, 0x49, 0xe7, 0x16, 0xee, 0x99, 0x87, 0x50, 0x95, |
| 229 | 0x81, 0xd4, 0x5c, 0x5b, 0x5a, 0x5d, 0x0a, 0x43, 0xa5, 0xa7, 0x8f, 0x5a, |
| 230 | 0x80, 0x49, 0xa0, 0xb7, 0x10, 0x85, 0xc7, 0xf4, 0x42, 0x34, 0x86, 0xb6, |
| 231 | 0x5f, 0x3f, 0x88, 0x9e, 0xc7, 0xf5, 0x59, 0x29, 0x39, 0x68, 0x48, 0xf2, |
| 232 | 0xd7, 0x08, 0x5b, 0x92, 0x8e, 0x6b, 0xea, 0xa5, 0x63, 0x5f, 0xc0, 0xfb, |
| 233 | 0xe4, 0xe1, 0xb2, 0x7d, 0xb7, 0x40, 0xe9, 0x55, 0x06, 0xbf, 0x58, 0x25, |
| 234 | 0x6f}; |
| 235 |
| 236 | // kPKCS1Ciphertext2 is "hello world" encrypted with kKey2 and RSAES-PKCS1-v1_5. |
| 237 | static const uint8_t kPKCS1Ciphertext2[] = { |
| 238 | 0x63, 0x0a, 0x30, 0x45, 0x43, 0x11, 0x45, 0xb7, 0x99, 0x67, 0x90, 0x35, |
| 239 | 0x37, 0x27, 0xff, 0xbc, 0xe0, 0xbf, 0xa6, 0xd1, 0x47, 0x50, 0xbb, 0x6c, |
| 240 | 0x1c, 0xaa, 0x66, 0xf2, 0xff, 0x9d, 0x9a, 0xa6, 0xb4, 0x16, 0x63, 0xb0, |
| 241 | 0xa1, 0x7c, 0x7c, 0x0c, 0xef, 0xb3, 0x66, 0x52, 0x42, 0xd7, 0x5e, 0xf3, |
| 242 | 0xa4, 0x15, 0x33, 0x40, 0x43, 0xe8, 0xb1, 0xfc, 0xe0, 0x42, 0x83, 0x46, |
| 243 | 0x28, 0xce, 0xde, 0x7b, 0x01, 0xeb, 0x28, 0x92, 0x70, 0xdf, 0x8d, 0x54, |
| 244 | 0x9e, 0xed, 0x23, 0xb4, 0x78, 0xc3, 0xca, 0x85, 0x53, 0x48, 0xd6, 0x8a, |
| 245 | 0x87, 0xf7, 0x69, 0xcd, 0x82, 0x8c, 0x4f, 0x5c, 0x05, 0x55, 0xa6, 0x78, |
| 246 | 0x89, 0xab, 0x4c, 0xd8, 0xa9, 0xd6, 0xa5, 0xf4, 0x29, 0x4c, 0x23, 0xc8, |
| 247 | 0xcf, 0xf0, 0x4c, 0x64, 0x6b, 0x4e, 0x02, 0x17, 0x69, 0xd6, 0x47, 0x83, |
| 248 | 0x30, 0x43, 0x02, 0x29, 0xda, 0xda, 0x75, 0x3b, 0xd7, 0xa7, 0x2b, 0x31, |
| 249 | 0xb3, 0xe9, 0x71, 0xa4, 0x41, 0xf7, 0x26, 0x9b, 0xcd, 0x23, 0xfa, 0x45, |
| 250 | 0x3c, 0x9b, 0x7d, 0x28, 0xf7, 0xf9, 0x67, 0x04, 0xba, 0xfc, 0x46, 0x75, |
| 251 | 0x11, 0x3c, 0xd5, 0x27, 0x43, 0x53, 0xb1, 0xb6, 0x9e, 0x18, 0xeb, 0x11, |
| 252 | 0xb4, 0x25, 0x20, 0x30, 0x0b, 0xe0, 0x1c, 0x17, 0x36, 0x22, 0x10, 0x0f, |
| 253 | 0x99, 0xb5, 0x50, 0x14, 0x73, 0x07, 0xf0, 0x2f, 0x5d, 0x4c, 0xe3, 0xf2, |
| 254 | 0x86, 0xc2, 0x05, 0xc8, 0x38, 0xed, 0xeb, 0x2a, 0x4a, 0xab, 0x76, 0xe3, |
| 255 | 0x1a, 0x75, 0x44, 0xf7, 0x6e, 0x94, 0xdc, 0x25, 0x62, 0x7e, 0x31, 0xca, |
| 256 | 0xc2, 0x73, 0x51, 0xb5, 0x03, 0xfb, 0xf9, 0xf6, 0xb5, 0x8d, 0x4e, 0x6c, |
| 257 | 0x21, 0x0e, 0xf9, 0x97, 0x26, 0x57, 0xf3, 0x52, 0x72, 0x07, 0xf8, 0xb4, |
| 258 | 0xcd, 0xb4, 0x39, 0xcf, 0xbf, 0x78, 0xcc, 0xb6, 0x87, 0xf9, 0xb7, 0x8b, |
| 259 | 0x6a, 0xce, 0x9f, 0xc8}; |
| 260 |
| 261 | // kOAEPCiphertext2 is a sample encryption of |kPlaintext| with |kKey2| using |
| 262 | // RSA OAEP, SHA-1, and no label. It was generated with: |
| 263 | // |
| 264 | // clang-format off |
| 265 | // openssl pkeyutl -encrypt -inkey key2.pem -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha1 -in plaintext | xxd -i |
| 266 | // clang-format on |
| 267 | static const uint8_t kOAEPCiphertext2[] = { |
| 268 | 0x56, 0x10, 0x50, 0x5b, 0x9a, 0xa8, 0x2e, 0x3f, 0x24, 0x06, 0x5b, 0xd3, |
| 269 | 0x06, 0x03, 0xde, 0x18, 0x69, 0xb2, 0x1b, 0xec, 0x12, 0x14, 0x76, 0xb9, |
| 270 | 0x8c, 0x7b, 0xf8, 0x4a, 0xaf, 0x87, 0xa8, 0x83, 0x49, 0x1c, 0x5e, 0xb4, |
| 271 | 0xe5, 0x9f, 0xff, 0x00, 0xf2, 0xdd, 0x34, 0xf0, 0x10, 0x9f, 0xca, 0xc6, |
| 272 | 0x02, 0x54, 0x23, 0xb2, 0xc3, 0xdc, 0x74, 0xa8, 0x9f, 0xd2, 0xdc, 0x87, |
| 273 | 0x48, 0x2f, 0x02, 0x8b, 0xf1, 0x7a, 0x91, 0x8d, 0x2d, 0x77, 0x7f, 0x6f, |
| 274 | 0x8f, 0x19, 0xde, 0x90, 0x54, 0x0d, 0x1b, 0x7b, 0x96, 0x81, 0x84, 0xf9, |
| 275 | 0x03, 0x48, 0xef, 0xab, 0xe5, 0x07, 0xcd, 0x7f, 0x01, 0xeb, 0x86, 0x8d, |
| 276 | 0x7e, 0x7e, 0xf8, 0x2a, 0x50, 0x02, 0xcd, 0xcb, 0xa5, 0xfe, 0xc2, 0x35, |
| 277 | 0x1f, 0x82, 0xef, 0xb9, 0x1d, 0x98, 0xd5, 0x07, 0x94, 0x37, 0x08, 0x13, |
| 278 | 0x1c, 0xc8, 0x19, 0x06, 0x13, 0x2d, 0x1c, 0xb2, 0x50, 0x34, 0xad, 0x99, |
| 279 | 0x3c, 0xe6, 0xce, 0x4c, 0x88, 0x6d, 0x96, 0xc8, 0x85, 0xd1, 0x5e, 0xd5, |
| 280 | 0x77, 0x02, 0x0a, 0xa9, 0x2a, 0xf1, 0xa3, 0x4a, 0x04, 0x65, 0x87, 0x05, |
| 281 | 0x6b, 0x34, 0x65, 0x1c, 0xef, 0x64, 0x11, 0xee, 0x23, 0x7e, 0x36, 0x4f, |
| 282 | 0x4c, 0x5d, 0xb7, 0xd6, 0x79, 0x30, 0xec, 0xdf, 0xde, 0x35, 0x32, 0xd0, |
| 283 | 0xb0, 0x7e, 0x26, 0x1f, 0xea, 0xa2, 0x78, 0x98, 0x4b, 0x77, 0x9b, 0x03, |
| 284 | 0x75, 0x33, 0x08, 0x72, 0x91, 0x0b, 0x77, 0xc0, 0x6e, 0xe1, 0x0f, 0x14, |
| 285 | 0xf1, 0xf0, 0xb9, 0xe5, 0x5f, 0x08, 0xc2, 0x92, 0x79, 0x2e, 0x6f, 0xef, |
| 286 | 0x2a, 0x1b, 0x31, 0x64, 0x36, 0x67, 0xf8, 0x1d, 0xc8, 0xb7, 0xc3, 0x15, |
| 287 | 0x6c, 0xd8, 0x35, 0x34, 0x44, 0xb2, 0x91, 0xf2, 0x07, 0x86, 0xd6, 0xfa, |
| 288 | 0x42, 0x04, 0xae, 0xc5, 0x17, 0x14, 0x61, 0x6f, 0x12, 0x84, 0xb9, 0x99, |
| 289 | 0x47, 0xd1, 0xdc, 0x3c}; |
| 290 |
| 291 | // kKey3 is a DER-encoded RSAPrivateKey. It is a 1024-bit RSA private key with |
| 292 | // exponent 17. |
| 293 | static const uint8_t kKey3[] = { |
| 294 | 0x30, 0x82, 0x02, 0x5b, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xbb, |
| 295 | 0xf8, 0x2f, 0x09, 0x06, 0x82, 0xce, 0x9c, 0x23, 0x38, 0xac, 0x2b, 0x9d, |
| 296 | 0xa8, 0x71, 0xf7, 0x36, 0x8d, 0x07, 0xee, 0xd4, 0x10, 0x43, 0xa4, 0x40, |
| 297 | 0xd6, 0xb6, 0xf0, 0x74, 0x54, 0xf5, 0x1f, 0xb8, 0xdf, 0xba, 0xaf, 0x03, |
| 298 | 0x5c, 0x02, 0xab, 0x61, 0xea, 0x48, 0xce, 0xeb, 0x6f, 0xcd, 0x48, 0x76, |
| 299 | 0xed, 0x52, 0x0d, 0x60, 0xe1, 0xec, 0x46, 0x19, 0x71, 0x9d, 0x8a, 0x5b, |
| 300 | 0x8b, 0x80, 0x7f, 0xaf, 0xb8, 0xe0, 0xa3, 0xdf, 0xc7, 0x37, 0x72, 0x3e, |
| 301 | 0xe6, 0xb4, 0xb7, 0xd9, 0x3a, 0x25, 0x84, 0xee, 0x6a, 0x64, 0x9d, 0x06, |
| 302 | 0x09, 0x53, 0x74, 0x88, 0x34, 0xb2, 0x45, 0x45, 0x98, 0x39, 0x4e, 0xe0, |
| 303 | 0xaa, 0xb1, 0x2d, 0x7b, 0x61, 0xa5, 0x1f, 0x52, 0x7a, 0x9a, 0x41, 0xf6, |
| 304 | 0xc1, 0x68, 0x7f, 0xe2, 0x53, 0x72, 0x98, 0xca, 0x2a, 0x8f, 0x59, 0x46, |
| 305 | 0xf8, 0xe5, 0xfd, 0x09, 0x1d, 0xbd, 0xcb, 0x02, 0x01, 0x11, 0x02, 0x81, |
| 306 | 0x81, 0x00, 0xa5, 0xda, 0xfc, 0x53, 0x41, 0xfa, 0xf2, 0x89, 0xc4, 0xb9, |
| 307 | 0x88, 0xdb, 0x30, 0xc1, 0xcd, 0xf8, 0x3f, 0x31, 0x25, 0x1e, 0x06, 0x68, |
| 308 | 0xb4, 0x27, 0x84, 0x81, 0x38, 0x01, 0x57, 0x96, 0x41, 0xb2, 0x94, 0x10, |
| 309 | 0xb3, 0xc7, 0x99, 0x8d, 0x6b, 0xc4, 0x65, 0x74, 0x5e, 0x5c, 0x39, 0x26, |
| 310 | 0x69, 0xd6, 0x87, 0x0d, 0xa2, 0xc0, 0x82, 0xa9, 0x39, 0xe3, 0x7f, 0xdc, |
| 311 | 0xb8, 0x2e, 0xc9, 0x3e, 0xda, 0xc9, 0x7f, 0xf3, 0xad, 0x59, 0x50, 0xac, |
| 312 | 0xcf, 0xbc, 0x11, 0x1c, 0x76, 0xf1, 0xa9, 0x52, 0x94, 0x44, 0xe5, 0x6a, |
| 313 | 0xaf, 0x68, 0xc5, 0x6c, 0x09, 0x2c, 0xd3, 0x8d, 0xc3, 0xbe, 0xf5, 0xd2, |
| 314 | 0x0a, 0x93, 0x99, 0x26, 0xed, 0x4f, 0x74, 0xa1, 0x3e, 0xdd, 0xfb, 0xe1, |
| 315 | 0xa1, 0xce, 0xcc, 0x48, 0x94, 0xaf, 0x94, 0x28, 0xc2, 0xb7, 0xb8, 0x88, |
| 316 | 0x3f, 0xe4, 0x46, 0x3a, 0x4b, 0xc8, 0x5b, 0x1c, 0xb3, 0xc1, 0x02, 0x41, |
| 317 | 0x00, 0xee, 0xcf, 0xae, 0x81, 0xb1, 0xb9, 0xb3, 0xc9, 0x08, 0x81, 0x0b, |
| 318 | 0x10, 0xa1, 0xb5, 0x60, 0x01, 0x99, 0xeb, 0x9f, 0x44, 0xae, 0xf4, 0xfd, |
| 319 | 0xa4, 0x93, 0xb8, 0x1a, 0x9e, 0x3d, 0x84, 0xf6, 0x32, 0x12, 0x4e, 0xf0, |
| 320 | 0x23, 0x6e, 0x5d, 0x1e, 0x3b, 0x7e, 0x28, 0xfa, 0xe7, 0xaa, 0x04, 0x0a, |
| 321 | 0x2d, 0x5b, 0x25, 0x21, 0x76, 0x45, 0x9d, 0x1f, 0x39, 0x75, 0x41, 0xba, |
| 322 | 0x2a, 0x58, 0xfb, 0x65, 0x99, 0x02, 0x41, 0x00, 0xc9, 0x7f, 0xb1, 0xf0, |
| 323 | 0x27, 0xf4, 0x53, 0xf6, 0x34, 0x12, 0x33, 0xea, 0xaa, 0xd1, 0xd9, 0x35, |
| 324 | 0x3f, 0x6c, 0x42, 0xd0, 0x88, 0x66, 0xb1, 0xd0, 0x5a, 0x0f, 0x20, 0x35, |
| 325 | 0x02, 0x8b, 0x9d, 0x86, 0x98, 0x40, 0xb4, 0x16, 0x66, 0xb4, 0x2e, 0x92, |
| 326 | 0xea, 0x0d, 0xa3, 0xb4, 0x32, 0x04, 0xb5, 0xcf, 0xce, 0x33, 0x52, 0x52, |
| 327 | 0x4d, 0x04, 0x16, 0xa5, 0xa4, 0x41, 0xe7, 0x00, 0xaf, 0x46, 0x15, 0x03, |
| 328 | 0x02, 0x40, 0x54, 0x49, 0x4c, 0xa6, 0x3e, 0xba, 0x03, 0x37, 0xe4, 0xe2, |
| 329 | 0x40, 0x23, 0xfc, 0xd6, 0x9a, 0x5a, 0xeb, 0x07, 0xdd, 0xdc, 0x01, 0x83, |
| 330 | 0xa4, 0xd0, 0xac, 0x9b, 0x54, 0xb0, 0x51, 0xf2, 0xb1, 0x3e, 0xd9, 0x49, |
| 331 | 0x09, 0x75, 0xea, 0xb7, 0x74, 0x14, 0xff, 0x59, 0xc1, 0xf7, 0x69, 0x2e, |
| 332 | 0x9a, 0x2e, 0x20, 0x2b, 0x38, 0xfc, 0x91, 0x0a, 0x47, 0x41, 0x74, 0xad, |
| 333 | 0xc9, 0x3c, 0x1f, 0x67, 0xc9, 0x81, 0x02, 0x40, 0x47, 0x1e, 0x02, 0x90, |
| 334 | 0xff, 0x0a, 0xf0, 0x75, 0x03, 0x51, 0xb7, 0xf8, 0x78, 0x86, 0x4c, 0xa9, |
| 335 | 0x61, 0xad, 0xbd, 0x3a, 0x8a, 0x7e, 0x99, 0x1c, 0x5c, 0x05, 0x56, 0xa9, |
| 336 | 0x4c, 0x31, 0x46, 0xa7, 0xf9, 0x80, 0x3f, 0x8f, 0x6f, 0x8a, 0xe3, 0x42, |
| 337 | 0xe9, 0x31, 0xfd, 0x8a, 0xe4, 0x7a, 0x22, 0x0d, 0x1b, 0x99, 0xa4, 0x95, |
| 338 | 0x84, 0x98, 0x07, 0xfe, 0x39, 0xf9, 0x24, 0x5a, 0x98, 0x36, 0xda, 0x3d, |
| 339 | 0x02, 0x41, 0x00, 0xb0, 0x6c, 0x4f, 0xda, 0xbb, 0x63, 0x01, 0x19, 0x8d, |
| 340 | 0x26, 0x5b, 0xdb, 0xae, 0x94, 0x23, 0xb3, 0x80, 0xf2, 0x71, 0xf7, 0x34, |
| 341 | 0x53, 0x88, 0x50, 0x93, 0x07, 0x7f, 0xcd, 0x39, 0xe2, 0x11, 0x9f, 0xc9, |
| 342 | 0x86, 0x32, 0x15, 0x4f, 0x58, 0x83, 0xb1, 0x67, 0xa9, 0x67, 0xbf, 0x40, |
| 343 | 0x2b, 0x4e, 0x9e, 0x2e, 0x0f, 0x96, 0x56, 0xe6, 0x98, 0xea, 0x36, 0x66, |
| 344 | 0xed, 0xfb, 0x25, 0x79, 0x80, 0x39, 0xf7}; |
| 345 |
| 346 | // kOAEPCiphertext3 is a sample encryption of |kPlaintext| with |kKey3| using |
| 347 | // RSA OAEP, SHA-1, and no label. |
| 348 | static const uint8_t kOAEPCiphertext3[] = { |
| 349 | 0xb8, 0x24, 0x6b, 0x56, 0xa6, 0xed, 0x58, 0x81, 0xae, 0xb5, 0x85, 0xd9, |
| 350 | 0xa2, 0x5b, 0x2a, 0xd7, 0x90, 0xc4, 0x17, 0xe0, 0x80, 0x68, 0x1b, 0xf1, |
| 351 | 0xac, 0x2b, 0xc3, 0xde, 0xb6, 0x9d, 0x8b, 0xce, 0xf0, 0xc4, 0x36, 0x6f, |
| 352 | 0xec, 0x40, 0x0a, 0xf0, 0x52, 0xa7, 0x2e, 0x9b, 0x0e, 0xff, 0xb5, 0xb3, |
| 353 | 0xf2, 0xf1, 0x92, 0xdb, 0xea, 0xca, 0x03, 0xc1, 0x27, 0x40, 0x05, 0x71, |
| 354 | 0x13, 0xbf, 0x1f, 0x06, 0x69, 0xac, 0x22, 0xe9, 0xf3, 0xa7, 0x85, 0x2e, |
| 355 | 0x3c, 0x15, 0xd9, 0x13, 0xca, 0xb0, 0xb8, 0x86, 0x3a, 0x95, 0xc9, 0x92, |
| 356 | 0x94, 0xce, 0x86, 0x74, 0x21, 0x49, 0x54, 0x61, 0x03, 0x46, 0xf4, 0xd4, |
| 357 | 0x74, 0xb2, 0x6f, 0x7c, 0x48, 0xb4, 0x2e, 0xe6, 0x8e, 0x1f, 0x57, 0x2a, |
| 358 | 0x1f, 0xc4, 0x02, 0x6a, 0xc4, 0x56, 0xb4, 0xf5, 0x9f, 0x7b, 0x62, 0x1e, |
| 359 | 0xa1, 0xb9, 0xd8, 0x8f, 0x64, 0x20, 0x2f, 0xb1}; |
| 360 |
| 361 | // kEstonianRSAKey is an RSAPublicKey encoded with a negative modulus. See |
| 362 | // https://crbug.com/532048. |
| 363 | static const uint8_t kEstonianRSAKey[] = { |
| 364 | 0x30, 0x82, 0x01, 0x09, 0x02, 0x82, 0x01, 0x00, 0x96, 0xa6, 0x2e, 0x9c, |
| 365 | 0x4e, 0x6a, 0xc3, 0xcc, 0xcd, 0x8f, 0x70, 0xc3, 0x55, 0xbf, 0x5e, 0x9c, |
| 366 | 0xd4, 0xf3, 0x17, 0xc3, 0x97, 0x70, 0xae, 0xdf, 0x12, 0x5c, 0x15, 0x80, |
| 367 | 0x03, 0xef, 0x2b, 0x18, 0x9d, 0x6a, 0xcb, 0x52, 0x22, 0xc1, 0x81, 0xb8, |
| 368 | 0x7e, 0x61, 0xe8, 0x0f, 0x79, 0x24, 0x0f, 0x82, 0x70, 0x24, 0x4e, 0x29, |
| 369 | 0x20, 0x05, 0x54, 0xeb, 0xd4, 0xa9, 0x65, 0x59, 0xb6, 0x3c, 0x75, 0x95, |
| 370 | 0x2f, 0x4c, 0xf6, 0x9d, 0xd1, 0xaf, 0x5f, 0x14, 0x14, 0xe7, 0x25, 0xea, |
| 371 | 0xa5, 0x47, 0x5d, 0xc6, 0x3e, 0x28, 0x8d, 0xdc, 0x54, 0x87, 0x2a, 0x7c, |
| 372 | 0x10, 0xe9, 0xc6, 0x76, 0x2d, 0xe7, 0x79, 0xd8, 0x0e, 0xbb, 0xa9, 0xac, |
| 373 | 0xb5, 0x18, 0x98, 0xd6, 0x47, 0x6e, 0x06, 0x70, 0xbf, 0x9e, 0x82, 0x25, |
| 374 | 0x95, 0x4e, 0xfd, 0x70, 0xd7, 0x73, 0x45, 0x2e, 0xc1, 0x1f, 0x7a, 0x9a, |
| 375 | 0x9d, 0x60, 0xc0, 0x1f, 0x67, 0x06, 0x2a, 0x4e, 0x87, 0x3f, 0x19, 0x88, |
| 376 | 0x69, 0x64, 0x4d, 0x9f, 0x75, 0xf5, 0xd3, 0x1a, 0x41, 0x3d, 0x35, 0x17, |
| 377 | 0xb6, 0xd1, 0x44, 0x0d, 0x25, 0x8b, 0xe7, 0x94, 0x39, 0xb0, 0x7c, 0xaf, |
| 378 | 0x3e, 0x6a, 0xfa, 0x8d, 0x90, 0x21, 0x0f, 0x8a, 0x43, 0x94, 0x37, 0x7c, |
| 379 | 0x2a, 0x15, 0x4c, 0xa0, 0xfa, 0xa9, 0x2f, 0x21, 0xa6, 0x6f, 0x8e, 0x2f, |
| 380 | 0x89, 0xbc, 0xbb, 0x33, 0xf8, 0x31, 0xfc, 0xdf, 0xcd, 0x68, 0x9a, 0xbc, |
| 381 | 0x75, 0x06, 0x95, 0xf1, 0x3d, 0xef, 0xca, 0x76, 0x27, 0xd2, 0xba, 0x8e, |
| 382 | 0x0e, 0x1c, 0x43, 0xd7, 0x70, 0xb9, 0xc6, 0x15, 0xca, 0xd5, 0x4d, 0x87, |
| 383 | 0xb9, 0xd1, 0xae, 0xde, 0x69, 0x73, 0x00, 0x2a, 0x97, 0x51, 0x4b, 0x30, |
| 384 | 0x01, 0xc2, 0x85, 0xd0, 0x05, 0xcc, 0x2e, 0xe8, 0xc7, 0x42, 0xe7, 0x94, |
| 385 | 0x51, 0xe3, 0xf5, 0x19, 0x35, 0xdc, 0x57, 0x96, 0xe7, 0xd9, 0xb4, 0x49, |
| 386 | 0x02, 0x03, 0x01, 0x00, 0x01, |
| 387 | }; |
| 388 |
| 389 | // kExponent1RSAKey is an RSAPublicKey encoded with an exponent of 1. See |
| 390 | // https://crbug.com/541257 |
| 391 | static const uint8_t kExponent1RSAKey[] = { |
| 392 | 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xcf, 0x86, 0x9a, |
| 393 | 0x7d, 0x5c, 0x9f, 0xbd, 0x33, 0xbb, 0xc2, 0xb1, 0x06, 0xa8, 0x3e, 0xc5, |
| 394 | 0x18, 0xf3, 0x01, 0x04, 0xdd, 0x7a, 0x38, 0x0e, 0x8e, 0x8d, 0x10, 0xaa, |
| 395 | 0xf8, 0x64, 0x49, 0x82, 0xa6, 0x16, 0x9d, 0xd9, 0xae, 0x5e, 0x7f, 0x9b, |
| 396 | 0x53, 0xcb, 0xbb, 0x29, 0xda, 0x98, 0x47, 0x26, 0x88, 0x2e, 0x1d, 0x64, |
| 397 | 0xb3, 0xbc, 0x7e, 0x96, 0x3a, 0xa7, 0xd6, 0x87, 0xf6, 0xf5, 0x3f, 0xa7, |
| 398 | 0x3b, 0xd3, 0xc5, 0xd5, 0x61, 0x3c, 0x63, 0x05, 0xf9, 0xbc, 0x64, 0x1d, |
| 399 | 0x71, 0x65, 0xf5, 0xc8, 0xe8, 0x64, 0x41, 0x35, 0x88, 0x81, 0x6b, 0x2a, |
| 400 | 0x24, 0xbb, 0xdd, 0x9f, 0x75, 0x4f, 0xea, 0x35, 0xe5, 0x32, 0x76, 0x5a, |
| 401 | 0x8b, 0x7a, 0xb5, 0x92, 0x65, 0x34, 0xb7, 0x88, 0x42, 0x5d, 0x41, 0x0b, |
| 402 | 0xd1, 0x00, 0x2d, 0x43, 0x47, 0x55, 0x60, 0x3c, 0x0e, 0x60, 0x04, 0x5c, |
| 403 | 0x88, 0x13, 0xc7, 0x42, 0x55, 0x16, 0x31, 0x32, 0x81, 0xba, 0xde, 0xa9, |
| 404 | 0x56, 0xeb, 0xdb, 0x66, 0x7f, 0x31, 0xba, 0xe8, 0x87, 0x1a, 0xcc, 0xad, |
| 405 | 0x90, 0x86, 0x4b, 0xa7, 0x6d, 0xd5, 0xc1, 0xb7, 0xe7, 0x67, 0x56, 0x41, |
| 406 | 0xf7, 0x03, 0xb3, 0x09, 0x61, 0x63, 0xb5, 0xb0, 0x19, 0x7b, 0xc5, 0x91, |
| 407 | 0xc8, 0x96, 0x5b, 0x6a, 0x80, 0xa1, 0x53, 0x0f, 0x9a, 0x47, 0xb5, 0x9a, |
| 408 | 0x44, 0x53, 0xbd, 0x93, 0xe3, 0xe4, 0xce, 0x0c, 0x17, 0x11, 0x51, 0x1d, |
| 409 | 0xfd, 0x6c, 0x74, 0xe4, 0xec, 0x2a, 0xce, 0x57, 0x27, 0xcc, 0x83, 0x98, |
| 410 | 0x08, 0x32, 0x2c, 0xd5, 0x75, 0xa9, 0x27, 0xfe, 0xaa, 0x5e, 0x48, 0xc9, |
| 411 | 0x46, 0x9a, 0x29, 0x3f, 0xe6, 0x01, 0x4d, 0x97, 0x4a, 0x70, 0xd1, 0x5d, |
| 412 | 0xf8, 0xc0, 0x0b, 0x23, 0xcb, 0xbe, 0xf5, 0x70, 0x0b, 0xc2, 0xf2, 0xc0, |
| 413 | 0x33, 0x9c, 0xc4, 0x8b, 0x39, 0x7e, 0x3d, 0xc6, 0x23, 0x39, 0x9a, 0x98, |
| 414 | 0xdd, 0x02, 0x01, 0x01, |
| 415 | }; |
| 416 |
| 417 | struct RSAEncryptParam { |
| 418 | bssl::Span<const uint8_t> der; |
| 419 | bssl::Span<const uint8_t> oaep_ciphertext; |
| 420 | } kRSAEncryptParams[] = { |
| 421 | {kKey1, kOAEPCiphertext1}, |
| 422 | {kKey2, kOAEPCiphertext2}, |
| 423 | {kKey3, kOAEPCiphertext3}, |
| 424 | }; |
| 425 |
| 426 | class RSAEncryptTest : public testing::TestWithParam<RSAEncryptParam> {}; |
| 427 |
| 428 | TEST_P(RSAEncryptTest, TestKey) { |
| 429 | // Construct an RSA key in different ways. |
| 430 | const auto ¶m = GetParam(); |
| 431 | bssl::UniquePtr<RSA> parsed( |
| 432 | RSA_private_key_from_bytes(param.der.data(), param.der.size())); |
| 433 | ASSERT_TRUE(parsed); |
| 434 | EXPECT_TRUE(RSA_get0_e(parsed.get())); |
| 435 | EXPECT_TRUE(RSA_get0_d(parsed.get())); |
| 436 |
| 437 | bssl::UniquePtr<RSA> constructed(RSA_new_private_key( |
| 438 | RSA_get0_n(parsed.get()), RSA_get0_e(parsed.get()), |
| 439 | RSA_get0_d(parsed.get()), RSA_get0_p(parsed.get()), |
| 440 | RSA_get0_q(parsed.get()), RSA_get0_dmp1(parsed.get()), |
| 441 | RSA_get0_dmq1(parsed.get()), RSA_get0_iqmp(parsed.get()))); |
| 442 | ASSERT_TRUE(constructed); |
| 443 | EXPECT_TRUE(RSA_get0_e(constructed.get())); |
| 444 | EXPECT_TRUE(RSA_get0_d(constructed.get())); |
| 445 |
| 446 | bssl::UniquePtr<RSA> no_crt(RSA_new_private_key_no_crt( |
| 447 | RSA_get0_n(parsed.get()), RSA_get0_e(parsed.get()), |
| 448 | RSA_get0_d(parsed.get()))); |
| 449 | ASSERT_TRUE(no_crt); |
| 450 | EXPECT_TRUE(RSA_get0_e(no_crt.get())); |
| 451 | EXPECT_TRUE(RSA_get0_d(no_crt.get())); |
| 452 |
| 453 | bssl::UniquePtr<RSA> no_e(RSA_new_private_key_no_e(RSA_get0_n(parsed.get()), |
| 454 | RSA_get0_d(parsed.get()))); |
| 455 | ASSERT_TRUE(no_e); |
| 456 | EXPECT_FALSE(RSA_get0_e(no_e.get())); |
| 457 | EXPECT_TRUE(RSA_get0_d(no_e.get())); |
| 458 |
| 459 | bssl::UniquePtr<RSA> pub( |
| 460 | RSA_new_public_key(RSA_get0_n(parsed.get()), RSA_get0_e(parsed.get()))); |
| 461 | ASSERT_TRUE(pub); |
| 462 | EXPECT_TRUE(RSA_get0_e(pub.get())); |
| 463 | EXPECT_FALSE(RSA_get0_d(pub.get())); |
| 464 |
| 465 | for (RSA *key : |
| 466 | {parsed.get(), constructed.get(), no_crt.get(), no_e.get(), pub.get()}) { |
| 467 | EXPECT_TRUE(RSA_check_key(key)); |
| 468 |
| 469 | std::vector<uint8_t> ciphertext(RSA_size(key)), plaintext(RSA_size(key)); |
| 470 | size_t ciphertext_len = 0, plaintext_len = 0; |
| 471 |
| 472 | if (RSA_get0_e(key) != nullptr) { |
| 473 | // Test that PKCS#1 v1.5 encryption round-trips. |
| 474 | ASSERT_TRUE(RSA_encrypt(key, &ciphertext_len, ciphertext.data(), |
| 475 | ciphertext.size(), kPlaintext, sizeof(kPlaintext), |
| 476 | RSA_PKCS1_PADDING)); |
| 477 | EXPECT_EQ(RSA_size(key), ciphertext_len); |
| 478 |
| 479 | ASSERT_TRUE(RSA_decrypt(parsed.get(), &plaintext_len, plaintext.data(), |
| 480 | plaintext.size(), ciphertext.data(), |
| 481 | ciphertext_len, RSA_PKCS1_PADDING)); |
| 482 | EXPECT_EQ(Bytes(kPlaintext), Bytes(plaintext.data(), plaintext_len)); |
| 483 |
| 484 | // Test that OAEP encryption round-trips. |
| 485 | ciphertext_len = 0; |
| 486 | ASSERT_TRUE(RSA_encrypt(key, &ciphertext_len, ciphertext.data(), |
| 487 | ciphertext.size(), kPlaintext, sizeof(kPlaintext), |
| 488 | RSA_PKCS1_OAEP_PADDING)); |
| 489 | EXPECT_EQ(RSA_size(key), ciphertext_len); |
| 490 |
| 491 | plaintext_len = 0; |
| 492 | ASSERT_TRUE(RSA_decrypt(parsed.get(), &plaintext_len, plaintext.data(), |
| 493 | plaintext.size(), ciphertext.data(), |
| 494 | ciphertext_len, RSA_PKCS1_OAEP_PADDING)); |
| 495 | EXPECT_EQ(Bytes(kPlaintext), Bytes(plaintext.data(), plaintext_len)); |
| 496 | } |
| 497 |
| 498 | if (RSA_get0_d(key) != nullptr) { |
| 499 | // |oaep_ciphertext| should decrypt to |kPlaintext|. |
| 500 | plaintext_len = 0; |
| 501 | ASSERT_TRUE(RSA_decrypt(key, &plaintext_len, plaintext.data(), |
| 502 | plaintext.size(), param.oaep_ciphertext.data(), |
| 503 | param.oaep_ciphertext.size(), |
| 504 | RSA_PKCS1_OAEP_PADDING)); |
| 505 | EXPECT_EQ(Bytes(kPlaintext), Bytes(plaintext.data(), plaintext_len)); |
| 506 |
| 507 | // Try decrypting corrupted ciphertexts. |
| 508 | ciphertext.assign( |
| 509 | param.oaep_ciphertext.data(), |
| 510 | param.oaep_ciphertext.data() + param.oaep_ciphertext.size()); |
| 511 | for (size_t i = 0; i < ciphertext.size(); i++) { |
| 512 | SCOPED_TRACE(i); |
| 513 | ciphertext[i] ^= 1; |
| 514 | EXPECT_FALSE(RSA_decrypt(key, &plaintext_len, plaintext.data(), |
| 515 | plaintext.size(), ciphertext.data(), |
| 516 | ciphertext.size(), RSA_PKCS1_OAEP_PADDING)); |
| 517 | ERR_clear_error(); |
| 518 | ciphertext[i] ^= 1; |
| 519 | } |
| 520 |
| 521 | // Test truncated ciphertexts. |
| 522 | for (size_t len = 0; len < ciphertext.size(); len++) { |
| 523 | SCOPED_TRACE(len); |
| 524 | EXPECT_FALSE(RSA_decrypt(key, &plaintext_len, plaintext.data(), |
| 525 | plaintext.size(), ciphertext.data(), len, |
| 526 | RSA_PKCS1_OAEP_PADDING)); |
| 527 | ERR_clear_error(); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 |
| 533 | INSTANTIATE_TEST_SUITE_P(All, RSAEncryptTest, |
| 534 | testing::ValuesIn(kRSAEncryptParams)); |
| 535 |
| 536 | TEST(RSATest, TestDecrypt) { |
| 537 | bssl::UniquePtr<RSA> rsa(RSA_private_key_from_bytes(kKey2, sizeof(kKey2))); |
| 538 | ASSERT_TRUE(rsa); |
| 539 |
| 540 | EXPECT_TRUE(RSA_check_key(rsa.get())); |
| 541 |
| 542 | std::vector<uint8_t> out(RSA_size(rsa.get())); |
| 543 | size_t out_len; |
| 544 | ASSERT_TRUE(RSA_decrypt(rsa.get(), &out_len, out.data(), out.size(), |
| 545 | kPKCS1Ciphertext2, sizeof(kPKCS1Ciphertext2), |
| 546 | RSA_PKCS1_PADDING)); |
| 547 | out.resize(out_len); |
| 548 | EXPECT_EQ(Bytes("hello world"), Bytes(out)); |
| 549 | } |
| 550 |
| 551 | TEST(RSATest, CheckFIPS) { |
| 552 | bssl::UniquePtr<RSA> rsa(RSA_private_key_from_bytes(kKey1, sizeof(kKey1))); |
| 553 | ASSERT_TRUE(rsa); |
| 554 | EXPECT_TRUE(RSA_check_fips(rsa.get())); |
| 555 |
| 556 | // Check that RSA_check_fips works on a public key. |
| 557 | bssl::UniquePtr<RSA> pub( |
| 558 | RSA_public_key_from_bytes(kKey1Public, sizeof(kKey1Public))); |
| 559 | ASSERT_TRUE(pub); |
| 560 | EXPECT_TRUE(RSA_check_fips(pub.get())); |
| 561 | } |
| 562 |
| 563 | TEST(RSATest, GenerateFIPS) { |
| 564 | bssl::UniquePtr<RSA> rsa(RSA_new()); |
| 565 | ASSERT_TRUE(rsa); |
| 566 |
| 567 | // RSA_generate_key_fips may only be used for 2048-, 3072-, and 4096-bit |
| 568 | // keys. |
| 569 | EXPECT_FALSE(RSA_generate_key_fips(rsa.get(), 512, nullptr)); |
| 570 | EXPECT_FALSE(RSA_generate_key_fips(rsa.get(), 1024, nullptr)); |
| 571 | EXPECT_FALSE(RSA_generate_key_fips(rsa.get(), 2047, nullptr)); |
| 572 | EXPECT_FALSE(RSA_generate_key_fips(rsa.get(), 2049, nullptr)); |
| 573 | EXPECT_FALSE(RSA_generate_key_fips(rsa.get(), 3071, nullptr)); |
| 574 | EXPECT_FALSE(RSA_generate_key_fips(rsa.get(), 3073, nullptr)); |
| 575 | EXPECT_FALSE(RSA_generate_key_fips(rsa.get(), 4095, nullptr)); |
| 576 | EXPECT_FALSE(RSA_generate_key_fips(rsa.get(), 4097, nullptr)); |
| 577 | ERR_clear_error(); |
| 578 |
| 579 | // Test that we can generate keys of the supported lengths: |
| 580 | for (const size_t bits : {2048, 3072, 4096}) { |
| 581 | SCOPED_TRACE(bits); |
| 582 |
| 583 | rsa.reset(RSA_new()); |
| 584 | ASSERT_TRUE(rsa); |
| 585 | ASSERT_TRUE(RSA_generate_key_fips(rsa.get(), bits, nullptr)); |
| 586 | EXPECT_EQ(bits, BN_num_bits(rsa->n)); |
| 587 | } |
| 588 | } |
| 589 |
| 590 | TEST(RSATest, BadKey) { |
| 591 | bssl::UniquePtr<RSA> key(RSA_new()); |
| 592 | bssl::UniquePtr<BIGNUM> e(BN_new()); |
| 593 | ASSERT_TRUE(key); |
| 594 | ASSERT_TRUE(e); |
| 595 | ASSERT_TRUE(BN_set_word(e.get(), RSA_F4)); |
| 596 |
| 597 | // Generate a bad key. |
| 598 | ASSERT_TRUE(RSA_generate_key_ex(key.get(), 2048, e.get(), nullptr)); |
| 599 | ASSERT_TRUE(BN_add(key->p, key->p, BN_value_one())); |
| 600 |
| 601 | // Bad keys are detected. |
| 602 | EXPECT_FALSE(RSA_check_key(key.get())); |
| 603 | EXPECT_FALSE(RSA_check_fips(key.get())); |
| 604 |
| 605 | // Bad keys may not be parsed. |
| 606 | uint8_t *der; |
| 607 | size_t der_len; |
| 608 | ASSERT_TRUE(RSA_private_key_to_bytes(&der, &der_len, key.get())); |
| 609 | bssl::UniquePtr<uint8_t> delete_der(der); |
| 610 | key.reset(RSA_private_key_from_bytes(der, der_len)); |
| 611 | EXPECT_FALSE(key); |
| 612 | } |
| 613 |
| 614 | TEST(RSATest, ASN1) { |
| 615 | // Test that private keys may be decoded. |
| 616 | bssl::UniquePtr<RSA> rsa(RSA_private_key_from_bytes(kKey1, sizeof(kKey1))); |
| 617 | ASSERT_TRUE(rsa); |
| 618 |
| 619 | // Test that the serialization round-trips. |
| 620 | uint8_t *der; |
| 621 | size_t der_len; |
| 622 | ASSERT_TRUE(RSA_private_key_to_bytes(&der, &der_len, rsa.get())); |
| 623 | bssl::UniquePtr<uint8_t> delete_der(der); |
| 624 | EXPECT_EQ(Bytes(kKey1), Bytes(der, der_len)); |
| 625 |
| 626 | // Test that serializing public keys works. |
| 627 | ASSERT_TRUE(RSA_public_key_to_bytes(&der, &der_len, rsa.get())); |
| 628 | delete_der.reset(der); |
| 629 |
| 630 | // Public keys may be parsed back out. |
| 631 | rsa.reset(RSA_public_key_from_bytes(der, der_len)); |
| 632 | ASSERT_TRUE(rsa); |
| 633 | EXPECT_FALSE(rsa->p); |
| 634 | EXPECT_FALSE(rsa->q); |
| 635 |
| 636 | // Serializing the result round-trips. |
| 637 | uint8_t *der2; |
| 638 | size_t der2_len; |
| 639 | ASSERT_TRUE(RSA_public_key_to_bytes(&der2, &der2_len, rsa.get())); |
| 640 | bssl::UniquePtr<uint8_t> delete_der2(der2); |
| 641 | EXPECT_EQ(Bytes(der, der_len), Bytes(der2, der2_len)); |
| 642 |
| 643 | // Public keys cannot be serialized as private keys. |
| 644 | int ok = RSA_private_key_to_bytes(&der, &der_len, rsa.get()); |
| 645 | if (ok) { |
| 646 | OPENSSL_free(der); |
| 647 | } |
| 648 | EXPECT_FALSE(ok); |
| 649 | ERR_clear_error(); |
| 650 |
| 651 | // Public keys with negative moduli are invalid. |
| 652 | rsa.reset( |
| 653 | RSA_public_key_from_bytes(kEstonianRSAKey, sizeof(kEstonianRSAKey))); |
| 654 | EXPECT_FALSE(rsa); |
| 655 | ERR_clear_error(); |
| 656 | } |
| 657 |
| 658 | TEST(RSATest, BadExponent) { |
| 659 | bssl::UniquePtr<RSA> rsa( |
| 660 | RSA_public_key_from_bytes(kExponent1RSAKey, sizeof(kExponent1RSAKey))); |
| 661 | EXPECT_FALSE(rsa); |
| 662 | ERR_clear_error(); |
| 663 | } |
| 664 |
| 665 | // Attempting to generate an excessively small key should fail. |
| 666 | TEST(RSATest, GenerateSmallKey) { |
| 667 | bssl::UniquePtr<RSA> rsa(RSA_new()); |
| 668 | ASSERT_TRUE(rsa); |
| 669 | bssl::UniquePtr<BIGNUM> e(BN_new()); |
| 670 | ASSERT_TRUE(e); |
| 671 | ASSERT_TRUE(BN_set_word(e.get(), RSA_F4)); |
| 672 |
| 673 | EXPECT_FALSE(RSA_generate_key_ex(rsa.get(), 255, e.get(), nullptr)); |
| 674 | EXPECT_TRUE( |
| 675 | ErrorEquals(ERR_get_error(), ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL)); |
| 676 | } |
| 677 |
| 678 | // Attempting to generate an funny RSA key length should round down. |
| 679 | TEST(RSATest, RoundKeyLengths) { |
| 680 | bssl::UniquePtr<BIGNUM> e(BN_new()); |
| 681 | ASSERT_TRUE(e); |
| 682 | ASSERT_TRUE(BN_set_word(e.get(), RSA_F4)); |
| 683 |
| 684 | bssl::UniquePtr<RSA> rsa(RSA_new()); |
| 685 | ASSERT_TRUE(rsa); |
| 686 | ASSERT_TRUE(RSA_generate_key_ex(rsa.get(), 1025, e.get(), nullptr)); |
| 687 | EXPECT_EQ(1024u, BN_num_bits(rsa->n)); |
| 688 |
| 689 | rsa.reset(RSA_new()); |
| 690 | ASSERT_TRUE(rsa); |
| 691 | ASSERT_TRUE(RSA_generate_key_ex(rsa.get(), 1027, e.get(), nullptr)); |
| 692 | EXPECT_EQ(1024u, BN_num_bits(rsa->n)); |
| 693 |
| 694 | rsa.reset(RSA_new()); |
| 695 | ASSERT_TRUE(rsa); |
| 696 | ASSERT_TRUE(RSA_generate_key_ex(rsa.get(), 1151, e.get(), nullptr)); |
| 697 | EXPECT_EQ(1024u, BN_num_bits(rsa->n)); |
| 698 |
| 699 | rsa.reset(RSA_new()); |
| 700 | ASSERT_TRUE(rsa); |
| 701 | ASSERT_TRUE(RSA_generate_key_ex(rsa.get(), 1152, e.get(), nullptr)); |
| 702 | EXPECT_EQ(1152u, BN_num_bits(rsa->n)); |
| 703 | } |
| 704 |
| 705 | TEST(RSATest, BlindingDisabled) { |
| 706 | bssl::UniquePtr<RSA> rsa(RSA_private_key_from_bytes(kKey2, sizeof(kKey2))); |
| 707 | ASSERT_TRUE(rsa); |
| 708 |
| 709 | rsa->flags |= RSA_FLAG_NO_BLINDING; |
| 710 |
| 711 | std::vector<uint8_t> sig(RSA_size(rsa.get())); |
| 712 | static const uint8_t kZeros[32] = {0}; |
| 713 | unsigned sig_len; |
| 714 | ASSERT_TRUE(RSA_sign(NID_sha256, kZeros, sizeof(kZeros), sig.data(), &sig_len, |
| 715 | rsa.get())); |
| 716 | EXPECT_TRUE(RSA_verify(NID_sha256, kZeros, sizeof(kZeros), sig.data(), |
| 717 | sig_len, rsa.get())); |
| 718 | } |
| 719 |
| 720 | TEST(RSATest, CheckKey) { |
| 721 | static const char kN[] = |
| 722 | "b5a5651bc2e15ce31d789f0984053a2ea0cf8f964a78068c45acfdf078c57fd62d5a287c" |
| 723 | "32f3baa879f5dfea27d7a3077c9d3a2a728368c3d90164690c3d82f660ffebc7f13fed45" |
| 724 | "4eb5103df943c10dc32ec60b0d9b6e307bfd7f9b943e0dc3901e42501765365f7286eff2" |
| 725 | "f1f728774aa6a371e108a3a7dd00d7bcd4c1a186c2865d4b370ea38cc89c0b23b318dbca" |
| 726 | "fbd872b4f9b833dfb2a4ca7fcc23298020044e8130bfe930adfb3e5cab8d324547adf4b2" |
| 727 | "ce34d7cea4298f0b613d85f2bf1df03da44aee0784a1a20a15ee0c38a0f8e84962f1f61b" |
| 728 | "18bd43781c7385f3c2b8e2aebd3c560b4faad208ad3938bad27ddda9ed9e933dba088021" |
| 729 | "2dd9e28d"; |
| 730 | static const char kE[] = "10001"; |
| 731 | static const char kD[] = |
| 732 | "fb9c6afd9568ce5ddac8e6a32bb881eb6cdd962bbc639dce5805548bf0fec2214f18ffd3" |
| 733 | "6a50aa520cfe4477f9507d87355a24e3ff537f9f29ccffe5730b11896ebb9142982ed0df" |
| 734 | "9c32ba98dddab863f3e5aa764d16ebff4500d3ee11de12fabd7aeca83c7ffa5d242b3ddc" |
| 735 | "ecc64bcb5220996e79249a6d3f78975dfde769710569812dee59c0f56e4650d02a939d9c" |
| 736 | "853e2cba9b0c2447a8757951ae9a0336dfa64c3d5476df9b20f200cfb52e3fbd2d4e3f34" |
| 737 | "200b1171cbac367096f23366e74592025875efb6a7e3b1dd365abb0d86f34ee65ddbfa93" |
| 738 | "90460da0d346833d6aa6277c0216b20073ba2f18471549c309e82d12e10714d0e0dbf146" |
| 739 | "6fcd1f1"; |
| 740 | static const char kP[] = |
| 741 | "edbe476fe8989f3966e72a20348ec6d8e924f44e1d9fa2c3485ea8a2ffd39f68574a5cef" |
| 742 | "ffbb92d6764789ac0f67149127239c2027fbc55b5268a1dac6588de44e614f3bdce00f0a" |
| 743 | "56d138800ad772d159a583c6548e37cadbfcf1b4ebfd50d01508986a516f36ed827b94ef" |
| 744 | "1f9b4e233bf5762b3a903d2dfbbbce1fba30e9f1"; |
| 745 | static const char kQ[] = |
| 746 | "c398518790a166dbe50498f04940d14c87ded09313fb0f69f69255c688142802ba3d4f9e" |
| 747 | "f9425dadc462170635593c06a332cfc5fc9e6e1c05281950a5ce3bad4fd7cc83a38bd4ad" |
| 748 | "6865594275af424f47c64c04af1caab2e261e95b975097c887d587dc8150df34cbeccd7d" |
| 749 | "0688c392d9f1c617810043c9b93b884bf6ed465d"; |
| 750 | static const char kDMP1[] = |
| 751 | "b44db5d1fa7e1d6ba44e36d59be6988a132f7294f7c484e543b27e84b82e9fdbbb2feb92" |
| 752 | "1cc9fe0fe63e54fc07e66e63b3623f5ae7d7fb124a4a8e4de4556eaf327e7c5ff3207e67" |
| 753 | "a1f624ba7efe6cd6b6fd5f160034a7bd92df9fd44d919d436260556f74793b181ff867b8" |
| 754 | "7ea9033697978e5a349d05b9250c86c3eb2a8391"; |
| 755 | static const char kDMQ1[] = |
| 756 | "7c06d9240265264927a6cba80a7b4c7c9fe77d10d669abb38083f85a24adcb55376d6b50" |
| 757 | "9e34241cecdb5a483889f6132b672bf31aa607a242eed3669d4cf1f08b2186f0ae431bc0" |
| 758 | "3de38e3f234ad7dc57e1f9103b4e0d3bd36b4cc324671968322207bd9e4e7ecb06c888e0" |
| 759 | "cfc4e766f646665b3f14c0e7684ac4b98ec1948d"; |
| 760 | static const char kIQMP[] = |
| 761 | "2887a5cb0c1bf6710e91c25da141dad92134a927431471c2d4a8b78036026d21182990e1" |
| 762 | "2c1d70635f07ee551383899365a69b33d4db23e5ff7371ff4244d2c3290ce2b91ac11adc" |
| 763 | "a54bb61ea5e64b9423102933ea100c12dad809fbf9589515e9d28e867f6b95c2d307f792" |
| 764 | "cac28c6d7d23f441cb5b62798233db29b5cc0348"; |
| 765 |
| 766 | bssl::UniquePtr<RSA> rsa(RSA_new()); |
| 767 | ASSERT_TRUE(rsa); |
| 768 |
| 769 | // Missing n or e does not pass. |
| 770 | ASSERT_TRUE(BN_hex2bn(&rsa->n, kN)); |
| 771 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 772 | ERR_clear_error(); |
| 773 |
| 774 | BN_free(rsa->n); |
| 775 | rsa->n = nullptr; |
| 776 | ASSERT_TRUE(BN_hex2bn(&rsa->e, kE)); |
| 777 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 778 | ERR_clear_error(); |
| 779 |
| 780 | // Public keys pass. |
| 781 | ASSERT_TRUE(BN_hex2bn(&rsa->n, kN)); |
| 782 | EXPECT_TRUE(RSA_check_key(rsa.get())); |
| 783 |
| 784 | // Configuring d also passes. |
| 785 | ASSERT_TRUE(BN_hex2bn(&rsa->d, kD)); |
| 786 | EXPECT_TRUE(RSA_check_key(rsa.get())); |
| 787 |
| 788 | // p and q must be provided together. |
| 789 | ASSERT_TRUE(BN_hex2bn(&rsa->p, kP)); |
| 790 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 791 | ERR_clear_error(); |
| 792 |
| 793 | BN_free(rsa->p); |
| 794 | rsa->p = nullptr; |
| 795 | ASSERT_TRUE(BN_hex2bn(&rsa->q, kQ)); |
| 796 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 797 | ERR_clear_error(); |
| 798 |
| 799 | // Supplying p and q without CRT parameters passes. |
| 800 | ASSERT_TRUE(BN_hex2bn(&rsa->p, kP)); |
| 801 | EXPECT_TRUE(RSA_check_key(rsa.get())); |
| 802 |
| 803 | // With p and q together, it is sufficient to check d against e. |
| 804 | ASSERT_TRUE(BN_add_word(rsa->d, 1)); |
| 805 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 806 | ERR_clear_error(); |
| 807 |
| 808 | // Test another invalid d. p-1 is divisible by 3, so there is no valid value |
| 809 | // of d here if e = 111. Set d to what extended GCD would have given if it |
| 810 | // forgot to check the inverse existed. |
| 811 | static const char kDBogus[] = |
| 812 | "140be923edb928cf4340a08ada19f23da680ff20275a81e033825ee8605afc3bf6039b87" |
| 813 | "f0ddc7ea3b95f214a6fdda1064d0c66b50ac7bfe8cfe6c85d3cd217ae6f5094cd72a39e5" |
| 814 | "a17a9ce43eae1ba5d7d8c3fb743d8cbcb3bcd74edd0b75fcca23a0b00bcea119864c0243" |
| 815 | "bf9ab32b25a4d73a1e062482f538055bc2258369353647d4325aec7a28dc1a6798e85fae" |
| 816 | "85850558868468d60015927cb10b2a893e23aa16b1f9278d4413f64d0a3122218f9000ae" |
| 817 | "cd8743b8e9e50bd9de81eebc4e0230d1f4f7bffc1e6f903606afba9ee694c2b40022f171" |
| 818 | "a760e7c63e736e31d7c7ff8b77dc206c2a3aa5afd540073060ebb9050bddce1ff1917630" |
| 819 | "47fff51d"; |
| 820 | ASSERT_TRUE(BN_set_word(rsa->e, 111)); |
| 821 | ASSERT_TRUE(BN_hex2bn(&rsa->d, kDBogus)); |
| 822 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 823 | ERR_clear_error(); |
| 824 | ASSERT_TRUE(BN_hex2bn(&rsa->e, kE)); |
| 825 |
| 826 | // d computed via the Euler totient rather than the Carmichael totient is also |
| 827 | // acceptable. |
| 828 | static const char kDEuler[] = |
| 829 | "3d231ff6ca0ee41ea50ab62c93bcd6aa5f01bd484e643b7ff6eb94c4dd414c17a0481a1c" |
| 830 | "4361f94f3f4d5c42098af09a527cf0d8dc96122ae8dd29189a4011d62f2bb40625d2e85f" |
| 831 | "4d706fb90c2e9bc9b00a0c2a28384a4c134f6d25c62d64a08fdf3f5e89a14d3daee46fda" |
| 832 | "8b4a2eda87cbb2735fd47290cb37bf65150edef854a28927ce5ac36d36107711cffb8ac3" |
| 833 | "2b090e409bb822b117744a9aabf878b8b1998d406337ec24cee3877795061c67322ac626" |
| 834 | "6c675a2cefe0f85f06b4d24eb6ad8e3fae7f218f5bd8ff2fb8bf8176d8527b0dfdaf8490" |
| 835 | "8f9bfaf3f37dcf8aa0211311bac07b1a478c3ed8a6369e5d5fc42b2afa93f5de8f520981" |
| 836 | "c62bbe81"; |
| 837 | ASSERT_TRUE(BN_hex2bn(&rsa->d, kDEuler)); |
| 838 | EXPECT_TRUE(RSA_check_key(rsa.get())); |
| 839 |
| 840 | // If d is completely out of range but otherwise valid, it is rejected. |
| 841 | static const char kDTooLarge[] = |
| 842 | "f2c885128cf04101c283553617c210d8ffd14cde98dc420c3c9892b55606cbedcda24298" |
| 843 | "7655b3f7b9433c2c316293a1cf1a2b034f197aeec1de8d81a67d94cc902b9fce1712d5a4" |
| 844 | "9c257ff705725cd77338d23535d3b87c8f4cecc15a6b72641ffd81aea106839d216b5fcd" |
| 845 | "7d415751d27255e540dd1638a8389721e9d0807d65d24d7b8c2f60e4b2c0bf250544ce68" |
| 846 | "b5ddbc1463d5a4638b2816b0f033dacdc0162f329af9e4d142352521fbd2fe14af824ef3" |
| 847 | "1601fe843c79cc3efbcb8eafd79262bdd25e2bdf21440f774e26d88ed7df938c5cf6982d" |
| 848 | "e9fa635b8ca36ce5c5fbd579a53cbb0348ceae752d4bc5621c5acc922ca2082494633337" |
| 849 | "42e770c1"; |
| 850 | ASSERT_TRUE(BN_hex2bn(&rsa->d, kDTooLarge)); |
| 851 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 852 | ERR_clear_error(); |
| 853 | ASSERT_TRUE(BN_hex2bn(&rsa->d, kD)); |
| 854 |
| 855 | // CRT value must either all be provided or all missing. |
| 856 | ASSERT_TRUE(BN_hex2bn(&rsa->dmp1, kDMP1)); |
| 857 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 858 | ERR_clear_error(); |
| 859 | BN_free(rsa->dmp1); |
| 860 | rsa->dmp1 = nullptr; |
| 861 |
| 862 | ASSERT_TRUE(BN_hex2bn(&rsa->dmq1, kDMQ1)); |
| 863 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 864 | ERR_clear_error(); |
| 865 | BN_free(rsa->dmq1); |
| 866 | rsa->dmq1 = nullptr; |
| 867 |
| 868 | ASSERT_TRUE(BN_hex2bn(&rsa->iqmp, kIQMP)); |
| 869 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 870 | ERR_clear_error(); |
| 871 |
| 872 | // The full key is accepted. |
| 873 | ASSERT_TRUE(BN_hex2bn(&rsa->dmp1, kDMP1)); |
| 874 | ASSERT_TRUE(BN_hex2bn(&rsa->dmq1, kDMQ1)); |
| 875 | EXPECT_TRUE(RSA_check_key(rsa.get())); |
| 876 |
| 877 | // Incorrect CRT values are rejected. |
| 878 | ASSERT_TRUE(BN_add_word(rsa->dmp1, 1)); |
| 879 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 880 | ERR_clear_error(); |
| 881 | ASSERT_TRUE(BN_sub_word(rsa->dmp1, 1)); |
| 882 |
| 883 | ASSERT_TRUE(BN_add_word(rsa->dmq1, 1)); |
| 884 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 885 | ERR_clear_error(); |
| 886 | ASSERT_TRUE(BN_sub_word(rsa->dmq1, 1)); |
| 887 |
| 888 | ASSERT_TRUE(BN_add_word(rsa->iqmp, 1)); |
| 889 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 890 | ERR_clear_error(); |
| 891 | ASSERT_TRUE(BN_sub_word(rsa->iqmp, 1)); |
| 892 |
| 893 | // Non-reduced CRT values are rejected. |
| 894 | ASSERT_TRUE(BN_add(rsa->dmp1, rsa->dmp1, rsa->p)); |
| 895 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 896 | ERR_clear_error(); |
| 897 | ASSERT_TRUE(BN_sub(rsa->dmp1, rsa->dmp1, rsa->p)); |
| 898 |
| 899 | ASSERT_TRUE(BN_add(rsa->dmq1, rsa->dmq1, rsa->q)); |
| 900 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 901 | ERR_clear_error(); |
| 902 | ASSERT_TRUE(BN_sub(rsa->dmq1, rsa->dmq1, rsa->q)); |
| 903 |
| 904 | ASSERT_TRUE(BN_add(rsa->iqmp, rsa->iqmp, rsa->p)); |
| 905 | EXPECT_FALSE(RSA_check_key(rsa.get())); |
| 906 | ERR_clear_error(); |
| 907 | ASSERT_TRUE(BN_sub(rsa->iqmp, rsa->iqmp, rsa->p)); |
| 908 | } |
| 909 |
| 910 | TEST(RSATest, KeygenFail) { |
| 911 | bssl::UniquePtr<RSA> rsa(RSA_new()); |
| 912 | ASSERT_TRUE(rsa); |
| 913 |
| 914 | // Cause RSA key generation after a prime has been generated, to test that |
| 915 | // |rsa| is left alone. |
| 916 | BN_GENCB cb; |
| 917 | BN_GENCB_set( |
| 918 | &cb, [](int event, int, BN_GENCB *) -> int { return event != 3; }, |
| 919 | nullptr); |
| 920 |
| 921 | bssl::UniquePtr<BIGNUM> e(BN_new()); |
| 922 | ASSERT_TRUE(e); |
| 923 | ASSERT_TRUE(BN_set_word(e.get(), RSA_F4)); |
| 924 |
| 925 | // Key generation should fail. |
| 926 | EXPECT_FALSE(RSA_generate_key_ex(rsa.get(), 2048, e.get(), &cb)); |
| 927 |
| 928 | // Failed key generations do not leave garbage in |rsa|. |
| 929 | EXPECT_FALSE(rsa->n); |
| 930 | EXPECT_FALSE(rsa->e); |
| 931 | EXPECT_FALSE(rsa->d); |
| 932 | EXPECT_FALSE(rsa->p); |
| 933 | EXPECT_FALSE(rsa->q); |
| 934 | EXPECT_FALSE(rsa->dmp1); |
| 935 | EXPECT_FALSE(rsa->dmq1); |
| 936 | EXPECT_FALSE(rsa->iqmp); |
| 937 | EXPECT_FALSE(rsa->mont_n); |
| 938 | EXPECT_FALSE(rsa->mont_p); |
| 939 | EXPECT_FALSE(rsa->mont_q); |
| 940 | EXPECT_FALSE(rsa->d_fixed); |
| 941 | EXPECT_FALSE(rsa->dmp1_fixed); |
| 942 | EXPECT_FALSE(rsa->dmq1_fixed); |
| 943 | EXPECT_FALSE(rsa->iqmp_mont); |
| 944 | EXPECT_FALSE(rsa->private_key_frozen); |
| 945 |
| 946 | // Failed key generations leave the previous contents alone. |
| 947 | EXPECT_TRUE(RSA_generate_key_ex(rsa.get(), 2048, e.get(), nullptr)); |
| 948 | uint8_t *der; |
| 949 | size_t der_len; |
| 950 | ASSERT_TRUE(RSA_private_key_to_bytes(&der, &der_len, rsa.get())); |
| 951 | bssl::UniquePtr<uint8_t> delete_der(der); |
| 952 |
| 953 | EXPECT_FALSE(RSA_generate_key_ex(rsa.get(), 2048, e.get(), &cb)); |
| 954 |
| 955 | uint8_t *der2; |
| 956 | size_t der2_len; |
| 957 | ASSERT_TRUE(RSA_private_key_to_bytes(&der2, &der2_len, rsa.get())); |
| 958 | bssl::UniquePtr<uint8_t> delete_der2(der2); |
| 959 | EXPECT_EQ(Bytes(der, der_len), Bytes(der2, der2_len)); |
| 960 |
| 961 | // Generating a key over an existing key works, despite any cached state. |
| 962 | EXPECT_TRUE(RSA_generate_key_ex(rsa.get(), 2048, e.get(), nullptr)); |
| 963 | EXPECT_TRUE(RSA_check_key(rsa.get())); |
| 964 | uint8_t *der3; |
| 965 | size_t der3_len; |
| 966 | ASSERT_TRUE(RSA_private_key_to_bytes(&der3, &der3_len, rsa.get())); |
| 967 | bssl::UniquePtr<uint8_t> delete_der3(der3); |
| 968 | EXPECT_NE(Bytes(der, der_len), Bytes(der3, der3_len)); |
| 969 | } |
| 970 |
| 971 | TEST(RSATest, KeygenFailOnce) { |
| 972 | bssl::UniquePtr<RSA> rsa(RSA_new()); |
| 973 | ASSERT_TRUE(rsa); |
| 974 |
| 975 | // Cause only the first iteration of RSA key generation to fail. |
| 976 | bool failed = false; |
| 977 | BN_GENCB cb; |
| 978 | BN_GENCB_set( |
| 979 | &cb, |
| 980 | [](int event, int n, BN_GENCB *cb_ptr) -> int { |
| 981 | bool *failed_ptr = static_cast<bool *>(cb_ptr->arg); |
| 982 | if (*failed_ptr) { |
| 983 | ADD_FAILURE() << "Callback called multiple times."; |
| 984 | return 1; |
| 985 | } |
| 986 | *failed_ptr = true; |
| 987 | return 0; |
| 988 | }, |
| 989 | &failed); |
| 990 |
| 991 | // Although key generation internally retries, the external behavior of |
| 992 | // |BN_GENCB| is preserved. |
| 993 | bssl::UniquePtr<BIGNUM> e(BN_new()); |
| 994 | ASSERT_TRUE(e); |
| 995 | ASSERT_TRUE(BN_set_word(e.get(), RSA_F4)); |
| 996 | EXPECT_FALSE(RSA_generate_key_ex(rsa.get(), 2048, e.get(), &cb)); |
| 997 | } |
| 998 |
| 999 | TEST(RSATest, KeygenInternalRetry) { |
| 1000 | bssl::UniquePtr<RSA> rsa(RSA_new()); |
| 1001 | ASSERT_TRUE(rsa); |
| 1002 |
| 1003 | // Simulate one internal attempt at key generation failing. |
| 1004 | bool failed = false; |
| 1005 | BN_GENCB cb; |
| 1006 | BN_GENCB_set( |
| 1007 | &cb, |
| 1008 | [](int event, int n, BN_GENCB *cb_ptr) -> int { |
| 1009 | bool *failed_ptr = static_cast<bool *>(cb_ptr->arg); |
| 1010 | if (*failed_ptr) { |
| 1011 | return 1; |
| 1012 | } |
| 1013 | *failed_ptr = true; |
| 1014 | // This test does not test any public API behavior. It is just |
| 1015 | // a hack to exercise the retry codepath and make sure it |
| 1016 | // works. |
| 1017 | OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_MANY_ITERATIONS); |
| 1018 | return 0; |
| 1019 | }, |
| 1020 | &failed); |
| 1021 |
| 1022 | // Key generation internally retries on RSA_R_TOO_MANY_ITERATIONS. |
| 1023 | bssl::UniquePtr<BIGNUM> e(BN_new()); |
| 1024 | ASSERT_TRUE(e); |
| 1025 | ASSERT_TRUE(BN_set_word(e.get(), RSA_F4)); |
| 1026 | EXPECT_TRUE(RSA_generate_key_ex(rsa.get(), 2048, e.get(), &cb)); |
| 1027 | } |
| 1028 |
| 1029 | // Test that, after a key has been used, it can still be modified into another |
| 1030 | // key. |
| 1031 | TEST(RSATest, OverwriteKey) { |
| 1032 | // Make a key and perform public and private key operations with it, so that |
| 1033 | // all derived values are filled in. |
| 1034 | bssl::UniquePtr<RSA> key1(RSA_private_key_from_bytes(kKey1, sizeof(kKey1))); |
| 1035 | ASSERT_TRUE(key1); |
| 1036 |
| 1037 | ASSERT_TRUE(RSA_check_key(key1.get())); |
| 1038 | size_t len; |
| 1039 | std::vector<uint8_t> ciphertext(RSA_size(key1.get())); |
| 1040 | ASSERT_TRUE(RSA_encrypt(key1.get(), &len, ciphertext.data(), |
| 1041 | ciphertext.size(), kPlaintext, sizeof(kPlaintext), |
| 1042 | RSA_PKCS1_OAEP_PADDING)); |
| 1043 | ciphertext.resize(len); |
| 1044 |
| 1045 | std::vector<uint8_t> plaintext(RSA_size(key1.get())); |
| 1046 | ASSERT_TRUE(RSA_decrypt(key1.get(), &len, plaintext.data(), plaintext.size(), |
| 1047 | ciphertext.data(), ciphertext.size(), |
| 1048 | RSA_PKCS1_OAEP_PADDING)); |
| 1049 | plaintext.resize(len); |
| 1050 | EXPECT_EQ(Bytes(plaintext), Bytes(kPlaintext)); |
| 1051 |
| 1052 | // Overwrite |key1| with the contents of |key2|. |
| 1053 | bssl::UniquePtr<RSA> key2(RSA_private_key_from_bytes(kKey2, sizeof(kKey2))); |
| 1054 | ASSERT_TRUE(key2); |
| 1055 |
| 1056 | auto copy_rsa_fields = [](RSA *dst, const RSA *src) { |
| 1057 | bssl::UniquePtr<BIGNUM> n(BN_dup(RSA_get0_n(src))); |
| 1058 | ASSERT_TRUE(n); |
| 1059 | bssl::UniquePtr<BIGNUM> e(BN_dup(RSA_get0_e(src))); |
| 1060 | ASSERT_TRUE(e); |
| 1061 | bssl::UniquePtr<BIGNUM> d(BN_dup(RSA_get0_d(src))); |
| 1062 | ASSERT_TRUE(d); |
| 1063 | bssl::UniquePtr<BIGNUM> p(BN_dup(RSA_get0_p(src))); |
| 1064 | ASSERT_TRUE(p); |
| 1065 | bssl::UniquePtr<BIGNUM> q(BN_dup(RSA_get0_q(src))); |
| 1066 | ASSERT_TRUE(q); |
| 1067 | bssl::UniquePtr<BIGNUM> dmp1(BN_dup(RSA_get0_dmp1(src))); |
| 1068 | ASSERT_TRUE(dmp1); |
| 1069 | bssl::UniquePtr<BIGNUM> dmq1(BN_dup(RSA_get0_dmq1(src))); |
| 1070 | ASSERT_TRUE(dmq1); |
| 1071 | bssl::UniquePtr<BIGNUM> iqmp(BN_dup(RSA_get0_iqmp(src))); |
| 1072 | ASSERT_TRUE(iqmp); |
| 1073 | ASSERT_TRUE(RSA_set0_key(dst, n.release(), e.release(), d.release())); |
| 1074 | ASSERT_TRUE(RSA_set0_factors(dst, p.release(), q.release())); |
| 1075 | ASSERT_TRUE(RSA_set0_crt_params(dst, dmp1.release(), dmq1.release(), |
| 1076 | iqmp.release())); |
| 1077 | }; |
| 1078 | ASSERT_NO_FATAL_FAILURE(copy_rsa_fields(key1.get(), key2.get())); |
| 1079 |
| 1080 | auto check_rsa_compatible = [&](RSA *enc, RSA *dec) { |
| 1081 | ciphertext.resize(RSA_size(enc)); |
| 1082 | ASSERT_TRUE(RSA_encrypt(enc, &len, ciphertext.data(), ciphertext.size(), |
| 1083 | kPlaintext, sizeof(kPlaintext), |
| 1084 | RSA_PKCS1_OAEP_PADDING)); |
| 1085 | ciphertext.resize(len); |
| 1086 |
| 1087 | plaintext.resize(RSA_size(dec)); |
| 1088 | ASSERT_TRUE(RSA_decrypt(dec, &len, plaintext.data(), plaintext.size(), |
| 1089 | ciphertext.data(), ciphertext.size(), |
| 1090 | RSA_PKCS1_OAEP_PADDING)); |
| 1091 | plaintext.resize(len); |
| 1092 | EXPECT_EQ(Bytes(plaintext), Bytes(kPlaintext)); |
| 1093 | }; |
| 1094 |
| 1095 | ASSERT_NO_FATAL_FAILURE( |
| 1096 | check_rsa_compatible(/*enc=*/key1.get(), /*dec=*/key2.get())); |
| 1097 | ASSERT_NO_FATAL_FAILURE( |
| 1098 | check_rsa_compatible(/*enc=*/key2.get(), /*dec=*/key1.get())); |
| 1099 |
| 1100 | // If we generate a new key on top of |key1|, it should be usable and |
| 1101 | // self-consistent. We test this by making a new key with the same parameters |
| 1102 | // and checking they behave the same. |
| 1103 | ASSERT_TRUE( |
| 1104 | RSA_generate_key_ex(key1.get(), 1024, RSA_get0_e(key2.get()), nullptr)); |
| 1105 | EXPECT_NE(0, BN_cmp(RSA_get0_n(key1.get()), RSA_get0_n(key2.get()))); |
| 1106 |
| 1107 | key2.reset(RSA_new()); |
| 1108 | ASSERT_TRUE(key2); |
| 1109 | ASSERT_NO_FATAL_FAILURE(copy_rsa_fields(key2.get(), key1.get())); |
| 1110 | ASSERT_NO_FATAL_FAILURE( |
| 1111 | check_rsa_compatible(/*enc=*/key1.get(), /*dec=*/key2.get())); |
| 1112 | ASSERT_NO_FATAL_FAILURE( |
| 1113 | check_rsa_compatible(/*enc=*/key2.get(), /*dec=*/key1.get())); |
| 1114 | } |
| 1115 |
| 1116 | // Test that RSA keys do not support operations will cleanly fail them. |
| 1117 | TEST(RSATest, MissingParameters) { |
| 1118 | bssl::UniquePtr<RSA> sample(RSA_private_key_from_bytes(kKey1, sizeof(kKey1))); |
| 1119 | ASSERT_TRUE(sample); |
| 1120 |
| 1121 | // Make a sample signature. |
| 1122 | const uint8_t kZeros[32] = {0}; |
| 1123 | std::vector<uint8_t> sig(RSA_size(sample.get())); |
| 1124 | unsigned len_u; |
| 1125 | ASSERT_TRUE(RSA_sign(NID_sha256, kZeros, sizeof(kZeros), sig.data(), &len_u, |
| 1126 | sample.get())); |
| 1127 | sig.resize(len_u); |
| 1128 |
| 1129 | // A public key cannot perform private key operations. |
| 1130 | bssl::UniquePtr<RSA> rsa( |
| 1131 | RSA_new_public_key(RSA_get0_n(sample.get()), RSA_get0_e(sample.get()))); |
| 1132 | ASSERT_TRUE(rsa); |
| 1133 |
| 1134 | std::vector<uint8_t> out(RSA_size(sample.get())); |
| 1135 | EXPECT_FALSE(RSA_sign(NID_sha256, kZeros, sizeof(kZeros), out.data(), &len_u, |
| 1136 | rsa.get())); |
| 1137 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_RSA, RSA_R_VALUE_MISSING)); |
| 1138 |
| 1139 | size_t len; |
| 1140 | EXPECT_FALSE(RSA_decrypt(rsa.get(), &len, out.data(), out.size(), |
| 1141 | kOAEPCiphertext1, sizeof(kOAEPCiphertext1), |
| 1142 | RSA_PKCS1_OAEP_PADDING)); |
| 1143 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_RSA, RSA_R_VALUE_MISSING)); |
| 1144 |
| 1145 | // A private key without e cannot perform public key operations. |
| 1146 | rsa.reset(RSA_new_private_key_no_e(RSA_get0_n(sample.get()), |
| 1147 | RSA_get0_d(sample.get()))); |
| 1148 | ASSERT_TRUE(rsa); |
| 1149 |
| 1150 | EXPECT_FALSE(RSA_verify(NID_sha256, kZeros, sizeof(kZeros), sig.data(), |
| 1151 | sig.size(), rsa.get())); |
| 1152 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_RSA, RSA_R_VALUE_MISSING)); |
| 1153 |
| 1154 | EXPECT_FALSE(RSA_encrypt(rsa.get(), &len, out.data(), out.size(), kPlaintext, |
| 1155 | sizeof(kPlaintext), RSA_PKCS1_OAEP_PADDING)); |
| 1156 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_RSA, RSA_R_VALUE_MISSING)); |
| 1157 | } |
| 1158 |
| 1159 | TEST(RSATest, Negative) { |
| 1160 | auto dup_neg = [](const BIGNUM *bn) -> bssl::UniquePtr<BIGNUM> { |
| 1161 | bssl::UniquePtr<BIGNUM> ret(BN_dup(bn)); |
| 1162 | if (!ret) { |
| 1163 | return nullptr; |
| 1164 | } |
| 1165 | BN_set_negative(ret.get(), 1); |
| 1166 | return ret; |
| 1167 | }; |
| 1168 |
| 1169 | bssl::UniquePtr<RSA> key(RSA_private_key_from_bytes(kKey1, sizeof(kKey1))); |
| 1170 | ASSERT_TRUE(key); |
| 1171 | const BIGNUM *n = RSA_get0_n(key.get()); |
| 1172 | bssl::UniquePtr<BIGNUM> neg_n = dup_neg(n); |
| 1173 | ASSERT_TRUE(neg_n); |
| 1174 | const BIGNUM *e = RSA_get0_e(key.get()); |
| 1175 | bssl::UniquePtr<BIGNUM> neg_e = dup_neg(e); |
| 1176 | ASSERT_TRUE(neg_e); |
| 1177 | const BIGNUM *d = RSA_get0_d(key.get()); |
| 1178 | bssl::UniquePtr<BIGNUM> neg_d = dup_neg(d); |
| 1179 | ASSERT_TRUE(neg_d); |
| 1180 | const BIGNUM *p = RSA_get0_p(key.get()); |
| 1181 | bssl::UniquePtr<BIGNUM> neg_p = dup_neg(p); |
| 1182 | ASSERT_TRUE(neg_p); |
| 1183 | const BIGNUM *q = RSA_get0_q(key.get()); |
| 1184 | bssl::UniquePtr<BIGNUM> neg_q = dup_neg(q); |
| 1185 | ASSERT_TRUE(neg_q); |
| 1186 | const BIGNUM *dmp1 = RSA_get0_dmp1(key.get()); |
| 1187 | bssl::UniquePtr<BIGNUM> neg_dmp1 = dup_neg(dmp1); |
| 1188 | ASSERT_TRUE(neg_dmp1); |
| 1189 | const BIGNUM *dmq1 = RSA_get0_dmq1(key.get()); |
| 1190 | bssl::UniquePtr<BIGNUM> neg_dmq1 = dup_neg(dmq1); |
| 1191 | ASSERT_TRUE(neg_dmq1); |
| 1192 | const BIGNUM *iqmp = RSA_get0_iqmp(key.get()); |
| 1193 | bssl::UniquePtr<BIGNUM> neg_iqmp = dup_neg(iqmp); |
| 1194 | ASSERT_TRUE(neg_iqmp); |
| 1195 |
| 1196 | EXPECT_FALSE(RSA_new_public_key(neg_n.get(), e)); |
| 1197 | EXPECT_FALSE(RSA_new_public_key(n, neg_e.get())); |
| 1198 | EXPECT_FALSE(RSA_new_private_key(neg_n.get(), e, d, p, q, dmp1, dmq1, iqmp)); |
| 1199 | EXPECT_FALSE(RSA_new_private_key(n, neg_e.get(), d, p, q, dmp1, dmq1, iqmp)); |
| 1200 | EXPECT_FALSE(RSA_new_private_key(n, e, neg_d.get(), p, q, dmp1, dmq1, iqmp)); |
| 1201 | EXPECT_FALSE(RSA_new_private_key(n, e, d, neg_p.get(), q, dmp1, dmq1, iqmp)); |
| 1202 | EXPECT_FALSE(RSA_new_private_key(n, e, d, p, neg_q.get(), dmp1, dmq1, iqmp)); |
| 1203 | EXPECT_FALSE(RSA_new_private_key(n, e, d, p, q, neg_dmp1.get(), dmq1, iqmp)); |
| 1204 | EXPECT_FALSE(RSA_new_private_key(n, e, d, p, q, dmp1, neg_dmq1.get(), iqmp)); |
| 1205 | EXPECT_FALSE(RSA_new_private_key(n, e, d, p, q, dmp1, dmq1, neg_iqmp.get())); |
| 1206 | } |
| 1207 |
| 1208 | TEST(RSATest, LargeE) { |
| 1209 | // Test an RSA key with large e by swapping d and e in kKey1. |
| 1210 | // Since e is small, e mod (p-1) and e mod (q-1) will simply be e. |
| 1211 | bssl::UniquePtr<RSA> key(RSA_private_key_from_bytes(kKey1, sizeof(kKey1))); |
| 1212 | ASSERT_TRUE(key); |
| 1213 | const BIGNUM *n = RSA_get0_n(key.get()); |
| 1214 | const BIGNUM *e = RSA_get0_e(key.get()); |
| 1215 | const BIGNUM *d = RSA_get0_d(key.get()); |
| 1216 | const BIGNUM *p = RSA_get0_p(key.get()); |
| 1217 | const BIGNUM *q = RSA_get0_q(key.get()); |
| 1218 | const BIGNUM *iqmp = RSA_get0_iqmp(key.get()); |
| 1219 |
| 1220 | // By default, the large exponent is not allowed as e. |
| 1221 | bssl::UniquePtr<RSA> pub(RSA_new_public_key(n, /*e=*/d)); |
| 1222 | EXPECT_FALSE(pub); |
| 1223 | bssl::UniquePtr<RSA> priv(RSA_new_private_key(n, /*e=*/d, /*d=*/e, p, q, |
| 1224 | /*dmp1=*/e, /*dmq1=*/e, iqmp)); |
| 1225 | EXPECT_FALSE(priv); |
| 1226 |
| 1227 | // Constructing such a key piecemeal also would not work. This was only |
| 1228 | // possible with private APIs, so when |RSA| is opaque, this case will be |
| 1229 | // impossible. |
| 1230 | priv.reset(RSA_new()); |
| 1231 | ASSERT_TRUE(priv); |
| 1232 | priv->n = BN_dup(n); |
| 1233 | ASSERT_TRUE(priv->n); |
| 1234 | priv->e = BN_dup(d); // Swapped |
| 1235 | ASSERT_TRUE(priv->e); |
| 1236 | priv->d = BN_dup(e); |
| 1237 | ASSERT_TRUE(priv->d); |
| 1238 |
| 1239 | static const uint8_t kDigest[32] = {0}; |
| 1240 | std::vector<uint8_t> sig(RSA_size(priv.get())); |
| 1241 | size_t len; |
| 1242 | EXPECT_FALSE(RSA_sign_pss_mgf1(priv.get(), &len, sig.data(), sig.size(), |
| 1243 | kDigest, sizeof(kDigest), EVP_sha256(), |
| 1244 | EVP_sha256(), /*salt_len=*/32)); |
| 1245 |
| 1246 | // But the "large e" APIs tolerate it. |
| 1247 | pub.reset(RSA_new_public_key_large_e(n, /*e=*/d)); |
| 1248 | ASSERT_TRUE(pub); |
| 1249 | priv.reset(RSA_new_private_key_large_e(n, /*e=*/d, /*d=*/e, p, q, /*dmp1=*/e, |
| 1250 | /*dmq1=*/e, iqmp)); |
| 1251 | ASSERT_TRUE(priv); |
| 1252 |
| 1253 | // Test that operations work correctly. |
| 1254 | sig.resize(RSA_size(priv.get())); |
| 1255 | ASSERT_TRUE(RSA_sign_pss_mgf1(priv.get(), &len, sig.data(), sig.size(), |
| 1256 | kDigest, sizeof(kDigest), EVP_sha256(), |
| 1257 | EVP_sha256(), /*salt_len=*/32)); |
| 1258 | sig.resize(len); |
| 1259 |
| 1260 | EXPECT_TRUE(RSA_verify_pss_mgf1(pub.get(), kDigest, sizeof(kDigest), |
| 1261 | EVP_sha256(), EVP_sha256(), /*salt_len=*/32, |
| 1262 | sig.data(), sig.size())); |
| 1263 |
| 1264 | // e = 1 is still invalid. |
| 1265 | EXPECT_FALSE(RSA_new_public_key_large_e(n, BN_value_one())); |
| 1266 |
| 1267 | // e must still be odd. |
| 1268 | bssl::UniquePtr<BIGNUM> bad_e(BN_dup(d)); |
| 1269 | ASSERT_TRUE(bad_e); |
| 1270 | ASSERT_TRUE(BN_add_word(bad_e.get(), 1)); |
| 1271 | EXPECT_FALSE(RSA_new_public_key_large_e(n, bad_e.get())); |
| 1272 |
| 1273 | // e must still be bounded by n. |
| 1274 | bad_e.reset(BN_dup(n)); |
| 1275 | ASSERT_TRUE(bad_e); |
| 1276 | ASSERT_TRUE(BN_add_word(bad_e.get(), 2)); // Preserve parity. |
| 1277 | EXPECT_FALSE(RSA_new_public_key_large_e(n, bad_e.get())); |
| 1278 | } |
| 1279 |
| 1280 | // Test minimum key limits on RSA keys. Currently, we require a minimum of |
| 1281 | // 512-bit RSA. |
| 1282 | // |
| 1283 | // TODO(crbug.com/boringssl/607): Raise this limit. 512-bit RSA was factored in |
| 1284 | // 1999. |
| 1285 | TEST(RSATest, SmallKey) { |
| 1286 | static const uint8_t kRSA511Private[] = { |
| 1287 | 0x30, 0x82, 0x01, 0x39, 0x02, 0x01, 0x00, 0x02, 0x40, 0x56, 0xc1, 0x3d, |
| 1288 | 0xb3, 0x4f, 0xe4, 0xe9, 0x2f, 0x29, 0x8a, 0xd3, 0xe2, 0xfe, 0xb3, 0x3b, |
| 1289 | 0x88, 0x02, 0x8b, 0xdd, 0x44, 0xb5, 0x41, 0x4b, 0x43, 0x97, 0x93, 0x75, |
| 1290 | 0x78, 0x4b, 0x10, 0x30, 0x88, 0xce, 0xd2, 0x32, 0xe3, 0x9e, 0xda, 0x68, |
| 1291 | 0xc9, 0xc3, 0xcd, 0xa1, 0xde, 0xbc, 0x4a, 0xeb, 0x37, 0x60, 0xd2, 0x82, |
| 1292 | 0x2f, 0x5d, 0x21, 0x3b, 0x88, 0x0e, 0x12, 0x44, 0x4d, 0x5d, 0x44, 0xc1, |
| 1293 | 0x9d, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x08, 0xe5, 0xf5, 0x30, |
| 1294 | 0x29, 0x27, 0xaf, 0x8b, 0x38, 0xd5, 0x96, 0x7a, 0x17, 0xe9, 0xc6, 0x57, |
| 1295 | 0x62, 0xfb, 0x79, 0x8c, 0x8c, 0x92, 0xcf, 0xe7, 0x74, 0xea, 0x99, 0x07, |
| 1296 | 0xe7, 0x9b, 0x17, 0x7f, 0x30, 0x9f, 0x86, 0x55, 0x15, 0x8d, 0xe6, 0xa8, |
| 1297 | 0x0d, 0x7b, 0x42, 0x41, 0x27, 0x18, 0x29, 0x55, 0xb1, 0x08, 0x07, 0x2a, |
| 1298 | 0x4e, 0x67, 0x19, 0x9c, 0xe3, 0xe4, 0x84, 0xd6, 0x82, 0x62, 0xd4, 0x81, |
| 1299 | 0x02, 0x21, 0x00, 0xcd, 0x5a, 0x9b, 0x23, 0x3d, 0xb5, 0x9c, 0x56, 0xbc, |
| 1300 | 0xc5, 0x56, 0xcf, 0x77, 0x58, 0xc0, 0x62, 0x72, 0xa0, 0x85, 0x77, 0xf4, |
| 1301 | 0xc3, 0xf8, 0x47, 0x6d, 0xc0, 0x8f, 0x18, 0x77, 0xee, 0xf1, 0xad, 0x02, |
| 1302 | 0x20, 0x6c, 0x26, 0xaa, 0x8a, 0xaf, 0x7b, 0x9f, 0x35, 0x19, 0x08, 0xc2, |
| 1303 | 0xa0, 0x9f, 0x4e, 0x9e, 0x62, 0x48, 0xb1, 0x7c, 0x0e, 0x68, 0x63, 0x0d, |
| 1304 | 0x05, 0x76, 0x73, 0x0a, 0xa0, 0xb3, 0xed, 0x6d, 0xb1, 0x02, 0x21, 0x00, |
| 1305 | 0xc2, 0x26, 0x1c, 0xb0, 0xa7, 0xe2, 0x31, 0x4a, 0x4c, 0x34, 0xe2, 0xcb, |
| 1306 | 0x49, 0x51, 0xce, 0xaa, 0x05, 0x27, 0xc0, 0xa8, 0x55, 0xf0, 0x85, 0xa6, |
| 1307 | 0xba, 0x9c, 0x28, 0x6e, 0x00, 0xce, 0x17, 0x0d, 0x02, 0x20, 0x65, 0x51, |
| 1308 | 0xb0, 0x11, 0xaf, 0x26, 0xbc, 0x57, 0x4d, 0x35, 0xb4, 0xc8, 0x2f, 0x96, |
| 1309 | 0xc2, 0xb0, 0xc6, 0xf3, 0x67, 0x8a, 0x43, 0xe7, 0x0f, 0xaa, 0xdf, 0x76, |
| 1310 | 0x15, 0x2d, 0xca, 0x82, 0x93, 0x71, 0x02, 0x21, 0x00, 0x9e, 0x89, 0x74, |
| 1311 | 0x15, 0x7e, 0x73, 0x43, 0xa0, 0x1e, 0xa9, 0xa5, 0x9f, 0xad, 0xf1, 0xa0, |
| 1312 | 0xfa, 0x13, 0x86, 0x10, 0x3f, 0xb0, 0xba, 0x3f, 0x45, 0x87, 0x13, 0x02, |
| 1313 | 0x86, 0xa4, 0xa4, 0x31, 0x92}; |
| 1314 | static const uint8_t kRSA511Public[] = { |
| 1315 | 0x30, 0x47, 0x02, 0x40, 0x56, 0xc1, 0x3d, 0xb3, 0x4f, 0xe4, 0xe9, |
| 1316 | 0x2f, 0x29, 0x8a, 0xd3, 0xe2, 0xfe, 0xb3, 0x3b, 0x88, 0x02, 0x8b, |
| 1317 | 0xdd, 0x44, 0xb5, 0x41, 0x4b, 0x43, 0x97, 0x93, 0x75, 0x78, 0x4b, |
| 1318 | 0x10, 0x30, 0x88, 0xce, 0xd2, 0x32, 0xe3, 0x9e, 0xda, 0x68, 0xc9, |
| 1319 | 0xc3, 0xcd, 0xa1, 0xde, 0xbc, 0x4a, 0xeb, 0x37, 0x60, 0xd2, 0x82, |
| 1320 | 0x2f, 0x5d, 0x21, 0x3b, 0x88, 0x0e, 0x12, 0x44, 0x4d, 0x5d, 0x44, |
| 1321 | 0xc1, 0x9d, 0x02, 0x03, 0x01, 0x00, 0x01}; |
| 1322 | static const uint8_t kRSA512Private[] = { |
| 1323 | 0x30, 0x82, 0x01, 0x3a, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00, 0xa5, 0x44, |
| 1324 | 0x8f, 0x3d, 0xa2, 0x0b, 0x20, 0xc6, 0xac, 0x10, 0xc1, 0x27, 0x11, 0xf0, |
| 1325 | 0x43, 0x5d, 0x05, 0xb7, 0x0f, 0x80, 0x3b, 0x9b, 0x85, 0xf1, 0x7a, 0x0e, |
| 1326 | 0xbd, 0x72, 0xed, 0x8a, 0xdc, 0xa1, 0xaa, 0xd4, 0x53, 0xcb, 0x65, 0x78, |
| 1327 | 0x4b, 0x30, 0x6b, 0x52, 0x51, 0xee, 0xcd, 0x2f, 0x90, 0x7b, 0xd1, 0x9c, |
| 1328 | 0xe9, 0x79, 0x98, 0x58, 0xe3, 0x47, 0x35, 0xa7, 0xcd, 0x6a, 0x71, 0x38, |
| 1329 | 0xb5, 0x0d, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x41, 0x00, 0x94, 0x24, |
| 1330 | 0x82, 0xa9, 0xe2, 0xa9, 0x4a, 0xf6, 0x0b, 0xa2, 0xf1, 0x21, 0x0e, 0x89, |
| 1331 | 0x6a, 0x38, 0xe6, 0x38, 0x93, 0xe2, 0x84, 0x8c, 0x02, 0x62, 0xd4, 0xe0, |
| 1332 | 0x85, 0x9d, 0x91, 0xa4, 0xd9, 0xe3, 0x77, 0x6c, 0x26, 0x85, 0xf6, 0x2e, |
| 1333 | 0x0a, 0xe4, 0x18, 0x73, 0x06, 0x9a, 0xea, 0xde, 0x78, 0x65, 0xba, 0x7a, |
| 1334 | 0xdb, 0xc0, 0x3b, 0xf7, 0x29, 0x1e, 0x43, 0xed, 0xaf, 0xf5, 0xaf, 0xa8, |
| 1335 | 0xdf, 0x01, 0x02, 0x21, 0x00, 0xdb, 0x93, 0x05, 0x2d, 0xf3, 0xdf, 0xe4, |
| 1336 | 0x31, 0xef, 0x50, 0xc7, 0x54, 0x0f, 0x08, 0x5d, 0x50, 0x42, 0xfa, 0xb9, |
| 1337 | 0x20, 0x37, 0x98, 0xd3, 0xc0, 0x64, 0x2f, 0xb6, 0xe4, 0xb2, 0xfe, 0xe5, |
| 1338 | 0x6d, 0x02, 0x21, 0x00, 0xc0, 0xaf, 0x3a, 0x1f, 0xd9, 0xba, 0x5a, 0x6a, |
| 1339 | 0xc2, 0x80, 0x2e, 0x7b, 0x65, 0x3d, 0x8a, 0x76, 0xae, 0x4b, 0x5a, 0xff, |
| 1340 | 0x7a, 0x9a, 0x5e, 0xc2, 0xfa, 0x07, 0xfb, 0x2d, 0x0c, 0x16, 0x6a, 0x21, |
| 1341 | 0x02, 0x20, 0x06, 0xf3, 0xb9, 0xb7, 0x41, 0xc0, 0x75, 0xfe, 0x2a, 0xc0, |
| 1342 | 0x98, 0xff, 0x0d, 0x56, 0xcb, 0x75, 0x8e, 0x19, 0x58, 0x21, 0x30, 0x01, |
| 1343 | 0x73, 0xba, 0xe4, 0xb1, 0x2a, 0x0e, 0x45, 0xa8, 0x92, 0x65, 0x02, 0x20, |
| 1344 | 0x25, 0xcd, 0xbb, 0x3f, 0xa8, 0x7e, 0x11, 0x63, 0x44, 0xc9, 0xd5, 0x54, |
| 1345 | 0xcc, 0x66, 0x28, 0x96, 0x64, 0x57, 0xd0, 0x80, 0xb3, 0x53, 0x3a, 0x28, |
| 1346 | 0x52, 0xd9, 0xe2, 0x03, 0xd2, 0x8d, 0x4b, 0x41, 0x02, 0x20, 0x09, 0x30, |
| 1347 | 0xd9, 0xfd, 0xad, 0x31, 0x1a, 0x38, 0xb7, 0x71, 0x06, 0xed, 0x49, 0xa6, |
| 1348 | 0xe2, 0xec, 0x42, 0xc2, 0x8e, 0xe9, 0xec, 0xf7, 0x3e, 0xb7, 0x4a, 0x5e, |
| 1349 | 0x2e, 0xa2, 0x7a, 0x8d, 0xa4, 0x95}; |
| 1350 | static const uint8_t kRSA512Public[] = { |
| 1351 | 0x30, 0x48, 0x02, 0x41, 0x00, 0xa5, 0x44, 0x8f, 0x3d, 0xa2, 0x0b, |
| 1352 | 0x20, 0xc6, 0xac, 0x10, 0xc1, 0x27, 0x11, 0xf0, 0x43, 0x5d, 0x05, |
| 1353 | 0xb7, 0x0f, 0x80, 0x3b, 0x9b, 0x85, 0xf1, 0x7a, 0x0e, 0xbd, 0x72, |
| 1354 | 0xed, 0x8a, 0xdc, 0xa1, 0xaa, 0xd4, 0x53, 0xcb, 0x65, 0x78, 0x4b, |
| 1355 | 0x30, 0x6b, 0x52, 0x51, 0xee, 0xcd, 0x2f, 0x90, 0x7b, 0xd1, 0x9c, |
| 1356 | 0xe9, 0x79, 0x98, 0x58, 0xe3, 0x47, 0x35, 0xa7, 0xcd, 0x6a, 0x71, |
| 1357 | 0x38, 0xb5, 0x0d, 0x02, 0x03, 0x01, 0x00, 0x01}; |
| 1358 |
| 1359 | bssl::UniquePtr<RSA> rsa( |
| 1360 | RSA_public_key_from_bytes(kRSA511Public, sizeof(kRSA511Public))); |
| 1361 | EXPECT_FALSE(rsa); |
| 1362 | rsa.reset(RSA_private_key_from_bytes(kRSA511Private, sizeof(kRSA511Private))); |
| 1363 | EXPECT_FALSE(rsa); |
| 1364 |
| 1365 | rsa.reset(RSA_public_key_from_bytes(kRSA512Public, sizeof(kRSA512Public))); |
| 1366 | EXPECT_TRUE(rsa); |
| 1367 | rsa.reset(RSA_private_key_from_bytes(kRSA512Private, sizeof(kRSA512Private))); |
| 1368 | EXPECT_TRUE(rsa); |
| 1369 | } |
| 1370 |
| 1371 | #if !defined(BORINGSSL_SHARED_LIBRARY) |
| 1372 | TEST(RSATest, SqrtTwo) { |
| 1373 | bssl::UniquePtr<BIGNUM> sqrt(BN_new()), pow2(BN_new()); |
| 1374 | bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new()); |
| 1375 | ASSERT_TRUE(sqrt); |
| 1376 | ASSERT_TRUE(pow2); |
| 1377 | ASSERT_TRUE(ctx); |
| 1378 |
| 1379 | size_t bits = kBoringSSLRSASqrtTwoLen * BN_BITS2; |
| 1380 | ASSERT_TRUE(BN_one(pow2.get())); |
| 1381 | ASSERT_TRUE(BN_lshift(pow2.get(), pow2.get(), 2 * bits - 1)); |
| 1382 |
| 1383 | // Check that sqrt² < pow2. |
| 1384 | ASSERT_TRUE( |
| 1385 | bn_set_words(sqrt.get(), kBoringSSLRSASqrtTwo, kBoringSSLRSASqrtTwoLen)); |
| 1386 | ASSERT_TRUE(BN_sqr(sqrt.get(), sqrt.get(), ctx.get())); |
| 1387 | EXPECT_LT(BN_cmp(sqrt.get(), pow2.get()), 0); |
| 1388 |
| 1389 | // Check that pow2 < (sqrt + 1)². |
| 1390 | ASSERT_TRUE( |
| 1391 | bn_set_words(sqrt.get(), kBoringSSLRSASqrtTwo, kBoringSSLRSASqrtTwoLen)); |
| 1392 | ASSERT_TRUE(BN_add_word(sqrt.get(), 1)); |
| 1393 | ASSERT_TRUE(BN_sqr(sqrt.get(), sqrt.get(), ctx.get())); |
| 1394 | EXPECT_LT(BN_cmp(pow2.get(), sqrt.get()), 0); |
| 1395 |
| 1396 | // Check the kBoringSSLRSASqrtTwo is sized for a 4096-bit RSA key. |
| 1397 | EXPECT_EQ(4096u / 2u, bits); |
| 1398 | } |
| 1399 | #endif // !BORINGSSL_SHARED_LIBRARY |
| 1400 |
| 1401 | #if defined(OPENSSL_THREADS) |
| 1402 | TEST(RSATest, Threads) { |
| 1403 | bssl::UniquePtr<RSA> rsa_template( |
| 1404 | RSA_private_key_from_bytes(kKey1, sizeof(kKey1))); |
| 1405 | ASSERT_TRUE(rsa_template); |
| 1406 |
| 1407 | const uint8_t kDummyHash[32] = {0}; |
| 1408 | std::vector<uint8_t> sig(RSA_size(rsa_template.get())); |
| 1409 | unsigned sig_len; |
| 1410 | EXPECT_TRUE(RSA_sign(NID_sha256, kDummyHash, sizeof(kDummyHash), sig.data(), |
| 1411 | &sig_len, rsa_template.get())); |
| 1412 | sig.resize(sig_len); |
| 1413 |
| 1414 | // RSA keys may be assembled piece-meal and then used in parallel between |
| 1415 | // threads, which requires internal locking to create some derived properties. |
| 1416 | bssl::UniquePtr<RSA> rsa(RSA_new()); |
| 1417 | rsa->n = BN_dup(rsa_template->n); |
| 1418 | ASSERT_TRUE(rsa->n); |
| 1419 | rsa->e = BN_dup(rsa_template->e); |
| 1420 | ASSERT_TRUE(rsa->e); |
| 1421 | rsa->d = BN_dup(rsa_template->d); |
| 1422 | ASSERT_TRUE(rsa->d); |
| 1423 | rsa->p = BN_dup(rsa_template->p); |
| 1424 | ASSERT_TRUE(rsa->p); |
| 1425 | rsa->q = BN_dup(rsa_template->q); |
| 1426 | ASSERT_TRUE(rsa->q); |
| 1427 | rsa->dmp1 = BN_dup(rsa_template->dmp1); |
| 1428 | ASSERT_TRUE(rsa->dmp1); |
| 1429 | rsa->dmq1 = BN_dup(rsa_template->dmq1); |
| 1430 | ASSERT_TRUE(rsa->dmq1); |
| 1431 | rsa->iqmp = BN_dup(rsa_template->iqmp); |
| 1432 | ASSERT_TRUE(rsa->iqmp); |
| 1433 |
| 1434 | // Each of these operations must be safe to do concurrently on different |
| 1435 | // threads. |
| 1436 | auto raw_access = [&] { EXPECT_EQ(0, BN_cmp(rsa->d, rsa_template->d)); }; |
| 1437 | auto getter = [&] { |
| 1438 | const BIGNUM *d; |
| 1439 | RSA_get0_key(rsa.get(), nullptr, nullptr, &d); |
| 1440 | EXPECT_EQ(0, BN_cmp(d, rsa_template->d)); |
| 1441 | }; |
| 1442 | auto sign = [&] { |
| 1443 | std::vector<uint8_t> sig2(RSA_size(rsa.get())); |
| 1444 | unsigned sig2_len; |
| 1445 | EXPECT_TRUE(RSA_sign(NID_sha256, kDummyHash, sizeof(kDummyHash), |
| 1446 | sig2.data(), &sig2_len, rsa.get())); |
| 1447 | sig2.resize(sig2_len); |
| 1448 | // RSASSA-PKCS1-v1_5 is deterministic. |
| 1449 | EXPECT_EQ(Bytes(sig), Bytes(sig2)); |
| 1450 | }; |
| 1451 | auto verify = [&] { |
| 1452 | EXPECT_TRUE(RSA_verify(NID_sha256, kDummyHash, sizeof(kDummyHash), |
| 1453 | sig.data(), sig.size(), rsa.get())); |
| 1454 | }; |
| 1455 |
| 1456 | std::vector<std::thread> threads; |
| 1457 | threads.emplace_back(raw_access); |
| 1458 | threads.emplace_back(raw_access); |
| 1459 | threads.emplace_back(getter); |
| 1460 | threads.emplace_back(getter); |
| 1461 | threads.emplace_back(sign); |
| 1462 | threads.emplace_back(sign); |
| 1463 | threads.emplace_back(verify); |
| 1464 | threads.emplace_back(verify); |
| 1465 | for (auto &thread : threads) { |
| 1466 | thread.join(); |
| 1467 | } |
| 1468 | } |
| 1469 |
| 1470 | // This test might be excessively slow on slower CPUs or platforms that do not |
| 1471 | // expect server workloads. It is disabled by default and reenabled on some |
| 1472 | // platforms when running tests standalone via all_tests.go. |
| 1473 | // |
| 1474 | // Additionally, even when running disabled tests standalone, limit this to |
| 1475 | // x86_64. On other platforms, this test hits resource limits or is too slow. We |
| 1476 | // also disable on FreeBSD. See https://crbug.com/boringssl/603. |
| 1477 | #if defined(OPENSSL_TSAN) || \ |
| 1478 | (defined(OPENSSL_X86_64) && !defined(OPENSSL_FREEBSD)) |
| 1479 | TEST(RSATest, DISABLED_BlindingCacheConcurrency) { |
| 1480 | bssl::UniquePtr<RSA> rsa(RSA_private_key_from_bytes(kKey1, sizeof(kKey1))); |
| 1481 | ASSERT_TRUE(rsa); |
| 1482 |
| 1483 | #if defined(OPENSSL_TSAN) |
| 1484 | constexpr size_t kSignaturesPerThread = 10; |
| 1485 | constexpr size_t kNumThreads = 10; |
| 1486 | #else |
| 1487 | constexpr size_t kSignaturesPerThread = 100; |
| 1488 | constexpr size_t kNumThreads = 2048; |
| 1489 | #endif |
| 1490 |
| 1491 | const uint8_t kDummyHash[32] = {0}; |
| 1492 | auto worker = [&] { |
| 1493 | std::vector<uint8_t> sig(RSA_size(rsa.get())); |
| 1494 | for (size_t i = 0; i < kSignaturesPerThread; i++) { |
| 1495 | unsigned sig_len = sig.size(); |
| 1496 | EXPECT_TRUE(RSA_sign(NID_sha256, kDummyHash, sizeof(kDummyHash), |
| 1497 | sig.data(), &sig_len, rsa.get())); |
| 1498 | } |
| 1499 | }; |
| 1500 |
| 1501 | std::vector<std::thread> threads; |
| 1502 | threads.reserve(kNumThreads); |
| 1503 | for (size_t i = 0; i < kNumThreads; i++) { |
| 1504 | threads.emplace_back(worker); |
| 1505 | } |
| 1506 | for (auto &thread : threads) { |
| 1507 | thread.join(); |
| 1508 | } |
| 1509 | } |
| 1510 | #endif // TSAN || (X86_64 && !FREEBSD) |
| 1511 |
| 1512 | #endif // THREADS |
| 1513 |
| 1514 | } // namespace |
| 1515 |