cipheraes.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <mp.h>
  12. #include <fcall.h>
  13. #include <thread.h>
  14. #include <9p.h>
  15. #include <libsec.h>
  16. #include "netssh.h"
  17. static QLock aeslock;
  18. struct CipherState {
  19. AESstate state;
  20. };
  21. static CipherState *
  22. initaes(Conn *c, int dir, int bits)
  23. {
  24. CipherState *cs;
  25. qlock(&aeslock);
  26. cs = emalloc9p(sizeof(CipherState));
  27. if(dir)
  28. setupAESstate(&cs->state, c->s2cek, bits/8, c->s2civ);
  29. else
  30. setupAESstate(&cs->state, c->c2sek, bits/8, c->c2siv);
  31. qunlock(&aeslock);
  32. return cs;
  33. }
  34. static CipherState*
  35. initaes128(Conn *c, int dir)
  36. {
  37. return initaes(c, dir, 128);
  38. }
  39. static CipherState*
  40. initaes192(Conn *c, int dir)
  41. {
  42. return initaes(c, dir, 192);
  43. }
  44. static CipherState*
  45. initaes256(Conn *c, int dir)
  46. {
  47. return initaes(c, dir, 256);
  48. }
  49. static void
  50. encryptaes(CipherState *cs, uint8_t *buf, int nbuf)
  51. {
  52. if(cs->state.setup != 0xcafebabe || cs->state.rounds > AESmaxrounds)
  53. return;
  54. qlock(&aeslock);
  55. aesCBCencrypt(buf, nbuf, &cs->state);
  56. qunlock(&aeslock);
  57. }
  58. static void
  59. decryptaes(CipherState *cs, uint8_t *buf, int nbuf)
  60. {
  61. if(cs->state.setup != 0xcafebabe || cs->state.rounds > AESmaxrounds)
  62. return;
  63. qlock(&aeslock);
  64. aesCBCdecrypt(buf, nbuf, &cs->state);
  65. qunlock(&aeslock);
  66. }
  67. Cipher cipheraes128 = {
  68. "aes128-cbc",
  69. AESbsize,
  70. initaes128,
  71. encryptaes,
  72. decryptaes,
  73. };
  74. Cipher cipheraes192 = {
  75. "aes192-cbc",
  76. AESbsize,
  77. initaes192,
  78. encryptaes,
  79. decryptaes,
  80. };
  81. Cipher cipheraes256 = {
  82. "aes256-cbc",
  83. AESbsize,
  84. initaes256,
  85. encryptaes,
  86. decryptaes,
  87. };