| 1 | /* crypto.c --- Simple crypto wrappers for applications. |
| 2 | * Copyright (C) 2002-2012 Simon Josefsson |
| 3 | * |
| 4 | * This file is part of GNU SASL Library. |
| 5 | * |
| 6 | * GNU SASL Library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public License |
| 8 | * as published by the Free Software Foundation; either version 2.1 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * GNU SASL Library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License License along with GNU SASL Library; if not, write to the |
| 18 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 | * Boston, MA 02110-1301, USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include "internal.h" |
| 24 | |
| 25 | #include "gc.h" |
| 26 | |
| 27 | /** |
| 28 | * gsasl_nonce: |
| 29 | * @data: output array to be filled with unpredictable random data. |
| 30 | * @datalen: size of output array. |
| 31 | * |
| 32 | * Store unpredictable data of given size in the provided buffer. |
| 33 | * |
| 34 | * Return value: Returns %GSASL_OK iff successful. |
| 35 | **/ |
| 36 | int |
| 37 | gsasl_nonce (char *data, size_t datalen) |
| 38 | { |
| 39 | return gc_nonce (data, datalen); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * gsasl_random: |
| 44 | * @data: output array to be filled with strong random data. |
| 45 | * @datalen: size of output array. |
| 46 | * |
| 47 | * Store cryptographically strong random data of given size in the |
| 48 | * provided buffer. |
| 49 | * |
| 50 | * Return value: Returns %GSASL_OK iff successful. |
| 51 | **/ |
| 52 | int |
| 53 | gsasl_random (char *data, size_t datalen) |
| 54 | { |
| 55 | return gc_random (data, datalen); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * gsasl_md5: |
| 60 | * @in: input character array of data to hash. |
| 61 | * @inlen: length of input character array of data to hash. |
| 62 | * @out: newly allocated character array with hash of data. |
| 63 | * |
| 64 | * Compute hash of data using MD5. The @out buffer must be |
| 65 | * deallocated by the caller. |
| 66 | * |
| 67 | * Return value: Returns %GSASL_OK iff successful. |
| 68 | **/ |
| 69 | int |
| 70 | gsasl_md5 (const char *in, size_t inlen, char *out[16]) |
| 71 | { |
| 72 | *out = malloc (16); |
| 73 | if (!*out) |
| 74 | return GSASL_MALLOC_ERROR; |
| 75 | return gc_md5 (in, inlen, *out); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * gsasl_hmac_md5: |
| 80 | * @key: input character array with key to use. |
| 81 | * @keylen: length of input character array with key to use. |
| 82 | * @in: input character array of data to hash. |
| 83 | * @inlen: length of input character array of data to hash. |
| 84 | * @outhash: newly allocated character array with keyed hash of data. |
| 85 | * |
| 86 | * Compute keyed checksum of data using HMAC-MD5. The @outhash buffer |
| 87 | * must be deallocated by the caller. |
| 88 | * |
| 89 | * Return value: Returns %GSASL_OK iff successful. |
| 90 | **/ |
| 91 | int |
| 92 | gsasl_hmac_md5 (const char *key, size_t keylen, |
| 93 | const char *in, size_t inlen, char *outhash[16]) |
| 94 | { |
| 95 | *outhash = malloc (16); |
| 96 | if (!*outhash) |
| 97 | return GSASL_MALLOC_ERROR; |
| 98 | return gc_hmac_md5 (key, keylen, in, inlen, *outhash); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * gsasl_sha1: |
| 103 | * @in: input character array of data to hash. |
| 104 | * @inlen: length of input character array of data to hash. |
| 105 | * @out: newly allocated character array with hash of data. |
| 106 | * |
| 107 | * Compute hash of data using SHA1. The @out buffer must be |
| 108 | * deallocated by the caller. |
| 109 | * |
| 110 | * Return value: Returns %GSASL_OK iff successful. |
| 111 | * |
| 112 | * Since: 1.3 |
| 113 | **/ |
| 114 | int |
| 115 | gsasl_sha1 (const char *in, size_t inlen, char *out[20]) |
| 116 | { |
| 117 | *out = malloc (20); |
| 118 | if (!*out) |
| 119 | return GSASL_MALLOC_ERROR; |
| 120 | return gc_sha1 (in, inlen, *out); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * gsasl_hmac_sha1: |
| 125 | * @key: input character array with key to use. |
| 126 | * @keylen: length of input character array with key to use. |
| 127 | * @in: input character array of data to hash. |
| 128 | * @inlen: length of input character array of data to hash. |
| 129 | * @outhash: newly allocated character array with keyed hash of data. |
| 130 | * |
| 131 | * Compute keyed checksum of data using HMAC-SHA1. The @outhash buffer |
| 132 | * must be deallocated by the caller. |
| 133 | * |
| 134 | * Return value: Returns %GSASL_OK iff successful. |
| 135 | * |
| 136 | * Since: 1.3 |
| 137 | **/ |
| 138 | int |
| 139 | gsasl_hmac_sha1 (const char *key, size_t keylen, |
| 140 | const char *in, size_t inlen, char *outhash[20]) |
| 141 | { |
| 142 | *outhash = malloc (20); |
| 143 | if (!*outhash) |
| 144 | return GSASL_MALLOC_ERROR; |
| 145 | return gc_hmac_sha1 (key, keylen, in, inlen, *outhash); |
| 146 | } |
| 147 | |