11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
51cb0ef41Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at
71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html
81cb0ef41Sopenharmony_ci */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci/*
111cb0ef41Sopenharmony_ci * DES low level APIs are deprecated for public use, but still ok for internal
121cb0ef41Sopenharmony_ci * use.
131cb0ef41Sopenharmony_ci */
141cb0ef41Sopenharmony_ci#include "internal/deprecated.h"
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci#include "des_local.h"
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci/*
191cb0ef41Sopenharmony_ci * The input and output are loaded in multiples of 8 bits. What this means is
201cb0ef41Sopenharmony_ci * that if you have numbits=12 and length=2 the first 12 bits will be
211cb0ef41Sopenharmony_ci * retrieved from the first byte and half the second.  The second 12 bits
221cb0ef41Sopenharmony_ci * will come from the 3rd and half the 4th byte.
231cb0ef41Sopenharmony_ci */
241cb0ef41Sopenharmony_civoid DES_ofb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
251cb0ef41Sopenharmony_ci                     long length, DES_key_schedule *schedule,
261cb0ef41Sopenharmony_ci                     DES_cblock *ivec)
271cb0ef41Sopenharmony_ci{
281cb0ef41Sopenharmony_ci    register DES_LONG d0, d1, vv0, vv1, v0, v1, n = (numbits + 7) / 8;
291cb0ef41Sopenharmony_ci    register DES_LONG mask0, mask1;
301cb0ef41Sopenharmony_ci    register long l = length;
311cb0ef41Sopenharmony_ci    register int num = numbits;
321cb0ef41Sopenharmony_ci    DES_LONG ti[2];
331cb0ef41Sopenharmony_ci    unsigned char *iv;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    if (num > 64)
361cb0ef41Sopenharmony_ci        return;
371cb0ef41Sopenharmony_ci    if (num > 32) {
381cb0ef41Sopenharmony_ci        mask0 = 0xffffffffL;
391cb0ef41Sopenharmony_ci        if (num >= 64)
401cb0ef41Sopenharmony_ci            mask1 = mask0;
411cb0ef41Sopenharmony_ci        else
421cb0ef41Sopenharmony_ci            mask1 = (1L << (num - 32)) - 1;
431cb0ef41Sopenharmony_ci    } else {
441cb0ef41Sopenharmony_ci        if (num == 32)
451cb0ef41Sopenharmony_ci            mask0 = 0xffffffffL;
461cb0ef41Sopenharmony_ci        else
471cb0ef41Sopenharmony_ci            mask0 = (1L << num) - 1;
481cb0ef41Sopenharmony_ci        mask1 = 0x00000000L;
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    iv = &(*ivec)[0];
521cb0ef41Sopenharmony_ci    c2l(iv, v0);
531cb0ef41Sopenharmony_ci    c2l(iv, v1);
541cb0ef41Sopenharmony_ci    ti[0] = v0;
551cb0ef41Sopenharmony_ci    ti[1] = v1;
561cb0ef41Sopenharmony_ci    while (l-- > 0) {
571cb0ef41Sopenharmony_ci        ti[0] = v0;
581cb0ef41Sopenharmony_ci        ti[1] = v1;
591cb0ef41Sopenharmony_ci        DES_encrypt1((DES_LONG *)ti, schedule, DES_ENCRYPT);
601cb0ef41Sopenharmony_ci        vv0 = ti[0];
611cb0ef41Sopenharmony_ci        vv1 = ti[1];
621cb0ef41Sopenharmony_ci        c2ln(in, d0, d1, n);
631cb0ef41Sopenharmony_ci        in += n;
641cb0ef41Sopenharmony_ci        d0 = (d0 ^ vv0) & mask0;
651cb0ef41Sopenharmony_ci        d1 = (d1 ^ vv1) & mask1;
661cb0ef41Sopenharmony_ci        l2cn(d0, d1, out, n);
671cb0ef41Sopenharmony_ci        out += n;
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci        if (num == 32) {
701cb0ef41Sopenharmony_ci            v0 = v1;
711cb0ef41Sopenharmony_ci            v1 = vv0;
721cb0ef41Sopenharmony_ci        } else if (num == 64) {
731cb0ef41Sopenharmony_ci            v0 = vv0;
741cb0ef41Sopenharmony_ci            v1 = vv1;
751cb0ef41Sopenharmony_ci        } else if (num > 32) {  /* && num != 64 */
761cb0ef41Sopenharmony_ci            v0 = ((v1 >> (num - 32)) | (vv0 << (64 - num))) & 0xffffffffL;
771cb0ef41Sopenharmony_ci            v1 = ((vv0 >> (num - 32)) | (vv1 << (64 - num))) & 0xffffffffL;
781cb0ef41Sopenharmony_ci        } else {                /* num < 32 */
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci            v0 = ((v0 >> num) | (v1 << (32 - num))) & 0xffffffffL;
811cb0ef41Sopenharmony_ci            v1 = ((v1 >> num) | (vv0 << (32 - num))) & 0xffffffffL;
821cb0ef41Sopenharmony_ci        }
831cb0ef41Sopenharmony_ci    }
841cb0ef41Sopenharmony_ci    iv = &(*ivec)[0];
851cb0ef41Sopenharmony_ci    l2c(v0, iv);
861cb0ef41Sopenharmony_ci    l2c(v1, iv);
871cb0ef41Sopenharmony_ci    v0 = v1 = d0 = d1 = ti[0] = ti[1] = vv0 = vv1 = 0;
881cb0ef41Sopenharmony_ci}
89