2 * PKCS #5 (Password-based Encryption)
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "crypto/crypto.h"
19 #include "crypto/md5.h"
31 unsigned int iter_count
;
35 enum pkcs5_alg
pkcs5_get_alg(struct asn1_oid
*oid
)
38 oid
->oid
[0] == 1 /* iso */ &&
39 oid
->oid
[1] == 2 /* member-body */ &&
40 oid
->oid
[2] == 840 /* us */ &&
41 oid
->oid
[3] == 113549 /* rsadsi */ &&
42 oid
->oid
[4] == 1 /* pkcs */ &&
43 oid
->oid
[5] == 5 /* pkcs-5 */ &&
44 oid
->oid
[6] == 3 /* pbeWithMD5AndDES-CBC */)
45 return PKCS5_ALG_MD5_DES_CBC
;
47 return PKCS5_ALG_UNKNOWN
;
51 static int pkcs5_get_params(const u8
*enc_alg
, size_t enc_alg_len
,
52 struct pkcs5_params
*params
)
55 const u8
*enc_alg_end
, *pos
, *end
;
59 /* AlgorithmIdentifier */
61 enc_alg_end
= enc_alg
+ enc_alg_len
;
63 os_memset(params
, 0, sizeof(*params
));
65 if (asn1_get_oid(enc_alg
, enc_alg_end
- enc_alg
, &oid
, &pos
)) {
66 wpa_printf(MSG_DEBUG
, "PKCS #5: Failed to parse OID "
71 asn1_oid_to_str(&oid
, obuf
, sizeof(obuf
));
72 wpa_printf(MSG_DEBUG
, "PKCS #5: encryption algorithm %s", obuf
);
73 params
->alg
= pkcs5_get_alg(&oid
);
74 if (params
->alg
== PKCS5_ALG_UNKNOWN
) {
75 wpa_printf(MSG_INFO
, "PKCS #5: unsupported encryption "
76 "algorithm %s", obuf
);
82 * PBEParameter ::= SEQUENCE {
83 * salt OCTET STRING SIZE(8),
84 * iterationCount INTEGER }
87 if (asn1_get_next(pos
, enc_alg_end
- pos
, &hdr
) < 0 ||
88 hdr
.class != ASN1_CLASS_UNIVERSAL
||
89 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
90 wpa_printf(MSG_DEBUG
, "PKCS #5: Expected SEQUENCE "
91 "(PBEParameter) - found class %d tag 0x%x",
96 end
= hdr
.payload
+ hdr
.length
;
98 /* salt OCTET STRING SIZE(8) */
99 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
100 hdr
.class != ASN1_CLASS_UNIVERSAL
||
101 hdr
.tag
!= ASN1_TAG_OCTETSTRING
||
103 wpa_printf(MSG_DEBUG
, "PKCS #5: Expected OCTETSTRING SIZE(8) "
104 "(salt) - found class %d tag 0x%x size %d",
105 hdr
.class, hdr
.tag
, hdr
.length
);
108 pos
= hdr
.payload
+ hdr
.length
;
109 os_memcpy(params
->salt
, hdr
.payload
, hdr
.length
);
110 params
->salt_len
= hdr
.length
;
111 wpa_hexdump(MSG_DEBUG
, "PKCS #5: salt",
112 params
->salt
, params
->salt_len
);
114 /* iterationCount INTEGER */
115 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
116 hdr
.class != ASN1_CLASS_UNIVERSAL
|| hdr
.tag
!= ASN1_TAG_INTEGER
) {
117 wpa_printf(MSG_DEBUG
, "PKCS #5: Expected INTEGER - found "
118 "class %d tag 0x%x", hdr
.class, hdr
.tag
);
122 params
->iter_count
= *hdr
.payload
;
123 else if (hdr
.length
== 2)
124 params
->iter_count
= WPA_GET_BE16(hdr
.payload
);
125 else if (hdr
.length
== 4)
126 params
->iter_count
= WPA_GET_BE32(hdr
.payload
);
128 wpa_hexdump(MSG_DEBUG
, "PKCS #5: Unsupported INTEGER value "
130 hdr
.payload
, hdr
.length
);
133 wpa_printf(MSG_DEBUG
, "PKCS #5: iterationCount=0x%x",
135 if (params
->iter_count
== 0 || params
->iter_count
> 0xffff) {
136 wpa_printf(MSG_INFO
, "PKCS #5: Unsupported "
137 "iterationCount=0x%x", params
->iter_count
);
145 static struct crypto_cipher
* pkcs5_crypto_init(struct pkcs5_params
*params
,
149 u8 hash
[MD5_MAC_LEN
];
153 if (params
->alg
!= PKCS5_ALG_MD5_DES_CBC
)
156 addr
[0] = (const u8
*) passwd
;
157 len
[0] = os_strlen(passwd
);
158 addr
[1] = params
->salt
;
159 len
[1] = params
->salt_len
;
160 if (md5_vector(2, addr
, len
, hash
) < 0)
163 len
[0] = MD5_MAC_LEN
;
164 for (i
= 1; i
< params
->iter_count
; i
++) {
165 if (md5_vector(1, addr
, len
, hash
) < 0)
168 /* TODO: DES key parity bits(?) */
169 wpa_hexdump_key(MSG_DEBUG
, "PKCS #5: DES key", hash
, 8);
170 wpa_hexdump_key(MSG_DEBUG
, "PKCS #5: DES IV", hash
+ 8, 8);
172 return crypto_cipher_init(CRYPTO_CIPHER_ALG_DES
, hash
+ 8, hash
, 8);
176 u8
* pkcs5_decrypt(const u8
*enc_alg
, size_t enc_alg_len
,
177 const u8
*enc_data
, size_t enc_data_len
,
178 const char *passwd
, size_t *data_len
)
180 struct crypto_cipher
*ctx
;
182 struct pkcs5_params params
;
185 if (pkcs5_get_params(enc_alg
, enc_alg_len
, ¶ms
) < 0) {
186 wpa_printf(MSG_DEBUG
, "PKCS #5: Unsupported parameters");
190 ctx
= pkcs5_crypto_init(¶ms
, passwd
);
192 wpa_printf(MSG_DEBUG
, "PKCS #5: Failed to initialize crypto");
196 /* PKCS #5, Section 7 - Decryption process */
197 if (enc_data_len
< 16 || enc_data_len
% 8) {
198 wpa_printf(MSG_INFO
, "PKCS #5: invalid length of ciphertext "
199 "%d", (int) enc_data_len
);
200 crypto_cipher_deinit(ctx
);
204 eb
= os_malloc(enc_data_len
);
206 crypto_cipher_deinit(ctx
);
210 if (crypto_cipher_decrypt(ctx
, enc_data
, eb
, enc_data_len
) < 0) {
211 wpa_printf(MSG_DEBUG
, "PKCS #5: Failed to decrypt EB");
212 crypto_cipher_deinit(ctx
);
216 crypto_cipher_deinit(ctx
);
218 pad
= eb
[enc_data_len
- 1];
220 wpa_printf(MSG_INFO
, "PKCS #5: Invalid PS octet 0x%x", pad
);
224 for (i
= enc_data_len
- pad
; i
< enc_data_len
; i
++) {
226 wpa_hexdump(MSG_INFO
, "PKCS #5: Invalid PS",
227 eb
+ enc_data_len
- pad
, pad
);
233 wpa_hexdump_key(MSG_MSGDUMP
, "PKCS #5: message M (encrypted key)",
234 eb
, enc_data_len
- pad
);
236 *data_len
= enc_data_len
- pad
;