padlock.c 4.5 KB

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