crypto_nss.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Crypto wrapper functions for NSS
  3. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  4. *
  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.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include <nspr/prtypes.h>
  16. #include <nspr/plarenas.h>
  17. #include <nspr/plhash.h>
  18. #include <nspr/prtime.h>
  19. #include <nspr/prinrval.h>
  20. #include <nspr/prclist.h>
  21. #include <nspr/prlock.h>
  22. #include <nss/sechash.h>
  23. #include <nss/pk11pub.h>
  24. #include "common.h"
  25. #include "crypto.h"
  26. static int nss_hash(HASH_HashType type, unsigned int max_res_len,
  27. size_t num_elem, const u8 *addr[], const size_t *len,
  28. u8 *mac)
  29. {
  30. HASHContext *ctx;
  31. size_t i;
  32. unsigned int reslen;
  33. ctx = HASH_Create(type);
  34. if (ctx == NULL)
  35. return -1;
  36. HASH_Begin(ctx);
  37. for (i = 0; i < num_elem; i++)
  38. HASH_Update(ctx, addr[i], len[i]);
  39. HASH_End(ctx, mac, &reslen, max_res_len);
  40. HASH_Destroy(ctx);
  41. return 0;
  42. }
  43. void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
  44. {
  45. PK11Context *ctx = NULL;
  46. PK11SlotInfo *slot;
  47. SECItem *param = NULL;
  48. PK11SymKey *symkey = NULL;
  49. SECItem item;
  50. int olen;
  51. u8 pkey[8], next, tmp;
  52. int i;
  53. /* Add parity bits to the key */
  54. next = 0;
  55. for (i = 0; i < 7; i++) {
  56. tmp = key[i];
  57. pkey[i] = (tmp >> i) | next | 1;
  58. next = tmp << (7 - i);
  59. }
  60. pkey[i] = next | 1;
  61. slot = PK11_GetBestSlot(CKM_DES_ECB, NULL);
  62. if (slot == NULL) {
  63. wpa_printf(MSG_ERROR, "NSS: PK11_GetBestSlot failed");
  64. goto out;
  65. }
  66. item.type = siBuffer;
  67. item.data = pkey;
  68. item.len = 8;
  69. symkey = PK11_ImportSymKey(slot, CKM_DES_ECB, PK11_OriginDerive,
  70. CKA_ENCRYPT, &item, NULL);
  71. if (symkey == NULL) {
  72. wpa_printf(MSG_ERROR, "NSS: PK11_ImportSymKey failed");
  73. goto out;
  74. }
  75. param = PK11_GenerateNewParam(CKM_DES_ECB, symkey);
  76. if (param == NULL) {
  77. wpa_printf(MSG_ERROR, "NSS: PK11_GenerateNewParam failed");
  78. goto out;
  79. }
  80. ctx = PK11_CreateContextBySymKey(CKM_DES_ECB, CKA_ENCRYPT,
  81. symkey, param);
  82. if (ctx == NULL) {
  83. wpa_printf(MSG_ERROR, "NSS: PK11_CreateContextBySymKey("
  84. "CKM_DES_ECB) failed");
  85. goto out;
  86. }
  87. if (PK11_CipherOp(ctx, cypher, &olen, 8, (void *) clear, 8) !=
  88. SECSuccess) {
  89. wpa_printf(MSG_ERROR, "NSS: PK11_CipherOp failed");
  90. goto out;
  91. }
  92. out:
  93. if (ctx)
  94. PK11_DestroyContext(ctx, PR_TRUE);
  95. if (symkey)
  96. PK11_FreeSymKey(symkey);
  97. if (param)
  98. SECITEM_FreeItem(param, PR_TRUE);
  99. }
  100. int rc4_skip(const u8 *key, size_t keylen, size_t skip,
  101. u8 *data, size_t data_len)
  102. {
  103. return -1;
  104. }
  105. int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  106. {
  107. return nss_hash(HASH_AlgMD5, 16, num_elem, addr, len, mac);
  108. }
  109. int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  110. {
  111. return nss_hash(HASH_AlgSHA1, 20, num_elem, addr, len, mac);
  112. }
  113. int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  114. u8 *mac)
  115. {
  116. return nss_hash(HASH_AlgSHA256, 32, num_elem, addr, len, mac);
  117. }
  118. void * aes_encrypt_init(const u8 *key, size_t len)
  119. {
  120. return NULL;
  121. }
  122. void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
  123. {
  124. }
  125. void aes_encrypt_deinit(void *ctx)
  126. {
  127. }
  128. void * aes_decrypt_init(const u8 *key, size_t len)
  129. {
  130. return NULL;
  131. }
  132. void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
  133. {
  134. }
  135. void aes_decrypt_deinit(void *ctx)
  136. {
  137. }
  138. int crypto_mod_exp(const u8 *base, size_t base_len,
  139. const u8 *power, size_t power_len,
  140. const u8 *modulus, size_t modulus_len,
  141. u8 *result, size_t *result_len)
  142. {
  143. return -1;
  144. }
  145. struct crypto_cipher {
  146. };
  147. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  148. const u8 *iv, const u8 *key,
  149. size_t key_len)
  150. {
  151. return NULL;
  152. }
  153. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  154. u8 *crypt, size_t len)
  155. {
  156. return -1;
  157. }
  158. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  159. u8 *plain, size_t len)
  160. {
  161. return -1;
  162. }
  163. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  164. {
  165. }