static const char* selftest(void) { ARCFOUR_context ctx; byte scratch[16]; /* Test vector from Cryptlib labeled there: "from the State/Commerce Department". */ static const byte key_1[] = { 0x61, 0x8A, 0x63, 0xD2, 0xFB }; static const byte plaintext_1[] = { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C }; static const byte ciphertext_1[] = { 0xF1, 0x38, 0x29, 0xC9, 0xDE }; arcfour_setkey( &ctx, key_1, sizeof(key_1)); encrypt_stream( &ctx, scratch, plaintext_1, sizeof(plaintext_1)); if ( memcmp (scratch, ciphertext_1, sizeof (ciphertext_1))) return "Arcfour encryption test 1 failed."; arcfour_setkey( &ctx, key_1, sizeof(key_1)); encrypt_stream(&ctx, scratch, scratch, sizeof(plaintext_1)); /* decrypt */ if ( memcmp (scratch, plaintext_1, sizeof (plaintext_1))) return "Arcfour decryption test 1 failed."; return NULL; }
int main (int argc, char *argv[]) { arcfour_context 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]; arcfour_setkey (&ctx, key_1, sizeof (key_1)); arcfour_stream (&ctx, plaintext_1, scratch, sizeof (plaintext_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 */ arcfour_setkey (&ctx, key_1, sizeof (key_1)); arcfour_stream (&ctx, scratch, scratch, sizeof (plaintext_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; } return 0; }
Gc_rc gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key) { _gc_cipher_ctx *ctx = handle; switch (ctx->alg) { #ifdef GNULIB_GC_ARCTWO case GC_ARCTWO40: arctwo_setkey (&ctx->arctwoContext, keylen, key); break; #endif #ifdef GNULIB_GC_ARCFOUR case GC_ARCFOUR128: case GC_ARCFOUR40: arcfour_setkey (&ctx->arcfourContext, key, keylen); break; #endif #ifdef GNULIB_GC_DES case GC_DES: if (keylen != 8) return GC_INVALID_CIPHER; gl_des_setkey (&ctx->desContext, key); break; #endif #ifdef GNULIB_GC_RIJNDAEL case GC_AES128: case GC_AES192: case GC_AES256: { rijndael_rc rc; size_t i; char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1]; for (i = 0; i < keylen; i++) sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF); rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT, keylen * 8, keyMaterial); if (rc < 0) return GC_INVALID_CIPHER; rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT, keylen * 8, keyMaterial); if (rc < 0) return GC_INVALID_CIPHER; rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL); if (rc < 0) return GC_INVALID_CIPHER; } break; #endif default: return GC_INVALID_CIPHER; } return GC_OK; }
static void arcfour256_key(void *handle, unsigned char *key) { ArcfourContext *ctx = (ArcfourContext *)handle; arcfour_setkey(ctx, key, 32); arcfour_stir(ctx); }