Skip to content

Commit

Permalink
clang-format almost all first-party code
Browse files Browse the repository at this point in the history
Added a .clang-format file and auto-formatted almost all first-party code
(all except core/include/config.h).

Required some additional #include directives in a few headers,
due to changes in #include ordering.

Also fixed up CMakeLists.txt:
- aes_gcm_dev.h is only included in dev builds
- aes_gcm_ncipher.h is only included in ncipher builds.

Required some additional #include directives in a few headers,
due to changes in #include ordering.
  • Loading branch information
ivmaykov committed Apr 19, 2023
1 parent 9879eea commit 307e1c4
Show file tree
Hide file tree
Showing 61 changed files with 1,011 additions and 786 deletions.
225 changes: 225 additions & 0 deletions core/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
# see https://clang.llvm.org/docs/ClangFormatStyleOptions.html
---
# BasedOnStyle: Mozilla

Language: Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: AlwaysBreak
AlignArrayOfStructures: None
AlignConsecutiveAssignments:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: true
AlignConsecutiveBitFields:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveDeclarations:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveMacros:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments:
Kind: Always
OverEmptyLines: 0
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: false
BinPackParameters: false
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: Never
AfterEnum: true
AfterExternBlock: true
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: true
BreakAfterAttributes: Never
BreakAfterJavaFieldAnnotations: false
BreakArrays: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Attach
BreakBeforeInlineASMColon: OnlyMultiline
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakStringLiterals: true
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: AfterExternBlock
IndentGotoLabels: true
IndentPPDirectives: BeforeHash
IndentRequiresClause: true
IndentWidth: 2
IndentWrappedFunctionNames: false
InsertBraces: true
InsertNewlineAtEOF: true
InsertTrailingCommas: None
IntegerLiteralSeparator:
Binary: 0
BinaryMinDigits: 0
Decimal: 0
DecimalMinDigits: 0
Hex: 0
HexMinDigits: 0
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
LambdaBodyIndentation: Signature
LineEnding: DeriveLF
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: false
PackConstructorInitializers: BinPack
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
PPIndentWidth: -1
QualifierAlignment: Leave
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
RemoveSemicolon: true
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
SeparateDefinitionBlocks: Always
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: LexicographicNumeric
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
AfterRequiresInClause: false
AfterRequiresInExpression: false
BeforeNonEmptyParentheses: false
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Latest
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseTab: Never
WhitespaceSensitiveMacros:
- BOOST_PP_STRINGIZE
- CF_SWIFT_NAME
- NS_SWIFT_NAME
- PP_STRINGIZE
- STRINGIZE
8 changes: 5 additions & 3 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ set(checks_SRC
list(APPEND main_SRC ${checks_SRC})

set(include_SRC
"include/aes_gcm_dev.h"
"include/aes_gcm_ncipher.h"
"include/checks.h"
"include/config.h"
"include/conv.h"
Expand All @@ -111,6 +109,8 @@ set(include_SRC
list(APPEND main_SRC ${include_SRC})

if ($ENV{TARGET} MATCHES "nCipher")
list(APPEND include_SRC "include/aes_gcm_ncipher.h")

set(extra_SRC
"src/ncipher/additional_checks.c"
"src/ncipher/aes_gcm.c"
Expand All @@ -123,7 +123,9 @@ if ($ENV{TARGET} MATCHES "nCipher")
"src/ncipher/transact.c"
)
list(APPEND main_SRC ${extra_SRC})
else ()
else () # "dev" target
list(APPEND include_SRC "include/aes_gcm_dev.h")

set(extra_SRC
"src/dev/additional_checks.c"
"src/dev/no_rollback.c"
Expand Down
1 change: 1 addition & 0 deletions core/external-crypto/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DisableFormat: true
26 changes: 18 additions & 8 deletions core/include/aes_gcm_dev.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <squareup/subzero/internal.pb.h> // For error codes
#include <stdint.h>

/* This header is meant for the dev target, and should only be included in the
* dev implementation */

Expand All @@ -14,17 +17,24 @@
// reserved for an invalid key object in the nCipher target. We would like to
// simulate this behavior in the dev target, and therefore avoid value 0 for
// M_KeyID enum below.
typedef enum {master_seed_encryption_key = 1,
pub_key_encryption_key = 2} M_KeyID;
typedef enum { master_seed_encryption_key = 1, pub_key_encryption_key = 2 } M_KeyID;

// Key encryption key declaration, for AES-128-GCM
// KEK[0], i.e., KEK[master_seed_encryption_key - 1], is master seed encryption key
// KEK[1], i.e., KEK[pub_key_encryption_key], is public key encryption key
extern uint8_t KEK[2][16];

Result aes_gcm_encrypt(M_KeyID keyId, uint8_t *plaintext, size_t plaintext_len,
uint8_t *ciphertext, size_t ciphertext_len,
size_t *bytes_written);
Result aes_gcm_decrypt(M_KeyID keyId, const uint8_t *ciphertext,
size_t ciphertext_len, uint8_t *plaintext,
size_t plaintext_len, size_t *bytes_written);
Result aes_gcm_encrypt(
M_KeyID keyId,
uint8_t* plaintext,
size_t plaintext_len,
uint8_t* ciphertext,
size_t ciphertext_len,
size_t* bytes_written);
Result aes_gcm_decrypt(
M_KeyID keyId,
const uint8_t* ciphertext,
size_t ciphertext_len,
uint8_t* plaintext,
size_t plaintext_len,
size_t* bytes_written);
24 changes: 18 additions & 6 deletions core/include/aes_gcm_ncipher.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
#pragma once

#include <nfastapp.h>
#include <squareup/subzero/internal.pb.h> // For error codes
#include <stdint.h>

/* This header is meant for the nCipher target, and should only be included in the
* codesafe/ncipher implementation */

// magic string for binary static analysis
// aes_gcm_ncipher:$(echo -n "aes_gcm_ncipher" | sha256sum | cut -c1-16)
#define MAGIC "aes_gcm_ncipher:ffb944993c73a1d3"

Result aes_gcm_encrypt(M_KeyID keyId, uint8_t *plaintext, size_t plaintext_len,
uint8_t *ciphertext, size_t ciphertext_len,
size_t *bytes_written);
Result aes_gcm_decrypt(M_KeyID keyId, const uint8_t *ciphertext,
size_t ciphertext_len, uint8_t *plaintext,
size_t plaintext_len, size_t *bytes_written);
Result aes_gcm_encrypt(
M_KeyID keyId,
uint8_t* plaintext,
size_t plaintext_len,
uint8_t* ciphertext,
size_t ciphertext_len,
size_t* bytes_written);
Result aes_gcm_decrypt(
M_KeyID keyId,
const uint8_t* ciphertext,
size_t ciphertext_len,
uint8_t* plaintext,
size_t plaintext_len,
size_t* bytes_written);
2 changes: 1 addition & 1 deletion core/include/checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ int verify_conv_btc_to_satoshi(void);
ERROR("got: %s", value); \
return -1; \
} \
} while(0)
} while (0)
5 changes: 5 additions & 0 deletions core/include/config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

// clang-format off
// (to avoid conflicting with production patch files)

// maximum size for xpub. TODO: we can make this 112 bytes, based on:
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format
#define XPUB_SIZE 128
Expand Down Expand Up @@ -118,3 +121,5 @@
#define MASTER_SEED_SIZE 64

#define COMPRESSED_PUBKEY_SIZE 33

// clang-format on
14 changes: 7 additions & 7 deletions core/include/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

#include <hasher.h>

void hash_uint8(Hasher *hasher, uint8_t value);
void hash_uint16(Hasher *hasher, uint16_t value);
void hash_uint32(Hasher *hasher, uint32_t value);
void hash_uint64(Hasher *hasher, uint64_t value);
void hash_uint8(Hasher* hasher, uint8_t value);
void hash_uint16(Hasher* hasher, uint16_t value);
void hash_uint32(Hasher* hasher, uint32_t value);
void hash_uint64(Hasher* hasher, uint64_t value);
// hashes are serialized in "normal" order
void hash_bytes(Hasher *hasher, const uint8_t *data, size_t len);
void hash_bytes(Hasher* hasher, const uint8_t* data, size_t len);
// addresses are serialized in reverse order
void hash_rev_bytes(Hasher *hasher, const uint8_t *data, size_t len);
void hash_rev_bytes(Hasher* hasher, const uint8_t* data, size_t len);
// scripts are serialized with a length prefix
void hash_var_bytes(Hasher *hasher, const uint8_t *data, size_t len);
void hash_var_bytes(Hasher* hasher, const uint8_t* data, size_t len);
7 changes: 3 additions & 4 deletions core/include/init_wallet.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#pragma once

#include "config.h"

#include <squareup/subzero/common.pb.h>
#include <squareup/subzero/internal.pb.h>
#include <stdint.h>

#include "config.h"

Result mix_entropy(uint8_t wallet_entropy[static MASTER_SEED_SIZE],
const InternalCommandRequest* const in);
Result mix_entropy(uint8_t wallet_entropy[static MASTER_SEED_SIZE], const InternalCommandRequest* const in);
Loading

0 comments on commit 307e1c4

Please sign in to comment.