crypto_nss.c 3.8 KB

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