2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2008, 2010 Free Software
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GNUTLS.
9 * The GNUTLS library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 #include <gnutls_int.h>
27 #include <gnutls_errors.h>
28 #include <gnutls_datum.h>
29 #include <auth_srp_passwd.h>
33 /* this a modified base64 for srp !!!
34 * It seems that everybody makes an own base64 conversion.
36 static const uint8_t b64table
[] =
37 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
39 static const uint8_t asciitable
[128] = {
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
46 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
47 0xff, 0xff, 0xff, 0xff, 0x3e, 0x3f,
48 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
49 0x06, 0x07, 0x08, 0x09, 0xff, 0xff,
50 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a,
51 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
52 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
53 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c,
54 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22,
55 0x23, 0xff, 0xff, 0xff, 0xff, 0xff,
56 0xff, 0x24, 0x25, 0x26, 0x27, 0x28,
57 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
58 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
59 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a,
60 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff,
65 encode (uint8_t * result
, const uint8_t * rdata
, int left
)
77 data
[0] = data
[1] = data
[2] = 0;
78 memcpy (data
, rdata
, data_len
);
83 result
[0] = b64table
[((data
[0] & 0xfc) >> 2)];
85 b64table
[(((((data
[0] & 0x03) & 0xff) << 4) & 0xff) |
86 ((data
[1] & 0xf0) >> 4))];
88 b64table
[((((data
[1] & 0x0f) << 2) & 0xff) |
89 ((data
[2] & 0xc0) >> 6))];
90 result
[3] = b64table
[(data
[2] & 0x3f) & 0xff];
93 if ((c
= ((data
[0] & 0xf0) >> 4)) != 0)
95 result
[0] = b64table
[c
];
97 b64table
[((((data
[0] & 0x0f) << 2) & 0xff) |
98 ((data
[1] & 0xc0) >> 6))];
99 result
[2] = b64table
[(data
[1] & 0x3f) & 0xff];
105 if ((c
= ((data
[0] & 0x0f) << 2) | ((data
[1] & 0xc0) >> 6)) != 0)
107 result
[0] = b64table
[c
];
108 result
[1] = b64table
[data
[1] & 0x3f];
115 result
[0] = b64table
[data
[0] & 0x3f];
124 if ((c
= ((data
[0] & 0xc0) >> 6)) != 0)
126 result
[0] = b64table
[c
];
127 result
[1] = b64table
[(data
[0] & 0x3f) & 0xff];
134 result
[0] = b64table
[(data
[0] & 0x3f) & 0xff];
149 /* encodes data and puts the result into result (locally allocated)
150 * The result_size is the return value
153 _gnutls_sbase64_encode (uint8_t * data
, size_t data_size
, uint8_t ** result
)
158 int mod
= data_size
% 3;
166 ret
+= (data_size
* 4) / 3;
168 (*result
) = gnutls_calloc (1, ret
+ 1);
169 if ((*result
) == NULL
)
173 /* encode the bytes that are not a multiple of 3
177 tmp
= encode (tmpres
, &data
[0], mod
);
180 gnutls_free ((*result
));
184 memcpy (&(*result
)[0], tmpres
, tmp
);
191 for (; i
< data_size
; i
+= 3, j
+= 4)
193 tmp
= encode (tmpres
, &data
[i
], data_size
- i
);
196 gnutls_free ((*result
));
199 memcpy (&(*result
)[j
], tmpres
, tmp
);
202 return strlen (*result
);
206 /* data must be 4 bytes
207 * result should be 3 bytes
209 #define TOASCII(c) (c < 127 ? asciitable[c] : 0xff)
211 decode (uint8_t * result
, const uint8_t * data
)
216 memset (result
, 0, 3);
218 a1
= TOASCII (data
[3]);
219 a2
= TOASCII (data
[2]);
221 result
[2] = a1
& 0xff;
225 result
[2] |= ((a2
& 0x03) << 6) & 0xff;
228 a2
= TOASCII (data
[1]);
230 result
[1] = ((a1
& 0x3c) >> 2);
232 result
[1] |= ((a2
& 0x0f) << 4);
233 else if (a1
== 0xff || result
[1] == 0)
237 a2
= TOASCII (data
[0]);
239 result
[0] = (((a1
& 0x30) >> 4) & 0xff);
241 result
[0] |= ((a2
<< 2) & 0xff);
242 else if (a1
== 0xff || result
[0] == 0)
248 /* decodes data and puts the result into result (locally allocated)
249 * The result_size is the return value.
250 * That function does not ignore newlines tabs etc. You should remove them
254 _gnutls_sbase64_decode (uint8_t * data
, size_t idata_size
, uint8_t ** result
)
262 data_size
= (idata_size
/ 4) * 4;
263 left
= idata_size
% 4;
265 ret
= (data_size
/ 4) * 3;
270 (*result
) = gnutls_malloc (ret
+ 1);
271 if ((*result
) == NULL
)
274 /* the first "block" is treated with special care */
278 memset (datrev
, 0, 4);
279 memcpy (&datrev
[4 - left
], data
, left
);
281 tmp
= decode (tmpres
, datrev
);
284 gnutls_free ((*result
));
289 memcpy (*result
, &tmpres
[3 - tmp
], tmp
);
295 for (i
= left
, j
= tmp
; i
< idata_size
; i
+= 4)
297 tmp
= decode (tmpres
, &data
[i
]);
300 gnutls_free ((*result
));
304 memcpy (&(*result
)[j
], tmpres
, tmp
);
314 * gnutls_srp_base64_encode - Encode raw data using SRP-Base64
315 * @data: contain the raw data
316 * @result: the place where base64 data will be copied
317 * @result_size: holds the size of the result
319 * This function will convert the given data to printable data, using
320 * the base64 encoding, as used in the libsrp. This is the encoding
321 * used in SRP password files. If the provided buffer is not long
322 * enough GNUTLS_E_SHORT_MEMORY_BUFFER is returned.
324 * Warning! This base64 encoding is not the "standard" encoding, so
325 * do not use it for non-SRP purposes.
327 * Returns: %GNUTLS_E_SHORT_MEMORY_BUFFER if the buffer given is not
328 * long enough, or 0 on success.
331 gnutls_srp_base64_encode (const gnutls_datum_t
* data
, char *result
,
332 size_t * result_size
)
337 size
= _gnutls_sbase64_encode (data
->data
, data
->size
, &ret
);
341 if (result
== NULL
|| *result_size
< (size_t) size
)
345 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
349 memcpy (result
, ret
, size
);
358 * gnutls_srp_base64_encode_alloc - encode raw data using SRP-Base64
359 * @data: contains the raw data
360 * @result: will hold the newly allocated encoded data
362 * This function will convert the given data to printable data, using
363 * the base64 encoding. This is the encoding used in SRP password
364 * files. This function will allocate the required memory to hold
367 * You should use gnutls_free() to free the returned data.
369 * Warning! This base64 encoding is not the "standard" encoding, so
370 * do not use it for non-SRP purposes.
372 * Returns: 0 on success, or an error code.
375 gnutls_srp_base64_encode_alloc (const gnutls_datum_t
* data
,
376 gnutls_datum_t
* result
)
381 size
= _gnutls_sbase64_encode (data
->data
, data
->size
, &ret
);
388 return GNUTLS_E_INVALID_REQUEST
;
400 * gnutls_srp_base64_decode - decode SRP-base64 encoded data
401 * @b64_data: contain the encoded data
402 * @result: the place where decoded data will be copied
403 * @result_size: holds the size of the result
405 * This function will decode the given encoded data, using the base64
406 * encoding found in libsrp.
408 * Note that b64_data should be null terminated.
410 * Warning! This base64 encoding is not the "standard" encoding, so
411 * do not use it for non-SRP purposes.
413 * Returns: %GNUTLS_E_SHORT_MEMORY_BUFFER if the buffer given is not
414 * long enough, or 0 on success.
417 gnutls_srp_base64_decode (const gnutls_datum_t
* b64_data
, char *result
,
418 size_t * result_size
)
423 size
= _gnutls_sbase64_decode (b64_data
->data
, b64_data
->size
, &ret
);
427 if (result
== NULL
|| *result_size
< (size_t) size
)
431 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
435 memcpy (result
, ret
, size
);
444 * gnutls_srp_base64_decode_alloc - decode SRP-base64 encoded data
445 * @b64_data: contains the encoded data
446 * @result: the place where decoded data lie
448 * This function will decode the given encoded data. The decoded data
449 * will be allocated, and stored into result. It will decode using
450 * the base64 algorithm as used in libsrp.
452 * You should use gnutls_free() to free the returned data.
454 * Warning! This base64 encoding is not the "standard" encoding, so
455 * do not use it for non-SRP purposes.
457 * Returns: 0 on success, or an error code.
460 gnutls_srp_base64_decode_alloc (const gnutls_datum_t
* b64_data
,
461 gnutls_datum_t
* result
)
466 size
= _gnutls_sbase64_decode (b64_data
->data
, b64_data
->size
, &ret
);
473 return GNUTLS_E_INVALID_REQUEST
;
484 #endif /* ENABLE_SRP */