BOLOS TEE
bolos_crypto.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 * BOLOS TEE
4 * (c) 2016 Ledger
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 ********************************************************************************/
18 
31 #ifndef __BOLOS_CRYPTO_H__
32 
33 #define __BOLOS_CRYPTO_H__
34 
35 /* ####################################################################### */
36 /* RAND */
37 /* ####################################################################### */
38 
44 uint8_t bls_rng_u8(void);
45 
46 
54 int bls_rng(uint8_t *buffer, size_t len);
55 
56 
57 /* ####################################################################### */
58 /* HASH/HMAC */
59 /* ####################################################################### */
60 
61 /* ======================================================================= */
62 /* HASH */
63 /* ======================================================================= */
64 
74 
83 int bls_sha1_init(bls_sha1_t *hash);
84 
93 int bls_sha256_init(bls_sha256_t *hash);
94 
103 int bls_sha512_init(bls_sha512_t *hash);
104 
135 int bls_hash(bls_hash_t *hash, int mode,
136  const uint8_t WIDE *in, size_t len, uint8_t *out);
137 
138 /* ======================================================================= */
139 /* HASH MAC */
140 /* ======================================================================= */
141 
157 int bls_hmac_ripemd160_init(bls_hmac_ripemd160_t *hmac, const uint8_t WIDE *key, size_t key_len);
158 
174 int bls_hmac_sha256_init(bls_hmac_sha256_t *hmac, const uint8_t WIDE *key, size_t key_len);
175 
191 int bls_hmac_sha512_init(bls_hmac_sha512_t *hmac, const uint8_t WIDE *key, size_t key_len);
192 
224 int bls_hmac(bls_hmac_t *hmac, int mode,
225  const uint8_t WIDE *in, size_t len, uint8_t *mac);
226 
227 /* ####################################################################### */
228 /* KEY STRETCHING */
229 /* ####################################################################### */
230 
255 int bls_pbkdf2(bls_md_t hash,
256  const bls_area_t *password,
257  const bls_area_t *salt,
258  int iterations,
259  uint8_t *out);
260 
261 /* ####################################################################### */
262 /* CIPHER/SIGNATURE */
263 /* ####################################################################### */
264 /* - DES
265  * - ECDSA
266  * - ECDH
267  * - RSA
268  */
269 
270 
271 /* ======================================================================= */
272 /* DES */
273 /* ======================================================================= */
274 
275 
296 int bls_des_init_key(const uint8_t WIDE *rawkey,
297  size_t key_len,
298  bls_des_key_t *key);
299 
300 
338 int bls_des(bls_des_key_t WIDE *key, int mode,
339  const bls_area_t *in, bls_area_t *out);
340 
341 
342 
386 int bls_des_iv(bls_des_key_t WIDE *key, int mode,
387  const bls_area_t *iv,
388  const bls_area_t *in, bls_area_t *out);
389 
390 
391 /* ======================================================================= */
392 /* AES */
393 /* ======================================================================= */
394 
395 
416 int bls_aes_init_key(const uint8_t WIDE *rawkey,
417  size_t key_len,
418  bls_aes_key_t *key);
419 
420 
462 int bls_aes(bls_aes_key_t WIDE *key, int mode,
463  const bls_area_t *in, bls_area_t *out);
464 
465 
512 int bls_aes_iv(bls_aes_key_t WIDE *key, int mode,
513  const bls_area_t *iv,
514  const bls_area_t *in, bls_area_t *out);
515 
516 
555 int bls_aes_iv_gcm(bls_aes_key_t WIDE *key, int mode,
556  const bls_area_t *in,
557  const bls_area_t *iv,
558  const bls_area_t WIDE *aadTag, // encrypt : aad in/tag out | decrypt : tag in
559  bls_area_t *out);
560 
561 
562 /* ======================================================================= */
563 /* RSA */
564 /* ======================================================================= */
565 
584 int bls_rsa_init_public_key(const bls_rsa_keypair_data_t WIDE *keyData,
586 
605 int bls_rsa_init_private_key(const bls_rsa_keypair_data_t WIDE *keyData,
607 
628 int bls_rsa_init_private_key_crt(const bls_rsa_crt_t WIDE *crtParameters,
630 
651 int bls_rsa_generate_keypair(int modulus_len, // in bits
652  bls_rsa_abstract_private_key_t *privateKey,
654  bls_rsa_keypair_data_t *generatedKeypairInfo
655  );
656 
671  bls_rsa_keypair_data_t *keyInfo);
672 
704 int bls_rsa_pub(bls_rsa_abstract_public_key_t WIDE *key, int mode,
705  bls_md_t hashID, const bls_area_t *src,
706  const bls_area_t *dest);
707 
739 int bls_rsa_priv(bls_rsa_abstract_private_key_t WIDE *key, int mode,
740  bls_md_t hashID, const bls_area_t *src,
741  const bls_area_t *dest);
742 
743 
744 /* ======================================================================= */
745 /* ECDSA */
746 /* ======================================================================= */
747 
748 
762 bls_curve_domain_t WIDE * bls_ecfp_get_domain(bls_curve_t curve);
763 
764 
783 int bls_ecfp_is_valid_point(const bls_curve_domain_t WIDE *domain, const uint8_t WIDE *point);
784 
816 int bls_ecfp_add_point(const bls_curve_domain_t WIDE *domain, uint8_t *R,
817  const uint8_t WIDE *P, const uint8_t WIDE *Q);
818 
846 int bls_ecdsa_init_public_key(bls_curve_t curve,
847  const uint8_t WIDE *rawkey, size_t key_len,
848  bls_ecfp_public_key_t *key);
849 
875 int bls_ecdsa_init_private_key(bls_curve_t curve,
876  const uint8_t WIDE *rawkey, size_t key_len,
897 int bls_ecfp_generate_pair(bls_curve_t curve,
898  bls_ecfp_public_key_t *public_key, bls_ecfp_private_key_t *private_key,
899  uint8_t *d);
900 
915  uint8_t *W);
916 
949 int bls_ecdsa_sign(bls_ecfp_private_key_t WIDE *key, int mode, bls_md_t hashID,
950  const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig);
982 int bls_ecdsa_verify(bls_ecfp_public_key_t WIDE *key, int mode, bls_md_t hashID,
983  const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig);
984 
1015 int bls_schnorr_sign(bls_ecfp_private_key_t WIDE *key, int mode, bls_md_t hashID,
1016  const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig);
1048 int bls_schnorr_verify(bls_ecfp_public_key_t WIDE *key, int mode, bls_md_t hashID,
1049  const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig);
1050 
1051 
1052 /* ======================================================================= */
1053 /* ECDH */
1054 /* ======================================================================= */
1055 
1084 int bls_ecdh(bls_ecfp_private_key_t WIDE *key, int mode,
1085  const uint8_t WIDE *public_point, uint8_t *secret);
1086 
1087 
1088 /* ======================================================================= */
1089 /* CRC */
1090 /* ======================================================================= */
1091 
1108 uint16_t bls_crc16(const void WIDE *buffer, size_t len);
1109 
1110 
1130 uint16_t bls_crc16_update(unsigned short crc, const void WIDE *buffer, size_t len);
1131 
1132 
1133 /* ======================================================================= */
1134 /* MATH */
1135 /* ======================================================================= */
1136 
1137 
1152 void bls_math_addm(uint8_t* r, const uint8_t WIDE *a, const uint8_t WIDE *b, const uint8_t WIDE *m, size_t len);
1153 
1154 
1168 int bls_math_cmp(const uint8_t WIDE *a, const uint8_t WIDE *b, size_t len);
1169 
1180 int bls_math_is_zero(const uint8_t WIDE *a, size_t len);
1181 
1193 void bls_math_modm(uint8_t *v, size_t len_v,
1194  const uint8_t WIDE *m, size_t len_m);
1195 
1196 #endif //__BOLOS_CRYPTO_H__
1197 
void bls_math_modm(uint8_t *v, size_t len_v, const uint8_t WIDE *m, size_t len_m)
Reduce in place (left zero padded) the given value : v = v mod m.
int bls_math_cmp(const uint8_t WIDE *a, const uint8_t WIDE *b, size_t len)
Compare to unsigned long big-endian integer.
int bls_rsa_init_public_key(const bls_rsa_keypair_data_t WIDE *keyData, bls_rsa_abstract_public_key_t *key)
Initialize a public RSA Key.
int bls_ecfp_generate_pair(bls_curve_t curve, bls_ecfp_public_key_t *public_key, bls_ecfp_private_key_t *private_key, uint8_t *d)
Generate a ecfp key pair.
int bls_ecdsa_sign(bls_ecfp_private_key_t WIDE *key, int mode, bls_md_t hashID, const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig)
Sign a hash message according to ECDSA scheme.
Definition: bolos_crypto_platform_tee.h:78
int bls_schnorr_verify(bls_ecfp_public_key_t WIDE *key, int mode, bls_md_t hashID, const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig)
Verify a hash message signature according to Schnorr scheme.
uint8_t bls_rng_u8(void)
generate a random byte
int bls_aes_iv_gcm(bls_aes_key_t WIDE *key, int mode, const bls_area_t *in, const bls_area_t *iv, const bls_area_t WIDE *aadTag, bls_area_t *out)
Add data to an AES GCM operation.
int bls_des_iv(bls_des_key_t WIDE *key, int mode, const bls_area_t *iv, const bls_area_t *in, bls_area_t *out)
Add data to a DES operation using an Initialization Vector.
int bls_rsa_pub(bls_rsa_abstract_public_key_t WIDE *key, int mode, bls_md_t hashID, const bls_area_t *src, const bls_area_t *dest)
Perform a RSA public operation.
int bls_rsa_init_private_key(const bls_rsa_keypair_data_t WIDE *keyData, bls_rsa_abstract_private_key_t *key)
Initialize a private RSA Key.
int bls_sha512_init(bls_sha512_t *hash)
Initialize a sha512 context.
Definition: bolos_crypto_platform_tee.h:68
int bls_ecdsa_init_private_key(bls_curve_t curve, const uint8_t WIDE *rawkey, size_t key_len, bls_ecfp_private_key_t *key)
Initialize a private ECFP Key.
int bls_des(bls_des_key_t WIDE *key, int mode, const bls_area_t *in, bls_area_t *out)
Add data to a DES operation.
int bls_rsa_generate_keypair(int modulus_len, bls_rsa_abstract_private_key_t *privateKey, bls_rsa_abstract_public_key_t *publicKey, bls_rsa_keypair_data_t *generatedKeypairInfo)
Generate a RSA keypair.
Definition: bolos_crypto_platform_tee.h:140
uint16_t bls_crc16_update(unsigned short crc, const void WIDE *buffer, size_t len)
Update a 16 bits checksum value.
int bls_ecfp_add_point(const bls_curve_domain_t WIDE *domain, uint8_t *R, const uint8_t WIDE *P, const uint8_t WIDE *Q)
Add two affine point.
int bls_des_init_key(const uint8_t WIDE *rawkey, size_t key_len, bls_des_key_t *key)
Initialize a DES Key.
int bls_hmac_ripemd160_init(bls_hmac_ripemd160_t *hmac, const uint8_t WIDE *key, size_t key_len)
Initialize a HMAC sha512 context.
Definition: bolos_crypto_platform_tee.h:45
int bls_aes_init_key(const uint8_t WIDE *rawkey, size_t key_len, bls_aes_key_t *key)
Initialize an AES Key.
Definition: bolos_crypto_platform_tee.h:144
Definition: bolos_crypto_platform_tee.h:102
int bls_hmac(bls_hmac_t *hmac, int mode, const uint8_t WIDE *in, size_t len, uint8_t *mac)
Add more data to HMAC.
Definition: bolos_crypto_platform_tee.h:114
int bls_aes(bls_aes_key_t WIDE *key, int mode, const bls_area_t *in, bls_area_t *out)
Add data to an AES operation.
int bls_rng(uint8_t *buffer, size_t len)
generate a random buffer
int bls_math_is_zero(const uint8_t WIDE *a, size_t len)
Compare to unsigned long big-endian integer to zero.
Definition: bolos_crypto_platform_tee.h:125
Definition: bolos_crypto_common.h:216
int bls_rsa_init_private_key_crt(const bls_rsa_crt_t WIDE *crtParameters, bls_rsa_abstract_private_key_t *key)
Initialize a private RSA Key with CRT parameters.
Definition: bolos_crypto_platform_tee.h:130
Definition: bolos_crypto_common.h:202
int bls_ecdsa_init_public_key(bls_curve_t curve, const uint8_t WIDE *rawkey, size_t key_len, bls_ecfp_public_key_t *key)
Initialize a public ECFP Key.
Definition: bolos_crypto_platform_tee.h:40
int bls_schnorr_sign(bls_ecfp_private_key_t WIDE *key, int mode, bls_md_t hashID, const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig)
Sign a hash message according to Schnorr scheme.
int bls_ecdsa_verify(bls_ecfp_public_key_t WIDE *key, int mode, bls_md_t hashID, const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig)
Verify a hash message signature according to ECDSA scheme.
int bls_ecfp_is_valid_point(const bls_curve_domain_t WIDE *domain, const uint8_t WIDE *point)
Verify that a given point is really on the specified curve.
int bls_rsa_get_public_key_data(bls_rsa_abstract_public_key_t *publicKey, bls_rsa_keypair_data_t *keyInfo)
Retrieve a RSA public key information.
Definition: bolos_crypto_platform_tee.h:148
int bls_hash(bls_hash_t *hash, int mode, const uint8_t WIDE *in, size_t len, uint8_t *out)
Add more data to hash.
int bls_hmac_sha256_init(bls_hmac_sha256_t *hmac, const uint8_t WIDE *key, size_t key_len)
Initialize a HMAC sha256 context.
Definition: bolos_crypto_platform_tee.h:50
int bls_hmac_sha512_init(bls_hmac_sha512_t *hmac, const uint8_t WIDE *key, size_t key_len)
Initialize a HMAC sha512 context.
int bls_ecfp_get_public_component(const bls_ecfp_public_key_t *public_key, uint8_t *W)
Return the uncompressed point of a public ECFP Key.
int bls_pbkdf2(bls_md_t hash, const bls_area_t *password, const bls_area_t *salt, int iterations, uint8_t *out)
Stretch a password using PBKDF2.
Definition: bolos_crypto_platform_tee.h:63
int bls_aes_iv(bls_aes_key_t WIDE *key, int mode, const bls_area_t *iv, const bls_area_t *in, bls_area_t *out)
Add data to an AES operation using an Initialization Vector.
int bls_rsa_priv(bls_rsa_abstract_private_key_t WIDE *key, int mode, bls_md_t hashID, const bls_area_t *src, const bls_area_t *dest)
Perform a RSA private operation.
uint16_t bls_crc16(const void WIDE *buffer, size_t len)
Compute a 16 bits checksum value.
Describe a buffer and length data structure.
Definition: bolos_core.h:38
int bls_ripemd160_init(bls_ripemd160_t *hash)
Initialize a ripmd160 context.
bls_curve_domain_t WIDE * bls_ecfp_get_domain(bls_curve_t curve)
Retrieve the domain of the curve.
void bls_math_addm(uint8_t *r, const uint8_t WIDE *a, const uint8_t WIDE *b, const uint8_t WIDE *m, size_t len)
Modular addition of tow big integer of the size: r = a+b mod m.
int bls_ecdh(bls_ecfp_private_key_t WIDE *key, int mode, const uint8_t WIDE *public_point, uint8_t *secret)
Compute a shared secret according to ECDH specifiaction.
int bls_sha1_init(bls_sha1_t *hash)
Initialize a sha1 context.
int bls_sha256_init(bls_sha256_t *hash)
Initialize a sha256 context.
Definition: bolos_crypto_platform_tee.h:35
Definition: bolos_crypto_platform_tee.h:73