3 * $Id: key-pass.c,v 1.5 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/dstr.h>
36 #include "passphrase.h"
39 #include "blowfish-cbc.h"
41 #include "rmd160-mgf.h"
42 #include "rmd160-hmac.h"
44 /*----- Main code ---------------------------------------------------------*/
48 * Choose a random 160-bit string %$R$%. Take the passphrase %$P$%, and
49 * the message %$m$%. Now, compute %$K_E \cat K_T = H(R \cat P)$%,
50 * %$y_0 = E_{K_E}(m)$% and %$\tau = T_{K_T}(y_0)$%. The ciphertext is
51 * %$y = N \cat \tau \cat y_0$%.
53 * This is not the original format. The original format was insecure, and
54 * has been replaced incompatibly.
57 /* --- @key_plock@ --- *
59 * Arguments: @const char *tag@ = tag to use for passphrase
60 * @key_data *k@ = source key data block
61 * @key_data *kt@ = target key data block
63 * Returns: Zero if successful, nonzero if there was a problem.
65 * Use: Locks a key by encrypting it with a passphrase.
68 int key_plock(const char *tag
, key_data
*k
, key_data
*kt
)
71 octet b
[RMD160_HASHSZ
* 2];
76 /* --- Sanity check --- */
78 assert(((void)"Key data is already encrypted",
79 (k
->e
& KF_ENCMASK
) != KENC_ENCRYPT
));
81 /* --- Format the stuff in the buffer --- */
83 DENSURE(&d
, RMD160_HASHSZ
* 2);
84 rand_get(RAND_GLOBAL
, d
.buf
, RMD160_HASHSZ
);
85 d
.len
+= RMD160_HASHSZ
* 2;
89 m
= (octet
*)d
.buf
+ RMD160_HASHSZ
* 2;
90 msz
= d
.len
- RMD160_HASHSZ
* 2;
92 /* --- Read the passphrase --- */
94 if (passphrase_read(tag
, PMODE_VERIFY
, buf
, sizeof(buf
))) {
99 /* --- Hash the passphrase to make a key --- */
103 rmd160_mgfkeybegin(&r
);
104 rmd160_mgfkeyadd(&r
, d
.buf
, RMD160_HASHSZ
);
105 rmd160_mgfkeyadd(&r
, buf
, strlen(buf
));
106 rmd160_mgfencrypt(&r
, 0, b
, sizeof(b
));
111 /* --- Encrypt the plaintext --- */
115 blowfish_cbcinit(&c
, b
, RMD160_HASHSZ
, 0);
116 blowfish_cbcencrypt(&c
, m
, m
, msz
);
120 /* --- MAC the ciphertext --- */
125 rmd160_hmacinit(&mk
, b
+ RMD160_HASHSZ
, RMD160_HASHSZ
);
126 rmd160_macinit(&mc
, &mk
);
127 rmd160_machash(&mc
, m
, msz
);
128 rmd160_macdone(&mc
, d
.buf
+ RMD160_HASHSZ
);
136 key_encrypted(kt
, d
.buf
, d
.len
);
141 /* --- @key_punlock@ --- *
143 * Arguments: @const char *tag@ = tag to use for passphrase
144 * @key_data *k@ = source key data block
145 * @key_data *kt@ = target key data block
147 * Returns: Zero if it worked, nonzero if it didn't.
149 * Use: Unlocks a passphrase-locked key.
152 int key_punlock(const char *tag
, key_data
*k
, key_data
*kt
)
154 octet b
[RMD160_HASHSZ
* 2];
159 /* --- Sanity check --- */
161 assert(((void)"Key data isn't encrypted",
162 (k
->e
& KF_ENCMASK
) == KENC_ENCRYPT
));
164 /* --- Check the size --- */
166 if (k
->u
.k
.sz
< RMD160_HASHSZ
* 2)
168 sz
= k
->u
.k
.sz
- RMD160_HASHSZ
* 2;
170 /* --- Fetch the passphrase --- */
172 if (passphrase_read(tag
, PMODE_READ
, buf
, sizeof(buf
)))
175 /* --- Hash the passphrase to make a key --- */
179 rmd160_mgfkeybegin(&r
);
180 rmd160_mgfkeyadd(&r
, k
->u
.k
.k
, RMD160_HASHSZ
);
181 rmd160_mgfkeyadd(&r
, buf
, strlen(buf
));
182 rmd160_mgfencrypt(&r
, 0, b
, sizeof(b
));
187 /* --- Verify the MAC --- */
192 rmd160_hmacinit(&mk
, b
+ RMD160_HASHSZ
, RMD160_HASHSZ
);
193 rmd160_macinit(&mc
, &mk
);
194 rmd160_machash(&mc
, k
->u
.k
.k
+ RMD160_HASHSZ
* 2, sz
);
195 rmd160_macdone(&mc
, b
+ RMD160_HASHSZ
);
196 if (memcmp(b
+ RMD160_HASHSZ
, k
->u
.k
.k
+ RMD160_HASHSZ
,
197 RMD160_HASHSZ
) != 0) {
198 passphrase_cancel(tag
);
205 /* --- Allocate a destination buffer --- */
209 /* --- Decrypt the key data --- */
213 blowfish_cbcinit(&c
, b
, RMD160_HASHSZ
, 0);
214 blowfish_cbcdecrypt(&c
, k
->u
.k
.k
+ RMD160_HASHSZ
* 2, p
, sz
);
218 /* --- Decode the key data into the destination buffer --- */
222 passphrase_cancel(tag
);
224 if (key_decode(p
, sz
, kt
))
232 /* --- Tidy up if things went wrong --- */
240 /*----- That's all, folks -------------------------------------------------*/