cryptodev.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /****************************************************************************
  2. * crypto/cryptodev.c
  3. *
  4. * Copyright (C) 2014 Gregory Nutt. All rights reserved.
  5. * Author: Max Nekludov <macscomp@gmail.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdbool.h>
  41. #include <string.h>
  42. #include <poll.h>
  43. #include <errno.h>
  44. #include <nuttx/fs/fs.h>
  45. #include <nuttx/drivers/drivers.h>
  46. #include <nuttx/crypto/crypto.h>
  47. #include <nuttx/crypto/cryptodev.h>
  48. /****************************************************************************
  49. * Pre-processor Definitions
  50. ****************************************************************************/
  51. #ifdef CONFIG_CRYPTO_AES
  52. # define AES_CYPHER(mode) \
  53. aes_cypher(op->dst, op->src, op->len, op->iv, ses->key, ses->keylen, \
  54. mode, encrypt)
  55. #endif
  56. /****************************************************************************
  57. * Private Function Prototypes
  58. ****************************************************************************/
  59. /* Character driver methods */
  60. static ssize_t cryptodev_read(FAR struct file *filep, FAR char *buffer,
  61. size_t len);
  62. static ssize_t cryptodev_write(FAR struct file *filep, FAR const char *buffer,
  63. size_t len);
  64. static int cryptodev_ioctl(FAR struct file *filep, int cmd,
  65. unsigned long arg);
  66. /****************************************************************************
  67. * Private Data
  68. ****************************************************************************/
  69. static const struct file_operations g_cryptodevops =
  70. {
  71. 0, /* open */
  72. 0, /* close */
  73. cryptodev_read, /* read */
  74. cryptodev_write, /* write */
  75. 0, /* seek */
  76. cryptodev_ioctl /* ioctl */
  77. #ifndef CONFIG_DISABLE_POLL
  78. , 0 /* poll */
  79. #endif
  80. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  81. , 0 /* unlink */
  82. #endif
  83. };
  84. /****************************************************************************
  85. * Private Functions
  86. ****************************************************************************/
  87. static ssize_t cryptodev_read(FAR struct file *filep, FAR char *buffer,
  88. size_t len)
  89. {
  90. return -EACCES;
  91. }
  92. static ssize_t cryptodev_write(FAR struct file *filep, FAR const char *buffer,
  93. size_t len)
  94. {
  95. return -EACCES;
  96. }
  97. static int cryptodev_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  98. {
  99. switch (cmd)
  100. {
  101. case CIOCGSESSION:
  102. {
  103. FAR struct session_op *ses = (FAR struct session_op *)arg;
  104. ses->ses = (uint32_t)ses;
  105. return OK;
  106. }
  107. case CIOCFSESSION:
  108. {
  109. return OK;
  110. }
  111. #ifdef CONFIG_CRYPTO_AES
  112. case CIOCCRYPT:
  113. {
  114. FAR struct crypt_op *op = (FAR struct crypt_op *)arg;
  115. FAR struct session_op *ses = (FAR struct session_op *)op->ses;
  116. int encrypt;
  117. switch (op->op)
  118. {
  119. case COP_ENCRYPT:
  120. encrypt = 1;
  121. break;
  122. case COP_DECRYPT:
  123. encrypt = 0;
  124. break;
  125. default:
  126. return -EINVAL;
  127. }
  128. switch (ses->cipher)
  129. {
  130. case CRYPTO_AES_ECB:
  131. return AES_CYPHER(AES_MODE_ECB);
  132. case CRYPTO_AES_CBC:
  133. return AES_CYPHER(AES_MODE_CBC);
  134. case CRYPTO_AES_CTR:
  135. return AES_CYPHER(AES_MODE_CTR);
  136. default:
  137. return -EINVAL;
  138. }
  139. }
  140. #endif
  141. default:
  142. return -ENOTTY;
  143. }
  144. }
  145. /****************************************************************************
  146. * Public Functions
  147. ****************************************************************************/
  148. void devcrypto_register(void)
  149. {
  150. (void)register_driver("/dev/crypto", &g_cryptodevops, 0666, NULL);
  151. }