Lines Matching +full:byte +full:- +full:len

1 // SPDX-License-Identifier: GPL-2.0-only
14 * asn1_encode_integer() - encode positive integer to ASN.1
16 * @end_data: end of data pointer, points one beyond last usable byte in @data
27 int data_len = end_data - data; in asn1_encode_integer()
34 return ERR_PTR(-EINVAL); in asn1_encode_integer()
41 return ERR_PTR(-EINVAL); in asn1_encode_integer()
44 data_len -= 2; in asn1_encode_integer()
52 for (i = sizeof(integer); i > 0 ; i--) { in asn1_encode_integer()
53 int byte = integer >> (8 * (i - 1)); in asn1_encode_integer() local
55 if (!found && byte == 0) in asn1_encode_integer()
59 * for a positive number the first byte must have bit in asn1_encode_integer()
64 if (!found && (byte & 0x80)) { in asn1_encode_integer()
67 * have len >= 1 in asn1_encode_integer()
70 data_len--; in asn1_encode_integer()
75 return ERR_PTR(-EINVAL); in asn1_encode_integer()
77 *d++ = byte; in asn1_encode_integer()
78 data_len--; in asn1_encode_integer()
82 data[1] = d - data - 2; in asn1_encode_integer()
96 return -EINVAL; in asn1_encode_oid_digit()
101 (*data_len)--; in asn1_encode_oid_digit()
106 start -= 7; in asn1_encode_oid_digit()
109 u8 byte; in asn1_encode_oid_digit() local
111 byte = oid >> start; in asn1_encode_oid_digit()
112 oid = oid - (byte << start); in asn1_encode_oid_digit()
113 start -= 7; in asn1_encode_oid_digit()
114 byte |= 0x80; in asn1_encode_oid_digit()
115 *data++ = byte; in asn1_encode_oid_digit()
116 (*data_len)--; in asn1_encode_oid_digit()
121 (*data_len)--; in asn1_encode_oid_digit()
123 ret = -EINVAL; in asn1_encode_oid_digit()
132 * asn1_encode_oid() - encode an oid to ASN.1
134 * @end_data: end of data pointer, points one beyond last usable byte in @data
144 int data_len = end_data - data; in asn1_encode_oid()
149 return ERR_PTR(-EINVAL); in asn1_encode_oid()
152 return ERR_PTR(-EINVAL); in asn1_encode_oid()
160 return ERR_PTR(-EINVAL); in asn1_encode_oid()
165 data_len -= 3; in asn1_encode_oid()
173 data[1] = d - data - 2; in asn1_encode_oid()
180 * asn1_encode_length() - encode a length to follow an ASN.1 tag
183 * @len: length to encode
189 * sequence will be re-encoded when the length is known.
191 static int asn1_encode_length(unsigned char **data, int *data_len, int len) in asn1_encode_length() argument
194 return -EINVAL; in asn1_encode_length()
196 if (len < 0) { in asn1_encode_length()
198 (*data_len)--; in asn1_encode_length()
202 if (len <= 0x7f) { in asn1_encode_length()
203 *((*data)++) = len; in asn1_encode_length()
204 (*data_len)--; in asn1_encode_length()
209 return -EINVAL; in asn1_encode_length()
211 if (len <= 0xff) { in asn1_encode_length()
213 *((*data)++) = len & 0xff; in asn1_encode_length()
214 *data_len -= 2; in asn1_encode_length()
219 return -EINVAL; in asn1_encode_length()
221 if (len <= 0xffff) { in asn1_encode_length()
223 *((*data)++) = (len >> 8) & 0xff; in asn1_encode_length()
224 *((*data)++) = len & 0xff; in asn1_encode_length()
225 *data_len -= 3; in asn1_encode_length()
229 if (WARN(len > 0xffffff, "ASN.1 length can't be > 0xffffff")) in asn1_encode_length()
230 return -EINVAL; in asn1_encode_length()
233 return -EINVAL; in asn1_encode_length()
235 *((*data)++) = (len >> 16) & 0xff; in asn1_encode_length()
236 *((*data)++) = (len >> 8) & 0xff; in asn1_encode_length()
237 *((*data)++) = len & 0xff; in asn1_encode_length()
238 *data_len -= 4; in asn1_encode_length()
244 * asn1_encode_tag() - add a tag for optional or explicit value
246 * @end_data: end of data pointer, points one beyond last usable byte in @data
249 * @len: the length of the data to be tagged
259 * To encode in place pass a NULL @string and -1 for @len and the
267 * returned it and still NULL for @string but the real length in @len.
271 u32 tag, const unsigned char *string, int len) in asn1_encode_tag() argument
273 int data_len = end_data - data; in asn1_encode_tag()
277 return ERR_PTR(-EINVAL); in asn1_encode_tag()
279 if (!string && WARN(len > 127, in asn1_encode_tag()
281 return ERR_PTR(-EINVAL); in asn1_encode_tag()
286 if (!string && len > 0) { in asn1_encode_tag()
292 data -= 2; in asn1_encode_tag()
297 return ERR_PTR(-EINVAL); in asn1_encode_tag()
300 data_len--; in asn1_encode_tag()
301 ret = asn1_encode_length(&data, &data_len, len); in asn1_encode_tag()
308 if (data_len < len) in asn1_encode_tag()
309 return ERR_PTR(-EINVAL); in asn1_encode_tag()
311 memcpy(data, string, len); in asn1_encode_tag()
312 data += len; in asn1_encode_tag()
319 * asn1_encode_octet_string() - encode an ASN.1 OCTET STRING
321 * @end_data: end of data pointer, points one beyond last usable byte in @data
323 * @len: length of string
330 const unsigned char *string, u32 len) in asn1_encode_octet_string() argument
332 int data_len = end_data - data; in asn1_encode_octet_string()
340 return ERR_PTR(-EINVAL); in asn1_encode_octet_string()
343 data_len--; in asn1_encode_octet_string()
345 ret = asn1_encode_length(&data, &data_len, len); in asn1_encode_octet_string()
349 if (data_len < len) in asn1_encode_octet_string()
350 return ERR_PTR(-EINVAL); in asn1_encode_octet_string()
352 memcpy(data, string, len); in asn1_encode_octet_string()
353 data += len; in asn1_encode_octet_string()
360 * asn1_encode_sequence() - wrap a byte stream in an ASN.1 SEQUENCE
362 * @end_data: end of data pointer, points one beyond last usable byte in @data
364 * @len: length of the data to be encoded as a sequence
366 * Fill in a sequence. To encode in place, pass NULL for @seq and -1
367 * for @len; then call again once the length is known (still with NULL
374 const unsigned char *seq, int len) in asn1_encode_sequence() argument
376 int data_len = end_data - data; in asn1_encode_sequence()
379 if (!seq && WARN(len > 127, in asn1_encode_sequence()
381 return ERR_PTR(-EINVAL); in asn1_encode_sequence()
386 if (!seq && len >= 0) { in asn1_encode_sequence()
392 data -= 2; in asn1_encode_sequence()
397 return ERR_PTR(-EINVAL); in asn1_encode_sequence()
400 data_len--; in asn1_encode_sequence()
402 ret = asn1_encode_length(&data, &data_len, len); in asn1_encode_sequence()
409 if (data_len < len) in asn1_encode_sequence()
410 return ERR_PTR(-EINVAL); in asn1_encode_sequence()
412 memcpy(data, seq, len); in asn1_encode_sequence()
413 data += len; in asn1_encode_sequence()
420 * asn1_encode_boolean() - encode a boolean value to ASN.1
422 * @end_data: end of data pointer, points one beyond last usable byte in @data
429 int data_len = end_data - data; in asn1_encode_boolean()
436 return ERR_PTR(-EINVAL); in asn1_encode_boolean()
439 data_len--; in asn1_encode_boolean()