crypto_gnutls.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * WPA Supplicant / wrapper functions for libgcrypt
  3. * Copyright (c) 2004-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 <gcrypt.h>
  10. #include "common.h"
  11. #include "crypto.h"
  12. int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  13. {
  14. gcry_md_hd_t hd;
  15. unsigned char *p;
  16. size_t i;
  17. if (gcry_md_open(&hd, GCRY_MD_MD4, 0) != GPG_ERR_NO_ERROR)
  18. return -1;
  19. for (i = 0; i < num_elem; i++)
  20. gcry_md_write(hd, addr[i], len[i]);
  21. p = gcry_md_read(hd, GCRY_MD_MD4);
  22. if (p)
  23. memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_MD4));
  24. gcry_md_close(hd);
  25. return 0;
  26. }
  27. void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
  28. {
  29. gcry_cipher_hd_t hd;
  30. u8 pkey[8], next, tmp;
  31. int i;
  32. /* Add parity bits to the key */
  33. next = 0;
  34. for (i = 0; i < 7; i++) {
  35. tmp = key[i];
  36. pkey[i] = (tmp >> i) | next | 1;
  37. next = tmp << (7 - i);
  38. }
  39. pkey[i] = next | 1;
  40. gcry_cipher_open(&hd, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
  41. gcry_err_code(gcry_cipher_setkey(hd, pkey, 8));
  42. gcry_cipher_encrypt(hd, cypher, 8, clear, 8);
  43. gcry_cipher_close(hd);
  44. }
  45. int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  46. {
  47. gcry_md_hd_t hd;
  48. unsigned char *p;
  49. size_t i;
  50. if (gcry_md_open(&hd, GCRY_MD_MD5, 0) != GPG_ERR_NO_ERROR)
  51. return -1;
  52. for (i = 0; i < num_elem; i++)
  53. gcry_md_write(hd, addr[i], len[i]);
  54. p = gcry_md_read(hd, GCRY_MD_MD5);
  55. if (p)
  56. memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_MD5));
  57. gcry_md_close(hd);
  58. return 0;
  59. }
  60. int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  61. {
  62. gcry_md_hd_t hd;
  63. unsigned char *p;
  64. size_t i;
  65. if (gcry_md_open(&hd, GCRY_MD_SHA1, 0) != GPG_ERR_NO_ERROR)
  66. return -1;
  67. for (i = 0; i < num_elem; i++)
  68. gcry_md_write(hd, addr[i], len[i]);
  69. p = gcry_md_read(hd, GCRY_MD_SHA1);
  70. if (p)
  71. memcpy(mac, p, gcry_md_get_algo_dlen(GCRY_MD_SHA1));
  72. gcry_md_close(hd);
  73. return 0;
  74. }
  75. void * aes_encrypt_init(const u8 *key, size_t len)
  76. {
  77. gcry_cipher_hd_t hd;
  78. if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
  79. GPG_ERR_NO_ERROR) {
  80. printf("cipher open failed\n");
  81. return NULL;
  82. }
  83. if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
  84. printf("setkey failed\n");
  85. gcry_cipher_close(hd);
  86. return NULL;
  87. }
  88. return hd;
  89. }
  90. void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
  91. {
  92. gcry_cipher_hd_t hd = ctx;
  93. gcry_cipher_encrypt(hd, crypt, 16, plain, 16);
  94. }
  95. void aes_encrypt_deinit(void *ctx)
  96. {
  97. gcry_cipher_hd_t hd = ctx;
  98. gcry_cipher_close(hd);
  99. }
  100. void * aes_decrypt_init(const u8 *key, size_t len)
  101. {
  102. gcry_cipher_hd_t hd;
  103. if (gcry_cipher_open(&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_ECB, 0) !=
  104. GPG_ERR_NO_ERROR)
  105. return NULL;
  106. if (gcry_cipher_setkey(hd, key, len) != GPG_ERR_NO_ERROR) {
  107. gcry_cipher_close(hd);
  108. return NULL;
  109. }
  110. return hd;
  111. }
  112. void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
  113. {
  114. gcry_cipher_hd_t hd = ctx;
  115. gcry_cipher_decrypt(hd, plain, 16, crypt, 16);
  116. }
  117. void aes_decrypt_deinit(void *ctx)
  118. {
  119. gcry_cipher_hd_t hd = ctx;
  120. gcry_cipher_close(hd);
  121. }
  122. int crypto_mod_exp(const u8 *base, size_t base_len,
  123. const u8 *power, size_t power_len,
  124. const u8 *modulus, size_t modulus_len,
  125. u8 *result, size_t *result_len)
  126. {
  127. gcry_mpi_t bn_base = NULL, bn_exp = NULL, bn_modulus = NULL,
  128. bn_result = NULL;
  129. int ret = -1;
  130. if (gcry_mpi_scan(&bn_base, GCRYMPI_FMT_USG, base, base_len, NULL) !=
  131. GPG_ERR_NO_ERROR ||
  132. gcry_mpi_scan(&bn_exp, GCRYMPI_FMT_USG, power, power_len, NULL) !=
  133. GPG_ERR_NO_ERROR ||
  134. gcry_mpi_scan(&bn_modulus, GCRYMPI_FMT_USG, modulus, modulus_len,
  135. NULL) != GPG_ERR_NO_ERROR)
  136. goto error;
  137. bn_result = gcry_mpi_new(modulus_len * 8);
  138. gcry_mpi_powm(bn_result, bn_base, bn_exp, bn_modulus);
  139. if (gcry_mpi_print(GCRYMPI_FMT_USG, result, *result_len, result_len,
  140. bn_result) != GPG_ERR_NO_ERROR)
  141. goto error;
  142. ret = 0;
  143. error:
  144. gcry_mpi_release(bn_base);
  145. gcry_mpi_release(bn_exp);
  146. gcry_mpi_release(bn_modulus);
  147. gcry_mpi_release(bn_result);
  148. return ret;
  149. }
  150. struct crypto_cipher {
  151. gcry_cipher_hd_t enc;
  152. gcry_cipher_hd_t dec;
  153. };
  154. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  155. const u8 *iv, const u8 *key,
  156. size_t key_len)
  157. {
  158. struct crypto_cipher *ctx;
  159. gcry_error_t res;
  160. enum gcry_cipher_algos a;
  161. int ivlen;
  162. ctx = os_zalloc(sizeof(*ctx));
  163. if (ctx == NULL)
  164. return NULL;
  165. switch (alg) {
  166. case CRYPTO_CIPHER_ALG_RC4:
  167. a = GCRY_CIPHER_ARCFOUR;
  168. res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_STREAM,
  169. 0);
  170. gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_STREAM, 0);
  171. break;
  172. case CRYPTO_CIPHER_ALG_AES:
  173. if (key_len == 24)
  174. a = GCRY_CIPHER_AES192;
  175. else if (key_len == 32)
  176. a = GCRY_CIPHER_AES256;
  177. else
  178. a = GCRY_CIPHER_AES;
  179. res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
  180. gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
  181. break;
  182. case CRYPTO_CIPHER_ALG_3DES:
  183. a = GCRY_CIPHER_3DES;
  184. res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
  185. gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
  186. break;
  187. case CRYPTO_CIPHER_ALG_DES:
  188. a = GCRY_CIPHER_DES;
  189. res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
  190. gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
  191. break;
  192. case CRYPTO_CIPHER_ALG_RC2:
  193. if (key_len == 5)
  194. a = GCRY_CIPHER_RFC2268_40;
  195. else
  196. a = GCRY_CIPHER_RFC2268_128;
  197. res = gcry_cipher_open(&ctx->enc, a, GCRY_CIPHER_MODE_CBC, 0);
  198. gcry_cipher_open(&ctx->dec, a, GCRY_CIPHER_MODE_CBC, 0);
  199. break;
  200. default:
  201. os_free(ctx);
  202. return NULL;
  203. }
  204. if (res != GPG_ERR_NO_ERROR) {
  205. os_free(ctx);
  206. return NULL;
  207. }
  208. if (gcry_cipher_setkey(ctx->enc, key, key_len) != GPG_ERR_NO_ERROR ||
  209. gcry_cipher_setkey(ctx->dec, key, key_len) != GPG_ERR_NO_ERROR) {
  210. gcry_cipher_close(ctx->enc);
  211. gcry_cipher_close(ctx->dec);
  212. os_free(ctx);
  213. return NULL;
  214. }
  215. ivlen = gcry_cipher_get_algo_blklen(a);
  216. if (gcry_cipher_setiv(ctx->enc, iv, ivlen) != GPG_ERR_NO_ERROR ||
  217. gcry_cipher_setiv(ctx->dec, iv, ivlen) != GPG_ERR_NO_ERROR) {
  218. gcry_cipher_close(ctx->enc);
  219. gcry_cipher_close(ctx->dec);
  220. os_free(ctx);
  221. return NULL;
  222. }
  223. return ctx;
  224. }
  225. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  226. u8 *crypt, size_t len)
  227. {
  228. if (gcry_cipher_encrypt(ctx->enc, crypt, len, plain, len) !=
  229. GPG_ERR_NO_ERROR)
  230. return -1;
  231. return 0;
  232. }
  233. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  234. u8 *plain, size_t len)
  235. {
  236. if (gcry_cipher_decrypt(ctx->dec, plain, len, crypt, len) !=
  237. GPG_ERR_NO_ERROR)
  238. return -1;
  239. return 0;
  240. }
  241. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  242. {
  243. gcry_cipher_close(ctx->enc);
  244. gcry_cipher_close(ctx->dec);
  245. os_free(ctx);
  246. }