summaryrefslogtreecommitdiff
path: root/cipher/bufhelp.h
blob: 638ca1b2b9ba940625e0dea77eb428cd7929574a (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* bufhelp.h  -  Some buffer manipulation helpers
 *	Copyright © 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser general Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */
#ifndef G10_BUFHELP_H
#define G10_BUFHELP_H

#ifdef HAVE_STDINT_H
# include <stdint.h> /* uintptr_t */
#elif defined(HAVE_INTTYPES_H)
# include <inttypes.h>
#else
/* In this case, uintptr_t is provided by config.h. */
#endif


#if defined(__i386__) || defined(__x86_64__)
/* These architechtures are able of unaligned memory accesses and can
   handle those fast.
 */
# define BUFHELP_FAST_UNALIGNED_ACCESS 1
#endif


/* Optimized function for buffer xoring */
static inline void
buf_xor(void *_dst, const void *_src1, const void *_src2, size_t len)
{
  byte *dst = _dst;
  const byte *src1 = _src1;
  const byte *src2 = _src2;
  uintptr_t *ldst;
  const uintptr_t *lsrc1, *lsrc2;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
  const unsigned int longmask = sizeof(uintptr_t) - 1;

  /* Skip fast processing if alignment of buffers do not match.  */
  if ((((uintptr_t)dst ^ (uintptr_t)src1) |
       ((uintptr_t)dst ^ (uintptr_t)src2)) & longmask)
    goto do_bytes;

  /* Handle unaligned head.  */
  for (; len && ((uintptr_t)dst & longmask); len--)
      *dst++ = *src1++ ^ *src2++;
#endif

  ldst = (uintptr_t *)dst;
  lsrc1 = (const uintptr_t *)src1;
  lsrc2 = (const uintptr_t *)src2;

  for (; len >= sizeof(uintptr_t); len -= sizeof(uintptr_t))
    *ldst++ = *lsrc1++ ^ *lsrc2++;

  dst = (byte *)ldst;
  src1 = (const byte *)lsrc1;
  src2 = (const byte *)lsrc2;

#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
do_bytes:
#endif
  /* Handle tail.  */
  for (; len; len--)
    *dst++ = *src1++ ^ *src2++;
}


/* Optimized function for buffer xoring with two destination buffers.  Used
   mainly by CFB mode encryption.  */
static inline void
buf_xor_2dst(void *_dst1, void *_dst2, const void *_src, size_t len)
{
  byte *dst1 = _dst1;
  byte *dst2 = _dst2;
  const byte *src = _src;
  uintptr_t *ldst1, *ldst2;
  const uintptr_t *lsrc;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
  const unsigned int longmask = sizeof(uintptr_t) - 1;

  /* Skip fast processing if alignment of buffers do not match.  */
  if ((((uintptr_t)src ^ (uintptr_t)dst1) |
       ((uintptr_t)src ^ (uintptr_t)dst2)) & longmask)
    goto do_bytes;

  /* Handle unaligned head.  */
  for (; len && ((uintptr_t)src & longmask); len--)
    *dst1++ = (*dst2++ ^= *src++);
#endif

  ldst1 = (uintptr_t *)dst1;
  ldst2 = (uintptr_t *)dst2;
  lsrc = (const uintptr_t *)src;

  for (; len >= sizeof(uintptr_t); len -= sizeof(uintptr_t))
    *ldst1++ = (*ldst2++ ^= *lsrc++);

  dst1 = (byte *)ldst1;
  dst2 = (byte *)ldst2;
  src = (const byte *)lsrc;

#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
do_bytes:
#endif
  /* Handle tail.  */
  for (; len; len--)
    *dst1++ = (*dst2++ ^= *src++);
}


/* Optimized function for combined buffer xoring and copying.  Used by mainly
   CFB mode decryption.  */
static inline void
buf_xor_n_copy(void *_dst_xor, void *_srcdst_cpy, const void *_src, size_t len)
{
  byte *dst_xor = _dst_xor;
  byte *srcdst_cpy = _srcdst_cpy;
  byte temp;
  const byte *src = _src;
  uintptr_t *ldst_xor, *lsrcdst_cpy;
  const uintptr_t *lsrc;
  uintptr_t ltemp;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
  const unsigned int longmask = sizeof(uintptr_t) - 1;

  /* Skip fast processing if alignment of buffers do not match.  */
  if ((((uintptr_t)src ^ (uintptr_t)dst_xor) |
       ((uintptr_t)src ^ (uintptr_t)srcdst_cpy)) & longmask)
    goto do_bytes;

  /* Handle unaligned head.  */
  for (; len && ((uintptr_t)src & longmask); len--)
    {
      temp = *src++;
      *dst_xor++ = *srcdst_cpy ^ temp;
      *srcdst_cpy++ = temp;
    }
#endif

  ldst_xor = (uintptr_t *)dst_xor;
  lsrcdst_cpy = (uintptr_t *)srcdst_cpy;
  lsrc = (const uintptr_t *)src;

  for (; len >= sizeof(uintptr_t); len -= sizeof(uintptr_t))
    {
      ltemp = *lsrc++;
      *ldst_xor++ = *lsrcdst_cpy ^ ltemp;
      *lsrcdst_cpy++ = ltemp;
    }

  dst_xor = (byte *)ldst_xor;
  srcdst_cpy = (byte *)lsrcdst_cpy;
  src = (const byte *)lsrc;

#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
do_bytes:
#endif
  /* Handle tail.  */
  for (; len; len--)
    {
      temp = *src++;
      *dst_xor++ = *srcdst_cpy ^ temp;
      *srcdst_cpy++ = temp;
    }
}

#endif /*G10_BITHELP_H*/