+ 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*,*twofish*,*cipher*");
+ }
+
+ SILC_LOG_DEBUG(("Registering builtin hash functions"));
+ silc_cipher_register_default();
+
+ SILC_LOG_DEBUG(("Allocating twofish-CBC cipher"));
+ if (!silc_cipher_alloc("twofish-128-cbc", &cipher)) {
+ SILC_LOG_DEBUG(("Allocating twofish-CBC cipher failed"));
+ goto err;
+ }
+ if (!silc_cipher_alloc("twofish-128-cbc", &cipher2)) {
+ SILC_LOG_DEBUG(("Allocating twofish-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 twofish-128-ctr cipher"));
+ if (!silc_cipher_alloc("twofish-128-ctr", &cipher)) {
+ SILC_LOG_DEBUG(("Allocating twofish-128-ctr cipher failed"));
+ goto err;
+ }
+
+ /* Third test vector */
+ SILC_LOG_DEBUG(("CTR test vector"));
+ memset(dst, 0, sizeof(dst));
+ memset(pdst, 0, sizeof(pdst));
+ silc_cipher_set_iv(cipher, iv3);
+ assert(silc_cipher_set_key(cipher, key3, key3_len, TRUE));
+ assert(silc_cipher_encrypt(cipher, p3, dst, p3_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 *)p3, p3_len);
+ SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p3_len);
+ SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c3, p3_len);
+ if (memcmp(dst, c3, p3_len)) {
+ SILC_LOG_DEBUG(("Encrypt failed"));
+ goto err;
+ }
+ SILC_LOG_DEBUG(("Encrypt is successful"));
+ silc_cipher_set_iv(cipher, iv3);
+ assert(silc_cipher_decrypt(cipher, dst, pdst, p3_len, NULL));
+ SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p3_len);
+ SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p3, p3_len);
+ if (memcmp(pdst, p3, p3_len)) {
+ SILC_LOG_DEBUG(("Decrypt failed"));
+ goto err;
+ }
+ SILC_LOG_DEBUG(("Decrypt is successful"));
+
+
+ /* Fourth test vector */
+ SILC_LOG_DEBUG(("CTR 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 twofish-256-ctr cipher"));
+ if (!silc_cipher_alloc("twofish-256-ctr", &cipher)) {
+ SILC_LOG_DEBUG(("Allocating twofish-256-ctr cipher failed"));
+ goto err;
+ }
+ if (!silc_cipher_alloc("twofish-256-ctr", &cipher2)) {
+ SILC_LOG_DEBUG(("Allocating twofish-256-ctr cipher failed"));
+ goto err;
+ }
+
+ /* Fifth test vector */
+ SILC_LOG_DEBUG(("CTR test vector"));
+ memset(dst, 0, sizeof(dst));
+ memset(pdst, 0, sizeof(pdst));
+ silc_cipher_set_iv(cipher, iv5);
+ assert(silc_cipher_set_key(cipher, key5, key5_len, TRUE));
+ assert(silc_cipher_set_key(cipher2, key5, key5_len, FALSE));
+ assert(silc_cipher_encrypt(cipher, p5, dst, p5_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 *)p5, p5_len);
+ SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p5_len);
+ SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c5, p5_len);
+ if (memcmp(dst, c5, p5_len)) {
+ SILC_LOG_DEBUG(("Encrypt failed"));
+ goto err;
+ }
+ SILC_LOG_DEBUG(("Encrypt is successful"));
+ silc_cipher_set_iv(cipher2, iv5);
+ assert(silc_cipher_decrypt(cipher2, dst, pdst, p5_len, NULL));
+ SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p5_len);
+ SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p5, p5_len);
+ if (memcmp(pdst, p5, p5_len)) {
+ SILC_LOG_DEBUG(("Decrypt failed"));
+ goto err;
+ }
+ SILC_LOG_DEBUG(("Decrypt is successful"));
+ silc_cipher_free(cipher2);
+
+
+ SILC_LOG_DEBUG(("Allocating twofish-256-cfb cipher"));
+ if (!silc_cipher_alloc("twofish-256-cfb", &cipher)) {
+ SILC_LOG_DEBUG(("Allocating twofish-256-cfb cipher failed"));
+ goto err;
+ }
+ if (!silc_cipher_alloc("twofish-256-cfb", &cipher2)) {
+ SILC_LOG_DEBUG(("Allocating twofish-256-cfb cipher failed"));
+ goto err;
+ }
+
+ /* Fifth test vector */
+ 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 *)p5, 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_free(cipher);
+ silc_cipher_unregister_all();
+ return success;