在 iOS 上签名的数据无法在 Java 中验证

Data signed on iOS can't be verified in Java

我有一些数据正在 iOS 上使用椭圆曲线私钥与 SecKeyRawSign 签署。但是,使用 Signature.verify() returns false

验证 Java 中的数据

数据是一个随机的 64 位整数,像这样分成字节

uint64_t nonce = (some 64 bit integer)
NSData *nonceData = [NSData dataWithBytes: &nonce length: sizeof(nonce)];

根据该数据,我正在创建 SHA256 摘要

int digestLength = CC_SHA256_DIGEST_LENGTH;
uint8_t *digest = malloc(digestLength);
CC_SHA256(nonceData.bytes, (CC_LONG)nonceData.length, digest);
NSData *digestData = [NSData dataWithBytes:digest length:digestLength];

然后用私钥签名

size_t signedBufferSize = kMaxCipherBufferSize;
uint8_t *signedBuffer = malloc(kMaxCipherBufferSize);

OSStatus status = SecKeyRawSign(privateKeyRef,
                                kSecPaddingPKCS1SHA256,
                                (const uint8_t *)digestData.bytes,
                                digestData.length,
                                &signedBuffer[0],
                                &signedBufferSize);

NSData *signedData = nil;
if (status == errSecSuccess) {
    signedData = [NSData dataWithBytes:signedBuffer length:signedBufferSize];
}

一切正常。

然后,在 Java 服务器中,我试图验证签名数据

PublicKey publicKey = (a public key sent from iOS, X509 encoded)

Long nonce = (64 bit integer sent from iOS)
String signedNonce = (base64 encoded signed data)

ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(nonce);
byte[] nonceBytes = buffer.array();
byte[] signedNonceBytes = Base64.getDecoder().decode(signedNonce.getBytes());

Signature signer = Signature.getInstance( "SHA256withECDSA" );
signer.initVerify( publicKey );
signer.update( nonceBytes );
Boolean isVerified = signer.verify( signedNonceBytes );

至此,signer.verify() returns false

我也尝试签署纯数据,而不是 SHA256 摘要,但这也不起作用。

我错过了什么?我是否正确签署了数据?我使用的填充是否正确?是否需要对数据进行其他处理才能使用 SHA256withECDSA 算法对其进行验证?

我是一个 java 人,所以我不能对 iOS 方面说什么,但是可以使用评论来快速检查 java 方面假设:

// Generate a new random EC keypair for testing
KeyPair keys = KeyPairGenerator.getInstance("EC").generateKeyPair();
PrivateKey privateKey = keys.getPrivate();
PublicKey publicKey = keys.getPublic();

// Generate a Random nonce to test with
byte[] nonceBytes = new byte[8]; // (some 64 bit integer)
new Random(System.nanoTime()).nextBytes(nonceBytes);

// sign     
Signature sign = Signature.getInstance("SHA256withECDSA");
sign.initSign(privateKey);
sign.update(nonceBytes);
byte[] signature = sign.sign();

//verify
Signature verify = Signature.getInstance("SHA256withECDSA");
verify.initVerify(publicKey);
verify.update(nonceBytes);
Boolean isVerified = verify.verify(signature);

// print results
System.out.println("nonce used  ::" + Base64.getEncoder().encodeToString(nonceBytes));
System.out.println("Signed nonce ::" + Base64.getEncoder().encodeToString(signature));
System.out.println("nonce used ::" + isVerified);

如您所料 returns,上面的代码将始终 return 验证签名。检查您的假设是否准确,并验证所使用的密钥在 两边是否正确。

我可以建议您使用一些可用于 iOS 和 JAVA 端的加密库 (f.e.:https://github.com/VirgilSecurity/virgil-crypto)。这将确保两种情况下的算法和块类型(等)相同,您无需再担心。我相信您会在 GitHub.

上找到许多加密库

在 getBytes() 中,您可以使用 java.nio.charset.StandardCharsets 指定编码技术。并对解码器做同样的事情。

https://docs.oracle.com/javase/7/docs/api/java/nio/charset/StandardCharsets.html

字节顺序不匹配:

  • iOS 是 little endian。您创建的方式nonceData,保留此顺序。
  • 在 Java 端,ByteBuffer 默认为 big endian,独立于底层操作系统/硬件。

因此您需要更改字节顺序:

ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putLong(nonce);