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 encrypted as though 64bit cfb mode is being used.
201cb0ef41Sopenharmony_ci * The extra state information to record how much of the 64bit block we have
211cb0ef41Sopenharmony_ci * used is contained in *num;
221cb0ef41Sopenharmony_ci */
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_civoid DES_cfb64_encrypt(const unsigned char *in, unsigned char *out,
251cb0ef41Sopenharmony_ci                       long length, DES_key_schedule *schedule,
261cb0ef41Sopenharmony_ci                       DES_cblock *ivec, int *num, int enc)
271cb0ef41Sopenharmony_ci{
281cb0ef41Sopenharmony_ci    register DES_LONG v0, v1;
291cb0ef41Sopenharmony_ci    register long l = length;
301cb0ef41Sopenharmony_ci    register int n = *num;
311cb0ef41Sopenharmony_ci    DES_LONG ti[2];
321cb0ef41Sopenharmony_ci    unsigned char *iv, c, cc;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    iv = &(*ivec)[0];
351cb0ef41Sopenharmony_ci    if (enc) {
361cb0ef41Sopenharmony_ci        while (l--) {
371cb0ef41Sopenharmony_ci            if (n == 0) {
381cb0ef41Sopenharmony_ci                c2l(iv, v0);
391cb0ef41Sopenharmony_ci                ti[0] = v0;
401cb0ef41Sopenharmony_ci                c2l(iv, v1);
411cb0ef41Sopenharmony_ci                ti[1] = v1;
421cb0ef41Sopenharmony_ci                DES_encrypt1(ti, schedule, DES_ENCRYPT);
431cb0ef41Sopenharmony_ci                iv = &(*ivec)[0];
441cb0ef41Sopenharmony_ci                v0 = ti[0];
451cb0ef41Sopenharmony_ci                l2c(v0, iv);
461cb0ef41Sopenharmony_ci                v0 = ti[1];
471cb0ef41Sopenharmony_ci                l2c(v0, iv);
481cb0ef41Sopenharmony_ci                iv = &(*ivec)[0];
491cb0ef41Sopenharmony_ci            }
501cb0ef41Sopenharmony_ci            c = *(in++) ^ iv[n];
511cb0ef41Sopenharmony_ci            *(out++) = c;
521cb0ef41Sopenharmony_ci            iv[n] = c;
531cb0ef41Sopenharmony_ci            n = (n + 1) & 0x07;
541cb0ef41Sopenharmony_ci        }
551cb0ef41Sopenharmony_ci    } else {
561cb0ef41Sopenharmony_ci        while (l--) {
571cb0ef41Sopenharmony_ci            if (n == 0) {
581cb0ef41Sopenharmony_ci                c2l(iv, v0);
591cb0ef41Sopenharmony_ci                ti[0] = v0;
601cb0ef41Sopenharmony_ci                c2l(iv, v1);
611cb0ef41Sopenharmony_ci                ti[1] = v1;
621cb0ef41Sopenharmony_ci                DES_encrypt1(ti, schedule, DES_ENCRYPT);
631cb0ef41Sopenharmony_ci                iv = &(*ivec)[0];
641cb0ef41Sopenharmony_ci                v0 = ti[0];
651cb0ef41Sopenharmony_ci                l2c(v0, iv);
661cb0ef41Sopenharmony_ci                v0 = ti[1];
671cb0ef41Sopenharmony_ci                l2c(v0, iv);
681cb0ef41Sopenharmony_ci                iv = &(*ivec)[0];
691cb0ef41Sopenharmony_ci            }
701cb0ef41Sopenharmony_ci            cc = *(in++);
711cb0ef41Sopenharmony_ci            c = iv[n];
721cb0ef41Sopenharmony_ci            iv[n] = cc;
731cb0ef41Sopenharmony_ci            *(out++) = c ^ cc;
741cb0ef41Sopenharmony_ci            n = (n + 1) & 0x07;
751cb0ef41Sopenharmony_ci        }
761cb0ef41Sopenharmony_ci    }
771cb0ef41Sopenharmony_ci    v0 = v1 = ti[0] = ti[1] = c = cc = 0;
781cb0ef41Sopenharmony_ci    *num = n;
791cb0ef41Sopenharmony_ci}
80