Better support for weird IV schemes (like ESP half-IV, or the swap
authorangelos <angelos@openbsd.org>
Wed, 29 Mar 2000 07:09:40 +0000 (07:09 +0000)
committerangelos <angelos@openbsd.org>
Wed, 29 Mar 2000 07:09:40 +0000 (07:09 +0000)
encryption block-number IV).

sys/crypto/crypto.h
sys/crypto/cryptosoft.c

index ef11a18..f4515fb 100644 (file)
@@ -66,6 +66,7 @@ struct cryptoini
     int                cri_klen;    /* Key length, in bits */
     int                cri_rnd;     /* Algorithm rounds, where relevant */
     caddr_t            cri_key;     /* key to use */
+    u_int8_t           cri_iv[EALG_MAX_BLOCK_LEN];      /* IV to use */
     struct cryptoini  *cri_next;
 };
 
@@ -78,10 +79,12 @@ struct cryptodesc
     int                crd_flags;
 
 #define CRD_F_ENCRYPT             0x1 /* Set when doing encryption */
-#define CRD_F_HALFIV              0x2
-#define CRD_F_IV_PRESENT          0x4 /* Used/sensible only when encrypting */
+#define CRD_F_IV_PRESENT          0x2 /* When encrypting, IV is already in
+                                        place, so don't copy. */
+#define CRD_F_IV_EXPLICIT         0x4 /* IV explicitly provided */
 
     struct cryptoini   CRD_INI;    /* Initialization/context data */
+#define crd_iv   CRD_INI.cri_iv
 #define crd_key  CRD_INI.cri_key
 #define crd_rnd  CRD_INI.cri_rnd
 #define crd_alg  CRD_INI.cri_alg
index ec29117..5091ea6 100644 (file)
@@ -85,25 +85,12 @@ swcr_encdec(struct cryptodesc *crd, struct swcr_data *sw, caddr_t buf,
     {
        if (crd->crd_flags & CRD_F_ENCRYPT)
        {
-           /* Inject IV */
-           if (crd->crd_flags & CRD_F_HALFIV)
-           {
-               if (crd->crd_flags & CRD_F_IV_PRESENT)
-                 bcopy(buf + crd->crd_inject, sw->sw_iv, blks / 2);
-
-               /* "Cook" half-IV */
-               for (k = 0; k < blks / 2; k++)
-                 sw->sw_iv[(blks / 2) + k] = ~sw->sw_iv[k];
+           /* IV explicitly provided ? */
+           if (crd->crd_flags & CRD_F_IV_EXPLICIT)
+             bcopy(crd->crd_iv, sw->sw_iv, blks);
 
-               bcopy(sw->sw_iv, buf + crd->crd_inject, blks / 2);
-           }
-           else
-           {
-               if (crd->crd_flags & CRD_F_IV_PRESENT)
-                 bcopy(buf + crd->crd_inject, sw->sw_iv, blks);
-               else
-                 bcopy(sw->sw_iv, buf + crd->crd_inject, blks);
-           }
+           if (!(crd->crd_flags & CRD_F_IV_PRESENT))
+             bcopy(sw->sw_iv, buf + crd->crd_inject, blks);
 
            for (i = crd->crd_skip;
                 i < crd->crd_skip + crd->crd_len;
@@ -125,13 +112,11 @@ swcr_encdec(struct cryptodesc *crd, struct swcr_data *sw, caddr_t buf,
        }
        else /* Decrypt */
        {
-           /* Copy the IV off the buffer */
-           bcopy(buf + crd->crd_inject, sw->sw_iv, blks);
-
-           /* "Cook" half-IV */
-           if (crd->crd_flags & CRD_F_HALFIV)
-             for (k = 0; k < blks / 2; k++)
-               sw->sw_iv[(blks / 2) + k] = ~sw->sw_iv[k];
+           /* IV explicitly provided ? */
+           if (crd->crd_flags & CRD_F_IV_EXPLICIT)
+             bcopy(crd->crd_iv, sw->sw_iv, blks);
+           else /* IV preceeds data */
+             bcopy(buf + crd->crd_inject, sw->sw_iv, blks);
 
            /*
             * Start at the end, so we don't need to keep the encrypted
@@ -162,32 +147,23 @@ swcr_encdec(struct cryptodesc *crd, struct swcr_data *sw, caddr_t buf,
        /* Initialize the IV */
        if (crd->crd_flags & CRD_F_ENCRYPT)
        {
-           if (crd->crd_flags & CRD_F_IV_PRESENT)
-             m_copydata(m, crd->crd_inject, blks, iv);
+           /* IV explicitly provided ? */
+           if (crd->crd_flags & CRD_F_IV_EXPLICIT)
+             bcopy(crd->crd_iv, iv, blks);
            else
-             bcopy(sw->sw_iv, iv, blks);
+             bcopy(sw->sw_iv, iv, blks); /* Use IV from context */
 
-           /* "Cook" half-IV */
-           if (crd->crd_flags & CRD_F_HALFIV)
-           {
-               for (k = 0; k < blks / 2; k++)
-                 iv[(blks / 2) + k] = ~iv[k];
-
-               if (!(crd->crd_flags & CRD_F_IV_PRESENT))
-                 m_copyback(m, crd->crd_inject, blks / 2, iv);
-           }
-           else
-             if (!(crd->crd_flags & CRD_F_IV_PRESENT))
-               m_copyback(m, crd->crd_inject, blks, iv);
+           /* Do we need to write the IV */
+           if (!(crd->crd_flags & CRD_F_IV_PRESENT))
+             m_copyback(m, crd->crd_inject, blks, iv);
        }
-       else
+       else /* Decryption */
        {
-           m_copydata(m, crd->crd_inject, blks, iv); /* Get IV off mbuf */
-
-           /* "Cook" half-IV */
-           if (crd->crd_flags & CRD_F_HALFIV)
-             for (k = 0; k < blks / 2; k++)
-               iv[(blks / 2) + k] = ~iv[k];
+           /* IV explicitly provided ? */
+           if (crd->crd_flags & CRD_F_IV_EXPLICIT)
+             bcopy(crd->crd_iv, iv, blks);
+           else
+             m_copydata(m, crd->crd_inject, blks, iv); /* Get IV off mbuf */
        }
 
        ivp = iv;