padlock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "padlock.h"
  28. #include <string.h>
  29. #if defined(MBEDTLS_HAVE_X86)
  30. /*
  31. * PadLock detection routine
  32. */
  33. int mbedtls_padlock_has_support(int feature)
  34. {
  35. static int flags = -1;
  36. int ebx = 0, edx = 0;
  37. if (flags == -1) {
  38. asm ("movl %%ebx, %0 \n\t"
  39. "movl $0xC0000000, %%eax \n\t"
  40. "cpuid \n\t"
  41. "cmpl $0xC0000001, %%eax \n\t"
  42. "movl $0, %%edx \n\t"
  43. "jb 1f \n\t"
  44. "movl $0xC0000001, %%eax \n\t"
  45. "cpuid \n\t"
  46. "1: \n\t"
  47. "movl %%edx, %1 \n\t"
  48. "movl %2, %%ebx \n\t"
  49. : "=m" (ebx), "=m" (edx)
  50. : "m" (ebx)
  51. : "eax", "ecx", "edx");
  52. flags = edx;
  53. }
  54. return flags & feature;
  55. }
  56. /*
  57. * PadLock AES-ECB block en(de)cryption
  58. */
  59. int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
  60. int mode,
  61. const unsigned char input[16],
  62. unsigned char output[16])
  63. {
  64. int ebx = 0;
  65. uint32_t *rk;
  66. uint32_t *blk;
  67. uint32_t *ctrl;
  68. unsigned char buf[256];
  69. rk = ctx->buf + ctx->rk_offset;
  70. if (((long) rk & 15) != 0) {
  71. return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
  72. }
  73. blk = MBEDTLS_PADLOCK_ALIGN16(buf);
  74. memcpy(blk, input, 16);
  75. ctrl = blk + 4;
  76. *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9);
  77. asm ("pushfl \n\t"
  78. "popfl \n\t"
  79. "movl %%ebx, %0 \n\t"
  80. "movl $1, %%ecx \n\t"
  81. "movl %2, %%edx \n\t"
  82. "movl %3, %%ebx \n\t"
  83. "movl %4, %%esi \n\t"
  84. "movl %4, %%edi \n\t"
  85. ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
  86. "movl %1, %%ebx \n\t"
  87. : "=m" (ebx)
  88. : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
  89. : "memory", "ecx", "edx", "esi", "edi");
  90. memcpy(output, blk, 16);
  91. return 0;
  92. }
  93. /*
  94. * PadLock AES-CBC buffer en(de)cryption
  95. */
  96. int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
  97. int mode,
  98. size_t length,
  99. unsigned char iv[16],
  100. const unsigned char *input,
  101. unsigned char *output)
  102. {
  103. int ebx = 0;
  104. size_t count;
  105. uint32_t *rk;
  106. uint32_t *iw;
  107. uint32_t *ctrl;
  108. unsigned char buf[256];
  109. rk = ctx->buf + ctx->rk_offset;
  110. if (((long) input & 15) != 0 ||
  111. ((long) output & 15) != 0 ||
  112. ((long) rk & 15) != 0) {
  113. return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
  114. }
  115. iw = MBEDTLS_PADLOCK_ALIGN16(buf);
  116. memcpy(iw, iv, 16);
  117. ctrl = iw + 4;
  118. *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
  119. count = (length + 15) >> 4;
  120. asm ("pushfl \n\t"
  121. "popfl \n\t"
  122. "movl %%ebx, %0 \n\t"
  123. "movl %2, %%ecx \n\t"
  124. "movl %3, %%edx \n\t"
  125. "movl %4, %%ebx \n\t"
  126. "movl %5, %%esi \n\t"
  127. "movl %6, %%edi \n\t"
  128. "movl %7, %%eax \n\t"
  129. ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
  130. "movl %1, %%ebx \n\t"
  131. : "=m" (ebx)
  132. : "m" (ebx), "m" (count), "m" (ctrl),
  133. "m" (rk), "m" (input), "m" (output), "m" (iw)
  134. : "memory", "eax", "ecx", "edx", "esi", "edi");
  135. memcpy(iv, iw, 16);
  136. return 0;
  137. }
  138. #endif /* MBEDTLS_HAVE_X86 */
  139. #endif /* MBEDTLS_PADLOCK_C */