Example #1
0
Gc_rc
gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
{
  _gc_cipher_ctx *ctx = handle;

  switch (ctx->alg)
    {
#ifdef GNULIB_GC_ARCTWO
    case GC_ARCTWO40:
      if (ivlen != ARCTWO_BLOCK_SIZE)
	return GC_INVALID_CIPHER;
      memcpy (ctx->arctwoIV, iv, ivlen);
      break;
#endif

#ifdef GNULIB_GC_RIJNDAEL
    case GC_AES128:
    case GC_AES192:
    case GC_AES256:
      switch (ctx->mode)
	{
	case GC_ECB:
	  /* Doesn't use IV. */
	  break;

	case GC_CBC:
	  {
	    rijndael_rc rc;
	    size_t i;
	    char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];

	    for (i = 0; i < ivlen; i++)
	      sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);

	    rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
				     ivMaterial);
	    if (rc < 0)
	      return GC_INVALID_CIPHER;
	  }
	  break;

	default:
	  return GC_INVALID_CIPHER;
	}
      break;
#endif

    default:
      return GC_INVALID_CIPHER;
    }

  return GC_OK;
}
Example #2
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;
}
Example #3
0
int
main (int argc, char *argv[])
{
  int rc;
  rijndaelKeyInstance key;
  rijndaelCipherInstance cipher;
  char in[RIJNDAEL_BITSPERBLOCK / 8];
  char out[RIJNDAEL_BITSPERBLOCK / 8];
  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";
  size_t i;

  rc = rijndaelMakeKey (&key, RIJNDAEL_DIR_ENCRYPT,
                        128, "00000000000000000000000000000000");
  if (rc != 0)
    printf ("makeKey failed %d\n", rc);

  rc = rijndaelCipherInit (&cipher, RIJNDAEL_MODE_ECB, NULL);
  if (rc != 0)
    printf ("cipherInit failed %d\n", rc);

  memset (in, 0, RIJNDAEL_BITSPERBLOCK / 8);

  for (i = 0; i < 10000; i++)
    {
      rc = rijndaelBlockEncrypt (&cipher, &key, in, 128, out);
      if (rc < 0)
        printf ("blockEncrypt failed %d\n", rc);

      memcpy (in, out, RIJNDAEL_BITSPERBLOCK / 8);
    }

  if (memcmp (out, ct, RIJNDAEL_BITSPERBLOCK / 8) != 0)
    {
      size_t i;
      printf ("expected:\n");
      for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
        printf ("%02x ", ct[i] & 0xFF);
      printf ("\ncomputed:\n");
      for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
        printf ("%02x ", out[i] & 0xFF);
      printf ("\n");
      return 1;
    }

  rc = rijndaelMakeKey (&key, RIJNDAEL_DIR_DECRYPT,
                        128, "00000000000000000000000000000000");
  if (rc != 0)
    printf ("makeKey failed %d\n", rc);

  rc = rijndaelCipherInit (&cipher, RIJNDAEL_MODE_ECB, NULL);
  if (rc != 0)
    printf ("cipherInit failed %d\n", rc);

  for (i = 0; i < 10000; i++)
    {
      memcpy (in, out, RIJNDAEL_BITSPERBLOCK / 8);

      rc = rijndaelBlockDecrypt (&cipher, &key, in, 128, out);
      if (rc < 0)
        printf ("blockEncrypt failed %d\n", rc);
    }

  if (memcmp (out, pt, RIJNDAEL_BITSPERBLOCK / 8) != 0)
    {
      size_t i;
      printf ("expected:\n");
      for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
        printf ("%02x ", pt[i] & 0xFF);
      printf ("\ncomputed:\n");
      for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
        printf ("%02x ", out[i] & 0xFF);
      printf ("\n");
      return 1;
    }

  return 0;
}