ssh_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. #include "tomcrypt_test.h"
  10. /**
  11. @file ssh_test.c
  12. Support for SSH data formats (RFC4251), Russ Williams
  13. */
  14. #ifdef LTC_SSH
  15. #define BUFSIZE 64
  16. /**
  17. Test vectors from from RFC4251, section 5
  18. uint32: "the value 699921578 (0x29b7f4aa) is stored as 29 b7 f4 aa"
  19. string: "the US-ASCII string "testing" is represented as 00 00 00 07 t e s t i n g"
  20. mpint:
  21. value (hex) representation (hex)
  22. ----------- --------------------
  23. 0 00 00 00 00
  24. 9a378f9b2e332a7 00 00 00 08 09 a3 78 f9 b2 e3 32 a7
  25. 80 00 00 00 02 00 80
  26. -1234 00 00 00 02 ed cc
  27. -deadbeef 00 00 00 05 ff 21 52 41 11
  28. name-list:
  29. value representation (hex)
  30. ----- --------------------
  31. (), the empty name-list 00 00 00 00
  32. ("zlib") 00 00 00 04 7a 6c 69 62
  33. ("zlib,none") 00 00 00 09 7a 6c 69 62 2c 6e 6f 6e 65
  34. */
  35. static const unsigned char byte1[] = {0x01};
  36. static const unsigned char byte2[] = {0x71};
  37. static const unsigned char uint32[] = {0x29, 0xb7, 0xf4, 0xaa};
  38. static const unsigned char uint64[] = {0x09, 0xa3, 0x78, 0xf9, 0xb2, 0xe3, 0x32, 0xa7};
  39. static const unsigned char string[] = {0x00, 0x00, 0x00, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67};
  40. static const unsigned char mpint1[] = {0x00, 0x00, 0x00, 0x00};
  41. static const unsigned char mpint2[] = {0x00, 0x00, 0x00, 0x08, 0x09, 0xa3, 0x78, 0xf9, 0xb2, 0xe3, 0x32, 0xa7};
  42. static const unsigned char mpint3[] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x80};
  43. static const unsigned char nlist1[] = {0x00, 0x00, 0x00, 0x00};
  44. static const unsigned char nlist2[] = {0x00, 0x00, 0x00, 0x04, 0x7a, 0x6c, 0x69, 0x62};
  45. static const unsigned char nlist3[] = {0x00, 0x00, 0x00, 0x09, 0x7a, 0x6c, 0x69, 0x62, 0x2c, 0x6e, 0x6f, 0x6e, 0x65};
  46. /**
  47. LTC_SSH encoding test
  48. @return CRYPT_OK if successful
  49. */
  50. static int _ssh_encoding_test(void)
  51. {
  52. unsigned char buffer[BUFSIZE];
  53. unsigned long buflen;
  54. void *v, *zero;
  55. int err;
  56. /* Buffer too short */
  57. buflen = 3;
  58. zeromem(buffer, BUFSIZE);
  59. err = ssh_encode_sequence_multi(buffer, &buflen,
  60. LTC_SSHDATA_UINT32, 0x29b7f4aa,
  61. LTC_SSHDATA_EOL, NULL);
  62. if (err != CRYPT_BUFFER_OVERFLOW) return CRYPT_FAIL_TESTVECTOR;
  63. /* byte */
  64. buflen = BUFSIZE;
  65. zeromem(buffer, BUFSIZE);
  66. DO(ssh_encode_sequence_multi(buffer, &buflen,
  67. LTC_SSHDATA_BYTE, 0x01,
  68. LTC_SSHDATA_EOL, NULL));
  69. COMPARE_TESTVECTOR(buffer, buflen, byte1, sizeof(byte1), "enc-byte", 1);
  70. buflen = BUFSIZE;
  71. zeromem(buffer, BUFSIZE);
  72. DO(ssh_encode_sequence_multi(buffer, &buflen,
  73. LTC_SSHDATA_BYTE, 0x71,
  74. LTC_SSHDATA_EOL, NULL));
  75. COMPARE_TESTVECTOR(buffer, buflen, byte2, sizeof(byte2), "enc-byte", 2);
  76. if (XMEMCMP(buffer, byte2, buflen) != 0) return CRYPT_FAIL_TESTVECTOR;
  77. /* boolean */
  78. buflen = BUFSIZE;
  79. zeromem(buffer, BUFSIZE);
  80. DO(ssh_encode_sequence_multi(buffer, &buflen,
  81. LTC_SSHDATA_BOOLEAN, 0x01,
  82. LTC_SSHDATA_EOL, NULL));
  83. COMPARE_TESTVECTOR(buffer, buflen, byte1, sizeof(byte1), "enc-boolean", 1);
  84. buflen = BUFSIZE;
  85. zeromem(buffer, BUFSIZE);
  86. DO(ssh_encode_sequence_multi(buffer, &buflen,
  87. LTC_SSHDATA_BOOLEAN, 0x71,
  88. LTC_SSHDATA_EOL, NULL));
  89. /* Should be written out as 0x01 */
  90. COMPARE_TESTVECTOR(buffer, buflen, byte1, sizeof(byte1), "enc-boolean", 2);
  91. /* uint32 */
  92. buflen = BUFSIZE;
  93. zeromem(buffer, BUFSIZE);
  94. DO(ssh_encode_sequence_multi(buffer, &buflen,
  95. LTC_SSHDATA_UINT32, 0x29b7f4aa,
  96. LTC_SSHDATA_EOL, NULL));
  97. COMPARE_TESTVECTOR(buffer, buflen, uint32, sizeof(uint32), "enc-uint32", 1);
  98. /* uint64 */
  99. buflen = BUFSIZE;
  100. zeromem(buffer, BUFSIZE);
  101. DO(ssh_encode_sequence_multi(buffer, &buflen,
  102. LTC_SSHDATA_UINT64, CONST64(0x09a378f9b2e332a7),
  103. LTC_SSHDATA_EOL, NULL));
  104. COMPARE_TESTVECTOR(buffer, buflen, uint64, sizeof(uint64), "enc-uint64", 1);
  105. /* string */
  106. buflen = BUFSIZE;
  107. zeromem(buffer, BUFSIZE);
  108. DO(ssh_encode_sequence_multi(buffer, &buflen,
  109. LTC_SSHDATA_STRING, "testing",
  110. LTC_SSHDATA_EOL, NULL));
  111. COMPARE_TESTVECTOR(buffer, buflen, string, sizeof(string), "enc-string", 1);
  112. /* mpint */
  113. if ((err = mp_init_multi(&zero, &v, NULL)) != CRYPT_OK) {
  114. return err;
  115. }
  116. buflen = BUFSIZE;
  117. zeromem(buffer, BUFSIZE);
  118. DO(mp_set(zero, 0));
  119. DO(ssh_encode_sequence_multi(buffer, &buflen,
  120. LTC_SSHDATA_MPINT, zero,
  121. LTC_SSHDATA_EOL, NULL));
  122. COMPARE_TESTVECTOR(buffer, buflen, mpint1, sizeof(mpint1), "enc-mpint", 1);
  123. buflen = BUFSIZE;
  124. zeromem(buffer, BUFSIZE);
  125. DO(mp_read_radix(v, "9a378f9b2e332a7", 16));
  126. DO(ssh_encode_sequence_multi(buffer, &buflen,
  127. LTC_SSHDATA_MPINT, v,
  128. LTC_SSHDATA_EOL, NULL));
  129. COMPARE_TESTVECTOR(buffer, buflen, mpint2, sizeof(mpint2), "enc-mpint", 2);
  130. buflen = BUFSIZE;
  131. zeromem(buffer, BUFSIZE);
  132. DO(mp_set(v, 0x80));
  133. DO(ssh_encode_sequence_multi(buffer, &buflen,
  134. LTC_SSHDATA_MPINT, v,
  135. LTC_SSHDATA_EOL, NULL));
  136. COMPARE_TESTVECTOR(buffer, buflen, mpint3, sizeof(mpint3), "enc-mpint", 3);
  137. mp_clear_multi(v, zero, NULL);
  138. /* name-list */
  139. buflen = BUFSIZE;
  140. zeromem(buffer, BUFSIZE);
  141. DO(ssh_encode_sequence_multi(buffer, &buflen,
  142. LTC_SSHDATA_NAMELIST, "",
  143. LTC_SSHDATA_EOL, NULL));
  144. COMPARE_TESTVECTOR(buffer, buflen, nlist1, sizeof(nlist1), "enc-nlist", 1);
  145. buflen = BUFSIZE;
  146. zeromem(buffer, BUFSIZE);
  147. DO(ssh_encode_sequence_multi(buffer, &buflen,
  148. LTC_SSHDATA_NAMELIST, "zlib",
  149. LTC_SSHDATA_EOL, NULL));
  150. COMPARE_TESTVECTOR(buffer, buflen, nlist2, sizeof(nlist2), "enc-nlist", 2);
  151. buflen = BUFSIZE;
  152. zeromem(buffer, BUFSIZE);
  153. DO(ssh_encode_sequence_multi(buffer, &buflen,
  154. LTC_SSHDATA_NAMELIST, "zlib,none",
  155. LTC_SSHDATA_EOL, NULL));
  156. COMPARE_TESTVECTOR(buffer, buflen, nlist3, sizeof(nlist3), "enc-nlist", 3);
  157. return CRYPT_OK;
  158. }
  159. /**
  160. LTC_SSH decoding test
  161. @return CRYPT_OK if successful
  162. */
  163. static int _ssh_decoding_test(void)
  164. {
  165. char strbuf[BUFSIZE];
  166. void *u, *v;
  167. ulong32 tmp32;
  168. ulong64 tmp64;
  169. unsigned char tmp8;
  170. int err;
  171. /* byte */
  172. DO(ssh_decode_sequence_multi(byte1, sizeof(byte1),
  173. LTC_SSHDATA_BYTE, &tmp8,
  174. LTC_SSHDATA_EOL, NULL));
  175. if (tmp8 != 0x01) return CRYPT_FAIL_TESTVECTOR;
  176. DO(ssh_decode_sequence_multi(byte2, sizeof(byte2),
  177. LTC_SSHDATA_BYTE, &tmp8,
  178. LTC_SSHDATA_EOL, NULL));
  179. if (tmp8 != 0x71) return CRYPT_FAIL_TESTVECTOR;
  180. /* boolean */
  181. DO(ssh_decode_sequence_multi(byte1, sizeof(byte1),
  182. LTC_SSHDATA_BOOLEAN, &tmp8,
  183. LTC_SSHDATA_EOL, NULL));
  184. if (tmp8 != 0x01) return CRYPT_FAIL_TESTVECTOR;
  185. DO(ssh_decode_sequence_multi(byte2, sizeof(byte2),
  186. LTC_SSHDATA_BOOLEAN, &tmp8,
  187. LTC_SSHDATA_EOL, NULL));
  188. if (tmp8 != 0x01) return CRYPT_FAIL_TESTVECTOR;
  189. /* uint32 */
  190. DO(ssh_decode_sequence_multi(uint32, sizeof(uint32),
  191. LTC_SSHDATA_UINT32, &tmp32,
  192. LTC_SSHDATA_EOL, NULL));
  193. if (tmp32 != 0x29b7f4aa) return CRYPT_FAIL_TESTVECTOR;
  194. /* uint64 */
  195. DO(ssh_decode_sequence_multi(uint64, sizeof(uint64),
  196. LTC_SSHDATA_UINT64, &tmp64,
  197. LTC_SSHDATA_EOL, NULL));
  198. if (tmp64 != CONST64(0x09a378f9b2e332a7)) return CRYPT_FAIL_TESTVECTOR;
  199. /* string */
  200. zeromem(strbuf, BUFSIZE);
  201. DO(ssh_decode_sequence_multi(string, sizeof(string),
  202. LTC_SSHDATA_STRING, strbuf, BUFSIZE,
  203. LTC_SSHDATA_EOL, NULL));
  204. if (XSTRCMP(strbuf, "testing") != 0) return CRYPT_FAIL_TESTVECTOR;
  205. /* mpint */
  206. if ((err = mp_init_multi(&u, &v, NULL)) != CRYPT_OK) {
  207. return err;
  208. }
  209. DO(ssh_decode_sequence_multi(mpint1, sizeof(mpint1),
  210. LTC_SSHDATA_MPINT, v,
  211. LTC_SSHDATA_EOL, NULL));
  212. if (mp_cmp_d(v, 0) != LTC_MP_EQ) return CRYPT_FAIL_TESTVECTOR;
  213. DO(mp_read_radix(u, "9a378f9b2e332a7", 16));
  214. DO(ssh_decode_sequence_multi(mpint2, sizeof(mpint2),
  215. LTC_SSHDATA_MPINT, v,
  216. LTC_SSHDATA_EOL, NULL));
  217. if (mp_cmp(u, v) != LTC_MP_EQ) return CRYPT_FAIL_TESTVECTOR;
  218. DO(ssh_decode_sequence_multi(mpint3, sizeof(mpint3),
  219. LTC_SSHDATA_MPINT, v,
  220. LTC_SSHDATA_EOL, NULL));
  221. if (mp_cmp_d(v, 0x80) != LTC_MP_EQ) return CRYPT_FAIL_TESTVECTOR;
  222. mp_clear_multi(v, u, NULL);
  223. /* name-list */
  224. zeromem(strbuf, BUFSIZE);
  225. DO(ssh_decode_sequence_multi(nlist1, sizeof(nlist1),
  226. LTC_SSHDATA_NAMELIST, strbuf, BUFSIZE,
  227. LTC_SSHDATA_EOL, NULL));
  228. if (XSTRCMP(strbuf, "") != 0) return CRYPT_FAIL_TESTVECTOR;
  229. zeromem(strbuf, BUFSIZE);
  230. DO(ssh_decode_sequence_multi(nlist2, sizeof(nlist2),
  231. LTC_SSHDATA_NAMELIST, strbuf, BUFSIZE,
  232. LTC_SSHDATA_EOL, NULL));
  233. if (XSTRCMP(strbuf, "zlib") != 0) return CRYPT_FAIL_TESTVECTOR;
  234. zeromem(strbuf, BUFSIZE);
  235. DO(ssh_decode_sequence_multi(nlist3, sizeof(nlist3),
  236. LTC_SSHDATA_NAMELIST, strbuf, BUFSIZE,
  237. LTC_SSHDATA_EOL, NULL));
  238. if (XSTRCMP(strbuf, "zlib,none") != 0) return CRYPT_FAIL_TESTVECTOR;
  239. return CRYPT_OK;
  240. }
  241. /**
  242. LTC_SSH self-test
  243. @return CRYPT_OK if successful, CRYPT_NOP if tests have been disabled.
  244. */
  245. int ssh_test(void)
  246. {
  247. if (ltc_mp.name == NULL) return CRYPT_NOP;
  248. DO(_ssh_encoding_test());
  249. DO(_ssh_decoding_test());
  250. return CRYPT_OK;
  251. }
  252. #else
  253. int ssh_test(void)
  254. {
  255. return CRYPT_NOP;
  256. }
  257. #endif
  258. /* ref: $Format:%D$ */
  259. /* git commit: $Format:%H$ */
  260. /* commit time: $Format:%ai$ */