padlock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * VIA PadLock support functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * This implementation is based on the VIA PadLock Programming Guide:
  21. *
  22. * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
  23. * programming_guide.pdf
  24. */
  25. #include "common.h"
  26. #if defined(MBEDTLS_PADLOCK_C)
  27. #include "mbedtls/padlock.h"
  28. #include <string.h>
  29. /* *INDENT-OFF* */
  30. #ifndef asm
  31. #define asm __asm
  32. #endif
  33. /* *INDENT-ON* */
  34. #if defined(MBEDTLS_HAVE_X86)
  35. /*
  36. * PadLock detection routine
  37. */
  38. int mbedtls_padlock_has_support(int feature)
  39. {
  40. static int flags = -1;
  41. int ebx = 0, edx = 0;
  42. if (flags == -1) {
  43. asm ("movl %%ebx, %0 \n\t"
  44. "movl $0xC0000000, %%eax \n\t"
  45. "cpuid \n\t"
  46. "cmpl $0xC0000001, %%eax \n\t"
  47. "movl $0, %%edx \n\t"
  48. "jb 1f \n\t"
  49. "movl $0xC0000001, %%eax \n\t"
  50. "cpuid \n\t"
  51. "1: \n\t"
  52. "movl %%edx, %1 \n\t"
  53. "movl %2, %%ebx \n\t"
  54. : "=m" (ebx), "=m" (edx)
  55. : "m" (ebx)
  56. : "eax", "ecx", "edx");
  57. flags = edx;
  58. }
  59. return flags & feature;
  60. }
  61. /*
  62. * PadLock AES-ECB block en(de)cryption
  63. */
  64. int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
  65. int mode,
  66. const unsigned char input[16],
  67. unsigned char output[16])
  68. {
  69. int ebx = 0;
  70. uint32_t *rk;
  71. uint32_t *blk;
  72. uint32_t *ctrl;
  73. unsigned char buf[256];
  74. rk = ctx->rk;
  75. blk = MBEDTLS_PADLOCK_ALIGN16(buf);
  76. memcpy(blk, input, 16);
  77. ctrl = blk + 4;
  78. *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9);
  79. asm ("pushfl \n\t"
  80. "popfl \n\t"
  81. "movl %%ebx, %0 \n\t"
  82. "movl $1, %%ecx \n\t"
  83. "movl %2, %%edx \n\t"
  84. "movl %3, %%ebx \n\t"
  85. "movl %4, %%esi \n\t"
  86. "movl %4, %%edi \n\t"
  87. ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
  88. "movl %1, %%ebx \n\t"
  89. : "=m" (ebx)
  90. : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
  91. : "memory", "ecx", "edx", "esi", "edi");
  92. memcpy(output, blk, 16);
  93. return 0;
  94. }
  95. /*
  96. * PadLock AES-CBC buffer en(de)cryption
  97. */
  98. int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
  99. int mode,
  100. size_t length,
  101. unsigned char iv[16],
  102. const unsigned char *input,
  103. unsigned char *output)
  104. {
  105. int ebx = 0;
  106. size_t count;
  107. uint32_t *rk;
  108. uint32_t *iw;
  109. uint32_t *ctrl;
  110. unsigned char buf[256];
  111. if (((long) input & 15) != 0 ||
  112. ((long) output & 15) != 0) {
  113. return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
  114. }
  115. rk = ctx->rk;
  116. iw = MBEDTLS_PADLOCK_ALIGN16(buf);
  117. memcpy(iw, iv, 16);
  118. ctrl = iw + 4;
  119. *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
  120. count = (length + 15) >> 4;
  121. asm ("pushfl \n\t"
  122. "popfl \n\t"
  123. "movl %%ebx, %0 \n\t"
  124. "movl %2, %%ecx \n\t"
  125. "movl %3, %%edx \n\t"
  126. "movl %4, %%ebx \n\t"
  127. "movl %5, %%esi \n\t"
  128. "movl %6, %%edi \n\t"
  129. "movl %7, %%eax \n\t"
  130. ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
  131. "movl %1, %%ebx \n\t"
  132. : "=m" (ebx)
  133. : "m" (ebx), "m" (count), "m" (ctrl),
  134. "m" (rk), "m" (input), "m" (output), "m" (iw)
  135. : "memory", "eax", "ecx", "edx", "esi", "edi");
  136. memcpy(iv, iw, 16);
  137. return 0;
  138. }
  139. #endif /* MBEDTLS_HAVE_X86 */
  140. #endif /* MBEDTLS_PADLOCK_C */