github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/src/tpm2/padtest.cc (about)

     1  
     2  #include <stdio.h>
     3  #include <stdlib.h>
     4  #include <sys/types.h>
     5  #include <sys/stat.h>
     6  #include <fcntl.h>
     7  #include <unistd.h>
     8  #include <string.h>
     9  #include <tpm2_lib.h>
    10  #include <errno.h>
    11  
    12  #include <openssl/aes.h>
    13  #include <openssl/rsa.h>
    14  #include <openssl/x509.h>
    15  #include <openssl_helpers.h>
    16  #include <openssl/rand.h>
    17  #include <openssl/hmac.h>
    18  #include <openssl/sha.h>
    19  
    20  #include <openssl_helpers.h>
    21  
    22  #include <string>
    23  using std::string;
    24  
    25  
    26  //
    27  // Copyright 2015 Google Corporation, All Rights Reserved.
    28  //
    29  // Licensed under the Apache License, Version 2.0 (the "License");
    30  // you may not use this file except in compliance with the License.
    31  // You may obtain a copy of the License at
    32  //     http://www.apache.org/licenses/LICENSE-2.0
    33  // or in the the file LICENSE-2.0.txt in the top level sourcedirectory
    34  // Unless required by applicable law or agreed to in writing, software
    35  // distributed under the License is distributed on an "AS IS" BASIS,
    36  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    37  // See the License for the specific language governing permissions and
    38  // limitations under the License
    39  //
    40  // Portions of this code were derived TPM2.0-TSS published
    41  // by Intel under the license set forth in intel_license.txt
    42  // and downloaded on or about August 6, 2015.
    43  // File: tpm2_lib.cc
    44  
    45  // standard buffer size
    46  
    47  bool white(const char c) {
    48    return c== ' ' || c == '\n' || c == ' ' || c == '\r';
    49  }
    50  
    51  int Convert(int in_size, char* in, byte* out) {
    52    extern byte ToHex(const char);
    53    int i;
    54    int n = 0;
    55    byte a, b;
    56    for (i = 0; i < in_size; i += 2) {
    57      if (i >= (in_size-1)) {
    58        if (white((const char)in[i]))
    59          break;
    60        a = ToHex((const char)in[i]);
    61        out[n++] = a;
    62        break;
    63      }
    64      if (in[i] == '\n')
    65        break;
    66      a = ToHex((const char)in[i]);
    67      b = ToHex((const char)in[i+1]);
    68      out[n++] = (a<<4) | b;
    69    }
    70    return n;
    71  }
    72  
    73  int main(int an, char** av) {
    74    string filename(av[1]);
    75    byte buf[1024];
    76    int buf_size = 1024;
    77    byte padded_buf[512];
    78    byte check[512];
    79    byte repadded_buf[1024];
    80    memset(check, 0, 512);
    81    memset(repadded_buf, 0, 512);
    82  
    83    if (!ReadFileIntoBlock(filename, &buf_size, buf)) {
    84      printf("Cant read %s\n", filename.c_str());
    85      return 1;
    86    }
    87    buf[buf_size] = 0;
    88    int pad_size = Convert(buf_size, (char*)buf, padded_buf);
    89    int check_len = RSA_padding_check_PKCS1_OAEP(check, 256, padded_buf, pad_size,
    90                      256, (byte*)"IDENTITY", strlen("IDENTITY")+1);
    91    printf("Padding %3d  : ", pad_size);PrintBytes(pad_size, padded_buf);printf("\n");
    92    printf("check %03d    : ", check_len);PrintBytes(check_len, check);printf("\n");
    93    int repadded_len = RSA_padding_add_PKCS1_OAEP(repadded_buf, 256, check, check_len,
    94                      	(byte*)"IDENTITY", strlen("IDENTITY")+1);
    95    printf("repadded  %03d : ", 256);PrintBytes(256, repadded_buf);printf("\n");
    96    return 0;
    97  }