3 * $Id: key-pass.c,v 1.1 1999/12/22 15:47:48 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.1 1999/12/22 15:47:48 mdw
34 * Major key-management revision.
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/dstr.h>
44 #include "passphrase.h"
47 #include "blowfish-cbc.h"
50 /*----- Main code ---------------------------------------------------------*/
52 /* --- @key_plock@ --- *
54 * Arguments: @const char *tag@ = tag to use for passphrase
55 * @key_data *k@ = source key data block
56 * @key_data *kt@ = target key data block
58 * Returns: Zero if successful, nonzero if there was a problem.
60 * Use: Locks a key by encrypting it with a passphrase.
63 int key_plock(const char *tag
, key_data
*k
, key_data
*kt
)
66 octet hash
[RMD160_HASHSZ
];
69 /* --- Sanity check --- */
71 assert(((void)"Key data is already encrypted",
72 (k
->e
& KF_ENCMASK
) != KENC_ENCRYPT
));
74 /* --- Read the passphrase --- */
76 if (passphrase_read(tag
, PMODE_VERIFY
, buf
, sizeof(buf
)))
79 /* --- Extract the key data --- *
81 * On the front, put four random bytes to act as a passphrase salt (which
82 * remain in the clear), and four zero bytes to be able to spot duff
83 * passphrases when unlocking.
87 rand_getgood(RAND_GLOBAL
, d
.buf
, 4);
88 memset(d
.buf
+ 4, 0, 4);
94 /* --- Hash the passphrase into a key --- */
99 rmd160_hash(&h
, d
.buf
, 4);
100 rmd160_hash(&h
, buf
, strlen(buf
));
101 rmd160_done(&h
, hash
);
106 /* --- Encrypt the key data --- */
110 blowfish_cbcinit(&c
, hash
, sizeof(hash
), 0);
111 blowfish_cbcencrypt(&c
, d
.buf
+ 4, d
.buf
+ 4, d
.len
- 4);
116 /* --- Put the key data back in the key --- */
118 key_encrypted(kt
, d
.buf
, d
.len
);
123 /* --- @key_punlock@ --- *
125 * Arguments: @const char *tag@ = tag to use for passphrase
126 * @key_data *k@ = source key data block
127 * @key_data *kt@ = target key data block
129 * Returns: Zero if it worked, nonzero if it didn't.
131 * Use: Unlocks a passphrase-locked key.
134 int key_punlock(const char *tag
, key_data
*k
, key_data
*kt
)
136 octet hash
[RMD160_HASHSZ
];
141 /* --- Sanity check --- */
143 assert(((void)"Key data isn't encrypted",
144 (k
->e
& KF_ENCMASK
) == KENC_ENCRYPT
));
146 /* --- Fetch the passphrase --- */
148 if (passphrase_read(tag
, PMODE_READ
, buf
, sizeof(buf
)))
151 /* --- Allocate a destination buffer --- *
153 * Minimum size for a key is four bytes salt, four bytes zeroes, two bytes
154 * type, and two bytes length, making a total of twelve.
160 p
= xmalloc(k
->u
.k
.sz
);
162 /* --- Hash the passphrase --- */
167 rmd160_hash(&h
, k
->u
.k
.k
, 4);
168 rmd160_hash(&h
, buf
, strlen(buf
));
169 rmd160_done(&h
, hash
);
174 /* --- Decrypt the key data --- */
178 blowfish_cbcinit(&c
, hash
, sizeof(hash
), 0);
179 blowfish_cbcdecrypt(&c
, k
->u
.k
.k
+ 4, p
, sz
);
182 if (LOAD32(p
) != 0) {
183 passphrase_cancel(tag
);
188 /* --- Decode the key data into the destination buffer --- */
192 passphrase_cancel(tag
);
194 if (key_decode(p
+ 4, sz
- 4, kt
))
202 /* --- Tidy up if things went wrong --- */
210 /*----- That's all, folks -------------------------------------------------*/