diff options
-rw-r--r-- | board/cr50/tpm2/rsa.c | 221 | ||||
-rw-r--r-- | chip/g/dcrypto/bn.c | 51 | ||||
-rw-r--r-- | chip/g/dcrypto/internal.h | 6 | ||||
-rw-r--r-- | chip/g/dcrypto/rsa.c | 4 |
4 files changed, 135 insertions, 147 deletions
diff --git a/board/cr50/tpm2/rsa.c b/board/cr50/tpm2/rsa.c index dfc996bca4..b31a5012fb 100644 --- a/board/cr50/tpm2/rsa.c +++ b/board/cr50/tpm2/rsa.c @@ -74,7 +74,7 @@ CRYPT_RESULT _cpri__EncryptRSA(uint32_t *out_len, uint8_t *out, rsa.e = key->exponent; rsa.N.dmax = key->publicKey->size / sizeof(uint32_t); - rsa.N.d = (uint32_t *) &key->publicKey->buffer; + rsa.N.d = (struct access_helper *) &key->publicKey->buffer; rsa.d.dmax = 0; rsa.d.d = NULL; @@ -101,9 +101,9 @@ CRYPT_RESULT _cpri__DecryptRSA(uint32_t *out_len, uint8_t *out, rsa.e = key->exponent; rsa.N.dmax = key->publicKey->size / sizeof(uint32_t); - rsa.N.d = (uint32_t *) &key->publicKey->buffer; + rsa.N.d = (struct access_helper *) &key->publicKey->buffer; rsa.d.dmax = key->privateKey->size / sizeof(uint32_t); - rsa.d.d = (uint32_t *) &key->privateKey->buffer; + rsa.d.d = (struct access_helper *) &key->privateKey->buffer; if (DCRYPTO_rsa_decrypt(&rsa, out, out_len, in, in_len, padding, hashing, label)) @@ -127,9 +127,9 @@ CRYPT_RESULT _cpri__SignRSA(uint32_t *out_len, uint8_t *out, rsa.e = key->exponent; rsa.N.dmax = key->publicKey->size / sizeof(uint32_t); - rsa.N.d = (uint32_t *) &key->publicKey->buffer; + rsa.N.d = (struct access_helper *) &key->publicKey->buffer; rsa.d.dmax = key->privateKey->size / sizeof(uint32_t); - rsa.d.d = (uint32_t *) &key->privateKey->buffer; + rsa.d.d = (struct access_helper *) &key->privateKey->buffer; if (DCRYPTO_rsa_sign(&rsa, out, out_len, in, in_len, padding, hashing)) return CRYPT_SUCCESS; @@ -153,7 +153,7 @@ CRYPT_RESULT _cpri__ValidateSignatureRSA( rsa.e = key->exponent; rsa.N.dmax = key->publicKey->size / sizeof(uint32_t); - rsa.N.d = (uint32_t *) &key->publicKey->buffer; + rsa.N.d = (struct access_helper *) &key->publicKey->buffer; rsa.d.dmax = 0; rsa.d.d = NULL; @@ -176,136 +176,123 @@ enum { TEST_RSA_KEYGEN = 4 }; -struct TPM2B_aligned { - uint16_t pad; - TPM2B_PUBLIC_KEY_RSA bn; -} __packed __aligned(4); - -static const struct TPM2B_aligned RSA_768_N = { - .bn = { - .t = {96, { - 0x69, 0x85, 0x39, 0x2d, 0x78, 0x2b, 0x90, 0x75, - 0xe1, 0x7c, 0xc1, 0x7b, 0xbd, 0x5b, 0xdd, 0xfd, - 0x00, 0x36, 0xf7, 0x38, 0x74, 0x33, 0x2b, 0xa8, - 0x53, 0x89, 0x10, 0xa7, 0x2d, 0x3c, 0xe6, 0x00, - 0xa3, 0xe5, 0x8b, 0x5f, 0xed, 0x77, 0x32, 0xc0, - 0x0f, 0xe2, 0x2c, 0x51, 0x1b, 0x46, 0xba, 0x18, - 0xc0, 0x4e, 0x1b, 0x44, 0xdf, 0x94, 0xcc, 0x15, - 0xe1, 0x67, 0x48, 0x3a, 0x12, 0xc4, 0x0c, 0x82, - 0xd2, 0xfa, 0xfe, 0x74, 0x6e, 0x49, 0xa4, 0x8b, - 0x64, 0xc2, 0x3b, 0x33, 0x36, 0x72, 0x24, 0xdb, - 0x17, 0x86, 0x5a, 0x35, 0xd2, 0x23, 0x20, 0xd4, - 0x7c, 0xf0, 0x32, 0xd9, 0x46, 0xed, 0xdb, 0xb0 - } +static const TPM2B_PUBLIC_KEY_RSA RSA_768_N = { + .t = {96, { + 0x69, 0x85, 0x39, 0x2d, 0x78, 0x2b, 0x90, 0x75, + 0xe1, 0x7c, 0xc1, 0x7b, 0xbd, 0x5b, 0xdd, 0xfd, + 0x00, 0x36, 0xf7, 0x38, 0x74, 0x33, 0x2b, 0xa8, + 0x53, 0x89, 0x10, 0xa7, 0x2d, 0x3c, 0xe6, 0x00, + 0xa3, 0xe5, 0x8b, 0x5f, 0xed, 0x77, 0x32, 0xc0, + 0x0f, 0xe2, 0x2c, 0x51, 0x1b, 0x46, 0xba, 0x18, + 0xc0, 0x4e, 0x1b, 0x44, 0xdf, 0x94, 0xcc, 0x15, + 0xe1, 0x67, 0x48, 0x3a, 0x12, 0xc4, 0x0c, 0x82, + 0xd2, 0xfa, 0xfe, 0x74, 0x6e, 0x49, 0xa4, 0x8b, + 0x64, 0xc2, 0x3b, 0x33, 0x36, 0x72, 0x24, 0xdb, + 0x17, 0x86, 0x5a, 0x35, 0xd2, 0x23, 0x20, 0xd4, + 0x7c, 0xf0, 0x32, 0xd9, 0x46, 0xed, 0xdb, 0xb0 } } }; -static const struct TPM2B_aligned RSA_768_D = { - .bn = { - .t = {96, { - 0x01, 0x40, 0x76, 0x7b, 0x41, 0xd6, 0xd9, 0x17, - 0xfe, 0x52, 0x6d, 0xdd, 0x24, 0x70, 0xbc, 0x97, - 0x7e, 0xcf, 0x54, 0x22, 0x4c, 0x71, 0x29, 0xf5, - 0xb2, 0xe2, 0xf6, 0xf8, 0x8b, 0x9e, 0x20, 0x1a, - 0x1e, 0x67, 0xee, 0x59, 0xf9, 0x83, 0x6b, 0x91, - 0x8d, 0xdf, 0x03, 0xfc, 0xdd, 0x0f, 0x35, 0xd7, - 0xa2, 0x5d, 0x06, 0x3f, 0x45, 0xb9, 0xb0, 0x23, - 0x90, 0x7b, 0x11, 0x32, 0xc1, 0xf2, 0x12, 0xdb, - 0x61, 0xf9, 0xa7, 0x31, 0x24, 0xc8, 0x66, 0x4e, - 0x49, 0x72, 0xb9, 0xce, 0xa6, 0x5b, 0xab, 0x46, - 0x45, 0xdf, 0x75, 0x76, 0x3e, 0xd3, 0x42, 0x9f, - 0x5c, 0x1b, 0x8c, 0x25, 0x50, 0xb9, 0xad, 0xae - } +static const TPM2B_PUBLIC_KEY_RSA RSA_768_D = { + .t = {96, { + 0x01, 0x40, 0x76, 0x7b, 0x41, 0xd6, 0xd9, 0x17, + 0xfe, 0x52, 0x6d, 0xdd, 0x24, 0x70, 0xbc, 0x97, + 0x7e, 0xcf, 0x54, 0x22, 0x4c, 0x71, 0x29, 0xf5, + 0xb2, 0xe2, 0xf6, 0xf8, 0x8b, 0x9e, 0x20, 0x1a, + 0x1e, 0x67, 0xee, 0x59, 0xf9, 0x83, 0x6b, 0x91, + 0x8d, 0xdf, 0x03, 0xfc, 0xdd, 0x0f, 0x35, 0xd7, + 0xa2, 0x5d, 0x06, 0x3f, 0x45, 0xb9, 0xb0, 0x23, + 0x90, 0x7b, 0x11, 0x32, 0xc1, 0xf2, 0x12, 0xdb, + 0x61, 0xf9, 0xa7, 0x31, 0x24, 0xc8, 0x66, 0x4e, + 0x49, 0x72, 0xb9, 0xce, 0xa6, 0x5b, 0xab, 0x46, + 0x45, 0xdf, 0x75, 0x76, 0x3e, 0xd3, 0x42, 0x9f, + 0x5c, 0x1b, 0x8c, 0x25, 0x50, 0xb9, 0xad, 0xae } } }; -static const struct TPM2B_aligned RSA_2048_N = { - .bn = { - .t = {256, { - 0x99, 0xa9, 0x93, 0xdf, 0xe8, 0xde, 0x41, 0x07, - 0xe9, 0xb1, 0x4f, 0x53, 0xa6, 0x11, 0xe3, 0x67, - 0x88, 0xc5, 0x9a, 0x57, 0xa5, 0x38, 0x1f, 0x69, - 0x51, 0xf2, 0xa7, 0xb5, 0x6a, 0xd2, 0x1a, 0xf2, - 0x0c, 0x62, 0xad, 0x33, 0x1f, 0x82, 0x21, 0x4a, - 0x72, 0xb3, 0x6e, 0xba, 0xfd, 0x66, 0x3e, 0xef, - 0x40, 0x78, 0xa7, 0x37, 0x97, 0x4a, 0x74, 0x63, - 0x23, 0x05, 0x2e, 0x55, 0x6d, 0x36, 0xd0, 0xb7, - 0x8c, 0xb7, 0x83, 0x60, 0x3b, 0xa1, 0x58, 0x5d, - 0xdc, 0xef, 0xf7, 0x2c, 0x5e, 0x05, 0x27, 0xbc, - 0xb0, 0x4d, 0xc9, 0xff, 0x04, 0x50, 0x22, 0x97, - 0xe7, 0x15, 0x66, 0xa5, 0x24, 0x0e, 0x86, 0xa6, - 0x36, 0x9c, 0x92, 0xa2, 0x16, 0x51, 0xed, 0xc2, - 0xea, 0xbf, 0xf4, 0xb2, 0x5e, 0x3a, 0xd7, 0xc5, - 0xa3, 0xfa, 0xf0, 0xcf, 0xcf, 0x7b, 0xc8, 0x5c, - 0x07, 0xe2, 0xcc, 0xa8, 0xb8, 0x36, 0x76, 0xb1, - 0xc9, 0xbb, 0x48, 0x38, 0xbe, 0x0b, 0x57, 0xce, - 0x05, 0x2d, 0xf1, 0xdb, 0x7b, 0x94, 0xb6, 0xcd, - 0x3a, 0xa8, 0x50, 0x49, 0xca, 0x18, 0xb3, 0x52, - 0x18, 0x49, 0xde, 0x10, 0xf8, 0x41, 0x40, 0x6e, - 0x51, 0xaf, 0xdd, 0x06, 0xc3, 0x30, 0xc7, 0x57, - 0x6b, 0xd4, 0xdc, 0x10, 0x46, 0x30, 0x04, 0x23, - 0x98, 0xc0, 0xf0, 0xb4, 0xeb, 0x5d, 0xc9, 0x6e, - 0x50, 0x1f, 0xd7, 0xd9, 0xac, 0xf2, 0x0d, 0x06, - 0xe3, 0x9b, 0x5e, 0xde, 0x2a, 0xaa, 0xb1, 0xaf, - 0xd6, 0x97, 0x68, 0x2d, 0xeb, 0x0c, 0x7b, 0x75, - 0x49, 0x23, 0x64, 0xbe, 0x90, 0x53, 0x82, 0x99, - 0xa2, 0x50, 0x78, 0x0c, 0x9f, 0x72, 0xc1, 0x0a, - 0x0f, 0x32, 0x75, 0xed, 0x1f, 0x6e, 0xef, 0x2c, - 0x2e, 0x1d, 0x4c, 0x19, 0x85, 0x5c, 0x90, 0x95, - 0xe3, 0x4b, 0x86, 0xf5, 0xb7, 0x9f, 0x73, 0xcd, - 0xbe, 0x15, 0x8e, 0x43, 0x2e, 0x61, 0xd7, 0x9c - } +static const TPM2B_PUBLIC_KEY_RSA RSA_2048_N = { + .t = {256, { + 0x99, 0xa9, 0x93, 0xdf, 0xe8, 0xde, 0x41, 0x07, + 0xe9, 0xb1, 0x4f, 0x53, 0xa6, 0x11, 0xe3, 0x67, + 0x88, 0xc5, 0x9a, 0x57, 0xa5, 0x38, 0x1f, 0x69, + 0x51, 0xf2, 0xa7, 0xb5, 0x6a, 0xd2, 0x1a, 0xf2, + 0x0c, 0x62, 0xad, 0x33, 0x1f, 0x82, 0x21, 0x4a, + 0x72, 0xb3, 0x6e, 0xba, 0xfd, 0x66, 0x3e, 0xef, + 0x40, 0x78, 0xa7, 0x37, 0x97, 0x4a, 0x74, 0x63, + 0x23, 0x05, 0x2e, 0x55, 0x6d, 0x36, 0xd0, 0xb7, + 0x8c, 0xb7, 0x83, 0x60, 0x3b, 0xa1, 0x58, 0x5d, + 0xdc, 0xef, 0xf7, 0x2c, 0x5e, 0x05, 0x27, 0xbc, + 0xb0, 0x4d, 0xc9, 0xff, 0x04, 0x50, 0x22, 0x97, + 0xe7, 0x15, 0x66, 0xa5, 0x24, 0x0e, 0x86, 0xa6, + 0x36, 0x9c, 0x92, 0xa2, 0x16, 0x51, 0xed, 0xc2, + 0xea, 0xbf, 0xf4, 0xb2, 0x5e, 0x3a, 0xd7, 0xc5, + 0xa3, 0xfa, 0xf0, 0xcf, 0xcf, 0x7b, 0xc8, 0x5c, + 0x07, 0xe2, 0xcc, 0xa8, 0xb8, 0x36, 0x76, 0xb1, + 0xc9, 0xbb, 0x48, 0x38, 0xbe, 0x0b, 0x57, 0xce, + 0x05, 0x2d, 0xf1, 0xdb, 0x7b, 0x94, 0xb6, 0xcd, + 0x3a, 0xa8, 0x50, 0x49, 0xca, 0x18, 0xb3, 0x52, + 0x18, 0x49, 0xde, 0x10, 0xf8, 0x41, 0x40, 0x6e, + 0x51, 0xaf, 0xdd, 0x06, 0xc3, 0x30, 0xc7, 0x57, + 0x6b, 0xd4, 0xdc, 0x10, 0x46, 0x30, 0x04, 0x23, + 0x98, 0xc0, 0xf0, 0xb4, 0xeb, 0x5d, 0xc9, 0x6e, + 0x50, 0x1f, 0xd7, 0xd9, 0xac, 0xf2, 0x0d, 0x06, + 0xe3, 0x9b, 0x5e, 0xde, 0x2a, 0xaa, 0xb1, 0xaf, + 0xd6, 0x97, 0x68, 0x2d, 0xeb, 0x0c, 0x7b, 0x75, + 0x49, 0x23, 0x64, 0xbe, 0x90, 0x53, 0x82, 0x99, + 0xa2, 0x50, 0x78, 0x0c, 0x9f, 0x72, 0xc1, 0x0a, + 0x0f, 0x32, 0x75, 0xed, 0x1f, 0x6e, 0xef, 0x2c, + 0x2e, 0x1d, 0x4c, 0x19, 0x85, 0x5c, 0x90, 0x95, + 0xe3, 0x4b, 0x86, 0xf5, 0xb7, 0x9f, 0x73, 0xcd, + 0xbe, 0x15, 0x8e, 0x43, 0x2e, 0x61, 0xd7, 0x9c } } }; -static const struct TPM2B_aligned RSA_2048_D = { - .bn = { - .t = {256, { - 0xf5, 0x95, 0x99, 0xca, 0x31, 0x84, 0x66, 0x4c, - 0xa9, 0x29, 0x24, 0x74, 0x22, 0x29, 0xb4, 0x64, - 0x5b, 0x22, 0xeb, 0x5d, 0x2f, 0xe3, 0x62, 0x21, - 0x02, 0x16, 0x33, 0x16, 0xe4, 0xad, 0x10, 0x52, - 0x3f, 0xf0, 0xf1, 0x86, 0x68, 0x54, 0x47, 0x24, - 0xcc, 0x5c, 0x08, 0x82, 0x0f, 0x68, 0xdd, 0x79, - 0x55, 0x11, 0x07, 0x6d, 0x56, 0x89, 0x30, 0xf1, - 0x7f, 0xaf, 0xb1, 0xb8, 0x41, 0xe8, 0x7a, 0x82, - 0x03, 0x1a, 0x95, 0xd7, 0x00, 0x7c, 0xb7, 0x04, - 0xee, 0x8e, 0x9b, 0xbc, 0x4f, 0xdf, 0xa8, 0x38, - 0xea, 0xbf, 0xfb, 0x79, 0xa0, 0xd3, 0xd6, 0xc2, - 0x1f, 0x67, 0xa2, 0x88, 0x2b, 0x1d, 0x23, 0xc6, - 0x19, 0xfc, 0x27, 0x45, 0xcf, 0xbd, 0xc7, 0xe9, - 0x6e, 0x7a, 0xe2, 0x84, 0x4c, 0x9c, 0x16, 0x65, - 0xb0, 0xa6, 0x88, 0xc5, 0xbe, 0x30, 0x70, 0xb9, - 0xc6, 0x6d, 0x3f, 0xf5, 0xcd, 0x52, 0x97, 0x54, - 0x15, 0x26, 0xd2, 0x06, 0x82, 0xcc, 0xe7, 0x02, - 0x1a, 0x23, 0xb8, 0x0a, 0x71, 0xde, 0x91, 0x82, - 0xe4, 0x1e, 0xbe, 0x67, 0xeb, 0x94, 0x24, 0x22, - 0xe7, 0x27, 0xfa, 0x52, 0xf2, 0x94, 0x5e, 0x6e, - 0x85, 0xc1, 0x47, 0x42, 0xdc, 0xae, 0x8b, 0xaf, - 0x4e, 0x32, 0xc6, 0x8d, 0xd3, 0xc0, 0xa2, 0x6b, - 0x02, 0x96, 0x76, 0x0a, 0x96, 0x87, 0x16, 0x35, - 0xc1, 0xea, 0xf7, 0x91, 0xa4, 0xa3, 0x1b, 0x40, - 0xc0, 0x95, 0x20, 0x14, 0x9f, 0x32, 0xad, 0x39, - 0x19, 0x29, 0xea, 0x80, 0x33, 0x2c, 0x31, 0x86, - 0xca, 0x5e, 0x89, 0xf0, 0x74, 0xdf, 0x8f, 0xdc, - 0xa3, 0xf3, 0xbe, 0x26, 0xd0, 0xa3, 0xb4, 0x7c, - 0x6e, 0xdf, 0xad, 0xdb, 0x26, 0xf3, 0xaa, 0xfb, - 0x68, 0x56, 0x43, 0xb9, 0x7f, 0x19, 0x70, 0x67, - 0x5a, 0x66, 0x15, 0x6f, 0xe2, 0x14, 0x8f, 0xbc, - 0x89, 0x8b, 0x4a, 0xdf, 0x1f, 0x02, 0x9d, 0x4e - } +static const TPM2B_PUBLIC_KEY_RSA RSA_2048_D = { + .t = {256, { + 0xf5, 0x95, 0x99, 0xca, 0x31, 0x84, 0x66, 0x4c, + 0xa9, 0x29, 0x24, 0x74, 0x22, 0x29, 0xb4, 0x64, + 0x5b, 0x22, 0xeb, 0x5d, 0x2f, 0xe3, 0x62, 0x21, + 0x02, 0x16, 0x33, 0x16, 0xe4, 0xad, 0x10, 0x52, + 0x3f, 0xf0, 0xf1, 0x86, 0x68, 0x54, 0x47, 0x24, + 0xcc, 0x5c, 0x08, 0x82, 0x0f, 0x68, 0xdd, 0x79, + 0x55, 0x11, 0x07, 0x6d, 0x56, 0x89, 0x30, 0xf1, + 0x7f, 0xaf, 0xb1, 0xb8, 0x41, 0xe8, 0x7a, 0x82, + 0x03, 0x1a, 0x95, 0xd7, 0x00, 0x7c, 0xb7, 0x04, + 0xee, 0x8e, 0x9b, 0xbc, 0x4f, 0xdf, 0xa8, 0x38, + 0xea, 0xbf, 0xfb, 0x79, 0xa0, 0xd3, 0xd6, 0xc2, + 0x1f, 0x67, 0xa2, 0x88, 0x2b, 0x1d, 0x23, 0xc6, + 0x19, 0xfc, 0x27, 0x45, 0xcf, 0xbd, 0xc7, 0xe9, + 0x6e, 0x7a, 0xe2, 0x84, 0x4c, 0x9c, 0x16, 0x65, + 0xb0, 0xa6, 0x88, 0xc5, 0xbe, 0x30, 0x70, 0xb9, + 0xc6, 0x6d, 0x3f, 0xf5, 0xcd, 0x52, 0x97, 0x54, + 0x15, 0x26, 0xd2, 0x06, 0x82, 0xcc, 0xe7, 0x02, + 0x1a, 0x23, 0xb8, 0x0a, 0x71, 0xde, 0x91, 0x82, + 0xe4, 0x1e, 0xbe, 0x67, 0xeb, 0x94, 0x24, 0x22, + 0xe7, 0x27, 0xfa, 0x52, 0xf2, 0x94, 0x5e, 0x6e, + 0x85, 0xc1, 0x47, 0x42, 0xdc, 0xae, 0x8b, 0xaf, + 0x4e, 0x32, 0xc6, 0x8d, 0xd3, 0xc0, 0xa2, 0x6b, + 0x02, 0x96, 0x76, 0x0a, 0x96, 0x87, 0x16, 0x35, + 0xc1, 0xea, 0xf7, 0x91, 0xa4, 0xa3, 0x1b, 0x40, + 0xc0, 0x95, 0x20, 0x14, 0x9f, 0x32, 0xad, 0x39, + 0x19, 0x29, 0xea, 0x80, 0x33, 0x2c, 0x31, 0x86, + 0xca, 0x5e, 0x89, 0xf0, 0x74, 0xdf, 0x8f, 0xdc, + 0xa3, 0xf3, 0xbe, 0x26, 0xd0, 0xa3, 0xb4, 0x7c, + 0x6e, 0xdf, 0xad, 0xdb, 0x26, 0xf3, 0xaa, 0xfb, + 0x68, 0x56, 0x43, 0xb9, 0x7f, 0x19, 0x70, 0x67, + 0x5a, 0x66, 0x15, 0x6f, 0xe2, 0x14, 0x8f, 0xbc, + 0x89, 0x8b, 0x4a, 0xdf, 0x1f, 0x02, 0x9d, 0x4e } } }; static const RSA_KEY RSA_768 = { - 65537, (TPM2B *) &RSA_768_N.bn.b, (TPM2B *) &RSA_768_D.bn.b + 65537, (TPM2B *) &RSA_768_N.b, (TPM2B *) &RSA_768_D.b }; static const RSA_KEY RSA_2048 = { - 65537, (TPM2B *) &RSA_2048_N.bn.b, (TPM2B *) &RSA_2048_D.bn.b + 65537, (TPM2B *) &RSA_2048_N.b, (TPM2B *) &RSA_2048_D.b }; #define MAX_MSG_BYTES RSA_MAX_BYTES diff --git a/chip/g/dcrypto/bn.c b/chip/g/dcrypto/bn.c index 829d10e602..f4aaf11912 100644 --- a/chip/g/dcrypto/bn.c +++ b/chip/g/dcrypto/bn.c @@ -18,16 +18,14 @@ void bn_init(struct BIGNUM *b, void *buf, size_t len) { /* Only word-multiple sized buffers accepted. */ assert((len & 0x3) == 0); - /* Only word-aligned buffers accepted. */ - assert(((uintptr_t) buf & 0x3) == 0); b->dmax = len / BN_BYTES; dcrypto_memset(buf, 0x00, len); - b->d = (uint32_t *) buf; + b->d = (struct access_helper *) buf; } int bn_check_topbit(const struct BIGNUM *N) { - return N->d[N->dmax - 1] >> 31; + return BN_DIGIT(N, N->dmax - 1) >> 31; } /* a[n]. */ @@ -43,7 +41,7 @@ static int bn_is_bit_set(const struct BIGNUM *a, int n) if (a->dmax <= i) return 0; - return (a->d[i] >> j) & 1; + return (BN_DIGIT(a, i) >> j) & 1; } /* a[] >= b[]. */ @@ -52,9 +50,9 @@ static int bn_gte(const struct BIGNUM *a, const struct BIGNUM *b) { int i; - for (i = a->dmax - 1; a->d[i] == b->d[i] && i > 0; --i) + for (i = a->dmax - 1; BN_DIGIT(a, i) == BN_DIGIT(b, i) && i > 0; --i) ; - return a->d[i] >= b->d[i]; + return BN_DIGIT(a, i) >= BN_DIGIT(b, i); } /* c[] = c[] - a[], assumes c > a. */ @@ -64,8 +62,8 @@ static uint32_t bn_sub(struct BIGNUM *c, const struct BIGNUM *a) int i; for (i = 0; i < a->dmax; i++) { - A += (uint64_t) c->d[i] - a->d[i]; - c->d[i] = (uint32_t) A; + A += (uint64_t) BN_DIGIT(c, i) - BN_DIGIT(a, i); + BN_DIGIT(c, i) = (uint32_t) A; A >>= 32; } return (uint32_t) A; /* 0 or -1. */ @@ -78,8 +76,8 @@ static uint32_t bn_add(struct BIGNUM *c, const struct BIGNUM *a) int i; for (i = 0; i < a->dmax; ++i) { - A += (uint64_t) c->d[i] + a->d[i]; - c->d[i] = (uint32_t) A; + A += (uint64_t) BN_DIGIT(c, i) + BN_DIGIT(a, i); + BN_DIGIT(c, i) = (uint32_t) A; A >>= 32; } @@ -94,9 +92,9 @@ static uint32_t bn_lshift(struct BIGNUM *r) uint32_t carry = 0; for (i = 0; i < r->dmax; i++) { - w = (r->d[i] << 1) | carry; - carry = r->d[i] >> 31; - r->d[i] = w; + w = (BN_DIGIT(r, i) << 1) | carry; + carry = BN_DIGIT(r, i) >> 31; + BN_DIGIT(r, i) = w; } return carry; } @@ -113,20 +111,21 @@ static void bn_mont_mul_add(struct BIGNUM *c, const uint32_t a, { register uint64_t tmp; - tmp = c->d[0] + (uint64_t) a * b->d[0]; + tmp = BN_DIGIT(c, 0) + (uint64_t) a * BN_DIGIT(b, 0); A = tmp >> 32; d0 = (uint32_t) tmp * (uint32_t) nprime; - tmp = (uint32_t)tmp + (uint64_t) d0 * N->d[0]; + tmp = (uint32_t)tmp + (uint64_t) d0 * BN_DIGIT(N, 0); B = tmp >> 32; } for (i = 0; i < N->dmax - 1;) { register uint64_t tmp; - tmp = A + (uint64_t) a * b->d[i + 1] + c->d[i + 1]; + tmp = A + (uint64_t) a * BN_DIGIT(b, i + 1) + + BN_DIGIT(c, i + 1); A = tmp >> 32; - tmp = B + (uint64_t) d0 * N->d[i + 1] + (uint32_t) tmp; - c->d[i] = (uint32_t) tmp; + tmp = B + (uint64_t) d0 * BN_DIGIT(N, i + 1) + (uint32_t) tmp; + BN_DIGIT(c, i) = (uint32_t) tmp; B = tmp >> 32; ++i; } @@ -134,7 +133,7 @@ static void bn_mont_mul_add(struct BIGNUM *c, const uint32_t a, { uint64_t tmp = (uint64_t) A + B; - c->d[i] = (uint32_t) tmp; + BN_DIGIT(c, i) = (uint32_t) tmp; A = tmp >> 32; /* 0 or 1. */ if (A) bn_sub(c, N); @@ -149,11 +148,11 @@ static void bn_mont_mul(struct BIGNUM *c, const struct BIGNUM *a, int i; for (i = 0; i < N->dmax; i++) - c->d[i] = 0; + BN_DIGIT(c, i) = 0; - bn_mont_mul_add(c, a ? a->d[0] : 1, b, nprime, N); + bn_mont_mul_add(c, a ? BN_DIGIT(a, 0) : 1, b, nprime, N); for (i = 1; i < N->dmax; i++) - bn_mont_mul_add(c, a ? a->d[i] : 0, b, nprime, N); + bn_mont_mul_add(c, a ? BN_DIGIT(a, i) : 0, b, nprime, N); } /* Mongomery R * R % N, R = 1 << (1 + log2N). */ @@ -206,11 +205,11 @@ void bn_mont_modexp(struct BIGNUM *output, const struct BIGNUM *input, bn_init(&acc, acc_buf, bn_size(N)); bn_init(&aR, aR_buf, bn_size(N)); - nprime = bn_compute_nprime(N->d[0]); + nprime = bn_compute_nprime(BN_DIGIT(N, 0)); bn_compute_RR(&RR, N); bn_mont_mul(&acc, NULL, &RR, nprime, N); /* R = 1 * RR / R % N */ bn_mont_mul(&aR, input, &RR, nprime, N); /* aR = a * RR / R % N */ - output->d[0] = 1; + BN_DIGIT(output, 0) = 1; /* TODO(ngm): burn stack space and use windowing. */ for (i = exp->dmax * BN_BITS2 - 1; i >= 0; i--) { @@ -232,7 +231,7 @@ void bn_mont_modexp(struct BIGNUM *output, const struct BIGNUM *input, bn_mont_mul(output, NULL, &acc, nprime, N); /* Convert out. */ /* Copy to output buffer if necessary. */ - if (acc.d != acc_buf) { + if (acc.d != (struct access_helper *) acc_buf) { memcpy(acc.d, acc_buf, bn_size(output)); *output = acc; } diff --git a/chip/g/dcrypto/internal.h b/chip/g/dcrypto/internal.h index 61db833a53..36aa35f6bf 100644 --- a/chip/g/dcrypto/internal.h +++ b/chip/g/dcrypto/internal.h @@ -89,10 +89,12 @@ void dcrypto_sha_wait(enum sha_mode mode, uint32_t *digest); #define BN_BYTES 4 struct BIGNUM { - uint32_t dmax; /* Size of d, in 32-bit words. */ - uint32_t *d; /* Word array, little endian format ... */ + uint32_t dmax; /* Size of d, in 32-bit words. */ + struct access_helper *d; /* Word array, little endian format ... */ }; +#define BN_DIGIT(b, i) ((b)->d[(i)].udata) + void bn_init(struct BIGNUM *bn, void *buf, size_t len); #define bn_size(b) ((b)->dmax * BN_BYTES) int bn_check_topbit(const struct BIGNUM *N); diff --git a/chip/g/dcrypto/rsa.c b/chip/g/dcrypto/rsa.c index d9774a2101..b6128923ef 100644 --- a/chip/g/dcrypto/rsa.c +++ b/chip/g/dcrypto/rsa.c @@ -339,7 +339,7 @@ int DCRYPTO_rsa_encrypt(struct RSA *rsa, uint8_t *out, uint32_t *out_len, bn_init(&padded, padded_buf, bn_size(&rsa->N)); bn_init(&encrypted, out, bn_size(&rsa->N)); bn_init(&e, e_buf, sizeof(e_buf)); - *e.d = rsa->e; + BN_DIGIT(&e, 0) = rsa->e; switch (padding) { case PADDING_MODE_OAEP: @@ -479,7 +479,7 @@ int DCRYPTO_rsa_verify(struct RSA *rsa, const uint8_t *digest, memcpy(signature_buf, sig, bn_size(&rsa->N)); bn_init(&padded, padded_buf, bn_size(&rsa->N)); bn_init(&e, e_buf, sizeof(e_buf)); - *e.d = rsa->e; + BN_DIGIT(&e, 0) = rsa->e; /* Reverse from big-endian to little-endian notation. */ reverse((uint8_t *) signature.d, signature.dmax * BN_BYTES); |