签名验证需要什么必要信息?

问题描述 投票:0回答:1

我基于rsa编写了一个签名和验证完整过程的测试演示,可帮助我弄清该过程的逻辑。

# https://cryptography.io/en/latest/hazmat/primitives/asymmetric/rsa
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa

# Preparation phase
# Generate key pairs
# private_key contains the both private key and public key
private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048,
    backend=default_backend()
)

# Serilize the keys
from cryptography.hazmat.primitives import serialization

pem = private_key.private_bytes(
   encoding=serialization.Encoding.PEM,
   format=serialization.PrivateFormat.PKCS8,
   encryption_algorithm=serialization.BestAvailableEncryption(b'mypassword')
)
with open('private-key.pem', 'wb') as f:
    f.write(pem)
    f.close()

public_key = private_key.public_key()
pem = public_key.public_bytes(
   encoding=serialization.Encoding.PEM,
   format=serialization.PublicFormat.SubjectPublicKeyInfo
)
with open('public-key.pem', 'wb') as f:
    f.write(pem)
    f.close()

# Signer
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.asymmetric import utils

with open('private-key.pem', 'rb') as f:
    private_key = serialization.load_pem_private_key(
        f.read(),
        password=b'mypassword',
        backend=default_backend()
    )

    chosen_hash = hashes.SHA256()
    hasher = hashes.Hash(chosen_hash, default_backend())
    hasher.update(b"data & ")
    hasher.update(b"more data")
    digest = hasher.finalize()

    signature = private_key.sign(
        digest,
        padding.PSS(
            mgf=padding.MGF1(hashes.SHA256()),
            salt_length=padding.PSS.MAX_LENGTH
        ),
        utils.Prehashed(chosen_hash)
    )

    with open('signature', 'wb') as f:
        f.write(signature)
        f.close()

# Verifier
chosen_hash = hashes.SHA256()
hasher = hashes.Hash(chosen_hash, default_backend())
hasher.update(b"data & ")
hasher.update(b"more data")
digest = hasher.finalize()

hasher1 = hashes.Hash(chosen_hash, default_backend())
hasher1.update(b"data & more data")
digest1 = hasher1.finalize()
print(digest == digest1)

with open('signature', 'rb') as f:
    signature = f.read()

with open('public-key.pem', 'rb') as f:
    public_key  = serialization.load_pem_public_key(
        f.read(),
        backend=default_backend()
    )

    if isinstance(public_key, rsa.RSAPublicKey):
        public_key.verify(
            signature,
            digest,
            padding.PSS(
                mgf=padding.MGF1(hashes.SHA256()),
                salt_length=padding.PSS.MAX_LENGTH
            ),
            utils.Prehashed(chosen_hash)
        )

问题:

验证时是否必须将填充类型(例如PSS)称为输入?

但是在 CLI Generate EC KeyPair from OpenSSL command line中>

openssl dgst -sha256 -verify public.pem -signature msg.signature.txt msg.digest.txt

为什么这里没有提到填充?我认为无论密钥对算法(ECC或RSA)是否相同,(标准?)验证方法的输入参数都应相同。

另一个问题,我在python中看到isinstance(public_key, rsa.RSAPublicKey)可以找出密钥的算法。

验证方法也需要算法类型吗?

像库内部一样,可能具有这种ecc_verify rsa_verify方法。

顺便说一句,verify方法参数(与openssl CLI相同:]

  • 公钥
  • 哈希类型
  • 签名

我基于rsa编写了签名和验证完整过程的测试演示,这有助于我弄清过程的逻辑。 #https://cryptography.io/en/latest/hazmat/primitives / ...

python openssl cryptography rsa pycrypto
1个回答
0
投票

验证时是否必须将填充类型(例如PSS)称为输入?

© www.soinside.com 2019 - 2024. All rights reserved.