]>
git.saurik.com Git - apple/security.git/blob - CertTool/cdsaUtils/cuEnc64.c
1 /* Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
3 * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT
4 * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE
5 * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE COMPUTER, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE COMPUTER,
7 * INC. ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
11 * enc64.c - encode/decode in 64-char IA5 format, per RFC 1421
16 * Added ECDSA_VERIFY_ONLY dependencies.
18 * Changed to compile with C++.
19 * 12 Dec 96 Doug Mitchell at NeXT
20 * Newlines optional in dec64() and isValidEnc64().
21 * 9 Oct 96 Doug Mitchell at NeXT
29 * 11/27/98 dmitch: The ECDSA_VERIFY_ONLY symbol, when #defined, disables all
30 * of the code in this module except that which is necessary for ECDSA
31 * siggnature verification.
35 #define NULL ((void *)0)
39 * map a 6-bit binary value to a printable character.
42 unsigned char bintoasc
[] =
43 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
46 * Map an 7-bit printable character to its corresponding binary value.
47 * Any illegal characters return high bit set.
50 unsigned char asctobin
[] =
52 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
53 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
54 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
55 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
56 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
57 0x80, 0x80, 0x80, 0x3e, 0x80, 0x80, 0x80, 0x3f,
58 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
59 0x3c, 0x3d, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
60 0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
61 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
62 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
63 0x17, 0x18, 0x19, 0x80, 0x80, 0x80, 0x80, 0x80,
64 0x80, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
65 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
66 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
67 0x31, 0x32, 0x33, 0x80, 0x80, 0x80, 0x80, 0x80
71 * map 6 bits to a printing char
73 #define ENC(c) (bintoasc[((c) & 0x3f)])
78 * map one group of up to 3 bytes at inp to 4 bytes at outp.
79 * Count is number of valid bytes in *inp; if less than 3, the
80 * 1 or two extras must be zeros.
82 static void encChunk(const unsigned char *inp
,
86 unsigned char c1
, c2
, c3
, c4
;
89 c2
= ((inp
[0] << 4) & 0x30) | ((inp
[1] >> 4) & 0xf);
90 c3
= ((inp
[1] << 2) & 0x3c) | ((inp
[2] >> 6) & 0x3);
109 * Given input buffer inbuf, length inlen, encode to 64-char IA5 format.
110 * Result is fmalloc'd and returned; it is terminated by Microsoft-style
111 * newline and NULL. Its length (including the trailing newline and NULL)
112 * is returned in *outlen.
115 unsigned char *enc64(const unsigned char *inbuf
,
117 unsigned *outlen
) // RETURNED
119 return enc64WithLines(inbuf
, inlen
, 0, outlen
);
122 unsigned char *enc64WithLines(const unsigned char *inbuf
,
128 unsigned len
; // to malloc, liberal
129 unsigned olen
= 0; // actual output size
130 unsigned char *outbuf
;
131 unsigned char endbuf
[3];
137 outTextLen
= ((inlen
+ 2) / 3) * 4;
140 * linelen must be 0 mod 4 for this to work; round up...
142 if((linelen
& 0x03) != 0) {
143 linelen
= (linelen
+ 3) & 0xfffffffc;
145 numLines
= (outTextLen
+ linelen
- 1)/ linelen
;
152 * Total output size = encoded text size plus one newline per
153 * line of output, plus trailing NULL. We always generate newlines
154 * as \n; when decoding, we tolerate \r\n (Microsoft) or \n.
156 len
= outTextLen
+ (2 * numLines
) + 1;
157 outbuf
= (unsigned char*)malloc(len
);
165 endbuf
[i
] = inbuf
[i
];
171 encChunk(endbuf
, outp
, inlen
);
175 encChunk(inbuf
, outp
, 3);
182 if((linelen
!= 0) && (thisLine
>= linelen
) && inlen
) {
184 * last trailing newline added below
185 * Note we don't split 4-byte output chunks over newlines
199 static inline int isWhite(unsigned char c
)
214 * Strip off all whitespace from a (supposedly) enc64-format string.
215 * Returns a malloc'd string.
217 static unsigned char *stringCleanse(const unsigned char *inbuf
,
221 unsigned char *news
; // cleansed inbuf
222 unsigned newsDex
; // index into news
225 news
= (unsigned char*)malloc(inlen
);
227 for(i
=0; i
<inlen
; i
++) {
228 if(!isWhite(inbuf
[i
])) {
229 news
[newsDex
++] = inbuf
[i
];
237 * Given input buffer inbuf, length inlen, decode from 64-char IA5 format to
238 * binary. Result is malloced and returned; its length is returned in *outlen.
239 * NULL return indicates corrupted input.
241 * All whitespace in input is ignored.
243 unsigned char *dec64(const unsigned char *inbuf
,
247 unsigned char *outbuf
;
248 unsigned char *outp
; // malloc'd outbuf size
250 const unsigned char *bp
;
251 unsigned olen
= 0; // actual output size
252 unsigned char c1
, c2
, c3
, c4
;
255 unsigned char *news
; // cleansed inbuf
259 * Strip out all whitespace; remainder must be multiple of four
262 news
= stringCleanse(inbuf
, inlen
, &newsLen
);
263 if((newsLen
& 0x03) != 0) {
265 return (unsigned char*) NULL
;
270 obuflen
= (inlen
/ 4) * 3;
271 outbuf
= (unsigned char*)malloc(obuflen
);
276 * Note inlen is always a multiple of four here
278 if (*bp
& 0x80 || (c1
= asctobin
[*bp
]) & 0x80) {
283 if (*bp
& 0x80 || (c2
= asctobin
[*bp
]) & 0x80){
290 * two input bytes, one output byte
309 } else if (*bp
& 0x80 || (c3
= asctobin
[*bp
]) & 0x80) {
316 * Three input bytes, two output
323 } else if (*bp
& 0x80 || (c4
= asctobin
[*bp
]) & 0x80) {
327 * Normal non-pad case
334 j
= (c1
<< 2) | (c2
>> 4);
337 j
= (c2
<< 4) | (c3
>> 2);
348 return outbuf
; /* normal return */
353 return (unsigned char*) NULL
;
357 * Determine if specified input data is valid enc64 format. Returns 1
358 * if valid, 0 if not.
359 * This doesn't do a full enc64 parse job; it scans for legal characters
360 * and proper sync when a possible pad is found.
362 int isValidEnc64(const unsigned char *inbuf
,
365 int padChars
= 0; // running count of PAD chars
366 int validEncChars
= 0;
372 * -- count valid chars
373 * -- ensure not more than 2 PAD chars, only at end
374 * -- ensure valid chars mod 4 == 0
385 return 0; // max of 2 PAD chars at end
388 else if(padChars
> 0) {
389 return 0; // no normal chars after seeing PAD
391 else if((c
& 0x80) || ((asctobin
[c
]) & 0x80)) {
392 return 0; // invalid encoded char
396 if((validEncChars
& 0x03) != 0) {