comparison libtomcrypt/src/pk/asn1/der/utf8/der_length_utf8_string.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 #include "tomcrypt.h" 9 #include "tomcrypt.h"
12 10
13 /** 11 /**
14 @file der_length_utf8_string.c 12 @file der_length_utf8_string.c
25 { 23 {
26 if (c <= 0x7F) { 24 if (c <= 0x7F) {
27 return 1; 25 return 1;
28 } else if (c <= 0x7FF) { 26 } else if (c <= 0x7FF) {
29 return 2; 27 return 2;
28 #if LTC_WCHAR_MAX == 0xFFFF
29 } else {
30 return 3;
31 }
32 #else
30 } else if (c <= 0xFFFF) { 33 } else if (c <= 0xFFFF) {
31 return 3; 34 return 3;
32 } else { 35 } else {
33 return 4; 36 return 4;
34 } 37 }
38 #endif
35 } 39 }
36 40
37 /** 41 /**
38 Gets length of DER encoding of UTF8 STRING 42 Test whether the given code point is valid character
43 @param c The UTF-8 character to test
44 @return 1 - valid, 0 - invalid
45 */
46 int der_utf8_valid_char(const wchar_t c)
47 {
48 LTC_UNUSED_PARAM(c);
49 #if !defined(LTC_WCHAR_MAX) || LTC_WCHAR_MAX > 0xFFFF
50 if (c > 0x10FFFF) return 0;
51 #endif
52 #if LTC_WCHAR_MAX != 0xFFFF && LTC_WCHAR_MAX != 0xFFFFFFFF
53 if (c < 0) return 0;
54 #endif
55 return 1;
56 }
57
58 /**
59 Gets length of DER encoding of UTF8 STRING
39 @param in The characters to measure the length of 60 @param in The characters to measure the length of
40 @param noctets The number of octets in the string to encode 61 @param noctets The number of octets in the string to encode
41 @param outlen [out] The length of the DER encoding for the given string 62 @param outlen [out] The length of the DER encoding for the given string
42 @return CRYPT_OK if successful 63 @return CRYPT_OK if successful
43 */ 64 */
48 LTC_ARGCHK(in != NULL); 69 LTC_ARGCHK(in != NULL);
49 LTC_ARGCHK(outlen != NULL); 70 LTC_ARGCHK(outlen != NULL);
50 71
51 len = 0; 72 len = 0;
52 for (x = 0; x < noctets; x++) { 73 for (x = 0; x < noctets; x++) {
53 if (in[x] < 0 || in[x] > 0x10FFFF) { 74 if (!der_utf8_valid_char(in[x])) return CRYPT_INVALID_ARG;
54 return CRYPT_INVALID_ARG;
55 }
56 len += der_utf8_charsize(in[x]); 75 len += der_utf8_charsize(in[x]);
57 } 76 }
58 77
59 if (len < 128) { 78 if (len < 128) {
60 /* 0C LL DD DD DD ... */ 79 /* 0C LL DD DD DD ... */
76 } 95 }
77 96
78 #endif 97 #endif
79 98
80 99
81 /* $Source$ */ 100 /* ref: $Format:%D$ */
82 /* $Revision$ */ 101 /* git commit: $Format:%H$ */
83 /* $Date$ */ 102 /* commit time: $Format:%ai$ */