summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/md5.c
blob: 9ac1efc708d280c177931c4ad69e14748b4af327 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm */

/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
rights reserved.

License to copy and use this software is granted provided that it
is identified as the "RSA Data Security, Inc. MD5 Message-Digest
Algorithm" in all material mentioning or referencing this software
or this function.

License is also granted to make and use derivative works provided
that such works are identified as "derived from the RSA Data
Security, Inc. MD5 Message-Digest Algorithm" in all material
mentioning or referencing the derived work.

RSA Data Security, Inc. makes no representations concerning either
the merchantability of this software or the suitability of this
software for any particular purpose. It is provided "as is"
without express or implied warranty of any kind.

These notices must be retained in any copies of any part of this
documentation and/or software.
 */

#include <vppinfra/string.h>	/* for memset */
#include <vppinfra/byte_order.h>
#include <vppinfra/md5.h>

/* F, G, H and I are basic MD5 functions. */
#define F(b, c, d) (d ^ (b & (c ^ d)))
#define G(b, c, d) F (d, b, c)
#define H(b, c, d) (b ^ c ^ d)
#define I(b, c, d) (c ^ (b | ~d))

/* ROTATE_LEFT rotates x left n bits. */
#define ROTATE_LEFT(x,n) \
  (((x) << (n)) | ((x) >> (32 - (n))))

/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
   Rotation is separate from addition to prevent recomputation. */
#define FF(a,b,c,d,x,s,ac)			\
do {						\
  a += F (b, c, d) + x + ac;			\
  a = ROTATE_LEFT (a, s);			\
  a += b;					\
} while (0)

#define GG(a,b,c,d,x,s,ac)			\
do {						\
  a += G (b, c, d) + x + ac;			\
  a = ROTATE_LEFT (a, s);			\
  a += b;					\
} while (0)

#define HH(a,b,c,d,x,s,ac)			\
do {						\
  a += H (b, c, d) + x + ac;			\
  a = ROTATE_LEFT (a, s);			\
  a += b;					\
} while (0)

#define II(a,b,c,d,x,s,ac)			\
do {						\
  a += I (b, c, d) + x + ac;			\
  a = ROTATE_LEFT (a, s);			\
  a += b;					\
} while (0)

#undef _

/* MD5 basic transformation. Transforms state based on block. */
static void
md5_transform (md5_context_t * m, u32 * data, u32 * result, int zero_buffer)
{
  u32 a = m->state[0], b = m->state[1], c = m->state[2], d = m->state[3];
  u32 *x = data;

/* Constants for MD5Transform routine. */
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21

  /* Round 1 */
  FF (a, b, c, d, clib_host_to_little_u32 (x[0]), S11, 0xd76aa478);	/* 1 */
  FF (d, a, b, c, clib_host_to_little_u32 (x[1]), S12, 0xe8c7b756);	/* 2 */
  FF (c, d, a, b, clib_host_to_little_u32 (x[2]), S13, 0x242070db);	/* 3 */
  FF (b, c, d, a, clib_host_to_little_u32 (x[3]), S14, 0xc1bdceee);	/* 4 */
  FF (a, b, c, d, clib_host_to_little_u32 (x[4]), S11, 0xf57c0faf);	/* 5 */
  FF (d, a, b, c, clib_host_to_little_u32 (x[5]), S12, 0x4787c62a);	/* 6 */
  FF (c, d, a, b, clib_host_to_little_u32 (x[6]), S13, 0xa8304613);	/* 7 */
  FF (b, c, d, a, clib_host_to_little_u32 (x[7]), S14, 0xfd469501);	/* 8 */
  FF (a, b, c, d, clib_host_to_little_u32 (x[8]), S11, 0x698098d8);	/* 9 */
  FF (d, a, b, c, clib_host_to_little_u32 (x[9]), S12, 0x8b44f7af);	/* 10 */
  FF (c, d, a, b, clib_host_to_little_u32 (x[10]), S13, 0xffff5bb1);	/* 11 */
  FF (b, c, d, a, clib_host_to_little_u32 (x[11]), S14, 0x895cd7be);	/* 12 */
  FF (a, b, c, d, clib_host_to_little_u32 (x[12]), S11, 0x6b901122);	/* 13 */
  FF (d, a, b, c, clib_host_to_little_u32 (x[13]), S12, 0xfd987193);	/* 14 */
  FF (c, d, a, b, clib_host_to_little_u32 (x[14]), S13, 0xa679438e);	/* 15 */
  FF (b, c, d, a, clib_host_to_little_u32 (x[15]), S14, 0x49b40821);	/* 16 */

  /* Round 2 */
  GG (a, b, c, d, x[1], S21, 0xf61e2562);	/* 17 */
  GG (d, a, b, c, x[6], S22, 0xc040b340);	/* 18 */
  GG (c, d, a, b, x[11], S23, 0x265e5a51);	/* 19 */
  GG (b, c, d, a, x[0], S24, 0xe9b6c7aa);	/* 20 */
  GG (a, b, c, d, x[5], S21, 0xd62f105d);	/* 21 */
  GG (d, a, b, c, x[10], S22, 0x02441453);	/* 22 */
  GG (c, d, a, b, x[15], S23, 0xd8a1e681);	/* 23 */
  GG (b, c, d, a, x[4], S24, 0xe7d3fbc8);	/* 24 */
  GG (a, b, c, d, x[9], S21, 0x21e1cde6);	/* 25 */
  GG (d, a, b, c, x[14], S22, 0xc33707d6);	/* 26 */
  GG (c, d, a, b, x[3], S23, 0xf4d50d87);	/* 27 */
  GG (b, c, d, a, x[8], S24, 0x455a14ed);	/* 28 */
  GG (a, b, c, d, x[13], S21, 0xa9e3e905);	/* 29 */
  GG (d, a, b, c, x[2], S22, 0xfcefa3f8);	/* 30 */
  GG (c, d, a, b, x[7], S23, 0x676f02d9);	/* 31 */
  GG (b, c, d, a, x[12], S24, 0x8d2a4c8a);	/* 32 */

  /* Round 3 */
  HH (a, b, c, d, x[5], S31, 0xfffa3942);	/* 33 */
  HH (d, a, b, c, x[8], S32, 0x8771f681);	/* 34 */
  HH (c, d, a, b, x[11], S33, 0x6d9d6122);	/* 35 */
  HH (b, c, d, a, x[14], S34, 0xfde5380c);	/* 36 */
  HH (a, b, c, d, x[1], S31, 0xa4beea44);	/* 37 */
  HH (d, a, b, c, x[4], S32, 0x4bdecfa9);	/* 38 */
  HH (c, d, a, b, x[7], S33, 0xf6bb4b60);	/* 39 */
  HH (b, c, d, a, x[10], S34, 0xbebfbc70);	/* 40 */
  HH (a, b, c, d, x[13], S31, 0x289b7ec6);	/* 41 */
  HH (d, a, b, c, x[0], S32, 0xeaa127fa);	/* 42 */
  HH (c, d, a, b, x[3], S33, 0xd4ef3085);	/* 43 */
  HH (b, c, d, a, x[6], S34, 0x04881d05);	/* 44 */
  HH (a, b, c, d, x[9], S31, 0xd9d4d039);	/* 45 */
  HH (d, a, b, c, x[12], S32, 0xe6db99e5);	/* 46 */
  HH (c, d, a, b, x[15], S33, 0x1fa27cf8);	/* 47 */
  HH (b, c, d, a, x[2], S34, 0xc4ac5665);	/* 48 */

  /* Round 4 */
  II (a, b, c, d, x[0], S41, 0xf4292244);	/* 49 */
  II (d, a, b, c, x[7], S42, 0x432aff97);	/* 50 */
  II (c, d, a, b, x[14], S43, 0xab9423a7);	/* 51 */
  II (b, c, d, a, x[5], S44, 0xfc93a039);	/* 52 */
  II (a, b, c, d, x[12], S41, 0x655b59c3);	/* 53 */
  II (d, a, b, c, x[3], S42, 0x8f0ccc92);	/* 54 */
  II (c, d, a, b, x[10], S43, 0xffeff47d);	/* 55 */
  II (b, c, d, a, x[1], S44, 0x85845dd1);	/* 56 */
  II (a, b, c, d, x[8], S41, 0x6fa87e4f);	/* 57 */
  II (d, a, b, c, x[15], S42, 0xfe2ce6e0);	/* 58 */
  II (c, d, a, b, x[6], S43, 0xa3014314);	/* 59 */
  II (b, c, d, a, x[13], S44, 0x4e0811a1);	/* 60 */
  II (a, b, c, d, x[4], S41, 0xf7537e82);	/* 61 */
  II (d, a, b, c, x[11], S42, 0xbd3af235);	/* 62 */
  II (c, d, a, b, x[2], S43, 0x2ad7d2bb);	/* 63 */
  II (b, c, d, a, x[9], S44, 0xeb86d391);	/* 64 */

  a += m->state[0];
  b += m->state[1];
  c += m->state[2];
  d += m->state[3];

  if (result)
    {
      result[0] = clib_host_to_little_u32 (a);
      result[1] = clib_host_to_little_u32 (b);
      result[2] = clib_host_to_little_u32 (c);
      result[3] = clib_host_to_little_u32 (d);
    }
  else
    {
      m->state[0] = a;
      m->state[1] = b;
      m->state[2] = c;
      m->state[3] = d;
    }

  /* Zero sensitive information. */
  if (result)
    memset (m, ~0, sizeof (m[0]));
  else if (zero_buffer)
    memset (m->input_buffer.b8, 0, sizeof (m->input_buffer));
}

/* MD5 initialization. Begins an MD5 operation, writing a new context. */
void
md5_init (md5_context_t * c)
{
  memset (c, 0, sizeof (c[0]));

  /* Load magic initialization constants. */
  c->state[0] = 0x67452301;
  c->state[1] = 0xefcdab89;
  c->state[2] = 0x98badcfe;
  c->state[3] = 0x10325476;
}

always_inline void __attribute__ ((unused))
md5_fill_buffer_aligned (md5_context_t * c, u32 * d32)
{
  int i;
  for (i = 0; i < ARRAY_LEN (c->input_buffer.b32); i++)
    c->input_buffer.b32[i] = d32[i];
}

/* MD5 block update operation. Continues an MD5 message-digest
  operation, processing another message block, and updating the
  context.
 */
void
md5_add (md5_context_t * c, void *data, int data_bytes)
{
  u32 data_bytes_left;
  void *d;

  if (data_bytes == 0)
    return;

  d = data;
  data_bytes_left = data_bytes;

  if ((pointer_to_uword (d) % sizeof (u32)) == 0
      && (c->n_bits % BITS (c->input_buffer)) == 0
      && data_bytes >= sizeof (c->input_buffer))
    {
      int is_last_iteration;
      /* Fast aligned version. */
      do
	{
	  data_bytes_left -= sizeof (c->input_buffer);
	  is_last_iteration = data_bytes_left < sizeof (c->input_buffer);
	  md5_transform (c, d, /* result */ 0,	/* zero_buffer */
			 is_last_iteration);
	  d += sizeof (c->input_buffer);
	}
      while (!is_last_iteration);
    }

  /* Slow unaligned version. */
  {
    int bi;
    u8 *d8 = d;

    bi = (c->n_bits / BITS (u8)) % ARRAY_LEN (c->input_buffer.b8);

    while (data_bytes_left > 0)
      {
	c->input_buffer.b8[bi] = d8[0];
	data_bytes_left -= 1;
	d8++;
	bi++;
	if (bi == ARRAY_LEN (c->input_buffer.b8))
	  {
	    bi = 0;
	    md5_transform (c, c->input_buffer.b32,
			   /* result */ 0,
			   /* zero_buffer */ 1);
	  }
      }
  }

  c->n_bits += data_bytes * BITS (u8);
}

void
md5_finish (md5_context_t * c, u8 * digest)
{
  u64 n_bits_save;
  int bi, n_pad;
  static u8 padding[sizeof (c->input_buffer)] = { 0x80, 0, };

  n_bits_save = c->n_bits;
  bi = (n_bits_save / BITS (u8)) % ARRAY_LEN (c->input_buffer.b8);

  n_pad = sizeof (c->input_buffer) - (bi + sizeof (u64));
  if (n_pad <= 0)
    n_pad += sizeof (c->input_buffer);
  md5_add (c, padding, n_pad);

  c->input_buffer.b64[ARRAY_LEN (c->input_buffer.b64) - 1]
    = clib_host_to_little_u64 (n_bits_save);

  md5_transform (c, c->input_buffer.b32, (u32 *) digest,
		 /* zero_buffer */ 1);
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
an> 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }; u8x32 lv = u8x32_splat (len); u8x32 add = u8x32_splat (32); s0 = u8x32_load_unaligned (src); s1 = u8x32_load_unaligned (src + 32); d0 = u8x32_load_unaligned (dst); d1 = u8x32_load_unaligned (dst + 32); d0 = u8x32_blend (d0, s0, u8x32_is_greater (lv, mask)); u8x32_store_unaligned (d0, dst); if (max_len <= 32) return; mask += add; d1 = u8x32_blend (d1, s1, u8x32_is_greater (lv, mask)); u8x32_store_unaligned (d1, dst + 32); #elif defined (CLIB_HAVE_VEC128) u8x16 s0, s1, s2, s3, d0, d1, d2, d3; u8x16 mask = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; u8x16 lv = u8x16_splat (len); u8x16 add = u8x16_splat (16); s0 = u8x16_load_unaligned (src); s1 = u8x16_load_unaligned (src + 16); s2 = u8x16_load_unaligned (src + 32); s3 = u8x16_load_unaligned (src + 48); d0 = u8x16_load_unaligned (dst); d1 = u8x16_load_unaligned (dst + 16); d2 = u8x16_load_unaligned (dst + 32); d3 = u8x16_load_unaligned (dst + 48); d0 = u8x16_blend (d0, s0, u8x16_is_greater (lv, mask)); u8x16_store_unaligned (d0, dst); if (max_len <= 16) return; mask += add; d1 = u8x16_blend (d1, s1, u8x16_is_greater (lv, mask)); u8x16_store_unaligned (d1, dst + 16); if (max_len <= 32) return; mask += add; d2 = u8x16_blend (d2, s2, u8x16_is_greater (lv, mask)); u8x16_store_unaligned (d2, dst + 32); mask += add; d3 = u8x16_blend (d3, s3, u8x16_is_greater (lv, mask)); u8x16_store_unaligned (d3, dst + 48); #else memmove (dst, src, len); #endif } static_always_inline void clib_memcpy_le64 (u8 * dst, u8 * src, u8 len) { clib_memcpy_le (dst, src, len, 64); } static_always_inline void clib_memcpy_le32 (u8 * dst, u8 * src, u8 len) { clib_memcpy_le (dst, src, len, 32); } static_always_inline void clib_memset_u64 (void *p, u64 val, uword count) { u64 *ptr = p; #if defined(CLIB_HAVE_VEC512) u64x8 v512 = u64x8_splat (val); while (count >= 8) { u64x8_store_unaligned (v512, ptr); ptr += 8; count -= 8; } if (count == 0) return; #endif #if defined(CLIB_HAVE_VEC256) u64x4 v256 = u64x4_splat (val); while (count >= 4) { u64x4_store_unaligned (v256, ptr); ptr += 4; count -= 4; } if (count == 0) return; #else while (count >= 4) { ptr[0] = ptr[1] = ptr[2] = ptr[3] = val; ptr += 4; count -= 4; } #endif while (count--) ptr++[0] = val; } static_always_inline void clib_memset_u32 (void *p, u32 val, uword count) { u32 *ptr = p; #if defined(CLIB_HAVE_VEC512) u32x16 v512 = u32x16_splat (val); while (count >= 16) { u32x16_store_unaligned (v512, ptr); ptr += 16; count -= 16; } if (count == 0) return; #endif #if defined(CLIB_HAVE_VEC256) u32x8 v256 = u32x8_splat (val); while (count >= 8) { u32x8_store_unaligned (v256, ptr); ptr += 8; count -= 8; } if (count == 0) return; #endif #if defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE) u32x4 v128 = u32x4_splat (val); while (count >= 4) { u32x4_store_unaligned (v128, ptr); ptr += 4; count -= 4; } #else while (count >= 4) { ptr[0] = ptr[1] = ptr[2] = ptr[3] = val; ptr += 4; count -= 4; } #endif while (count--) ptr++[0] = val; } static_always_inline void clib_memset_u16 (void *p, u16 val, uword count) { u16 *ptr = p; #if defined(CLIB_HAVE_VEC512) u16x32 v512 = u16x32_splat (val); while (count >= 32) { u16x32_store_unaligned (v512, ptr); ptr += 32; count -= 32; } if (count == 0) return; #endif #if defined(CLIB_HAVE_VEC256) u16x16 v256 = u16x16_splat (val); while (count >= 16) { u16x16_store_unaligned (v256, ptr); ptr += 16; count -= 16; } if (count == 0) return; #endif #if defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE) u16x8 v128 = u16x8_splat (val); while (count >= 8) { u16x8_store_unaligned (v128, ptr); ptr += 8; count -= 8; } #else while (count >= 4) { ptr[0] = ptr[1] = ptr[2] = ptr[3] = val; ptr += 4; count -= 4; } #endif while (count--) ptr++[0] = val; } static_always_inline void clib_memset_u8 (void *p, u8 val, uword count) { u8 *ptr = p; #if defined(CLIB_HAVE_VEC512) u8x64 v512 = u8x64_splat (val); while (count >= 64) { u8x64_store_unaligned (v512, ptr); ptr += 64; count -= 64; } if (count == 0) return; #endif #if defined(CLIB_HAVE_VEC256) u8x32 v256 = u8x32_splat (val); while (count >= 32) { u8x32_store_unaligned (v256, ptr); ptr += 32; count -= 32; } if (count == 0) return; #endif #if defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE) u8x16 v128 = u8x16_splat (val); while (count >= 16) { u8x16_store_unaligned (v128, ptr); ptr += 16; count -= 16; } #else while (count >= 4) { ptr[0] = ptr[1] = ptr[2] = ptr[3] = val; ptr += 4; count -= 4; } #endif while (count--) ptr++[0] = val; } static_always_inline uword clib_count_equal_u64 (u64 * data, uword max_count) { uword count; u64 first; if (max_count == 1) return 1; if (data[0] != data[1]) return 1; count = 0; first = data[0]; #if defined(CLIB_HAVE_VEC256) u64x4 splat = u64x4_splat (first); while (1) { u64 bmp; bmp = u8x32_msb_mask ((u8x32) (u64x4_load_unaligned (data) == splat)); if (bmp != 0xffffffff) { count += count_trailing_zeros (~bmp) / 8; return clib_min (count, max_count); } data += 4; count += 4; if (count >= max_count) return max_count; } #endif count += 2; data += 2; while (count + 3 < max_count && ((data[0] ^ first) | (data[1] ^ first) | (data[2] ^ first) | (data[3] ^ first)) == 0) { data += 4; count += 4; } while (count < max_count && (data[0] == first)) { data += 1; count += 1; } return count; } static_always_inline uword clib_count_equal_u32 (u32 * data, uword max_count) { uword count; u32 first; if (max_count == 1) return 1; if (data[0] != data[1]) return 1; count = 0; first = data[0]; #if defined(CLIB_HAVE_VEC256) u32x8 splat = u32x8_splat (first); while (1) { u64 bmp; bmp = u8x32_msb_mask ((u8x32) (u32x8_load_unaligned (data) == splat)); if (bmp != 0xffffffff) { count += count_trailing_zeros (~bmp) / 4; return clib_min (count, max_count); } data += 8; count += 8; if (count >= max_count) return max_count; } #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK) u32x4 splat = u32x4_splat (first); while (1) { u64 bmp; bmp = u8x16_msb_mask ((u8x16) (u32x4_load_unaligned (data) == splat)); if (bmp != 0xffff) { count += count_trailing_zeros (~bmp) / 4; return clib_min (count, max_count); } data += 4; count += 4; if (count >= max_count) return max_count; } #endif count += 2; data += 2; while (count + 3 < max_count && ((data[0] ^ first) | (data[1] ^ first) | (data[2] ^ first) | (data[3] ^ first)) == 0) { data += 4; count += 4; } while (count < max_count && (data[0] == first)) { data += 1; count += 1; } return count; } static_always_inline uword clib_count_equal_u16 (u16 * data, uword max_count) { uword count; u16 first; if (max_count == 1) return 1; if (data[0] != data[1]) return 1; count = 0; first = data[0]; #if defined(CLIB_HAVE_VEC256) u16x16 splat = u16x16_splat (first); while (1) { u64 bmp; bmp = u8x32_msb_mask ((u8x32) (u16x16_load_unaligned (data) == splat)); if (bmp != 0xffffffff) { count += count_trailing_zeros (~bmp) / 2; return clib_min (count, max_count); } data += 16; count += 16; if (count >= max_count) return max_count; } #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK) u16x8 splat = u16x8_splat (first); while (1) { u64 bmp; bmp = u8x16_msb_mask ((u8x16) (u16x8_load_unaligned (data) == splat)); if (bmp != 0xffff) { count += count_trailing_zeros (~bmp) / 2; return clib_min (count, max_count); } data += 8; count += 8; if (count >= max_count) return max_count; } #endif count += 2; data += 2; while (count + 3 < max_count && ((data[0] ^ first) | (data[1] ^ first) | (data[2] ^ first) | (data[3] ^ first)) == 0) { data += 4; count += 4; } while (count < max_count && (data[0] == first)) { data += 1; count += 1; } return count; } static_always_inline uword clib_count_equal_u8 (u8 * data, uword max_count) { uword count; u8 first; if (max_count == 1) return 1; if (data[0] != data[1]) return 1; count = 0; first = data[0]; #if defined(CLIB_HAVE_VEC256) u8x32 splat = u8x32_splat (first); while (1) { u64 bmp; bmp = u8x32_msb_mask ((u8x32) (u8x32_load_unaligned (data) == splat)); if (bmp != 0xffffffff) { count += count_trailing_zeros (~bmp); return clib_min (count, max_count); } data += 32; count += 32; if (count >= max_count) return max_count; } #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK) u8x16 splat = u8x16_splat (first); while (1) { u64 bmp; bmp = u8x16_msb_mask ((u8x16) (u8x16_load_unaligned (data) == splat)); if (bmp != 0xffff) { count += count_trailing_zeros (~bmp); return clib_min (count, max_count); } data += 16; count += 16; if (count >= max_count) return max_count; } #endif count += 2; data += 2; while (count + 3 < max_count && ((data[0] ^ first) | (data[1] ^ first) | (data[2] ^ first) | (data[3] ^ first)) == 0) { data += 4; count += 4; } while (count < max_count && (data[0] == first)) { data += 1; count += 1; } return count; } /* * This macro is to provide smooth mapping from memcmp to memcmp_s. * memcmp has fewer parameters and fewer returns than memcmp_s. * This macro is somewhat a crutch. When err != EOK is returned from memcmp_s, * we return 0 and spit out a message in the console because there is * no way to return the error code to the memcmp callers. * This condition happens when s1 or s2 is null. Please note * in the extant memcmp calls, if s1, s2, or both are null, memcmp returns 0 * anyway. So we are consistent in this case for the comparison return * although we also spit out a C11 violation message in the console to * warn that they pass null pointers for both s1 and s2. * Applications are encouraged to use the cool C11 memcmp_s API to get the * maximum benefit out of it. */ #define clib_memcmp(s1,s2,m1) \ ({ int __diff = 0; \ memcmp_s_inline (s1, m1, s2, m1, &__diff); \ __diff; \ }) errno_t memcmp_s (const void *s1, rsize_t s1max, const void *s2, rsize_t s2max, int *diff); always_inline errno_t memcmp_s_inline (const void *s1, rsize_t s1max, const void *s2, rsize_t s2max, int *diff) { u8 bad; bad = (s1 == 0) + (s2 == 0) + (diff == 0) + (s2max > s1max) + (s2max == 0) + (s1max == 0); if (PREDICT_FALSE (bad != 0)) { if (s1 == NULL) clib_c11_violation ("s1 NULL"); if (s2 == NULL) clib_c11_violation ("s2 NULL"); if (diff == NULL) clib_c11_violation ("diff NULL"); if (s2max > s1max) clib_c11_violation ("s2max > s1max"); if (s2max == 0) clib_c11_violation ("s2max 0"); if (s1max == 0) clib_c11_violation ("s1max 0"); return EINVAL; } if (PREDICT_FALSE (s1 == s2)) { *diff = 0; return EOK; } *diff = memcmp (s1, s2, s2max); return EOK; } /* * This macro is to provide smooth mapping from strnlen to strnlen_s */ #define clib_strnlen(s,m) strnlen_s_inline(s,m) size_t strnlen_s (const char *s, size_t maxsize); always_inline size_t strnlen_s_inline (const char *s, size_t maxsize) { u8 bad; bad = (s == 0) + (maxsize == 0); if (PREDICT_FALSE (bad != 0)) { if (s == 0) clib_c11_violation ("s NULL"); if (maxsize == 0) clib_c11_violation ("maxsize 0"); return 0; } return strnlen (s, maxsize); } /* * This macro is to provide smooth mapping from strcmp to strcmp_s. * strcmp has fewer parameters and fewer returns than strcmp_s. * This macro is somewhat a crutch. When err != EOK is returned from strcmp_s, * we return 0 and spit out a message in the console because * there is no way to return the error to the strcmp callers. * This condition happens when s1 or s2 is null. Please note in the extant * strcmp call, they would end up crashing if one of them is null. * So the new behavior is no crash, but an error is displayed in the * console which I think is more user friendly. If both s1 and s2 are null, * strcmp returns 0. Obviously, strcmp did the pointers comparison prior * to actually accessing the pointer contents. We are still consistent * in this case for the comparison return although we also spit out a * C11 violation message in the console to warn that they pass null pointers * for both s1 and s2. The other problem is strcmp does not provide s1max, * we use CLIB_STRING_MACRO_MAX and hopefully, s1 is null terminated. * If not, we may be accessing memory beyonf what is intended. * Applications are encouraged to use the cool C11 strcmp_s API to get the * maximum benefit out of it. */ #define clib_strcmp(s1,s2) \ ({ int __indicator = 0; \ strcmp_s_inline (s1, CLIB_STRING_MACRO_MAX, s2, &__indicator); \ __indicator; \ }) errno_t strcmp_s (const char *s1, rsize_t s1max, const char *s2, int *indicator); always_inline errno_t strcmp_s_inline (const char *s1, rsize_t s1max, const char *s2, int *indicator) { u8 bad; bad = (indicator == 0) + (s1 == 0) + (s2 == 0) + (s1max == 0) + (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0'); if (PREDICT_FALSE (bad != 0)) { if (indicator == NULL) clib_c11_violation ("indicator NULL"); if (s1 == NULL) clib_c11_violation ("s1 NULL"); if (s2 == NULL) clib_c11_violation ("s2 NULL"); if (s1max == 0) clib_c11_violation ("s1max 0"); if (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0') clib_c11_violation ("s1 unterminated"); return EINVAL; } *indicator = strcmp (s1, s2); return EOK; } /* * This macro is to provide smooth mapping from strncmp to strncmp_s. * strncmp has fewer parameters and fewer returns than strncmp_s. That said, * this macro is somewhat a crutch. When we get err != EOK from strncmp_s, * we return 0 and spit out a message in the console because there is no * means to return the error to the strncmp caller. * This condition happens when s1 or s2 is null. In the extant strncmp call, * they would end up crashing if one of them is null. So the new behavior is * no crash, but error is displayed in the console which is more * user friendly. If s1 and s2 are null, strncmp returns 0. Obviously, * strncmp did the pointers comparison prior to actually accessing the * pointer contents. We are still consistent in this case for the comparison * return although we also spit out a C11 violation message in the console to * warn that they pass null pointers for both s1 and s2. * Applications are encouraged to use the cool C11 strncmp_s API to get the * maximum benefit out of it. */ #define clib_strncmp(s1,s2,n) \ ({ int __indicator = 0; \ strncmp_s_inline (s1, CLIB_STRING_MACRO_MAX, s2, n, &__indicator); \ __indicator; \ }) errno_t strncmp_s (const char *s1, rsize_t s1max, const char *s2, rsize_t n, int *indicator); always_inline errno_t strncmp_s_inline (const char *s1, rsize_t s1max, const char *s2, rsize_t n, int *indicator) { u8 bad; u8 s1_greater_s1max = (s1 && s1max && n > clib_strnlen (s1, s1max)); if (PREDICT_FALSE (s1_greater_s1max && indicator)) { /* * strcmp allows n > s1max. If indicator is non null, we can still * do the compare without any harm and return EINVAL as well as the * result in indicator. */ clib_c11_violation ("n exceeds s1 length"); *indicator = strncmp (s1, s2, n); return EINVAL; } bad = (s1 == 0) + (s2 == 0) + (indicator == 0) + (s1max == 0) + (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0') + s1_greater_s1max; if (PREDICT_FALSE (bad != 0)) { if (indicator == NULL) clib_c11_violation ("indicator NULL"); if (s1 == NULL) clib_c11_violation ("s1 NULL"); if (s2 == NULL) clib_c11_violation ("s2 NULL"); if (s1max == 0) clib_c11_violation ("s1max 0"); if (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0') clib_c11_violation ("s1 unterminated"); if (s1_greater_s1max) clib_c11_violation ("n exceeds s1 length"); return EINVAL; } *indicator = strncmp (s1, s2, n); return EOK; } /* * This macro is provided for smooth migration from strcpy. It is not perfect * because we don't know the size of the destination buffer to pass to strcpy_s. * We improvise dmax with CLIB_STRING_MACRO_MAX. * Applications are encouraged to move to the C11 strcpy_s API. */ #define clib_strcpy(d,s) strcpy_s_inline(d,CLIB_STRING_MACRO_MAX,s) errno_t strcpy_s (char *__restrict__ dest, rsize_t dmax, const char *__restrict__ src); always_inline errno_t strcpy_s_inline (char *__restrict__ dest, rsize_t dmax, const char *__restrict__ src) { u8 bad; uword low, hi; size_t n; bad = (dest == 0) + (dmax == 0) + (src == 0); if (PREDICT_FALSE (bad != 0)) { if (dest == 0) clib_c11_violation ("dest NULL"); if (src == 0) clib_c11_violation ("src NULL"); if (dmax == 0) clib_c11_violation ("dmax 0"); return EINVAL; } n = clib_strnlen (src, dmax); if (PREDICT_FALSE (n >= dmax)) { clib_c11_violation ("not enough space for dest"); return (EINVAL); } /* Not actually trying to copy anything is OK */ if (PREDICT_FALSE (n == 0)) return EOK; /* Check for src/dst overlap, which is not allowed */ low = (uword) (src < dest ? src : dest); hi = (uword) (src < dest ? dest : src); if (PREDICT_FALSE (low + (n - 1) >= hi)) { clib_c11_violation ("src/dest overlap"); return EINVAL; } clib_memcpy_fast (dest, src, n); dest[n] = '\0'; return EOK; } /* * This macro is provided for smooth migration from strncpy. It is not perfect * because we don't know the size of the destination buffer to pass to * strncpy_s. We improvise dmax with CLIB_STRING_MACRO_MAX. * Applications are encouraged to move to the C11 strncpy_s API and provide * the correct dmax for better error checking. */ #define clib_strncpy(d,s,n) strncpy_s_inline(d,CLIB_STRING_MACRO_MAX,s,n) errno_t strncpy_s (char *__restrict__ dest, rsize_t dmax, const char *__restrict__ src, rsize_t n); always_inline errno_t strncpy_s_inline (char *__restrict__ dest, rsize_t dmax, const char *__restrict__ src, rsize_t n) { u8 bad; uword low, hi; rsize_t m; errno_t status = EOK; bad = (dest == 0) + (dmax == 0) + (src == 0) + (n == 0); if (PREDICT_FALSE (bad != 0)) { /* Not actually trying to copy anything is OK */ if (n == 0) return EOK; if (dest == 0) clib_c11_violation ("dest NULL"); if (src == 0) clib_c11_violation ("src NULL"); if (dmax == 0) clib_c11_violation ("dmax 0"); return EINVAL; } if (PREDICT_FALSE (n >= dmax)) { /* Relax and use strnlen of src */ clib_c11_violation ("n >= dmax"); m = clib_strnlen (src, dmax); if (m >= dmax) { /* Truncate, adjust copy length to fit dest */ m = dmax - 1; status = EOVERFLOW; } } else /* cap the copy to strlen(src) in case n > strlen(src) */ m = clib_strnlen (src, n); /* Check for src/dst overlap, which is not allowed */ low = (uword) (src < dest ? src : dest); hi = (uword) (src < dest ? dest : src); /* * This check may fail innocently if src + dmax >= dst, but * src + strlen(src) < dst. If it fails, check more carefully before * blowing the whistle. */ if (PREDICT_FALSE (low + (m - 1) >= hi)) { m = clib_strnlen (src, m); if (low + (m - 1) >= hi) { clib_c11_violation ("src/dest overlap"); return EINVAL; } } clib_memcpy_fast (dest, src, m); dest[m] = '\0'; return status; } /* * This macro is to provide smooth migration from strcat to strcat_s. * Because there is no dmax in strcat, we improvise it with * CLIB_STRING_MACRO_MAX. Please note there may be a chance to overwrite dest * with too many bytes from src. * Applications are encouraged to use C11 API to provide the actual dmax * for proper checking and protection. */ #define clib_strcat(d,s) strcat_s_inline(d,CLIB_STRING_MACRO_MAX,s) errno_t strcat_s (char *__restrict__ dest, rsize_t dmax, const char *__restrict__ src); always_inline errno_t strcat_s_inline (char *__restrict__ dest, rsize_t dmax, const char *__restrict__ src) { u8 bad; uword low, hi; size_t m, n, dest_size; bad = (dest == 0) + (dmax == 0) + (src == 0); if (PREDICT_FALSE (bad != 0)) { if (dest == 0) clib_c11_violation ("dest NULL"); if (src == 0) clib_c11_violation ("src NULL"); if (dmax == 0) clib_c11_violation ("dmax 0"); return EINVAL; } dest_size = clib_strnlen (dest, dmax); m = dmax - dest_size; n = clib_strnlen (src, m); if (PREDICT_FALSE (n >= m)) { clib_c11_violation ("not enough space for dest"); return EINVAL; } /* Not actually trying to concatenate anything is OK */ if (PREDICT_FALSE (n == 0)) return EOK; /* Check for src/dst overlap, which is not allowed */ low = (uword) (src < dest ? src : dest); hi = (uword) (src < dest ? dest : src); if (PREDICT_FALSE (low + (n - 1) >= hi)) { clib_c11_violation ("src/dest overlap"); return EINVAL; } clib_memcpy_fast (dest + dest_size, src, n); dest[dest_size + n] = '\0'; return EOK; } /* * This macro is to provide smooth migration from strncat to strncat_s. * The unsafe strncat does not have s1max. We improvise it with * CLIB_STRING_MACRO_MAX. Please note there may be a chance to overwrite * dest with too many bytes from src. * Applications are encouraged to move to C11 strncat_s which requires dmax * from the caller and provides checking to safeguard the memory corruption. */ #define clib_strncat(d,s,n) strncat_s_inline(d,CLIB_STRING_MACRO_MAX,s,n) errno_t strncat_s (char *__restrict__ dest, rsize_t dmax, const char *__restrict__ src, rsize_t n); always_inline errno_t strncat_s_inline (char *__restrict__ dest, rsize_t dmax, const char *__restrict__ src, rsize_t n) { u8 bad; uword low, hi; size_t m, dest_size, allowed_size; errno_t status = EOK; bad = (dest == 0) + (src == 0) + (dmax == 0) + (n == 0); if (PREDICT_FALSE (bad != 0)) { /* Not actually trying to concatenate anything is OK */ if (n == 0) return EOK; if (dest == 0) clib_c11_violation ("dest NULL"); if (src == 0) clib_c11_violation ("src NULL"); if (dmax == 0) clib_c11_violation ("dmax 0"); return EINVAL; } /* Check for src/dst overlap, which is not allowed */ low = (uword) (src < dest ? src : dest); hi = (uword) (src < dest ? dest : src); if (PREDICT_FALSE (low + (n - 1) >= hi)) { clib_c11_violation ("src/dest overlap"); return EINVAL; } dest_size = clib_strnlen (dest, dmax); allowed_size = dmax - dest_size; if (PREDICT_FALSE (allowed_size == 0)) { clib_c11_violation ("no space left in dest"); return (EINVAL); } if (PREDICT_FALSE (n >= allowed_size)) { /* * unlike strcat_s, strncat_s will do the concatenation anyway when * there is not enough space in dest. But it will do the truncation and * null terminate dest */ m = clib_strnlen (src, allowed_size); if (m >= allowed_size) { m = allowed_size - 1; status = EOVERFLOW; } } else m = clib_strnlen (src, n); clib_memcpy_fast (dest + dest_size, src, m); dest[dest_size + m] = '\0'; return status; } /* * This macro is to provide smooth mapping from strtok_r to strtok_s. * To map strtok to this macro, the caller would have to supply an additional * argument. strtokr_s requires s1max which the unsafe API does not have. So * we have to improvise it with CLIB_STRING_MACRO_MAX. Unlike strtok_s, * this macro cannot catch unterminated s1 and s2. * Applications are encouraged to use the cool C11 strtok_s API to avoid * these problems. */ #define clib_strtok(s1,s2,p) \ ({ rsize_t __s1max = CLIB_STRING_MACRO_MAX; \ strtok_s_inline (s1, &__s1max, s2, p); \ }) char *strtok_s (char *__restrict__ s1, rsize_t * __restrict__ s1max, const char *__restrict__ s2, char **__restrict__ ptr); always_inline char * strtok_s_inline (char *__restrict__ s1, rsize_t * __restrict__ s1max, const char *__restrict__ s2, char **__restrict__ ptr) { #define STRTOK_DELIM_MAX_LEN 16 u8 bad; const char *pt; char *ptoken; uword dlen, slen; bad = (s1max == 0) + (s2 == 0) + (ptr == 0) + ((s1 == 0) && ptr && (*ptr == 0)); if (PREDICT_FALSE (bad != 0)) { if (s2 == NULL) clib_c11_violation ("s2 NULL"); if (s1max == NULL) clib_c11_violation ("s1max is NULL"); if (ptr == NULL) clib_c11_violation ("ptr is NULL"); /* s1 == 0 and *ptr == null is no good */ if ((s1 == 0) && ptr && (*ptr == 0)) clib_c11_violation ("s1 and ptr contents are NULL"); return 0; } if (s1 == 0) s1 = *ptr; /* * scan s1 for a delimiter */ dlen = *s1max; ptoken = 0; while (*s1 != '\0' && !ptoken) { if (PREDICT_FALSE (dlen == 0)) { *ptr = 0; clib_c11_violation ("s1 unterminated"); return 0; } /* * must scan the entire delimiter list * ISO should have included a delimiter string limit!! */ slen = STRTOK_DELIM_MAX_LEN; pt = s2; while (*pt != '\0') { if (PREDICT_FALSE (slen == 0)) { *ptr = 0; clib_c11_violation ("s2 unterminated"); return 0; } slen--; if (*s1 == *pt) { ptoken = 0; break; } else { pt++; ptoken = s1; } } s1++; dlen--; } /* * if the beginning of a token was not found, then no * need to continue the scan. */ if (ptoken == 0) { *s1max = dlen; return (ptoken); } /* * Now we need to locate the end of the token */ while (*s1 != '\0') { if (dlen == 0) { *ptr = 0; clib_c11_violation ("s1 unterminated"); return 0; } slen = STRTOK_DELIM_MAX_LEN; pt = s2; while (*pt != '\0') { if (slen == 0) { *ptr = 0; clib_c11_violation ("s2 unterminated"); return 0; } slen--; if (*s1 == *pt) { /* * found a delimiter, set to null * and return context ptr to next char */ *s1 = '\0'; *ptr = (s1 + 1); /* return pointer for next scan */ *s1max = dlen - 1; /* account for the nulled delimiter */ return (ptoken); } else { /* * simply scanning through the delimiter string */ pt++; } } s1++; dlen--; } *ptr = s1; *s1max = dlen; return (ptoken); } /* * This macro is to provide smooth mapping from strstr to strstr_s. * strstr_s requires s1max and s2max which the unsafe API does not have. So * we have to improvise them with CLIB_STRING_MACRO_MAX which may cause us * to access memory beyond it is intended if s1 or s2 is unterminated. * For the record, strstr crashes if s1 or s2 is unterminated. But this macro * does not. * Applications are encouraged to use the cool C11 strstr_s API to avoid * this problem. */ #define clib_strstr(s1,s2) \ ({ char * __substring = 0; \ strstr_s_inline (s1, CLIB_STRING_MACRO_MAX, s2, CLIB_STRING_MACRO_MAX, \ &__substring); \ __substring; \ }) errno_t strstr_s (char *s1, rsize_t s1max, const char *s2, rsize_t s2max, char **substring); always_inline errno_t strstr_s_inline (char *s1, rsize_t s1max, const char *s2, rsize_t s2max, char **substring) { u8 bad; size_t s1_size, s2_size; bad = (s1 == 0) + (s2 == 0) + (substring == 0) + (s1max == 0) + (s2max == 0) + (s1 && s1max && (s1[clib_strnlen (s1, s1max)] != '\0')) + (s2 && s2max && (s2[clib_strnlen (s2, s2max)] != '\0')); if (PREDICT_FALSE (bad != 0)) { if (s1 == 0) clib_c11_violation ("s1 NULL"); if (s2 == 0) clib_c11_violation ("s2 NULL"); if (s1max == 0) clib_c11_violation ("s1max 0"); if (s2max == 0) clib_c11_violation ("s2max 0"); if (substring == 0) clib_c11_violation ("substring NULL"); if (s1 && s1max && (s1[clib_strnlen (s1, s1max)] != '\0')) clib_c11_violation ("s1 unterminated"); if (s2 && s2max && (s2[clib_strnlen (s2, s1max)] != '\0')) clib_c11_violation ("s2 unterminated"); return EINVAL; } /* * s2 points to a string with zero length, or s2 equals s1, return s1 */ if (PREDICT_FALSE (*s2 == '\0' || s1 == s2)) { *substring = s1; return EOK; } /* * s2_size > s1_size, it won't find match. */ s1_size = clib_strnlen (s1, s1max); s2_size = clib_strnlen (s2, s2max); if (PREDICT_FALSE (s2_size > s1_size)) return ESRCH; *substring = strstr (s1, s2); if (*substring == 0) return ESRCH; return EOK; } #endif /* included_clib_string_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */