padlock.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * VIA PadLock support functions
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /*
  22. * This implementation is based on the VIA PadLock Programming Guide:
  23. *
  24. * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
  25. * programming_guide.pdf
  26. */
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "mbedtls/config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #if defined(MBEDTLS_PADLOCK_C)
  33. #include "mbedtls/padlock.h"
  34. #include <string.h>
  35. #ifndef asm
  36. #define asm __asm
  37. #endif
  38. #if defined(MBEDTLS_HAVE_X86)
  39. /*
  40. * PadLock detection routine
  41. */
  42. int mbedtls_padlock_has_support( int feature )
  43. {
  44. static int flags = -1;
  45. int ebx = 0, edx = 0;
  46. if( flags == -1 )
  47. {
  48. asm( "movl %%ebx, %0 \n\t"
  49. "movl $0xC0000000, %%eax \n\t"
  50. "cpuid \n\t"
  51. "cmpl $0xC0000001, %%eax \n\t"
  52. "movl $0, %%edx \n\t"
  53. "jb unsupported \n\t"
  54. "movl $0xC0000001, %%eax \n\t"
  55. "cpuid \n\t"
  56. "unsupported: \n\t"
  57. "movl %%edx, %1 \n\t"
  58. "movl %2, %%ebx \n\t"
  59. : "=m" (ebx), "=m" (edx)
  60. : "m" (ebx)
  61. : "eax", "ecx", "edx" );
  62. flags = edx;
  63. }
  64. return( flags & feature );
  65. }
  66. /*
  67. * PadLock AES-ECB block en(de)cryption
  68. */
  69. int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx,
  70. int mode,
  71. const unsigned char input[16],
  72. unsigned char output[16] )
  73. {
  74. int ebx = 0;
  75. uint32_t *rk;
  76. uint32_t *blk;
  77. uint32_t *ctrl;
  78. unsigned char buf[256];
  79. rk = ctx->rk;
  80. blk = MBEDTLS_PADLOCK_ALIGN16( buf );
  81. memcpy( blk, input, 16 );
  82. ctrl = blk + 4;
  83. *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
  84. asm( "pushfl \n\t"
  85. "popfl \n\t"
  86. "movl %%ebx, %0 \n\t"
  87. "movl $1, %%ecx \n\t"
  88. "movl %2, %%edx \n\t"
  89. "movl %3, %%ebx \n\t"
  90. "movl %4, %%esi \n\t"
  91. "movl %4, %%edi \n\t"
  92. ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
  93. "movl %1, %%ebx \n\t"
  94. : "=m" (ebx)
  95. : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
  96. : "memory", "ecx", "edx", "esi", "edi" );
  97. memcpy( output, blk, 16 );
  98. return( 0 );
  99. }
  100. /*
  101. * PadLock AES-CBC buffer en(de)cryption
  102. */
  103. int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx,
  104. int mode,
  105. size_t length,
  106. unsigned char iv[16],
  107. const unsigned char *input,
  108. unsigned char *output )
  109. {
  110. int ebx = 0;
  111. size_t count;
  112. uint32_t *rk;
  113. uint32_t *iw;
  114. uint32_t *ctrl;
  115. unsigned char buf[256];
  116. if( ( (long) input & 15 ) != 0 ||
  117. ( (long) output & 15 ) != 0 )
  118. return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED );
  119. rk = ctx->rk;
  120. iw = MBEDTLS_PADLOCK_ALIGN16( buf );
  121. memcpy( iw, iv, 16 );
  122. ctrl = iw + 4;
  123. *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
  124. count = ( length + 15 ) >> 4;
  125. asm( "pushfl \n\t"
  126. "popfl \n\t"
  127. "movl %%ebx, %0 \n\t"
  128. "movl %2, %%ecx \n\t"
  129. "movl %3, %%edx \n\t"
  130. "movl %4, %%ebx \n\t"
  131. "movl %5, %%esi \n\t"
  132. "movl %6, %%edi \n\t"
  133. "movl %7, %%eax \n\t"
  134. ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
  135. "movl %1, %%ebx \n\t"
  136. : "=m" (ebx)
  137. : "m" (ebx), "m" (count), "m" (ctrl),
  138. "m" (rk), "m" (input), "m" (output), "m" (iw)
  139. : "memory", "eax", "ecx", "edx", "esi", "edi" );
  140. memcpy( iv, iw, 16 );
  141. return( 0 );
  142. }
  143. #endif /* MBEDTLS_HAVE_X86 */
  144. #endif /* MBEDTLS_PADLOCK_C */