open c: sms crypto example implementation notes...

26
Open C: SMS Crypto Example Implementation Notes Version 1.1 July 6, 2007 S60 platform

Upload: truongtu

Post on 22-Apr-2018

221 views

Category:

Documents


2 download

TRANSCRIPT

Open C: SMS Crypto Example Implementation Notes Version 1.1 July 6, 2007

S60

p

la

tf

or

m

Open C: SMS Crypto Example Implementation Notes | 2

Version 1.1 | July 6, 2007

Legal notice

Copyright © 2007 Nokia Corporation. All rights reserved.

Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. Other product and company names mentioned herein may be trademarks or trade names of their respective owners.

Disclaimer

The information in this document is provided “as is,” with no warranties whatsoever, including any warranty of merchantability, fitness for any particular purpose, or any warranty otherwise arising out of any proposal, specification, or sample. This document is provided for informational purposes only.

Nokia Corporation disclaims all liability, including liability for infringement of any proprietary rights, relating to implementation of information presented in this document. Nokia Corporation does not warrant or represent that such use will not infringe such rights.

Nokia Corporation retains the right to make changes to this specification at any time, without notice.

License

A license is hereby granted to download and print a copy of this specification for personal use only. No other license to any other intellectual property rights is granted herein.

Open C: SMS Crypto Example Implementation Notes | 3

Version 1.1 | July 6, 2007

Contents

1. Introduction ......................................................................................................... 5 2. UI design .............................................................................................................. 6

2.1 Send message ................................................................................................................8 2.2 Read message ..............................................................................................................10

3. Python implementation..................................................................................... 12 3.1 Information sources.......................................................................................................12 3.2 Tools and libraries used for development .....................................................................12 3.3 Application components ................................................................................................13 3.4 Implementation details ..................................................................................................14 3.5 Development process ...................................................................................................14 3.6 Running on hardware....................................................................................................15 3.7 Python version code metrics .........................................................................................15 3.8 Comments.....................................................................................................................15

4. Native implementation ...................................................................................... 17 4.1 Implementation details ..................................................................................................17 4.2 Native version code metrics ..........................................................................................19 4.3 Comments.....................................................................................................................19

5. Code examples .................................................................................................. 20 5.1 Input recipient phone number .......................................................................................20 5.2 Sending an SMS message............................................................................................20

6. Conclusion......................................................................................................... 22 7. References ......................................................................................................... 25 8. Evaluate this resource ...................................................................................... 26

Open C: SMS Crypto Example Implementation Notes | 4

Version 1.1 | July 6, 2007

Change history

June 27, 2007 Version 1.0 Initial document release

July 6, 2007 Version 1.1 Chapter 5 containing simple code examples added.

A comparison of the amount of work required to implement Python UI and Symbian C++ UI added to Chapter 6.

Minor updates in Section 3.2.

Open C: SMS Crypto Example Implementation Notes | 5

Version 1.1 | July 6, 2007

1 . I n t r o d u c t i o n

The purpose of this document is to provide useful information and hints for developers planning to create software using Python for S60 and Open C. In this example Python is used to create a user interface, and message encryption/decryption is done using libraries provided with Open C. This document also includes a discussion about the differences between using Python or Symbian C++ to develop the UI, and the potential pros and cons of choosing either programming language.

The example includes both Python and Symbian C++ versions of the application. Both versions operate in the same way, and their user interfaces are almost identical. The languages can also be used in conjunction with each other as both encrypt and decrypt messages using exactly the same libraries included in Open C.

The document provides readers with some insight into the feasibility of developing software for the S60 platform without having a deep knowledge of Symbian C++, using alternative programming languages and tools such as Python and Open C. Therefore, time usage, the learning curve, and the overall difficulty of developing are all topics that receive close inspection. The tools and methods selected were chosen to give developers the easiest, fastest possible approach for completing both application versions with the least amount of additional work. The results are discussed in the conclusion of this document.

Open C: SMS Crypto Example Implementation Notes | 6

Version 1.1 | July 6, 2007

2 . U I d e s i g n

Both versions of the example provide basic messaging use cases:

Encrypted short message service (SMS) messages can be viewed. (Example applications automatically filter only encrypted messages for reading from the messaging inbox).

The user can decrypt an SMS message if the right password is known.

The user can write, encrypt, and send an SMS message with any password s/he has chosen.

The user can refresh the message list if s/he thinks that new encrypted messages have been received while using the example application.

The different phases of use cases can be seen in Figure 1.

Open C: SMS Crypto Example Implementation Notes | 7

Version 1.1 | July 6, 2007

Figure 1: Application activity diagram

Open C: SMS Crypto Example Implementation Notes | 8

Version 1.1 | July 6, 2007

2.1 Send message

Sending a message involves the following steps in both versions of the application:

Figure 2: Choosing “New Message” in the Options menu

Figure 3: Choosing “Send” after the message is written in the text editor

Open C: SMS Crypto Example Implementation Notes | 9

Version 1.1 | July 6, 2007

Figure 4: Entering a password

Figure 5: Entering recipient information

Open C: SMS Crypto Example Implementation Notes | 10

Version 1.1 | July 6, 2007

Figure 6: Message is being sent

2.2 Read message

Reading a message involves the following steps in both versions of the application:

Figure 7: Choosing “Open” in the Options menu

Open C: SMS Crypto Example Implementation Notes | 11

Version 1.1 | July 6, 2007

Figure 8: Entering a password

Figure 9: Reading the message

Open C: SMS Crypto Example Implementation Notes | 12

Version 1.1 | July 6, 2007

3 . P y t h o n i m p l e m e n ta t i o n

3.1 Information sources

All the needed basic information for developing this example or a similar application can be found from the following sources:

Creating S60 Python Extensions: http://wiki.opensource.nokia.com/projects/PyS60_creating_extensions

Creating Standalone 3rd Edition Applications: http://wiki.opensource.nokia.com/projects/Creating_standalone_apps_in_3rd_ed

Nokia S60 Wiki: http://wiki.opensource.nokia.com/projects/Python_for_S60

Official Python C-API documentation: http://www.python.org/doc/2.3.5/api/api.html

Official Python Extending/Embedding documentation: http://www.python.org/doc/2.3.5/ext/ext.html

3.2 Tools and libraries used for development

Carbide.c++: http://forum.nokia.com/main/resources/tools_and_sdks/carbide_cpp/

o The standard tool for Symbian development. In this project, Carbide.c++ is used for compiling the extension DLL.

Eclipse: http://www.eclipse.org/

o The general-purpose coding IDE that Carbide is based on. Eclipse can be used to test and develop Python-specific code but it is not required for building this example.

Ensymble: http://www.nbl.fi/~nbl928/ensymble.html

o Utility for creating a SIS from a Python script so that the script can be installed to a mobile phone as an application.

PyDev plug-in for Eclipse: http://pydev.sourceforge.net/

o Third-party plug-in for Eclipse that enables syntax highlighting and other helpers for editing Python code. This plug-in is not required for building this example, but it is mentioned here as a helpful tool for developers.

PyS60 Emulation Library: http://sourceforge.net/projects/pys60-compat/

o A compatibility library for Python that emulates the appuifw API and lets you test your script on a standard Windows/Linux Python interpreter. This library is not required for building this example, but it is mentioned here as a helpful tool for developers.

Open C: SMS Crypto Example Implementation Notes | 13

Version 1.1 | July 6, 2007

3.3 Application components

The functions used for encryption and decryption are implemented in s60crypt.c and s60crypt.h. This module does not contain any dependencies to Python, but is a very simple wrapper on top of OpenSSL encryption functions, which can encrypt and decrypt a string of characters using a pass phrase.

wrapper.c contains the "glue code" that exposes the encryption functions to the Python runtime. The function pycrypt_simplecrypt uses a simple XOR-based algorithm and can be used to verify that the extension module works without having any dependencies to OpenC. pycrypt_encrypt and pycrypt_decrypt are straightforward wrappers for the s60crypt functions that convert Python objects given as parameters into C strings (char*). The process is detailed in the official Python extending and embedding documentation and the C-API documentation, but the main idea is that the parameters to the Python function call arrive as a Python tuple object in the "args" parameter, and that tuple has to be broken down into C data types. This is done using the PyArg_ParseTuple function that takes the signature of the Python function as its second parameter. In this case, "s#s#" signifies two string parameters that can contain NULL characters, so each string is broken up into a pair of values: a char pointer to the actual data and an int that contains the length of the data.

s60crypt.c and wrapper.c are compiled into a DLL that is dynamically loaded by the Python interpreter when the script calls "import pycrypt." DLLs containing Python modules are named using the .pyd file extension, otherwise the Python interpreter will refuse to import them.

Finally, there is the encsms.py Python script that contains the user interface and application logic.

encsms.py S60 Python pycrypt.pyd

import pycrypt load DLL

initpycrypt()

pycrypt module object

Figure 10: Importing the extension module

Open C: SMS Crypto Example Implementation Notes | 14

Version 1.1 | July 6, 2007

encsms.py wrapper.cS60 Python s60crypt.c libcrypto

encrypt(text, pw) pyncrypt_encrypt

s60_encrypt(...)MD5

RC4encrypted text

Python C-API

PyArg_ParseTuple

args as C data

Py_BuildValue

result as PyObjectencrypted text

encrypted text

Figure 11: Calling a C function from S60 Python

3.4 Implementation details

The program consists of a main class called "EncSMSApp" and a class for each UI view (SendView, ReadView, and MsgView). Each view is triggered simply by instantiating the view object, and each view class contains the callbacks and UI references used by that particular view. The view objects store a reference to themselves via the callback methods given to UI components, so the application doesn't have to store the views used into variables.

3.5 Development process

Since this example is used to demonstrate a situation where we have an existing C-library (s60crypt in this case) and want to use it in Symbian via Open C, the original Python extension was first implemented and tested on a Linux-based PC. The GUI was prototyped using the PyS60 Emulation Library, which produces a crude version of the UI as a native Windows or Linux application (using wxWindows), so that you can implement most of the UI code without touching the S60 emulator. It's mostly a matter of personal preference, but usually it is easier to work with the standard Python interpreter for as long as possible and then move on to the S60 emulator only for the finishing touches and final testing.

For emulator testing, the extension was compiled by creating an S60 DLL project in Carbide.c++ and adding the s60crypt and wrapper files to it. When compiling a Python extension to S60, the MMP file needs to contain the following lines:

SYSTEMINCLUDE \epoc32\include\python

LIBRARY python222.lib

The compiled DLL needs to be renamed to use a .pyd file extension, and then moved to \Epoc32\winscw\c\sys\bin in order to be usable from the S60 Python in the emulator. Any test scripts that use the extension need to be copied into \Epoc32\winscw\c\python.

Before testing the extension in a proper application, it is a good idea to run the Python Shell in the S60 emulator in order to test importing the extension module and calling a few functions.

Open C: SMS Crypto Example Implementation Notes | 15

Version 1.1 | July 6, 2007

3.6 Running on hardware

To be able to install the created .pyd extension into hardware, a rudimentary sis installer was created that basically just copies the compiled DLL file to \sys\bin\ on hardware so it can be used by Python. This separate installation procedure was chosen to simulate a real-life situation where Python extensions are used with Python applications.

3.7 Python version code metrics

The Python UI implementation in encsms.py contains 4,163 characters of code. Stripping out comments and empty lines leaves 136 effective lines of code for the whole application.

The C portion is a total of 2,980 characters bytes of code, with 92 effective lines of code (of which 44 lines are taken by the wrapper code for Python).

The whole application contains four source code files in total, and the usual bld.inf and .mmp files for building.

3.8 Comments

Coding the GUI is generally very easy on the Python side.

o You don’t have to know about descriptors, leaves, traps, and other Symbian C++-specific concepts.

o The appuifw module is much simpler than the native S60 GUI API.

There are quite a lot of limitations and quirks though, so the appuifw module isn’t quite ready for commercial-quality UI development, due to the following:

o Limited selection of UI components;

o Graphical glitches on some components when running on hardware (although they work fine in the emulator);

o Quality issues such as the cursor from text edit control staying on the screen after the UI component is destroyed.

Knowledge about Python extensions translates directly into the Symbian world

o …as far as writing the code goes. Successfully building the extension DLL required quite a lot of Symbian-specific knowledge, especially when Open C was added to the mix.

S60 3rd Edition contains a lot of things that need to be taken into consideration, including:

o Python tool support and documentation for S60 3rd Edition is still somewhat lacking;

o Advanced Platform Security knowledge is needed for developing and when debugging errors.

Open C: SMS Crypto Example Implementation Notes | 16

Version 1.1 | July 6, 2007

Testing on hardware is cumbersome:

o There is no easy way to transfer files directly to the mobile phone’s C:\Python directory. One option is to build a SIS file and install it every time something is tested.

Tools for building SIS files from Python scripts don’t feel all that mature yet, and the standalone exes wrapped from Python scripts sometimes behave differently than the original script.

Open C: SMS Crypto Example Implementation Notes | 17

Version 1.1 | July 6, 2007

4 . N a t i v e i m p l e m e n ta t i o n

4.1 Implementation details

The native Symbian C++ implementation follows basic S60 guidelines for all UI views. There are two main views, SMSCryptoSymbianCppUIListBoxView and SMSCryptoSymbianCppUITextEditorView, which include functionality similar to the Python implementation. The text editor has two main use cases, as it is used to edit as well as to view received and decrypted messages.

The example application consists of the components illustrated in Figure 12. The executable is built from standard S60 application classes (entry point, application, document, and UI). All encryption and decryption logic written in C is in Crypter, which essentially includes almost identical code if compared to the Python extension described in the previous chapter. All other code is native Symbian C++.

Development was done using Carbide.c++ v1.2. The initial versions of the interface views were created using the UI designer and were further edited by hand. The additional convenience code created by the UI designer was left in the final application.

Open C: SMS Crypto Example Implementation Notes | 18

Version 1.1 | July 6, 2007

Figure 12: Native implementation component diagram

Open C: SMS Crypto Example Implementation Notes | 19

Version 1.1 | July 6, 2007

4.2 Native version code metrics

The native implementation contains 62,202 characters of code. Stripping out comments and empty lines leaves 1,326 effective lines of code for the whole application.

The C portion is a total of 2,066 characters of code, with 26 effective lines of code.

The other files, including .pan, .rss, .loc contain a total of 15,955 characters.

The entire application contains 16 source code files and 17 other resource files totaling 33 files, and the usual bld.inf and .mmp files for building.

4.3 Comments

No mentionable problems were encountered while developing. However, the UI designer of Carbide.c++ v1.2 Developer Edition does overwrite any changes made by hand, if the UI designer is used continuously. This can lead to problems, especially with events of menu items, which can be lost if the editor is used carelessly.

Open C: SMS Crypto Example Implementation Notes | 20

Version 1.1 | July 6, 2007

5 . C o d e e x a m p l e s

This chapter contains two code examples from both the Python and Symbian C++ implementation.

5.1 Input recipient phone number

The following code snippet demonstrates how the user can be requested to input a recipient phone number with a simple dialog in Python and do nothing if no phone number is given. The recipient phone number is saved into rcp.

rcp = ui.query(u'Recipient', 'text') if rcp is None: return

The following code snippet enables similar functionality written in Symbian C++. The text in R_RECIPIENT_QUERY is included from the resource file.

TBuf<querySize> recipient;

CAknTextQueryDialog* RecipientDialog = new ( ELeave ) CAknTextQueryDialog(recipient);

RecipientDialog->PrepareLC( R_RECIPIENT_QUERY );

if ( !RecipientDialog->RunLD() )

{

return;

}

5.2 Sending an SMS message

The following code example shows how to send an SMS message using Python:

messaging.sms_send(rcp, txt)

Similar functionality written in Symbian C++ is implemented in the following code snippet:

RSendAs srv;

if ( srv.Connect() == KErrNone )

{

CleanupClosePushL(srv);

RSendAsMessage msg;

CleanupClosePushL(msg);

msg.CreateL(srv, KSenduiMtmSmsUid);

msg.AddRecipientL(recipient,RSendAsMessage::ESendAsRecipientTo);

msg.SetBodyTextL(message);

Open C: SMS Crypto Example Implementation Notes | 21

Version 1.1 | July 6, 2007

msg.SendMessageAndCloseL();

CleanupStack::Pop(&msg);

}

else

{

CleanupClosePushL(srv);

}

Open C: SMS Crypto Example Implementation Notes | 22

Version 1.1 | July 6, 2007

6 . C o n c l u s i o n

This example shows that developing with Python differs considerably from work done with native Symbian C++. Table 1 shows the metrics of both projects:

Python Symbian C++

Characters of code 4,163 62,202

Effective lines of code 136 1,326

Characters of C code 2,980 2,066

Effective lines of C code 92 26

Characters of other resource files 15,955

Total files (excluding build related) 4 33

Table 1: Code metrics comparison

Table 2 shows the estimated working hours required for creating the Python example.

Python tasks

Task Description Hours

Setting up the environment Installing and configuring the S60 SDK, Carbide.c++, Python for Windows, S60 Python, and Open C SDK.

2

Prototyping the Python UI Prototyping the UI code using the py60s-compat library.

4

Writing Python wrappers for Open C code

Writing the wrappers that publish C functions to the Python interpreter.

3

Compiling the extension for the emulator

Making the necessary build files, compiling the extension .pyd file, and testing in the emulator.

3

Python UI in the emulator Finalizing the UI prototype and testing it in the S60 emulator.

3

Compiling the extension for hardware

Making the necessary adjustments to the build process to build the extension for hardware and testing it on a real device.

3

Python UI in the hardware Testing, fixing, and adjusting the Python UI on hardware.

2

System testing Final testing for all application functionality, especially using device features such as the phonebook and messaging.

5

Open C: SMS Crypto Example Implementation Notes | 23

Version 1.1 | July 6, 2007

Python tasks

Task Description Hours

Total 25

Table 2: Estimated working hours for the Python example

Table 3 shows the estimated working hours required for creating the Symbian C++ example. It should be noted that the S60 UI was first prototyped with Carbide UI designer. Therefore, the actual working hours would be somewhat higher if all the code was written by hand.

Symbian C++ tasks

Task Description Hours

Setting up the environment Installing and configuring the S60 SDK, Carbide.c++, and Open C SDK.

1

Prototyping the S60 UI Prototyping the UI code using the Carbide.c++ UI designer.

1

Writing C class for Open C code

Writing the class which publishes C functions to the native implementation.

1

Implementing required UI functionality

Implementing all the needed functionality and UI views.

25

S60 UI in the emulator Finalizing the UI and testing it in the S60 emulator.

5

S60 UI in the hardware Testing, fixing, and adjusting the S60 UI on hardware.

2

System testing Final testing for all application functionality, especially using device features such as the phonebook and messaging.

6

Total 41

Table 3: Estimated working hours for the Symbian C++ example

It is very obvious that Python implementation is much more compact and is able to provide the same kind of functionality with much lesser complexity. Most of the difference is caused by the huge amount of required S60 UI specific code in the native implementation. However, some of the Carbide UI Designer generated code in native implementation could be removed since it is effectively not needed in this version of the example. In addition, it should be remembered that the native Symbian C++ version already includes localization support (that is, all UI texts are in separate resource files), which naturally adds to the total sum of code. Python does not include the same kind of ready framework, so for similar end results localization would have to be implemented by the developer who creates his or her own code for this purpose.

Even though Python seems to be superior to native Symbian C++ in many aspects, there are many issues (discussed in Section 3.8) that need to be considered carefully. Python implementation for S60 is clearly not polished enough to be used as an all-around

Open C: SMS Crypto Example Implementation Notes | 24

Version 1.1 | July 6, 2007

solution for software development as of now. The main issues include UI quality quirks when Python-based UIs are used in hardware and incomplete tool support.

Carbide UI designer can also be used for fast UI prototyping and it does not require you to learn new programming languages like Python. It should be noted that UI designer automatically generates such UI component related code that may not be needed and may, therefore, clutter up the prototype code. The developer must also realize the features and shortcomings of UI designer since it is even possible to lose code due to the automatic code generation and formatting if it is used carelessly. If prototype code is not reused as a basis for the final application, both Python and Carbide UI designer can greatly benefit the initial design phase as means to rapidly develop and test UI features.

In summary, both Python and Symbian C++ have their places as development platforms at the moment. Native Symbian C++ with S60 is still clearly the best choice for serious software development, even if Python can provide developers with a much faster and easier approach than Symbian C++. One of the suggested uses for Python could include fast UI prototyping before actual programming tasks begin.

Open C: SMS Crypto Example Implementation Notes | 25

Version 1.1 | July 6, 2007

7 . R e f e r e n c e s

[1] Open C SDK Plug-In for S60 3rd Edition SDKs, for Symbian OS, for C++. Available at http://www.forum.nokia.com.

[2] Carbide.c++ Express v1.2. Available at http://www.forum.nokia.com.

[3] S60 3rd Edition SDK for Symbian OS, for C++, Maintenance Release. Available at http://www.forum.nokia.com.

Open C: SMS Crypto Example Implementation Notes | 26

Version 1.1 | July 6, 2007

8 . E v a l u a t e t h i s r e s o u r c e

Please spare a moment to help us improve documentation quality and recognize the resources you find most valuable, by rating this resource.