list_t * entry (char *host, int sock, int flags) { list_t *p = xmalloc (sizeof *p); p->host = xstrdup (host); #if WITH_SSL p->ssl = flags & 1 ? SSL_new (client) : SSL_new (server); SSL_set_fd (p->ssl, sock); #else p->sock = sock; gc_cipher_open (GC_AES256, GC_STREAM, &p->cipher); mpz_t my_num; mpz_init (my_num); char *key = xcalloc (len, sizeof *key); pthread_cleanup_push (free, key); gc_random (key, len); mpz_import (my_num, len, 1, 1, 1, 0, key); mpz_t sent; mpz_init_set (sent, base); mpz_powm (sent, sent, my_num, prime); char *buff = xcalloc (len, sizeof *buff); pthread_cleanup_push (free, buff); mpz_export (buff, NULL, 1, 1, 1, 0, sent); if (send (sock, buff, len, 0) < 0) error (1, errno, "Failed to perform handshake"); if (recv (sock, buff, len, 0) < 0) error (1, errno, "Failed to perform handshake"); mpz_import (sent, len, 1, 1, 1, 0, buff); pthread_cleanup_pop (1); mpz_powm (sent, sent, my_num, prime); mpz_export (key, NULL, 1, 1, 1, 0, sent); gc_cipher_setkey (p->cipher, len, key); pthread_cleanup_pop (1); #endif /* WITH_SSL */ return p; }
int main (int argc, char *argv[]) { gc_cipher_handle ctx; /* Test vector from Cryptlib via Libgcrypt labeled there: "from the State/Commerce Department". */ static char key_1[] = { 0x61, 0x8A, 0x63, 0xD2, 0xFB }; static char plaintext_1[] = { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C }; static const char ciphertext_1[] = { 0xF1, 0x38, 0x29, 0xC9, 0xDE }; char scratch[16]; Gc_rc rc; rc = gc_init (); if (rc != GC_OK) { printf ("gc_init() failed\n"); return 1; } rc = gc_cipher_open (GC_ARCFOUR40, GC_STREAM, &ctx); if (rc != GC_OK) return 1; rc = gc_cipher_setkey (ctx, sizeof (key_1), key_1); if (rc != GC_OK) return 1; memcpy (scratch, plaintext_1, sizeof (plaintext_1)); rc = gc_cipher_encrypt_inline (ctx, sizeof (plaintext_1), scratch); if (rc != GC_OK) return 1; if (memcmp (scratch, ciphertext_1, sizeof (ciphertext_1))) { size_t i; printf ("expected:\n"); for (i = 0; i < 5; i++) printf ("%02x ", scratch[i] & 0xFF); printf ("\ncomputed:\n"); for (i = 0; i < 5; i++) printf ("%02x ", ciphertext_1[i] & 0xFF); printf ("\n"); return 1; } /* decrypt */ rc = gc_cipher_setkey (ctx, sizeof (key_1), key_1); if (rc != GC_OK) return 1; rc = gc_cipher_decrypt_inline (ctx, sizeof (plaintext_1), scratch); if (rc != GC_OK) return 1; if (memcmp (scratch, plaintext_1, sizeof (plaintext_1))) { size_t i; printf ("expected:\n"); for (i = 0; i < 5; i++) printf ("%02x ", plaintext_1[i] & 0xFF); printf ("\ncomputed:\n"); for (i = 0; i < 5; i++) printf ("%02x ", scratch[i] & 0xFF); printf ("\n"); return 1; } gc_done (); return 0; }
int main (int argc, char *argv[]) { gc_cipher_handle ctx; Gc_rc rc; rc = gc_init (); if (rc != GC_OK) { printf ("gc_init() failed\n"); return 1; } /* * DES Maintenance Test */ { int i; char key[8] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 }; char input[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; char result[8] = { 0x24, 0x6e, 0x9d, 0xb9, 0xc5, 0x50, 0x38, 0x1a }; char temp1[8], temp2[8], temp3[8]; for (i = 0; i < 64; ++i) { rc = gc_cipher_open (GC_DES, GC_ECB, &ctx); if (rc != GC_OK) return 1; rc = gc_cipher_setkey (ctx, 8, key); if (rc != GC_OK) return 1; memcpy (temp1, input, 8); rc = gc_cipher_encrypt_inline (ctx, 8, temp1); if (rc != GC_OK) return 1; memcpy (temp2, temp1, 8); rc = gc_cipher_encrypt_inline (ctx, 8, temp2); if (rc != GC_OK) return 1; rc = gc_cipher_setkey (ctx, 8, temp2); if (rc != GC_OK) return 1; memcpy (temp3, temp1, 8); rc = gc_cipher_decrypt_inline (ctx, 8, temp3); if (rc != GC_OK) return 1; memcpy (key, temp3, 8); memcpy (input, temp1, 8); } if (memcmp (temp3, result, 8)) return 1; } gc_done (); return 0; }
int main (int argc, char *argv[]) { Gc_rc rc; rc = gc_init (); if (rc != GC_OK) { printf ("gc_init() failed\n"); return 1; } { char buf[16]; char key[] = "\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00"; char pt[] = "\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00"; char ct[] = "\xC3\x4C\x05\x2C\xC0\xDA\x8D\x73" "\x45\x1A\xFE\x5F\x03\xBE\x29\x7F"; gc_cipher_handle ctx; size_t i; rc = gc_cipher_open (GC_AES128, GC_ECB, &ctx); if (rc != GC_OK) return 1; rc = gc_cipher_setkey (ctx, 16, key); if (rc != GC_OK) return 1; memcpy (buf, pt, 16); for (i = 0; i < 10000; i++) { rc = gc_cipher_encrypt_inline (ctx, 16, buf); if (rc != GC_OK) { printf ("encrypt failed %d\n", rc); return 1; } } if (memcmp (buf, ct, 16) != 0) { size_t i; printf ("expected:\n"); for (i = 0; i < 16; i++) printf ("%02x ", ct[i] & 0xFF); printf ("\ncomputed:\n"); for (i = 0; i < 16; i++) printf ("%02x ", buf[i] & 0xFF); printf ("\n"); return 1; } for (i = 0; i < 10000; i++) { rc = gc_cipher_decrypt_inline (ctx, 16, buf); if (rc != GC_OK) { printf ("decrypt failed %d\n", rc); return 1; } } if (memcmp (buf, pt, 16) != 0) { size_t i; printf ("expected:\n"); for (i = 0; i < 16; i++) printf ("%02x ", pt[i] & 0xFF); printf ("\ncomputed:\n"); for (i = 0; i < 16; i++) printf ("%02x ", buf[i] & 0xFF); printf ("\n"); return 1; } gc_cipher_close (ctx); } { char buf[16]; char iv[] = "\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00"; char key[] = "\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00"; char pt[] = "\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00"; char ct[] = "\x66\xe9\x4b\xd4\xef\x8a\x2c\x3b" "\x88\x4c\xfa\x59\xca\x34\x2b\x2e"; gc_cipher_handle ctx; size_t i; rc = gc_cipher_open (GC_AES128, GC_CBC, &ctx); if (rc != GC_OK) return 1; rc = gc_cipher_setkey (ctx, 16, key); if (rc != GC_OK) return 1; rc = gc_cipher_setiv (ctx, 16, iv); if (rc != GC_OK) return 1; memcpy (buf, pt, 16); for (i = 0; i < 10000; i++) { rc = gc_cipher_encrypt_inline (ctx, 16, buf); if (rc != GC_OK) { printf ("encrypt failed %d\n", rc); return 1; } } if (memcmp (buf, ct, 16) != 0) { size_t i; printf ("expected:\n"); for (i = 0; i < 16; i++) printf ("%02x ", ct[i] & 0xFF); printf ("\ncomputed:\n"); for (i = 0; i < 16; i++) printf ("%02x ", buf[i] & 0xFF); printf ("\n"); return 1; } gc_cipher_close (ctx); } gc_done (); return 0; }
int main (int argc, char *argv[]) { gc_cipher_handle ctx; /* Test vectors from RFC 2268. */ static char key[8] = "\xff\xff\xff\xff\xff\xff\xff\xff"; static char plaintext[8] = "\xff\xff\xff\xff\xff\xff\xff\xff"; static const char ciphertext[8] = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49"; char scratch[16]; Gc_rc rc; rc = gc_init (); if (rc != GC_OK) { printf ("gc_init() failed\n"); return 1; } rc = gc_cipher_open (GC_ARCTWO40, GC_ECB, &ctx); if (rc != GC_OK) return 1; rc = gc_cipher_setkey (ctx, sizeof (key), key); if (rc != GC_OK) return 1; memcpy (scratch, plaintext, sizeof (plaintext)); rc = gc_cipher_encrypt_inline (ctx, sizeof (plaintext), scratch); if (rc != GC_OK) return 1; if (memcmp (scratch, ciphertext, sizeof (ciphertext))) { size_t i; printf ("expected:\n"); for (i = 0; i < 5; i++) printf ("%02x ", scratch[i] & 0xFF); printf ("\ncomputed:\n"); for (i = 0; i < 5; i++) printf ("%02x ", ciphertext[i] & 0xFF); printf ("\n"); return 1; } /* decrypt */ rc = gc_cipher_setkey (ctx, sizeof (key), key); if (rc != GC_OK) return 1; rc = gc_cipher_decrypt_inline (ctx, sizeof (plaintext), scratch); if (rc != GC_OK) return 1; if (memcmp (scratch, plaintext, sizeof (plaintext))) { size_t i; printf ("expected:\n"); for (i = 0; i < 5; i++) printf ("%02x ", plaintext[i] & 0xFF); printf ("\ncomputed:\n"); for (i = 0; i < 5; i++) printf ("%02x ", scratch[i] & 0xFF); printf ("\n"); return 1; } gc_done (); return 0; }