11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 1998-2016 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 * #included by:
121cb0ef41Sopenharmony_ci *    cbc_enc.c  (DES_cbc_encrypt)
131cb0ef41Sopenharmony_ci *    des_enc.c  (DES_ncbc_encrypt)
141cb0ef41Sopenharmony_ci */
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci#include "des_local.h"
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci#ifdef CBC_ENC_C__DONT_UPDATE_IV
191cb0ef41Sopenharmony_civoid DES_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
201cb0ef41Sopenharmony_ci                     DES_key_schedule *_schedule, DES_cblock *ivec, int enc)
211cb0ef41Sopenharmony_ci#else
221cb0ef41Sopenharmony_civoid DES_ncbc_encrypt(const unsigned char *in, unsigned char *out,
231cb0ef41Sopenharmony_ci                      long length, DES_key_schedule *_schedule,
241cb0ef41Sopenharmony_ci                      DES_cblock *ivec, int enc)
251cb0ef41Sopenharmony_ci#endif
261cb0ef41Sopenharmony_ci{
271cb0ef41Sopenharmony_ci    register DES_LONG tin0, tin1;
281cb0ef41Sopenharmony_ci    register DES_LONG tout0, tout1, xor0, xor1;
291cb0ef41Sopenharmony_ci    register long l = length;
301cb0ef41Sopenharmony_ci    DES_LONG tin[2];
311cb0ef41Sopenharmony_ci    unsigned char *iv;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    iv = &(*ivec)[0];
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    if (enc) {
361cb0ef41Sopenharmony_ci        c2l(iv, tout0);
371cb0ef41Sopenharmony_ci        c2l(iv, tout1);
381cb0ef41Sopenharmony_ci        for (l -= 8; l >= 0; l -= 8) {
391cb0ef41Sopenharmony_ci            c2l(in, tin0);
401cb0ef41Sopenharmony_ci            c2l(in, tin1);
411cb0ef41Sopenharmony_ci            tin0 ^= tout0;
421cb0ef41Sopenharmony_ci            tin[0] = tin0;
431cb0ef41Sopenharmony_ci            tin1 ^= tout1;
441cb0ef41Sopenharmony_ci            tin[1] = tin1;
451cb0ef41Sopenharmony_ci            DES_encrypt1((DES_LONG *)tin, _schedule, DES_ENCRYPT);
461cb0ef41Sopenharmony_ci            tout0 = tin[0];
471cb0ef41Sopenharmony_ci            l2c(tout0, out);
481cb0ef41Sopenharmony_ci            tout1 = tin[1];
491cb0ef41Sopenharmony_ci            l2c(tout1, out);
501cb0ef41Sopenharmony_ci        }
511cb0ef41Sopenharmony_ci        if (l != -8) {
521cb0ef41Sopenharmony_ci            c2ln(in, tin0, tin1, l + 8);
531cb0ef41Sopenharmony_ci            tin0 ^= tout0;
541cb0ef41Sopenharmony_ci            tin[0] = tin0;
551cb0ef41Sopenharmony_ci            tin1 ^= tout1;
561cb0ef41Sopenharmony_ci            tin[1] = tin1;
571cb0ef41Sopenharmony_ci            DES_encrypt1((DES_LONG *)tin, _schedule, DES_ENCRYPT);
581cb0ef41Sopenharmony_ci            tout0 = tin[0];
591cb0ef41Sopenharmony_ci            l2c(tout0, out);
601cb0ef41Sopenharmony_ci            tout1 = tin[1];
611cb0ef41Sopenharmony_ci            l2c(tout1, out);
621cb0ef41Sopenharmony_ci        }
631cb0ef41Sopenharmony_ci#ifndef CBC_ENC_C__DONT_UPDATE_IV
641cb0ef41Sopenharmony_ci        iv = &(*ivec)[0];
651cb0ef41Sopenharmony_ci        l2c(tout0, iv);
661cb0ef41Sopenharmony_ci        l2c(tout1, iv);
671cb0ef41Sopenharmony_ci#endif
681cb0ef41Sopenharmony_ci    } else {
691cb0ef41Sopenharmony_ci        c2l(iv, xor0);
701cb0ef41Sopenharmony_ci        c2l(iv, xor1);
711cb0ef41Sopenharmony_ci        for (l -= 8; l >= 0; l -= 8) {
721cb0ef41Sopenharmony_ci            c2l(in, tin0);
731cb0ef41Sopenharmony_ci            tin[0] = tin0;
741cb0ef41Sopenharmony_ci            c2l(in, tin1);
751cb0ef41Sopenharmony_ci            tin[1] = tin1;
761cb0ef41Sopenharmony_ci            DES_encrypt1((DES_LONG *)tin, _schedule, DES_DECRYPT);
771cb0ef41Sopenharmony_ci            tout0 = tin[0] ^ xor0;
781cb0ef41Sopenharmony_ci            tout1 = tin[1] ^ xor1;
791cb0ef41Sopenharmony_ci            l2c(tout0, out);
801cb0ef41Sopenharmony_ci            l2c(tout1, out);
811cb0ef41Sopenharmony_ci            xor0 = tin0;
821cb0ef41Sopenharmony_ci            xor1 = tin1;
831cb0ef41Sopenharmony_ci        }
841cb0ef41Sopenharmony_ci        if (l != -8) {
851cb0ef41Sopenharmony_ci            c2l(in, tin0);
861cb0ef41Sopenharmony_ci            tin[0] = tin0;
871cb0ef41Sopenharmony_ci            c2l(in, tin1);
881cb0ef41Sopenharmony_ci            tin[1] = tin1;
891cb0ef41Sopenharmony_ci            DES_encrypt1((DES_LONG *)tin, _schedule, DES_DECRYPT);
901cb0ef41Sopenharmony_ci            tout0 = tin[0] ^ xor0;
911cb0ef41Sopenharmony_ci            tout1 = tin[1] ^ xor1;
921cb0ef41Sopenharmony_ci            l2cn(tout0, tout1, out, l + 8);
931cb0ef41Sopenharmony_ci#ifndef CBC_ENC_C__DONT_UPDATE_IV
941cb0ef41Sopenharmony_ci            xor0 = tin0;
951cb0ef41Sopenharmony_ci            xor1 = tin1;
961cb0ef41Sopenharmony_ci#endif
971cb0ef41Sopenharmony_ci        }
981cb0ef41Sopenharmony_ci#ifndef CBC_ENC_C__DONT_UPDATE_IV
991cb0ef41Sopenharmony_ci        iv = &(*ivec)[0];
1001cb0ef41Sopenharmony_ci        l2c(xor0, iv);
1011cb0ef41Sopenharmony_ci        l2c(xor1, iv);
1021cb0ef41Sopenharmony_ci#endif
1031cb0ef41Sopenharmony_ci    }
1041cb0ef41Sopenharmony_ci    tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
1051cb0ef41Sopenharmony_ci    tin[0] = tin[1] = 0;
1061cb0ef41Sopenharmony_ci}
107