static void sftp_attr(SilcSFTP sftp, SilcSFTPStatus status, const SilcSFTPAttributes attrs, void *context) { SilcSFTPHandle handle = (SilcSFTPHandle)context; int i; fprintf(stderr, "Status %d\n", status); if (status != SILC_SFTP_STATUS_OK) { SILC_LOG_DEBUG(("Error status")); success = FALSE; end_test(); return; } SILC_LOG_DEBUG(("Attr.flags: %d", attrs->flags)); SILC_LOG_DEBUG(("Attr.size: %lu", attrs->size)); SILC_LOG_DEBUG(("Attr.uid: %d", attrs->uid)); SILC_LOG_DEBUG(("Attr.gid: %d", attrs->gid)); SILC_LOG_DEBUG(("Attr.permissions: %d", attrs->permissions)); SILC_LOG_DEBUG(("Attr.atime: %d", attrs->atime)); SILC_LOG_DEBUG(("Attr.mtime: %d", attrs->mtime)); SILC_LOG_DEBUG(("Attr.extended count: %d", attrs->extended_count)); for (i = 0; i < attrs->extended_count; i++) { SILC_LOG_HEXDUMP(("Attr.extended_type[i]:", i), attrs->extended_type[i]->data, silc_buffer_len(attrs->extended_type[i])); SILC_LOG_HEXDUMP(("Attr.extended_data[i]:", i), attrs->extended_data[i]->data, silc_buffer_len(attrs->extended_data[i])); } if (!file) { fprintf(stderr, "Closing file\n"); silc_sftp_close(sftp, handle, sftp_status, context); return; } fprintf(stderr, "LStatting file %s\n", file); silc_sftp_lstat(sftp, file, sftp_attr, context); file = NULL; }
static void sftp_data(SilcSFTP sftp, SilcSFTPStatus status, const unsigned char *data, SilcUInt32 data_len, void *context) { SilcSFTPHandle handle = (SilcSFTPHandle)context; if (status != SILC_SFTP_STATUS_OK) { SilcSFTPAttributesStruct attrs; fprintf(stderr, "Status %d\n", status); if (status != SILC_SFTP_STATUS_EOF) { SILC_LOG_DEBUG(("Error status")); success = FALSE; end_test(); return; } if (!strcmp(file, "/sftp/sftp_server.c")) { fprintf(stderr, "FStatting file handle %s\n", file); silc_sftp_fstat(sftp, handle, sftp_attr, context); return; } /* Open another file */ opendir = FALSE; memset(&attrs, 0, sizeof(attrs)); file = "/sftp/sftp_server.c"; fprintf(stderr, "Opening file %s\n", file); offset = 0; silc_sftp_open(sftp, file, SILC_SFTP_FXF_READ, &attrs, sftp_handle, gclient); return; } SILC_LOG_HEXDUMP(("data"), (unsigned char *)data, data_len); offset += data_len; /* Attempt to read more */ fprintf(stderr, "Reading more of file %s\n", file); silc_sftp_read(sftp, handle, offset, 2048, sftp_data, handle); }
static void silc_sftp_send_packet(SilcSFTPServer sftp, SilcSFTPPacket type, SilcUInt32 len, ...) { SilcBuffer tmp; va_list vp; int ret; va_start(vp, len); tmp = silc_sftp_packet_encode_vp(type, sftp->packet, len, vp); va_end(vp); if (!tmp) return; sftp->packet = tmp; SILC_LOG_HEXDUMP(("SFTP packet to client"), silc_buffer_data(sftp->packet), silc_buffer_len(sftp->packet)); /* Send the packet */ while (silc_buffer_len(sftp->packet) > 0) { ret = silc_stream_write(sftp->stream, silc_buffer_data(sftp->packet), silc_buffer_len(sftp->packet)); if (ret == -2) { SILC_LOG_ERROR(("Error sending SFTP packet type %d", type)); sftp->error((SilcSFTP)sftp, SILC_SFTP_STATUS_NO_CONNECTION, sftp->context); silc_buffer_reset(sftp->packet); return; } if (ret == 0) { sftp->error((SilcSFTP)sftp, SILC_SFTP_STATUS_EOF, sftp->context); silc_buffer_reset(sftp->packet); return; } if (ret == -1) return; silc_buffer_pull(sftp->packet, ret); } /* Clear packet */ silc_buffer_reset(sftp->packet); }
int main(int argc, char **argv) { SilcBool success = FALSE; unsigned char digest[32], tmp[4096], digest2[32]; SilcUInt32 tmp_len; SilcHash sha256; if (argc > 1 && !strcmp(argv[1], "-d")) { silc_log_debug(TRUE); silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*crypt*,*hash*,*sha256*"); } SILC_LOG_DEBUG(("Registering builtin hash functions")); silc_hash_register_default(); SILC_LOG_DEBUG(("Allocating sha256 hash function")); if (!silc_hash_alloc("sha256", &sha256)) { SILC_LOG_DEBUG(("Allocating sha256 hash function failed")); goto err; } SilcUInt64 t1, t2; silc_timer_synchronize(&timer); /* First test vector */ SILC_LOG_DEBUG(("First test vector")); silc_hash_init(sha256); silc_hash_update(sha256, data1, strlen(data1)); memset(digest, 0, sizeof(digest)); t1 = silc_timer_tick(&timer, FALSE); silc_hash_final(sha256, digest); t2 = silc_timer_tick(&timer, TRUE); SILC_LOG_DEBUG(("cycles: %d", t2 - t1)); SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data1, strlen(data1)); SILC_LOG_HEXDUMP(("Digest"), digest, sizeof(digest)); SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data1_digest, sizeof(digest)); if (memcmp(digest, data1_digest, sizeof(digest))) { SILC_LOG_DEBUG(("Hash failed")); goto err; } SILC_LOG_DEBUG(("Hash is successful")); /* Second test vector */ SILC_LOG_DEBUG(("Second test vector")); silc_hash_init(sha256); silc_hash_update(sha256, data2, strlen(data2)); silc_hash_final(sha256, digest); SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data2, strlen(data2)); SILC_LOG_HEXDUMP(("Digest"), digest, sizeof(digest)); SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data2_digest, sizeof(digest)); if (memcmp(digest, data2_digest, sizeof(digest))) { SILC_LOG_DEBUG(("Hash failed")); goto err; } SILC_LOG_DEBUG(("Hash is successful")); /* Third test vector */ SILC_LOG_DEBUG(("Third test vector")); silc_hash_init(sha256); silc_hex2data(data3, tmp, sizeof(tmp), &tmp_len); silc_hash_update(sha256, tmp, tmp_len); silc_hash_final(sha256, digest); SILC_LOG_HEXDUMP(("Message"), tmp, tmp_len); SILC_LOG_HEXDUMP(("Digest"), digest, sizeof(digest)); silc_hex2data(data3_digest, digest2, sizeof(digest2), NULL); SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)digest2, sizeof(digest)); if (memcmp(digest, digest2, sizeof(digest))) { SILC_LOG_DEBUG(("Hash failed")); goto err; } SILC_LOG_DEBUG(("Hash is successful")); success = TRUE; err: SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); silc_hash_free(sha256); silc_hash_unregister_all(); return success; }
int main(int argc, char **argv) { SilcBool success = FALSE; unsigned char digest[20]; SilcUInt32 len; SilcHmac hmac; if (argc > 1 && !strcmp(argv[1], "-d")) { silc_log_debug(TRUE); silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*crypt*,*hash*,*sha1*,*hmac*"); } SILC_LOG_DEBUG(("Registering builtin hash functions")); silc_hash_register_default(); silc_hmac_register_default(); SILC_LOG_DEBUG(("Allocating sha1 HMAC")); if (!silc_hmac_alloc("hmac-sha1", NULL, &hmac)) { SILC_LOG_DEBUG(("Allocating sha1 HMAC failed")); goto err; } /* First test vector */ SILC_LOG_DEBUG(("First test vector")); silc_hmac_init_with_key(hmac, key1, key1_len); silc_hmac_update(hmac, data1, strlen(data1)); silc_hmac_final(hmac, digest, &len); SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key1, key1_len); SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data1, strlen(data1)); SILC_LOG_HEXDUMP(("Digest"), digest, len); SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data1_digest, len); if (memcmp(digest, data1_digest, len)) { SILC_LOG_DEBUG(("HMAC failed")); goto err; } SILC_LOG_DEBUG(("HMAC is successful")); /* Second test vector */ SILC_LOG_DEBUG(("Second test vector")); silc_hmac_init_with_key(hmac, key2, key2_len); silc_hmac_update(hmac, data2, strlen(data2)); silc_hmac_final(hmac, digest, &len); SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key2, key2_len); SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data2, strlen(data2)); SILC_LOG_HEXDUMP(("Digest"), digest, len); SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data2_digest, len); if (memcmp(digest, data2_digest, len)) { SILC_LOG_DEBUG(("HMAC failed")); goto err; } SILC_LOG_DEBUG(("HMAC is successful")); /* Third test vector */ SILC_LOG_DEBUG(("Third test vector")); silc_hmac_init_with_key(hmac, key3, key3_len); memset(data3, '\xdd', sizeof(data3)); silc_hmac_update(hmac, data3, sizeof(data3)); silc_hmac_final(hmac, digest, &len); SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key3, key3_len); SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data3, sizeof(data3)); SILC_LOG_HEXDUMP(("Digest"), digest, len); SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data3_digest, len); if (memcmp(digest, data3_digest, len)) { SILC_LOG_DEBUG(("HMAC failed")); goto err; } SILC_LOG_DEBUG(("HMAC is successful")); /* Fourth test vector */ SILC_LOG_DEBUG(("Fourth test vector")); memset(key4, '\xaa', key4_len); silc_hmac_init_with_key(hmac, key4, key4_len); silc_hmac_update(hmac, data4, strlen(data4)); silc_hmac_final(hmac, digest, &len); SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key4, key4_len); SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data4, sizeof(data4)); SILC_LOG_HEXDUMP(("Digest"), digest, len); SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data4_digest, len); if (memcmp(digest, data4_digest, len)) { SILC_LOG_DEBUG(("HMAC failed")); goto err; } SILC_LOG_DEBUG(("HMAC is successful")); success = TRUE; err: SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); silc_hmac_free(hmac); silc_hash_unregister_all(); silc_hmac_unregister_all(); return success; }
int main(int argc, char **argv) { SilcBool success = FALSE; SilcMessagePayload message; SilcBuffer buf; const char *msg = "FOOBAR MESSAGE"; unsigned char *data, tmp[1023], *tmp2; SilcUInt32 data_len; SilcUInt16 flags; int i, n; if (argc > 1 && !strcmp(argv[1], "-d")) { silc_log_debug(TRUE); silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*message*"); } silc_cipher_register_default(); silc_hash_register_default(); silc_hmac_register_default(); silc_pkcs_register_default(); SILC_LOG_DEBUG(("Load keypair")); if (!silc_load_key_pair("pubkey.pub", "privkey.prv", "", &public_key, &private_key)) { SILC_LOG_DEBUG(("Create keypair")); if (!silc_create_key_pair("rsa", 2048, "pubkey.pub", "privkey.prv", NULL, "", &public_key, &private_key, FALSE)) goto err; } SILC_LOG_DEBUG(("Alloc RNG")); rng = silc_rng_alloc(); silc_rng_init(rng); SILC_LOG_DEBUG(("Alloc AES")); if (!silc_cipher_alloc("aes-128-cbc", &key)) goto err; SILC_LOG_DEBUG(("Alloc SHA-256")); if (!silc_hash_alloc("sha256", &hash)) goto err; SILC_LOG_DEBUG(("Alloc HMAC")); if (!silc_hmac_alloc("hmac-sha256-96", hash, &hmac)) goto err; SILC_LOG_DEBUG(("Set static key: '1234567890123456'")); if (!silc_cipher_set_key(key, "1234567890123456", 16 * 8)) goto err; SILC_LOG_DEBUG(("Set HMAC key: '1234567890123456'")); silc_hmac_set_key(hmac, "1234567890123456", 16); /* Simple private message */ SILC_LOG_DEBUG(("Encoding private message len %d (static key)", strlen(msg))); buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 | SILC_MESSAGE_FLAG_ACK, msg, strlen(msg), TRUE, TRUE, key, hmac, rng, NULL, NULL, NULL, NULL); if (!buf) goto err; SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf)); SILC_LOG_DEBUG(("Parsing private messsage (static key)")); message = silc_message_payload_parse(silc_buffer_data(buf), silc_buffer_len(buf), TRUE, TRUE, key, hmac, NULL, FALSE, NULL); if (!message) goto err; flags = silc_message_get_flags(message); SILC_LOG_DEBUG(("Flags: %x", flags)); if (!(flags & SILC_MESSAGE_FLAG_ACTION)) goto err; if (!(flags & SILC_MESSAGE_FLAG_UTF8)) goto err; if (!(flags & SILC_MESSAGE_FLAG_ACK)) goto err; data = silc_message_get_data(message, &data_len); SILC_LOG_HEXDUMP(("Data"), data, data_len); if (data_len != strlen(msg) || memcmp(data, msg, strlen(msg))) goto err; SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message), silc_hmac_len(hmac)); silc_message_payload_free(message); /* Simple private message */ n = 10; SILC_LOG_DEBUG(("Encoding private message len %d (static key)", n)); buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 | SILC_MESSAGE_FLAG_ACK, msg, n, TRUE, TRUE, key, hmac, rng, NULL, NULL, NULL, buf); if (!buf) goto err; SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf)); SILC_LOG_DEBUG(("Parsing private messsage (static key)")); message = silc_message_payload_parse(silc_buffer_data(buf), silc_buffer_len(buf), TRUE, TRUE, key, hmac, NULL, FALSE, NULL); if (!message) goto err; flags = silc_message_get_flags(message); SILC_LOG_DEBUG(("Flags: %x", flags)); if (!(flags & SILC_MESSAGE_FLAG_ACTION)) goto err; if (!(flags & SILC_MESSAGE_FLAG_UTF8)) goto err; if (!(flags & SILC_MESSAGE_FLAG_ACK)) goto err; data = silc_message_get_data(message, &data_len); SILC_LOG_HEXDUMP(("Data"), data, data_len); if (data_len != n || memcmp(data, msg, n)) goto err; SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message), silc_hmac_len(hmac)); silc_message_payload_free(message); /* Simple private message */ n = 1; SILC_LOG_DEBUG(("Encoding private message len %d (static key)", n)); buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 | SILC_MESSAGE_FLAG_ACK, msg, n, TRUE, TRUE, key, hmac, rng, NULL, NULL, NULL, buf); if (!buf) goto err; SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf)); SILC_LOG_DEBUG(("Parsing private messsage (static key)")); message = silc_message_payload_parse(silc_buffer_data(buf), silc_buffer_len(buf), TRUE, TRUE, key, hmac, NULL, FALSE, NULL); if (!message) goto err; flags = silc_message_get_flags(message); SILC_LOG_DEBUG(("Flags: %x", flags)); if (!(flags & SILC_MESSAGE_FLAG_ACTION)) goto err; if (!(flags & SILC_MESSAGE_FLAG_UTF8)) goto err; if (!(flags & SILC_MESSAGE_FLAG_ACK)) goto err; data = silc_message_get_data(message, &data_len); SILC_LOG_HEXDUMP(("Data"), data, data_len); if (data_len != n || memcmp(data, msg, n)) goto err; SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message), silc_hmac_len(hmac)); silc_message_payload_free(message); /* Simple private message */ for (i = 0; i < sizeof(tmp); i++) tmp[i] = (32 + i) & 127; SILC_LOG_DEBUG(("Encoding private message len %d (static key)", sizeof(tmp))); buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 | SILC_MESSAGE_FLAG_ACK, tmp, sizeof(tmp), TRUE, TRUE, key, hmac, rng, NULL, NULL, NULL, buf); if (!buf) goto err; SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf)); SILC_LOG_DEBUG(("Parsing private messsage (static key)")); message = silc_message_payload_parse(silc_buffer_data(buf), silc_buffer_len(buf), TRUE, TRUE, key, hmac, NULL, FALSE, NULL); if (!message) goto err; flags = silc_message_get_flags(message); SILC_LOG_DEBUG(("Flags: %x", flags)); if (!(flags & SILC_MESSAGE_FLAG_ACTION)) goto err; if (!(flags & SILC_MESSAGE_FLAG_UTF8)) goto err; if (!(flags & SILC_MESSAGE_FLAG_ACK)) goto err; data = silc_message_get_data(message, &data_len); SILC_LOG_HEXDUMP(("Data"), data, data_len); if (data_len != sizeof(tmp) || memcmp(data, tmp, sizeof(tmp))) goto err; SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message), silc_hmac_len(hmac)); silc_message_payload_free(message); /* Digitally signed private message */ for (i = 0; i < sizeof(tmp); i++) tmp[i] = (32 + i) & 127; SILC_LOG_DEBUG(("Encoding private message len %d (static key) SIGNED", sizeof(tmp))); buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 | SILC_MESSAGE_FLAG_ACK | SILC_MESSAGE_FLAG_SIGNED, tmp, sizeof(tmp), TRUE, TRUE, key, hmac, rng, public_key, private_key, hash, buf); if (!buf) goto err; SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf)); SILC_LOG_DEBUG(("Parsing private messsage (static key)")); message = silc_message_payload_parse(silc_buffer_data(buf), silc_buffer_len(buf), TRUE, TRUE, key, hmac, NULL, FALSE, NULL); if (!message) goto err; flags = silc_message_get_flags(message); SILC_LOG_DEBUG(("Flags: %x", flags)); if (!(flags & SILC_MESSAGE_FLAG_ACTION)) goto err; if (!(flags & SILC_MESSAGE_FLAG_UTF8)) goto err; if (!(flags & SILC_MESSAGE_FLAG_ACK)) goto err; if (!(flags & SILC_MESSAGE_FLAG_SIGNED)) goto err; data = silc_message_get_data(message, &data_len); SILC_LOG_HEXDUMP(("Data"), data, data_len); if (data_len != sizeof(tmp) || memcmp(data, tmp, sizeof(tmp))) goto err; SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message), silc_hmac_len(hmac)); SILC_LOG_DEBUG(("Verifying signature")); if (silc_message_signed_verify(message, public_key, hash) != SILC_AUTH_OK) goto err; SILC_LOG_DEBUG(("Signature Ok")); SILC_LOG_DEBUG(("Get public key")); pk2 = silc_message_signed_get_public_key(message, NULL, NULL); if (!pk2) goto err; SILC_LOG_DEBUG(("Verify public key")); if (!silc_pkcs_public_key_compare(public_key, pk2)) goto err; SILC_LOG_DEBUG(("Public key Ok")); silc_pkcs_public_key_free(pk2); silc_message_payload_free(message); /* Digitally signed channel message */ for (i = 0; i < sizeof(tmp) / 2; i++) tmp[i] = (32 + i) & 127; SILC_LOG_DEBUG(("Encoding channel message len %d (static key) SIGNED", sizeof(tmp) / 2)); buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 | SILC_MESSAGE_FLAG_ACK | SILC_MESSAGE_FLAG_SIGNED, tmp, sizeof(tmp) / 2, TRUE, FALSE, key, hmac, rng, public_key, private_key, hash, buf); if (!buf) goto err; SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf)); SILC_LOG_DEBUG(("Parsing channel messsage (static key)")); message = silc_message_payload_parse(silc_buffer_data(buf), silc_buffer_len(buf), FALSE, TRUE, key, hmac, NULL, FALSE, NULL); if (!message) goto err; flags = silc_message_get_flags(message); SILC_LOG_DEBUG(("Flags: %x", flags)); if (!(flags & SILC_MESSAGE_FLAG_ACTION)) goto err; if (!(flags & SILC_MESSAGE_FLAG_UTF8)) goto err; if (!(flags & SILC_MESSAGE_FLAG_ACK)) goto err; if (!(flags & SILC_MESSAGE_FLAG_SIGNED)) goto err; data = silc_message_get_data(message, &data_len); SILC_LOG_HEXDUMP(("Data"), data, data_len); if (data_len != sizeof(tmp) / 2 || memcmp(data, tmp, sizeof(tmp) / 2)) goto err; SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message), silc_hmac_len(hmac)); SILC_LOG_DEBUG(("Verifying signature")); if (silc_message_signed_verify(message, public_key, hash) != SILC_AUTH_OK) goto err; SILC_LOG_DEBUG(("Signature Ok")); SILC_LOG_DEBUG(("Get public key")); pk2 = silc_message_signed_get_public_key(message, NULL, NULL); if (!pk2) goto err; SILC_LOG_DEBUG(("Verify public key")); if (!silc_pkcs_public_key_compare(public_key, pk2)) goto err; SILC_LOG_DEBUG(("Public key Ok")); silc_pkcs_public_key_free(pk2); silc_message_payload_free(message); /* Digitally signed private message (no encryption) */ for (i = 0; i < sizeof(tmp) / 2; i++) tmp[i] = (32 + i) & 127; SILC_LOG_DEBUG(("Encoding private message len %d SIGNED", sizeof(tmp) / 2)); buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 | SILC_MESSAGE_FLAG_ACK | SILC_MESSAGE_FLAG_SIGNED, tmp, sizeof(tmp) / 2, FALSE, TRUE, NULL, NULL, rng, public_key, private_key, hash, buf); if (!buf) goto err; SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf)); SILC_LOG_DEBUG(("Parsing private messsage (static key)")); message = silc_message_payload_parse(silc_buffer_data(buf), silc_buffer_len(buf), TRUE, FALSE, NULL, NULL, NULL, FALSE, NULL); if (!message) goto err; flags = silc_message_get_flags(message); SILC_LOG_DEBUG(("Flags: %x", flags)); if (!(flags & SILC_MESSAGE_FLAG_ACTION)) goto err; if (!(flags & SILC_MESSAGE_FLAG_UTF8)) goto err; if (!(flags & SILC_MESSAGE_FLAG_ACK)) goto err; if (!(flags & SILC_MESSAGE_FLAG_SIGNED)) goto err; data = silc_message_get_data(message, &data_len); SILC_LOG_HEXDUMP(("Data"), data, data_len); if (data_len != sizeof(tmp) / 2 || memcmp(data, tmp, sizeof(tmp) / 2)) goto err; SILC_LOG_DEBUG(("Verifying signature")); if (silc_message_signed_verify(message, public_key, hash) != SILC_AUTH_OK) goto err; SILC_LOG_DEBUG(("Signature Ok")); SILC_LOG_DEBUG(("Get public key")); pk2 = silc_message_signed_get_public_key(message, NULL, NULL); if (!pk2) goto err; SILC_LOG_DEBUG(("Verify public key")); if (!silc_pkcs_public_key_compare(public_key, pk2)) goto err; SILC_LOG_DEBUG(("Public key Ok")); silc_pkcs_public_key_free(pk2); silc_message_payload_free(message); /* Digitally signed channel message (LARGE) */ n = 65550; tmp2 = silc_malloc(n); if (!tmp2) goto err; SILC_LOG_DEBUG(("Encoding channel message len %d (static key) SIGNED LARGE", n)); buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 | SILC_MESSAGE_FLAG_ACK | SILC_MESSAGE_FLAG_SIGNED, tmp2, n, TRUE, FALSE, key, hmac, rng, public_key, private_key, hash, buf); if (!buf) goto err; SILC_LOG_DEBUG(("Message length: %d", silc_buffer_len(buf))); if (silc_buffer_len(buf) > SILC_PACKET_MAX_LEN) goto err; SILC_LOG_DEBUG(("Parsing channel messsage (static key)")); message = silc_message_payload_parse(silc_buffer_data(buf), silc_buffer_len(buf), FALSE, TRUE, key, hmac, NULL, FALSE, NULL); if (!message) goto err; flags = silc_message_get_flags(message); SILC_LOG_DEBUG(("Flags: %x", flags)); if (!(flags & SILC_MESSAGE_FLAG_ACTION)) goto err; if (!(flags & SILC_MESSAGE_FLAG_UTF8)) goto err; if (!(flags & SILC_MESSAGE_FLAG_ACK)) goto err; if (!(flags & SILC_MESSAGE_FLAG_SIGNED)) goto err; data = silc_message_get_data(message, &data_len); SILC_LOG_DEBUG(("Data len: %d", data_len)); if (silc_buffer_len(buf) > SILC_PACKET_MAX_LEN) goto err; SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message), silc_hmac_len(hmac)); SILC_LOG_DEBUG(("Verifying signature")); if (silc_message_signed_verify(message, public_key, hash) != SILC_AUTH_OK) goto err; SILC_LOG_DEBUG(("Signature Ok")); SILC_LOG_DEBUG(("Get public key")); pk2 = silc_message_signed_get_public_key(message, NULL, NULL); if (!pk2) goto err; SILC_LOG_DEBUG(("Verify public key")); if (!silc_pkcs_public_key_compare(public_key, pk2)) goto err; SILC_LOG_DEBUG(("Public key Ok")); silc_pkcs_public_key_free(pk2); silc_message_payload_free(message); silc_free(tmp2); success = TRUE; SILC_LOG_DEBUG(("Cleanup")); silc_pkcs_public_key_free(public_key); silc_pkcs_private_key_free(private_key); silc_cipher_free(key); silc_hash_free(hash); silc_rng_free(rng); err: silc_cipher_unregister_all(); silc_hash_unregister_all(); silc_hmac_unregister_all(); silc_pkcs_unregister_all(); SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); return success; }
int main(int argc, char **argv) { SilcArgumentPayload payload; SilcBuffer args, args2; char arg[ARG_NUM + 2]; int i; unsigned char **argvv, *a; SilcUInt32 *argvv_lens, l; SilcUInt32 *argvv_types, t; SilcBool success = FALSE; if (argc > 1 && !strcmp(argv[1], "-d")) { silc_debug = 1; silc_debug_hexdump = 1; silc_log_set_debug_string("*argument*"); } SILC_LOG_DEBUG(("Encoding %d arguments", ARG_NUM)); argvv = silc_calloc(ARG_NUM, sizeof(unsigned char *)); argvv_lens = silc_calloc(ARG_NUM, sizeof(SilcUInt32)); argvv_types = silc_calloc(ARG_NUM, sizeof(SilcUInt32)); for (i = 0; i < ARG_NUM; i++) { memset(arg, 0, sizeof(arg)); memset(arg, 'a', i + 1); SILC_LOG_DEBUG(("Argument %d, len %d, type %d", i + 1, strlen(arg), i + 1)); SILC_LOG_HEXDUMP(("Argument data"), arg, strlen(arg)); argvv[i] = silc_memdup(arg, strlen(arg)); argvv_lens[i] = strlen(arg); argvv_types[i] = i + 1; } args = silc_argument_payload_encode(ARG_NUM, argvv, argvv_lens, argvv_types); if (!args) goto out; SILC_LOG_DEBUG(("Encoding was successful")); SILC_LOG_DEBUG(("Adding one extra argument")); memset(arg, 0, sizeof(arg)); memset(arg, 'a', ARG_NUM + 1); SILC_LOG_DEBUG(("Argument %d, len %d, type %d", ARG_NUM + 1, strlen(arg), ARG_NUM + 1)); SILC_LOG_HEXDUMP(("Argument data"), arg, strlen(arg)); args = silc_argument_payload_encode_one(args, arg, strlen(arg), ARG_NUM + 1); if (!args) goto out; SILC_LOG_DEBUG(("Adding one argument was successful")); SILC_LOG_HEXDUMP(("Encoded payload"), args->data, args->len); SILC_LOG_DEBUG(("Parsing the encoded payload")); payload = silc_argument_payload_parse(args->data, args->len, ARG_NUM + 1); if (!payload) goto out; SILC_LOG_DEBUG(("Parsing was successful")); SILC_LOG_DEBUG(("Re-encoding the parsed payload")); args2 = silc_argument_payload_encode_payload(payload); if (!args2) goto out; if (args2->len != args->len || memcmp(args2->data, args->data, args->len)) { SILC_LOG_DEBUG(("Re-encoding failed")); goto out; } silc_buffer_free(args2); SILC_LOG_DEBUG(("Re-encoding was successful")); SILC_LOG_DEBUG(("Checking number of arguments")); SILC_LOG_DEBUG(("Number of arguments: %d (expecting %d)", silc_argument_get_arg_num(payload), ARG_NUM + 1)); if (silc_argument_get_arg_num(payload) != ARG_NUM + 1) goto out; SILC_LOG_DEBUG(("Traversing the parsed arguments")); i = 0; a = silc_argument_get_first_arg(payload, &t, &l); if (!a || t != argvv_types[0] || l != argvv_lens[0] || memcmp(a, argvv[0], l)) { SILC_LOG_DEBUG(("First argument failed")); goto out; } while (a) { if (i + 1 == ARG_NUM + 1) { SILC_LOG_DEBUG(("Argument %d, len %d (expected %d), " "type %d (expected %d)", i + 1, l, strlen(arg), t, ARG_NUM + 1)); if (!a || t != ARG_NUM + 1 || l != strlen(arg) || memcmp(a, arg, l)) { SILC_LOG_DEBUG(("Argument %d failed", ARG_NUM + 1)); goto out; } } else { SILC_LOG_DEBUG(("Argument %d, len %d (expected %d), " "type %d (expected %d)", i + 1, l, argvv_lens[i], t, argvv_types[i])); if (!a || t != argvv_types[i] || l != argvv_lens[i] || memcmp(a, argvv[i], l)) { SILC_LOG_DEBUG(("Argument %d failed", i + 1)); goto out; } } a = silc_argument_get_next_arg(payload, &t, &l); i++; } if (i != ARG_NUM + 1) { SILC_LOG_DEBUG(("All arguments was not parsed, missing %d args", ARG_NUM + 1 - i)); goto out; } SILC_LOG_DEBUG(("Traversing successful")); SILC_LOG_DEBUG(("Traversing arguments by type")); for (i = 0; i < ARG_NUM + 1; i++) { a = silc_argument_get_arg_type(payload, i + 1, &l); if (i + 1 == ARG_NUM + 1) { if (!a || t != ARG_NUM + 1 || l != strlen(arg) || memcmp(a, arg, l)) { SILC_LOG_DEBUG(("Argument %d failed", ARG_NUM + 1)); goto out; } } else { if (!a || l != argvv_lens[i] || memcmp(a, argvv[i], l)) { SILC_LOG_DEBUG(("Argument %d failed", i + 1)); goto out; } } } SILC_LOG_DEBUG(("Traversing successful")); success = TRUE; out: silc_argument_payload_free(payload); for (i = 0; i < ARG_NUM; i++) silc_free(argvv[i]); silc_free(argvv); silc_free(argvv_lens); silc_free(argvv_types); silc_buffer_free(args); SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); exit(success); }
int main(int argc, char **argv) { SilcBool success = FALSE; SilcCipher cipher, cipher2; unsigned char dst[256], pdst[256]; int i; if (argc > 1 && !strcmp(argv[1], "-d")) { silc_log_debug(TRUE); silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*crypt*,*cast*,*cipher*"); } SILC_LOG_DEBUG(("Registering builtin hash functions")); silc_cipher_register_default(); SILC_LOG_DEBUG(("Allocating cast5-CBC cipher")); if (!silc_cipher_alloc("cast5-128-cbc", &cipher)) { SILC_LOG_DEBUG(("Allocating cas5-CBC cipher failed")); goto err; } if (!silc_cipher_alloc("cast5-128-cbc", &cipher2)) { SILC_LOG_DEBUG(("Allocating cast5-CBC cipher failed")); goto err; } /* First test vector */ SILC_LOG_DEBUG(("First test vector")); memset(dst, 0, sizeof(dst)); memset(pdst, 0, sizeof(pdst)); silc_cipher_set_iv(cipher, iv1); assert(silc_cipher_set_key(cipher, key1, key1_len, TRUE)); assert(silc_cipher_set_key(cipher2, key1, key1_len, FALSE)); assert(silc_cipher_encrypt(cipher, p1, dst, p1_len, NULL)); SILC_LOG_DEBUG(("block len %d, key len %d, name %s", silc_cipher_get_block_len(cipher), silc_cipher_get_key_len(cipher), silc_cipher_get_name(cipher))); SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p1, p1_len); SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p1_len); SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c1, p1_len); if (memcmp(dst, c1, p1_len)) { SILC_LOG_DEBUG(("Encrypt failed")); goto err; } SILC_LOG_DEBUG(("Encrypt is successful")); silc_cipher_set_iv(cipher2, iv1); assert(silc_cipher_decrypt(cipher2, dst, pdst, p1_len, NULL)); SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p1_len); SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p1, p1_len); if (memcmp(pdst, p1, p1_len)) { SILC_LOG_DEBUG(("Decrypt failed")); goto err; } SILC_LOG_DEBUG(("Decrypt is successful")); /* Second test vector */ SILC_LOG_DEBUG(("Second test vector")); memset(dst, 0, sizeof(dst)); memset(pdst, 0, sizeof(pdst)); silc_cipher_set_iv(cipher, iv2); assert(silc_cipher_set_key(cipher, key2, key2_len, TRUE)); assert(silc_cipher_set_key(cipher2, key2, key2_len, FALSE)); assert(silc_cipher_encrypt(cipher, p2, dst, p2_len, NULL)); SILC_LOG_DEBUG(("block len %d, key len %d, name %s", silc_cipher_get_block_len(cipher), silc_cipher_get_key_len(cipher), silc_cipher_get_name(cipher))); SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p2, p2_len); SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p2_len); SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c2, p2_len); if (memcmp(dst, c2, p2_len)) { SILC_LOG_DEBUG(("Encrypt failed")); goto err; } SILC_LOG_DEBUG(("Encrypt is successful")); silc_cipher_set_iv(cipher2, iv2); assert(silc_cipher_decrypt(cipher2, dst, pdst, p2_len, NULL)); SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p2_len); SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p2, p2_len); if (memcmp(pdst, p2, p2_len)) { SILC_LOG_DEBUG(("Decrypt failed")); goto err; } SILC_LOG_DEBUG(("Decrypt is successful")); silc_cipher_free(cipher); silc_cipher_free(cipher2); SILC_LOG_DEBUG(("Allocating cast5-128-ctr cipher")); if (!silc_cipher_alloc("cast5-128-ctr", &cipher)) { SILC_LOG_DEBUG(("Allocating cast5-128-ctr cipher failed")); goto err; } /* Fourth test vector */ SILC_LOG_DEBUG(("Fourth test vector")); memset(dst, 0, sizeof(dst)); memset(pdst, 0, sizeof(pdst)); silc_cipher_set_iv(cipher, iv4); assert(silc_cipher_set_key(cipher, key4, key4_len, TRUE)); assert(silc_cipher_encrypt(cipher, p4, dst, p4_len, NULL)); SILC_LOG_DEBUG(("block len %d, key len %d, name %s", silc_cipher_get_block_len(cipher), silc_cipher_get_key_len(cipher), silc_cipher_get_name(cipher))); SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p4, p4_len); SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p4_len); SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c4, p4_len); if (memcmp(dst, c4, p4_len)) { SILC_LOG_DEBUG(("Encrypt failed")); goto err; } SILC_LOG_DEBUG(("Encrypt is successful")); silc_cipher_set_iv(cipher, iv4); assert(silc_cipher_decrypt(cipher, dst, pdst, p4_len, NULL)); SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p4_len); SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p4, p4_len); if (memcmp(pdst, p4, p4_len)) { SILC_LOG_DEBUG(("Decrypt failed")); goto err; } SILC_LOG_DEBUG(("Decrypt is successful")); silc_cipher_free(cipher); SILC_LOG_DEBUG(("Allocating cast5-128-cfb cipher")); if (!silc_cipher_alloc("cast5-128-cfb", &cipher)) { SILC_LOG_DEBUG(("Allocating cast5-128-cfb cipher failed")); goto err; } if (!silc_cipher_alloc("cast5-128-cfb", &cipher2)) { SILC_LOG_DEBUG(("Allocating cast5-128-cfb cipher failed")); goto err; } SILC_LOG_DEBUG(("CFB test vector")); memset(dst, 0, sizeof(dst)); memset(pdst, 0, sizeof(pdst)); silc_cipher_set_iv(cipher, iv6); assert(silc_cipher_set_key(cipher, key6, key6_len, TRUE)); assert(silc_cipher_set_key(cipher2, key6, key6_len, FALSE)); assert(silc_cipher_encrypt(cipher, p6, dst, p6_len, NULL)); SILC_LOG_DEBUG(("block len %d, key len %d, name %s", silc_cipher_get_block_len(cipher), silc_cipher_get_key_len(cipher), silc_cipher_get_name(cipher))); SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p6, p6_len); SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p6_len); SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c6, p6_len); if (memcmp(dst, c6, p6_len)) { SILC_LOG_DEBUG(("Encrypt failed")); goto err; } SILC_LOG_DEBUG(("Encrypt is successful")); silc_cipher_set_iv(cipher2, iv6); assert(silc_cipher_decrypt(cipher2, dst, pdst, p6_len, NULL)); SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p6_len); SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p6, p6_len); if (memcmp(pdst, p6, p6_len)) { SILC_LOG_DEBUG(("Decrypt failed")); goto err; } SILC_LOG_DEBUG(("Decrypt is successful")); silc_cipher_free(cipher2); success = TRUE; err: SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); silc_cipher_unregister_all(); return success; }
int main(int argc, char **argv) { SilcBool success = FALSE; unsigned char *data, iv[SILC_CIPHER_MAX_IV_SIZE]; SilcUInt32 i, k; silc_runtime_init(); silc_crypto_init(NULL); if (argc > 1 && !strcmp(argv[1], "-d")) { silc_log_debug(TRUE); silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*acc*,*cipher*,*twofish*"); } if (!silc_acc_init(SILC_SOFTACC, (void *)0x01, "min_threads", 2, "max_threads", 8, NULL)) exit(1); data = malloc(ENC_LEN * sizeof(*data)); if (!data) exit(1); /* Plaintext */ for (i = 0; i < ENC_LEN; i++) data[i] = i % 255; SILC_LOG_HEXDUMP(("data"), data, ENC_LEN); /* IV */ for (i = 0; i < SILC_CIPHER_MAX_IV_SIZE; i++) iv[i] = i % 255; SILC_LOG_HEXDUMP(("IV"), iv, SILC_CIPHER_MAX_IV_SIZE); for (i = 0; silc_default_ciphers[i].name; i++) { if (!silc_cipher_alloc(silc_default_ciphers[i].name, &enc_cipher)) { fprintf(stderr, "Error allocating %s\n", silc_default_ciphers[i].name); exit(1); } if (!silc_cipher_alloc(silc_default_ciphers[i].name, &dec_cipher)) { fprintf(stderr, "Error allocating %s\n", silc_default_ciphers[i].name); exit(1); } enc_acc_cipher = silc_acc_cipher(SILC_SOFTACC, enc_cipher); if (!enc_acc_cipher) continue; dec_acc_cipher = silc_acc_cipher(SILC_SOFTACC, dec_cipher); if (!dec_acc_cipher) continue; SILC_LOG_DEBUG(("Allocated cipher %s", silc_default_ciphers[i].name)); SILC_LOG_DEBUG(("Set key")); silc_cipher_set_key(enc_acc_cipher, data, silc_cipher_get_key_len(enc_cipher), TRUE); silc_cipher_set_key(dec_acc_cipher, data, silc_cipher_get_key_len(dec_cipher), FALSE); SILC_LOG_DEBUG(("Set IV")); silc_cipher_set_iv(enc_acc_cipher, iv); SILC_LOG_DEBUG(("Encrypt with accelerated cipher")); for (k = 0; k < ENC_ROUND; k++) silc_cipher_encrypt(enc_acc_cipher, data, data, ENC_LEN, NULL); SILC_LOG_HEXDUMP(("data"), data, ENC_LEN); SILC_LOG_DEBUG(("Set IV")); silc_cipher_set_iv(dec_cipher, iv); SILC_LOG_DEBUG(("Decrypt with associated cipher")); for (k = 0; k < ENC_ROUND; k++) silc_cipher_decrypt(dec_cipher, data, data, ENC_LEN, NULL); SILC_LOG_HEXDUMP(("data"), data, ENC_LEN); /* Verify */ SILC_LOG_DEBUG(("Verify")); for (k = 0; k < ENC_LEN; k++) if (data[k] != k % 255) goto err; SILC_LOG_DEBUG(("Ok")); SILC_LOG_DEBUG(("Set IV")); silc_cipher_set_iv(enc_cipher, iv); SILC_LOG_DEBUG(("Encrypt with associated cipher")); for (k = 0; k < ENC_ROUND; k++) silc_cipher_encrypt(enc_cipher, data, data, ENC_LEN, NULL); SILC_LOG_HEXDUMP(("data"), data, ENC_LEN); SILC_LOG_DEBUG(("Set IV")); silc_cipher_set_iv(dec_acc_cipher, iv); SILC_LOG_DEBUG(("Decrypt with accelerated cipher")); for (k = 0; k < ENC_ROUND; k++) silc_cipher_decrypt(dec_acc_cipher, data, data, ENC_LEN, NULL); SILC_LOG_HEXDUMP(("data"), data, ENC_LEN); /* Verify */ SILC_LOG_DEBUG(("Verify")); for (k = 0; k < ENC_LEN; k++) if (data[k] != k % 255) goto err; SILC_LOG_DEBUG(("Ok")); SILC_LOG_DEBUG(("Set IV")); silc_cipher_set_iv(enc_acc_cipher, iv); SILC_LOG_DEBUG(("Encrypt with accelerated cipher")); for (k = 0; k < ENC_ROUND; k++) silc_cipher_encrypt(enc_acc_cipher, data, data, ENC_LEN, NULL); SILC_LOG_HEXDUMP(("data"), data, ENC_LEN); SILC_LOG_DEBUG(("Set IV")); silc_cipher_set_iv(dec_acc_cipher, iv); SILC_LOG_DEBUG(("Decrypt with accelerated cipher")); for (k = 0; k < ENC_ROUND; k++) silc_cipher_decrypt(dec_acc_cipher, data, data, ENC_LEN, NULL); SILC_LOG_HEXDUMP(("data"), data, ENC_LEN); /* Verify */ SILC_LOG_DEBUG(("Verify")); for (k = 0; k < ENC_LEN; k++) if (data[k] != k % 255) goto err; SILC_LOG_DEBUG(("Ok")); SILC_LOG_DEBUG(("Set IV")); silc_cipher_set_iv(enc_cipher, iv); SILC_LOG_DEBUG(("Encrypt with associated cipher")); for (k = 0; k < ENC_ROUND; k++) silc_cipher_encrypt(enc_cipher, data, data, ENC_LEN, NULL); SILC_LOG_HEXDUMP(("data"), data, ENC_LEN); SILC_LOG_DEBUG(("Set IV")); silc_cipher_set_iv(dec_cipher, iv); SILC_LOG_DEBUG(("Decrypt with associated cipher")); for (k = 0; k < ENC_ROUND; k++) silc_cipher_decrypt(dec_cipher, data, data, ENC_LEN, NULL); SILC_LOG_HEXDUMP(("data"), data, ENC_LEN); /* Verify */ SILC_LOG_DEBUG(("Verify")); for (k = 0; k < ENC_LEN; k++) if (data[k] != k % 255) goto err; SILC_LOG_DEBUG(("Ok")); silc_cipher_free(enc_acc_cipher); silc_cipher_free(enc_cipher); silc_cipher_free(dec_acc_cipher); silc_cipher_free(dec_cipher); } silc_acc_uninit(SILC_SOFTACC); success = TRUE; err: SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); silc_crypto_uninit(); silc_runtime_uninit(); return !success; }
SilcBool silc_pkcs1_verify(void *public_key, unsigned char *signature, SilcUInt32 signature_len, unsigned char *data, SilcUInt32 data_len, SilcHash hash) { RsaPublicKey *key = public_key; SilcBool ret = FALSE; SilcMPInt mp_tmp2; SilcMPInt mp_dst; unsigned char *verify, unpadded[2048 + 1], hashr[SILC_HASH_MAXLEN]; SilcUInt32 verify_len, len = (key->bits + 7) / 8; SilcBufferStruct di, ldi; SilcHash ihash = NULL; SilcAsn1 asn1 = NULL; char *oid; SILC_LOG_DEBUG(("Verify signature")); asn1 = silc_asn1_alloc(); if (!asn1) return FALSE; silc_mp_init(&mp_tmp2); silc_mp_init(&mp_dst); /* Format the signature into MP int */ silc_mp_bin2mp(signature, signature_len, &mp_tmp2); /* Verify */ silc_rsa_public_operation(key, &mp_tmp2, &mp_dst); /* MP to data */ verify = silc_mp_mp2bin(&mp_dst, len, &verify_len); /* Unpad data */ if (!silc_pkcs1_decode(SILC_PKCS1_BT_PRV1, verify, verify_len, unpadded, sizeof(unpadded), &len)) goto err; silc_buffer_set(&di, unpadded, len); /* If hash isn't given, allocate the one given in digest info */ if (!hash) { /* Decode digest info */ if (!silc_asn1_decode(asn1, &di, SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL), SILC_ASN1_SEQUENCE, SILC_ASN1_SEQUENCE, SILC_ASN1_OID(&oid), SILC_ASN1_END, SILC_ASN1_END, SILC_ASN1_END)) goto err; if (!silc_hash_alloc_by_oid(oid, &ihash)) { SILC_LOG_DEBUG(("Unknown OID %s", oid)); goto err; } hash = ihash; } /* Hash the data */ silc_hash_make(hash, data, data_len, hashr); data = hashr; data_len = silc_hash_len(hash); oid = (char *)silc_hash_get_oid(hash); /* Encode digest info for comparison */ memset(&ldi, 0, sizeof(ldi)); if (!silc_asn1_encode(asn1, &ldi, SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL), SILC_ASN1_SEQUENCE, SILC_ASN1_SEQUENCE, SILC_ASN1_OID(oid), SILC_ASN1_NULL, SILC_ASN1_END, SILC_ASN1_OCTET_STRING(data, data_len), SILC_ASN1_END, SILC_ASN1_END)) goto err; SILC_LOG_HEXDUMP(("DigestInfo remote"), silc_buffer_data(&di), silc_buffer_len(&di)); SILC_LOG_HEXDUMP(("DigestInfo local"), silc_buffer_data(&ldi), silc_buffer_len(&ldi)); /* Compare */ if (silc_buffer_len(&di) == silc_buffer_len(&ldi) && !memcmp(silc_buffer_data(&di), silc_buffer_data(&ldi), silc_buffer_len(&ldi))) ret = TRUE; memset(verify, 0, verify_len); memset(unpadded, 0, sizeof(unpadded)); silc_free(verify); silc_mp_uninit(&mp_tmp2); silc_mp_uninit(&mp_dst); if (hash) memset(hashr, 0, sizeof(hashr)); if (ihash) silc_hash_free(ihash); silc_asn1_free(asn1); return ret; err: memset(verify, 0, verify_len); silc_free(verify); silc_mp_uninit(&mp_tmp2); silc_mp_uninit(&mp_dst); if (ihash) silc_hash_free(ihash); silc_asn1_free(asn1); return FALSE; }
SilcBool silc_pkcs1_sign(void *private_key, unsigned char *src, SilcUInt32 src_len, unsigned char *signature, SilcUInt32 signature_size, SilcUInt32 *ret_signature_len, SilcBool compute_hash, SilcHash hash) { RsaPrivateKey *key = private_key; unsigned char padded[2048 + 1], hashr[SILC_HASH_MAXLEN]; SilcMPInt mp_tmp; SilcMPInt mp_dst; SilcBufferStruct di; SilcUInt32 len = (key->bits + 7) / 8; const char *oid; SilcAsn1 asn1; SILC_LOG_DEBUG(("Sign")); if (sizeof(padded) < len) return FALSE; if (signature_size < len) return FALSE; oid = silc_hash_get_oid(hash); if (!oid) return FALSE; asn1 = silc_asn1_alloc(); if (!asn1) return FALSE; /* Compute hash */ if (compute_hash) { silc_hash_make(hash, src, src_len, hashr); src = hashr; src_len = silc_hash_len(hash); } /* Encode digest info */ memset(&di, 0, sizeof(di)); if (!silc_asn1_encode(asn1, &di, SILC_ASN1_SEQUENCE, SILC_ASN1_SEQUENCE, SILC_ASN1_OID(oid), SILC_ASN1_NULL, SILC_ASN1_END, SILC_ASN1_OCTET_STRING(src, src_len), SILC_ASN1_END, SILC_ASN1_END)) { silc_asn1_free(asn1); return FALSE; } SILC_LOG_HEXDUMP(("DigestInfo"), silc_buffer_data(&di), silc_buffer_len(&di)); /* Pad data */ if (!silc_pkcs1_encode(SILC_PKCS1_BT_PRV1, silc_buffer_data(&di), silc_buffer_len(&di), padded, len, NULL)) { silc_asn1_free(asn1); return FALSE; } silc_mp_init(&mp_tmp); silc_mp_init(&mp_dst); /* Data to MP */ silc_mp_bin2mp(padded, len, &mp_tmp); /* Sign */ silc_rsa_private_operation(key, &mp_tmp, &mp_dst); /* MP to data */ silc_mp_mp2bin_noalloc(&mp_dst, signature, len); *ret_signature_len = len; memset(padded, 0, sizeof(padded)); silc_mp_uninit(&mp_tmp); silc_mp_uninit(&mp_dst); if (compute_hash) memset(hashr, 0, sizeof(hashr)); silc_asn1_free(asn1); return TRUE; }
static void silc_buffer_stream_io(SilcStream stream, SilcStreamStatus status, void *context) { SilcBufferStream bs = context; SilcBuffer buffer = NULL; SilcUInt32 buf_len; int ret, len; if (bs->closed) return; if (status == SILC_STREAM_CAN_READ) { /* Read data */ SILC_LOG_DEBUG(("Read data from buffer stream %p", bs)); while ((ret = silc_stream_read(bs->stream, bs->inbuf->tail, silc_buffer_taillen(bs->inbuf))) > 0) { if (!buffer) { buffer = silc_buffer_alloc(0); if (!buffer) return; } silc_buffer_pull_tail(bs->inbuf, ret); /* Parse the buffer */ while ((len = silc_buffer_unformat(bs->inbuf, SILC_STR_BUFFER_ALLOC(buffer), SILC_STR_END)) > 0) { /* Deliver the buffer */ SILC_LOG_HEXDUMP(("Received buffer, size %d", silc_buffer_len(buffer)), silc_buffer_data(buffer), silc_buffer_len(buffer)); bs->receiver(SILC_OK, (SilcStream)bs, buffer, bs->context); silc_buffer_pull(bs->inbuf, len); buffer = silc_buffer_alloc(0); if (!buffer) return; } if (silc_buffer_len(bs->inbuf) > 0) { /* Not complete buffer, read more data */ buf_len = 4; if (silc_buffer_len(bs->inbuf) >= 4) { SILC_GET32_MSB(buf_len, bs->inbuf->data); SILC_LOG_DEBUG(("Incomplete buffer, wait for rest, buffer size %d", buf_len)); } /* Enlarge inbuf if needed */ if (silc_buffer_taillen(bs->inbuf) < buf_len) silc_buffer_realloc(bs->inbuf, silc_buffer_truelen(bs->inbuf) + buf_len); continue; } /* All data read, read more */ silc_buffer_reset(bs->inbuf); } silc_buffer_free(buffer); if (ret == 0 || ret == -2) { bs->receiver(silc_errno, (SilcStream)bs, NULL, bs->context); return; } } else { /* Write any pending data */ SILC_LOG_DEBUG(("Write pending data to buffer stream %p", bs)); while (silc_buffer_len(&bs->queue) > 0) { ret = silc_stream_write(bs->stream, silc_buffer_data(&bs->queue), silc_buffer_len(&bs->queue)); if (silc_unlikely(ret == 0)) return; if (silc_unlikely(ret == -2)) return; if (silc_unlikely(ret == -1)) { SILC_LOG_DEBUG(("Buffer stream %p would block, send later", bs)); return; } /* Wrote data */ silc_buffer_pull(&bs->queue, ret); } memset(&bs->queue, 0, sizeof(bs->queue)); silc_buffer_reset(bs->outbuf); } }
SilcBool silc_buffer_stream_send(SilcStream stream, SilcBuffer buffer) { SilcBufferStream bs = stream; int ret; SILC_LOG_HEXDUMP(("Send to buffer stream %p %d bytes", bs, silc_buffer_len(buffer)), silc_buffer_data(buffer), silc_buffer_len(buffer)); if (silc_unlikely(!SILC_IS_BUFFER_STREAM(bs))) { silc_set_errno(SILC_ERR_INVALID_ARGUMENT); return FALSE; } if (silc_unlikely(!buffer)) { silc_set_errno(SILC_ERR_INVALID_ARGUMENT); return FALSE; } if (silc_unlikely(bs->closed)) { SILC_LOG_DEBUG(("Buffer stream %p is closed", bs)); silc_set_errno(SILC_ERR_NOT_VALID); return FALSE; } /* Put to queue */ if (silc_buffer_format(bs->outbuf, SILC_STR_ADVANCE, SILC_STR_BUFFER(buffer), SILC_STR_END) < 0) return FALSE; ret = silc_buffer_headlen(&bs->queue); bs->queue.head = bs->outbuf->head; bs->queue.data = bs->queue.head + ret; bs->queue.tail = bs->outbuf->data; bs->queue.end = bs->outbuf->end; /* Write the queue buffer */ while (silc_buffer_len(&bs->queue) > 0) { ret = silc_stream_write(bs->stream, silc_buffer_data(&bs->queue), silc_buffer_len(&bs->queue)); if (silc_unlikely(ret == 0)) return FALSE; if (silc_unlikely(ret == -2)) return FALSE; if (silc_unlikely(ret == -1)) { SILC_LOG_DEBUG(("Buffer stream %p would block, send later", bs)); return TRUE; } /* Wrote data */ silc_buffer_pull(&bs->queue, ret); } memset(&bs->queue, 0, sizeof(bs->queue)); silc_buffer_reset(bs->outbuf); SILC_LOG_DEBUG(("Buffer sent to buffer stream %p", bs)); return TRUE; }