cryptodev.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. NULL, /* open */
  72. NULL, /* close */
  73. cryptodev_read, /* read */
  74. cryptodev_write, /* write */
  75. NULL, /* seek */
  76. cryptodev_ioctl, /* ioctl */
  77. NULL /* poll */
  78. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  79. , NULL /* unlink */
  80. #endif
  81. };
  82. /****************************************************************************
  83. * Private Functions
  84. ****************************************************************************/
  85. static ssize_t cryptodev_read(FAR struct file *filep, FAR char *buffer,
  86. size_t len)
  87. {
  88. return -EACCES;
  89. }
  90. static ssize_t cryptodev_write(FAR struct file *filep, FAR const char *buffer,
  91. size_t len)
  92. {
  93. return -EACCES;
  94. }
  95. static int cryptodev_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  96. {
  97. switch (cmd)
  98. {
  99. case CIOCGSESSION:
  100. {
  101. FAR struct session_op *ses = (FAR struct session_op *)arg;
  102. ses->ses = (uint32_t)ses;
  103. return OK;
  104. }
  105. case CIOCFSESSION:
  106. {
  107. return OK;
  108. }
  109. #ifdef CONFIG_CRYPTO_AES
  110. case CIOCCRYPT:
  111. {
  112. FAR struct crypt_op *op = (FAR struct crypt_op *)arg;
  113. FAR struct session_op *ses = (FAR struct session_op *)op->ses;
  114. int encrypt;
  115. switch (op->op)
  116. {
  117. case COP_ENCRYPT:
  118. encrypt = 1;
  119. break;
  120. case COP_DECRYPT:
  121. encrypt = 0;
  122. break;
  123. default:
  124. return -EINVAL;
  125. }
  126. switch (ses->cipher)
  127. {
  128. case CRYPTO_AES_ECB:
  129. return AES_CYPHER(AES_MODE_ECB);
  130. case CRYPTO_AES_CBC:
  131. return AES_CYPHER(AES_MODE_CBC);
  132. case CRYPTO_AES_CTR:
  133. return AES_CYPHER(AES_MODE_CTR);
  134. default:
  135. return -EINVAL;
  136. }
  137. }
  138. #endif
  139. default:
  140. return -ENOTTY;
  141. }
  142. }
  143. /****************************************************************************
  144. * Public Functions
  145. ****************************************************************************/
  146. void devcrypto_register(void)
  147. {
  148. (void)register_driver("/dev/crypto", &g_cryptodevops, 0666, NULL);
  149. }