ec.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* libanode: the Anode C reference implementation
  2. * Copyright (C) 2009-2010 Adam Ierymenko <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <openssl/bn.h>
  20. #include <openssl/obj_mac.h>
  21. #include <openssl/rand.h>
  22. #include <openssl/ec.h>
  23. #include <openssl/ecdh.h>
  24. #include <openssl/ecdsa.h>
  25. #include "types.h"
  26. #include "misc.h"
  27. #include "ec.h"
  28. static EC_GROUP *AnodeEC_group = (EC_GROUP *)0;
  29. static void *AnodeEC_KDF(const void *in,size_t inlen,void *out,size_t *outlen)
  30. {
  31. unsigned long i,longest_length;
  32. if (!*outlen)
  33. return out;
  34. for(i=0;i<(unsigned long)*outlen;++i)
  35. ((unsigned char *)out)[i] = (unsigned char)0;
  36. longest_length = inlen;
  37. if (longest_length < *outlen)
  38. longest_length = *outlen;
  39. for(i=0;i<longest_length;++i)
  40. ((unsigned char *)out)[i % (unsigned long)*outlen] ^= ((const unsigned char *)in)[i % (unsigned long)inlen];
  41. return out;
  42. }
  43. int AnodeECKeyPair_generate(struct AnodeECKeyPair *pair)
  44. {
  45. EC_KEY *key;
  46. int len;
  47. #ifdef HAS_DEV_URANDOM
  48. char buf[128];
  49. FILE *f = fopen("/dev/urandom","r");
  50. if (f) {
  51. if (fread(buf,1,sizeof(buf),f) == sizeof(buf))
  52. RAND_add(buf,sizeof(buf),sizeof(buf)/2);
  53. fclose(f);
  54. }
  55. #endif
  56. if (!AnodeEC_group) {
  57. AnodeEC_group = EC_GROUP_new_by_curve_name(ANODE_EC_GROUP);
  58. if (!AnodeEC_group) return 0;
  59. }
  60. key = EC_KEY_new();
  61. if (!key) return 0;
  62. if (!EC_KEY_set_group(key,AnodeEC_group)) {
  63. EC_KEY_free(key);
  64. return 0;
  65. }
  66. if (!EC_KEY_generate_key(key)) {
  67. EC_KEY_free(key);
  68. return 0;
  69. }
  70. Anode_zero(pair,sizeof(struct AnodeECKeyPair));
  71. /* Stuff the private key into priv.key */
  72. len = BN_num_bytes(EC_KEY_get0_private_key(key));
  73. if ((len > ANODE_EC_PRIME_BYTES)||(len < 0)) {
  74. EC_KEY_free(key);
  75. return 0;
  76. }
  77. BN_bn2bin(EC_KEY_get0_private_key(key),&(pair->priv.key[ANODE_EC_PRIME_BYTES - len]));
  78. pair->priv.bytes = ANODE_EC_PRIME_BYTES;
  79. len = EC_POINT_point2oct(AnodeEC_group,EC_KEY_get0_public_key(key),POINT_CONVERSION_COMPRESSED,pair->pub.key,sizeof(pair->pub.key),0);
  80. if (len != ANODE_EC_PUBLIC_KEY_BYTES) {
  81. EC_KEY_free(key);
  82. return 0;
  83. }
  84. pair->pub.bytes = ANODE_EC_PUBLIC_KEY_BYTES;
  85. /* Keep a copy of OpenSSL's structure around so we don't have to re-init
  86. * it every time we use our key pair structure. */
  87. pair->internal_key = key;
  88. return 1;
  89. }
  90. int AnodeECKeyPair_init(struct AnodeECKeyPair *pair,const struct AnodeECKey *pub,const struct AnodeECKey *priv)
  91. {
  92. EC_KEY *key;
  93. EC_POINT *kxy;
  94. BIGNUM *pn;
  95. if (!AnodeEC_group) {
  96. AnodeEC_group = EC_GROUP_new_by_curve_name(ANODE_EC_GROUP);
  97. if (!AnodeEC_group) return 0;
  98. }
  99. key = EC_KEY_new();
  100. if (!key)
  101. return 0;
  102. if (!EC_KEY_set_group(key,AnodeEC_group)) {
  103. EC_KEY_free(key);
  104. return 0;
  105. }
  106. /* Grab the private key */
  107. if (priv->bytes != ANODE_EC_PRIME_BYTES) {
  108. EC_KEY_free(key);
  109. return 0;
  110. }
  111. pn = BN_new();
  112. if (!pn) {
  113. EC_KEY_free(key);
  114. return 0;
  115. }
  116. if (!BN_bin2bn(priv->key,ANODE_EC_PRIME_BYTES,pn)) {
  117. BN_free(pn);
  118. EC_KEY_free(key);
  119. return 0;
  120. }
  121. if (!EC_KEY_set_private_key(key,pn)) {
  122. BN_free(pn);
  123. EC_KEY_free(key);
  124. return 0;
  125. }
  126. BN_free(pn);
  127. /* Set the public key */
  128. if (pub->bytes != ANODE_EC_PUBLIC_KEY_BYTES) {
  129. EC_KEY_free(key);
  130. return 0;
  131. }
  132. kxy = EC_POINT_new(AnodeEC_group);
  133. if (!kxy) {
  134. EC_KEY_free(key);
  135. return 0;
  136. }
  137. EC_POINT_oct2point(AnodeEC_group,kxy,pub->key,ANODE_EC_PUBLIC_KEY_BYTES,0);
  138. if (!EC_KEY_set_public_key(key,kxy)) {
  139. EC_POINT_free(kxy);
  140. EC_KEY_free(key);
  141. return 0;
  142. }
  143. EC_POINT_free(kxy);
  144. Anode_zero(pair,sizeof(struct AnodeECKeyPair));
  145. Anode_memcpy((void *)&(pair->pub),(const void *)pub,sizeof(struct AnodeECKey));
  146. Anode_memcpy((void *)&(pair->priv),(const void *)priv,sizeof(struct AnodeECKey));
  147. pair->internal_key = key;
  148. return 1;
  149. }
  150. void AnodeECKeyPair_destroy(struct AnodeECKeyPair *pair)
  151. {
  152. if (pair) {
  153. if (pair->internal_key)
  154. EC_KEY_free((EC_KEY *)pair->internal_key);
  155. }
  156. }
  157. int AnodeECKeyPair_agree(const struct AnodeECKeyPair *my_key_pair,const struct AnodeECKey *their_pub_key,unsigned char *key_buf,unsigned int key_len)
  158. {
  159. EC_POINT *pub;
  160. int i;
  161. if (!AnodeEC_group) {
  162. AnodeEC_group = EC_GROUP_new_by_curve_name(ANODE_EC_GROUP);
  163. if (!AnodeEC_group) return 0;
  164. }
  165. if (!my_key_pair->internal_key)
  166. return 0;
  167. if (their_pub_key->bytes != ANODE_EC_PUBLIC_KEY_BYTES)
  168. return 0;
  169. pub = EC_POINT_new(AnodeEC_group);
  170. if (!pub)
  171. return 0;
  172. EC_POINT_oct2point(AnodeEC_group,pub,their_pub_key->key,ANODE_EC_PUBLIC_KEY_BYTES,0);
  173. i = ECDH_compute_key(key_buf,key_len,pub,(EC_KEY *)my_key_pair->internal_key,&AnodeEC_KDF);
  174. if (i != (int)key_len) {
  175. EC_POINT_free(pub);
  176. return 0;
  177. }
  178. EC_POINT_free(pub);
  179. return 1;
  180. }
  181. void AnodeEC_random(unsigned char *buf,unsigned int len)
  182. {
  183. RAND_pseudo_bytes(buf,len);
  184. }