padlock.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * VIA PadLock support functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * This implementation is based on the VIA PadLock Programming Guide:
  9. *
  10. * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
  11. * programming_guide.pdf
  12. */
  13. #include "common.h"
  14. #if defined(MBEDTLS_PADLOCK_C)
  15. #include "padlock.h"
  16. #include <string.h>
  17. #if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE)
  18. /*
  19. * PadLock detection routine
  20. */
  21. int mbedtls_padlock_has_support(int feature)
  22. {
  23. static int flags = -1;
  24. int ebx = 0, edx = 0;
  25. if (flags == -1) {
  26. asm ("movl %%ebx, %0 \n\t"
  27. "movl $0xC0000000, %%eax \n\t"
  28. "cpuid \n\t"
  29. "cmpl $0xC0000001, %%eax \n\t"
  30. "movl $0, %%edx \n\t"
  31. "jb 1f \n\t"
  32. "movl $0xC0000001, %%eax \n\t"
  33. "cpuid \n\t"
  34. "1: \n\t"
  35. "movl %%edx, %1 \n\t"
  36. "movl %2, %%ebx \n\t"
  37. : "=m" (ebx), "=m" (edx)
  38. : "m" (ebx)
  39. : "eax", "ecx", "edx");
  40. flags = edx;
  41. }
  42. return flags & feature;
  43. }
  44. /*
  45. * PadLock AES-ECB block en(de)cryption
  46. */
  47. int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
  48. int mode,
  49. const unsigned char input[16],
  50. unsigned char output[16])
  51. {
  52. int ebx = 0;
  53. uint32_t *rk;
  54. uint32_t *blk;
  55. uint32_t *ctrl;
  56. unsigned char buf[256];
  57. rk = ctx->buf + ctx->rk_offset;
  58. if (((long) rk & 15) != 0) {
  59. return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
  60. }
  61. blk = MBEDTLS_PADLOCK_ALIGN16(buf);
  62. memcpy(blk, input, 16);
  63. ctrl = blk + 4;
  64. *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9);
  65. asm ("pushfl \n\t"
  66. "popfl \n\t"
  67. "movl %%ebx, %0 \n\t"
  68. "movl $1, %%ecx \n\t"
  69. "movl %2, %%edx \n\t"
  70. "movl %3, %%ebx \n\t"
  71. "movl %4, %%esi \n\t"
  72. "movl %4, %%edi \n\t"
  73. ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
  74. "movl %1, %%ebx \n\t"
  75. : "=m" (ebx)
  76. : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
  77. : "memory", "ecx", "edx", "esi", "edi");
  78. memcpy(output, blk, 16);
  79. return 0;
  80. }
  81. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  82. /*
  83. * PadLock AES-CBC buffer en(de)cryption
  84. */
  85. int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
  86. int mode,
  87. size_t length,
  88. unsigned char iv[16],
  89. const unsigned char *input,
  90. unsigned char *output)
  91. {
  92. int ebx = 0;
  93. size_t count;
  94. uint32_t *rk;
  95. uint32_t *iw;
  96. uint32_t *ctrl;
  97. unsigned char buf[256];
  98. rk = ctx->buf + ctx->rk_offset;
  99. if (((long) input & 15) != 0 ||
  100. ((long) output & 15) != 0 ||
  101. ((long) rk & 15) != 0) {
  102. return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
  103. }
  104. iw = MBEDTLS_PADLOCK_ALIGN16(buf);
  105. memcpy(iw, iv, 16);
  106. ctrl = iw + 4;
  107. *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
  108. count = (length + 15) >> 4;
  109. asm ("pushfl \n\t"
  110. "popfl \n\t"
  111. "movl %%ebx, %0 \n\t"
  112. "movl %2, %%ecx \n\t"
  113. "movl %3, %%edx \n\t"
  114. "movl %4, %%ebx \n\t"
  115. "movl %5, %%esi \n\t"
  116. "movl %6, %%edi \n\t"
  117. "movl %7, %%eax \n\t"
  118. ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
  119. "movl %1, %%ebx \n\t"
  120. : "=m" (ebx)
  121. : "m" (ebx), "m" (count), "m" (ctrl),
  122. "m" (rk), "m" (input), "m" (output), "m" (iw)
  123. : "memory", "eax", "ecx", "edx", "esi", "edi");
  124. memcpy(iv, iw, 16);
  125. return 0;
  126. }
  127. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  128. #endif /* MBEDTLS_VIA_PADLOCK_HAVE_CODE */
  129. #endif /* MBEDTLS_PADLOCK_C */