title: Data signed on iOS can't be verified in Java
body: <p>I have some data that I'm signing on iOS with <code>SecKeyRawSign</code> using Elliptic Curve private key. However, verifying that data in Java using <code>Signature.verify()</code> returns <code>false</code></p>
  
  <p>The data is a random 64 bit integer, split into bytes like so  </p>
  
  <pre><code>uint64_t nonce = (some 64 bit integer)
  NSData *nonceData = [NSData dataWithBytes: &amp;nonce length: sizeof(nonce)];
  </code></pre>
  
  <p>From that data I'm creating a SHA256 digest  </p>
  
  <pre><code>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];
  </code></pre>
  
  <p>and then signing it with private key  </p>
  
  <pre><code>size_t signedBufferSize = kMaxCipherBufferSize;
  uint8_t *signedBuffer = malloc(kMaxCipherBufferSize);
  
  OSStatus status = SecKeyRawSign(privateKeyRef,
                                  kSecPaddingPKCS1SHA256,
                                  (const uint8_t *)digestData.bytes,
                                  digestData.length,
                                  &amp;signedBuffer[0],
                                  &amp;signedBufferSize);
  
  NSData *signedData = nil;
  if (status == errSecSuccess) {
      signedData = [NSData dataWithBytes:signedBuffer length:signedBufferSize];
  }
  </code></pre>
  
  <p>Everything appears to work fine.</p>
  
  <p>Then, in Java server, I'm trying to verify that signed data  </p>
  
  <pre><code>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 );
  </code></pre>
  
  <p>At this point, <code>signer.verify()</code> returns <code>false</code></p>
  
  <p>I also tried to sign plain data, instead of SHA256 digest, but that doesn't work either.</p>
  
  <p>What am I missing? Am I signing the data correctly? Am I using correct padding? Is there something else to be done with data to be able to verify it with <code>SHA256withECDSA</code> algorithm?</p>