blob: 6efd1530e70230698956ec616667174c0d7fbb6e [file] [log] [blame]
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cryptohome/cryptolib.h"
#include <openssl/rsa.h>
#include <brillo/secure_blob.h>
#include <crypto/scoped_openssl_types.h>
#include <gtest/gtest.h>
using brillo::SecureBlob;
namespace cryptohome {
TEST(CryptoLibTest, RsaOaepDecrypt) {
// Generate the input data.
constexpr int kKeySizeBits = 1024;
constexpr int kKeySizeBytes = kKeySizeBits / 8;
constexpr int kPlaintextSize = 32;
crypto::ScopedRSA rsa(
RSA_generate_key(kKeySizeBits, kWellKnownExponent, nullptr, nullptr));
SecureBlob plaintext(kPlaintextSize);
CryptoLib::GetSecureRandom(, plaintext.size());
// Test decryption when a non-empty label is used.
const SecureBlob kFirstOaepLabel("foo");
SecureBlob first_padded_data(kKeySizeBytes);
1, RSA_padding_add_PKCS1_OAEP(, kKeySizeBytes,,
plaintext.size(),, kFirstOaepLabel.size()));
SecureBlob first_ciphertext(kKeySizeBytes);
ASSERT_NE(-1, RSA_public_encrypt(kKeySizeBytes,,, rsa.get(),
SecureBlob first_decrypt_result;
EXPECT_TRUE(CryptoLib::RsaOaepDecrypt(first_ciphertext, kFirstOaepLabel,
rsa.get(), &first_decrypt_result));
EXPECT_EQ(plaintext, first_decrypt_result);
// Test the empty label case in which the encryption is done by a single call
// to OpenSSL.
SecureBlob second_ciphertext(kKeySizeBytes);
ASSERT_NE(-1, RSA_public_encrypt(kPlaintextSize,,, rsa.get(),
SecureBlob second_decrypt_result;
EXPECT_TRUE(CryptoLib::RsaOaepDecrypt(second_ciphertext, SecureBlob(),
rsa.get(), &second_decrypt_result));
EXPECT_EQ(plaintext, second_decrypt_result);
TEST(CryptoLibTest, TestRocaVulnerable) {
// This is a modulus from key generated by a TPM running vulnerable firmware.
const uint8_t vulnerable_modulus[] = {
0x00, 0x9e, 0x31, 0xea, 0x73, 0xed, 0x06, 0x22,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5b, 0x79, 0xc7, 0x12, 0x78, 0x2d, 0x8a, 0x8f, 0x2d, 0x00, 0xf7,
0x0d, 0x5e, 0x00, 0xa0, 0x35, 0x9a, 0x02, 0xb0, 0x73, 0xad, 0xbc, 0x44,
0xd2, 0x67, 0x73, 0x64, 0x08, 0xc8, 0x60, 0x58, 0x04, 0xf1, 0xa5, 0xd2,
0xd5, 0x18, 0x4e, 0x39, 0x3e, 0x68, 0xe6, 0xfa, 0xa7, 0x55, 0xd9, 0xeb,
0xd8, 0x5f, 0xe7, 0xde, 0xab, 0x2e, 0x8b, 0x17, 0x5d, 0x08, 0x79, 0x6b,
0x7a, 0x7e, 0xf0, 0x06, 0x61,
crypto::ScopedBIGNUM vulnerable_modulus_bn(
BN_bin2bn(vulnerable_modulus, sizeof(vulnerable_modulus), nullptr));
// A key generated by a non-vulnerable TPM.
const uint8_t good_modulus[] = {
0x00, 0xcc, 0xe8, 0xcf, 0xb5, 0x6e, 0x36, 0x99, 0x21, 0x7b, 0x95, 0xb9,
0x75, 0xa6, 0x80, 0x12, 0xb0, 0x54, 0x1c, 0x62, 0x10, 0x77, 0x06, 0xbf,
0x2c, 0xad, 0xa6, 0x5a, 0x79, 0x6a, 0x23, 0x06, 0x87, 0x2a, 0xf8, 0x37,
0x4c, 0x47, 0xa7, 0xcf, 0x82, 0x7e, 0xa1, 0xd5, 0x73, 0x56, 0x04, 0xc4,
0x60, 0xd7, 0x43, 0x5d, 0xa6, 0x6b, 0x44, 0x83, 0x77, 0xf9, 0x72, 0xff,
0x7d, 0xc4, 0x5c, 0x74, 0x3a, 0x43, 0x97, 0x68, 0xa1, 0x01, 0x57, 0x94,
0x22, 0xd8, 0xea, 0x19, 0x50, 0xf0, 0x4d, 0x29, 0x59, 0x04, 0xca, 0x92,
0x64, 0xb1, 0x3e, 0x13, 0x9e, 0x38, 0x82, 0xbf, 0xaa, 0xb5, 0x25, 0x57,
0xa1, 0xe0, 0x46, 0x89, 0x7f, 0x5d, 0x22, 0x03, 0x82, 0x89, 0x93, 0xa7,
0x6f, 0xb9, 0xb5, 0x2f, 0x51, 0x98, 0xa1, 0x8a, 0xae, 0xca, 0x97, 0x6b,
0x1d, 0x33, 0xbf, 0xc0, 0x04, 0x63, 0x47, 0x04, 0x5c, 0xfc, 0x98, 0x88,
0x6c, 0xb1, 0x05, 0x9b, 0xab, 0x69, 0x91, 0xca, 0xab, 0xa0, 0x39, 0x62,
0xcd, 0x0e, 0xa2, 0xb0, 0x04, 0x36, 0xa3, 0x1f, 0x08, 0x82, 0xf0, 0x16,
0xd9, 0xf8, 0xdf, 0x08, 0xaa, 0xa6, 0xac, 0x2e, 0x60, 0x77, 0xb3, 0xbb,
0x17, 0x71, 0x60, 0x7e, 0xb1, 0x46, 0x0d, 0x7b, 0xf2, 0x81, 0xef, 0x45,
0xb0, 0xa5, 0xbd, 0x3f, 0x8a, 0xe4, 0x3d, 0x81, 0x51, 0x3b, 0xbe, 0xc4,
0x84, 0x5d, 0x82, 0xba, 0xff, 0xca, 0x6c, 0x21, 0x90, 0x9c, 0x94, 0x3f,
0x1e, 0x34, 0x41, 0x02, 0x87, 0xcb, 0xa9, 0xd8, 0x01, 0x48, 0xe5, 0x8b,
0x7f, 0x38, 0xd4, 0x6e, 0xf3, 0xf8, 0x7b, 0xd8, 0xa3, 0x8e, 0x3d, 0xb9,
0x58, 0x8c, 0xab, 0x57, 0x03, 0x3b, 0xff, 0x94, 0x0b, 0x8b, 0x94, 0xf4,
0x36, 0xd7, 0x7f, 0x4f, 0xf6, 0x56, 0x3f, 0x80, 0x2a, 0x4a, 0xea, 0xfd,
0x74, 0x20, 0x5f, 0x90, 0xa3,
crypto::ScopedBIGNUM good_modulus_bn(
BN_bin2bn(good_modulus, sizeof(good_modulus), nullptr));
} // namespace cryptohome