Ejemplo n.º 1
0
// The input encrypted as though 128bit counter mode is being used.  The extra
// state information to record how much of the 128bit block we have used is
// contained in *num, and the encrypted counter is kept in ecount_buf.  Both
// *num and ecount_buf must be initialised with zeros before the first call to
// CRYPTO_ctr128_encrypt().
//
// This algorithm assumes that the counter is in the x lower bits of the IV
// (ivec), and that the application has full control over overflow and the rest
// of the IV.  This implementation takes NO responsibility for checking that
// the counter doesn't overflow into the rest of the IV when incremented.
void CRYPTO_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
                           const void *key, uint8_t ivec[16],
                           uint8_t ecount_buf[16], unsigned int *num,
                           block128_f block) {
  unsigned int n;

  assert(key && ecount_buf && num);
  assert(len == 0 || (in && out));
  assert(*num < 16);

  n = *num;

  while (n && len) {
    *(out++) = *(in++) ^ ecount_buf[n];
    --len;
    n = (n + 1) % 16;
  }

#if STRICT_ALIGNMENT
  if (((uintptr_t)in | (uintptr_t)out |
        (uintptr_t)ecount_buf) % sizeof(size_t) != 0) {
    size_t l = 0;
    while (l < len) {
      if (n == 0) {
        (*block)(ivec, ecount_buf, key);
        ctr128_inc(ivec);
      }
      out[l] = in[l] ^ ecount_buf[n];
      ++l;
      n = (n + 1) % 16;
    }

    *num = n;
    return;
  }
#endif

  while (len >= 16) {
    (*block)(ivec, ecount_buf, key);
    ctr128_inc(ivec);
    for (n = 0; n < 16; n += sizeof(size_t)) {
      store_word_le(out + n,
                    load_word_le(in + n) ^ load_word_le(ecount_buf + n));
    }
    len -= 16;
    out += 16;
    in += 16;
    n = 0;
  }
  if (len) {
    (*block)(ivec, ecount_buf, key);
    ctr128_inc(ivec);
    while (len--) {
      out[n] = in[n] ^ ecount_buf[n];
      ++n;
    }
  }
  *num = n;
}
Ejemplo n.º 2
0
void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
                           const AES_KEY *key, uint8_t ivec[16],
                           block128_f block) {
  size_t n;
  const uint8_t *iv = ivec;

  assert(key != NULL && ivec != NULL);
  assert(len == 0 || (in != NULL && out != NULL));

  if (STRICT_ALIGNMENT &&
      ((uintptr_t)in | (uintptr_t)out | (uintptr_t)ivec) % sizeof(size_t) !=
          0) {
    while (len >= 16) {
      for (n = 0; n < 16; ++n) {
        out[n] = in[n] ^ iv[n];
      }
      (*block)(out, out, key);
      iv = out;
      len -= 16;
      in += 16;
      out += 16;
    }
  } else {
    while (len >= 16) {
      for (n = 0; n < 16; n += sizeof(size_t)) {
        store_word_le(out + n, load_word_le(in + n) ^ load_word_le(iv + n));
      }
      (*block)(out, out, key);
      iv = out;
      len -= 16;
      in += 16;
      out += 16;
    }
  }

  while (len) {
    for (n = 0; n < 16 && n < len; ++n) {
      out[n] = in[n] ^ iv[n];
    }
    for (; n < 16; ++n) {
      out[n] = iv[n];
    }
    (*block)(out, out, key);
    iv = out;
    if (len <= 16) {
      break;
    }
    len -= 16;
    in += 16;
    out += 16;
  }

  OPENSSL_memcpy(ivec, iv, 16);
}
Ejemplo n.º 3
0
void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
                           const AES_KEY *key, uint8_t ivec[16],
                           block128_f block) {
  size_t n;
  union {
    size_t t[16 / sizeof(size_t)];
    uint8_t c[16];
  } tmp;

  assert(key != NULL && ivec != NULL);
  assert(len == 0 || (in != NULL && out != NULL));

  const uintptr_t inptr = (uintptr_t) in;
  const uintptr_t outptr = (uintptr_t) out;
  // If |in| and |out| alias, |in| must be ahead.
  assert(inptr >= outptr || inptr + len <= outptr);

  if ((inptr >= 32 && outptr <= inptr - 32) || inptr < outptr) {
    // If |out| is at least two blocks behind |in| or completely disjoint, there
    // is no need to decrypt to a temporary block.
    const uint8_t *iv = ivec;

    if (STRICT_ALIGNMENT &&
        ((uintptr_t)in | (uintptr_t)out | (uintptr_t)ivec) % sizeof(size_t) !=
            0) {
      while (len >= 16) {
        (*block)(in, out, key);
        for (n = 0; n < 16; ++n) {
          out[n] ^= iv[n];
        }
        iv = in;
        len -= 16;
        in += 16;
        out += 16;
      }
    } else if (16 % sizeof(size_t) == 0) {  // always true
      while (len >= 16) {
        (*block)(in, out, key);
        for (n = 0; n < 16; n += sizeof(size_t)) {
          store_word_le(out + n, load_word_le(out + n) ^ load_word_le(iv + n));
        }
        iv = in;
        len -= 16;
        in += 16;
        out += 16;
      }
    }
    OPENSSL_memcpy(ivec, iv, 16);
  } else {
    // |out| is less than two blocks behind |in|. Decrypting an input block
    // directly to |out| would overwrite a ciphertext block before it is used as
    // the next block's IV. Decrypt to a temporary block instead.
    if (STRICT_ALIGNMENT &&
        ((uintptr_t)in | (uintptr_t)out | (uintptr_t)ivec) % sizeof(size_t) !=
            0) {
      uint8_t c;
      while (len >= 16) {
        (*block)(in, tmp.c, key);
        for (n = 0; n < 16; ++n) {
          c = in[n];
          out[n] = tmp.c[n] ^ ivec[n];
          ivec[n] = c;
        }
        len -= 16;
        in += 16;
        out += 16;
      }
    } else if (16 % sizeof(size_t) == 0) {  // always true
      while (len >= 16) {
        (*block)(in, tmp.c, key);
        for (n = 0; n < 16; n += sizeof(size_t)) {
          size_t c = load_word_le(in + n);
          store_word_le(out + n,
                        tmp.t[n / sizeof(size_t)] ^ load_word_le(ivec + n));
          store_word_le(ivec + n, c);
        }
        len -= 16;
        in += 16;
        out += 16;
      }
    }
  }

  while (len) {
    uint8_t c;
    (*block)(in, tmp.c, key);
    for (n = 0; n < 16 && n < len; ++n) {
      c = in[n];
      out[n] = tmp.c[n] ^ ivec[n];
      ivec[n] = c;
    }
    if (len <= 16) {
      for (; n < 16; ++n) {
        ivec[n] = in[n];
      }
      break;
    }
    len -= 16;
    in += 16;
    out += 16;
  }
}