int main(int argc, char **argv) { SilcUInt64 sec; SilcUInt32 usec; double totsec; unsigned char *data; SilcUInt32 rounds; SilcUInt32 i, k; data = malloc(ENC_LEN * sizeof(*data)); if (!data) exit(1); for (i = 0; i < ENC_LEN; i++) data[i] = i % 255; silc_timer_synchronize(&timer); for (i = 0; silc_default_ciphers[i].name; i++) { if (!silc_cipher_alloc(silc_default_ciphers[i].name, &cipher)) { fprintf(stderr, "Error allocating %s\n", silc_default_ciphers[i].name); exit(1); } silc_cipher_set_key(cipher, data, silc_cipher_get_key_len(cipher), TRUE); silc_cipher_set_iv(cipher, data); rounds = ENC_ROUND; retry: silc_timer_start(&timer); for (k = 0; k < rounds; k++) silc_cipher_encrypt(cipher, data, data, ENC_LEN, NULL); silc_timer_stop(&timer); silc_timer_value(&timer, &sec, &usec); totsec = (double)sec; totsec += ((double)usec / (double)(1000 * 1000)); if (totsec < ENC_MIN_TIME) { rounds += rounds; goto retry; } printf("%s:\t%.2f KB (%.2f MB) / sec (total test time %.2f secs)\n", silc_default_ciphers[i].name, (((double)(ENC_LEN * rounds) / 1024.0) / totsec), (((double)(ENC_LEN * rounds) / (1024.0 * 1024.0)) / totsec), totsec); silc_cipher_free(cipher); } return 0; }
int main(int argc, char **argv) { SilcUInt64 sec; SilcUInt32 usec; double totsec; unsigned char *data; SilcUInt32 rounds; SilcUInt32 i, k; silc_runtime_init(); silc_crypto_init(NULL); #if 0 silc_log_debug(TRUE); silc_log_quick(TRUE); silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*acc*,*thread*"); #endif 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); for (i = 0; i < ENC_LEN; i++) data[i] = i % 255; silc_timer_synchronize(&timer); for (i = 0; silc_default_ciphers[i].name; i++) { if (!silc_cipher_alloc(silc_default_ciphers[i].name, &cipher)) { fprintf(stderr, "Error allocating %s\n", silc_default_ciphers[i].name); exit(1); } acc_cipher = silc_acc_cipher(SILC_SOFTACC, cipher); if (!acc_cipher) continue; silc_cipher_set_iv(acc_cipher, data); silc_cipher_set_key(acc_cipher, data, silc_cipher_get_key_len(cipher), TRUE); sleep(1); rounds = ENC_ROUND; retry: silc_timer_start(&timer); for (k = 0; k < rounds; k++) silc_cipher_encrypt(acc_cipher, data, data, ENC_LEN, NULL); silc_timer_stop(&timer); silc_timer_value(&timer, &sec, &usec); totsec = (double)sec; totsec += ((double)usec / (double)((double)1000 * (double)1000)); if (totsec < ENC_MIN_TIME) { rounds += rounds; goto retry; } silc_cipher_free(acc_cipher); silc_cipher_free(cipher); sleep(1); printf("%s:\t%.2f KB (%.2f MB, %.2f Mbit) / sec (total %.3f secs)\n", silc_default_ciphers[i].name, (((double)((double)ENC_LEN * (double)rounds) / 1024.0) / totsec), (((double)((double)ENC_LEN * (double)rounds) / (1024.0 * 1024.0)) / totsec), ((((double)((double)ENC_LEN * (double)rounds) / 1024.0) / 128.0) / totsec), totsec); } silc_acc_uninit(SILC_SOFTACC); silc_crypto_uninit(); silc_runtime_uninit(); return 0; }
SilcBool silc_client_add_private_message_key_ske(SilcClient client, SilcClientConnection conn, SilcClientEntry client_entry, const char *cipher, const char *hmac, SilcSKEKeyMaterial keymat) { if (!client || !client_entry) return FALSE; /* Return FALSE if key already set */ if (client_entry->internal.send_key && client_entry->internal.receive_key) return FALSE; if (!cipher) cipher = SILC_DEFAULT_CIPHER; if (!hmac) hmac = SILC_DEFAULT_HMAC; /* Check the requested cipher and HMAC */ if (!silc_cipher_is_supported(cipher)) return FALSE; if (!silc_hmac_is_supported(hmac)) return FALSE; client_entry->internal.generated = TRUE; /* Allocate the cipher and HMAC */ if (!silc_cipher_alloc(cipher, &client_entry->internal.send_key)) return FALSE; if (!silc_cipher_alloc(cipher, &client_entry->internal.receive_key)) return FALSE; if (!silc_hmac_alloc(hmac, NULL, &client_entry->internal.hmac_send)) return FALSE; if (!silc_hmac_alloc(hmac, NULL, &client_entry->internal.hmac_receive)) return FALSE; /* Set the keys */ if (client_entry->internal.prv_resp) { silc_cipher_set_key(client_entry->internal.send_key, keymat->receive_enc_key, keymat->enc_key_len, TRUE); silc_cipher_set_iv(client_entry->internal.send_key, keymat->receive_iv); silc_cipher_set_key(client_entry->internal.receive_key, keymat->send_enc_key, keymat->enc_key_len, FALSE); silc_cipher_set_iv(client_entry->internal.receive_key, keymat->send_iv); silc_hmac_set_key(client_entry->internal.hmac_send, keymat->receive_hmac_key, keymat->hmac_key_len); silc_hmac_set_key(client_entry->internal.hmac_receive, keymat->send_hmac_key, keymat->hmac_key_len); } else { silc_cipher_set_key(client_entry->internal.send_key, keymat->send_enc_key, keymat->enc_key_len, TRUE); silc_cipher_set_iv(client_entry->internal.send_key, keymat->send_iv); silc_cipher_set_key(client_entry->internal.receive_key, keymat->receive_enc_key, keymat->enc_key_len, FALSE); silc_cipher_set_iv(client_entry->internal.receive_key, keymat->receive_iv); silc_hmac_set_key(client_entry->internal.hmac_send, keymat->send_hmac_key, keymat->hmac_key_len); silc_hmac_set_key(client_entry->internal.hmac_receive, keymat->receive_hmac_key, keymat->hmac_key_len); } return TRUE; }
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; }