Commit 7eb5b4d0 authored by Geoff Simmons's avatar Geoff Simmons

Some necessary fixes and additions, so that first tests with AES pass.

* IV and CTR are set for the en-/decrypt operations, not in the constructor.
  This means that the crypto handle must be copied to the stack during the
  en-/decrypt calls, because setting IV/CTR changes its internal state.
* Add code for padding (not yet tested).
parent d811eecb
Pipeline #160 skipped
......@@ -38,7 +38,7 @@ Privacy Guard cryptographic suite (GnuPG or GPG).
CONTENTS
========
* symmetric(ENUM {AES,AES128,RIJNDAEL,RIJNDAEL128,AES192,RIJNDAEL192,AES256,RIJNDAEL256}, ENUM {ECB,CFB,CBC,OFB,CTR}, BLOB, BLOB, BLOB, BOOL, BOOL, BOOL, BOOL)
* symmetric(ENUM {AES,AES128,RIJNDAEL,RIJNDAEL128,AES192,RIJNDAEL192,AES256,RIJNDAEL256}, ENUM {ECB,CFB,CBC,OFB,CTR}, ENUM {PKCS7,ISO7816,X923,NONE}, BLOB, BOOL, BOOL, BOOL, BOOL)
* STRING version()
* STRING gcrypt_version()
......@@ -49,7 +49,7 @@ symmetric
::
new OBJ = symmetric(ENUM {AES,AES128,RIJNDAEL,RIJNDAEL128,AES192,RIJNDAEL192,AES256,RIJNDAEL256} cipher, ENUM {ECB,CFB,CBC,OFB,CTR} mode, BLOB key, BLOB iv=0, BLOB ctr=0, BOOL secure=1, BOOL enable_sync=0, BOOL cbc_cts=0, BOOL cbc_mac=0)
new OBJ = symmetric(ENUM {AES,AES128,RIJNDAEL,RIJNDAEL128,AES192,RIJNDAEL192,AES256,RIJNDAEL256} cipher, ENUM {ECB,CFB,CBC,OFB,CTR} mode, ENUM {PKCS7,ISO7816,X923,NONE} padding="PKCS7", BLOB key, BOOL secure=1, BOOL enable_sync=0, BOOL cbc_cts=0, BOOL cbc_mac=0)
.. _func_symmetric.encrypt:
......@@ -58,7 +58,7 @@ symmetric.encrypt
::
BLOB symmetric.encrypt(BLOB)
BLOB symmetric.encrypt(BLOB plaintext, BLOB iv=0, BLOB ctr=0)
.. _func_symmetric.decrypt:
......@@ -67,7 +67,7 @@ symmetric.decrypt
::
BLOB symmetric.decrypt(BLOB)
BLOB symmetric.decrypt(BLOB ciphertext, BLOB iv=0, BLOB ctr=0)
.. _func_version:
......
......@@ -25,8 +25,10 @@
*
*/
MODE(ECB, GCRY_CIPHER_MODE_ECB)
MODE(CFB, GCRY_CIPHER_MODE_CFB)
MODE(CBC, GCRY_CIPHER_MODE_CBC)
MODE(OFB, GCRY_CIPHER_MODE_OFB)
MODE(CTR, GCRY_CIPHER_MODE_CTR)
/* VCL enum, gcrypt constant, need padding, need iv, need ctr */
MODE(ECB, GCRY_CIPHER_MODE_ECB, 1, 0, 0)
MODE(CFB, GCRY_CIPHER_MODE_CFB, 0, 1, 0)
MODE(CBC, GCRY_CIPHER_MODE_CBC, 1, 1, 0)
MODE(OFB, GCRY_CIPHER_MODE_OFB, 0, 1, 0)
MODE(CTR, GCRY_CIPHER_MODE_CTR, 0, 0, 1)
/*-
* Copyright 2017 UPLEX - Nils Goroll Systemoptimierung
* All rights reserved.
*
* Author: Geoffrey Simmons <geoffrey.simmons@uplex.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
PADDING(PKCS7)
PADDING(ISO7816)
PADDING(X923)
PADDING(NONE)
# looks like -*- vcl -*-
varnishtest "AES"
# from selftest() in libgcrypt cipher/rijndael.c
varnish v1 -vcl {
import blobcode;
import gcrypt from "${vmod_topbuild}/src/.libs/libvmod_gcrypt.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new k1 = blobcode.blob(HEX, "E8E9EAEBEDEEEFF0F2F3F4F5F7F8F9FA");
new aes = gcrypt.symmetric(AES, ECB, NONE, key=k1.get());
new p1 = blobcode.blob(HEX, "014BAF2278A69D331D5180103643E99A");
new c1 = blobcode.blob(HEX, "6743C3D1519AB4F2CD9A78AB09A511BD");
}
sub vcl_recv {
return(synth(200));
}
sub vcl_synth {
set resp.http.c1
= blobcode.encode(HEXUC, aes.encrypt(p1.get()));
set resp.http.p1
= blobcode.encode(HEXUC, aes.decrypt(c1.get()));
return(deliver);
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.c1 == "6743C3D1519AB4F2CD9A78AB09A511BD"
expect resp.http.p1 == "014BAF2278A69D331D5180103643E99A"
} -run
# from check_aes128_cbc_cts_cipher() in libgcrypt tests/basic.c
varnish v1 -vcl {
import blobcode;
import gcrypt from "${vmod_topbuild}/src/.libs/libvmod_gcrypt.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new k = blobcode.blob(encoded="chicken teriyaki");
new iv = blobcode.blob(encoded="");
new aes = gcrypt.symmetric(AES, CBC, key=k.get(),
cbc_cts=true);
}
sub vcl_recv {
return(synth(200));
}
sub vcl_synth {
set resp.http.plaintext =
"I would like the General Gau's Chicken, please, and wonton soup.";
set resp.http.ciphertext17
= blobcode.encode(HEXLC,
aes.encrypt(blobcode.decode_n(17,
encoded=resp.http.plaintext),
iv=iv.get()));
set resp.http.ciphertext31
= blobcode.encode(HEXLC,
aes.encrypt(blobcode.decode_n(31,
encoded=resp.http.plaintext),
iv=iv.get()));
set resp.http.ciphertext32
= blobcode.encode(HEXLC,
aes.encrypt(blobcode.decode_n(32,
encoded=resp.http.plaintext),
iv=iv.get()));
set resp.http.ciphertext47
= blobcode.encode(HEXLC,
aes.encrypt(blobcode.decode_n(47,
encoded=resp.http.plaintext),
iv=iv.get()));
set resp.http.ciphertext48
= blobcode.encode(HEXLC,
aes.encrypt(blobcode.decode_n(48,
encoded=resp.http.plaintext),
iv=iv.get()));
set resp.http.ciphertext64
= blobcode.encode(HEXLC,
aes.encrypt(blobcode.decode_n(64,
encoded=resp.http.plaintext),
iv=iv.get()));
set resp.http.ciphertext
= blobcode.encode(HEXLC,
aes.encrypt(blobcode.decode(
encoded=resp.http.plaintext),
iv=iv.get()));
set resp.http.plaintext17
= blobcode.encode(blob=aes.decrypt(blobcode.decode(HEX,
resp.http.ciphertext17),
iv=iv.get()));
set resp.http.plaintext31
= blobcode.encode(blob=aes.decrypt(blobcode.decode(HEX,
resp.http.ciphertext31),
iv=iv.get()));
set resp.http.plaintext32
= blobcode.encode(blob=aes.decrypt(blobcode.decode(HEX,
resp.http.ciphertext32),
iv=iv.get()));
set resp.http.plaintext47
= blobcode.encode(blob=aes.decrypt(blobcode.decode(HEX,
resp.http.ciphertext47),
iv=iv.get()));
set resp.http.plaintext48
= blobcode.encode(blob=aes.decrypt(blobcode.decode(HEX,
resp.http.ciphertext48),
iv=iv.get()));
set resp.http.plaintext64
= blobcode.encode(blob=aes.decrypt(blobcode.decode(HEX,
resp.http.ciphertext64),
iv=iv.get()));
set resp.http.plaintext
= blobcode.encode(blob=aes.decrypt(blobcode.decode(HEX,
resp.http.ciphertext),
iv=iv.get()));
return(deliver);
}
}
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.ciphertext17 == "c6353568f2bf8cb4d8a580362da7ff7f97"
expect resp.http.ciphertext31 == "fc00783e0efdb2c1d445d4c8eff7ed2297687268d6ecccc0c07b25e25ecfe5"
expect resp.http.ciphertext32 == "39312523a78662d5be7fcbcc98ebf5a897687268d6ecccc0c07b25e25ecfe584"
expect resp.http.ciphertext47 == "97687268d6ecccc0c07b25e25ecfe584b3fffd940c16a18c1b5549d2f838029e39312523a78662d5be7fcbcc98ebf5"
expect resp.http.ciphertext48 == "97687268d6ecccc0c07b25e25ecfe5849dad8bbb96c4cdc03bc103e1a194bbd839312523a78662d5be7fcbcc98ebf5a8"
expect resp.http.ciphertext64 == "97687268d6ecccc0c07b25e25ecfe58439312523a78662d5be7fcbcc98ebf5a84807efe836ee89a526730dbc2f7bc8409dad8bbb96c4cdc03bc103e1a194bbd8"
expect resp.http.ciphertext == resp.http.ciphertext64
expect resp.http.plaintext17 == "I would like the "
expect resp.http.plaintext31 == "I would like the General Gau's "
expect resp.http.plaintext32 == "I would like the General Gau's C"
expect resp.http.plaintext47 == "I would like the General Gau's Chicken, please,"
expect resp.http.plaintext48 == "I would like the General Gau's Chicken, please, "
expect resp.http.plaintext64 == "I would like the General Gau's Chicken, please, and wonton soup."
expect resp.http.plaintext == resp.http.plaintext64
} -run
This diff is collapsed.
/*-
* Copyright 2017 UPLEX - Nils Goroll Systemoptimierung
* All rights reserved.
*
* Author: Geoffrey Simmons <geoffrey.simmons@uplex.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
enum padding {
#define PADDING(p) p,
#include "paddings.h"
#undef PADDING
_MAX_PADDING,
};
static const int need_padding[] = {
#define MODE(e, m, p, i, c) [m] = p,
#include "modes.h"
#undef MODE
};
static const int need_iv[] = {
#define MODE(e, m, p, i, c) [m] = i,
#include "modes.h"
#undef MODE
};
static const int need_ctr[] = {
#define MODE(e, m, p, i, c) [m] = c,
#include "modes.h"
#undef MODE
};
......@@ -20,12 +20,13 @@ Privacy Guard cryptographic suite (GnuPG or GPG).
$Object symmetric(ENUM {AES, AES128, RIJNDAEL, RIJNDAEL128, AES192, RIJNDAEL192,
AES256, RIJNDAEL256} cipher, ENUM {ECB, CFB, CBC, OFB, CTR}
mode, BLOB key, BLOB iv=0, BLOB ctr=0, BOOL secure=1,
BOOL enable_sync=0, BOOL cbc_cts=0, BOOL cbc_mac=0)
mode, ENUM {PKCS7, ISO7816, X923, NONE} padding="PKCS7",
BLOB key, BOOL secure=1, BOOL enable_sync=0, BOOL cbc_cts=0,
BOOL cbc_mac=0)
$Method BLOB .encrypt(BLOB)
$Method BLOB .encrypt(BLOB plaintext, BLOB iv=0, BLOB ctr=0)
$Method BLOB .decrypt(BLOB)
$Method BLOB .decrypt(BLOB ciphertext, BLOB iv=0, BLOB ctr=0)
$Function STRING version()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment