Beispiel #1
0
Gc_rc
gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
{
  _gc_cipher_ctx *ctx = handle;

  switch (ctx->alg)
    {
#ifdef GNULIB_GC_ARCTWO
    case GC_ARCTWO40:
      switch (ctx->mode)
	{
	case GC_ECB:
	  arctwo_encrypt (&ctx->arctwoContext, data, data, len);
	  break;

	case GC_CBC:
	  for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
		 data += ARCTWO_BLOCK_SIZE)
	    {
	      size_t i;
	      for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
		data[i] ^= ctx->arctwoIV[i];
	      arctwo_encrypt (&ctx->arctwoContext, data, data,
			      ARCTWO_BLOCK_SIZE);
	      memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
	    }
	    break;

	default:
	  return GC_INVALID_CIPHER;
	}
      break;
#endif

#ifdef GNULIB_GC_ARCFOUR
    case GC_ARCFOUR128:
    case GC_ARCFOUR40:
      arcfour_stream (&ctx->arcfourContext, data, data, len);
      break;
#endif

#ifdef GNULIB_GC_DES
    case GC_DES:
      for (; len >= 8; len -= 8, data += 8)
	gl_des_ecb_encrypt (&ctx->desContext, data, data);
      break;
#endif

#ifdef GNULIB_GC_RIJNDAEL
    case GC_AES128:
    case GC_AES192:
    case GC_AES256:
      {
	int nblocks;

	nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
					data, 8 * len, data);
	if (nblocks < 0)
	  return GC_INVALID_CIPHER;
      }
      break;
#endif

    default:
      return GC_INVALID_CIPHER;
    }

  return GC_OK;
}
Beispiel #2
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;
}