rfc2268.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* rfc2268.c - The cipher described in rfc2268; aka Ron's Cipher 2.
  2. * Copyright (C) 2003 Nikos Mavroyanopoulos
  3. * Copyright (C) 2004 Free Software Foundation, Inc.
  4. *
  5. * This file is part of Libgcrypt
  6. *
  7. * Libgcrypt is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser general Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * Libgcrypt is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. /* This implementation was written by Nikos Mavroyanopoulos for GNUTLS
  22. * as a Libgcrypt module (gnutls/lib/x509/rc2.c) and later adapted for
  23. * direct use by Libgcrypt by Werner Koch. This implementation is
  24. * only useful for pkcs#12 decryption.
  25. *
  26. * The implementation here is based on Peter Gutmann's RRC.2 paper.
  27. */
  28. #include <config.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "g10lib.h"
  33. #include "types.h"
  34. #include "cipher.h"
  35. #define RFC2268_BLOCKSIZE 8
  36. typedef struct
  37. {
  38. u16 S[64];
  39. } RFC2268_context;
  40. static const unsigned char rfc2268_sbox[] = {
  41. 217, 120, 249, 196, 25, 221, 181, 237,
  42. 40, 233, 253, 121, 74, 160, 216, 157,
  43. 198, 126, 55, 131, 43, 118, 83, 142,
  44. 98, 76, 100, 136, 68, 139, 251, 162,
  45. 23, 154, 89, 245, 135, 179, 79, 19,
  46. 97, 69, 109, 141, 9, 129, 125, 50,
  47. 189, 143, 64, 235, 134, 183, 123, 11,
  48. 240, 149, 33, 34, 92, 107, 78, 130,
  49. 84, 214, 101, 147, 206, 96, 178, 28,
  50. 115, 86, 192, 20, 167, 140, 241, 220,
  51. 18, 117, 202, 31, 59, 190, 228, 209,
  52. 66, 61, 212, 48, 163, 60, 182, 38,
  53. 111, 191, 14, 218, 70, 105, 7, 87,
  54. 39, 242, 29, 155, 188, 148, 67, 3,
  55. 248, 17, 199, 246, 144, 239, 62, 231,
  56. 6, 195, 213, 47, 200, 102, 30, 215,
  57. 8, 232, 234, 222, 128, 82, 238, 247,
  58. 132, 170, 114, 172, 53, 77, 106, 42,
  59. 150, 26, 210, 113, 90, 21, 73, 116,
  60. 75, 159, 208, 94, 4, 24, 164, 236,
  61. 194, 224, 65, 110, 15, 81, 203, 204,
  62. 36, 145, 175, 80, 161, 244, 112, 57,
  63. 153, 124, 58, 133, 35, 184, 180, 122,
  64. 252, 2, 54, 91, 37, 85, 151, 49,
  65. 45, 93, 250, 152, 227, 138, 146, 174,
  66. 5, 223, 41, 16, 103, 108, 186, 201,
  67. 211, 0, 230, 207, 225, 158, 168, 44,
  68. 99, 22, 1, 63, 88, 226, 137, 169,
  69. 13, 56, 52, 27, 171, 51, 255, 176,
  70. 187, 72, 12, 95, 185, 177, 205, 46,
  71. 197, 243, 219, 71, 229, 165, 156, 119,
  72. 10, 166, 32, 104, 254, 127, 193, 173
  73. };
  74. #define rotl16(x,n) (((x) << ((u16)(n))) | ((x) >> (16 - (u16)(n))))
  75. #define rotr16(x,n) (((x) >> ((u16)(n))) | ((x) << (16 - (u16)(n))))
  76. static const char *selftest (void);
  77. static void
  78. do_encrypt (void *context, unsigned char *outbuf, const unsigned char *inbuf)
  79. {
  80. RFC2268_context *ctx = context;
  81. register int i, j;
  82. u16 word0 = 0, word1 = 0, word2 = 0, word3 = 0;
  83. word0 = (word0 << 8) | inbuf[1];
  84. word0 = (word0 << 8) | inbuf[0];
  85. word1 = (word1 << 8) | inbuf[3];
  86. word1 = (word1 << 8) | inbuf[2];
  87. word2 = (word2 << 8) | inbuf[5];
  88. word2 = (word2 << 8) | inbuf[4];
  89. word3 = (word3 << 8) | inbuf[7];
  90. word3 = (word3 << 8) | inbuf[6];
  91. for (i = 0; i < 16; i++)
  92. {
  93. j = i * 4;
  94. /* For some reason I cannot combine those steps. */
  95. word0 += (word1 & ~word3) + (word2 & word3) + ctx->S[j];
  96. word0 = rotl16(word0, 1);
  97. word1 += (word2 & ~word0) + (word3 & word0) + ctx->S[j + 1];
  98. word1 = rotl16(word1, 2);
  99. word2 += (word3 & ~word1) + (word0 & word1) + ctx->S[j + 2];
  100. word2 = rotl16(word2, 3);
  101. word3 += (word0 & ~word2) + (word1 & word2) + ctx->S[j + 3];
  102. word3 = rotl16(word3, 5);
  103. if (i == 4 || i == 10)
  104. {
  105. word0 += ctx->S[word3 & 63];
  106. word1 += ctx->S[word0 & 63];
  107. word2 += ctx->S[word1 & 63];
  108. word3 += ctx->S[word2 & 63];
  109. }
  110. }
  111. outbuf[0] = word0 & 255;
  112. outbuf[1] = word0 >> 8;
  113. outbuf[2] = word1 & 255;
  114. outbuf[3] = word1 >> 8;
  115. outbuf[4] = word2 & 255;
  116. outbuf[5] = word2 >> 8;
  117. outbuf[6] = word3 & 255;
  118. outbuf[7] = word3 >> 8;
  119. }
  120. static void
  121. do_decrypt (void *context, unsigned char *outbuf, const unsigned char *inbuf)
  122. {
  123. RFC2268_context *ctx = context;
  124. register int i, j;
  125. u16 word0 = 0, word1 = 0, word2 = 0, word3 = 0;
  126. word0 = (word0 << 8) | inbuf[1];
  127. word0 = (word0 << 8) | inbuf[0];
  128. word1 = (word1 << 8) | inbuf[3];
  129. word1 = (word1 << 8) | inbuf[2];
  130. word2 = (word2 << 8) | inbuf[5];
  131. word2 = (word2 << 8) | inbuf[4];
  132. word3 = (word3 << 8) | inbuf[7];
  133. word3 = (word3 << 8) | inbuf[6];
  134. for (i = 15; i >= 0; i--)
  135. {
  136. j = i * 4;
  137. word3 = rotr16(word3, 5);
  138. word3 -= (word0 & ~word2) + (word1 & word2) + ctx->S[j + 3];
  139. word2 = rotr16(word2, 3);
  140. word2 -= (word3 & ~word1) + (word0 & word1) + ctx->S[j + 2];
  141. word1 = rotr16(word1, 2);
  142. word1 -= (word2 & ~word0) + (word3 & word0) + ctx->S[j + 1];
  143. word0 = rotr16(word0, 1);
  144. word0 -= (word1 & ~word3) + (word2 & word3) + ctx->S[j];
  145. if (i == 5 || i == 11)
  146. {
  147. word3 = word3 - ctx->S[word2 & 63];
  148. word2 = word2 - ctx->S[word1 & 63];
  149. word1 = word1 - ctx->S[word0 & 63];
  150. word0 = word0 - ctx->S[word3 & 63];
  151. }
  152. }
  153. outbuf[0] = word0 & 255;
  154. outbuf[1] = word0 >> 8;
  155. outbuf[2] = word1 & 255;
  156. outbuf[3] = word1 >> 8;
  157. outbuf[4] = word2 & 255;
  158. outbuf[5] = word2 >> 8;
  159. outbuf[6] = word3 & 255;
  160. outbuf[7] = word3 >> 8;
  161. }
  162. static gpg_err_code_t
  163. setkey_core (void *context, const unsigned char *key, unsigned int keylen, int with_phase2)
  164. {
  165. static int initialized;
  166. static const char *selftest_failed;
  167. RFC2268_context *ctx = context;
  168. unsigned int i;
  169. unsigned char *S, x;
  170. int len;
  171. int bits = keylen * 8;
  172. if (!initialized)
  173. {
  174. initialized = 1;
  175. selftest_failed = selftest ();
  176. if (selftest_failed)
  177. log_error ("RFC2268 selftest failed (%s).\n", selftest_failed);
  178. }
  179. if (selftest_failed)
  180. return GPG_ERR_SELFTEST_FAILED;
  181. if (keylen < 40 / 8) /* We want at least 40 bits. */
  182. return GPG_ERR_INV_KEYLEN;
  183. S = (unsigned char *) ctx->S;
  184. for (i = 0; i < keylen; i++)
  185. S[i] = key[i];
  186. for (i = keylen; i < 128; i++)
  187. S[i] = rfc2268_sbox[(S[i - keylen] + S[i - 1]) & 255];
  188. S[0] = rfc2268_sbox[S[0]];
  189. /* Phase 2 - reduce effective key size to "bits". This was not
  190. * discussed in Gutmann's paper. I've copied that from the public
  191. * domain code posted in sci.crypt. */
  192. if (with_phase2)
  193. {
  194. len = (bits + 7) >> 3;
  195. i = 128 - len;
  196. x = rfc2268_sbox[S[i] & (255 >> (7 & -bits))];
  197. S[i] = x;
  198. while (i--)
  199. {
  200. x = rfc2268_sbox[x ^ S[i + len]];
  201. S[i] = x;
  202. }
  203. }
  204. /* Make the expanded key, endian independent. */
  205. for (i = 0; i < 64; i++)
  206. ctx->S[i] = ( (u16) S[i * 2] | (((u16) S[i * 2 + 1]) << 8));
  207. return 0;
  208. }
  209. static gpg_err_code_t
  210. do_setkey (void *context, const unsigned char *key, unsigned int keylen)
  211. {
  212. return setkey_core (context, key, keylen, 1);
  213. }
  214. static const char *
  215. selftest (void)
  216. {
  217. RFC2268_context ctx;
  218. unsigned char scratch[16];
  219. /* Test vectors from Peter Gutmann's paper. */
  220. static unsigned char key_1[] =
  221. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  222. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  223. };
  224. static unsigned char plaintext_1[] =
  225. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  226. static const unsigned char ciphertext_1[] =
  227. { 0x1C, 0x19, 0x8A, 0x83, 0x8D, 0xF0, 0x28, 0xB7 };
  228. static unsigned char key_2[] =
  229. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  230. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  231. };
  232. static unsigned char plaintext_2[] =
  233. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  234. static unsigned char ciphertext_2[] =
  235. { 0x50, 0xDC, 0x01, 0x62, 0xBD, 0x75, 0x7F, 0x31 };
  236. /* This one was checked against libmcrypt's RFC2268. */
  237. static unsigned char key_3[] =
  238. { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  239. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  240. };
  241. static unsigned char plaintext_3[] =
  242. { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  243. static unsigned char ciphertext_3[] =
  244. { 0x8f, 0xd1, 0x03, 0x89, 0x33, 0x6b, 0xf9, 0x5e };
  245. /* First test. */
  246. setkey_core (&ctx, key_1, sizeof(key_1), 0);
  247. do_encrypt (&ctx, scratch, plaintext_1);
  248. if (memcmp (scratch, ciphertext_1, sizeof(ciphertext_1)))
  249. return "RFC2268 encryption test 1 failed.";
  250. setkey_core (&ctx, key_1, sizeof(key_1), 0);
  251. do_decrypt (&ctx, scratch, scratch);
  252. if (memcmp (scratch, plaintext_1, sizeof(plaintext_1)))
  253. return "RFC2268 decryption test 1 failed.";
  254. /* Second test. */
  255. setkey_core (&ctx, key_2, sizeof(key_2), 0);
  256. do_encrypt (&ctx, scratch, plaintext_2);
  257. if (memcmp (scratch, ciphertext_2, sizeof(ciphertext_2)))
  258. return "RFC2268 encryption test 2 failed.";
  259. setkey_core (&ctx, key_2, sizeof(key_2), 0);
  260. do_decrypt (&ctx, scratch, scratch);
  261. if (memcmp (scratch, plaintext_2, sizeof(plaintext_2)))
  262. return "RFC2268 decryption test 2 failed.";
  263. /* Third test. */
  264. setkey_core(&ctx, key_3, sizeof(key_3), 0);
  265. do_encrypt(&ctx, scratch, plaintext_3);
  266. if (memcmp(scratch, ciphertext_3, sizeof(ciphertext_3)))
  267. return "RFC2268 encryption test 3 failed.";
  268. setkey_core (&ctx, key_3, sizeof(key_3), 0);
  269. do_decrypt (&ctx, scratch, scratch);
  270. if (memcmp(scratch, plaintext_3, sizeof(plaintext_3)))
  271. return "RFC2268 decryption test 3 failed.";
  272. return NULL;
  273. }
  274. static gcry_cipher_oid_spec_t oids_rfc2268_40[] =
  275. {
  276. /*{ "1.2.840.113549.3.2", GCRY_CIPHER_MODE_CBC },*/
  277. /* pbeWithSHAAnd40BitRC2_CBC */
  278. { "1.2.840.113549.1.12.1.6", GCRY_CIPHER_MODE_CBC },
  279. { NULL }
  280. };
  281. gcry_cipher_spec_t _gcry_cipher_spec_rfc2268_40 = {
  282. "RFC2268_40", NULL, oids_rfc2268_40,
  283. RFC2268_BLOCKSIZE, 40, sizeof(RFC2268_context),
  284. do_setkey, do_encrypt, do_decrypt
  285. };