Added RSA/OAEP implementation.
This commit is contained in:
parent
f81a282878
commit
d592e99932
18 changed files with 1513 additions and 6 deletions
56
src/hash/mgf1.c
Normal file
56
src/hash/mgf1.c
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_mgf1_xor(void *data, size_t len,
|
||||
const br_hash_class *dig, const void *seed, size_t seed_len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
size_t u, hlen;
|
||||
uint32_t c;
|
||||
|
||||
buf = data;
|
||||
hlen = br_digest_size(dig);
|
||||
for (u = 0, c = 0; u < len; u += hlen, c ++) {
|
||||
br_hash_compat_context hc;
|
||||
unsigned char tmp[64];
|
||||
size_t v;
|
||||
|
||||
hc.vtable = dig;
|
||||
dig->init(&hc.vtable);
|
||||
dig->update(&hc.vtable, seed, seed_len);
|
||||
br_enc32be(tmp, c);
|
||||
dig->update(&hc.vtable, tmp, 4);
|
||||
dig->out(&hc.vtable, tmp);
|
||||
for (v = 0; v < hlen; v ++) {
|
||||
if ((u + v) >= len) {
|
||||
break;
|
||||
}
|
||||
buf[u + v] ^= tmp[v];
|
||||
}
|
||||
}
|
||||
}
|
||||
25
src/inner.h
25
src/inner.h
|
|
@ -1887,6 +1887,31 @@ uint32_t br_rsa_pkcs1_sig_unpad(const unsigned char *sig, size_t sig_len,
|
|||
const unsigned char *hash_oid, size_t hash_len,
|
||||
unsigned char *hash_out);
|
||||
|
||||
/*
|
||||
* Apply OAEP padding. Returned value is the actual padded string length,
|
||||
* or zero on error.
|
||||
*/
|
||||
size_t br_rsa_oaep_pad(const br_prng_class **rnd, const br_hash_class *dig,
|
||||
const void *label, size_t label_len, const br_rsa_public_key *pk,
|
||||
void *dst, size_t dst_nax_len, const void *src, size_t src_len);
|
||||
|
||||
/*
|
||||
* Unravel and check OAEP padding. If the padding is correct, then 1 is
|
||||
* returned, '*len' is adjusted to the length of the message, and the
|
||||
* data is moved to the start of the 'data' buffer. If the padding is
|
||||
* incorrect, then 0 is returned and '*len' is untouched. Either way,
|
||||
* the complete buffer contents are altered.
|
||||
*/
|
||||
uint32_t br_rsa_oaep_unpad(const br_hash_class *dig,
|
||||
const void *label, size_t label_len, void *data, size_t *len);
|
||||
|
||||
/*
|
||||
* Compute MGF1 for a given seed, and XOR the output into the provided
|
||||
* buffer.
|
||||
*/
|
||||
void br_mgf1_xor(void *data, size_t len,
|
||||
const br_hash_class *dig, const void *seed, size_t seed_len);
|
||||
|
||||
/* ==================================================================== */
|
||||
/*
|
||||
* Elliptic curves.
|
||||
|
|
|
|||
38
src/rsa/rsa_default_oaep_decrypt.c
Normal file
38
src/rsa/rsa_default_oaep_decrypt.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
br_rsa_oaep_decrypt
|
||||
br_rsa_oaep_decrypt_get_default(void)
|
||||
{
|
||||
#if BR_INT128 || BR_UMUL128
|
||||
return &br_rsa_i62_oaep_decrypt;
|
||||
#elif BR_LOMUL
|
||||
return &br_rsa_i15_oaep_decrypt;
|
||||
#else
|
||||
return &br_rsa_i31_oaep_decrypt;
|
||||
#endif
|
||||
}
|
||||
38
src/rsa/rsa_default_oaep_encrypt.c
Normal file
38
src/rsa/rsa_default_oaep_encrypt.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
br_rsa_oaep_encrypt
|
||||
br_rsa_oaep_encrypt_get_default(void)
|
||||
{
|
||||
#if BR_INT128 || BR_UMUL128
|
||||
return &br_rsa_i62_oaep_encrypt;
|
||||
#elif BR_LOMUL
|
||||
return &br_rsa_i15_oaep_encrypt;
|
||||
#else
|
||||
return &br_rsa_i31_oaep_encrypt;
|
||||
#endif
|
||||
}
|
||||
41
src/rsa/rsa_i15_oaep_decrypt.c
Normal file
41
src/rsa/rsa_i15_oaep_decrypt.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
uint32_t
|
||||
br_rsa_i15_oaep_decrypt(const br_hash_class *dig,
|
||||
const void *label, size_t label_len,
|
||||
const br_rsa_private_key *sk, void *data, size_t *len)
|
||||
{
|
||||
uint32_t r;
|
||||
|
||||
if (*len != ((sk->n_bitlen + 7) >> 3)) {
|
||||
return 0;
|
||||
}
|
||||
r = br_rsa_i15_private(data, sk);
|
||||
r &= br_rsa_oaep_unpad(dig, label, label_len, data, len);
|
||||
return r;
|
||||
}
|
||||
44
src/rsa/rsa_i15_oaep_encrypt.c
Normal file
44
src/rsa/rsa_i15_oaep_encrypt.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
size_t
|
||||
br_rsa_i15_oaep_encrypt(
|
||||
const br_prng_class **rnd, const br_hash_class *dig,
|
||||
const void *label, size_t label_len,
|
||||
const br_rsa_public_key *pk,
|
||||
void *dst, size_t dst_max_len,
|
||||
const void *src, size_t src_len)
|
||||
{
|
||||
size_t dlen;
|
||||
|
||||
dlen = br_rsa_oaep_pad(rnd, dig, label, label_len,
|
||||
pk, dst, dst_max_len, src, src_len);
|
||||
if (dlen == 0) {
|
||||
return 0;
|
||||
}
|
||||
return dlen & -(size_t)br_rsa_i15_public(dst, dlen, pk);
|
||||
}
|
||||
41
src/rsa/rsa_i31_oaep_decrypt.c
Normal file
41
src/rsa/rsa_i31_oaep_decrypt.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
uint32_t
|
||||
br_rsa_i31_oaep_decrypt(const br_hash_class *dig,
|
||||
const void *label, size_t label_len,
|
||||
const br_rsa_private_key *sk, void *data, size_t *len)
|
||||
{
|
||||
uint32_t r;
|
||||
|
||||
if (*len != ((sk->n_bitlen + 7) >> 3)) {
|
||||
return 0;
|
||||
}
|
||||
r = br_rsa_i31_private(data, sk);
|
||||
r &= br_rsa_oaep_unpad(dig, label, label_len, data, len);
|
||||
return r;
|
||||
}
|
||||
44
src/rsa/rsa_i31_oaep_encrypt.c
Normal file
44
src/rsa/rsa_i31_oaep_encrypt.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
size_t
|
||||
br_rsa_i31_oaep_encrypt(
|
||||
const br_prng_class **rnd, const br_hash_class *dig,
|
||||
const void *label, size_t label_len,
|
||||
const br_rsa_public_key *pk,
|
||||
void *dst, size_t dst_max_len,
|
||||
const void *src, size_t src_len)
|
||||
{
|
||||
size_t dlen;
|
||||
|
||||
dlen = br_rsa_oaep_pad(rnd, dig, label, label_len,
|
||||
pk, dst, dst_max_len, src, src_len);
|
||||
if (dlen == 0) {
|
||||
return 0;
|
||||
}
|
||||
return dlen & -(size_t)br_rsa_i31_public(dst, dlen, pk);
|
||||
}
|
||||
41
src/rsa/rsa_i32_oaep_decrypt.c
Normal file
41
src/rsa/rsa_i32_oaep_decrypt.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
uint32_t
|
||||
br_rsa_i32_oaep_decrypt(const br_hash_class *dig,
|
||||
const void *label, size_t label_len,
|
||||
const br_rsa_private_key *sk, void *data, size_t *len)
|
||||
{
|
||||
uint32_t r;
|
||||
|
||||
if (*len != ((sk->n_bitlen + 7) >> 3)) {
|
||||
return 0;
|
||||
}
|
||||
r = br_rsa_i32_private(data, sk);
|
||||
r &= br_rsa_oaep_unpad(dig, label, label_len, data, len);
|
||||
return r;
|
||||
}
|
||||
44
src/rsa/rsa_i32_oaep_encrypt.c
Normal file
44
src/rsa/rsa_i32_oaep_encrypt.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
size_t
|
||||
br_rsa_i32_oaep_encrypt(
|
||||
const br_prng_class **rnd, const br_hash_class *dig,
|
||||
const void *label, size_t label_len,
|
||||
const br_rsa_public_key *pk,
|
||||
void *dst, size_t dst_max_len,
|
||||
const void *src, size_t src_len)
|
||||
{
|
||||
size_t dlen;
|
||||
|
||||
dlen = br_rsa_oaep_pad(rnd, dig, label, label_len,
|
||||
pk, dst, dst_max_len, src, src_len);
|
||||
if (dlen == 0) {
|
||||
return 0;
|
||||
}
|
||||
return dlen & -(size_t)br_rsa_i32_public(dst, dlen, pk);
|
||||
}
|
||||
61
src/rsa/rsa_i62_oaep_decrypt.c
Normal file
61
src/rsa/rsa_i62_oaep_decrypt.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
#if BR_INT128 || BR_UMUL128
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
uint32_t
|
||||
br_rsa_i62_oaep_decrypt(const br_hash_class *dig,
|
||||
const void *label, size_t label_len,
|
||||
const br_rsa_private_key *sk, void *data, size_t *len)
|
||||
{
|
||||
uint32_t r;
|
||||
|
||||
if (*len != ((sk->n_bitlen + 7) >> 3)) {
|
||||
return 0;
|
||||
}
|
||||
r = br_rsa_i62_private(data, sk);
|
||||
r &= br_rsa_oaep_unpad(dig, label, label_len, data, len);
|
||||
return r;
|
||||
}
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
br_rsa_oaep_decrypt
|
||||
br_rsa_i62_oaep_decrypt_get(void)
|
||||
{
|
||||
return &br_rsa_i62_oaep_decrypt;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
br_rsa_oaep_decrypt
|
||||
br_rsa_i62_oaep_decrypt_get(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
64
src/rsa/rsa_i62_oaep_encrypt.c
Normal file
64
src/rsa/rsa_i62_oaep_encrypt.c
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
#if BR_INT128 || BR_UMUL128
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
size_t
|
||||
br_rsa_i62_oaep_encrypt(
|
||||
const br_prng_class **rnd, const br_hash_class *dig,
|
||||
const void *label, size_t label_len,
|
||||
const br_rsa_public_key *pk,
|
||||
void *dst, size_t dst_max_len,
|
||||
const void *src, size_t src_len)
|
||||
{
|
||||
size_t dlen;
|
||||
|
||||
dlen = br_rsa_oaep_pad(rnd, dig, label, label_len,
|
||||
pk, dst, dst_max_len, src, src_len);
|
||||
if (dlen == 0) {
|
||||
return 0;
|
||||
}
|
||||
return dlen & -(size_t)br_rsa_i62_public(dst, dlen, pk);
|
||||
}
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
br_rsa_oaep_encrypt
|
||||
br_rsa_i62_oaep_encrypt_get(void)
|
||||
{
|
||||
return &br_rsa_i62_oaep_encrypt;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
br_rsa_oaep_encrypt
|
||||
br_rsa_i62_oaep_encrypt_get(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
112
src/rsa/rsa_oaep_pad.c
Normal file
112
src/rsa/rsa_oaep_pad.c
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/*
|
||||
* Hash some data. This is put as a separate function so that stack
|
||||
* allocation of the hash function context is done only for the duration
|
||||
* of the hash.
|
||||
*/
|
||||
static void
|
||||
hash_data(const br_hash_class *dig, void *dst, const void *src, size_t len)
|
||||
{
|
||||
br_hash_compat_context hc;
|
||||
|
||||
hc.vtable = dig;
|
||||
dig->init(&hc.vtable);
|
||||
dig->update(&hc.vtable, src, len);
|
||||
dig->out(&hc.vtable, dst);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
size_t
|
||||
br_rsa_oaep_pad(const br_prng_class **rnd, const br_hash_class *dig,
|
||||
const void *label, size_t label_len,
|
||||
const br_rsa_public_key *pk,
|
||||
void *dst, size_t dst_max_len,
|
||||
const void *src, size_t src_len)
|
||||
{
|
||||
size_t k, hlen;
|
||||
unsigned char *buf;
|
||||
|
||||
hlen = br_digest_size(dig);
|
||||
|
||||
/*
|
||||
* Compute actual modulus length (in bytes).
|
||||
*/
|
||||
k = pk->nlen;
|
||||
while (k > 0 && pk->n[k - 1] == 0) {
|
||||
k --;
|
||||
}
|
||||
|
||||
/*
|
||||
* An error is reported if:
|
||||
* - the modulus is too short;
|
||||
* - the source message length is too long;
|
||||
* - the destination buffer is too short.
|
||||
*/
|
||||
if (k < ((hlen << 1) + 2)
|
||||
|| src_len > (k - (hlen << 1) - 2)
|
||||
|| dst_max_len < k)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Apply padding. At this point, things cannot fail.
|
||||
*/
|
||||
buf = dst;
|
||||
|
||||
/*
|
||||
* Assemble: DB = lHash || PS || 0x01 || M
|
||||
* We first place the source message M with memmove(), so that
|
||||
* overlaps between source and destination buffers are supported.
|
||||
*/
|
||||
memmove(buf + k - src_len, src, src_len);
|
||||
hash_data(dig, buf + 1 + hlen, label, label_len);
|
||||
memset(buf + 1 + (hlen << 1), 0, k - src_len - (hlen << 1) - 2);
|
||||
buf[k - src_len - 1] = 0x01;
|
||||
|
||||
/*
|
||||
* Make the random seed.
|
||||
*/
|
||||
(*rnd)->generate(rnd, buf + 1, hlen);
|
||||
|
||||
/*
|
||||
* Mask DB with the mask generated from the seed.
|
||||
*/
|
||||
br_mgf1_xor(buf + 1 + hlen, k - hlen - 1, dig, buf + 1, hlen);
|
||||
|
||||
/*
|
||||
* Mask the seed with the mask generated from the masked DB.
|
||||
*/
|
||||
br_mgf1_xor(buf + 1, hlen, dig, buf + 1 + hlen, k - hlen - 1);
|
||||
|
||||
/*
|
||||
* Padding result: EM = 0x00 || maskedSeed || maskedDB.
|
||||
*/
|
||||
buf[0] = 0x00;
|
||||
return k;
|
||||
}
|
||||
145
src/rsa/rsa_oaep_unpad.c
Normal file
145
src/rsa/rsa_oaep_unpad.c
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/*
|
||||
* Hash some data and XOR the result into the provided buffer. This is put
|
||||
* as a separate function so that stack allocation of the hash function
|
||||
* context is done only for the duration of the hash.
|
||||
*/
|
||||
static void
|
||||
xor_hash_data(const br_hash_class *dig, void *dst, const void *src, size_t len)
|
||||
{
|
||||
br_hash_compat_context hc;
|
||||
unsigned char tmp[64];
|
||||
unsigned char *buf;
|
||||
size_t u, hlen;
|
||||
|
||||
hc.vtable = dig;
|
||||
dig->init(&hc.vtable);
|
||||
dig->update(&hc.vtable, src, len);
|
||||
dig->out(&hc.vtable, tmp);
|
||||
buf = dst;
|
||||
hlen = br_digest_size(dig);
|
||||
for (u = 0; u < hlen; u ++) {
|
||||
buf[u] ^= tmp[u];
|
||||
}
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
uint32_t
|
||||
br_rsa_oaep_unpad(const br_hash_class *dig,
|
||||
const void *label, size_t label_len,
|
||||
void *data, size_t *len)
|
||||
{
|
||||
size_t u, k, hlen;
|
||||
unsigned char *buf;
|
||||
uint32_t r, s, zlen;
|
||||
|
||||
hlen = br_digest_size(dig);
|
||||
k = *len;
|
||||
buf = data;
|
||||
|
||||
/*
|
||||
* There must be room for the padding.
|
||||
*/
|
||||
if (k < ((hlen << 1) + 2)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Unmask the seed, then the DB value.
|
||||
*/
|
||||
br_mgf1_xor(buf + 1, hlen, dig, buf + 1 + hlen, k - hlen - 1);
|
||||
br_mgf1_xor(buf + 1 + hlen, k - hlen - 1, dig, buf + 1, hlen);
|
||||
|
||||
/*
|
||||
* Hash the label and XOR it with the value in the array; if
|
||||
* they are equal then these should yield only zeros.
|
||||
*/
|
||||
xor_hash_data(dig, buf + 1 + hlen, label, label_len);
|
||||
|
||||
/*
|
||||
* At that point, if the padding was correct, when we should
|
||||
* have: 0x00 || seed || 0x00 ... 0x00 0x01 || M
|
||||
* Padding is valid as long as:
|
||||
* - There is at least hlen+1 leading bytes of value 0x00.
|
||||
* - There is at least one non-zero byte.
|
||||
* - The first (leftmost) non-zero byte has value 0x01.
|
||||
*
|
||||
* Ultimately, we may leak the resulting message length, i.e.
|
||||
* the position of the byte of value 0x01, but we must take care
|
||||
* to do so only if the number of zero bytes has been verified
|
||||
* to be at least hlen+1.
|
||||
*
|
||||
* The loop below counts the number of bytes of value 0x00, and
|
||||
* checks that the next byte has value 0x01, in constant-time.
|
||||
*
|
||||
* - If the initial byte (before the seed) is not 0x00, then
|
||||
* r and s are set to 0, and stay there.
|
||||
* - Value r is 1 until the first non-zero byte is reached
|
||||
* (after the seed); it switches to 0 at that point.
|
||||
* - Value s is set to 1 if and only if the data encountered
|
||||
* at the time of the transition of r from 1 to 0 has value
|
||||
* exactly 0x01.
|
||||
* - Value zlen counts the number of leading bytes of value zero
|
||||
* (after the seed).
|
||||
*/
|
||||
r = 1 - ((buf[0] + 0xFF) >> 8);
|
||||
s = 0;
|
||||
zlen = 0;
|
||||
for (u = hlen + 1; u < k; u ++) {
|
||||
uint32_t w, nz;
|
||||
|
||||
w = buf[u];
|
||||
|
||||
/*
|
||||
* nz == 1 only for the first non-zero byte.
|
||||
*/
|
||||
nz = r & ((w + 0xFF) >> 8);
|
||||
s |= nz & EQ(w, 0x01);
|
||||
r &= NOT(nz);
|
||||
zlen += r;
|
||||
}
|
||||
|
||||
/*
|
||||
* Padding is correct only if s == 1, _and_ zlen >= hlen.
|
||||
*/
|
||||
s &= GE(zlen, (uint32_t)hlen);
|
||||
|
||||
/*
|
||||
* At that point, padding was verified, and we are now allowed
|
||||
* to make conditional jumps.
|
||||
*/
|
||||
if (s) {
|
||||
size_t plen;
|
||||
|
||||
plen = 2 + hlen + zlen;
|
||||
k -= plen;
|
||||
memmove(data, data + plen, k);
|
||||
*len = k;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue