Crypt-Nettle

 view release on metacpan or  search on metacpan

Nettle.xs  view on Meta::CPAN

#include <gmp.h>
#include <string.h>

static const char my_name[] = "Crypt::Nettle";
static const char author[] = "Daniel Kahn Gillmor <dkg@fifthhorseman.net>";


enum cnc_cipher_mode {
  CNC_MODE_UNKNOWN,
  CNC_MODE_ECB,
  CNC_MODE_CBC,
  CNC_MODE_CTR
};
struct cnc_cipher_mode_name {
  enum cnc_cipher_mode mode;
  const char * name;
};

const struct cnc_cipher_mode_name cipher_modes_available[] = {
  { CNC_MODE_ECB, "ecb" },
  { CNC_MODE_CBC, "cbc" },
  { CNC_MODE_CTR, "ctr" }
};

STATIC
enum cnc_cipher_mode
_cnc_cipher_mode_lookup(const char* name) {
  int i;
  for (i = 0; i < sizeof(cipher_modes_available)/sizeof(*cipher_modes_available); i++)
    if (0 == strcasecmp(name, cipher_modes_available[i].name))
      return cipher_modes_available[i].mode;

Nettle.xs  view on Meta::CPAN

    break;
  case CNC_MODE_CTR: /* encrypt and decrypt are the same function by definition in CTR mode */
    ctr_crypt(cnc->cipher_context, 
              cnc->ciphertype->encrypt,
              cnc->ciphertype->block_size,
              cnc->chain_state, 
              datalen,
              outbuf,
              databuf);
    break;
  case CNC_MODE_CBC:
    if (cnc->is_encrypt)
      cbc_encrypt(cnc->cipher_context, 
                  cnc->ciphertype->encrypt,
                  cnc->ciphertype->block_size,
                  cnc->chain_state, 
                  datalen,
                  outbuf,
                  databuf);
    else
      cbc_decrypt(cnc->cipher_context, 

lib/Crypt/Nettle/Cipher.pm  view on Meta::CPAN

The parameter $algo must be the name of a symmetric encryption
algorithm supported by libnettle.

Note that $key must match the key_size() for the selected algorithm.

If $is_encrypt is 'decrypt' or 0, the new object will do decryption;
If $is_encrypt is 'encrypt' or 1, it will encrypt.

You can set the $mode of the cipher with 'ecb' (Electronic Code Book),
'cbc' (Cipher Block Chaining), or 'ctr' (Counter).  ECB is the default
because it is simpler to configure, but you probably want CBC or CTR
for security.

If you use CBC or CTR, you'll need to supply an initialization vector
(CBC) or initialization counter (CTR) in the $iv parameter.  $iv
should be the size of block_size().

On error, will return undefined.

Supported encryption algorithms are: aes128, aes192, aes256, arctwo40,
arctwo64, arctwo128, arctwo_gutmann128, arcfour128, camellia128,
camellia192, camellia256, cast128, serpent128, serpent192, serpent256,
twofish128, twofish192, twofish256.

(you can retrieve these programmatically with algos_available()).

t/03-cipher.t  view on Meta::CPAN


#########################

# i generated this with:

# for key in 0 deadbeef ffffffff; do printf "  '%s' => {\n" "$key";  for secret in '0123456789abcdef0123456789abcdef' '________________________________' 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; do printf "    '%s' => {\n" "$secret"; for  algo in aes camel...


# FIXME: i'm only testing AES, CAMELLIA, and CAST here.  should try to test more!

# FIXME: i'm only testing ECB mode.  We should test CTR and CBC modes as well!

# key -> cleartext -> algorithm -> ciphertext
my $ciphers = {
  '0' => {
    '0123456789abcdef0123456789abcdef' => {
      'aes128' => 'FPX+dGlm8pJlHCKIu/9GCRT1/nRpZvKSZRwiiLv/Rgk=',
      'aes192' => 'VAuhOrO8xcaLJZhKON6Z3FQLoTqzvMXGiyWYSjjemdw=',
      'aes256' => 'uMMzGtqcnpOzXOYBwDQNrbjDMxranJ6Ts1zmAcA0Da0=',
      'camellia128' => 'WJ3mFua227sR8qEjht3WoFid5hbmttu7EfKhI4bd1qA=',
      'camellia192' => 'ZtOzFqft8hYo0Qeyw3ChYWbTsxan7fIWKNEHssNwoWE=',



( run in 0.829 second using v1.01-cache-2.11-cpan-e1769b4cff6 )