+/*
+ * Key comparison function for the 2-3-4 tree of SSH2 keys.
+ */
+static int cmpkeys_ssh2(void *av, void *bv) {
+ struct ssh2_userkey *a = (struct ssh2_userkey *)av;
+ struct ssh2_userkey *b = (struct ssh2_userkey *)bv;
+ int i;
+ int alen, blen;
+ unsigned char *ablob, *bblob;
+ int c;
+
+ /*
+ * Compare purely by public blob.
+ */
+ ablob = a->alg->public_blob(a->data, &alen);
+ bblob = b->alg->public_blob(b->data, &blen);
+
+ c = 0;
+ for (i = 0; i < alen && i < blen; i++) {
+ if (ablob[i] < bblob[i]) {
+ c = -1; break;
+ } else if (ablob[i] > bblob[i]) {
+ c = +1; break;
+ }
+ }
+ if (c == 0 && i < alen) c = +1; /* a is longer */
+ if (c == 0 && i < blen) c = -1; /* a is longer */
+
+ sfree(ablob);
+ sfree(bblob);
+
+ return c;
+}
+
+/*
+ * Key comparison function for looking up a blob in the 2-3-4 tree
+ * of SSH2 keys.
+ */
+static int cmpkeys_ssh2_asymm(void *av, void *bv) {
+ struct blob *a = (struct blob *)av;
+ struct ssh2_userkey *b = (struct ssh2_userkey *)bv;
+ int i;
+ int alen, blen;
+ unsigned char *ablob, *bblob;
+ int c;
+
+ /*
+ * Compare purely by public blob.
+ */
+ ablob = a->blob;
+ alen = a->len;
+ bblob = b->alg->public_blob(b->data, &blen);
+
+ c = 0;
+ for (i = 0; i < alen && i < blen; i++) {
+ if (ablob[i] < bblob[i]) {
+ c = -1; break;
+ } else if (ablob[i] > bblob[i]) {
+ c = +1; break;
+ }
+ }
+ if (c == 0 && i < alen) c = +1; /* a is longer */
+ if (c == 0 && i < blen) c = -1; /* a is longer */
+
+ sfree(bblob);
+
+ return c;
+}
+