github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/tao/key_decoding.go (about)

     1  //  Copyright (c) 2017, John Manferdelli, All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tao
    16  
    17  import (
    18  	"crypto/x509"
    19  )
    20  
    21  func ptrFromString(str string) *string {
    22  	return &str
    23  }
    24  
    25  func PublicKeyAlgFromSignerAlg(signerAlg string) int {
    26  	switch(signerAlg) {
    27  	case "ecdsap256", "ecdsap384", "ecdsap521":
    28  		return int(x509.ECDSA)
    29  	case "rsa1024", "rsa2048", "rsa3072":
    30  		return int(x509.RSA)
    31  	default:
    32  		return -1
    33  	}
    34  	return -1
    35  }
    36  
    37  func SignatureAlgFromSignerAlg(signerAlg string) int {
    38  	switch(signerAlg) {
    39  	case "ecdsap256", "ecdsap384", "ecdsap521":
    40  		return int(x509.ECDSAWithSHA256)
    41  	case "rsa1024", "rsa2048", "rsa3072":
    42  		return int(x509.SHA256WithRSA)
    43  	default:
    44  		return -1
    45  	}
    46  	return -1
    47  }
    48  
    49  func CrypterTypeFromSuiteName(suiteName string) *string {
    50  	switch suiteName {
    51  	case Basic128BitCipherSuite:
    52  		return ptrFromString("aes128-ctr-hmacsha256")
    53  	case Basic192BitCipherSuite:
    54  		return ptrFromString("aes256-ctr-hmacsha384")
    55  	case Basic256BitCipherSuite:
    56  		return ptrFromString("aes256-ctr-hmacsha512")
    57  	default:
    58  		return nil
    59  	}
    60  	return nil
    61  }
    62  
    63  func SignerTypeFromSuiteName(suiteName string) *string {
    64  	switch suiteName {
    65  	case Basic128BitCipherSuite:
    66  		return ptrFromString("ecdsap256")
    67  	case Basic192BitCipherSuite:
    68  		return ptrFromString("ecdsap384")
    69  	case Basic256BitCipherSuite:
    70  		return ptrFromString("ecdsap521")
    71  	default:
    72  		return nil
    73  	}
    74  	return nil
    75  }
    76  
    77  func VerifierTypeFromSuiteName(suiteName string) *string {
    78  	n := SignerTypeFromSuiteName(suiteName)
    79  	if n == nil {
    80  		return nil
    81  	}
    82  	return ptrFromString(*n + "-public")
    83  }
    84  
    85  func DeriverTypeFromSuiteName(suiteName string) *string {
    86  	switch suiteName {
    87  	case Basic128BitCipherSuite, Basic192BitCipherSuite, Basic256BitCipherSuite:
    88  		return ptrFromString("hdkf-sha256")
    89  	/*
    90  	// Replace later?
    91  	case Basic192BitCipherSuite:
    92  		return ptrFromString("hdkf-sha384")
    93  	case Basic256BitCipherSuite:
    94  		return ptrFromString("hdkf-sha512")
    95  	 */
    96  	default:
    97  		return nil
    98  	}
    99  	return nil
   100  }
   101  
   102  func HmacTypeFromSuiteName(suiteName string) *string {
   103  	switch suiteName {
   104  	case Basic128BitCipherSuite:
   105  		return ptrFromString("hmacsha256")
   106  	case Basic192BitCipherSuite:
   107  		return ptrFromString("hmacsha384")
   108  	case Basic256BitCipherSuite:
   109  		return ptrFromString("hmacsha512")
   110  	default:
   111  		return nil
   112  	}
   113  	return nil
   114  }
   115  
   116  func CipherTypeFromSuiteName(suiteName string) *string {
   117  	switch suiteName {
   118  	case Basic128BitCipherSuite:
   119  		return ptrFromString("aes128-ctr")
   120  	case Basic192BitCipherSuite, Basic256BitCipherSuite:
   121  		return ptrFromString("aes256-ctr")
   122  	default:
   123  		return nil
   124  	}
   125  	return nil
   126  }
   127  
   128  func HashTypeFromSuiteName(suiteName string) *string {
   129  	switch suiteName {
   130  	case Basic128BitCipherSuite:
   131  		return ptrFromString("sha256")
   132  	case Basic192BitCipherSuite:
   133  		return ptrFromString("sha384")
   134  	case Basic256BitCipherSuite:
   135  		return ptrFromString("sha512")
   136  	default:
   137  		return nil
   138  	}
   139  	return nil
   140  }
   141  
   142  func IsSinger(keyType string) bool {
   143  	switch(keyType) {
   144  	default:
   145  		return false
   146  	case "rsa1024", "rsa2048", "rsa3072",
   147  	     "ecdsap256", "ecdsap384", "ecdsap521":
   148  		return true
   149  	}
   150  	return false
   151  }
   152  
   153  func IsCrypter(keyType string) bool {
   154  	switch(keyType) {
   155  	default:
   156  		return false
   157  	case "aes128-ctr-hmacsha256", "aes256-ctr-hmacsha256":
   158  		return true
   159  	}
   160  	return false
   161  }
   162  
   163  func IsDeriver(keyType string) bool {
   164  	switch(keyType) {
   165  	default:
   166  		return false
   167  	case "hdkf-sha256":
   168  		return true
   169  	}
   170  	return false
   171  }
   172  
   173  func SymmetricKeySizeFromAlgorithmName(keyType string) *int {
   174  	var n int
   175  	switch(keyType) {
   176  	default:
   177  		return nil
   178  	case "aes128-ctr-hmacsha256":
   179  		n = 16
   180  		return &n
   181  	case "aes256-ctr-hmacsha384", "aes256-ctr-hmacsha512":
   182  		n = 32
   183  		return &n
   184  	}
   185  	return nil
   186  }
   187  
   188  func HmacSizeFromAlgorithmName(keyType string) *int {
   189  	var n int
   190  	switch(keyType) {
   191  	default:
   192  		return nil
   193  	case "aes128-ctr-hmacsha256":
   194  		n = 32
   195  		return &n
   196  	case "aes256-ctr-hmacsha384":
   197  		n = 48
   198  		return &n
   199  	case "aes256-ctr-hmacsha512":
   200  		n = 64
   201  		return &n
   202  	}
   203  	return nil
   204  }
   205  
   206  func HmacKeySizeFromAlgorithmName(keyType string) *int {
   207  	var n int
   208  	switch(keyType) {
   209  	default:
   210  		return nil
   211  	case "aes128-ctr-hmacsha256":
   212  		n = 32
   213  		return &n
   214  	case "aes256-ctr-hmacsha384":
   215  		n = 48
   216  		return &n
   217  	case "aes256-ctr-hmacsha512":
   218  		n = 64
   219  		return &n
   220  	}
   221  	return nil
   222  }
   223  
   224  func CombinedKeySizeFromAlgorithmName(keyType string) *int {
   225  	var n int
   226  	switch(keyType) {
   227  	default:
   228  		return nil
   229  	case "aes128-ctr-hmacsha256":
   230  		n = 48
   231  		return &n
   232  	case "aes256-ctr-hmacsha384":
   233  		n = 64
   234  		return &n
   235  	case "aes256-ctr-hmacsha512":
   236  		n = 96
   237  		return &n
   238  	}
   239  	return nil
   240  }
   241  
   242  func SymmetricBlockSizeFromAlgorithmName(keyType string) *int {
   243  	var n int
   244  	switch(keyType) {
   245  	default:
   246  		return nil
   247  	case "aes128-ctr-hmacsha256", "aes256-ctr-hmacsha384", "aes256-ctr-hmacsha512",
   248  		"aes128-raw", "aes256-raw":
   249  		n = 16
   250  		return &n
   251  	}
   252  	return &n
   253  }