/ ltc.c
ltc.c
1 /* 2 * Licensed under the Apache License, Version 2.0 (the "License"); 3 * you may not use this file except in compliance with the License. 4 * See the NOTICE file distributed with this work for additional 5 * information regarding copyright ownership. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <lscore/plugin.h> 18 #include <lscore/registration.h> 19 #include <tomcrypt.h> 20 #include <lsplugin/mkobject.h> 21 #include <lsplugin/mkoperator.h> 22 #include <lsplugin/register.h> 23 #include "ltc-methods.h" 24 25 static const LSplugin_key_desc_t *ltc_key_implementations[] = { 26 <c_dh_desc, 27 <c_dsa_desc, 28 <c_rsa_desc, 29 <c_aes_desc, 30 <c_des3_desc, 31 <c_blowfish_desc, 32 <c_twofish_desc, 33 <c_cast5_desc, 34 <c_kseed_desc, 35 <c_camellia_desc, 36 <c_chacha_desc, 37 <c_rc4_desc, 38 <c_poly1305_desc, 39 <c_blake2b_desc, 40 <c_blake2s_desc, 41 NULL 42 }; 43 44 static const LSplugin_signer_desc_t *ltc_sig_implementations[] = { 45 <c_rsa_sha1_signer_desc, 46 NULL 47 }; 48 49 static const LSplugin_verifier_desc_t *ltc_ver_implementations[] = { 50 <c_rsa_sha1_verifier_desc, 51 NULL 52 }; 53 54 static const LSplugin_encryptor_desc_t *ltc_enc_implementations[] = { 55 <c_aes_ecb_encryptor_desc, 56 <c_aes_cbc_encryptor_desc, 57 <c_aes_ctr_encryptor_desc, 58 <c_aes_cfb_encryptor_desc, 59 <c_aes_ofb_encryptor_desc, 60 <c_des3_ecb_encryptor_desc, 61 <c_des3_cbc_encryptor_desc, 62 <c_des3_ctr_encryptor_desc, 63 <c_des3_cfb_encryptor_desc, 64 <c_des3_ofb_encryptor_desc, 65 <c_blowfish_ecb_encryptor_desc, 66 <c_blowfish_cbc_encryptor_desc, 67 <c_blowfish_ctr_encryptor_desc, 68 <c_blowfish_cfb_encryptor_desc, 69 <c_blowfish_ofb_encryptor_desc, 70 <c_twofish_ecb_encryptor_desc, 71 <c_twofish_cbc_encryptor_desc, 72 <c_twofish_ctr_encryptor_desc, 73 <c_twofish_cfb_encryptor_desc, 74 <c_twofish_ofb_encryptor_desc, 75 <c_cast5_ecb_encryptor_desc, 76 <c_cast5_cbc_encryptor_desc, 77 <c_cast5_ctr_encryptor_desc, 78 <c_cast5_cfb_encryptor_desc, 79 <c_cast5_ofb_encryptor_desc, 80 <c_kseed_ecb_encryptor_desc, 81 <c_kseed_cbc_encryptor_desc, 82 <c_kseed_ctr_encryptor_desc, 83 <c_kseed_cfb_encryptor_desc, 84 <c_kseed_ofb_encryptor_desc, 85 <c_camellia_ecb_encryptor_desc, 86 <c_camellia_cbc_encryptor_desc, 87 <c_camellia_ctr_encryptor_desc, 88 <c_camellia_cfb_encryptor_desc, 89 <c_camellia_ofb_encryptor_desc, 90 <c_chacha_encryptor_desc, 91 <c_rc4_encryptor_desc, 92 NULL 93 }; 94 95 static const LSplugin_decryptor_desc_t *ltc_dec_implementations[] = { 96 <c_aes_ecb_decryptor_desc, 97 <c_aes_cbc_decryptor_desc, 98 <c_aes_ctr_decryptor_desc, 99 <c_aes_cfb_decryptor_desc, 100 <c_aes_ofb_decryptor_desc, 101 <c_des3_ecb_decryptor_desc, 102 <c_des3_cbc_decryptor_desc, 103 <c_des3_ctr_decryptor_desc, 104 <c_des3_cfb_decryptor_desc, 105 <c_des3_ofb_decryptor_desc, 106 <c_blowfish_ecb_decryptor_desc, 107 <c_blowfish_cbc_decryptor_desc, 108 <c_blowfish_ctr_decryptor_desc, 109 <c_blowfish_cfb_decryptor_desc, 110 <c_blowfish_ofb_decryptor_desc, 111 <c_twofish_ecb_decryptor_desc, 112 <c_twofish_cbc_decryptor_desc, 113 <c_twofish_ctr_decryptor_desc, 114 <c_twofish_cfb_decryptor_desc, 115 <c_twofish_ofb_decryptor_desc, 116 <c_cast5_ecb_decryptor_desc, 117 <c_cast5_cbc_decryptor_desc, 118 <c_cast5_ctr_decryptor_desc, 119 <c_cast5_cfb_decryptor_desc, 120 <c_cast5_ofb_decryptor_desc, 121 <c_kseed_ecb_decryptor_desc, 122 <c_kseed_cbc_decryptor_desc, 123 <c_kseed_ctr_decryptor_desc, 124 <c_kseed_cfb_decryptor_desc, 125 <c_kseed_ofb_decryptor_desc, 126 <c_camellia_ecb_decryptor_desc, 127 <c_camellia_cbc_decryptor_desc, 128 <c_camellia_ctr_decryptor_desc, 129 <c_camellia_cfb_decryptor_desc, 130 <c_camellia_ofb_decryptor_desc, 131 <c_chacha_decryptor_desc, 132 <c_rc4_decryptor_desc, 133 NULL 134 }; 135 136 static const LSplugin_digester_desc_t *ltc_digest_implementations[] = { 137 <c_whirlpool_desc, 138 <c_sha3_512_desc, 139 <c_sha512_desc, 140 <c_blake2b_512_desc, 141 <c_sha3_384_desc, 142 <c_sha384_desc, 143 <c_rmd320_desc, 144 <c_sha512_256_desc, 145 <c_sha3_256_desc, 146 <c_sha256_desc, 147 <c_rmd256_desc, 148 <c_blake2s_256_desc, 149 <c_blake2b_256_desc, 150 <c_sha512_224_desc, 151 <c_sha3_224_desc, 152 <c_sha224_desc, 153 <c_blake2s_224_desc, 154 <c_blake2b_384_desc, 155 <c_tiger_desc, 156 <c_sha1_desc, 157 <c_rmd160_desc, 158 <c_blake2s_160_desc, 159 <c_blake2b_160_desc, 160 <c_rmd128_desc, 161 <c_md5_desc, 162 <c_md4_desc, 163 <c_md2_desc, 164 <c_blake2s_128_desc, 165 <c_shake128_desc, 166 <c_shake256_desc, 167 NULL 168 }; 169 170 static const LSplugin_mac_desc_t *ltc_mac_implementations[] = { 171 <c_poly1305_mac_desc, 172 <c_blake2b_mac_desc, 173 <c_blake2s_mac_desc, 174 NULL 175 }; 176 177 static const LSplugin_rbg_desc_t *ltc_rbg_implementations[] = { 178 <c_fortuna_desc, 179 NULL 180 }; 181 182 #ifdef LTC_STATIC_DEFINE 183 # define LSC_plugin_start ltc_plugin_start 184 # define LSC_plugin_stop ltc_plugin_stop 185 #endif 186 187 LSC_EXPORT LE_STATUS LSC_plugin_start(LSC_plugin_t *plugin) 188 { 189 LSC_env_t *env; 190 LE_STATUS sts; 191 192 if (!LE_status_is_OK(sts = LSC_get_plugin_parent_environment(plugin, &env))) 193 return sts; 194 195 #define reg(T, N) \ 196 do { \ 197 sts = LSplugin_register_##T##_implementations \ 198 (env, plugin, ltc_##N##_implementations); \ 199 if (!LE_status_is_OK(sts)) \ 200 return sts; \ 201 } while(0) 202 203 reg(key, key); 204 reg(signer, sig); 205 reg(verifier, ver); 206 reg(encryptor, enc); 207 reg(decryptor, dec); 208 reg(digester, digest); 209 reg(mac, mac); 210 reg(rbg, rbg); 211 212 /* Initialize LibTomCrypt */ 213 /* Right now, we use standard libtommath */ 214 ltc_mp = ltm_desc; 215 216 return sts; 217 } 218 219 LSC_EXPORT LE_STATUS LSC_plugin_stop(LSC_plugin_t *plugin) 220 { 221 LSC_env_t *env; 222 LE_STATUS sts; 223 224 if (!LE_status_is_OK(sts = LSC_get_plugin_parent_environment(plugin, &env))) 225 return sts; 226 227 #define dereg(T, N) \ 228 do { \ 229 sts = LSplugin_deregister_##T##_implementations \ 230 (env, plugin, ltc_##N##_implementations); \ 231 if (!LE_status_is_OK(sts)) \ 232 return sts; \ 233 } while(0) 234 235 dereg(rbg, rbg); 236 dereg(mac, mac); 237 dereg(digester, digest); 238 dereg(decryptor, dec); 239 dereg(encryptor, enc); 240 dereg(verifier, ver); 241 dereg(signer, sig); 242 dereg(key, key); 243 244 return sts; 245 }