2 * PKCS #5 (Password-based Encryption)
3 * Copyright (c) 2009-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/crypto.h"
13 #include "crypto/md5.h"
14 #include "crypto/sha1.h"
22 PKCS5_ALG_MD5_DES_CBC,
24 PKCS5_ALG_SHA1_3DES_CBC,
28 unsigned int iter_count;
30 PBES2_ENC_ALG_UNKNOWN,
31 PBES2_ENC_ALG_DES_EDE3_CBC,
38 static int oid_is_rsadsi(struct asn1_oid *oid)
40 return oid->len >= 4 &&
41 oid->oid[0] == 1 /* iso */ &&
42 oid->oid[1] == 2 /* member-body */ &&
43 oid->oid[2] == 840 /* us */ &&
44 oid->oid[3] == 113549 /* rsadsi */;
48 static int pkcs5_is_oid(struct asn1_oid *oid, unsigned long alg)
50 return oid->len == 7 &&
52 oid->oid[4] == 1 /* pkcs */ &&
53 oid->oid[5] == 5 /* pkcs-5 */ &&
58 static int enc_alg_is_oid(struct asn1_oid *oid, unsigned long alg)
60 return oid->len == 6 &&
62 oid->oid[4] == 3 /* encryptionAlgorithm */ &&
67 static int pkcs12_is_pbe_oid(struct asn1_oid *oid, unsigned long alg)
69 return oid->len == 8 &&
71 oid->oid[4] == 1 /* pkcs */ &&
72 oid->oid[5] == 12 /* pkcs-12 */ &&
73 oid->oid[6] == 1 /* pkcs-12PbeIds */ &&
78 static enum pkcs5_alg pkcs5_get_alg(struct asn1_oid *oid)
80 if (pkcs5_is_oid(oid, 3)) /* pbeWithMD5AndDES-CBC (PBES1) */
81 return PKCS5_ALG_MD5_DES_CBC;
82 if (pkcs12_is_pbe_oid(oid, 3)) /* pbeWithSHAAnd3-KeyTripleDES-CBC */
83 return PKCS5_ALG_SHA1_3DES_CBC;
84 if (pkcs5_is_oid(oid, 13)) /* id-PBES2 (PBES2) */
85 return PKCS5_ALG_PBES2;
86 return PKCS5_ALG_UNKNOWN;
90 static int pkcs5_get_params_pbes2(struct pkcs5_params *params, const u8 *pos,
91 const u8 *enc_alg_end)
94 const u8 *end, *kdf_end;
101 * PBES2-params ::= SEQUENCE {
102 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
103 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}} }
105 * PBES2-KDFs ALGORITHM-IDENTIFIER ::=
106 * { {PBKDF2-params IDENTIFIED BY id-PBKDF2}, ... }
109 if (asn1_get_next(pos, enc_alg_end - pos, &hdr) < 0 ||
110 hdr.class != ASN1_CLASS_UNIVERSAL ||
111 hdr.tag != ASN1_TAG_SEQUENCE) {
112 wpa_printf(MSG_DEBUG,
113 "PKCS #5: Expected SEQUENCE (PBES2-params) - found class %d tag 0x%x",
118 end = hdr.payload + hdr.length;
120 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
121 hdr.class != ASN1_CLASS_UNIVERSAL ||
122 hdr.tag != ASN1_TAG_SEQUENCE) {
123 wpa_printf(MSG_DEBUG,
124 "PKCS #5: Expected SEQUENCE (keyDerivationFunc) - found class %d tag 0x%x",
130 kdf_end = end = hdr.payload + hdr.length;
132 if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
133 wpa_printf(MSG_DEBUG,
134 "PKCS #5: Failed to parse OID (keyDerivationFunc algorithm)");
138 asn1_oid_to_str(&oid, obuf, sizeof(obuf));
139 wpa_printf(MSG_DEBUG, "PKCS #5: PBES2 keyDerivationFunc algorithm %s",
141 if (!pkcs5_is_oid(&oid, 12)) /* id-PBKDF2 */ {
142 wpa_printf(MSG_DEBUG,
143 "PKCS #5: Unsupported PBES2 keyDerivationFunc algorithm %s",
151 * PBKDF2-params ::= SEQUENCE {
153 * specified OCTET STRING,
154 * otherSource AlgorithmIdentifier {{PBKDF2-SaltSources}}
156 * iterationCount INTEGER (1..MAX),
157 * keyLength INTEGER (1..MAX) OPTIONAL,
158 * prf AlgorithmIdentifier {{PBKDF2-PRFs}} DEFAULT
163 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
164 hdr.class != ASN1_CLASS_UNIVERSAL ||
165 hdr.tag != ASN1_TAG_SEQUENCE) {
166 wpa_printf(MSG_DEBUG,
167 "PKCS #5: Expected SEQUENCE (PBKDF2-params) - found class %d tag 0x%x",
173 end = hdr.payload + hdr.length;
175 /* For now, only support the salt CHOICE specified (OCTET STRING) */
176 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
177 hdr.class != ASN1_CLASS_UNIVERSAL ||
178 hdr.tag != ASN1_TAG_OCTETSTRING ||
179 hdr.length > sizeof(params->salt)) {
180 wpa_printf(MSG_DEBUG,
181 "PKCS #5: Expected OCTET STRING (salt.specified) - found class %d tag 0x%x size %d",
182 hdr.class, hdr.tag, hdr.length);
185 pos = hdr.payload + hdr.length;
186 os_memcpy(params->salt, hdr.payload, hdr.length);
187 params->salt_len = hdr.length;
188 wpa_hexdump(MSG_DEBUG, "PKCS #5: salt", params->salt, params->salt_len);
190 /* iterationCount INTEGER */
191 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
192 hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {
193 wpa_printf(MSG_DEBUG,
194 "PKCS #5: Expected INTEGER - found class %d tag 0x%x",
198 if (hdr.length == 1) {
199 params->iter_count = *hdr.payload;
200 } else if (hdr.length == 2) {
201 params->iter_count = WPA_GET_BE16(hdr.payload);
202 } else if (hdr.length == 4) {
203 params->iter_count = WPA_GET_BE32(hdr.payload);
205 wpa_hexdump(MSG_DEBUG,
206 "PKCS #5: Unsupported INTEGER value (iterationCount)",
207 hdr.payload, hdr.length);
210 wpa_printf(MSG_DEBUG, "PKCS #5: iterationCount=0x%x",
212 if (params->iter_count == 0 || params->iter_count > 0xffff) {
213 wpa_printf(MSG_INFO, "PKCS #5: Unsupported iterationCount=0x%x",
218 /* For now, ignore optional keyLength and prf */
222 /* encryptionScheme AlgorithmIdentifier {{PBES2-Encs}} */
224 if (asn1_get_next(pos, enc_alg_end - pos, &hdr) < 0 ||
225 hdr.class != ASN1_CLASS_UNIVERSAL ||
226 hdr.tag != ASN1_TAG_SEQUENCE) {
227 wpa_printf(MSG_DEBUG,
228 "PKCS #5: Expected SEQUENCE (encryptionScheme) - found class %d tag 0x%x",
234 end = hdr.payload + hdr.length;
236 if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
237 wpa_printf(MSG_DEBUG,
238 "PKCS #5: Failed to parse OID (encryptionScheme algorithm)");
242 asn1_oid_to_str(&oid, obuf, sizeof(obuf));
243 wpa_printf(MSG_DEBUG, "PKCS #5: PBES2 encryptionScheme algorithm %s",
245 if (enc_alg_is_oid(&oid, 7)) {
246 params->enc_alg = PBES2_ENC_ALG_DES_EDE3_CBC;
248 wpa_printf(MSG_DEBUG,
249 "PKCS #5: Unsupported PBES2 encryptionScheme algorithm %s",
256 * The parameters field associated with this OID in an
257 * AlgorithmIdentifier shall have type OCTET STRING (SIZE(8)),
258 * specifying the initialization vector for CBC mode.
260 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
261 hdr.class != ASN1_CLASS_UNIVERSAL ||
262 hdr.tag != ASN1_TAG_OCTETSTRING ||
264 wpa_printf(MSG_DEBUG,
265 "PKCS #5: Expected OCTET STRING (SIZE(8)) (IV) - found class %d tag 0x%x size %d",
266 hdr.class, hdr.tag, hdr.length);
269 os_memcpy(params->iv, hdr.payload, hdr.length);
270 params->iv_len = hdr.length;
271 wpa_hexdump(MSG_DEBUG, "PKCS #5: IV", params->iv, params->iv_len);
277 static int pkcs5_get_params(const u8 *enc_alg, size_t enc_alg_len,
278 struct pkcs5_params *params)
281 const u8 *enc_alg_end, *pos, *end;
285 /* AlgorithmIdentifier */
287 enc_alg_end = enc_alg + enc_alg_len;
289 os_memset(params, 0, sizeof(*params));
291 if (asn1_get_oid(enc_alg, enc_alg_end - enc_alg, &oid, &pos)) {
292 wpa_printf(MSG_DEBUG, "PKCS #5: Failed to parse OID "
297 asn1_oid_to_str(&oid, obuf, sizeof(obuf));
298 wpa_printf(MSG_DEBUG, "PKCS #5: encryption algorithm %s", obuf);
299 params->alg = pkcs5_get_alg(&oid);
300 if (params->alg == PKCS5_ALG_UNKNOWN) {
301 wpa_printf(MSG_INFO, "PKCS #5: unsupported encryption "
302 "algorithm %s", obuf);
306 if (params->alg == PKCS5_ALG_PBES2)
307 return pkcs5_get_params_pbes2(params, pos, enc_alg_end);
313 * PBEParameter ::= SEQUENCE {
314 * salt OCTET STRING SIZE(8),
315 * iterationCount INTEGER }
317 * Note: The same implementation can be used to parse the PKCS #12
318 * version described in RFC 7292, C:
319 * pkcs-12PbeParams ::= SEQUENCE {
325 if (asn1_get_next(pos, enc_alg_end - pos, &hdr) < 0 ||
326 hdr.class != ASN1_CLASS_UNIVERSAL ||
327 hdr.tag != ASN1_TAG_SEQUENCE) {
328 wpa_printf(MSG_DEBUG, "PKCS #5: Expected SEQUENCE "
329 "(PBEParameter) - found class %d tag 0x%x",
334 end = hdr.payload + hdr.length;
336 /* salt OCTET STRING SIZE(8) (PKCS #5) or OCTET STRING (PKCS #12) */
337 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
338 hdr.class != ASN1_CLASS_UNIVERSAL ||
339 hdr.tag != ASN1_TAG_OCTETSTRING ||
340 hdr.length > sizeof(params->salt)) {
341 wpa_printf(MSG_DEBUG, "PKCS #5: Expected OCTETSTRING SIZE(8) "
342 "(salt) - found class %d tag 0x%x size %d",
343 hdr.class, hdr.tag, hdr.length);
346 pos = hdr.payload + hdr.length;
347 os_memcpy(params->salt, hdr.payload, hdr.length);
348 params->salt_len = hdr.length;
349 wpa_hexdump(MSG_DEBUG, "PKCS #5: salt",
350 params->salt, params->salt_len);
352 /* iterationCount INTEGER */
353 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
354 hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {
355 wpa_printf(MSG_DEBUG, "PKCS #5: Expected INTEGER - found "
356 "class %d tag 0x%x", hdr.class, hdr.tag);
360 params->iter_count = *hdr.payload;
361 else if (hdr.length == 2)
362 params->iter_count = WPA_GET_BE16(hdr.payload);
363 else if (hdr.length == 4)
364 params->iter_count = WPA_GET_BE32(hdr.payload);
366 wpa_hexdump(MSG_DEBUG, "PKCS #5: Unsupported INTEGER value "
368 hdr.payload, hdr.length);
371 wpa_printf(MSG_DEBUG, "PKCS #5: iterationCount=0x%x",
373 if (params->iter_count == 0 || params->iter_count > 0xffff) {
374 wpa_printf(MSG_INFO, "PKCS #5: Unsupported "
375 "iterationCount=0x%x", params->iter_count);
383 static struct crypto_cipher *
384 pkcs5_crypto_init_pbes2(struct pkcs5_params *params, const char *passwd)
388 if (params->enc_alg != PBES2_ENC_ALG_DES_EDE3_CBC ||
392 wpa_hexdump_ascii_key(MSG_DEBUG, "PKCS #5: PBES2 password for PBKDF2",
393 passwd, os_strlen(passwd));
394 wpa_hexdump(MSG_DEBUG, "PKCS #5: PBES2 salt for PBKDF2",
395 params->salt, params->salt_len);
396 wpa_printf(MSG_DEBUG, "PKCS #5: PBES2 PBKDF2 iterations: %u",
398 if (pbkdf2_sha1(passwd, params->salt, params->salt_len,
399 params->iter_count, key, sizeof(key)) < 0)
401 wpa_hexdump_key(MSG_DEBUG, "PKCS #5: DES EDE3 key", key, sizeof(key));
402 wpa_hexdump(MSG_DEBUG, "PKCS #5: DES IV", params->iv, params->iv_len);
404 return crypto_cipher_init(CRYPTO_CIPHER_ALG_3DES, params->iv,
409 static void add_byte_array_mod(u8 *a, const u8 *b, size_t len)
412 unsigned int carry = 0;
414 for (i = len - 1; i < len; i--) {
415 carry = carry + a[i] + b[i];
422 static int pkcs12_key_gen(const u8 *pw, size_t pw_len, const u8 *salt,
423 size_t salt_len, u8 id, unsigned int iter,
424 size_t out_len, u8 *out)
426 unsigned int u, v, S_len, P_len, i;
427 u8 *D = NULL, *I = NULL, *B = NULL, *pos;
434 /* D = copies of ID */
440 /* S = copies of salt; P = copies of password, I = S || P */
441 S_len = v * ((salt_len + v - 1) / v);
442 P_len = v * ((pw_len + v - 1) / v);
443 I = os_malloc(S_len + P_len);
448 for (i = 0; i < S_len; i++)
449 *pos++ = salt[i % salt_len];
452 for (i = 0; i < P_len; i++)
453 *pos++ = pw[i % pw_len];
461 u8 hash[SHA1_MAC_LEN];
468 len[1] = S_len + P_len;
469 if (sha1_vector(2, addr, len, hash) < 0)
473 len[0] = SHA1_MAC_LEN;
474 for (i = 1; i < iter; i++) {
475 if (sha1_vector(1, addr, len, hash) < 0)
480 os_memcpy(out, hash, out_len);
485 os_memcpy(out, hash, u);
489 /* I_j = (I_j + B + 1) mod 2^(v*8) */
490 /* B = copies of Ai (final hash value) */
491 for (i = 0; i < v; i++)
493 inc_byte_array(B, v);
494 for (i = 0; i < S_len + P_len; i += v)
495 add_byte_array_mod(&I[i], B, v);
506 #define PKCS12_ID_ENC 1
507 #define PKCS12_ID_IV 2
508 #define PKCS12_ID_MAC 3
510 static struct crypto_cipher *
511 pkcs12_crypto_init_sha1(struct pkcs5_params *params, const char *passwd)
519 if (params->alg != PKCS5_ALG_SHA1_3DES_CBC)
522 pw_len = passwd ? os_strlen(passwd) : 0;
523 pw = os_malloc(2 * (pw_len + 1));
527 for (i = 0; i <= pw_len; i++)
528 WPA_PUT_BE16(&pw[2 * i], passwd[i]);
529 pw_len = 2 * (pw_len + 1);
532 if (pkcs12_key_gen(pw, pw_len, params->salt, params->salt_len,
533 PKCS12_ID_ENC, params->iter_count,
534 sizeof(key), key) < 0 ||
535 pkcs12_key_gen(pw, pw_len, params->salt, params->salt_len,
536 PKCS12_ID_IV, params->iter_count,
537 sizeof(iv), iv) < 0) {
544 wpa_hexdump_key(MSG_DEBUG, "PKCS #12: DES key", key, sizeof(key));
545 wpa_hexdump_key(MSG_DEBUG, "PKCS #12: DES IV", iv, sizeof(iv));
547 return crypto_cipher_init(CRYPTO_CIPHER_ALG_3DES, iv, key, sizeof(key));
551 static struct crypto_cipher * pkcs5_crypto_init(struct pkcs5_params *params,
555 u8 hash[MD5_MAC_LEN];
559 if (params->alg == PKCS5_ALG_PBES2)
560 return pkcs5_crypto_init_pbes2(params, passwd);
562 if (params->alg == PKCS5_ALG_SHA1_3DES_CBC)
563 return pkcs12_crypto_init_sha1(params, passwd);
565 if (params->alg != PKCS5_ALG_MD5_DES_CBC)
568 addr[0] = (const u8 *) passwd;
569 len[0] = os_strlen(passwd);
570 addr[1] = params->salt;
571 len[1] = params->salt_len;
572 if (md5_vector(2, addr, len, hash) < 0)
575 len[0] = MD5_MAC_LEN;
576 for (i = 1; i < params->iter_count; i++) {
577 if (md5_vector(1, addr, len, hash) < 0)
580 /* TODO: DES key parity bits(?) */
581 wpa_hexdump_key(MSG_DEBUG, "PKCS #5: DES key", hash, 8);
582 wpa_hexdump_key(MSG_DEBUG, "PKCS #5: DES IV", hash + 8, 8);
584 return crypto_cipher_init(CRYPTO_CIPHER_ALG_DES, hash + 8, hash, 8);
588 u8 * pkcs5_decrypt(const u8 *enc_alg, size_t enc_alg_len,
589 const u8 *enc_data, size_t enc_data_len,
590 const char *passwd, size_t *data_len)
592 struct crypto_cipher *ctx;
594 struct pkcs5_params params;
597 if (pkcs5_get_params(enc_alg, enc_alg_len, ¶ms) < 0) {
598 wpa_printf(MSG_DEBUG, "PKCS #5: Unsupported parameters");
602 ctx = pkcs5_crypto_init(¶ms, passwd);
604 wpa_printf(MSG_DEBUG, "PKCS #5: Failed to initialize crypto");
608 /* PKCS #5, Section 7 - Decryption process */
609 if (enc_data_len < 16 || enc_data_len % 8) {
610 wpa_printf(MSG_INFO, "PKCS #5: invalid length of ciphertext "
611 "%d", (int) enc_data_len);
612 crypto_cipher_deinit(ctx);
616 eb = os_malloc(enc_data_len);
618 crypto_cipher_deinit(ctx);
622 if (crypto_cipher_decrypt(ctx, enc_data, eb, enc_data_len) < 0) {
623 wpa_printf(MSG_DEBUG, "PKCS #5: Failed to decrypt EB");
624 crypto_cipher_deinit(ctx);
628 crypto_cipher_deinit(ctx);
630 pad = eb[enc_data_len - 1];
632 wpa_printf(MSG_INFO, "PKCS #5: Invalid PS octet 0x%x", pad);
636 for (i = enc_data_len - pad; i < enc_data_len; i++) {
638 wpa_hexdump(MSG_INFO, "PKCS #5: Invalid PS",
639 eb + enc_data_len - pad, pad);
645 wpa_hexdump_key(MSG_MSGDUMP, "PKCS #5: message M (encrypted key)",
646 eb, enc_data_len - pad);
648 *data_len = enc_data_len - pad;