1 |
/* Include file for encryption routines. |
2 |
* |
3 |
* IRC Services is copyright (c) 1996-2009 Andrew Church. |
4 |
* E-mail: <achurch@achurch.org> |
5 |
* Parts written by Andrew Kempe and others. |
6 |
* This program is free but copyrighted software; see the file GPL.txt for |
7 |
* details. |
8 |
*/ |
9 |
|
10 |
#ifndef ENCRYPT_H |
11 |
#define ENCRYPT_H |
12 |
|
13 |
/*************************************************************************/ |
14 |
|
15 |
/* Structure encapsulating a password and the type of encryption used to |
16 |
* encrypt it. */ |
17 |
|
18 |
typedef struct { |
19 |
char password[PASSMAX]; /* The password itself, possibly encrypted */ |
20 |
const char *cipher; /* Encryption cipher name, or NULL for none */ |
21 |
} Password; |
22 |
|
23 |
/*************************************************************************/ |
24 |
|
25 |
/* High-level password manipulation functions. */ |
26 |
|
27 |
|
28 |
/* Allocate and return a new, empty Password structure. Always succeeds |
29 |
* (smalloc() will throw a signal if memory cannot be allocated). */ |
30 |
extern Password *new_password(void); |
31 |
|
32 |
/* Initialize a preallocated Password structure. Identical in behavior to |
33 |
* new_password(), except that the passed-in structure is used instead of |
34 |
* allocating a new one, and the structure pointer is not returned. */ |
35 |
extern void init_password(Password *password); |
36 |
|
37 |
/* Set the contents of a Password structure to the given values. If |
38 |
* cipher is not NULL, a copy of it is made, so the original string may be |
39 |
* disposed of after calling set_password(). */ |
40 |
extern void set_password(Password *password, |
41 |
const char password_buffer[PASSMAX], |
42 |
const char *cipher); |
43 |
|
44 |
/* Copy the contents of a Password structure to another Password structure. |
45 |
* The destination password comes first, a la memcpy(). */ |
46 |
extern void copy_password(Password *to, const Password *from); |
47 |
|
48 |
/* Clear and free memory used by the contents of a Password structure, |
49 |
* without freeing the structure itself. Similar to init_password(), but |
50 |
* assumes that the contents of the Password structure are valid (in |
51 |
* particular, assumes that password->cipher needs to be freed if it is not |
52 |
* NULL). */ |
53 |
extern void clear_password(Password *password); |
54 |
|
55 |
/* Free a Password structure allocated with new_password(). Does nothing |
56 |
* if NULL is given. */ |
57 |
extern void free_password(Password *password); |
58 |
|
59 |
/* Encrypt string `plaintext' of length `len', placing the result in |
60 |
* `password'. Returns: |
61 |
* 0 on success |
62 |
* -2 if the encrypted password is too long to fit in the buffer |
63 |
* -1 on other error */ |
64 |
extern int encrypt_password(const char *plaintext, int len, |
65 |
Password *password); |
66 |
|
67 |
/* Decrypt `password' into buffer `dest' of length `size'. Returns: |
68 |
* 0 on success |
69 |
* +N if the destination buffer is too small; N is the minimum size |
70 |
* buffer required to hold the decrypted password |
71 |
* -2 if the encryption algorithm does not allow decryption |
72 |
* -1 on other error */ |
73 |
extern int decrypt_password(const Password *password, char *dest, int size); |
74 |
|
75 |
/* Check an input password `plaintext' against a stored, encrypted password |
76 |
* `password'. Return value is: |
77 |
* 1 if the password matches |
78 |
* 0 if the password does not match |
79 |
* -1 if an error occurred while checking */ |
80 |
extern int check_password(const char *plaintext, const Password *password); |
81 |
|
82 |
/*************************************************************************/ |
83 |
|
84 |
/* Low-level encryption/decryption functions. Each encryption module must |
85 |
* implement all of these functions. */ |
86 |
|
87 |
|
88 |
/* encrypt(): Encrypt `src' of length `len' into `dest' of size `size'. |
89 |
* Returns: |
90 |
* 0 on success |
91 |
* +N if the destination buffer is too small; N is the minimum size |
92 |
* buffer required to hold the encrypted text |
93 |
* -1 on other error */ |
94 |
typedef int (*encrypt_func_t)(const char *src, int len, char *dest, int size); |
95 |
|
96 |
/* Decrypt `src' into buffer `dest' of size `size'. Returns: |
97 |
* 0 on success |
98 |
* +N if the destination buffer is too small; N is the minimum size |
99 |
* buffer required to hold the decrypted text |
100 |
* -2 if the encryption algorithm does not allow decryption |
101 |
* -1 on other error */ |
102 |
typedef int (*decrypt_func_t)(const char *src, char *dest, int size); |
103 |
|
104 |
/* Check an input password `plaintext' against a stored, encrypted password |
105 |
* `password'. Return value is: |
106 |
* 1 if the password matches |
107 |
* 0 if the password does not match |
108 |
* -1 if an error occurred while checking */ |
109 |
typedef int (*check_password_func_t)(const char *plaintext, |
110 |
const char *password); |
111 |
|
112 |
/*************************************************************************/ |
113 |
|
114 |
/* Registration and de-registration of ciphers (encryption modules). */ |
115 |
|
116 |
typedef struct cipherinfo_ CipherInfo; |
117 |
struct cipherinfo_ { |
118 |
CipherInfo *next, *prev; /* Internal use only */ |
119 |
const char *name; /* Cipher name (use the module name) */ |
120 |
encrypt_func_t encrypt; /* Cipher functions */ |
121 |
decrypt_func_t decrypt; |
122 |
check_password_func_t check_password; |
123 |
}; |
124 |
|
125 |
/* Register a new cipher. */ |
126 |
extern void register_cipher(CipherInfo *ci); |
127 |
|
128 |
/* Unregister a cipher. Does nothing if the cipher was not registered. */ |
129 |
extern void unregister_cipher(CipherInfo *ci); |
130 |
|
131 |
/*************************************************************************/ |
132 |
|
133 |
#endif /* ENCRYPT_H */ |
134 |
|
135 |
/* |
136 |
* Local variables: |
137 |
* c-file-style: "stroustrup" |
138 |
* c-file-offsets: ((case-label . *) (statement-case-intro . *)) |
139 |
* indent-tabs-mode: nil |
140 |
* End: |
141 |
* |
142 |
* vim: expandtab shiftwidth=4: |
143 |
*/ |