-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Symmetric keygen: high-level wrappers (#561)
* Symmetric keygen: ThemisPP C++ provides both convenient and in-place key generation routines. In-place key generation may be useful for C++03 code that does not want to have an extra copy of std::vector when it's being returned. ThemisPP does not provide separate types for keys, it works with plain std::vector. * Symmetric keygen: WasmThemis Straightforward implementation. The only interestring thing is that size_t is mapped to i32 with Emscripten, as noted in all other APIs. * Symmetric keygen: GoThemis Straightforward implementation. GoThemis provides wrapper types for private and public keys so we add another one with the same interface for symmetric keys. Currently GoThemis does not return Themis error codes to Go, so CGo code returns "bool" for simplicity and consistency. * Symmetric keygen: ObjCThemis and SwiftThemis Straightforward implementation. Objective-C function is automatically bridged to Swift, we don't have to do anything for that. Keypair generation API for ObjCThemis does not report exact errors to the user and simply returns "nil", We do the same for symmetric key generation for consistency. * Symmetric keygen: PHPThemis Straightforward implementation. PHPThemis exports its functionality as PHP functions so just add a new one. Byte arrays are returned as simply strings, no new types here. Just don't forget that we have to support both PHP 5 and PHP 7 wrappers. * Symmetric keygen: PyThemis Straightforward implementation. string_at() returns "str" with Python 2 and "bytes" with Python 3. * Symmetric keygen: RbThemis Straightforward implementation. Probably the most concise one :) * Symmetric keygen: RustThemis Straigtforward implementation (albeit verbose). RustThemis has a relatively high bar for API documentation quality. Don't let it drop and provide detailed descriptions. themis_gen_sym_key() is added to libthemis-sys by bindgen automatically, we don't have to remember to do that manually.
- Loading branch information
Showing
24 changed files
with
577 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package keys | ||
|
||
/* | ||
#cgo LDFLAGS: -lthemis | ||
#include <stdbool.h> | ||
#include <themis/themis.h> | ||
static bool get_sym_key_size(size_t *key_len) | ||
{ | ||
return themis_gen_sym_key(NULL, key_len) == THEMIS_BUFFER_TOO_SMALL; | ||
} | ||
static bool gen_sym_key(void *key, size_t key_len) | ||
{ | ||
return themis_gen_sym_key(key, &key_len) == THEMIS_SUCCESS; | ||
} | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"github.com/cossacklabs/themis/gothemis/errors" | ||
) | ||
|
||
// SymmetricKey stores a master key for Secure Cell. | ||
type SymmetricKey struct { | ||
Value []byte | ||
} | ||
|
||
// NewSymmetricKey generates a new random symmetric key. | ||
func NewSymmetricKey() (*SymmetricKey, error) { | ||
var len C.size_t | ||
if !bool(C.get_sym_key_size(&len)) { | ||
return nil, errors.New("Failed to get symmetric key size") | ||
} | ||
|
||
key := make([]byte, int(len), int(len)) | ||
if !bool(C.gen_sym_key(unsafe.Pointer(&key[0]), len)) { | ||
return nil, errors.New("Failed to generate symmetric key") | ||
} | ||
|
||
return &SymmetricKey{Value: key}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package keys | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
const defaultLength = 32 | ||
|
||
func TestNewSymmetricKey(t *testing.T) { | ||
key, err := NewSymmetricKey() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if len(key.Value) != defaultLength { | ||
t.Error("invalid key.Value") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.