]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Add new functions to wrap digest and sign/checksig.
authorNick Mathewson <nickm@torproject.org>
Thu, 1 Apr 2004 22:10:33 +0000 (22:10 +0000)
committerNick Mathewson <nickm@torproject.org>
Thu, 1 Apr 2004 22:10:33 +0000 (22:10 +0000)
svn:r1436

src/common/crypto.c
src/common/crypto.h
src/or/test.c

index 7a548a2d146cd60a0655c8fa102ce08728cf09bc..04859ed4ba9a354061312975f10a284ce06008e3 100644 (file)
@@ -656,6 +656,44 @@ int crypto_pk_private_sign(crypto_pk_env_t *env, unsigned char *from, int fromle
   }
 }
 
+/* Return 0 if sig is a correct signature for SHA1(data).  Else return -1.
+ */
+int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, unsigned char *data, int datalen, unsigned char *sig, int siglen)
+{
+  char digest[CRYPTO_SHA1_DIGEST_LEN];
+  char buf[1024];
+  int r;
+
+  assert(env && data && sig);
+
+  if (crypto_SHA_digest(data,datalen,digest)<0) {
+    log_fn(LOG_WARN, "couldn't compute digest");
+    return -1;
+  }
+  r = crypto_pk_public_checksig(env,sig,siglen,buf);
+  if (r != CRYPTO_SHA1_DIGEST_LEN) {
+    log_fn(LOG_WARN, "Invalid signature");
+    return -1;
+  }
+  if (memcmp(buf, digest, CRYPTO_SHA1_DIGEST_LEN)) {
+    log_fn(LOG_WARN, "Signature mismatched with digest.");
+    return -1;
+  }
+
+  return 0;
+}
+
+/* Fill 'to' with a signature of SHA1(from).
+ */
+int crypto_pk_private_sign_digest(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to)
+{
+  char digest[CRYPTO_SHA1_DIGEST_LEN];
+  if (crypto_SHA_digest(from,fromlen,digest)<0)
+    return 0;
+  return crypto_pk_private_sign(env,digest,CRYPTO_SHA1_DIGEST_LEN,to);
+}
+
+
 /* Perform a hybrid (public/secret) encryption on 'fromlen' bytes of data
  * from 'from', with padding type 'padding', storing the results on 'to'.
  *
index a09a4deda1579a8c6e1e8288e578497eeb569be5..1ecd5a3ded5afa49579a01e8fbbdf05e1943aece 100644 (file)
@@ -58,7 +58,9 @@ int crypto_pk_keysize(crypto_pk_env_t *env);
 int crypto_pk_public_encrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding);
 int crypto_pk_private_decrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding);
 int crypto_pk_private_sign(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to);
+int crypto_pk_private_sign_digest(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to);
 int crypto_pk_public_checksig(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to);
+int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, unsigned char *data, int datalen, unsigned char *sig, int siglen);
 int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env, unsigned char *from,
                                    int fromlen, unsigned char *to,
                                    int padding);
index 18b7541f280da1ee9c23c4a2b79d8e01c75284dc..d09f7bd1298d4900f113883b2ec6d48bf73f35d3 100644 (file)
@@ -406,6 +406,11 @@ test_crypto()
   test_eq(128, crypto_pk_private_sign(pk1, data1, 10, data2));
   test_eq(10, crypto_pk_public_checksig(pk1, data2, 128, data3));
   test_streq(data3, "Ossifrage");
+  /* Try signing digests. */
+  test_eq(128, crypto_pk_private_sign_digest(pk1, data1, 10, data2));
+  test_eq(20, crypto_pk_public_checksig(pk1, data2, 128, data3));
+  test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
+  test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
   /*XXXX test failed signing*/
 
   /* Try encoding */