8335b1
diff --git a/modules/session/mod_session_crypto.c b/modules/session/mod_session_crypto.c
8335b1
index 4d65bb8..9231a5e 100644
8335b1
--- a/modules/session/mod_session_crypto.c
8335b1
+++ b/modules/session/mod_session_crypto.c
8335b1
@@ -18,6 +18,7 @@
8335b1
 #include "apu_version.h"
8335b1
 #include "apr_base64.h"                /* for apr_base64_decode et al */
8335b1
 #include "apr_lib.h"
8335b1
+#include "apr_md5.h"
8335b1
 #include "apr_strings.h"
8335b1
 #include "http_log.h"
8335b1
 #include "http_core.h"
8335b1
@@ -57,6 +58,146 @@ typedef struct {
8335b1
     int library_set;
8335b1
 } session_crypto_conf;
8335b1
 
8335b1
+/* Wrappers around apr_siphash24() and apr_crypto_equals(),
8335b1
+ * available in APU-1.6/APR-2.0 only.
8335b1
+ */
8335b1
+#if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 6)
8335b1
+
8335b1
+#include "apr_siphash.h"
8335b1
+
8335b1
+#define AP_SIPHASH_DSIZE    APR_SIPHASH_DSIZE
8335b1
+#define AP_SIPHASH_KSIZE    APR_SIPHASH_KSIZE
8335b1
+#define ap_siphash24_auth   apr_siphash24_auth
8335b1
+
8335b1
+#define ap_crypto_equals    apr_crypto_equals
8335b1
+
8335b1
+#else
8335b1
+
8335b1
+#define AP_SIPHASH_DSIZE    8
8335b1
+#define AP_SIPHASH_KSIZE    16
8335b1
+
8335b1
+#define ROTL64(x, n) (((x) << (n)) | ((x) >> (64 - (n))))
8335b1
+
8335b1
+#define U8TO64_LE(p) \
8335b1
+    (((apr_uint64_t)((p)[0])      ) | \
8335b1
+     ((apr_uint64_t)((p)[1]) <<  8) | \
8335b1
+     ((apr_uint64_t)((p)[2]) << 16) | \
8335b1
+     ((apr_uint64_t)((p)[3]) << 24) | \
8335b1
+     ((apr_uint64_t)((p)[4]) << 32) | \
8335b1
+     ((apr_uint64_t)((p)[5]) << 40) | \
8335b1
+     ((apr_uint64_t)((p)[6]) << 48) | \
8335b1
+     ((apr_uint64_t)((p)[7]) << 56))
8335b1
+
8335b1
+#define U64TO8_LE(p, v) \
8335b1
+do { \
8335b1
+    (p)[0] = (unsigned char)((v)      ); \
8335b1
+    (p)[1] = (unsigned char)((v) >>  8); \
8335b1
+    (p)[2] = (unsigned char)((v) >> 16); \
8335b1
+    (p)[3] = (unsigned char)((v) >> 24); \
8335b1
+    (p)[4] = (unsigned char)((v) >> 32); \
8335b1
+    (p)[5] = (unsigned char)((v) >> 40); \
8335b1
+    (p)[6] = (unsigned char)((v) >> 48); \
8335b1
+    (p)[7] = (unsigned char)((v) >> 56); \
8335b1
+} while (0)
8335b1
+
8335b1
+#define SIPROUND() \
8335b1
+do { \
8335b1
+    v0 += v1; v1=ROTL64(v1,13); v1 ^= v0; v0=ROTL64(v0,32); \
8335b1
+    v2 += v3; v3=ROTL64(v3,16); v3 ^= v2; \
8335b1
+    v0 += v3; v3=ROTL64(v3,21); v3 ^= v0; \
8335b1
+    v2 += v1; v1=ROTL64(v1,17); v1 ^= v2; v2=ROTL64(v2,32); \
8335b1
+} while(0)
8335b1
+
8335b1
+static apr_uint64_t ap_siphash24(const void *src, apr_size_t len,
8335b1
+                                 const unsigned char key[AP_SIPHASH_KSIZE])
8335b1
+{
8335b1
+    const unsigned char *ptr, *end;
8335b1
+    apr_uint64_t v0, v1, v2, v3, m;
8335b1
+    apr_uint64_t k0, k1;
8335b1
+    unsigned int rem;
8335b1
+
8335b1
+    k0 = U8TO64_LE(key + 0);
8335b1
+    k1 = U8TO64_LE(key + 8);
8335b1
+    v3 = k1 ^ (apr_uint64_t)0x7465646279746573ULL;
8335b1
+    v2 = k0 ^ (apr_uint64_t)0x6c7967656e657261ULL;
8335b1
+    v1 = k1 ^ (apr_uint64_t)0x646f72616e646f6dULL;
8335b1
+    v0 = k0 ^ (apr_uint64_t)0x736f6d6570736575ULL;
8335b1
+
8335b1
+    rem = (unsigned int)(len & 0x7);
8335b1
+    for (ptr = src, end = ptr + len - rem; ptr < end; ptr += 8) {
8335b1
+        m = U8TO64_LE(ptr);
8335b1
+        v3 ^= m;
8335b1
+        SIPROUND();
8335b1
+        SIPROUND();
8335b1
+        v0 ^= m;
8335b1
+    }
8335b1
+    m = (apr_uint64_t)(len & 0xff) << 56;
8335b1
+    switch (rem) {
8335b1
+        case 7: m |= (apr_uint64_t)ptr[6] << 48;
8335b1
+        case 6: m |= (apr_uint64_t)ptr[5] << 40;
8335b1
+        case 5: m |= (apr_uint64_t)ptr[4] << 32;
8335b1
+        case 4: m |= (apr_uint64_t)ptr[3] << 24;
8335b1
+        case 3: m |= (apr_uint64_t)ptr[2] << 16;
8335b1
+        case 2: m |= (apr_uint64_t)ptr[1] << 8;
8335b1
+        case 1: m |= (apr_uint64_t)ptr[0];
8335b1
+        case 0: break;
8335b1
+    }
8335b1
+    v3 ^= m;
8335b1
+    SIPROUND();
8335b1
+    SIPROUND();
8335b1
+    v0 ^= m;
8335b1
+
8335b1
+    v2 ^= 0xff;
8335b1
+    SIPROUND();
8335b1
+    SIPROUND();
8335b1
+    SIPROUND();
8335b1
+    SIPROUND();
8335b1
+
8335b1
+    return v0 ^ v1 ^ v2 ^ v3;
8335b1
+}
8335b1
+
8335b1
+static void ap_siphash24_auth(unsigned char out[AP_SIPHASH_DSIZE],
8335b1
+                              const void *src, apr_size_t len,
8335b1
+                              const unsigned char key[AP_SIPHASH_KSIZE])
8335b1
+{
8335b1
+    apr_uint64_t h;
8335b1
+    h = ap_siphash24(src, len, key);
8335b1
+    U64TO8_LE(out, h);
8335b1
+}
8335b1
+
8335b1
+static int ap_crypto_equals(const void *buf1, const void *buf2,
8335b1
+                            apr_size_t size)
8335b1
+{
8335b1
+    const unsigned char *p1 = buf1;
8335b1
+    const unsigned char *p2 = buf2;
8335b1
+    unsigned char diff = 0;
8335b1
+    apr_size_t i;
8335b1
+
8335b1
+    for (i = 0; i < size; ++i) {
8335b1
+        diff |= p1[i] ^ p2[i];
8335b1
+    }
8335b1
+
8335b1
+    return 1 & ((diff - 1) >> 8);
8335b1
+}
8335b1
+
8335b1
+#endif
8335b1
+
8335b1
+static void compute_auth(const void *src, apr_size_t len,
8335b1
+                         const char *passphrase, apr_size_t passlen,
8335b1
+                         unsigned char auth[AP_SIPHASH_DSIZE])
8335b1
+{
8335b1
+    unsigned char key[APR_MD5_DIGESTSIZE];
8335b1
+
8335b1
+    /* XXX: if we had a way to get the raw bytes from an apr_crypto_key_t
8335b1
+     *      we could use them directly (not available in APR-1.5.x).
8335b1
+     * MD5 is 128bit too, so use it to get a suitable siphash key
8335b1
+     * from the passphrase.
8335b1
+     */
8335b1
+    apr_md5(key, passphrase, passlen);
8335b1
+
8335b1
+    ap_siphash24_auth(auth, src, len, key);
8335b1
+}
8335b1
+
8335b1
 /**
8335b1
  * Initialise the encryption as per the current config.
8335b1
  *
8335b1
@@ -128,21 +269,14 @@ static apr_status_t encrypt_string(request_rec * r, const apr_crypto_t *f,
8335b1
     apr_crypto_block_t *block = NULL;
8335b1
     unsigned char *encrypt = NULL;
8335b1
     unsigned char *combined = NULL;
8335b1
-    apr_size_t encryptlen, tlen;
8335b1
+    apr_size_t encryptlen, tlen, combinedlen;
8335b1
     char *base64;
8335b1
     apr_size_t blockSize = 0;
8335b1
     const unsigned char *iv = NULL;
8335b1
     apr_uuid_t salt;
8335b1
     apr_crypto_block_key_type_e *cipher;
8335b1
     const char *passphrase;
8335b1
-
8335b1
-    /* by default, return an empty string */
8335b1
-    *out = "";
8335b1
-
8335b1
-    /* don't attempt to encrypt an empty string, trying to do so causes a segfault */
8335b1
-    if (!in || !*in) {
8335b1
-        return APR_SUCCESS;
8335b1
-    }
8335b1
+    apr_size_t passlen;
8335b1
 
8335b1
     /* use a uuid as a salt value, and prepend it to our result */
8335b1
     apr_uuid_get(&salt);
8335b1
@@ -152,9 +286,9 @@ static apr_status_t encrypt_string(request_rec * r, const apr_crypto_t *f,
8335b1
     }
8335b1
 
8335b1
     /* encrypt using the first passphrase in the list */
8335b1
-    passphrase = APR_ARRAY_IDX(dconf->passphrases, 0, char *);
8335b1
-    res = apr_crypto_passphrase(&key, &ivSize, passphrase,
8335b1
-            strlen(passphrase),
8335b1
+    passphrase = APR_ARRAY_IDX(dconf->passphrases, 0, const char *);
8335b1
+    passlen = strlen(passphrase);
8335b1
+    res = apr_crypto_passphrase(&key, &ivSize, passphrase, passlen,
8335b1
             (unsigned char *) (&salt), sizeof(apr_uuid_t),
8335b1
             *cipher, APR_MODE_CBC, 1, 4096, f, r->pool);
8335b1
     if (APR_STATUS_IS_ENOKEY(res)) {
8335b1
@@ -183,8 +317,9 @@ static apr_status_t encrypt_string(request_rec * r, const apr_crypto_t *f,
8335b1
     }
8335b1
 
8335b1
     /* encrypt the given string */
8335b1
-    res = apr_crypto_block_encrypt(&encrypt, &encryptlen, (unsigned char *)in,
8335b1
-            strlen(in), block);
8335b1
+    res = apr_crypto_block_encrypt(&encrypt, &encryptlen,
8335b1
+                                   (const unsigned char *)in, strlen(in),
8335b1
+                                   block);
8335b1
     if (APR_SUCCESS != res) {
8335b1
         ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, APLOGNO(01830)
8335b1
                 "apr_crypto_block_encrypt failed");
8335b1
@@ -198,18 +333,20 @@ static apr_status_t encrypt_string(request_rec * r, const apr_crypto_t *f,
8335b1
     }
8335b1
     encryptlen += tlen;
8335b1
 
8335b1
-    /* prepend the salt and the iv to the result */
8335b1
-    combined = apr_palloc(r->pool, ivSize + encryptlen + sizeof(apr_uuid_t));
8335b1
-    memcpy(combined, &salt, sizeof(apr_uuid_t));
8335b1
-    memcpy(combined + sizeof(apr_uuid_t), iv, ivSize);
8335b1
-    memcpy(combined + sizeof(apr_uuid_t) + ivSize, encrypt, encryptlen);
8335b1
-
8335b1
-    /* base64 encode the result */
8335b1
-    base64 = apr_palloc(r->pool, apr_base64_encode_len(ivSize + encryptlen +
8335b1
-                    sizeof(apr_uuid_t) + 1)
8335b1
-            * sizeof(char));
8335b1
-    apr_base64_encode(base64, (const char *) combined,
8335b1
-            ivSize + encryptlen + sizeof(apr_uuid_t));
8335b1
+    /* prepend the salt and the iv to the result (keep room for the MAC) */
8335b1
+    combinedlen = AP_SIPHASH_DSIZE + sizeof(apr_uuid_t) + ivSize + encryptlen;
8335b1
+    combined = apr_palloc(r->pool, combinedlen);
8335b1
+    memcpy(combined + AP_SIPHASH_DSIZE, &salt, sizeof(apr_uuid_t));
8335b1
+    memcpy(combined + AP_SIPHASH_DSIZE + sizeof(apr_uuid_t), iv, ivSize);
8335b1
+    memcpy(combined + AP_SIPHASH_DSIZE + sizeof(apr_uuid_t) + ivSize,
8335b1
+           encrypt, encryptlen);
8335b1
+    /* authenticate the whole salt+IV+ciphertext with a leading MAC */
8335b1
+    compute_auth(combined + AP_SIPHASH_DSIZE, combinedlen - AP_SIPHASH_DSIZE,
8335b1
+                 passphrase, passlen, combined);
8335b1
+
8335b1
+    /* base64 encode the result (APR handles the trailing '\0') */
8335b1
+    base64 = apr_palloc(r->pool, apr_base64_encode_len(combinedlen));
8335b1
+    apr_base64_encode(base64, (const char *) combined, combinedlen);
8335b1
     *out = base64;
8335b1
 
8335b1
     return res;
8335b1
@@ -234,6 +371,7 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
8335b1
     char *decoded;
8335b1
     apr_size_t blockSize = 0;
8335b1
     apr_crypto_block_key_type_e *cipher;
8335b1
+    unsigned char auth[AP_SIPHASH_DSIZE];
8335b1
     int i = 0;
8335b1
 
8335b1
     /* strip base64 from the string */
8335b1
@@ -241,6 +379,13 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
8335b1
     decodedlen = apr_base64_decode(decoded, in);
8335b1
     decoded[decodedlen] = '\0';
8335b1
 
8335b1
+    /* sanity check - decoded too short? */
8335b1
+    if (decodedlen < (AP_SIPHASH_DSIZE + sizeof(apr_uuid_t))) {
8335b1
+        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO()
8335b1
+                "too short to decrypt, aborting");
8335b1
+        return APR_ECRYPT;
8335b1
+    }
8335b1
+
8335b1
     res = crypt_init(r, f, &cipher, dconf);
8335b1
     if (res != APR_SUCCESS) {
8335b1
         return res;
8335b1
@@ -249,14 +394,25 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
8335b1
     /* try each passphrase in turn */
8335b1
     for (; i < dconf->passphrases->nelts; i++) {
8335b1
         const char *passphrase = APR_ARRAY_IDX(dconf->passphrases, i, char *);
8335b1
-        apr_size_t len = decodedlen;
8335b1
-        char *slider = decoded;
8335b1
+        apr_size_t passlen = strlen(passphrase);
8335b1
+        apr_size_t len = decodedlen - AP_SIPHASH_DSIZE;
8335b1
+        unsigned char *slider = (unsigned char *)decoded + AP_SIPHASH_DSIZE;
8335b1
+
8335b1
+        /* Verify authentication of the whole salt+IV+ciphertext by computing
8335b1
+         * the MAC and comparing it (timing safe) with the one in the payload.
8335b1
+         */
8335b1
+        compute_auth(slider, len, passphrase, passlen, auth);
8335b1
+        if (!ap_crypto_equals(auth, decoded, AP_SIPHASH_DSIZE)) {
8335b1
+            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, res, r, APLOGNO()
8335b1
+                    "auth does not match, skipping");
8335b1
+            continue;
8335b1
+        }
8335b1
 
8335b1
         /* encrypt using the first passphrase in the list */
8335b1
-        res = apr_crypto_passphrase(&key, &ivSize, passphrase,
8335b1
-                strlen(passphrase),
8335b1
-                (unsigned char *)decoded, sizeof(apr_uuid_t),
8335b1
-                *cipher, APR_MODE_CBC, 1, 4096, f, r->pool);
8335b1
+        res = apr_crypto_passphrase(&key, &ivSize, passphrase, passlen,
8335b1
+                                    slider, sizeof(apr_uuid_t),
8335b1
+                                    *cipher, APR_MODE_CBC, 1, 4096,
8335b1
+                                    f, r->pool);
8335b1
         if (APR_STATUS_IS_ENOKEY(res)) {
8335b1
             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, res, r, APLOGNO(01832)
8335b1
                     "the passphrase '%s' was empty", passphrase);
8335b1
@@ -279,7 +435,7 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
8335b1
         }
8335b1
 
8335b1
         /* sanity check - decoded too short? */
8335b1
-        if (decodedlen < (sizeof(apr_uuid_t) + ivSize)) {
8335b1
+        if (len < (sizeof(apr_uuid_t) + ivSize)) {
8335b1
             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(01836)
8335b1
                     "too short to decrypt, skipping");
8335b1
             res = APR_ECRYPT;
8335b1
@@ -290,8 +446,8 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
8335b1
         slider += sizeof(apr_uuid_t);
8335b1
         len -= sizeof(apr_uuid_t);
8335b1
 
8335b1
-        res = apr_crypto_block_decrypt_init(&block, &blockSize, (unsigned char *)slider, key,
8335b1
-                r->pool);
8335b1
+        res = apr_crypto_block_decrypt_init(&block, &blockSize, slider, key,
8335b1
+                                            r->pool);
8335b1
         if (APR_SUCCESS != res) {
8335b1
             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, res, r, APLOGNO(01837)
8335b1
                     "apr_crypto_block_decrypt_init failed");
8335b1
@@ -304,7 +460,7 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
8335b1
 
8335b1
         /* decrypt the given string */
8335b1
         res = apr_crypto_block_decrypt(&decrypted, &decryptedlen,
8335b1
-                (unsigned char *)slider, len, block);
8335b1
+                                       slider, len, block);
8335b1
         if (res) {
8335b1
             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, res, r, APLOGNO(01838)
8335b1
                     "apr_crypto_block_decrypt failed");
8335b1