2 * Crypto wrapper functions for NSS
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.
16 #include <nspr/prtypes.h>
17 #include <nspr/plarenas.h>
18 #include <nspr/plhash.h>
19 #include <nspr/prtime.h>
20 #include <nspr/prinrval.h>
21 #include <nspr/prclist.h>
22 #include <nspr/prlock.h>
23 #include <nss/sechash.h>
24 #include <nss/pk11pub.h>
30 static int nss_hash(HASH_HashType type
, unsigned int max_res_len
,
31 size_t num_elem
, const u8
*addr
[], const size_t *len
,
38 ctx
= HASH_Create(type
);
43 for (i
= 0; i
< num_elem
; i
++)
44 HASH_Update(ctx
, addr
[i
], len
[i
]);
45 HASH_End(ctx
, mac
, &reslen
, max_res_len
);
52 void des_encrypt(const u8
*clear
, const u8
*key
, u8
*cypher
)
54 PK11Context
*ctx
= NULL
;
56 SECItem
*param
= NULL
;
57 PK11SymKey
*symkey
= NULL
;
60 u8 pkey
[8], next
, tmp
;
63 /* Add parity bits to the key */
65 for (i
= 0; i
< 7; i
++) {
67 pkey
[i
] = (tmp
>> i
) | next
| 1;
68 next
= tmp
<< (7 - i
);
72 slot
= PK11_GetBestSlot(CKM_DES_ECB
, NULL
);
74 wpa_printf(MSG_ERROR
, "NSS: PK11_GetBestSlot failed");
81 symkey
= PK11_ImportSymKey(slot
, CKM_DES_ECB
, PK11_OriginDerive
,
82 CKA_ENCRYPT
, &item
, NULL
);
84 wpa_printf(MSG_ERROR
, "NSS: PK11_ImportSymKey failed");
88 param
= PK11_GenerateNewParam(CKM_DES_ECB
, symkey
);
90 wpa_printf(MSG_ERROR
, "NSS: PK11_GenerateNewParam failed");
94 ctx
= PK11_CreateContextBySymKey(CKM_DES_ECB
, CKA_ENCRYPT
,
97 wpa_printf(MSG_ERROR
, "NSS: PK11_CreateContextBySymKey("
98 "CKM_DES_ECB) failed");
102 if (PK11_CipherOp(ctx
, cypher
, &olen
, 8, (void *) clear
, 8) !=
104 wpa_printf(MSG_ERROR
, "NSS: PK11_CipherOp failed");
110 PK11_DestroyContext(ctx
, PR_TRUE
);
112 PK11_FreeSymKey(symkey
);
114 SECITEM_FreeItem(param
, PR_TRUE
);
118 int rc4_skip(const u8
*key
, size_t keylen
, size_t skip
,
119 u8
*data
, size_t data_len
)
125 int md5_vector(size_t num_elem
, const u8
*addr
[], const size_t *len
, u8
*mac
)
127 return nss_hash(HASH_AlgMD5
, 16, num_elem
, addr
, len
, mac
);
131 int sha1_vector(size_t num_elem
, const u8
*addr
[], const size_t *len
, u8
*mac
)
133 return nss_hash(HASH_AlgSHA1
, 20, num_elem
, addr
, len
, mac
);
137 int sha256_vector(size_t num_elem
, const u8
*addr
[], const size_t *len
,
140 return nss_hash(HASH_AlgSHA256
, 32, num_elem
, addr
, len
, mac
);
144 void * aes_encrypt_init(const u8
*key
, size_t len
)
150 void aes_encrypt(void *ctx
, const u8
*plain
, u8
*crypt
)
155 void aes_encrypt_deinit(void *ctx
)
160 void * aes_decrypt_init(const u8
*key
, size_t len
)
166 void aes_decrypt(void *ctx
, const u8
*crypt
, u8
*plain
)
171 void aes_decrypt_deinit(void *ctx
)
176 int crypto_mod_exp(const u8
*base
, size_t base_len
,
177 const u8
*power
, size_t power_len
,
178 const u8
*modulus
, size_t modulus_len
,
179 u8
*result
, size_t *result_len
)
185 struct crypto_cipher
{
189 struct crypto_cipher
* crypto_cipher_init(enum crypto_cipher_alg alg
,
190 const u8
*iv
, const u8
*key
,
197 int crypto_cipher_encrypt(struct crypto_cipher
*ctx
, const u8
*plain
,
198 u8
*crypt
, size_t len
)
204 int crypto_cipher_decrypt(struct crypto_cipher
*ctx
, const u8
*crypt
,
205 u8
*plain
, size_t len
)
211 void crypto_cipher_deinit(struct crypto_cipher
*ctx
)