forked from aws/aws-lc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
298 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR ISC | ||
|
||
// | ||
// Created by Smith, Justin on 9/10/24. | ||
// | ||
|
||
#include <iostream> | ||
|
||
#include "internal.h" | ||
using namespace std; | ||
|
||
#include <gtest/gtest.h> | ||
#include <openssl/evp.h> | ||
#include <openssl/rsa.h> | ||
|
||
class EvpPkeyCtxCtrlStrTest : public ::testing::Test { | ||
protected: | ||
void SetUp() override { | ||
} | ||
|
||
void TearDown() override { | ||
} | ||
|
||
}; | ||
|
||
|
||
|
||
static bssl::UniquePtr<EVP_PKEY_CTX> gen_RSA_inner(const int id) { | ||
EVP_PKEY* raw = nullptr; | ||
bssl::UniquePtr<EVP_PKEY_CTX> keygen_ctx(EVP_PKEY_CTX_new_id(id, nullptr)); | ||
if(!EVP_PKEY_keygen_init(keygen_ctx.get()) || | ||
!EVP_PKEY_CTX_set_rsa_keygen_bits(keygen_ctx.get(), 2048) || | ||
!EVP_PKEY_keygen(keygen_ctx.get(), &raw)) { | ||
return nullptr; | ||
} | ||
return bssl::UniquePtr<EVP_PKEY_CTX>(EVP_PKEY_CTX_new(raw, nullptr)); | ||
} | ||
|
||
static bssl::UniquePtr<EVP_PKEY_CTX> gen_RSA() { | ||
return gen_RSA_inner(EVP_PKEY_RSA); | ||
} | ||
|
||
// static bssl::UniquePtr<EVP_PKEY_CTX> gen_RSA_PSS() { | ||
// return gen_RSA_inner(EVP_PKEY_RSA_PSS); | ||
// } | ||
|
||
TEST_F(EvpPkeyCtxCtrlStrTest, RsaMissingValue) { | ||
// Create a EVP_PKEY_CTX with a newly generated RSA key | ||
bssl::UniquePtr<EVP_PKEY_CTX>ctx = gen_RSA(); | ||
ASSERT_TRUE(ctx); | ||
EXPECT_FALSE(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_padding_mode", nullptr)); | ||
unsigned long err = ERR_get_error(); | ||
EXPECT_EQ(ERR_GET_LIB(err), ERR_LIB_EVP); | ||
EXPECT_EQ(ERR_GET_REASON(err), RSA_R_VALUE_MISSING); | ||
} | ||
|
||
TEST_F(EvpPkeyCtxCtrlStrTest, RsaPaddingModeValid) { | ||
bssl::UniquePtr<EVP_PKEY_CTX> ctx = gen_RSA(); | ||
ASSERT_TRUE(ctx); | ||
|
||
int padding = 0; | ||
|
||
// Padding for sign | ||
ASSERT_TRUE(EVP_PKEY_sign_init(ctx.get())); | ||
|
||
EXPECT_TRUE(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_padding_mode", "pkcs1")); | ||
EXPECT_TRUE(EVP_PKEY_CTX_get_rsa_padding(ctx.get(), &padding)); | ||
EXPECT_EQ(padding, RSA_PKCS1_PADDING); | ||
|
||
EXPECT_EQ(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_padding_mode", "none"), 1); | ||
EXPECT_TRUE(EVP_PKEY_CTX_get_rsa_padding(ctx.get(), &padding)); | ||
EXPECT_EQ(padding, RSA_NO_PADDING); | ||
|
||
EXPECT_EQ(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_padding_mode", "pss"), 1); | ||
EXPECT_TRUE(EVP_PKEY_CTX_get_rsa_padding(ctx.get(), &padding)); | ||
EXPECT_EQ(padding, RSA_PKCS1_PSS_PADDING); | ||
|
||
// Padding for encrypt | ||
ASSERT_TRUE(EVP_PKEY_encrypt_init(ctx.get())); | ||
|
||
EXPECT_EQ(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_padding_mode", "oaep"), 1); | ||
EXPECT_TRUE(EVP_PKEY_CTX_get_rsa_padding(ctx.get(), &padding)); | ||
EXPECT_EQ(padding, RSA_PKCS1_OAEP_PADDING); | ||
|
||
EXPECT_EQ(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_padding_mode", "oeap"), 1); | ||
EXPECT_TRUE(EVP_PKEY_CTX_get_rsa_padding(ctx.get(), &padding)); | ||
EXPECT_EQ(padding, RSA_PKCS1_OAEP_PADDING); | ||
} | ||
|
||
TEST_F(EvpPkeyCtxCtrlStrTest, RsaPssSaltlen) { | ||
// Create a EVP_PKEY_CTX with a newly generated RSA key | ||
bssl::UniquePtr<EVP_PKEY_CTX>ctx = gen_RSA(); | ||
ASSERT_TRUE(ctx); | ||
|
||
ASSERT_TRUE(EVP_PKEY_sign_init(ctx.get())); | ||
EXPECT_EQ(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_padding_mode", "pss"), 1); | ||
EXPECT_EQ(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_pss_saltlen", "128"), 1); | ||
|
||
int saltlen = 0; | ||
EXPECT_EQ(EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx.get(), &saltlen), 1); | ||
EXPECT_EQ(saltlen, 128); | ||
|
||
EXPECT_EQ(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_pss_saltlen", "digest"), 1); | ||
EXPECT_EQ(EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx.get(), &saltlen), 1); | ||
EXPECT_EQ(saltlen, -1); | ||
} | ||
|
||
TEST_F(EvpPkeyCtxCtrlStrTest, RsaKeygenBits) { | ||
// Create a EVP_PKEY_CTX with a newly generated RSA key | ||
EVP_PKEY* raw = nullptr; | ||
bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr)); | ||
ASSERT_TRUE(ctx); | ||
ASSERT_TRUE(EVP_PKEY_keygen_init(ctx.get())); | ||
ASSERT_EQ(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_keygen_bits", "2048"), 1); | ||
ASSERT_TRUE(EVP_PKEY_keygen(ctx.get(), &raw)); | ||
|
||
ASSERT_EQ(EVP_PKEY_bits(raw), 2048); | ||
} | ||
|
||
TEST_F(EvpPkeyCtxCtrlStrTest, RsaKeygenPubexp) { | ||
// Create a EVP_PKEY_CTX with a newly generated RSA key | ||
EVP_PKEY* raw = nullptr; | ||
bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr)); | ||
ASSERT_TRUE(ctx); | ||
ASSERT_TRUE(EVP_PKEY_keygen_init(ctx.get())); | ||
ASSERT_EQ(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_keygen_pubexp", "729"), 1); | ||
ASSERT_TRUE(EVP_PKEY_keygen(ctx.get(), &raw)); | ||
|
||
bssl::UniquePtr<RSA> rsa_key(EVP_PKEY_get1_RSA(raw)); | ||
ASSERT_TRUE(rsa_key); | ||
const BIGNUM* const_pe_bn = RSA_get0_e(rsa_key.get()); | ||
ASSERT_TRUE(const_pe_bn != nullptr); | ||
|
||
const uint64_t expected_pe = 729; | ||
uint64_t pe_u64; | ||
ASSERT_TRUE(BN_get_u64(const_pe_bn, &pe_u64)); | ||
EXPECT_EQ(pe_u64, expected_pe); | ||
} | ||
|
||
TEST_F(EvpPkeyCtxCtrlStrTest, RsaMgf1Md) { | ||
// rsa_mgf1_md | ||
bssl::UniquePtr<EVP_PKEY_CTX> ctx = gen_RSA(); | ||
ASSERT_TRUE(ctx); | ||
|
||
ASSERT_TRUE(EVP_PKEY_sign_init(ctx.get())); | ||
ASSERT_TRUE(EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_PKCS1_PSS_PADDING)); | ||
EXPECT_EQ(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_mgf1_md", "sha256"), 1); | ||
|
||
const EVP_MD *out_md; | ||
ASSERT_TRUE(EVP_PKEY_CTX_get_rsa_mgf1_md(ctx.get(), &out_md)); | ||
EXPECT_STREQ(EVP_MD_name(out_md), "SHA256"); | ||
} | ||
|
||
//rsa_pss_keygen_mgf1_md | ||
TEST_F(EvpPkeyCtxCtrlStrTest, RsaPssKeygenMgf1Md) { | ||
EVP_PKEY* raw = nullptr; | ||
bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_RSA_PSS, nullptr)); | ||
ASSERT_TRUE(ctx); | ||
ASSERT_TRUE(EVP_PKEY_keygen_init(ctx.get())); | ||
|
||
EXPECT_EQ(EVP_PKEY_CTX_ctrl_str(ctx.get(), "rsa_pss_keygen_mgf1_md", "sha256"), 1); | ||
|
||
const EVP_MD *out_md; | ||
ASSERT_TRUE(EVP_PKEY_CTX_get_rsa_mgf1_md(ctx.get(), &out_md)); | ||
EXPECT_STREQ(EVP_MD_name(out_md), "SHA256"); | ||
|
||
ASSERT_TRUE(EVP_PKEY_keygen(ctx.get(), &raw)); | ||
EVP_PKEY_free(raw); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters