3 * $Id: key-pass.c,v 1.2 2000/06/17 11:26:35 mdw Exp $
5 * Encrypting keys with passphrases
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb 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 Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: key-pass.c,v $
33 * Revision 1.2 2000/06/17 11:26:35 mdw
34 * `rand_getgood' is deprecated.
36 * Revision 1.1 1999/12/22 15:47:48 mdw
37 * Major key-management revision.
41 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/dstr.h>
47 #include "passphrase.h"
50 #include "blowfish-cbc.h"
53 /*----- Main code ---------------------------------------------------------*/
55 /* --- @key_plock@ --- *
57 * Arguments: @const char *tag@ = tag to use for passphrase
58 * @key_data *k@ = source key data block
59 * @key_data *kt@ = target key data block
61 * Returns: Zero if successful, nonzero if there was a problem.
63 * Use: Locks a key by encrypting it with a passphrase.
66 int key_plock(const char *tag
, key_data
*k
, key_data
*kt
)
69 octet hash
[RMD160_HASHSZ
];
72 /* --- Sanity check --- */
74 assert(((void)"Key data is already encrypted",
75 (k
->e
& KF_ENCMASK
) != KENC_ENCRYPT
));
77 /* --- Read the passphrase --- */
79 if (passphrase_read(tag
, PMODE_VERIFY
, buf
, sizeof(buf
)))
82 /* --- Extract the key data --- *
84 * On the front, put four random bytes to act as a passphrase salt (which
85 * remain in the clear), and four zero bytes to be able to spot duff
86 * passphrases when unlocking.
90 rand_get(RAND_GLOBAL
, d
.buf
, 4);
91 memset(d
.buf
+ 4, 0, 4);
97 /* --- Hash the passphrase into a key --- */
102 rmd160_hash(&h
, d
.buf
, 4);
103 rmd160_hash(&h
, buf
, strlen(buf
));
104 rmd160_done(&h
, hash
);
109 /* --- Encrypt the key data --- */
113 blowfish_cbcinit(&c
, hash
, sizeof(hash
), 0);
114 blowfish_cbcencrypt(&c
, d
.buf
+ 4, d
.buf
+ 4, d
.len
- 4);
119 /* --- Put the key data back in the key --- */
121 key_encrypted(kt
, d
.buf
, d
.len
);
126 /* --- @key_punlock@ --- *
128 * Arguments: @const char *tag@ = tag to use for passphrase
129 * @key_data *k@ = source key data block
130 * @key_data *kt@ = target key data block
132 * Returns: Zero if it worked, nonzero if it didn't.
134 * Use: Unlocks a passphrase-locked key.
137 int key_punlock(const char *tag
, key_data
*k
, key_data
*kt
)
139 octet hash
[RMD160_HASHSZ
];
144 /* --- Sanity check --- */
146 assert(((void)"Key data isn't encrypted",
147 (k
->e
& KF_ENCMASK
) == KENC_ENCRYPT
));
149 /* --- Fetch the passphrase --- */
151 if (passphrase_read(tag
, PMODE_READ
, buf
, sizeof(buf
)))
154 /* --- Allocate a destination buffer --- *
156 * Minimum size for a key is four bytes salt, four bytes zeroes, two bytes
157 * type, and two bytes length, making a total of twelve.
163 p
= xmalloc(k
->u
.k
.sz
);
165 /* --- Hash the passphrase --- */
170 rmd160_hash(&h
, k
->u
.k
.k
, 4);
171 rmd160_hash(&h
, buf
, strlen(buf
));
172 rmd160_done(&h
, hash
);
177 /* --- Decrypt the key data --- */
181 blowfish_cbcinit(&c
, hash
, sizeof(hash
), 0);
182 blowfish_cbcdecrypt(&c
, k
->u
.k
.k
+ 4, p
, sz
);
185 if (LOAD32(p
) != 0) {
186 passphrase_cancel(tag
);
191 /* --- Decode the key data into the destination buffer --- */
195 passphrase_cancel(tag
);
197 if (key_decode(p
+ 4, sz
- 4, kt
))
205 /* --- Tidy up if things went wrong --- */
213 /*----- That's all, folks -------------------------------------------------*/