pkcs1.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * PKCS #1 (RSA Encryption)
  3. * Copyright (c) 2006-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 "common.h"
  10. #include "rsa.h"
  11. #include "pkcs1.h"
  12. static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,
  13. const u8 *in, size_t inlen,
  14. u8 *out, size_t *outlen)
  15. {
  16. size_t ps_len;
  17. u8 *pos;
  18. /*
  19. * PKCS #1 v1.5, 8.1:
  20. *
  21. * EB = 00 || BT || PS || 00 || D
  22. * BT = 00 or 01 for private-key operation; 02 for public-key operation
  23. * PS = k-3-||D||; at least eight octets
  24. * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)
  25. * k = length of modulus in octets (modlen)
  26. */
  27. if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {
  28. wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "
  29. "lengths (modlen=%lu outlen=%lu inlen=%lu)",
  30. __func__, (unsigned long) modlen,
  31. (unsigned long) *outlen,
  32. (unsigned long) inlen);
  33. return -1;
  34. }
  35. pos = out;
  36. *pos++ = 0x00;
  37. *pos++ = block_type; /* BT */
  38. ps_len = modlen - inlen - 3;
  39. switch (block_type) {
  40. case 0:
  41. os_memset(pos, 0x00, ps_len);
  42. pos += ps_len;
  43. break;
  44. case 1:
  45. os_memset(pos, 0xff, ps_len);
  46. pos += ps_len;
  47. break;
  48. case 2:
  49. if (os_get_random(pos, ps_len) < 0) {
  50. wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "
  51. "random data for PS", __func__);
  52. return -1;
  53. }
  54. while (ps_len--) {
  55. if (*pos == 0x00)
  56. *pos = 0x01;
  57. pos++;
  58. }
  59. break;
  60. default:
  61. wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
  62. "%d", __func__, block_type);
  63. return -1;
  64. }
  65. *pos++ = 0x00;
  66. os_memcpy(pos, in, inlen); /* D */
  67. return 0;
  68. }
  69. int pkcs1_encrypt(int block_type, struct crypto_rsa_key *key,
  70. int use_private, const u8 *in, size_t inlen,
  71. u8 *out, size_t *outlen)
  72. {
  73. size_t modlen;
  74. modlen = crypto_rsa_get_modulus_len(key);
  75. if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
  76. out, outlen) < 0)
  77. return -1;
  78. return crypto_rsa_exptmod(out, modlen, out, outlen, key, use_private);
  79. }
  80. int pkcs1_v15_private_key_decrypt(struct crypto_rsa_key *key,
  81. const u8 *in, size_t inlen,
  82. u8 *out, size_t *outlen)
  83. {
  84. int res;
  85. u8 *pos, *end;
  86. res = crypto_rsa_exptmod(in, inlen, out, outlen, key, 1);
  87. if (res)
  88. return res;
  89. if (*outlen < 2 || out[0] != 0 || out[1] != 2)
  90. return -1;
  91. /* Skip PS (pseudorandom non-zero octets) */
  92. pos = out + 2;
  93. end = out + *outlen;
  94. while (*pos && pos < end)
  95. pos++;
  96. if (pos == end)
  97. return -1;
  98. pos++;
  99. *outlen -= pos - out;
  100. /* Strip PKCS #1 header */
  101. os_memmove(out, pos, *outlen);
  102. return 0;
  103. }
  104. int pkcs1_decrypt_public_key(struct crypto_rsa_key *key,
  105. const u8 *crypt, size_t crypt_len,
  106. u8 *plain, size_t *plain_len)
  107. {
  108. size_t len;
  109. u8 *pos;
  110. len = *plain_len;
  111. if (crypto_rsa_exptmod(crypt, crypt_len, plain, &len, key, 0) < 0)
  112. return -1;
  113. /*
  114. * PKCS #1 v1.5, 8.1:
  115. *
  116. * EB = 00 || BT || PS || 00 || D
  117. * BT = 00 or 01
  118. * PS = k-3-||D|| times (00 if BT=00) or (FF if BT=01)
  119. * k = length of modulus in octets
  120. */
  121. if (len < 3 + 8 + 16 /* min hash len */ ||
  122. plain[0] != 0x00 || (plain[1] != 0x00 && plain[1] != 0x01)) {
  123. wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
  124. "structure");
  125. return -1;
  126. }
  127. pos = plain + 3;
  128. if (plain[1] == 0x00) {
  129. /* BT = 00 */
  130. if (plain[2] != 0x00) {
  131. wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
  132. "PS (BT=00)");
  133. return -1;
  134. }
  135. while (pos + 1 < plain + len && *pos == 0x00 && pos[1] == 0x00)
  136. pos++;
  137. } else {
  138. /* BT = 01 */
  139. if (plain[2] != 0xff) {
  140. wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
  141. "PS (BT=01)");
  142. return -1;
  143. }
  144. while (pos < plain + len && *pos == 0xff)
  145. pos++;
  146. }
  147. if (pos - plain - 2 < 8) {
  148. /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
  149. wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
  150. "padding");
  151. return -1;
  152. }
  153. if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
  154. wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
  155. "structure (2)");
  156. return -1;
  157. }
  158. pos++;
  159. len -= pos - plain;
  160. /* Strip PKCS #1 header */
  161. os_memmove(plain, pos, len);
  162. *plain_len = len;
  163. return 0;
  164. }