summaryrefslogtreecommitdiff
path: root/lib/dns/sec/dnssafe/seccbce.c
blob: 6fe5c863b90b76093fc4151942999e324e1faebc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Copyright (C) RSA Data Security, Inc. created 1990, 1996.  This is an
   unpublished work protected as such under copyright law.  This work
   contains proprietary, confidential, and trade secret information of
   RSA Data Security, Inc.  Use, disclosure or reproduction without the
   express written authorization of RSA Data Security, Inc. is
   prohibited.
 */

#include "global.h"
#include "algae.h"
#include "secrcbc.h"

/* On first call, it is assumed that *remainderLen is zero.
   Returns AE_OUTPUT_LEN, 0.
 */
int SecretCBCEncryptUpdate
  (context, xorBlock, remainder, remainderLen, SecretEncrypt, output,
   outputLen, maxOutputLen, input, inputLen)
POINTER context;
unsigned char *xorBlock;
unsigned char *remainder;
unsigned int *remainderLen;
SECRET_CRYPT SecretEncrypt;
unsigned char *output;
unsigned int *outputLen;
unsigned int maxOutputLen;
unsigned char *input;
unsigned int inputLen;
{
  unsigned int partialLen, totalLen, i;

  totalLen = *remainderLen + inputLen;

  /* Output length will be all available 8-byte blocks.
   */
  if ((*outputLen = 8 * (totalLen / 8)) > maxOutputLen)
    return (AE_OUTPUT_LEN);
  
  if (totalLen < 8) {
    /* Not enough to encrypt, just accumulate into remainder.
     */
    T_memcpy
      ((POINTER)remainder + *remainderLen, (POINTER)input, inputLen);
    *remainderLen = totalLen;
    
    return (0);
  }
  
  /* Accumulate enough bytes from input into remainder to encrypt the
       remainder.
   */
  T_memcpy
    ((POINTER)remainder + *remainderLen, (POINTER)input,
     partialLen = 8 - *remainderLen);
    
  for (i = 0; i < 8; i++)
    output[i] = remainder[i] ^ xorBlock[i];
  /* Encrypt in place */
  (*SecretEncrypt) (context, output, output);
  
  T_memcpy ((POINTER)xorBlock, (POINTER)output, 8);
  input += partialLen;
  inputLen -= partialLen;
  output += 8;

  /* Now encrypt the bulk of the input.
   */
  while (inputLen >= 8) {
    for (i = 0; i < 8; i++)
      output[i] = *(input++) ^ xorBlock[i];
    /* Encrypt in place */
    (*SecretEncrypt) (context, output, output);
    T_memcpy ((POINTER)xorBlock, (POINTER)output, 8);
    output += 8;
    inputLen -= 8;
  }

  /* inputLen is now < 8, so copy input to remainder.
   */
  T_memcpy ((POINTER)remainder, (POINTER)input, inputLen);
  *remainderLen = inputLen;
   
  return (0);
}

/* This just ensures that *remainderLen is zero.
   The caller must restart the context (setting remainderLen to zero).
   Returns AE_INPUT_LEN, 0.
 */
int SecretCBCEncryptFinal (remainderLen)
unsigned int remainderLen;
{
  if (remainderLen != 0)
    return (AE_INPUT_LEN);
  
  return (0);
}