sshauxcrypt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * sshauxcrypt.c: wrapper functions on crypto primitives for use in
  3. * other contexts than the main SSH packet protocol, such as
  4. * encrypting private key files and performing XDM-AUTHORIZATION-1.
  5. *
  6. * These all work through the standard cipher/hash/MAC APIs, so they
  7. * don't need to live in the same actual source files as the ciphers
  8. * they wrap, and I think it keeps things tidier to have them out of
  9. * the way here instead.
  10. */
  11. #include "ssh.h"
  12. static ssh_cipher *aes256_pubkey_cipher(const void *key, const void *iv)
  13. {
  14. /*
  15. * PuTTY's own .PPK format for SSH-2 private key files is
  16. * encrypted with 256-bit AES in CBC mode.
  17. */
  18. { // WINSCP
  19. ssh_cipher *cipher = ssh_cipher_new(&ssh_aes256_cbc);
  20. ssh_cipher_setkey(cipher, key);
  21. ssh_cipher_setiv(cipher, iv);
  22. return cipher;
  23. } // WINSCP
  24. }
  25. void aes256_encrypt_pubkey(const void *key, const void *iv, void *blk, int len)
  26. {
  27. ssh_cipher *c = aes256_pubkey_cipher(key, iv);
  28. ssh_cipher_encrypt(c, blk, len);
  29. ssh_cipher_free(c);
  30. }
  31. void aes256_decrypt_pubkey(const void *key, const void *iv, void *blk, int len)
  32. {
  33. ssh_cipher *c = aes256_pubkey_cipher(key, iv);
  34. ssh_cipher_decrypt(c, blk, len);
  35. ssh_cipher_free(c);
  36. }
  37. static ssh_cipher *des3_pubkey_cipher(const void *vkey)
  38. {
  39. /*
  40. * SSH-1 private key files are encrypted with triple-DES in SSH-1
  41. * style (three separate CBC layers), but the same key is used for
  42. * the first and third layers.
  43. */
  44. ssh_cipher *c = ssh_cipher_new(&ssh_3des_ssh1);
  45. uint8_t keys3[24], iv[8];
  46. memcpy(keys3, vkey, 16);
  47. memcpy(keys3 + 16, vkey, 8);
  48. ssh_cipher_setkey(c, keys3);
  49. smemclr(keys3, sizeof(keys3));
  50. memset(iv, 0, 8);
  51. ssh_cipher_setiv(c, iv);
  52. return c;
  53. }
  54. void des3_decrypt_pubkey(const void *vkey, void *vblk, int len)
  55. {
  56. ssh_cipher *c = des3_pubkey_cipher(vkey);
  57. ssh_cipher_decrypt(c, vblk, len);
  58. ssh_cipher_free(c);
  59. }
  60. void des3_encrypt_pubkey(const void *vkey, void *vblk, int len)
  61. {
  62. ssh_cipher *c = des3_pubkey_cipher(vkey);
  63. ssh_cipher_encrypt(c, vblk, len);
  64. ssh_cipher_free(c);
  65. }
  66. static ssh_cipher *des3_pubkey_ossh_cipher(const void *vkey, const void *viv)
  67. {
  68. /*
  69. * OpenSSH PEM private key files are encrypted with triple-DES in
  70. * SSH-2 style (one CBC layer), with three distinct keys, and an
  71. * IV also generated from the passphrase.
  72. */
  73. ssh_cipher *c = ssh_cipher_new(&ssh_3des_ssh2);
  74. ssh_cipher_setkey(c, vkey);
  75. ssh_cipher_setiv(c, viv);
  76. return c;
  77. }
  78. void des3_decrypt_pubkey_ossh(const void *vkey, const void *viv,
  79. void *vblk, int len)
  80. {
  81. ssh_cipher *c = des3_pubkey_ossh_cipher(vkey, viv);
  82. ssh_cipher_decrypt(c, vblk, len);
  83. ssh_cipher_free(c);
  84. }
  85. void des3_encrypt_pubkey_ossh(const void *vkey, const void *viv,
  86. void *vblk, int len)
  87. {
  88. ssh_cipher *c = des3_pubkey_ossh_cipher(vkey, viv);
  89. ssh_cipher_encrypt(c, vblk, len);
  90. ssh_cipher_free(c);
  91. }
  92. static ssh_cipher *des_xdmauth_cipher(const void *vkeydata)
  93. {
  94. /*
  95. * XDM-AUTHORIZATION-1 uses single-DES, but packs the key into 7
  96. * bytes, so here we have to repack it manually into the canonical
  97. * form where it occupies 8 bytes each with the low bit unused.
  98. */
  99. const unsigned char *keydata = (const unsigned char *)vkeydata;
  100. unsigned char key[8];
  101. int i, nbits, j;
  102. unsigned int bits;
  103. bits = 0;
  104. nbits = 0;
  105. j = 0;
  106. for (i = 0; i < 8; i++) {
  107. if (nbits < 7) {
  108. bits = (bits << 8) | keydata[j];
  109. nbits += 8;
  110. j++;
  111. }
  112. key[i] = (bits >> (nbits - 7)) << 1;
  113. bits &= ~(0x7F << (nbits - 7));
  114. nbits -= 7;
  115. }
  116. { // WINSCP
  117. ssh_cipher *c = ssh_cipher_new(&ssh_des);
  118. ssh_cipher_setkey(c, key);
  119. smemclr(key, sizeof(key));
  120. ssh_cipher_setiv(c, key);
  121. return c;
  122. } // WINSCP
  123. }
  124. void des_encrypt_xdmauth(const void *keydata, void *blk, int len)
  125. {
  126. ssh_cipher *c = des_xdmauth_cipher(keydata);
  127. ssh_cipher_encrypt(c, blk, len);
  128. ssh_cipher_free(c);
  129. }
  130. void des_decrypt_xdmauth(const void *keydata, void *blk, int len)
  131. {
  132. ssh_cipher *c = des_xdmauth_cipher(keydata);
  133. ssh_cipher_decrypt(c, blk, len);
  134. ssh_cipher_free(c);
  135. }
  136. void hash_simple(const ssh_hashalg *alg, ptrlen data, void *output)
  137. {
  138. ssh_hash *hash = ssh_hash_new(alg);
  139. put_datapl(hash, data);
  140. ssh_hash_final(hash, output);
  141. }
  142. void mac_simple(const ssh2_macalg *alg, ptrlen key, ptrlen data, void *output)
  143. {
  144. ssh2_mac *mac = ssh2_mac_new(alg, NULL);
  145. ssh2_mac_setkey(mac, key);
  146. ssh2_mac_start(mac);
  147. put_datapl(mac, data);
  148. ssh2_mac_genresult(mac, output);
  149. ssh2_mac_free(mac);
  150. }