crypto_nettle.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Wrapper functions for libnettle and libgmp
  3. * Copyright (c) 2017, 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 <nettle/nettle-meta.h>
  10. #include <nettle/des.h>
  11. #undef des_encrypt
  12. #include <nettle/hmac.h>
  13. #include <nettle/aes.h>
  14. #undef aes_encrypt
  15. #undef aes_decrypt
  16. #include <nettle/arcfour.h>
  17. #include <nettle/bignum.h>
  18. #include "common.h"
  19. #include "md5.h"
  20. #include "sha1.h"
  21. #include "sha256.h"
  22. #include "sha384.h"
  23. #include "sha512.h"
  24. #include "crypto.h"
  25. int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
  26. {
  27. struct des_ctx ctx;
  28. u8 pkey[8], next, tmp;
  29. int i;
  30. /* Add parity bits to the key */
  31. next = 0;
  32. for (i = 0; i < 7; i++) {
  33. tmp = key[i];
  34. pkey[i] = (tmp >> i) | next | 1;
  35. next = tmp << (7 - i);
  36. }
  37. pkey[i] = next | 1;
  38. nettle_des_set_key(&ctx, pkey);
  39. nettle_des_encrypt(&ctx, DES_BLOCK_SIZE, cypher, clear);
  40. os_memset(&ctx, 0, sizeof(ctx));
  41. return 0;
  42. }
  43. static int nettle_digest_vector(const struct nettle_hash *alg, size_t num_elem,
  44. const u8 *addr[], const size_t *len, u8 *mac)
  45. {
  46. void *ctx;
  47. size_t i;
  48. if (TEST_FAIL())
  49. return -1;
  50. ctx = os_malloc(alg->context_size);
  51. if (!ctx)
  52. return -1;
  53. alg->init(ctx);
  54. for (i = 0; i < num_elem; i++)
  55. alg->update(ctx, len[i], addr[i]);
  56. alg->digest(ctx, alg->digest_size, mac);
  57. bin_clear_free(ctx, alg->context_size);
  58. return 0;
  59. }
  60. int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  61. {
  62. return nettle_digest_vector(&nettle_md4, num_elem, addr, len, mac);
  63. }
  64. int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  65. {
  66. return nettle_digest_vector(&nettle_md5, num_elem, addr, len, mac);
  67. }
  68. int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  69. {
  70. return nettle_digest_vector(&nettle_sha1, num_elem, addr, len, mac);
  71. }
  72. int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  73. {
  74. return nettle_digest_vector(&nettle_sha256, num_elem, addr, len, mac);
  75. }
  76. int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  77. {
  78. return nettle_digest_vector(&nettle_sha384, num_elem, addr, len, mac);
  79. }
  80. int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  81. {
  82. return nettle_digest_vector(&nettle_sha512, num_elem, addr, len, mac);
  83. }
  84. int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
  85. const u8 *addr[], const size_t *len, u8 *mac)
  86. {
  87. struct hmac_md5_ctx ctx;
  88. size_t i;
  89. if (TEST_FAIL())
  90. return -1;
  91. hmac_md5_set_key(&ctx, key_len, key);
  92. for (i = 0; i < num_elem; i++)
  93. hmac_md5_update(&ctx, len[i], addr[i]);
  94. hmac_md5_digest(&ctx, MD5_DIGEST_SIZE, mac);
  95. os_memset(&ctx, 0, sizeof(ctx));
  96. return 0;
  97. }
  98. int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  99. u8 *mac)
  100. {
  101. return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
  102. }
  103. int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
  104. const u8 *addr[], const size_t *len, u8 *mac)
  105. {
  106. struct hmac_sha1_ctx ctx;
  107. size_t i;
  108. if (TEST_FAIL())
  109. return -1;
  110. hmac_sha1_set_key(&ctx, key_len, key);
  111. for (i = 0; i < num_elem; i++)
  112. hmac_sha1_update(&ctx, len[i], addr[i]);
  113. hmac_sha1_digest(&ctx, SHA1_DIGEST_SIZE, mac);
  114. os_memset(&ctx, 0, sizeof(ctx));
  115. return 0;
  116. }
  117. int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  118. u8 *mac)
  119. {
  120. return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
  121. }
  122. #ifdef CONFIG_SHA256
  123. int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
  124. const u8 *addr[], const size_t *len, u8 *mac)
  125. {
  126. struct hmac_sha256_ctx ctx;
  127. size_t i;
  128. if (TEST_FAIL())
  129. return -1;
  130. hmac_sha256_set_key(&ctx, key_len, key);
  131. for (i = 0; i < num_elem; i++)
  132. hmac_sha256_update(&ctx, len[i], addr[i]);
  133. hmac_sha256_digest(&ctx, SHA256_DIGEST_SIZE, mac);
  134. os_memset(&ctx, 0, sizeof(ctx));
  135. return 0;
  136. }
  137. int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
  138. size_t data_len, u8 *mac)
  139. {
  140. return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
  141. }
  142. #endif /* CONFIG_SHA256 */
  143. #ifdef CONFIG_SHA384
  144. int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
  145. const u8 *addr[], const size_t *len, u8 *mac)
  146. {
  147. struct hmac_sha384_ctx ctx;
  148. size_t i;
  149. if (TEST_FAIL())
  150. return -1;
  151. hmac_sha384_set_key(&ctx, key_len, key);
  152. for (i = 0; i < num_elem; i++)
  153. hmac_sha384_update(&ctx, len[i], addr[i]);
  154. hmac_sha384_digest(&ctx, SHA384_DIGEST_SIZE, mac);
  155. os_memset(&ctx, 0, sizeof(ctx));
  156. return 0;
  157. }
  158. int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
  159. size_t data_len, u8 *mac)
  160. {
  161. return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
  162. }
  163. #endif /* CONFIG_SHA384 */
  164. #ifdef CONFIG_SHA512
  165. int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem,
  166. const u8 *addr[], const size_t *len, u8 *mac)
  167. {
  168. struct hmac_sha512_ctx ctx;
  169. size_t i;
  170. if (TEST_FAIL())
  171. return -1;
  172. hmac_sha512_set_key(&ctx, key_len, key);
  173. for (i = 0; i < num_elem; i++)
  174. hmac_sha512_update(&ctx, len[i], addr[i]);
  175. hmac_sha512_digest(&ctx, SHA512_DIGEST_SIZE, mac);
  176. os_memset(&ctx, 0, sizeof(ctx));
  177. return 0;
  178. }
  179. int hmac_sha512(const u8 *key, size_t key_len, const u8 *data,
  180. size_t data_len, u8 *mac)
  181. {
  182. return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac);
  183. }
  184. #endif /* CONFIG_SHA512 */
  185. void * aes_encrypt_init(const u8 *key, size_t len)
  186. {
  187. struct aes_ctx *ctx;
  188. if (TEST_FAIL())
  189. return NULL;
  190. ctx = os_malloc(sizeof(*ctx));
  191. if (!ctx)
  192. return NULL;
  193. nettle_aes_set_encrypt_key(ctx, len, key);
  194. return ctx;
  195. }
  196. int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
  197. {
  198. struct aes_ctx *actx = ctx;
  199. nettle_aes_encrypt(actx, AES_BLOCK_SIZE, crypt, plain);
  200. return 0;
  201. }
  202. void aes_encrypt_deinit(void *ctx)
  203. {
  204. struct aes_ctx *actx = ctx;
  205. bin_clear_free(actx, sizeof(*actx));
  206. }
  207. void * aes_decrypt_init(const u8 *key, size_t len)
  208. {
  209. struct aes_ctx *ctx;
  210. if (TEST_FAIL())
  211. return NULL;
  212. ctx = os_malloc(sizeof(*ctx));
  213. if (!ctx)
  214. return NULL;
  215. nettle_aes_set_decrypt_key(ctx, len, key);
  216. return ctx;
  217. }
  218. int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
  219. {
  220. struct aes_ctx *actx = ctx;
  221. nettle_aes_decrypt(actx, AES_BLOCK_SIZE, plain, crypt);
  222. return 0;
  223. }
  224. void aes_decrypt_deinit(void *ctx)
  225. {
  226. struct aes_ctx *actx = ctx;
  227. bin_clear_free(actx, sizeof(*actx));
  228. }
  229. int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey,
  230. u8 *pubkey)
  231. {
  232. size_t pubkey_len, pad;
  233. if (os_get_random(privkey, prime_len) < 0)
  234. return -1;
  235. if (os_memcmp(privkey, prime, prime_len) > 0) {
  236. /* Make sure private value is smaller than prime */
  237. privkey[0] = 0;
  238. }
  239. pubkey_len = prime_len;
  240. if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len,
  241. pubkey, &pubkey_len) < 0)
  242. return -1;
  243. if (pubkey_len < prime_len) {
  244. pad = prime_len - pubkey_len;
  245. os_memmove(pubkey + pad, pubkey, pubkey_len);
  246. os_memset(pubkey, 0, pad);
  247. }
  248. return 0;
  249. }
  250. int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
  251. const u8 *privkey, size_t privkey_len,
  252. const u8 *pubkey, size_t pubkey_len,
  253. u8 *secret, size_t *len)
  254. {
  255. return crypto_mod_exp(pubkey, pubkey_len, privkey, privkey_len,
  256. prime, prime_len, secret, len);
  257. }
  258. int crypto_mod_exp(const u8 *base, size_t base_len,
  259. const u8 *power, size_t power_len,
  260. const u8 *modulus, size_t modulus_len,
  261. u8 *result, size_t *result_len)
  262. {
  263. mpz_t bn_base, bn_exp, bn_modulus, bn_result;
  264. int ret = -1;
  265. size_t len;
  266. mpz_inits(bn_base, bn_exp, bn_modulus, bn_result, NULL);
  267. mpz_import(bn_base, base_len, 1, 1, 1, 0, base);
  268. mpz_import(bn_exp, power_len, 1, 1, 1, 0, power);
  269. mpz_import(bn_modulus, modulus_len, 1, 1, 1, 0, modulus);
  270. mpz_powm(bn_result, bn_base, bn_exp, bn_modulus);
  271. len = mpz_sizeinbase(bn_result, 2);
  272. len = (len + 7) / 8;
  273. if (*result_len < len)
  274. goto error;
  275. mpz_export(result, result_len, 1, 1, 1, 0, bn_result);
  276. ret = 0;
  277. error:
  278. mpz_clears(bn_base, bn_exp, bn_modulus, bn_result, NULL);
  279. return ret;
  280. }
  281. struct crypto_cipher {
  282. enum crypto_cipher_alg alg;
  283. union {
  284. struct arcfour_ctx arcfour_ctx;
  285. } u;
  286. };
  287. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  288. const u8 *iv, const u8 *key,
  289. size_t key_len)
  290. {
  291. struct crypto_cipher *ctx;
  292. ctx = os_zalloc(sizeof(*ctx));
  293. if (!ctx)
  294. return NULL;
  295. ctx->alg = alg;
  296. switch (alg) {
  297. case CRYPTO_CIPHER_ALG_RC4:
  298. nettle_arcfour_set_key(&ctx->u.arcfour_ctx, key_len, key);
  299. break;
  300. default:
  301. os_free(ctx);
  302. return NULL;
  303. }
  304. return ctx;
  305. }
  306. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  307. u8 *crypt, size_t len)
  308. {
  309. switch (ctx->alg) {
  310. case CRYPTO_CIPHER_ALG_RC4:
  311. nettle_arcfour_crypt(&ctx->u.arcfour_ctx, len, crypt, plain);
  312. break;
  313. default:
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  319. u8 *plain, size_t len)
  320. {
  321. switch (ctx->alg) {
  322. case CRYPTO_CIPHER_ALG_RC4:
  323. nettle_arcfour_crypt(&ctx->u.arcfour_ctx, len, plain, crypt);
  324. break;
  325. default:
  326. return -1;
  327. }
  328. return 0;
  329. }
  330. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  331. {
  332. bin_clear_free(ctx, sizeof(*ctx));
  333. }