| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: crypt.h,v 1.4 1998/01/12 16:45:57 mdw Exp $ |
| 4 | * |
| 5 | * Cryptographic transfer of `become' requests |
| 6 | * |
| 7 | * (c) 1998 EBI |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of `become' |
| 13 | * |
| 14 | * `Become' is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * `Become' is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with `become'; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Revision history --------------------------------------------------* |
| 30 | * |
| 31 | * $Log: crypt.h,v $ |
| 32 | * Revision 1.4 1998/01/12 16:45:57 mdw |
| 33 | * Fix copyright date. |
| 34 | * |
| 35 | * Revision 1.3 1997/09/26 09:14:58 mdw |
| 36 | * Merged blowfish branch into trunk. |
| 37 | * |
| 38 | * Revision 1.2.2.1 1997/09/26 09:08:04 mdw |
| 39 | * Use the Blowfish encryption algorithm instead of IDEA. This is partly |
| 40 | * because I prefer Blowfish (without any particularly strong evidence) but |
| 41 | * mainly because IDEA is patented and Blowfish isn't. |
| 42 | * |
| 43 | * Revision 1.2 1997/08/04 10:24:21 mdw |
| 44 | * Sources placed under CVS control. |
| 45 | * |
| 46 | * Revision 1.1 1997/07/21 13:47:51 mdw |
| 47 | * Initial revision |
| 48 | * |
| 49 | */ |
| 50 | |
| 51 | #ifndef CRYPT_H |
| 52 | #define CRYPT_H |
| 53 | |
| 54 | #ifdef __cplusplus |
| 55 | extern "C" { |
| 56 | #endif |
| 57 | |
| 58 | /*----- Required headers --------------------------------------------------*/ |
| 59 | |
| 60 | #include <string.h> |
| 61 | |
| 62 | #ifndef BECOME_H |
| 63 | # include "become.h" |
| 64 | #endif |
| 65 | |
| 66 | #ifndef CONFIG_H |
| 67 | # include "config.h" |
| 68 | #endif |
| 69 | |
| 70 | /*----- Type definitions and data structures ------------------------------*/ |
| 71 | |
| 72 | /* --- Encryption formats --- */ |
| 73 | |
| 74 | enum { |
| 75 | cryptType_blowfish, /* Symmetric Blowfish encryption */ |
| 76 | cryptType_rsa /* Public key RSA (later project) */ |
| 77 | }; |
| 78 | |
| 79 | /* --- Blowfish has a variable key size --- * |
| 80 | * |
| 81 | * Fix a key size here. |
| 82 | */ |
| 83 | |
| 84 | #define BLOWFISH_KEYSIZE (16u) |
| 85 | |
| 86 | /* --- Encrypted buffer format --- * |
| 87 | * |
| 88 | * C structures are no good here. Time for some explicit offsets. |
| 89 | */ |
| 90 | |
| 91 | enum { |
| 92 | crq_cryptType = 0, /* Encryption type (1 byte) */ |
| 93 | crq_iv = crq_cryptType + 1, /* Plaintext IV (8 bytes) */ |
| 94 | crq_session = crq_iv + 8, /* Session key (16 bytes) */ |
| 95 | crq_cipher = crq_session + 16, /* Where to start encrypting */ |
| 96 | crq_time = crq_cipher, /* Time stamp (4 bytes) */ |
| 97 | crq_pid = crq_time + 4, /* Process ID (4 bytes) */ |
| 98 | crq_from = crq_pid + 4, /* From user id (4 bytes) */ |
| 99 | crq_to = crq_from + 4, /* To user id (4 bytes) */ |
| 100 | crq_cmd = crq_to + 4, /* Command string (lots of bytes) */ |
| 101 | crq_check = crq_cmd + CMDLEN_MAX, /* Checksum for request (4 bytes) */ |
| 102 | crq_size = crq_check + 4 /* Size of encrypted request */ |
| 103 | }; |
| 104 | |
| 105 | /* --- Encrypted result format --- */ |
| 106 | |
| 107 | enum { |
| 108 | crp_iv = 0, /* Plaintext IV (8 bytes) */ |
| 109 | crp_cipher = crp_iv + 8, /* Where to start encrypting */ |
| 110 | crp_time = crp_cipher, /* Time of request (4 bytes) */ |
| 111 | crp_pid = crp_time + 4, /* Process ID of client (4 bytes) */ |
| 112 | crp_answer = crp_pid + 4, /* Answer (1 or 0) (1 byte) */ |
| 113 | crp_check = crp_answer + 1, /* Checksum for reply (4 bytes) */ |
| 114 | crp_size = crp_check + 4 /* Size of encrypted reply */ |
| 115 | }; |
| 116 | |
| 117 | /*----- Functions provided ------------------------------------------------*/ |
| 118 | |
| 119 | /* --- @crypt_packRequest@ --- * |
| 120 | * |
| 121 | * Arguments: @request *rq@ = pointer to request block |
| 122 | * @unsigned char *buff@ = pointer to a buffer |
| 123 | * @time_t t@ = the current time |
| 124 | * @pid_t pid@ = my process ID |
| 125 | * @unsigned char *k@ = pointer to 128-bit key |
| 126 | * @unsigned char *sk@ = where to put the session key |
| 127 | * |
| 128 | * Returns: The number of bytes written. |
| 129 | * |
| 130 | * Use: Packs a request block into a buffer. The buffer should have |
| 131 | * space for at least @crq_size@ bytes. The buffer comes back |
| 132 | * encrypted and ready to send. |
| 133 | */ |
| 134 | |
| 135 | extern void crypt_packRequest(request */*rq*/, unsigned char */*buff*/, |
| 136 | time_t /*t*/, pid_t /*pid*/, |
| 137 | unsigned char */*k*/, unsigned char */*sk*/); |
| 138 | |
| 139 | /* --- @crypt_unpackRequest@ --- * |
| 140 | * |
| 141 | * Arguments: @reqest *rq@ = pointer to destination request block |
| 142 | * @unsigned char *buff@ = pointer to source buffer |
| 143 | * @unsigned char *k@ = pointer to encryption key |
| 144 | * @unsigned char *sk@ = pointer to where to store session key |
| 145 | * @unsigned char *rpl@ = where to start building reply |
| 146 | * |
| 147 | * Returns: --- |
| 148 | * |
| 149 | * Use: Decrypts and unpacks a request buffer. |
| 150 | */ |
| 151 | |
| 152 | extern int crypt_unpackRequest(request */*rq*/, unsigned char */*buff*/, |
| 153 | unsigned char */*k*/, unsigned char */*sk*/, |
| 154 | unsigned char */*rpl*/); |
| 155 | |
| 156 | /* --- @crypt_packReply@ --- * |
| 157 | * |
| 158 | * Arguments: @unsigned char *buff@ = pointer to reply block |
| 159 | * @unsigned char *sk@ = pointer to session key |
| 160 | * @int answer@ = yes or no |
| 161 | * |
| 162 | * Returns: --- |
| 163 | * |
| 164 | * Use: Packs and encrypts a reply block. |
| 165 | */ |
| 166 | |
| 167 | extern void crypt_packReply(unsigned char */*buff*/, unsigned char */*sk*/, |
| 168 | int /*answer*/); |
| 169 | |
| 170 | /* --- @crypt_unpackReply@ --- * |
| 171 | * |
| 172 | * Arguments: @unsigned char *buff@ = pointer to reply buffer |
| 173 | * @unsigned char *sk@ = pointer to session key |
| 174 | * @time_t t@ = time at which request was sent |
| 175 | * @pid_t pid@ = my process ID |
| 176 | * |
| 177 | * Returns: >0 if request granted, zero if denied, <0 if reply rejected |
| 178 | * |
| 179 | * Use: Unpacks a reply block, and informs the caller of the outcome. |
| 180 | */ |
| 181 | |
| 182 | extern int crypt_unpackReply(unsigned char */*buff*/, unsigned char */*sk*/, |
| 183 | time_t /*t*/, pid_t /*pid*/); |
| 184 | |
| 185 | /*----- That's all, folks -------------------------------------------------*/ |
| 186 | |
| 187 | #ifdef __cplusplus |
| 188 | } |
| 189 | #endif |
| 190 | |
| 191 | #endif |