Imported Upstream version 6.2p2
[platform/upstream/openssh.git] / mac.c
diff --git a/mac.c b/mac.c
index 332d3c6..3f2dc6f 100644 (file)
--- a/mac.c
+++ b/mac.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mac.c,v 1.17 2011/12/02 00:43:57 djm Exp $ */
+/* $OpenBSD: mac.c,v 1.21 2012/12/11 22:51:45 sthen Exp $ */
 /*
  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
  *
@@ -48,6 +48,7 @@
 
 #define SSH_EVP                1       /* OpenSSL EVP-based MAC */
 #define SSH_UMAC       2       /* UMAC (not integrated with OpenSSL) */
+#define SSH_UMAC128    3
 
 struct {
        char            *name;
@@ -56,21 +57,36 @@ struct {
        int             truncatebits;   /* truncate digest if != 0 */
        int             key_len;        /* just for UMAC */
        int             len;            /* just for UMAC */
+       int             etm;            /* Encrypt-then-MAC */
 } macs[] = {
-       { "hmac-sha1",                  SSH_EVP, EVP_sha1, 0, -1, -1 },
-       { "hmac-sha1-96",               SSH_EVP, EVP_sha1, 96, -1, -1 },
+       /* Encrypt-and-MAC (encrypt-and-authenticate) variants */
+       { "hmac-sha1",                          SSH_EVP, EVP_sha1, 0, 0, 0, 0 },
+       { "hmac-sha1-96",                       SSH_EVP, EVP_sha1, 96, 0, 0, 0 },
 #ifdef HAVE_EVP_SHA256
-       { "hmac-sha2-256",              SSH_EVP, EVP_sha256, 0, -1, -1 },
-       { "hmac-sha2-256-96",           SSH_EVP, EVP_sha256, 96, -1, -1 },
-       { "hmac-sha2-512",              SSH_EVP, EVP_sha512, 0, -1, -1 },
-       { "hmac-sha2-512-96",           SSH_EVP, EVP_sha512, 96, -1, -1 },
+       { "hmac-sha2-256",                      SSH_EVP, EVP_sha256, 0, 0, 0, 0 },
+       { "hmac-sha2-512",                      SSH_EVP, EVP_sha512, 0, 0, 0, 0 },
 #endif
-       { "hmac-md5",                   SSH_EVP, EVP_md5, 0, -1, -1 },
-       { "hmac-md5-96",                SSH_EVP, EVP_md5, 96, -1, -1 },
-       { "hmac-ripemd160",             SSH_EVP, EVP_ripemd160, 0, -1, -1 },
-       { "hmac-ripemd160@openssh.com", SSH_EVP, EVP_ripemd160, 0, -1, -1 },
-       { "umac-64@openssh.com",        SSH_UMAC, NULL, 0, 128, 64 },
-       { NULL,                         0, NULL, 0, -1, -1 }
+       { "hmac-md5",                           SSH_EVP, EVP_md5, 0, 0, 0, 0 },
+       { "hmac-md5-96",                        SSH_EVP, EVP_md5, 96, 0, 0, 0 },
+       { "hmac-ripemd160",                     SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
+       { "hmac-ripemd160@openssh.com",         SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
+       { "umac-64@openssh.com",                SSH_UMAC, NULL, 0, 128, 64, 0 },
+       { "umac-128@openssh.com",               SSH_UMAC128, NULL, 0, 128, 128, 0 },
+
+       /* Encrypt-then-MAC variants */
+       { "hmac-sha1-etm@openssh.com",          SSH_EVP, EVP_sha1, 0, 0, 0, 1 },
+       { "hmac-sha1-96-etm@openssh.com",       SSH_EVP, EVP_sha1, 96, 0, 0, 1 },
+#ifdef HAVE_EVP_SHA256
+       { "hmac-sha2-256-etm@openssh.com",      SSH_EVP, EVP_sha256, 0, 0, 0, 1 },
+       { "hmac-sha2-512-etm@openssh.com",      SSH_EVP, EVP_sha512, 0, 0, 0, 1 },
+#endif
+       { "hmac-md5-etm@openssh.com",           SSH_EVP, EVP_md5, 0, 0, 0, 1 },
+       { "hmac-md5-96-etm@openssh.com",        SSH_EVP, EVP_md5, 96, 0, 0, 1 },
+       { "hmac-ripemd160-etm@openssh.com",     SSH_EVP, EVP_ripemd160, 0, 0, 0, 1 },
+       { "umac-64-etm@openssh.com",            SSH_UMAC, NULL, 0, 128, 64, 1 },
+       { "umac-128-etm@openssh.com",           SSH_UMAC128, NULL, 0, 128, 128, 1 },
+
+       { NULL,                                 0, NULL, 0, 0, 0, 0 }
 };
 
 static void
@@ -90,6 +106,7 @@ mac_setup_by_id(Mac *mac, int which)
        }
        if (macs[which].truncatebits != 0)
                mac->mac_len = macs[which].truncatebits / 8;
+       mac->etm = macs[which].etm;
 }
 
 int
@@ -124,6 +141,9 @@ mac_init(Mac *mac)
        case SSH_UMAC:
                mac->umac_ctx = umac_new(mac->key);
                return 0;
+       case SSH_UMAC128:
+               mac->umac_ctx = umac128_new(mac->key);
+               return 0;
        default:
                return -1;
        }
@@ -153,6 +173,11 @@ mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
                umac_update(mac->umac_ctx, data, datalen);
                umac_final(mac->umac_ctx, m, nonce);
                break;
+       case SSH_UMAC128:
+               put_u64(nonce, seqno);
+               umac128_update(mac->umac_ctx, data, datalen);
+               umac128_final(mac->umac_ctx, m, nonce);
+               break;
        default:
                fatal("mac_compute: unknown MAC type");
        }
@@ -165,6 +190,9 @@ mac_clear(Mac *mac)
        if (mac->type == SSH_UMAC) {
                if (mac->umac_ctx != NULL)
                        umac_delete(mac->umac_ctx);
+       } else if (mac->type == SSH_UMAC128) {
+               if (mac->umac_ctx != NULL)
+                       umac128_delete(mac->umac_ctx);
        } else if (mac->evp_md != NULL)
                HMAC_cleanup(&mac->evp_ctx);
        mac->evp_md = NULL;