+/*
+ * Verify that the public data in an RSA key matches the private
+ * data.
+ */
+int rsa_verify(struct RSAKey *key) {
+ Bignum n, ed, pm1, qm1, pm1qm1;
+ int cmp;
+
+ /* n must equal pq. */
+ n = bigmul(key->p, key->q);
+ cmp = bignum_cmp(n, key->modulus);
+ freebn(n);
+ if (cmp != 0)
+ return 0;
+
+ /* e * d must be congruent to 1, modulo (p-1)(q-1). */
+ pm1 = copybn(key->p);
+ decbn(pm1);
+ qm1 = copybn(key->q);
+ decbn(qm1);
+ pm1qm1 = bigmul(pm1, qm1);
+ freebn(pm1);
+ freebn(qm1);
+ ed = modmul(key->exponent, key->private_exponent, pm1qm1);
+ sfree(pm1qm1);
+ cmp = bignum_cmp(ed, One);
+ sfree(ed);
+ if (cmp != 0)
+ return 0;
+}
+