ucryptolib/test.c
static void testRSA() {
  EVP_PKEY* key = parseKey(kPrivateKey);
  unsigned char* sig;
  size_t siglen;
  unsigned char msg[1024];
  int msg_size;
  clBignumModulus myKey;
  clHASH_CTX myHash;

  // Compute key representation for cryptolib.
  precomputeRSA(key, &myKey);
  cprint(&myKey);

  // Construct random message.
  msg_size = rand() & 1023;
  RAND_bytes(msg, msg_size);

  printHex("msg:  ", msg, msg_size);

  // Check pkcs15-sha256 =========================
  CHECK(ossl_sign(key, EVP_sha256(), msg, msg_size, &sig, &siglen) == 1);

  printHex("sig:  ", sig, siglen);

  // Check signature matches hash.
  clSHA256_init(&myHash);
  clHASH_update(&myHash, msg, msg_size);
  CHECK(clRSA2K_verify(&myKey, sig, siglen, &myHash) == 1);

  // Flip evil bit in sig.
  sig[0] ^= 1;

  // Check fail.
  clSHA256_init(&myHash);
  clHASH_update(&myHash, msg, msg_size);
  CHECK(clRSA2K_verify(&myKey, sig, siglen, &myHash) == 0);

  free(sig);

  // Check pkcs15-sha1 ============================
  CHECK(ossl_sign(key, EVP_sha1(), msg, msg_size, &sig, &siglen) == 1);

  printHex("sig:  ", sig, siglen);

  // Check signature matches hash.
  clSHA1_init(&myHash);
  clHASH_update(&myHash, msg, msg_size);
  CHECK(clRSA2K_verify(&myKey, sig, siglen, &myHash) == 1);

  // Flip evil bit in sig.
  sig[0] ^= 1;

  // Check fail.
  clSHA1_init(&myHash);
  clHASH_update(&myHash, msg, msg_size);
  CHECK(clRSA2K_verify(&myKey, sig, siglen, &myHash) == 0);

  free(sig);

  freeKey(key);
}
// Sign random test message with openssl, verify using simple C code.
Sign random test message with openssl, verify using simple C code.
Sign random test message with openssl verify using simple C code .
static void testRSA() {
  EVP_PKEY* key = parseKey(kPrivateKey);
  unsigned char* sig;
  size_t siglen;
  unsigned char msg[1024];
  int msg_size;
  clBignumModulus myKey;
  clHASH_CTX myHash;
  precomputeRSA(key, &myKey);
  cprint(&myKey);
  msg_size = rand() & 1023;
  RAND_bytes(msg, msg_size);
  printHex("msg:  ", msg, msg_size);
  CHECK(ossl_sign(key, EVP_sha256(), msg, msg_size, &sig, &siglen) == 1);
  printHex("sig:  ", sig, siglen);
  clSHA256_init(&myHash);
  clHASH_update(&myHash, msg, msg_size);
  CHECK(clRSA2K_verify(&myKey, sig, siglen, &myHash) == 1);
  sig[0] ^= 1;
  clSHA256_init(&myHash);
  clHASH_update(&myHash, msg, msg_size);
  CHECK(clRSA2K_verify(&myKey, sig, siglen, &myHash) == 0);
  free(sig);
  CHECK(ossl_sign(key, EVP_sha1(), msg, msg_size, &sig, &siglen) == 1);
  printHex("sig:  ", sig, siglen);
  clSHA1_init(&myHash);
  clHASH_update(&myHash, msg, msg_size);
  CHECK(clRSA2K_verify(&myKey, sig, siglen, &myHash) == 1);
  sig[0] ^= 1;
  clSHA1_init(&myHash);
  clHASH_update(&myHash, msg, msg_size);
  CHECK(clRSA2K_verify(&myKey, sig, siglen, &myHash) == 0);
  free(sig);
  freeKey(key);
}
static void testRSA ( ) { EVP_PKEY * key = parseKey ( kPrivateKey ) ; unsigned char * sig ; size_t siglen ; unsigned char msg [ 1024 ] ; int msg_size ; clBignumModulus myKey ; clHASH_CTX myHash ; precomputeRSA ( key , & myKey ) ; cprint ( & myKey ) ; msg_size = rand ( ) & 1023 ; RAND_bytes ( msg , msg_size ) ; printHex ( " " , msg , msg_size ) ; CHECK ( ossl_sign ( key , EVP_sha256 ( ) , msg , msg_size , & sig , & siglen ) == 1 ) ; printHex ( " " , sig , siglen ) ; clSHA256_init ( & myHash ) ; clHASH_update ( & myHash , msg , msg_size ) ; CHECK ( clRSA2K_verify ( & myKey , sig , siglen , & myHash ) == 1 ) ; sig [ 0 ] ^= 1 ; clSHA256_init ( & myHash ) ; clHASH_update ( & myHash , msg , msg_size ) ; CHECK ( clRSA2K_verify ( & myKey , sig , siglen , & myHash ) == 0 ) ; free ( sig ) ; CHECK ( ossl_sign ( key , EVP_sha1 ( ) , msg , msg_size , & sig , & siglen ) == 1 ) ; printHex ( " " , sig , siglen ) ; clSHA1_init ( & myHash ) ; clHASH_update ( & myHash , msg , msg_size ) ; CHECK ( clRSA2K_verify ( & myKey , sig , siglen , & myHash ) == 1 ) ; sig [ 0 ] ^= 1 ; clSHA1_init ( & myHash ) ; clHASH_update ( & myHash , msg , msg_size ) ; CHECK ( clRSA2K_verify ( & myKey , sig , siglen , & myHash ) == 0 ) ; free ( sig ) ; freeKey ( key ) ; }
Sign random test message with openssl, verify using simple C code.
Sign random test message with openssl verify using simple C code .
// Compute key representation for cryptolib. // Construct random message. // Check pkcs15-sha256 ========================= // Check signature matches hash. // Flip evil bit in sig. // Check fail. // Check pkcs15-sha1 ============================ // Check signature matches hash. // Flip evil bit in sig. // Check fail.