Skip to content

Commit a96997d

Browse files
karel-msjaeckel
authored andcommitted
add pkcs12_utf8_to_utf16()
1 parent b3f483a commit a96997d

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/headers/tomcrypt_private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long i
309309

310310
#ifdef LTC_PKCS_12
311311

312+
int pkcs12_utf8_to_utf16(const unsigned char *in, unsigned long inlen,
313+
unsigned char *out, unsigned long *outlen);
314+
312315
int pkcs12_kdf( int hash_id,
313316
const unsigned char *pw, unsigned long pwlen,
314317
const unsigned char *salt, unsigned long saltlen,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2+
*
3+
* LibTomCrypt is a library that provides various cryptographic
4+
* algorithms in a highly modular and flexible manner.
5+
*
6+
* The library is free for all purposes without any express
7+
* guarantee it works.
8+
*/
9+
10+
#include "tomcrypt_private.h"
11+
12+
#ifdef LTC_PKCS_12
13+
14+
int pkcs12_utf8_to_utf16(const unsigned char *in, unsigned long inlen,
15+
unsigned char *out, unsigned long *outlen) {
16+
unsigned long len = 0;
17+
const unsigned char* in_end = in + inlen;
18+
const ulong32 offset[6] = {
19+
0x00000000UL, 0x00003080UL, 0x000E2080UL,
20+
0x03C82080UL, 0xFA082080UL, 0x82082080UL
21+
};
22+
int err = CRYPT_ERROR;
23+
24+
while (in < in_end) {
25+
ulong32 ch = 0;
26+
unsigned short extra = 0; /* 0 */
27+
if (*in >= 192) extra++; /* 1 */
28+
if (*in >= 224) extra++; /* 2 */
29+
if (*in >= 240) extra++; /* 3 */
30+
if (*in >= 248) extra++; /* 4 */
31+
if (*in >= 252) extra++; /* 5 */
32+
if (in + extra >= in_end) goto ERROR;
33+
switch (extra) {
34+
case 5: ch += *in++; ch <<= 6;
35+
case 4: ch += *in++; ch <<= 6;
36+
case 3: ch += *in++; ch <<= 6;
37+
case 2: ch += *in++; ch <<= 6;
38+
case 1: ch += *in++; ch <<= 6;
39+
case 0: ch += *in++;
40+
}
41+
ch -= offset[extra];
42+
if (ch > 0xFFFF) goto ERROR;
43+
if (*outlen >= len + 2) {
44+
out[len] = (unsigned short)((ch >> 8) & 0xFF);
45+
out[len + 1] = (unsigned char)(ch & 0xFF);
46+
}
47+
len += 2;
48+
}
49+
50+
err = len > *outlen ? CRYPT_BUFFER_OVERFLOW : CRYPT_OK;
51+
*outlen = len;
52+
ERROR:
53+
return err;
54+
}
55+
56+
#endif
57+
58+
/* ref: $Format:%D$ */
59+
/* git commit: $Format:%H$ */
60+
/* commit time: $Format:%ai$ */

0 commit comments

Comments
 (0)