3 * $Id: key-pass.c,v 1.3 2004/03/27 00:04:19 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.3 2004/03/27 00:04:19 mdw
34 * INCOMPATIBLE CHANGE. Use proper authentication on encrypted keys.
36 * Revision 1.2 2000/06/17 11:26:35 mdw
37 * `rand_getgood' is deprecated.
39 * Revision 1.1 1999/12/22 15:47:48 mdw
40 * Major key-management revision.
44 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/dstr.h>
50 #include "passphrase.h"
53 #include "blowfish-cbc.h"
55 #include "rmd160-mgf.h"
56 #include "rmd160-hmac.h"
58 /*----- Main code ---------------------------------------------------------*/
62 * Choose a random 160-bit string %$R$%. Take the passphrase %$P$%, and
63 * the message %$m$%. Now, compute %$K_E \cat K_T = H(R \cat P)$%,
64 * %$y_0 = E_{K_E}(m)$% and %$\tau = T_{K_T}(y_0)$%. The ciphertext is
65 * %$y = N \cat \tau \cat y_0$%.
67 * This is not the original format. The original format was insecure, and
68 * has been replaced incompatibly.
71 /* --- @key_plock@ --- *
73 * Arguments: @const char *tag@ = tag to use for passphrase
74 * @key_data *k@ = source key data block
75 * @key_data *kt@ = target key data block
77 * Returns: Zero if successful, nonzero if there was a problem.
79 * Use: Locks a key by encrypting it with a passphrase.
82 int key_plock(const char *tag
, key_data
*k
, key_data
*kt
)
85 octet b
[RMD160_HASHSZ
* 2];
90 /* --- Sanity check --- */
92 assert(((void)"Key data is already encrypted",
93 (k
->e
& KF_ENCMASK
) != KENC_ENCRYPT
));
95 /* --- Format the stuff in the buffer --- */
97 DENSURE(&d
, RMD160_HASHSZ
* 2);
98 rand_get(RAND_GLOBAL
, d
.buf
, RMD160_HASHSZ
);
99 d
.len
+= RMD160_HASHSZ
* 2;
100 key_encode(k
, &d
, 0);
103 m
= (octet
*)d
.buf
+ RMD160_HASHSZ
* 2;
104 msz
= d
.len
- RMD160_HASHSZ
* 2;
106 /* --- Read the passphrase --- */
108 if (passphrase_read(tag
, PMODE_VERIFY
, buf
, sizeof(buf
))) {
113 /* --- Hash the passphrase to make a key --- */
117 rmd160_mgfkeybegin(&r
);
118 rmd160_mgfkeyadd(&r
, d
.buf
, RMD160_HASHSZ
);
119 rmd160_mgfkeyadd(&r
, buf
, strlen(buf
));
120 rmd160_mgfencrypt(&r
, 0, b
, sizeof(b
));
125 /* --- Encrypt the plaintext --- */
129 blowfish_cbcinit(&c
, b
, RMD160_HASHSZ
, 0);
130 blowfish_cbcencrypt(&c
, m
, m
, msz
);
134 /* --- MAC the ciphertext --- */
139 rmd160_hmacinit(&mk
, b
+ RMD160_HASHSZ
, RMD160_HASHSZ
);
140 rmd160_macinit(&mc
, &mk
);
141 rmd160_machash(&mc
, m
, msz
);
142 rmd160_macdone(&mc
, d
.buf
+ RMD160_HASHSZ
);
150 key_encrypted(kt
, d
.buf
, d
.len
);
155 /* --- @key_punlock@ --- *
157 * Arguments: @const char *tag@ = tag to use for passphrase
158 * @key_data *k@ = source key data block
159 * @key_data *kt@ = target key data block
161 * Returns: Zero if it worked, nonzero if it didn't.
163 * Use: Unlocks a passphrase-locked key.
166 int key_punlock(const char *tag
, key_data
*k
, key_data
*kt
)
168 octet b
[RMD160_HASHSZ
* 2];
173 /* --- Sanity check --- */
175 assert(((void)"Key data isn't encrypted",
176 (k
->e
& KF_ENCMASK
) == KENC_ENCRYPT
));
178 /* --- Allocate a destination buffer --- */
180 if (k
->u
.k
.sz
< RMD160_HASHSZ
* 2)
182 sz
= k
->u
.k
.sz
- RMD160_HASHSZ
* 2;
183 p
= xmalloc(k
->u
.k
.sz
);
185 /* --- Fetch the passphrase --- */
187 if (passphrase_read(tag
, PMODE_READ
, buf
, sizeof(buf
)))
190 /* --- Hash the passphrase to make a key --- */
194 rmd160_mgfkeybegin(&r
);
195 rmd160_mgfkeyadd(&r
, k
->u
.k
.k
, RMD160_HASHSZ
);
196 rmd160_mgfkeyadd(&r
, buf
, strlen(buf
));
197 rmd160_mgfencrypt(&r
, 0, b
, sizeof(b
));
202 /* --- Decrypt the key data --- */
206 blowfish_cbcinit(&c
, b
, sizeof(b
), 0);
207 blowfish_cbcdecrypt(&c
, k
->u
.k
.k
+ RMD160_HASHSZ
, p
, sz
);
211 /* --- Verify the MAC --- */
216 rmd160_hmacinit(&mk
, b
+ RMD160_HASHSZ
, RMD160_HASHSZ
);
217 rmd160_macinit(&mc
, &mk
);
218 rmd160_machash(&mc
, p
, sz
);
219 rmd160_macdone(&mc
, b
);
220 if (memcmp(b
, k
->u
.k
.k
+ RMD160_HASHSZ
, RMD160_HASHSZ
) != 0) {
221 passphrase_cancel(tag
);
228 /* --- Decode the key data into the destination buffer --- */
232 passphrase_cancel(tag
);
234 if (key_decode(p
, sz
, kt
))
242 /* --- Tidy up if things went wrong --- */
250 /*----- That's all, folks -------------------------------------------------*/